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