blob: 5b0fbc1bfce26023638eea6fdd1c3ce5a872f60d [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
1270 * the ib_srpt driver, change the state to the next state. Changing
1271 * the state of the command from SRPT_STATE_NEED_DATA to
1272 * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
1273 * function a second time.
1274 */
1275
1276 spin_lock_irqsave(&ioctx->spinlock, flags);
1277 state = ioctx->state;
1278 switch (state) {
1279 case SRPT_STATE_NEED_DATA:
1280 ioctx->state = SRPT_STATE_DATA_IN;
1281 break;
1282 case SRPT_STATE_DATA_IN:
1283 case SRPT_STATE_CMD_RSP_SENT:
1284 case SRPT_STATE_MGMT_RSP_SENT:
1285 ioctx->state = SRPT_STATE_DONE;
1286 break;
1287 default:
1288 break;
1289 }
1290 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1291
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001292 if (state == SRPT_STATE_DONE) {
1293 struct srpt_rdma_ch *ch = ioctx->ch;
1294
1295 BUG_ON(ch->sess == NULL);
1296
Bart Van Asscheafc16602015-04-27 13:52:36 +02001297 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001298 goto out;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001299 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001300
1301 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
Bart Van Assche649ee052015-04-14 13:26:44 +02001302 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001303
1304 switch (state) {
1305 case SRPT_STATE_NEW:
1306 case SRPT_STATE_DATA_IN:
1307 case SRPT_STATE_MGMT:
1308 /*
1309 * Do nothing - defer abort processing until
1310 * srpt_queue_response() is invoked.
1311 */
1312 WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
1313 break;
1314 case SRPT_STATE_NEED_DATA:
1315 /* DMA_TO_DEVICE (write) - RDMA read error. */
Christoph Hellwige672a472012-07-08 15:58:43 -04001316
1317 /* XXX(hch): this is a horrible layering violation.. */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001318 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
Christoph Hellwige672a472012-07-08 15:58:43 -04001319 ioctx->cmd.transport_state &= ~CMD_T_ACTIVE;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001320 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001321 break;
1322 case SRPT_STATE_CMD_RSP_SENT:
1323 /*
1324 * SRP_RSP sending failed or the SRP_RSP send completion has
1325 * not been received in time.
1326 */
1327 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001328 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001329 break;
1330 case SRPT_STATE_MGMT_RSP_SENT:
1331 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001332 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001333 break;
1334 default:
Grant Grundler532ec6f2013-03-26 21:48:28 +00001335 WARN(1, "Unexpected command state (%d)", state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001336 break;
1337 }
1338
1339out:
1340 return state;
1341}
1342
1343/**
Christoph Hellwige672a472012-07-08 15:58:43 -04001344 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1345 * the data that has been transferred via IB RDMA had to be postponed until the
Masanari Iida142ad5d2012-08-10 00:07:58 +00001346 * check_stop_free() callback. None of this is necessary anymore and needs to
Christoph Hellwige672a472012-07-08 15:58:43 -04001347 * be cleaned up.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001348 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001349static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001350{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001351 struct srpt_rdma_ch *ch = cq->cq_context;
1352 struct srpt_send_ioctx *ioctx =
Bart Van Assche19f57292015-12-31 09:56:43 +01001353 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001354
Bart Van Asschea42d9852011-10-14 01:30:46 +00001355 WARN_ON(ioctx->n_rdma <= 0);
1356 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1357
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001358 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1359 pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
1360 ioctx, wc->status);
1361 srpt_abort_cmd(ioctx);
1362 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001363 }
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001364
1365 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1366 SRPT_STATE_DATA_IN))
1367 target_execute_cmd(&ioctx->cmd);
1368 else
1369 pr_err("%s[%d]: wrong state = %d\n", __func__,
1370 __LINE__, srpt_get_cmd_state(ioctx));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001371}
1372
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001373static void srpt_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001374{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001375 struct srpt_send_ioctx *ioctx =
Bart Van Assche19f57292015-12-31 09:56:43 +01001376 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001377
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001378 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1379 pr_info("RDMA_WRITE for ioctx 0x%p failed with status %d\n",
1380 ioctx, wc->status);
1381 srpt_abort_cmd(ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001382 }
1383}
1384
1385/**
1386 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1387 * @ch: RDMA channel through which the request has been received.
1388 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1389 * be built in the buffer ioctx->buf points at and hence this function will
1390 * overwrite the request data.
1391 * @tag: tag of the request for which this response is being generated.
1392 * @status: value for the STATUS field of the SRP_RSP information unit.
1393 *
1394 * Returns the size in bytes of the SRP_RSP response.
1395 *
1396 * An SRP_RSP response contains a SCSI status or service response. See also
1397 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1398 * response. See also SPC-2 for more information about sense data.
1399 */
1400static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1401 struct srpt_send_ioctx *ioctx, u64 tag,
1402 int status)
1403{
1404 struct srp_rsp *srp_rsp;
1405 const u8 *sense_data;
1406 int sense_data_len, max_sense_len;
1407
1408 /*
1409 * The lowest bit of all SAM-3 status codes is zero (see also
1410 * paragraph 5.3 in SAM-3).
1411 */
1412 WARN_ON(status & 1);
1413
1414 srp_rsp = ioctx->ioctx.buf;
1415 BUG_ON(!srp_rsp);
1416
1417 sense_data = ioctx->sense_data;
1418 sense_data_len = ioctx->cmd.scsi_sense_length;
1419 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1420
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08001421 memset(srp_rsp, 0, sizeof(*srp_rsp));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001422 srp_rsp->opcode = SRP_RSP;
1423 srp_rsp->req_lim_delta =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301424 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001425 srp_rsp->tag = tag;
1426 srp_rsp->status = status;
1427
1428 if (sense_data_len) {
1429 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1430 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1431 if (sense_data_len > max_sense_len) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001432 pr_warn("truncated sense data from %d to %d"
1433 " bytes\n", sense_data_len, max_sense_len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001434 sense_data_len = max_sense_len;
1435 }
1436
1437 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1438 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1439 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1440 }
1441
1442 return sizeof(*srp_rsp) + sense_data_len;
1443}
1444
1445/**
1446 * srpt_build_tskmgmt_rsp() - Build a task management response.
1447 * @ch: RDMA channel through which the request has been received.
1448 * @ioctx: I/O context in which the SRP_RSP response will be built.
1449 * @rsp_code: RSP_CODE that will be stored in the response.
1450 * @tag: Tag of the request for which this response is being generated.
1451 *
1452 * Returns the size in bytes of the SRP_RSP response.
1453 *
1454 * An SRP_RSP response contains a SCSI status or service response. See also
1455 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1456 * response.
1457 */
1458static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1459 struct srpt_send_ioctx *ioctx,
1460 u8 rsp_code, u64 tag)
1461{
1462 struct srp_rsp *srp_rsp;
1463 int resp_data_len;
1464 int resp_len;
1465
Jack Wangc807f642013-09-30 10:09:05 +02001466 resp_data_len = 4;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001467 resp_len = sizeof(*srp_rsp) + resp_data_len;
1468
1469 srp_rsp = ioctx->ioctx.buf;
1470 BUG_ON(!srp_rsp);
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08001471 memset(srp_rsp, 0, sizeof(*srp_rsp));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001472
1473 srp_rsp->opcode = SRP_RSP;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301474 srp_rsp->req_lim_delta =
1475 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001476 srp_rsp->tag = tag;
1477
Jack Wangc807f642013-09-30 10:09:05 +02001478 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1479 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1480 srp_rsp->data[3] = rsp_code;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001481
1482 return resp_len;
1483}
1484
Bart Van Asschea42d9852011-10-14 01:30:46 +00001485static int srpt_check_stop_free(struct se_cmd *cmd)
1486{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001487 struct srpt_send_ioctx *ioctx = container_of(cmd,
1488 struct srpt_send_ioctx, cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001489
Bart Van Asscheafc16602015-04-27 13:52:36 +02001490 return target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001491}
1492
1493/**
1494 * srpt_handle_cmd() - Process SRP_CMD.
1495 */
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001496static void srpt_handle_cmd(struct srpt_rdma_ch *ch,
1497 struct srpt_recv_ioctx *recv_ioctx,
1498 struct srpt_send_ioctx *send_ioctx)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001499{
1500 struct se_cmd *cmd;
1501 struct srp_cmd *srp_cmd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001502 u64 data_len;
1503 enum dma_data_direction dir;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001504 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001505
1506 BUG_ON(!send_ioctx);
1507
1508 srp_cmd = recv_ioctx->ioctx.buf;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001509 cmd = &send_ioctx->cmd;
Bart Van Assche649ee052015-04-14 13:26:44 +02001510 cmd->tag = srp_cmd->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001511
1512 switch (srp_cmd->task_attr) {
1513 case SRP_CMD_SIMPLE_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001514 cmd->sam_task_attr = TCM_SIMPLE_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001515 break;
1516 case SRP_CMD_ORDERED_Q:
1517 default:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001518 cmd->sam_task_attr = TCM_ORDERED_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001519 break;
1520 case SRP_CMD_HEAD_OF_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001521 cmd->sam_task_attr = TCM_HEAD_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001522 break;
1523 case SRP_CMD_ACA:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001524 cmd->sam_task_attr = TCM_ACA_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001525 break;
1526 }
1527
Christoph Hellwigde103c92012-11-06 12:24:09 -08001528 if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001529 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001530 srp_cmd->tag);
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001531 goto release_ioctx;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001532 }
1533
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001534 rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
Bart Van Asschee1dd4132016-02-11 11:05:19 -08001535 &send_ioctx->sense_data[0],
1536 scsilun_to_int(&srp_cmd->lun), data_len,
1537 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001538 if (rc != 0) {
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001539 pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc,
1540 srp_cmd->tag);
1541 goto release_ioctx;
Nicholas Bellinger187e70a2012-03-17 20:12:36 -07001542 }
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001543 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001544
Bart Van Assche2c7f37f2016-02-11 11:06:55 -08001545release_ioctx:
1546 send_ioctx->state = SRPT_STATE_DONE;
1547 srpt_release_cmd(cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001548}
1549
Bart Van Asschea42d9852011-10-14 01:30:46 +00001550static int srp_tmr_to_tcm(int fn)
1551{
1552 switch (fn) {
1553 case SRP_TSK_ABORT_TASK:
1554 return TMR_ABORT_TASK;
1555 case SRP_TSK_ABORT_TASK_SET:
1556 return TMR_ABORT_TASK_SET;
1557 case SRP_TSK_CLEAR_TASK_SET:
1558 return TMR_CLEAR_TASK_SET;
1559 case SRP_TSK_LUN_RESET:
1560 return TMR_LUN_RESET;
1561 case SRP_TSK_CLEAR_ACA:
1562 return TMR_CLEAR_ACA;
1563 default:
1564 return -1;
1565 }
1566}
1567
1568/**
1569 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1570 *
1571 * Returns 0 if and only if the request will be processed by the target core.
1572 *
1573 * For more information about SRP_TSK_MGMT information units, see also section
1574 * 6.7 in the SRP r16a document.
1575 */
1576static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1577 struct srpt_recv_ioctx *recv_ioctx,
1578 struct srpt_send_ioctx *send_ioctx)
1579{
1580 struct srp_tsk_mgmt *srp_tsk;
1581 struct se_cmd *cmd;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001582 struct se_session *sess = ch->sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001583 int tcm_tmr;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001584 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001585
1586 BUG_ON(!send_ioctx);
1587
1588 srp_tsk = recv_ioctx->ioctx.buf;
1589 cmd = &send_ioctx->cmd;
1590
1591 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1592 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1593 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1594
1595 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
Bart Van Assche649ee052015-04-14 13:26:44 +02001596 send_ioctx->cmd.tag = srp_tsk->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001597 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
Bart Van Asschee1dd4132016-02-11 11:05:19 -08001598 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL,
1599 scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr,
1600 GFP_KERNEL, srp_tsk->task_tag,
1601 TARGET_SCF_ACK_KREF);
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001602 if (rc != 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001603 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001604 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001605 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001606 return;
1607fail:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001608 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001609}
1610
1611/**
1612 * srpt_handle_new_iu() - Process a newly received information unit.
1613 * @ch: RDMA channel through which the information unit has been received.
1614 * @ioctx: SRPT I/O context associated with the information unit.
1615 */
1616static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1617 struct srpt_recv_ioctx *recv_ioctx,
1618 struct srpt_send_ioctx *send_ioctx)
1619{
1620 struct srp_cmd *srp_cmd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001621
1622 BUG_ON(!ch);
1623 BUG_ON(!recv_ioctx);
1624
1625 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1626 recv_ioctx->ioctx.dma, srp_max_req_size,
1627 DMA_FROM_DEVICE);
1628
Bart Van Assche33912d72016-02-11 11:04:43 -08001629 if (unlikely(ch->state == CH_CONNECTING)) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001630 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1631 goto out;
1632 }
1633
Bart Van Assche33912d72016-02-11 11:04:43 -08001634 if (unlikely(ch->state != CH_LIVE))
Bart Van Asschea42d9852011-10-14 01:30:46 +00001635 goto out;
1636
1637 srp_cmd = recv_ioctx->ioctx.buf;
1638 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1639 if (!send_ioctx)
1640 send_ioctx = srpt_get_send_ioctx(ch);
1641 if (unlikely(!send_ioctx)) {
1642 list_add_tail(&recv_ioctx->wait_list,
1643 &ch->cmd_wait_list);
1644 goto out;
1645 }
1646 }
1647
Bart Van Asschea42d9852011-10-14 01:30:46 +00001648 switch (srp_cmd->opcode) {
1649 case SRP_CMD:
1650 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1651 break;
1652 case SRP_TSK_MGMT:
1653 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1654 break;
1655 case SRP_I_LOGOUT:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001656 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001657 break;
1658 case SRP_CRED_RSP:
1659 pr_debug("received SRP_CRED_RSP\n");
1660 break;
1661 case SRP_AER_RSP:
1662 pr_debug("received SRP_AER_RSP\n");
1663 break;
1664 case SRP_RSP:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001665 pr_err("Received SRP_RSP\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001666 break;
1667 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001668 pr_err("received IU with unknown opcode 0x%x\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001669 srp_cmd->opcode);
1670 break;
1671 }
1672
1673 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1674out:
1675 return;
1676}
1677
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001678static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001679{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001680 struct srpt_rdma_ch *ch = cq->cq_context;
1681 struct srpt_recv_ioctx *ioctx =
1682 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001683
Bart Van Asschea42d9852011-10-14 01:30:46 +00001684 if (wc->status == IB_WC_SUCCESS) {
1685 int req_lim;
1686
1687 req_lim = atomic_dec_return(&ch->req_lim);
1688 if (unlikely(req_lim < 0))
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001689 pr_err("req_lim = %d < 0\n", req_lim);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001690 srpt_handle_new_iu(ch, ioctx, NULL);
1691 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001692 pr_info("receiving failed for ioctx %p with status %d\n",
1693 ioctx, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001694 }
1695}
1696
1697/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001698 * Note: Although this has not yet been observed during tests, at least in
1699 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1700 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1701 * value in each response is set to one, and it is possible that this response
1702 * makes the initiator send a new request before the send completion for that
1703 * response has been processed. This could e.g. happen if the call to
1704 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1705 * if IB retransmission causes generation of the send completion to be
1706 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1707 * are queued on cmd_wait_list. The code below processes these delayed
1708 * requests one at a time.
1709 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001710static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001711{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001712 struct srpt_rdma_ch *ch = cq->cq_context;
1713 struct srpt_send_ioctx *ioctx =
1714 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1715 enum srpt_command_state state;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001716
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001717 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1718
1719 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1720 state != SRPT_STATE_MGMT_RSP_SENT);
1721
1722 atomic_inc(&ch->sq_wr_avail);
1723
1724 if (wc->status != IB_WC_SUCCESS) {
1725 pr_info("sending response for ioctx 0x%p failed"
1726 " with status %d\n", ioctx, wc->status);
1727
1728 atomic_dec(&ch->req_lim);
1729 srpt_abort_cmd(ioctx);
1730 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001731 }
1732
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001733 if (state != SRPT_STATE_DONE) {
1734 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1735 transport_generic_free_cmd(&ioctx->cmd, 0);
1736 } else {
1737 pr_err("IB completion has been received too late for"
1738 " wr_id = %u.\n", ioctx->ioctx.index);
1739 }
1740
1741out:
1742 while (!list_empty(&ch->cmd_wait_list) &&
Bart Van Assche33912d72016-02-11 11:04:43 -08001743 ch->state == CH_LIVE &&
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001744 (ioctx = srpt_get_send_ioctx(ch)) != NULL) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001745 struct srpt_recv_ioctx *recv_ioctx;
1746
1747 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
1748 struct srpt_recv_ioctx,
1749 wait_list);
1750 list_del(&recv_ioctx->wait_list);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001751 srpt_handle_new_iu(ch, recv_ioctx, ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001752 }
1753}
1754
Bart Van Asschea42d9852011-10-14 01:30:46 +00001755/**
1756 * srpt_create_ch_ib() - Create receive and send completion queues.
1757 */
1758static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1759{
1760 struct ib_qp_init_attr *qp_init;
1761 struct srpt_port *sport = ch->sport;
1762 struct srpt_device *sdev = sport->sdev;
1763 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
1764 int ret;
1765
1766 WARN_ON(ch->rq_size < 1);
1767
1768 ret = -ENOMEM;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08001769 qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001770 if (!qp_init)
1771 goto out;
1772
Bart Van Asscheab477c12014-10-19 18:05:33 +03001773retry:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001774 ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size,
1775 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001776 if (IS_ERR(ch->cq)) {
1777 ret = PTR_ERR(ch->cq);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001778 pr_err("failed to create CQ cqe= %d ret= %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001779 ch->rq_size + srp_sq_size, ret);
1780 goto out;
1781 }
1782
1783 qp_init->qp_context = (void *)ch;
1784 qp_init->event_handler
1785 = (void(*)(struct ib_event *, void*))srpt_qp_event;
1786 qp_init->send_cq = ch->cq;
1787 qp_init->recv_cq = ch->cq;
1788 qp_init->srq = sdev->srq;
1789 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1790 qp_init->qp_type = IB_QPT_RC;
1791 qp_init->cap.max_send_wr = srp_sq_size;
1792 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
1793
1794 ch->qp = ib_create_qp(sdev->pd, qp_init);
1795 if (IS_ERR(ch->qp)) {
1796 ret = PTR_ERR(ch->qp);
Bart Van Asscheab477c12014-10-19 18:05:33 +03001797 if (ret == -ENOMEM) {
1798 srp_sq_size /= 2;
1799 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
1800 ib_destroy_cq(ch->cq);
1801 goto retry;
1802 }
1803 }
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001804 pr_err("failed to create_qp ret= %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001805 goto err_destroy_cq;
1806 }
1807
1808 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1809
1810 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
1811 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1812 qp_init->cap.max_send_wr, ch->cm_id);
1813
1814 ret = srpt_init_ch_qp(ch, ch->qp);
1815 if (ret)
1816 goto err_destroy_qp;
1817
Bart Van Asschea42d9852011-10-14 01:30:46 +00001818out:
1819 kfree(qp_init);
1820 return ret;
1821
1822err_destroy_qp:
1823 ib_destroy_qp(ch->qp);
1824err_destroy_cq:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001825 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001826 goto out;
1827}
1828
1829static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
1830{
Bart Van Asschea42d9852011-10-14 01:30:46 +00001831 ib_destroy_qp(ch->qp);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001832 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001833}
1834
1835/**
1836 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
1837 *
1838 * Reset the QP and make sure all resources associated with the channel will
1839 * be deallocated at an appropriate time.
1840 *
1841 * Note: The caller must hold ch->sport->sdev->spinlock.
1842 */
1843static void __srpt_close_ch(struct srpt_rdma_ch *ch)
1844{
Bart Van Asschea42d9852011-10-14 01:30:46 +00001845 enum rdma_ch_state prev_state;
1846 unsigned long flags;
1847
Bart Van Asschea42d9852011-10-14 01:30:46 +00001848 spin_lock_irqsave(&ch->spinlock, flags);
1849 prev_state = ch->state;
1850 switch (prev_state) {
1851 case CH_CONNECTING:
1852 case CH_LIVE:
1853 ch->state = CH_DISCONNECTING;
1854 break;
1855 default:
1856 break;
1857 }
1858 spin_unlock_irqrestore(&ch->spinlock, flags);
1859
1860 switch (prev_state) {
1861 case CH_CONNECTING:
1862 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
1863 NULL, 0);
1864 /* fall through */
1865 case CH_LIVE:
1866 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001867 pr_err("sending CM DREQ failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001868 break;
1869 case CH_DISCONNECTING:
1870 break;
1871 case CH_DRAINING:
1872 case CH_RELEASING:
1873 break;
1874 }
1875}
1876
1877/**
1878 * srpt_close_ch() - Close an RDMA channel.
1879 */
1880static void srpt_close_ch(struct srpt_rdma_ch *ch)
1881{
1882 struct srpt_device *sdev;
1883
1884 sdev = ch->sport->sdev;
1885 spin_lock_irq(&sdev->spinlock);
1886 __srpt_close_ch(ch);
1887 spin_unlock_irq(&sdev->spinlock);
1888}
1889
1890/**
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07001891 * srpt_shutdown_session() - Whether or not a session may be shut down.
1892 */
1893static int srpt_shutdown_session(struct se_session *se_sess)
1894{
Bart Van Assche88936252016-02-11 11:05:58 -08001895 return 1;
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07001896}
1897
1898/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001899 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
1900 * @cm_id: Pointer to the CM ID of the channel to be drained.
1901 *
1902 * Note: Must be called from inside srpt_cm_handler to avoid a race between
1903 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
1904 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
1905 * waits until all target sessions for the associated IB device have been
1906 * unregistered and target session registration involves a call to
1907 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
1908 * this function has finished).
1909 */
1910static void srpt_drain_channel(struct ib_cm_id *cm_id)
1911{
1912 struct srpt_device *sdev;
1913 struct srpt_rdma_ch *ch;
1914 int ret;
1915 bool do_reset = false;
1916
1917 WARN_ON_ONCE(irqs_disabled());
1918
1919 sdev = cm_id->context;
1920 BUG_ON(!sdev);
1921 spin_lock_irq(&sdev->spinlock);
1922 list_for_each_entry(ch, &sdev->rch_list, list) {
1923 if (ch->cm_id == cm_id) {
Bart Van Asschef130c222016-02-11 11:05:38 -08001924 do_reset = srpt_set_ch_state(ch, CH_DRAINING);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001925 break;
1926 }
1927 }
1928 spin_unlock_irq(&sdev->spinlock);
1929
1930 if (do_reset) {
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07001931 if (ch->sess)
1932 srpt_shutdown_session(ch->sess);
1933
Bart Van Asschea42d9852011-10-14 01:30:46 +00001934 ret = srpt_ch_qp_err(ch);
1935 if (ret < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001936 pr_err("Setting queue pair in error state"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001937 " failed: %d\n", ret);
1938 }
1939}
1940
1941/**
1942 * srpt_find_channel() - Look up an RDMA channel.
1943 * @cm_id: Pointer to the CM ID of the channel to be looked up.
1944 *
1945 * Return NULL if no matching RDMA channel has been found.
1946 */
1947static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
1948 struct ib_cm_id *cm_id)
1949{
1950 struct srpt_rdma_ch *ch;
1951 bool found;
1952
1953 WARN_ON_ONCE(irqs_disabled());
1954 BUG_ON(!sdev);
1955
1956 found = false;
1957 spin_lock_irq(&sdev->spinlock);
1958 list_for_each_entry(ch, &sdev->rch_list, list) {
1959 if (ch->cm_id == cm_id) {
1960 found = true;
1961 break;
1962 }
1963 }
1964 spin_unlock_irq(&sdev->spinlock);
1965
1966 return found ? ch : NULL;
1967}
1968
1969/**
1970 * srpt_release_channel() - Release channel resources.
1971 *
1972 * Schedules the actual release because:
1973 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
1974 * trigger a deadlock.
1975 * - It is not safe to call TCM transport_* functions from interrupt context.
1976 */
1977static void srpt_release_channel(struct srpt_rdma_ch *ch)
1978{
1979 schedule_work(&ch->release_work);
1980}
1981
1982static void srpt_release_channel_work(struct work_struct *w)
1983{
1984 struct srpt_rdma_ch *ch;
1985 struct srpt_device *sdev;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001986 struct se_session *se_sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001987
1988 ch = container_of(w, struct srpt_rdma_ch, release_work);
Bart Van Asschef108f0f2016-02-11 11:06:14 -08001989 pr_debug("%s: %s-%d; release_done = %p\n", __func__, ch->sess_name,
1990 ch->qp->qp_num, ch->release_done);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001991
1992 sdev = ch->sport->sdev;
1993 BUG_ON(!sdev);
1994
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001995 se_sess = ch->sess;
1996 BUG_ON(!se_sess);
1997
Bart Van Assche88936252016-02-11 11:05:58 -08001998 target_sess_cmd_list_set_waiting(se_sess);
Joern Engelbe646c2d2013-05-15 00:44:07 -07001999 target_wait_for_sess_cmds(se_sess);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002000
2001 transport_deregister_session_configfs(se_sess);
2002 transport_deregister_session(se_sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002003 ch->sess = NULL;
2004
Nicholas Bellinger0b41d6c2013-09-18 12:48:27 -07002005 ib_destroy_cm_id(ch->cm_id);
2006
Bart Van Asschea42d9852011-10-14 01:30:46 +00002007 srpt_destroy_ch_ib(ch);
2008
2009 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2010 ch->sport->sdev, ch->rq_size,
2011 ch->rsp_size, DMA_TO_DEVICE);
2012
2013 spin_lock_irq(&sdev->spinlock);
Bart Van Asschef108f0f2016-02-11 11:06:14 -08002014 list_del_init(&ch->list);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002015 if (ch->release_done)
2016 complete(ch->release_done);
Bart Van Asschef108f0f2016-02-11 11:06:14 -08002017 spin_unlock_irq(&sdev->spinlock);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002018
2019 wake_up(&sdev->ch_releaseQ);
2020
2021 kfree(ch);
2022}
2023
Bart Van Asschea42d9852011-10-14 01:30:46 +00002024/**
2025 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2026 *
2027 * Ownership of the cm_id is transferred to the target session if this
2028 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2029 */
2030static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2031 struct ib_cm_req_event_param *param,
2032 void *private_data)
2033{
2034 struct srpt_device *sdev = cm_id->context;
2035 struct srpt_port *sport = &sdev->port[param->port - 1];
2036 struct srp_login_req *req;
2037 struct srp_login_rsp *rsp;
2038 struct srp_login_rej *rej;
2039 struct ib_cm_rep_param *rep_param;
2040 struct srpt_rdma_ch *ch, *tmp_ch;
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002041 struct se_node_acl *se_acl;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002042 u32 it_iu_len;
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002043 int i, ret = 0;
2044 unsigned char *p;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002045
2046 WARN_ON_ONCE(irqs_disabled());
2047
2048 if (WARN_ON(!sdev || !private_data))
2049 return -EINVAL;
2050
2051 req = (struct srp_login_req *)private_data;
2052
2053 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2054
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002055 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2056 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2057 " (guid=0x%llx:0x%llx)\n",
2058 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2059 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2060 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2061 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2062 it_iu_len,
2063 param->port,
2064 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2065 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002066
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002067 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
2068 rej = kzalloc(sizeof(*rej), GFP_KERNEL);
2069 rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002070
2071 if (!rsp || !rej || !rep_param) {
2072 ret = -ENOMEM;
2073 goto out;
2074 }
2075
2076 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302077 rej->reason = cpu_to_be32(
2078 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002079 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002080 pr_err("rejected SRP_LOGIN_REQ because its"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002081 " length (%d bytes) is out of range (%d .. %d)\n",
2082 it_iu_len, 64, srp_max_req_size);
2083 goto reject;
2084 }
2085
2086 if (!sport->enabled) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302087 rej->reason = cpu_to_be32(
2088 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002089 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002090 pr_err("rejected SRP_LOGIN_REQ because the target port"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002091 " has not yet been enabled\n");
2092 goto reject;
2093 }
2094
2095 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2096 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2097
2098 spin_lock_irq(&sdev->spinlock);
2099
2100 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2101 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2102 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2103 && param->port == ch->sport->port
2104 && param->listen_id == ch->sport->sdev->cm_id
2105 && ch->cm_id) {
Bart Van Assche33912d72016-02-11 11:04:43 -08002106 if (ch->state != CH_CONNECTING
2107 && ch->state != CH_LIVE)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002108 continue;
2109
2110 /* found an existing channel */
2111 pr_debug("Found existing channel %s"
2112 " cm_id= %p state= %d\n",
Bart Van Assche33912d72016-02-11 11:04:43 -08002113 ch->sess_name, ch->cm_id, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002114
2115 __srpt_close_ch(ch);
2116
2117 rsp->rsp_flags =
2118 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2119 }
2120 }
2121
2122 spin_unlock_irq(&sdev->spinlock);
2123
2124 } else
2125 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2126
2127 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2128 || *(__be64 *)(req->target_port_id + 8) !=
2129 cpu_to_be64(srpt_service_guid)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302130 rej->reason = cpu_to_be32(
2131 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002132 ret = -ENOMEM;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002133 pr_err("rejected SRP_LOGIN_REQ because it"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002134 " has an invalid target port identifier.\n");
2135 goto reject;
2136 }
2137
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002138 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002139 if (!ch) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302140 rej->reason = cpu_to_be32(
2141 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002142 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002143 ret = -ENOMEM;
2144 goto reject;
2145 }
2146
2147 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2148 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2149 memcpy(ch->t_port_id, req->target_port_id, 16);
2150 ch->sport = &sdev->port[param->port - 1];
2151 ch->cm_id = cm_id;
2152 /*
2153 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2154 * for the SRP protocol to the command queue size.
2155 */
2156 ch->rq_size = SRPT_RQ_SIZE;
2157 spin_lock_init(&ch->spinlock);
2158 ch->state = CH_CONNECTING;
2159 INIT_LIST_HEAD(&ch->cmd_wait_list);
2160 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2161
2162 ch->ioctx_ring = (struct srpt_send_ioctx **)
2163 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2164 sizeof(*ch->ioctx_ring[0]),
2165 ch->rsp_size, DMA_TO_DEVICE);
2166 if (!ch->ioctx_ring)
2167 goto free_ch;
2168
2169 INIT_LIST_HEAD(&ch->free_list);
2170 for (i = 0; i < ch->rq_size; i++) {
2171 ch->ioctx_ring[i]->ch = ch;
2172 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2173 }
2174
2175 ret = srpt_create_ch_ib(ch);
2176 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302177 rej->reason = cpu_to_be32(
2178 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002179 pr_err("rejected SRP_LOGIN_REQ because creating"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002180 " a new RDMA channel failed.\n");
2181 goto free_ring;
2182 }
2183
2184 ret = srpt_ch_qp_rtr(ch, ch->qp);
2185 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302186 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002187 pr_err("rejected SRP_LOGIN_REQ because enabling"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002188 " RTR failed (error code = %d)\n", ret);
2189 goto destroy_ib;
2190 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002191
Bart Van Asschea42d9852011-10-14 01:30:46 +00002192 /*
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002193 * Use the initator port identifier as the session name, when
2194 * checking against se_node_acl->initiatorname[] this can be
2195 * with or without preceeding '0x'.
Bart Van Asschea42d9852011-10-14 01:30:46 +00002196 */
2197 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2198 be64_to_cpu(*(__be64 *)ch->i_port_id),
2199 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2200
2201 pr_debug("registering session %s\n", ch->sess_name);
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002202 p = &ch->sess_name[0];
Bart Van Asschea42d9852011-10-14 01:30:46 +00002203
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002204 ch->sess = transport_init_session(TARGET_PROT_NORMAL);
Dan Carpenter3af33632011-11-04 21:27:32 +03002205 if (IS_ERR(ch->sess)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302206 rej->reason = cpu_to_be32(
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002207 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002208 pr_debug("Failed to create session\n");
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002209 goto destroy_ib;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002210 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002211
2212try_again:
2213 se_acl = core_tpg_get_initiator_node_acl(&sport->port_tpg_1, p);
2214 if (!se_acl) {
2215 pr_info("Rejected login because no ACL has been"
2216 " configured yet for initiator %s.\n", ch->sess_name);
2217 /*
2218 * XXX: Hack to retry of ch->i_port_id without leading '0x'
2219 */
2220 if (p == &ch->sess_name[0]) {
2221 p += 2;
2222 goto try_again;
2223 }
2224 rej->reason = cpu_to_be32(
2225 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2226 transport_free_session(ch->sess);
2227 goto destroy_ib;
2228 }
2229 ch->sess->se_node_acl = se_acl;
2230
2231 transport_register_session(&sport->port_tpg_1, se_acl, ch->sess, ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002232
2233 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2234 ch->sess_name, ch->cm_id);
2235
2236 /* create srp_login_response */
2237 rsp->opcode = SRP_LOGIN_RSP;
2238 rsp->tag = req->tag;
2239 rsp->max_it_iu_len = req->req_it_iu_len;
2240 rsp->max_ti_iu_len = req->req_it_iu_len;
2241 ch->max_ti_iu_len = it_iu_len;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302242 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2243 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002244 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2245 atomic_set(&ch->req_lim, ch->rq_size);
2246 atomic_set(&ch->req_lim_delta, 0);
2247
2248 /* create cm reply */
2249 rep_param->qp_num = ch->qp->qp_num;
2250 rep_param->private_data = (void *)rsp;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002251 rep_param->private_data_len = sizeof(*rsp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002252 rep_param->rnr_retry_count = 7;
2253 rep_param->flow_control = 1;
2254 rep_param->failover_accepted = 0;
2255 rep_param->srq = 1;
2256 rep_param->responder_resources = 4;
2257 rep_param->initiator_depth = 4;
2258
2259 ret = ib_send_cm_rep(cm_id, rep_param);
2260 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002261 pr_err("sending SRP_LOGIN_REQ response failed"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002262 " (error code = %d)\n", ret);
2263 goto release_channel;
2264 }
2265
2266 spin_lock_irq(&sdev->spinlock);
2267 list_add_tail(&ch->list, &sdev->rch_list);
2268 spin_unlock_irq(&sdev->spinlock);
2269
2270 goto out;
2271
2272release_channel:
2273 srpt_set_ch_state(ch, CH_RELEASING);
2274 transport_deregister_session_configfs(ch->sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002275 transport_deregister_session(ch->sess);
2276 ch->sess = NULL;
2277
2278destroy_ib:
2279 srpt_destroy_ch_ib(ch);
2280
2281free_ring:
2282 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2283 ch->sport->sdev, ch->rq_size,
2284 ch->rsp_size, DMA_TO_DEVICE);
2285free_ch:
2286 kfree(ch);
2287
2288reject:
2289 rej->opcode = SRP_LOGIN_REJ;
2290 rej->tag = req->tag;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302291 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2292 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002293
2294 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002295 (void *)rej, sizeof(*rej));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002296
2297out:
2298 kfree(rep_param);
2299 kfree(rsp);
2300 kfree(rej);
2301
2302 return ret;
2303}
2304
2305static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2306{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002307 pr_info("Received IB REJ for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002308 srpt_drain_channel(cm_id);
2309}
2310
2311/**
2312 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2313 *
2314 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2315 * and that the recipient may begin transmitting (RTU = ready to use).
2316 */
2317static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2318{
2319 struct srpt_rdma_ch *ch;
2320 int ret;
2321
2322 ch = srpt_find_channel(cm_id->context, cm_id);
2323 BUG_ON(!ch);
2324
Bart Van Asschef130c222016-02-11 11:05:38 -08002325 if (srpt_set_ch_state(ch, CH_LIVE)) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00002326 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2327
2328 ret = srpt_ch_qp_rts(ch, ch->qp);
2329
2330 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2331 wait_list) {
2332 list_del(&ioctx->wait_list);
2333 srpt_handle_new_iu(ch, ioctx, NULL);
2334 }
2335 if (ret)
2336 srpt_close_ch(ch);
2337 }
2338}
2339
2340static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2341{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002342 pr_info("Received IB TimeWait exit for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002343 srpt_drain_channel(cm_id);
2344}
2345
2346static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2347{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002348 pr_info("Received IB REP error for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002349 srpt_drain_channel(cm_id);
2350}
2351
2352/**
2353 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2354 */
2355static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2356{
2357 struct srpt_rdma_ch *ch;
2358 unsigned long flags;
2359 bool send_drep = false;
2360
2361 ch = srpt_find_channel(cm_id->context, cm_id);
2362 BUG_ON(!ch);
2363
Bart Van Assche33912d72016-02-11 11:04:43 -08002364 pr_debug("cm_id= %p ch->state= %d\n", cm_id, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002365
2366 spin_lock_irqsave(&ch->spinlock, flags);
2367 switch (ch->state) {
2368 case CH_CONNECTING:
2369 case CH_LIVE:
2370 send_drep = true;
2371 ch->state = CH_DISCONNECTING;
2372 break;
2373 case CH_DISCONNECTING:
2374 case CH_DRAINING:
2375 case CH_RELEASING:
2376 WARN(true, "unexpected channel state %d\n", ch->state);
2377 break;
2378 }
2379 spin_unlock_irqrestore(&ch->spinlock, flags);
2380
2381 if (send_drep) {
2382 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002383 pr_err("Sending IB DREP failed.\n");
2384 pr_info("Received DREQ and sent DREP for session %s.\n",
2385 ch->sess_name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002386 }
2387}
2388
2389/**
2390 * srpt_cm_drep_recv() - Process reception of a DREP message.
2391 */
2392static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2393{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002394 pr_info("Received InfiniBand DREP message for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002395 srpt_drain_channel(cm_id);
2396}
2397
2398/**
2399 * srpt_cm_handler() - IB connection manager callback function.
2400 *
2401 * A non-zero return value will cause the caller destroy the CM ID.
2402 *
2403 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2404 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2405 * a non-zero value in any other case will trigger a race with the
2406 * ib_destroy_cm_id() call in srpt_release_channel().
2407 */
2408static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2409{
2410 int ret;
2411
2412 ret = 0;
2413 switch (event->event) {
2414 case IB_CM_REQ_RECEIVED:
2415 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2416 event->private_data);
2417 break;
2418 case IB_CM_REJ_RECEIVED:
2419 srpt_cm_rej_recv(cm_id);
2420 break;
2421 case IB_CM_RTU_RECEIVED:
2422 case IB_CM_USER_ESTABLISHED:
2423 srpt_cm_rtu_recv(cm_id);
2424 break;
2425 case IB_CM_DREQ_RECEIVED:
2426 srpt_cm_dreq_recv(cm_id);
2427 break;
2428 case IB_CM_DREP_RECEIVED:
2429 srpt_cm_drep_recv(cm_id);
2430 break;
2431 case IB_CM_TIMEWAIT_EXIT:
2432 srpt_cm_timewait_exit(cm_id);
2433 break;
2434 case IB_CM_REP_ERROR:
2435 srpt_cm_rep_error(cm_id);
2436 break;
2437 case IB_CM_DREQ_ERROR:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002438 pr_info("Received IB DREQ ERROR event.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002439 break;
2440 case IB_CM_MRA_RECEIVED:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002441 pr_info("Received IB MRA event\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002442 break;
2443 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002444 pr_err("received unrecognized IB CM event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002445 break;
2446 }
2447
2448 return ret;
2449}
2450
2451/**
2452 * srpt_perform_rdmas() - Perform IB RDMA.
2453 *
2454 * Returns zero upon success or a negative number upon failure.
2455 */
2456static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2457 struct srpt_send_ioctx *ioctx)
2458{
Bart Van Asschea42d9852011-10-14 01:30:46 +00002459 struct ib_send_wr *bad_wr;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002460 int sq_wr_avail, ret, i;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002461 enum dma_data_direction dir;
2462 const int n_rdma = ioctx->n_rdma;
2463
2464 dir = ioctx->cmd.data_direction;
2465 if (dir == DMA_TO_DEVICE) {
2466 /* write */
2467 ret = -ENOMEM;
2468 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2469 if (sq_wr_avail < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002470 pr_warn("IB send queue full (needed %d)\n",
2471 n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002472 goto out;
2473 }
2474 }
2475
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002476 for (i = 0; i < n_rdma; i++) {
2477 struct ib_send_wr *wr = &ioctx->rdma_wrs[i].wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002478
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002479 wr->opcode = (dir == DMA_FROM_DEVICE) ?
2480 IB_WR_RDMA_WRITE : IB_WR_RDMA_READ;
2481
2482 if (i == n_rdma - 1) {
2483 /* only get completion event for the last rdma read */
2484 if (dir == DMA_TO_DEVICE) {
2485 wr->send_flags = IB_SEND_SIGNALED;
2486 ioctx->rdma_cqe.done = srpt_rdma_read_done;
2487 } else {
2488 ioctx->rdma_cqe.done = srpt_rdma_write_done;
2489 }
2490 wr->wr_cqe = &ioctx->rdma_cqe;
2491 wr->next = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002492 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002493 wr->wr_cqe = NULL;
2494 wr->next = &ioctx->rdma_wrs[i + 1].wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002495 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00002496 }
2497
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002498 ret = ib_post_send(ch->qp, &ioctx->rdma_wrs->wr, &bad_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002499 if (ret)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002500 pr_err("%s[%d]: ib_post_send() returned %d for %d/%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002501 __func__, __LINE__, ret, i, n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002502out:
2503 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2504 atomic_add(n_rdma, &ch->sq_wr_avail);
2505 return ret;
2506}
2507
2508/**
2509 * srpt_xfer_data() - Start data transfer from initiator to target.
2510 */
2511static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2512 struct srpt_send_ioctx *ioctx)
2513{
2514 int ret;
2515
2516 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2517 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002518 pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002519 goto out;
2520 }
2521
2522 ret = srpt_perform_rdmas(ch, ioctx);
2523 if (ret) {
2524 if (ret == -EAGAIN || ret == -ENOMEM)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002525 pr_info("%s[%d] queue full -- ret=%d\n",
2526 __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002527 else
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002528 pr_err("%s[%d] fatal error -- ret=%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002529 __func__, __LINE__, ret);
2530 goto out_unmap;
2531 }
2532
2533out:
2534 return ret;
2535out_unmap:
2536 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2537 goto out;
2538}
2539
2540static int srpt_write_pending_status(struct se_cmd *se_cmd)
2541{
2542 struct srpt_send_ioctx *ioctx;
2543
2544 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2545 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2546}
2547
2548/*
2549 * srpt_write_pending() - Start data transfer from initiator to target (write).
2550 */
2551static int srpt_write_pending(struct se_cmd *se_cmd)
2552{
2553 struct srpt_rdma_ch *ch;
2554 struct srpt_send_ioctx *ioctx;
2555 enum srpt_command_state new_state;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002556 int ret;
2557
2558 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2559
2560 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2561 WARN_ON(new_state == SRPT_STATE_DONE);
2562
2563 ch = ioctx->ch;
2564 BUG_ON(!ch);
2565
Bart Van Assche33912d72016-02-11 11:04:43 -08002566 switch (ch->state) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00002567 case CH_CONNECTING:
Bart Van Assche33912d72016-02-11 11:04:43 -08002568 WARN(true, "unexpected channel state %d\n", ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002569 ret = -EINVAL;
2570 goto out;
2571 case CH_LIVE:
2572 break;
2573 case CH_DISCONNECTING:
2574 case CH_DRAINING:
2575 case CH_RELEASING:
2576 pr_debug("cmd with tag %lld: channel disconnecting\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002577 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002578 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2579 ret = -EINVAL;
2580 goto out;
2581 }
2582 ret = srpt_xfer_data(ch, ioctx);
2583
2584out:
2585 return ret;
2586}
2587
2588static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2589{
2590 switch (tcm_mgmt_status) {
2591 case TMR_FUNCTION_COMPLETE:
2592 return SRP_TSK_MGMT_SUCCESS;
2593 case TMR_FUNCTION_REJECTED:
2594 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2595 }
2596 return SRP_TSK_MGMT_FAILED;
2597}
2598
2599/**
2600 * srpt_queue_response() - Transmits the response to a SCSI command.
2601 *
2602 * Callback function called by the TCM core. Must not block since it can be
2603 * invoked on the context of the IB completion handler.
2604 */
Joern Engelb79fafa2013-07-03 11:22:17 -04002605static void srpt_queue_response(struct se_cmd *cmd)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002606{
2607 struct srpt_rdma_ch *ch;
2608 struct srpt_send_ioctx *ioctx;
2609 enum srpt_command_state state;
2610 unsigned long flags;
2611 int ret;
2612 enum dma_data_direction dir;
2613 int resp_len;
2614 u8 srp_tm_status;
2615
Bart Van Asschea42d9852011-10-14 01:30:46 +00002616 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2617 ch = ioctx->ch;
2618 BUG_ON(!ch);
2619
2620 spin_lock_irqsave(&ioctx->spinlock, flags);
2621 state = ioctx->state;
2622 switch (state) {
2623 case SRPT_STATE_NEW:
2624 case SRPT_STATE_DATA_IN:
2625 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2626 break;
2627 case SRPT_STATE_MGMT:
2628 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2629 break;
2630 default:
2631 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2632 ch, ioctx->ioctx.index, ioctx->state);
2633 break;
2634 }
2635 spin_unlock_irqrestore(&ioctx->spinlock, flags);
2636
2637 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
2638 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
2639 atomic_inc(&ch->req_lim_delta);
2640 srpt_abort_cmd(ioctx);
Joern Engelb79fafa2013-07-03 11:22:17 -04002641 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002642 }
2643
2644 dir = ioctx->cmd.data_direction;
2645
2646 /* For read commands, transfer the data to the initiator. */
2647 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
2648 !ioctx->queue_status_only) {
2649 ret = srpt_xfer_data(ch, ioctx);
2650 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002651 pr_err("xfer_data failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002652 ioctx->cmd.tag);
Joern Engelb79fafa2013-07-03 11:22:17 -04002653 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002654 }
2655 }
2656
2657 if (state != SRPT_STATE_MGMT)
Bart Van Assche649ee052015-04-14 13:26:44 +02002658 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002659 cmd->scsi_status);
2660 else {
2661 srp_tm_status
2662 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2663 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
Bart Van Assche649ee052015-04-14 13:26:44 +02002664 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002665 }
2666 ret = srpt_post_send(ch, ioctx, resp_len);
2667 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002668 pr_err("sending cmd response failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002669 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002670 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2671 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02002672 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002673 }
Joern Engelb79fafa2013-07-03 11:22:17 -04002674}
Bart Van Asschea42d9852011-10-14 01:30:46 +00002675
Joern Engelb79fafa2013-07-03 11:22:17 -04002676static int srpt_queue_data_in(struct se_cmd *cmd)
2677{
2678 srpt_queue_response(cmd);
2679 return 0;
2680}
2681
2682static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2683{
2684 srpt_queue_response(cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002685}
2686
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002687static void srpt_aborted_task(struct se_cmd *cmd)
2688{
2689 struct srpt_send_ioctx *ioctx = container_of(cmd,
2690 struct srpt_send_ioctx, cmd);
2691
2692 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
2693}
2694
Bart Van Asschea42d9852011-10-14 01:30:46 +00002695static int srpt_queue_status(struct se_cmd *cmd)
2696{
2697 struct srpt_send_ioctx *ioctx;
2698
2699 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2700 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2701 if (cmd->se_cmd_flags &
2702 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2703 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2704 ioctx->queue_status_only = true;
Joern Engelb79fafa2013-07-03 11:22:17 -04002705 srpt_queue_response(cmd);
2706 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002707}
2708
2709static void srpt_refresh_port_work(struct work_struct *work)
2710{
2711 struct srpt_port *sport = container_of(work, struct srpt_port, work);
2712
2713 srpt_refresh_port(sport);
2714}
2715
2716static int srpt_ch_list_empty(struct srpt_device *sdev)
2717{
2718 int res;
2719
2720 spin_lock_irq(&sdev->spinlock);
2721 res = list_empty(&sdev->rch_list);
2722 spin_unlock_irq(&sdev->spinlock);
2723
2724 return res;
2725}
2726
2727/**
2728 * srpt_release_sdev() - Free the channel resources associated with a target.
2729 */
2730static int srpt_release_sdev(struct srpt_device *sdev)
2731{
2732 struct srpt_rdma_ch *ch, *tmp_ch;
2733 int res;
2734
2735 WARN_ON_ONCE(irqs_disabled());
2736
2737 BUG_ON(!sdev);
2738
2739 spin_lock_irq(&sdev->spinlock);
2740 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
2741 __srpt_close_ch(ch);
2742 spin_unlock_irq(&sdev->spinlock);
2743
2744 res = wait_event_interruptible(sdev->ch_releaseQ,
2745 srpt_ch_list_empty(sdev));
2746 if (res)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002747 pr_err("%s: interrupted.\n", __func__);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002748
2749 return 0;
2750}
2751
2752static struct srpt_port *__srpt_lookup_port(const char *name)
2753{
2754 struct ib_device *dev;
2755 struct srpt_device *sdev;
2756 struct srpt_port *sport;
2757 int i;
2758
2759 list_for_each_entry(sdev, &srpt_dev_list, list) {
2760 dev = sdev->device;
2761 if (!dev)
2762 continue;
2763
2764 for (i = 0; i < dev->phys_port_cnt; i++) {
2765 sport = &sdev->port[i];
2766
2767 if (!strcmp(sport->port_guid, name))
2768 return sport;
2769 }
2770 }
2771
2772 return NULL;
2773}
2774
2775static struct srpt_port *srpt_lookup_port(const char *name)
2776{
2777 struct srpt_port *sport;
2778
2779 spin_lock(&srpt_dev_lock);
2780 sport = __srpt_lookup_port(name);
2781 spin_unlock(&srpt_dev_lock);
2782
2783 return sport;
2784}
2785
2786/**
2787 * srpt_add_one() - Infiniband device addition callback function.
2788 */
2789static void srpt_add_one(struct ib_device *device)
2790{
2791 struct srpt_device *sdev;
2792 struct srpt_port *sport;
2793 struct ib_srq_init_attr srq_attr;
2794 int i;
2795
2796 pr_debug("device = %p, device->dma_ops = %p\n", device,
2797 device->dma_ops);
2798
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002799 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002800 if (!sdev)
2801 goto err;
2802
2803 sdev->device = device;
2804 INIT_LIST_HEAD(&sdev->rch_list);
2805 init_waitqueue_head(&sdev->ch_releaseQ);
2806 spin_lock_init(&sdev->spinlock);
2807
Bart Van Asschea42d9852011-10-14 01:30:46 +00002808 sdev->pd = ib_alloc_pd(device);
2809 if (IS_ERR(sdev->pd))
2810 goto free_dev;
2811
Or Gerlitz4a061b22015-12-18 10:59:46 +02002812 sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002813
2814 srq_attr.event_handler = srpt_srq_event;
2815 srq_attr.srq_context = (void *)sdev;
2816 srq_attr.attr.max_wr = sdev->srq_size;
2817 srq_attr.attr.max_sge = 1;
2818 srq_attr.attr.srq_limit = 0;
Roland Dreier6f360332012-04-12 07:51:08 -07002819 srq_attr.srq_type = IB_SRQT_BASIC;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002820
2821 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
2822 if (IS_ERR(sdev->srq))
Jason Gunthorpe5a783952015-07-30 17:22:24 -06002823 goto err_pd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002824
2825 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
Or Gerlitz4a061b22015-12-18 10:59:46 +02002826 __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002827 device->name);
2828
2829 if (!srpt_service_guid)
2830 srpt_service_guid = be64_to_cpu(device->node_guid);
2831
2832 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
2833 if (IS_ERR(sdev->cm_id))
2834 goto err_srq;
2835
2836 /* print out target login information */
2837 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
2838 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
2839 srpt_service_guid, srpt_service_guid);
2840
2841 /*
2842 * We do not have a consistent service_id (ie. also id_ext of target_id)
2843 * to identify this target. We currently use the guid of the first HCA
2844 * in the system as service_id; therefore, the target_id will change
2845 * if this HCA is gone bad and replaced by different HCA
2846 */
Haggai Eran73fec7f2015-07-30 17:50:26 +03002847 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
Bart Van Asschea42d9852011-10-14 01:30:46 +00002848 goto err_cm;
2849
2850 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
2851 srpt_event_handler);
2852 if (ib_register_event_handler(&sdev->event_handler))
2853 goto err_cm;
2854
2855 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
2856 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
2857 sizeof(*sdev->ioctx_ring[0]),
2858 srp_max_req_size, DMA_FROM_DEVICE);
2859 if (!sdev->ioctx_ring)
2860 goto err_event;
2861
2862 for (i = 0; i < sdev->srq_size; ++i)
2863 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
2864
Roland Dreierf2250662012-02-02 12:55:58 -08002865 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002866
2867 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
2868 sport = &sdev->port[i - 1];
2869 sport->sdev = sdev;
2870 sport->port = i;
2871 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
2872 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
2873 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
2874 INIT_WORK(&sport->work, srpt_refresh_port_work);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002875
2876 if (srpt_refresh_port(sport)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002877 pr_err("MAD registration failed for %s-%d.\n",
Bart Van Asschef68cba4e92016-02-11 11:04:20 -08002878 sdev->device->name, i);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002879 goto err_ring;
2880 }
2881 snprintf(sport->port_guid, sizeof(sport->port_guid),
2882 "0x%016llx%016llx",
2883 be64_to_cpu(sport->gid.global.subnet_prefix),
2884 be64_to_cpu(sport->gid.global.interface_id));
2885 }
2886
2887 spin_lock(&srpt_dev_lock);
2888 list_add_tail(&sdev->list, &srpt_dev_list);
2889 spin_unlock(&srpt_dev_lock);
2890
2891out:
2892 ib_set_client_data(device, &srpt_client, sdev);
2893 pr_debug("added %s.\n", device->name);
2894 return;
2895
2896err_ring:
2897 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2898 sdev->srq_size, srp_max_req_size,
2899 DMA_FROM_DEVICE);
2900err_event:
2901 ib_unregister_event_handler(&sdev->event_handler);
2902err_cm:
2903 ib_destroy_cm_id(sdev->cm_id);
2904err_srq:
2905 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002906err_pd:
2907 ib_dealloc_pd(sdev->pd);
2908free_dev:
2909 kfree(sdev);
2910err:
2911 sdev = NULL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002912 pr_info("%s(%s) failed.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002913 goto out;
2914}
2915
2916/**
2917 * srpt_remove_one() - InfiniBand device removal callback function.
2918 */
Haggai Eran7c1eb452015-07-30 17:50:14 +03002919static void srpt_remove_one(struct ib_device *device, void *client_data)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002920{
Haggai Eran7c1eb452015-07-30 17:50:14 +03002921 struct srpt_device *sdev = client_data;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002922 int i;
2923
Bart Van Asschea42d9852011-10-14 01:30:46 +00002924 if (!sdev) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002925 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002926 return;
2927 }
2928
2929 srpt_unregister_mad_agent(sdev);
2930
2931 ib_unregister_event_handler(&sdev->event_handler);
2932
2933 /* Cancel any work queued by the just unregistered IB event handler. */
2934 for (i = 0; i < sdev->device->phys_port_cnt; i++)
2935 cancel_work_sync(&sdev->port[i].work);
2936
2937 ib_destroy_cm_id(sdev->cm_id);
2938
2939 /*
2940 * Unregistering a target must happen after destroying sdev->cm_id
2941 * such that no new SRP_LOGIN_REQ information units can arrive while
2942 * destroying the target.
2943 */
2944 spin_lock(&srpt_dev_lock);
2945 list_del(&sdev->list);
2946 spin_unlock(&srpt_dev_lock);
2947 srpt_release_sdev(sdev);
2948
2949 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002950 ib_dealloc_pd(sdev->pd);
2951
2952 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2953 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
2954 sdev->ioctx_ring = NULL;
2955 kfree(sdev);
2956}
2957
2958static struct ib_client srpt_client = {
2959 .name = DRV_NAME,
2960 .add = srpt_add_one,
2961 .remove = srpt_remove_one
2962};
2963
2964static int srpt_check_true(struct se_portal_group *se_tpg)
2965{
2966 return 1;
2967}
2968
2969static int srpt_check_false(struct se_portal_group *se_tpg)
2970{
2971 return 0;
2972}
2973
2974static char *srpt_get_fabric_name(void)
2975{
2976 return "srpt";
2977}
2978
Bart Van Asschea42d9852011-10-14 01:30:46 +00002979static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
2980{
2981 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
2982
2983 return sport->port_guid;
2984}
2985
2986static u16 srpt_get_tag(struct se_portal_group *tpg)
2987{
2988 return 1;
2989}
2990
Bart Van Asschea42d9852011-10-14 01:30:46 +00002991static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
2992{
2993 return 1;
2994}
2995
2996static void srpt_release_cmd(struct se_cmd *se_cmd)
2997{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002998 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
2999 struct srpt_send_ioctx, cmd);
3000 struct srpt_rdma_ch *ch = ioctx->ch;
3001 unsigned long flags;
3002
3003 WARN_ON(ioctx->state != SRPT_STATE_DONE);
3004 WARN_ON(ioctx->mapped_sg_count != 0);
3005
3006 if (ioctx->n_rbuf > 1) {
3007 kfree(ioctx->rbufs);
3008 ioctx->rbufs = NULL;
3009 ioctx->n_rbuf = 0;
3010 }
3011
3012 spin_lock_irqsave(&ch->spinlock, flags);
3013 list_add(&ioctx->free_list, &ch->free_list);
3014 spin_unlock_irqrestore(&ch->spinlock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003015}
3016
3017/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003018 * srpt_close_session() - Forcibly close a session.
3019 *
3020 * Callback function invoked by the TCM core to clean up sessions associated
3021 * with a node ACL when the user invokes
3022 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3023 */
3024static void srpt_close_session(struct se_session *se_sess)
3025{
3026 DECLARE_COMPLETION_ONSTACK(release_done);
Bart Van Asschef108f0f2016-02-11 11:06:14 -08003027 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
3028 struct srpt_device *sdev = ch->sport->sdev;
3029 bool wait;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003030
Bart Van Asschef108f0f2016-02-11 11:06:14 -08003031 pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num,
3032 ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003033
Bart Van Asschea42d9852011-10-14 01:30:46 +00003034 spin_lock_irq(&sdev->spinlock);
3035 BUG_ON(ch->release_done);
3036 ch->release_done = &release_done;
Bart Van Asschef108f0f2016-02-11 11:06:14 -08003037 wait = !list_empty(&ch->list);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003038 __srpt_close_ch(ch);
3039 spin_unlock_irq(&sdev->spinlock);
3040
Bart Van Asschef108f0f2016-02-11 11:06:14 -08003041 if (!wait)
3042 return;
3043
3044 while (wait_for_completion_timeout(&release_done, 180 * HZ) == 0)
3045 pr_info("%s(%s-%d state %d): still waiting ...\n", __func__,
3046 ch->sess_name, ch->qp->qp_num, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003047}
3048
3049/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003050 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3051 *
3052 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3053 * This object represents an arbitrary integer used to uniquely identify a
3054 * particular attached remote initiator port to a particular SCSI target port
3055 * within a particular SCSI target device within a particular SCSI instance.
3056 */
3057static u32 srpt_sess_get_index(struct se_session *se_sess)
3058{
3059 return 0;
3060}
3061
3062static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3063{
3064}
3065
Bart Van Asschea42d9852011-10-14 01:30:46 +00003066/* Note: only used from inside debug printk's by the TCM core. */
3067static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3068{
3069 struct srpt_send_ioctx *ioctx;
3070
3071 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3072 return srpt_get_cmd_state(ioctx);
3073}
3074
Bart Van Asschea42d9852011-10-14 01:30:46 +00003075/**
3076 * srpt_parse_i_port_id() - Parse an initiator port ID.
3077 * @name: ASCII representation of a 128-bit initiator port ID.
3078 * @i_port_id: Binary 128-bit port ID.
3079 */
3080static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3081{
3082 const char *p;
3083 unsigned len, count, leading_zero_bytes;
3084 int ret, rc;
3085
3086 p = name;
Rasmus Villemoesb60459f2014-10-13 15:54:46 -07003087 if (strncasecmp(p, "0x", 2) == 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003088 p += 2;
3089 ret = -EINVAL;
3090 len = strlen(p);
3091 if (len % 2)
3092 goto out;
3093 count = min(len / 2, 16U);
3094 leading_zero_bytes = 16 - count;
3095 memset(i_port_id, 0, leading_zero_bytes);
3096 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3097 if (rc < 0)
3098 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3099 ret = 0;
3100out:
3101 return ret;
3102}
3103
3104/*
3105 * configfs callback function invoked for
3106 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3107 */
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003108static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003109{
Bart Van Asschea42d9852011-10-14 01:30:46 +00003110 u8 i_port_id[16];
3111
3112 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003113 pr_err("invalid initiator port ID %s\n", name);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003114 return -EINVAL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003115 }
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003116 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003117}
3118
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003119static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
3120 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003121{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003122 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003123 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3124
3125 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3126}
3127
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003128static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
3129 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003130{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003131 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003132 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3133 unsigned long val;
3134 int ret;
3135
Jingoo Han9d8abf42014-02-05 11:22:05 +09003136 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003137 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003138 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003139 return -EINVAL;
3140 }
3141 if (val > MAX_SRPT_RDMA_SIZE) {
3142 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3143 MAX_SRPT_RDMA_SIZE);
3144 return -EINVAL;
3145 }
3146 if (val < DEFAULT_MAX_RDMA_SIZE) {
3147 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3148 val, DEFAULT_MAX_RDMA_SIZE);
3149 return -EINVAL;
3150 }
3151 sport->port_attrib.srp_max_rdma_size = val;
3152
3153 return count;
3154}
3155
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003156static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
3157 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003158{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003159 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003160 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3161
3162 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3163}
3164
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003165static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
3166 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003167{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003168 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003169 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3170 unsigned long val;
3171 int ret;
3172
Jingoo Han9d8abf42014-02-05 11:22:05 +09003173 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003174 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003175 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003176 return -EINVAL;
3177 }
3178 if (val > MAX_SRPT_RSP_SIZE) {
3179 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3180 MAX_SRPT_RSP_SIZE);
3181 return -EINVAL;
3182 }
3183 if (val < MIN_MAX_RSP_SIZE) {
3184 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3185 MIN_MAX_RSP_SIZE);
3186 return -EINVAL;
3187 }
3188 sport->port_attrib.srp_max_rsp_size = val;
3189
3190 return count;
3191}
3192
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003193static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
3194 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003195{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003196 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003197 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3198
3199 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3200}
3201
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003202static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
3203 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003204{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003205 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003206 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3207 unsigned long val;
3208 int ret;
3209
Jingoo Han9d8abf42014-02-05 11:22:05 +09003210 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003211 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003212 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003213 return -EINVAL;
3214 }
3215 if (val > MAX_SRPT_SRQ_SIZE) {
3216 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3217 MAX_SRPT_SRQ_SIZE);
3218 return -EINVAL;
3219 }
3220 if (val < MIN_SRPT_SRQ_SIZE) {
3221 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3222 MIN_SRPT_SRQ_SIZE);
3223 return -EINVAL;
3224 }
3225 sport->port_attrib.srp_sq_size = val;
3226
3227 return count;
3228}
3229
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003230CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
3231CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
3232CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003233
3234static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003235 &srpt_tpg_attrib_attr_srp_max_rdma_size,
3236 &srpt_tpg_attrib_attr_srp_max_rsp_size,
3237 &srpt_tpg_attrib_attr_srp_sq_size,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003238 NULL,
3239};
3240
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003241static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003242{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003243 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003244 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3245
3246 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3247}
3248
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003249static ssize_t srpt_tpg_enable_store(struct config_item *item,
3250 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003251{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003252 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003253 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3254 unsigned long tmp;
3255 int ret;
3256
Jingoo Han9d8abf42014-02-05 11:22:05 +09003257 ret = kstrtoul(page, 0, &tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003258 if (ret < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003259 pr_err("Unable to extract srpt_tpg_store_enable\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003260 return -EINVAL;
3261 }
3262
3263 if ((tmp != 0) && (tmp != 1)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003264 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003265 return -EINVAL;
3266 }
3267 if (tmp == 1)
3268 sport->enabled = true;
3269 else
3270 sport->enabled = false;
3271
3272 return count;
3273}
3274
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003275CONFIGFS_ATTR(srpt_tpg_, enable);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003276
3277static struct configfs_attribute *srpt_tpg_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003278 &srpt_tpg_attr_enable,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003279 NULL,
3280};
3281
3282/**
3283 * configfs callback invoked for
3284 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3285 */
3286static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3287 struct config_group *group,
3288 const char *name)
3289{
3290 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3291 int res;
3292
3293 /* Initialize sport->port_wwn and sport->port_tpg_1 */
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07003294 res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003295 if (res)
3296 return ERR_PTR(res);
3297
3298 return &sport->port_tpg_1;
3299}
3300
3301/**
3302 * configfs callback invoked for
3303 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3304 */
3305static void srpt_drop_tpg(struct se_portal_group *tpg)
3306{
3307 struct srpt_port *sport = container_of(tpg,
3308 struct srpt_port, port_tpg_1);
3309
3310 sport->enabled = false;
3311 core_tpg_deregister(&sport->port_tpg_1);
3312}
3313
3314/**
3315 * configfs callback invoked for
3316 * mkdir /sys/kernel/config/target/$driver/$port
3317 */
3318static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3319 struct config_group *group,
3320 const char *name)
3321{
3322 struct srpt_port *sport;
3323 int ret;
3324
3325 sport = srpt_lookup_port(name);
3326 pr_debug("make_tport(%s)\n", name);
3327 ret = -EINVAL;
3328 if (!sport)
3329 goto err;
3330
3331 return &sport->port_wwn;
3332
3333err:
3334 return ERR_PTR(ret);
3335}
3336
3337/**
3338 * configfs callback invoked for
3339 * rmdir /sys/kernel/config/target/$driver/$port
3340 */
3341static void srpt_drop_tport(struct se_wwn *wwn)
3342{
3343 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3344
3345 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3346}
3347
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003348static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003349{
3350 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3351}
3352
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003353CONFIGFS_ATTR_RO(srpt_wwn_, version);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003354
3355static struct configfs_attribute *srpt_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003356 &srpt_wwn_attr_version,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003357 NULL,
3358};
3359
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003360static const struct target_core_fabric_ops srpt_template = {
3361 .module = THIS_MODULE,
3362 .name = "srpt",
Bart Van Asschea42d9852011-10-14 01:30:46 +00003363 .get_fabric_name = srpt_get_fabric_name,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003364 .tpg_get_wwn = srpt_get_fabric_wwn,
3365 .tpg_get_tag = srpt_get_tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003366 .tpg_check_demo_mode = srpt_check_false,
3367 .tpg_check_demo_mode_cache = srpt_check_true,
3368 .tpg_check_demo_mode_write_protect = srpt_check_true,
3369 .tpg_check_prod_mode_write_protect = srpt_check_false,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003370 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3371 .release_cmd = srpt_release_cmd,
3372 .check_stop_free = srpt_check_stop_free,
3373 .shutdown_session = srpt_shutdown_session,
3374 .close_session = srpt_close_session,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003375 .sess_get_index = srpt_sess_get_index,
3376 .sess_get_initiator_sid = NULL,
3377 .write_pending = srpt_write_pending,
3378 .write_pending_status = srpt_write_pending_status,
3379 .set_default_node_attributes = srpt_set_default_node_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003380 .get_cmd_state = srpt_get_tcm_cmd_state,
Joern Engelb79fafa2013-07-03 11:22:17 -04003381 .queue_data_in = srpt_queue_data_in,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003382 .queue_status = srpt_queue_status,
Joern Engelb79fafa2013-07-03 11:22:17 -04003383 .queue_tm_rsp = srpt_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003384 .aborted_task = srpt_aborted_task,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003385 /*
3386 * Setup function pointers for generic logic in
3387 * target_core_fabric_configfs.c
3388 */
3389 .fabric_make_wwn = srpt_make_tport,
3390 .fabric_drop_wwn = srpt_drop_tport,
3391 .fabric_make_tpg = srpt_make_tpg,
3392 .fabric_drop_tpg = srpt_drop_tpg,
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003393 .fabric_init_nodeacl = srpt_init_nodeacl,
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003394
3395 .tfc_wwn_attrs = srpt_wwn_attrs,
3396 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3397 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003398};
3399
3400/**
3401 * srpt_init_module() - Kernel module initialization.
3402 *
3403 * Note: Since ib_register_client() registers callback functions, and since at
3404 * least one of these callback functions (srpt_add_one()) calls target core
3405 * functions, this driver must be registered with the target core before
3406 * ib_register_client() is called.
3407 */
3408static int __init srpt_init_module(void)
3409{
3410 int ret;
3411
3412 ret = -EINVAL;
3413 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003414 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003415 " srp_max_req_size -- must be at least %d.\n",
3416 srp_max_req_size, MIN_MAX_REQ_SIZE);
3417 goto out;
3418 }
3419
3420 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3421 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003422 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003423 " srpt_srq_size -- must be in the range [%d..%d].\n",
3424 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3425 goto out;
3426 }
3427
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003428 ret = target_register_template(&srpt_template);
3429 if (ret)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003430 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003431
3432 ret = ib_register_client(&srpt_client);
3433 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003434 pr_err("couldn't register IB client\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003435 goto out_unregister_target;
3436 }
3437
3438 return 0;
3439
3440out_unregister_target:
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003441 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003442out:
3443 return ret;
3444}
3445
3446static void __exit srpt_cleanup_module(void)
3447{
3448 ib_unregister_client(&srpt_client);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003449 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003450}
3451
3452module_init(srpt_init_module);
3453module_exit(srpt_cleanup_module);