blob: 64e9107f6f4f43a2f51446b50ae176b608e63b94 [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 Asschea42d9852011-10-14 01:30:46 +000095static void srpt_release_channel(struct srpt_rdma_ch *ch);
96static 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 Asschea42d9852011-10-14 01:30:46 +000099
Bart Van Asschef130c222016-02-11 11:05:38 -0800100/*
101 * The only allowed channel state changes are those that change the channel
102 * state into a state with a higher numerical value. Hence the new > prev test.
Bart Van Asschea42d9852011-10-14 01:30:46 +0000103 */
Bart Van Asschef130c222016-02-11 11:05:38 -0800104static bool srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new)
Bart Van Asschea42d9852011-10-14 01:30:46 +0000105{
106 unsigned long flags;
107 enum rdma_ch_state prev;
Bart Van Asschef130c222016-02-11 11:05:38 -0800108 bool changed = false;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000109
110 spin_lock_irqsave(&ch->spinlock, flags);
111 prev = ch->state;
Bart Van Asschef130c222016-02-11 11:05:38 -0800112 if (new > prev) {
Bart Van Asschea42d9852011-10-14 01:30:46 +0000113 ch->state = new;
Bart Van Asschef130c222016-02-11 11:05:38 -0800114 changed = true;
115 }
Bart Van Asschea42d9852011-10-14 01:30:46 +0000116 spin_unlock_irqrestore(&ch->spinlock, flags);
Bart Van Asschef130c222016-02-11 11:05:38 -0800117
118 return changed;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000119}
120
121/**
122 * srpt_event_handler() - Asynchronous IB event callback function.
123 *
124 * Callback function called by the InfiniBand core when an asynchronous IB
125 * event occurs. This callback may occur in interrupt context. See also
126 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
127 * Architecture Specification.
128 */
129static void srpt_event_handler(struct ib_event_handler *handler,
130 struct ib_event *event)
131{
132 struct srpt_device *sdev;
133 struct srpt_port *sport;
134
135 sdev = ib_get_client_data(event->device, &srpt_client);
136 if (!sdev || sdev->device != event->device)
137 return;
138
139 pr_debug("ASYNC event= %d on device= %s\n", event->event,
Bart Van Asschef68cba4e92016-02-11 11:04:20 -0800140 sdev->device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000141
142 switch (event->event) {
143 case IB_EVENT_PORT_ERR:
144 if (event->element.port_num <= sdev->device->phys_port_cnt) {
145 sport = &sdev->port[event->element.port_num - 1];
146 sport->lid = 0;
147 sport->sm_lid = 0;
148 }
149 break;
150 case IB_EVENT_PORT_ACTIVE:
151 case IB_EVENT_LID_CHANGE:
152 case IB_EVENT_PKEY_CHANGE:
153 case IB_EVENT_SM_CHANGE:
154 case IB_EVENT_CLIENT_REREGISTER:
Doug Ledford2aa1cf62014-08-12 19:20:10 -0400155 case IB_EVENT_GID_CHANGE:
Bart Van Asschea42d9852011-10-14 01:30:46 +0000156 /* Refresh port data asynchronously. */
157 if (event->element.port_num <= sdev->device->phys_port_cnt) {
158 sport = &sdev->port[event->element.port_num - 1];
159 if (!sport->lid && !sport->sm_lid)
160 schedule_work(&sport->work);
161 }
162 break;
163 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400164 pr_err("received unrecognized IB event %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +0000165 event->event);
166 break;
167 }
168}
169
170/**
171 * srpt_srq_event() - SRQ event callback function.
172 */
173static void srpt_srq_event(struct ib_event *event, void *ctx)
174{
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400175 pr_info("SRQ event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000176}
177
178/**
179 * srpt_qp_event() - QP event callback function.
180 */
181static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
182{
183 pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
Bart Van Assche33912d72016-02-11 11:04:43 -0800184 event->event, ch->cm_id, ch->sess_name, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000185
186 switch (event->event) {
187 case IB_EVENT_COMM_EST:
188 ib_cm_notify(ch->cm_id, event->event);
189 break;
190 case IB_EVENT_QP_LAST_WQE_REACHED:
Bart Van Asschef130c222016-02-11 11:05:38 -0800191 if (srpt_set_ch_state(ch, CH_RELEASING))
Bart Van Asschea42d9852011-10-14 01:30:46 +0000192 srpt_release_channel(ch);
193 else
194 pr_debug("%s: state %d - ignored LAST_WQE.\n",
Bart Van Assche33912d72016-02-11 11:04:43 -0800195 ch->sess_name, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000196 break;
197 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400198 pr_err("received unrecognized IB QP event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000199 break;
200 }
201}
202
203/**
204 * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
205 *
206 * @slot: one-based slot number.
207 * @value: four-bit value.
208 *
209 * Copies the lowest four bits of value in element slot of the array of four
210 * bit elements called c_list (controller list). The index slot is one-based.
211 */
212static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
213{
214 u16 id;
215 u8 tmp;
216
217 id = (slot - 1) / 2;
218 if (slot & 0x1) {
219 tmp = c_list[id] & 0xf;
220 c_list[id] = (value << 4) | tmp;
221 } else {
222 tmp = c_list[id] & 0xf0;
223 c_list[id] = (value & 0xf) | tmp;
224 }
225}
226
227/**
228 * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
229 *
230 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
231 * Specification.
232 */
233static void srpt_get_class_port_info(struct ib_dm_mad *mad)
234{
235 struct ib_class_port_info *cif;
236
237 cif = (struct ib_class_port_info *)mad->data;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800238 memset(cif, 0, sizeof(*cif));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000239 cif->base_version = 1;
240 cif->class_version = 1;
241 cif->resp_time_value = 20;
242
243 mad->mad_hdr.status = 0;
244}
245
246/**
247 * srpt_get_iou() - Write IOUnitInfo to a management datagram.
248 *
249 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
250 * Specification. See also section B.7, table B.6 in the SRP r16a document.
251 */
252static void srpt_get_iou(struct ib_dm_mad *mad)
253{
254 struct ib_dm_iou_info *ioui;
255 u8 slot;
256 int i;
257
258 ioui = (struct ib_dm_iou_info *)mad->data;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530259 ioui->change_id = cpu_to_be16(1);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000260 ioui->max_controllers = 16;
261
262 /* set present for slot 1 and empty for the rest */
263 srpt_set_ioc(ioui->controller_list, 1, 1);
264 for (i = 1, slot = 2; i < 16; i++, slot++)
265 srpt_set_ioc(ioui->controller_list, slot, 0);
266
267 mad->mad_hdr.status = 0;
268}
269
270/**
271 * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
272 *
273 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
274 * Architecture Specification. See also section B.7, table B.7 in the SRP
275 * r16a document.
276 */
277static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
278 struct ib_dm_mad *mad)
279{
280 struct srpt_device *sdev = sport->sdev;
281 struct ib_dm_ioc_profile *iocp;
282
283 iocp = (struct ib_dm_ioc_profile *)mad->data;
284
285 if (!slot || slot > 16) {
286 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530287 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000288 return;
289 }
290
291 if (slot > 2) {
292 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530293 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000294 return;
295 }
296
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800297 memset(iocp, 0, sizeof(*iocp));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000298 strcpy(iocp->id_string, SRPT_ID_STRING);
299 iocp->guid = cpu_to_be64(srpt_service_guid);
Or Gerlitz4a061b22015-12-18 10:59:46 +0200300 iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
301 iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
302 iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
303 iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000304 iocp->subsys_device_id = 0x0;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530305 iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
306 iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
307 iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
308 iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000309 iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
310 iocp->rdma_read_depth = 4;
311 iocp->send_size = cpu_to_be32(srp_max_req_size);
312 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
313 1U << 24));
314 iocp->num_svc_entries = 1;
315 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
316 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
317
318 mad->mad_hdr.status = 0;
319}
320
321/**
322 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
323 *
324 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
325 * Specification. See also section B.7, table B.8 in the SRP r16a document.
326 */
327static void srpt_get_svc_entries(u64 ioc_guid,
328 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
329{
330 struct ib_dm_svc_entries *svc_entries;
331
332 WARN_ON(!ioc_guid);
333
334 if (!slot || slot > 16) {
335 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530336 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000337 return;
338 }
339
340 if (slot > 2 || lo > hi || hi > 1) {
341 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530342 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000343 return;
344 }
345
346 svc_entries = (struct ib_dm_svc_entries *)mad->data;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800347 memset(svc_entries, 0, sizeof(*svc_entries));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000348 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
349 snprintf(svc_entries->service_entries[0].name,
350 sizeof(svc_entries->service_entries[0].name),
351 "%s%016llx",
352 SRP_SERVICE_NAME_PREFIX,
353 ioc_guid);
354
355 mad->mad_hdr.status = 0;
356}
357
358/**
359 * srpt_mgmt_method_get() - Process a received management datagram.
360 * @sp: source port through which the MAD has been received.
361 * @rq_mad: received MAD.
362 * @rsp_mad: response MAD.
363 */
364static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
365 struct ib_dm_mad *rsp_mad)
366{
367 u16 attr_id;
368 u32 slot;
369 u8 hi, lo;
370
371 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
372 switch (attr_id) {
373 case DM_ATTR_CLASS_PORT_INFO:
374 srpt_get_class_port_info(rsp_mad);
375 break;
376 case DM_ATTR_IOU_INFO:
377 srpt_get_iou(rsp_mad);
378 break;
379 case DM_ATTR_IOC_PROFILE:
380 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
381 srpt_get_ioc(sp, slot, rsp_mad);
382 break;
383 case DM_ATTR_SVC_ENTRIES:
384 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
385 hi = (u8) ((slot >> 8) & 0xff);
386 lo = (u8) (slot & 0xff);
387 slot = (u16) ((slot >> 16) & 0xffff);
388 srpt_get_svc_entries(srpt_service_guid,
389 slot, hi, lo, rsp_mad);
390 break;
391 default:
392 rsp_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530393 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000394 break;
395 }
396}
397
398/**
399 * srpt_mad_send_handler() - Post MAD-send callback function.
400 */
401static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
402 struct ib_mad_send_wc *mad_wc)
403{
404 ib_destroy_ah(mad_wc->send_buf->ah);
405 ib_free_send_mad(mad_wc->send_buf);
406}
407
408/**
409 * srpt_mad_recv_handler() - MAD reception callback function.
410 */
411static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
Christoph Hellwigca281262016-01-04 14:15:58 +0100412 struct ib_mad_send_buf *send_buf,
Bart Van Asschea42d9852011-10-14 01:30:46 +0000413 struct ib_mad_recv_wc *mad_wc)
414{
415 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
416 struct ib_ah *ah;
417 struct ib_mad_send_buf *rsp;
418 struct ib_dm_mad *dm_mad;
419
420 if (!mad_wc || !mad_wc->recv_buf.mad)
421 return;
422
423 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
424 mad_wc->recv_buf.grh, mad_agent->port_num);
425 if (IS_ERR(ah))
426 goto err;
427
428 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
429
430 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
431 mad_wc->wc->pkey_index, 0,
432 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
Ira Weinyda2dfaa2015-06-06 14:38:28 -0400433 GFP_KERNEL,
434 IB_MGMT_BASE_VERSION);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000435 if (IS_ERR(rsp))
436 goto err_rsp;
437
438 rsp->ah = ah;
439
440 dm_mad = rsp->mad;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800441 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000442 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
443 dm_mad->mad_hdr.status = 0;
444
445 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
446 case IB_MGMT_METHOD_GET:
447 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
448 break;
449 case IB_MGMT_METHOD_SET:
450 dm_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530451 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000452 break;
453 default:
454 dm_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530455 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000456 break;
457 }
458
459 if (!ib_post_send_mad(rsp, NULL)) {
460 ib_free_recv_mad(mad_wc);
461 /* will destroy_ah & free_send_mad in send completion */
462 return;
463 }
464
465 ib_free_send_mad(rsp);
466
467err_rsp:
468 ib_destroy_ah(ah);
469err:
470 ib_free_recv_mad(mad_wc);
471}
472
473/**
474 * srpt_refresh_port() - Configure a HCA port.
475 *
476 * Enable InfiniBand management datagram processing, update the cached sm_lid,
477 * lid and gid values, and register a callback function for processing MADs
478 * on the specified port.
479 *
480 * Note: It is safe to call this function more than once for the same port.
481 */
482static int srpt_refresh_port(struct srpt_port *sport)
483{
484 struct ib_mad_reg_req reg_req;
485 struct ib_port_modify port_modify;
486 struct ib_port_attr port_attr;
487 int ret;
488
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800489 memset(&port_modify, 0, sizeof(port_modify));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000490 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
491 port_modify.clr_port_cap_mask = 0;
492
493 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
494 if (ret)
495 goto err_mod_port;
496
497 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
498 if (ret)
499 goto err_query_port;
500
501 sport->sm_lid = port_attr.sm_lid;
502 sport->lid = port_attr.lid;
503
Matan Barak55ee3ab2015-10-15 18:38:45 +0300504 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid,
505 NULL);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000506 if (ret)
507 goto err_query_port;
508
509 if (!sport->mad_agent) {
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800510 memset(&reg_req, 0, sizeof(reg_req));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000511 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
512 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
513 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
514 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
515
516 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
517 sport->port,
518 IB_QPT_GSI,
519 &reg_req, 0,
520 srpt_mad_send_handler,
521 srpt_mad_recv_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -0400522 sport, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000523 if (IS_ERR(sport->mad_agent)) {
524 ret = PTR_ERR(sport->mad_agent);
525 sport->mad_agent = NULL;
526 goto err_query_port;
527 }
528 }
529
530 return 0;
531
532err_query_port:
533
534 port_modify.set_port_cap_mask = 0;
535 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
536 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
537
538err_mod_port:
539
540 return ret;
541}
542
543/**
544 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
545 *
546 * Note: It is safe to call this function more than once for the same device.
547 */
548static void srpt_unregister_mad_agent(struct srpt_device *sdev)
549{
550 struct ib_port_modify port_modify = {
551 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
552 };
553 struct srpt_port *sport;
554 int i;
555
556 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
557 sport = &sdev->port[i - 1];
558 WARN_ON(sport->port != i);
559 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400560 pr_err("disabling MAD processing failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000561 if (sport->mad_agent) {
562 ib_unregister_mad_agent(sport->mad_agent);
563 sport->mad_agent = NULL;
564 }
565 }
566}
567
568/**
569 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
570 */
571static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
572 int ioctx_size, int dma_size,
573 enum dma_data_direction dir)
574{
575 struct srpt_ioctx *ioctx;
576
577 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
578 if (!ioctx)
579 goto err;
580
581 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
582 if (!ioctx->buf)
583 goto err_free_ioctx;
584
585 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
586 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
587 goto err_free_buf;
588
589 return ioctx;
590
591err_free_buf:
592 kfree(ioctx->buf);
593err_free_ioctx:
594 kfree(ioctx);
595err:
596 return NULL;
597}
598
599/**
600 * srpt_free_ioctx() - Free an SRPT I/O context structure.
601 */
602static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
603 int dma_size, enum dma_data_direction dir)
604{
605 if (!ioctx)
606 return;
607
608 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
609 kfree(ioctx->buf);
610 kfree(ioctx);
611}
612
613/**
614 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
615 * @sdev: Device to allocate the I/O context ring for.
616 * @ring_size: Number of elements in the I/O context ring.
617 * @ioctx_size: I/O context size.
618 * @dma_size: DMA buffer size.
619 * @dir: DMA data direction.
620 */
621static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
622 int ring_size, int ioctx_size,
623 int dma_size, enum dma_data_direction dir)
624{
625 struct srpt_ioctx **ring;
626 int i;
627
628 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
629 && ioctx_size != sizeof(struct srpt_send_ioctx));
630
631 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
632 if (!ring)
633 goto out;
634 for (i = 0; i < ring_size; ++i) {
635 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
636 if (!ring[i])
637 goto err;
638 ring[i]->index = i;
639 }
640 goto out;
641
642err:
643 while (--i >= 0)
644 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
645 kfree(ring);
Jesper Juhl715252d2012-02-04 23:49:40 +0100646 ring = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000647out:
648 return ring;
649}
650
651/**
652 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
653 */
654static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
655 struct srpt_device *sdev, int ring_size,
656 int dma_size, enum dma_data_direction dir)
657{
658 int i;
659
660 for (i = 0; i < ring_size; ++i)
661 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
662 kfree(ioctx_ring);
663}
664
665/**
666 * srpt_get_cmd_state() - Get the state of a SCSI command.
667 */
668static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
669{
670 enum srpt_command_state state;
671 unsigned long flags;
672
673 BUG_ON(!ioctx);
674
675 spin_lock_irqsave(&ioctx->spinlock, flags);
676 state = ioctx->state;
677 spin_unlock_irqrestore(&ioctx->spinlock, flags);
678 return state;
679}
680
681/**
682 * srpt_set_cmd_state() - Set the state of a SCSI command.
683 *
684 * Does not modify the state of aborted commands. Returns the previous command
685 * state.
686 */
687static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
688 enum srpt_command_state new)
689{
690 enum srpt_command_state previous;
691 unsigned long flags;
692
693 BUG_ON(!ioctx);
694
695 spin_lock_irqsave(&ioctx->spinlock, flags);
696 previous = ioctx->state;
697 if (previous != SRPT_STATE_DONE)
698 ioctx->state = new;
699 spin_unlock_irqrestore(&ioctx->spinlock, flags);
700
701 return previous;
702}
703
704/**
705 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
706 *
707 * Returns true if and only if the previous command state was equal to 'old'.
708 */
709static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
710 enum srpt_command_state old,
711 enum srpt_command_state new)
712{
713 enum srpt_command_state previous;
714 unsigned long flags;
715
716 WARN_ON(!ioctx);
717 WARN_ON(old == SRPT_STATE_DONE);
718 WARN_ON(new == SRPT_STATE_NEW);
719
720 spin_lock_irqsave(&ioctx->spinlock, flags);
721 previous = ioctx->state;
722 if (previous == old)
723 ioctx->state = new;
724 spin_unlock_irqrestore(&ioctx->spinlock, flags);
725 return previous == old;
726}
727
728/**
729 * srpt_post_recv() - Post an IB receive request.
730 */
731static int srpt_post_recv(struct srpt_device *sdev,
732 struct srpt_recv_ioctx *ioctx)
733{
734 struct ib_sge list;
735 struct ib_recv_wr wr, *bad_wr;
736
737 BUG_ON(!sdev);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000738 list.addr = ioctx->ioctx.dma;
739 list.length = srp_max_req_size;
Jason Gunthorpe5a783952015-07-30 17:22:24 -0600740 list.lkey = sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000741
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200742 ioctx->ioctx.cqe.done = srpt_recv_done;
743 wr.wr_cqe = &ioctx->ioctx.cqe;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000744 wr.next = NULL;
745 wr.sg_list = &list;
746 wr.num_sge = 1;
747
748 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
749}
750
751/**
752 * srpt_post_send() - Post an IB send request.
753 *
754 * Returns zero upon success and a non-zero value upon failure.
755 */
756static int srpt_post_send(struct srpt_rdma_ch *ch,
757 struct srpt_send_ioctx *ioctx, int len)
758{
759 struct ib_sge list;
760 struct ib_send_wr wr, *bad_wr;
761 struct srpt_device *sdev = ch->sport->sdev;
762 int ret;
763
764 atomic_inc(&ch->req_lim);
765
766 ret = -ENOMEM;
767 if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400768 pr_warn("IB send queue full (needed 1)\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000769 goto out;
770 }
771
772 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
773 DMA_TO_DEVICE);
774
775 list.addr = ioctx->ioctx.dma;
776 list.length = len;
Jason Gunthorpe5a783952015-07-30 17:22:24 -0600777 list.lkey = sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000778
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200779 ioctx->ioctx.cqe.done = srpt_send_done;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000780 wr.next = NULL;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200781 wr.wr_cqe = &ioctx->ioctx.cqe;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000782 wr.sg_list = &list;
783 wr.num_sge = 1;
784 wr.opcode = IB_WR_SEND;
785 wr.send_flags = IB_SEND_SIGNALED;
786
787 ret = ib_post_send(ch->qp, &wr, &bad_wr);
788
789out:
790 if (ret < 0) {
791 atomic_inc(&ch->sq_wr_avail);
792 atomic_dec(&ch->req_lim);
793 }
794 return ret;
795}
796
797/**
798 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
799 * @ioctx: Pointer to the I/O context associated with the request.
800 * @srp_cmd: Pointer to the SRP_CMD request data.
801 * @dir: Pointer to the variable to which the transfer direction will be
802 * written.
803 * @data_len: Pointer to the variable to which the total data length of all
804 * descriptors in the SRP_CMD request will be written.
805 *
806 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
807 *
808 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
809 * -ENOMEM when memory allocation fails and zero upon success.
810 */
811static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
812 struct srp_cmd *srp_cmd,
813 enum dma_data_direction *dir, u64 *data_len)
814{
815 struct srp_indirect_buf *idb;
816 struct srp_direct_buf *db;
817 unsigned add_cdb_offset;
818 int ret;
819
820 /*
821 * The pointer computations below will only be compiled correctly
822 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
823 * whether srp_cmd::add_data has been declared as a byte pointer.
824 */
825 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
826 && !__same_type(srp_cmd->add_data[0], (u8)0));
827
828 BUG_ON(!dir);
829 BUG_ON(!data_len);
830
831 ret = 0;
832 *data_len = 0;
833
834 /*
835 * The lower four bits of the buffer format field contain the DATA-IN
836 * buffer descriptor format, and the highest four bits contain the
837 * DATA-OUT buffer descriptor format.
838 */
839 *dir = DMA_NONE;
840 if (srp_cmd->buf_fmt & 0xf)
841 /* DATA-IN: transfer data from target to initiator (read). */
842 *dir = DMA_FROM_DEVICE;
843 else if (srp_cmd->buf_fmt >> 4)
844 /* DATA-OUT: transfer data from initiator to target (write). */
845 *dir = DMA_TO_DEVICE;
846
847 /*
848 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
849 * CDB LENGTH' field are reserved and the size in bytes of this field
850 * is four times the value specified in bits 3..7. Hence the "& ~3".
851 */
852 add_cdb_offset = srp_cmd->add_cdb_len & ~3;
853 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
854 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
855 ioctx->n_rbuf = 1;
856 ioctx->rbufs = &ioctx->single_rbuf;
857
858 db = (struct srp_direct_buf *)(srp_cmd->add_data
859 + add_cdb_offset);
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800860 memcpy(ioctx->rbufs, db, sizeof(*db));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000861 *data_len = be32_to_cpu(db->len);
862 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
863 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
864 idb = (struct srp_indirect_buf *)(srp_cmd->add_data
865 + add_cdb_offset);
866
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800867 ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof(*db);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000868
869 if (ioctx->n_rbuf >
870 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400871 pr_err("received unsupported SRP_CMD request"
Bart Van Asschea42d9852011-10-14 01:30:46 +0000872 " type (%u out + %u in != %u / %zu)\n",
873 srp_cmd->data_out_desc_cnt,
874 srp_cmd->data_in_desc_cnt,
875 be32_to_cpu(idb->table_desc.len),
876 sizeof(*db));
877 ioctx->n_rbuf = 0;
878 ret = -EINVAL;
879 goto out;
880 }
881
882 if (ioctx->n_rbuf == 1)
883 ioctx->rbufs = &ioctx->single_rbuf;
884 else {
885 ioctx->rbufs =
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800886 kmalloc(ioctx->n_rbuf * sizeof(*db), GFP_ATOMIC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000887 if (!ioctx->rbufs) {
888 ioctx->n_rbuf = 0;
889 ret = -ENOMEM;
890 goto out;
891 }
892 }
893
894 db = idb->desc_list;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800895 memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof(*db));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000896 *data_len = be32_to_cpu(idb->len);
897 }
898out:
899 return ret;
900}
901
902/**
903 * srpt_init_ch_qp() - Initialize queue pair attributes.
904 *
905 * Initialized the attributes of queue pair 'qp' by allowing local write,
906 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
907 */
908static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
909{
910 struct ib_qp_attr *attr;
911 int ret;
912
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800913 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000914 if (!attr)
915 return -ENOMEM;
916
917 attr->qp_state = IB_QPS_INIT;
918 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
919 IB_ACCESS_REMOTE_WRITE;
920 attr->port_num = ch->sport->port;
921 attr->pkey_index = 0;
922
923 ret = ib_modify_qp(qp, attr,
924 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
925 IB_QP_PKEY_INDEX);
926
927 kfree(attr);
928 return ret;
929}
930
931/**
932 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
933 * @ch: channel of the queue pair.
934 * @qp: queue pair to change the state of.
935 *
936 * Returns zero upon success and a negative value upon failure.
937 *
938 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
939 * If this structure ever becomes larger, it might be necessary to allocate
940 * it dynamically instead of on the stack.
941 */
942static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
943{
944 struct ib_qp_attr qp_attr;
945 int attr_mask;
946 int ret;
947
948 qp_attr.qp_state = IB_QPS_RTR;
949 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
950 if (ret)
951 goto out;
952
953 qp_attr.max_dest_rd_atomic = 4;
954
955 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
956
957out:
958 return ret;
959}
960
961/**
962 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
963 * @ch: channel of the queue pair.
964 * @qp: queue pair to change the state of.
965 *
966 * Returns zero upon success and a negative value upon failure.
967 *
968 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
969 * If this structure ever becomes larger, it might be necessary to allocate
970 * it dynamically instead of on the stack.
971 */
972static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
973{
974 struct ib_qp_attr qp_attr;
975 int attr_mask;
976 int ret;
977
978 qp_attr.qp_state = IB_QPS_RTS;
979 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
980 if (ret)
981 goto out;
982
983 qp_attr.max_rd_atomic = 4;
984
985 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
986
987out:
988 return ret;
989}
990
991/**
992 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
993 */
994static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
995{
996 struct ib_qp_attr qp_attr;
997
998 qp_attr.qp_state = IB_QPS_ERR;
999 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1000}
1001
1002/**
1003 * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
1004 */
1005static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1006 struct srpt_send_ioctx *ioctx)
1007{
1008 struct scatterlist *sg;
1009 enum dma_data_direction dir;
1010
1011 BUG_ON(!ch);
1012 BUG_ON(!ioctx);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001013 BUG_ON(ioctx->n_rdma && !ioctx->rdma_wrs);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001014
1015 while (ioctx->n_rdma)
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001016 kfree(ioctx->rdma_wrs[--ioctx->n_rdma].wr.sg_list);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001017
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001018 kfree(ioctx->rdma_wrs);
1019 ioctx->rdma_wrs = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001020
1021 if (ioctx->mapped_sg_count) {
1022 sg = ioctx->sg;
1023 WARN_ON(!sg);
1024 dir = ioctx->cmd.data_direction;
1025 BUG_ON(dir == DMA_NONE);
1026 ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
Bart Van Assche671ec1b2016-02-11 11:05:01 -08001027 target_reverse_dma_direction(&ioctx->cmd));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001028 ioctx->mapped_sg_count = 0;
1029 }
1030}
1031
1032/**
1033 * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
1034 */
1035static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1036 struct srpt_send_ioctx *ioctx)
1037{
Mike Marciniszynb0768082014-04-07 13:58:35 -04001038 struct ib_device *dev = ch->sport->sdev->device;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001039 struct se_cmd *cmd;
1040 struct scatterlist *sg, *sg_orig;
1041 int sg_cnt;
1042 enum dma_data_direction dir;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001043 struct ib_rdma_wr *riu;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001044 struct srp_direct_buf *db;
1045 dma_addr_t dma_addr;
1046 struct ib_sge *sge;
1047 u64 raddr;
1048 u32 rsize;
1049 u32 tsize;
1050 u32 dma_len;
1051 int count, nrdma;
1052 int i, j, k;
1053
1054 BUG_ON(!ch);
1055 BUG_ON(!ioctx);
1056 cmd = &ioctx->cmd;
1057 dir = cmd->data_direction;
1058 BUG_ON(dir == DMA_NONE);
1059
Roland Dreier6f9e7f02012-03-30 11:29:12 -07001060 ioctx->sg = sg = sg_orig = cmd->t_data_sg;
1061 ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001062
1063 count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
Bart Van Assche671ec1b2016-02-11 11:05:01 -08001064 target_reverse_dma_direction(cmd));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001065 if (unlikely(!count))
1066 return -EAGAIN;
1067
1068 ioctx->mapped_sg_count = count;
1069
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001070 if (ioctx->rdma_wrs && ioctx->n_rdma_wrs)
1071 nrdma = ioctx->n_rdma_wrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001072 else {
1073 nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
1074 + ioctx->n_rbuf;
1075
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001076 ioctx->rdma_wrs = kcalloc(nrdma, sizeof(*ioctx->rdma_wrs),
1077 GFP_KERNEL);
1078 if (!ioctx->rdma_wrs)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001079 goto free_mem;
1080
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001081 ioctx->n_rdma_wrs = nrdma;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001082 }
1083
1084 db = ioctx->rbufs;
1085 tsize = cmd->data_length;
Mike Marciniszynb0768082014-04-07 13:58:35 -04001086 dma_len = ib_sg_dma_len(dev, &sg[0]);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001087 riu = ioctx->rdma_wrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001088
1089 /*
1090 * For each remote desc - calculate the #ib_sge.
1091 * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1092 * each remote desc rdma_iu is required a rdma wr;
1093 * else
1094 * we need to allocate extra rdma_iu to carry extra #ib_sge in
1095 * another rdma wr
1096 */
1097 for (i = 0, j = 0;
1098 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1099 rsize = be32_to_cpu(db->len);
1100 raddr = be64_to_cpu(db->va);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001101 riu->remote_addr = raddr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001102 riu->rkey = be32_to_cpu(db->key);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001103 riu->wr.num_sge = 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001104
1105 /* calculate how many sge required for this remote_buf */
1106 while (rsize > 0 && tsize > 0) {
1107
1108 if (rsize >= dma_len) {
1109 tsize -= dma_len;
1110 rsize -= dma_len;
1111 raddr += dma_len;
1112
1113 if (tsize > 0) {
1114 ++j;
1115 if (j < count) {
1116 sg = sg_next(sg);
Mike Marciniszynb0768082014-04-07 13:58:35 -04001117 dma_len = ib_sg_dma_len(
1118 dev, sg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001119 }
1120 }
1121 } else {
1122 tsize -= rsize;
1123 dma_len -= rsize;
1124 rsize = 0;
1125 }
1126
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001127 ++riu->wr.num_sge;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001128
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001129 if (rsize > 0 &&
1130 riu->wr.num_sge == SRPT_DEF_SG_PER_WQE) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001131 ++ioctx->n_rdma;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001132 riu->wr.sg_list = kmalloc_array(riu->wr.num_sge,
1133 sizeof(*riu->wr.sg_list),
1134 GFP_KERNEL);
1135 if (!riu->wr.sg_list)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001136 goto free_mem;
1137
1138 ++riu;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001139 riu->wr.num_sge = 0;
1140 riu->remote_addr = raddr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001141 riu->rkey = be32_to_cpu(db->key);
1142 }
1143 }
1144
1145 ++ioctx->n_rdma;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001146 riu->wr.sg_list = kmalloc_array(riu->wr.num_sge,
1147 sizeof(*riu->wr.sg_list),
1148 GFP_KERNEL);
1149 if (!riu->wr.sg_list)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001150 goto free_mem;
1151 }
1152
1153 db = ioctx->rbufs;
1154 tsize = cmd->data_length;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001155 riu = ioctx->rdma_wrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001156 sg = sg_orig;
Mike Marciniszynb0768082014-04-07 13:58:35 -04001157 dma_len = ib_sg_dma_len(dev, &sg[0]);
1158 dma_addr = ib_sg_dma_address(dev, &sg[0]);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001159
1160 /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1161 for (i = 0, j = 0;
1162 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1163 rsize = be32_to_cpu(db->len);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001164 sge = riu->wr.sg_list;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001165 k = 0;
1166
1167 while (rsize > 0 && tsize > 0) {
1168 sge->addr = dma_addr;
Jason Gunthorpe5a783952015-07-30 17:22:24 -06001169 sge->lkey = ch->sport->sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001170
1171 if (rsize >= dma_len) {
1172 sge->length =
1173 (tsize < dma_len) ? tsize : dma_len;
1174 tsize -= dma_len;
1175 rsize -= dma_len;
1176
1177 if (tsize > 0) {
1178 ++j;
1179 if (j < count) {
1180 sg = sg_next(sg);
Mike Marciniszynb0768082014-04-07 13:58:35 -04001181 dma_len = ib_sg_dma_len(
1182 dev, sg);
1183 dma_addr = ib_sg_dma_address(
1184 dev, sg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001185 }
1186 }
1187 } else {
1188 sge->length = (tsize < rsize) ? tsize : rsize;
1189 tsize -= rsize;
1190 dma_len -= rsize;
1191 dma_addr += rsize;
1192 rsize = 0;
1193 }
1194
1195 ++k;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001196 if (k == riu->wr.num_sge && rsize > 0 && tsize > 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001197 ++riu;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001198 sge = riu->wr.sg_list;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001199 k = 0;
1200 } else if (rsize > 0 && tsize > 0)
1201 ++sge;
1202 }
1203 }
1204
1205 return 0;
1206
1207free_mem:
1208 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1209
1210 return -ENOMEM;
1211}
1212
1213/**
1214 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1215 */
1216static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1217{
1218 struct srpt_send_ioctx *ioctx;
1219 unsigned long flags;
1220
1221 BUG_ON(!ch);
1222
1223 ioctx = NULL;
1224 spin_lock_irqsave(&ch->spinlock, flags);
1225 if (!list_empty(&ch->free_list)) {
1226 ioctx = list_first_entry(&ch->free_list,
1227 struct srpt_send_ioctx, free_list);
1228 list_del(&ioctx->free_list);
1229 }
1230 spin_unlock_irqrestore(&ch->spinlock, flags);
1231
1232 if (!ioctx)
1233 return ioctx;
1234
1235 BUG_ON(ioctx->ch != ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001236 spin_lock_init(&ioctx->spinlock);
1237 ioctx->state = SRPT_STATE_NEW;
1238 ioctx->n_rbuf = 0;
1239 ioctx->rbufs = NULL;
1240 ioctx->n_rdma = 0;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001241 ioctx->n_rdma_wrs = 0;
1242 ioctx->rdma_wrs = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001243 ioctx->mapped_sg_count = 0;
1244 init_completion(&ioctx->tx_done);
1245 ioctx->queue_status_only = false;
1246 /*
1247 * transport_init_se_cmd() does not initialize all fields, so do it
1248 * here.
1249 */
1250 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1251 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1252
1253 return ioctx;
1254}
1255
1256/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001257 * srpt_abort_cmd() - Abort a SCSI command.
1258 * @ioctx: I/O context associated with the SCSI command.
1259 * @context: Preferred execution context.
1260 */
1261static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1262{
1263 enum srpt_command_state state;
1264 unsigned long flags;
1265
1266 BUG_ON(!ioctx);
1267
1268 /*
1269 * If the command is in a state where the target core is waiting for
Bart Van Assche49f40162016-02-11 11:07:11 -08001270 * the ib_srpt driver, change the state to the next state.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001271 */
1272
1273 spin_lock_irqsave(&ioctx->spinlock, flags);
1274 state = ioctx->state;
1275 switch (state) {
1276 case SRPT_STATE_NEED_DATA:
1277 ioctx->state = SRPT_STATE_DATA_IN;
1278 break;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001279 case SRPT_STATE_CMD_RSP_SENT:
1280 case SRPT_STATE_MGMT_RSP_SENT:
1281 ioctx->state = SRPT_STATE_DONE;
1282 break;
1283 default:
Bart Van Assche49f40162016-02-11 11:07:11 -08001284 WARN_ONCE(true, "%s: unexpected I/O context state %d\n",
1285 __func__, state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001286 break;
1287 }
1288 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1289
Bart Van Asschea42d9852011-10-14 01:30:46 +00001290 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
Bart Van Assche649ee052015-04-14 13:26:44 +02001291 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001292
1293 switch (state) {
1294 case SRPT_STATE_NEW:
1295 case SRPT_STATE_DATA_IN:
1296 case SRPT_STATE_MGMT:
Bart Van Assche49f40162016-02-11 11:07:11 -08001297 case SRPT_STATE_DONE:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001298 /*
1299 * Do nothing - defer abort processing until
1300 * srpt_queue_response() is invoked.
1301 */
Bart Van Asschea42d9852011-10-14 01:30:46 +00001302 break;
1303 case SRPT_STATE_NEED_DATA:
Bart Van Assche49f40162016-02-11 11:07:11 -08001304 pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag);
1305 transport_generic_request_failure(&ioctx->cmd,
1306 TCM_CHECK_CONDITION_ABORT_CMD);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001307 break;
1308 case SRPT_STATE_CMD_RSP_SENT:
1309 /*
1310 * SRP_RSP sending failed or the SRP_RSP send completion has
1311 * not been received in time.
1312 */
1313 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
Bart Van Assche49f40162016-02-11 11:07:11 -08001314 transport_generic_free_cmd(&ioctx->cmd, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001315 break;
1316 case SRPT_STATE_MGMT_RSP_SENT:
Bart Van Assche49f40162016-02-11 11:07:11 -08001317 transport_generic_free_cmd(&ioctx->cmd, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001318 break;
1319 default:
Grant Grundler532ec6f2013-03-26 21:48:28 +00001320 WARN(1, "Unexpected command state (%d)", state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001321 break;
1322 }
1323
Bart Van Asschea42d9852011-10-14 01:30:46 +00001324 return state;
1325}
1326
1327/**
Christoph Hellwige672a472012-07-08 15:58:43 -04001328 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1329 * the data that has been transferred via IB RDMA had to be postponed until the
Masanari Iida142ad5d2012-08-10 00:07:58 +00001330 * check_stop_free() callback. None of this is necessary anymore and needs to
Christoph Hellwige672a472012-07-08 15:58:43 -04001331 * be cleaned up.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001332 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001333static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001334{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001335 struct srpt_rdma_ch *ch = cq->cq_context;
1336 struct srpt_send_ioctx *ioctx =
Bart Van Assche19f57292015-12-31 09:56:43 +01001337 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001338
Bart Van Asschea42d9852011-10-14 01:30:46 +00001339 WARN_ON(ioctx->n_rdma <= 0);
1340 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1341
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001342 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1343 pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
1344 ioctx, wc->status);
1345 srpt_abort_cmd(ioctx);
1346 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001347 }
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001348
1349 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1350 SRPT_STATE_DATA_IN))
1351 target_execute_cmd(&ioctx->cmd);
1352 else
1353 pr_err("%s[%d]: wrong state = %d\n", __func__,
1354 __LINE__, srpt_get_cmd_state(ioctx));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001355}
1356
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001357static void srpt_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001358{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001359 struct srpt_send_ioctx *ioctx =
Bart Van Assche19f57292015-12-31 09:56:43 +01001360 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001361
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001362 if (unlikely(wc->status != IB_WC_SUCCESS)) {
Bart Van Assche49f40162016-02-11 11:07:11 -08001363 /*
1364 * Note: if an RDMA write error completion is received that
1365 * means that a SEND also has been posted. Defer further
1366 * processing of the associated command until the send error
1367 * completion has been received.
1368 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001369 pr_info("RDMA_WRITE for ioctx 0x%p failed with status %d\n",
1370 ioctx, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001371 }
1372}
1373
1374/**
1375 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1376 * @ch: RDMA channel through which the request has been received.
1377 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1378 * be built in the buffer ioctx->buf points at and hence this function will
1379 * overwrite the request data.
1380 * @tag: tag of the request for which this response is being generated.
1381 * @status: value for the STATUS field of the SRP_RSP information unit.
1382 *
1383 * Returns the size in bytes of the SRP_RSP response.
1384 *
1385 * An SRP_RSP response contains a SCSI status or service response. See also
1386 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1387 * response. See also SPC-2 for more information about sense data.
1388 */
1389static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1390 struct srpt_send_ioctx *ioctx, u64 tag,
1391 int status)
1392{
1393 struct srp_rsp *srp_rsp;
1394 const u8 *sense_data;
1395 int sense_data_len, max_sense_len;
1396
1397 /*
1398 * The lowest bit of all SAM-3 status codes is zero (see also
1399 * paragraph 5.3 in SAM-3).
1400 */
1401 WARN_ON(status & 1);
1402
1403 srp_rsp = ioctx->ioctx.buf;
1404 BUG_ON(!srp_rsp);
1405
1406 sense_data = ioctx->sense_data;
1407 sense_data_len = ioctx->cmd.scsi_sense_length;
1408 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1409
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08001410 memset(srp_rsp, 0, sizeof(*srp_rsp));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001411 srp_rsp->opcode = SRP_RSP;
1412 srp_rsp->req_lim_delta =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301413 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001414 srp_rsp->tag = tag;
1415 srp_rsp->status = status;
1416
1417 if (sense_data_len) {
1418 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1419 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1420 if (sense_data_len > max_sense_len) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001421 pr_warn("truncated sense data from %d to %d"
1422 " bytes\n", sense_data_len, max_sense_len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001423 sense_data_len = max_sense_len;
1424 }
1425
1426 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1427 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1428 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1429 }
1430
1431 return sizeof(*srp_rsp) + sense_data_len;
1432}
1433
1434/**
1435 * srpt_build_tskmgmt_rsp() - Build a task management response.
1436 * @ch: RDMA channel through which the request has been received.
1437 * @ioctx: I/O context in which the SRP_RSP response will be built.
1438 * @rsp_code: RSP_CODE that will be stored in the response.
1439 * @tag: Tag of the request for which this response is being generated.
1440 *
1441 * Returns the size in bytes of the SRP_RSP response.
1442 *
1443 * An SRP_RSP response contains a SCSI status or service response. See also
1444 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1445 * response.
1446 */
1447static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1448 struct srpt_send_ioctx *ioctx,
1449 u8 rsp_code, u64 tag)
1450{
1451 struct srp_rsp *srp_rsp;
1452 int resp_data_len;
1453 int resp_len;
1454
Jack Wangc807f642013-09-30 10:09:05 +02001455 resp_data_len = 4;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001456 resp_len = sizeof(*srp_rsp) + resp_data_len;
1457
1458 srp_rsp = ioctx->ioctx.buf;
1459 BUG_ON(!srp_rsp);
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08001460 memset(srp_rsp, 0, sizeof(*srp_rsp));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001461
1462 srp_rsp->opcode = SRP_RSP;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301463 srp_rsp->req_lim_delta =
1464 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001465 srp_rsp->tag = tag;
1466
Jack Wangc807f642013-09-30 10:09:05 +02001467 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1468 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1469 srp_rsp->data[3] = rsp_code;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001470
1471 return resp_len;
1472}
1473
Bart Van Asschea42d9852011-10-14 01:30:46 +00001474static int srpt_check_stop_free(struct se_cmd *cmd)
1475{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001476 struct srpt_send_ioctx *ioctx = container_of(cmd,
1477 struct srpt_send_ioctx, cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001478
Bart Van Asscheafc16602015-04-27 13:52:36 +02001479 return target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001480}
1481
1482/**
1483 * srpt_handle_cmd() - Process SRP_CMD.
1484 */
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001485static void srpt_handle_cmd(struct srpt_rdma_ch *ch,
1486 struct srpt_recv_ioctx *recv_ioctx,
1487 struct srpt_send_ioctx *send_ioctx)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001488{
1489 struct se_cmd *cmd;
1490 struct srp_cmd *srp_cmd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001491 u64 data_len;
1492 enum dma_data_direction dir;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001493 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001494
1495 BUG_ON(!send_ioctx);
1496
1497 srp_cmd = recv_ioctx->ioctx.buf;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001498 cmd = &send_ioctx->cmd;
Bart Van Assche649ee052015-04-14 13:26:44 +02001499 cmd->tag = srp_cmd->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001500
1501 switch (srp_cmd->task_attr) {
1502 case SRP_CMD_SIMPLE_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001503 cmd->sam_task_attr = TCM_SIMPLE_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001504 break;
1505 case SRP_CMD_ORDERED_Q:
1506 default:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001507 cmd->sam_task_attr = TCM_ORDERED_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001508 break;
1509 case SRP_CMD_HEAD_OF_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001510 cmd->sam_task_attr = TCM_HEAD_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001511 break;
1512 case SRP_CMD_ACA:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001513 cmd->sam_task_attr = TCM_ACA_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001514 break;
1515 }
1516
Christoph Hellwigde103c92012-11-06 12:24:09 -08001517 if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001518 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001519 srp_cmd->tag);
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001520 goto release_ioctx;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001521 }
1522
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001523 rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
Bart Van Asschee1dd4132016-02-11 11:05:19 -08001524 &send_ioctx->sense_data[0],
1525 scsilun_to_int(&srp_cmd->lun), data_len,
1526 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001527 if (rc != 0) {
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001528 pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc,
1529 srp_cmd->tag);
1530 goto release_ioctx;
Nicholas Bellinger187e70a2012-03-17 20:12:36 -07001531 }
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001532 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001533
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001534release_ioctx:
1535 send_ioctx->state = SRPT_STATE_DONE;
1536 srpt_release_cmd(cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001537}
1538
Bart Van Asschea42d9852011-10-14 01:30:46 +00001539static int srp_tmr_to_tcm(int fn)
1540{
1541 switch (fn) {
1542 case SRP_TSK_ABORT_TASK:
1543 return TMR_ABORT_TASK;
1544 case SRP_TSK_ABORT_TASK_SET:
1545 return TMR_ABORT_TASK_SET;
1546 case SRP_TSK_CLEAR_TASK_SET:
1547 return TMR_CLEAR_TASK_SET;
1548 case SRP_TSK_LUN_RESET:
1549 return TMR_LUN_RESET;
1550 case SRP_TSK_CLEAR_ACA:
1551 return TMR_CLEAR_ACA;
1552 default:
1553 return -1;
1554 }
1555}
1556
1557/**
1558 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1559 *
1560 * Returns 0 if and only if the request will be processed by the target core.
1561 *
1562 * For more information about SRP_TSK_MGMT information units, see also section
1563 * 6.7 in the SRP r16a document.
1564 */
1565static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1566 struct srpt_recv_ioctx *recv_ioctx,
1567 struct srpt_send_ioctx *send_ioctx)
1568{
1569 struct srp_tsk_mgmt *srp_tsk;
1570 struct se_cmd *cmd;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001571 struct se_session *sess = ch->sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001572 int tcm_tmr;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001573 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001574
1575 BUG_ON(!send_ioctx);
1576
1577 srp_tsk = recv_ioctx->ioctx.buf;
1578 cmd = &send_ioctx->cmd;
1579
1580 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1581 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1582 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1583
1584 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
Bart Van Assche649ee052015-04-14 13:26:44 +02001585 send_ioctx->cmd.tag = srp_tsk->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001586 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
Bart Van Asschee1dd4132016-02-11 11:05:19 -08001587 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL,
1588 scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr,
1589 GFP_KERNEL, srp_tsk->task_tag,
1590 TARGET_SCF_ACK_KREF);
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001591 if (rc != 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001592 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001593 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001594 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001595 return;
1596fail:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001597 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001598}
1599
1600/**
1601 * srpt_handle_new_iu() - Process a newly received information unit.
1602 * @ch: RDMA channel through which the information unit has been received.
1603 * @ioctx: SRPT I/O context associated with the information unit.
1604 */
1605static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1606 struct srpt_recv_ioctx *recv_ioctx,
1607 struct srpt_send_ioctx *send_ioctx)
1608{
1609 struct srp_cmd *srp_cmd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001610
1611 BUG_ON(!ch);
1612 BUG_ON(!recv_ioctx);
1613
1614 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1615 recv_ioctx->ioctx.dma, srp_max_req_size,
1616 DMA_FROM_DEVICE);
1617
Bart Van Assche33912d72016-02-11 11:04:43 -08001618 if (unlikely(ch->state == CH_CONNECTING)) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001619 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1620 goto out;
1621 }
1622
Bart Van Assche33912d72016-02-11 11:04:43 -08001623 if (unlikely(ch->state != CH_LIVE))
Bart Van Asschea42d9852011-10-14 01:30:46 +00001624 goto out;
1625
1626 srp_cmd = recv_ioctx->ioctx.buf;
1627 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1628 if (!send_ioctx)
1629 send_ioctx = srpt_get_send_ioctx(ch);
1630 if (unlikely(!send_ioctx)) {
1631 list_add_tail(&recv_ioctx->wait_list,
1632 &ch->cmd_wait_list);
1633 goto out;
1634 }
1635 }
1636
Bart Van Asschea42d9852011-10-14 01:30:46 +00001637 switch (srp_cmd->opcode) {
1638 case SRP_CMD:
1639 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1640 break;
1641 case SRP_TSK_MGMT:
1642 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1643 break;
1644 case SRP_I_LOGOUT:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001645 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001646 break;
1647 case SRP_CRED_RSP:
1648 pr_debug("received SRP_CRED_RSP\n");
1649 break;
1650 case SRP_AER_RSP:
1651 pr_debug("received SRP_AER_RSP\n");
1652 break;
1653 case SRP_RSP:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001654 pr_err("Received SRP_RSP\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001655 break;
1656 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001657 pr_err("received IU with unknown opcode 0x%x\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001658 srp_cmd->opcode);
1659 break;
1660 }
1661
1662 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1663out:
1664 return;
1665}
1666
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001667static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001668{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001669 struct srpt_rdma_ch *ch = cq->cq_context;
1670 struct srpt_recv_ioctx *ioctx =
1671 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001672
Bart Van Asschea42d9852011-10-14 01:30:46 +00001673 if (wc->status == IB_WC_SUCCESS) {
1674 int req_lim;
1675
1676 req_lim = atomic_dec_return(&ch->req_lim);
1677 if (unlikely(req_lim < 0))
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001678 pr_err("req_lim = %d < 0\n", req_lim);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001679 srpt_handle_new_iu(ch, ioctx, NULL);
1680 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001681 pr_info("receiving failed for ioctx %p with status %d\n",
1682 ioctx, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001683 }
1684}
1685
1686/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001687 * Note: Although this has not yet been observed during tests, at least in
1688 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1689 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1690 * value in each response is set to one, and it is possible that this response
1691 * makes the initiator send a new request before the send completion for that
1692 * response has been processed. This could e.g. happen if the call to
1693 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1694 * if IB retransmission causes generation of the send completion to be
1695 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1696 * are queued on cmd_wait_list. The code below processes these delayed
1697 * requests one at a time.
1698 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001699static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001700{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001701 struct srpt_rdma_ch *ch = cq->cq_context;
1702 struct srpt_send_ioctx *ioctx =
1703 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1704 enum srpt_command_state state;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001705
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001706 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1707
1708 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1709 state != SRPT_STATE_MGMT_RSP_SENT);
1710
1711 atomic_inc(&ch->sq_wr_avail);
1712
Bart Van Assche49f40162016-02-11 11:07:11 -08001713 if (wc->status != IB_WC_SUCCESS)
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001714 pr_info("sending response for ioctx 0x%p failed"
1715 " with status %d\n", ioctx, wc->status);
1716
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001717 if (state != SRPT_STATE_DONE) {
1718 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1719 transport_generic_free_cmd(&ioctx->cmd, 0);
1720 } else {
1721 pr_err("IB completion has been received too late for"
1722 " wr_id = %u.\n", ioctx->ioctx.index);
1723 }
1724
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001725 while (!list_empty(&ch->cmd_wait_list) &&
Bart Van Assche33912d72016-02-11 11:04:43 -08001726 ch->state == CH_LIVE &&
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001727 (ioctx = srpt_get_send_ioctx(ch)) != NULL) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001728 struct srpt_recv_ioctx *recv_ioctx;
1729
1730 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
1731 struct srpt_recv_ioctx,
1732 wait_list);
1733 list_del(&recv_ioctx->wait_list);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001734 srpt_handle_new_iu(ch, recv_ioctx, ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001735 }
1736}
1737
Bart Van Asschea42d9852011-10-14 01:30:46 +00001738/**
1739 * srpt_create_ch_ib() - Create receive and send completion queues.
1740 */
1741static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1742{
1743 struct ib_qp_init_attr *qp_init;
1744 struct srpt_port *sport = ch->sport;
1745 struct srpt_device *sdev = sport->sdev;
1746 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
1747 int ret;
1748
1749 WARN_ON(ch->rq_size < 1);
1750
1751 ret = -ENOMEM;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08001752 qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001753 if (!qp_init)
1754 goto out;
1755
Bart Van Asscheab477c12014-10-19 18:05:33 +03001756retry:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001757 ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size,
1758 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001759 if (IS_ERR(ch->cq)) {
1760 ret = PTR_ERR(ch->cq);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001761 pr_err("failed to create CQ cqe= %d ret= %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001762 ch->rq_size + srp_sq_size, ret);
1763 goto out;
1764 }
1765
1766 qp_init->qp_context = (void *)ch;
1767 qp_init->event_handler
1768 = (void(*)(struct ib_event *, void*))srpt_qp_event;
1769 qp_init->send_cq = ch->cq;
1770 qp_init->recv_cq = ch->cq;
1771 qp_init->srq = sdev->srq;
1772 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1773 qp_init->qp_type = IB_QPT_RC;
1774 qp_init->cap.max_send_wr = srp_sq_size;
1775 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
1776
1777 ch->qp = ib_create_qp(sdev->pd, qp_init);
1778 if (IS_ERR(ch->qp)) {
1779 ret = PTR_ERR(ch->qp);
Bart Van Asscheab477c12014-10-19 18:05:33 +03001780 if (ret == -ENOMEM) {
1781 srp_sq_size /= 2;
1782 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
1783 ib_destroy_cq(ch->cq);
1784 goto retry;
1785 }
1786 }
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001787 pr_err("failed to create_qp ret= %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001788 goto err_destroy_cq;
1789 }
1790
1791 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1792
1793 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
1794 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1795 qp_init->cap.max_send_wr, ch->cm_id);
1796
1797 ret = srpt_init_ch_qp(ch, ch->qp);
1798 if (ret)
1799 goto err_destroy_qp;
1800
Bart Van Asschea42d9852011-10-14 01:30:46 +00001801out:
1802 kfree(qp_init);
1803 return ret;
1804
1805err_destroy_qp:
1806 ib_destroy_qp(ch->qp);
1807err_destroy_cq:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001808 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001809 goto out;
1810}
1811
1812static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
1813{
Bart Van Asschea42d9852011-10-14 01:30:46 +00001814 ib_destroy_qp(ch->qp);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001815 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001816}
1817
1818/**
1819 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
1820 *
1821 * Reset the QP and make sure all resources associated with the channel will
1822 * be deallocated at an appropriate time.
1823 *
1824 * Note: The caller must hold ch->sport->sdev->spinlock.
1825 */
1826static void __srpt_close_ch(struct srpt_rdma_ch *ch)
1827{
Bart Van Asschea42d9852011-10-14 01:30:46 +00001828 enum rdma_ch_state prev_state;
1829 unsigned long flags;
1830
Bart Van Asschea42d9852011-10-14 01:30:46 +00001831 spin_lock_irqsave(&ch->spinlock, flags);
1832 prev_state = ch->state;
1833 switch (prev_state) {
1834 case CH_CONNECTING:
1835 case CH_LIVE:
1836 ch->state = CH_DISCONNECTING;
1837 break;
1838 default:
1839 break;
1840 }
1841 spin_unlock_irqrestore(&ch->spinlock, flags);
1842
1843 switch (prev_state) {
1844 case CH_CONNECTING:
1845 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
1846 NULL, 0);
1847 /* fall through */
1848 case CH_LIVE:
1849 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001850 pr_err("sending CM DREQ failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001851 break;
1852 case CH_DISCONNECTING:
1853 break;
1854 case CH_DRAINING:
1855 case CH_RELEASING:
1856 break;
1857 }
1858}
1859
1860/**
1861 * srpt_close_ch() - Close an RDMA channel.
1862 */
1863static void srpt_close_ch(struct srpt_rdma_ch *ch)
1864{
1865 struct srpt_device *sdev;
1866
1867 sdev = ch->sport->sdev;
1868 spin_lock_irq(&sdev->spinlock);
1869 __srpt_close_ch(ch);
1870 spin_unlock_irq(&sdev->spinlock);
1871}
1872
1873/**
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07001874 * srpt_shutdown_session() - Whether or not a session may be shut down.
1875 */
1876static int srpt_shutdown_session(struct se_session *se_sess)
1877{
Bart Van Assche88936252016-02-11 11:05:58 -08001878 return 1;
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07001879}
1880
1881/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001882 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
1883 * @cm_id: Pointer to the CM ID of the channel to be drained.
1884 *
1885 * Note: Must be called from inside srpt_cm_handler to avoid a race between
1886 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
1887 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
1888 * waits until all target sessions for the associated IB device have been
1889 * unregistered and target session registration involves a call to
1890 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
1891 * this function has finished).
1892 */
1893static void srpt_drain_channel(struct ib_cm_id *cm_id)
1894{
1895 struct srpt_device *sdev;
1896 struct srpt_rdma_ch *ch;
1897 int ret;
1898 bool do_reset = false;
1899
1900 WARN_ON_ONCE(irqs_disabled());
1901
1902 sdev = cm_id->context;
1903 BUG_ON(!sdev);
1904 spin_lock_irq(&sdev->spinlock);
1905 list_for_each_entry(ch, &sdev->rch_list, list) {
1906 if (ch->cm_id == cm_id) {
Bart Van Asschef130c222016-02-11 11:05:38 -08001907 do_reset = srpt_set_ch_state(ch, CH_DRAINING);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001908 break;
1909 }
1910 }
1911 spin_unlock_irq(&sdev->spinlock);
1912
1913 if (do_reset) {
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07001914 if (ch->sess)
1915 srpt_shutdown_session(ch->sess);
1916
Bart Van Asschea42d9852011-10-14 01:30:46 +00001917 ret = srpt_ch_qp_err(ch);
1918 if (ret < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001919 pr_err("Setting queue pair in error state"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001920 " failed: %d\n", ret);
1921 }
1922}
1923
1924/**
1925 * srpt_find_channel() - Look up an RDMA channel.
1926 * @cm_id: Pointer to the CM ID of the channel to be looked up.
1927 *
1928 * Return NULL if no matching RDMA channel has been found.
1929 */
1930static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
1931 struct ib_cm_id *cm_id)
1932{
1933 struct srpt_rdma_ch *ch;
1934 bool found;
1935
1936 WARN_ON_ONCE(irqs_disabled());
1937 BUG_ON(!sdev);
1938
1939 found = false;
1940 spin_lock_irq(&sdev->spinlock);
1941 list_for_each_entry(ch, &sdev->rch_list, list) {
1942 if (ch->cm_id == cm_id) {
1943 found = true;
1944 break;
1945 }
1946 }
1947 spin_unlock_irq(&sdev->spinlock);
1948
1949 return found ? ch : NULL;
1950}
1951
1952/**
1953 * srpt_release_channel() - Release channel resources.
1954 *
1955 * Schedules the actual release because:
1956 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
1957 * trigger a deadlock.
1958 * - It is not safe to call TCM transport_* functions from interrupt context.
1959 */
1960static void srpt_release_channel(struct srpt_rdma_ch *ch)
1961{
1962 schedule_work(&ch->release_work);
1963}
1964
1965static void srpt_release_channel_work(struct work_struct *w)
1966{
1967 struct srpt_rdma_ch *ch;
1968 struct srpt_device *sdev;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001969 struct se_session *se_sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001970
1971 ch = container_of(w, struct srpt_rdma_ch, release_work);
Bart Van Asschef108f0f2016-02-11 11:06:14 -08001972 pr_debug("%s: %s-%d; release_done = %p\n", __func__, ch->sess_name,
1973 ch->qp->qp_num, ch->release_done);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001974
1975 sdev = ch->sport->sdev;
1976 BUG_ON(!sdev);
1977
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001978 se_sess = ch->sess;
1979 BUG_ON(!se_sess);
1980
Bart Van Assche88936252016-02-11 11:05:58 -08001981 target_sess_cmd_list_set_waiting(se_sess);
Joern Engelbe646c2d2013-05-15 00:44:07 -07001982 target_wait_for_sess_cmds(se_sess);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001983
1984 transport_deregister_session_configfs(se_sess);
1985 transport_deregister_session(se_sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001986 ch->sess = NULL;
1987
Nicholas Bellinger0b41d6c2013-09-18 12:48:27 -07001988 ib_destroy_cm_id(ch->cm_id);
1989
Bart Van Asschea42d9852011-10-14 01:30:46 +00001990 srpt_destroy_ch_ib(ch);
1991
1992 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
1993 ch->sport->sdev, ch->rq_size,
1994 ch->rsp_size, DMA_TO_DEVICE);
1995
1996 spin_lock_irq(&sdev->spinlock);
Bart Van Asschef108f0f2016-02-11 11:06:14 -08001997 list_del_init(&ch->list);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001998 if (ch->release_done)
1999 complete(ch->release_done);
Bart Van Asschef108f0f2016-02-11 11:06:14 -08002000 spin_unlock_irq(&sdev->spinlock);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002001
2002 wake_up(&sdev->ch_releaseQ);
2003
2004 kfree(ch);
2005}
2006
Bart Van Asschea42d9852011-10-14 01:30:46 +00002007/**
2008 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2009 *
2010 * Ownership of the cm_id is transferred to the target session if this
2011 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2012 */
2013static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2014 struct ib_cm_req_event_param *param,
2015 void *private_data)
2016{
2017 struct srpt_device *sdev = cm_id->context;
2018 struct srpt_port *sport = &sdev->port[param->port - 1];
2019 struct srp_login_req *req;
2020 struct srp_login_rsp *rsp;
2021 struct srp_login_rej *rej;
2022 struct ib_cm_rep_param *rep_param;
2023 struct srpt_rdma_ch *ch, *tmp_ch;
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002024 struct se_node_acl *se_acl;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002025 u32 it_iu_len;
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002026 int i, ret = 0;
2027 unsigned char *p;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002028
2029 WARN_ON_ONCE(irqs_disabled());
2030
2031 if (WARN_ON(!sdev || !private_data))
2032 return -EINVAL;
2033
2034 req = (struct srp_login_req *)private_data;
2035
2036 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2037
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002038 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2039 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2040 " (guid=0x%llx:0x%llx)\n",
2041 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2042 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2043 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2044 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2045 it_iu_len,
2046 param->port,
2047 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2048 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002049
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002050 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
2051 rej = kzalloc(sizeof(*rej), GFP_KERNEL);
2052 rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002053
2054 if (!rsp || !rej || !rep_param) {
2055 ret = -ENOMEM;
2056 goto out;
2057 }
2058
2059 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302060 rej->reason = cpu_to_be32(
2061 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002062 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002063 pr_err("rejected SRP_LOGIN_REQ because its"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002064 " length (%d bytes) is out of range (%d .. %d)\n",
2065 it_iu_len, 64, srp_max_req_size);
2066 goto reject;
2067 }
2068
2069 if (!sport->enabled) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302070 rej->reason = cpu_to_be32(
2071 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002072 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002073 pr_err("rejected SRP_LOGIN_REQ because the target port"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002074 " has not yet been enabled\n");
2075 goto reject;
2076 }
2077
2078 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2079 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2080
2081 spin_lock_irq(&sdev->spinlock);
2082
2083 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2084 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2085 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2086 && param->port == ch->sport->port
2087 && param->listen_id == ch->sport->sdev->cm_id
2088 && ch->cm_id) {
Bart Van Assche33912d72016-02-11 11:04:43 -08002089 if (ch->state != CH_CONNECTING
2090 && ch->state != CH_LIVE)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002091 continue;
2092
2093 /* found an existing channel */
2094 pr_debug("Found existing channel %s"
2095 " cm_id= %p state= %d\n",
Bart Van Assche33912d72016-02-11 11:04:43 -08002096 ch->sess_name, ch->cm_id, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002097
2098 __srpt_close_ch(ch);
2099
2100 rsp->rsp_flags =
2101 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2102 }
2103 }
2104
2105 spin_unlock_irq(&sdev->spinlock);
2106
2107 } else
2108 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2109
2110 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2111 || *(__be64 *)(req->target_port_id + 8) !=
2112 cpu_to_be64(srpt_service_guid)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302113 rej->reason = cpu_to_be32(
2114 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002115 ret = -ENOMEM;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002116 pr_err("rejected SRP_LOGIN_REQ because it"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002117 " has an invalid target port identifier.\n");
2118 goto reject;
2119 }
2120
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002121 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002122 if (!ch) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302123 rej->reason = cpu_to_be32(
2124 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002125 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002126 ret = -ENOMEM;
2127 goto reject;
2128 }
2129
2130 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2131 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2132 memcpy(ch->t_port_id, req->target_port_id, 16);
2133 ch->sport = &sdev->port[param->port - 1];
2134 ch->cm_id = cm_id;
2135 /*
2136 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2137 * for the SRP protocol to the command queue size.
2138 */
2139 ch->rq_size = SRPT_RQ_SIZE;
2140 spin_lock_init(&ch->spinlock);
2141 ch->state = CH_CONNECTING;
2142 INIT_LIST_HEAD(&ch->cmd_wait_list);
2143 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2144
2145 ch->ioctx_ring = (struct srpt_send_ioctx **)
2146 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2147 sizeof(*ch->ioctx_ring[0]),
2148 ch->rsp_size, DMA_TO_DEVICE);
2149 if (!ch->ioctx_ring)
2150 goto free_ch;
2151
2152 INIT_LIST_HEAD(&ch->free_list);
2153 for (i = 0; i < ch->rq_size; i++) {
2154 ch->ioctx_ring[i]->ch = ch;
2155 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2156 }
2157
2158 ret = srpt_create_ch_ib(ch);
2159 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302160 rej->reason = cpu_to_be32(
2161 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002162 pr_err("rejected SRP_LOGIN_REQ because creating"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002163 " a new RDMA channel failed.\n");
2164 goto free_ring;
2165 }
2166
2167 ret = srpt_ch_qp_rtr(ch, ch->qp);
2168 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302169 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002170 pr_err("rejected SRP_LOGIN_REQ because enabling"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002171 " RTR failed (error code = %d)\n", ret);
2172 goto destroy_ib;
2173 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002174
Bart Van Asschea42d9852011-10-14 01:30:46 +00002175 /*
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002176 * Use the initator port identifier as the session name, when
2177 * checking against se_node_acl->initiatorname[] this can be
2178 * with or without preceeding '0x'.
Bart Van Asschea42d9852011-10-14 01:30:46 +00002179 */
2180 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2181 be64_to_cpu(*(__be64 *)ch->i_port_id),
2182 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2183
2184 pr_debug("registering session %s\n", ch->sess_name);
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002185 p = &ch->sess_name[0];
Bart Van Asschea42d9852011-10-14 01:30:46 +00002186
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002187 ch->sess = transport_init_session(TARGET_PROT_NORMAL);
Dan Carpenter3af33632011-11-04 21:27:32 +03002188 if (IS_ERR(ch->sess)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302189 rej->reason = cpu_to_be32(
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002190 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002191 pr_debug("Failed to create session\n");
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002192 goto destroy_ib;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002193 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002194
2195try_again:
2196 se_acl = core_tpg_get_initiator_node_acl(&sport->port_tpg_1, p);
2197 if (!se_acl) {
2198 pr_info("Rejected login because no ACL has been"
2199 " configured yet for initiator %s.\n", ch->sess_name);
2200 /*
2201 * XXX: Hack to retry of ch->i_port_id without leading '0x'
2202 */
2203 if (p == &ch->sess_name[0]) {
2204 p += 2;
2205 goto try_again;
2206 }
2207 rej->reason = cpu_to_be32(
2208 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2209 transport_free_session(ch->sess);
2210 goto destroy_ib;
2211 }
2212 ch->sess->se_node_acl = se_acl;
2213
2214 transport_register_session(&sport->port_tpg_1, se_acl, ch->sess, ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002215
2216 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2217 ch->sess_name, ch->cm_id);
2218
2219 /* create srp_login_response */
2220 rsp->opcode = SRP_LOGIN_RSP;
2221 rsp->tag = req->tag;
2222 rsp->max_it_iu_len = req->req_it_iu_len;
2223 rsp->max_ti_iu_len = req->req_it_iu_len;
2224 ch->max_ti_iu_len = it_iu_len;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302225 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2226 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002227 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2228 atomic_set(&ch->req_lim, ch->rq_size);
2229 atomic_set(&ch->req_lim_delta, 0);
2230
2231 /* create cm reply */
2232 rep_param->qp_num = ch->qp->qp_num;
2233 rep_param->private_data = (void *)rsp;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002234 rep_param->private_data_len = sizeof(*rsp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002235 rep_param->rnr_retry_count = 7;
2236 rep_param->flow_control = 1;
2237 rep_param->failover_accepted = 0;
2238 rep_param->srq = 1;
2239 rep_param->responder_resources = 4;
2240 rep_param->initiator_depth = 4;
2241
2242 ret = ib_send_cm_rep(cm_id, rep_param);
2243 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002244 pr_err("sending SRP_LOGIN_REQ response failed"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002245 " (error code = %d)\n", ret);
2246 goto release_channel;
2247 }
2248
2249 spin_lock_irq(&sdev->spinlock);
2250 list_add_tail(&ch->list, &sdev->rch_list);
2251 spin_unlock_irq(&sdev->spinlock);
2252
2253 goto out;
2254
2255release_channel:
2256 srpt_set_ch_state(ch, CH_RELEASING);
2257 transport_deregister_session_configfs(ch->sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002258 transport_deregister_session(ch->sess);
2259 ch->sess = NULL;
2260
2261destroy_ib:
2262 srpt_destroy_ch_ib(ch);
2263
2264free_ring:
2265 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2266 ch->sport->sdev, ch->rq_size,
2267 ch->rsp_size, DMA_TO_DEVICE);
2268free_ch:
2269 kfree(ch);
2270
2271reject:
2272 rej->opcode = SRP_LOGIN_REJ;
2273 rej->tag = req->tag;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302274 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2275 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002276
2277 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002278 (void *)rej, sizeof(*rej));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002279
2280out:
2281 kfree(rep_param);
2282 kfree(rsp);
2283 kfree(rej);
2284
2285 return ret;
2286}
2287
2288static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2289{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002290 pr_info("Received IB REJ for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002291 srpt_drain_channel(cm_id);
2292}
2293
2294/**
2295 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2296 *
2297 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2298 * and that the recipient may begin transmitting (RTU = ready to use).
2299 */
2300static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2301{
2302 struct srpt_rdma_ch *ch;
2303 int ret;
2304
2305 ch = srpt_find_channel(cm_id->context, cm_id);
2306 BUG_ON(!ch);
2307
Bart Van Asschef130c222016-02-11 11:05:38 -08002308 if (srpt_set_ch_state(ch, CH_LIVE)) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00002309 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2310
2311 ret = srpt_ch_qp_rts(ch, ch->qp);
2312
2313 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2314 wait_list) {
2315 list_del(&ioctx->wait_list);
2316 srpt_handle_new_iu(ch, ioctx, NULL);
2317 }
2318 if (ret)
2319 srpt_close_ch(ch);
2320 }
2321}
2322
2323static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2324{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002325 pr_info("Received IB TimeWait exit for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002326 srpt_drain_channel(cm_id);
2327}
2328
2329static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2330{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002331 pr_info("Received IB REP error for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002332 srpt_drain_channel(cm_id);
2333}
2334
2335/**
2336 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2337 */
2338static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2339{
2340 struct srpt_rdma_ch *ch;
2341 unsigned long flags;
2342 bool send_drep = false;
2343
2344 ch = srpt_find_channel(cm_id->context, cm_id);
2345 BUG_ON(!ch);
2346
Bart Van Assche33912d72016-02-11 11:04:43 -08002347 pr_debug("cm_id= %p ch->state= %d\n", cm_id, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002348
2349 spin_lock_irqsave(&ch->spinlock, flags);
2350 switch (ch->state) {
2351 case CH_CONNECTING:
2352 case CH_LIVE:
2353 send_drep = true;
2354 ch->state = CH_DISCONNECTING;
2355 break;
2356 case CH_DISCONNECTING:
2357 case CH_DRAINING:
2358 case CH_RELEASING:
2359 WARN(true, "unexpected channel state %d\n", ch->state);
2360 break;
2361 }
2362 spin_unlock_irqrestore(&ch->spinlock, flags);
2363
2364 if (send_drep) {
2365 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002366 pr_err("Sending IB DREP failed.\n");
2367 pr_info("Received DREQ and sent DREP for session %s.\n",
2368 ch->sess_name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002369 }
2370}
2371
2372/**
2373 * srpt_cm_drep_recv() - Process reception of a DREP message.
2374 */
2375static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2376{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002377 pr_info("Received InfiniBand DREP message for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002378 srpt_drain_channel(cm_id);
2379}
2380
2381/**
2382 * srpt_cm_handler() - IB connection manager callback function.
2383 *
2384 * A non-zero return value will cause the caller destroy the CM ID.
2385 *
2386 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2387 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2388 * a non-zero value in any other case will trigger a race with the
2389 * ib_destroy_cm_id() call in srpt_release_channel().
2390 */
2391static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2392{
2393 int ret;
2394
2395 ret = 0;
2396 switch (event->event) {
2397 case IB_CM_REQ_RECEIVED:
2398 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2399 event->private_data);
2400 break;
2401 case IB_CM_REJ_RECEIVED:
2402 srpt_cm_rej_recv(cm_id);
2403 break;
2404 case IB_CM_RTU_RECEIVED:
2405 case IB_CM_USER_ESTABLISHED:
2406 srpt_cm_rtu_recv(cm_id);
2407 break;
2408 case IB_CM_DREQ_RECEIVED:
2409 srpt_cm_dreq_recv(cm_id);
2410 break;
2411 case IB_CM_DREP_RECEIVED:
2412 srpt_cm_drep_recv(cm_id);
2413 break;
2414 case IB_CM_TIMEWAIT_EXIT:
2415 srpt_cm_timewait_exit(cm_id);
2416 break;
2417 case IB_CM_REP_ERROR:
2418 srpt_cm_rep_error(cm_id);
2419 break;
2420 case IB_CM_DREQ_ERROR:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002421 pr_info("Received IB DREQ ERROR event.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002422 break;
2423 case IB_CM_MRA_RECEIVED:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002424 pr_info("Received IB MRA event\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002425 break;
2426 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002427 pr_err("received unrecognized IB CM event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002428 break;
2429 }
2430
2431 return ret;
2432}
2433
2434/**
2435 * srpt_perform_rdmas() - Perform IB RDMA.
2436 *
2437 * Returns zero upon success or a negative number upon failure.
2438 */
2439static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2440 struct srpt_send_ioctx *ioctx)
2441{
Bart Van Asschea42d9852011-10-14 01:30:46 +00002442 struct ib_send_wr *bad_wr;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002443 int sq_wr_avail, ret, i;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002444 enum dma_data_direction dir;
2445 const int n_rdma = ioctx->n_rdma;
2446
2447 dir = ioctx->cmd.data_direction;
2448 if (dir == DMA_TO_DEVICE) {
2449 /* write */
2450 ret = -ENOMEM;
2451 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2452 if (sq_wr_avail < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002453 pr_warn("IB send queue full (needed %d)\n",
2454 n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002455 goto out;
2456 }
2457 }
2458
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002459 for (i = 0; i < n_rdma; i++) {
2460 struct ib_send_wr *wr = &ioctx->rdma_wrs[i].wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002461
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002462 wr->opcode = (dir == DMA_FROM_DEVICE) ?
2463 IB_WR_RDMA_WRITE : IB_WR_RDMA_READ;
2464
2465 if (i == n_rdma - 1) {
2466 /* only get completion event for the last rdma read */
2467 if (dir == DMA_TO_DEVICE) {
2468 wr->send_flags = IB_SEND_SIGNALED;
2469 ioctx->rdma_cqe.done = srpt_rdma_read_done;
2470 } else {
2471 ioctx->rdma_cqe.done = srpt_rdma_write_done;
2472 }
2473 wr->wr_cqe = &ioctx->rdma_cqe;
2474 wr->next = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002475 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002476 wr->wr_cqe = NULL;
2477 wr->next = &ioctx->rdma_wrs[i + 1].wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002478 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00002479 }
2480
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002481 ret = ib_post_send(ch->qp, &ioctx->rdma_wrs->wr, &bad_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002482 if (ret)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002483 pr_err("%s[%d]: ib_post_send() returned %d for %d/%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002484 __func__, __LINE__, ret, i, n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002485out:
2486 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2487 atomic_add(n_rdma, &ch->sq_wr_avail);
2488 return ret;
2489}
2490
2491/**
2492 * srpt_xfer_data() - Start data transfer from initiator to target.
2493 */
2494static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2495 struct srpt_send_ioctx *ioctx)
2496{
2497 int ret;
2498
2499 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2500 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002501 pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002502 goto out;
2503 }
2504
2505 ret = srpt_perform_rdmas(ch, ioctx);
2506 if (ret) {
2507 if (ret == -EAGAIN || ret == -ENOMEM)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002508 pr_info("%s[%d] queue full -- ret=%d\n",
2509 __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002510 else
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002511 pr_err("%s[%d] fatal error -- ret=%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002512 __func__, __LINE__, ret);
2513 goto out_unmap;
2514 }
2515
2516out:
2517 return ret;
2518out_unmap:
2519 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2520 goto out;
2521}
2522
2523static int srpt_write_pending_status(struct se_cmd *se_cmd)
2524{
2525 struct srpt_send_ioctx *ioctx;
2526
2527 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2528 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2529}
2530
2531/*
2532 * srpt_write_pending() - Start data transfer from initiator to target (write).
2533 */
2534static int srpt_write_pending(struct se_cmd *se_cmd)
2535{
2536 struct srpt_rdma_ch *ch;
2537 struct srpt_send_ioctx *ioctx;
2538 enum srpt_command_state new_state;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002539 int ret;
2540
2541 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2542
2543 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2544 WARN_ON(new_state == SRPT_STATE_DONE);
2545
2546 ch = ioctx->ch;
2547 BUG_ON(!ch);
2548
Bart Van Assche33912d72016-02-11 11:04:43 -08002549 switch (ch->state) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00002550 case CH_CONNECTING:
Bart Van Assche33912d72016-02-11 11:04:43 -08002551 WARN(true, "unexpected channel state %d\n", ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002552 ret = -EINVAL;
2553 goto out;
2554 case CH_LIVE:
2555 break;
2556 case CH_DISCONNECTING:
2557 case CH_DRAINING:
2558 case CH_RELEASING:
2559 pr_debug("cmd with tag %lld: channel disconnecting\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002560 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002561 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2562 ret = -EINVAL;
2563 goto out;
2564 }
2565 ret = srpt_xfer_data(ch, ioctx);
2566
2567out:
2568 return ret;
2569}
2570
2571static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2572{
2573 switch (tcm_mgmt_status) {
2574 case TMR_FUNCTION_COMPLETE:
2575 return SRP_TSK_MGMT_SUCCESS;
2576 case TMR_FUNCTION_REJECTED:
2577 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2578 }
2579 return SRP_TSK_MGMT_FAILED;
2580}
2581
2582/**
2583 * srpt_queue_response() - Transmits the response to a SCSI command.
2584 *
2585 * Callback function called by the TCM core. Must not block since it can be
2586 * invoked on the context of the IB completion handler.
2587 */
Joern Engelb79fafa2013-07-03 11:22:17 -04002588static void srpt_queue_response(struct se_cmd *cmd)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002589{
2590 struct srpt_rdma_ch *ch;
2591 struct srpt_send_ioctx *ioctx;
2592 enum srpt_command_state state;
2593 unsigned long flags;
2594 int ret;
2595 enum dma_data_direction dir;
2596 int resp_len;
2597 u8 srp_tm_status;
2598
Bart Van Asschea42d9852011-10-14 01:30:46 +00002599 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2600 ch = ioctx->ch;
2601 BUG_ON(!ch);
2602
2603 spin_lock_irqsave(&ioctx->spinlock, flags);
2604 state = ioctx->state;
2605 switch (state) {
2606 case SRPT_STATE_NEW:
2607 case SRPT_STATE_DATA_IN:
2608 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2609 break;
2610 case SRPT_STATE_MGMT:
2611 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2612 break;
2613 default:
2614 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2615 ch, ioctx->ioctx.index, ioctx->state);
2616 break;
2617 }
2618 spin_unlock_irqrestore(&ioctx->spinlock, flags);
2619
2620 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
2621 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
2622 atomic_inc(&ch->req_lim_delta);
2623 srpt_abort_cmd(ioctx);
Joern Engelb79fafa2013-07-03 11:22:17 -04002624 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002625 }
2626
2627 dir = ioctx->cmd.data_direction;
2628
2629 /* For read commands, transfer the data to the initiator. */
2630 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
2631 !ioctx->queue_status_only) {
2632 ret = srpt_xfer_data(ch, ioctx);
2633 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002634 pr_err("xfer_data failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002635 ioctx->cmd.tag);
Joern Engelb79fafa2013-07-03 11:22:17 -04002636 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002637 }
2638 }
2639
2640 if (state != SRPT_STATE_MGMT)
Bart Van Assche649ee052015-04-14 13:26:44 +02002641 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002642 cmd->scsi_status);
2643 else {
2644 srp_tm_status
2645 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2646 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
Bart Van Assche649ee052015-04-14 13:26:44 +02002647 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002648 }
2649 ret = srpt_post_send(ch, ioctx, resp_len);
2650 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002651 pr_err("sending cmd response failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002652 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002653 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2654 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02002655 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002656 }
Joern Engelb79fafa2013-07-03 11:22:17 -04002657}
Bart Van Asschea42d9852011-10-14 01:30:46 +00002658
Joern Engelb79fafa2013-07-03 11:22:17 -04002659static int srpt_queue_data_in(struct se_cmd *cmd)
2660{
2661 srpt_queue_response(cmd);
2662 return 0;
2663}
2664
2665static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2666{
2667 srpt_queue_response(cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002668}
2669
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002670static void srpt_aborted_task(struct se_cmd *cmd)
2671{
2672 struct srpt_send_ioctx *ioctx = container_of(cmd,
2673 struct srpt_send_ioctx, cmd);
2674
2675 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
2676}
2677
Bart Van Asschea42d9852011-10-14 01:30:46 +00002678static int srpt_queue_status(struct se_cmd *cmd)
2679{
2680 struct srpt_send_ioctx *ioctx;
2681
2682 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2683 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2684 if (cmd->se_cmd_flags &
2685 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2686 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2687 ioctx->queue_status_only = true;
Joern Engelb79fafa2013-07-03 11:22:17 -04002688 srpt_queue_response(cmd);
2689 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002690}
2691
2692static void srpt_refresh_port_work(struct work_struct *work)
2693{
2694 struct srpt_port *sport = container_of(work, struct srpt_port, work);
2695
2696 srpt_refresh_port(sport);
2697}
2698
2699static int srpt_ch_list_empty(struct srpt_device *sdev)
2700{
2701 int res;
2702
2703 spin_lock_irq(&sdev->spinlock);
2704 res = list_empty(&sdev->rch_list);
2705 spin_unlock_irq(&sdev->spinlock);
2706
2707 return res;
2708}
2709
2710/**
2711 * srpt_release_sdev() - Free the channel resources associated with a target.
2712 */
2713static int srpt_release_sdev(struct srpt_device *sdev)
2714{
2715 struct srpt_rdma_ch *ch, *tmp_ch;
2716 int res;
2717
2718 WARN_ON_ONCE(irqs_disabled());
2719
2720 BUG_ON(!sdev);
2721
2722 spin_lock_irq(&sdev->spinlock);
2723 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
2724 __srpt_close_ch(ch);
2725 spin_unlock_irq(&sdev->spinlock);
2726
2727 res = wait_event_interruptible(sdev->ch_releaseQ,
2728 srpt_ch_list_empty(sdev));
2729 if (res)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002730 pr_err("%s: interrupted.\n", __func__);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002731
2732 return 0;
2733}
2734
2735static struct srpt_port *__srpt_lookup_port(const char *name)
2736{
2737 struct ib_device *dev;
2738 struct srpt_device *sdev;
2739 struct srpt_port *sport;
2740 int i;
2741
2742 list_for_each_entry(sdev, &srpt_dev_list, list) {
2743 dev = sdev->device;
2744 if (!dev)
2745 continue;
2746
2747 for (i = 0; i < dev->phys_port_cnt; i++) {
2748 sport = &sdev->port[i];
2749
2750 if (!strcmp(sport->port_guid, name))
2751 return sport;
2752 }
2753 }
2754
2755 return NULL;
2756}
2757
2758static struct srpt_port *srpt_lookup_port(const char *name)
2759{
2760 struct srpt_port *sport;
2761
2762 spin_lock(&srpt_dev_lock);
2763 sport = __srpt_lookup_port(name);
2764 spin_unlock(&srpt_dev_lock);
2765
2766 return sport;
2767}
2768
2769/**
2770 * srpt_add_one() - Infiniband device addition callback function.
2771 */
2772static void srpt_add_one(struct ib_device *device)
2773{
2774 struct srpt_device *sdev;
2775 struct srpt_port *sport;
2776 struct ib_srq_init_attr srq_attr;
2777 int i;
2778
2779 pr_debug("device = %p, device->dma_ops = %p\n", device,
2780 device->dma_ops);
2781
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002782 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002783 if (!sdev)
2784 goto err;
2785
2786 sdev->device = device;
2787 INIT_LIST_HEAD(&sdev->rch_list);
2788 init_waitqueue_head(&sdev->ch_releaseQ);
2789 spin_lock_init(&sdev->spinlock);
2790
Bart Van Asschea42d9852011-10-14 01:30:46 +00002791 sdev->pd = ib_alloc_pd(device);
2792 if (IS_ERR(sdev->pd))
2793 goto free_dev;
2794
Or Gerlitz4a061b22015-12-18 10:59:46 +02002795 sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002796
2797 srq_attr.event_handler = srpt_srq_event;
2798 srq_attr.srq_context = (void *)sdev;
2799 srq_attr.attr.max_wr = sdev->srq_size;
2800 srq_attr.attr.max_sge = 1;
2801 srq_attr.attr.srq_limit = 0;
Roland Dreier6f360332012-04-12 07:51:08 -07002802 srq_attr.srq_type = IB_SRQT_BASIC;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002803
2804 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
2805 if (IS_ERR(sdev->srq))
Jason Gunthorpe5a783952015-07-30 17:22:24 -06002806 goto err_pd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002807
2808 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
Or Gerlitz4a061b22015-12-18 10:59:46 +02002809 __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002810 device->name);
2811
2812 if (!srpt_service_guid)
2813 srpt_service_guid = be64_to_cpu(device->node_guid);
2814
2815 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
2816 if (IS_ERR(sdev->cm_id))
2817 goto err_srq;
2818
2819 /* print out target login information */
2820 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
2821 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
2822 srpt_service_guid, srpt_service_guid);
2823
2824 /*
2825 * We do not have a consistent service_id (ie. also id_ext of target_id)
2826 * to identify this target. We currently use the guid of the first HCA
2827 * in the system as service_id; therefore, the target_id will change
2828 * if this HCA is gone bad and replaced by different HCA
2829 */
Haggai Eran73fec7f2015-07-30 17:50:26 +03002830 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
Bart Van Asschea42d9852011-10-14 01:30:46 +00002831 goto err_cm;
2832
2833 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
2834 srpt_event_handler);
2835 if (ib_register_event_handler(&sdev->event_handler))
2836 goto err_cm;
2837
2838 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
2839 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
2840 sizeof(*sdev->ioctx_ring[0]),
2841 srp_max_req_size, DMA_FROM_DEVICE);
2842 if (!sdev->ioctx_ring)
2843 goto err_event;
2844
2845 for (i = 0; i < sdev->srq_size; ++i)
2846 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
2847
Roland Dreierf2250662012-02-02 12:55:58 -08002848 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002849
2850 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
2851 sport = &sdev->port[i - 1];
2852 sport->sdev = sdev;
2853 sport->port = i;
2854 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
2855 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
2856 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
2857 INIT_WORK(&sport->work, srpt_refresh_port_work);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002858
2859 if (srpt_refresh_port(sport)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002860 pr_err("MAD registration failed for %s-%d.\n",
Bart Van Asschef68cba4e92016-02-11 11:04:20 -08002861 sdev->device->name, i);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002862 goto err_ring;
2863 }
2864 snprintf(sport->port_guid, sizeof(sport->port_guid),
2865 "0x%016llx%016llx",
2866 be64_to_cpu(sport->gid.global.subnet_prefix),
2867 be64_to_cpu(sport->gid.global.interface_id));
2868 }
2869
2870 spin_lock(&srpt_dev_lock);
2871 list_add_tail(&sdev->list, &srpt_dev_list);
2872 spin_unlock(&srpt_dev_lock);
2873
2874out:
2875 ib_set_client_data(device, &srpt_client, sdev);
2876 pr_debug("added %s.\n", device->name);
2877 return;
2878
2879err_ring:
2880 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2881 sdev->srq_size, srp_max_req_size,
2882 DMA_FROM_DEVICE);
2883err_event:
2884 ib_unregister_event_handler(&sdev->event_handler);
2885err_cm:
2886 ib_destroy_cm_id(sdev->cm_id);
2887err_srq:
2888 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002889err_pd:
2890 ib_dealloc_pd(sdev->pd);
2891free_dev:
2892 kfree(sdev);
2893err:
2894 sdev = NULL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002895 pr_info("%s(%s) failed.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002896 goto out;
2897}
2898
2899/**
2900 * srpt_remove_one() - InfiniBand device removal callback function.
2901 */
Haggai Eran7c1eb452015-07-30 17:50:14 +03002902static void srpt_remove_one(struct ib_device *device, void *client_data)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002903{
Haggai Eran7c1eb452015-07-30 17:50:14 +03002904 struct srpt_device *sdev = client_data;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002905 int i;
2906
Bart Van Asschea42d9852011-10-14 01:30:46 +00002907 if (!sdev) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002908 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002909 return;
2910 }
2911
2912 srpt_unregister_mad_agent(sdev);
2913
2914 ib_unregister_event_handler(&sdev->event_handler);
2915
2916 /* Cancel any work queued by the just unregistered IB event handler. */
2917 for (i = 0; i < sdev->device->phys_port_cnt; i++)
2918 cancel_work_sync(&sdev->port[i].work);
2919
2920 ib_destroy_cm_id(sdev->cm_id);
2921
2922 /*
2923 * Unregistering a target must happen after destroying sdev->cm_id
2924 * such that no new SRP_LOGIN_REQ information units can arrive while
2925 * destroying the target.
2926 */
2927 spin_lock(&srpt_dev_lock);
2928 list_del(&sdev->list);
2929 spin_unlock(&srpt_dev_lock);
2930 srpt_release_sdev(sdev);
2931
2932 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002933 ib_dealloc_pd(sdev->pd);
2934
2935 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2936 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
2937 sdev->ioctx_ring = NULL;
2938 kfree(sdev);
2939}
2940
2941static struct ib_client srpt_client = {
2942 .name = DRV_NAME,
2943 .add = srpt_add_one,
2944 .remove = srpt_remove_one
2945};
2946
2947static int srpt_check_true(struct se_portal_group *se_tpg)
2948{
2949 return 1;
2950}
2951
2952static int srpt_check_false(struct se_portal_group *se_tpg)
2953{
2954 return 0;
2955}
2956
2957static char *srpt_get_fabric_name(void)
2958{
2959 return "srpt";
2960}
2961
Bart Van Asschea42d9852011-10-14 01:30:46 +00002962static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
2963{
2964 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
2965
2966 return sport->port_guid;
2967}
2968
2969static u16 srpt_get_tag(struct se_portal_group *tpg)
2970{
2971 return 1;
2972}
2973
Bart Van Asschea42d9852011-10-14 01:30:46 +00002974static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
2975{
2976 return 1;
2977}
2978
2979static void srpt_release_cmd(struct se_cmd *se_cmd)
2980{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002981 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
2982 struct srpt_send_ioctx, cmd);
2983 struct srpt_rdma_ch *ch = ioctx->ch;
2984 unsigned long flags;
2985
2986 WARN_ON(ioctx->state != SRPT_STATE_DONE);
2987 WARN_ON(ioctx->mapped_sg_count != 0);
2988
2989 if (ioctx->n_rbuf > 1) {
2990 kfree(ioctx->rbufs);
2991 ioctx->rbufs = NULL;
2992 ioctx->n_rbuf = 0;
2993 }
2994
2995 spin_lock_irqsave(&ch->spinlock, flags);
2996 list_add(&ioctx->free_list, &ch->free_list);
2997 spin_unlock_irqrestore(&ch->spinlock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002998}
2999
3000/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003001 * srpt_close_session() - Forcibly close a session.
3002 *
3003 * Callback function invoked by the TCM core to clean up sessions associated
3004 * with a node ACL when the user invokes
3005 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3006 */
3007static void srpt_close_session(struct se_session *se_sess)
3008{
3009 DECLARE_COMPLETION_ONSTACK(release_done);
Bart Van Asschef108f0f2016-02-11 11:06:14 -08003010 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
3011 struct srpt_device *sdev = ch->sport->sdev;
3012 bool wait;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003013
Bart Van Asschef108f0f2016-02-11 11:06:14 -08003014 pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num,
3015 ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003016
Bart Van Asschea42d9852011-10-14 01:30:46 +00003017 spin_lock_irq(&sdev->spinlock);
3018 BUG_ON(ch->release_done);
3019 ch->release_done = &release_done;
Bart Van Asschef108f0f2016-02-11 11:06:14 -08003020 wait = !list_empty(&ch->list);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003021 __srpt_close_ch(ch);
3022 spin_unlock_irq(&sdev->spinlock);
3023
Bart Van Asschef108f0f2016-02-11 11:06:14 -08003024 if (!wait)
3025 return;
3026
3027 while (wait_for_completion_timeout(&release_done, 180 * HZ) == 0)
3028 pr_info("%s(%s-%d state %d): still waiting ...\n", __func__,
3029 ch->sess_name, ch->qp->qp_num, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003030}
3031
3032/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003033 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3034 *
3035 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3036 * This object represents an arbitrary integer used to uniquely identify a
3037 * particular attached remote initiator port to a particular SCSI target port
3038 * within a particular SCSI target device within a particular SCSI instance.
3039 */
3040static u32 srpt_sess_get_index(struct se_session *se_sess)
3041{
3042 return 0;
3043}
3044
3045static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3046{
3047}
3048
Bart Van Asschea42d9852011-10-14 01:30:46 +00003049/* Note: only used from inside debug printk's by the TCM core. */
3050static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3051{
3052 struct srpt_send_ioctx *ioctx;
3053
3054 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3055 return srpt_get_cmd_state(ioctx);
3056}
3057
Bart Van Asschea42d9852011-10-14 01:30:46 +00003058/**
3059 * srpt_parse_i_port_id() - Parse an initiator port ID.
3060 * @name: ASCII representation of a 128-bit initiator port ID.
3061 * @i_port_id: Binary 128-bit port ID.
3062 */
3063static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3064{
3065 const char *p;
3066 unsigned len, count, leading_zero_bytes;
3067 int ret, rc;
3068
3069 p = name;
Rasmus Villemoesb60459f2014-10-13 15:54:46 -07003070 if (strncasecmp(p, "0x", 2) == 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003071 p += 2;
3072 ret = -EINVAL;
3073 len = strlen(p);
3074 if (len % 2)
3075 goto out;
3076 count = min(len / 2, 16U);
3077 leading_zero_bytes = 16 - count;
3078 memset(i_port_id, 0, leading_zero_bytes);
3079 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3080 if (rc < 0)
3081 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3082 ret = 0;
3083out:
3084 return ret;
3085}
3086
3087/*
3088 * configfs callback function invoked for
3089 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3090 */
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003091static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003092{
Bart Van Asschea42d9852011-10-14 01:30:46 +00003093 u8 i_port_id[16];
3094
3095 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003096 pr_err("invalid initiator port ID %s\n", name);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003097 return -EINVAL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003098 }
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003099 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003100}
3101
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003102static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
3103 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003104{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003105 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003106 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3107
3108 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3109}
3110
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003111static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
3112 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003113{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003114 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003115 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3116 unsigned long val;
3117 int ret;
3118
Jingoo Han9d8abf42014-02-05 11:22:05 +09003119 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003120 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003121 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003122 return -EINVAL;
3123 }
3124 if (val > MAX_SRPT_RDMA_SIZE) {
3125 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3126 MAX_SRPT_RDMA_SIZE);
3127 return -EINVAL;
3128 }
3129 if (val < DEFAULT_MAX_RDMA_SIZE) {
3130 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3131 val, DEFAULT_MAX_RDMA_SIZE);
3132 return -EINVAL;
3133 }
3134 sport->port_attrib.srp_max_rdma_size = val;
3135
3136 return count;
3137}
3138
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003139static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
3140 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003141{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003142 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003143 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3144
3145 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3146}
3147
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003148static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
3149 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003150{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003151 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003152 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3153 unsigned long val;
3154 int ret;
3155
Jingoo Han9d8abf42014-02-05 11:22:05 +09003156 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003157 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003158 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003159 return -EINVAL;
3160 }
3161 if (val > MAX_SRPT_RSP_SIZE) {
3162 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3163 MAX_SRPT_RSP_SIZE);
3164 return -EINVAL;
3165 }
3166 if (val < MIN_MAX_RSP_SIZE) {
3167 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3168 MIN_MAX_RSP_SIZE);
3169 return -EINVAL;
3170 }
3171 sport->port_attrib.srp_max_rsp_size = val;
3172
3173 return count;
3174}
3175
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003176static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
3177 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003178{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003179 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003180 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3181
3182 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3183}
3184
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003185static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
3186 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003187{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003188 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003189 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3190 unsigned long val;
3191 int ret;
3192
Jingoo Han9d8abf42014-02-05 11:22:05 +09003193 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003194 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003195 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003196 return -EINVAL;
3197 }
3198 if (val > MAX_SRPT_SRQ_SIZE) {
3199 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3200 MAX_SRPT_SRQ_SIZE);
3201 return -EINVAL;
3202 }
3203 if (val < MIN_SRPT_SRQ_SIZE) {
3204 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3205 MIN_SRPT_SRQ_SIZE);
3206 return -EINVAL;
3207 }
3208 sport->port_attrib.srp_sq_size = val;
3209
3210 return count;
3211}
3212
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003213CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
3214CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
3215CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003216
3217static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003218 &srpt_tpg_attrib_attr_srp_max_rdma_size,
3219 &srpt_tpg_attrib_attr_srp_max_rsp_size,
3220 &srpt_tpg_attrib_attr_srp_sq_size,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003221 NULL,
3222};
3223
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003224static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003225{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003226 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003227 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3228
3229 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3230}
3231
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003232static ssize_t srpt_tpg_enable_store(struct config_item *item,
3233 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003234{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003235 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003236 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3237 unsigned long tmp;
3238 int ret;
3239
Jingoo Han9d8abf42014-02-05 11:22:05 +09003240 ret = kstrtoul(page, 0, &tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003241 if (ret < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003242 pr_err("Unable to extract srpt_tpg_store_enable\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003243 return -EINVAL;
3244 }
3245
3246 if ((tmp != 0) && (tmp != 1)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003247 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003248 return -EINVAL;
3249 }
3250 if (tmp == 1)
3251 sport->enabled = true;
3252 else
3253 sport->enabled = false;
3254
3255 return count;
3256}
3257
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003258CONFIGFS_ATTR(srpt_tpg_, enable);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003259
3260static struct configfs_attribute *srpt_tpg_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003261 &srpt_tpg_attr_enable,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003262 NULL,
3263};
3264
3265/**
3266 * configfs callback invoked for
3267 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3268 */
3269static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3270 struct config_group *group,
3271 const char *name)
3272{
3273 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3274 int res;
3275
3276 /* Initialize sport->port_wwn and sport->port_tpg_1 */
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07003277 res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003278 if (res)
3279 return ERR_PTR(res);
3280
3281 return &sport->port_tpg_1;
3282}
3283
3284/**
3285 * configfs callback invoked for
3286 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3287 */
3288static void srpt_drop_tpg(struct se_portal_group *tpg)
3289{
3290 struct srpt_port *sport = container_of(tpg,
3291 struct srpt_port, port_tpg_1);
3292
3293 sport->enabled = false;
3294 core_tpg_deregister(&sport->port_tpg_1);
3295}
3296
3297/**
3298 * configfs callback invoked for
3299 * mkdir /sys/kernel/config/target/$driver/$port
3300 */
3301static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3302 struct config_group *group,
3303 const char *name)
3304{
3305 struct srpt_port *sport;
3306 int ret;
3307
3308 sport = srpt_lookup_port(name);
3309 pr_debug("make_tport(%s)\n", name);
3310 ret = -EINVAL;
3311 if (!sport)
3312 goto err;
3313
3314 return &sport->port_wwn;
3315
3316err:
3317 return ERR_PTR(ret);
3318}
3319
3320/**
3321 * configfs callback invoked for
3322 * rmdir /sys/kernel/config/target/$driver/$port
3323 */
3324static void srpt_drop_tport(struct se_wwn *wwn)
3325{
3326 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3327
3328 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3329}
3330
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003331static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003332{
3333 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3334}
3335
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003336CONFIGFS_ATTR_RO(srpt_wwn_, version);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003337
3338static struct configfs_attribute *srpt_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003339 &srpt_wwn_attr_version,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003340 NULL,
3341};
3342
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003343static const struct target_core_fabric_ops srpt_template = {
3344 .module = THIS_MODULE,
3345 .name = "srpt",
Bart Van Asschea42d9852011-10-14 01:30:46 +00003346 .get_fabric_name = srpt_get_fabric_name,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003347 .tpg_get_wwn = srpt_get_fabric_wwn,
3348 .tpg_get_tag = srpt_get_tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003349 .tpg_check_demo_mode = srpt_check_false,
3350 .tpg_check_demo_mode_cache = srpt_check_true,
3351 .tpg_check_demo_mode_write_protect = srpt_check_true,
3352 .tpg_check_prod_mode_write_protect = srpt_check_false,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003353 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3354 .release_cmd = srpt_release_cmd,
3355 .check_stop_free = srpt_check_stop_free,
3356 .shutdown_session = srpt_shutdown_session,
3357 .close_session = srpt_close_session,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003358 .sess_get_index = srpt_sess_get_index,
3359 .sess_get_initiator_sid = NULL,
3360 .write_pending = srpt_write_pending,
3361 .write_pending_status = srpt_write_pending_status,
3362 .set_default_node_attributes = srpt_set_default_node_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003363 .get_cmd_state = srpt_get_tcm_cmd_state,
Joern Engelb79fafa2013-07-03 11:22:17 -04003364 .queue_data_in = srpt_queue_data_in,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003365 .queue_status = srpt_queue_status,
Joern Engelb79fafa2013-07-03 11:22:17 -04003366 .queue_tm_rsp = srpt_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003367 .aborted_task = srpt_aborted_task,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003368 /*
3369 * Setup function pointers for generic logic in
3370 * target_core_fabric_configfs.c
3371 */
3372 .fabric_make_wwn = srpt_make_tport,
3373 .fabric_drop_wwn = srpt_drop_tport,
3374 .fabric_make_tpg = srpt_make_tpg,
3375 .fabric_drop_tpg = srpt_drop_tpg,
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003376 .fabric_init_nodeacl = srpt_init_nodeacl,
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003377
3378 .tfc_wwn_attrs = srpt_wwn_attrs,
3379 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3380 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003381};
3382
3383/**
3384 * srpt_init_module() - Kernel module initialization.
3385 *
3386 * Note: Since ib_register_client() registers callback functions, and since at
3387 * least one of these callback functions (srpt_add_one()) calls target core
3388 * functions, this driver must be registered with the target core before
3389 * ib_register_client() is called.
3390 */
3391static int __init srpt_init_module(void)
3392{
3393 int ret;
3394
3395 ret = -EINVAL;
3396 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003397 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003398 " srp_max_req_size -- must be at least %d.\n",
3399 srp_max_req_size, MIN_MAX_REQ_SIZE);
3400 goto out;
3401 }
3402
3403 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3404 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003405 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003406 " srpt_srq_size -- must be in the range [%d..%d].\n",
3407 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3408 goto out;
3409 }
3410
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003411 ret = target_register_template(&srpt_template);
3412 if (ret)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003413 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003414
3415 ret = ib_register_client(&srpt_client);
3416 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003417 pr_err("couldn't register IB client\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003418 goto out_unregister_target;
3419 }
3420
3421 return 0;
3422
3423out_unregister_target:
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003424 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003425out:
3426 return ret;
3427}
3428
3429static void __exit srpt_cleanup_module(void)
3430{
3431 ib_unregister_client(&srpt_client);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003432 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003433}
3434
3435module_init(srpt_init_module);
3436module_exit(srpt_cleanup_module);