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