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