blob: 716f4292838a0007866cb65f9670fdeaa3ebd897 [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 Asschea42d9852011-10-14 01:30:46 +000094static void srpt_release_channel(struct srpt_rdma_ch *ch);
95static int srpt_queue_status(struct se_cmd *cmd);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +020096static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc);
97static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc);
Bart Van Asschea42d9852011-10-14 01:30:46 +000098
Bart Van Asschea42d9852011-10-14 01:30:46 +000099static enum rdma_ch_state
100srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new_state)
101{
102 unsigned long flags;
103 enum rdma_ch_state prev;
104
105 spin_lock_irqsave(&ch->spinlock, flags);
106 prev = ch->state;
107 ch->state = new_state;
108 spin_unlock_irqrestore(&ch->spinlock, flags);
109 return prev;
110}
111
112/**
113 * srpt_test_and_set_ch_state() - Test and set the channel state.
114 *
115 * Returns true if and only if the channel state has been set to the new state.
116 */
117static bool
118srpt_test_and_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state old,
119 enum rdma_ch_state new)
120{
121 unsigned long flags;
122 enum rdma_ch_state prev;
123
124 spin_lock_irqsave(&ch->spinlock, flags);
125 prev = ch->state;
126 if (prev == old)
127 ch->state = new;
128 spin_unlock_irqrestore(&ch->spinlock, flags);
129 return prev == old;
130}
131
132/**
133 * srpt_event_handler() - Asynchronous IB event callback function.
134 *
135 * Callback function called by the InfiniBand core when an asynchronous IB
136 * event occurs. This callback may occur in interrupt context. See also
137 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
138 * Architecture Specification.
139 */
140static void srpt_event_handler(struct ib_event_handler *handler,
141 struct ib_event *event)
142{
143 struct srpt_device *sdev;
144 struct srpt_port *sport;
145
146 sdev = ib_get_client_data(event->device, &srpt_client);
147 if (!sdev || sdev->device != event->device)
148 return;
149
150 pr_debug("ASYNC event= %d on device= %s\n", event->event,
Bart Van Asschef68cba4e92016-02-11 11:04:20 -0800151 sdev->device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000152
153 switch (event->event) {
154 case IB_EVENT_PORT_ERR:
155 if (event->element.port_num <= sdev->device->phys_port_cnt) {
156 sport = &sdev->port[event->element.port_num - 1];
157 sport->lid = 0;
158 sport->sm_lid = 0;
159 }
160 break;
161 case IB_EVENT_PORT_ACTIVE:
162 case IB_EVENT_LID_CHANGE:
163 case IB_EVENT_PKEY_CHANGE:
164 case IB_EVENT_SM_CHANGE:
165 case IB_EVENT_CLIENT_REREGISTER:
Doug Ledford2aa1cf62014-08-12 19:20:10 -0400166 case IB_EVENT_GID_CHANGE:
Bart Van Asschea42d9852011-10-14 01:30:46 +0000167 /* Refresh port data asynchronously. */
168 if (event->element.port_num <= sdev->device->phys_port_cnt) {
169 sport = &sdev->port[event->element.port_num - 1];
170 if (!sport->lid && !sport->sm_lid)
171 schedule_work(&sport->work);
172 }
173 break;
174 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400175 pr_err("received unrecognized IB event %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +0000176 event->event);
177 break;
178 }
179}
180
181/**
182 * srpt_srq_event() - SRQ event callback function.
183 */
184static void srpt_srq_event(struct ib_event *event, void *ctx)
185{
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400186 pr_info("SRQ event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000187}
188
189/**
190 * srpt_qp_event() - QP event callback function.
191 */
192static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
193{
194 pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
Bart Van Assche33912d72016-02-11 11:04:43 -0800195 event->event, ch->cm_id, ch->sess_name, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000196
197 switch (event->event) {
198 case IB_EVENT_COMM_EST:
199 ib_cm_notify(ch->cm_id, event->event);
200 break;
201 case IB_EVENT_QP_LAST_WQE_REACHED:
202 if (srpt_test_and_set_ch_state(ch, CH_DRAINING,
203 CH_RELEASING))
204 srpt_release_channel(ch);
205 else
206 pr_debug("%s: state %d - ignored LAST_WQE.\n",
Bart Van Assche33912d72016-02-11 11:04:43 -0800207 ch->sess_name, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000208 break;
209 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400210 pr_err("received unrecognized IB QP event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000211 break;
212 }
213}
214
215/**
216 * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
217 *
218 * @slot: one-based slot number.
219 * @value: four-bit value.
220 *
221 * Copies the lowest four bits of value in element slot of the array of four
222 * bit elements called c_list (controller list). The index slot is one-based.
223 */
224static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
225{
226 u16 id;
227 u8 tmp;
228
229 id = (slot - 1) / 2;
230 if (slot & 0x1) {
231 tmp = c_list[id] & 0xf;
232 c_list[id] = (value << 4) | tmp;
233 } else {
234 tmp = c_list[id] & 0xf0;
235 c_list[id] = (value & 0xf) | tmp;
236 }
237}
238
239/**
240 * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
241 *
242 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
243 * Specification.
244 */
245static void srpt_get_class_port_info(struct ib_dm_mad *mad)
246{
247 struct ib_class_port_info *cif;
248
249 cif = (struct ib_class_port_info *)mad->data;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800250 memset(cif, 0, sizeof(*cif));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000251 cif->base_version = 1;
252 cif->class_version = 1;
253 cif->resp_time_value = 20;
254
255 mad->mad_hdr.status = 0;
256}
257
258/**
259 * srpt_get_iou() - Write IOUnitInfo to a management datagram.
260 *
261 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
262 * Specification. See also section B.7, table B.6 in the SRP r16a document.
263 */
264static void srpt_get_iou(struct ib_dm_mad *mad)
265{
266 struct ib_dm_iou_info *ioui;
267 u8 slot;
268 int i;
269
270 ioui = (struct ib_dm_iou_info *)mad->data;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530271 ioui->change_id = cpu_to_be16(1);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000272 ioui->max_controllers = 16;
273
274 /* set present for slot 1 and empty for the rest */
275 srpt_set_ioc(ioui->controller_list, 1, 1);
276 for (i = 1, slot = 2; i < 16; i++, slot++)
277 srpt_set_ioc(ioui->controller_list, slot, 0);
278
279 mad->mad_hdr.status = 0;
280}
281
282/**
283 * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
284 *
285 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
286 * Architecture Specification. See also section B.7, table B.7 in the SRP
287 * r16a document.
288 */
289static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
290 struct ib_dm_mad *mad)
291{
292 struct srpt_device *sdev = sport->sdev;
293 struct ib_dm_ioc_profile *iocp;
294
295 iocp = (struct ib_dm_ioc_profile *)mad->data;
296
297 if (!slot || slot > 16) {
298 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530299 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000300 return;
301 }
302
303 if (slot > 2) {
304 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530305 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000306 return;
307 }
308
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800309 memset(iocp, 0, sizeof(*iocp));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000310 strcpy(iocp->id_string, SRPT_ID_STRING);
311 iocp->guid = cpu_to_be64(srpt_service_guid);
Or Gerlitz4a061b22015-12-18 10:59:46 +0200312 iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
313 iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
314 iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
315 iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000316 iocp->subsys_device_id = 0x0;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530317 iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
318 iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
319 iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
320 iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000321 iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
322 iocp->rdma_read_depth = 4;
323 iocp->send_size = cpu_to_be32(srp_max_req_size);
324 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
325 1U << 24));
326 iocp->num_svc_entries = 1;
327 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
328 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
329
330 mad->mad_hdr.status = 0;
331}
332
333/**
334 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
335 *
336 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
337 * Specification. See also section B.7, table B.8 in the SRP r16a document.
338 */
339static void srpt_get_svc_entries(u64 ioc_guid,
340 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
341{
342 struct ib_dm_svc_entries *svc_entries;
343
344 WARN_ON(!ioc_guid);
345
346 if (!slot || slot > 16) {
347 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530348 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000349 return;
350 }
351
352 if (slot > 2 || lo > hi || hi > 1) {
353 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530354 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000355 return;
356 }
357
358 svc_entries = (struct ib_dm_svc_entries *)mad->data;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800359 memset(svc_entries, 0, sizeof(*svc_entries));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000360 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
361 snprintf(svc_entries->service_entries[0].name,
362 sizeof(svc_entries->service_entries[0].name),
363 "%s%016llx",
364 SRP_SERVICE_NAME_PREFIX,
365 ioc_guid);
366
367 mad->mad_hdr.status = 0;
368}
369
370/**
371 * srpt_mgmt_method_get() - Process a received management datagram.
372 * @sp: source port through which the MAD has been received.
373 * @rq_mad: received MAD.
374 * @rsp_mad: response MAD.
375 */
376static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
377 struct ib_dm_mad *rsp_mad)
378{
379 u16 attr_id;
380 u32 slot;
381 u8 hi, lo;
382
383 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
384 switch (attr_id) {
385 case DM_ATTR_CLASS_PORT_INFO:
386 srpt_get_class_port_info(rsp_mad);
387 break;
388 case DM_ATTR_IOU_INFO:
389 srpt_get_iou(rsp_mad);
390 break;
391 case DM_ATTR_IOC_PROFILE:
392 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
393 srpt_get_ioc(sp, slot, rsp_mad);
394 break;
395 case DM_ATTR_SVC_ENTRIES:
396 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
397 hi = (u8) ((slot >> 8) & 0xff);
398 lo = (u8) (slot & 0xff);
399 slot = (u16) ((slot >> 16) & 0xffff);
400 srpt_get_svc_entries(srpt_service_guid,
401 slot, hi, lo, rsp_mad);
402 break;
403 default:
404 rsp_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530405 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000406 break;
407 }
408}
409
410/**
411 * srpt_mad_send_handler() - Post MAD-send callback function.
412 */
413static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
414 struct ib_mad_send_wc *mad_wc)
415{
416 ib_destroy_ah(mad_wc->send_buf->ah);
417 ib_free_send_mad(mad_wc->send_buf);
418}
419
420/**
421 * srpt_mad_recv_handler() - MAD reception callback function.
422 */
423static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
Christoph Hellwigca281262016-01-04 14:15:58 +0100424 struct ib_mad_send_buf *send_buf,
Bart Van Asschea42d9852011-10-14 01:30:46 +0000425 struct ib_mad_recv_wc *mad_wc)
426{
427 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
428 struct ib_ah *ah;
429 struct ib_mad_send_buf *rsp;
430 struct ib_dm_mad *dm_mad;
431
432 if (!mad_wc || !mad_wc->recv_buf.mad)
433 return;
434
435 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
436 mad_wc->recv_buf.grh, mad_agent->port_num);
437 if (IS_ERR(ah))
438 goto err;
439
440 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
441
442 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
443 mad_wc->wc->pkey_index, 0,
444 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
Ira Weinyda2dfaa2015-06-06 14:38:28 -0400445 GFP_KERNEL,
446 IB_MGMT_BASE_VERSION);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000447 if (IS_ERR(rsp))
448 goto err_rsp;
449
450 rsp->ah = ah;
451
452 dm_mad = rsp->mad;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800453 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000454 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
455 dm_mad->mad_hdr.status = 0;
456
457 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
458 case IB_MGMT_METHOD_GET:
459 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
460 break;
461 case IB_MGMT_METHOD_SET:
462 dm_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530463 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000464 break;
465 default:
466 dm_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530467 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000468 break;
469 }
470
471 if (!ib_post_send_mad(rsp, NULL)) {
472 ib_free_recv_mad(mad_wc);
473 /* will destroy_ah & free_send_mad in send completion */
474 return;
475 }
476
477 ib_free_send_mad(rsp);
478
479err_rsp:
480 ib_destroy_ah(ah);
481err:
482 ib_free_recv_mad(mad_wc);
483}
484
485/**
486 * srpt_refresh_port() - Configure a HCA port.
487 *
488 * Enable InfiniBand management datagram processing, update the cached sm_lid,
489 * lid and gid values, and register a callback function for processing MADs
490 * on the specified port.
491 *
492 * Note: It is safe to call this function more than once for the same port.
493 */
494static int srpt_refresh_port(struct srpt_port *sport)
495{
496 struct ib_mad_reg_req reg_req;
497 struct ib_port_modify port_modify;
498 struct ib_port_attr port_attr;
499 int ret;
500
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800501 memset(&port_modify, 0, sizeof(port_modify));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000502 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
503 port_modify.clr_port_cap_mask = 0;
504
505 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
506 if (ret)
507 goto err_mod_port;
508
509 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
510 if (ret)
511 goto err_query_port;
512
513 sport->sm_lid = port_attr.sm_lid;
514 sport->lid = port_attr.lid;
515
Matan Barak55ee3ab2015-10-15 18:38:45 +0300516 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid,
517 NULL);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000518 if (ret)
519 goto err_query_port;
520
521 if (!sport->mad_agent) {
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800522 memset(&reg_req, 0, sizeof(reg_req));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000523 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
524 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
525 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
526 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
527
528 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
529 sport->port,
530 IB_QPT_GSI,
531 &reg_req, 0,
532 srpt_mad_send_handler,
533 srpt_mad_recv_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -0400534 sport, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000535 if (IS_ERR(sport->mad_agent)) {
536 ret = PTR_ERR(sport->mad_agent);
537 sport->mad_agent = NULL;
538 goto err_query_port;
539 }
540 }
541
542 return 0;
543
544err_query_port:
545
546 port_modify.set_port_cap_mask = 0;
547 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
548 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
549
550err_mod_port:
551
552 return ret;
553}
554
555/**
556 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
557 *
558 * Note: It is safe to call this function more than once for the same device.
559 */
560static void srpt_unregister_mad_agent(struct srpt_device *sdev)
561{
562 struct ib_port_modify port_modify = {
563 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
564 };
565 struct srpt_port *sport;
566 int i;
567
568 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
569 sport = &sdev->port[i - 1];
570 WARN_ON(sport->port != i);
571 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400572 pr_err("disabling MAD processing failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000573 if (sport->mad_agent) {
574 ib_unregister_mad_agent(sport->mad_agent);
575 sport->mad_agent = NULL;
576 }
577 }
578}
579
580/**
581 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
582 */
583static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
584 int ioctx_size, int dma_size,
585 enum dma_data_direction dir)
586{
587 struct srpt_ioctx *ioctx;
588
589 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
590 if (!ioctx)
591 goto err;
592
593 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
594 if (!ioctx->buf)
595 goto err_free_ioctx;
596
597 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
598 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
599 goto err_free_buf;
600
601 return ioctx;
602
603err_free_buf:
604 kfree(ioctx->buf);
605err_free_ioctx:
606 kfree(ioctx);
607err:
608 return NULL;
609}
610
611/**
612 * srpt_free_ioctx() - Free an SRPT I/O context structure.
613 */
614static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
615 int dma_size, enum dma_data_direction dir)
616{
617 if (!ioctx)
618 return;
619
620 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
621 kfree(ioctx->buf);
622 kfree(ioctx);
623}
624
625/**
626 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
627 * @sdev: Device to allocate the I/O context ring for.
628 * @ring_size: Number of elements in the I/O context ring.
629 * @ioctx_size: I/O context size.
630 * @dma_size: DMA buffer size.
631 * @dir: DMA data direction.
632 */
633static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
634 int ring_size, int ioctx_size,
635 int dma_size, enum dma_data_direction dir)
636{
637 struct srpt_ioctx **ring;
638 int i;
639
640 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
641 && ioctx_size != sizeof(struct srpt_send_ioctx));
642
643 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
644 if (!ring)
645 goto out;
646 for (i = 0; i < ring_size; ++i) {
647 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
648 if (!ring[i])
649 goto err;
650 ring[i]->index = i;
651 }
652 goto out;
653
654err:
655 while (--i >= 0)
656 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
657 kfree(ring);
Jesper Juhl715252d2012-02-04 23:49:40 +0100658 ring = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000659out:
660 return ring;
661}
662
663/**
664 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
665 */
666static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
667 struct srpt_device *sdev, int ring_size,
668 int dma_size, enum dma_data_direction dir)
669{
670 int i;
671
672 for (i = 0; i < ring_size; ++i)
673 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
674 kfree(ioctx_ring);
675}
676
677/**
678 * srpt_get_cmd_state() - Get the state of a SCSI command.
679 */
680static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
681{
682 enum srpt_command_state state;
683 unsigned long flags;
684
685 BUG_ON(!ioctx);
686
687 spin_lock_irqsave(&ioctx->spinlock, flags);
688 state = ioctx->state;
689 spin_unlock_irqrestore(&ioctx->spinlock, flags);
690 return state;
691}
692
693/**
694 * srpt_set_cmd_state() - Set the state of a SCSI command.
695 *
696 * Does not modify the state of aborted commands. Returns the previous command
697 * state.
698 */
699static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
700 enum srpt_command_state new)
701{
702 enum srpt_command_state previous;
703 unsigned long flags;
704
705 BUG_ON(!ioctx);
706
707 spin_lock_irqsave(&ioctx->spinlock, flags);
708 previous = ioctx->state;
709 if (previous != SRPT_STATE_DONE)
710 ioctx->state = new;
711 spin_unlock_irqrestore(&ioctx->spinlock, flags);
712
713 return previous;
714}
715
716/**
717 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
718 *
719 * Returns true if and only if the previous command state was equal to 'old'.
720 */
721static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
722 enum srpt_command_state old,
723 enum srpt_command_state new)
724{
725 enum srpt_command_state previous;
726 unsigned long flags;
727
728 WARN_ON(!ioctx);
729 WARN_ON(old == SRPT_STATE_DONE);
730 WARN_ON(new == SRPT_STATE_NEW);
731
732 spin_lock_irqsave(&ioctx->spinlock, flags);
733 previous = ioctx->state;
734 if (previous == old)
735 ioctx->state = new;
736 spin_unlock_irqrestore(&ioctx->spinlock, flags);
737 return previous == old;
738}
739
740/**
741 * srpt_post_recv() - Post an IB receive request.
742 */
743static int srpt_post_recv(struct srpt_device *sdev,
744 struct srpt_recv_ioctx *ioctx)
745{
746 struct ib_sge list;
747 struct ib_recv_wr wr, *bad_wr;
748
749 BUG_ON(!sdev);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000750 list.addr = ioctx->ioctx.dma;
751 list.length = srp_max_req_size;
Jason Gunthorpe5a783952015-07-30 17:22:24 -0600752 list.lkey = sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000753
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200754 ioctx->ioctx.cqe.done = srpt_recv_done;
755 wr.wr_cqe = &ioctx->ioctx.cqe;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000756 wr.next = NULL;
757 wr.sg_list = &list;
758 wr.num_sge = 1;
759
760 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
761}
762
763/**
764 * srpt_post_send() - Post an IB send request.
765 *
766 * Returns zero upon success and a non-zero value upon failure.
767 */
768static int srpt_post_send(struct srpt_rdma_ch *ch,
769 struct srpt_send_ioctx *ioctx, int len)
770{
771 struct ib_sge list;
772 struct ib_send_wr wr, *bad_wr;
773 struct srpt_device *sdev = ch->sport->sdev;
774 int ret;
775
776 atomic_inc(&ch->req_lim);
777
778 ret = -ENOMEM;
779 if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400780 pr_warn("IB send queue full (needed 1)\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000781 goto out;
782 }
783
784 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
785 DMA_TO_DEVICE);
786
787 list.addr = ioctx->ioctx.dma;
788 list.length = len;
Jason Gunthorpe5a783952015-07-30 17:22:24 -0600789 list.lkey = sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000790
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200791 ioctx->ioctx.cqe.done = srpt_send_done;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000792 wr.next = NULL;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200793 wr.wr_cqe = &ioctx->ioctx.cqe;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000794 wr.sg_list = &list;
795 wr.num_sge = 1;
796 wr.opcode = IB_WR_SEND;
797 wr.send_flags = IB_SEND_SIGNALED;
798
799 ret = ib_post_send(ch->qp, &wr, &bad_wr);
800
801out:
802 if (ret < 0) {
803 atomic_inc(&ch->sq_wr_avail);
804 atomic_dec(&ch->req_lim);
805 }
806 return ret;
807}
808
809/**
810 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
811 * @ioctx: Pointer to the I/O context associated with the request.
812 * @srp_cmd: Pointer to the SRP_CMD request data.
813 * @dir: Pointer to the variable to which the transfer direction will be
814 * written.
815 * @data_len: Pointer to the variable to which the total data length of all
816 * descriptors in the SRP_CMD request will be written.
817 *
818 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
819 *
820 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
821 * -ENOMEM when memory allocation fails and zero upon success.
822 */
823static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
824 struct srp_cmd *srp_cmd,
825 enum dma_data_direction *dir, u64 *data_len)
826{
827 struct srp_indirect_buf *idb;
828 struct srp_direct_buf *db;
829 unsigned add_cdb_offset;
830 int ret;
831
832 /*
833 * The pointer computations below will only be compiled correctly
834 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
835 * whether srp_cmd::add_data has been declared as a byte pointer.
836 */
837 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
838 && !__same_type(srp_cmd->add_data[0], (u8)0));
839
840 BUG_ON(!dir);
841 BUG_ON(!data_len);
842
843 ret = 0;
844 *data_len = 0;
845
846 /*
847 * The lower four bits of the buffer format field contain the DATA-IN
848 * buffer descriptor format, and the highest four bits contain the
849 * DATA-OUT buffer descriptor format.
850 */
851 *dir = DMA_NONE;
852 if (srp_cmd->buf_fmt & 0xf)
853 /* DATA-IN: transfer data from target to initiator (read). */
854 *dir = DMA_FROM_DEVICE;
855 else if (srp_cmd->buf_fmt >> 4)
856 /* DATA-OUT: transfer data from initiator to target (write). */
857 *dir = DMA_TO_DEVICE;
858
859 /*
860 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
861 * CDB LENGTH' field are reserved and the size in bytes of this field
862 * is four times the value specified in bits 3..7. Hence the "& ~3".
863 */
864 add_cdb_offset = srp_cmd->add_cdb_len & ~3;
865 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
866 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
867 ioctx->n_rbuf = 1;
868 ioctx->rbufs = &ioctx->single_rbuf;
869
870 db = (struct srp_direct_buf *)(srp_cmd->add_data
871 + add_cdb_offset);
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800872 memcpy(ioctx->rbufs, db, sizeof(*db));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000873 *data_len = be32_to_cpu(db->len);
874 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
875 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
876 idb = (struct srp_indirect_buf *)(srp_cmd->add_data
877 + add_cdb_offset);
878
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800879 ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof(*db);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000880
881 if (ioctx->n_rbuf >
882 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400883 pr_err("received unsupported SRP_CMD request"
Bart Van Asschea42d9852011-10-14 01:30:46 +0000884 " type (%u out + %u in != %u / %zu)\n",
885 srp_cmd->data_out_desc_cnt,
886 srp_cmd->data_in_desc_cnt,
887 be32_to_cpu(idb->table_desc.len),
888 sizeof(*db));
889 ioctx->n_rbuf = 0;
890 ret = -EINVAL;
891 goto out;
892 }
893
894 if (ioctx->n_rbuf == 1)
895 ioctx->rbufs = &ioctx->single_rbuf;
896 else {
897 ioctx->rbufs =
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800898 kmalloc(ioctx->n_rbuf * sizeof(*db), GFP_ATOMIC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000899 if (!ioctx->rbufs) {
900 ioctx->n_rbuf = 0;
901 ret = -ENOMEM;
902 goto out;
903 }
904 }
905
906 db = idb->desc_list;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800907 memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof(*db));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000908 *data_len = be32_to_cpu(idb->len);
909 }
910out:
911 return ret;
912}
913
914/**
915 * srpt_init_ch_qp() - Initialize queue pair attributes.
916 *
917 * Initialized the attributes of queue pair 'qp' by allowing local write,
918 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
919 */
920static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
921{
922 struct ib_qp_attr *attr;
923 int ret;
924
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800925 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000926 if (!attr)
927 return -ENOMEM;
928
929 attr->qp_state = IB_QPS_INIT;
930 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
931 IB_ACCESS_REMOTE_WRITE;
932 attr->port_num = ch->sport->port;
933 attr->pkey_index = 0;
934
935 ret = ib_modify_qp(qp, attr,
936 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
937 IB_QP_PKEY_INDEX);
938
939 kfree(attr);
940 return ret;
941}
942
943/**
944 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
945 * @ch: channel of the queue pair.
946 * @qp: queue pair to change the state of.
947 *
948 * Returns zero upon success and a negative value upon failure.
949 *
950 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
951 * If this structure ever becomes larger, it might be necessary to allocate
952 * it dynamically instead of on the stack.
953 */
954static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
955{
956 struct ib_qp_attr qp_attr;
957 int attr_mask;
958 int ret;
959
960 qp_attr.qp_state = IB_QPS_RTR;
961 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
962 if (ret)
963 goto out;
964
965 qp_attr.max_dest_rd_atomic = 4;
966
967 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
968
969out:
970 return ret;
971}
972
973/**
974 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
975 * @ch: channel of the queue pair.
976 * @qp: queue pair to change the state of.
977 *
978 * Returns zero upon success and a negative value upon failure.
979 *
980 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
981 * If this structure ever becomes larger, it might be necessary to allocate
982 * it dynamically instead of on the stack.
983 */
984static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
985{
986 struct ib_qp_attr qp_attr;
987 int attr_mask;
988 int ret;
989
990 qp_attr.qp_state = IB_QPS_RTS;
991 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
992 if (ret)
993 goto out;
994
995 qp_attr.max_rd_atomic = 4;
996
997 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
998
999out:
1000 return ret;
1001}
1002
1003/**
1004 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1005 */
1006static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1007{
1008 struct ib_qp_attr qp_attr;
1009
1010 qp_attr.qp_state = IB_QPS_ERR;
1011 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1012}
1013
1014/**
1015 * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
1016 */
1017static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1018 struct srpt_send_ioctx *ioctx)
1019{
1020 struct scatterlist *sg;
1021 enum dma_data_direction dir;
1022
1023 BUG_ON(!ch);
1024 BUG_ON(!ioctx);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001025 BUG_ON(ioctx->n_rdma && !ioctx->rdma_wrs);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001026
1027 while (ioctx->n_rdma)
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001028 kfree(ioctx->rdma_wrs[--ioctx->n_rdma].wr.sg_list);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001029
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001030 kfree(ioctx->rdma_wrs);
1031 ioctx->rdma_wrs = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001032
1033 if (ioctx->mapped_sg_count) {
1034 sg = ioctx->sg;
1035 WARN_ON(!sg);
1036 dir = ioctx->cmd.data_direction;
1037 BUG_ON(dir == DMA_NONE);
1038 ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
Bart Van Assche671ec1b2016-02-11 11:05:01 -08001039 target_reverse_dma_direction(&ioctx->cmd));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001040 ioctx->mapped_sg_count = 0;
1041 }
1042}
1043
1044/**
1045 * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
1046 */
1047static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1048 struct srpt_send_ioctx *ioctx)
1049{
Mike Marciniszynb0768082014-04-07 13:58:35 -04001050 struct ib_device *dev = ch->sport->sdev->device;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001051 struct se_cmd *cmd;
1052 struct scatterlist *sg, *sg_orig;
1053 int sg_cnt;
1054 enum dma_data_direction dir;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001055 struct ib_rdma_wr *riu;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001056 struct srp_direct_buf *db;
1057 dma_addr_t dma_addr;
1058 struct ib_sge *sge;
1059 u64 raddr;
1060 u32 rsize;
1061 u32 tsize;
1062 u32 dma_len;
1063 int count, nrdma;
1064 int i, j, k;
1065
1066 BUG_ON(!ch);
1067 BUG_ON(!ioctx);
1068 cmd = &ioctx->cmd;
1069 dir = cmd->data_direction;
1070 BUG_ON(dir == DMA_NONE);
1071
Roland Dreier6f9e7f02012-03-30 11:29:12 -07001072 ioctx->sg = sg = sg_orig = cmd->t_data_sg;
1073 ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001074
1075 count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
Bart Van Assche671ec1b2016-02-11 11:05:01 -08001076 target_reverse_dma_direction(cmd));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001077 if (unlikely(!count))
1078 return -EAGAIN;
1079
1080 ioctx->mapped_sg_count = count;
1081
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001082 if (ioctx->rdma_wrs && ioctx->n_rdma_wrs)
1083 nrdma = ioctx->n_rdma_wrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001084 else {
1085 nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
1086 + ioctx->n_rbuf;
1087
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001088 ioctx->rdma_wrs = kcalloc(nrdma, sizeof(*ioctx->rdma_wrs),
1089 GFP_KERNEL);
1090 if (!ioctx->rdma_wrs)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001091 goto free_mem;
1092
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001093 ioctx->n_rdma_wrs = nrdma;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001094 }
1095
1096 db = ioctx->rbufs;
1097 tsize = cmd->data_length;
Mike Marciniszynb0768082014-04-07 13:58:35 -04001098 dma_len = ib_sg_dma_len(dev, &sg[0]);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001099 riu = ioctx->rdma_wrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001100
1101 /*
1102 * For each remote desc - calculate the #ib_sge.
1103 * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1104 * each remote desc rdma_iu is required a rdma wr;
1105 * else
1106 * we need to allocate extra rdma_iu to carry extra #ib_sge in
1107 * another rdma wr
1108 */
1109 for (i = 0, j = 0;
1110 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1111 rsize = be32_to_cpu(db->len);
1112 raddr = be64_to_cpu(db->va);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001113 riu->remote_addr = raddr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001114 riu->rkey = be32_to_cpu(db->key);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001115 riu->wr.num_sge = 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001116
1117 /* calculate how many sge required for this remote_buf */
1118 while (rsize > 0 && tsize > 0) {
1119
1120 if (rsize >= dma_len) {
1121 tsize -= dma_len;
1122 rsize -= dma_len;
1123 raddr += dma_len;
1124
1125 if (tsize > 0) {
1126 ++j;
1127 if (j < count) {
1128 sg = sg_next(sg);
Mike Marciniszynb0768082014-04-07 13:58:35 -04001129 dma_len = ib_sg_dma_len(
1130 dev, sg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001131 }
1132 }
1133 } else {
1134 tsize -= rsize;
1135 dma_len -= rsize;
1136 rsize = 0;
1137 }
1138
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001139 ++riu->wr.num_sge;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001140
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001141 if (rsize > 0 &&
1142 riu->wr.num_sge == SRPT_DEF_SG_PER_WQE) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001143 ++ioctx->n_rdma;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001144 riu->wr.sg_list = kmalloc_array(riu->wr.num_sge,
1145 sizeof(*riu->wr.sg_list),
1146 GFP_KERNEL);
1147 if (!riu->wr.sg_list)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001148 goto free_mem;
1149
1150 ++riu;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001151 riu->wr.num_sge = 0;
1152 riu->remote_addr = raddr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001153 riu->rkey = be32_to_cpu(db->key);
1154 }
1155 }
1156
1157 ++ioctx->n_rdma;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001158 riu->wr.sg_list = kmalloc_array(riu->wr.num_sge,
1159 sizeof(*riu->wr.sg_list),
1160 GFP_KERNEL);
1161 if (!riu->wr.sg_list)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001162 goto free_mem;
1163 }
1164
1165 db = ioctx->rbufs;
1166 tsize = cmd->data_length;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001167 riu = ioctx->rdma_wrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001168 sg = sg_orig;
Mike Marciniszynb0768082014-04-07 13:58:35 -04001169 dma_len = ib_sg_dma_len(dev, &sg[0]);
1170 dma_addr = ib_sg_dma_address(dev, &sg[0]);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001171
1172 /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1173 for (i = 0, j = 0;
1174 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1175 rsize = be32_to_cpu(db->len);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001176 sge = riu->wr.sg_list;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001177 k = 0;
1178
1179 while (rsize > 0 && tsize > 0) {
1180 sge->addr = dma_addr;
Jason Gunthorpe5a783952015-07-30 17:22:24 -06001181 sge->lkey = ch->sport->sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001182
1183 if (rsize >= dma_len) {
1184 sge->length =
1185 (tsize < dma_len) ? tsize : dma_len;
1186 tsize -= dma_len;
1187 rsize -= dma_len;
1188
1189 if (tsize > 0) {
1190 ++j;
1191 if (j < count) {
1192 sg = sg_next(sg);
Mike Marciniszynb0768082014-04-07 13:58:35 -04001193 dma_len = ib_sg_dma_len(
1194 dev, sg);
1195 dma_addr = ib_sg_dma_address(
1196 dev, sg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001197 }
1198 }
1199 } else {
1200 sge->length = (tsize < rsize) ? tsize : rsize;
1201 tsize -= rsize;
1202 dma_len -= rsize;
1203 dma_addr += rsize;
1204 rsize = 0;
1205 }
1206
1207 ++k;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001208 if (k == riu->wr.num_sge && rsize > 0 && tsize > 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001209 ++riu;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001210 sge = riu->wr.sg_list;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001211 k = 0;
1212 } else if (rsize > 0 && tsize > 0)
1213 ++sge;
1214 }
1215 }
1216
1217 return 0;
1218
1219free_mem:
1220 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1221
1222 return -ENOMEM;
1223}
1224
1225/**
1226 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1227 */
1228static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1229{
1230 struct srpt_send_ioctx *ioctx;
1231 unsigned long flags;
1232
1233 BUG_ON(!ch);
1234
1235 ioctx = NULL;
1236 spin_lock_irqsave(&ch->spinlock, flags);
1237 if (!list_empty(&ch->free_list)) {
1238 ioctx = list_first_entry(&ch->free_list,
1239 struct srpt_send_ioctx, free_list);
1240 list_del(&ioctx->free_list);
1241 }
1242 spin_unlock_irqrestore(&ch->spinlock, flags);
1243
1244 if (!ioctx)
1245 return ioctx;
1246
1247 BUG_ON(ioctx->ch != ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001248 spin_lock_init(&ioctx->spinlock);
1249 ioctx->state = SRPT_STATE_NEW;
1250 ioctx->n_rbuf = 0;
1251 ioctx->rbufs = NULL;
1252 ioctx->n_rdma = 0;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001253 ioctx->n_rdma_wrs = 0;
1254 ioctx->rdma_wrs = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001255 ioctx->mapped_sg_count = 0;
1256 init_completion(&ioctx->tx_done);
1257 ioctx->queue_status_only = false;
1258 /*
1259 * transport_init_se_cmd() does not initialize all fields, so do it
1260 * here.
1261 */
1262 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1263 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1264
1265 return ioctx;
1266}
1267
1268/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001269 * srpt_abort_cmd() - Abort a SCSI command.
1270 * @ioctx: I/O context associated with the SCSI command.
1271 * @context: Preferred execution context.
1272 */
1273static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1274{
1275 enum srpt_command_state state;
1276 unsigned long flags;
1277
1278 BUG_ON(!ioctx);
1279
1280 /*
1281 * If the command is in a state where the target core is waiting for
1282 * the ib_srpt driver, change the state to the next state. Changing
1283 * the state of the command from SRPT_STATE_NEED_DATA to
1284 * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
1285 * function a second time.
1286 */
1287
1288 spin_lock_irqsave(&ioctx->spinlock, flags);
1289 state = ioctx->state;
1290 switch (state) {
1291 case SRPT_STATE_NEED_DATA:
1292 ioctx->state = SRPT_STATE_DATA_IN;
1293 break;
1294 case SRPT_STATE_DATA_IN:
1295 case SRPT_STATE_CMD_RSP_SENT:
1296 case SRPT_STATE_MGMT_RSP_SENT:
1297 ioctx->state = SRPT_STATE_DONE;
1298 break;
1299 default:
1300 break;
1301 }
1302 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1303
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001304 if (state == SRPT_STATE_DONE) {
1305 struct srpt_rdma_ch *ch = ioctx->ch;
1306
1307 BUG_ON(ch->sess == NULL);
1308
Bart Van Asscheafc16602015-04-27 13:52:36 +02001309 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001310 goto out;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001311 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001312
1313 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
Bart Van Assche649ee052015-04-14 13:26:44 +02001314 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001315
1316 switch (state) {
1317 case SRPT_STATE_NEW:
1318 case SRPT_STATE_DATA_IN:
1319 case SRPT_STATE_MGMT:
1320 /*
1321 * Do nothing - defer abort processing until
1322 * srpt_queue_response() is invoked.
1323 */
1324 WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
1325 break;
1326 case SRPT_STATE_NEED_DATA:
1327 /* DMA_TO_DEVICE (write) - RDMA read error. */
Christoph Hellwige672a472012-07-08 15:58:43 -04001328
1329 /* XXX(hch): this is a horrible layering violation.. */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001330 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
Christoph Hellwige672a472012-07-08 15:58:43 -04001331 ioctx->cmd.transport_state &= ~CMD_T_ACTIVE;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001332 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001333 break;
1334 case SRPT_STATE_CMD_RSP_SENT:
1335 /*
1336 * SRP_RSP sending failed or the SRP_RSP send completion has
1337 * not been received in time.
1338 */
1339 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001340 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001341 break;
1342 case SRPT_STATE_MGMT_RSP_SENT:
1343 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001344 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001345 break;
1346 default:
Grant Grundler532ec6f2013-03-26 21:48:28 +00001347 WARN(1, "Unexpected command state (%d)", state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001348 break;
1349 }
1350
1351out:
1352 return state;
1353}
1354
1355/**
Christoph Hellwige672a472012-07-08 15:58:43 -04001356 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1357 * the data that has been transferred via IB RDMA had to be postponed until the
Masanari Iida142ad5d2012-08-10 00:07:58 +00001358 * check_stop_free() callback. None of this is necessary anymore and needs to
Christoph Hellwige672a472012-07-08 15:58:43 -04001359 * be cleaned up.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001360 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001361static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001362{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001363 struct srpt_rdma_ch *ch = cq->cq_context;
1364 struct srpt_send_ioctx *ioctx =
Bart Van Assche19f57292015-12-31 09:56:43 +01001365 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001366
Bart Van Asschea42d9852011-10-14 01:30:46 +00001367 WARN_ON(ioctx->n_rdma <= 0);
1368 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1369
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001370 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1371 pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
1372 ioctx, wc->status);
1373 srpt_abort_cmd(ioctx);
1374 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001375 }
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001376
1377 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1378 SRPT_STATE_DATA_IN))
1379 target_execute_cmd(&ioctx->cmd);
1380 else
1381 pr_err("%s[%d]: wrong state = %d\n", __func__,
1382 __LINE__, srpt_get_cmd_state(ioctx));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001383}
1384
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001385static void srpt_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001386{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001387 struct srpt_send_ioctx *ioctx =
Bart Van Assche19f57292015-12-31 09:56:43 +01001388 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001389
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001390 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1391 pr_info("RDMA_WRITE for ioctx 0x%p failed with status %d\n",
1392 ioctx, wc->status);
1393 srpt_abort_cmd(ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001394 }
1395}
1396
1397/**
1398 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1399 * @ch: RDMA channel through which the request has been received.
1400 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1401 * be built in the buffer ioctx->buf points at and hence this function will
1402 * overwrite the request data.
1403 * @tag: tag of the request for which this response is being generated.
1404 * @status: value for the STATUS field of the SRP_RSP information unit.
1405 *
1406 * Returns the size in bytes of the SRP_RSP response.
1407 *
1408 * An SRP_RSP response contains a SCSI status or service response. See also
1409 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1410 * response. See also SPC-2 for more information about sense data.
1411 */
1412static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1413 struct srpt_send_ioctx *ioctx, u64 tag,
1414 int status)
1415{
1416 struct srp_rsp *srp_rsp;
1417 const u8 *sense_data;
1418 int sense_data_len, max_sense_len;
1419
1420 /*
1421 * The lowest bit of all SAM-3 status codes is zero (see also
1422 * paragraph 5.3 in SAM-3).
1423 */
1424 WARN_ON(status & 1);
1425
1426 srp_rsp = ioctx->ioctx.buf;
1427 BUG_ON(!srp_rsp);
1428
1429 sense_data = ioctx->sense_data;
1430 sense_data_len = ioctx->cmd.scsi_sense_length;
1431 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1432
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08001433 memset(srp_rsp, 0, sizeof(*srp_rsp));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001434 srp_rsp->opcode = SRP_RSP;
1435 srp_rsp->req_lim_delta =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301436 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001437 srp_rsp->tag = tag;
1438 srp_rsp->status = status;
1439
1440 if (sense_data_len) {
1441 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1442 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1443 if (sense_data_len > max_sense_len) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001444 pr_warn("truncated sense data from %d to %d"
1445 " bytes\n", sense_data_len, max_sense_len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001446 sense_data_len = max_sense_len;
1447 }
1448
1449 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1450 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1451 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1452 }
1453
1454 return sizeof(*srp_rsp) + sense_data_len;
1455}
1456
1457/**
1458 * srpt_build_tskmgmt_rsp() - Build a task management response.
1459 * @ch: RDMA channel through which the request has been received.
1460 * @ioctx: I/O context in which the SRP_RSP response will be built.
1461 * @rsp_code: RSP_CODE that will be stored in the response.
1462 * @tag: Tag of the request for which this response is being generated.
1463 *
1464 * Returns the size in bytes of the SRP_RSP response.
1465 *
1466 * An SRP_RSP response contains a SCSI status or service response. See also
1467 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1468 * response.
1469 */
1470static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1471 struct srpt_send_ioctx *ioctx,
1472 u8 rsp_code, u64 tag)
1473{
1474 struct srp_rsp *srp_rsp;
1475 int resp_data_len;
1476 int resp_len;
1477
Jack Wangc807f642013-09-30 10:09:05 +02001478 resp_data_len = 4;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001479 resp_len = sizeof(*srp_rsp) + resp_data_len;
1480
1481 srp_rsp = ioctx->ioctx.buf;
1482 BUG_ON(!srp_rsp);
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08001483 memset(srp_rsp, 0, sizeof(*srp_rsp));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001484
1485 srp_rsp->opcode = SRP_RSP;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301486 srp_rsp->req_lim_delta =
1487 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001488 srp_rsp->tag = tag;
1489
Jack Wangc807f642013-09-30 10:09:05 +02001490 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1491 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1492 srp_rsp->data[3] = rsp_code;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001493
1494 return resp_len;
1495}
1496
Bart Van Asschea42d9852011-10-14 01:30:46 +00001497static int srpt_check_stop_free(struct se_cmd *cmd)
1498{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001499 struct srpt_send_ioctx *ioctx = container_of(cmd,
1500 struct srpt_send_ioctx, cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001501
Bart Van Asscheafc16602015-04-27 13:52:36 +02001502 return target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001503}
1504
1505/**
1506 * srpt_handle_cmd() - Process SRP_CMD.
1507 */
1508static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
1509 struct srpt_recv_ioctx *recv_ioctx,
1510 struct srpt_send_ioctx *send_ioctx)
1511{
1512 struct se_cmd *cmd;
1513 struct srp_cmd *srp_cmd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001514 u64 data_len;
1515 enum dma_data_direction dir;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001516 sense_reason_t ret;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001517 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001518
1519 BUG_ON(!send_ioctx);
1520
1521 srp_cmd = recv_ioctx->ioctx.buf;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001522 cmd = &send_ioctx->cmd;
Bart Van Assche649ee052015-04-14 13:26:44 +02001523 cmd->tag = srp_cmd->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001524
1525 switch (srp_cmd->task_attr) {
1526 case SRP_CMD_SIMPLE_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001527 cmd->sam_task_attr = TCM_SIMPLE_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001528 break;
1529 case SRP_CMD_ORDERED_Q:
1530 default:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001531 cmd->sam_task_attr = TCM_ORDERED_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001532 break;
1533 case SRP_CMD_HEAD_OF_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001534 cmd->sam_task_attr = TCM_HEAD_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001535 break;
1536 case SRP_CMD_ACA:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001537 cmd->sam_task_attr = TCM_ACA_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001538 break;
1539 }
1540
Christoph Hellwigde103c92012-11-06 12:24:09 -08001541 if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001542 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001543 srp_cmd->tag);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001544 ret = TCM_INVALID_CDB_FIELD;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001545 goto send_sense;
1546 }
1547
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001548 rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
Bart Van Asschee1dd4132016-02-11 11:05:19 -08001549 &send_ioctx->sense_data[0],
1550 scsilun_to_int(&srp_cmd->lun), data_len,
1551 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001552 if (rc != 0) {
1553 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001554 goto send_sense;
Nicholas Bellinger187e70a2012-03-17 20:12:36 -07001555 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001556 return 0;
1557
1558send_sense:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001559 transport_send_check_condition_and_sense(cmd, ret, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001560 return -1;
1561}
1562
Bart Van Asschea42d9852011-10-14 01:30:46 +00001563static int srp_tmr_to_tcm(int fn)
1564{
1565 switch (fn) {
1566 case SRP_TSK_ABORT_TASK:
1567 return TMR_ABORT_TASK;
1568 case SRP_TSK_ABORT_TASK_SET:
1569 return TMR_ABORT_TASK_SET;
1570 case SRP_TSK_CLEAR_TASK_SET:
1571 return TMR_CLEAR_TASK_SET;
1572 case SRP_TSK_LUN_RESET:
1573 return TMR_LUN_RESET;
1574 case SRP_TSK_CLEAR_ACA:
1575 return TMR_CLEAR_ACA;
1576 default:
1577 return -1;
1578 }
1579}
1580
1581/**
1582 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1583 *
1584 * Returns 0 if and only if the request will be processed by the target core.
1585 *
1586 * For more information about SRP_TSK_MGMT information units, see also section
1587 * 6.7 in the SRP r16a document.
1588 */
1589static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1590 struct srpt_recv_ioctx *recv_ioctx,
1591 struct srpt_send_ioctx *send_ioctx)
1592{
1593 struct srp_tsk_mgmt *srp_tsk;
1594 struct se_cmd *cmd;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001595 struct se_session *sess = ch->sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001596 int tcm_tmr;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001597 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001598
1599 BUG_ON(!send_ioctx);
1600
1601 srp_tsk = recv_ioctx->ioctx.buf;
1602 cmd = &send_ioctx->cmd;
1603
1604 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1605 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1606 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1607
1608 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
Bart Van Assche649ee052015-04-14 13:26:44 +02001609 send_ioctx->cmd.tag = srp_tsk->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001610 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
Bart Van Asschee1dd4132016-02-11 11:05:19 -08001611 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL,
1612 scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr,
1613 GFP_KERNEL, srp_tsk->task_tag,
1614 TARGET_SCF_ACK_KREF);
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001615 if (rc != 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001616 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001617 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001618 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001619 return;
1620fail:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001621 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001622}
1623
1624/**
1625 * srpt_handle_new_iu() - Process a newly received information unit.
1626 * @ch: RDMA channel through which the information unit has been received.
1627 * @ioctx: SRPT I/O context associated with the information unit.
1628 */
1629static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1630 struct srpt_recv_ioctx *recv_ioctx,
1631 struct srpt_send_ioctx *send_ioctx)
1632{
1633 struct srp_cmd *srp_cmd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001634
1635 BUG_ON(!ch);
1636 BUG_ON(!recv_ioctx);
1637
1638 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1639 recv_ioctx->ioctx.dma, srp_max_req_size,
1640 DMA_FROM_DEVICE);
1641
Bart Van Assche33912d72016-02-11 11:04:43 -08001642 if (unlikely(ch->state == CH_CONNECTING)) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001643 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1644 goto out;
1645 }
1646
Bart Van Assche33912d72016-02-11 11:04:43 -08001647 if (unlikely(ch->state != CH_LIVE))
Bart Van Asschea42d9852011-10-14 01:30:46 +00001648 goto out;
1649
1650 srp_cmd = recv_ioctx->ioctx.buf;
1651 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1652 if (!send_ioctx)
1653 send_ioctx = srpt_get_send_ioctx(ch);
1654 if (unlikely(!send_ioctx)) {
1655 list_add_tail(&recv_ioctx->wait_list,
1656 &ch->cmd_wait_list);
1657 goto out;
1658 }
1659 }
1660
Bart Van Asschea42d9852011-10-14 01:30:46 +00001661 switch (srp_cmd->opcode) {
1662 case SRP_CMD:
1663 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1664 break;
1665 case SRP_TSK_MGMT:
1666 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1667 break;
1668 case SRP_I_LOGOUT:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001669 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001670 break;
1671 case SRP_CRED_RSP:
1672 pr_debug("received SRP_CRED_RSP\n");
1673 break;
1674 case SRP_AER_RSP:
1675 pr_debug("received SRP_AER_RSP\n");
1676 break;
1677 case SRP_RSP:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001678 pr_err("Received SRP_RSP\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001679 break;
1680 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001681 pr_err("received IU with unknown opcode 0x%x\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001682 srp_cmd->opcode);
1683 break;
1684 }
1685
1686 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1687out:
1688 return;
1689}
1690
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001691static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001692{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001693 struct srpt_rdma_ch *ch = cq->cq_context;
1694 struct srpt_recv_ioctx *ioctx =
1695 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001696
Bart Van Asschea42d9852011-10-14 01:30:46 +00001697 if (wc->status == IB_WC_SUCCESS) {
1698 int req_lim;
1699
1700 req_lim = atomic_dec_return(&ch->req_lim);
1701 if (unlikely(req_lim < 0))
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001702 pr_err("req_lim = %d < 0\n", req_lim);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001703 srpt_handle_new_iu(ch, ioctx, NULL);
1704 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001705 pr_info("receiving failed for ioctx %p with status %d\n",
1706 ioctx, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001707 }
1708}
1709
1710/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001711 * Note: Although this has not yet been observed during tests, at least in
1712 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1713 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1714 * value in each response is set to one, and it is possible that this response
1715 * makes the initiator send a new request before the send completion for that
1716 * response has been processed. This could e.g. happen if the call to
1717 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1718 * if IB retransmission causes generation of the send completion to be
1719 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1720 * are queued on cmd_wait_list. The code below processes these delayed
1721 * requests one at a time.
1722 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001723static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001724{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001725 struct srpt_rdma_ch *ch = cq->cq_context;
1726 struct srpt_send_ioctx *ioctx =
1727 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1728 enum srpt_command_state state;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001729
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001730 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1731
1732 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1733 state != SRPT_STATE_MGMT_RSP_SENT);
1734
1735 atomic_inc(&ch->sq_wr_avail);
1736
1737 if (wc->status != IB_WC_SUCCESS) {
1738 pr_info("sending response for ioctx 0x%p failed"
1739 " with status %d\n", ioctx, wc->status);
1740
1741 atomic_dec(&ch->req_lim);
1742 srpt_abort_cmd(ioctx);
1743 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001744 }
1745
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001746 if (state != SRPT_STATE_DONE) {
1747 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1748 transport_generic_free_cmd(&ioctx->cmd, 0);
1749 } else {
1750 pr_err("IB completion has been received too late for"
1751 " wr_id = %u.\n", ioctx->ioctx.index);
1752 }
1753
1754out:
1755 while (!list_empty(&ch->cmd_wait_list) &&
Bart Van Assche33912d72016-02-11 11:04:43 -08001756 ch->state == CH_LIVE &&
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001757 (ioctx = srpt_get_send_ioctx(ch)) != NULL) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001758 struct srpt_recv_ioctx *recv_ioctx;
1759
1760 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
1761 struct srpt_recv_ioctx,
1762 wait_list);
1763 list_del(&recv_ioctx->wait_list);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001764 srpt_handle_new_iu(ch, recv_ioctx, ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001765 }
1766}
1767
Bart Van Asschea42d9852011-10-14 01:30:46 +00001768/**
1769 * srpt_create_ch_ib() - Create receive and send completion queues.
1770 */
1771static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1772{
1773 struct ib_qp_init_attr *qp_init;
1774 struct srpt_port *sport = ch->sport;
1775 struct srpt_device *sdev = sport->sdev;
1776 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
1777 int ret;
1778
1779 WARN_ON(ch->rq_size < 1);
1780
1781 ret = -ENOMEM;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08001782 qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001783 if (!qp_init)
1784 goto out;
1785
Bart Van Asscheab477c12014-10-19 18:05:33 +03001786retry:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001787 ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size,
1788 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001789 if (IS_ERR(ch->cq)) {
1790 ret = PTR_ERR(ch->cq);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001791 pr_err("failed to create CQ cqe= %d ret= %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001792 ch->rq_size + srp_sq_size, ret);
1793 goto out;
1794 }
1795
1796 qp_init->qp_context = (void *)ch;
1797 qp_init->event_handler
1798 = (void(*)(struct ib_event *, void*))srpt_qp_event;
1799 qp_init->send_cq = ch->cq;
1800 qp_init->recv_cq = ch->cq;
1801 qp_init->srq = sdev->srq;
1802 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1803 qp_init->qp_type = IB_QPT_RC;
1804 qp_init->cap.max_send_wr = srp_sq_size;
1805 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
1806
1807 ch->qp = ib_create_qp(sdev->pd, qp_init);
1808 if (IS_ERR(ch->qp)) {
1809 ret = PTR_ERR(ch->qp);
Bart Van Asscheab477c12014-10-19 18:05:33 +03001810 if (ret == -ENOMEM) {
1811 srp_sq_size /= 2;
1812 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
1813 ib_destroy_cq(ch->cq);
1814 goto retry;
1815 }
1816 }
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001817 pr_err("failed to create_qp ret= %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001818 goto err_destroy_cq;
1819 }
1820
1821 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1822
1823 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
1824 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1825 qp_init->cap.max_send_wr, ch->cm_id);
1826
1827 ret = srpt_init_ch_qp(ch, ch->qp);
1828 if (ret)
1829 goto err_destroy_qp;
1830
Bart Van Asschea42d9852011-10-14 01:30:46 +00001831out:
1832 kfree(qp_init);
1833 return ret;
1834
1835err_destroy_qp:
1836 ib_destroy_qp(ch->qp);
1837err_destroy_cq:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001838 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001839 goto out;
1840}
1841
1842static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
1843{
Bart Van Asschea42d9852011-10-14 01:30:46 +00001844 ib_destroy_qp(ch->qp);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001845 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001846}
1847
1848/**
1849 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
1850 *
1851 * Reset the QP and make sure all resources associated with the channel will
1852 * be deallocated at an appropriate time.
1853 *
1854 * Note: The caller must hold ch->sport->sdev->spinlock.
1855 */
1856static void __srpt_close_ch(struct srpt_rdma_ch *ch)
1857{
Bart Van Asschea42d9852011-10-14 01:30:46 +00001858 enum rdma_ch_state prev_state;
1859 unsigned long flags;
1860
Bart Van Asschea42d9852011-10-14 01:30:46 +00001861 spin_lock_irqsave(&ch->spinlock, flags);
1862 prev_state = ch->state;
1863 switch (prev_state) {
1864 case CH_CONNECTING:
1865 case CH_LIVE:
1866 ch->state = CH_DISCONNECTING;
1867 break;
1868 default:
1869 break;
1870 }
1871 spin_unlock_irqrestore(&ch->spinlock, flags);
1872
1873 switch (prev_state) {
1874 case CH_CONNECTING:
1875 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
1876 NULL, 0);
1877 /* fall through */
1878 case CH_LIVE:
1879 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001880 pr_err("sending CM DREQ failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001881 break;
1882 case CH_DISCONNECTING:
1883 break;
1884 case CH_DRAINING:
1885 case CH_RELEASING:
1886 break;
1887 }
1888}
1889
1890/**
1891 * srpt_close_ch() - Close an RDMA channel.
1892 */
1893static void srpt_close_ch(struct srpt_rdma_ch *ch)
1894{
1895 struct srpt_device *sdev;
1896
1897 sdev = ch->sport->sdev;
1898 spin_lock_irq(&sdev->spinlock);
1899 __srpt_close_ch(ch);
1900 spin_unlock_irq(&sdev->spinlock);
1901}
1902
1903/**
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07001904 * srpt_shutdown_session() - Whether or not a session may be shut down.
1905 */
1906static int srpt_shutdown_session(struct se_session *se_sess)
1907{
1908 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
1909 unsigned long flags;
1910
1911 spin_lock_irqsave(&ch->spinlock, flags);
1912 if (ch->in_shutdown) {
1913 spin_unlock_irqrestore(&ch->spinlock, flags);
1914 return true;
1915 }
1916
1917 ch->in_shutdown = true;
1918 target_sess_cmd_list_set_waiting(se_sess);
1919 spin_unlock_irqrestore(&ch->spinlock, flags);
1920
1921 return true;
1922}
1923
1924/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001925 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
1926 * @cm_id: Pointer to the CM ID of the channel to be drained.
1927 *
1928 * Note: Must be called from inside srpt_cm_handler to avoid a race between
1929 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
1930 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
1931 * waits until all target sessions for the associated IB device have been
1932 * unregistered and target session registration involves a call to
1933 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
1934 * this function has finished).
1935 */
1936static void srpt_drain_channel(struct ib_cm_id *cm_id)
1937{
1938 struct srpt_device *sdev;
1939 struct srpt_rdma_ch *ch;
1940 int ret;
1941 bool do_reset = false;
1942
1943 WARN_ON_ONCE(irqs_disabled());
1944
1945 sdev = cm_id->context;
1946 BUG_ON(!sdev);
1947 spin_lock_irq(&sdev->spinlock);
1948 list_for_each_entry(ch, &sdev->rch_list, list) {
1949 if (ch->cm_id == cm_id) {
1950 do_reset = srpt_test_and_set_ch_state(ch,
1951 CH_CONNECTING, CH_DRAINING) ||
1952 srpt_test_and_set_ch_state(ch,
1953 CH_LIVE, CH_DRAINING) ||
1954 srpt_test_and_set_ch_state(ch,
1955 CH_DISCONNECTING, CH_DRAINING);
1956 break;
1957 }
1958 }
1959 spin_unlock_irq(&sdev->spinlock);
1960
1961 if (do_reset) {
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07001962 if (ch->sess)
1963 srpt_shutdown_session(ch->sess);
1964
Bart Van Asschea42d9852011-10-14 01:30:46 +00001965 ret = srpt_ch_qp_err(ch);
1966 if (ret < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001967 pr_err("Setting queue pair in error state"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001968 " failed: %d\n", ret);
1969 }
1970}
1971
1972/**
1973 * srpt_find_channel() - Look up an RDMA channel.
1974 * @cm_id: Pointer to the CM ID of the channel to be looked up.
1975 *
1976 * Return NULL if no matching RDMA channel has been found.
1977 */
1978static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
1979 struct ib_cm_id *cm_id)
1980{
1981 struct srpt_rdma_ch *ch;
1982 bool found;
1983
1984 WARN_ON_ONCE(irqs_disabled());
1985 BUG_ON(!sdev);
1986
1987 found = false;
1988 spin_lock_irq(&sdev->spinlock);
1989 list_for_each_entry(ch, &sdev->rch_list, list) {
1990 if (ch->cm_id == cm_id) {
1991 found = true;
1992 break;
1993 }
1994 }
1995 spin_unlock_irq(&sdev->spinlock);
1996
1997 return found ? ch : NULL;
1998}
1999
2000/**
2001 * srpt_release_channel() - Release channel resources.
2002 *
2003 * Schedules the actual release because:
2004 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
2005 * trigger a deadlock.
2006 * - It is not safe to call TCM transport_* functions from interrupt context.
2007 */
2008static void srpt_release_channel(struct srpt_rdma_ch *ch)
2009{
2010 schedule_work(&ch->release_work);
2011}
2012
2013static void srpt_release_channel_work(struct work_struct *w)
2014{
2015 struct srpt_rdma_ch *ch;
2016 struct srpt_device *sdev;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002017 struct se_session *se_sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002018
2019 ch = container_of(w, struct srpt_rdma_ch, release_work);
2020 pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2021 ch->release_done);
2022
2023 sdev = ch->sport->sdev;
2024 BUG_ON(!sdev);
2025
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002026 se_sess = ch->sess;
2027 BUG_ON(!se_sess);
2028
Joern Engelbe646c2d2013-05-15 00:44:07 -07002029 target_wait_for_sess_cmds(se_sess);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002030
2031 transport_deregister_session_configfs(se_sess);
2032 transport_deregister_session(se_sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002033 ch->sess = NULL;
2034
Nicholas Bellinger0b41d6c2013-09-18 12:48:27 -07002035 ib_destroy_cm_id(ch->cm_id);
2036
Bart Van Asschea42d9852011-10-14 01:30:46 +00002037 srpt_destroy_ch_ib(ch);
2038
2039 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2040 ch->sport->sdev, ch->rq_size,
2041 ch->rsp_size, DMA_TO_DEVICE);
2042
2043 spin_lock_irq(&sdev->spinlock);
2044 list_del(&ch->list);
2045 spin_unlock_irq(&sdev->spinlock);
2046
Bart Van Asschea42d9852011-10-14 01:30:46 +00002047 if (ch->release_done)
2048 complete(ch->release_done);
2049
2050 wake_up(&sdev->ch_releaseQ);
2051
2052 kfree(ch);
2053}
2054
Bart Van Asschea42d9852011-10-14 01:30:46 +00002055/**
2056 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2057 *
2058 * Ownership of the cm_id is transferred to the target session if this
2059 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2060 */
2061static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2062 struct ib_cm_req_event_param *param,
2063 void *private_data)
2064{
2065 struct srpt_device *sdev = cm_id->context;
2066 struct srpt_port *sport = &sdev->port[param->port - 1];
2067 struct srp_login_req *req;
2068 struct srp_login_rsp *rsp;
2069 struct srp_login_rej *rej;
2070 struct ib_cm_rep_param *rep_param;
2071 struct srpt_rdma_ch *ch, *tmp_ch;
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002072 struct se_node_acl *se_acl;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002073 u32 it_iu_len;
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002074 int i, ret = 0;
2075 unsigned char *p;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002076
2077 WARN_ON_ONCE(irqs_disabled());
2078
2079 if (WARN_ON(!sdev || !private_data))
2080 return -EINVAL;
2081
2082 req = (struct srp_login_req *)private_data;
2083
2084 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2085
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002086 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2087 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2088 " (guid=0x%llx:0x%llx)\n",
2089 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2090 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2091 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2092 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2093 it_iu_len,
2094 param->port,
2095 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2096 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002097
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08002098 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
2099 rej = kzalloc(sizeof(*rej), GFP_KERNEL);
2100 rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002101
2102 if (!rsp || !rej || !rep_param) {
2103 ret = -ENOMEM;
2104 goto out;
2105 }
2106
2107 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302108 rej->reason = cpu_to_be32(
2109 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002110 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002111 pr_err("rejected SRP_LOGIN_REQ because its"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002112 " length (%d bytes) is out of range (%d .. %d)\n",
2113 it_iu_len, 64, srp_max_req_size);
2114 goto reject;
2115 }
2116
2117 if (!sport->enabled) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302118 rej->reason = cpu_to_be32(
2119 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002120 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002121 pr_err("rejected SRP_LOGIN_REQ because the target port"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002122 " has not yet been enabled\n");
2123 goto reject;
2124 }
2125
2126 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2127 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2128
2129 spin_lock_irq(&sdev->spinlock);
2130
2131 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2132 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2133 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2134 && param->port == ch->sport->port
2135 && param->listen_id == ch->sport->sdev->cm_id
2136 && ch->cm_id) {
Bart Van Assche33912d72016-02-11 11:04:43 -08002137 if (ch->state != CH_CONNECTING
2138 && ch->state != CH_LIVE)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002139 continue;
2140
2141 /* found an existing channel */
2142 pr_debug("Found existing channel %s"
2143 " cm_id= %p state= %d\n",
Bart Van Assche33912d72016-02-11 11:04:43 -08002144 ch->sess_name, ch->cm_id, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002145
2146 __srpt_close_ch(ch);
2147
2148 rsp->rsp_flags =
2149 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2150 }
2151 }
2152
2153 spin_unlock_irq(&sdev->spinlock);
2154
2155 } else
2156 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2157
2158 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2159 || *(__be64 *)(req->target_port_id + 8) !=
2160 cpu_to_be64(srpt_service_guid)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302161 rej->reason = cpu_to_be32(
2162 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002163 ret = -ENOMEM;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002164 pr_err("rejected SRP_LOGIN_REQ because it"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002165 " has an invalid target port identifier.\n");
2166 goto reject;
2167 }
2168
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08002169 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002170 if (!ch) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302171 rej->reason = cpu_to_be32(
2172 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002173 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002174 ret = -ENOMEM;
2175 goto reject;
2176 }
2177
2178 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2179 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2180 memcpy(ch->t_port_id, req->target_port_id, 16);
2181 ch->sport = &sdev->port[param->port - 1];
2182 ch->cm_id = cm_id;
2183 /*
2184 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2185 * for the SRP protocol to the command queue size.
2186 */
2187 ch->rq_size = SRPT_RQ_SIZE;
2188 spin_lock_init(&ch->spinlock);
2189 ch->state = CH_CONNECTING;
2190 INIT_LIST_HEAD(&ch->cmd_wait_list);
2191 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2192
2193 ch->ioctx_ring = (struct srpt_send_ioctx **)
2194 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2195 sizeof(*ch->ioctx_ring[0]),
2196 ch->rsp_size, DMA_TO_DEVICE);
2197 if (!ch->ioctx_ring)
2198 goto free_ch;
2199
2200 INIT_LIST_HEAD(&ch->free_list);
2201 for (i = 0; i < ch->rq_size; i++) {
2202 ch->ioctx_ring[i]->ch = ch;
2203 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2204 }
2205
2206 ret = srpt_create_ch_ib(ch);
2207 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302208 rej->reason = cpu_to_be32(
2209 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002210 pr_err("rejected SRP_LOGIN_REQ because creating"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002211 " a new RDMA channel failed.\n");
2212 goto free_ring;
2213 }
2214
2215 ret = srpt_ch_qp_rtr(ch, ch->qp);
2216 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302217 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002218 pr_err("rejected SRP_LOGIN_REQ because enabling"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002219 " RTR failed (error code = %d)\n", ret);
2220 goto destroy_ib;
2221 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002222
Bart Van Asschea42d9852011-10-14 01:30:46 +00002223 /*
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002224 * Use the initator port identifier as the session name, when
2225 * checking against se_node_acl->initiatorname[] this can be
2226 * with or without preceeding '0x'.
Bart Van Asschea42d9852011-10-14 01:30:46 +00002227 */
2228 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2229 be64_to_cpu(*(__be64 *)ch->i_port_id),
2230 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2231
2232 pr_debug("registering session %s\n", ch->sess_name);
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002233 p = &ch->sess_name[0];
Bart Van Asschea42d9852011-10-14 01:30:46 +00002234
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002235 ch->sess = transport_init_session(TARGET_PROT_NORMAL);
Dan Carpenter3af33632011-11-04 21:27:32 +03002236 if (IS_ERR(ch->sess)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302237 rej->reason = cpu_to_be32(
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002238 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002239 pr_debug("Failed to create session\n");
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002240 goto destroy_ib;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002241 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002242
2243try_again:
2244 se_acl = core_tpg_get_initiator_node_acl(&sport->port_tpg_1, p);
2245 if (!se_acl) {
2246 pr_info("Rejected login because no ACL has been"
2247 " configured yet for initiator %s.\n", ch->sess_name);
2248 /*
2249 * XXX: Hack to retry of ch->i_port_id without leading '0x'
2250 */
2251 if (p == &ch->sess_name[0]) {
2252 p += 2;
2253 goto try_again;
2254 }
2255 rej->reason = cpu_to_be32(
2256 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2257 transport_free_session(ch->sess);
2258 goto destroy_ib;
2259 }
2260 ch->sess->se_node_acl = se_acl;
2261
2262 transport_register_session(&sport->port_tpg_1, se_acl, ch->sess, ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002263
2264 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2265 ch->sess_name, ch->cm_id);
2266
2267 /* create srp_login_response */
2268 rsp->opcode = SRP_LOGIN_RSP;
2269 rsp->tag = req->tag;
2270 rsp->max_it_iu_len = req->req_it_iu_len;
2271 rsp->max_ti_iu_len = req->req_it_iu_len;
2272 ch->max_ti_iu_len = it_iu_len;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302273 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2274 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002275 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2276 atomic_set(&ch->req_lim, ch->rq_size);
2277 atomic_set(&ch->req_lim_delta, 0);
2278
2279 /* create cm reply */
2280 rep_param->qp_num = ch->qp->qp_num;
2281 rep_param->private_data = (void *)rsp;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08002282 rep_param->private_data_len = sizeof(*rsp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002283 rep_param->rnr_retry_count = 7;
2284 rep_param->flow_control = 1;
2285 rep_param->failover_accepted = 0;
2286 rep_param->srq = 1;
2287 rep_param->responder_resources = 4;
2288 rep_param->initiator_depth = 4;
2289
2290 ret = ib_send_cm_rep(cm_id, rep_param);
2291 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002292 pr_err("sending SRP_LOGIN_REQ response failed"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002293 " (error code = %d)\n", ret);
2294 goto release_channel;
2295 }
2296
2297 spin_lock_irq(&sdev->spinlock);
2298 list_add_tail(&ch->list, &sdev->rch_list);
2299 spin_unlock_irq(&sdev->spinlock);
2300
2301 goto out;
2302
2303release_channel:
2304 srpt_set_ch_state(ch, CH_RELEASING);
2305 transport_deregister_session_configfs(ch->sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002306 transport_deregister_session(ch->sess);
2307 ch->sess = NULL;
2308
2309destroy_ib:
2310 srpt_destroy_ch_ib(ch);
2311
2312free_ring:
2313 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2314 ch->sport->sdev, ch->rq_size,
2315 ch->rsp_size, DMA_TO_DEVICE);
2316free_ch:
2317 kfree(ch);
2318
2319reject:
2320 rej->opcode = SRP_LOGIN_REJ;
2321 rej->tag = req->tag;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302322 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2323 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002324
2325 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08002326 (void *)rej, sizeof(*rej));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002327
2328out:
2329 kfree(rep_param);
2330 kfree(rsp);
2331 kfree(rej);
2332
2333 return ret;
2334}
2335
2336static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2337{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002338 pr_info("Received IB REJ for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002339 srpt_drain_channel(cm_id);
2340}
2341
2342/**
2343 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2344 *
2345 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2346 * and that the recipient may begin transmitting (RTU = ready to use).
2347 */
2348static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2349{
2350 struct srpt_rdma_ch *ch;
2351 int ret;
2352
2353 ch = srpt_find_channel(cm_id->context, cm_id);
2354 BUG_ON(!ch);
2355
2356 if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
2357 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2358
2359 ret = srpt_ch_qp_rts(ch, ch->qp);
2360
2361 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2362 wait_list) {
2363 list_del(&ioctx->wait_list);
2364 srpt_handle_new_iu(ch, ioctx, NULL);
2365 }
2366 if (ret)
2367 srpt_close_ch(ch);
2368 }
2369}
2370
2371static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2372{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002373 pr_info("Received IB TimeWait exit for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002374 srpt_drain_channel(cm_id);
2375}
2376
2377static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2378{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002379 pr_info("Received IB REP error for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002380 srpt_drain_channel(cm_id);
2381}
2382
2383/**
2384 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2385 */
2386static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2387{
2388 struct srpt_rdma_ch *ch;
2389 unsigned long flags;
2390 bool send_drep = false;
2391
2392 ch = srpt_find_channel(cm_id->context, cm_id);
2393 BUG_ON(!ch);
2394
Bart Van Assche33912d72016-02-11 11:04:43 -08002395 pr_debug("cm_id= %p ch->state= %d\n", cm_id, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002396
2397 spin_lock_irqsave(&ch->spinlock, flags);
2398 switch (ch->state) {
2399 case CH_CONNECTING:
2400 case CH_LIVE:
2401 send_drep = true;
2402 ch->state = CH_DISCONNECTING;
2403 break;
2404 case CH_DISCONNECTING:
2405 case CH_DRAINING:
2406 case CH_RELEASING:
2407 WARN(true, "unexpected channel state %d\n", ch->state);
2408 break;
2409 }
2410 spin_unlock_irqrestore(&ch->spinlock, flags);
2411
2412 if (send_drep) {
2413 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002414 pr_err("Sending IB DREP failed.\n");
2415 pr_info("Received DREQ and sent DREP for session %s.\n",
2416 ch->sess_name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002417 }
2418}
2419
2420/**
2421 * srpt_cm_drep_recv() - Process reception of a DREP message.
2422 */
2423static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2424{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002425 pr_info("Received InfiniBand DREP message for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002426 srpt_drain_channel(cm_id);
2427}
2428
2429/**
2430 * srpt_cm_handler() - IB connection manager callback function.
2431 *
2432 * A non-zero return value will cause the caller destroy the CM ID.
2433 *
2434 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2435 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2436 * a non-zero value in any other case will trigger a race with the
2437 * ib_destroy_cm_id() call in srpt_release_channel().
2438 */
2439static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2440{
2441 int ret;
2442
2443 ret = 0;
2444 switch (event->event) {
2445 case IB_CM_REQ_RECEIVED:
2446 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2447 event->private_data);
2448 break;
2449 case IB_CM_REJ_RECEIVED:
2450 srpt_cm_rej_recv(cm_id);
2451 break;
2452 case IB_CM_RTU_RECEIVED:
2453 case IB_CM_USER_ESTABLISHED:
2454 srpt_cm_rtu_recv(cm_id);
2455 break;
2456 case IB_CM_DREQ_RECEIVED:
2457 srpt_cm_dreq_recv(cm_id);
2458 break;
2459 case IB_CM_DREP_RECEIVED:
2460 srpt_cm_drep_recv(cm_id);
2461 break;
2462 case IB_CM_TIMEWAIT_EXIT:
2463 srpt_cm_timewait_exit(cm_id);
2464 break;
2465 case IB_CM_REP_ERROR:
2466 srpt_cm_rep_error(cm_id);
2467 break;
2468 case IB_CM_DREQ_ERROR:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002469 pr_info("Received IB DREQ ERROR event.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002470 break;
2471 case IB_CM_MRA_RECEIVED:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002472 pr_info("Received IB MRA event\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002473 break;
2474 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002475 pr_err("received unrecognized IB CM event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002476 break;
2477 }
2478
2479 return ret;
2480}
2481
2482/**
2483 * srpt_perform_rdmas() - Perform IB RDMA.
2484 *
2485 * Returns zero upon success or a negative number upon failure.
2486 */
2487static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2488 struct srpt_send_ioctx *ioctx)
2489{
Bart Van Asschea42d9852011-10-14 01:30:46 +00002490 struct ib_send_wr *bad_wr;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002491 int sq_wr_avail, ret, i;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002492 enum dma_data_direction dir;
2493 const int n_rdma = ioctx->n_rdma;
2494
2495 dir = ioctx->cmd.data_direction;
2496 if (dir == DMA_TO_DEVICE) {
2497 /* write */
2498 ret = -ENOMEM;
2499 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2500 if (sq_wr_avail < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002501 pr_warn("IB send queue full (needed %d)\n",
2502 n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002503 goto out;
2504 }
2505 }
2506
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002507 for (i = 0; i < n_rdma; i++) {
2508 struct ib_send_wr *wr = &ioctx->rdma_wrs[i].wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002509
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002510 wr->opcode = (dir == DMA_FROM_DEVICE) ?
2511 IB_WR_RDMA_WRITE : IB_WR_RDMA_READ;
2512
2513 if (i == n_rdma - 1) {
2514 /* only get completion event for the last rdma read */
2515 if (dir == DMA_TO_DEVICE) {
2516 wr->send_flags = IB_SEND_SIGNALED;
2517 ioctx->rdma_cqe.done = srpt_rdma_read_done;
2518 } else {
2519 ioctx->rdma_cqe.done = srpt_rdma_write_done;
2520 }
2521 wr->wr_cqe = &ioctx->rdma_cqe;
2522 wr->next = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002523 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002524 wr->wr_cqe = NULL;
2525 wr->next = &ioctx->rdma_wrs[i + 1].wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002526 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00002527 }
2528
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002529 ret = ib_post_send(ch->qp, &ioctx->rdma_wrs->wr, &bad_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002530 if (ret)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002531 pr_err("%s[%d]: ib_post_send() returned %d for %d/%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002532 __func__, __LINE__, ret, i, n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002533out:
2534 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2535 atomic_add(n_rdma, &ch->sq_wr_avail);
2536 return ret;
2537}
2538
2539/**
2540 * srpt_xfer_data() - Start data transfer from initiator to target.
2541 */
2542static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2543 struct srpt_send_ioctx *ioctx)
2544{
2545 int ret;
2546
2547 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2548 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002549 pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002550 goto out;
2551 }
2552
2553 ret = srpt_perform_rdmas(ch, ioctx);
2554 if (ret) {
2555 if (ret == -EAGAIN || ret == -ENOMEM)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002556 pr_info("%s[%d] queue full -- ret=%d\n",
2557 __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002558 else
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002559 pr_err("%s[%d] fatal error -- ret=%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002560 __func__, __LINE__, ret);
2561 goto out_unmap;
2562 }
2563
2564out:
2565 return ret;
2566out_unmap:
2567 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2568 goto out;
2569}
2570
2571static int srpt_write_pending_status(struct se_cmd *se_cmd)
2572{
2573 struct srpt_send_ioctx *ioctx;
2574
2575 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2576 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2577}
2578
2579/*
2580 * srpt_write_pending() - Start data transfer from initiator to target (write).
2581 */
2582static int srpt_write_pending(struct se_cmd *se_cmd)
2583{
2584 struct srpt_rdma_ch *ch;
2585 struct srpt_send_ioctx *ioctx;
2586 enum srpt_command_state new_state;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002587 int ret;
2588
2589 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2590
2591 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2592 WARN_ON(new_state == SRPT_STATE_DONE);
2593
2594 ch = ioctx->ch;
2595 BUG_ON(!ch);
2596
Bart Van Assche33912d72016-02-11 11:04:43 -08002597 switch (ch->state) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00002598 case CH_CONNECTING:
Bart Van Assche33912d72016-02-11 11:04:43 -08002599 WARN(true, "unexpected channel state %d\n", ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002600 ret = -EINVAL;
2601 goto out;
2602 case CH_LIVE:
2603 break;
2604 case CH_DISCONNECTING:
2605 case CH_DRAINING:
2606 case CH_RELEASING:
2607 pr_debug("cmd with tag %lld: channel disconnecting\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002608 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002609 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2610 ret = -EINVAL;
2611 goto out;
2612 }
2613 ret = srpt_xfer_data(ch, ioctx);
2614
2615out:
2616 return ret;
2617}
2618
2619static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2620{
2621 switch (tcm_mgmt_status) {
2622 case TMR_FUNCTION_COMPLETE:
2623 return SRP_TSK_MGMT_SUCCESS;
2624 case TMR_FUNCTION_REJECTED:
2625 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2626 }
2627 return SRP_TSK_MGMT_FAILED;
2628}
2629
2630/**
2631 * srpt_queue_response() - Transmits the response to a SCSI command.
2632 *
2633 * Callback function called by the TCM core. Must not block since it can be
2634 * invoked on the context of the IB completion handler.
2635 */
Joern Engelb79fafa2013-07-03 11:22:17 -04002636static void srpt_queue_response(struct se_cmd *cmd)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002637{
2638 struct srpt_rdma_ch *ch;
2639 struct srpt_send_ioctx *ioctx;
2640 enum srpt_command_state state;
2641 unsigned long flags;
2642 int ret;
2643 enum dma_data_direction dir;
2644 int resp_len;
2645 u8 srp_tm_status;
2646
Bart Van Asschea42d9852011-10-14 01:30:46 +00002647 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2648 ch = ioctx->ch;
2649 BUG_ON(!ch);
2650
2651 spin_lock_irqsave(&ioctx->spinlock, flags);
2652 state = ioctx->state;
2653 switch (state) {
2654 case SRPT_STATE_NEW:
2655 case SRPT_STATE_DATA_IN:
2656 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2657 break;
2658 case SRPT_STATE_MGMT:
2659 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2660 break;
2661 default:
2662 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2663 ch, ioctx->ioctx.index, ioctx->state);
2664 break;
2665 }
2666 spin_unlock_irqrestore(&ioctx->spinlock, flags);
2667
2668 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
2669 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
2670 atomic_inc(&ch->req_lim_delta);
2671 srpt_abort_cmd(ioctx);
Joern Engelb79fafa2013-07-03 11:22:17 -04002672 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002673 }
2674
2675 dir = ioctx->cmd.data_direction;
2676
2677 /* For read commands, transfer the data to the initiator. */
2678 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
2679 !ioctx->queue_status_only) {
2680 ret = srpt_xfer_data(ch, ioctx);
2681 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002682 pr_err("xfer_data failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002683 ioctx->cmd.tag);
Joern Engelb79fafa2013-07-03 11:22:17 -04002684 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002685 }
2686 }
2687
2688 if (state != SRPT_STATE_MGMT)
Bart Van Assche649ee052015-04-14 13:26:44 +02002689 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002690 cmd->scsi_status);
2691 else {
2692 srp_tm_status
2693 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2694 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
Bart Van Assche649ee052015-04-14 13:26:44 +02002695 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002696 }
2697 ret = srpt_post_send(ch, ioctx, resp_len);
2698 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002699 pr_err("sending cmd response failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002700 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002701 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2702 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02002703 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002704 }
Joern Engelb79fafa2013-07-03 11:22:17 -04002705}
Bart Van Asschea42d9852011-10-14 01:30:46 +00002706
Joern Engelb79fafa2013-07-03 11:22:17 -04002707static int srpt_queue_data_in(struct se_cmd *cmd)
2708{
2709 srpt_queue_response(cmd);
2710 return 0;
2711}
2712
2713static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2714{
2715 srpt_queue_response(cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002716}
2717
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002718static void srpt_aborted_task(struct se_cmd *cmd)
2719{
2720 struct srpt_send_ioctx *ioctx = container_of(cmd,
2721 struct srpt_send_ioctx, cmd);
2722
2723 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
2724}
2725
Bart Van Asschea42d9852011-10-14 01:30:46 +00002726static int srpt_queue_status(struct se_cmd *cmd)
2727{
2728 struct srpt_send_ioctx *ioctx;
2729
2730 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2731 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2732 if (cmd->se_cmd_flags &
2733 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2734 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2735 ioctx->queue_status_only = true;
Joern Engelb79fafa2013-07-03 11:22:17 -04002736 srpt_queue_response(cmd);
2737 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002738}
2739
2740static void srpt_refresh_port_work(struct work_struct *work)
2741{
2742 struct srpt_port *sport = container_of(work, struct srpt_port, work);
2743
2744 srpt_refresh_port(sport);
2745}
2746
2747static int srpt_ch_list_empty(struct srpt_device *sdev)
2748{
2749 int res;
2750
2751 spin_lock_irq(&sdev->spinlock);
2752 res = list_empty(&sdev->rch_list);
2753 spin_unlock_irq(&sdev->spinlock);
2754
2755 return res;
2756}
2757
2758/**
2759 * srpt_release_sdev() - Free the channel resources associated with a target.
2760 */
2761static int srpt_release_sdev(struct srpt_device *sdev)
2762{
2763 struct srpt_rdma_ch *ch, *tmp_ch;
2764 int res;
2765
2766 WARN_ON_ONCE(irqs_disabled());
2767
2768 BUG_ON(!sdev);
2769
2770 spin_lock_irq(&sdev->spinlock);
2771 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
2772 __srpt_close_ch(ch);
2773 spin_unlock_irq(&sdev->spinlock);
2774
2775 res = wait_event_interruptible(sdev->ch_releaseQ,
2776 srpt_ch_list_empty(sdev));
2777 if (res)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002778 pr_err("%s: interrupted.\n", __func__);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002779
2780 return 0;
2781}
2782
2783static struct srpt_port *__srpt_lookup_port(const char *name)
2784{
2785 struct ib_device *dev;
2786 struct srpt_device *sdev;
2787 struct srpt_port *sport;
2788 int i;
2789
2790 list_for_each_entry(sdev, &srpt_dev_list, list) {
2791 dev = sdev->device;
2792 if (!dev)
2793 continue;
2794
2795 for (i = 0; i < dev->phys_port_cnt; i++) {
2796 sport = &sdev->port[i];
2797
2798 if (!strcmp(sport->port_guid, name))
2799 return sport;
2800 }
2801 }
2802
2803 return NULL;
2804}
2805
2806static struct srpt_port *srpt_lookup_port(const char *name)
2807{
2808 struct srpt_port *sport;
2809
2810 spin_lock(&srpt_dev_lock);
2811 sport = __srpt_lookup_port(name);
2812 spin_unlock(&srpt_dev_lock);
2813
2814 return sport;
2815}
2816
2817/**
2818 * srpt_add_one() - Infiniband device addition callback function.
2819 */
2820static void srpt_add_one(struct ib_device *device)
2821{
2822 struct srpt_device *sdev;
2823 struct srpt_port *sport;
2824 struct ib_srq_init_attr srq_attr;
2825 int i;
2826
2827 pr_debug("device = %p, device->dma_ops = %p\n", device,
2828 device->dma_ops);
2829
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08002830 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002831 if (!sdev)
2832 goto err;
2833
2834 sdev->device = device;
2835 INIT_LIST_HEAD(&sdev->rch_list);
2836 init_waitqueue_head(&sdev->ch_releaseQ);
2837 spin_lock_init(&sdev->spinlock);
2838
Bart Van Asschea42d9852011-10-14 01:30:46 +00002839 sdev->pd = ib_alloc_pd(device);
2840 if (IS_ERR(sdev->pd))
2841 goto free_dev;
2842
Or Gerlitz4a061b22015-12-18 10:59:46 +02002843 sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002844
2845 srq_attr.event_handler = srpt_srq_event;
2846 srq_attr.srq_context = (void *)sdev;
2847 srq_attr.attr.max_wr = sdev->srq_size;
2848 srq_attr.attr.max_sge = 1;
2849 srq_attr.attr.srq_limit = 0;
Roland Dreier6f360332012-04-12 07:51:08 -07002850 srq_attr.srq_type = IB_SRQT_BASIC;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002851
2852 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
2853 if (IS_ERR(sdev->srq))
Jason Gunthorpe5a783952015-07-30 17:22:24 -06002854 goto err_pd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002855
2856 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
Or Gerlitz4a061b22015-12-18 10:59:46 +02002857 __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002858 device->name);
2859
2860 if (!srpt_service_guid)
2861 srpt_service_guid = be64_to_cpu(device->node_guid);
2862
2863 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
2864 if (IS_ERR(sdev->cm_id))
2865 goto err_srq;
2866
2867 /* print out target login information */
2868 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
2869 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
2870 srpt_service_guid, srpt_service_guid);
2871
2872 /*
2873 * We do not have a consistent service_id (ie. also id_ext of target_id)
2874 * to identify this target. We currently use the guid of the first HCA
2875 * in the system as service_id; therefore, the target_id will change
2876 * if this HCA is gone bad and replaced by different HCA
2877 */
Haggai Eran73fec7f2015-07-30 17:50:26 +03002878 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
Bart Van Asschea42d9852011-10-14 01:30:46 +00002879 goto err_cm;
2880
2881 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
2882 srpt_event_handler);
2883 if (ib_register_event_handler(&sdev->event_handler))
2884 goto err_cm;
2885
2886 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
2887 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
2888 sizeof(*sdev->ioctx_ring[0]),
2889 srp_max_req_size, DMA_FROM_DEVICE);
2890 if (!sdev->ioctx_ring)
2891 goto err_event;
2892
2893 for (i = 0; i < sdev->srq_size; ++i)
2894 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
2895
Roland Dreierf2250662012-02-02 12:55:58 -08002896 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002897
2898 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
2899 sport = &sdev->port[i - 1];
2900 sport->sdev = sdev;
2901 sport->port = i;
2902 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
2903 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
2904 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
2905 INIT_WORK(&sport->work, srpt_refresh_port_work);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002906
2907 if (srpt_refresh_port(sport)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002908 pr_err("MAD registration failed for %s-%d.\n",
Bart Van Asschef68cba4e92016-02-11 11:04:20 -08002909 sdev->device->name, i);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002910 goto err_ring;
2911 }
2912 snprintf(sport->port_guid, sizeof(sport->port_guid),
2913 "0x%016llx%016llx",
2914 be64_to_cpu(sport->gid.global.subnet_prefix),
2915 be64_to_cpu(sport->gid.global.interface_id));
2916 }
2917
2918 spin_lock(&srpt_dev_lock);
2919 list_add_tail(&sdev->list, &srpt_dev_list);
2920 spin_unlock(&srpt_dev_lock);
2921
2922out:
2923 ib_set_client_data(device, &srpt_client, sdev);
2924 pr_debug("added %s.\n", device->name);
2925 return;
2926
2927err_ring:
2928 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2929 sdev->srq_size, srp_max_req_size,
2930 DMA_FROM_DEVICE);
2931err_event:
2932 ib_unregister_event_handler(&sdev->event_handler);
2933err_cm:
2934 ib_destroy_cm_id(sdev->cm_id);
2935err_srq:
2936 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002937err_pd:
2938 ib_dealloc_pd(sdev->pd);
2939free_dev:
2940 kfree(sdev);
2941err:
2942 sdev = NULL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002943 pr_info("%s(%s) failed.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002944 goto out;
2945}
2946
2947/**
2948 * srpt_remove_one() - InfiniBand device removal callback function.
2949 */
Haggai Eran7c1eb452015-07-30 17:50:14 +03002950static void srpt_remove_one(struct ib_device *device, void *client_data)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002951{
Haggai Eran7c1eb452015-07-30 17:50:14 +03002952 struct srpt_device *sdev = client_data;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002953 int i;
2954
Bart Van Asschea42d9852011-10-14 01:30:46 +00002955 if (!sdev) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002956 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002957 return;
2958 }
2959
2960 srpt_unregister_mad_agent(sdev);
2961
2962 ib_unregister_event_handler(&sdev->event_handler);
2963
2964 /* Cancel any work queued by the just unregistered IB event handler. */
2965 for (i = 0; i < sdev->device->phys_port_cnt; i++)
2966 cancel_work_sync(&sdev->port[i].work);
2967
2968 ib_destroy_cm_id(sdev->cm_id);
2969
2970 /*
2971 * Unregistering a target must happen after destroying sdev->cm_id
2972 * such that no new SRP_LOGIN_REQ information units can arrive while
2973 * destroying the target.
2974 */
2975 spin_lock(&srpt_dev_lock);
2976 list_del(&sdev->list);
2977 spin_unlock(&srpt_dev_lock);
2978 srpt_release_sdev(sdev);
2979
2980 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002981 ib_dealloc_pd(sdev->pd);
2982
2983 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2984 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
2985 sdev->ioctx_ring = NULL;
2986 kfree(sdev);
2987}
2988
2989static struct ib_client srpt_client = {
2990 .name = DRV_NAME,
2991 .add = srpt_add_one,
2992 .remove = srpt_remove_one
2993};
2994
2995static int srpt_check_true(struct se_portal_group *se_tpg)
2996{
2997 return 1;
2998}
2999
3000static int srpt_check_false(struct se_portal_group *se_tpg)
3001{
3002 return 0;
3003}
3004
3005static char *srpt_get_fabric_name(void)
3006{
3007 return "srpt";
3008}
3009
Bart Van Asschea42d9852011-10-14 01:30:46 +00003010static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3011{
3012 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3013
3014 return sport->port_guid;
3015}
3016
3017static u16 srpt_get_tag(struct se_portal_group *tpg)
3018{
3019 return 1;
3020}
3021
Bart Van Asschea42d9852011-10-14 01:30:46 +00003022static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3023{
3024 return 1;
3025}
3026
3027static void srpt_release_cmd(struct se_cmd *se_cmd)
3028{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08003029 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3030 struct srpt_send_ioctx, cmd);
3031 struct srpt_rdma_ch *ch = ioctx->ch;
3032 unsigned long flags;
3033
3034 WARN_ON(ioctx->state != SRPT_STATE_DONE);
3035 WARN_ON(ioctx->mapped_sg_count != 0);
3036
3037 if (ioctx->n_rbuf > 1) {
3038 kfree(ioctx->rbufs);
3039 ioctx->rbufs = NULL;
3040 ioctx->n_rbuf = 0;
3041 }
3042
3043 spin_lock_irqsave(&ch->spinlock, flags);
3044 list_add(&ioctx->free_list, &ch->free_list);
3045 spin_unlock_irqrestore(&ch->spinlock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003046}
3047
3048/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003049 * srpt_close_session() - Forcibly close a session.
3050 *
3051 * Callback function invoked by the TCM core to clean up sessions associated
3052 * with a node ACL when the user invokes
3053 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3054 */
3055static void srpt_close_session(struct se_session *se_sess)
3056{
3057 DECLARE_COMPLETION_ONSTACK(release_done);
3058 struct srpt_rdma_ch *ch;
3059 struct srpt_device *sdev;
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003060 unsigned long res;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003061
3062 ch = se_sess->fabric_sess_ptr;
3063 WARN_ON(ch->sess != se_sess);
3064
Bart Van Assche33912d72016-02-11 11:04:43 -08003065 pr_debug("ch %p state %d\n", ch, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003066
3067 sdev = ch->sport->sdev;
3068 spin_lock_irq(&sdev->spinlock);
3069 BUG_ON(ch->release_done);
3070 ch->release_done = &release_done;
3071 __srpt_close_ch(ch);
3072 spin_unlock_irq(&sdev->spinlock);
3073
3074 res = wait_for_completion_timeout(&release_done, 60 * HZ);
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003075 WARN_ON(res == 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003076}
3077
3078/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003079 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3080 *
3081 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3082 * This object represents an arbitrary integer used to uniquely identify a
3083 * particular attached remote initiator port to a particular SCSI target port
3084 * within a particular SCSI target device within a particular SCSI instance.
3085 */
3086static u32 srpt_sess_get_index(struct se_session *se_sess)
3087{
3088 return 0;
3089}
3090
3091static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3092{
3093}
3094
Bart Van Asschea42d9852011-10-14 01:30:46 +00003095/* Note: only used from inside debug printk's by the TCM core. */
3096static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3097{
3098 struct srpt_send_ioctx *ioctx;
3099
3100 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3101 return srpt_get_cmd_state(ioctx);
3102}
3103
Bart Van Asschea42d9852011-10-14 01:30:46 +00003104/**
3105 * srpt_parse_i_port_id() - Parse an initiator port ID.
3106 * @name: ASCII representation of a 128-bit initiator port ID.
3107 * @i_port_id: Binary 128-bit port ID.
3108 */
3109static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3110{
3111 const char *p;
3112 unsigned len, count, leading_zero_bytes;
3113 int ret, rc;
3114
3115 p = name;
Rasmus Villemoesb60459f2014-10-13 15:54:46 -07003116 if (strncasecmp(p, "0x", 2) == 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003117 p += 2;
3118 ret = -EINVAL;
3119 len = strlen(p);
3120 if (len % 2)
3121 goto out;
3122 count = min(len / 2, 16U);
3123 leading_zero_bytes = 16 - count;
3124 memset(i_port_id, 0, leading_zero_bytes);
3125 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3126 if (rc < 0)
3127 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3128 ret = 0;
3129out:
3130 return ret;
3131}
3132
3133/*
3134 * configfs callback function invoked for
3135 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3136 */
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003137static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003138{
Bart Van Asschea42d9852011-10-14 01:30:46 +00003139 u8 i_port_id[16];
3140
3141 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003142 pr_err("invalid initiator port ID %s\n", name);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003143 return -EINVAL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003144 }
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003145 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003146}
3147
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003148static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
3149 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003150{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003151 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003152 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3153
3154 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3155}
3156
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003157static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
3158 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003159{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003160 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003161 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3162 unsigned long val;
3163 int ret;
3164
Jingoo Han9d8abf42014-02-05 11:22:05 +09003165 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003166 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003167 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003168 return -EINVAL;
3169 }
3170 if (val > MAX_SRPT_RDMA_SIZE) {
3171 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3172 MAX_SRPT_RDMA_SIZE);
3173 return -EINVAL;
3174 }
3175 if (val < DEFAULT_MAX_RDMA_SIZE) {
3176 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3177 val, DEFAULT_MAX_RDMA_SIZE);
3178 return -EINVAL;
3179 }
3180 sport->port_attrib.srp_max_rdma_size = val;
3181
3182 return count;
3183}
3184
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003185static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
3186 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003187{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003188 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003189 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3190
3191 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3192}
3193
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003194static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
3195 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003196{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003197 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003198 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3199 unsigned long val;
3200 int ret;
3201
Jingoo Han9d8abf42014-02-05 11:22:05 +09003202 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003203 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003204 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003205 return -EINVAL;
3206 }
3207 if (val > MAX_SRPT_RSP_SIZE) {
3208 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3209 MAX_SRPT_RSP_SIZE);
3210 return -EINVAL;
3211 }
3212 if (val < MIN_MAX_RSP_SIZE) {
3213 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3214 MIN_MAX_RSP_SIZE);
3215 return -EINVAL;
3216 }
3217 sport->port_attrib.srp_max_rsp_size = val;
3218
3219 return count;
3220}
3221
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003222static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
3223 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003224{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003225 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003226 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3227
3228 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3229}
3230
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003231static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
3232 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003233{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003234 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003235 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3236 unsigned long val;
3237 int ret;
3238
Jingoo Han9d8abf42014-02-05 11:22:05 +09003239 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003240 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003241 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003242 return -EINVAL;
3243 }
3244 if (val > MAX_SRPT_SRQ_SIZE) {
3245 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3246 MAX_SRPT_SRQ_SIZE);
3247 return -EINVAL;
3248 }
3249 if (val < MIN_SRPT_SRQ_SIZE) {
3250 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3251 MIN_SRPT_SRQ_SIZE);
3252 return -EINVAL;
3253 }
3254 sport->port_attrib.srp_sq_size = val;
3255
3256 return count;
3257}
3258
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003259CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
3260CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
3261CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003262
3263static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003264 &srpt_tpg_attrib_attr_srp_max_rdma_size,
3265 &srpt_tpg_attrib_attr_srp_max_rsp_size,
3266 &srpt_tpg_attrib_attr_srp_sq_size,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003267 NULL,
3268};
3269
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003270static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003271{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003272 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003273 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3274
3275 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3276}
3277
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003278static ssize_t srpt_tpg_enable_store(struct config_item *item,
3279 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003280{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003281 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003282 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3283 unsigned long tmp;
3284 int ret;
3285
Jingoo Han9d8abf42014-02-05 11:22:05 +09003286 ret = kstrtoul(page, 0, &tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003287 if (ret < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003288 pr_err("Unable to extract srpt_tpg_store_enable\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003289 return -EINVAL;
3290 }
3291
3292 if ((tmp != 0) && (tmp != 1)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003293 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003294 return -EINVAL;
3295 }
3296 if (tmp == 1)
3297 sport->enabled = true;
3298 else
3299 sport->enabled = false;
3300
3301 return count;
3302}
3303
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003304CONFIGFS_ATTR(srpt_tpg_, enable);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003305
3306static struct configfs_attribute *srpt_tpg_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003307 &srpt_tpg_attr_enable,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003308 NULL,
3309};
3310
3311/**
3312 * configfs callback invoked for
3313 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3314 */
3315static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3316 struct config_group *group,
3317 const char *name)
3318{
3319 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3320 int res;
3321
3322 /* Initialize sport->port_wwn and sport->port_tpg_1 */
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07003323 res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003324 if (res)
3325 return ERR_PTR(res);
3326
3327 return &sport->port_tpg_1;
3328}
3329
3330/**
3331 * configfs callback invoked for
3332 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3333 */
3334static void srpt_drop_tpg(struct se_portal_group *tpg)
3335{
3336 struct srpt_port *sport = container_of(tpg,
3337 struct srpt_port, port_tpg_1);
3338
3339 sport->enabled = false;
3340 core_tpg_deregister(&sport->port_tpg_1);
3341}
3342
3343/**
3344 * configfs callback invoked for
3345 * mkdir /sys/kernel/config/target/$driver/$port
3346 */
3347static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3348 struct config_group *group,
3349 const char *name)
3350{
3351 struct srpt_port *sport;
3352 int ret;
3353
3354 sport = srpt_lookup_port(name);
3355 pr_debug("make_tport(%s)\n", name);
3356 ret = -EINVAL;
3357 if (!sport)
3358 goto err;
3359
3360 return &sport->port_wwn;
3361
3362err:
3363 return ERR_PTR(ret);
3364}
3365
3366/**
3367 * configfs callback invoked for
3368 * rmdir /sys/kernel/config/target/$driver/$port
3369 */
3370static void srpt_drop_tport(struct se_wwn *wwn)
3371{
3372 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3373
3374 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3375}
3376
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003377static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003378{
3379 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3380}
3381
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003382CONFIGFS_ATTR_RO(srpt_wwn_, version);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003383
3384static struct configfs_attribute *srpt_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003385 &srpt_wwn_attr_version,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003386 NULL,
3387};
3388
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003389static const struct target_core_fabric_ops srpt_template = {
3390 .module = THIS_MODULE,
3391 .name = "srpt",
Bart Van Asschea42d9852011-10-14 01:30:46 +00003392 .get_fabric_name = srpt_get_fabric_name,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003393 .tpg_get_wwn = srpt_get_fabric_wwn,
3394 .tpg_get_tag = srpt_get_tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003395 .tpg_check_demo_mode = srpt_check_false,
3396 .tpg_check_demo_mode_cache = srpt_check_true,
3397 .tpg_check_demo_mode_write_protect = srpt_check_true,
3398 .tpg_check_prod_mode_write_protect = srpt_check_false,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003399 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3400 .release_cmd = srpt_release_cmd,
3401 .check_stop_free = srpt_check_stop_free,
3402 .shutdown_session = srpt_shutdown_session,
3403 .close_session = srpt_close_session,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003404 .sess_get_index = srpt_sess_get_index,
3405 .sess_get_initiator_sid = NULL,
3406 .write_pending = srpt_write_pending,
3407 .write_pending_status = srpt_write_pending_status,
3408 .set_default_node_attributes = srpt_set_default_node_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003409 .get_cmd_state = srpt_get_tcm_cmd_state,
Joern Engelb79fafa2013-07-03 11:22:17 -04003410 .queue_data_in = srpt_queue_data_in,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003411 .queue_status = srpt_queue_status,
Joern Engelb79fafa2013-07-03 11:22:17 -04003412 .queue_tm_rsp = srpt_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003413 .aborted_task = srpt_aborted_task,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003414 /*
3415 * Setup function pointers for generic logic in
3416 * target_core_fabric_configfs.c
3417 */
3418 .fabric_make_wwn = srpt_make_tport,
3419 .fabric_drop_wwn = srpt_drop_tport,
3420 .fabric_make_tpg = srpt_make_tpg,
3421 .fabric_drop_tpg = srpt_drop_tpg,
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003422 .fabric_init_nodeacl = srpt_init_nodeacl,
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003423
3424 .tfc_wwn_attrs = srpt_wwn_attrs,
3425 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3426 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003427};
3428
3429/**
3430 * srpt_init_module() - Kernel module initialization.
3431 *
3432 * Note: Since ib_register_client() registers callback functions, and since at
3433 * least one of these callback functions (srpt_add_one()) calls target core
3434 * functions, this driver must be registered with the target core before
3435 * ib_register_client() is called.
3436 */
3437static int __init srpt_init_module(void)
3438{
3439 int ret;
3440
3441 ret = -EINVAL;
3442 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003443 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003444 " srp_max_req_size -- must be at least %d.\n",
3445 srp_max_req_size, MIN_MAX_REQ_SIZE);
3446 goto out;
3447 }
3448
3449 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3450 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003451 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003452 " srpt_srq_size -- must be in the range [%d..%d].\n",
3453 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3454 goto out;
3455 }
3456
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003457 ret = target_register_template(&srpt_template);
3458 if (ret)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003459 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003460
3461 ret = ib_register_client(&srpt_client);
3462 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003463 pr_err("couldn't register IB client\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003464 goto out_unregister_target;
3465 }
3466
3467 return 0;
3468
3469out_unregister_target:
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003470 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003471out:
3472 return ret;
3473}
3474
3475static void __exit srpt_cleanup_module(void)
3476{
3477 ib_unregister_client(&srpt_client);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003478 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003479}
3480
3481module_init(srpt_init_module);
3482module_exit(srpt_cleanup_module);