blob: 402275be0931e1c3218a821a0633ec9f924e78b4 [file] [log] [blame]
Bart Van Asschea42d9852011-10-14 01:30:46 +00001/*
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>
Bart Van Asscheba929992015-05-08 10:11:12 +020044#include <scsi/scsi_proto.h>
Bart Van Asschea42d9852011-10-14 01:30:46 +000045#include <scsi/scsi_tcq.h>
Bart Van Asschea42d9852011-10-14 01:30:46 +000046#include <target/target_core_base.h>
Bart Van Asschea42d9852011-10-14 01:30:46 +000047#include <target/target_core_fabric.h>
Bart Van Asschea42d9852011-10-14 01:30:46 +000048#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;
Roland Dreier486d8b92012-02-02 12:55:58 -080070static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
71static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
Bart Van Asschea42d9852011-10-14 01:30:46 +000072
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
83static int srpt_get_u64_x(char *buffer, struct kernel_param *kp)
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;
Bart Van Assche2c7f37f2016-02-11 11:06:55 -080094static void srpt_release_cmd(struct se_cmd *se_cmd);
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -080095static void srpt_free_ch(struct kref *kref);
Bart Van Asschea42d9852011-10-14 01:30:46 +000096static int srpt_queue_status(struct se_cmd *cmd);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +020097static 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);
Bart Van Assche387add42016-02-11 11:10:09 -080099static void srpt_process_wait_list(struct srpt_rdma_ch *ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000100
Bart Van Asschef130c222016-02-11 11:05:38 -0800101/*
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.
Bart Van Asschea42d9852011-10-14 01:30:46 +0000104 */
Bart Van Asschef130c222016-02-11 11:05:38 -0800105static bool srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new)
Bart Van Asschea42d9852011-10-14 01:30:46 +0000106{
107 unsigned long flags;
108 enum rdma_ch_state prev;
Bart Van Asschef130c222016-02-11 11:05:38 -0800109 bool changed = false;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000110
111 spin_lock_irqsave(&ch->spinlock, flags);
112 prev = ch->state;
Bart Van Asschef130c222016-02-11 11:05:38 -0800113 if (new > prev) {
Bart Van Asschea42d9852011-10-14 01:30:46 +0000114 ch->state = new;
Bart Van Asschef130c222016-02-11 11:05:38 -0800115 changed = true;
116 }
Bart Van Asschea42d9852011-10-14 01:30:46 +0000117 spin_unlock_irqrestore(&ch->spinlock, flags);
Bart Van Asschef130c222016-02-11 11:05:38 -0800118
119 return changed;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000120}
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,
Bart Van Asschef68cba4e92016-02-11 11:04:20 -0800141 sdev->device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000142
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:
Doug Ledford2aa1cf62014-08-12 19:20:10 -0400156 case IB_EVENT_GID_CHANGE:
Bart Van Asschea42d9852011-10-14 01:30:46 +0000157 /* 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:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400165 pr_err("received unrecognized IB event %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +0000166 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{
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400176 pr_info("SRQ event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000177}
178
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -0800179static 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
Bart Van Asschea42d9852011-10-14 01:30:46 +0000196/**
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",
Bart Van Assche33912d72016-02-11 11:04:43 -0800202 event->event, ch->cm_id, ch->sess_name, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000203
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:
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -0800209 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));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000212 break;
213 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400214 pr_err("received unrecognized IB QP event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000215 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;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800254 memset(cif, 0, sizeof(*cif));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000255 cif->base_version = 1;
256 cif->class_version = 1;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000257
Erez Shitrit507f6af2016-05-25 22:02:04 +0300258 ib_set_cpi_resp_time(cif, 20);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000259 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;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530275 ioui->change_id = cpu_to_be16(1);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000276 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;
298
299 iocp = (struct ib_dm_ioc_profile *)mad->data;
300
301 if (!slot || slot > 16) {
302 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530303 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000304 return;
305 }
306
307 if (slot > 2) {
308 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530309 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000310 return;
311 }
312
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800313 memset(iocp, 0, sizeof(*iocp));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000314 strcpy(iocp->id_string, SRPT_ID_STRING);
315 iocp->guid = cpu_to_be64(srpt_service_guid);
Or Gerlitz4a061b22015-12-18 10:59:46 +0200316 iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
317 iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
318 iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
319 iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000320 iocp->subsys_device_id = 0x0;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530321 iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
322 iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
323 iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
324 iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000325 iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
326 iocp->rdma_read_depth = 4;
327 iocp->send_size = cpu_to_be32(srp_max_req_size);
328 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
329 1U << 24));
330 iocp->num_svc_entries = 1;
331 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
332 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
333
334 mad->mad_hdr.status = 0;
335}
336
337/**
338 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
339 *
340 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
341 * Specification. See also section B.7, table B.8 in the SRP r16a document.
342 */
343static void srpt_get_svc_entries(u64 ioc_guid,
344 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
345{
346 struct ib_dm_svc_entries *svc_entries;
347
348 WARN_ON(!ioc_guid);
349
350 if (!slot || slot > 16) {
351 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530352 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000353 return;
354 }
355
356 if (slot > 2 || lo > hi || hi > 1) {
357 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530358 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000359 return;
360 }
361
362 svc_entries = (struct ib_dm_svc_entries *)mad->data;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800363 memset(svc_entries, 0, sizeof(*svc_entries));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000364 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
365 snprintf(svc_entries->service_entries[0].name,
366 sizeof(svc_entries->service_entries[0].name),
367 "%s%016llx",
368 SRP_SERVICE_NAME_PREFIX,
369 ioc_guid);
370
371 mad->mad_hdr.status = 0;
372}
373
374/**
375 * srpt_mgmt_method_get() - Process a received management datagram.
376 * @sp: source port through which the MAD has been received.
377 * @rq_mad: received MAD.
378 * @rsp_mad: response MAD.
379 */
380static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
381 struct ib_dm_mad *rsp_mad)
382{
383 u16 attr_id;
384 u32 slot;
385 u8 hi, lo;
386
387 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
388 switch (attr_id) {
389 case DM_ATTR_CLASS_PORT_INFO:
390 srpt_get_class_port_info(rsp_mad);
391 break;
392 case DM_ATTR_IOU_INFO:
393 srpt_get_iou(rsp_mad);
394 break;
395 case DM_ATTR_IOC_PROFILE:
396 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
397 srpt_get_ioc(sp, slot, rsp_mad);
398 break;
399 case DM_ATTR_SVC_ENTRIES:
400 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
401 hi = (u8) ((slot >> 8) & 0xff);
402 lo = (u8) (slot & 0xff);
403 slot = (u16) ((slot >> 16) & 0xffff);
404 srpt_get_svc_entries(srpt_service_guid,
405 slot, hi, lo, rsp_mad);
406 break;
407 default:
408 rsp_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530409 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000410 break;
411 }
412}
413
414/**
415 * srpt_mad_send_handler() - Post MAD-send callback function.
416 */
417static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
418 struct ib_mad_send_wc *mad_wc)
419{
Dasaratharaman Chandramouli36523152017-04-29 14:41:22 -0400420 rdma_destroy_ah(mad_wc->send_buf->ah);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000421 ib_free_send_mad(mad_wc->send_buf);
422}
423
424/**
425 * srpt_mad_recv_handler() - MAD reception callback function.
426 */
427static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
Christoph Hellwigca281262016-01-04 14:15:58 +0100428 struct ib_mad_send_buf *send_buf,
Bart Van Asschea42d9852011-10-14 01:30:46 +0000429 struct ib_mad_recv_wc *mad_wc)
430{
431 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
432 struct ib_ah *ah;
433 struct ib_mad_send_buf *rsp;
434 struct ib_dm_mad *dm_mad;
435
436 if (!mad_wc || !mad_wc->recv_buf.mad)
437 return;
438
439 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
440 mad_wc->recv_buf.grh, mad_agent->port_num);
441 if (IS_ERR(ah))
442 goto err;
443
444 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
445
446 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
447 mad_wc->wc->pkey_index, 0,
448 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
Ira Weinyda2dfaa2015-06-06 14:38:28 -0400449 GFP_KERNEL,
450 IB_MGMT_BASE_VERSION);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000451 if (IS_ERR(rsp))
452 goto err_rsp;
453
454 rsp->ah = ah;
455
456 dm_mad = rsp->mad;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800457 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000458 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
459 dm_mad->mad_hdr.status = 0;
460
461 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
462 case IB_MGMT_METHOD_GET:
463 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
464 break;
465 case IB_MGMT_METHOD_SET:
466 dm_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530467 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000468 break;
469 default:
470 dm_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530471 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000472 break;
473 }
474
475 if (!ib_post_send_mad(rsp, NULL)) {
476 ib_free_recv_mad(mad_wc);
477 /* will destroy_ah & free_send_mad in send completion */
478 return;
479 }
480
481 ib_free_send_mad(rsp);
482
483err_rsp:
Dasaratharaman Chandramouli36523152017-04-29 14:41:22 -0400484 rdma_destroy_ah(ah);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000485err:
486 ib_free_recv_mad(mad_wc);
487}
488
489/**
490 * srpt_refresh_port() - Configure a HCA port.
491 *
492 * Enable InfiniBand management datagram processing, update the cached sm_lid,
493 * lid and gid values, and register a callback function for processing MADs
494 * on the specified port.
495 *
496 * Note: It is safe to call this function more than once for the same port.
497 */
498static int srpt_refresh_port(struct srpt_port *sport)
499{
500 struct ib_mad_reg_req reg_req;
501 struct ib_port_modify port_modify;
502 struct ib_port_attr port_attr;
Bart Van Assche2bce1a62016-12-09 11:00:33 -0800503 __be16 *guid;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000504 int ret;
505
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800506 memset(&port_modify, 0, sizeof(port_modify));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000507 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
508 port_modify.clr_port_cap_mask = 0;
509
510 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
511 if (ret)
512 goto err_mod_port;
513
514 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
515 if (ret)
516 goto err_query_port;
517
518 sport->sm_lid = port_attr.sm_lid;
519 sport->lid = port_attr.lid;
520
Matan Barak55ee3ab2015-10-15 18:38:45 +0300521 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid,
522 NULL);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000523 if (ret)
524 goto err_query_port;
525
Bart Van Assche2bce1a62016-12-09 11:00:33 -0800526 sport->port_guid_wwn.priv = sport;
527 guid = (__be16 *)&sport->gid.global.interface_id;
Doug Ledford716b076b2016-08-24 12:14:19 -0400528 snprintf(sport->port_guid, sizeof(sport->port_guid),
Bart Van Assche2bce1a62016-12-09 11:00:33 -0800529 "%04x:%04x:%04x:%04x",
530 be16_to_cpu(guid[0]), be16_to_cpu(guid[1]),
531 be16_to_cpu(guid[2]), be16_to_cpu(guid[3]));
532 sport->port_gid_wwn.priv = sport;
533 snprintf(sport->port_gid, sizeof(sport->port_gid),
534 "0x%016llx%016llx",
535 be64_to_cpu(sport->gid.global.subnet_prefix),
536 be64_to_cpu(sport->gid.global.interface_id));
Doug Ledford716b076b2016-08-24 12:14:19 -0400537
Bart Van Asschea42d9852011-10-14 01:30:46 +0000538 if (!sport->mad_agent) {
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800539 memset(&reg_req, 0, sizeof(reg_req));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000540 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
541 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
542 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
543 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
544
545 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
546 sport->port,
547 IB_QPT_GSI,
548 &reg_req, 0,
549 srpt_mad_send_handler,
550 srpt_mad_recv_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -0400551 sport, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000552 if (IS_ERR(sport->mad_agent)) {
553 ret = PTR_ERR(sport->mad_agent);
554 sport->mad_agent = NULL;
555 goto err_query_port;
556 }
557 }
558
559 return 0;
560
561err_query_port:
562
563 port_modify.set_port_cap_mask = 0;
564 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
565 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
566
567err_mod_port:
568
569 return ret;
570}
571
572/**
573 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
574 *
575 * Note: It is safe to call this function more than once for the same device.
576 */
577static void srpt_unregister_mad_agent(struct srpt_device *sdev)
578{
579 struct ib_port_modify port_modify = {
580 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
581 };
582 struct srpt_port *sport;
583 int i;
584
585 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
586 sport = &sdev->port[i - 1];
587 WARN_ON(sport->port != i);
588 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400589 pr_err("disabling MAD processing failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000590 if (sport->mad_agent) {
591 ib_unregister_mad_agent(sport->mad_agent);
592 sport->mad_agent = NULL;
593 }
594 }
595}
596
597/**
598 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
599 */
600static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
601 int ioctx_size, int dma_size,
602 enum dma_data_direction dir)
603{
604 struct srpt_ioctx *ioctx;
605
606 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
607 if (!ioctx)
608 goto err;
609
610 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
611 if (!ioctx->buf)
612 goto err_free_ioctx;
613
614 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
615 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
616 goto err_free_buf;
617
618 return ioctx;
619
620err_free_buf:
621 kfree(ioctx->buf);
622err_free_ioctx:
623 kfree(ioctx);
624err:
625 return NULL;
626}
627
628/**
629 * srpt_free_ioctx() - Free an SRPT I/O context structure.
630 */
631static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
632 int dma_size, enum dma_data_direction dir)
633{
634 if (!ioctx)
635 return;
636
637 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
638 kfree(ioctx->buf);
639 kfree(ioctx);
640}
641
642/**
643 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
644 * @sdev: Device to allocate the I/O context ring for.
645 * @ring_size: Number of elements in the I/O context ring.
646 * @ioctx_size: I/O context size.
647 * @dma_size: DMA buffer size.
648 * @dir: DMA data direction.
649 */
650static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
651 int ring_size, int ioctx_size,
652 int dma_size, enum dma_data_direction dir)
653{
654 struct srpt_ioctx **ring;
655 int i;
656
657 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
658 && ioctx_size != sizeof(struct srpt_send_ioctx));
659
660 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
661 if (!ring)
662 goto out;
663 for (i = 0; i < ring_size; ++i) {
664 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
665 if (!ring[i])
666 goto err;
667 ring[i]->index = i;
668 }
669 goto out;
670
671err:
672 while (--i >= 0)
673 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
674 kfree(ring);
Jesper Juhl715252d2012-02-04 23:49:40 +0100675 ring = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000676out:
677 return ring;
678}
679
680/**
681 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
682 */
683static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
684 struct srpt_device *sdev, int ring_size,
685 int dma_size, enum dma_data_direction dir)
686{
687 int i;
688
689 for (i = 0; i < ring_size; ++i)
690 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
691 kfree(ioctx_ring);
692}
693
694/**
695 * srpt_get_cmd_state() - Get the state of a SCSI command.
696 */
697static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
698{
699 enum srpt_command_state state;
700 unsigned long flags;
701
702 BUG_ON(!ioctx);
703
704 spin_lock_irqsave(&ioctx->spinlock, flags);
705 state = ioctx->state;
706 spin_unlock_irqrestore(&ioctx->spinlock, flags);
707 return state;
708}
709
710/**
711 * srpt_set_cmd_state() - Set the state of a SCSI command.
712 *
713 * Does not modify the state of aborted commands. Returns the previous command
714 * state.
715 */
716static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
717 enum srpt_command_state new)
718{
719 enum srpt_command_state previous;
720 unsigned long flags;
721
722 BUG_ON(!ioctx);
723
724 spin_lock_irqsave(&ioctx->spinlock, flags);
725 previous = ioctx->state;
726 if (previous != SRPT_STATE_DONE)
727 ioctx->state = new;
728 spin_unlock_irqrestore(&ioctx->spinlock, flags);
729
730 return previous;
731}
732
733/**
734 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
735 *
736 * Returns true if and only if the previous command state was equal to 'old'.
737 */
738static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
739 enum srpt_command_state old,
740 enum srpt_command_state new)
741{
742 enum srpt_command_state previous;
743 unsigned long flags;
744
745 WARN_ON(!ioctx);
746 WARN_ON(old == SRPT_STATE_DONE);
747 WARN_ON(new == SRPT_STATE_NEW);
748
749 spin_lock_irqsave(&ioctx->spinlock, flags);
750 previous = ioctx->state;
751 if (previous == old)
752 ioctx->state = new;
753 spin_unlock_irqrestore(&ioctx->spinlock, flags);
754 return previous == old;
755}
756
757/**
758 * srpt_post_recv() - Post an IB receive request.
759 */
760static int srpt_post_recv(struct srpt_device *sdev,
761 struct srpt_recv_ioctx *ioctx)
762{
763 struct ib_sge list;
764 struct ib_recv_wr wr, *bad_wr;
765
766 BUG_ON(!sdev);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000767 list.addr = ioctx->ioctx.dma;
768 list.length = srp_max_req_size;
Jason Gunthorpe5a783952015-07-30 17:22:24 -0600769 list.lkey = sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000770
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200771 ioctx->ioctx.cqe.done = srpt_recv_done;
772 wr.wr_cqe = &ioctx->ioctx.cqe;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000773 wr.next = NULL;
774 wr.sg_list = &list;
775 wr.num_sge = 1;
776
777 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
778}
779
780/**
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -0800781 * srpt_zerolength_write() - Perform a zero-length RDMA write.
782 *
783 * A quote from the InfiniBand specification: C9-88: For an HCA responder
784 * using Reliable Connection service, for each zero-length RDMA READ or WRITE
785 * request, the R_Key shall not be validated, even if the request includes
786 * Immediate data.
787 */
788static int srpt_zerolength_write(struct srpt_rdma_ch *ch)
789{
790 struct ib_send_wr wr, *bad_wr;
791
792 memset(&wr, 0, sizeof(wr));
793 wr.opcode = IB_WR_RDMA_WRITE;
794 wr.wr_cqe = &ch->zw_cqe;
795 wr.send_flags = IB_SEND_SIGNALED;
796 return ib_post_send(ch->qp, &wr, &bad_wr);
797}
798
799static void srpt_zerolength_write_done(struct ib_cq *cq, struct ib_wc *wc)
800{
801 struct srpt_rdma_ch *ch = cq->cq_context;
802
Bart Van Assche387add42016-02-11 11:10:09 -0800803 if (wc->status == IB_WC_SUCCESS) {
804 srpt_process_wait_list(ch);
805 } else {
806 if (srpt_set_ch_state(ch, CH_DISCONNECTED))
807 schedule_work(&ch->release_work);
808 else
Dan Carpenter56586002016-03-18 08:41:59 +0300809 WARN_ONCE(1, "%s-%d\n", ch->sess_name, ch->qp->qp_num);
Bart Van Assche387add42016-02-11 11:10:09 -0800810 }
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -0800811}
812
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200813static int srpt_alloc_rw_ctxs(struct srpt_send_ioctx *ioctx,
814 struct srp_direct_buf *db, int nbufs, struct scatterlist **sg,
815 unsigned *sg_cnt)
816{
817 enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
818 struct srpt_rdma_ch *ch = ioctx->ch;
819 struct scatterlist *prev = NULL;
820 unsigned prev_nents;
821 int ret, i;
822
823 if (nbufs == 1) {
824 ioctx->rw_ctxs = &ioctx->s_rw_ctx;
825 } else {
826 ioctx->rw_ctxs = kmalloc_array(nbufs, sizeof(*ioctx->rw_ctxs),
827 GFP_KERNEL);
828 if (!ioctx->rw_ctxs)
829 return -ENOMEM;
830 }
831
832 for (i = ioctx->n_rw_ctx; i < nbufs; i++, db++) {
833 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
834 u64 remote_addr = be64_to_cpu(db->va);
835 u32 size = be32_to_cpu(db->len);
836 u32 rkey = be32_to_cpu(db->key);
837
838 ret = target_alloc_sgl(&ctx->sg, &ctx->nents, size, false,
839 i < nbufs - 1);
840 if (ret)
841 goto unwind;
842
843 ret = rdma_rw_ctx_init(&ctx->rw, ch->qp, ch->sport->port,
844 ctx->sg, ctx->nents, 0, remote_addr, rkey, dir);
845 if (ret < 0) {
846 target_free_sgl(ctx->sg, ctx->nents);
847 goto unwind;
848 }
849
850 ioctx->n_rdma += ret;
851 ioctx->n_rw_ctx++;
852
853 if (prev) {
854 sg_unmark_end(&prev[prev_nents - 1]);
855 sg_chain(prev, prev_nents + 1, ctx->sg);
856 } else {
857 *sg = ctx->sg;
858 }
859
860 prev = ctx->sg;
861 prev_nents = ctx->nents;
862
863 *sg_cnt += ctx->nents;
864 }
865
866 return 0;
867
868unwind:
869 while (--i >= 0) {
870 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
871
872 rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
873 ctx->sg, ctx->nents, dir);
874 target_free_sgl(ctx->sg, ctx->nents);
875 }
876 if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
877 kfree(ioctx->rw_ctxs);
878 return ret;
879}
880
881static void srpt_free_rw_ctxs(struct srpt_rdma_ch *ch,
882 struct srpt_send_ioctx *ioctx)
883{
884 enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
885 int i;
886
887 for (i = 0; i < ioctx->n_rw_ctx; i++) {
888 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
889
890 rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
891 ctx->sg, ctx->nents, dir);
892 target_free_sgl(ctx->sg, ctx->nents);
893 }
894
895 if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
896 kfree(ioctx->rw_ctxs);
897}
898
899static inline void *srpt_get_desc_buf(struct srp_cmd *srp_cmd)
900{
901 /*
902 * The pointer computations below will only be compiled correctly
903 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
904 * whether srp_cmd::add_data has been declared as a byte pointer.
905 */
906 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0) &&
907 !__same_type(srp_cmd->add_data[0], (u8)0));
908
909 /*
910 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
911 * CDB LENGTH' field are reserved and the size in bytes of this field
912 * is four times the value specified in bits 3..7. Hence the "& ~3".
913 */
914 return srp_cmd->add_data + (srp_cmd->add_cdb_len & ~3);
915}
916
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -0800917/**
Bart Van Asschea42d9852011-10-14 01:30:46 +0000918 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
919 * @ioctx: Pointer to the I/O context associated with the request.
920 * @srp_cmd: Pointer to the SRP_CMD request data.
921 * @dir: Pointer to the variable to which the transfer direction will be
922 * written.
923 * @data_len: Pointer to the variable to which the total data length of all
924 * descriptors in the SRP_CMD request will be written.
925 *
926 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
927 *
928 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
929 * -ENOMEM when memory allocation fails and zero upon success.
930 */
931static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200932 struct srp_cmd *srp_cmd, enum dma_data_direction *dir,
933 struct scatterlist **sg, unsigned *sg_cnt, u64 *data_len)
Bart Van Asschea42d9852011-10-14 01:30:46 +0000934{
Bart Van Asschea42d9852011-10-14 01:30:46 +0000935 BUG_ON(!dir);
936 BUG_ON(!data_len);
937
Bart Van Asschea42d9852011-10-14 01:30:46 +0000938 /*
939 * The lower four bits of the buffer format field contain the DATA-IN
940 * buffer descriptor format, and the highest four bits contain the
941 * DATA-OUT buffer descriptor format.
942 */
Bart Van Asschea42d9852011-10-14 01:30:46 +0000943 if (srp_cmd->buf_fmt & 0xf)
944 /* DATA-IN: transfer data from target to initiator (read). */
945 *dir = DMA_FROM_DEVICE;
946 else if (srp_cmd->buf_fmt >> 4)
947 /* DATA-OUT: transfer data from initiator to target (write). */
948 *dir = DMA_TO_DEVICE;
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200949 else
950 *dir = DMA_NONE;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000951
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200952 /* initialize data_direction early as srpt_alloc_rw_ctxs needs it */
953 ioctx->cmd.data_direction = *dir;
954
Bart Van Asschea42d9852011-10-14 01:30:46 +0000955 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
956 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200957 struct srp_direct_buf *db = srpt_get_desc_buf(srp_cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000958
Bart Van Asschea42d9852011-10-14 01:30:46 +0000959 *data_len = be32_to_cpu(db->len);
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200960 return srpt_alloc_rw_ctxs(ioctx, db, 1, sg, sg_cnt);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000961 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
962 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200963 struct srp_indirect_buf *idb = srpt_get_desc_buf(srp_cmd);
964 int nbufs = be32_to_cpu(idb->table_desc.len) /
965 sizeof(struct srp_direct_buf);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000966
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200967 if (nbufs >
Bart Van Asschea42d9852011-10-14 01:30:46 +0000968 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400969 pr_err("received unsupported SRP_CMD request"
Bart Van Asschea42d9852011-10-14 01:30:46 +0000970 " type (%u out + %u in != %u / %zu)\n",
971 srp_cmd->data_out_desc_cnt,
972 srp_cmd->data_in_desc_cnt,
973 be32_to_cpu(idb->table_desc.len),
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200974 sizeof(struct srp_direct_buf));
975 return -EINVAL;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000976 }
977
Bart Van Asschea42d9852011-10-14 01:30:46 +0000978 *data_len = be32_to_cpu(idb->len);
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200979 return srpt_alloc_rw_ctxs(ioctx, idb->desc_list, nbufs,
980 sg, sg_cnt);
981 } else {
982 *data_len = 0;
983 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000984 }
Bart Van Asschea42d9852011-10-14 01:30:46 +0000985}
986
987/**
988 * srpt_init_ch_qp() - Initialize queue pair attributes.
989 *
990 * Initialized the attributes of queue pair 'qp' by allowing local write,
991 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
992 */
993static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
994{
995 struct ib_qp_attr *attr;
996 int ret;
997
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800998 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000999 if (!attr)
1000 return -ENOMEM;
1001
1002 attr->qp_state = IB_QPS_INIT;
1003 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
1004 IB_ACCESS_REMOTE_WRITE;
1005 attr->port_num = ch->sport->port;
1006 attr->pkey_index = 0;
1007
1008 ret = ib_modify_qp(qp, attr,
1009 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
1010 IB_QP_PKEY_INDEX);
1011
1012 kfree(attr);
1013 return ret;
1014}
1015
1016/**
1017 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
1018 * @ch: channel of the queue pair.
1019 * @qp: queue pair to change the state of.
1020 *
1021 * Returns zero upon success and a negative value upon failure.
1022 *
1023 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1024 * If this structure ever becomes larger, it might be necessary to allocate
1025 * it dynamically instead of on the stack.
1026 */
1027static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1028{
1029 struct ib_qp_attr qp_attr;
1030 int attr_mask;
1031 int ret;
1032
1033 qp_attr.qp_state = IB_QPS_RTR;
1034 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1035 if (ret)
1036 goto out;
1037
1038 qp_attr.max_dest_rd_atomic = 4;
1039
1040 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1041
1042out:
1043 return ret;
1044}
1045
1046/**
1047 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
1048 * @ch: channel of the queue pair.
1049 * @qp: queue pair to change the state of.
1050 *
1051 * Returns zero upon success and a negative value upon failure.
1052 *
1053 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1054 * If this structure ever becomes larger, it might be necessary to allocate
1055 * it dynamically instead of on the stack.
1056 */
1057static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1058{
1059 struct ib_qp_attr qp_attr;
1060 int attr_mask;
1061 int ret;
1062
1063 qp_attr.qp_state = IB_QPS_RTS;
1064 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1065 if (ret)
1066 goto out;
1067
1068 qp_attr.max_rd_atomic = 4;
1069
1070 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1071
1072out:
1073 return ret;
1074}
1075
1076/**
1077 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1078 */
1079static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1080{
1081 struct ib_qp_attr qp_attr;
1082
1083 qp_attr.qp_state = IB_QPS_ERR;
1084 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1085}
1086
1087/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001088 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1089 */
1090static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1091{
1092 struct srpt_send_ioctx *ioctx;
Bart Van Assche3c968882016-04-07 15:55:04 -07001093 unsigned long flags;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001094
1095 BUG_ON(!ch);
1096
Bart Van Assche3c968882016-04-07 15:55:04 -07001097 ioctx = NULL;
1098 spin_lock_irqsave(&ch->spinlock, flags);
1099 if (!list_empty(&ch->free_list)) {
1100 ioctx = list_first_entry(&ch->free_list,
1101 struct srpt_send_ioctx, free_list);
1102 list_del(&ioctx->free_list);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001103 }
Bart Van Assche3c968882016-04-07 15:55:04 -07001104 spin_unlock_irqrestore(&ch->spinlock, flags);
1105
1106 if (!ioctx)
1107 return ioctx;
1108
1109 BUG_ON(ioctx->ch != ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001110 spin_lock_init(&ioctx->spinlock);
1111 ioctx->state = SRPT_STATE_NEW;
Bart Van Assche3c968882016-04-07 15:55:04 -07001112 ioctx->n_rdma = 0;
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001113 ioctx->n_rw_ctx = 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001114 init_completion(&ioctx->tx_done);
Bart Van Assche3c968882016-04-07 15:55:04 -07001115 ioctx->queue_status_only = false;
1116 /*
1117 * transport_init_se_cmd() does not initialize all fields, so do it
1118 * here.
1119 */
1120 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1121 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001122
1123 return ioctx;
1124}
1125
1126/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001127 * srpt_abort_cmd() - Abort a SCSI command.
1128 * @ioctx: I/O context associated with the SCSI command.
1129 * @context: Preferred execution context.
1130 */
1131static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1132{
1133 enum srpt_command_state state;
1134 unsigned long flags;
1135
1136 BUG_ON(!ioctx);
1137
1138 /*
1139 * If the command is in a state where the target core is waiting for
Bart Van Assche49f40162016-02-11 11:07:11 -08001140 * the ib_srpt driver, change the state to the next state.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001141 */
1142
1143 spin_lock_irqsave(&ioctx->spinlock, flags);
1144 state = ioctx->state;
1145 switch (state) {
1146 case SRPT_STATE_NEED_DATA:
1147 ioctx->state = SRPT_STATE_DATA_IN;
1148 break;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001149 case SRPT_STATE_CMD_RSP_SENT:
1150 case SRPT_STATE_MGMT_RSP_SENT:
1151 ioctx->state = SRPT_STATE_DONE;
1152 break;
1153 default:
Bart Van Assche49f40162016-02-11 11:07:11 -08001154 WARN_ONCE(true, "%s: unexpected I/O context state %d\n",
1155 __func__, state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001156 break;
1157 }
1158 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1159
Bart Van Assche13fdd442017-05-23 16:48:35 -07001160 pr_debug("Aborting cmd with state %d -> %d and tag %lld\n", state,
1161 ioctx->state, ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001162
1163 switch (state) {
1164 case SRPT_STATE_NEW:
1165 case SRPT_STATE_DATA_IN:
1166 case SRPT_STATE_MGMT:
Bart Van Assche49f40162016-02-11 11:07:11 -08001167 case SRPT_STATE_DONE:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001168 /*
1169 * Do nothing - defer abort processing until
1170 * srpt_queue_response() is invoked.
1171 */
Bart Van Asschea42d9852011-10-14 01:30:46 +00001172 break;
1173 case SRPT_STATE_NEED_DATA:
Bart Van Assche49f40162016-02-11 11:07:11 -08001174 pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag);
1175 transport_generic_request_failure(&ioctx->cmd,
1176 TCM_CHECK_CONDITION_ABORT_CMD);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001177 break;
1178 case SRPT_STATE_CMD_RSP_SENT:
1179 /*
1180 * SRP_RSP sending failed or the SRP_RSP send completion has
1181 * not been received in time.
1182 */
Bart Van Assche49f40162016-02-11 11:07:11 -08001183 transport_generic_free_cmd(&ioctx->cmd, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001184 break;
1185 case SRPT_STATE_MGMT_RSP_SENT:
Bart Van Assche49f40162016-02-11 11:07:11 -08001186 transport_generic_free_cmd(&ioctx->cmd, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001187 break;
1188 default:
Grant Grundler532ec6f2013-03-26 21:48:28 +00001189 WARN(1, "Unexpected command state (%d)", state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001190 break;
1191 }
1192
Bart Van Asschea42d9852011-10-14 01:30:46 +00001193 return state;
1194}
1195
1196/**
Christoph Hellwige672a472012-07-08 15:58:43 -04001197 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1198 * the data that has been transferred via IB RDMA had to be postponed until the
Masanari Iida142ad5d2012-08-10 00:07:58 +00001199 * check_stop_free() callback. None of this is necessary anymore and needs to
Christoph Hellwige672a472012-07-08 15:58:43 -04001200 * be cleaned up.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001201 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001202static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001203{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001204 struct srpt_rdma_ch *ch = cq->cq_context;
1205 struct srpt_send_ioctx *ioctx =
Bart Van Assche19f57292015-12-31 09:56:43 +01001206 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001207
Bart Van Asschea42d9852011-10-14 01:30:46 +00001208 WARN_ON(ioctx->n_rdma <= 0);
1209 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001210 ioctx->n_rdma = 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001211
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001212 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1213 pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
1214 ioctx, wc->status);
1215 srpt_abort_cmd(ioctx);
1216 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001217 }
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001218
1219 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1220 SRPT_STATE_DATA_IN))
1221 target_execute_cmd(&ioctx->cmd);
1222 else
1223 pr_err("%s[%d]: wrong state = %d\n", __func__,
1224 __LINE__, srpt_get_cmd_state(ioctx));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001225}
1226
Bart Van Asschea42d9852011-10-14 01:30:46 +00001227/**
1228 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1229 * @ch: RDMA channel through which the request has been received.
1230 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1231 * be built in the buffer ioctx->buf points at and hence this function will
1232 * overwrite the request data.
1233 * @tag: tag of the request for which this response is being generated.
1234 * @status: value for the STATUS field of the SRP_RSP information unit.
1235 *
1236 * Returns the size in bytes of the SRP_RSP response.
1237 *
1238 * An SRP_RSP response contains a SCSI status or service response. See also
1239 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1240 * response. See also SPC-2 for more information about sense data.
1241 */
1242static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1243 struct srpt_send_ioctx *ioctx, u64 tag,
1244 int status)
1245{
1246 struct srp_rsp *srp_rsp;
1247 const u8 *sense_data;
1248 int sense_data_len, max_sense_len;
1249
1250 /*
1251 * The lowest bit of all SAM-3 status codes is zero (see also
1252 * paragraph 5.3 in SAM-3).
1253 */
1254 WARN_ON(status & 1);
1255
1256 srp_rsp = ioctx->ioctx.buf;
1257 BUG_ON(!srp_rsp);
1258
1259 sense_data = ioctx->sense_data;
1260 sense_data_len = ioctx->cmd.scsi_sense_length;
1261 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1262
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08001263 memset(srp_rsp, 0, sizeof(*srp_rsp));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001264 srp_rsp->opcode = SRP_RSP;
1265 srp_rsp->req_lim_delta =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301266 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001267 srp_rsp->tag = tag;
1268 srp_rsp->status = status;
1269
1270 if (sense_data_len) {
1271 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1272 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1273 if (sense_data_len > max_sense_len) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001274 pr_warn("truncated sense data from %d to %d"
1275 " bytes\n", sense_data_len, max_sense_len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001276 sense_data_len = max_sense_len;
1277 }
1278
1279 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1280 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1281 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1282 }
1283
1284 return sizeof(*srp_rsp) + sense_data_len;
1285}
1286
1287/**
1288 * srpt_build_tskmgmt_rsp() - Build a task management response.
1289 * @ch: RDMA channel through which the request has been received.
1290 * @ioctx: I/O context in which the SRP_RSP response will be built.
1291 * @rsp_code: RSP_CODE that will be stored in the response.
1292 * @tag: Tag of the request for which this response is being generated.
1293 *
1294 * Returns the size in bytes of the SRP_RSP response.
1295 *
1296 * An SRP_RSP response contains a SCSI status or service response. See also
1297 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1298 * response.
1299 */
1300static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1301 struct srpt_send_ioctx *ioctx,
1302 u8 rsp_code, u64 tag)
1303{
1304 struct srp_rsp *srp_rsp;
1305 int resp_data_len;
1306 int resp_len;
1307
Jack Wangc807f642013-09-30 10:09:05 +02001308 resp_data_len = 4;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001309 resp_len = sizeof(*srp_rsp) + resp_data_len;
1310
1311 srp_rsp = ioctx->ioctx.buf;
1312 BUG_ON(!srp_rsp);
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08001313 memset(srp_rsp, 0, sizeof(*srp_rsp));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001314
1315 srp_rsp->opcode = SRP_RSP;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301316 srp_rsp->req_lim_delta =
1317 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001318 srp_rsp->tag = tag;
1319
Jack Wangc807f642013-09-30 10:09:05 +02001320 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1321 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1322 srp_rsp->data[3] = rsp_code;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001323
1324 return resp_len;
1325}
1326
Bart Van Asschea42d9852011-10-14 01:30:46 +00001327static int srpt_check_stop_free(struct se_cmd *cmd)
1328{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001329 struct srpt_send_ioctx *ioctx = container_of(cmd,
1330 struct srpt_send_ioctx, cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001331
Bart Van Asscheafc16602015-04-27 13:52:36 +02001332 return target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001333}
1334
1335/**
1336 * srpt_handle_cmd() - Process SRP_CMD.
1337 */
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001338static void srpt_handle_cmd(struct srpt_rdma_ch *ch,
1339 struct srpt_recv_ioctx *recv_ioctx,
1340 struct srpt_send_ioctx *send_ioctx)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001341{
1342 struct se_cmd *cmd;
1343 struct srp_cmd *srp_cmd;
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001344 struct scatterlist *sg = NULL;
1345 unsigned sg_cnt = 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001346 u64 data_len;
1347 enum dma_data_direction dir;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001348 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001349
1350 BUG_ON(!send_ioctx);
1351
1352 srp_cmd = recv_ioctx->ioctx.buf;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001353 cmd = &send_ioctx->cmd;
Bart Van Assche649ee052015-04-14 13:26:44 +02001354 cmd->tag = srp_cmd->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001355
1356 switch (srp_cmd->task_attr) {
1357 case SRP_CMD_SIMPLE_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001358 cmd->sam_task_attr = TCM_SIMPLE_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001359 break;
1360 case SRP_CMD_ORDERED_Q:
1361 default:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001362 cmd->sam_task_attr = TCM_ORDERED_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001363 break;
1364 case SRP_CMD_HEAD_OF_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001365 cmd->sam_task_attr = TCM_HEAD_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001366 break;
1367 case SRP_CMD_ACA:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001368 cmd->sam_task_attr = TCM_ACA_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001369 break;
1370 }
1371
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001372 rc = srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &sg, &sg_cnt,
1373 &data_len);
1374 if (rc) {
1375 if (rc != -EAGAIN) {
1376 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
1377 srp_cmd->tag);
1378 }
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001379 goto release_ioctx;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001380 }
1381
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001382 rc = target_submit_cmd_map_sgls(cmd, ch->sess, srp_cmd->cdb,
Bart Van Asschee1dd4132016-02-11 11:05:19 -08001383 &send_ioctx->sense_data[0],
1384 scsilun_to_int(&srp_cmd->lun), data_len,
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001385 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF,
1386 sg, sg_cnt, NULL, 0, NULL, 0);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001387 if (rc != 0) {
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001388 pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc,
1389 srp_cmd->tag);
1390 goto release_ioctx;
Nicholas Bellinger187e70a2012-03-17 20:12:36 -07001391 }
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001392 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001393
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001394release_ioctx:
1395 send_ioctx->state = SRPT_STATE_DONE;
1396 srpt_release_cmd(cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001397}
1398
Bart Van Asschea42d9852011-10-14 01:30:46 +00001399static int srp_tmr_to_tcm(int fn)
1400{
1401 switch (fn) {
1402 case SRP_TSK_ABORT_TASK:
1403 return TMR_ABORT_TASK;
1404 case SRP_TSK_ABORT_TASK_SET:
1405 return TMR_ABORT_TASK_SET;
1406 case SRP_TSK_CLEAR_TASK_SET:
1407 return TMR_CLEAR_TASK_SET;
1408 case SRP_TSK_LUN_RESET:
1409 return TMR_LUN_RESET;
1410 case SRP_TSK_CLEAR_ACA:
1411 return TMR_CLEAR_ACA;
1412 default:
1413 return -1;
1414 }
1415}
1416
1417/**
1418 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1419 *
1420 * Returns 0 if and only if the request will be processed by the target core.
1421 *
1422 * For more information about SRP_TSK_MGMT information units, see also section
1423 * 6.7 in the SRP r16a document.
1424 */
1425static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1426 struct srpt_recv_ioctx *recv_ioctx,
1427 struct srpt_send_ioctx *send_ioctx)
1428{
1429 struct srp_tsk_mgmt *srp_tsk;
1430 struct se_cmd *cmd;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001431 struct se_session *sess = ch->sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001432 int tcm_tmr;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001433 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001434
1435 BUG_ON(!send_ioctx);
1436
1437 srp_tsk = recv_ioctx->ioctx.buf;
1438 cmd = &send_ioctx->cmd;
1439
1440 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1441 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1442 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1443
1444 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
Bart Van Assche649ee052015-04-14 13:26:44 +02001445 send_ioctx->cmd.tag = srp_tsk->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001446 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
Bart Van Asschee1dd4132016-02-11 11:05:19 -08001447 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL,
1448 scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr,
1449 GFP_KERNEL, srp_tsk->task_tag,
1450 TARGET_SCF_ACK_KREF);
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001451 if (rc != 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001452 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001453 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001454 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001455 return;
1456fail:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001457 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001458}
1459
1460/**
1461 * srpt_handle_new_iu() - Process a newly received information unit.
1462 * @ch: RDMA channel through which the information unit has been received.
1463 * @ioctx: SRPT I/O context associated with the information unit.
1464 */
1465static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1466 struct srpt_recv_ioctx *recv_ioctx,
1467 struct srpt_send_ioctx *send_ioctx)
1468{
1469 struct srp_cmd *srp_cmd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001470
1471 BUG_ON(!ch);
1472 BUG_ON(!recv_ioctx);
1473
1474 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1475 recv_ioctx->ioctx.dma, srp_max_req_size,
1476 DMA_FROM_DEVICE);
1477
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001478 if (unlikely(ch->state == CH_CONNECTING))
1479 goto out_wait;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001480
Bart Van Assche33912d72016-02-11 11:04:43 -08001481 if (unlikely(ch->state != CH_LIVE))
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001482 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001483
1484 srp_cmd = recv_ioctx->ioctx.buf;
1485 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001486 if (!send_ioctx) {
1487 if (!list_empty(&ch->cmd_wait_list))
1488 goto out_wait;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001489 send_ioctx = srpt_get_send_ioctx(ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001490 }
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001491 if (unlikely(!send_ioctx))
1492 goto out_wait;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001493 }
1494
Bart Van Asschea42d9852011-10-14 01:30:46 +00001495 switch (srp_cmd->opcode) {
1496 case SRP_CMD:
1497 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1498 break;
1499 case SRP_TSK_MGMT:
1500 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1501 break;
1502 case SRP_I_LOGOUT:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001503 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001504 break;
1505 case SRP_CRED_RSP:
1506 pr_debug("received SRP_CRED_RSP\n");
1507 break;
1508 case SRP_AER_RSP:
1509 pr_debug("received SRP_AER_RSP\n");
1510 break;
1511 case SRP_RSP:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001512 pr_err("Received SRP_RSP\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001513 break;
1514 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001515 pr_err("received IU with unknown opcode 0x%x\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001516 srp_cmd->opcode);
1517 break;
1518 }
1519
1520 srpt_post_recv(ch->sport->sdev, recv_ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001521 return;
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001522
1523out_wait:
1524 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001525}
1526
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001527static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001528{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001529 struct srpt_rdma_ch *ch = cq->cq_context;
1530 struct srpt_recv_ioctx *ioctx =
1531 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001532
Bart Van Asschea42d9852011-10-14 01:30:46 +00001533 if (wc->status == IB_WC_SUCCESS) {
1534 int req_lim;
1535
1536 req_lim = atomic_dec_return(&ch->req_lim);
1537 if (unlikely(req_lim < 0))
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001538 pr_err("req_lim = %d < 0\n", req_lim);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001539 srpt_handle_new_iu(ch, ioctx, NULL);
1540 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001541 pr_info("receiving failed for ioctx %p with status %d\n",
1542 ioctx, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001543 }
1544}
1545
Bart Van Assche539b3242016-02-11 11:09:50 -08001546/*
1547 * This function must be called from the context in which RDMA completions are
1548 * processed because it accesses the wait list without protection against
1549 * access from other threads.
1550 */
1551static void srpt_process_wait_list(struct srpt_rdma_ch *ch)
1552{
1553 struct srpt_send_ioctx *ioctx;
1554
1555 while (!list_empty(&ch->cmd_wait_list) &&
1556 ch->state >= CH_LIVE &&
1557 (ioctx = srpt_get_send_ioctx(ch)) != NULL) {
1558 struct srpt_recv_ioctx *recv_ioctx;
1559
1560 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
1561 struct srpt_recv_ioctx,
1562 wait_list);
1563 list_del(&recv_ioctx->wait_list);
1564 srpt_handle_new_iu(ch, recv_ioctx, ioctx);
1565 }
1566}
1567
Bart Van Asschea42d9852011-10-14 01:30:46 +00001568/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001569 * Note: Although this has not yet been observed during tests, at least in
1570 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1571 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1572 * value in each response is set to one, and it is possible that this response
1573 * makes the initiator send a new request before the send completion for that
1574 * response has been processed. This could e.g. happen if the call to
1575 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1576 * if IB retransmission causes generation of the send completion to be
1577 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1578 * are queued on cmd_wait_list. The code below processes these delayed
1579 * requests one at a time.
1580 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001581static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001582{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001583 struct srpt_rdma_ch *ch = cq->cq_context;
1584 struct srpt_send_ioctx *ioctx =
1585 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1586 enum srpt_command_state state;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001587
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001588 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1589
1590 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1591 state != SRPT_STATE_MGMT_RSP_SENT);
1592
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001593 atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001594
Bart Van Assche49f40162016-02-11 11:07:11 -08001595 if (wc->status != IB_WC_SUCCESS)
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001596 pr_info("sending response for ioctx 0x%p failed"
1597 " with status %d\n", ioctx, wc->status);
1598
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001599 if (state != SRPT_STATE_DONE) {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001600 transport_generic_free_cmd(&ioctx->cmd, 0);
1601 } else {
1602 pr_err("IB completion has been received too late for"
1603 " wr_id = %u.\n", ioctx->ioctx.index);
1604 }
1605
Bart Van Assche539b3242016-02-11 11:09:50 -08001606 srpt_process_wait_list(ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001607}
1608
Bart Van Asschea42d9852011-10-14 01:30:46 +00001609/**
1610 * srpt_create_ch_ib() - Create receive and send completion queues.
1611 */
1612static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1613{
1614 struct ib_qp_init_attr *qp_init;
1615 struct srpt_port *sport = ch->sport;
1616 struct srpt_device *sdev = sport->sdev;
Bart Van Assche30c6d872016-07-21 13:03:47 -07001617 const struct ib_device_attr *attrs = &sdev->device->attrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001618 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
1619 int ret;
1620
1621 WARN_ON(ch->rq_size < 1);
1622
1623 ret = -ENOMEM;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08001624 qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001625 if (!qp_init)
1626 goto out;
1627
Bart Van Asscheab477c12014-10-19 18:05:33 +03001628retry:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001629 ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size,
1630 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001631 if (IS_ERR(ch->cq)) {
1632 ret = PTR_ERR(ch->cq);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001633 pr_err("failed to create CQ cqe= %d ret= %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001634 ch->rq_size + srp_sq_size, ret);
1635 goto out;
1636 }
1637
1638 qp_init->qp_context = (void *)ch;
1639 qp_init->event_handler
1640 = (void(*)(struct ib_event *, void*))srpt_qp_event;
1641 qp_init->send_cq = ch->cq;
1642 qp_init->recv_cq = ch->cq;
1643 qp_init->srq = sdev->srq;
1644 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1645 qp_init->qp_type = IB_QPT_RC;
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001646 /*
1647 * We divide up our send queue size into half SEND WRs to send the
1648 * completions, and half R/W contexts to actually do the RDMA
1649 * READ/WRITE transfers. Note that we need to allocate CQ slots for
1650 * both both, as RDMA contexts will also post completions for the
1651 * RDMA READ case.
1652 */
1653 qp_init->cap.max_send_wr = srp_sq_size / 2;
1654 qp_init->cap.max_rdma_ctxs = srp_sq_size / 2;
Bart Van Assche30c6d872016-07-21 13:03:47 -07001655 qp_init->cap.max_send_sge = min(attrs->max_sge, SRPT_MAX_SG_PER_WQE);
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001656 qp_init->port_num = ch->sport->port;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001657
1658 ch->qp = ib_create_qp(sdev->pd, qp_init);
1659 if (IS_ERR(ch->qp)) {
1660 ret = PTR_ERR(ch->qp);
Bart Van Asscheab477c12014-10-19 18:05:33 +03001661 if (ret == -ENOMEM) {
1662 srp_sq_size /= 2;
1663 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
1664 ib_destroy_cq(ch->cq);
1665 goto retry;
1666 }
1667 }
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001668 pr_err("failed to create_qp ret= %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001669 goto err_destroy_cq;
1670 }
1671
1672 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1673
1674 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
1675 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1676 qp_init->cap.max_send_wr, ch->cm_id);
1677
1678 ret = srpt_init_ch_qp(ch, ch->qp);
1679 if (ret)
1680 goto err_destroy_qp;
1681
Bart Van Asschea42d9852011-10-14 01:30:46 +00001682out:
1683 kfree(qp_init);
1684 return ret;
1685
1686err_destroy_qp:
1687 ib_destroy_qp(ch->qp);
1688err_destroy_cq:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001689 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001690 goto out;
1691}
1692
1693static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
1694{
Bart Van Asschea42d9852011-10-14 01:30:46 +00001695 ib_destroy_qp(ch->qp);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001696 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001697}
1698
1699/**
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001700 * srpt_close_ch() - Close an RDMA channel.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001701 *
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001702 * Make sure all resources associated with the channel will be deallocated at
1703 * an appropriate time.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001704 *
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001705 * Returns true if and only if the channel state has been modified into
1706 * CH_DRAINING.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001707 */
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001708static bool srpt_close_ch(struct srpt_rdma_ch *ch)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001709{
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001710 int ret;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001711
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001712 if (!srpt_set_ch_state(ch, CH_DRAINING)) {
1713 pr_debug("%s-%d: already closed\n", ch->sess_name,
1714 ch->qp->qp_num);
1715 return false;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001716 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001717
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001718 kref_get(&ch->kref);
1719
1720 ret = srpt_ch_qp_err(ch);
1721 if (ret < 0)
1722 pr_err("%s-%d: changing queue pair into error state failed: %d\n",
1723 ch->sess_name, ch->qp->qp_num, ret);
1724
1725 pr_debug("%s-%d: queued zerolength write\n", ch->sess_name,
1726 ch->qp->qp_num);
1727 ret = srpt_zerolength_write(ch);
1728 if (ret < 0) {
1729 pr_err("%s-%d: queuing zero-length write failed: %d\n",
1730 ch->sess_name, ch->qp->qp_num, ret);
1731 if (srpt_set_ch_state(ch, CH_DISCONNECTED))
1732 schedule_work(&ch->release_work);
1733 else
1734 WARN_ON_ONCE(true);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001735 }
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001736
1737 kref_put(&ch->kref, srpt_free_ch);
1738
1739 return true;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001740}
1741
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001742/*
1743 * Change the channel state into CH_DISCONNECTING. If a channel has not yet
1744 * reached the connected state, close it. If a channel is in the connected
1745 * state, send a DREQ. If a DREQ has been received, send a DREP. Note: it is
1746 * the responsibility of the caller to ensure that this function is not
1747 * invoked concurrently with the code that accepts a connection. This means
1748 * that this function must either be invoked from inside a CM callback
1749 * function or that it must be invoked with the srpt_port.mutex held.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001750 */
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001751static int srpt_disconnect_ch(struct srpt_rdma_ch *ch)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001752{
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001753 int ret;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001754
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001755 if (!srpt_set_ch_state(ch, CH_DISCONNECTING))
1756 return -ENOTCONN;
1757
1758 ret = ib_send_cm_dreq(ch->cm_id, NULL, 0);
1759 if (ret < 0)
1760 ret = ib_send_cm_drep(ch->cm_id, NULL, 0);
1761
1762 if (ret < 0 && srpt_close_ch(ch))
1763 ret = 0;
1764
1765 return ret;
1766}
1767
1768static void __srpt_close_all_ch(struct srpt_device *sdev)
1769{
1770 struct srpt_rdma_ch *ch;
1771
1772 lockdep_assert_held(&sdev->mutex);
1773
1774 list_for_each_entry(ch, &sdev->rch_list, list) {
1775 if (srpt_disconnect_ch(ch) >= 0)
1776 pr_info("Closing channel %s-%d because target %s has been disabled\n",
1777 ch->sess_name, ch->qp->qp_num,
1778 sdev->device->name);
1779 srpt_close_ch(ch);
1780 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001781}
1782
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001783static void srpt_free_ch(struct kref *kref)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001784{
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001785 struct srpt_rdma_ch *ch = container_of(kref, struct srpt_rdma_ch, kref);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001786
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001787 kfree(ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001788}
1789
1790static void srpt_release_channel_work(struct work_struct *w)
1791{
1792 struct srpt_rdma_ch *ch;
1793 struct srpt_device *sdev;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001794 struct se_session *se_sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001795
1796 ch = container_of(w, struct srpt_rdma_ch, release_work);
Bart Van Asschef108f0f2016-02-11 11:06:14 -08001797 pr_debug("%s: %s-%d; release_done = %p\n", __func__, ch->sess_name,
1798 ch->qp->qp_num, ch->release_done);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001799
1800 sdev = ch->sport->sdev;
1801 BUG_ON(!sdev);
1802
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001803 se_sess = ch->sess;
1804 BUG_ON(!se_sess);
1805
Bart Van Assche88936252016-02-11 11:05:58 -08001806 target_sess_cmd_list_set_waiting(se_sess);
Joern Engelbe646c2d2013-05-15 00:44:07 -07001807 target_wait_for_sess_cmds(se_sess);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001808
1809 transport_deregister_session_configfs(se_sess);
1810 transport_deregister_session(se_sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001811 ch->sess = NULL;
1812
Nicholas Bellinger0b41d6c2013-09-18 12:48:27 -07001813 ib_destroy_cm_id(ch->cm_id);
1814
Bart Van Asschea42d9852011-10-14 01:30:46 +00001815 srpt_destroy_ch_ib(ch);
1816
1817 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
1818 ch->sport->sdev, ch->rq_size,
1819 ch->rsp_size, DMA_TO_DEVICE);
1820
Bart Van Assche86289912016-02-11 11:08:34 -08001821 mutex_lock(&sdev->mutex);
Bart Van Asschef108f0f2016-02-11 11:06:14 -08001822 list_del_init(&ch->list);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001823 if (ch->release_done)
1824 complete(ch->release_done);
Bart Van Assche86289912016-02-11 11:08:34 -08001825 mutex_unlock(&sdev->mutex);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001826
1827 wake_up(&sdev->ch_releaseQ);
1828
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001829 kref_put(&ch->kref, srpt_free_ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001830}
1831
Bart Van Asschea42d9852011-10-14 01:30:46 +00001832/**
1833 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
1834 *
1835 * Ownership of the cm_id is transferred to the target session if this
1836 * functions returns zero. Otherwise the caller remains the owner of cm_id.
1837 */
1838static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
1839 struct ib_cm_req_event_param *param,
1840 void *private_data)
1841{
1842 struct srpt_device *sdev = cm_id->context;
1843 struct srpt_port *sport = &sdev->port[param->port - 1];
1844 struct srp_login_req *req;
1845 struct srp_login_rsp *rsp;
1846 struct srp_login_rej *rej;
1847 struct ib_cm_rep_param *rep_param;
1848 struct srpt_rdma_ch *ch, *tmp_ch;
Bart Van Assche2bce1a62016-12-09 11:00:33 -08001849 __be16 *guid;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001850 u32 it_iu_len;
Bart Van Assche3c968882016-04-07 15:55:04 -07001851 int i, ret = 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001852
1853 WARN_ON_ONCE(irqs_disabled());
1854
1855 if (WARN_ON(!sdev || !private_data))
1856 return -EINVAL;
1857
1858 req = (struct srp_login_req *)private_data;
1859
1860 it_iu_len = be32_to_cpu(req->req_it_iu_len);
1861
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001862 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
1863 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
1864 " (guid=0x%llx:0x%llx)\n",
1865 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
1866 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
1867 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
1868 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
1869 it_iu_len,
1870 param->port,
1871 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
1872 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001873
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08001874 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
1875 rej = kzalloc(sizeof(*rej), GFP_KERNEL);
1876 rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001877
1878 if (!rsp || !rej || !rep_param) {
1879 ret = -ENOMEM;
1880 goto out;
1881 }
1882
1883 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301884 rej->reason = cpu_to_be32(
1885 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001886 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001887 pr_err("rejected SRP_LOGIN_REQ because its"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001888 " length (%d bytes) is out of range (%d .. %d)\n",
1889 it_iu_len, 64, srp_max_req_size);
1890 goto reject;
1891 }
1892
1893 if (!sport->enabled) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301894 rej->reason = cpu_to_be32(
1895 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001896 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001897 pr_err("rejected SRP_LOGIN_REQ because the target port"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001898 " has not yet been enabled\n");
1899 goto reject;
1900 }
1901
1902 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
1903 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
1904
Bart Van Assche86289912016-02-11 11:08:34 -08001905 mutex_lock(&sdev->mutex);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001906
1907 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
1908 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
1909 && !memcmp(ch->t_port_id, req->target_port_id, 16)
1910 && param->port == ch->sport->port
1911 && param->listen_id == ch->sport->sdev->cm_id
1912 && ch->cm_id) {
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001913 if (srpt_disconnect_ch(ch) < 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001914 continue;
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001915 pr_info("Relogin - closed existing channel %s\n",
1916 ch->sess_name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001917 rsp->rsp_flags =
1918 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
1919 }
1920 }
1921
Bart Van Assche86289912016-02-11 11:08:34 -08001922 mutex_unlock(&sdev->mutex);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001923
1924 } else
1925 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
1926
1927 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
1928 || *(__be64 *)(req->target_port_id + 8) !=
1929 cpu_to_be64(srpt_service_guid)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301930 rej->reason = cpu_to_be32(
1931 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001932 ret = -ENOMEM;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001933 pr_err("rejected SRP_LOGIN_REQ because it"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001934 " has an invalid target port identifier.\n");
1935 goto reject;
1936 }
1937
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08001938 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001939 if (!ch) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301940 rej->reason = cpu_to_be32(
1941 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001942 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001943 ret = -ENOMEM;
1944 goto reject;
1945 }
1946
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001947 kref_init(&ch->kref);
1948 ch->zw_cqe.done = srpt_zerolength_write_done;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001949 INIT_WORK(&ch->release_work, srpt_release_channel_work);
1950 memcpy(ch->i_port_id, req->initiator_port_id, 16);
1951 memcpy(ch->t_port_id, req->target_port_id, 16);
1952 ch->sport = &sdev->port[param->port - 1];
1953 ch->cm_id = cm_id;
Bart Van Assche2739b592016-02-11 11:07:49 -08001954 cm_id->context = ch;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001955 /*
1956 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
1957 * for the SRP protocol to the command queue size.
1958 */
1959 ch->rq_size = SRPT_RQ_SIZE;
1960 spin_lock_init(&ch->spinlock);
1961 ch->state = CH_CONNECTING;
1962 INIT_LIST_HEAD(&ch->cmd_wait_list);
1963 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
1964
1965 ch->ioctx_ring = (struct srpt_send_ioctx **)
1966 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
1967 sizeof(*ch->ioctx_ring[0]),
1968 ch->rsp_size, DMA_TO_DEVICE);
1969 if (!ch->ioctx_ring)
1970 goto free_ch;
1971
Bart Van Assche3c968882016-04-07 15:55:04 -07001972 INIT_LIST_HEAD(&ch->free_list);
1973 for (i = 0; i < ch->rq_size; i++) {
1974 ch->ioctx_ring[i]->ch = ch;
1975 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
1976 }
1977
Bart Van Asschea42d9852011-10-14 01:30:46 +00001978 ret = srpt_create_ch_ib(ch);
1979 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301980 rej->reason = cpu_to_be32(
1981 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001982 pr_err("rejected SRP_LOGIN_REQ because creating"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001983 " a new RDMA channel failed.\n");
1984 goto free_ring;
1985 }
1986
1987 ret = srpt_ch_qp_rtr(ch, ch->qp);
1988 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301989 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001990 pr_err("rejected SRP_LOGIN_REQ because enabling"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001991 " RTR failed (error code = %d)\n", ret);
1992 goto destroy_ib;
1993 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08001994
Bart Van Assche2bce1a62016-12-09 11:00:33 -08001995 guid = (__be16 *)&param->primary_path->sgid.global.interface_id;
1996 snprintf(ch->ini_guid, sizeof(ch->ini_guid), "%04x:%04x:%04x:%04x",
1997 be16_to_cpu(guid[0]), be16_to_cpu(guid[1]),
1998 be16_to_cpu(guid[2]), be16_to_cpu(guid[3]));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001999 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2000 be64_to_cpu(*(__be64 *)ch->i_port_id),
2001 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2002
2003 pr_debug("registering session %s\n", ch->sess_name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002004
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002005 if (sport->port_guid_tpg.se_tpg_wwn)
2006 ch->sess = target_alloc_session(&sport->port_guid_tpg, 0, 0,
2007 TARGET_PROT_NORMAL,
2008 ch->ini_guid, ch, NULL);
2009 if (sport->port_gid_tpg.se_tpg_wwn && IS_ERR_OR_NULL(ch->sess))
2010 ch->sess = target_alloc_session(&sport->port_gid_tpg, 0, 0,
Bart Van Assche0d38c242016-11-11 16:36:06 -08002011 TARGET_PROT_NORMAL, ch->sess_name, ch,
2012 NULL);
2013 /* Retry without leading "0x" */
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002014 if (sport->port_gid_tpg.se_tpg_wwn && IS_ERR_OR_NULL(ch->sess))
2015 ch->sess = target_alloc_session(&sport->port_gid_tpg, 0, 0,
Bart Van Assche0d38c242016-11-11 16:36:06 -08002016 TARGET_PROT_NORMAL,
2017 ch->sess_name + 2, ch, NULL);
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002018 if (IS_ERR_OR_NULL(ch->sess)) {
Bart Van Assche0d38c242016-11-11 16:36:06 -08002019 pr_info("Rejected login because no ACL has been configured yet for initiator %s.\n",
2020 ch->sess_name);
Nicholas Bellingerb42057a2016-01-09 20:42:43 -08002021 rej->reason = cpu_to_be32((PTR_ERR(ch->sess) == -ENOMEM) ?
2022 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES :
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002023 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002024 goto destroy_ib;
2025 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00002026
2027 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2028 ch->sess_name, ch->cm_id);
2029
2030 /* create srp_login_response */
2031 rsp->opcode = SRP_LOGIN_RSP;
2032 rsp->tag = req->tag;
2033 rsp->max_it_iu_len = req->req_it_iu_len;
2034 rsp->max_ti_iu_len = req->req_it_iu_len;
2035 ch->max_ti_iu_len = it_iu_len;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302036 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2037 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002038 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2039 atomic_set(&ch->req_lim, ch->rq_size);
2040 atomic_set(&ch->req_lim_delta, 0);
2041
2042 /* create cm reply */
2043 rep_param->qp_num = ch->qp->qp_num;
2044 rep_param->private_data = (void *)rsp;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002045 rep_param->private_data_len = sizeof(*rsp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002046 rep_param->rnr_retry_count = 7;
2047 rep_param->flow_control = 1;
2048 rep_param->failover_accepted = 0;
2049 rep_param->srq = 1;
2050 rep_param->responder_resources = 4;
2051 rep_param->initiator_depth = 4;
2052
2053 ret = ib_send_cm_rep(cm_id, rep_param);
2054 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002055 pr_err("sending SRP_LOGIN_REQ response failed"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002056 " (error code = %d)\n", ret);
2057 goto release_channel;
2058 }
2059
Bart Van Assche86289912016-02-11 11:08:34 -08002060 mutex_lock(&sdev->mutex);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002061 list_add_tail(&ch->list, &sdev->rch_list);
Bart Van Assche86289912016-02-11 11:08:34 -08002062 mutex_unlock(&sdev->mutex);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002063
2064 goto out;
2065
2066release_channel:
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08002067 srpt_disconnect_ch(ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002068 transport_deregister_session_configfs(ch->sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002069 transport_deregister_session(ch->sess);
2070 ch->sess = NULL;
2071
2072destroy_ib:
2073 srpt_destroy_ch_ib(ch);
2074
2075free_ring:
2076 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2077 ch->sport->sdev, ch->rq_size,
2078 ch->rsp_size, DMA_TO_DEVICE);
2079free_ch:
2080 kfree(ch);
2081
2082reject:
2083 rej->opcode = SRP_LOGIN_REJ;
2084 rej->tag = req->tag;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302085 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2086 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002087
2088 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002089 (void *)rej, sizeof(*rej));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002090
2091out:
2092 kfree(rep_param);
2093 kfree(rsp);
2094 kfree(rej);
2095
2096 return ret;
2097}
2098
Bart Van Assche2739b592016-02-11 11:07:49 -08002099static void srpt_cm_rej_recv(struct srpt_rdma_ch *ch,
2100 enum ib_cm_rej_reason reason,
2101 const u8 *private_data,
2102 u8 private_data_len)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002103{
Bart Van Asschec13c90e2016-02-11 11:08:12 -08002104 char *priv = NULL;
2105 int i;
2106
2107 if (private_data_len && (priv = kmalloc(private_data_len * 3 + 1,
2108 GFP_KERNEL))) {
2109 for (i = 0; i < private_data_len; i++)
2110 sprintf(priv + 3 * i, " %02x", private_data[i]);
2111 }
2112 pr_info("Received CM REJ for ch %s-%d; reason %d%s%s.\n",
2113 ch->sess_name, ch->qp->qp_num, reason, private_data_len ?
2114 "; private data" : "", priv ? priv : " (?)");
2115 kfree(priv);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002116}
2117
2118/**
2119 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2120 *
2121 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2122 * and that the recipient may begin transmitting (RTU = ready to use).
2123 */
Bart Van Assche2739b592016-02-11 11:07:49 -08002124static void srpt_cm_rtu_recv(struct srpt_rdma_ch *ch)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002125{
Bart Van Asschea42d9852011-10-14 01:30:46 +00002126 int ret;
2127
Bart Van Asschef130c222016-02-11 11:05:38 -08002128 if (srpt_set_ch_state(ch, CH_LIVE)) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00002129 ret = srpt_ch_qp_rts(ch, ch->qp);
2130
Bart Van Assche387add42016-02-11 11:10:09 -08002131 if (ret == 0) {
2132 /* Trigger wait list processing. */
2133 ret = srpt_zerolength_write(ch);
2134 WARN_ONCE(ret < 0, "%d\n", ret);
2135 } else {
Bart Van Asschea42d9852011-10-14 01:30:46 +00002136 srpt_close_ch(ch);
Bart Van Assche387add42016-02-11 11:10:09 -08002137 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00002138 }
2139}
2140
Bart Van Asschea42d9852011-10-14 01:30:46 +00002141/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00002142 * srpt_cm_handler() - IB connection manager callback function.
2143 *
2144 * A non-zero return value will cause the caller destroy the CM ID.
2145 *
2146 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2147 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2148 * a non-zero value in any other case will trigger a race with the
2149 * ib_destroy_cm_id() call in srpt_release_channel().
2150 */
2151static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2152{
Bart Van Assche2739b592016-02-11 11:07:49 -08002153 struct srpt_rdma_ch *ch = cm_id->context;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002154 int ret;
2155
2156 ret = 0;
2157 switch (event->event) {
2158 case IB_CM_REQ_RECEIVED:
2159 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2160 event->private_data);
2161 break;
2162 case IB_CM_REJ_RECEIVED:
Bart Van Assche2739b592016-02-11 11:07:49 -08002163 srpt_cm_rej_recv(ch, event->param.rej_rcvd.reason,
2164 event->private_data,
2165 IB_CM_REJ_PRIVATE_DATA_SIZE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002166 break;
2167 case IB_CM_RTU_RECEIVED:
2168 case IB_CM_USER_ESTABLISHED:
Bart Van Assche2739b592016-02-11 11:07:49 -08002169 srpt_cm_rtu_recv(ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002170 break;
2171 case IB_CM_DREQ_RECEIVED:
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08002172 srpt_disconnect_ch(ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002173 break;
2174 case IB_CM_DREP_RECEIVED:
Bart Van Assche2739b592016-02-11 11:07:49 -08002175 pr_info("Received CM DREP message for ch %s-%d.\n",
2176 ch->sess_name, ch->qp->qp_num);
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08002177 srpt_close_ch(ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002178 break;
2179 case IB_CM_TIMEWAIT_EXIT:
Bart Van Assche2739b592016-02-11 11:07:49 -08002180 pr_info("Received CM TimeWait exit for ch %s-%d.\n",
2181 ch->sess_name, ch->qp->qp_num);
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08002182 srpt_close_ch(ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002183 break;
2184 case IB_CM_REP_ERROR:
Bart Van Assche2739b592016-02-11 11:07:49 -08002185 pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
2186 ch->qp->qp_num);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002187 break;
2188 case IB_CM_DREQ_ERROR:
Bart Van Assche1e20a2a2016-02-11 11:07:29 -08002189 pr_info("Received CM DREQ ERROR event.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002190 break;
2191 case IB_CM_MRA_RECEIVED:
Bart Van Assche1e20a2a2016-02-11 11:07:29 -08002192 pr_info("Received CM MRA event\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002193 break;
2194 default:
Bart Van Assche1e20a2a2016-02-11 11:07:29 -08002195 pr_err("received unrecognized CM event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002196 break;
2197 }
2198
2199 return ret;
2200}
2201
Bart Van Asschea42d9852011-10-14 01:30:46 +00002202static int srpt_write_pending_status(struct se_cmd *se_cmd)
2203{
2204 struct srpt_send_ioctx *ioctx;
2205
2206 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2207 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2208}
2209
2210/*
2211 * srpt_write_pending() - Start data transfer from initiator to target (write).
2212 */
2213static int srpt_write_pending(struct se_cmd *se_cmd)
2214{
Bart Van Asschefc3af582016-02-11 11:09:10 -08002215 struct srpt_send_ioctx *ioctx =
2216 container_of(se_cmd, struct srpt_send_ioctx, cmd);
2217 struct srpt_rdma_ch *ch = ioctx->ch;
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002218 struct ib_send_wr *first_wr = NULL, *bad_wr;
2219 struct ib_cqe *cqe = &ioctx->rdma_cqe;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002220 enum srpt_command_state new_state;
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002221 int ret, i;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002222
2223 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2224 WARN_ON(new_state == SRPT_STATE_DONE);
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002225
2226 if (atomic_sub_return(ioctx->n_rdma, &ch->sq_wr_avail) < 0) {
2227 pr_warn("%s: IB send queue full (needed %d)\n",
2228 __func__, ioctx->n_rdma);
2229 ret = -ENOMEM;
2230 goto out_undo;
2231 }
2232
2233 cqe->done = srpt_rdma_read_done;
2234 for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
2235 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
2236
2237 first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, ch->sport->port,
2238 cqe, first_wr);
2239 cqe = NULL;
2240 }
2241
2242 ret = ib_post_send(ch->qp, first_wr, &bad_wr);
2243 if (ret) {
2244 pr_err("%s: ib_post_send() returned %d for %d (avail: %d)\n",
2245 __func__, ret, ioctx->n_rdma,
2246 atomic_read(&ch->sq_wr_avail));
2247 goto out_undo;
2248 }
2249
2250 return 0;
2251out_undo:
2252 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
2253 return ret;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002254}
2255
2256static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2257{
2258 switch (tcm_mgmt_status) {
2259 case TMR_FUNCTION_COMPLETE:
2260 return SRP_TSK_MGMT_SUCCESS;
2261 case TMR_FUNCTION_REJECTED:
2262 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2263 }
2264 return SRP_TSK_MGMT_FAILED;
2265}
2266
2267/**
2268 * srpt_queue_response() - Transmits the response to a SCSI command.
2269 *
2270 * Callback function called by the TCM core. Must not block since it can be
2271 * invoked on the context of the IB completion handler.
2272 */
Joern Engelb79fafa2013-07-03 11:22:17 -04002273static void srpt_queue_response(struct se_cmd *cmd)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002274{
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002275 struct srpt_send_ioctx *ioctx =
2276 container_of(cmd, struct srpt_send_ioctx, cmd);
2277 struct srpt_rdma_ch *ch = ioctx->ch;
2278 struct srpt_device *sdev = ch->sport->sdev;
Bart Van Assche10fce582016-07-21 13:04:06 -07002279 struct ib_send_wr send_wr, *first_wr = &send_wr, *bad_wr;
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002280 struct ib_sge sge;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002281 enum srpt_command_state state;
2282 unsigned long flags;
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002283 int resp_len, ret, i;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002284 u8 srp_tm_status;
2285
Bart Van Asschea42d9852011-10-14 01:30:46 +00002286 BUG_ON(!ch);
2287
2288 spin_lock_irqsave(&ioctx->spinlock, flags);
2289 state = ioctx->state;
2290 switch (state) {
2291 case SRPT_STATE_NEW:
2292 case SRPT_STATE_DATA_IN:
2293 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2294 break;
2295 case SRPT_STATE_MGMT:
2296 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2297 break;
2298 default:
2299 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2300 ch, ioctx->ioctx.index, ioctx->state);
2301 break;
2302 }
2303 spin_unlock_irqrestore(&ioctx->spinlock, flags);
2304
Bart Van Assche55d69422017-05-04 15:50:53 -07002305 if (unlikely(WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT)))
Joern Engelb79fafa2013-07-03 11:22:17 -04002306 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002307
Bart Van Asschea42d9852011-10-14 01:30:46 +00002308 /* For read commands, transfer the data to the initiator. */
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002309 if (ioctx->cmd.data_direction == DMA_FROM_DEVICE &&
2310 ioctx->cmd.data_length &&
Bart Van Asschea42d9852011-10-14 01:30:46 +00002311 !ioctx->queue_status_only) {
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002312 for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
2313 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
2314
2315 first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp,
Bart Van Assche10fce582016-07-21 13:04:06 -07002316 ch->sport->port, NULL, first_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002317 }
2318 }
2319
2320 if (state != SRPT_STATE_MGMT)
Bart Van Assche649ee052015-04-14 13:26:44 +02002321 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002322 cmd->scsi_status);
2323 else {
2324 srp_tm_status
2325 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2326 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
Bart Van Assche649ee052015-04-14 13:26:44 +02002327 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002328 }
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002329
2330 atomic_inc(&ch->req_lim);
2331
2332 if (unlikely(atomic_sub_return(1 + ioctx->n_rdma,
2333 &ch->sq_wr_avail) < 0)) {
2334 pr_warn("%s: IB send queue full (needed %d)\n",
2335 __func__, ioctx->n_rdma);
2336 ret = -ENOMEM;
2337 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002338 }
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002339
2340 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, resp_len,
2341 DMA_TO_DEVICE);
2342
2343 sge.addr = ioctx->ioctx.dma;
2344 sge.length = resp_len;
2345 sge.lkey = sdev->pd->local_dma_lkey;
2346
2347 ioctx->ioctx.cqe.done = srpt_send_done;
2348 send_wr.next = NULL;
2349 send_wr.wr_cqe = &ioctx->ioctx.cqe;
2350 send_wr.sg_list = &sge;
2351 send_wr.num_sge = 1;
2352 send_wr.opcode = IB_WR_SEND;
2353 send_wr.send_flags = IB_SEND_SIGNALED;
2354
2355 ret = ib_post_send(ch->qp, first_wr, &bad_wr);
2356 if (ret < 0) {
2357 pr_err("%s: sending cmd response failed for tag %llu (%d)\n",
2358 __func__, ioctx->cmd.tag, ret);
2359 goto out;
2360 }
2361
2362 return;
2363
2364out:
2365 atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
2366 atomic_dec(&ch->req_lim);
2367 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
2368 target_put_sess_cmd(&ioctx->cmd);
Joern Engelb79fafa2013-07-03 11:22:17 -04002369}
Bart Van Asschea42d9852011-10-14 01:30:46 +00002370
Joern Engelb79fafa2013-07-03 11:22:17 -04002371static int srpt_queue_data_in(struct se_cmd *cmd)
2372{
2373 srpt_queue_response(cmd);
2374 return 0;
2375}
2376
2377static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2378{
2379 srpt_queue_response(cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002380}
2381
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002382static void srpt_aborted_task(struct se_cmd *cmd)
2383{
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002384}
2385
Bart Van Asschea42d9852011-10-14 01:30:46 +00002386static int srpt_queue_status(struct se_cmd *cmd)
2387{
2388 struct srpt_send_ioctx *ioctx;
2389
2390 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2391 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2392 if (cmd->se_cmd_flags &
2393 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2394 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2395 ioctx->queue_status_only = true;
Joern Engelb79fafa2013-07-03 11:22:17 -04002396 srpt_queue_response(cmd);
2397 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002398}
2399
2400static void srpt_refresh_port_work(struct work_struct *work)
2401{
2402 struct srpt_port *sport = container_of(work, struct srpt_port, work);
2403
2404 srpt_refresh_port(sport);
2405}
2406
Bart Van Asschea42d9852011-10-14 01:30:46 +00002407/**
2408 * srpt_release_sdev() - Free the channel resources associated with a target.
2409 */
2410static int srpt_release_sdev(struct srpt_device *sdev)
2411{
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08002412 int i, res;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002413
2414 WARN_ON_ONCE(irqs_disabled());
2415
2416 BUG_ON(!sdev);
2417
Bart Van Assche86289912016-02-11 11:08:34 -08002418 mutex_lock(&sdev->mutex);
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08002419 for (i = 0; i < ARRAY_SIZE(sdev->port); i++)
2420 sdev->port[i].enabled = false;
2421 __srpt_close_all_ch(sdev);
Bart Van Assche86289912016-02-11 11:08:34 -08002422 mutex_unlock(&sdev->mutex);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002423
2424 res = wait_event_interruptible(sdev->ch_releaseQ,
Bart Van Assche86289912016-02-11 11:08:34 -08002425 list_empty_careful(&sdev->rch_list));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002426 if (res)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002427 pr_err("%s: interrupted.\n", __func__);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002428
2429 return 0;
2430}
2431
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002432static struct se_wwn *__srpt_lookup_wwn(const char *name)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002433{
2434 struct ib_device *dev;
2435 struct srpt_device *sdev;
2436 struct srpt_port *sport;
2437 int i;
2438
2439 list_for_each_entry(sdev, &srpt_dev_list, list) {
2440 dev = sdev->device;
2441 if (!dev)
2442 continue;
2443
2444 for (i = 0; i < dev->phys_port_cnt; i++) {
2445 sport = &sdev->port[i];
2446
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002447 if (strcmp(sport->port_guid, name) == 0)
2448 return &sport->port_guid_wwn;
2449 if (strcmp(sport->port_gid, name) == 0)
2450 return &sport->port_gid_wwn;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002451 }
2452 }
2453
2454 return NULL;
2455}
2456
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002457static struct se_wwn *srpt_lookup_wwn(const char *name)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002458{
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002459 struct se_wwn *wwn;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002460
2461 spin_lock(&srpt_dev_lock);
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002462 wwn = __srpt_lookup_wwn(name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002463 spin_unlock(&srpt_dev_lock);
2464
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002465 return wwn;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002466}
2467
2468/**
2469 * srpt_add_one() - Infiniband device addition callback function.
2470 */
2471static void srpt_add_one(struct ib_device *device)
2472{
2473 struct srpt_device *sdev;
2474 struct srpt_port *sport;
2475 struct ib_srq_init_attr srq_attr;
2476 int i;
2477
Bart Van Asschee3dfa602017-01-20 13:04:33 -08002478 pr_debug("device = %p\n", device);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002479
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002480 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002481 if (!sdev)
2482 goto err;
2483
2484 sdev->device = device;
2485 INIT_LIST_HEAD(&sdev->rch_list);
2486 init_waitqueue_head(&sdev->ch_releaseQ);
Bart Van Assche86289912016-02-11 11:08:34 -08002487 mutex_init(&sdev->mutex);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002488
Christoph Hellwiged082d32016-09-05 12:56:17 +02002489 sdev->pd = ib_alloc_pd(device, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002490 if (IS_ERR(sdev->pd))
2491 goto free_dev;
2492
Or Gerlitz4a061b22015-12-18 10:59:46 +02002493 sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002494
2495 srq_attr.event_handler = srpt_srq_event;
2496 srq_attr.srq_context = (void *)sdev;
2497 srq_attr.attr.max_wr = sdev->srq_size;
2498 srq_attr.attr.max_sge = 1;
2499 srq_attr.attr.srq_limit = 0;
Roland Dreier6f360332012-04-12 07:51:08 -07002500 srq_attr.srq_type = IB_SRQT_BASIC;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002501
2502 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
2503 if (IS_ERR(sdev->srq))
Jason Gunthorpe5a783952015-07-30 17:22:24 -06002504 goto err_pd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002505
2506 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
Or Gerlitz4a061b22015-12-18 10:59:46 +02002507 __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002508 device->name);
2509
2510 if (!srpt_service_guid)
2511 srpt_service_guid = be64_to_cpu(device->node_guid);
2512
2513 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
2514 if (IS_ERR(sdev->cm_id))
2515 goto err_srq;
2516
2517 /* print out target login information */
2518 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
2519 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
2520 srpt_service_guid, srpt_service_guid);
2521
2522 /*
2523 * We do not have a consistent service_id (ie. also id_ext of target_id)
2524 * to identify this target. We currently use the guid of the first HCA
2525 * in the system as service_id; therefore, the target_id will change
2526 * if this HCA is gone bad and replaced by different HCA
2527 */
Haggai Eran73fec7f2015-07-30 17:50:26 +03002528 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
Bart Van Asschea42d9852011-10-14 01:30:46 +00002529 goto err_cm;
2530
2531 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
2532 srpt_event_handler);
2533 if (ib_register_event_handler(&sdev->event_handler))
2534 goto err_cm;
2535
2536 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
2537 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
2538 sizeof(*sdev->ioctx_ring[0]),
2539 srp_max_req_size, DMA_FROM_DEVICE);
2540 if (!sdev->ioctx_ring)
2541 goto err_event;
2542
2543 for (i = 0; i < sdev->srq_size; ++i)
2544 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
2545
Roland Dreierf2250662012-02-02 12:55:58 -08002546 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002547
2548 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
2549 sport = &sdev->port[i - 1];
2550 sport->sdev = sdev;
2551 sport->port = i;
2552 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
2553 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
2554 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
2555 INIT_WORK(&sport->work, srpt_refresh_port_work);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002556
2557 if (srpt_refresh_port(sport)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002558 pr_err("MAD registration failed for %s-%d.\n",
Bart Van Asschef68cba4e92016-02-11 11:04:20 -08002559 sdev->device->name, i);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002560 goto err_ring;
2561 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00002562 }
2563
2564 spin_lock(&srpt_dev_lock);
2565 list_add_tail(&sdev->list, &srpt_dev_list);
2566 spin_unlock(&srpt_dev_lock);
2567
2568out:
2569 ib_set_client_data(device, &srpt_client, sdev);
2570 pr_debug("added %s.\n", device->name);
2571 return;
2572
2573err_ring:
2574 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2575 sdev->srq_size, srp_max_req_size,
2576 DMA_FROM_DEVICE);
2577err_event:
2578 ib_unregister_event_handler(&sdev->event_handler);
2579err_cm:
2580 ib_destroy_cm_id(sdev->cm_id);
2581err_srq:
2582 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002583err_pd:
2584 ib_dealloc_pd(sdev->pd);
2585free_dev:
2586 kfree(sdev);
2587err:
2588 sdev = NULL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002589 pr_info("%s(%s) failed.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002590 goto out;
2591}
2592
2593/**
2594 * srpt_remove_one() - InfiniBand device removal callback function.
2595 */
Haggai Eran7c1eb452015-07-30 17:50:14 +03002596static void srpt_remove_one(struct ib_device *device, void *client_data)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002597{
Haggai Eran7c1eb452015-07-30 17:50:14 +03002598 struct srpt_device *sdev = client_data;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002599 int i;
2600
Bart Van Asschea42d9852011-10-14 01:30:46 +00002601 if (!sdev) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002602 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002603 return;
2604 }
2605
2606 srpt_unregister_mad_agent(sdev);
2607
2608 ib_unregister_event_handler(&sdev->event_handler);
2609
2610 /* Cancel any work queued by the just unregistered IB event handler. */
2611 for (i = 0; i < sdev->device->phys_port_cnt; i++)
2612 cancel_work_sync(&sdev->port[i].work);
2613
2614 ib_destroy_cm_id(sdev->cm_id);
2615
2616 /*
2617 * Unregistering a target must happen after destroying sdev->cm_id
2618 * such that no new SRP_LOGIN_REQ information units can arrive while
2619 * destroying the target.
2620 */
2621 spin_lock(&srpt_dev_lock);
2622 list_del(&sdev->list);
2623 spin_unlock(&srpt_dev_lock);
2624 srpt_release_sdev(sdev);
2625
2626 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002627 ib_dealloc_pd(sdev->pd);
2628
2629 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2630 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
2631 sdev->ioctx_ring = NULL;
2632 kfree(sdev);
2633}
2634
2635static struct ib_client srpt_client = {
2636 .name = DRV_NAME,
2637 .add = srpt_add_one,
2638 .remove = srpt_remove_one
2639};
2640
2641static int srpt_check_true(struct se_portal_group *se_tpg)
2642{
2643 return 1;
2644}
2645
2646static int srpt_check_false(struct se_portal_group *se_tpg)
2647{
2648 return 0;
2649}
2650
2651static char *srpt_get_fabric_name(void)
2652{
2653 return "srpt";
2654}
2655
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002656static struct srpt_port *srpt_tpg_to_sport(struct se_portal_group *tpg)
2657{
2658 return tpg->se_tpg_wwn->priv;
2659}
2660
Bart Van Asschea42d9852011-10-14 01:30:46 +00002661static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
2662{
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002663 struct srpt_port *sport = srpt_tpg_to_sport(tpg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002664
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002665 WARN_ON_ONCE(tpg != &sport->port_guid_tpg &&
2666 tpg != &sport->port_gid_tpg);
2667 return tpg == &sport->port_guid_tpg ? sport->port_guid :
2668 sport->port_gid;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002669}
2670
2671static u16 srpt_get_tag(struct se_portal_group *tpg)
2672{
2673 return 1;
2674}
2675
Bart Van Asschea42d9852011-10-14 01:30:46 +00002676static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
2677{
2678 return 1;
2679}
2680
2681static void srpt_release_cmd(struct se_cmd *se_cmd)
2682{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002683 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
2684 struct srpt_send_ioctx, cmd);
2685 struct srpt_rdma_ch *ch = ioctx->ch;
Bart Van Assche3c968882016-04-07 15:55:04 -07002686 unsigned long flags;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002687
Bart Van Asschebd2c52d2017-05-04 15:50:54 -07002688 WARN_ON_ONCE(ioctx->state != SRPT_STATE_DONE &&
2689 !(ioctx->cmd.transport_state & CMD_T_ABORTED));
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002690
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002691 if (ioctx->n_rw_ctx) {
2692 srpt_free_rw_ctxs(ch, ioctx);
2693 ioctx->n_rw_ctx = 0;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002694 }
2695
Bart Van Assche3c968882016-04-07 15:55:04 -07002696 spin_lock_irqsave(&ch->spinlock, flags);
2697 list_add(&ioctx->free_list, &ch->free_list);
2698 spin_unlock_irqrestore(&ch->spinlock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002699}
2700
2701/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00002702 * srpt_close_session() - Forcibly close a session.
2703 *
2704 * Callback function invoked by the TCM core to clean up sessions associated
2705 * with a node ACL when the user invokes
2706 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
2707 */
2708static void srpt_close_session(struct se_session *se_sess)
2709{
2710 DECLARE_COMPLETION_ONSTACK(release_done);
Bart Van Asschef108f0f2016-02-11 11:06:14 -08002711 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
2712 struct srpt_device *sdev = ch->sport->sdev;
2713 bool wait;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002714
Bart Van Asschef108f0f2016-02-11 11:06:14 -08002715 pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num,
2716 ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002717
Bart Van Assche86289912016-02-11 11:08:34 -08002718 mutex_lock(&sdev->mutex);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002719 BUG_ON(ch->release_done);
2720 ch->release_done = &release_done;
Bart Van Asschef108f0f2016-02-11 11:06:14 -08002721 wait = !list_empty(&ch->list);
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08002722 srpt_disconnect_ch(ch);
Bart Van Assche86289912016-02-11 11:08:34 -08002723 mutex_unlock(&sdev->mutex);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002724
Bart Van Asschef108f0f2016-02-11 11:06:14 -08002725 if (!wait)
2726 return;
2727
2728 while (wait_for_completion_timeout(&release_done, 180 * HZ) == 0)
2729 pr_info("%s(%s-%d state %d): still waiting ...\n", __func__,
2730 ch->sess_name, ch->qp->qp_num, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002731}
2732
2733/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00002734 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
2735 *
2736 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
2737 * This object represents an arbitrary integer used to uniquely identify a
2738 * particular attached remote initiator port to a particular SCSI target port
2739 * within a particular SCSI target device within a particular SCSI instance.
2740 */
2741static u32 srpt_sess_get_index(struct se_session *se_sess)
2742{
2743 return 0;
2744}
2745
2746static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
2747{
2748}
2749
Bart Van Asschea42d9852011-10-14 01:30:46 +00002750/* Note: only used from inside debug printk's by the TCM core. */
2751static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
2752{
2753 struct srpt_send_ioctx *ioctx;
2754
2755 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2756 return srpt_get_cmd_state(ioctx);
2757}
2758
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002759static int srpt_parse_guid(u64 *guid, const char *name)
2760{
2761 u16 w[4];
2762 int ret = -EINVAL;
2763
2764 if (sscanf(name, "%hx:%hx:%hx:%hx", &w[0], &w[1], &w[2], &w[3]) != 4)
2765 goto out;
2766 *guid = get_unaligned_be64(w);
2767 ret = 0;
2768out:
2769 return ret;
2770}
2771
Bart Van Asschea42d9852011-10-14 01:30:46 +00002772/**
2773 * srpt_parse_i_port_id() - Parse an initiator port ID.
2774 * @name: ASCII representation of a 128-bit initiator port ID.
2775 * @i_port_id: Binary 128-bit port ID.
2776 */
2777static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
2778{
2779 const char *p;
2780 unsigned len, count, leading_zero_bytes;
2781 int ret, rc;
2782
2783 p = name;
Rasmus Villemoesb60459f2014-10-13 15:54:46 -07002784 if (strncasecmp(p, "0x", 2) == 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002785 p += 2;
2786 ret = -EINVAL;
2787 len = strlen(p);
2788 if (len % 2)
2789 goto out;
2790 count = min(len / 2, 16U);
2791 leading_zero_bytes = 16 - count;
2792 memset(i_port_id, 0, leading_zero_bytes);
2793 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
2794 if (rc < 0)
2795 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
2796 ret = 0;
2797out:
2798 return ret;
2799}
2800
2801/*
2802 * configfs callback function invoked for
2803 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
2804 */
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02002805static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002806{
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002807 u64 guid;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002808 u8 i_port_id[16];
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002809 int ret;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002810
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002811 ret = srpt_parse_guid(&guid, name);
2812 if (ret < 0)
2813 ret = srpt_parse_i_port_id(i_port_id, name);
2814 if (ret < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002815 pr_err("invalid initiator port ID %s\n", name);
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002816 return ret;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002817}
2818
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002819static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
2820 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002821{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002822 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002823 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002824
2825 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
2826}
2827
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002828static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
2829 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002830{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002831 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002832 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002833 unsigned long val;
2834 int ret;
2835
Jingoo Han9d8abf42014-02-05 11:22:05 +09002836 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002837 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09002838 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002839 return -EINVAL;
2840 }
2841 if (val > MAX_SRPT_RDMA_SIZE) {
2842 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
2843 MAX_SRPT_RDMA_SIZE);
2844 return -EINVAL;
2845 }
2846 if (val < DEFAULT_MAX_RDMA_SIZE) {
2847 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
2848 val, DEFAULT_MAX_RDMA_SIZE);
2849 return -EINVAL;
2850 }
2851 sport->port_attrib.srp_max_rdma_size = val;
2852
2853 return count;
2854}
2855
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002856static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
2857 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002858{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002859 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002860 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002861
2862 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
2863}
2864
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002865static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
2866 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002867{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002868 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002869 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002870 unsigned long val;
2871 int ret;
2872
Jingoo Han9d8abf42014-02-05 11:22:05 +09002873 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002874 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09002875 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002876 return -EINVAL;
2877 }
2878 if (val > MAX_SRPT_RSP_SIZE) {
2879 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
2880 MAX_SRPT_RSP_SIZE);
2881 return -EINVAL;
2882 }
2883 if (val < MIN_MAX_RSP_SIZE) {
2884 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
2885 MIN_MAX_RSP_SIZE);
2886 return -EINVAL;
2887 }
2888 sport->port_attrib.srp_max_rsp_size = val;
2889
2890 return count;
2891}
2892
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002893static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
2894 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002895{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002896 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002897 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002898
2899 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
2900}
2901
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002902static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
2903 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002904{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002905 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002906 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002907 unsigned long val;
2908 int ret;
2909
Jingoo Han9d8abf42014-02-05 11:22:05 +09002910 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002911 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09002912 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002913 return -EINVAL;
2914 }
2915 if (val > MAX_SRPT_SRQ_SIZE) {
2916 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
2917 MAX_SRPT_SRQ_SIZE);
2918 return -EINVAL;
2919 }
2920 if (val < MIN_SRPT_SRQ_SIZE) {
2921 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
2922 MIN_SRPT_SRQ_SIZE);
2923 return -EINVAL;
2924 }
2925 sport->port_attrib.srp_sq_size = val;
2926
2927 return count;
2928}
2929
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002930CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
2931CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
2932CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002933
2934static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002935 &srpt_tpg_attrib_attr_srp_max_rdma_size,
2936 &srpt_tpg_attrib_attr_srp_max_rsp_size,
2937 &srpt_tpg_attrib_attr_srp_sq_size,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002938 NULL,
2939};
2940
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002941static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002942{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002943 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002944 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002945
2946 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
2947}
2948
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002949static ssize_t srpt_tpg_enable_store(struct config_item *item,
2950 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002951{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002952 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Assche2bce1a62016-12-09 11:00:33 -08002953 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
Bart Van Assche043a6802016-02-11 11:09:28 -08002954 struct srpt_device *sdev = sport->sdev;
2955 struct srpt_rdma_ch *ch;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002956 unsigned long tmp;
2957 int ret;
2958
Jingoo Han9d8abf42014-02-05 11:22:05 +09002959 ret = kstrtoul(page, 0, &tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002960 if (ret < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002961 pr_err("Unable to extract srpt_tpg_store_enable\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002962 return -EINVAL;
2963 }
2964
2965 if ((tmp != 0) && (tmp != 1)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002966 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002967 return -EINVAL;
2968 }
Bart Van Assche043a6802016-02-11 11:09:28 -08002969 if (sport->enabled == tmp)
2970 goto out;
2971 sport->enabled = tmp;
2972 if (sport->enabled)
2973 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002974
Bart Van Assche043a6802016-02-11 11:09:28 -08002975 mutex_lock(&sdev->mutex);
2976 list_for_each_entry(ch, &sdev->rch_list, list) {
2977 if (ch->sport == sport) {
2978 pr_debug("%s: ch %p %s-%d\n", __func__, ch,
2979 ch->sess_name, ch->qp->qp_num);
2980 srpt_disconnect_ch(ch);
2981 srpt_close_ch(ch);
2982 }
2983 }
2984 mutex_unlock(&sdev->mutex);
2985
2986out:
Bart Van Asschea42d9852011-10-14 01:30:46 +00002987 return count;
2988}
2989
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002990CONFIGFS_ATTR(srpt_tpg_, enable);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002991
2992static struct configfs_attribute *srpt_tpg_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002993 &srpt_tpg_attr_enable,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002994 NULL,
2995};
2996
2997/**
2998 * configfs callback invoked for
2999 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3000 */
3001static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3002 struct config_group *group,
3003 const char *name)
3004{
Bart Van Assche2bce1a62016-12-09 11:00:33 -08003005 struct srpt_port *sport = wwn->priv;
3006 static struct se_portal_group *tpg;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003007 int res;
3008
Bart Van Assche2bce1a62016-12-09 11:00:33 -08003009 WARN_ON_ONCE(wwn != &sport->port_guid_wwn &&
3010 wwn != &sport->port_gid_wwn);
3011 tpg = wwn == &sport->port_guid_wwn ? &sport->port_guid_tpg :
3012 &sport->port_gid_tpg;
3013 res = core_tpg_register(wwn, tpg, SCSI_PROTOCOL_SRP);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003014 if (res)
3015 return ERR_PTR(res);
3016
Bart Van Assche2bce1a62016-12-09 11:00:33 -08003017 return tpg;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003018}
3019
3020/**
3021 * configfs callback invoked for
3022 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3023 */
3024static void srpt_drop_tpg(struct se_portal_group *tpg)
3025{
Bart Van Assche2bce1a62016-12-09 11:00:33 -08003026 struct srpt_port *sport = srpt_tpg_to_sport(tpg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003027
3028 sport->enabled = false;
Bart Van Assche2bce1a62016-12-09 11:00:33 -08003029 core_tpg_deregister(tpg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003030}
3031
3032/**
3033 * configfs callback invoked for
3034 * mkdir /sys/kernel/config/target/$driver/$port
3035 */
3036static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3037 struct config_group *group,
3038 const char *name)
3039{
Bart Van Assche2bce1a62016-12-09 11:00:33 -08003040 return srpt_lookup_wwn(name) ? : ERR_PTR(-EINVAL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003041}
3042
3043/**
3044 * configfs callback invoked for
3045 * rmdir /sys/kernel/config/target/$driver/$port
3046 */
3047static void srpt_drop_tport(struct se_wwn *wwn)
3048{
Bart Van Asschea42d9852011-10-14 01:30:46 +00003049}
3050
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003051static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003052{
3053 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3054}
3055
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003056CONFIGFS_ATTR_RO(srpt_wwn_, version);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003057
3058static struct configfs_attribute *srpt_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003059 &srpt_wwn_attr_version,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003060 NULL,
3061};
3062
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003063static const struct target_core_fabric_ops srpt_template = {
3064 .module = THIS_MODULE,
3065 .name = "srpt",
Bart Van Asschea42d9852011-10-14 01:30:46 +00003066 .get_fabric_name = srpt_get_fabric_name,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003067 .tpg_get_wwn = srpt_get_fabric_wwn,
3068 .tpg_get_tag = srpt_get_tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003069 .tpg_check_demo_mode = srpt_check_false,
3070 .tpg_check_demo_mode_cache = srpt_check_true,
3071 .tpg_check_demo_mode_write_protect = srpt_check_true,
3072 .tpg_check_prod_mode_write_protect = srpt_check_false,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003073 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3074 .release_cmd = srpt_release_cmd,
3075 .check_stop_free = srpt_check_stop_free,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003076 .close_session = srpt_close_session,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003077 .sess_get_index = srpt_sess_get_index,
3078 .sess_get_initiator_sid = NULL,
3079 .write_pending = srpt_write_pending,
3080 .write_pending_status = srpt_write_pending_status,
3081 .set_default_node_attributes = srpt_set_default_node_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003082 .get_cmd_state = srpt_get_tcm_cmd_state,
Joern Engelb79fafa2013-07-03 11:22:17 -04003083 .queue_data_in = srpt_queue_data_in,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003084 .queue_status = srpt_queue_status,
Joern Engelb79fafa2013-07-03 11:22:17 -04003085 .queue_tm_rsp = srpt_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003086 .aborted_task = srpt_aborted_task,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003087 /*
3088 * Setup function pointers for generic logic in
3089 * target_core_fabric_configfs.c
3090 */
3091 .fabric_make_wwn = srpt_make_tport,
3092 .fabric_drop_wwn = srpt_drop_tport,
3093 .fabric_make_tpg = srpt_make_tpg,
3094 .fabric_drop_tpg = srpt_drop_tpg,
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003095 .fabric_init_nodeacl = srpt_init_nodeacl,
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003096
3097 .tfc_wwn_attrs = srpt_wwn_attrs,
3098 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3099 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003100};
3101
3102/**
3103 * srpt_init_module() - Kernel module initialization.
3104 *
3105 * Note: Since ib_register_client() registers callback functions, and since at
3106 * least one of these callback functions (srpt_add_one()) calls target core
3107 * functions, this driver must be registered with the target core before
3108 * ib_register_client() is called.
3109 */
3110static int __init srpt_init_module(void)
3111{
3112 int ret;
3113
3114 ret = -EINVAL;
3115 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003116 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003117 " srp_max_req_size -- must be at least %d.\n",
3118 srp_max_req_size, MIN_MAX_REQ_SIZE);
3119 goto out;
3120 }
3121
3122 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3123 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003124 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003125 " srpt_srq_size -- must be in the range [%d..%d].\n",
3126 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3127 goto out;
3128 }
3129
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003130 ret = target_register_template(&srpt_template);
3131 if (ret)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003132 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003133
3134 ret = ib_register_client(&srpt_client);
3135 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003136 pr_err("couldn't register IB client\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003137 goto out_unregister_target;
3138 }
3139
3140 return 0;
3141
3142out_unregister_target:
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003143 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003144out:
3145 return ret;
3146}
3147
3148static void __exit srpt_cleanup_module(void)
3149{
3150 ib_unregister_client(&srpt_client);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003151 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003152}
3153
3154module_init(srpt_init_module);
3155module_exit(srpt_cleanup_module);