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