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