GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / infiniband / ulp / srpt / ib_srpt.c
1 /*
2  * Copyright (c) 2006 - 2009 Mellanox Technology Inc.  All rights reserved.
3  * Copyright (C) 2008 - 2011 Bart Van Assche <bvanassche@acm.org>.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  *
33  */
34
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/err.h>
39 #include <linux/ctype.h>
40 #include <linux/kthread.h>
41 #include <linux/string.h>
42 #include <linux/delay.h>
43 #include <linux/atomic.h>
44 #include <linux/inet.h>
45 #include <rdma/ib_cache.h>
46 #include <scsi/scsi_proto.h>
47 #include <scsi/scsi_tcq.h>
48 #include <target/target_core_base.h>
49 #include <target/target_core_fabric.h>
50 #include "ib_srpt.h"
51
52 /* Name of this kernel module. */
53 #define DRV_NAME                "ib_srpt"
54 #define DRV_VERSION             "2.0.0"
55 #define DRV_RELDATE             "2011-02-14"
56
57 #define SRPT_ID_STRING  "Linux SRP target"
58
59 #undef pr_fmt
60 #define pr_fmt(fmt) DRV_NAME " " fmt
61
62 MODULE_AUTHOR("Vu Pham and Bart Van Assche");
63 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
64                    "v" DRV_VERSION " (" DRV_RELDATE ")");
65 MODULE_LICENSE("Dual BSD/GPL");
66
67 /*
68  * Global Variables
69  */
70
71 static u64 srpt_service_guid;
72 static DEFINE_SPINLOCK(srpt_dev_lock);  /* Protects srpt_dev_list. */
73 static LIST_HEAD(srpt_dev_list);        /* List of srpt_device structures. */
74
75 static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
76 module_param(srp_max_req_size, int, 0444);
77 MODULE_PARM_DESC(srp_max_req_size,
78                  "Maximum size of SRP request messages in bytes.");
79
80 static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
81 module_param(srpt_srq_size, int, 0444);
82 MODULE_PARM_DESC(srpt_srq_size,
83                  "Shared receive queue (SRQ) size.");
84
85 static int srpt_get_u64_x(char *buffer, const struct kernel_param *kp)
86 {
87         return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
88 }
89 module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
90                   0444);
91 MODULE_PARM_DESC(srpt_service_guid,
92                  "Using this value for ioc_guid, id_ext, and cm_listen_id"
93                  " instead of using the node_guid of the first HCA.");
94
95 static struct ib_client srpt_client;
96 /* Protects both rdma_cm_port and rdma_cm_id. */
97 static DEFINE_MUTEX(rdma_cm_mutex);
98 /* Port number RDMA/CM will bind to. */
99 static u16 rdma_cm_port;
100 static struct rdma_cm_id *rdma_cm_id;
101 static void srpt_release_cmd(struct se_cmd *se_cmd);
102 static void srpt_free_ch(struct kref *kref);
103 static int srpt_queue_status(struct se_cmd *cmd);
104 static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc);
105 static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc);
106 static void srpt_process_wait_list(struct srpt_rdma_ch *ch);
107
108 /*
109  * The only allowed channel state changes are those that change the channel
110  * state into a state with a higher numerical value. Hence the new > prev test.
111  */
112 static bool srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new)
113 {
114         unsigned long flags;
115         enum rdma_ch_state prev;
116         bool changed = false;
117
118         spin_lock_irqsave(&ch->spinlock, flags);
119         prev = ch->state;
120         if (new > prev) {
121                 ch->state = new;
122                 changed = true;
123         }
124         spin_unlock_irqrestore(&ch->spinlock, flags);
125
126         return changed;
127 }
128
129 /**
130  * srpt_event_handler - asynchronous IB event callback function
131  * @handler: IB event handler registered by ib_register_event_handler().
132  * @event: Description of the event that occurred.
133  *
134  * Callback function called by the InfiniBand core when an asynchronous IB
135  * event occurs. This callback may occur in interrupt context. See also
136  * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
137  * Architecture Specification.
138  */
139 static void srpt_event_handler(struct ib_event_handler *handler,
140                                struct ib_event *event)
141 {
142         struct srpt_device *sdev;
143         struct srpt_port *sport;
144         u8 port_num;
145
146         sdev = ib_get_client_data(event->device, &srpt_client);
147         if (!sdev || sdev->device != event->device)
148                 return;
149
150         pr_debug("ASYNC event= %d on device= %s\n", event->event,
151                  sdev->device->name);
152
153         switch (event->event) {
154         case IB_EVENT_PORT_ERR:
155                 port_num = event->element.port_num - 1;
156                 if (port_num < sdev->device->phys_port_cnt) {
157                         sport = &sdev->port[port_num];
158                         sport->lid = 0;
159                         sport->sm_lid = 0;
160                 } else {
161                         WARN(true, "event %d: port_num %d out of range 1..%d\n",
162                              event->event, port_num + 1,
163                              sdev->device->phys_port_cnt);
164                 }
165                 break;
166         case IB_EVENT_PORT_ACTIVE:
167         case IB_EVENT_LID_CHANGE:
168         case IB_EVENT_PKEY_CHANGE:
169         case IB_EVENT_SM_CHANGE:
170         case IB_EVENT_CLIENT_REREGISTER:
171         case IB_EVENT_GID_CHANGE:
172                 /* Refresh port data asynchronously. */
173                 port_num = event->element.port_num - 1;
174                 if (port_num < sdev->device->phys_port_cnt) {
175                         sport = &sdev->port[port_num];
176                         if (!sport->lid && !sport->sm_lid)
177                                 schedule_work(&sport->work);
178                 } else {
179                         WARN(true, "event %d: port_num %d out of range 1..%d\n",
180                              event->event, port_num + 1,
181                              sdev->device->phys_port_cnt);
182                 }
183                 break;
184         default:
185                 pr_err("received unrecognized IB event %d\n", event->event);
186                 break;
187         }
188 }
189
190 /**
191  * srpt_srq_event - SRQ event callback function
192  * @event: Description of the event that occurred.
193  * @ctx: Context pointer specified at SRQ creation time.
194  */
195 static void srpt_srq_event(struct ib_event *event, void *ctx)
196 {
197         pr_debug("SRQ event %d\n", event->event);
198 }
199
200 static const char *get_ch_state_name(enum rdma_ch_state s)
201 {
202         switch (s) {
203         case CH_CONNECTING:
204                 return "connecting";
205         case CH_LIVE:
206                 return "live";
207         case CH_DISCONNECTING:
208                 return "disconnecting";
209         case CH_DRAINING:
210                 return "draining";
211         case CH_DISCONNECTED:
212                 return "disconnected";
213         }
214         return "???";
215 }
216
217 /**
218  * srpt_qp_event - QP event callback function
219  * @event: Description of the event that occurred.
220  * @ch: SRPT RDMA channel.
221  */
222 static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
223 {
224         pr_debug("QP event %d on ch=%p sess_name=%s state=%d\n",
225                  event->event, ch, ch->sess_name, ch->state);
226
227         switch (event->event) {
228         case IB_EVENT_COMM_EST:
229                 if (ch->using_rdma_cm)
230                         rdma_notify(ch->rdma_cm.cm_id, event->event);
231                 else
232                         ib_cm_notify(ch->ib_cm.cm_id, event->event);
233                 break;
234         case IB_EVENT_QP_LAST_WQE_REACHED:
235                 pr_debug("%s-%d, state %s: received Last WQE event.\n",
236                          ch->sess_name, ch->qp->qp_num,
237                          get_ch_state_name(ch->state));
238                 break;
239         default:
240                 pr_err("received unrecognized IB QP event %d\n", event->event);
241                 break;
242         }
243 }
244
245 /**
246  * srpt_set_ioc - initialize a IOUnitInfo structure
247  * @c_list: controller list.
248  * @slot: one-based slot number.
249  * @value: four-bit value.
250  *
251  * Copies the lowest four bits of value in element slot of the array of four
252  * bit elements called c_list (controller list). The index slot is one-based.
253  */
254 static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
255 {
256         u16 id;
257         u8 tmp;
258
259         id = (slot - 1) / 2;
260         if (slot & 0x1) {
261                 tmp = c_list[id] & 0xf;
262                 c_list[id] = (value << 4) | tmp;
263         } else {
264                 tmp = c_list[id] & 0xf0;
265                 c_list[id] = (value & 0xf) | tmp;
266         }
267 }
268
269 /**
270  * srpt_get_class_port_info - copy ClassPortInfo to a management datagram
271  * @mad: Datagram that will be sent as response to DM_ATTR_CLASS_PORT_INFO.
272  *
273  * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
274  * Specification.
275  */
276 static void srpt_get_class_port_info(struct ib_dm_mad *mad)
277 {
278         struct ib_class_port_info *cif;
279
280         cif = (struct ib_class_port_info *)mad->data;
281         memset(cif, 0, sizeof(*cif));
282         cif->base_version = 1;
283         cif->class_version = 1;
284
285         ib_set_cpi_resp_time(cif, 20);
286         mad->mad_hdr.status = 0;
287 }
288
289 /**
290  * srpt_get_iou - write IOUnitInfo to a management datagram
291  * @mad: Datagram that will be sent as response to DM_ATTR_IOU_INFO.
292  *
293  * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
294  * Specification. See also section B.7, table B.6 in the SRP r16a document.
295  */
296 static void srpt_get_iou(struct ib_dm_mad *mad)
297 {
298         struct ib_dm_iou_info *ioui;
299         u8 slot;
300         int i;
301
302         ioui = (struct ib_dm_iou_info *)mad->data;
303         ioui->change_id = cpu_to_be16(1);
304         ioui->max_controllers = 16;
305
306         /* set present for slot 1 and empty for the rest */
307         srpt_set_ioc(ioui->controller_list, 1, 1);
308         for (i = 1, slot = 2; i < 16; i++, slot++)
309                 srpt_set_ioc(ioui->controller_list, slot, 0);
310
311         mad->mad_hdr.status = 0;
312 }
313
314 /**
315  * srpt_get_ioc - write IOControllerprofile to a management datagram
316  * @sport: HCA port through which the MAD has been received.
317  * @slot: Slot number specified in DM_ATTR_IOC_PROFILE query.
318  * @mad: Datagram that will be sent as response to DM_ATTR_IOC_PROFILE.
319  *
320  * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
321  * Architecture Specification. See also section B.7, table B.7 in the SRP
322  * r16a document.
323  */
324 static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
325                          struct ib_dm_mad *mad)
326 {
327         struct srpt_device *sdev = sport->sdev;
328         struct ib_dm_ioc_profile *iocp;
329         int send_queue_depth;
330
331         iocp = (struct ib_dm_ioc_profile *)mad->data;
332
333         if (!slot || slot > 16) {
334                 mad->mad_hdr.status
335                         = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
336                 return;
337         }
338
339         if (slot > 2) {
340                 mad->mad_hdr.status
341                         = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
342                 return;
343         }
344
345         if (sdev->use_srq)
346                 send_queue_depth = sdev->srq_size;
347         else
348                 send_queue_depth = min(MAX_SRPT_RQ_SIZE,
349                                        sdev->device->attrs.max_qp_wr);
350
351         memset(iocp, 0, sizeof(*iocp));
352         strcpy(iocp->id_string, SRPT_ID_STRING);
353         iocp->guid = cpu_to_be64(srpt_service_guid);
354         iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
355         iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
356         iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
357         iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
358         iocp->subsys_device_id = 0x0;
359         iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
360         iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
361         iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
362         iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
363         iocp->send_queue_depth = cpu_to_be16(send_queue_depth);
364         iocp->rdma_read_depth = 4;
365         iocp->send_size = cpu_to_be32(srp_max_req_size);
366         iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
367                                           1U << 24));
368         iocp->num_svc_entries = 1;
369         iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
370                 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
371
372         mad->mad_hdr.status = 0;
373 }
374
375 /**
376  * srpt_get_svc_entries - write ServiceEntries to a management datagram
377  * @ioc_guid: I/O controller GUID to use in reply.
378  * @slot: I/O controller number.
379  * @hi: End of the range of service entries to be specified in the reply.
380  * @lo: Start of the range of service entries to be specified in the reply..
381  * @mad: Datagram that will be sent as response to DM_ATTR_SVC_ENTRIES.
382  *
383  * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
384  * Specification. See also section B.7, table B.8 in the SRP r16a document.
385  */
386 static void srpt_get_svc_entries(u64 ioc_guid,
387                                  u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
388 {
389         struct ib_dm_svc_entries *svc_entries;
390
391         WARN_ON(!ioc_guid);
392
393         if (!slot || slot > 16) {
394                 mad->mad_hdr.status
395                         = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
396                 return;
397         }
398
399         if (slot > 2 || lo > hi || hi > 1) {
400                 mad->mad_hdr.status
401                         = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
402                 return;
403         }
404
405         svc_entries = (struct ib_dm_svc_entries *)mad->data;
406         memset(svc_entries, 0, sizeof(*svc_entries));
407         svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
408         snprintf(svc_entries->service_entries[0].name,
409                  sizeof(svc_entries->service_entries[0].name),
410                  "%s%016llx",
411                  SRP_SERVICE_NAME_PREFIX,
412                  ioc_guid);
413
414         mad->mad_hdr.status = 0;
415 }
416
417 /**
418  * srpt_mgmt_method_get - process a received management datagram
419  * @sp:      HCA port through which the MAD has been received.
420  * @rq_mad:  received MAD.
421  * @rsp_mad: response MAD.
422  */
423 static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
424                                  struct ib_dm_mad *rsp_mad)
425 {
426         u16 attr_id;
427         u32 slot;
428         u8 hi, lo;
429
430         attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
431         switch (attr_id) {
432         case DM_ATTR_CLASS_PORT_INFO:
433                 srpt_get_class_port_info(rsp_mad);
434                 break;
435         case DM_ATTR_IOU_INFO:
436                 srpt_get_iou(rsp_mad);
437                 break;
438         case DM_ATTR_IOC_PROFILE:
439                 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
440                 srpt_get_ioc(sp, slot, rsp_mad);
441                 break;
442         case DM_ATTR_SVC_ENTRIES:
443                 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
444                 hi = (u8) ((slot >> 8) & 0xff);
445                 lo = (u8) (slot & 0xff);
446                 slot = (u16) ((slot >> 16) & 0xffff);
447                 srpt_get_svc_entries(srpt_service_guid,
448                                      slot, hi, lo, rsp_mad);
449                 break;
450         default:
451                 rsp_mad->mad_hdr.status =
452                     cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
453                 break;
454         }
455 }
456
457 /**
458  * srpt_mad_send_handler - MAD send completion callback
459  * @mad_agent: Return value of ib_register_mad_agent().
460  * @mad_wc: Work completion reporting that the MAD has been sent.
461  */
462 static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
463                                   struct ib_mad_send_wc *mad_wc)
464 {
465         rdma_destroy_ah(mad_wc->send_buf->ah);
466         ib_free_send_mad(mad_wc->send_buf);
467 }
468
469 /**
470  * srpt_mad_recv_handler - MAD reception callback function
471  * @mad_agent: Return value of ib_register_mad_agent().
472  * @send_buf: Not used.
473  * @mad_wc: Work completion reporting that a MAD has been received.
474  */
475 static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
476                                   struct ib_mad_send_buf *send_buf,
477                                   struct ib_mad_recv_wc *mad_wc)
478 {
479         struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
480         struct ib_ah *ah;
481         struct ib_mad_send_buf *rsp;
482         struct ib_dm_mad *dm_mad;
483
484         if (!mad_wc || !mad_wc->recv_buf.mad)
485                 return;
486
487         ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
488                                   mad_wc->recv_buf.grh, mad_agent->port_num);
489         if (IS_ERR(ah))
490                 goto err;
491
492         BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
493
494         rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
495                                  mad_wc->wc->pkey_index, 0,
496                                  IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
497                                  GFP_KERNEL,
498                                  IB_MGMT_BASE_VERSION);
499         if (IS_ERR(rsp))
500                 goto err_rsp;
501
502         rsp->ah = ah;
503
504         dm_mad = rsp->mad;
505         memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
506         dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
507         dm_mad->mad_hdr.status = 0;
508
509         switch (mad_wc->recv_buf.mad->mad_hdr.method) {
510         case IB_MGMT_METHOD_GET:
511                 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
512                 break;
513         case IB_MGMT_METHOD_SET:
514                 dm_mad->mad_hdr.status =
515                     cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
516                 break;
517         default:
518                 dm_mad->mad_hdr.status =
519                     cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
520                 break;
521         }
522
523         if (!ib_post_send_mad(rsp, NULL)) {
524                 ib_free_recv_mad(mad_wc);
525                 /* will destroy_ah & free_send_mad in send completion */
526                 return;
527         }
528
529         ib_free_send_mad(rsp);
530
531 err_rsp:
532         rdma_destroy_ah(ah);
533 err:
534         ib_free_recv_mad(mad_wc);
535 }
536
537 static int srpt_format_guid(char *buf, unsigned int size, const __be64 *guid)
538 {
539         const __be16 *g = (const __be16 *)guid;
540
541         return snprintf(buf, size, "%04x:%04x:%04x:%04x",
542                         be16_to_cpu(g[0]), be16_to_cpu(g[1]),
543                         be16_to_cpu(g[2]), be16_to_cpu(g[3]));
544 }
545
546 /**
547  * srpt_refresh_port - configure a HCA port
548  * @sport: SRPT HCA port.
549  *
550  * Enable InfiniBand management datagram processing, update the cached sm_lid,
551  * lid and gid values, and register a callback function for processing MADs
552  * on the specified port.
553  *
554  * Note: It is safe to call this function more than once for the same port.
555  */
556 static int srpt_refresh_port(struct srpt_port *sport)
557 {
558         struct ib_mad_reg_req reg_req;
559         struct ib_port_modify port_modify;
560         struct ib_port_attr port_attr;
561         int ret;
562
563         memset(&port_modify, 0, sizeof(port_modify));
564         port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
565         port_modify.clr_port_cap_mask = 0;
566
567         ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
568         if (ret)
569                 goto err_mod_port;
570
571         ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
572         if (ret)
573                 goto err_query_port;
574
575         sport->sm_lid = port_attr.sm_lid;
576         sport->lid = port_attr.lid;
577
578         ret = rdma_query_gid(sport->sdev->device, sport->port, 0, &sport->gid);
579         if (ret)
580                 goto err_query_port;
581
582         sport->port_guid_wwn.priv = sport;
583         srpt_format_guid(sport->port_guid, sizeof(sport->port_guid),
584                          &sport->gid.global.interface_id);
585         sport->port_gid_wwn.priv = sport;
586         snprintf(sport->port_gid, sizeof(sport->port_gid),
587                  "0x%016llx%016llx",
588                  be64_to_cpu(sport->gid.global.subnet_prefix),
589                  be64_to_cpu(sport->gid.global.interface_id));
590
591         if (!sport->mad_agent) {
592                 memset(&reg_req, 0, sizeof(reg_req));
593                 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
594                 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
595                 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
596                 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
597
598                 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
599                                                          sport->port,
600                                                          IB_QPT_GSI,
601                                                          &reg_req, 0,
602                                                          srpt_mad_send_handler,
603                                                          srpt_mad_recv_handler,
604                                                          sport, 0);
605                 if (IS_ERR(sport->mad_agent)) {
606                         ret = PTR_ERR(sport->mad_agent);
607                         sport->mad_agent = NULL;
608                         goto err_query_port;
609                 }
610         }
611
612         return 0;
613
614 err_query_port:
615
616         port_modify.set_port_cap_mask = 0;
617         port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
618         ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
619
620 err_mod_port:
621
622         return ret;
623 }
624
625 /**
626  * srpt_unregister_mad_agent - unregister MAD callback functions
627  * @sdev: SRPT HCA pointer.
628  *
629  * Note: It is safe to call this function more than once for the same device.
630  */
631 static void srpt_unregister_mad_agent(struct srpt_device *sdev)
632 {
633         struct ib_port_modify port_modify = {
634                 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
635         };
636         struct srpt_port *sport;
637         int i;
638
639         for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
640                 sport = &sdev->port[i - 1];
641                 WARN_ON(sport->port != i);
642                 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
643                         pr_err("disabling MAD processing failed.\n");
644                 if (sport->mad_agent) {
645                         ib_unregister_mad_agent(sport->mad_agent);
646                         sport->mad_agent = NULL;
647                 }
648         }
649 }
650
651 /**
652  * srpt_alloc_ioctx - allocate a SRPT I/O context structure
653  * @sdev: SRPT HCA pointer.
654  * @ioctx_size: I/O context size.
655  * @dma_size: Size of I/O context DMA buffer.
656  * @dir: DMA data direction.
657  */
658 static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
659                                            int ioctx_size, int dma_size,
660                                            enum dma_data_direction dir)
661 {
662         struct srpt_ioctx *ioctx;
663
664         ioctx = kmalloc(ioctx_size, GFP_KERNEL);
665         if (!ioctx)
666                 goto err;
667
668         ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
669         if (!ioctx->buf)
670                 goto err_free_ioctx;
671
672         ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
673         if (ib_dma_mapping_error(sdev->device, ioctx->dma))
674                 goto err_free_buf;
675
676         return ioctx;
677
678 err_free_buf:
679         kfree(ioctx->buf);
680 err_free_ioctx:
681         kfree(ioctx);
682 err:
683         return NULL;
684 }
685
686 /**
687  * srpt_free_ioctx - free a SRPT I/O context structure
688  * @sdev: SRPT HCA pointer.
689  * @ioctx: I/O context pointer.
690  * @dma_size: Size of I/O context DMA buffer.
691  * @dir: DMA data direction.
692  */
693 static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
694                             int dma_size, enum dma_data_direction dir)
695 {
696         if (!ioctx)
697                 return;
698
699         ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
700         kfree(ioctx->buf);
701         kfree(ioctx);
702 }
703
704 /**
705  * srpt_alloc_ioctx_ring - allocate a ring of SRPT I/O context structures
706  * @sdev:       Device to allocate the I/O context ring for.
707  * @ring_size:  Number of elements in the I/O context ring.
708  * @ioctx_size: I/O context size.
709  * @dma_size:   DMA buffer size.
710  * @dir:        DMA data direction.
711  */
712 static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
713                                 int ring_size, int ioctx_size,
714                                 int dma_size, enum dma_data_direction dir)
715 {
716         struct srpt_ioctx **ring;
717         int i;
718
719         WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
720                 && ioctx_size != sizeof(struct srpt_send_ioctx));
721
722         ring = kvmalloc_array(ring_size, sizeof(ring[0]), GFP_KERNEL);
723         if (!ring)
724                 goto out;
725         for (i = 0; i < ring_size; ++i) {
726                 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
727                 if (!ring[i])
728                         goto err;
729                 ring[i]->index = i;
730         }
731         goto out;
732
733 err:
734         while (--i >= 0)
735                 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
736         kvfree(ring);
737         ring = NULL;
738 out:
739         return ring;
740 }
741
742 /**
743  * srpt_free_ioctx_ring - free the ring of SRPT I/O context structures
744  * @ioctx_ring: I/O context ring to be freed.
745  * @sdev: SRPT HCA pointer.
746  * @ring_size: Number of ring elements.
747  * @dma_size: Size of I/O context DMA buffer.
748  * @dir: DMA data direction.
749  */
750 static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
751                                  struct srpt_device *sdev, int ring_size,
752                                  int dma_size, enum dma_data_direction dir)
753 {
754         int i;
755
756         if (!ioctx_ring)
757                 return;
758
759         for (i = 0; i < ring_size; ++i)
760                 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
761         kvfree(ioctx_ring);
762 }
763
764 /**
765  * srpt_set_cmd_state - set the state of a SCSI command
766  * @ioctx: Send I/O context.
767  * @new: New I/O context state.
768  *
769  * Does not modify the state of aborted commands. Returns the previous command
770  * state.
771  */
772 static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
773                                                   enum srpt_command_state new)
774 {
775         enum srpt_command_state previous;
776
777         previous = ioctx->state;
778         if (previous != SRPT_STATE_DONE)
779                 ioctx->state = new;
780
781         return previous;
782 }
783
784 /**
785  * srpt_test_and_set_cmd_state - test and set the state of a command
786  * @ioctx: Send I/O context.
787  * @old: Current I/O context state.
788  * @new: New I/O context state.
789  *
790  * Returns true if and only if the previous command state was equal to 'old'.
791  */
792 static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
793                                         enum srpt_command_state old,
794                                         enum srpt_command_state new)
795 {
796         enum srpt_command_state previous;
797
798         WARN_ON(!ioctx);
799         WARN_ON(old == SRPT_STATE_DONE);
800         WARN_ON(new == SRPT_STATE_NEW);
801
802         previous = ioctx->state;
803         if (previous == old)
804                 ioctx->state = new;
805
806         return previous == old;
807 }
808
809 /**
810  * srpt_post_recv - post an IB receive request
811  * @sdev: SRPT HCA pointer.
812  * @ch: SRPT RDMA channel.
813  * @ioctx: Receive I/O context pointer.
814  */
815 static int srpt_post_recv(struct srpt_device *sdev, struct srpt_rdma_ch *ch,
816                           struct srpt_recv_ioctx *ioctx)
817 {
818         struct ib_sge list;
819         struct ib_recv_wr wr;
820
821         BUG_ON(!sdev);
822         list.addr = ioctx->ioctx.dma;
823         list.length = srp_max_req_size;
824         list.lkey = sdev->lkey;
825
826         ioctx->ioctx.cqe.done = srpt_recv_done;
827         wr.wr_cqe = &ioctx->ioctx.cqe;
828         wr.next = NULL;
829         wr.sg_list = &list;
830         wr.num_sge = 1;
831
832         if (sdev->use_srq)
833                 return ib_post_srq_recv(sdev->srq, &wr, NULL);
834         else
835                 return ib_post_recv(ch->qp, &wr, NULL);
836 }
837
838 /**
839  * srpt_zerolength_write - perform a zero-length RDMA write
840  * @ch: SRPT RDMA channel.
841  *
842  * A quote from the InfiniBand specification: C9-88: For an HCA responder
843  * using Reliable Connection service, for each zero-length RDMA READ or WRITE
844  * request, the R_Key shall not be validated, even if the request includes
845  * Immediate data.
846  */
847 static int srpt_zerolength_write(struct srpt_rdma_ch *ch)
848 {
849         struct ib_rdma_wr wr = {
850                 .wr = {
851                         .next           = NULL,
852                         { .wr_cqe       = &ch->zw_cqe, },
853                         .opcode         = IB_WR_RDMA_WRITE,
854                         .send_flags     = IB_SEND_SIGNALED,
855                 }
856         };
857
858         pr_debug("%s-%d: queued zerolength write\n", ch->sess_name,
859                  ch->qp->qp_num);
860
861         return ib_post_send(ch->qp, &wr.wr, NULL);
862 }
863
864 static void srpt_zerolength_write_done(struct ib_cq *cq, struct ib_wc *wc)
865 {
866         struct srpt_rdma_ch *ch = cq->cq_context;
867
868         pr_debug("%s-%d wc->status %d\n", ch->sess_name, ch->qp->qp_num,
869                  wc->status);
870
871         if (wc->status == IB_WC_SUCCESS) {
872                 srpt_process_wait_list(ch);
873         } else {
874                 if (srpt_set_ch_state(ch, CH_DISCONNECTED))
875                         schedule_work(&ch->release_work);
876                 else
877                         pr_debug("%s-%d: already disconnected.\n",
878                                  ch->sess_name, ch->qp->qp_num);
879         }
880 }
881
882 static int srpt_alloc_rw_ctxs(struct srpt_send_ioctx *ioctx,
883                 struct srp_direct_buf *db, int nbufs, struct scatterlist **sg,
884                 unsigned *sg_cnt)
885 {
886         enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
887         struct srpt_rdma_ch *ch = ioctx->ch;
888         struct scatterlist *prev = NULL;
889         unsigned prev_nents;
890         int ret, i;
891
892         if (nbufs == 1) {
893                 ioctx->rw_ctxs = &ioctx->s_rw_ctx;
894         } else {
895                 ioctx->rw_ctxs = kmalloc_array(nbufs, sizeof(*ioctx->rw_ctxs),
896                         GFP_KERNEL);
897                 if (!ioctx->rw_ctxs)
898                         return -ENOMEM;
899         }
900
901         for (i = ioctx->n_rw_ctx; i < nbufs; i++, db++) {
902                 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
903                 u64 remote_addr = be64_to_cpu(db->va);
904                 u32 size = be32_to_cpu(db->len);
905                 u32 rkey = be32_to_cpu(db->key);
906
907                 ret = target_alloc_sgl(&ctx->sg, &ctx->nents, size, false,
908                                 i < nbufs - 1);
909                 if (ret)
910                         goto unwind;
911
912                 ret = rdma_rw_ctx_init(&ctx->rw, ch->qp, ch->sport->port,
913                                 ctx->sg, ctx->nents, 0, remote_addr, rkey, dir);
914                 if (ret < 0) {
915                         target_free_sgl(ctx->sg, ctx->nents);
916                         goto unwind;
917                 }
918
919                 ioctx->n_rdma += ret;
920                 ioctx->n_rw_ctx++;
921
922                 if (prev) {
923                         sg_unmark_end(&prev[prev_nents - 1]);
924                         sg_chain(prev, prev_nents + 1, ctx->sg);
925                 } else {
926                         *sg = ctx->sg;
927                 }
928
929                 prev = ctx->sg;
930                 prev_nents = ctx->nents;
931
932                 *sg_cnt += ctx->nents;
933         }
934
935         return 0;
936
937 unwind:
938         while (--i >= 0) {
939                 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
940
941                 rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
942                                 ctx->sg, ctx->nents, dir);
943                 target_free_sgl(ctx->sg, ctx->nents);
944         }
945         if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
946                 kfree(ioctx->rw_ctxs);
947         return ret;
948 }
949
950 static void srpt_free_rw_ctxs(struct srpt_rdma_ch *ch,
951                                     struct srpt_send_ioctx *ioctx)
952 {
953         enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
954         int i;
955
956         for (i = 0; i < ioctx->n_rw_ctx; i++) {
957                 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
958
959                 rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
960                                 ctx->sg, ctx->nents, dir);
961                 target_free_sgl(ctx->sg, ctx->nents);
962         }
963
964         if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
965                 kfree(ioctx->rw_ctxs);
966 }
967
968 static inline void *srpt_get_desc_buf(struct srp_cmd *srp_cmd)
969 {
970         /*
971          * The pointer computations below will only be compiled correctly
972          * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
973          * whether srp_cmd::add_data has been declared as a byte pointer.
974          */
975         BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0) &&
976                      !__same_type(srp_cmd->add_data[0], (u8)0));
977
978         /*
979          * According to the SRP spec, the lower two bits of the 'ADDITIONAL
980          * CDB LENGTH' field are reserved and the size in bytes of this field
981          * is four times the value specified in bits 3..7. Hence the "& ~3".
982          */
983         return srp_cmd->add_data + (srp_cmd->add_cdb_len & ~3);
984 }
985
986 /**
987  * srpt_get_desc_tbl - parse the data descriptors of a SRP_CMD request
988  * @ioctx: Pointer to the I/O context associated with the request.
989  * @srp_cmd: Pointer to the SRP_CMD request data.
990  * @dir: Pointer to the variable to which the transfer direction will be
991  *   written.
992  * @sg: [out] scatterlist allocated for the parsed SRP_CMD.
993  * @sg_cnt: [out] length of @sg.
994  * @data_len: Pointer to the variable to which the total data length of all
995  *   descriptors in the SRP_CMD request will be written.
996  *
997  * This function initializes ioctx->nrbuf and ioctx->r_bufs.
998  *
999  * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
1000  * -ENOMEM when memory allocation fails and zero upon success.
1001  */
1002 static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
1003                 struct srp_cmd *srp_cmd, enum dma_data_direction *dir,
1004                 struct scatterlist **sg, unsigned *sg_cnt, u64 *data_len)
1005 {
1006         BUG_ON(!dir);
1007         BUG_ON(!data_len);
1008
1009         /*
1010          * The lower four bits of the buffer format field contain the DATA-IN
1011          * buffer descriptor format, and the highest four bits contain the
1012          * DATA-OUT buffer descriptor format.
1013          */
1014         if (srp_cmd->buf_fmt & 0xf)
1015                 /* DATA-IN: transfer data from target to initiator (read). */
1016                 *dir = DMA_FROM_DEVICE;
1017         else if (srp_cmd->buf_fmt >> 4)
1018                 /* DATA-OUT: transfer data from initiator to target (write). */
1019                 *dir = DMA_TO_DEVICE;
1020         else
1021                 *dir = DMA_NONE;
1022
1023         /* initialize data_direction early as srpt_alloc_rw_ctxs needs it */
1024         ioctx->cmd.data_direction = *dir;
1025
1026         if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
1027             ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
1028                 struct srp_direct_buf *db = srpt_get_desc_buf(srp_cmd);
1029
1030                 *data_len = be32_to_cpu(db->len);
1031                 return srpt_alloc_rw_ctxs(ioctx, db, 1, sg, sg_cnt);
1032         } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
1033                    ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
1034                 struct srp_indirect_buf *idb = srpt_get_desc_buf(srp_cmd);
1035                 int nbufs = be32_to_cpu(idb->table_desc.len) /
1036                                 sizeof(struct srp_direct_buf);
1037
1038                 if (nbufs >
1039                     (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
1040                         pr_err("received unsupported SRP_CMD request"
1041                                " type (%u out + %u in != %u / %zu)\n",
1042                                srp_cmd->data_out_desc_cnt,
1043                                srp_cmd->data_in_desc_cnt,
1044                                be32_to_cpu(idb->table_desc.len),
1045                                sizeof(struct srp_direct_buf));
1046                         return -EINVAL;
1047                 }
1048
1049                 *data_len = be32_to_cpu(idb->len);
1050                 return srpt_alloc_rw_ctxs(ioctx, idb->desc_list, nbufs,
1051                                 sg, sg_cnt);
1052         } else {
1053                 *data_len = 0;
1054                 return 0;
1055         }
1056 }
1057
1058 /**
1059  * srpt_init_ch_qp - initialize queue pair attributes
1060  * @ch: SRPT RDMA channel.
1061  * @qp: Queue pair pointer.
1062  *
1063  * Initialized the attributes of queue pair 'qp' by allowing local write,
1064  * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
1065  */
1066 static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1067 {
1068         struct ib_qp_attr *attr;
1069         int ret;
1070
1071         WARN_ON_ONCE(ch->using_rdma_cm);
1072
1073         attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1074         if (!attr)
1075                 return -ENOMEM;
1076
1077         attr->qp_state = IB_QPS_INIT;
1078         attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE;
1079         attr->port_num = ch->sport->port;
1080
1081         ret = ib_find_cached_pkey(ch->sport->sdev->device, ch->sport->port,
1082                                   ch->pkey, &attr->pkey_index);
1083         if (ret < 0)
1084                 pr_err("Translating pkey %#x failed (%d) - using index 0\n",
1085                        ch->pkey, ret);
1086
1087         ret = ib_modify_qp(qp, attr,
1088                            IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
1089                            IB_QP_PKEY_INDEX);
1090
1091         kfree(attr);
1092         return ret;
1093 }
1094
1095 /**
1096  * srpt_ch_qp_rtr - change the state of a channel to 'ready to receive' (RTR)
1097  * @ch: channel of the queue pair.
1098  * @qp: queue pair to change the state of.
1099  *
1100  * Returns zero upon success and a negative value upon failure.
1101  *
1102  * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1103  * If this structure ever becomes larger, it might be necessary to allocate
1104  * it dynamically instead of on the stack.
1105  */
1106 static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1107 {
1108         struct ib_qp_attr qp_attr;
1109         int attr_mask;
1110         int ret;
1111
1112         WARN_ON_ONCE(ch->using_rdma_cm);
1113
1114         qp_attr.qp_state = IB_QPS_RTR;
1115         ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask);
1116         if (ret)
1117                 goto out;
1118
1119         qp_attr.max_dest_rd_atomic = 4;
1120
1121         ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1122
1123 out:
1124         return ret;
1125 }
1126
1127 /**
1128  * srpt_ch_qp_rts - change the state of a channel to 'ready to send' (RTS)
1129  * @ch: channel of the queue pair.
1130  * @qp: queue pair to change the state of.
1131  *
1132  * Returns zero upon success and a negative value upon failure.
1133  *
1134  * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1135  * If this structure ever becomes larger, it might be necessary to allocate
1136  * it dynamically instead of on the stack.
1137  */
1138 static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1139 {
1140         struct ib_qp_attr qp_attr;
1141         int attr_mask;
1142         int ret;
1143
1144         qp_attr.qp_state = IB_QPS_RTS;
1145         ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask);
1146         if (ret)
1147                 goto out;
1148
1149         qp_attr.max_rd_atomic = 4;
1150
1151         ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1152
1153 out:
1154         return ret;
1155 }
1156
1157 /**
1158  * srpt_ch_qp_err - set the channel queue pair state to 'error'
1159  * @ch: SRPT RDMA channel.
1160  */
1161 static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1162 {
1163         struct ib_qp_attr qp_attr;
1164
1165         qp_attr.qp_state = IB_QPS_ERR;
1166         return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1167 }
1168
1169 /**
1170  * srpt_get_send_ioctx - obtain an I/O context for sending to the initiator
1171  * @ch: SRPT RDMA channel.
1172  */
1173 static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1174 {
1175         struct srpt_send_ioctx *ioctx;
1176         unsigned long flags;
1177
1178         BUG_ON(!ch);
1179
1180         ioctx = NULL;
1181         spin_lock_irqsave(&ch->spinlock, flags);
1182         if (!list_empty(&ch->free_list)) {
1183                 ioctx = list_first_entry(&ch->free_list,
1184                                          struct srpt_send_ioctx, free_list);
1185                 list_del(&ioctx->free_list);
1186         }
1187         spin_unlock_irqrestore(&ch->spinlock, flags);
1188
1189         if (!ioctx)
1190                 return ioctx;
1191
1192         BUG_ON(ioctx->ch != ch);
1193         ioctx->state = SRPT_STATE_NEW;
1194         ioctx->n_rdma = 0;
1195         ioctx->n_rw_ctx = 0;
1196         ioctx->queue_status_only = false;
1197         /*
1198          * transport_init_se_cmd() does not initialize all fields, so do it
1199          * here.
1200          */
1201         memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1202         memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1203
1204         return ioctx;
1205 }
1206
1207 /**
1208  * srpt_abort_cmd - abort a SCSI command
1209  * @ioctx:   I/O context associated with the SCSI command.
1210  */
1211 static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1212 {
1213         enum srpt_command_state state;
1214
1215         BUG_ON(!ioctx);
1216
1217         /*
1218          * If the command is in a state where the target core is waiting for
1219          * the ib_srpt driver, change the state to the next state.
1220          */
1221
1222         state = ioctx->state;
1223         switch (state) {
1224         case SRPT_STATE_NEED_DATA:
1225                 ioctx->state = SRPT_STATE_DATA_IN;
1226                 break;
1227         case SRPT_STATE_CMD_RSP_SENT:
1228         case SRPT_STATE_MGMT_RSP_SENT:
1229                 ioctx->state = SRPT_STATE_DONE;
1230                 break;
1231         default:
1232                 WARN_ONCE(true, "%s: unexpected I/O context state %d\n",
1233                           __func__, state);
1234                 break;
1235         }
1236
1237         pr_debug("Aborting cmd with state %d -> %d and tag %lld\n", state,
1238                  ioctx->state, ioctx->cmd.tag);
1239
1240         switch (state) {
1241         case SRPT_STATE_NEW:
1242         case SRPT_STATE_DATA_IN:
1243         case SRPT_STATE_MGMT:
1244         case SRPT_STATE_DONE:
1245                 /*
1246                  * Do nothing - defer abort processing until
1247                  * srpt_queue_response() is invoked.
1248                  */
1249                 break;
1250         case SRPT_STATE_NEED_DATA:
1251                 pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag);
1252                 transport_generic_request_failure(&ioctx->cmd,
1253                                         TCM_CHECK_CONDITION_ABORT_CMD);
1254                 break;
1255         case SRPT_STATE_CMD_RSP_SENT:
1256                 /*
1257                  * SRP_RSP sending failed or the SRP_RSP send completion has
1258                  * not been received in time.
1259                  */
1260                 transport_generic_free_cmd(&ioctx->cmd, 0);
1261                 break;
1262         case SRPT_STATE_MGMT_RSP_SENT:
1263                 transport_generic_free_cmd(&ioctx->cmd, 0);
1264                 break;
1265         default:
1266                 WARN(1, "Unexpected command state (%d)", state);
1267                 break;
1268         }
1269
1270         return state;
1271 }
1272
1273 /**
1274  * srpt_rdma_read_done - RDMA read completion callback
1275  * @cq: Completion queue.
1276  * @wc: Work completion.
1277  *
1278  * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1279  * the data that has been transferred via IB RDMA had to be postponed until the
1280  * check_stop_free() callback.  None of this is necessary anymore and needs to
1281  * be cleaned up.
1282  */
1283 static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
1284 {
1285         struct srpt_rdma_ch *ch = cq->cq_context;
1286         struct srpt_send_ioctx *ioctx =
1287                 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
1288
1289         WARN_ON(ioctx->n_rdma <= 0);
1290         atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1291         ioctx->n_rdma = 0;
1292
1293         if (unlikely(wc->status != IB_WC_SUCCESS)) {
1294                 pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
1295                         ioctx, wc->status);
1296                 srpt_abort_cmd(ioctx);
1297                 return;
1298         }
1299
1300         if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1301                                         SRPT_STATE_DATA_IN))
1302                 target_execute_cmd(&ioctx->cmd);
1303         else
1304                 pr_err("%s[%d]: wrong state = %d\n", __func__,
1305                        __LINE__, ioctx->state);
1306 }
1307
1308 /**
1309  * srpt_build_cmd_rsp - build a SRP_RSP response
1310  * @ch: RDMA channel through which the request has been received.
1311  * @ioctx: I/O context associated with the SRP_CMD request. The response will
1312  *   be built in the buffer ioctx->buf points at and hence this function will
1313  *   overwrite the request data.
1314  * @tag: tag of the request for which this response is being generated.
1315  * @status: value for the STATUS field of the SRP_RSP information unit.
1316  *
1317  * Returns the size in bytes of the SRP_RSP response.
1318  *
1319  * An SRP_RSP response contains a SCSI status or service response. See also
1320  * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1321  * response. See also SPC-2 for more information about sense data.
1322  */
1323 static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1324                               struct srpt_send_ioctx *ioctx, u64 tag,
1325                               int status)
1326 {
1327         struct se_cmd *cmd = &ioctx->cmd;
1328         struct srp_rsp *srp_rsp;
1329         const u8 *sense_data;
1330         int sense_data_len, max_sense_len;
1331         u32 resid = cmd->residual_count;
1332
1333         /*
1334          * The lowest bit of all SAM-3 status codes is zero (see also
1335          * paragraph 5.3 in SAM-3).
1336          */
1337         WARN_ON(status & 1);
1338
1339         srp_rsp = ioctx->ioctx.buf;
1340         BUG_ON(!srp_rsp);
1341
1342         sense_data = ioctx->sense_data;
1343         sense_data_len = ioctx->cmd.scsi_sense_length;
1344         WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1345
1346         memset(srp_rsp, 0, sizeof(*srp_rsp));
1347         srp_rsp->opcode = SRP_RSP;
1348         srp_rsp->req_lim_delta =
1349                 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1350         srp_rsp->tag = tag;
1351         srp_rsp->status = status;
1352
1353         if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
1354                 if (cmd->data_direction == DMA_TO_DEVICE) {
1355                         /* residual data from an underflow write */
1356                         srp_rsp->flags = SRP_RSP_FLAG_DOUNDER;
1357                         srp_rsp->data_out_res_cnt = cpu_to_be32(resid);
1358                 } else if (cmd->data_direction == DMA_FROM_DEVICE) {
1359                         /* residual data from an underflow read */
1360                         srp_rsp->flags = SRP_RSP_FLAG_DIUNDER;
1361                         srp_rsp->data_in_res_cnt = cpu_to_be32(resid);
1362                 }
1363         } else if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
1364                 if (cmd->data_direction == DMA_TO_DEVICE) {
1365                         /* residual data from an overflow write */
1366                         srp_rsp->flags = SRP_RSP_FLAG_DOOVER;
1367                         srp_rsp->data_out_res_cnt = cpu_to_be32(resid);
1368                 } else if (cmd->data_direction == DMA_FROM_DEVICE) {
1369                         /* residual data from an overflow read */
1370                         srp_rsp->flags = SRP_RSP_FLAG_DIOVER;
1371                         srp_rsp->data_in_res_cnt = cpu_to_be32(resid);
1372                 }
1373         }
1374
1375         if (sense_data_len) {
1376                 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1377                 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1378                 if (sense_data_len > max_sense_len) {
1379                         pr_warn("truncated sense data from %d to %d"
1380                                 " bytes\n", sense_data_len, max_sense_len);
1381                         sense_data_len = max_sense_len;
1382                 }
1383
1384                 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1385                 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1386                 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1387         }
1388
1389         return sizeof(*srp_rsp) + sense_data_len;
1390 }
1391
1392 /**
1393  * srpt_build_tskmgmt_rsp - build a task management response
1394  * @ch:       RDMA channel through which the request has been received.
1395  * @ioctx:    I/O context in which the SRP_RSP response will be built.
1396  * @rsp_code: RSP_CODE that will be stored in the response.
1397  * @tag:      Tag of the request for which this response is being generated.
1398  *
1399  * Returns the size in bytes of the SRP_RSP response.
1400  *
1401  * An SRP_RSP response contains a SCSI status or service response. See also
1402  * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1403  * response.
1404  */
1405 static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1406                                   struct srpt_send_ioctx *ioctx,
1407                                   u8 rsp_code, u64 tag)
1408 {
1409         struct srp_rsp *srp_rsp;
1410         int resp_data_len;
1411         int resp_len;
1412
1413         resp_data_len = 4;
1414         resp_len = sizeof(*srp_rsp) + resp_data_len;
1415
1416         srp_rsp = ioctx->ioctx.buf;
1417         BUG_ON(!srp_rsp);
1418         memset(srp_rsp, 0, sizeof(*srp_rsp));
1419
1420         srp_rsp->opcode = SRP_RSP;
1421         srp_rsp->req_lim_delta =
1422                 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1423         srp_rsp->tag = tag;
1424
1425         srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1426         srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1427         srp_rsp->data[3] = rsp_code;
1428
1429         return resp_len;
1430 }
1431
1432 static int srpt_check_stop_free(struct se_cmd *cmd)
1433 {
1434         struct srpt_send_ioctx *ioctx = container_of(cmd,
1435                                 struct srpt_send_ioctx, cmd);
1436
1437         return target_put_sess_cmd(&ioctx->cmd);
1438 }
1439
1440 /**
1441  * srpt_handle_cmd - process a SRP_CMD information unit
1442  * @ch: SRPT RDMA channel.
1443  * @recv_ioctx: Receive I/O context.
1444  * @send_ioctx: Send I/O context.
1445  */
1446 static void srpt_handle_cmd(struct srpt_rdma_ch *ch,
1447                             struct srpt_recv_ioctx *recv_ioctx,
1448                             struct srpt_send_ioctx *send_ioctx)
1449 {
1450         struct se_cmd *cmd;
1451         struct srp_cmd *srp_cmd;
1452         struct scatterlist *sg = NULL;
1453         unsigned sg_cnt = 0;
1454         u64 data_len;
1455         enum dma_data_direction dir;
1456         int rc;
1457
1458         BUG_ON(!send_ioctx);
1459
1460         srp_cmd = recv_ioctx->ioctx.buf;
1461         cmd = &send_ioctx->cmd;
1462         cmd->tag = srp_cmd->tag;
1463
1464         switch (srp_cmd->task_attr) {
1465         case SRP_CMD_SIMPLE_Q:
1466                 cmd->sam_task_attr = TCM_SIMPLE_TAG;
1467                 break;
1468         case SRP_CMD_ORDERED_Q:
1469         default:
1470                 cmd->sam_task_attr = TCM_ORDERED_TAG;
1471                 break;
1472         case SRP_CMD_HEAD_OF_Q:
1473                 cmd->sam_task_attr = TCM_HEAD_TAG;
1474                 break;
1475         case SRP_CMD_ACA:
1476                 cmd->sam_task_attr = TCM_ACA_TAG;
1477                 break;
1478         }
1479
1480         rc = srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &sg, &sg_cnt,
1481                         &data_len);
1482         if (rc) {
1483                 if (rc != -EAGAIN) {
1484                         pr_err("0x%llx: parsing SRP descriptor table failed.\n",
1485                                srp_cmd->tag);
1486                 }
1487                 goto release_ioctx;
1488         }
1489
1490         rc = target_submit_cmd_map_sgls(cmd, ch->sess, srp_cmd->cdb,
1491                                &send_ioctx->sense_data[0],
1492                                scsilun_to_int(&srp_cmd->lun), data_len,
1493                                TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF,
1494                                sg, sg_cnt, NULL, 0, NULL, 0);
1495         if (rc != 0) {
1496                 pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc,
1497                          srp_cmd->tag);
1498                 goto release_ioctx;
1499         }
1500         return;
1501
1502 release_ioctx:
1503         send_ioctx->state = SRPT_STATE_DONE;
1504         srpt_release_cmd(cmd);
1505 }
1506
1507 static int srp_tmr_to_tcm(int fn)
1508 {
1509         switch (fn) {
1510         case SRP_TSK_ABORT_TASK:
1511                 return TMR_ABORT_TASK;
1512         case SRP_TSK_ABORT_TASK_SET:
1513                 return TMR_ABORT_TASK_SET;
1514         case SRP_TSK_CLEAR_TASK_SET:
1515                 return TMR_CLEAR_TASK_SET;
1516         case SRP_TSK_LUN_RESET:
1517                 return TMR_LUN_RESET;
1518         case SRP_TSK_CLEAR_ACA:
1519                 return TMR_CLEAR_ACA;
1520         default:
1521                 return -1;
1522         }
1523 }
1524
1525 /**
1526  * srpt_handle_tsk_mgmt - process a SRP_TSK_MGMT information unit
1527  * @ch: SRPT RDMA channel.
1528  * @recv_ioctx: Receive I/O context.
1529  * @send_ioctx: Send I/O context.
1530  *
1531  * Returns 0 if and only if the request will be processed by the target core.
1532  *
1533  * For more information about SRP_TSK_MGMT information units, see also section
1534  * 6.7 in the SRP r16a document.
1535  */
1536 static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1537                                  struct srpt_recv_ioctx *recv_ioctx,
1538                                  struct srpt_send_ioctx *send_ioctx)
1539 {
1540         struct srp_tsk_mgmt *srp_tsk;
1541         struct se_cmd *cmd;
1542         struct se_session *sess = ch->sess;
1543         int tcm_tmr;
1544         int rc;
1545
1546         BUG_ON(!send_ioctx);
1547
1548         srp_tsk = recv_ioctx->ioctx.buf;
1549         cmd = &send_ioctx->cmd;
1550
1551         pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld ch %p sess %p\n",
1552                  srp_tsk->tsk_mgmt_func, srp_tsk->task_tag, srp_tsk->tag, ch,
1553                  ch->sess);
1554
1555         srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
1556         send_ioctx->cmd.tag = srp_tsk->tag;
1557         tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1558         rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL,
1559                                scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr,
1560                                GFP_KERNEL, srp_tsk->task_tag,
1561                                TARGET_SCF_ACK_KREF);
1562         if (rc != 0) {
1563                 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
1564                 goto fail;
1565         }
1566         return;
1567 fail:
1568         transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
1569 }
1570
1571 /**
1572  * srpt_handle_new_iu - process a newly received information unit
1573  * @ch:    RDMA channel through which the information unit has been received.
1574  * @recv_ioctx: Receive I/O context associated with the information unit.
1575  */
1576 static bool
1577 srpt_handle_new_iu(struct srpt_rdma_ch *ch, struct srpt_recv_ioctx *recv_ioctx)
1578 {
1579         struct srpt_send_ioctx *send_ioctx = NULL;
1580         struct srp_cmd *srp_cmd;
1581         bool res = false;
1582         u8 opcode;
1583
1584         BUG_ON(!ch);
1585         BUG_ON(!recv_ioctx);
1586
1587         if (unlikely(ch->state == CH_CONNECTING))
1588                 goto push;
1589
1590         ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1591                                    recv_ioctx->ioctx.dma, srp_max_req_size,
1592                                    DMA_FROM_DEVICE);
1593
1594         srp_cmd = recv_ioctx->ioctx.buf;
1595         opcode = srp_cmd->opcode;
1596         if (opcode == SRP_CMD || opcode == SRP_TSK_MGMT) {
1597                 send_ioctx = srpt_get_send_ioctx(ch);
1598                 if (unlikely(!send_ioctx))
1599                         goto push;
1600         }
1601
1602         if (!list_empty(&recv_ioctx->wait_list)) {
1603                 WARN_ON_ONCE(!ch->processing_wait_list);
1604                 list_del_init(&recv_ioctx->wait_list);
1605         }
1606
1607         switch (opcode) {
1608         case SRP_CMD:
1609                 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1610                 break;
1611         case SRP_TSK_MGMT:
1612                 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1613                 break;
1614         case SRP_I_LOGOUT:
1615                 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
1616                 break;
1617         case SRP_CRED_RSP:
1618                 pr_debug("received SRP_CRED_RSP\n");
1619                 break;
1620         case SRP_AER_RSP:
1621                 pr_debug("received SRP_AER_RSP\n");
1622                 break;
1623         case SRP_RSP:
1624                 pr_err("Received SRP_RSP\n");
1625                 break;
1626         default:
1627                 pr_err("received IU with unknown opcode 0x%x\n", opcode);
1628                 break;
1629         }
1630
1631         srpt_post_recv(ch->sport->sdev, ch, recv_ioctx);
1632         res = true;
1633
1634 out:
1635         return res;
1636
1637 push:
1638         if (list_empty(&recv_ioctx->wait_list)) {
1639                 WARN_ON_ONCE(ch->processing_wait_list);
1640                 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1641         }
1642         goto out;
1643 }
1644
1645 static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1646 {
1647         struct srpt_rdma_ch *ch = cq->cq_context;
1648         struct srpt_recv_ioctx *ioctx =
1649                 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
1650
1651         if (wc->status == IB_WC_SUCCESS) {
1652                 int req_lim;
1653
1654                 req_lim = atomic_dec_return(&ch->req_lim);
1655                 if (unlikely(req_lim < 0))
1656                         pr_err("req_lim = %d < 0\n", req_lim);
1657                 srpt_handle_new_iu(ch, ioctx);
1658         } else {
1659                 pr_info_ratelimited("receiving failed for ioctx %p with status %d\n",
1660                                     ioctx, wc->status);
1661         }
1662 }
1663
1664 /*
1665  * This function must be called from the context in which RDMA completions are
1666  * processed because it accesses the wait list without protection against
1667  * access from other threads.
1668  */
1669 static void srpt_process_wait_list(struct srpt_rdma_ch *ch)
1670 {
1671         struct srpt_recv_ioctx *recv_ioctx, *tmp;
1672
1673         WARN_ON_ONCE(ch->state == CH_CONNECTING);
1674
1675         if (list_empty(&ch->cmd_wait_list))
1676                 return;
1677
1678         WARN_ON_ONCE(ch->processing_wait_list);
1679         ch->processing_wait_list = true;
1680         list_for_each_entry_safe(recv_ioctx, tmp, &ch->cmd_wait_list,
1681                                  wait_list) {
1682                 if (!srpt_handle_new_iu(ch, recv_ioctx))
1683                         break;
1684         }
1685         ch->processing_wait_list = false;
1686 }
1687
1688 /**
1689  * srpt_send_done - send completion callback
1690  * @cq: Completion queue.
1691  * @wc: Work completion.
1692  *
1693  * Note: Although this has not yet been observed during tests, at least in
1694  * theory it is possible that the srpt_get_send_ioctx() call invoked by
1695  * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1696  * value in each response is set to one, and it is possible that this response
1697  * makes the initiator send a new request before the send completion for that
1698  * response has been processed. This could e.g. happen if the call to
1699  * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1700  * if IB retransmission causes generation of the send completion to be
1701  * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1702  * are queued on cmd_wait_list. The code below processes these delayed
1703  * requests one at a time.
1704  */
1705 static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
1706 {
1707         struct srpt_rdma_ch *ch = cq->cq_context;
1708         struct srpt_send_ioctx *ioctx =
1709                 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1710         enum srpt_command_state state;
1711
1712         state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1713
1714         WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1715                 state != SRPT_STATE_MGMT_RSP_SENT);
1716
1717         atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
1718
1719         if (wc->status != IB_WC_SUCCESS)
1720                 pr_info("sending response for ioctx 0x%p failed"
1721                         " with status %d\n", ioctx, wc->status);
1722
1723         if (state != SRPT_STATE_DONE) {
1724                 transport_generic_free_cmd(&ioctx->cmd, 0);
1725         } else {
1726                 pr_err("IB completion has been received too late for"
1727                        " wr_id = %u.\n", ioctx->ioctx.index);
1728         }
1729
1730         srpt_process_wait_list(ch);
1731 }
1732
1733 /**
1734  * srpt_create_ch_ib - create receive and send completion queues
1735  * @ch: SRPT RDMA channel.
1736  */
1737 static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1738 {
1739         struct ib_qp_init_attr *qp_init;
1740         struct srpt_port *sport = ch->sport;
1741         struct srpt_device *sdev = sport->sdev;
1742         const struct ib_device_attr *attrs = &sdev->device->attrs;
1743         int sq_size = sport->port_attrib.srp_sq_size;
1744         int i, ret;
1745
1746         WARN_ON(ch->rq_size < 1);
1747
1748         ret = -ENOMEM;
1749         qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
1750         if (!qp_init)
1751                 goto out;
1752
1753 retry:
1754         ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + sq_size,
1755                         0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
1756         if (IS_ERR(ch->cq)) {
1757                 ret = PTR_ERR(ch->cq);
1758                 pr_err("failed to create CQ cqe= %d ret= %d\n",
1759                        ch->rq_size + sq_size, ret);
1760                 goto out;
1761         }
1762
1763         qp_init->qp_context = (void *)ch;
1764         qp_init->event_handler
1765                 = (void(*)(struct ib_event *, void*))srpt_qp_event;
1766         qp_init->send_cq = ch->cq;
1767         qp_init->recv_cq = ch->cq;
1768         qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1769         qp_init->qp_type = IB_QPT_RC;
1770         /*
1771          * We divide up our send queue size into half SEND WRs to send the
1772          * completions, and half R/W contexts to actually do the RDMA
1773          * READ/WRITE transfers.  Note that we need to allocate CQ slots for
1774          * both both, as RDMA contexts will also post completions for the
1775          * RDMA READ case.
1776          */
1777         qp_init->cap.max_send_wr = min(sq_size / 2, attrs->max_qp_wr);
1778         qp_init->cap.max_rdma_ctxs = sq_size / 2;
1779         qp_init->cap.max_send_sge = min(attrs->max_send_sge,
1780                                         SRPT_MAX_SG_PER_WQE);
1781         qp_init->port_num = ch->sport->port;
1782         if (sdev->use_srq) {
1783                 qp_init->srq = sdev->srq;
1784         } else {
1785                 qp_init->cap.max_recv_wr = ch->rq_size;
1786                 qp_init->cap.max_recv_sge = min(attrs->max_recv_sge,
1787                                                 SRPT_MAX_SG_PER_WQE);
1788         }
1789
1790         if (ch->using_rdma_cm) {
1791                 ret = rdma_create_qp(ch->rdma_cm.cm_id, sdev->pd, qp_init);
1792                 ch->qp = ch->rdma_cm.cm_id->qp;
1793         } else {
1794                 ch->qp = ib_create_qp(sdev->pd, qp_init);
1795                 if (!IS_ERR(ch->qp)) {
1796                         ret = srpt_init_ch_qp(ch, ch->qp);
1797                         if (ret)
1798                                 ib_destroy_qp(ch->qp);
1799                 } else {
1800                         ret = PTR_ERR(ch->qp);
1801                 }
1802         }
1803         if (ret) {
1804                 bool retry = sq_size > MIN_SRPT_SQ_SIZE;
1805
1806                 if (retry) {
1807                         pr_debug("failed to create queue pair with sq_size = %d (%d) - retrying\n",
1808                                  sq_size, ret);
1809                         ib_free_cq(ch->cq);
1810                         sq_size = max(sq_size / 2, MIN_SRPT_SQ_SIZE);
1811                         goto retry;
1812                 } else {
1813                         pr_err("failed to create queue pair with sq_size = %d (%d)\n",
1814                                sq_size, ret);
1815                         goto err_destroy_cq;
1816                 }
1817         }
1818
1819         atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1820
1821         pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d ch= %p\n",
1822                  __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1823                  qp_init->cap.max_send_wr, ch);
1824
1825         if (!sdev->use_srq)
1826                 for (i = 0; i < ch->rq_size; i++)
1827                         srpt_post_recv(sdev, ch, ch->ioctx_recv_ring[i]);
1828
1829 out:
1830         kfree(qp_init);
1831         return ret;
1832
1833 err_destroy_cq:
1834         ch->qp = NULL;
1835         ib_free_cq(ch->cq);
1836         goto out;
1837 }
1838
1839 static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
1840 {
1841         ib_destroy_qp(ch->qp);
1842         ib_free_cq(ch->cq);
1843 }
1844
1845 /**
1846  * srpt_close_ch - close a RDMA channel
1847  * @ch: SRPT RDMA channel.
1848  *
1849  * Make sure all resources associated with the channel will be deallocated at
1850  * an appropriate time.
1851  *
1852  * Returns true if and only if the channel state has been modified into
1853  * CH_DRAINING.
1854  */
1855 static bool srpt_close_ch(struct srpt_rdma_ch *ch)
1856 {
1857         int ret;
1858
1859         if (!srpt_set_ch_state(ch, CH_DRAINING)) {
1860                 pr_debug("%s: already closed\n", ch->sess_name);
1861                 return false;
1862         }
1863
1864         kref_get(&ch->kref);
1865
1866         ret = srpt_ch_qp_err(ch);
1867         if (ret < 0)
1868                 pr_err("%s-%d: changing queue pair into error state failed: %d\n",
1869                        ch->sess_name, ch->qp->qp_num, ret);
1870
1871         ret = srpt_zerolength_write(ch);
1872         if (ret < 0) {
1873                 pr_err("%s-%d: queuing zero-length write failed: %d\n",
1874                        ch->sess_name, ch->qp->qp_num, ret);
1875                 if (srpt_set_ch_state(ch, CH_DISCONNECTED))
1876                         schedule_work(&ch->release_work);
1877                 else
1878                         WARN_ON_ONCE(true);
1879         }
1880
1881         kref_put(&ch->kref, srpt_free_ch);
1882
1883         return true;
1884 }
1885
1886 /*
1887  * Change the channel state into CH_DISCONNECTING. If a channel has not yet
1888  * reached the connected state, close it. If a channel is in the connected
1889  * state, send a DREQ. If a DREQ has been received, send a DREP. Note: it is
1890  * the responsibility of the caller to ensure that this function is not
1891  * invoked concurrently with the code that accepts a connection. This means
1892  * that this function must either be invoked from inside a CM callback
1893  * function or that it must be invoked with the srpt_port.mutex held.
1894  */
1895 static int srpt_disconnect_ch(struct srpt_rdma_ch *ch)
1896 {
1897         int ret;
1898
1899         if (!srpt_set_ch_state(ch, CH_DISCONNECTING))
1900                 return -ENOTCONN;
1901
1902         if (ch->using_rdma_cm) {
1903                 ret = rdma_disconnect(ch->rdma_cm.cm_id);
1904         } else {
1905                 ret = ib_send_cm_dreq(ch->ib_cm.cm_id, NULL, 0);
1906                 if (ret < 0)
1907                         ret = ib_send_cm_drep(ch->ib_cm.cm_id, NULL, 0);
1908         }
1909
1910         if (ret < 0 && srpt_close_ch(ch))
1911                 ret = 0;
1912
1913         return ret;
1914 }
1915
1916 static bool srpt_ch_closed(struct srpt_port *sport, struct srpt_rdma_ch *ch)
1917 {
1918         struct srpt_nexus *nexus;
1919         struct srpt_rdma_ch *ch2;
1920         bool res = true;
1921
1922         rcu_read_lock();
1923         list_for_each_entry(nexus, &sport->nexus_list, entry) {
1924                 list_for_each_entry(ch2, &nexus->ch_list, list) {
1925                         if (ch2 == ch) {
1926                                 res = false;
1927                                 goto done;
1928                         }
1929                 }
1930         }
1931 done:
1932         rcu_read_unlock();
1933
1934         return res;
1935 }
1936
1937 /* Send DREQ and wait for DREP. */
1938 static void srpt_disconnect_ch_sync(struct srpt_rdma_ch *ch)
1939 {
1940         struct srpt_port *sport = ch->sport;
1941
1942         pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num,
1943                  ch->state);
1944
1945         mutex_lock(&sport->mutex);
1946         srpt_disconnect_ch(ch);
1947         mutex_unlock(&sport->mutex);
1948
1949         while (wait_event_timeout(sport->ch_releaseQ, srpt_ch_closed(sport, ch),
1950                                   5 * HZ) == 0)
1951                 pr_info("%s(%s-%d state %d): still waiting ...\n", __func__,
1952                         ch->sess_name, ch->qp->qp_num, ch->state);
1953
1954 }
1955
1956 static void __srpt_close_all_ch(struct srpt_port *sport)
1957 {
1958         struct srpt_nexus *nexus;
1959         struct srpt_rdma_ch *ch;
1960
1961         lockdep_assert_held(&sport->mutex);
1962
1963         list_for_each_entry(nexus, &sport->nexus_list, entry) {
1964                 list_for_each_entry(ch, &nexus->ch_list, list) {
1965                         if (srpt_disconnect_ch(ch) >= 0)
1966                                 pr_info("Closing channel %s because target %s_%d has been disabled\n",
1967                                         ch->sess_name,
1968                                         sport->sdev->device->name, sport->port);
1969                         srpt_close_ch(ch);
1970                 }
1971         }
1972 }
1973
1974 /*
1975  * Look up (i_port_id, t_port_id) in sport->nexus_list. Create an entry if
1976  * it does not yet exist.
1977  */
1978 static struct srpt_nexus *srpt_get_nexus(struct srpt_port *sport,
1979                                          const u8 i_port_id[16],
1980                                          const u8 t_port_id[16])
1981 {
1982         struct srpt_nexus *nexus = NULL, *tmp_nexus = NULL, *n;
1983
1984         for (;;) {
1985                 mutex_lock(&sport->mutex);
1986                 list_for_each_entry(n, &sport->nexus_list, entry) {
1987                         if (memcmp(n->i_port_id, i_port_id, 16) == 0 &&
1988                             memcmp(n->t_port_id, t_port_id, 16) == 0) {
1989                                 nexus = n;
1990                                 break;
1991                         }
1992                 }
1993                 if (!nexus && tmp_nexus) {
1994                         list_add_tail_rcu(&tmp_nexus->entry,
1995                                           &sport->nexus_list);
1996                         swap(nexus, tmp_nexus);
1997                 }
1998                 mutex_unlock(&sport->mutex);
1999
2000                 if (nexus)
2001                         break;
2002                 tmp_nexus = kzalloc(sizeof(*nexus), GFP_KERNEL);
2003                 if (!tmp_nexus) {
2004                         nexus = ERR_PTR(-ENOMEM);
2005                         break;
2006                 }
2007                 INIT_LIST_HEAD(&tmp_nexus->ch_list);
2008                 memcpy(tmp_nexus->i_port_id, i_port_id, 16);
2009                 memcpy(tmp_nexus->t_port_id, t_port_id, 16);
2010         }
2011
2012         kfree(tmp_nexus);
2013
2014         return nexus;
2015 }
2016
2017 static void srpt_set_enabled(struct srpt_port *sport, bool enabled)
2018         __must_hold(&sport->mutex)
2019 {
2020         lockdep_assert_held(&sport->mutex);
2021
2022         if (sport->enabled == enabled)
2023                 return;
2024         sport->enabled = enabled;
2025         if (!enabled)
2026                 __srpt_close_all_ch(sport);
2027 }
2028
2029 static void srpt_free_ch(struct kref *kref)
2030 {
2031         struct srpt_rdma_ch *ch = container_of(kref, struct srpt_rdma_ch, kref);
2032
2033         kfree_rcu(ch, rcu);
2034 }
2035
2036 /*
2037  * Shut down the SCSI target session, tell the connection manager to
2038  * disconnect the associated RDMA channel, transition the QP to the error
2039  * state and remove the channel from the channel list. This function is
2040  * typically called from inside srpt_zerolength_write_done(). Concurrent
2041  * srpt_zerolength_write() calls from inside srpt_close_ch() are possible
2042  * as long as the channel is on sport->nexus_list.
2043  */
2044 static void srpt_release_channel_work(struct work_struct *w)
2045 {
2046         struct srpt_rdma_ch *ch;
2047         struct srpt_device *sdev;
2048         struct srpt_port *sport;
2049         struct se_session *se_sess;
2050
2051         ch = container_of(w, struct srpt_rdma_ch, release_work);
2052         pr_debug("%s-%d\n", ch->sess_name, ch->qp->qp_num);
2053
2054         sdev = ch->sport->sdev;
2055         BUG_ON(!sdev);
2056
2057         se_sess = ch->sess;
2058         BUG_ON(!se_sess);
2059
2060         target_sess_cmd_list_set_waiting(se_sess);
2061         target_wait_for_sess_cmds(se_sess);
2062
2063         target_remove_session(se_sess);
2064         ch->sess = NULL;
2065
2066         if (ch->using_rdma_cm)
2067                 rdma_destroy_id(ch->rdma_cm.cm_id);
2068         else
2069                 ib_destroy_cm_id(ch->ib_cm.cm_id);
2070
2071         sport = ch->sport;
2072         mutex_lock(&sport->mutex);
2073         list_del_rcu(&ch->list);
2074         mutex_unlock(&sport->mutex);
2075
2076         srpt_destroy_ch_ib(ch);
2077
2078         srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2079                              ch->sport->sdev, ch->rq_size,
2080                              ch->max_rsp_size, DMA_TO_DEVICE);
2081
2082         srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring,
2083                              sdev, ch->rq_size,
2084                              srp_max_req_size, DMA_FROM_DEVICE);
2085
2086         wake_up(&sport->ch_releaseQ);
2087
2088         kref_put(&ch->kref, srpt_free_ch);
2089 }
2090
2091 /**
2092  * srpt_cm_req_recv - process the event IB_CM_REQ_RECEIVED
2093  * @sdev: HCA through which the login request was received.
2094  * @ib_cm_id: IB/CM connection identifier in case of IB/CM.
2095  * @rdma_cm_id: RDMA/CM connection identifier in case of RDMA/CM.
2096  * @port_num: Port through which the REQ message was received.
2097  * @pkey: P_Key of the incoming connection.
2098  * @req: SRP login request.
2099  * @src_addr: GID (IB/CM) or IP address (RDMA/CM) of the port that submitted
2100  * the login request.
2101  *
2102  * Ownership of the cm_id is transferred to the target session if this
2103  * function returns zero. Otherwise the caller remains the owner of cm_id.
2104  */
2105 static int srpt_cm_req_recv(struct srpt_device *const sdev,
2106                             struct ib_cm_id *ib_cm_id,
2107                             struct rdma_cm_id *rdma_cm_id,
2108                             u8 port_num, __be16 pkey,
2109                             const struct srp_login_req *req,
2110                             const char *src_addr)
2111 {
2112         struct srpt_port *sport = &sdev->port[port_num - 1];
2113         struct srpt_nexus *nexus;
2114         struct srp_login_rsp *rsp = NULL;
2115         struct srp_login_rej *rej = NULL;
2116         union {
2117                 struct rdma_conn_param rdma_cm;
2118                 struct ib_cm_rep_param ib_cm;
2119         } *rep_param = NULL;
2120         struct srpt_rdma_ch *ch = NULL;
2121         char i_port_id[36];
2122         u32 it_iu_len;
2123         int i, ret;
2124
2125         WARN_ON_ONCE(irqs_disabled());
2126
2127         if (WARN_ON(!sdev || !req))
2128                 return -EINVAL;
2129
2130         it_iu_len = be32_to_cpu(req->req_it_iu_len);
2131
2132         pr_info("Received SRP_LOGIN_REQ with i_port_id %pI6, t_port_id %pI6 and it_iu_len %d on port %d (guid=%pI6); pkey %#04x\n",
2133                 req->initiator_port_id, req->target_port_id, it_iu_len,
2134                 port_num, &sport->gid, be16_to_cpu(pkey));
2135
2136         nexus = srpt_get_nexus(sport, req->initiator_port_id,
2137                                req->target_port_id);
2138         if (IS_ERR(nexus)) {
2139                 ret = PTR_ERR(nexus);
2140                 goto out;
2141         }
2142
2143         ret = -ENOMEM;
2144         rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
2145         rej = kzalloc(sizeof(*rej), GFP_KERNEL);
2146         rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
2147         if (!rsp || !rej || !rep_param)
2148                 goto out;
2149
2150         ret = -EINVAL;
2151         if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
2152                 rej->reason = cpu_to_be32(
2153                                 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
2154                 pr_err("rejected SRP_LOGIN_REQ because its length (%d bytes) is out of range (%d .. %d)\n",
2155                        it_iu_len, 64, srp_max_req_size);
2156                 goto reject;
2157         }
2158
2159         if (!sport->enabled) {
2160                 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2161                 pr_info("rejected SRP_LOGIN_REQ because target port %s_%d has not yet been enabled\n",
2162                         sport->sdev->device->name, port_num);
2163                 goto reject;
2164         }
2165
2166         if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2167             || *(__be64 *)(req->target_port_id + 8) !=
2168                cpu_to_be64(srpt_service_guid)) {
2169                 rej->reason = cpu_to_be32(
2170                                 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
2171                 pr_err("rejected SRP_LOGIN_REQ because it has an invalid target port identifier.\n");
2172                 goto reject;
2173         }
2174
2175         ret = -ENOMEM;
2176         ch = kzalloc(sizeof(*ch), GFP_KERNEL);
2177         if (!ch) {
2178                 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2179                 pr_err("rejected SRP_LOGIN_REQ because out of memory.\n");
2180                 goto reject;
2181         }
2182
2183         kref_init(&ch->kref);
2184         ch->pkey = be16_to_cpu(pkey);
2185         ch->nexus = nexus;
2186         ch->zw_cqe.done = srpt_zerolength_write_done;
2187         INIT_WORK(&ch->release_work, srpt_release_channel_work);
2188         ch->sport = sport;
2189         if (ib_cm_id) {
2190                 ch->ib_cm.cm_id = ib_cm_id;
2191                 ib_cm_id->context = ch;
2192         } else {
2193                 ch->using_rdma_cm = true;
2194                 ch->rdma_cm.cm_id = rdma_cm_id;
2195                 rdma_cm_id->context = ch;
2196         }
2197         /*
2198          * ch->rq_size should be at least as large as the initiator queue
2199          * depth to avoid that the initiator driver has to report QUEUE_FULL
2200          * to the SCSI mid-layer.
2201          */
2202         ch->rq_size = min(MAX_SRPT_RQ_SIZE, sdev->device->attrs.max_qp_wr);
2203         spin_lock_init(&ch->spinlock);
2204         ch->state = CH_CONNECTING;
2205         INIT_LIST_HEAD(&ch->cmd_wait_list);
2206         ch->max_rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2207
2208         ch->ioctx_ring = (struct srpt_send_ioctx **)
2209                 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2210                                       sizeof(*ch->ioctx_ring[0]),
2211                                       ch->max_rsp_size, DMA_TO_DEVICE);
2212         if (!ch->ioctx_ring) {
2213                 pr_err("rejected SRP_LOGIN_REQ because creating a new QP SQ ring failed.\n");
2214                 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2215                 goto free_ch;
2216         }
2217
2218         INIT_LIST_HEAD(&ch->free_list);
2219         for (i = 0; i < ch->rq_size; i++) {
2220                 ch->ioctx_ring[i]->ch = ch;
2221                 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2222         }
2223         if (!sdev->use_srq) {
2224                 ch->ioctx_recv_ring = (struct srpt_recv_ioctx **)
2225                         srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2226                                               sizeof(*ch->ioctx_recv_ring[0]),
2227                                               srp_max_req_size,
2228                                               DMA_FROM_DEVICE);
2229                 if (!ch->ioctx_recv_ring) {
2230                         pr_err("rejected SRP_LOGIN_REQ because creating a new QP RQ ring failed.\n");
2231                         rej->reason =
2232                             cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2233                         goto free_ring;
2234                 }
2235                 for (i = 0; i < ch->rq_size; i++)
2236                         INIT_LIST_HEAD(&ch->ioctx_recv_ring[i]->wait_list);
2237         }
2238
2239         ret = srpt_create_ch_ib(ch);
2240         if (ret) {
2241                 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2242                 pr_err("rejected SRP_LOGIN_REQ because creating a new RDMA channel failed.\n");
2243                 goto free_recv_ring;
2244         }
2245
2246         strlcpy(ch->sess_name, src_addr, sizeof(ch->sess_name));
2247         snprintf(i_port_id, sizeof(i_port_id), "0x%016llx%016llx",
2248                         be64_to_cpu(*(__be64 *)nexus->i_port_id),
2249                         be64_to_cpu(*(__be64 *)(nexus->i_port_id + 8)));
2250
2251         pr_debug("registering session %s\n", ch->sess_name);
2252
2253         if (sport->port_guid_tpg.se_tpg_wwn)
2254                 ch->sess = target_setup_session(&sport->port_guid_tpg, 0, 0,
2255                                                 TARGET_PROT_NORMAL,
2256                                                 ch->sess_name, ch, NULL);
2257         if (sport->port_gid_tpg.se_tpg_wwn && IS_ERR_OR_NULL(ch->sess))
2258                 ch->sess = target_setup_session(&sport->port_gid_tpg, 0, 0,
2259                                         TARGET_PROT_NORMAL, i_port_id, ch,
2260                                         NULL);
2261         /* Retry without leading "0x" */
2262         if (sport->port_gid_tpg.se_tpg_wwn && IS_ERR_OR_NULL(ch->sess))
2263                 ch->sess = target_setup_session(&sport->port_gid_tpg, 0, 0,
2264                                                 TARGET_PROT_NORMAL,
2265                                                 i_port_id + 2, ch, NULL);
2266         if (IS_ERR_OR_NULL(ch->sess)) {
2267                 WARN_ON_ONCE(ch->sess == NULL);
2268                 ret = PTR_ERR(ch->sess);
2269                 ch->sess = NULL;
2270                 pr_info("Rejected login for initiator %s: ret = %d.\n",
2271                         ch->sess_name, ret);
2272                 rej->reason = cpu_to_be32(ret == -ENOMEM ?
2273                                 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES :
2274                                 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2275                 goto destroy_ib;
2276         }
2277
2278         mutex_lock(&sport->mutex);
2279
2280         if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2281                 struct srpt_rdma_ch *ch2;
2282
2283                 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2284
2285                 list_for_each_entry(ch2, &nexus->ch_list, list) {
2286                         if (srpt_disconnect_ch(ch2) < 0)
2287                                 continue;
2288                         pr_info("Relogin - closed existing channel %s\n",
2289                                 ch2->sess_name);
2290                         rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2291                 }
2292         } else {
2293                 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2294         }
2295
2296         list_add_tail_rcu(&ch->list, &nexus->ch_list);
2297
2298         if (!sport->enabled) {
2299                 rej->reason = cpu_to_be32(
2300                                 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2301                 pr_info("rejected SRP_LOGIN_REQ because target %s_%d is not enabled\n",
2302                         sdev->device->name, port_num);
2303                 mutex_unlock(&sport->mutex);
2304                 ret = -EINVAL;
2305                 goto reject;
2306         }
2307
2308         mutex_unlock(&sport->mutex);
2309
2310         ret = ch->using_rdma_cm ? 0 : srpt_ch_qp_rtr(ch, ch->qp);
2311         if (ret) {
2312                 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2313                 pr_err("rejected SRP_LOGIN_REQ because enabling RTR failed (error code = %d)\n",
2314                        ret);
2315                 goto reject;
2316         }
2317
2318         pr_debug("Establish connection sess=%p name=%s ch=%p\n", ch->sess,
2319                  ch->sess_name, ch);
2320
2321         /* create srp_login_response */
2322         rsp->opcode = SRP_LOGIN_RSP;
2323         rsp->tag = req->tag;
2324         rsp->max_it_iu_len = req->req_it_iu_len;
2325         rsp->max_ti_iu_len = req->req_it_iu_len;
2326         ch->max_ti_iu_len = it_iu_len;
2327         rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
2328                                    SRP_BUF_FORMAT_INDIRECT);
2329         rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2330         atomic_set(&ch->req_lim, ch->rq_size);
2331         atomic_set(&ch->req_lim_delta, 0);
2332
2333         /* create cm reply */
2334         if (ch->using_rdma_cm) {
2335                 rep_param->rdma_cm.private_data = (void *)rsp;
2336                 rep_param->rdma_cm.private_data_len = sizeof(*rsp);
2337                 rep_param->rdma_cm.rnr_retry_count = 7;
2338                 rep_param->rdma_cm.flow_control = 1;
2339                 rep_param->rdma_cm.responder_resources = 4;
2340                 rep_param->rdma_cm.initiator_depth = 4;
2341         } else {
2342                 rep_param->ib_cm.qp_num = ch->qp->qp_num;
2343                 rep_param->ib_cm.private_data = (void *)rsp;
2344                 rep_param->ib_cm.private_data_len = sizeof(*rsp);
2345                 rep_param->ib_cm.rnr_retry_count = 7;
2346                 rep_param->ib_cm.flow_control = 1;
2347                 rep_param->ib_cm.failover_accepted = 0;
2348                 rep_param->ib_cm.srq = 1;
2349                 rep_param->ib_cm.responder_resources = 4;
2350                 rep_param->ib_cm.initiator_depth = 4;
2351         }
2352
2353         /*
2354          * Hold the sport mutex while accepting a connection to avoid that
2355          * srpt_disconnect_ch() is invoked concurrently with this code.
2356          */
2357         mutex_lock(&sport->mutex);
2358         if (sport->enabled && ch->state == CH_CONNECTING) {
2359                 if (ch->using_rdma_cm)
2360                         ret = rdma_accept(rdma_cm_id, &rep_param->rdma_cm);
2361                 else
2362                         ret = ib_send_cm_rep(ib_cm_id, &rep_param->ib_cm);
2363         } else {
2364                 ret = -EINVAL;
2365         }
2366         mutex_unlock(&sport->mutex);
2367
2368         switch (ret) {
2369         case 0:
2370                 break;
2371         case -EINVAL:
2372                 goto reject;
2373         default:
2374                 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2375                 pr_err("sending SRP_LOGIN_REQ response failed (error code = %d)\n",
2376                        ret);
2377                 goto reject;
2378         }
2379
2380         goto out;
2381
2382 destroy_ib:
2383         srpt_destroy_ch_ib(ch);
2384
2385 free_recv_ring:
2386         srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring,
2387                              ch->sport->sdev, ch->rq_size,
2388                              srp_max_req_size, DMA_FROM_DEVICE);
2389
2390 free_ring:
2391         srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2392                              ch->sport->sdev, ch->rq_size,
2393                              ch->max_rsp_size, DMA_TO_DEVICE);
2394
2395 free_ch:
2396         if (rdma_cm_id)
2397                 rdma_cm_id->context = NULL;
2398         else
2399                 ib_cm_id->context = NULL;
2400         kfree(ch);
2401         ch = NULL;
2402
2403         WARN_ON_ONCE(ret == 0);
2404
2405 reject:
2406         pr_info("Rejecting login with reason %#x\n", be32_to_cpu(rej->reason));
2407         rej->opcode = SRP_LOGIN_REJ;
2408         rej->tag = req->tag;
2409         rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
2410                                    SRP_BUF_FORMAT_INDIRECT);
2411
2412         if (rdma_cm_id)
2413                 rdma_reject(rdma_cm_id, rej, sizeof(*rej));
2414         else
2415                 ib_send_cm_rej(ib_cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2416                                rej, sizeof(*rej));
2417
2418         if (ch && ch->sess) {
2419                 srpt_close_ch(ch);
2420                 /*
2421                  * Tell the caller not to free cm_id since
2422                  * srpt_release_channel_work() will do that.
2423                  */
2424                 ret = 0;
2425         }
2426
2427 out:
2428         kfree(rep_param);
2429         kfree(rsp);
2430         kfree(rej);
2431
2432         return ret;
2433 }
2434
2435 static int srpt_ib_cm_req_recv(struct ib_cm_id *cm_id,
2436                                const struct ib_cm_req_event_param *param,
2437                                void *private_data)
2438 {
2439         char sguid[40];
2440
2441         srpt_format_guid(sguid, sizeof(sguid),
2442                          &param->primary_path->dgid.global.interface_id);
2443
2444         return srpt_cm_req_recv(cm_id->context, cm_id, NULL, param->port,
2445                                 param->primary_path->pkey,
2446                                 private_data, sguid);
2447 }
2448
2449 static int srpt_rdma_cm_req_recv(struct rdma_cm_id *cm_id,
2450                                  struct rdma_cm_event *event)
2451 {
2452         struct srpt_device *sdev;
2453         struct srp_login_req req;
2454         const struct srp_login_req_rdma *req_rdma;
2455         char src_addr[40];
2456
2457         sdev = ib_get_client_data(cm_id->device, &srpt_client);
2458         if (!sdev)
2459                 return -ECONNREFUSED;
2460
2461         if (event->param.conn.private_data_len < sizeof(*req_rdma))
2462                 return -EINVAL;
2463
2464         /* Transform srp_login_req_rdma into srp_login_req. */
2465         req_rdma = event->param.conn.private_data;
2466         memset(&req, 0, sizeof(req));
2467         req.opcode              = req_rdma->opcode;
2468         req.tag                 = req_rdma->tag;
2469         req.req_it_iu_len       = req_rdma->req_it_iu_len;
2470         req.req_buf_fmt         = req_rdma->req_buf_fmt;
2471         req.req_flags           = req_rdma->req_flags;
2472         memcpy(req.initiator_port_id, req_rdma->initiator_port_id, 16);
2473         memcpy(req.target_port_id, req_rdma->target_port_id, 16);
2474
2475         snprintf(src_addr, sizeof(src_addr), "%pIS",
2476                  &cm_id->route.addr.src_addr);
2477
2478         return srpt_cm_req_recv(sdev, NULL, cm_id, cm_id->port_num,
2479                                 cm_id->route.path_rec->pkey, &req, src_addr);
2480 }
2481
2482 static void srpt_cm_rej_recv(struct srpt_rdma_ch *ch,
2483                              enum ib_cm_rej_reason reason,
2484                              const u8 *private_data,
2485                              u8 private_data_len)
2486 {
2487         char *priv = NULL;
2488         int i;
2489
2490         if (private_data_len && (priv = kmalloc(private_data_len * 3 + 1,
2491                                                 GFP_KERNEL))) {
2492                 for (i = 0; i < private_data_len; i++)
2493                         sprintf(priv + 3 * i, " %02x", private_data[i]);
2494         }
2495         pr_info("Received CM REJ for ch %s-%d; reason %d%s%s.\n",
2496                 ch->sess_name, ch->qp->qp_num, reason, private_data_len ?
2497                 "; private data" : "", priv ? priv : " (?)");
2498         kfree(priv);
2499 }
2500
2501 /**
2502  * srpt_cm_rtu_recv - process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event
2503  * @ch: SRPT RDMA channel.
2504  *
2505  * An RTU (ready to use) message indicates that the connection has been
2506  * established and that the recipient may begin transmitting.
2507  */
2508 static void srpt_cm_rtu_recv(struct srpt_rdma_ch *ch)
2509 {
2510         int ret;
2511
2512         ret = ch->using_rdma_cm ? 0 : srpt_ch_qp_rts(ch, ch->qp);
2513         if (ret < 0) {
2514                 pr_err("%s-%d: QP transition to RTS failed\n", ch->sess_name,
2515                        ch->qp->qp_num);
2516                 srpt_close_ch(ch);
2517                 return;
2518         }
2519
2520         /*
2521          * Note: calling srpt_close_ch() if the transition to the LIVE state
2522          * fails is not necessary since that means that that function has
2523          * already been invoked from another thread.
2524          */
2525         if (!srpt_set_ch_state(ch, CH_LIVE)) {
2526                 pr_err("%s-%d: channel transition to LIVE state failed\n",
2527                        ch->sess_name, ch->qp->qp_num);
2528                 return;
2529         }
2530
2531         /* Trigger wait list processing. */
2532         ret = srpt_zerolength_write(ch);
2533         WARN_ONCE(ret < 0, "%d\n", ret);
2534 }
2535
2536 /**
2537  * srpt_cm_handler - IB connection manager callback function
2538  * @cm_id: IB/CM connection identifier.
2539  * @event: IB/CM event.
2540  *
2541  * A non-zero return value will cause the caller destroy the CM ID.
2542  *
2543  * Note: srpt_cm_handler() must only return a non-zero value when transferring
2544  * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2545  * a non-zero value in any other case will trigger a race with the
2546  * ib_destroy_cm_id() call in srpt_release_channel().
2547  */
2548 static int srpt_cm_handler(struct ib_cm_id *cm_id,
2549                            const struct ib_cm_event *event)
2550 {
2551         struct srpt_rdma_ch *ch = cm_id->context;
2552         int ret;
2553
2554         ret = 0;
2555         switch (event->event) {
2556         case IB_CM_REQ_RECEIVED:
2557                 ret = srpt_ib_cm_req_recv(cm_id, &event->param.req_rcvd,
2558                                           event->private_data);
2559                 break;
2560         case IB_CM_REJ_RECEIVED:
2561                 srpt_cm_rej_recv(ch, event->param.rej_rcvd.reason,
2562                                  event->private_data,
2563                                  IB_CM_REJ_PRIVATE_DATA_SIZE);
2564                 break;
2565         case IB_CM_RTU_RECEIVED:
2566         case IB_CM_USER_ESTABLISHED:
2567                 srpt_cm_rtu_recv(ch);
2568                 break;
2569         case IB_CM_DREQ_RECEIVED:
2570                 srpt_disconnect_ch(ch);
2571                 break;
2572         case IB_CM_DREP_RECEIVED:
2573                 pr_info("Received CM DREP message for ch %s-%d.\n",
2574                         ch->sess_name, ch->qp->qp_num);
2575                 srpt_close_ch(ch);
2576                 break;
2577         case IB_CM_TIMEWAIT_EXIT:
2578                 pr_info("Received CM TimeWait exit for ch %s-%d.\n",
2579                         ch->sess_name, ch->qp->qp_num);
2580                 srpt_close_ch(ch);
2581                 break;
2582         case IB_CM_REP_ERROR:
2583                 pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
2584                         ch->qp->qp_num);
2585                 break;
2586         case IB_CM_DREQ_ERROR:
2587                 pr_info("Received CM DREQ ERROR event.\n");
2588                 break;
2589         case IB_CM_MRA_RECEIVED:
2590                 pr_info("Received CM MRA event\n");
2591                 break;
2592         default:
2593                 pr_err("received unrecognized CM event %d\n", event->event);
2594                 break;
2595         }
2596
2597         return ret;
2598 }
2599
2600 static int srpt_rdma_cm_handler(struct rdma_cm_id *cm_id,
2601                                 struct rdma_cm_event *event)
2602 {
2603         struct srpt_rdma_ch *ch = cm_id->context;
2604         int ret = 0;
2605
2606         switch (event->event) {
2607         case RDMA_CM_EVENT_CONNECT_REQUEST:
2608                 ret = srpt_rdma_cm_req_recv(cm_id, event);
2609                 break;
2610         case RDMA_CM_EVENT_REJECTED:
2611                 srpt_cm_rej_recv(ch, event->status,
2612                                  event->param.conn.private_data,
2613                                  event->param.conn.private_data_len);
2614                 break;
2615         case RDMA_CM_EVENT_ESTABLISHED:
2616                 srpt_cm_rtu_recv(ch);
2617                 break;
2618         case RDMA_CM_EVENT_DISCONNECTED:
2619                 if (ch->state < CH_DISCONNECTING)
2620                         srpt_disconnect_ch(ch);
2621                 else
2622                         srpt_close_ch(ch);
2623                 break;
2624         case RDMA_CM_EVENT_TIMEWAIT_EXIT:
2625                 srpt_close_ch(ch);
2626                 break;
2627         case RDMA_CM_EVENT_UNREACHABLE:
2628                 pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
2629                         ch->qp->qp_num);
2630                 break;
2631         case RDMA_CM_EVENT_DEVICE_REMOVAL:
2632         case RDMA_CM_EVENT_ADDR_CHANGE:
2633                 break;
2634         default:
2635                 pr_err("received unrecognized RDMA CM event %d\n",
2636                        event->event);
2637                 break;
2638         }
2639
2640         return ret;
2641 }
2642
2643 static int srpt_write_pending_status(struct se_cmd *se_cmd)
2644 {
2645         struct srpt_send_ioctx *ioctx;
2646
2647         ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2648         return ioctx->state == SRPT_STATE_NEED_DATA;
2649 }
2650
2651 /*
2652  * srpt_write_pending - Start data transfer from initiator to target (write).
2653  */
2654 static int srpt_write_pending(struct se_cmd *se_cmd)
2655 {
2656         struct srpt_send_ioctx *ioctx =
2657                 container_of(se_cmd, struct srpt_send_ioctx, cmd);
2658         struct srpt_rdma_ch *ch = ioctx->ch;
2659         struct ib_send_wr *first_wr = NULL;
2660         struct ib_cqe *cqe = &ioctx->rdma_cqe;
2661         enum srpt_command_state new_state;
2662         int ret, i;
2663
2664         new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2665         WARN_ON(new_state == SRPT_STATE_DONE);
2666
2667         if (atomic_sub_return(ioctx->n_rdma, &ch->sq_wr_avail) < 0) {
2668                 pr_warn("%s: IB send queue full (needed %d)\n",
2669                                 __func__, ioctx->n_rdma);
2670                 ret = -ENOMEM;
2671                 goto out_undo;
2672         }
2673
2674         cqe->done = srpt_rdma_read_done;
2675         for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
2676                 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
2677
2678                 first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, ch->sport->port,
2679                                 cqe, first_wr);
2680                 cqe = NULL;
2681         }
2682
2683         ret = ib_post_send(ch->qp, first_wr, NULL);
2684         if (ret) {
2685                 pr_err("%s: ib_post_send() returned %d for %d (avail: %d)\n",
2686                          __func__, ret, ioctx->n_rdma,
2687                          atomic_read(&ch->sq_wr_avail));
2688                 goto out_undo;
2689         }
2690
2691         return 0;
2692 out_undo:
2693         atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
2694         return ret;
2695 }
2696
2697 static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2698 {
2699         switch (tcm_mgmt_status) {
2700         case TMR_FUNCTION_COMPLETE:
2701                 return SRP_TSK_MGMT_SUCCESS;
2702         case TMR_FUNCTION_REJECTED:
2703                 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2704         }
2705         return SRP_TSK_MGMT_FAILED;
2706 }
2707
2708 /**
2709  * srpt_queue_response - transmit the response to a SCSI command
2710  * @cmd: SCSI target command.
2711  *
2712  * Callback function called by the TCM core. Must not block since it can be
2713  * invoked on the context of the IB completion handler.
2714  */
2715 static void srpt_queue_response(struct se_cmd *cmd)
2716 {
2717         struct srpt_send_ioctx *ioctx =
2718                 container_of(cmd, struct srpt_send_ioctx, cmd);
2719         struct srpt_rdma_ch *ch = ioctx->ch;
2720         struct srpt_device *sdev = ch->sport->sdev;
2721         struct ib_send_wr send_wr, *first_wr = &send_wr;
2722         struct ib_sge sge;
2723         enum srpt_command_state state;
2724         int resp_len, ret, i;
2725         u8 srp_tm_status;
2726
2727         BUG_ON(!ch);
2728
2729         state = ioctx->state;
2730         switch (state) {
2731         case SRPT_STATE_NEW:
2732         case SRPT_STATE_DATA_IN:
2733                 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2734                 break;
2735         case SRPT_STATE_MGMT:
2736                 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2737                 break;
2738         default:
2739                 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2740                         ch, ioctx->ioctx.index, ioctx->state);
2741                 break;
2742         }
2743
2744         if (unlikely(WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT)))
2745                 return;
2746
2747         /* For read commands, transfer the data to the initiator. */
2748         if (ioctx->cmd.data_direction == DMA_FROM_DEVICE &&
2749             ioctx->cmd.data_length &&
2750             !ioctx->queue_status_only) {
2751                 for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
2752                         struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
2753
2754                         first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp,
2755                                         ch->sport->port, NULL, first_wr);
2756                 }
2757         }
2758
2759         if (state != SRPT_STATE_MGMT)
2760                 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
2761                                               cmd->scsi_status);
2762         else {
2763                 srp_tm_status
2764                         = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2765                 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
2766                                                  ioctx->cmd.tag);
2767         }
2768
2769         atomic_inc(&ch->req_lim);
2770
2771         if (unlikely(atomic_sub_return(1 + ioctx->n_rdma,
2772                         &ch->sq_wr_avail) < 0)) {
2773                 pr_warn("%s: IB send queue full (needed %d)\n",
2774                                 __func__, ioctx->n_rdma);
2775                 ret = -ENOMEM;
2776                 goto out;
2777         }
2778
2779         ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, resp_len,
2780                                       DMA_TO_DEVICE);
2781
2782         sge.addr = ioctx->ioctx.dma;
2783         sge.length = resp_len;
2784         sge.lkey = sdev->lkey;
2785
2786         ioctx->ioctx.cqe.done = srpt_send_done;
2787         send_wr.next = NULL;
2788         send_wr.wr_cqe = &ioctx->ioctx.cqe;
2789         send_wr.sg_list = &sge;
2790         send_wr.num_sge = 1;
2791         send_wr.opcode = IB_WR_SEND;
2792         send_wr.send_flags = IB_SEND_SIGNALED;
2793
2794         ret = ib_post_send(ch->qp, first_wr, NULL);
2795         if (ret < 0) {
2796                 pr_err("%s: sending cmd response failed for tag %llu (%d)\n",
2797                         __func__, ioctx->cmd.tag, ret);
2798                 goto out;
2799         }
2800
2801         return;
2802
2803 out:
2804         atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
2805         atomic_dec(&ch->req_lim);
2806         srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
2807         target_put_sess_cmd(&ioctx->cmd);
2808 }
2809
2810 static int srpt_queue_data_in(struct se_cmd *cmd)
2811 {
2812         srpt_queue_response(cmd);
2813         return 0;
2814 }
2815
2816 static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2817 {
2818         srpt_queue_response(cmd);
2819 }
2820
2821 /*
2822  * This function is called for aborted commands if no response is sent to the
2823  * initiator. Make sure that the credits freed by aborting a command are
2824  * returned to the initiator the next time a response is sent by incrementing
2825  * ch->req_lim_delta.
2826  */
2827 static void srpt_aborted_task(struct se_cmd *cmd)
2828 {
2829         struct srpt_send_ioctx *ioctx = container_of(cmd,
2830                                 struct srpt_send_ioctx, cmd);
2831         struct srpt_rdma_ch *ch = ioctx->ch;
2832
2833         atomic_inc(&ch->req_lim_delta);
2834 }
2835
2836 static int srpt_queue_status(struct se_cmd *cmd)
2837 {
2838         struct srpt_send_ioctx *ioctx;
2839
2840         ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2841         BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2842         if (cmd->se_cmd_flags &
2843             (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2844                 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2845         ioctx->queue_status_only = true;
2846         srpt_queue_response(cmd);
2847         return 0;
2848 }
2849
2850 static void srpt_refresh_port_work(struct work_struct *work)
2851 {
2852         struct srpt_port *sport = container_of(work, struct srpt_port, work);
2853
2854         srpt_refresh_port(sport);
2855 }
2856
2857 static bool srpt_ch_list_empty(struct srpt_port *sport)
2858 {
2859         struct srpt_nexus *nexus;
2860         bool res = true;
2861
2862         rcu_read_lock();
2863         list_for_each_entry(nexus, &sport->nexus_list, entry)
2864                 if (!list_empty(&nexus->ch_list))
2865                         res = false;
2866         rcu_read_unlock();
2867
2868         return res;
2869 }
2870
2871 /**
2872  * srpt_release_sport - disable login and wait for associated channels
2873  * @sport: SRPT HCA port.
2874  */
2875 static int srpt_release_sport(struct srpt_port *sport)
2876 {
2877         struct srpt_nexus *nexus, *next_n;
2878         struct srpt_rdma_ch *ch;
2879
2880         WARN_ON_ONCE(irqs_disabled());
2881
2882         mutex_lock(&sport->mutex);
2883         srpt_set_enabled(sport, false);
2884         mutex_unlock(&sport->mutex);
2885
2886         while (wait_event_timeout(sport->ch_releaseQ,
2887                                   srpt_ch_list_empty(sport), 5 * HZ) <= 0) {
2888                 pr_info("%s_%d: waiting for session unregistration ...\n",
2889                         sport->sdev->device->name, sport->port);
2890                 rcu_read_lock();
2891                 list_for_each_entry(nexus, &sport->nexus_list, entry) {
2892                         list_for_each_entry(ch, &nexus->ch_list, list) {
2893                                 pr_info("%s-%d: state %s\n",
2894                                         ch->sess_name, ch->qp->qp_num,
2895                                         get_ch_state_name(ch->state));
2896                         }
2897                 }
2898                 rcu_read_unlock();
2899         }
2900
2901         mutex_lock(&sport->mutex);
2902         list_for_each_entry_safe(nexus, next_n, &sport->nexus_list, entry) {
2903                 list_del(&nexus->entry);
2904                 kfree_rcu(nexus, rcu);
2905         }
2906         mutex_unlock(&sport->mutex);
2907
2908         return 0;
2909 }
2910
2911 static struct se_wwn *__srpt_lookup_wwn(const char *name)
2912 {
2913         struct ib_device *dev;
2914         struct srpt_device *sdev;
2915         struct srpt_port *sport;
2916         int i;
2917
2918         list_for_each_entry(sdev, &srpt_dev_list, list) {
2919                 dev = sdev->device;
2920                 if (!dev)
2921                         continue;
2922
2923                 for (i = 0; i < dev->phys_port_cnt; i++) {
2924                         sport = &sdev->port[i];
2925
2926                         if (strcmp(sport->port_guid, name) == 0)
2927                                 return &sport->port_guid_wwn;
2928                         if (strcmp(sport->port_gid, name) == 0)
2929                                 return &sport->port_gid_wwn;
2930                 }
2931         }
2932
2933         return NULL;
2934 }
2935
2936 static struct se_wwn *srpt_lookup_wwn(const char *name)
2937 {
2938         struct se_wwn *wwn;
2939
2940         spin_lock(&srpt_dev_lock);
2941         wwn = __srpt_lookup_wwn(name);
2942         spin_unlock(&srpt_dev_lock);
2943
2944         return wwn;
2945 }
2946
2947 static void srpt_free_srq(struct srpt_device *sdev)
2948 {
2949         if (!sdev->srq)
2950                 return;
2951
2952         ib_destroy_srq(sdev->srq);
2953         srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2954                              sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
2955         sdev->srq = NULL;
2956 }
2957
2958 static int srpt_alloc_srq(struct srpt_device *sdev)
2959 {
2960         struct ib_srq_init_attr srq_attr = {
2961                 .event_handler = srpt_srq_event,
2962                 .srq_context = (void *)sdev,
2963                 .attr.max_wr = sdev->srq_size,
2964                 .attr.max_sge = 1,
2965                 .srq_type = IB_SRQT_BASIC,
2966         };
2967         struct ib_device *device = sdev->device;
2968         struct ib_srq *srq;
2969         int i;
2970
2971         WARN_ON_ONCE(sdev->srq);
2972         srq = ib_create_srq(sdev->pd, &srq_attr);
2973         if (IS_ERR(srq)) {
2974                 pr_debug("ib_create_srq() failed: %ld\n", PTR_ERR(srq));
2975                 return PTR_ERR(srq);
2976         }
2977
2978         pr_debug("create SRQ #wr= %d max_allow=%d dev= %s\n", sdev->srq_size,
2979                  sdev->device->attrs.max_srq_wr, device->name);
2980
2981         sdev->ioctx_ring = (struct srpt_recv_ioctx **)
2982                 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
2983                                       sizeof(*sdev->ioctx_ring[0]),
2984                                       srp_max_req_size, DMA_FROM_DEVICE);
2985         if (!sdev->ioctx_ring) {
2986                 ib_destroy_srq(srq);
2987                 return -ENOMEM;
2988         }
2989
2990         sdev->use_srq = true;
2991         sdev->srq = srq;
2992
2993         for (i = 0; i < sdev->srq_size; ++i) {
2994                 INIT_LIST_HEAD(&sdev->ioctx_ring[i]->wait_list);
2995                 srpt_post_recv(sdev, NULL, sdev->ioctx_ring[i]);
2996         }
2997
2998         return 0;
2999 }
3000
3001 static int srpt_use_srq(struct srpt_device *sdev, bool use_srq)
3002 {
3003         struct ib_device *device = sdev->device;
3004         int ret = 0;
3005
3006         if (!use_srq) {
3007                 srpt_free_srq(sdev);
3008                 sdev->use_srq = false;
3009         } else if (use_srq && !sdev->srq) {
3010                 ret = srpt_alloc_srq(sdev);
3011         }
3012         pr_debug("%s(%s): use_srq = %d; ret = %d\n", __func__, device->name,
3013                  sdev->use_srq, ret);
3014         return ret;
3015 }
3016
3017 /**
3018  * srpt_add_one - InfiniBand device addition callback function
3019  * @device: Describes a HCA.
3020  */
3021 static void srpt_add_one(struct ib_device *device)
3022 {
3023         struct srpt_device *sdev;
3024         struct srpt_port *sport;
3025         int i, ret;
3026
3027         pr_debug("device = %p\n", device);
3028
3029         sdev = kzalloc(struct_size(sdev, port, device->phys_port_cnt),
3030                        GFP_KERNEL);
3031         if (!sdev)
3032                 goto err;
3033
3034         sdev->device = device;
3035         mutex_init(&sdev->sdev_mutex);
3036
3037         sdev->pd = ib_alloc_pd(device, 0);
3038         if (IS_ERR(sdev->pd))
3039                 goto free_dev;
3040
3041         sdev->lkey = sdev->pd->local_dma_lkey;
3042
3043         sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
3044
3045         srpt_use_srq(sdev, sdev->port[0].port_attrib.use_srq);
3046
3047         if (!srpt_service_guid)
3048                 srpt_service_guid = be64_to_cpu(device->node_guid);
3049
3050         if (rdma_port_get_link_layer(device, 1) == IB_LINK_LAYER_INFINIBAND)
3051                 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
3052         if (IS_ERR(sdev->cm_id)) {
3053                 pr_info("ib_create_cm_id() failed: %ld\n",
3054                         PTR_ERR(sdev->cm_id));
3055                 sdev->cm_id = NULL;
3056                 if (!rdma_cm_id)
3057                         goto err_ring;
3058         }
3059
3060         /* print out target login information */
3061         pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
3062                  "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
3063                  srpt_service_guid, srpt_service_guid);
3064
3065         /*
3066          * We do not have a consistent service_id (ie. also id_ext of target_id)
3067          * to identify this target. We currently use the guid of the first HCA
3068          * in the system as service_id; therefore, the target_id will change
3069          * if this HCA is gone bad and replaced by different HCA
3070          */
3071         ret = sdev->cm_id ?
3072                 ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0) :
3073                 0;
3074         if (ret < 0) {
3075                 pr_err("ib_cm_listen() failed: %d (cm_id state = %d)\n", ret,
3076                        sdev->cm_id->state);
3077                 goto err_cm;
3078         }
3079
3080         INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
3081                               srpt_event_handler);
3082         ib_register_event_handler(&sdev->event_handler);
3083
3084         for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3085                 sport = &sdev->port[i - 1];
3086                 INIT_LIST_HEAD(&sport->nexus_list);
3087                 init_waitqueue_head(&sport->ch_releaseQ);
3088                 mutex_init(&sport->mutex);
3089                 sport->sdev = sdev;
3090                 sport->port = i;
3091                 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3092                 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3093                 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3094                 sport->port_attrib.use_srq = false;
3095                 INIT_WORK(&sport->work, srpt_refresh_port_work);
3096
3097                 if (srpt_refresh_port(sport)) {
3098                         pr_err("MAD registration failed for %s-%d.\n",
3099                                sdev->device->name, i);
3100                         goto err_event;
3101                 }
3102         }
3103
3104         spin_lock(&srpt_dev_lock);
3105         list_add_tail(&sdev->list, &srpt_dev_list);
3106         spin_unlock(&srpt_dev_lock);
3107
3108 out:
3109         ib_set_client_data(device, &srpt_client, sdev);
3110         pr_debug("added %s.\n", device->name);
3111         return;
3112
3113 err_event:
3114         ib_unregister_event_handler(&sdev->event_handler);
3115 err_cm:
3116         if (sdev->cm_id)
3117                 ib_destroy_cm_id(sdev->cm_id);
3118 err_ring:
3119         srpt_free_srq(sdev);
3120         ib_dealloc_pd(sdev->pd);
3121 free_dev:
3122         kfree(sdev);
3123 err:
3124         sdev = NULL;
3125         pr_info("%s(%s) failed.\n", __func__, device->name);
3126         goto out;
3127 }
3128
3129 /**
3130  * srpt_remove_one - InfiniBand device removal callback function
3131  * @device: Describes a HCA.
3132  * @client_data: The value passed as the third argument to ib_set_client_data().
3133  */
3134 static void srpt_remove_one(struct ib_device *device, void *client_data)
3135 {
3136         struct srpt_device *sdev = client_data;
3137         int i;
3138
3139         if (!sdev) {
3140                 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
3141                 return;
3142         }
3143
3144         srpt_unregister_mad_agent(sdev);
3145
3146         ib_unregister_event_handler(&sdev->event_handler);
3147
3148         /* Cancel any work queued by the just unregistered IB event handler. */
3149         for (i = 0; i < sdev->device->phys_port_cnt; i++)
3150                 cancel_work_sync(&sdev->port[i].work);
3151
3152         if (sdev->cm_id)
3153                 ib_destroy_cm_id(sdev->cm_id);
3154
3155         ib_set_client_data(device, &srpt_client, NULL);
3156
3157         /*
3158          * Unregistering a target must happen after destroying sdev->cm_id
3159          * such that no new SRP_LOGIN_REQ information units can arrive while
3160          * destroying the target.
3161          */
3162         spin_lock(&srpt_dev_lock);
3163         list_del(&sdev->list);
3164         spin_unlock(&srpt_dev_lock);
3165
3166         for (i = 0; i < sdev->device->phys_port_cnt; i++)
3167                 srpt_release_sport(&sdev->port[i]);
3168
3169         srpt_free_srq(sdev);
3170
3171         ib_dealloc_pd(sdev->pd);
3172
3173         kfree(sdev);
3174 }
3175
3176 static struct ib_client srpt_client = {
3177         .name = DRV_NAME,
3178         .add = srpt_add_one,
3179         .remove = srpt_remove_one
3180 };
3181
3182 static int srpt_check_true(struct se_portal_group *se_tpg)
3183 {
3184         return 1;
3185 }
3186
3187 static int srpt_check_false(struct se_portal_group *se_tpg)
3188 {
3189         return 0;
3190 }
3191
3192 static char *srpt_get_fabric_name(void)
3193 {
3194         return "srpt";
3195 }
3196
3197 static struct srpt_port *srpt_tpg_to_sport(struct se_portal_group *tpg)
3198 {
3199         return tpg->se_tpg_wwn->priv;
3200 }
3201
3202 static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3203 {
3204         struct srpt_port *sport = srpt_tpg_to_sport(tpg);
3205
3206         WARN_ON_ONCE(tpg != &sport->port_guid_tpg &&
3207                      tpg != &sport->port_gid_tpg);
3208         return tpg == &sport->port_guid_tpg ? sport->port_guid :
3209                 sport->port_gid;
3210 }
3211
3212 static u16 srpt_get_tag(struct se_portal_group *tpg)
3213 {
3214         return 1;
3215 }
3216
3217 static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3218 {
3219         return 1;
3220 }
3221
3222 static void srpt_release_cmd(struct se_cmd *se_cmd)
3223 {
3224         struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3225                                 struct srpt_send_ioctx, cmd);
3226         struct srpt_rdma_ch *ch = ioctx->ch;
3227         unsigned long flags;
3228
3229         WARN_ON_ONCE(ioctx->state != SRPT_STATE_DONE &&
3230                      !(ioctx->cmd.transport_state & CMD_T_ABORTED));
3231
3232         if (ioctx->n_rw_ctx) {
3233                 srpt_free_rw_ctxs(ch, ioctx);
3234                 ioctx->n_rw_ctx = 0;
3235         }
3236
3237         spin_lock_irqsave(&ch->spinlock, flags);
3238         list_add(&ioctx->free_list, &ch->free_list);
3239         spin_unlock_irqrestore(&ch->spinlock, flags);
3240 }
3241
3242 /**
3243  * srpt_close_session - forcibly close a session
3244  * @se_sess: SCSI target session.
3245  *
3246  * Callback function invoked by the TCM core to clean up sessions associated
3247  * with a node ACL when the user invokes
3248  * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3249  */
3250 static void srpt_close_session(struct se_session *se_sess)
3251 {
3252         struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
3253
3254         srpt_disconnect_ch_sync(ch);
3255 }
3256
3257 /**
3258  * srpt_sess_get_index - return the value of scsiAttIntrPortIndex (SCSI-MIB)
3259  * @se_sess: SCSI target session.
3260  *
3261  * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3262  * This object represents an arbitrary integer used to uniquely identify a
3263  * particular attached remote initiator port to a particular SCSI target port
3264  * within a particular SCSI target device within a particular SCSI instance.
3265  */
3266 static u32 srpt_sess_get_index(struct se_session *se_sess)
3267 {
3268         return 0;
3269 }
3270
3271 static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3272 {
3273 }
3274
3275 /* Note: only used from inside debug printk's by the TCM core. */
3276 static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3277 {
3278         struct srpt_send_ioctx *ioctx;
3279
3280         ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3281         return ioctx->state;
3282 }
3283
3284 static int srpt_parse_guid(u64 *guid, const char *name)
3285 {
3286         u16 w[4];
3287         int ret = -EINVAL;
3288
3289         if (sscanf(name, "%hx:%hx:%hx:%hx", &w[0], &w[1], &w[2], &w[3]) != 4)
3290                 goto out;
3291         *guid = get_unaligned_be64(w);
3292         ret = 0;
3293 out:
3294         return ret;
3295 }
3296
3297 /**
3298  * srpt_parse_i_port_id - parse an initiator port ID
3299  * @name: ASCII representation of a 128-bit initiator port ID.
3300  * @i_port_id: Binary 128-bit port ID.
3301  */
3302 static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3303 {
3304         const char *p;
3305         unsigned len, count, leading_zero_bytes;
3306         int ret;
3307
3308         p = name;
3309         if (strncasecmp(p, "0x", 2) == 0)
3310                 p += 2;
3311         ret = -EINVAL;
3312         len = strlen(p);
3313         if (len % 2)
3314                 goto out;
3315         count = min(len / 2, 16U);
3316         leading_zero_bytes = 16 - count;
3317         memset(i_port_id, 0, leading_zero_bytes);
3318         ret = hex2bin(i_port_id + leading_zero_bytes, p, count);
3319
3320 out:
3321         return ret;
3322 }
3323
3324 /*
3325  * configfs callback function invoked for mkdir
3326  * /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3327  *
3328  * i_port_id must be an initiator port GUID, GID or IP address. See also the
3329  * target_alloc_session() calls in this driver. Examples of valid initiator
3330  * port IDs:
3331  * 0x0000000000000000505400fffe4a0b7b
3332  * 0000000000000000505400fffe4a0b7b
3333  * 5054:00ff:fe4a:0b7b
3334  * 192.168.122.76
3335  */
3336 static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
3337 {
3338         struct sockaddr_storage sa;
3339         u64 guid;
3340         u8 i_port_id[16];
3341         int ret;
3342
3343         ret = srpt_parse_guid(&guid, name);
3344         if (ret < 0)
3345                 ret = srpt_parse_i_port_id(i_port_id, name);
3346         if (ret < 0)
3347                 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, name, NULL,
3348                                            &sa);
3349         if (ret < 0)
3350                 pr_err("invalid initiator port ID %s\n", name);
3351         return ret;
3352 }
3353
3354 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
3355                 char *page)
3356 {
3357         struct se_portal_group *se_tpg = attrib_to_tpg(item);
3358         struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3359
3360         return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3361 }
3362
3363 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
3364                 const char *page, size_t count)
3365 {
3366         struct se_portal_group *se_tpg = attrib_to_tpg(item);
3367         struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3368         unsigned long val;
3369         int ret;
3370
3371         ret = kstrtoul(page, 0, &val);
3372         if (ret < 0) {
3373                 pr_err("kstrtoul() failed with ret: %d\n", ret);
3374                 return -EINVAL;
3375         }
3376         if (val > MAX_SRPT_RDMA_SIZE) {
3377                 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3378                         MAX_SRPT_RDMA_SIZE);
3379                 return -EINVAL;
3380         }
3381         if (val < DEFAULT_MAX_RDMA_SIZE) {
3382                 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3383                         val, DEFAULT_MAX_RDMA_SIZE);
3384                 return -EINVAL;
3385         }
3386         sport->port_attrib.srp_max_rdma_size = val;
3387
3388         return count;
3389 }
3390
3391 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
3392                 char *page)
3393 {
3394         struct se_portal_group *se_tpg = attrib_to_tpg(item);
3395         struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3396
3397         return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3398 }
3399
3400 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
3401                 const char *page, size_t count)
3402 {
3403         struct se_portal_group *se_tpg = attrib_to_tpg(item);
3404         struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3405         unsigned long val;
3406         int ret;
3407
3408         ret = kstrtoul(page, 0, &val);
3409         if (ret < 0) {
3410                 pr_err("kstrtoul() failed with ret: %d\n", ret);
3411                 return -EINVAL;
3412         }
3413         if (val > MAX_SRPT_RSP_SIZE) {
3414                 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3415                         MAX_SRPT_RSP_SIZE);
3416                 return -EINVAL;
3417         }
3418         if (val < MIN_MAX_RSP_SIZE) {
3419                 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3420                         MIN_MAX_RSP_SIZE);
3421                 return -EINVAL;
3422         }
3423         sport->port_attrib.srp_max_rsp_size = val;
3424
3425         return count;
3426 }
3427
3428 static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
3429                 char *page)
3430 {
3431         struct se_portal_group *se_tpg = attrib_to_tpg(item);
3432         struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3433
3434         return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3435 }
3436
3437 static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
3438                 const char *page, size_t count)
3439 {
3440         struct se_portal_group *se_tpg = attrib_to_tpg(item);
3441         struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3442         unsigned long val;
3443         int ret;
3444
3445         ret = kstrtoul(page, 0, &val);
3446         if (ret < 0) {
3447                 pr_err("kstrtoul() failed with ret: %d\n", ret);
3448                 return -EINVAL;
3449         }
3450         if (val > MAX_SRPT_SRQ_SIZE) {
3451                 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3452                         MAX_SRPT_SRQ_SIZE);
3453                 return -EINVAL;
3454         }
3455         if (val < MIN_SRPT_SRQ_SIZE) {
3456                 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3457                         MIN_SRPT_SRQ_SIZE);
3458                 return -EINVAL;
3459         }
3460         sport->port_attrib.srp_sq_size = val;
3461
3462         return count;
3463 }
3464
3465 static ssize_t srpt_tpg_attrib_use_srq_show(struct config_item *item,
3466                                             char *page)
3467 {
3468         struct se_portal_group *se_tpg = attrib_to_tpg(item);
3469         struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3470
3471         return sprintf(page, "%d\n", sport->port_attrib.use_srq);
3472 }
3473
3474 static ssize_t srpt_tpg_attrib_use_srq_store(struct config_item *item,
3475                                              const char *page, size_t count)
3476 {
3477         struct se_portal_group *se_tpg = attrib_to_tpg(item);
3478         struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3479         struct srpt_device *sdev = sport->sdev;
3480         unsigned long val;
3481         bool enabled;
3482         int ret;
3483
3484         ret = kstrtoul(page, 0, &val);
3485         if (ret < 0)
3486                 return ret;
3487         if (val != !!val)
3488                 return -EINVAL;
3489
3490         ret = mutex_lock_interruptible(&sdev->sdev_mutex);
3491         if (ret < 0)
3492                 return ret;
3493         ret = mutex_lock_interruptible(&sport->mutex);
3494         if (ret < 0)
3495                 goto unlock_sdev;
3496         enabled = sport->enabled;
3497         /* Log out all initiator systems before changing 'use_srq'. */
3498         srpt_set_enabled(sport, false);
3499         sport->port_attrib.use_srq = val;
3500         srpt_use_srq(sdev, sport->port_attrib.use_srq);
3501         srpt_set_enabled(sport, enabled);
3502         ret = count;
3503         mutex_unlock(&sport->mutex);
3504 unlock_sdev:
3505         mutex_unlock(&sdev->sdev_mutex);
3506
3507         return ret;
3508 }
3509
3510 CONFIGFS_ATTR(srpt_tpg_attrib_,  srp_max_rdma_size);
3511 CONFIGFS_ATTR(srpt_tpg_attrib_,  srp_max_rsp_size);
3512 CONFIGFS_ATTR(srpt_tpg_attrib_,  srp_sq_size);
3513 CONFIGFS_ATTR(srpt_tpg_attrib_,  use_srq);
3514
3515 static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
3516         &srpt_tpg_attrib_attr_srp_max_rdma_size,
3517         &srpt_tpg_attrib_attr_srp_max_rsp_size,
3518         &srpt_tpg_attrib_attr_srp_sq_size,
3519         &srpt_tpg_attrib_attr_use_srq,
3520         NULL,
3521 };
3522
3523 static struct rdma_cm_id *srpt_create_rdma_id(struct sockaddr *listen_addr)
3524 {
3525         struct rdma_cm_id *rdma_cm_id;
3526         int ret;
3527
3528         rdma_cm_id = rdma_create_id(&init_net, srpt_rdma_cm_handler,
3529                                     NULL, RDMA_PS_TCP, IB_QPT_RC);
3530         if (IS_ERR(rdma_cm_id)) {
3531                 pr_err("RDMA/CM ID creation failed: %ld\n",
3532                        PTR_ERR(rdma_cm_id));
3533                 goto out;
3534         }
3535
3536         ret = rdma_bind_addr(rdma_cm_id, listen_addr);
3537         if (ret) {
3538                 char addr_str[64];
3539
3540                 snprintf(addr_str, sizeof(addr_str), "%pISp", listen_addr);
3541                 pr_err("Binding RDMA/CM ID to address %s failed: %d\n",
3542                        addr_str, ret);
3543                 rdma_destroy_id(rdma_cm_id);
3544                 rdma_cm_id = ERR_PTR(ret);
3545                 goto out;
3546         }
3547
3548         ret = rdma_listen(rdma_cm_id, 128);
3549         if (ret) {
3550                 pr_err("rdma_listen() failed: %d\n", ret);
3551                 rdma_destroy_id(rdma_cm_id);
3552                 rdma_cm_id = ERR_PTR(ret);
3553         }
3554
3555 out:
3556         return rdma_cm_id;
3557 }
3558
3559 static ssize_t srpt_rdma_cm_port_show(struct config_item *item, char *page)
3560 {
3561         return sprintf(page, "%d\n", rdma_cm_port);
3562 }
3563
3564 static ssize_t srpt_rdma_cm_port_store(struct config_item *item,
3565                                        const char *page, size_t count)
3566 {
3567         struct sockaddr_in  addr4 = { .sin_family  = AF_INET  };
3568         struct sockaddr_in6 addr6 = { .sin6_family = AF_INET6 };
3569         struct rdma_cm_id *new_id = NULL;
3570         u16 val;
3571         int ret;
3572
3573         ret = kstrtou16(page, 0, &val);
3574         if (ret < 0)
3575                 return ret;
3576         ret = count;
3577         if (rdma_cm_port == val)
3578                 goto out;
3579
3580         if (val) {
3581                 addr6.sin6_port = cpu_to_be16(val);
3582                 new_id = srpt_create_rdma_id((struct sockaddr *)&addr6);
3583                 if (IS_ERR(new_id)) {
3584                         addr4.sin_port = cpu_to_be16(val);
3585                         new_id = srpt_create_rdma_id((struct sockaddr *)&addr4);
3586                         if (IS_ERR(new_id)) {
3587                                 ret = PTR_ERR(new_id);
3588                                 goto out;
3589                         }
3590                 }
3591         }
3592
3593         mutex_lock(&rdma_cm_mutex);
3594         rdma_cm_port = val;
3595         swap(rdma_cm_id, new_id);
3596         mutex_unlock(&rdma_cm_mutex);
3597
3598         if (new_id)
3599                 rdma_destroy_id(new_id);
3600         ret = count;
3601 out:
3602         return ret;
3603 }
3604
3605 CONFIGFS_ATTR(srpt_, rdma_cm_port);
3606
3607 static struct configfs_attribute *srpt_da_attrs[] = {
3608         &srpt_attr_rdma_cm_port,
3609         NULL,
3610 };
3611
3612 static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
3613 {
3614         struct se_portal_group *se_tpg = to_tpg(item);
3615         struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3616
3617         return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3618 }
3619
3620 static ssize_t srpt_tpg_enable_store(struct config_item *item,
3621                 const char *page, size_t count)
3622 {
3623         struct se_portal_group *se_tpg = to_tpg(item);
3624         struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3625         unsigned long tmp;
3626         int ret;
3627
3628         ret = kstrtoul(page, 0, &tmp);
3629         if (ret < 0) {
3630                 pr_err("Unable to extract srpt_tpg_store_enable\n");
3631                 return -EINVAL;
3632         }
3633
3634         if ((tmp != 0) && (tmp != 1)) {
3635                 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
3636                 return -EINVAL;
3637         }
3638
3639         mutex_lock(&sport->mutex);
3640         srpt_set_enabled(sport, tmp);
3641         mutex_unlock(&sport->mutex);
3642
3643         return count;
3644 }
3645
3646 CONFIGFS_ATTR(srpt_tpg_, enable);
3647
3648 static struct configfs_attribute *srpt_tpg_attrs[] = {
3649         &srpt_tpg_attr_enable,
3650         NULL,
3651 };
3652
3653 /**
3654  * srpt_make_tpg - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port/$tpg
3655  * @wwn: Corresponds to $driver/$port.
3656  * @name: $tpg.
3657  */
3658 static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3659                                              const char *name)
3660 {
3661         struct srpt_port *sport = wwn->priv;
3662         static struct se_portal_group *tpg;
3663         int res;
3664
3665         WARN_ON_ONCE(wwn != &sport->port_guid_wwn &&
3666                      wwn != &sport->port_gid_wwn);
3667         tpg = wwn == &sport->port_guid_wwn ? &sport->port_guid_tpg :
3668                 &sport->port_gid_tpg;
3669         res = core_tpg_register(wwn, tpg, SCSI_PROTOCOL_SRP);
3670         if (res)
3671                 return ERR_PTR(res);
3672
3673         return tpg;
3674 }
3675
3676 /**
3677  * srpt_drop_tpg - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port/$tpg
3678  * @tpg: Target portal group to deregister.
3679  */
3680 static void srpt_drop_tpg(struct se_portal_group *tpg)
3681 {
3682         struct srpt_port *sport = srpt_tpg_to_sport(tpg);
3683
3684         sport->enabled = false;
3685         core_tpg_deregister(tpg);
3686 }
3687
3688 /**
3689  * srpt_make_tport - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port
3690  * @tf: Not used.
3691  * @group: Not used.
3692  * @name: $port.
3693  */
3694 static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3695                                       struct config_group *group,
3696                                       const char *name)
3697 {
3698         return srpt_lookup_wwn(name) ? : ERR_PTR(-EINVAL);
3699 }
3700
3701 /**
3702  * srpt_drop_tport - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port
3703  * @wwn: $port.
3704  */
3705 static void srpt_drop_tport(struct se_wwn *wwn)
3706 {
3707 }
3708
3709 static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
3710 {
3711         return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3712 }
3713
3714 CONFIGFS_ATTR_RO(srpt_wwn_, version);
3715
3716 static struct configfs_attribute *srpt_wwn_attrs[] = {
3717         &srpt_wwn_attr_version,
3718         NULL,
3719 };
3720
3721 static const struct target_core_fabric_ops srpt_template = {
3722         .module                         = THIS_MODULE,
3723         .name                           = "srpt",
3724         .get_fabric_name                = srpt_get_fabric_name,
3725         .tpg_get_wwn                    = srpt_get_fabric_wwn,
3726         .tpg_get_tag                    = srpt_get_tag,
3727         .tpg_check_demo_mode            = srpt_check_false,
3728         .tpg_check_demo_mode_cache      = srpt_check_true,
3729         .tpg_check_demo_mode_write_protect = srpt_check_true,
3730         .tpg_check_prod_mode_write_protect = srpt_check_false,
3731         .tpg_get_inst_index             = srpt_tpg_get_inst_index,
3732         .release_cmd                    = srpt_release_cmd,
3733         .check_stop_free                = srpt_check_stop_free,
3734         .close_session                  = srpt_close_session,
3735         .sess_get_index                 = srpt_sess_get_index,
3736         .sess_get_initiator_sid         = NULL,
3737         .write_pending                  = srpt_write_pending,
3738         .write_pending_status           = srpt_write_pending_status,
3739         .set_default_node_attributes    = srpt_set_default_node_attrs,
3740         .get_cmd_state                  = srpt_get_tcm_cmd_state,
3741         .queue_data_in                  = srpt_queue_data_in,
3742         .queue_status                   = srpt_queue_status,
3743         .queue_tm_rsp                   = srpt_queue_tm_rsp,
3744         .aborted_task                   = srpt_aborted_task,
3745         /*
3746          * Setup function pointers for generic logic in
3747          * target_core_fabric_configfs.c
3748          */
3749         .fabric_make_wwn                = srpt_make_tport,
3750         .fabric_drop_wwn                = srpt_drop_tport,
3751         .fabric_make_tpg                = srpt_make_tpg,
3752         .fabric_drop_tpg                = srpt_drop_tpg,
3753         .fabric_init_nodeacl            = srpt_init_nodeacl,
3754
3755         .tfc_discovery_attrs            = srpt_da_attrs,
3756         .tfc_wwn_attrs                  = srpt_wwn_attrs,
3757         .tfc_tpg_base_attrs             = srpt_tpg_attrs,
3758         .tfc_tpg_attrib_attrs           = srpt_tpg_attrib_attrs,
3759 };
3760
3761 /**
3762  * srpt_init_module - kernel module initialization
3763  *
3764  * Note: Since ib_register_client() registers callback functions, and since at
3765  * least one of these callback functions (srpt_add_one()) calls target core
3766  * functions, this driver must be registered with the target core before
3767  * ib_register_client() is called.
3768  */
3769 static int __init srpt_init_module(void)
3770 {
3771         int ret;
3772
3773         ret = -EINVAL;
3774         if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
3775                 pr_err("invalid value %d for kernel module parameter"
3776                        " srp_max_req_size -- must be at least %d.\n",
3777                        srp_max_req_size, MIN_MAX_REQ_SIZE);
3778                 goto out;
3779         }
3780
3781         if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3782             || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
3783                 pr_err("invalid value %d for kernel module parameter"
3784                        " srpt_srq_size -- must be in the range [%d..%d].\n",
3785                        srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3786                 goto out;
3787         }
3788
3789         ret = target_register_template(&srpt_template);
3790         if (ret)
3791                 goto out;
3792
3793         ret = ib_register_client(&srpt_client);
3794         if (ret) {
3795                 pr_err("couldn't register IB client\n");
3796                 goto out_unregister_target;
3797         }
3798
3799         return 0;
3800
3801 out_unregister_target:
3802         target_unregister_template(&srpt_template);
3803 out:
3804         return ret;
3805 }
3806
3807 static void __exit srpt_cleanup_module(void)
3808 {
3809         if (rdma_cm_id)
3810                 rdma_destroy_id(rdma_cm_id);
3811         ib_unregister_client(&srpt_client);
3812         target_unregister_template(&srpt_template);
3813 }
3814
3815 module_init(srpt_init_module);
3816 module_exit(srpt_cleanup_module);