The patch below fixes the sparse warnings in ib_srpt.c about missing
[mirror/scst/.git] / srpt / src / ib_srpt.c
1 /*
2  * Copyright (c) 2006-2008 Mellanox Technology Inc.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  */
33
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/err.h>
38 #include <linux/string.h>
39 #include <linux/kthread.h>
40
41 #include <asm/atomic.h>
42
43 #include "ib_srpt.h"
44
45 #define DRV_NAME                "ib_srpt"
46 #define PFX                     DRV_NAME ": "
47 #define DRV_VERSION             "1.0"
48 #define DRV_RELDATE             "July 10, 2008"
49
50 #define MELLANOX_SRPT_ID_STRING "Mellanox OFED SRP target"
51
52 MODULE_AUTHOR("Vu Pham");
53 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
54                    "v" DRV_VERSION " (" DRV_RELDATE ")");
55 MODULE_LICENSE("Dual BSD/GPL");
56
57 struct srpt_thread {
58         spinlock_t thread_lock;
59         struct list_head thread_ioctx_list;
60         struct task_struct *thread;
61 };
62
63 static u64 mellanox_ioc_guid;
64 static struct list_head srpt_devices;
65 static int thread = 1;
66 static struct srpt_thread srpt_thread;
67 static DECLARE_WAIT_QUEUE_HEAD(ioctx_list_waitQ);
68
69 module_param(thread, int, 0444);
70 MODULE_PARM_DESC(thread,
71                  "Executing ioctx in thread context. Default thread = 1");
72
73 static void srpt_add_one(struct ib_device *device);
74 static void srpt_remove_one(struct ib_device *device);
75 static int srpt_disconnect_channel(struct srpt_rdma_ch *ch, int dreq);
76
77 static struct ib_client srpt_client = {
78         .name = DRV_NAME,
79         .add = srpt_add_one,
80         .remove = srpt_remove_one
81 };
82
83 static void srpt_event_handler(struct ib_event_handler *handler,
84                                struct ib_event *event)
85 {
86         struct srpt_device *sdev =
87             ib_get_client_data(event->device, &srpt_client);
88         struct srpt_port *sport;
89
90         if (!sdev || sdev->device != event->device)
91                 return;
92
93         printk(KERN_WARNING PFX "ASYNC event= %d on device= %s\n",
94                 event->event, sdev->device->name);
95
96         switch (event->event) {
97         case IB_EVENT_PORT_ERR:
98                 if (event->element.port_num <= sdev->device->phys_port_cnt) {
99                         sport = &sdev->port[event->element.port_num - 1];
100                         sport->lid = 0;
101                         sport->sm_lid = 0;
102                 }
103                 break;
104         case IB_EVENT_PORT_ACTIVE:
105         case IB_EVENT_LID_CHANGE:
106         case IB_EVENT_PKEY_CHANGE:
107         case IB_EVENT_SM_CHANGE:
108         case IB_EVENT_CLIENT_REREGISTER:
109                 if (event->element.port_num <= sdev->device->phys_port_cnt) {
110                         sport = &sdev->port[event->element.port_num - 1];
111                         if (!sport->lid && !sport->sm_lid)
112                                 schedule_work(&sport->work);
113                 }
114                 break;
115         default:
116                 break;
117         }
118
119 }
120
121 static void srpt_srq_event(struct ib_event *event, void *ctx)
122 {
123         printk(KERN_WARNING PFX "SRQ event %d\n", event->event);
124 }
125
126 static void srpt_qp_event(struct ib_event *event, void *ctx)
127 {
128         struct srpt_rdma_ch *ch = ctx;
129
130         printk(KERN_WARNING PFX
131                "QP event %d on cm_id=%p sess_name=%s state=%d\n",
132                event->event, ch->cm_id, ch->sess_name, ch->state);
133
134         switch (event->event) {
135         case IB_EVENT_COMM_EST:
136                 ib_cm_notify(ch->cm_id, event->event);
137                 break;
138         case IB_EVENT_QP_LAST_WQE_REACHED:
139                 if (ch->state == RDMA_CHANNEL_LIVE) {
140                         printk(KERN_WARNING PFX
141                                "Schedule CM_DISCONNECT_WORK\n");
142                         srpt_disconnect_channel(ch, 1);
143                 }
144                 break;
145         default:
146                 break;
147         }
148 }
149
150 static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
151 {
152         u16 id;
153         u8 tmp;
154
155         id = (slot - 1) / 2;
156         if (slot & 0x1) {
157                 tmp = c_list[id] & 0xf;
158                 c_list[id] = (value << 4) | tmp;
159         } else {
160                 tmp = c_list[id] & 0xf0;
161                 c_list[id] = (value & 0xf) | tmp;
162         }
163 }
164
165 static void srpt_get_class_port_info(struct ib_dm_mad *mad)
166 {
167         struct ib_class_port_info *cif;
168
169         cif = (struct ib_class_port_info *)mad->data;
170         memset(cif, 0, sizeof *cif);
171         cif->base_version = 1;
172         cif->class_version = 1;
173         cif->resp_time_value = 20;
174
175         mad->mad_hdr.status = 0;
176 }
177
178 static void srpt_get_iou(struct ib_dm_mad *mad)
179 {
180         struct ib_dm_iou_info *ioui;
181         u8 slot;
182         int i;
183
184         ioui = (struct ib_dm_iou_info *)mad->data;
185         ioui->change_id = 1;
186         ioui->max_controllers = 16;
187
188         /* set present for slot 1 and empty for the rest */
189         srpt_set_ioc(ioui->controller_list, 1, 1);
190         for (i = 1, slot = 2; i < 16; i++, slot++)
191                 srpt_set_ioc(ioui->controller_list, slot, 0);
192
193         mad->mad_hdr.status = 0;
194 }
195
196 static void srpt_get_ioc(struct srpt_device *sdev, u32 slot,
197                          struct ib_dm_mad *mad)
198 {
199         struct ib_dm_ioc_profile *iocp;
200
201         iocp = (struct ib_dm_ioc_profile *)mad->data;
202
203         if (!slot || slot > 16) {
204                 mad->mad_hdr.status = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
205                 return;
206         }
207
208         if (slot > 2) {
209                 mad->mad_hdr.status = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
210                 return;
211         }
212
213         memset(iocp, 0, sizeof *iocp);
214         strcpy(iocp->id_string, MELLANOX_SRPT_ID_STRING);
215         iocp->guid = cpu_to_be64(mellanox_ioc_guid);
216         iocp->vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
217         iocp->device_id = cpu_to_be32(sdev->dev_attr.vendor_part_id);
218         iocp->device_version = cpu_to_be16(sdev->dev_attr.hw_ver);
219         iocp->subsys_vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
220         iocp->subsys_device_id = 0x0;
221         iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
222         iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
223         iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
224         iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
225         iocp->send_queue_depth = cpu_to_be16(SRPT_SRQ_SIZE);
226         iocp->rdma_read_depth = 4;
227         iocp->send_size = cpu_to_be32(MAX_MESSAGE_SIZE);
228         iocp->rdma_size = cpu_to_be32(MAX_RDMA_SIZE);
229         iocp->num_svc_entries = 1;
230         iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
231             SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
232
233         mad->mad_hdr.status = 0;
234 }
235
236 static void srpt_get_svc_entries(u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
237 {
238         struct ib_dm_svc_entries *svc_entries;
239
240         if (!slot || slot > 16) {
241                 mad->mad_hdr.status = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
242                 return;
243         }
244
245         if (slot > 2 || lo > hi || hi > 1) {
246                 mad->mad_hdr.status = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
247                 return;
248         }
249
250         svc_entries = (struct ib_dm_svc_entries *)mad->data;
251         memset(svc_entries, 0, sizeof *svc_entries);
252         svc_entries->service_entries[0].id = cpu_to_be64(mellanox_ioc_guid);
253         sprintf(svc_entries->service_entries[0].name, "%s%016llx",
254                 SRP_SERVICE_NAME_PREFIX, (unsigned long long)mellanox_ioc_guid);
255
256         mad->mad_hdr.status = 0;
257 }
258
259 static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
260                                  struct ib_dm_mad *rsp_mad)
261 {
262         u16 attr_id;
263         u32 slot;
264         u8 hi, lo;
265
266         attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
267         switch (attr_id) {
268         case DM_ATTR_CLASS_PORT_INFO:
269                 srpt_get_class_port_info(rsp_mad);
270                 break;
271         case DM_ATTR_IOU_INFO:
272                 srpt_get_iou(rsp_mad);
273                 break;
274         case DM_ATTR_IOC_PROFILE:
275                 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
276                 srpt_get_ioc(sp->sdev, slot, rsp_mad);
277                 break;
278         case DM_ATTR_SVC_ENTRIES:
279                 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
280                 hi = (u8) ((slot >> 8) & 0xff);
281                 lo = (u8) (slot & 0xff);
282                 slot = (u16) ((slot >> 16) & 0xffff);
283                 srpt_get_svc_entries(slot, hi, lo, rsp_mad);
284                 break;
285         default:
286                 rsp_mad->mad_hdr.status =
287                     cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
288                 break;
289         }
290 }
291
292 static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
293                                   struct ib_mad_send_wc *mad_wc)
294 {
295         ib_destroy_ah(mad_wc->send_buf->ah);
296         ib_free_send_mad(mad_wc->send_buf);
297 }
298
299 static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
300                                   struct ib_mad_recv_wc *mad_wc)
301 {
302         struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
303         struct ib_ah *ah;
304         struct ib_mad_send_buf *rsp;
305         struct ib_dm_mad *dm_mad;
306
307         if (!mad_wc || !mad_wc->recv_buf.mad)
308                 return;
309
310         ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
311                                   mad_wc->recv_buf.grh, mad_agent->port_num);
312         if (IS_ERR(ah))
313                 goto err;
314
315         rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
316                                  mad_wc->wc->pkey_index, 0,
317                                  IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
318                                  GFP_KERNEL);
319         if (IS_ERR(rsp))
320                 goto err_rsp;
321
322         rsp->ah = ah;
323
324         dm_mad = rsp->mad;
325         memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof *dm_mad);
326         dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
327         dm_mad->mad_hdr.status = 0;
328
329         switch (mad_wc->recv_buf.mad->mad_hdr.method) {
330         case IB_MGMT_METHOD_GET:
331                 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
332                 break;
333         case IB_MGMT_METHOD_SET:
334                 dm_mad->mad_hdr.status =
335                     cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
336                 break;
337         default:
338                 dm_mad->mad_hdr.status =
339                     cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
340                 break;
341         }
342
343         if (!ib_post_send_mad(rsp, NULL)) {
344                 ib_free_recv_mad(mad_wc);
345                 /* will destroy_ah & free_send_mad in send completion */
346                 return;
347         }
348
349         ib_free_send_mad(rsp);
350
351 err_rsp:
352         ib_destroy_ah(ah);
353 err:
354         ib_free_recv_mad(mad_wc);
355 }
356
357 static int srpt_refresh_port(struct srpt_port *sport)
358 {
359         struct ib_mad_reg_req reg_req;
360         struct ib_port_modify port_modify;
361         struct ib_port_attr port_attr;
362         int ret;
363
364         memset(&port_modify, 0, sizeof port_modify);
365         port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
366         port_modify.clr_port_cap_mask = 0;
367
368         ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
369         if (ret)
370                 goto err_mod_port;
371
372         ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
373         if (ret)
374                 goto err_query_port;
375
376         sport->sm_lid = port_attr.sm_lid;
377         sport->lid = port_attr.lid;
378
379         ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid);
380         if (ret)
381                 goto err_query_port;
382
383         if (!sport->mad_agent) {
384                 memset(&reg_req, 0, sizeof reg_req);
385                 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
386                 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
387                 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
388                 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
389
390                 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
391                                                          sport->port,
392                                                          IB_QPT_GSI,
393                                                          &reg_req, 0,
394                                                          srpt_mad_send_handler,
395                                                          srpt_mad_recv_handler,
396                                                          sport);
397                 if (IS_ERR(sport->mad_agent))
398                         goto err_query_port;
399         }
400
401         return 0;
402
403 err_query_port:
404
405         port_modify.set_port_cap_mask = 0;
406         port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
407         ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
408
409 err_mod_port:
410
411         return ret;
412 }
413
414 static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev)
415 {
416         struct srpt_ioctx *ioctx;
417
418         ioctx = kmalloc(sizeof *ioctx, GFP_KERNEL);
419         if (!ioctx)
420                 goto out;
421
422         ioctx->buf = kzalloc(MAX_MESSAGE_SIZE, GFP_KERNEL);
423         if (!ioctx->buf)
424                 goto out_free_ioctx;
425
426         ioctx->dma = dma_map_single(sdev->device->dma_device, ioctx->buf,
427                                     MAX_MESSAGE_SIZE, DMA_BIDIRECTIONAL);
428         if (dma_mapping_error(ioctx->dma))
429                 goto out_free_buf;
430
431         return ioctx;
432
433 out_free_buf:
434         kfree(ioctx->buf);
435 out_free_ioctx:
436         kfree(ioctx);
437 out:
438         return NULL;
439 }
440
441 static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx)
442 {
443         if (!ioctx)
444                 return;
445
446         dma_unmap_single(sdev->device->dma_device, ioctx->dma,
447                          MAX_MESSAGE_SIZE, DMA_BIDIRECTIONAL);
448         kfree(ioctx->buf);
449         kfree(ioctx);
450 }
451
452 static int srpt_alloc_ioctx_ring(struct srpt_device *sdev)
453 {
454         int i;
455
456         for (i = 0; i < SRPT_SRQ_SIZE; ++i) {
457                 sdev->ioctx_ring[i] = srpt_alloc_ioctx(sdev);
458
459                 if (!sdev->ioctx_ring[i])
460                         goto err;
461
462                 sdev->ioctx_ring[i]->index = i;
463         }
464
465         return 0;
466
467 err:
468         while (--i > 0) {
469                 srpt_free_ioctx(sdev, sdev->ioctx_ring[i]);
470                 sdev->ioctx_ring[i] = NULL;
471         }
472         return -ENOMEM;
473 }
474
475 static int srpt_post_recv(struct srpt_device *sdev, struct srpt_ioctx *ioctx)
476 {
477         struct ib_sge list;
478         struct ib_recv_wr wr, *bad_wr;
479
480         wr.wr_id = ioctx->index | SRPT_OP_RECV;
481
482         list.addr = ioctx->dma;
483         list.length = MAX_MESSAGE_SIZE;
484         list.lkey = sdev->mr->lkey;
485
486         wr.next = NULL;
487         wr.sg_list = &list;
488         wr.num_sge = 1;
489
490         return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
491 }
492
493 static int srpt_post_send(struct srpt_rdma_ch *ch, struct srpt_ioctx *ioctx,
494                           int len)
495 {
496         struct ib_sge list;
497         struct ib_send_wr wr, *bad_wr;
498         struct srpt_device *sdev = ch->sport->sdev;
499
500         dma_sync_single_for_device(sdev->device->dma_device, ioctx->dma,
501                                    MAX_MESSAGE_SIZE, DMA_TO_DEVICE);
502
503         list.addr = ioctx->dma;
504         list.length = len;
505         list.lkey = sdev->mr->lkey;
506
507         wr.next = NULL;
508         wr.wr_id = ioctx->index;
509         wr.sg_list = &list;
510         wr.num_sge = 1;
511         wr.opcode = IB_WR_SEND;
512         wr.send_flags = IB_SEND_SIGNALED;
513
514         return ib_post_send(ch->qp, &wr, &bad_wr);
515 }
516
517 static int srpt_get_desc_tbl(struct srpt_ioctx *ioctx, struct srp_cmd *srp_cmd,
518                              int *ind)
519 {
520         struct srp_indirect_buf *idb;
521         struct srp_direct_buf *db;
522
523         *ind = 0;
524         if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
525             ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
526                 ioctx->n_rbuf = 1;
527                 ioctx->rbufs = &ioctx->single_rbuf;
528
529                 db = (void *)srp_cmd->add_data;
530                 memcpy(ioctx->rbufs, db, sizeof *db);
531                 ioctx->data_len = be32_to_cpu(db->len);
532         } else {
533                 idb = (void *)srp_cmd->add_data;
534
535                 ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof *db;
536
537                 if (ioctx->n_rbuf >
538                     (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
539                         *ind = 1;
540                         ioctx->n_rbuf = 0;
541                         goto out;
542                 }
543
544                 if (ioctx->n_rbuf == 1)
545                         ioctx->rbufs = &ioctx->single_rbuf;
546                 else
547                         ioctx->rbufs =
548                                 kmalloc(ioctx->n_rbuf * sizeof *db, GFP_ATOMIC);
549                 if (!ioctx->rbufs) {
550                         ioctx->n_rbuf = 0;
551                         return -ENOMEM;
552                 }
553
554                 db = idb->desc_list;
555                 memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof *db);
556                 ioctx->data_len = be32_to_cpu(idb->len);
557         }
558 out:
559         return 0;
560 }
561
562 static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
563 {
564         struct ib_qp_attr *attr;
565         int ret;
566
567         attr = kzalloc(sizeof *attr, GFP_KERNEL);
568         if (!attr)
569                 return -ENOMEM;
570
571         attr->qp_state = IB_QPS_INIT;
572         attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
573             IB_ACCESS_REMOTE_WRITE;
574         attr->port_num = ch->sport->port;
575         attr->pkey_index = 0;
576
577         ret = ib_modify_qp(qp, attr,
578                            IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
579                            IB_QP_PKEY_INDEX);
580
581         kfree(attr);
582         return ret;
583 }
584
585 static int srpt_ch_qp_rtr_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp,
586                               enum ib_qp_state qp_state)
587 {
588         struct ib_qp_attr *qp_attr;
589         int attr_mask;
590         int ret;
591
592         qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
593         if (!qp_attr)
594                 return -ENOMEM;
595
596         qp_attr->qp_state = qp_state;
597         ret = ib_cm_init_qp_attr(ch->cm_id, qp_attr, &attr_mask);
598         if (ret)
599                 goto out;
600
601         if (qp_state == IB_QPS_RTR)
602                 qp_attr->max_dest_rd_atomic = 4;
603         else
604                 qp_attr->max_rd_atomic = 4;
605
606         ret = ib_modify_qp(qp, qp_attr, attr_mask);
607
608 out:
609         kfree(qp_attr);
610         return ret;
611 }
612
613 static void srpt_reset_ioctx(struct srpt_rdma_ch *ch, struct srpt_ioctx *ioctx)
614 {
615         int i;
616
617         if (ioctx->n_rdma_ius > 0 && ioctx->rdma_ius) {
618                 struct rdma_iu *riu = ioctx->rdma_ius;
619
620                 for (i = 0; i < ioctx->n_rdma_ius; ++i, ++riu)
621                         kfree(riu->sge);
622                 kfree(ioctx->rdma_ius);
623         }
624
625         if (ioctx->n_rbuf > 1)
626                 kfree(ioctx->rbufs);
627
628         if (srpt_post_recv(ch->sport->sdev, ioctx))
629                 printk(KERN_ERR PFX "SRQ post_recv failed - this is serious\n");
630                 /* we should queue it back to free_ioctx queue */
631         else
632                 atomic_inc(&ch->req_lim_delta);
633 }
634
635 static void srpt_handle_err_comp(struct srpt_rdma_ch *ch, struct ib_wc *wc)
636 {
637         struct srpt_ioctx *ioctx;
638         struct srpt_device *sdev = ch->sport->sdev;
639         scst_data_direction dir = SCST_DATA_NONE;
640
641         if (wc->wr_id & SRPT_OP_RECV) {
642                 ioctx = sdev->ioctx_ring[wc->wr_id & ~SRPT_OP_RECV];
643                 printk(KERN_ERR PFX "This is serious - SRQ is in bad state\n");
644         } else {
645                 ioctx = sdev->ioctx_ring[wc->wr_id];
646
647                 if (ioctx->scmnd) {
648                         struct scst_cmd *scmnd = ioctx->scmnd;
649
650                         dir = scst_cmd_get_data_direction(scmnd);
651
652                         if (dir == SCST_DATA_NONE)
653                                 scst_tgt_cmd_done(scmnd);
654                         else {
655                                 dma_unmap_sg(sdev->device->dma_device,
656                                              scst_cmd_get_sg(scmnd),
657                                              scst_cmd_get_sg_cnt(scmnd),
658                                              scst_to_tgt_dma_dir(dir));
659
660                                 if (scmnd->data_buf_tgt_alloc &&
661                                     scmnd->data_buf_alloced) {
662                                         kfree(scmnd->sg);
663                                         scmnd->sg = NULL;
664                                         scmnd->sg_cnt = 0;
665                                 }
666
667                                 if (scmnd->state == SCST_CMD_STATE_RDY_TO_XFER)
668                                         scst_rx_data(scmnd,
669                                                      SCST_RX_STATUS_ERROR,
670                                                      SCST_CONTEXT_THREAD);
671                                 else if (scmnd->state ==
672                                          SCST_CMD_STATE_XMIT_WAIT)
673                                         scst_tgt_cmd_done(scmnd);
674                         }
675                 } else
676                         srpt_reset_ioctx(ch, ioctx);
677         }
678
679 }
680
681 static void srpt_handle_send_comp(struct srpt_rdma_ch *ch,
682                                   struct srpt_ioctx *ioctx)
683 {
684         if (ioctx->scmnd) {
685                 scst_data_direction dir =
686                         scst_cmd_get_data_direction(ioctx->scmnd);
687
688                 if (dir != SCST_DATA_NONE)
689                         dma_unmap_sg(ch->sport->sdev->device->dma_device,
690                                      scst_cmd_get_sg(ioctx->scmnd),
691                                      scst_cmd_get_sg_cnt(ioctx->scmnd),
692                                      scst_to_tgt_dma_dir(dir));
693
694                 if (ioctx->scmnd->data_buf_tgt_alloc &&
695                     ioctx->scmnd->data_buf_alloced) {
696                         kfree(ioctx->scmnd->sg);
697                         ioctx->scmnd->sg = NULL;
698                         ioctx->scmnd->sg_cnt = 0;
699                 }
700
701                 scst_tgt_cmd_done(ioctx->scmnd);
702         } else
703                 srpt_reset_ioctx(ch, ioctx);
704 }
705
706 static void srpt_handle_rdma_comp(struct srpt_rdma_ch *ch,
707                                   struct srpt_ioctx *ioctx)
708 {
709         if (!ioctx->scmnd) {
710                 srpt_reset_ioctx(ch, ioctx);
711                 return;
712         }
713
714         if (scst_cmd_get_data_direction(ioctx->scmnd) == SCST_DATA_WRITE)
715                 scst_rx_data(ioctx->scmnd, SCST_RX_STATUS_SUCCESS,
716                              SCST_CONTEXT_THREAD);
717 }
718
719 static void srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
720                                struct srpt_ioctx *ioctx, u8 s_key, u8 s_code,
721                                u64 tag)
722 {
723         struct srp_rsp *srp_rsp;
724         struct sense_data *sense;
725         int limit_delta;
726
727         srp_rsp = ioctx->buf;
728         memset(srp_rsp, 0, sizeof *srp_rsp);
729
730         limit_delta = atomic_read(&ch->req_lim_delta);
731         atomic_sub(limit_delta, &ch->req_lim_delta);
732
733         srp_rsp->opcode = SRP_RSP;
734         srp_rsp->req_lim_delta = cpu_to_be32(limit_delta);
735         srp_rsp->tag = tag;
736
737         if (s_key != NO_SENSE) {
738                 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
739                 srp_rsp->status = SAM_STAT_CHECK_CONDITION;
740                 srp_rsp->sense_data_len =
741                     cpu_to_be32(sizeof *sense + (sizeof *sense % 4));
742
743                 sense = (struct sense_data *)(srp_rsp + 1);
744                 sense->err_code = 0x70;
745                 sense->key = s_key;
746                 sense->asc_ascq = s_code;
747         }
748 }
749
750 static void srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
751                                    struct srpt_ioctx *ioctx, u8 rsp_code,
752                                    u64 tag)
753 {
754         struct srp_rsp *srp_rsp;
755         int limit_delta;
756
757         dma_sync_single_for_cpu(ch->sport->sdev->device->dma_device, ioctx->dma,
758                                 MAX_MESSAGE_SIZE, DMA_TO_DEVICE);
759
760         srp_rsp = ioctx->buf;
761         memset(srp_rsp, 0, sizeof *srp_rsp);
762
763         limit_delta = atomic_read(&ch->req_lim_delta);
764         atomic_sub(limit_delta, &ch->req_lim_delta);
765
766         srp_rsp->opcode = SRP_RSP;
767         srp_rsp->req_lim_delta = cpu_to_be32(limit_delta);
768         srp_rsp->tag = tag;
769
770         if (rsp_code != SRP_TSK_MGMT_SUCCESS) {
771                 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
772                 srp_rsp->resp_data_len = cpu_to_be32(4);
773                 srp_rsp->data[3] = rsp_code;
774         }
775 }
776
777 static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
778                                struct srpt_ioctx *ioctx)
779 {
780         struct scst_cmd *scmnd = NULL;
781         struct srp_cmd *srp_cmd = NULL;
782         struct srp_tsk_mgmt *srp_tsk = NULL;
783         struct srpt_mgmt_ioctx *mgmt_ioctx;
784         scst_data_direction dir = SCST_DATA_NONE;
785         int indirect_desc = 0;
786         u8 op;
787         int ret;
788
789         if (ch->state != RDMA_CHANNEL_LIVE) {
790                 if (ch->state == RDMA_CHANNEL_CONNECTING) {
791                         spin_lock_irq(&ch->spinlock);
792                         list_add_tail(&ioctx->wait_list, &ch->cmd_wait_list);
793                         spin_unlock_irq(&ch->spinlock);
794                 } else
795                         srpt_reset_ioctx(ch, ioctx);
796
797                 return;
798         }
799
800         dma_sync_single_for_cpu(ch->sport->sdev->device->dma_device, ioctx->dma,
801                                 MAX_MESSAGE_SIZE, DMA_FROM_DEVICE);
802
803         ioctx->data_len = 0;
804         ioctx->n_rbuf = 0;
805         ioctx->rbufs = NULL;
806         ioctx->n_rdma = 0;
807         ioctx->n_rdma_ius = 0;
808         ioctx->rdma_ius = NULL;
809         ioctx->scmnd = NULL;
810
811         op = *(u8 *) ioctx->buf;
812         switch (op) {
813         case SRP_CMD:
814                 srp_cmd = ioctx->buf;
815
816                 if (srp_cmd->buf_fmt) {
817                         ret = srpt_get_desc_tbl(ioctx, srp_cmd, &indirect_desc);
818                         if (ret) {
819                                 srpt_build_cmd_rsp(ch, ioctx, NO_SENSE,
820                                                    NO_ADD_SENSE, srp_cmd->tag);
821                                 ((struct srp_rsp *)ioctx->buf)->status =
822                                         SAM_STAT_TASK_SET_FULL;
823                                 goto send_rsp;
824                         }
825
826                         if (indirect_desc) {
827                                 srpt_build_cmd_rsp(ch, ioctx, NO_SENSE,
828                                                    NO_ADD_SENSE, srp_cmd->tag);
829                                 ((struct srp_rsp *)ioctx->buf)->status =
830                                         SAM_STAT_TASK_SET_FULL;
831                                 goto send_rsp;
832                         }
833
834                         if (srp_cmd->buf_fmt & 0xf)
835                                 dir = SCST_DATA_READ;
836                         else if (srp_cmd->buf_fmt >> 4)
837                                 dir = SCST_DATA_WRITE;
838                         else
839                                 dir = SCST_DATA_NONE;
840                 } else
841                         dir = SCST_DATA_NONE;
842
843                 scmnd = scst_rx_cmd(ch->scst_sess, (u8 *) &srp_cmd->lun,
844                                     sizeof srp_cmd->lun, srp_cmd->cdb, 16,
845                                     thread ? SCST_NON_ATOMIC : SCST_ATOMIC);
846                 if (!scmnd) {
847                         srpt_build_cmd_rsp(ch, ioctx, NO_SENSE,
848                                            NO_ADD_SENSE, srp_cmd->tag);
849                         ((struct srp_rsp *)ioctx->buf)->status =
850                                 SAM_STAT_TASK_SET_FULL;
851                         goto send_rsp;
852                 }
853
854                 ioctx->scmnd = scmnd;
855
856                 switch (srp_cmd->task_attr) {
857                 case SRP_CMD_HEAD_OF_Q:
858                         scmnd->queue_type = SCST_CMD_QUEUE_HEAD_OF_QUEUE;
859                         break;
860                 case SRP_CMD_ORDERED_Q:
861                         scmnd->queue_type = SCST_CMD_QUEUE_ORDERED;
862                         break;
863                 case SRP_CMD_SIMPLE_Q:
864                         scmnd->queue_type = SCST_CMD_QUEUE_SIMPLE;
865                         break;
866                 case SRP_CMD_ACA:
867                         scmnd->queue_type = SCST_CMD_QUEUE_ACA;
868                         break;
869                 default:
870                         scmnd->queue_type = SCST_CMD_QUEUE_ORDERED;
871                         break;
872                 }
873
874                 scst_cmd_set_tag(scmnd, srp_cmd->tag);
875                 scst_cmd_set_tgt_priv(scmnd, ioctx);
876                 scst_cmd_set_expected(scmnd, dir, ioctx->data_len);
877
878                 spin_lock_irq(&ch->spinlock);
879                 list_add_tail(&ioctx->scmnd_list, &ch->active_scmnd_list);
880                 ch->active_scmnd_cnt++;
881                 scst_cmd_init_done(scmnd, SCST_CONTEXT_THREAD);
882                 spin_unlock_irq(&ch->spinlock);
883
884                 break;
885
886         case SRP_TSK_MGMT:
887                 srp_tsk = ioctx->buf;
888
889                 printk(KERN_WARNING PFX
890                        "recv_tsk_mgmt= %d for task_tag= %lld"
891                        " using tag= %lld cm_id= %p sess= %p\n",
892                        srp_tsk->tsk_mgmt_func,
893                        (unsigned long long) srp_tsk->task_tag,
894                        (unsigned long long) srp_tsk->tag,
895                        ch->cm_id, ch->scst_sess);
896
897                 mgmt_ioctx = kmalloc(sizeof *mgmt_ioctx, GFP_ATOMIC);
898                 if (!mgmt_ioctx) {
899                         srpt_build_tskmgmt_rsp(ch, ioctx, SRP_TSK_MGMT_FAILED,
900                                                srp_tsk->tag);
901                         goto send_rsp;
902                 }
903
904                 mgmt_ioctx->ioctx = ioctx;
905                 mgmt_ioctx->ch = ch;
906                 mgmt_ioctx->tag = srp_tsk->tag;
907
908                 switch (srp_tsk->tsk_mgmt_func) {
909                 case SRP_TSK_ABORT_TASK:
910                         ret = scst_rx_mgmt_fn_tag(ch->scst_sess,
911                                                   SCST_ABORT_TASK,
912                                                   srp_tsk->task_tag,
913                                                   thread ?
914                                                   SCST_NON_ATOMIC : SCST_ATOMIC,
915                                                   mgmt_ioctx);
916                         break;
917                 case SRP_TSK_ABORT_TASK_SET:
918                         ret = scst_rx_mgmt_fn_lun(ch->scst_sess,
919                                                   SCST_ABORT_TASK_SET,
920                                                   (u8 *) &srp_tsk->lun,
921                                                   sizeof srp_tsk->lun,
922                                                   thread ?
923                                                   SCST_NON_ATOMIC : SCST_ATOMIC,
924                                                   mgmt_ioctx);
925                         break;
926                 case SRP_TSK_CLEAR_TASK_SET:
927                         ret = scst_rx_mgmt_fn_lun(ch->scst_sess,
928                                                   SCST_CLEAR_TASK_SET,
929                                                   (u8 *) &srp_tsk->lun,
930                                                   sizeof srp_tsk->lun,
931                                                   thread ?
932                                                   SCST_NON_ATOMIC : SCST_ATOMIC,
933                                                   mgmt_ioctx);
934                         break;
935 #if 0
936                 case SRP_TSK_LUN_RESET:
937                         ret = scst_rx_mgmt_fn_lun(ch->scst_sess,
938                                                   SCST_LUN_RESET,
939                                                   (u8 *) &srp_tsk->lun,
940                                                   sizeof srp_tsk->lun,
941                                                   thread ?
942                                                   SCST_NON_ATOMIC : SCST_ATOMIC,
943                                                   mgmt_ioctx);
944                         break;
945 #endif
946                 case SRP_TSK_CLEAR_ACA:
947                         ret = scst_rx_mgmt_fn_lun(ch->scst_sess,
948                                                   SCST_CLEAR_ACA,
949                                                   (u8 *) &srp_tsk->lun,
950                                                   sizeof srp_tsk->lun,
951                                                   thread ?
952                                                   SCST_NON_ATOMIC : SCST_ATOMIC,
953                                                   mgmt_ioctx);
954                         break;
955                 default:
956                         srpt_build_tskmgmt_rsp(ch, ioctx,
957                                                SRP_TSK_MGMT_FUNC_NOT_SUPP,
958                                                srp_tsk->tag);
959                         goto send_rsp;
960                 }
961
962                 break;
963         case SRP_I_LOGOUT:
964         case SRP_AER_REQ:
965         default:
966                 srpt_build_cmd_rsp(ch, ioctx, ILLEGAL_REQUEST, INVALID_CDB,
967                                    ((struct srp_cmd *)ioctx->buf)->tag);
968
969                 goto send_rsp;
970         }
971
972         dma_sync_single_for_device(ch->sport->sdev->device->dma_device,
973                                    ioctx->dma, MAX_MESSAGE_SIZE,
974                                    DMA_FROM_DEVICE);
975
976         return;
977
978 send_rsp:
979         if (ch->state != RDMA_CHANNEL_LIVE ||
980             srpt_post_send(ch, ioctx,
981                            sizeof(struct srp_rsp) +
982                            be32_to_cpu(((struct srp_rsp *)ioctx->buf)->
983                                        sense_data_len)))
984                 srpt_reset_ioctx(ch, ioctx);
985 }
986
987 static inline int srpt_test_ioctx_list(void)
988 {
989         int res = (!list_empty(&srpt_thread.thread_ioctx_list) ||
990                    unlikely(kthread_should_stop()));
991         return res;
992 }
993
994 static inline void srpt_schedule_thread(struct srpt_ioctx *ioctx)
995 {
996         unsigned long flags;
997
998         spin_lock_irqsave(&srpt_thread.thread_lock, flags);
999         list_add_tail(&ioctx->comp_list, &srpt_thread.thread_ioctx_list);
1000         spin_unlock_irqrestore(&srpt_thread.thread_lock, flags);
1001         wake_up(&ioctx_list_waitQ);
1002 }
1003
1004 static void srpt_completion(struct ib_cq *cq, void *ctx)
1005 {
1006         struct srpt_rdma_ch *ch = ctx;
1007         struct srpt_device *sdev = ch->sport->sdev;
1008         struct ib_wc wc;
1009         struct srpt_ioctx *ioctx;
1010
1011         ib_req_notify_cq(ch->cq, IB_CQ_NEXT_COMP);
1012         while (ib_poll_cq(ch->cq, 1, &wc) > 0) {
1013                 if (wc.status) {
1014                         printk(KERN_ERR PFX "failed %s status= %d\n",
1015                                wc.wr_id & SRPT_OP_RECV ? "receive" : "send",
1016                                wc.status);
1017                         srpt_handle_err_comp(ch, &wc);
1018                         break;
1019                 }
1020
1021                 if (wc.wr_id & SRPT_OP_RECV) {
1022                         ioctx = sdev->ioctx_ring[wc.wr_id & ~SRPT_OP_RECV];
1023                         if (thread) {
1024                                 ioctx->ch = ch;
1025                                 ioctx->op = IB_WC_RECV;
1026                                 srpt_schedule_thread(ioctx);
1027                         } else
1028                                 srpt_handle_new_iu(ch, ioctx);
1029                         continue;
1030                 } else
1031                         ioctx = sdev->ioctx_ring[wc.wr_id];
1032
1033                 if (thread) {
1034                         ioctx->ch = ch;
1035                         ioctx->op = wc.opcode;
1036                         srpt_schedule_thread(ioctx);
1037                 } else {
1038                         switch (wc.opcode) {
1039                         case IB_WC_SEND:
1040                                 srpt_handle_send_comp(ch, ioctx);
1041                                 break;
1042                         case IB_WC_RDMA_WRITE:
1043                         case IB_WC_RDMA_READ:
1044                                 srpt_handle_rdma_comp(ch, ioctx);
1045                                 break;
1046                         default:
1047                                 break;
1048                         }
1049                 }
1050         }
1051 }
1052
1053 static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1054 {
1055         struct ib_qp_init_attr *qp_init;
1056         struct srpt_device *sdev = ch->sport->sdev;
1057         int cqe;
1058         int ret;
1059
1060         qp_init = kzalloc(sizeof *qp_init, GFP_KERNEL);
1061         if (!qp_init)
1062                 return -ENOMEM;
1063
1064         cqe = SRPT_RQ_SIZE + SRPT_SQ_SIZE - 1;
1065 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
1066         ch->cq = ib_create_cq(sdev->device, srpt_completion, NULL, ch, cqe);
1067 #else
1068         ch->cq = ib_create_cq(sdev->device, srpt_completion, NULL, ch, cqe, 0);
1069 #endif
1070         if (IS_ERR(ch->cq)) {
1071                 ret = PTR_ERR(ch->cq);
1072                 printk(KERN_ERR PFX "failed to create_cq cqe= %d ret= %d\n",
1073                         cqe, ret);
1074                 goto out;
1075         }
1076
1077         ib_req_notify_cq(ch->cq, IB_CQ_NEXT_COMP);
1078
1079         qp_init->qp_context = (void *)ch;
1080         qp_init->event_handler = srpt_qp_event;
1081         qp_init->send_cq = ch->cq;
1082         qp_init->recv_cq = ch->cq;
1083         qp_init->srq = sdev->srq;
1084         qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1085         qp_init->qp_type = IB_QPT_RC;
1086         qp_init->cap.max_send_wr = SRPT_SQ_SIZE;
1087         qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
1088
1089         ch->qp = ib_create_qp(sdev->pd, qp_init);
1090         if (IS_ERR(ch->qp)) {
1091                 ret = PTR_ERR(ch->qp);
1092                 ib_destroy_cq(ch->cq);
1093                 printk(KERN_ERR PFX "failed to create_qp ret= %d\n", ret);
1094                 goto out;
1095         }
1096
1097         printk(KERN_DEBUG PFX "%s: max_cqe= %d max_sge= %d cm_id= %p\n",
1098                __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1099                ch->cm_id);
1100
1101         ret = srpt_init_ch_qp(ch, ch->qp);
1102         if (ret) {
1103                 ib_destroy_qp(ch->qp);
1104                 ib_destroy_cq(ch->cq);
1105                 goto out;
1106         }
1107
1108         atomic_set(&ch->req_lim_delta, SRPT_RQ_SIZE);
1109 out:
1110         kfree(qp_init);
1111         return ret;
1112 }
1113
1114 static struct srpt_rdma_ch *srpt_find_channel(struct ib_cm_id *cm_id)
1115 {
1116         struct srpt_device *sdev = cm_id->context;
1117         struct srpt_rdma_ch *ch, *tmp_ch;
1118
1119         spin_lock_irq(&sdev->spinlock);
1120         list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
1121                 if (ch->cm_id == cm_id) {
1122                         spin_unlock_irq(&sdev->spinlock);
1123                         return ch;
1124                 }
1125         }
1126
1127         spin_unlock_irq(&sdev->spinlock);
1128
1129         return NULL;
1130 }
1131
1132 static int srpt_release_channel(struct srpt_rdma_ch *ch, int destroy_cmid)
1133 {
1134         spin_lock_irq(&ch->sport->sdev->spinlock);
1135         list_del(&ch->list);
1136         spin_unlock_irq(&ch->sport->sdev->spinlock);
1137
1138         if (ch->cm_id && destroy_cmid) {
1139                 printk(KERN_WARNING PFX
1140                        "%s: destroy cm_id= %p\n", __func__, ch->cm_id);
1141                 ib_destroy_cm_id(ch->cm_id);
1142                 ch->cm_id = NULL;
1143         }
1144
1145         ib_destroy_qp(ch->qp);
1146         ib_destroy_cq(ch->cq);
1147
1148         if (ch->scst_sess) {
1149                 struct srpt_ioctx *ioctx, *ioctx_tmp;
1150
1151                 printk(KERN_WARNING PFX
1152                        "%s: release sess= %p sess_name= %s active_cmd= %d\n",
1153                        __func__, ch->scst_sess, ch->sess_name,
1154                        ch->active_scmnd_cnt);
1155
1156                 list_for_each_entry_safe(ioctx, ioctx_tmp,
1157                                          &ch->active_scmnd_list, scmnd_list) {
1158                         list_del(&ioctx->scmnd_list);
1159                         ch->active_scmnd_cnt--;
1160                 }
1161
1162                 scst_unregister_session(ch->scst_sess, 0, NULL);
1163                 ch->scst_sess = NULL;
1164         }
1165
1166         kfree(ch);
1167
1168         return destroy_cmid ? 0 : 1;
1169 }
1170
1171 static void srpt_register_channel_done(struct scst_session *scst_sess,
1172                                        void *data, int status)
1173 {
1174         struct srpt_rdma_ch *ch = data;
1175
1176         BUG_ON(!ch);
1177
1178         if (status) {
1179                 if (ch->scst_sess) {
1180                         scst_unregister_session(ch->scst_sess, 0, NULL);
1181                         ch->scst_sess = NULL;
1182                 }
1183                 printk(KERN_ERR PFX
1184                        "%s: Failed to establish sess= %p status= %d\n",
1185                        __func__, scst_sess, status);
1186         }
1187
1188         complete(&ch->scst_sess_done);
1189 }
1190
1191 static int srpt_disconnect_channel(struct srpt_rdma_ch *ch, int dreq)
1192 {
1193         spin_lock_irq(&ch->spinlock);
1194         ch->state = RDMA_CHANNEL_DISCONNECTING;
1195         spin_unlock_irq(&ch->spinlock);
1196
1197         if (dreq)
1198                 ib_send_cm_dreq(ch->cm_id, NULL, 0);
1199         else
1200                 ib_send_cm_drep(ch->cm_id, NULL, 0);
1201
1202         return 0;
1203 }
1204
1205 static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
1206                             struct ib_cm_req_event_param *param,
1207                             void *private_data)
1208 {
1209         struct srpt_device *sdev = cm_id->context;
1210         struct srp_login_req *req;
1211         struct srp_login_rsp *rsp;
1212         struct srp_login_rej *rej;
1213         struct ib_cm_rep_param *rep_param;
1214         struct srpt_rdma_ch *ch, *tmp_ch;
1215         u32 it_iu_len;
1216         int ret = 0;
1217
1218         if (!sdev || !private_data)
1219                 return -EINVAL;
1220
1221         rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
1222         rej = kzalloc(sizeof *rej, GFP_KERNEL);
1223         rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
1224
1225         if (!rsp || !rej || !rep_param) {
1226                 ret = -ENOMEM;
1227                 goto out;
1228         }
1229
1230         req = (struct srp_login_req *)private_data;
1231
1232         it_iu_len = be32_to_cpu(req->req_it_iu_len);
1233
1234         printk(KERN_DEBUG PFX
1235                "Host login i_port_id=0x%llx:0x%llx t_port_id=0x%llx:0x%llx"
1236                " it_iu_len=%d\n",
1237                (unsigned long long)
1238                be64_to_cpu(*(u64 *)&req->initiator_port_id[0]),
1239                (unsigned long long)
1240                be64_to_cpu(*(u64 *)&req->initiator_port_id[8]),
1241                (unsigned long long)be64_to_cpu(*(u64 *)&req->target_port_id[0]),
1242                (unsigned long long)be64_to_cpu(*(u64 *)&req->target_port_id[8]),
1243                it_iu_len);
1244
1245         if (it_iu_len > MAX_MESSAGE_SIZE || it_iu_len < 64) {
1246                 rej->reason =
1247                     cpu_to_be32(SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
1248                 ret = -EINVAL;
1249                 printk(KERN_WARNING PFX
1250                        "Reject invalid it_iu_len=%d\n", it_iu_len);
1251                 goto reject;
1252         }
1253
1254         if ((req->req_flags & 0x3) == SRP_MULTICHAN_SINGLE) {
1255                 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
1256
1257                 spin_lock_irq(&sdev->spinlock);
1258
1259                 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
1260                         if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
1261                             && !memcmp(ch->t_port_id, req->target_port_id, 16)
1262                             && param->port == ch->sport->port
1263                             && param->listen_id == ch->sport->sdev->cm_id
1264                             && ch->cm_id) {
1265                                 /* found an existing channel */
1266                                 printk(KERN_WARNING PFX
1267                                        "Found existing channel name= %s"
1268                                        " cm_id= %p state= %d\n",
1269                                        ch->sess_name, ch->cm_id, ch->state);
1270
1271                                 spin_unlock_irq(&sdev->spinlock);
1272
1273                                 rsp->rsp_flags =
1274                                     SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
1275
1276                                 if (ch->state == RDMA_CHANNEL_LIVE)
1277                                         srpt_disconnect_channel(ch, 1);
1278                                 else if (ch->state == RDMA_CHANNEL_CONNECTING) {
1279                                         ib_send_cm_rej(ch->cm_id,
1280                                                        IB_CM_REJ_NO_RESOURCES,
1281                                                        NULL, 0, NULL, 0);
1282                                         srpt_release_channel(ch, 1);
1283                                 }
1284
1285                                 spin_lock_irq(&sdev->spinlock);
1286                         }
1287                 }
1288
1289                 spin_unlock_irq(&sdev->spinlock);
1290
1291         } else
1292                 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
1293
1294         if (((u64) (*(u64 *) req->target_port_id) !=
1295              cpu_to_be64(mellanox_ioc_guid)) ||
1296             ((u64) (*(u64 *) (req->target_port_id + 8)) !=
1297              cpu_to_be64(mellanox_ioc_guid))) {
1298                 rej->reason =
1299                     cpu_to_be32(SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
1300                 ret = -ENOMEM;
1301                 printk(KERN_WARNING PFX "Reject invalid target_port_id\n");
1302                 goto reject;
1303         }
1304
1305         ch = kzalloc(sizeof *ch, GFP_KERNEL);
1306         if (!ch) {
1307                 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
1308                 printk(KERN_WARNING PFX "Reject failed allocate rdma_ch\n");
1309                 ret = -ENOMEM;
1310                 goto reject;
1311         }
1312
1313         spin_lock_init(&ch->spinlock);
1314         memcpy(ch->i_port_id, req->initiator_port_id, 16);
1315         memcpy(ch->t_port_id, req->target_port_id, 16);
1316         ch->sport = &sdev->port[param->port - 1];
1317         ch->cm_id = cm_id;
1318         ch->state = RDMA_CHANNEL_CONNECTING;
1319         INIT_LIST_HEAD(&ch->cmd_wait_list);
1320         INIT_LIST_HEAD(&ch->active_scmnd_list);
1321
1322         ret = srpt_create_ch_ib(ch);
1323         if (ret) {
1324                 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
1325                 printk(KERN_WARNING PFX "Reject failed to create rdma_ch\n");
1326                 goto free_ch;
1327         }
1328
1329         ret = srpt_ch_qp_rtr_rts(ch, ch->qp, IB_QPS_RTR);
1330         if (ret) {
1331                 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
1332                 printk(KERN_WARNING PFX
1333                        "Reject failed qp to rtr/rts ret=%d\n", ret);
1334                 goto destroy_ib;
1335         }
1336
1337         init_completion(&ch->scst_sess_done);
1338         sprintf(ch->sess_name, "0x%016llx%016llx",
1339                 (unsigned long long)be64_to_cpu(*(u64 *)ch->i_port_id),
1340                 (unsigned long long)be64_to_cpu(*(u64 *)(ch->i_port_id + 8)));
1341         ch->scst_sess =
1342             scst_register_session(sdev->scst_tgt, 1, ch->sess_name,
1343                                   ch, srpt_register_channel_done);
1344
1345         wait_for_completion(&ch->scst_sess_done);
1346
1347         if (!ch->scst_sess) {
1348                 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
1349                 printk(KERN_WARNING PFX "Reject failed to create scst sess");
1350                 goto destroy_ib;
1351         }
1352
1353         spin_lock_irq(&sdev->spinlock);
1354         list_add_tail(&ch->list, &sdev->rch_list);
1355         spin_unlock_irq(&sdev->spinlock);
1356
1357         printk(KERN_DEBUG PFX "Establish connection sess=%p name=%s cm_id=%p\n",
1358                ch->scst_sess, ch->sess_name, ch->cm_id);
1359
1360         scst_sess_set_tgt_priv(ch->scst_sess, ch);
1361
1362         /* create srp_login_response */
1363         rsp->opcode = SRP_LOGIN_RSP;
1364         rsp->tag = req->tag;
1365         rsp->max_it_iu_len = req->req_it_iu_len;
1366         rsp->max_ti_iu_len = req->req_it_iu_len;
1367         rsp->buf_fmt =
1368             cpu_to_be16(SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT);
1369         rsp->req_lim_delta = cpu_to_be32(SRPT_RQ_SIZE);
1370         atomic_set(&ch->req_lim_delta, 0);
1371
1372         /* create cm reply */
1373         rep_param->qp_num = ch->qp->qp_num;
1374         rep_param->private_data = (void *)rsp;
1375         rep_param->private_data_len = sizeof *rsp;
1376         rep_param->rnr_retry_count = 7;
1377         rep_param->flow_control = 1;
1378         rep_param->failover_accepted = 0;
1379         rep_param->srq = 1;
1380         rep_param->responder_resources = 4;
1381         rep_param->initiator_depth = 4;
1382
1383         ret = ib_send_cm_rep(cm_id, rep_param);
1384         if (ret)
1385                 srpt_release_channel(ch, 0);
1386
1387         goto out;
1388
1389 destroy_ib:
1390         ib_destroy_qp(ch->qp);
1391         ib_destroy_cq(ch->cq);
1392
1393 free_ch:
1394         kfree(ch);
1395
1396 reject:
1397         rej->opcode = SRP_LOGIN_REJ;
1398         rej->tag = req->tag;
1399         rej->buf_fmt =
1400             cpu_to_be16(SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT);
1401
1402         ret = ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
1403                              (void *)rej, sizeof *rej);
1404
1405 out:
1406         kfree(rep_param);
1407         kfree(rsp);
1408         kfree(rej);
1409
1410         return ret;
1411 }
1412
1413 static int srpt_find_and_release_channel(struct ib_cm_id *cm_id)
1414 {
1415         struct srpt_rdma_ch *ch;
1416
1417         ch = srpt_find_channel(cm_id);
1418         if (!ch)
1419                 return -EINVAL;
1420
1421         return srpt_release_channel(ch, 0);
1422 }
1423
1424 static int srpt_cm_rej_recv(struct ib_cm_id *cm_id)
1425 {
1426         printk(KERN_DEBUG PFX "%s: cm_id=%p\n", __func__, cm_id);
1427         return srpt_find_and_release_channel(cm_id);
1428 }
1429
1430 static int srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
1431 {
1432         struct srpt_rdma_ch *ch;
1433         int ret;
1434
1435         ch = srpt_find_channel(cm_id);
1436         if (!ch)
1437                 return -EINVAL;
1438
1439         if (ch->state == RDMA_CHANNEL_CONNECTING) {
1440                 struct srpt_ioctx *ioctx, *ioctx_tmp;
1441
1442                 spin_lock_irq(&ch->spinlock);
1443                 ch->state = RDMA_CHANNEL_LIVE;
1444                 spin_unlock_irq(&ch->spinlock);
1445                 ret = srpt_ch_qp_rtr_rts(ch, ch->qp, IB_QPS_RTS);
1446
1447                 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
1448                                          wait_list) {
1449                         list_del(&ioctx->wait_list);
1450                         srpt_handle_new_iu(ch, ioctx);
1451                 }
1452         } else if (ch->state == RDMA_CHANNEL_DISCONNECTING)
1453                 ret = -EAGAIN;
1454         else
1455                 ret = 0;
1456
1457         if (ret) {
1458                 printk(KERN_ERR PFX "cm_id=%p sess_name=%s state=%d\n",
1459                        cm_id, ch->sess_name, ch->state);
1460                 srpt_disconnect_channel(ch, 1);
1461         }
1462
1463         return ret;
1464 }
1465
1466 static int srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
1467 {
1468         printk(KERN_DEBUG PFX "%s: cm_id=%p\n", __func__, cm_id);
1469         return srpt_find_and_release_channel(cm_id);
1470 }
1471
1472 static int srpt_cm_rep_error(struct ib_cm_id *cm_id)
1473 {
1474         printk(KERN_DEBUG PFX "%s: cm_id=%p\n", __func__, cm_id);
1475         return srpt_find_and_release_channel(cm_id);
1476 }
1477
1478 static int srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
1479 {
1480         struct srpt_rdma_ch *ch;
1481         int ret = 0;
1482
1483         ch = srpt_find_channel(cm_id);
1484
1485         if (!ch)
1486                 return -EINVAL;
1487
1488         printk(KERN_DEBUG PFX "%s: cm_id= %p ch->state= %d\n",
1489                  __func__, cm_id, ch->state);
1490
1491         switch (ch->state) {
1492         case RDMA_CHANNEL_LIVE:
1493         case RDMA_CHANNEL_CONNECTING:
1494                 ret = srpt_disconnect_channel(ch, 0);
1495                 break;
1496         case RDMA_CHANNEL_DISCONNECTING:
1497         default:
1498                 break;
1499         }
1500
1501         return ret;
1502 }
1503
1504 static int srpt_cm_drep_recv(struct ib_cm_id *cm_id)
1505 {
1506         printk(KERN_DEBUG PFX "%s: cm_id=%p\n", __func__, cm_id);
1507         return srpt_find_and_release_channel(cm_id);
1508 }
1509
1510 static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1511 {
1512         int ret = 0;
1513
1514         switch (event->event) {
1515         case IB_CM_REQ_RECEIVED:
1516                 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
1517                                        event->private_data);
1518                 break;
1519         case IB_CM_REJ_RECEIVED:
1520                 ret = srpt_cm_rej_recv(cm_id);
1521                 break;
1522         case IB_CM_RTU_RECEIVED:
1523         case IB_CM_USER_ESTABLISHED:
1524                 ret = srpt_cm_rtu_recv(cm_id);
1525                 break;
1526         case IB_CM_DREQ_RECEIVED:
1527                 ret = srpt_cm_dreq_recv(cm_id);
1528                 break;
1529         case IB_CM_DREP_RECEIVED:
1530                 ret = srpt_cm_drep_recv(cm_id);
1531                 break;
1532         case IB_CM_TIMEWAIT_EXIT:
1533                 ret = srpt_cm_timewait_exit(cm_id);
1534                 break;
1535         case IB_CM_REP_ERROR:
1536                 ret = srpt_cm_rep_error(cm_id);
1537                 break;
1538         default:
1539                 break;
1540         }
1541
1542         return ret;
1543 }
1544
1545 static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1546                                  struct srpt_ioctx *ioctx,
1547                                  struct scst_cmd *scmnd)
1548 {
1549         struct scatterlist *scat;
1550         scst_data_direction dir;
1551         struct rdma_iu *riu;
1552         struct srp_direct_buf *db;
1553         dma_addr_t dma_addr;
1554         struct ib_sge *sge;
1555         u64 raddr;
1556         u32 rsize;
1557         u32 tsize;
1558         u32 dma_len;
1559         int count, nrdma;
1560         int i, j, k;
1561
1562         scat = scst_cmd_get_sg(scmnd);
1563         dir = scst_cmd_get_data_direction(scmnd);
1564         count = dma_map_sg(ch->sport->sdev->device->dma_device, scat,
1565                            scst_cmd_get_sg_cnt(scmnd),
1566                            scst_to_tgt_dma_dir(dir));
1567         if (unlikely(!count))
1568                 return -EBUSY;
1569
1570         if (ioctx->rdma_ius && ioctx->n_rdma_ius)
1571                 nrdma = ioctx->n_rdma_ius;
1572         else {
1573                 nrdma = count / SRPT_DEF_SG_PER_WQE + ioctx->n_rbuf;
1574
1575                 ioctx->rdma_ius = kzalloc(nrdma * sizeof *riu,
1576                                           scst_cmd_atomic(scmnd)
1577                                           ? GFP_ATOMIC : GFP_KERNEL);
1578                 if (!ioctx->rdma_ius) {
1579                         dma_unmap_sg(ch->sport->sdev->device->dma_device,
1580                                      scat, scst_cmd_get_sg_cnt(scmnd),
1581                                      scst_to_tgt_dma_dir(dir));
1582                         return -ENOMEM;
1583                 }
1584
1585                 ioctx->n_rdma_ius = nrdma;
1586         }
1587
1588         db = ioctx->rbufs;
1589         tsize = (dir == SCST_DATA_READ) ?
1590                 scst_cmd_get_resp_data_len(scmnd) : scst_cmd_get_bufflen(scmnd);
1591         dma_len = sg_dma_len(&scat[0]);
1592         riu = ioctx->rdma_ius;
1593
1594         /*
1595          * For each remote desc - calculate the #ib_sge.
1596          * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1597          *      each remote desc rdma_iu is required a rdma wr;
1598          * else
1599          *      we need to allocate extra rdma_iu to carry extra #ib_sge in
1600          *      another rdma wr
1601          */
1602         for (i = 0, j = 0;
1603              j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1604                 rsize = be32_to_cpu(db->len);
1605                 raddr = be64_to_cpu(db->va);
1606                 riu->raddr = raddr;
1607                 riu->rkey = be32_to_cpu(db->key);
1608                 riu->sge_cnt = 0;
1609
1610                 /* calculate how many sge required for this remote_buf */
1611                 while (rsize > 0 && tsize > 0) {
1612
1613                         if (rsize >= dma_len) {
1614                                 tsize -= dma_len;
1615                                 rsize -= dma_len;
1616                                 raddr += dma_len;
1617
1618                                 if (tsize > 0) {
1619                                         ++j;
1620                                         if (j < count)
1621                                                 dma_len = sg_dma_len(&scat[j]);
1622                                 }
1623                         } else {
1624                                 tsize -= rsize;
1625                                 dma_len -= rsize;
1626                                 rsize = 0;
1627                         }
1628
1629                         ++riu->sge_cnt;
1630
1631                         if (rsize > 0 && riu->sge_cnt == SRPT_DEF_SG_PER_WQE) {
1632                                 riu->sge =
1633                                     kmalloc(riu->sge_cnt * sizeof *riu->sge,
1634                                             scst_cmd_atomic(scmnd)
1635                                             ? GFP_ATOMIC : GFP_KERNEL);
1636                                 if (!riu->sge)
1637                                         goto free_mem;
1638
1639                                 ++ioctx->n_rdma;
1640                                 ++riu;
1641                                 riu->sge_cnt = 0;
1642                                 riu->raddr = raddr;
1643                                 riu->rkey = be32_to_cpu(db->key);
1644                         }
1645                 }
1646
1647                 riu->sge = kmalloc(riu->sge_cnt * sizeof *riu->sge,
1648                                    scst_cmd_atomic(scmnd)
1649                                    ? GFP_ATOMIC : GFP_KERNEL);
1650
1651                 if (!riu->sge)
1652                         goto free_mem;
1653
1654                 ++ioctx->n_rdma;
1655         }
1656
1657         db = ioctx->rbufs;
1658         scat = scst_cmd_get_sg(scmnd);
1659         tsize = (dir == SCST_DATA_READ) ?
1660                 scst_cmd_get_resp_data_len(scmnd) : scst_cmd_get_bufflen(scmnd);
1661         riu = ioctx->rdma_ius;
1662         dma_len = sg_dma_len(&scat[0]);
1663         dma_addr = sg_dma_address(&scat[0]);
1664
1665         /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1666         for (i = 0, j = 0;
1667              j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1668                 rsize = be32_to_cpu(db->len);
1669                 sge = riu->sge;
1670                 k = 0;
1671
1672                 while (rsize > 0 && tsize > 0) {
1673                         sge->addr = dma_addr;
1674                         sge->lkey = ch->sport->sdev->mr->lkey;
1675
1676                         if (rsize >= dma_len) {
1677                                 sge->length =
1678                                         (tsize < dma_len) ? tsize : dma_len;
1679                                 tsize -= dma_len;
1680                                 rsize -= dma_len;
1681
1682                                 if (tsize > 0) {
1683                                         ++j;
1684                                         if (j < count) {
1685                                                 dma_len = sg_dma_len(&scat[j]);
1686                                                 dma_addr =
1687                                                     sg_dma_address(&scat[j]);
1688                                         }
1689                                 }
1690                         } else {
1691                                 sge->length = (tsize < rsize) ? tsize : rsize;
1692                                 tsize -= rsize;
1693                                 dma_len -= rsize;
1694                                 dma_addr += rsize;
1695                                 rsize = 0;
1696                         }
1697
1698                         ++k;
1699                         if (k == riu->sge_cnt && rsize > 0) {
1700                                 ++riu;
1701                                 sge = riu->sge;
1702                                 k = 0;
1703                         } else if (rsize > 0)
1704                                 ++sge;
1705                 }
1706         }
1707
1708         return 0;
1709
1710 free_mem:
1711         while (ioctx->n_rdma)
1712                 kfree(ioctx->rdma_ius[ioctx->n_rdma--].sge);
1713
1714         kfree(ioctx->rdma_ius);
1715
1716         dma_unmap_sg(ch->sport->sdev->device->dma_device,
1717                      scat, scst_cmd_get_sg_cnt(scmnd),
1718                      scst_to_tgt_dma_dir(dir));
1719
1720         return -ENOMEM;
1721 }
1722
1723 static int srpt_perform_rdmas(struct srpt_rdma_ch *ch, struct srpt_ioctx *ioctx,
1724                               scst_data_direction dir)
1725 {
1726         struct ib_send_wr wr;
1727         struct ib_send_wr *bad_wr;
1728         struct rdma_iu *riu;
1729         int i;
1730         int ret = 0;
1731
1732         riu = ioctx->rdma_ius;
1733         memset(&wr, 0, sizeof wr);
1734
1735         for (i = 0; i < ioctx->n_rdma; ++i, ++riu) {
1736                 wr.opcode = (dir == SCST_DATA_READ) ?
1737                     IB_WR_RDMA_WRITE : IB_WR_RDMA_READ;
1738                 wr.next = NULL;
1739                 wr.wr_id = ioctx->index;
1740                 wr.wr.rdma.remote_addr = riu->raddr;
1741                 wr.wr.rdma.rkey = riu->rkey;
1742                 wr.num_sge = riu->sge_cnt;
1743                 wr.sg_list = riu->sge;
1744
1745                 /* only get completion event for the last rdma wr */
1746                 if (i == (ioctx->n_rdma - 1) && dir == SCST_DATA_WRITE)
1747                         wr.send_flags = IB_SEND_SIGNALED;
1748
1749                 ret = ib_post_send(ch->qp, &wr, &bad_wr);
1750                 if (ret)
1751                         break;
1752         }
1753
1754         return ret;
1755 }
1756
1757 static int srpt_xfer_data(struct srpt_rdma_ch *ch, struct srpt_ioctx *ioctx,
1758                           struct scst_cmd *scmnd)
1759 {
1760         int ret;
1761
1762         ret = srpt_map_sg_to_ib_sge(ch, ioctx, scmnd);
1763         if (ret) {
1764                 printk(KERN_ERR PFX "%s[%d] ret=%d\n", __func__, __LINE__, ret);
1765                 ret = SCST_TGT_RES_QUEUE_FULL;
1766                 goto out;
1767         }
1768
1769         ret = srpt_perform_rdmas(ch, ioctx, scst_cmd_get_data_direction(scmnd));
1770         if (ret) {
1771                 printk(KERN_ERR PFX "%s[%d] ret=%d\n", __func__, __LINE__, ret);
1772                 if (ret == -EAGAIN || ret == -ENOMEM)
1773                         ret = SCST_TGT_RES_QUEUE_FULL;
1774                 else
1775                         ret = SCST_TGT_RES_FATAL_ERROR;
1776                 goto out;
1777         }
1778
1779         ret = SCST_TGT_RES_SUCCESS;
1780
1781 out:
1782         return ret;
1783 }
1784
1785 static int srpt_rdy_to_xfer(struct scst_cmd *scmnd)
1786 {
1787         struct srpt_rdma_ch *ch;
1788         struct srpt_ioctx *ioctx;
1789
1790         ioctx = scst_cmd_get_tgt_priv(scmnd);
1791         BUG_ON(!ioctx);
1792
1793         ch = scst_sess_get_tgt_priv(scst_cmd_get_session(scmnd));
1794         BUG_ON(!ch);
1795
1796         if (ch->state == RDMA_CHANNEL_DISCONNECTING)
1797                 return SCST_TGT_RES_FATAL_ERROR;
1798         else if (ch->state == RDMA_CHANNEL_CONNECTING)
1799                 return SCST_TGT_RES_QUEUE_FULL;
1800
1801         return srpt_xfer_data(ch, ioctx, scmnd);
1802 }
1803
1804 static int srpt_xmit_response(struct scst_cmd *scmnd)
1805 {
1806         struct srpt_rdma_ch *ch;
1807         struct srpt_ioctx *ioctx;
1808         struct srp_rsp *srp_rsp;
1809         u64 tag;
1810         int ret = SCST_TGT_RES_SUCCESS;
1811         int dir;
1812         int status;
1813
1814         ioctx = scst_cmd_get_tgt_priv(scmnd);
1815         BUG_ON(!ioctx);
1816
1817         ch = scst_sess_get_tgt_priv(scst_cmd_get_session(scmnd));
1818         BUG_ON(!ch);
1819
1820         tag = scst_cmd_get_tag(scmnd);
1821
1822         if (ch->state != RDMA_CHANNEL_LIVE) {
1823                 printk(KERN_ERR PFX
1824                        "%s: tag= %lld channel in bad state %d\n",
1825                        __func__, (unsigned long long)tag, ch->state);
1826
1827                 if (ch->state == RDMA_CHANNEL_DISCONNECTING)
1828                         ret = SCST_TGT_RES_FATAL_ERROR;
1829                 else if (ch->state == RDMA_CHANNEL_CONNECTING)
1830                         ret = SCST_TGT_RES_QUEUE_FULL;
1831
1832                 if (unlikely(scst_cmd_aborted(scmnd))) {
1833                         scst_set_delivery_status(scmnd,
1834                                                  SCST_CMD_DELIVERY_ABORTED);
1835                         ret = SCST_TGT_RES_SUCCESS;
1836                 }
1837
1838                 goto out;
1839         }
1840
1841         dma_sync_single_for_cpu(ch->sport->sdev->device->dma_device, ioctx->dma,
1842                                 MAX_MESSAGE_SIZE, DMA_TO_DEVICE);
1843
1844         srp_rsp = ioctx->buf;
1845
1846         if (unlikely(scst_cmd_aborted(scmnd))) {
1847                 printk(KERN_ERR PFX
1848                        "%s: tag= %lld already get aborted\n",
1849                        __func__, (unsigned long long)tag);
1850                 scst_set_delivery_status(ioctx->scmnd,
1851                                          SCST_CMD_DELIVERY_ABORTED);
1852                 scst_tgt_cmd_done(ioctx->scmnd);
1853                 goto out;
1854         }
1855
1856         dir = scst_cmd_get_data_direction(scmnd);
1857         status = scst_cmd_get_status(scmnd) & 0xff;
1858
1859         srpt_build_cmd_rsp(ch, ioctx, NO_SENSE, NO_ADD_SENSE, tag);
1860
1861         if (SCST_SENSE_VALID(scst_cmd_get_sense_buffer(scmnd))) {
1862                 srp_rsp->sense_data_len = scst_cmd_get_sense_buffer_len(scmnd);
1863                 if (srp_rsp->sense_data_len >
1864                     (MAX_MESSAGE_SIZE - sizeof *srp_rsp))
1865                         srp_rsp->sense_data_len =
1866                             MAX_MESSAGE_SIZE - sizeof *srp_rsp;
1867
1868                 memcpy((u8 *) (srp_rsp + 1), scst_cmd_get_sense_buffer(scmnd),
1869                        srp_rsp->sense_data_len);
1870
1871                 srp_rsp->sense_data_len = cpu_to_be32(srp_rsp->sense_data_len);
1872                 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1873
1874                 if (!status)
1875                         status = SAM_STAT_CHECK_CONDITION;
1876         }
1877
1878         srp_rsp->status = status;
1879
1880         /* transfer read data if any */
1881         if (dir == SCST_DATA_READ && scst_cmd_get_resp_data_len(scmnd)) {
1882                 ret = srpt_xfer_data(ch, ioctx, scmnd);
1883                 if (ret != SCST_TGT_RES_SUCCESS) {
1884                         printk(KERN_ERR PFX
1885                                "%s: tag= %lld xfer_data failed\n",
1886                                __func__, (unsigned long long)tag);
1887                         goto out;
1888                 }
1889         }
1890
1891         if (srpt_post_send(ch, ioctx,
1892                            sizeof *srp_rsp +
1893                            be32_to_cpu(srp_rsp->sense_data_len))) {
1894                 printk(KERN_ERR PFX "%s: ch->state= %d tag= %lld\n",
1895                        __func__, ch->state,
1896                        (unsigned long long)tag);
1897                 ret = SCST_TGT_RES_FATAL_ERROR;
1898         }
1899
1900 out:
1901         return ret;
1902 }
1903
1904 static void srpt_tsk_mgmt_done(struct scst_mgmt_cmd *mcmnd)
1905 {
1906         struct srpt_rdma_ch *ch;
1907         struct srpt_mgmt_ioctx *mgmt_ioctx;
1908         struct srpt_ioctx *ioctx;
1909
1910         mgmt_ioctx = scst_mgmt_cmd_get_tgt_priv(mcmnd);
1911         BUG_ON(!mgmt_ioctx);
1912
1913         ch = mgmt_ioctx->ch;
1914         BUG_ON(!ch);
1915
1916         ioctx = mgmt_ioctx->ioctx;
1917         BUG_ON(!ioctx);
1918
1919         printk(KERN_WARNING PFX
1920                "%s: tsk_mgmt_done for tag= %lld status=%d\n",
1921                __func__, (unsigned long long)mgmt_ioctx->tag,
1922                scst_mgmt_cmd_get_status(mcmnd));
1923
1924         srpt_build_tskmgmt_rsp(ch, ioctx,
1925                                (scst_mgmt_cmd_get_status(mcmnd) ==
1926                                 SCST_MGMT_STATUS_SUCCESS) ?
1927                                SRP_TSK_MGMT_SUCCESS : SRP_TSK_MGMT_FAILED,
1928                                mgmt_ioctx->tag);
1929         srpt_post_send(ch, ioctx, sizeof(struct srp_rsp) + 4);
1930
1931         scst_mgmt_cmd_set_tgt_priv(mcmnd, NULL);
1932
1933         kfree(mgmt_ioctx);
1934 }
1935
1936 static void srpt_on_free_cmd(struct scst_cmd *scmnd)
1937 {
1938         struct srpt_rdma_ch *ch;
1939         struct srpt_ioctx *ioctx;
1940
1941         ioctx = scst_cmd_get_tgt_priv(scmnd);
1942         BUG_ON(!ioctx);
1943
1944         ch = scst_sess_get_tgt_priv(scst_cmd_get_session(scmnd));
1945         BUG_ON(!ch);
1946
1947         spin_lock_irq(&ch->spinlock);
1948         list_del(&ioctx->scmnd_list);
1949         ch->active_scmnd_cnt--;
1950         spin_unlock_irq(&ch->spinlock);
1951
1952         srpt_reset_ioctx(ch, ioctx);
1953         scst_cmd_set_tgt_priv(scmnd, NULL);
1954 }
1955
1956 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
1957 static void srpt_refresh_port_work(void *ctx)
1958 #else
1959 static void srpt_refresh_port_work(struct work_struct *work)
1960 #endif
1961 {
1962 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
1963         struct srpt_port *sport = (struct srpt_port *)ctx;
1964 #else
1965         struct srpt_port *sport = container_of(work, struct srpt_port, work);
1966 #endif
1967
1968         srpt_refresh_port(sport);
1969 }
1970
1971 static int srpt_detect(struct scst_tgt_template *tp)
1972 {
1973         struct srpt_device *sdev;
1974         struct srpt_port *sport;
1975         int i;
1976         int count = 0;
1977
1978         list_for_each_entry(sdev, &srpt_devices, list) {
1979
1980                 sdev->scst_tgt = scst_register(tp, NULL);
1981                 if (!sdev->scst_tgt)
1982                         goto out;
1983
1984                 scst_tgt_set_tgt_priv(sdev->scst_tgt, sdev);
1985
1986                 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
1987                         sport = &sdev->port[i - 1];
1988                         sport->sdev = sdev;
1989                         sport->port = i;
1990 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
1991                         INIT_WORK(&sport->work, srpt_refresh_port_work, sport);
1992 #else
1993                         INIT_WORK(&sport->work, srpt_refresh_port_work);
1994 #endif
1995
1996                         if (srpt_refresh_port(sport)) {
1997                                 scst_unregister(sdev->scst_tgt);
1998                                 goto out;
1999                         }
2000                 }
2001
2002                 ++count;
2003         }
2004 out:
2005         return count;
2006 }
2007
2008 static int srpt_release(struct scst_tgt *scst_tgt)
2009 {
2010         struct srpt_device *sdev = scst_tgt_get_tgt_priv(scst_tgt);
2011         struct srpt_port *sport;
2012         struct srpt_rdma_ch *ch, *tmp_ch;
2013         struct ib_port_modify port_modify = {
2014                 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP
2015         };
2016         int i;
2017
2018         list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
2019             srpt_release_channel(ch, 1);
2020
2021         for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
2022                 sport = &sdev->port[i - 1];
2023                 ib_modify_port(sdev->device, sport->port, 0, &port_modify);
2024                 ib_unregister_mad_agent(sport->mad_agent);
2025         }
2026
2027         scst_tgt_set_tgt_priv(scst_tgt, NULL);
2028
2029         complete(&sdev->scst_released);
2030
2031         return 0;
2032 }
2033
2034 static int srpt_ioctx_thread(void *arg)
2035 {
2036         struct srpt_ioctx *ioctx;
2037
2038         current->flags |= PF_NOFREEZE;
2039
2040         spin_lock_irq(&srpt_thread.thread_lock);
2041         while (!kthread_should_stop()) {
2042                 wait_queue_t wait;
2043                 init_waitqueue_entry(&wait, current);
2044
2045                 if (!srpt_test_ioctx_list()) {
2046                         add_wait_queue_exclusive(&ioctx_list_waitQ, &wait);
2047
2048                         for (;;) {
2049                                 set_current_state(TASK_INTERRUPTIBLE);
2050                                 if (srpt_test_ioctx_list())
2051                                         break;
2052                                 spin_unlock_irq(&srpt_thread.thread_lock);
2053                                 schedule();
2054                                 spin_lock_irq(&srpt_thread.thread_lock);
2055                         }
2056                         set_current_state(TASK_RUNNING);
2057                         remove_wait_queue(&ioctx_list_waitQ, &wait);
2058                 }
2059
2060                 while (!list_empty(&srpt_thread.thread_ioctx_list)) {
2061                         ioctx = list_entry(srpt_thread.thread_ioctx_list.next,
2062                                            struct srpt_ioctx, comp_list);
2063
2064                         list_del(&ioctx->comp_list);
2065
2066                         spin_unlock_irq(&srpt_thread.thread_lock);
2067                         switch (ioctx->op) {
2068                         case IB_WC_SEND:
2069                                 srpt_handle_send_comp(ioctx->ch, ioctx);
2070                                 break;
2071                         case IB_WC_RDMA_WRITE:
2072                         case IB_WC_RDMA_READ:
2073                                 srpt_handle_rdma_comp(ioctx->ch, ioctx);
2074                                 break;
2075                         case IB_WC_RECV:
2076                                 srpt_handle_new_iu(ioctx->ch, ioctx);
2077                                 break;
2078                         default:
2079                                 break;
2080                         }
2081                         spin_lock_irq(&srpt_thread.thread_lock);
2082                 }
2083         }
2084         spin_unlock_irq(&srpt_thread.thread_lock);
2085
2086         return 0;
2087 }
2088
2089 static struct scst_tgt_template srpt_template = {
2090         .name = DRV_NAME,
2091         .sg_tablesize = SRPT_DEF_SG_TABLESIZE,
2092         .xmit_response_atomic = 1,
2093         .rdy_to_xfer_atomic = 1,
2094         .no_proc_entry = 1,
2095         .detect = srpt_detect,
2096         .release = srpt_release,
2097         .xmit_response = srpt_xmit_response,
2098         .rdy_to_xfer = srpt_rdy_to_xfer,
2099         .on_free_cmd = srpt_on_free_cmd,
2100         .task_mgmt_fn_done = srpt_tsk_mgmt_done
2101 };
2102
2103 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
2104 static void srpt_release_class_dev(struct class_device *class_dev)
2105 #else
2106 static void srpt_release_class_dev(struct device *dev)
2107 #endif
2108 {
2109 }
2110
2111 static struct class srpt_class = {
2112         .name = "infiniband_srpt",
2113 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
2114         .release = srpt_release_class_dev
2115 #else
2116         .dev_release = srpt_release_class_dev
2117 #endif
2118 };
2119
2120 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
2121 static ssize_t show_login_info(struct class_device *class_dev, char *buf)
2122 #else
2123 static ssize_t show_login_info(struct device *dev,
2124                                struct device_attribute *attr, char *buf)
2125 #endif
2126 {
2127         struct srpt_device *sdev =
2128 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
2129                 container_of(class_dev, struct srpt_device, class_dev);
2130 #else
2131                 container_of(dev, struct srpt_device, dev);
2132 #endif
2133         struct srpt_port *sport;
2134         int i;
2135         int len = 0;
2136
2137         for (i = 0; i < sdev->device->phys_port_cnt; i++) {
2138                 sport = &sdev->port[i];
2139
2140                 len += sprintf(buf,
2141                                "tid_ext=%016llx,ioc_guid=%016llx,pkey=ffff,"
2142                                "dgid=%04x%04x%04x%04x%04x%04x%04x%04x,"
2143                                "service_id=%016llx\n",
2144                                (unsigned long long) mellanox_ioc_guid,
2145                                (unsigned long long) mellanox_ioc_guid,
2146                                be16_to_cpu(((__be16 *) sport->gid.raw)[0]),
2147                                be16_to_cpu(((__be16 *) sport->gid.raw)[1]),
2148                                be16_to_cpu(((__be16 *) sport->gid.raw)[2]),
2149                                be16_to_cpu(((__be16 *) sport->gid.raw)[3]),
2150                                be16_to_cpu(((__be16 *) sport->gid.raw)[4]),
2151                                be16_to_cpu(((__be16 *) sport->gid.raw)[5]),
2152                                be16_to_cpu(((__be16 *) sport->gid.raw)[6]),
2153                                be16_to_cpu(((__be16 *) sport->gid.raw)[7]),
2154                                (unsigned long long) mellanox_ioc_guid);
2155                 buf += len;
2156         }
2157
2158         return len;
2159 }
2160
2161 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
2162 static CLASS_DEVICE_ATTR(login_info, S_IRUGO, show_login_info, NULL);
2163 #else
2164 static DEVICE_ATTR(login_info, S_IRUGO, show_login_info, NULL);
2165 #endif
2166
2167 static void srpt_add_one(struct ib_device *device)
2168 {
2169         struct srpt_device *sdev;
2170         struct ib_srq_init_attr srq_attr;
2171         int i;
2172
2173         sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
2174         if (!sdev)
2175                 return;
2176
2177         sdev->device = device;
2178         init_completion(&sdev->scst_released);
2179
2180 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
2181         sdev->class_dev.class = &srpt_class;
2182         sdev->class_dev.dev = device->dma_device;
2183         snprintf(sdev->class_dev.class_id, BUS_ID_SIZE,
2184                  "srpt-%s", device->name);
2185 #else
2186         sdev->dev.class = &srpt_class;
2187         sdev->dev.parent = device->dma_device;
2188         snprintf(sdev->dev.bus_id, BUS_ID_SIZE, "srpt-%s", device->name);
2189 #endif
2190
2191 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
2192         if (class_device_register(&sdev->class_dev))
2193                 goto free_dev;
2194         if (class_device_create_file(&sdev->class_dev,
2195                                      &class_device_attr_login_info))
2196                 goto err_dev;
2197 #else
2198         if (device_register(&sdev->dev))
2199                 goto free_dev;
2200         if (device_create_file(&sdev->dev, &dev_attr_login_info))
2201                 goto err_dev;
2202 #endif
2203
2204         if (ib_query_device(device, &sdev->dev_attr))
2205                 goto err_dev;
2206
2207         sdev->pd = ib_alloc_pd(device);
2208         if (IS_ERR(sdev->pd))
2209                 goto err_dev;
2210
2211         sdev->mr = ib_get_dma_mr(sdev->pd, IB_ACCESS_LOCAL_WRITE);
2212         if (IS_ERR(sdev->mr))
2213                 goto err_pd;
2214
2215         srq_attr.event_handler = srpt_srq_event;
2216         srq_attr.srq_context = (void *)sdev;
2217         srq_attr.attr.max_wr = min(SRPT_SRQ_SIZE, sdev->dev_attr.max_srq_wr);
2218         srq_attr.attr.max_sge = 1;
2219         srq_attr.attr.srq_limit = 0;
2220
2221         sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
2222         if (IS_ERR(sdev->srq))
2223                 goto err_mr;
2224
2225         printk(KERN_DEBUG PFX "%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
2226                __func__, srq_attr.attr.max_wr,
2227               sdev->dev_attr.max_srq_wr, device->name);
2228
2229         if (!mellanox_ioc_guid)
2230                 mellanox_ioc_guid = be64_to_cpu(device->node_guid);
2231
2232         sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
2233         if (IS_ERR(sdev->cm_id))
2234                 goto err_srq;
2235
2236         /* print out target login information */
2237         printk(KERN_DEBUG PFX "Target login info: id_ext=%016llx,"
2238                 "ioc_guid=%016llx,pkey=ffff,service_id=%016llx\n",
2239                 (unsigned long long) mellanox_ioc_guid,
2240                 (unsigned long long) mellanox_ioc_guid,
2241                 (unsigned long long) mellanox_ioc_guid);
2242
2243         /*
2244          * We do not have a consistent service_id (ie. also id_ext of target_id)
2245          * to identify this target. We currently use the guid of the first HCA
2246          * in the system as service_id; therefore, the target_id will change
2247          * if this HCA is gone bad and replaced by different HCA
2248          */
2249         if (ib_cm_listen(sdev->cm_id, cpu_to_be64(mellanox_ioc_guid), 0, NULL))
2250                 goto err_cm;
2251
2252         INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
2253                               srpt_event_handler);
2254         if (ib_register_event_handler(&sdev->event_handler))
2255                 goto err_cm;
2256
2257         if (srpt_alloc_ioctx_ring(sdev))
2258                 goto err_event;
2259
2260         INIT_LIST_HEAD(&sdev->rch_list);
2261         spin_lock_init(&sdev->spinlock);
2262
2263         for (i = 0; i < SRPT_SRQ_SIZE; ++i)
2264                 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
2265
2266         list_add_tail(&sdev->list, &srpt_devices);
2267
2268         ib_set_client_data(device, &srpt_client, sdev);
2269
2270         return;
2271
2272 err_event:
2273         ib_unregister_event_handler(&sdev->event_handler);
2274 err_cm:
2275         ib_destroy_cm_id(sdev->cm_id);
2276 err_srq:
2277         ib_destroy_srq(sdev->srq);
2278 err_mr:
2279         ib_dereg_mr(sdev->mr);
2280 err_pd:
2281         ib_dealloc_pd(sdev->pd);
2282 err_dev:
2283 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
2284         class_device_unregister(&sdev->class_dev);
2285 #else
2286         device_unregister(&sdev->dev);
2287 #endif
2288 free_dev:
2289         kfree(sdev);
2290 }
2291
2292 static void srpt_remove_one(struct ib_device *device)
2293 {
2294         struct srpt_device *sdev;
2295         int i;
2296
2297         sdev = ib_get_client_data(device, &srpt_client);
2298         if (!sdev)
2299                 return;
2300
2301         wait_for_completion(&sdev->scst_released);
2302
2303         ib_unregister_event_handler(&sdev->event_handler);
2304         ib_destroy_cm_id(sdev->cm_id);
2305         ib_destroy_srq(sdev->srq);
2306         ib_dereg_mr(sdev->mr);
2307         ib_dealloc_pd(sdev->pd);
2308 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
2309         class_device_unregister(&sdev->class_dev);
2310 #else
2311         device_unregister(&sdev->dev);
2312 #endif
2313
2314         for (i = 0; i < SRPT_SRQ_SIZE; ++i)
2315                 srpt_free_ioctx(sdev, sdev->ioctx_ring[i]);
2316
2317         list_del(&sdev->list);
2318         kfree(sdev);
2319 }
2320
2321 static int __init srpt_init_module(void)
2322 {
2323         int ret;
2324
2325         INIT_LIST_HEAD(&srpt_devices);
2326
2327         ret = class_register(&srpt_class);
2328         if (ret) {
2329                 printk(KERN_ERR PFX "couldn't register class ib_srpt\n");
2330                 return ret;
2331         }
2332
2333         ret = ib_register_client(&srpt_client);
2334         if (ret) {
2335                 printk(KERN_ERR PFX "couldn't register IB client\n");
2336                 goto mem_out;
2337         }
2338
2339         ret = scst_register_target_template(&srpt_template);
2340         if (ret < 0) {
2341                 printk(KERN_ERR PFX "couldn't register with scst\n");
2342                 ret = -ENODEV;
2343                 goto ib_out;
2344         }
2345
2346         if (thread) {
2347                 spin_lock_init(&srpt_thread.thread_lock);
2348                 INIT_LIST_HEAD(&srpt_thread.thread_ioctx_list);
2349                 srpt_thread.thread = kthread_run(srpt_ioctx_thread,
2350                                                  NULL, "srpt_thread");
2351                 if (IS_ERR(srpt_thread.thread)) {
2352                         srpt_thread.thread = NULL;
2353                         thread = 0;
2354                 }
2355         }
2356
2357         return 0;
2358
2359 ib_out:
2360         ib_unregister_client(&srpt_client);
2361 mem_out:
2362         class_unregister(&srpt_class);
2363         return ret;
2364 }
2365
2366 static void __exit srpt_cleanup_module(void)
2367 {
2368         if (srpt_thread.thread)
2369                 kthread_stop(srpt_thread.thread);
2370         scst_unregister_target_template(&srpt_template);
2371         ib_unregister_client(&srpt_client);
2372         class_unregister(&srpt_class);
2373 }
2374
2375 module_init(srpt_init_module);
2376 module_exit(srpt_cleanup_module);