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