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