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