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