blob: 883bbfe08e0efa64d87ab2652b0e773516d54450 [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 Assche9d2aa2b42016-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 Assche9d2aa2b42016-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 Assche9d2aa2b42016-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{
420 ib_destroy_ah(mad_wc->send_buf->ah);
421 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 Assche9d2aa2b42016-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:
484 ib_destroy_ah(ah);
485err:
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;
503 int ret;
504
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800505 memset(&port_modify, 0, sizeof(port_modify));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000506 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
507 port_modify.clr_port_cap_mask = 0;
508
509 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
510 if (ret)
511 goto err_mod_port;
512
513 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
514 if (ret)
515 goto err_query_port;
516
517 sport->sm_lid = port_attr.sm_lid;
518 sport->lid = port_attr.lid;
519
Matan Barak55ee3ab2015-10-15 18:38:45 +0300520 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid,
521 NULL);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000522 if (ret)
523 goto err_query_port;
524
Doug Ledford716b076b2016-08-24 12:14:19 -0400525 snprintf(sport->port_guid, sizeof(sport->port_guid),
526 "0x%016llx%016llx",
527 be64_to_cpu(sport->gid.global.subnet_prefix),
528 be64_to_cpu(sport->gid.global.interface_id));
529
Bart Van Asschea42d9852011-10-14 01:30:46 +0000530 if (!sport->mad_agent) {
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800531 memset(&reg_req, 0, sizeof(reg_req));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000532 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
533 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
534 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
535 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
536
537 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
538 sport->port,
539 IB_QPT_GSI,
540 &reg_req, 0,
541 srpt_mad_send_handler,
542 srpt_mad_recv_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -0400543 sport, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000544 if (IS_ERR(sport->mad_agent)) {
545 ret = PTR_ERR(sport->mad_agent);
546 sport->mad_agent = NULL;
547 goto err_query_port;
548 }
549 }
550
551 return 0;
552
553err_query_port:
554
555 port_modify.set_port_cap_mask = 0;
556 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
557 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
558
559err_mod_port:
560
561 return ret;
562}
563
564/**
565 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
566 *
567 * Note: It is safe to call this function more than once for the same device.
568 */
569static void srpt_unregister_mad_agent(struct srpt_device *sdev)
570{
571 struct ib_port_modify port_modify = {
572 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
573 };
574 struct srpt_port *sport;
575 int i;
576
577 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
578 sport = &sdev->port[i - 1];
579 WARN_ON(sport->port != i);
580 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400581 pr_err("disabling MAD processing failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000582 if (sport->mad_agent) {
583 ib_unregister_mad_agent(sport->mad_agent);
584 sport->mad_agent = NULL;
585 }
586 }
587}
588
589/**
590 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
591 */
592static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
593 int ioctx_size, int dma_size,
594 enum dma_data_direction dir)
595{
596 struct srpt_ioctx *ioctx;
597
598 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
599 if (!ioctx)
600 goto err;
601
602 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
603 if (!ioctx->buf)
604 goto err_free_ioctx;
605
606 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
607 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
608 goto err_free_buf;
609
610 return ioctx;
611
612err_free_buf:
613 kfree(ioctx->buf);
614err_free_ioctx:
615 kfree(ioctx);
616err:
617 return NULL;
618}
619
620/**
621 * srpt_free_ioctx() - Free an SRPT I/O context structure.
622 */
623static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
624 int dma_size, enum dma_data_direction dir)
625{
626 if (!ioctx)
627 return;
628
629 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
630 kfree(ioctx->buf);
631 kfree(ioctx);
632}
633
634/**
635 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
636 * @sdev: Device to allocate the I/O context ring for.
637 * @ring_size: Number of elements in the I/O context ring.
638 * @ioctx_size: I/O context size.
639 * @dma_size: DMA buffer size.
640 * @dir: DMA data direction.
641 */
642static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
643 int ring_size, int ioctx_size,
644 int dma_size, enum dma_data_direction dir)
645{
646 struct srpt_ioctx **ring;
647 int i;
648
649 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
650 && ioctx_size != sizeof(struct srpt_send_ioctx));
651
652 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
653 if (!ring)
654 goto out;
655 for (i = 0; i < ring_size; ++i) {
656 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
657 if (!ring[i])
658 goto err;
659 ring[i]->index = i;
660 }
661 goto out;
662
663err:
664 while (--i >= 0)
665 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
666 kfree(ring);
Jesper Juhl715252d2012-02-04 23:49:40 +0100667 ring = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000668out:
669 return ring;
670}
671
672/**
673 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
674 */
675static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
676 struct srpt_device *sdev, int ring_size,
677 int dma_size, enum dma_data_direction dir)
678{
679 int i;
680
681 for (i = 0; i < ring_size; ++i)
682 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
683 kfree(ioctx_ring);
684}
685
686/**
687 * srpt_get_cmd_state() - Get the state of a SCSI command.
688 */
689static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
690{
691 enum srpt_command_state state;
692 unsigned long flags;
693
694 BUG_ON(!ioctx);
695
696 spin_lock_irqsave(&ioctx->spinlock, flags);
697 state = ioctx->state;
698 spin_unlock_irqrestore(&ioctx->spinlock, flags);
699 return state;
700}
701
702/**
703 * srpt_set_cmd_state() - Set the state of a SCSI command.
704 *
705 * Does not modify the state of aborted commands. Returns the previous command
706 * state.
707 */
708static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
709 enum srpt_command_state new)
710{
711 enum srpt_command_state previous;
712 unsigned long flags;
713
714 BUG_ON(!ioctx);
715
716 spin_lock_irqsave(&ioctx->spinlock, flags);
717 previous = ioctx->state;
718 if (previous != SRPT_STATE_DONE)
719 ioctx->state = new;
720 spin_unlock_irqrestore(&ioctx->spinlock, flags);
721
722 return previous;
723}
724
725/**
726 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
727 *
728 * Returns true if and only if the previous command state was equal to 'old'.
729 */
730static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
731 enum srpt_command_state old,
732 enum srpt_command_state new)
733{
734 enum srpt_command_state previous;
735 unsigned long flags;
736
737 WARN_ON(!ioctx);
738 WARN_ON(old == SRPT_STATE_DONE);
739 WARN_ON(new == SRPT_STATE_NEW);
740
741 spin_lock_irqsave(&ioctx->spinlock, flags);
742 previous = ioctx->state;
743 if (previous == old)
744 ioctx->state = new;
745 spin_unlock_irqrestore(&ioctx->spinlock, flags);
746 return previous == old;
747}
748
749/**
750 * srpt_post_recv() - Post an IB receive request.
751 */
752static int srpt_post_recv(struct srpt_device *sdev,
753 struct srpt_recv_ioctx *ioctx)
754{
755 struct ib_sge list;
756 struct ib_recv_wr wr, *bad_wr;
757
758 BUG_ON(!sdev);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000759 list.addr = ioctx->ioctx.dma;
760 list.length = srp_max_req_size;
Jason Gunthorpe5a783952015-07-30 17:22:24 -0600761 list.lkey = sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000762
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200763 ioctx->ioctx.cqe.done = srpt_recv_done;
764 wr.wr_cqe = &ioctx->ioctx.cqe;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000765 wr.next = NULL;
766 wr.sg_list = &list;
767 wr.num_sge = 1;
768
769 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
770}
771
772/**
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -0800773 * srpt_zerolength_write() - Perform a zero-length RDMA write.
774 *
775 * A quote from the InfiniBand specification: C9-88: For an HCA responder
776 * using Reliable Connection service, for each zero-length RDMA READ or WRITE
777 * request, the R_Key shall not be validated, even if the request includes
778 * Immediate data.
779 */
780static int srpt_zerolength_write(struct srpt_rdma_ch *ch)
781{
782 struct ib_send_wr wr, *bad_wr;
783
784 memset(&wr, 0, sizeof(wr));
785 wr.opcode = IB_WR_RDMA_WRITE;
786 wr.wr_cqe = &ch->zw_cqe;
787 wr.send_flags = IB_SEND_SIGNALED;
788 return ib_post_send(ch->qp, &wr, &bad_wr);
789}
790
791static void srpt_zerolength_write_done(struct ib_cq *cq, struct ib_wc *wc)
792{
793 struct srpt_rdma_ch *ch = cq->cq_context;
794
Bart Van Assche387add42016-02-11 11:10:09 -0800795 if (wc->status == IB_WC_SUCCESS) {
796 srpt_process_wait_list(ch);
797 } else {
798 if (srpt_set_ch_state(ch, CH_DISCONNECTED))
799 schedule_work(&ch->release_work);
800 else
Dan Carpenter56586002016-03-18 08:41:59 +0300801 WARN_ONCE(1, "%s-%d\n", ch->sess_name, ch->qp->qp_num);
Bart Van Assche387add42016-02-11 11:10:09 -0800802 }
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -0800803}
804
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200805static int srpt_alloc_rw_ctxs(struct srpt_send_ioctx *ioctx,
806 struct srp_direct_buf *db, int nbufs, struct scatterlist **sg,
807 unsigned *sg_cnt)
808{
809 enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
810 struct srpt_rdma_ch *ch = ioctx->ch;
811 struct scatterlist *prev = NULL;
812 unsigned prev_nents;
813 int ret, i;
814
815 if (nbufs == 1) {
816 ioctx->rw_ctxs = &ioctx->s_rw_ctx;
817 } else {
818 ioctx->rw_ctxs = kmalloc_array(nbufs, sizeof(*ioctx->rw_ctxs),
819 GFP_KERNEL);
820 if (!ioctx->rw_ctxs)
821 return -ENOMEM;
822 }
823
824 for (i = ioctx->n_rw_ctx; i < nbufs; i++, db++) {
825 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
826 u64 remote_addr = be64_to_cpu(db->va);
827 u32 size = be32_to_cpu(db->len);
828 u32 rkey = be32_to_cpu(db->key);
829
830 ret = target_alloc_sgl(&ctx->sg, &ctx->nents, size, false,
831 i < nbufs - 1);
832 if (ret)
833 goto unwind;
834
835 ret = rdma_rw_ctx_init(&ctx->rw, ch->qp, ch->sport->port,
836 ctx->sg, ctx->nents, 0, remote_addr, rkey, dir);
837 if (ret < 0) {
838 target_free_sgl(ctx->sg, ctx->nents);
839 goto unwind;
840 }
841
842 ioctx->n_rdma += ret;
843 ioctx->n_rw_ctx++;
844
845 if (prev) {
846 sg_unmark_end(&prev[prev_nents - 1]);
847 sg_chain(prev, prev_nents + 1, ctx->sg);
848 } else {
849 *sg = ctx->sg;
850 }
851
852 prev = ctx->sg;
853 prev_nents = ctx->nents;
854
855 *sg_cnt += ctx->nents;
856 }
857
858 return 0;
859
860unwind:
861 while (--i >= 0) {
862 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
863
864 rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
865 ctx->sg, ctx->nents, dir);
866 target_free_sgl(ctx->sg, ctx->nents);
867 }
868 if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
869 kfree(ioctx->rw_ctxs);
870 return ret;
871}
872
873static void srpt_free_rw_ctxs(struct srpt_rdma_ch *ch,
874 struct srpt_send_ioctx *ioctx)
875{
876 enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
877 int i;
878
879 for (i = 0; i < ioctx->n_rw_ctx; i++) {
880 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
881
882 rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
883 ctx->sg, ctx->nents, dir);
884 target_free_sgl(ctx->sg, ctx->nents);
885 }
886
887 if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
888 kfree(ioctx->rw_ctxs);
889}
890
891static inline void *srpt_get_desc_buf(struct srp_cmd *srp_cmd)
892{
893 /*
894 * The pointer computations below will only be compiled correctly
895 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
896 * whether srp_cmd::add_data has been declared as a byte pointer.
897 */
898 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0) &&
899 !__same_type(srp_cmd->add_data[0], (u8)0));
900
901 /*
902 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
903 * CDB LENGTH' field are reserved and the size in bytes of this field
904 * is four times the value specified in bits 3..7. Hence the "& ~3".
905 */
906 return srp_cmd->add_data + (srp_cmd->add_cdb_len & ~3);
907}
908
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -0800909/**
Bart Van Asschea42d9852011-10-14 01:30:46 +0000910 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
911 * @ioctx: Pointer to the I/O context associated with the request.
912 * @srp_cmd: Pointer to the SRP_CMD request data.
913 * @dir: Pointer to the variable to which the transfer direction will be
914 * written.
915 * @data_len: Pointer to the variable to which the total data length of all
916 * descriptors in the SRP_CMD request will be written.
917 *
918 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
919 *
920 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
921 * -ENOMEM when memory allocation fails and zero upon success.
922 */
923static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200924 struct srp_cmd *srp_cmd, enum dma_data_direction *dir,
925 struct scatterlist **sg, unsigned *sg_cnt, u64 *data_len)
Bart Van Asschea42d9852011-10-14 01:30:46 +0000926{
Bart Van Asschea42d9852011-10-14 01:30:46 +0000927 BUG_ON(!dir);
928 BUG_ON(!data_len);
929
Bart Van Asschea42d9852011-10-14 01:30:46 +0000930 /*
931 * The lower four bits of the buffer format field contain the DATA-IN
932 * buffer descriptor format, and the highest four bits contain the
933 * DATA-OUT buffer descriptor format.
934 */
Bart Van Asschea42d9852011-10-14 01:30:46 +0000935 if (srp_cmd->buf_fmt & 0xf)
936 /* DATA-IN: transfer data from target to initiator (read). */
937 *dir = DMA_FROM_DEVICE;
938 else if (srp_cmd->buf_fmt >> 4)
939 /* DATA-OUT: transfer data from initiator to target (write). */
940 *dir = DMA_TO_DEVICE;
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200941 else
942 *dir = DMA_NONE;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000943
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200944 /* initialize data_direction early as srpt_alloc_rw_ctxs needs it */
945 ioctx->cmd.data_direction = *dir;
946
Bart Van Asschea42d9852011-10-14 01:30:46 +0000947 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
948 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200949 struct srp_direct_buf *db = srpt_get_desc_buf(srp_cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000950
Bart Van Asschea42d9852011-10-14 01:30:46 +0000951 *data_len = be32_to_cpu(db->len);
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200952 return srpt_alloc_rw_ctxs(ioctx, db, 1, sg, sg_cnt);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000953 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
954 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200955 struct srp_indirect_buf *idb = srpt_get_desc_buf(srp_cmd);
956 int nbufs = be32_to_cpu(idb->table_desc.len) /
957 sizeof(struct srp_direct_buf);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000958
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200959 if (nbufs >
Bart Van Asschea42d9852011-10-14 01:30:46 +0000960 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400961 pr_err("received unsupported SRP_CMD request"
Bart Van Asschea42d9852011-10-14 01:30:46 +0000962 " type (%u out + %u in != %u / %zu)\n",
963 srp_cmd->data_out_desc_cnt,
964 srp_cmd->data_in_desc_cnt,
965 be32_to_cpu(idb->table_desc.len),
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200966 sizeof(struct srp_direct_buf));
967 return -EINVAL;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000968 }
969
Bart Van Asschea42d9852011-10-14 01:30:46 +0000970 *data_len = be32_to_cpu(idb->len);
Christoph Hellwigb99f8e42016-05-03 18:01:11 +0200971 return srpt_alloc_rw_ctxs(ioctx, idb->desc_list, nbufs,
972 sg, sg_cnt);
973 } else {
974 *data_len = 0;
975 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000976 }
Bart Van Asschea42d9852011-10-14 01:30:46 +0000977}
978
979/**
980 * srpt_init_ch_qp() - Initialize queue pair attributes.
981 *
982 * Initialized the attributes of queue pair 'qp' by allowing local write,
983 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
984 */
985static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
986{
987 struct ib_qp_attr *attr;
988 int ret;
989
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800990 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000991 if (!attr)
992 return -ENOMEM;
993
994 attr->qp_state = IB_QPS_INIT;
995 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
996 IB_ACCESS_REMOTE_WRITE;
997 attr->port_num = ch->sport->port;
998 attr->pkey_index = 0;
999
1000 ret = ib_modify_qp(qp, attr,
1001 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
1002 IB_QP_PKEY_INDEX);
1003
1004 kfree(attr);
1005 return ret;
1006}
1007
1008/**
1009 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
1010 * @ch: channel of the queue pair.
1011 * @qp: queue pair to change the state of.
1012 *
1013 * Returns zero upon success and a negative value upon failure.
1014 *
1015 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1016 * If this structure ever becomes larger, it might be necessary to allocate
1017 * it dynamically instead of on the stack.
1018 */
1019static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1020{
1021 struct ib_qp_attr qp_attr;
1022 int attr_mask;
1023 int ret;
1024
1025 qp_attr.qp_state = IB_QPS_RTR;
1026 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1027 if (ret)
1028 goto out;
1029
1030 qp_attr.max_dest_rd_atomic = 4;
1031
1032 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1033
1034out:
1035 return ret;
1036}
1037
1038/**
1039 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
1040 * @ch: channel of the queue pair.
1041 * @qp: queue pair to change the state of.
1042 *
1043 * Returns zero upon success and a negative value upon failure.
1044 *
1045 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1046 * If this structure ever becomes larger, it might be necessary to allocate
1047 * it dynamically instead of on the stack.
1048 */
1049static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1050{
1051 struct ib_qp_attr qp_attr;
1052 int attr_mask;
1053 int ret;
1054
1055 qp_attr.qp_state = IB_QPS_RTS;
1056 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1057 if (ret)
1058 goto out;
1059
1060 qp_attr.max_rd_atomic = 4;
1061
1062 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1063
1064out:
1065 return ret;
1066}
1067
1068/**
1069 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1070 */
1071static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1072{
1073 struct ib_qp_attr qp_attr;
1074
1075 qp_attr.qp_state = IB_QPS_ERR;
1076 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1077}
1078
1079/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001080 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1081 */
1082static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1083{
1084 struct srpt_send_ioctx *ioctx;
Bart Van Assche3c968882016-04-07 15:55:04 -07001085 unsigned long flags;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001086
1087 BUG_ON(!ch);
1088
Bart Van Assche3c968882016-04-07 15:55:04 -07001089 ioctx = NULL;
1090 spin_lock_irqsave(&ch->spinlock, flags);
1091 if (!list_empty(&ch->free_list)) {
1092 ioctx = list_first_entry(&ch->free_list,
1093 struct srpt_send_ioctx, free_list);
1094 list_del(&ioctx->free_list);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001095 }
Bart Van Assche3c968882016-04-07 15:55:04 -07001096 spin_unlock_irqrestore(&ch->spinlock, flags);
1097
1098 if (!ioctx)
1099 return ioctx;
1100
1101 BUG_ON(ioctx->ch != ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001102 spin_lock_init(&ioctx->spinlock);
1103 ioctx->state = SRPT_STATE_NEW;
Bart Van Assche3c968882016-04-07 15:55:04 -07001104 ioctx->n_rdma = 0;
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001105 ioctx->n_rw_ctx = 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001106 init_completion(&ioctx->tx_done);
Bart Van Assche3c968882016-04-07 15:55:04 -07001107 ioctx->queue_status_only = false;
1108 /*
1109 * transport_init_se_cmd() does not initialize all fields, so do it
1110 * here.
1111 */
1112 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1113 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001114
1115 return ioctx;
1116}
1117
1118/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001119 * srpt_abort_cmd() - Abort a SCSI command.
1120 * @ioctx: I/O context associated with the SCSI command.
1121 * @context: Preferred execution context.
1122 */
1123static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1124{
1125 enum srpt_command_state state;
1126 unsigned long flags;
1127
1128 BUG_ON(!ioctx);
1129
1130 /*
1131 * If the command is in a state where the target core is waiting for
Bart Van Assche49f40162016-02-11 11:07:11 -08001132 * the ib_srpt driver, change the state to the next state.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001133 */
1134
1135 spin_lock_irqsave(&ioctx->spinlock, flags);
1136 state = ioctx->state;
1137 switch (state) {
1138 case SRPT_STATE_NEED_DATA:
1139 ioctx->state = SRPT_STATE_DATA_IN;
1140 break;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001141 case SRPT_STATE_CMD_RSP_SENT:
1142 case SRPT_STATE_MGMT_RSP_SENT:
1143 ioctx->state = SRPT_STATE_DONE;
1144 break;
1145 default:
Bart Van Assche49f40162016-02-11 11:07:11 -08001146 WARN_ONCE(true, "%s: unexpected I/O context state %d\n",
1147 __func__, state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001148 break;
1149 }
1150 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1151
Bart Van Asschea42d9852011-10-14 01:30:46 +00001152 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
Bart Van Assche649ee052015-04-14 13:26:44 +02001153 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001154
1155 switch (state) {
1156 case SRPT_STATE_NEW:
1157 case SRPT_STATE_DATA_IN:
1158 case SRPT_STATE_MGMT:
Bart Van Assche49f40162016-02-11 11:07:11 -08001159 case SRPT_STATE_DONE:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001160 /*
1161 * Do nothing - defer abort processing until
1162 * srpt_queue_response() is invoked.
1163 */
Bart Van Asschea42d9852011-10-14 01:30:46 +00001164 break;
1165 case SRPT_STATE_NEED_DATA:
Bart Van Assche49f40162016-02-11 11:07:11 -08001166 pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag);
1167 transport_generic_request_failure(&ioctx->cmd,
1168 TCM_CHECK_CONDITION_ABORT_CMD);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001169 break;
1170 case SRPT_STATE_CMD_RSP_SENT:
1171 /*
1172 * SRP_RSP sending failed or the SRP_RSP send completion has
1173 * not been received in time.
1174 */
Bart Van Assche49f40162016-02-11 11:07:11 -08001175 transport_generic_free_cmd(&ioctx->cmd, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001176 break;
1177 case SRPT_STATE_MGMT_RSP_SENT:
Bart Van Assche49f40162016-02-11 11:07:11 -08001178 transport_generic_free_cmd(&ioctx->cmd, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001179 break;
1180 default:
Grant Grundler532ec6f2013-03-26 21:48:28 +00001181 WARN(1, "Unexpected command state (%d)", state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001182 break;
1183 }
1184
Bart Van Asschea42d9852011-10-14 01:30:46 +00001185 return state;
1186}
1187
1188/**
Christoph Hellwige672a472012-07-08 15:58:43 -04001189 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1190 * the data that has been transferred via IB RDMA had to be postponed until the
Masanari Iida142ad5d2012-08-10 00:07:58 +00001191 * check_stop_free() callback. None of this is necessary anymore and needs to
Christoph Hellwige672a472012-07-08 15:58:43 -04001192 * be cleaned up.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001193 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001194static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001195{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001196 struct srpt_rdma_ch *ch = cq->cq_context;
1197 struct srpt_send_ioctx *ioctx =
Bart Van Assche19f57292015-12-31 09:56:43 +01001198 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001199
Bart Van Asschea42d9852011-10-14 01:30:46 +00001200 WARN_ON(ioctx->n_rdma <= 0);
1201 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001202 ioctx->n_rdma = 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001203
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001204 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1205 pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
1206 ioctx, wc->status);
1207 srpt_abort_cmd(ioctx);
1208 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001209 }
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001210
1211 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1212 SRPT_STATE_DATA_IN))
1213 target_execute_cmd(&ioctx->cmd);
1214 else
1215 pr_err("%s[%d]: wrong state = %d\n", __func__,
1216 __LINE__, srpt_get_cmd_state(ioctx));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001217}
1218
Bart Van Asschea42d9852011-10-14 01:30:46 +00001219/**
1220 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1221 * @ch: RDMA channel through which the request has been received.
1222 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1223 * be built in the buffer ioctx->buf points at and hence this function will
1224 * overwrite the request data.
1225 * @tag: tag of the request for which this response is being generated.
1226 * @status: value for the STATUS field of the SRP_RSP information unit.
1227 *
1228 * Returns the size in bytes of the SRP_RSP response.
1229 *
1230 * An SRP_RSP response contains a SCSI status or service response. See also
1231 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1232 * response. See also SPC-2 for more information about sense data.
1233 */
1234static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1235 struct srpt_send_ioctx *ioctx, u64 tag,
1236 int status)
1237{
1238 struct srp_rsp *srp_rsp;
1239 const u8 *sense_data;
1240 int sense_data_len, max_sense_len;
1241
1242 /*
1243 * The lowest bit of all SAM-3 status codes is zero (see also
1244 * paragraph 5.3 in SAM-3).
1245 */
1246 WARN_ON(status & 1);
1247
1248 srp_rsp = ioctx->ioctx.buf;
1249 BUG_ON(!srp_rsp);
1250
1251 sense_data = ioctx->sense_data;
1252 sense_data_len = ioctx->cmd.scsi_sense_length;
1253 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1254
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08001255 memset(srp_rsp, 0, sizeof(*srp_rsp));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001256 srp_rsp->opcode = SRP_RSP;
1257 srp_rsp->req_lim_delta =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301258 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001259 srp_rsp->tag = tag;
1260 srp_rsp->status = status;
1261
1262 if (sense_data_len) {
1263 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1264 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1265 if (sense_data_len > max_sense_len) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001266 pr_warn("truncated sense data from %d to %d"
1267 " bytes\n", sense_data_len, max_sense_len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001268 sense_data_len = max_sense_len;
1269 }
1270
1271 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1272 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1273 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1274 }
1275
1276 return sizeof(*srp_rsp) + sense_data_len;
1277}
1278
1279/**
1280 * srpt_build_tskmgmt_rsp() - Build a task management response.
1281 * @ch: RDMA channel through which the request has been received.
1282 * @ioctx: I/O context in which the SRP_RSP response will be built.
1283 * @rsp_code: RSP_CODE that will be stored in the response.
1284 * @tag: Tag of the request for which this response is being generated.
1285 *
1286 * Returns the size in bytes of the SRP_RSP response.
1287 *
1288 * An SRP_RSP response contains a SCSI status or service response. See also
1289 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1290 * response.
1291 */
1292static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1293 struct srpt_send_ioctx *ioctx,
1294 u8 rsp_code, u64 tag)
1295{
1296 struct srp_rsp *srp_rsp;
1297 int resp_data_len;
1298 int resp_len;
1299
Jack Wangc807f642013-09-30 10:09:05 +02001300 resp_data_len = 4;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001301 resp_len = sizeof(*srp_rsp) + resp_data_len;
1302
1303 srp_rsp = ioctx->ioctx.buf;
1304 BUG_ON(!srp_rsp);
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08001305 memset(srp_rsp, 0, sizeof(*srp_rsp));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001306
1307 srp_rsp->opcode = SRP_RSP;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301308 srp_rsp->req_lim_delta =
1309 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001310 srp_rsp->tag = tag;
1311
Jack Wangc807f642013-09-30 10:09:05 +02001312 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1313 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1314 srp_rsp->data[3] = rsp_code;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001315
1316 return resp_len;
1317}
1318
Bart Van Asschea42d9852011-10-14 01:30:46 +00001319static int srpt_check_stop_free(struct se_cmd *cmd)
1320{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001321 struct srpt_send_ioctx *ioctx = container_of(cmd,
1322 struct srpt_send_ioctx, cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001323
Bart Van Asscheafc16602015-04-27 13:52:36 +02001324 return target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001325}
1326
1327/**
1328 * srpt_handle_cmd() - Process SRP_CMD.
1329 */
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001330static void srpt_handle_cmd(struct srpt_rdma_ch *ch,
1331 struct srpt_recv_ioctx *recv_ioctx,
1332 struct srpt_send_ioctx *send_ioctx)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001333{
1334 struct se_cmd *cmd;
1335 struct srp_cmd *srp_cmd;
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001336 struct scatterlist *sg = NULL;
1337 unsigned sg_cnt = 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001338 u64 data_len;
1339 enum dma_data_direction dir;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001340 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001341
1342 BUG_ON(!send_ioctx);
1343
1344 srp_cmd = recv_ioctx->ioctx.buf;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001345 cmd = &send_ioctx->cmd;
Bart Van Assche649ee052015-04-14 13:26:44 +02001346 cmd->tag = srp_cmd->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001347
1348 switch (srp_cmd->task_attr) {
1349 case SRP_CMD_SIMPLE_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001350 cmd->sam_task_attr = TCM_SIMPLE_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001351 break;
1352 case SRP_CMD_ORDERED_Q:
1353 default:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001354 cmd->sam_task_attr = TCM_ORDERED_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001355 break;
1356 case SRP_CMD_HEAD_OF_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001357 cmd->sam_task_attr = TCM_HEAD_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001358 break;
1359 case SRP_CMD_ACA:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001360 cmd->sam_task_attr = TCM_ACA_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001361 break;
1362 }
1363
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001364 rc = srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &sg, &sg_cnt,
1365 &data_len);
1366 if (rc) {
1367 if (rc != -EAGAIN) {
1368 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
1369 srp_cmd->tag);
1370 }
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001371 goto release_ioctx;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001372 }
1373
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001374 rc = target_submit_cmd_map_sgls(cmd, ch->sess, srp_cmd->cdb,
Bart Van Asschee1dd4132016-02-11 11:05:19 -08001375 &send_ioctx->sense_data[0],
1376 scsilun_to_int(&srp_cmd->lun), data_len,
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001377 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF,
1378 sg, sg_cnt, NULL, 0, NULL, 0);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001379 if (rc != 0) {
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001380 pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc,
1381 srp_cmd->tag);
1382 goto release_ioctx;
Nicholas Bellinger187e70a2012-03-17 20:12:36 -07001383 }
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001384 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001385
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001386release_ioctx:
1387 send_ioctx->state = SRPT_STATE_DONE;
1388 srpt_release_cmd(cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001389}
1390
Bart Van Asschea42d9852011-10-14 01:30:46 +00001391static int srp_tmr_to_tcm(int fn)
1392{
1393 switch (fn) {
1394 case SRP_TSK_ABORT_TASK:
1395 return TMR_ABORT_TASK;
1396 case SRP_TSK_ABORT_TASK_SET:
1397 return TMR_ABORT_TASK_SET;
1398 case SRP_TSK_CLEAR_TASK_SET:
1399 return TMR_CLEAR_TASK_SET;
1400 case SRP_TSK_LUN_RESET:
1401 return TMR_LUN_RESET;
1402 case SRP_TSK_CLEAR_ACA:
1403 return TMR_CLEAR_ACA;
1404 default:
1405 return -1;
1406 }
1407}
1408
1409/**
1410 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1411 *
1412 * Returns 0 if and only if the request will be processed by the target core.
1413 *
1414 * For more information about SRP_TSK_MGMT information units, see also section
1415 * 6.7 in the SRP r16a document.
1416 */
1417static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1418 struct srpt_recv_ioctx *recv_ioctx,
1419 struct srpt_send_ioctx *send_ioctx)
1420{
1421 struct srp_tsk_mgmt *srp_tsk;
1422 struct se_cmd *cmd;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001423 struct se_session *sess = ch->sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001424 int tcm_tmr;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001425 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001426
1427 BUG_ON(!send_ioctx);
1428
1429 srp_tsk = recv_ioctx->ioctx.buf;
1430 cmd = &send_ioctx->cmd;
1431
1432 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1433 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1434 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1435
1436 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
Bart Van Assche649ee052015-04-14 13:26:44 +02001437 send_ioctx->cmd.tag = srp_tsk->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001438 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
Bart Van Asschee1dd4132016-02-11 11:05:19 -08001439 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL,
1440 scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr,
1441 GFP_KERNEL, srp_tsk->task_tag,
1442 TARGET_SCF_ACK_KREF);
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001443 if (rc != 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001444 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001445 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001446 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001447 return;
1448fail:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001449 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001450}
1451
1452/**
1453 * srpt_handle_new_iu() - Process a newly received information unit.
1454 * @ch: RDMA channel through which the information unit has been received.
1455 * @ioctx: SRPT I/O context associated with the information unit.
1456 */
1457static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1458 struct srpt_recv_ioctx *recv_ioctx,
1459 struct srpt_send_ioctx *send_ioctx)
1460{
1461 struct srp_cmd *srp_cmd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001462
1463 BUG_ON(!ch);
1464 BUG_ON(!recv_ioctx);
1465
1466 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1467 recv_ioctx->ioctx.dma, srp_max_req_size,
1468 DMA_FROM_DEVICE);
1469
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001470 if (unlikely(ch->state == CH_CONNECTING))
1471 goto out_wait;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001472
Bart Van Assche33912d72016-02-11 11:04:43 -08001473 if (unlikely(ch->state != CH_LIVE))
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001474 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001475
1476 srp_cmd = recv_ioctx->ioctx.buf;
1477 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001478 if (!send_ioctx) {
1479 if (!list_empty(&ch->cmd_wait_list))
1480 goto out_wait;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001481 send_ioctx = srpt_get_send_ioctx(ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001482 }
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001483 if (unlikely(!send_ioctx))
1484 goto out_wait;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001485 }
1486
Bart Van Asschea42d9852011-10-14 01:30:46 +00001487 switch (srp_cmd->opcode) {
1488 case SRP_CMD:
1489 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1490 break;
1491 case SRP_TSK_MGMT:
1492 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1493 break;
1494 case SRP_I_LOGOUT:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001495 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001496 break;
1497 case SRP_CRED_RSP:
1498 pr_debug("received SRP_CRED_RSP\n");
1499 break;
1500 case SRP_AER_RSP:
1501 pr_debug("received SRP_AER_RSP\n");
1502 break;
1503 case SRP_RSP:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001504 pr_err("Received SRP_RSP\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001505 break;
1506 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001507 pr_err("received IU with unknown opcode 0x%x\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001508 srp_cmd->opcode);
1509 break;
1510 }
1511
1512 srpt_post_recv(ch->sport->sdev, recv_ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001513 return;
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001514
1515out_wait:
1516 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001517}
1518
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001519static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001520{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001521 struct srpt_rdma_ch *ch = cq->cq_context;
1522 struct srpt_recv_ioctx *ioctx =
1523 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001524
Bart Van Asschea42d9852011-10-14 01:30:46 +00001525 if (wc->status == IB_WC_SUCCESS) {
1526 int req_lim;
1527
1528 req_lim = atomic_dec_return(&ch->req_lim);
1529 if (unlikely(req_lim < 0))
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001530 pr_err("req_lim = %d < 0\n", req_lim);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001531 srpt_handle_new_iu(ch, ioctx, NULL);
1532 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001533 pr_info("receiving failed for ioctx %p with status %d\n",
1534 ioctx, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001535 }
1536}
1537
Bart Van Assche539b3242016-02-11 11:09:50 -08001538/*
1539 * This function must be called from the context in which RDMA completions are
1540 * processed because it accesses the wait list without protection against
1541 * access from other threads.
1542 */
1543static void srpt_process_wait_list(struct srpt_rdma_ch *ch)
1544{
1545 struct srpt_send_ioctx *ioctx;
1546
1547 while (!list_empty(&ch->cmd_wait_list) &&
1548 ch->state >= CH_LIVE &&
1549 (ioctx = srpt_get_send_ioctx(ch)) != NULL) {
1550 struct srpt_recv_ioctx *recv_ioctx;
1551
1552 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
1553 struct srpt_recv_ioctx,
1554 wait_list);
1555 list_del(&recv_ioctx->wait_list);
1556 srpt_handle_new_iu(ch, recv_ioctx, ioctx);
1557 }
1558}
1559
Bart Van Asschea42d9852011-10-14 01:30:46 +00001560/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001561 * Note: Although this has not yet been observed during tests, at least in
1562 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1563 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1564 * value in each response is set to one, and it is possible that this response
1565 * makes the initiator send a new request before the send completion for that
1566 * response has been processed. This could e.g. happen if the call to
1567 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1568 * if IB retransmission causes generation of the send completion to be
1569 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1570 * are queued on cmd_wait_list. The code below processes these delayed
1571 * requests one at a time.
1572 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001573static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001574{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001575 struct srpt_rdma_ch *ch = cq->cq_context;
1576 struct srpt_send_ioctx *ioctx =
1577 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1578 enum srpt_command_state state;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001579
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001580 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1581
1582 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1583 state != SRPT_STATE_MGMT_RSP_SENT);
1584
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001585 atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001586
Bart Van Assche49f40162016-02-11 11:07:11 -08001587 if (wc->status != IB_WC_SUCCESS)
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001588 pr_info("sending response for ioctx 0x%p failed"
1589 " with status %d\n", ioctx, wc->status);
1590
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001591 if (state != SRPT_STATE_DONE) {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001592 transport_generic_free_cmd(&ioctx->cmd, 0);
1593 } else {
1594 pr_err("IB completion has been received too late for"
1595 " wr_id = %u.\n", ioctx->ioctx.index);
1596 }
1597
Bart Van Assche539b3242016-02-11 11:09:50 -08001598 srpt_process_wait_list(ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001599}
1600
Bart Van Asschea42d9852011-10-14 01:30:46 +00001601/**
1602 * srpt_create_ch_ib() - Create receive and send completion queues.
1603 */
1604static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1605{
1606 struct ib_qp_init_attr *qp_init;
1607 struct srpt_port *sport = ch->sport;
1608 struct srpt_device *sdev = sport->sdev;
Bart Van Assche30c6d872016-07-21 13:03:47 -07001609 const struct ib_device_attr *attrs = &sdev->device->attrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001610 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
1611 int ret;
1612
1613 WARN_ON(ch->rq_size < 1);
1614
1615 ret = -ENOMEM;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08001616 qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001617 if (!qp_init)
1618 goto out;
1619
Bart Van Asscheab477c12014-10-19 18:05:33 +03001620retry:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001621 ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size,
1622 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001623 if (IS_ERR(ch->cq)) {
1624 ret = PTR_ERR(ch->cq);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001625 pr_err("failed to create CQ cqe= %d ret= %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001626 ch->rq_size + srp_sq_size, ret);
1627 goto out;
1628 }
1629
1630 qp_init->qp_context = (void *)ch;
1631 qp_init->event_handler
1632 = (void(*)(struct ib_event *, void*))srpt_qp_event;
1633 qp_init->send_cq = ch->cq;
1634 qp_init->recv_cq = ch->cq;
1635 qp_init->srq = sdev->srq;
1636 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1637 qp_init->qp_type = IB_QPT_RC;
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001638 /*
1639 * We divide up our send queue size into half SEND WRs to send the
1640 * completions, and half R/W contexts to actually do the RDMA
1641 * READ/WRITE transfers. Note that we need to allocate CQ slots for
1642 * both both, as RDMA contexts will also post completions for the
1643 * RDMA READ case.
1644 */
1645 qp_init->cap.max_send_wr = srp_sq_size / 2;
1646 qp_init->cap.max_rdma_ctxs = srp_sq_size / 2;
Bart Van Assche30c6d872016-07-21 13:03:47 -07001647 qp_init->cap.max_send_sge = min(attrs->max_sge, SRPT_MAX_SG_PER_WQE);
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02001648 qp_init->port_num = ch->sport->port;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001649
1650 ch->qp = ib_create_qp(sdev->pd, qp_init);
1651 if (IS_ERR(ch->qp)) {
1652 ret = PTR_ERR(ch->qp);
Bart Van Asscheab477c12014-10-19 18:05:33 +03001653 if (ret == -ENOMEM) {
1654 srp_sq_size /= 2;
1655 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
1656 ib_destroy_cq(ch->cq);
1657 goto retry;
1658 }
1659 }
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001660 pr_err("failed to create_qp ret= %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001661 goto err_destroy_cq;
1662 }
1663
1664 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1665
1666 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
1667 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1668 qp_init->cap.max_send_wr, ch->cm_id);
1669
1670 ret = srpt_init_ch_qp(ch, ch->qp);
1671 if (ret)
1672 goto err_destroy_qp;
1673
Bart Van Asschea42d9852011-10-14 01:30:46 +00001674out:
1675 kfree(qp_init);
1676 return ret;
1677
1678err_destroy_qp:
1679 ib_destroy_qp(ch->qp);
1680err_destroy_cq:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001681 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001682 goto out;
1683}
1684
1685static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
1686{
Bart Van Asschea42d9852011-10-14 01:30:46 +00001687 ib_destroy_qp(ch->qp);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001688 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001689}
1690
1691/**
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001692 * srpt_close_ch() - Close an RDMA channel.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001693 *
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001694 * Make sure all resources associated with the channel will be deallocated at
1695 * an appropriate time.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001696 *
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001697 * Returns true if and only if the channel state has been modified into
1698 * CH_DRAINING.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001699 */
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001700static bool srpt_close_ch(struct srpt_rdma_ch *ch)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001701{
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001702 int ret;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001703
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001704 if (!srpt_set_ch_state(ch, CH_DRAINING)) {
1705 pr_debug("%s-%d: already closed\n", ch->sess_name,
1706 ch->qp->qp_num);
1707 return false;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001708 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001709
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001710 kref_get(&ch->kref);
1711
1712 ret = srpt_ch_qp_err(ch);
1713 if (ret < 0)
1714 pr_err("%s-%d: changing queue pair into error state failed: %d\n",
1715 ch->sess_name, ch->qp->qp_num, ret);
1716
1717 pr_debug("%s-%d: queued zerolength write\n", ch->sess_name,
1718 ch->qp->qp_num);
1719 ret = srpt_zerolength_write(ch);
1720 if (ret < 0) {
1721 pr_err("%s-%d: queuing zero-length write failed: %d\n",
1722 ch->sess_name, ch->qp->qp_num, ret);
1723 if (srpt_set_ch_state(ch, CH_DISCONNECTED))
1724 schedule_work(&ch->release_work);
1725 else
1726 WARN_ON_ONCE(true);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001727 }
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001728
1729 kref_put(&ch->kref, srpt_free_ch);
1730
1731 return true;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001732}
1733
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001734/*
1735 * Change the channel state into CH_DISCONNECTING. If a channel has not yet
1736 * reached the connected state, close it. If a channel is in the connected
1737 * state, send a DREQ. If a DREQ has been received, send a DREP. Note: it is
1738 * the responsibility of the caller to ensure that this function is not
1739 * invoked concurrently with the code that accepts a connection. This means
1740 * that this function must either be invoked from inside a CM callback
1741 * function or that it must be invoked with the srpt_port.mutex held.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001742 */
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001743static int srpt_disconnect_ch(struct srpt_rdma_ch *ch)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001744{
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001745 int ret;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001746
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001747 if (!srpt_set_ch_state(ch, CH_DISCONNECTING))
1748 return -ENOTCONN;
1749
1750 ret = ib_send_cm_dreq(ch->cm_id, NULL, 0);
1751 if (ret < 0)
1752 ret = ib_send_cm_drep(ch->cm_id, NULL, 0);
1753
1754 if (ret < 0 && srpt_close_ch(ch))
1755 ret = 0;
1756
1757 return ret;
1758}
1759
1760static void __srpt_close_all_ch(struct srpt_device *sdev)
1761{
1762 struct srpt_rdma_ch *ch;
1763
1764 lockdep_assert_held(&sdev->mutex);
1765
1766 list_for_each_entry(ch, &sdev->rch_list, list) {
1767 if (srpt_disconnect_ch(ch) >= 0)
1768 pr_info("Closing channel %s-%d because target %s has been disabled\n",
1769 ch->sess_name, ch->qp->qp_num,
1770 sdev->device->name);
1771 srpt_close_ch(ch);
1772 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001773}
1774
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001775static void srpt_free_ch(struct kref *kref)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001776{
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001777 struct srpt_rdma_ch *ch = container_of(kref, struct srpt_rdma_ch, kref);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001778
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001779 kfree(ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001780}
1781
1782static void srpt_release_channel_work(struct work_struct *w)
1783{
1784 struct srpt_rdma_ch *ch;
1785 struct srpt_device *sdev;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001786 struct se_session *se_sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001787
1788 ch = container_of(w, struct srpt_rdma_ch, release_work);
Bart Van Asschef108f0f2016-02-11 11:06:14 -08001789 pr_debug("%s: %s-%d; release_done = %p\n", __func__, ch->sess_name,
1790 ch->qp->qp_num, ch->release_done);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001791
1792 sdev = ch->sport->sdev;
1793 BUG_ON(!sdev);
1794
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001795 se_sess = ch->sess;
1796 BUG_ON(!se_sess);
1797
Bart Van Assche88936252016-02-11 11:05:58 -08001798 target_sess_cmd_list_set_waiting(se_sess);
Joern Engelbe646c2d2013-05-15 00:44:07 -07001799 target_wait_for_sess_cmds(se_sess);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001800
1801 transport_deregister_session_configfs(se_sess);
1802 transport_deregister_session(se_sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001803 ch->sess = NULL;
1804
Nicholas Bellinger0b41d6c2013-09-18 12:48:27 -07001805 ib_destroy_cm_id(ch->cm_id);
1806
Bart Van Asschea42d9852011-10-14 01:30:46 +00001807 srpt_destroy_ch_ib(ch);
1808
1809 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
1810 ch->sport->sdev, ch->rq_size,
1811 ch->rsp_size, DMA_TO_DEVICE);
1812
Bart Van Assche86289912016-02-11 11:08:34 -08001813 mutex_lock(&sdev->mutex);
Bart Van Asschef108f0f2016-02-11 11:06:14 -08001814 list_del_init(&ch->list);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001815 if (ch->release_done)
1816 complete(ch->release_done);
Bart Van Assche86289912016-02-11 11:08:34 -08001817 mutex_unlock(&sdev->mutex);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001818
1819 wake_up(&sdev->ch_releaseQ);
1820
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001821 kref_put(&ch->kref, srpt_free_ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001822}
1823
Bart Van Asschea42d9852011-10-14 01:30:46 +00001824/**
1825 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
1826 *
1827 * Ownership of the cm_id is transferred to the target session if this
1828 * functions returns zero. Otherwise the caller remains the owner of cm_id.
1829 */
1830static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
1831 struct ib_cm_req_event_param *param,
1832 void *private_data)
1833{
1834 struct srpt_device *sdev = cm_id->context;
1835 struct srpt_port *sport = &sdev->port[param->port - 1];
1836 struct srp_login_req *req;
1837 struct srp_login_rsp *rsp;
1838 struct srp_login_rej *rej;
1839 struct ib_cm_rep_param *rep_param;
1840 struct srpt_rdma_ch *ch, *tmp_ch;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001841 u32 it_iu_len;
Bart Van Assche3c968882016-04-07 15:55:04 -07001842 int i, ret = 0;
Nicholas Bellingerf246c942016-01-07 22:19:21 -08001843 unsigned char *p;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001844
1845 WARN_ON_ONCE(irqs_disabled());
1846
1847 if (WARN_ON(!sdev || !private_data))
1848 return -EINVAL;
1849
1850 req = (struct srp_login_req *)private_data;
1851
1852 it_iu_len = be32_to_cpu(req->req_it_iu_len);
1853
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001854 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
1855 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
1856 " (guid=0x%llx:0x%llx)\n",
1857 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
1858 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
1859 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
1860 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
1861 it_iu_len,
1862 param->port,
1863 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
1864 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001865
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08001866 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
1867 rej = kzalloc(sizeof(*rej), GFP_KERNEL);
1868 rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001869
1870 if (!rsp || !rej || !rep_param) {
1871 ret = -ENOMEM;
1872 goto out;
1873 }
1874
1875 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301876 rej->reason = cpu_to_be32(
1877 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001878 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001879 pr_err("rejected SRP_LOGIN_REQ because its"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001880 " length (%d bytes) is out of range (%d .. %d)\n",
1881 it_iu_len, 64, srp_max_req_size);
1882 goto reject;
1883 }
1884
1885 if (!sport->enabled) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301886 rej->reason = cpu_to_be32(
1887 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001888 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001889 pr_err("rejected SRP_LOGIN_REQ because the target port"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001890 " has not yet been enabled\n");
1891 goto reject;
1892 }
1893
1894 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
1895 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
1896
Bart Van Assche86289912016-02-11 11:08:34 -08001897 mutex_lock(&sdev->mutex);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001898
1899 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
1900 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
1901 && !memcmp(ch->t_port_id, req->target_port_id, 16)
1902 && param->port == ch->sport->port
1903 && param->listen_id == ch->sport->sdev->cm_id
1904 && ch->cm_id) {
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001905 if (srpt_disconnect_ch(ch) < 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001906 continue;
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001907 pr_info("Relogin - closed existing channel %s\n",
1908 ch->sess_name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001909 rsp->rsp_flags =
1910 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
1911 }
1912 }
1913
Bart Van Assche86289912016-02-11 11:08:34 -08001914 mutex_unlock(&sdev->mutex);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001915
1916 } else
1917 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
1918
1919 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
1920 || *(__be64 *)(req->target_port_id + 8) !=
1921 cpu_to_be64(srpt_service_guid)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301922 rej->reason = cpu_to_be32(
1923 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001924 ret = -ENOMEM;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001925 pr_err("rejected SRP_LOGIN_REQ because it"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001926 " has an invalid target port identifier.\n");
1927 goto reject;
1928 }
1929
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08001930 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001931 if (!ch) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301932 rej->reason = cpu_to_be32(
1933 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001934 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001935 ret = -ENOMEM;
1936 goto reject;
1937 }
1938
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08001939 kref_init(&ch->kref);
1940 ch->zw_cqe.done = srpt_zerolength_write_done;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001941 INIT_WORK(&ch->release_work, srpt_release_channel_work);
1942 memcpy(ch->i_port_id, req->initiator_port_id, 16);
1943 memcpy(ch->t_port_id, req->target_port_id, 16);
1944 ch->sport = &sdev->port[param->port - 1];
1945 ch->cm_id = cm_id;
Bart Van Assche2739b592016-02-11 11:07:49 -08001946 cm_id->context = ch;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001947 /*
1948 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
1949 * for the SRP protocol to the command queue size.
1950 */
1951 ch->rq_size = SRPT_RQ_SIZE;
1952 spin_lock_init(&ch->spinlock);
1953 ch->state = CH_CONNECTING;
1954 INIT_LIST_HEAD(&ch->cmd_wait_list);
1955 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
1956
1957 ch->ioctx_ring = (struct srpt_send_ioctx **)
1958 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
1959 sizeof(*ch->ioctx_ring[0]),
1960 ch->rsp_size, DMA_TO_DEVICE);
1961 if (!ch->ioctx_ring)
1962 goto free_ch;
1963
Bart Van Assche3c968882016-04-07 15:55:04 -07001964 INIT_LIST_HEAD(&ch->free_list);
1965 for (i = 0; i < ch->rq_size; i++) {
1966 ch->ioctx_ring[i]->ch = ch;
1967 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
1968 }
1969
Bart Van Asschea42d9852011-10-14 01:30:46 +00001970 ret = srpt_create_ch_ib(ch);
1971 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301972 rej->reason = cpu_to_be32(
1973 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001974 pr_err("rejected SRP_LOGIN_REQ because creating"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001975 " a new RDMA channel failed.\n");
1976 goto free_ring;
1977 }
1978
1979 ret = srpt_ch_qp_rtr(ch, ch->qp);
1980 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301981 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001982 pr_err("rejected SRP_LOGIN_REQ because enabling"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001983 " RTR failed (error code = %d)\n", ret);
1984 goto destroy_ib;
1985 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08001986
Bart Van Asschea42d9852011-10-14 01:30:46 +00001987 /*
Nicholas Bellingerf246c942016-01-07 22:19:21 -08001988 * Use the initator port identifier as the session name, when
1989 * checking against se_node_acl->initiatorname[] this can be
1990 * with or without preceeding '0x'.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001991 */
1992 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
1993 be64_to_cpu(*(__be64 *)ch->i_port_id),
1994 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
1995
1996 pr_debug("registering session %s\n", ch->sess_name);
Nicholas Bellingerf246c942016-01-07 22:19:21 -08001997 p = &ch->sess_name[0];
Bart Van Asschea42d9852011-10-14 01:30:46 +00001998
Nicholas Bellingerf246c942016-01-07 22:19:21 -08001999try_again:
Bart Van Assche3c968882016-04-07 15:55:04 -07002000 ch->sess = target_alloc_session(&sport->port_tpg_1, 0, 0,
Nicholas Bellingerb42057a2016-01-09 20:42:43 -08002001 TARGET_PROT_NORMAL, p, ch, NULL);
2002 if (IS_ERR(ch->sess)) {
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002003 pr_info("Rejected login because no ACL has been"
Nicholas Bellingerb42057a2016-01-09 20:42:43 -08002004 " configured yet for initiator %s.\n", p);
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002005 /*
2006 * XXX: Hack to retry of ch->i_port_id without leading '0x'
2007 */
2008 if (p == &ch->sess_name[0]) {
2009 p += 2;
2010 goto try_again;
2011 }
Nicholas Bellingerb42057a2016-01-09 20:42:43 -08002012 rej->reason = cpu_to_be32((PTR_ERR(ch->sess) == -ENOMEM) ?
2013 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES :
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002014 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002015 goto destroy_ib;
2016 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00002017
2018 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2019 ch->sess_name, ch->cm_id);
2020
2021 /* create srp_login_response */
2022 rsp->opcode = SRP_LOGIN_RSP;
2023 rsp->tag = req->tag;
2024 rsp->max_it_iu_len = req->req_it_iu_len;
2025 rsp->max_ti_iu_len = req->req_it_iu_len;
2026 ch->max_ti_iu_len = it_iu_len;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302027 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2028 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002029 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2030 atomic_set(&ch->req_lim, ch->rq_size);
2031 atomic_set(&ch->req_lim_delta, 0);
2032
2033 /* create cm reply */
2034 rep_param->qp_num = ch->qp->qp_num;
2035 rep_param->private_data = (void *)rsp;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08002036 rep_param->private_data_len = sizeof(*rsp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002037 rep_param->rnr_retry_count = 7;
2038 rep_param->flow_control = 1;
2039 rep_param->failover_accepted = 0;
2040 rep_param->srq = 1;
2041 rep_param->responder_resources = 4;
2042 rep_param->initiator_depth = 4;
2043
2044 ret = ib_send_cm_rep(cm_id, rep_param);
2045 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002046 pr_err("sending SRP_LOGIN_REQ response failed"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002047 " (error code = %d)\n", ret);
2048 goto release_channel;
2049 }
2050
Bart Van Assche86289912016-02-11 11:08:34 -08002051 mutex_lock(&sdev->mutex);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002052 list_add_tail(&ch->list, &sdev->rch_list);
Bart Van Assche86289912016-02-11 11:08:34 -08002053 mutex_unlock(&sdev->mutex);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002054
2055 goto out;
2056
2057release_channel:
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08002058 srpt_disconnect_ch(ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002059 transport_deregister_session_configfs(ch->sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002060 transport_deregister_session(ch->sess);
2061 ch->sess = NULL;
2062
2063destroy_ib:
2064 srpt_destroy_ch_ib(ch);
2065
2066free_ring:
2067 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2068 ch->sport->sdev, ch->rq_size,
2069 ch->rsp_size, DMA_TO_DEVICE);
2070free_ch:
2071 kfree(ch);
2072
2073reject:
2074 rej->opcode = SRP_LOGIN_REJ;
2075 rej->tag = req->tag;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302076 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2077 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002078
2079 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08002080 (void *)rej, sizeof(*rej));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002081
2082out:
2083 kfree(rep_param);
2084 kfree(rsp);
2085 kfree(rej);
2086
2087 return ret;
2088}
2089
Bart Van Assche2739b592016-02-11 11:07:49 -08002090static void srpt_cm_rej_recv(struct srpt_rdma_ch *ch,
2091 enum ib_cm_rej_reason reason,
2092 const u8 *private_data,
2093 u8 private_data_len)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002094{
Bart Van Asschec13c90e2016-02-11 11:08:12 -08002095 char *priv = NULL;
2096 int i;
2097
2098 if (private_data_len && (priv = kmalloc(private_data_len * 3 + 1,
2099 GFP_KERNEL))) {
2100 for (i = 0; i < private_data_len; i++)
2101 sprintf(priv + 3 * i, " %02x", private_data[i]);
2102 }
2103 pr_info("Received CM REJ for ch %s-%d; reason %d%s%s.\n",
2104 ch->sess_name, ch->qp->qp_num, reason, private_data_len ?
2105 "; private data" : "", priv ? priv : " (?)");
2106 kfree(priv);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002107}
2108
2109/**
2110 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2111 *
2112 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2113 * and that the recipient may begin transmitting (RTU = ready to use).
2114 */
Bart Van Assche2739b592016-02-11 11:07:49 -08002115static void srpt_cm_rtu_recv(struct srpt_rdma_ch *ch)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002116{
Bart Van Asschea42d9852011-10-14 01:30:46 +00002117 int ret;
2118
Bart Van Asschef130c222016-02-11 11:05:38 -08002119 if (srpt_set_ch_state(ch, CH_LIVE)) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00002120 ret = srpt_ch_qp_rts(ch, ch->qp);
2121
Bart Van Assche387add42016-02-11 11:10:09 -08002122 if (ret == 0) {
2123 /* Trigger wait list processing. */
2124 ret = srpt_zerolength_write(ch);
2125 WARN_ONCE(ret < 0, "%d\n", ret);
2126 } else {
Bart Van Asschea42d9852011-10-14 01:30:46 +00002127 srpt_close_ch(ch);
Bart Van Assche387add42016-02-11 11:10:09 -08002128 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00002129 }
2130}
2131
Bart Van Asschea42d9852011-10-14 01:30:46 +00002132/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00002133 * srpt_cm_handler() - IB connection manager callback function.
2134 *
2135 * A non-zero return value will cause the caller destroy the CM ID.
2136 *
2137 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2138 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2139 * a non-zero value in any other case will trigger a race with the
2140 * ib_destroy_cm_id() call in srpt_release_channel().
2141 */
2142static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2143{
Bart Van Assche2739b592016-02-11 11:07:49 -08002144 struct srpt_rdma_ch *ch = cm_id->context;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002145 int ret;
2146
2147 ret = 0;
2148 switch (event->event) {
2149 case IB_CM_REQ_RECEIVED:
2150 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2151 event->private_data);
2152 break;
2153 case IB_CM_REJ_RECEIVED:
Bart Van Assche2739b592016-02-11 11:07:49 -08002154 srpt_cm_rej_recv(ch, event->param.rej_rcvd.reason,
2155 event->private_data,
2156 IB_CM_REJ_PRIVATE_DATA_SIZE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002157 break;
2158 case IB_CM_RTU_RECEIVED:
2159 case IB_CM_USER_ESTABLISHED:
Bart Van Assche2739b592016-02-11 11:07:49 -08002160 srpt_cm_rtu_recv(ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002161 break;
2162 case IB_CM_DREQ_RECEIVED:
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08002163 srpt_disconnect_ch(ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002164 break;
2165 case IB_CM_DREP_RECEIVED:
Bart Van Assche2739b592016-02-11 11:07:49 -08002166 pr_info("Received CM DREP message for ch %s-%d.\n",
2167 ch->sess_name, ch->qp->qp_num);
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08002168 srpt_close_ch(ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002169 break;
2170 case IB_CM_TIMEWAIT_EXIT:
Bart Van Assche2739b592016-02-11 11:07:49 -08002171 pr_info("Received CM TimeWait exit for ch %s-%d.\n",
2172 ch->sess_name, ch->qp->qp_num);
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08002173 srpt_close_ch(ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002174 break;
2175 case IB_CM_REP_ERROR:
Bart Van Assche2739b592016-02-11 11:07:49 -08002176 pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
2177 ch->qp->qp_num);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002178 break;
2179 case IB_CM_DREQ_ERROR:
Bart Van Assche1e20a2a2016-02-11 11:07:29 -08002180 pr_info("Received CM DREQ ERROR event.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002181 break;
2182 case IB_CM_MRA_RECEIVED:
Bart Van Assche1e20a2a2016-02-11 11:07:29 -08002183 pr_info("Received CM MRA event\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002184 break;
2185 default:
Bart Van Assche1e20a2a2016-02-11 11:07:29 -08002186 pr_err("received unrecognized CM event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002187 break;
2188 }
2189
2190 return ret;
2191}
2192
Bart Van Asschea42d9852011-10-14 01:30:46 +00002193static int srpt_write_pending_status(struct se_cmd *se_cmd)
2194{
2195 struct srpt_send_ioctx *ioctx;
2196
2197 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2198 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2199}
2200
2201/*
2202 * srpt_write_pending() - Start data transfer from initiator to target (write).
2203 */
2204static int srpt_write_pending(struct se_cmd *se_cmd)
2205{
Bart Van Asschefc3af582016-02-11 11:09:10 -08002206 struct srpt_send_ioctx *ioctx =
2207 container_of(se_cmd, struct srpt_send_ioctx, cmd);
2208 struct srpt_rdma_ch *ch = ioctx->ch;
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002209 struct ib_send_wr *first_wr = NULL, *bad_wr;
2210 struct ib_cqe *cqe = &ioctx->rdma_cqe;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002211 enum srpt_command_state new_state;
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002212 int ret, i;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002213
2214 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2215 WARN_ON(new_state == SRPT_STATE_DONE);
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002216
2217 if (atomic_sub_return(ioctx->n_rdma, &ch->sq_wr_avail) < 0) {
2218 pr_warn("%s: IB send queue full (needed %d)\n",
2219 __func__, ioctx->n_rdma);
2220 ret = -ENOMEM;
2221 goto out_undo;
2222 }
2223
2224 cqe->done = srpt_rdma_read_done;
2225 for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
2226 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
2227
2228 first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, ch->sport->port,
2229 cqe, first_wr);
2230 cqe = NULL;
2231 }
2232
2233 ret = ib_post_send(ch->qp, first_wr, &bad_wr);
2234 if (ret) {
2235 pr_err("%s: ib_post_send() returned %d for %d (avail: %d)\n",
2236 __func__, ret, ioctx->n_rdma,
2237 atomic_read(&ch->sq_wr_avail));
2238 goto out_undo;
2239 }
2240
2241 return 0;
2242out_undo:
2243 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
2244 return ret;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002245}
2246
2247static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2248{
2249 switch (tcm_mgmt_status) {
2250 case TMR_FUNCTION_COMPLETE:
2251 return SRP_TSK_MGMT_SUCCESS;
2252 case TMR_FUNCTION_REJECTED:
2253 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2254 }
2255 return SRP_TSK_MGMT_FAILED;
2256}
2257
2258/**
2259 * srpt_queue_response() - Transmits the response to a SCSI command.
2260 *
2261 * Callback function called by the TCM core. Must not block since it can be
2262 * invoked on the context of the IB completion handler.
2263 */
Joern Engelb79fafa2013-07-03 11:22:17 -04002264static void srpt_queue_response(struct se_cmd *cmd)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002265{
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002266 struct srpt_send_ioctx *ioctx =
2267 container_of(cmd, struct srpt_send_ioctx, cmd);
2268 struct srpt_rdma_ch *ch = ioctx->ch;
2269 struct srpt_device *sdev = ch->sport->sdev;
Bart Van Assche10fce582016-07-21 13:04:06 -07002270 struct ib_send_wr send_wr, *first_wr = &send_wr, *bad_wr;
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002271 struct ib_sge sge;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002272 enum srpt_command_state state;
2273 unsigned long flags;
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002274 int resp_len, ret, i;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002275 u8 srp_tm_status;
2276
Bart Van Asschea42d9852011-10-14 01:30:46 +00002277 BUG_ON(!ch);
2278
2279 spin_lock_irqsave(&ioctx->spinlock, flags);
2280 state = ioctx->state;
2281 switch (state) {
2282 case SRPT_STATE_NEW:
2283 case SRPT_STATE_DATA_IN:
2284 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2285 break;
2286 case SRPT_STATE_MGMT:
2287 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2288 break;
2289 default:
2290 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2291 ch, ioctx->ioctx.index, ioctx->state);
2292 break;
2293 }
2294 spin_unlock_irqrestore(&ioctx->spinlock, flags);
2295
2296 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
2297 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
2298 atomic_inc(&ch->req_lim_delta);
2299 srpt_abort_cmd(ioctx);
Joern Engelb79fafa2013-07-03 11:22:17 -04002300 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002301 }
2302
Bart Van Asschea42d9852011-10-14 01:30:46 +00002303 /* For read commands, transfer the data to the initiator. */
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002304 if (ioctx->cmd.data_direction == DMA_FROM_DEVICE &&
2305 ioctx->cmd.data_length &&
Bart Van Asschea42d9852011-10-14 01:30:46 +00002306 !ioctx->queue_status_only) {
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002307 for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
2308 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
2309
2310 first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp,
Bart Van Assche10fce582016-07-21 13:04:06 -07002311 ch->sport->port, NULL, first_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002312 }
2313 }
2314
2315 if (state != SRPT_STATE_MGMT)
Bart Van Assche649ee052015-04-14 13:26:44 +02002316 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002317 cmd->scsi_status);
2318 else {
2319 srp_tm_status
2320 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2321 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
Bart Van Assche649ee052015-04-14 13:26:44 +02002322 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002323 }
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002324
2325 atomic_inc(&ch->req_lim);
2326
2327 if (unlikely(atomic_sub_return(1 + ioctx->n_rdma,
2328 &ch->sq_wr_avail) < 0)) {
2329 pr_warn("%s: IB send queue full (needed %d)\n",
2330 __func__, ioctx->n_rdma);
2331 ret = -ENOMEM;
2332 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002333 }
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002334
2335 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, resp_len,
2336 DMA_TO_DEVICE);
2337
2338 sge.addr = ioctx->ioctx.dma;
2339 sge.length = resp_len;
2340 sge.lkey = sdev->pd->local_dma_lkey;
2341
2342 ioctx->ioctx.cqe.done = srpt_send_done;
2343 send_wr.next = NULL;
2344 send_wr.wr_cqe = &ioctx->ioctx.cqe;
2345 send_wr.sg_list = &sge;
2346 send_wr.num_sge = 1;
2347 send_wr.opcode = IB_WR_SEND;
2348 send_wr.send_flags = IB_SEND_SIGNALED;
2349
2350 ret = ib_post_send(ch->qp, first_wr, &bad_wr);
2351 if (ret < 0) {
2352 pr_err("%s: sending cmd response failed for tag %llu (%d)\n",
2353 __func__, ioctx->cmd.tag, ret);
2354 goto out;
2355 }
2356
2357 return;
2358
2359out:
2360 atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
2361 atomic_dec(&ch->req_lim);
2362 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
2363 target_put_sess_cmd(&ioctx->cmd);
Joern Engelb79fafa2013-07-03 11:22:17 -04002364}
Bart Van Asschea42d9852011-10-14 01:30:46 +00002365
Joern Engelb79fafa2013-07-03 11:22:17 -04002366static int srpt_queue_data_in(struct se_cmd *cmd)
2367{
2368 srpt_queue_response(cmd);
2369 return 0;
2370}
2371
2372static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2373{
2374 srpt_queue_response(cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002375}
2376
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002377static void srpt_aborted_task(struct se_cmd *cmd)
2378{
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002379}
2380
Bart Van Asschea42d9852011-10-14 01:30:46 +00002381static int srpt_queue_status(struct se_cmd *cmd)
2382{
2383 struct srpt_send_ioctx *ioctx;
2384
2385 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2386 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2387 if (cmd->se_cmd_flags &
2388 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2389 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2390 ioctx->queue_status_only = true;
Joern Engelb79fafa2013-07-03 11:22:17 -04002391 srpt_queue_response(cmd);
2392 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002393}
2394
2395static void srpt_refresh_port_work(struct work_struct *work)
2396{
2397 struct srpt_port *sport = container_of(work, struct srpt_port, work);
2398
2399 srpt_refresh_port(sport);
2400}
2401
Bart Van Asschea42d9852011-10-14 01:30:46 +00002402/**
2403 * srpt_release_sdev() - Free the channel resources associated with a target.
2404 */
2405static int srpt_release_sdev(struct srpt_device *sdev)
2406{
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08002407 int i, res;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002408
2409 WARN_ON_ONCE(irqs_disabled());
2410
2411 BUG_ON(!sdev);
2412
Bart Van Assche86289912016-02-11 11:08:34 -08002413 mutex_lock(&sdev->mutex);
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08002414 for (i = 0; i < ARRAY_SIZE(sdev->port); i++)
2415 sdev->port[i].enabled = false;
2416 __srpt_close_all_ch(sdev);
Bart Van Assche86289912016-02-11 11:08:34 -08002417 mutex_unlock(&sdev->mutex);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002418
2419 res = wait_event_interruptible(sdev->ch_releaseQ,
Bart Van Assche86289912016-02-11 11:08:34 -08002420 list_empty_careful(&sdev->rch_list));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002421 if (res)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002422 pr_err("%s: interrupted.\n", __func__);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002423
2424 return 0;
2425}
2426
2427static struct srpt_port *__srpt_lookup_port(const char *name)
2428{
2429 struct ib_device *dev;
2430 struct srpt_device *sdev;
2431 struct srpt_port *sport;
2432 int i;
2433
2434 list_for_each_entry(sdev, &srpt_dev_list, list) {
2435 dev = sdev->device;
2436 if (!dev)
2437 continue;
2438
2439 for (i = 0; i < dev->phys_port_cnt; i++) {
2440 sport = &sdev->port[i];
2441
2442 if (!strcmp(sport->port_guid, name))
2443 return sport;
2444 }
2445 }
2446
2447 return NULL;
2448}
2449
2450static struct srpt_port *srpt_lookup_port(const char *name)
2451{
2452 struct srpt_port *sport;
2453
2454 spin_lock(&srpt_dev_lock);
2455 sport = __srpt_lookup_port(name);
2456 spin_unlock(&srpt_dev_lock);
2457
2458 return sport;
2459}
2460
2461/**
2462 * srpt_add_one() - Infiniband device addition callback function.
2463 */
2464static void srpt_add_one(struct ib_device *device)
2465{
2466 struct srpt_device *sdev;
2467 struct srpt_port *sport;
2468 struct ib_srq_init_attr srq_attr;
2469 int i;
2470
2471 pr_debug("device = %p, device->dma_ops = %p\n", device,
2472 device->dma_ops);
2473
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08002474 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002475 if (!sdev)
2476 goto err;
2477
2478 sdev->device = device;
2479 INIT_LIST_HEAD(&sdev->rch_list);
2480 init_waitqueue_head(&sdev->ch_releaseQ);
Bart Van Assche86289912016-02-11 11:08:34 -08002481 mutex_init(&sdev->mutex);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002482
Bart Van Asschea42d9852011-10-14 01:30:46 +00002483 sdev->pd = ib_alloc_pd(device);
2484 if (IS_ERR(sdev->pd))
2485 goto free_dev;
2486
Or Gerlitz4a061b22015-12-18 10:59:46 +02002487 sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002488
2489 srq_attr.event_handler = srpt_srq_event;
2490 srq_attr.srq_context = (void *)sdev;
2491 srq_attr.attr.max_wr = sdev->srq_size;
2492 srq_attr.attr.max_sge = 1;
2493 srq_attr.attr.srq_limit = 0;
Roland Dreier6f360332012-04-12 07:51:08 -07002494 srq_attr.srq_type = IB_SRQT_BASIC;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002495
2496 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
2497 if (IS_ERR(sdev->srq))
Jason Gunthorpe5a783952015-07-30 17:22:24 -06002498 goto err_pd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002499
2500 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
Or Gerlitz4a061b22015-12-18 10:59:46 +02002501 __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002502 device->name);
2503
2504 if (!srpt_service_guid)
2505 srpt_service_guid = be64_to_cpu(device->node_guid);
2506
2507 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
2508 if (IS_ERR(sdev->cm_id))
2509 goto err_srq;
2510
2511 /* print out target login information */
2512 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
2513 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
2514 srpt_service_guid, srpt_service_guid);
2515
2516 /*
2517 * We do not have a consistent service_id (ie. also id_ext of target_id)
2518 * to identify this target. We currently use the guid of the first HCA
2519 * in the system as service_id; therefore, the target_id will change
2520 * if this HCA is gone bad and replaced by different HCA
2521 */
Haggai Eran73fec7f2015-07-30 17:50:26 +03002522 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
Bart Van Asschea42d9852011-10-14 01:30:46 +00002523 goto err_cm;
2524
2525 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
2526 srpt_event_handler);
2527 if (ib_register_event_handler(&sdev->event_handler))
2528 goto err_cm;
2529
2530 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
2531 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
2532 sizeof(*sdev->ioctx_ring[0]),
2533 srp_max_req_size, DMA_FROM_DEVICE);
2534 if (!sdev->ioctx_ring)
2535 goto err_event;
2536
2537 for (i = 0; i < sdev->srq_size; ++i)
2538 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
2539
Roland Dreierf2250662012-02-02 12:55:58 -08002540 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002541
2542 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
2543 sport = &sdev->port[i - 1];
2544 sport->sdev = sdev;
2545 sport->port = i;
2546 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
2547 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
2548 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
2549 INIT_WORK(&sport->work, srpt_refresh_port_work);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002550
2551 if (srpt_refresh_port(sport)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002552 pr_err("MAD registration failed for %s-%d.\n",
Bart Van Asschef68cba4e92016-02-11 11:04:20 -08002553 sdev->device->name, i);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002554 goto err_ring;
2555 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00002556 }
2557
2558 spin_lock(&srpt_dev_lock);
2559 list_add_tail(&sdev->list, &srpt_dev_list);
2560 spin_unlock(&srpt_dev_lock);
2561
2562out:
2563 ib_set_client_data(device, &srpt_client, sdev);
2564 pr_debug("added %s.\n", device->name);
2565 return;
2566
2567err_ring:
2568 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2569 sdev->srq_size, srp_max_req_size,
2570 DMA_FROM_DEVICE);
2571err_event:
2572 ib_unregister_event_handler(&sdev->event_handler);
2573err_cm:
2574 ib_destroy_cm_id(sdev->cm_id);
2575err_srq:
2576 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002577err_pd:
2578 ib_dealloc_pd(sdev->pd);
2579free_dev:
2580 kfree(sdev);
2581err:
2582 sdev = NULL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002583 pr_info("%s(%s) failed.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002584 goto out;
2585}
2586
2587/**
2588 * srpt_remove_one() - InfiniBand device removal callback function.
2589 */
Haggai Eran7c1eb452015-07-30 17:50:14 +03002590static void srpt_remove_one(struct ib_device *device, void *client_data)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002591{
Haggai Eran7c1eb452015-07-30 17:50:14 +03002592 struct srpt_device *sdev = client_data;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002593 int i;
2594
Bart Van Asschea42d9852011-10-14 01:30:46 +00002595 if (!sdev) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002596 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002597 return;
2598 }
2599
2600 srpt_unregister_mad_agent(sdev);
2601
2602 ib_unregister_event_handler(&sdev->event_handler);
2603
2604 /* Cancel any work queued by the just unregistered IB event handler. */
2605 for (i = 0; i < sdev->device->phys_port_cnt; i++)
2606 cancel_work_sync(&sdev->port[i].work);
2607
2608 ib_destroy_cm_id(sdev->cm_id);
2609
2610 /*
2611 * Unregistering a target must happen after destroying sdev->cm_id
2612 * such that no new SRP_LOGIN_REQ information units can arrive while
2613 * destroying the target.
2614 */
2615 spin_lock(&srpt_dev_lock);
2616 list_del(&sdev->list);
2617 spin_unlock(&srpt_dev_lock);
2618 srpt_release_sdev(sdev);
2619
2620 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002621 ib_dealloc_pd(sdev->pd);
2622
2623 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2624 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
2625 sdev->ioctx_ring = NULL;
2626 kfree(sdev);
2627}
2628
2629static struct ib_client srpt_client = {
2630 .name = DRV_NAME,
2631 .add = srpt_add_one,
2632 .remove = srpt_remove_one
2633};
2634
2635static int srpt_check_true(struct se_portal_group *se_tpg)
2636{
2637 return 1;
2638}
2639
2640static int srpt_check_false(struct se_portal_group *se_tpg)
2641{
2642 return 0;
2643}
2644
2645static char *srpt_get_fabric_name(void)
2646{
2647 return "srpt";
2648}
2649
Bart Van Asschea42d9852011-10-14 01:30:46 +00002650static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
2651{
2652 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
2653
2654 return sport->port_guid;
2655}
2656
2657static u16 srpt_get_tag(struct se_portal_group *tpg)
2658{
2659 return 1;
2660}
2661
Bart Van Asschea42d9852011-10-14 01:30:46 +00002662static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
2663{
2664 return 1;
2665}
2666
2667static void srpt_release_cmd(struct se_cmd *se_cmd)
2668{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002669 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
2670 struct srpt_send_ioctx, cmd);
2671 struct srpt_rdma_ch *ch = ioctx->ch;
Bart Van Assche3c968882016-04-07 15:55:04 -07002672 unsigned long flags;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002673
2674 WARN_ON(ioctx->state != SRPT_STATE_DONE);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002675
Christoph Hellwigb99f8e42016-05-03 18:01:11 +02002676 if (ioctx->n_rw_ctx) {
2677 srpt_free_rw_ctxs(ch, ioctx);
2678 ioctx->n_rw_ctx = 0;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002679 }
2680
Bart Van Assche3c968882016-04-07 15:55:04 -07002681 spin_lock_irqsave(&ch->spinlock, flags);
2682 list_add(&ioctx->free_list, &ch->free_list);
2683 spin_unlock_irqrestore(&ch->spinlock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002684}
2685
2686/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00002687 * srpt_close_session() - Forcibly close a session.
2688 *
2689 * Callback function invoked by the TCM core to clean up sessions associated
2690 * with a node ACL when the user invokes
2691 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
2692 */
2693static void srpt_close_session(struct se_session *se_sess)
2694{
2695 DECLARE_COMPLETION_ONSTACK(release_done);
Bart Van Asschef108f0f2016-02-11 11:06:14 -08002696 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
2697 struct srpt_device *sdev = ch->sport->sdev;
2698 bool wait;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002699
Bart Van Asschef108f0f2016-02-11 11:06:14 -08002700 pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num,
2701 ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002702
Bart Van Assche86289912016-02-11 11:08:34 -08002703 mutex_lock(&sdev->mutex);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002704 BUG_ON(ch->release_done);
2705 ch->release_done = &release_done;
Bart Van Asschef108f0f2016-02-11 11:06:14 -08002706 wait = !list_empty(&ch->list);
Bart Van Asscheaaf45bd2016-02-11 11:08:53 -08002707 srpt_disconnect_ch(ch);
Bart Van Assche86289912016-02-11 11:08:34 -08002708 mutex_unlock(&sdev->mutex);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002709
Bart Van Asschef108f0f2016-02-11 11:06:14 -08002710 if (!wait)
2711 return;
2712
2713 while (wait_for_completion_timeout(&release_done, 180 * HZ) == 0)
2714 pr_info("%s(%s-%d state %d): still waiting ...\n", __func__,
2715 ch->sess_name, ch->qp->qp_num, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002716}
2717
2718/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00002719 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
2720 *
2721 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
2722 * This object represents an arbitrary integer used to uniquely identify a
2723 * particular attached remote initiator port to a particular SCSI target port
2724 * within a particular SCSI target device within a particular SCSI instance.
2725 */
2726static u32 srpt_sess_get_index(struct se_session *se_sess)
2727{
2728 return 0;
2729}
2730
2731static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
2732{
2733}
2734
Bart Van Asschea42d9852011-10-14 01:30:46 +00002735/* Note: only used from inside debug printk's by the TCM core. */
2736static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
2737{
2738 struct srpt_send_ioctx *ioctx;
2739
2740 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2741 return srpt_get_cmd_state(ioctx);
2742}
2743
Bart Van Asschea42d9852011-10-14 01:30:46 +00002744/**
2745 * srpt_parse_i_port_id() - Parse an initiator port ID.
2746 * @name: ASCII representation of a 128-bit initiator port ID.
2747 * @i_port_id: Binary 128-bit port ID.
2748 */
2749static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
2750{
2751 const char *p;
2752 unsigned len, count, leading_zero_bytes;
2753 int ret, rc;
2754
2755 p = name;
Rasmus Villemoesb60459f2014-10-13 15:54:46 -07002756 if (strncasecmp(p, "0x", 2) == 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002757 p += 2;
2758 ret = -EINVAL;
2759 len = strlen(p);
2760 if (len % 2)
2761 goto out;
2762 count = min(len / 2, 16U);
2763 leading_zero_bytes = 16 - count;
2764 memset(i_port_id, 0, leading_zero_bytes);
2765 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
2766 if (rc < 0)
2767 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
2768 ret = 0;
2769out:
2770 return ret;
2771}
2772
2773/*
2774 * configfs callback function invoked for
2775 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
2776 */
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02002777static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002778{
Bart Van Asschea42d9852011-10-14 01:30:46 +00002779 u8 i_port_id[16];
2780
2781 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002782 pr_err("invalid initiator port ID %s\n", name);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02002783 return -EINVAL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002784 }
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02002785 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002786}
2787
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002788static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
2789 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002790{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002791 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002792 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
2793
2794 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
2795}
2796
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002797static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
2798 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002799{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002800 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002801 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
2802 unsigned long val;
2803 int ret;
2804
Jingoo Han9d8abf42014-02-05 11:22:05 +09002805 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002806 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09002807 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002808 return -EINVAL;
2809 }
2810 if (val > MAX_SRPT_RDMA_SIZE) {
2811 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
2812 MAX_SRPT_RDMA_SIZE);
2813 return -EINVAL;
2814 }
2815 if (val < DEFAULT_MAX_RDMA_SIZE) {
2816 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
2817 val, DEFAULT_MAX_RDMA_SIZE);
2818 return -EINVAL;
2819 }
2820 sport->port_attrib.srp_max_rdma_size = val;
2821
2822 return count;
2823}
2824
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002825static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
2826 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002827{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002828 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002829 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
2830
2831 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
2832}
2833
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002834static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
2835 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002836{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002837 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002838 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
2839 unsigned long val;
2840 int ret;
2841
Jingoo Han9d8abf42014-02-05 11:22:05 +09002842 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002843 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09002844 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002845 return -EINVAL;
2846 }
2847 if (val > MAX_SRPT_RSP_SIZE) {
2848 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
2849 MAX_SRPT_RSP_SIZE);
2850 return -EINVAL;
2851 }
2852 if (val < MIN_MAX_RSP_SIZE) {
2853 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
2854 MIN_MAX_RSP_SIZE);
2855 return -EINVAL;
2856 }
2857 sport->port_attrib.srp_max_rsp_size = val;
2858
2859 return count;
2860}
2861
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002862static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
2863 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002864{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002865 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002866 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
2867
2868 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
2869}
2870
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002871static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
2872 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002873{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002874 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002875 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
2876 unsigned long val;
2877 int ret;
2878
Jingoo Han9d8abf42014-02-05 11:22:05 +09002879 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002880 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09002881 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002882 return -EINVAL;
2883 }
2884 if (val > MAX_SRPT_SRQ_SIZE) {
2885 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
2886 MAX_SRPT_SRQ_SIZE);
2887 return -EINVAL;
2888 }
2889 if (val < MIN_SRPT_SRQ_SIZE) {
2890 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
2891 MIN_SRPT_SRQ_SIZE);
2892 return -EINVAL;
2893 }
2894 sport->port_attrib.srp_sq_size = val;
2895
2896 return count;
2897}
2898
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002899CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
2900CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
2901CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002902
2903static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002904 &srpt_tpg_attrib_attr_srp_max_rdma_size,
2905 &srpt_tpg_attrib_attr_srp_max_rsp_size,
2906 &srpt_tpg_attrib_attr_srp_sq_size,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002907 NULL,
2908};
2909
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002910static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002911{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002912 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002913 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
2914
2915 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
2916}
2917
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002918static ssize_t srpt_tpg_enable_store(struct config_item *item,
2919 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002920{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002921 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002922 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
Bart Van Assche043a6802016-02-11 11:09:28 -08002923 struct srpt_device *sdev = sport->sdev;
2924 struct srpt_rdma_ch *ch;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002925 unsigned long tmp;
2926 int ret;
2927
Jingoo Han9d8abf42014-02-05 11:22:05 +09002928 ret = kstrtoul(page, 0, &tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002929 if (ret < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002930 pr_err("Unable to extract srpt_tpg_store_enable\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002931 return -EINVAL;
2932 }
2933
2934 if ((tmp != 0) && (tmp != 1)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002935 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002936 return -EINVAL;
2937 }
Bart Van Assche043a6802016-02-11 11:09:28 -08002938 if (sport->enabled == tmp)
2939 goto out;
2940 sport->enabled = tmp;
2941 if (sport->enabled)
2942 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002943
Bart Van Assche043a6802016-02-11 11:09:28 -08002944 mutex_lock(&sdev->mutex);
2945 list_for_each_entry(ch, &sdev->rch_list, list) {
2946 if (ch->sport == sport) {
2947 pr_debug("%s: ch %p %s-%d\n", __func__, ch,
2948 ch->sess_name, ch->qp->qp_num);
2949 srpt_disconnect_ch(ch);
2950 srpt_close_ch(ch);
2951 }
2952 }
2953 mutex_unlock(&sdev->mutex);
2954
2955out:
Bart Van Asschea42d9852011-10-14 01:30:46 +00002956 return count;
2957}
2958
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002959CONFIGFS_ATTR(srpt_tpg_, enable);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002960
2961static struct configfs_attribute *srpt_tpg_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002962 &srpt_tpg_attr_enable,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002963 NULL,
2964};
2965
2966/**
2967 * configfs callback invoked for
2968 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
2969 */
2970static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
2971 struct config_group *group,
2972 const char *name)
2973{
2974 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
2975 int res;
2976
2977 /* Initialize sport->port_wwn and sport->port_tpg_1 */
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07002978 res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002979 if (res)
2980 return ERR_PTR(res);
2981
2982 return &sport->port_tpg_1;
2983}
2984
2985/**
2986 * configfs callback invoked for
2987 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
2988 */
2989static void srpt_drop_tpg(struct se_portal_group *tpg)
2990{
2991 struct srpt_port *sport = container_of(tpg,
2992 struct srpt_port, port_tpg_1);
2993
2994 sport->enabled = false;
2995 core_tpg_deregister(&sport->port_tpg_1);
2996}
2997
2998/**
2999 * configfs callback invoked for
3000 * mkdir /sys/kernel/config/target/$driver/$port
3001 */
3002static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3003 struct config_group *group,
3004 const char *name)
3005{
3006 struct srpt_port *sport;
3007 int ret;
3008
3009 sport = srpt_lookup_port(name);
3010 pr_debug("make_tport(%s)\n", name);
3011 ret = -EINVAL;
3012 if (!sport)
3013 goto err;
3014
3015 return &sport->port_wwn;
3016
3017err:
3018 return ERR_PTR(ret);
3019}
3020
3021/**
3022 * configfs callback invoked for
3023 * rmdir /sys/kernel/config/target/$driver/$port
3024 */
3025static void srpt_drop_tport(struct se_wwn *wwn)
3026{
3027 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3028
3029 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3030}
3031
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003032static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003033{
3034 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3035}
3036
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003037CONFIGFS_ATTR_RO(srpt_wwn_, version);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003038
3039static struct configfs_attribute *srpt_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003040 &srpt_wwn_attr_version,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003041 NULL,
3042};
3043
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003044static const struct target_core_fabric_ops srpt_template = {
3045 .module = THIS_MODULE,
3046 .name = "srpt",
Bart Van Asschea42d9852011-10-14 01:30:46 +00003047 .get_fabric_name = srpt_get_fabric_name,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003048 .tpg_get_wwn = srpt_get_fabric_wwn,
3049 .tpg_get_tag = srpt_get_tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003050 .tpg_check_demo_mode = srpt_check_false,
3051 .tpg_check_demo_mode_cache = srpt_check_true,
3052 .tpg_check_demo_mode_write_protect = srpt_check_true,
3053 .tpg_check_prod_mode_write_protect = srpt_check_false,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003054 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3055 .release_cmd = srpt_release_cmd,
3056 .check_stop_free = srpt_check_stop_free,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003057 .close_session = srpt_close_session,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003058 .sess_get_index = srpt_sess_get_index,
3059 .sess_get_initiator_sid = NULL,
3060 .write_pending = srpt_write_pending,
3061 .write_pending_status = srpt_write_pending_status,
3062 .set_default_node_attributes = srpt_set_default_node_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003063 .get_cmd_state = srpt_get_tcm_cmd_state,
Joern Engelb79fafa2013-07-03 11:22:17 -04003064 .queue_data_in = srpt_queue_data_in,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003065 .queue_status = srpt_queue_status,
Joern Engelb79fafa2013-07-03 11:22:17 -04003066 .queue_tm_rsp = srpt_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003067 .aborted_task = srpt_aborted_task,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003068 /*
3069 * Setup function pointers for generic logic in
3070 * target_core_fabric_configfs.c
3071 */
3072 .fabric_make_wwn = srpt_make_tport,
3073 .fabric_drop_wwn = srpt_drop_tport,
3074 .fabric_make_tpg = srpt_make_tpg,
3075 .fabric_drop_tpg = srpt_drop_tpg,
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003076 .fabric_init_nodeacl = srpt_init_nodeacl,
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003077
3078 .tfc_wwn_attrs = srpt_wwn_attrs,
3079 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3080 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003081};
3082
3083/**
3084 * srpt_init_module() - Kernel module initialization.
3085 *
3086 * Note: Since ib_register_client() registers callback functions, and since at
3087 * least one of these callback functions (srpt_add_one()) calls target core
3088 * functions, this driver must be registered with the target core before
3089 * ib_register_client() is called.
3090 */
3091static int __init srpt_init_module(void)
3092{
3093 int ret;
3094
3095 ret = -EINVAL;
3096 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003097 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003098 " srp_max_req_size -- must be at least %d.\n",
3099 srp_max_req_size, MIN_MAX_REQ_SIZE);
3100 goto out;
3101 }
3102
3103 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3104 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003105 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003106 " srpt_srq_size -- must be in the range [%d..%d].\n",
3107 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3108 goto out;
3109 }
3110
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003111 ret = target_register_template(&srpt_template);
3112 if (ret)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003113 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003114
3115 ret = ib_register_client(&srpt_client);
3116 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003117 pr_err("couldn't register IB client\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003118 goto out_unregister_target;
3119 }
3120
3121 return 0;
3122
3123out_unregister_target:
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003124 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003125out:
3126 return ret;
3127}
3128
3129static void __exit srpt_cleanup_module(void)
3130{
3131 ib_unregister_client(&srpt_client);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003132 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003133}
3134
3135module_init(srpt_init_module);
3136module_exit(srpt_cleanup_module);