Make soft IRQ context default
[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 = 0;
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)
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 void srpt_register_channel_done(struct scst_session *scst_sess,
1165                                        void *data, int status)
1166 {
1167         struct srpt_rdma_ch *ch = data;
1168
1169         BUG_ON(!ch);
1170
1171         if (status) {
1172                 if (ch->scst_sess) {
1173                         scst_unregister_session(ch->scst_sess, 0, NULL);
1174                         ch->scst_sess = NULL;
1175                 }
1176                 printk(KERN_ERR PFX
1177                        "%s: Failed to establish sess= %p status= %d\n",
1178                        __func__, scst_sess, status);
1179         }
1180
1181         complete(&ch->scst_sess_done);
1182 }
1183
1184 static int srpt_disconnect_channel(struct srpt_rdma_ch *ch, int dreq)
1185 {
1186         spin_lock_irq(&ch->spinlock);
1187         ch->state = RDMA_CHANNEL_DISCONNECTING;
1188         spin_unlock_irq(&ch->spinlock);
1189
1190         if (dreq)
1191                 ib_send_cm_dreq(ch->cm_id, NULL, 0);
1192         else
1193                 ib_send_cm_drep(ch->cm_id, NULL, 0);
1194
1195         return 0;
1196 }
1197
1198 static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
1199                             struct ib_cm_req_event_param *param,
1200                             void *private_data)
1201 {
1202         struct srpt_device *sdev = cm_id->context;
1203         struct srp_login_req *req;
1204         struct srp_login_rsp *rsp;
1205         struct srp_login_rej *rej;
1206         struct ib_cm_rep_param *rep_param;
1207         struct srpt_rdma_ch *ch, *tmp_ch;
1208         u32 it_iu_len;
1209         int ret = 0;
1210
1211         if (!sdev || !private_data)
1212                 return -EINVAL;
1213
1214         rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
1215         rej = kzalloc(sizeof *rej, GFP_KERNEL);
1216         rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
1217
1218         if (!rsp || !rej || !rep_param) {
1219                 ret = -ENOMEM;
1220                 goto out;
1221         }
1222
1223         req = (struct srp_login_req *)private_data;
1224
1225         it_iu_len = be32_to_cpu(req->req_it_iu_len);
1226
1227         printk(KERN_DEBUG PFX
1228                "Host login i_port_id=0x%llx:0x%llx t_port_id=0x%llx:0x%llx"
1229                " it_iu_len=%d\n",
1230                (unsigned long long)
1231                be64_to_cpu(*(u64 *)&req->initiator_port_id[0]),
1232                (unsigned long long)
1233                be64_to_cpu(*(u64 *)&req->initiator_port_id[8]),
1234                (unsigned long long)be64_to_cpu(*(u64 *)&req->target_port_id[0]),
1235                (unsigned long long)be64_to_cpu(*(u64 *)&req->target_port_id[8]),
1236                it_iu_len);
1237
1238         if (it_iu_len > MAX_MESSAGE_SIZE || it_iu_len < 64) {
1239                 rej->reason =
1240                     cpu_to_be32(SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
1241                 ret = -EINVAL;
1242                 printk(KERN_WARNING PFX
1243                        "Reject invalid it_iu_len=%d\n", it_iu_len);
1244                 goto reject;
1245         }
1246
1247         if ((req->req_flags & 0x3) == SRP_MULTICHAN_SINGLE) {
1248                 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
1249
1250                 spin_lock_irq(&sdev->spinlock);
1251
1252                 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
1253                         if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
1254                             && !memcmp(ch->t_port_id, req->target_port_id, 16)
1255                             && param->port == ch->sport->port
1256                             && param->listen_id == ch->sport->sdev->cm_id
1257                             && ch->cm_id) {
1258                                 /* found an existing channel */
1259                                 printk(KERN_WARNING PFX
1260                                        "Found existing channel name= %s"
1261                                        " cm_id= %p state= %d\n",
1262                                        ch->sess_name, ch->cm_id, ch->state);
1263
1264                                 spin_unlock_irq(&sdev->spinlock);
1265
1266                                 rsp->rsp_flags =
1267                                     SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
1268
1269                                 if (ch->state == RDMA_CHANNEL_LIVE)
1270                                         srpt_disconnect_channel(ch, 1);
1271                                 else if (ch->state == RDMA_CHANNEL_CONNECTING) {
1272                                         ib_send_cm_rej(ch->cm_id,
1273                                                        IB_CM_REJ_NO_RESOURCES,
1274                                                        NULL, 0, NULL, 0);
1275                                         srpt_release_channel(ch, 1);
1276                                 }
1277
1278                                 spin_lock_irq(&sdev->spinlock);
1279                         }
1280                 }
1281
1282                 spin_unlock_irq(&sdev->spinlock);
1283
1284         } else
1285                 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
1286
1287         if (((u64) (*(u64 *) req->target_port_id) !=
1288              cpu_to_be64(mellanox_ioc_guid)) ||
1289             ((u64) (*(u64 *) (req->target_port_id + 8)) !=
1290              cpu_to_be64(mellanox_ioc_guid))) {
1291                 rej->reason =
1292                     cpu_to_be32(SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
1293                 ret = -ENOMEM;
1294                 printk(KERN_WARNING PFX "Reject invalid target_port_id\n");
1295                 goto reject;
1296         }
1297
1298         ch = kzalloc(sizeof *ch, GFP_KERNEL);
1299         if (!ch) {
1300                 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
1301                 printk(KERN_WARNING PFX "Reject failed allocate rdma_ch\n");
1302                 ret = -ENOMEM;
1303                 goto reject;
1304         }
1305
1306         spin_lock_init(&ch->spinlock);
1307         memcpy(ch->i_port_id, req->initiator_port_id, 16);
1308         memcpy(ch->t_port_id, req->target_port_id, 16);
1309         ch->sport = &sdev->port[param->port - 1];
1310         ch->cm_id = cm_id;
1311         ch->state = RDMA_CHANNEL_CONNECTING;
1312         INIT_LIST_HEAD(&ch->cmd_wait_list);
1313         INIT_LIST_HEAD(&ch->active_scmnd_list);
1314
1315         ret = srpt_create_ch_ib(ch);
1316         if (ret) {
1317                 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
1318                 printk(KERN_WARNING PFX "Reject failed to create rdma_ch\n");
1319                 goto free_ch;
1320         }
1321
1322         ret = srpt_ch_qp_rtr_rts(ch, ch->qp, IB_QPS_RTR);
1323         if (ret) {
1324                 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
1325                 printk(KERN_WARNING PFX
1326                        "Reject failed qp to rtr/rts ret=%d\n", ret);
1327                 goto destroy_ib;
1328         }
1329
1330         init_completion(&ch->scst_sess_done);
1331         sprintf(ch->sess_name, "0x%016llx%016llx",
1332                 (unsigned long long)be64_to_cpu(*(u64 *)ch->i_port_id),
1333                 (unsigned long long)be64_to_cpu(*(u64 *)(ch->i_port_id + 8)));
1334         ch->scst_sess =
1335             scst_register_session(sdev->scst_tgt, 1, ch->sess_name,
1336                                   ch, srpt_register_channel_done);
1337
1338         wait_for_completion(&ch->scst_sess_done);
1339
1340         if (!ch->scst_sess) {
1341                 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
1342                 printk(KERN_WARNING PFX "Reject failed to create scst sess");
1343                 goto destroy_ib;
1344         }
1345
1346         spin_lock_irq(&sdev->spinlock);
1347         list_add_tail(&ch->list, &sdev->rch_list);
1348         spin_unlock_irq(&sdev->spinlock);
1349
1350         printk(KERN_DEBUG PFX "Establish connection sess=%p name=%s cm_id=%p\n",
1351                ch->scst_sess, ch->sess_name, ch->cm_id);
1352
1353         scst_sess_set_tgt_priv(ch->scst_sess, ch);
1354
1355         /* create srp_login_response */
1356         rsp->opcode = SRP_LOGIN_RSP;
1357         rsp->tag = req->tag;
1358         rsp->max_it_iu_len = req->req_it_iu_len;
1359         rsp->max_ti_iu_len = req->req_it_iu_len;
1360         rsp->buf_fmt =
1361             cpu_to_be16(SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT);
1362         rsp->req_lim_delta = cpu_to_be32(SRPT_RQ_SIZE);
1363         atomic_set(&ch->req_lim_delta, 0);
1364
1365         /* create cm reply */
1366         rep_param->qp_num = ch->qp->qp_num;
1367         rep_param->private_data = (void *)rsp;
1368         rep_param->private_data_len = sizeof *rsp;
1369         rep_param->rnr_retry_count = 7;
1370         rep_param->flow_control = 1;
1371         rep_param->failover_accepted = 0;
1372         rep_param->srq = 1;
1373         rep_param->responder_resources = 4;
1374         rep_param->initiator_depth = 4;
1375
1376         ret = ib_send_cm_rep(cm_id, rep_param);
1377         if (ret)
1378                 srpt_release_channel(ch, 0);
1379
1380         goto out;
1381
1382 destroy_ib:
1383         ib_destroy_qp(ch->qp);
1384         ib_destroy_cq(ch->cq);
1385
1386 free_ch:
1387         kfree(ch);
1388
1389 reject:
1390         rej->opcode = SRP_LOGIN_REJ;
1391         rej->tag = req->tag;
1392         rej->buf_fmt =
1393             cpu_to_be16(SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT);
1394
1395         ret = ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
1396                              (void *)rej, sizeof *rej);
1397
1398 out:
1399         kfree(rep_param);
1400         kfree(rsp);
1401         kfree(rej);
1402
1403         return ret;
1404 }
1405
1406 static int srpt_find_and_release_channel(struct ib_cm_id *cm_id)
1407 {
1408         struct srpt_rdma_ch *ch;
1409
1410         ch = srpt_find_channel(cm_id);
1411         if (!ch)
1412                 return -EINVAL;
1413
1414         return srpt_release_channel(ch, 0);
1415 }
1416
1417 static int srpt_cm_rej_recv(struct ib_cm_id *cm_id)
1418 {
1419         printk(KERN_DEBUG PFX "%s: cm_id=%p\n", __func__, cm_id);
1420         return srpt_find_and_release_channel(cm_id);
1421 }
1422
1423 static int srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
1424 {
1425         struct srpt_rdma_ch *ch;
1426         int ret;
1427
1428         ch = srpt_find_channel(cm_id);
1429         if (!ch)
1430                 return -EINVAL;
1431
1432         if (ch->state == RDMA_CHANNEL_CONNECTING) {
1433                 struct srpt_ioctx *ioctx, *ioctx_tmp;
1434
1435                 spin_lock_irq(&ch->spinlock);
1436                 ch->state = RDMA_CHANNEL_LIVE;
1437                 spin_unlock_irq(&ch->spinlock);
1438                 ret = srpt_ch_qp_rtr_rts(ch, ch->qp, IB_QPS_RTS);
1439
1440                 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
1441                                          wait_list) {
1442                         list_del(&ioctx->wait_list);
1443                         srpt_handle_new_iu(ch, ioctx);
1444                 }
1445         } else if (ch->state == RDMA_CHANNEL_DISCONNECTING)
1446                 ret = -EAGAIN;
1447         else
1448                 ret = 0;
1449
1450         if (ret) {
1451                 printk(KERN_ERR PFX "cm_id=%p sess_name=%s state=%d\n",
1452                        cm_id, ch->sess_name, ch->state);
1453                 srpt_disconnect_channel(ch, 1);
1454         }
1455
1456         return ret;
1457 }
1458
1459 static int srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
1460 {
1461         printk(KERN_DEBUG PFX "%s: cm_id=%p\n", __func__, cm_id);
1462         return srpt_find_and_release_channel(cm_id);
1463 }
1464
1465 static int srpt_cm_rep_error(struct ib_cm_id *cm_id)
1466 {
1467         printk(KERN_DEBUG PFX "%s: cm_id=%p\n", __func__, cm_id);
1468         return srpt_find_and_release_channel(cm_id);
1469 }
1470
1471 static int srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
1472 {
1473         struct srpt_rdma_ch *ch;
1474         int ret = 0;
1475
1476         ch = srpt_find_channel(cm_id);
1477
1478         if (!ch)
1479                 return -EINVAL;
1480
1481         printk(KERN_DEBUG PFX "%s: cm_id= %p ch->state= %d\n",
1482                  __func__, cm_id, ch->state);
1483
1484         switch (ch->state) {
1485         case RDMA_CHANNEL_LIVE:
1486         case RDMA_CHANNEL_CONNECTING:
1487                 ret = srpt_disconnect_channel(ch, 0);
1488                 break;
1489         case RDMA_CHANNEL_DISCONNECTING:
1490         default:
1491                 break;
1492         }
1493
1494         return ret;
1495 }
1496
1497 static int srpt_cm_drep_recv(struct ib_cm_id *cm_id)
1498 {
1499         printk(KERN_DEBUG PFX "%s: cm_id=%p\n", __func__, cm_id);
1500         return srpt_find_and_release_channel(cm_id);
1501 }
1502
1503 static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1504 {
1505         int ret = 0;
1506
1507         switch (event->event) {
1508         case IB_CM_REQ_RECEIVED:
1509                 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
1510                                        event->private_data);
1511                 break;
1512         case IB_CM_REJ_RECEIVED:
1513                 ret = srpt_cm_rej_recv(cm_id);
1514                 break;
1515         case IB_CM_RTU_RECEIVED:
1516         case IB_CM_USER_ESTABLISHED:
1517                 ret = srpt_cm_rtu_recv(cm_id);
1518                 break;
1519         case IB_CM_DREQ_RECEIVED:
1520                 ret = srpt_cm_dreq_recv(cm_id);
1521                 break;
1522         case IB_CM_DREP_RECEIVED:
1523                 ret = srpt_cm_drep_recv(cm_id);
1524                 break;
1525         case IB_CM_TIMEWAIT_EXIT:
1526                 ret = srpt_cm_timewait_exit(cm_id);
1527                 break;
1528         case IB_CM_REP_ERROR:
1529                 ret = srpt_cm_rep_error(cm_id);
1530                 break;
1531         default:
1532                 break;
1533         }
1534
1535         return ret;
1536 }
1537
1538 static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1539                                  struct srpt_ioctx *ioctx,
1540                                  struct scst_cmd *scmnd)
1541 {
1542         struct scatterlist *scat;
1543         scst_data_direction dir;
1544         struct rdma_iu *riu;
1545         struct srp_direct_buf *db;
1546         dma_addr_t dma_addr;
1547         struct ib_sge *sge;
1548         u64 raddr;
1549         u32 rsize;
1550         u32 tsize;
1551         u32 dma_len;
1552         int count, nrdma;
1553         int i, j, k;
1554
1555         scat = scst_cmd_get_sg(scmnd);
1556         dir = scst_cmd_get_data_direction(scmnd);
1557         count = dma_map_sg(ch->sport->sdev->device->dma_device, scat,
1558                            scst_cmd_get_sg_cnt(scmnd),
1559                            scst_to_tgt_dma_dir(dir));
1560         if (unlikely(!count))
1561                 return -EBUSY;
1562
1563         if (ioctx->rdma_ius && ioctx->n_rdma_ius)
1564                 nrdma = ioctx->n_rdma_ius;
1565         else {
1566                 nrdma = count / SRPT_DEF_SG_PER_WQE + ioctx->n_rbuf;
1567
1568                 ioctx->rdma_ius = kzalloc(nrdma * sizeof *riu,
1569                                           scst_cmd_atomic(scmnd)
1570                                           ? GFP_ATOMIC : GFP_KERNEL);
1571                 if (!ioctx->rdma_ius) {
1572                         dma_unmap_sg(ch->sport->sdev->device->dma_device,
1573                                      scat, scst_cmd_get_sg_cnt(scmnd),
1574                                      scst_to_tgt_dma_dir(dir));
1575                         return -ENOMEM;
1576                 }
1577
1578                 ioctx->n_rdma_ius = nrdma;
1579         }
1580
1581         db = ioctx->rbufs;
1582         tsize = (dir == SCST_DATA_READ) ?
1583                 scst_cmd_get_resp_data_len(scmnd) : scst_cmd_get_bufflen(scmnd);
1584         dma_len = sg_dma_len(&scat[0]);
1585         riu = ioctx->rdma_ius;
1586
1587         /*
1588          * For each remote desc - calculate the #ib_sge.
1589          * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1590          *      each remote desc rdma_iu is required a rdma wr;
1591          * else
1592          *      we need to allocate extra rdma_iu to carry extra #ib_sge in
1593          *      another rdma wr
1594          */
1595         for (i = 0, j = 0;
1596              j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1597                 rsize = be32_to_cpu(db->len);
1598                 raddr = be64_to_cpu(db->va);
1599                 riu->raddr = raddr;
1600                 riu->rkey = be32_to_cpu(db->key);
1601                 riu->sge_cnt = 0;
1602
1603                 /* calculate how many sge required for this remote_buf */
1604                 while (rsize > 0 && tsize > 0) {
1605
1606                         if (rsize >= dma_len) {
1607                                 tsize -= dma_len;
1608                                 rsize -= dma_len;
1609                                 raddr += dma_len;
1610
1611                                 if (tsize > 0) {
1612                                         ++j;
1613                                         if (j < count)
1614                                                 dma_len = sg_dma_len(&scat[j]);
1615                                 }
1616                         } else {
1617                                 tsize -= rsize;
1618                                 dma_len -= rsize;
1619                                 rsize = 0;
1620                         }
1621
1622                         ++riu->sge_cnt;
1623
1624                         if (rsize > 0 && riu->sge_cnt == SRPT_DEF_SG_PER_WQE) {
1625                                 riu->sge =
1626                                     kmalloc(riu->sge_cnt * sizeof *riu->sge,
1627                                             scst_cmd_atomic(scmnd)
1628                                             ? GFP_ATOMIC : GFP_KERNEL);
1629                                 if (!riu->sge)
1630                                         goto free_mem;
1631
1632                                 ++ioctx->n_rdma;
1633                                 ++riu;
1634                                 riu->sge_cnt = 0;
1635                                 riu->raddr = raddr;
1636                                 riu->rkey = be32_to_cpu(db->key);
1637                         }
1638                 }
1639
1640                 riu->sge = kmalloc(riu->sge_cnt * sizeof *riu->sge,
1641                                    scst_cmd_atomic(scmnd)
1642                                    ? GFP_ATOMIC : GFP_KERNEL);
1643
1644                 if (!riu->sge)
1645                         goto free_mem;
1646
1647                 ++ioctx->n_rdma;
1648         }
1649
1650         db = ioctx->rbufs;
1651         scat = scst_cmd_get_sg(scmnd);
1652         tsize = (dir == SCST_DATA_READ) ?
1653                 scst_cmd_get_resp_data_len(scmnd) : scst_cmd_get_bufflen(scmnd);
1654         riu = ioctx->rdma_ius;
1655         dma_len = sg_dma_len(&scat[0]);
1656         dma_addr = sg_dma_address(&scat[0]);
1657
1658         /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1659         for (i = 0, j = 0;
1660              j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1661                 rsize = be32_to_cpu(db->len);
1662                 sge = riu->sge;
1663                 k = 0;
1664
1665                 while (rsize > 0 && tsize > 0) {
1666                         sge->addr = dma_addr;
1667                         sge->lkey = ch->sport->sdev->mr->lkey;
1668
1669                         if (rsize >= dma_len) {
1670                                 sge->length =
1671                                         (tsize < dma_len) ? tsize : dma_len;
1672                                 tsize -= dma_len;
1673                                 rsize -= dma_len;
1674
1675                                 if (tsize > 0) {
1676                                         ++j;
1677                                         if (j < count) {
1678                                                 dma_len = sg_dma_len(&scat[j]);
1679                                                 dma_addr =
1680                                                     sg_dma_address(&scat[j]);
1681                                         }
1682                                 }
1683                         } else {
1684                                 sge->length = (tsize < rsize) ? tsize : rsize;
1685                                 tsize -= rsize;
1686                                 dma_len -= rsize;
1687                                 dma_addr += rsize;
1688                                 rsize = 0;
1689                         }
1690
1691                         ++k;
1692                         if (k == riu->sge_cnt && rsize > 0) {
1693                                 ++riu;
1694                                 sge = riu->sge;
1695                                 k = 0;
1696                         } else if (rsize > 0)
1697                                 ++sge;
1698                 }
1699         }
1700
1701         return 0;
1702
1703 free_mem:
1704         while (ioctx->n_rdma)
1705                 kfree(ioctx->rdma_ius[ioctx->n_rdma--].sge);
1706
1707         kfree(ioctx->rdma_ius);
1708
1709         dma_unmap_sg(ch->sport->sdev->device->dma_device,
1710                      scat, scst_cmd_get_sg_cnt(scmnd),
1711                      scst_to_tgt_dma_dir(dir));
1712
1713         return -ENOMEM;
1714 }
1715
1716 static int srpt_perform_rdmas(struct srpt_rdma_ch *ch, struct srpt_ioctx *ioctx,
1717                               scst_data_direction dir)
1718 {
1719         struct ib_send_wr wr;
1720         struct ib_send_wr *bad_wr;
1721         struct rdma_iu *riu;
1722         int i;
1723         int ret = 0;
1724
1725         riu = ioctx->rdma_ius;
1726         memset(&wr, 0, sizeof wr);
1727
1728         for (i = 0; i < ioctx->n_rdma; ++i, ++riu) {
1729                 wr.opcode = (dir == SCST_DATA_READ) ?
1730                     IB_WR_RDMA_WRITE : IB_WR_RDMA_READ;
1731                 wr.next = NULL;
1732                 wr.wr_id = ioctx->index;
1733                 wr.wr.rdma.remote_addr = riu->raddr;
1734                 wr.wr.rdma.rkey = riu->rkey;
1735                 wr.num_sge = riu->sge_cnt;
1736                 wr.sg_list = riu->sge;
1737
1738                 /* only get completion event for the last rdma wr */
1739                 if (i == (ioctx->n_rdma - 1) && dir == SCST_DATA_WRITE)
1740                         wr.send_flags = IB_SEND_SIGNALED;
1741
1742                 ret = ib_post_send(ch->qp, &wr, &bad_wr);
1743                 if (ret)
1744                         break;
1745         }
1746
1747         return ret;
1748 }
1749
1750 static int srpt_xfer_data(struct srpt_rdma_ch *ch, struct srpt_ioctx *ioctx,
1751                           struct scst_cmd *scmnd)
1752 {
1753         int ret;
1754
1755         ret = srpt_map_sg_to_ib_sge(ch, ioctx, scmnd);
1756         if (ret) {
1757                 printk(KERN_ERR PFX "%s[%d] ret=%d\n", __func__, __LINE__, ret);
1758                 ret = SCST_TGT_RES_QUEUE_FULL;
1759                 goto out;
1760         }
1761
1762         ret = srpt_perform_rdmas(ch, ioctx, scst_cmd_get_data_direction(scmnd));
1763         if (ret) {
1764                 printk(KERN_ERR PFX "%s[%d] ret=%d\n", __func__, __LINE__, ret);
1765                 if (ret == -EAGAIN || ret == -ENOMEM)
1766                         ret = SCST_TGT_RES_QUEUE_FULL;
1767                 else
1768                         ret = SCST_TGT_RES_FATAL_ERROR;
1769                 goto out;
1770         }
1771
1772         ret = SCST_TGT_RES_SUCCESS;
1773
1774 out:
1775         return ret;
1776 }
1777
1778 static int srpt_rdy_to_xfer(struct scst_cmd *scmnd)
1779 {
1780         struct srpt_rdma_ch *ch;
1781         struct srpt_ioctx *ioctx;
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         if (ch->state == RDMA_CHANNEL_DISCONNECTING)
1790                 return SCST_TGT_RES_FATAL_ERROR;
1791         else if (ch->state == RDMA_CHANNEL_CONNECTING)
1792                 return SCST_TGT_RES_QUEUE_FULL;
1793
1794         return srpt_xfer_data(ch, ioctx, scmnd);
1795 }
1796
1797 static int srpt_xmit_response(struct scst_cmd *scmnd)
1798 {
1799         struct srpt_rdma_ch *ch;
1800         struct srpt_ioctx *ioctx;
1801         struct srp_rsp *srp_rsp;
1802         u64 tag;
1803         int ret = SCST_TGT_RES_SUCCESS;
1804         int dir;
1805         int status;
1806
1807         ioctx = scst_cmd_get_tgt_priv(scmnd);
1808         BUG_ON(!ioctx);
1809
1810         ch = scst_sess_get_tgt_priv(scst_cmd_get_session(scmnd));
1811         BUG_ON(!ch);
1812
1813         tag = scst_cmd_get_tag(scmnd);
1814
1815         if (ch->state != RDMA_CHANNEL_LIVE) {
1816                 printk(KERN_ERR PFX
1817                        "%s: tag= %lld channel in bad state %d\n",
1818                        __func__, (unsigned long long)tag, ch->state);
1819
1820                 if (ch->state == RDMA_CHANNEL_DISCONNECTING)
1821                         ret = SCST_TGT_RES_FATAL_ERROR;
1822                 else if (ch->state == RDMA_CHANNEL_CONNECTING)
1823                         ret = SCST_TGT_RES_QUEUE_FULL;
1824
1825                 if (unlikely(scst_cmd_aborted(scmnd)))
1826                         goto out_aborted;
1827
1828                 goto out;
1829         }
1830
1831         dma_sync_single_for_cpu(ch->sport->sdev->device->dma_device, ioctx->dma,
1832                                 MAX_MESSAGE_SIZE, DMA_TO_DEVICE);
1833
1834         srp_rsp = ioctx->buf;
1835
1836         if (unlikely(scst_cmd_aborted(scmnd))) {
1837                 printk(KERN_ERR PFX
1838                        "%s: tag= %lld already get aborted\n",
1839                        __func__, (unsigned long long)tag);
1840                 goto out_aborted;
1841         }
1842
1843         dir = scst_cmd_get_data_direction(scmnd);
1844         status = scst_cmd_get_status(scmnd) & 0xff;
1845
1846         srpt_build_cmd_rsp(ch, ioctx, NO_SENSE, NO_ADD_SENSE, tag);
1847
1848         if (SCST_SENSE_VALID(scst_cmd_get_sense_buffer(scmnd))) {
1849                 srp_rsp->sense_data_len = scst_cmd_get_sense_buffer_len(scmnd);
1850                 if (srp_rsp->sense_data_len >
1851                     (MAX_MESSAGE_SIZE - sizeof *srp_rsp))
1852                         srp_rsp->sense_data_len =
1853                             MAX_MESSAGE_SIZE - sizeof *srp_rsp;
1854
1855                 memcpy((u8 *) (srp_rsp + 1), scst_cmd_get_sense_buffer(scmnd),
1856                        srp_rsp->sense_data_len);
1857
1858                 srp_rsp->sense_data_len = cpu_to_be32(srp_rsp->sense_data_len);
1859                 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1860
1861                 if (!status)
1862                         status = SAM_STAT_CHECK_CONDITION;
1863         }
1864
1865         srp_rsp->status = status;
1866
1867         /* transfer read data if any */
1868         if (dir == SCST_DATA_READ && scst_cmd_get_resp_data_len(scmnd)) {
1869                 ret = srpt_xfer_data(ch, ioctx, scmnd);
1870                 if (ret != SCST_TGT_RES_SUCCESS) {
1871                         printk(KERN_ERR PFX
1872                                "%s: tag= %lld xfer_data failed\n",
1873                                __func__, (unsigned long long)tag);
1874                         goto out;
1875                 }
1876         }
1877
1878         if (srpt_post_send(ch, ioctx,
1879                            sizeof *srp_rsp +
1880                            be32_to_cpu(srp_rsp->sense_data_len))) {
1881                 printk(KERN_ERR PFX "%s: ch->state= %d tag= %lld\n",
1882                        __func__, ch->state,
1883                        (unsigned long long)tag);
1884                 ret = SCST_TGT_RES_FATAL_ERROR;
1885         }
1886
1887 out:
1888         return ret;
1889
1890 out_aborted:
1891         ret = SCST_TGT_RES_SUCCESS;
1892         scst_set_delivery_status(scmnd, SCST_CMD_DELIVERY_ABORTED);
1893         scst_tgt_cmd_done(scmnd, SCST_CONTEXT_SAME);
1894         goto out;
1895 }
1896
1897 static void srpt_tsk_mgmt_done(struct scst_mgmt_cmd *mcmnd)
1898 {
1899         struct srpt_rdma_ch *ch;
1900         struct srpt_mgmt_ioctx *mgmt_ioctx;
1901         struct srpt_ioctx *ioctx;
1902
1903         mgmt_ioctx = scst_mgmt_cmd_get_tgt_priv(mcmnd);
1904         BUG_ON(!mgmt_ioctx);
1905
1906         ch = mgmt_ioctx->ch;
1907         BUG_ON(!ch);
1908
1909         ioctx = mgmt_ioctx->ioctx;
1910         BUG_ON(!ioctx);
1911
1912         printk(KERN_WARNING PFX
1913                "%s: tsk_mgmt_done for tag= %lld status=%d\n",
1914                __func__, (unsigned long long)mgmt_ioctx->tag,
1915                scst_mgmt_cmd_get_status(mcmnd));
1916
1917         srpt_build_tskmgmt_rsp(ch, ioctx,
1918                                (scst_mgmt_cmd_get_status(mcmnd) ==
1919                                 SCST_MGMT_STATUS_SUCCESS) ?
1920                                SRP_TSK_MGMT_SUCCESS : SRP_TSK_MGMT_FAILED,
1921                                mgmt_ioctx->tag);
1922         srpt_post_send(ch, ioctx, sizeof(struct srp_rsp) + 4);
1923
1924         scst_mgmt_cmd_set_tgt_priv(mcmnd, NULL);
1925
1926         kfree(mgmt_ioctx);
1927 }
1928
1929 static void srpt_on_free_cmd(struct scst_cmd *scmnd)
1930 {
1931         struct srpt_rdma_ch *ch;
1932         struct srpt_ioctx *ioctx;
1933
1934         ioctx = scst_cmd_get_tgt_priv(scmnd);
1935         BUG_ON(!ioctx);
1936
1937         ch = scst_sess_get_tgt_priv(scst_cmd_get_session(scmnd));
1938         BUG_ON(!ch);
1939
1940         spin_lock_irq(&ch->spinlock);
1941         list_del(&ioctx->scmnd_list);
1942         ch->active_scmnd_cnt--;
1943         spin_unlock_irq(&ch->spinlock);
1944
1945         srpt_reset_ioctx(ch, ioctx);
1946         scst_cmd_set_tgt_priv(scmnd, NULL);
1947 }
1948
1949 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
1950 static void srpt_refresh_port_work(void *ctx)
1951 #else
1952 static void srpt_refresh_port_work(struct work_struct *work)
1953 #endif
1954 {
1955 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
1956         struct srpt_port *sport = (struct srpt_port *)ctx;
1957 #else
1958         struct srpt_port *sport = container_of(work, struct srpt_port, work);
1959 #endif
1960
1961         srpt_refresh_port(sport);
1962 }
1963
1964 static int srpt_detect(struct scst_tgt_template *tp)
1965 {
1966         struct srpt_device *sdev;
1967         struct srpt_port *sport;
1968         int i;
1969         int count = 0;
1970
1971         list_for_each_entry(sdev, &srpt_devices, list) {
1972
1973                 sdev->scst_tgt = scst_register(tp, NULL);
1974                 if (!sdev->scst_tgt)
1975                         goto out;
1976
1977                 scst_tgt_set_tgt_priv(sdev->scst_tgt, sdev);
1978
1979                 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
1980                         sport = &sdev->port[i - 1];
1981                         sport->sdev = sdev;
1982                         sport->port = i;
1983 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
1984                         INIT_WORK(&sport->work, srpt_refresh_port_work, sport);
1985 #else
1986                         INIT_WORK(&sport->work, srpt_refresh_port_work);
1987 #endif
1988
1989                         if (srpt_refresh_port(sport)) {
1990                                 scst_unregister(sdev->scst_tgt);
1991                                 goto out;
1992                         }
1993                 }
1994
1995                 ++count;
1996         }
1997 out:
1998         return count;
1999 }
2000
2001 static int srpt_release(struct scst_tgt *scst_tgt)
2002 {
2003         struct srpt_device *sdev = scst_tgt_get_tgt_priv(scst_tgt);
2004         struct srpt_port *sport;
2005         struct srpt_rdma_ch *ch, *tmp_ch;
2006         struct ib_port_modify port_modify = {
2007                 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP
2008         };
2009         int i;
2010
2011         list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
2012             srpt_release_channel(ch, 1);
2013
2014         for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
2015                 sport = &sdev->port[i - 1];
2016                 ib_modify_port(sdev->device, sport->port, 0, &port_modify);
2017                 ib_unregister_mad_agent(sport->mad_agent);
2018         }
2019
2020         scst_tgt_set_tgt_priv(scst_tgt, NULL);
2021
2022         complete(&sdev->scst_released);
2023
2024         return 0;
2025 }
2026
2027 static int srpt_ioctx_thread(void *arg)
2028 {
2029         struct srpt_ioctx *ioctx;
2030
2031         current->flags |= PF_NOFREEZE;
2032
2033         spin_lock_irq(&srpt_thread.thread_lock);
2034         while (!kthread_should_stop()) {
2035                 wait_queue_t wait;
2036                 init_waitqueue_entry(&wait, current);
2037
2038                 if (!srpt_test_ioctx_list()) {
2039                         add_wait_queue_exclusive(&ioctx_list_waitQ, &wait);
2040
2041                         for (;;) {
2042                                 set_current_state(TASK_INTERRUPTIBLE);
2043                                 if (srpt_test_ioctx_list())
2044                                         break;
2045                                 spin_unlock_irq(&srpt_thread.thread_lock);
2046                                 schedule();
2047                                 spin_lock_irq(&srpt_thread.thread_lock);
2048                         }
2049                         set_current_state(TASK_RUNNING);
2050                         remove_wait_queue(&ioctx_list_waitQ, &wait);
2051                 }
2052
2053                 while (!list_empty(&srpt_thread.thread_ioctx_list)) {
2054                         ioctx = list_entry(srpt_thread.thread_ioctx_list.next,
2055                                            struct srpt_ioctx, comp_list);
2056
2057                         list_del(&ioctx->comp_list);
2058
2059                         spin_unlock_irq(&srpt_thread.thread_lock);
2060                         switch (ioctx->op) {
2061                         case IB_WC_SEND:
2062                                 srpt_handle_send_comp(ioctx->ch, ioctx,
2063                                         SCST_CONTEXT_DIRECT);
2064                                 break;
2065                         case IB_WC_RDMA_WRITE:
2066                         case IB_WC_RDMA_READ:
2067                                 srpt_handle_rdma_comp(ioctx->ch, ioctx);
2068                                 break;
2069                         case IB_WC_RECV:
2070                                 srpt_handle_new_iu(ioctx->ch, ioctx);
2071                                 break;
2072                         default:
2073                                 break;
2074                         }
2075                         spin_lock_irq(&srpt_thread.thread_lock);
2076                 }
2077         }
2078         spin_unlock_irq(&srpt_thread.thread_lock);
2079
2080         return 0;
2081 }
2082
2083 static struct scst_tgt_template srpt_template = {
2084         .name = DRV_NAME,
2085         .sg_tablesize = SRPT_DEF_SG_TABLESIZE,
2086         .xmit_response_atomic = 1,
2087         .rdy_to_xfer_atomic = 1,
2088         .no_proc_entry = 1,
2089         .detect = srpt_detect,
2090         .release = srpt_release,
2091         .xmit_response = srpt_xmit_response,
2092         .rdy_to_xfer = srpt_rdy_to_xfer,
2093         .on_free_cmd = srpt_on_free_cmd,
2094         .task_mgmt_fn_done = srpt_tsk_mgmt_done
2095 };
2096
2097 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
2098 static void srpt_release_class_dev(struct class_device *class_dev)
2099 #else
2100 static void srpt_release_class_dev(struct device *dev)
2101 #endif
2102 {
2103 }
2104
2105 static struct class srpt_class = {
2106         .name = "infiniband_srpt",
2107 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
2108         .release = srpt_release_class_dev
2109 #else
2110         .dev_release = srpt_release_class_dev
2111 #endif
2112 };
2113
2114 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
2115 static ssize_t show_login_info(struct class_device *class_dev, char *buf)
2116 #else
2117 static ssize_t show_login_info(struct device *dev,
2118                                struct device_attribute *attr, char *buf)
2119 #endif
2120 {
2121         struct srpt_device *sdev =
2122 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
2123                 container_of(class_dev, struct srpt_device, class_dev);
2124 #else
2125                 container_of(dev, struct srpt_device, dev);
2126 #endif
2127         struct srpt_port *sport;
2128         int i;
2129         int len = 0;
2130
2131         for (i = 0; i < sdev->device->phys_port_cnt; i++) {
2132                 sport = &sdev->port[i];
2133
2134                 len += sprintf(buf,
2135                                "tid_ext=%016llx,ioc_guid=%016llx,pkey=ffff,"
2136                                "dgid=%04x%04x%04x%04x%04x%04x%04x%04x,"
2137                                "service_id=%016llx\n",
2138                                (unsigned long long) mellanox_ioc_guid,
2139                                (unsigned long long) mellanox_ioc_guid,
2140                                be16_to_cpu(((__be16 *) sport->gid.raw)[0]),
2141                                be16_to_cpu(((__be16 *) sport->gid.raw)[1]),
2142                                be16_to_cpu(((__be16 *) sport->gid.raw)[2]),
2143                                be16_to_cpu(((__be16 *) sport->gid.raw)[3]),
2144                                be16_to_cpu(((__be16 *) sport->gid.raw)[4]),
2145                                be16_to_cpu(((__be16 *) sport->gid.raw)[5]),
2146                                be16_to_cpu(((__be16 *) sport->gid.raw)[6]),
2147                                be16_to_cpu(((__be16 *) sport->gid.raw)[7]),
2148                                (unsigned long long) mellanox_ioc_guid);
2149                 buf += len;
2150         }
2151
2152         return len;
2153 }
2154
2155 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
2156 static CLASS_DEVICE_ATTR(login_info, S_IRUGO, show_login_info, NULL);
2157 #else
2158 static DEVICE_ATTR(login_info, S_IRUGO, show_login_info, NULL);
2159 #endif
2160
2161 static void srpt_add_one(struct ib_device *device)
2162 {
2163         struct srpt_device *sdev;
2164         struct ib_srq_init_attr srq_attr;
2165         int i;
2166
2167         sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
2168         if (!sdev)
2169                 return;
2170
2171         sdev->device = device;
2172         init_completion(&sdev->scst_released);
2173
2174 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
2175         sdev->class_dev.class = &srpt_class;
2176         sdev->class_dev.dev = device->dma_device;
2177         snprintf(sdev->class_dev.class_id, BUS_ID_SIZE,
2178                  "srpt-%s", device->name);
2179 #else
2180         sdev->dev.class = &srpt_class;
2181         sdev->dev.parent = device->dma_device;
2182         snprintf(sdev->dev.bus_id, BUS_ID_SIZE, "srpt-%s", device->name);
2183 #endif
2184
2185 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
2186         if (class_device_register(&sdev->class_dev))
2187                 goto free_dev;
2188         if (class_device_create_file(&sdev->class_dev,
2189                                      &class_device_attr_login_info))
2190                 goto err_dev;
2191 #else
2192         if (device_register(&sdev->dev))
2193                 goto free_dev;
2194         if (device_create_file(&sdev->dev, &dev_attr_login_info))
2195                 goto err_dev;
2196 #endif
2197
2198         if (ib_query_device(device, &sdev->dev_attr))
2199                 goto err_dev;
2200
2201         sdev->pd = ib_alloc_pd(device);
2202         if (IS_ERR(sdev->pd))
2203                 goto err_dev;
2204
2205         sdev->mr = ib_get_dma_mr(sdev->pd, IB_ACCESS_LOCAL_WRITE);
2206         if (IS_ERR(sdev->mr))
2207                 goto err_pd;
2208
2209         srq_attr.event_handler = srpt_srq_event;
2210         srq_attr.srq_context = (void *)sdev;
2211         srq_attr.attr.max_wr = min(SRPT_SRQ_SIZE, sdev->dev_attr.max_srq_wr);
2212         srq_attr.attr.max_sge = 1;
2213         srq_attr.attr.srq_limit = 0;
2214
2215         sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
2216         if (IS_ERR(sdev->srq))
2217                 goto err_mr;
2218
2219         printk(KERN_DEBUG PFX "%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
2220                __func__, srq_attr.attr.max_wr,
2221               sdev->dev_attr.max_srq_wr, device->name);
2222
2223         if (!mellanox_ioc_guid)
2224                 mellanox_ioc_guid = be64_to_cpu(device->node_guid);
2225
2226         sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
2227         if (IS_ERR(sdev->cm_id))
2228                 goto err_srq;
2229
2230         /* print out target login information */
2231         printk(KERN_DEBUG PFX "Target login info: id_ext=%016llx,"
2232                 "ioc_guid=%016llx,pkey=ffff,service_id=%016llx\n",
2233                 (unsigned long long) mellanox_ioc_guid,
2234                 (unsigned long long) mellanox_ioc_guid,
2235                 (unsigned long long) mellanox_ioc_guid);
2236
2237         /*
2238          * We do not have a consistent service_id (ie. also id_ext of target_id)
2239          * to identify this target. We currently use the guid of the first HCA
2240          * in the system as service_id; therefore, the target_id will change
2241          * if this HCA is gone bad and replaced by different HCA
2242          */
2243         if (ib_cm_listen(sdev->cm_id, cpu_to_be64(mellanox_ioc_guid), 0, NULL))
2244                 goto err_cm;
2245
2246         INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
2247                               srpt_event_handler);
2248         if (ib_register_event_handler(&sdev->event_handler))
2249                 goto err_cm;
2250
2251         if (srpt_alloc_ioctx_ring(sdev))
2252                 goto err_event;
2253
2254         INIT_LIST_HEAD(&sdev->rch_list);
2255         spin_lock_init(&sdev->spinlock);
2256
2257         for (i = 0; i < SRPT_SRQ_SIZE; ++i)
2258                 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
2259
2260         list_add_tail(&sdev->list, &srpt_devices);
2261
2262         ib_set_client_data(device, &srpt_client, sdev);
2263
2264         return;
2265
2266 err_event:
2267         ib_unregister_event_handler(&sdev->event_handler);
2268 err_cm:
2269         ib_destroy_cm_id(sdev->cm_id);
2270 err_srq:
2271         ib_destroy_srq(sdev->srq);
2272 err_mr:
2273         ib_dereg_mr(sdev->mr);
2274 err_pd:
2275         ib_dealloc_pd(sdev->pd);
2276 err_dev:
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 free_dev:
2283         kfree(sdev);
2284 }
2285
2286 static void srpt_remove_one(struct ib_device *device)
2287 {
2288         struct srpt_device *sdev;
2289         int i;
2290
2291         sdev = ib_get_client_data(device, &srpt_client);
2292         if (!sdev)
2293                 return;
2294
2295         wait_for_completion(&sdev->scst_released);
2296
2297         ib_unregister_event_handler(&sdev->event_handler);
2298         ib_destroy_cm_id(sdev->cm_id);
2299         ib_destroy_srq(sdev->srq);
2300         ib_dereg_mr(sdev->mr);
2301         ib_dealloc_pd(sdev->pd);
2302 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
2303         class_device_unregister(&sdev->class_dev);
2304 #else
2305         device_unregister(&sdev->dev);
2306 #endif
2307
2308         for (i = 0; i < SRPT_SRQ_SIZE; ++i)
2309                 srpt_free_ioctx(sdev, sdev->ioctx_ring[i]);
2310
2311         list_del(&sdev->list);
2312         kfree(sdev);
2313 }
2314
2315 static int __init srpt_init_module(void)
2316 {
2317         int ret;
2318
2319         INIT_LIST_HEAD(&srpt_devices);
2320
2321         ret = class_register(&srpt_class);
2322         if (ret) {
2323                 printk(KERN_ERR PFX "couldn't register class ib_srpt\n");
2324                 return ret;
2325         }
2326
2327         ret = ib_register_client(&srpt_client);
2328         if (ret) {
2329                 printk(KERN_ERR PFX "couldn't register IB client\n");
2330                 goto mem_out;
2331         }
2332
2333         ret = scst_register_target_template(&srpt_template);
2334         if (ret < 0) {
2335                 printk(KERN_ERR PFX "couldn't register with scst\n");
2336                 ret = -ENODEV;
2337                 goto ib_out;
2338         }
2339
2340         if (thread) {
2341                 spin_lock_init(&srpt_thread.thread_lock);
2342                 INIT_LIST_HEAD(&srpt_thread.thread_ioctx_list);
2343                 srpt_thread.thread = kthread_run(srpt_ioctx_thread,
2344                                                  NULL, "srpt_thread");
2345                 if (IS_ERR(srpt_thread.thread)) {
2346                         srpt_thread.thread = NULL;
2347                         thread = 0;
2348                 }
2349         }
2350
2351         return 0;
2352
2353 ib_out:
2354         ib_unregister_client(&srpt_client);
2355 mem_out:
2356         class_unregister(&srpt_class);
2357         return ret;
2358 }
2359
2360 static void __exit srpt_cleanup_module(void)
2361 {
2362         if (srpt_thread.thread)
2363                 kthread_stop(srpt_thread.thread);
2364         scst_unregister_target_template(&srpt_template);
2365         ib_unregister_client(&srpt_client);
2366         class_unregister(&srpt_class);
2367 }
2368
2369 module_init(srpt_init_module);
2370 module_exit(srpt_cleanup_module);