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