blob: 5da59f4850436789d13c0300e4bf5db8d87c7c3e [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 Assche9d2aa2b2016-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 Assche9d2aa2b2016-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 Assche9d2aa2b2016-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 Assche9d2aa2b2016-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 Assche9d2aa2b2016-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 Assche9d2aa2b2016-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 Assche9d2aa2b2016-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 Assche9d2aa2b2016-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 Assche9d2aa2b2016-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 Assche9d2aa2b2016-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 Assche9d2aa2b2016-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 Assche9d2aa2b2016-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 Assche9d2aa2b2016-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
1497#define NO_SUCH_LUN ((uint64_t)-1LL)
1498
1499/*
1500 * SCSI LUN addressing method. See also SAM-2 and the section about
1501 * eight byte LUNs.
1502 */
1503enum scsi_lun_addr_method {
1504 SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
1505 SCSI_LUN_ADDR_METHOD_FLAT = 1,
1506 SCSI_LUN_ADDR_METHOD_LUN = 2,
1507 SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
1508};
1509
1510/*
1511 * srpt_unpack_lun() - Convert from network LUN to linear LUN.
1512 *
1513 * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
1514 * order (big endian) to a linear LUN. Supports three LUN addressing methods:
1515 * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
1516 */
1517static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
1518{
1519 uint64_t res = NO_SUCH_LUN;
1520 int addressing_method;
1521
1522 if (unlikely(len < 2)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001523 pr_err("Illegal LUN length %d, expected 2 bytes or more\n",
1524 len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001525 goto out;
1526 }
1527
1528 switch (len) {
1529 case 8:
1530 if ((*((__be64 *)lun) &
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301531 cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001532 goto out_err;
1533 break;
1534 case 4:
1535 if (*((__be16 *)&lun[2]) != 0)
1536 goto out_err;
1537 break;
1538 case 6:
1539 if (*((__be32 *)&lun[2]) != 0)
1540 goto out_err;
1541 break;
1542 case 2:
1543 break;
1544 default:
1545 goto out_err;
1546 }
1547
1548 addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
1549 switch (addressing_method) {
1550 case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
1551 case SCSI_LUN_ADDR_METHOD_FLAT:
1552 case SCSI_LUN_ADDR_METHOD_LUN:
1553 res = *(lun + 1) | (((*lun) & 0x3f) << 8);
1554 break;
1555
1556 case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
1557 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001558 pr_err("Unimplemented LUN addressing method %u\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001559 addressing_method);
1560 break;
1561 }
1562
1563out:
1564 return res;
1565
1566out_err:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001567 pr_err("Support for multi-level LUNs has not yet been implemented\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001568 goto out;
1569}
1570
1571static int srpt_check_stop_free(struct se_cmd *cmd)
1572{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001573 struct srpt_send_ioctx *ioctx = container_of(cmd,
1574 struct srpt_send_ioctx, cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001575
Bart Van Asscheafc16602015-04-27 13:52:36 +02001576 return target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001577}
1578
1579/**
1580 * srpt_handle_cmd() - Process SRP_CMD.
1581 */
1582static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
1583 struct srpt_recv_ioctx *recv_ioctx,
1584 struct srpt_send_ioctx *send_ioctx)
1585{
1586 struct se_cmd *cmd;
1587 struct srp_cmd *srp_cmd;
1588 uint64_t unpacked_lun;
1589 u64 data_len;
1590 enum dma_data_direction dir;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001591 sense_reason_t ret;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001592 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001593
1594 BUG_ON(!send_ioctx);
1595
1596 srp_cmd = recv_ioctx->ioctx.buf;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001597 cmd = &send_ioctx->cmd;
Bart Van Assche649ee052015-04-14 13:26:44 +02001598 cmd->tag = srp_cmd->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001599
1600 switch (srp_cmd->task_attr) {
1601 case SRP_CMD_SIMPLE_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001602 cmd->sam_task_attr = TCM_SIMPLE_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001603 break;
1604 case SRP_CMD_ORDERED_Q:
1605 default:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001606 cmd->sam_task_attr = TCM_ORDERED_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001607 break;
1608 case SRP_CMD_HEAD_OF_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001609 cmd->sam_task_attr = TCM_HEAD_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001610 break;
1611 case SRP_CMD_ACA:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001612 cmd->sam_task_attr = TCM_ACA_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001613 break;
1614 }
1615
Christoph Hellwigde103c92012-11-06 12:24:09 -08001616 if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001617 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001618 srp_cmd->tag);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001619 ret = TCM_INVALID_CDB_FIELD;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001620 goto send_sense;
1621 }
1622
Bart Van Asschea42d9852011-10-14 01:30:46 +00001623 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
1624 sizeof(srp_cmd->lun));
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001625 rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
1626 &send_ioctx->sense_data[0], unpacked_lun, data_len,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001627 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001628 if (rc != 0) {
1629 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001630 goto send_sense;
Nicholas Bellinger187e70a2012-03-17 20:12:36 -07001631 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001632 return 0;
1633
1634send_sense:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001635 transport_send_check_condition_and_sense(cmd, ret, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001636 return -1;
1637}
1638
Bart Van Asschea42d9852011-10-14 01:30:46 +00001639static int srp_tmr_to_tcm(int fn)
1640{
1641 switch (fn) {
1642 case SRP_TSK_ABORT_TASK:
1643 return TMR_ABORT_TASK;
1644 case SRP_TSK_ABORT_TASK_SET:
1645 return TMR_ABORT_TASK_SET;
1646 case SRP_TSK_CLEAR_TASK_SET:
1647 return TMR_CLEAR_TASK_SET;
1648 case SRP_TSK_LUN_RESET:
1649 return TMR_LUN_RESET;
1650 case SRP_TSK_CLEAR_ACA:
1651 return TMR_CLEAR_ACA;
1652 default:
1653 return -1;
1654 }
1655}
1656
1657/**
1658 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1659 *
1660 * Returns 0 if and only if the request will be processed by the target core.
1661 *
1662 * For more information about SRP_TSK_MGMT information units, see also section
1663 * 6.7 in the SRP r16a document.
1664 */
1665static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1666 struct srpt_recv_ioctx *recv_ioctx,
1667 struct srpt_send_ioctx *send_ioctx)
1668{
1669 struct srp_tsk_mgmt *srp_tsk;
1670 struct se_cmd *cmd;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001671 struct se_session *sess = ch->sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001672 uint64_t unpacked_lun;
1673 int tcm_tmr;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001674 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001675
1676 BUG_ON(!send_ioctx);
1677
1678 srp_tsk = recv_ioctx->ioctx.buf;
1679 cmd = &send_ioctx->cmd;
1680
1681 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1682 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1683 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1684
1685 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
Bart Van Assche649ee052015-04-14 13:26:44 +02001686 send_ioctx->cmd.tag = srp_tsk->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001687 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001688 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
1689 sizeof(srp_tsk->lun));
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001690 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, unpacked_lun,
Bart Van Assche51093252016-02-11 11:03:09 -08001691 srp_tsk, tcm_tmr, GFP_KERNEL, srp_tsk->task_tag,
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001692 TARGET_SCF_ACK_KREF);
1693 if (rc != 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001694 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001695 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001696 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001697 return;
1698fail:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001699 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001700}
1701
1702/**
1703 * srpt_handle_new_iu() - Process a newly received information unit.
1704 * @ch: RDMA channel through which the information unit has been received.
1705 * @ioctx: SRPT I/O context associated with the information unit.
1706 */
1707static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1708 struct srpt_recv_ioctx *recv_ioctx,
1709 struct srpt_send_ioctx *send_ioctx)
1710{
1711 struct srp_cmd *srp_cmd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001712
1713 BUG_ON(!ch);
1714 BUG_ON(!recv_ioctx);
1715
1716 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1717 recv_ioctx->ioctx.dma, srp_max_req_size,
1718 DMA_FROM_DEVICE);
1719
Bart Van Assche33912d72016-02-11 11:04:43 -08001720 if (unlikely(ch->state == CH_CONNECTING)) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001721 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1722 goto out;
1723 }
1724
Bart Van Assche33912d72016-02-11 11:04:43 -08001725 if (unlikely(ch->state != CH_LIVE))
Bart Van Asschea42d9852011-10-14 01:30:46 +00001726 goto out;
1727
1728 srp_cmd = recv_ioctx->ioctx.buf;
1729 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1730 if (!send_ioctx)
1731 send_ioctx = srpt_get_send_ioctx(ch);
1732 if (unlikely(!send_ioctx)) {
1733 list_add_tail(&recv_ioctx->wait_list,
1734 &ch->cmd_wait_list);
1735 goto out;
1736 }
1737 }
1738
Bart Van Asschea42d9852011-10-14 01:30:46 +00001739 switch (srp_cmd->opcode) {
1740 case SRP_CMD:
1741 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1742 break;
1743 case SRP_TSK_MGMT:
1744 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1745 break;
1746 case SRP_I_LOGOUT:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001747 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001748 break;
1749 case SRP_CRED_RSP:
1750 pr_debug("received SRP_CRED_RSP\n");
1751 break;
1752 case SRP_AER_RSP:
1753 pr_debug("received SRP_AER_RSP\n");
1754 break;
1755 case SRP_RSP:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001756 pr_err("Received SRP_RSP\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001757 break;
1758 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001759 pr_err("received IU with unknown opcode 0x%x\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001760 srp_cmd->opcode);
1761 break;
1762 }
1763
1764 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1765out:
1766 return;
1767}
1768
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001769static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001770{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001771 struct srpt_rdma_ch *ch = cq->cq_context;
1772 struct srpt_recv_ioctx *ioctx =
1773 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001774
Bart Van Asschea42d9852011-10-14 01:30:46 +00001775 if (wc->status == IB_WC_SUCCESS) {
1776 int req_lim;
1777
1778 req_lim = atomic_dec_return(&ch->req_lim);
1779 if (unlikely(req_lim < 0))
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001780 pr_err("req_lim = %d < 0\n", req_lim);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001781 srpt_handle_new_iu(ch, ioctx, NULL);
1782 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001783 pr_info("receiving failed for ioctx %p with status %d\n",
1784 ioctx, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001785 }
1786}
1787
1788/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001789 * Note: Although this has not yet been observed during tests, at least in
1790 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1791 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1792 * value in each response is set to one, and it is possible that this response
1793 * makes the initiator send a new request before the send completion for that
1794 * response has been processed. This could e.g. happen if the call to
1795 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1796 * if IB retransmission causes generation of the send completion to be
1797 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1798 * are queued on cmd_wait_list. The code below processes these delayed
1799 * requests one at a time.
1800 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001801static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001802{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001803 struct srpt_rdma_ch *ch = cq->cq_context;
1804 struct srpt_send_ioctx *ioctx =
1805 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1806 enum srpt_command_state state;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001807
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001808 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1809
1810 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1811 state != SRPT_STATE_MGMT_RSP_SENT);
1812
1813 atomic_inc(&ch->sq_wr_avail);
1814
1815 if (wc->status != IB_WC_SUCCESS) {
1816 pr_info("sending response for ioctx 0x%p failed"
1817 " with status %d\n", ioctx, wc->status);
1818
1819 atomic_dec(&ch->req_lim);
1820 srpt_abort_cmd(ioctx);
1821 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001822 }
1823
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001824 if (state != SRPT_STATE_DONE) {
1825 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1826 transport_generic_free_cmd(&ioctx->cmd, 0);
1827 } else {
1828 pr_err("IB completion has been received too late for"
1829 " wr_id = %u.\n", ioctx->ioctx.index);
1830 }
1831
1832out:
1833 while (!list_empty(&ch->cmd_wait_list) &&
Bart Van Assche33912d72016-02-11 11:04:43 -08001834 ch->state == CH_LIVE &&
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001835 (ioctx = srpt_get_send_ioctx(ch)) != NULL) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001836 struct srpt_recv_ioctx *recv_ioctx;
1837
1838 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
1839 struct srpt_recv_ioctx,
1840 wait_list);
1841 list_del(&recv_ioctx->wait_list);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001842 srpt_handle_new_iu(ch, recv_ioctx, ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001843 }
1844}
1845
Bart Van Asschea42d9852011-10-14 01:30:46 +00001846/**
1847 * srpt_create_ch_ib() - Create receive and send completion queues.
1848 */
1849static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1850{
1851 struct ib_qp_init_attr *qp_init;
1852 struct srpt_port *sport = ch->sport;
1853 struct srpt_device *sdev = sport->sdev;
1854 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
1855 int ret;
1856
1857 WARN_ON(ch->rq_size < 1);
1858
1859 ret = -ENOMEM;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08001860 qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001861 if (!qp_init)
1862 goto out;
1863
Bart Van Asscheab477c12014-10-19 18:05:33 +03001864retry:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001865 ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size,
1866 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001867 if (IS_ERR(ch->cq)) {
1868 ret = PTR_ERR(ch->cq);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001869 pr_err("failed to create CQ cqe= %d ret= %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001870 ch->rq_size + srp_sq_size, ret);
1871 goto out;
1872 }
1873
1874 qp_init->qp_context = (void *)ch;
1875 qp_init->event_handler
1876 = (void(*)(struct ib_event *, void*))srpt_qp_event;
1877 qp_init->send_cq = ch->cq;
1878 qp_init->recv_cq = ch->cq;
1879 qp_init->srq = sdev->srq;
1880 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1881 qp_init->qp_type = IB_QPT_RC;
1882 qp_init->cap.max_send_wr = srp_sq_size;
1883 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
1884
1885 ch->qp = ib_create_qp(sdev->pd, qp_init);
1886 if (IS_ERR(ch->qp)) {
1887 ret = PTR_ERR(ch->qp);
Bart Van Asscheab477c12014-10-19 18:05:33 +03001888 if (ret == -ENOMEM) {
1889 srp_sq_size /= 2;
1890 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
1891 ib_destroy_cq(ch->cq);
1892 goto retry;
1893 }
1894 }
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001895 pr_err("failed to create_qp ret= %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001896 goto err_destroy_cq;
1897 }
1898
1899 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1900
1901 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
1902 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1903 qp_init->cap.max_send_wr, ch->cm_id);
1904
1905 ret = srpt_init_ch_qp(ch, ch->qp);
1906 if (ret)
1907 goto err_destroy_qp;
1908
Bart Van Asschea42d9852011-10-14 01:30:46 +00001909out:
1910 kfree(qp_init);
1911 return ret;
1912
1913err_destroy_qp:
1914 ib_destroy_qp(ch->qp);
1915err_destroy_cq:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001916 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001917 goto out;
1918}
1919
1920static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
1921{
Bart Van Asschea42d9852011-10-14 01:30:46 +00001922 ib_destroy_qp(ch->qp);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001923 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001924}
1925
1926/**
1927 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
1928 *
1929 * Reset the QP and make sure all resources associated with the channel will
1930 * be deallocated at an appropriate time.
1931 *
1932 * Note: The caller must hold ch->sport->sdev->spinlock.
1933 */
1934static void __srpt_close_ch(struct srpt_rdma_ch *ch)
1935{
Bart Van Asschea42d9852011-10-14 01:30:46 +00001936 enum rdma_ch_state prev_state;
1937 unsigned long flags;
1938
Bart Van Asschea42d9852011-10-14 01:30:46 +00001939 spin_lock_irqsave(&ch->spinlock, flags);
1940 prev_state = ch->state;
1941 switch (prev_state) {
1942 case CH_CONNECTING:
1943 case CH_LIVE:
1944 ch->state = CH_DISCONNECTING;
1945 break;
1946 default:
1947 break;
1948 }
1949 spin_unlock_irqrestore(&ch->spinlock, flags);
1950
1951 switch (prev_state) {
1952 case CH_CONNECTING:
1953 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
1954 NULL, 0);
1955 /* fall through */
1956 case CH_LIVE:
1957 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001958 pr_err("sending CM DREQ failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001959 break;
1960 case CH_DISCONNECTING:
1961 break;
1962 case CH_DRAINING:
1963 case CH_RELEASING:
1964 break;
1965 }
1966}
1967
1968/**
1969 * srpt_close_ch() - Close an RDMA channel.
1970 */
1971static void srpt_close_ch(struct srpt_rdma_ch *ch)
1972{
1973 struct srpt_device *sdev;
1974
1975 sdev = ch->sport->sdev;
1976 spin_lock_irq(&sdev->spinlock);
1977 __srpt_close_ch(ch);
1978 spin_unlock_irq(&sdev->spinlock);
1979}
1980
1981/**
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07001982 * srpt_shutdown_session() - Whether or not a session may be shut down.
1983 */
1984static int srpt_shutdown_session(struct se_session *se_sess)
1985{
1986 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
1987 unsigned long flags;
1988
1989 spin_lock_irqsave(&ch->spinlock, flags);
1990 if (ch->in_shutdown) {
1991 spin_unlock_irqrestore(&ch->spinlock, flags);
1992 return true;
1993 }
1994
1995 ch->in_shutdown = true;
1996 target_sess_cmd_list_set_waiting(se_sess);
1997 spin_unlock_irqrestore(&ch->spinlock, flags);
1998
1999 return true;
2000}
2001
2002/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00002003 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
2004 * @cm_id: Pointer to the CM ID of the channel to be drained.
2005 *
2006 * Note: Must be called from inside srpt_cm_handler to avoid a race between
2007 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
2008 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
2009 * waits until all target sessions for the associated IB device have been
2010 * unregistered and target session registration involves a call to
2011 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
2012 * this function has finished).
2013 */
2014static void srpt_drain_channel(struct ib_cm_id *cm_id)
2015{
2016 struct srpt_device *sdev;
2017 struct srpt_rdma_ch *ch;
2018 int ret;
2019 bool do_reset = false;
2020
2021 WARN_ON_ONCE(irqs_disabled());
2022
2023 sdev = cm_id->context;
2024 BUG_ON(!sdev);
2025 spin_lock_irq(&sdev->spinlock);
2026 list_for_each_entry(ch, &sdev->rch_list, list) {
2027 if (ch->cm_id == cm_id) {
2028 do_reset = srpt_test_and_set_ch_state(ch,
2029 CH_CONNECTING, CH_DRAINING) ||
2030 srpt_test_and_set_ch_state(ch,
2031 CH_LIVE, CH_DRAINING) ||
2032 srpt_test_and_set_ch_state(ch,
2033 CH_DISCONNECTING, CH_DRAINING);
2034 break;
2035 }
2036 }
2037 spin_unlock_irq(&sdev->spinlock);
2038
2039 if (do_reset) {
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07002040 if (ch->sess)
2041 srpt_shutdown_session(ch->sess);
2042
Bart Van Asschea42d9852011-10-14 01:30:46 +00002043 ret = srpt_ch_qp_err(ch);
2044 if (ret < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002045 pr_err("Setting queue pair in error state"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002046 " failed: %d\n", ret);
2047 }
2048}
2049
2050/**
2051 * srpt_find_channel() - Look up an RDMA channel.
2052 * @cm_id: Pointer to the CM ID of the channel to be looked up.
2053 *
2054 * Return NULL if no matching RDMA channel has been found.
2055 */
2056static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
2057 struct ib_cm_id *cm_id)
2058{
2059 struct srpt_rdma_ch *ch;
2060 bool found;
2061
2062 WARN_ON_ONCE(irqs_disabled());
2063 BUG_ON(!sdev);
2064
2065 found = false;
2066 spin_lock_irq(&sdev->spinlock);
2067 list_for_each_entry(ch, &sdev->rch_list, list) {
2068 if (ch->cm_id == cm_id) {
2069 found = true;
2070 break;
2071 }
2072 }
2073 spin_unlock_irq(&sdev->spinlock);
2074
2075 return found ? ch : NULL;
2076}
2077
2078/**
2079 * srpt_release_channel() - Release channel resources.
2080 *
2081 * Schedules the actual release because:
2082 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
2083 * trigger a deadlock.
2084 * - It is not safe to call TCM transport_* functions from interrupt context.
2085 */
2086static void srpt_release_channel(struct srpt_rdma_ch *ch)
2087{
2088 schedule_work(&ch->release_work);
2089}
2090
2091static void srpt_release_channel_work(struct work_struct *w)
2092{
2093 struct srpt_rdma_ch *ch;
2094 struct srpt_device *sdev;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002095 struct se_session *se_sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002096
2097 ch = container_of(w, struct srpt_rdma_ch, release_work);
2098 pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2099 ch->release_done);
2100
2101 sdev = ch->sport->sdev;
2102 BUG_ON(!sdev);
2103
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002104 se_sess = ch->sess;
2105 BUG_ON(!se_sess);
2106
Joern Engelbe646c2d2013-05-15 00:44:07 -07002107 target_wait_for_sess_cmds(se_sess);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002108
2109 transport_deregister_session_configfs(se_sess);
2110 transport_deregister_session(se_sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002111 ch->sess = NULL;
2112
Nicholas Bellinger0b41d6c2013-09-18 12:48:27 -07002113 ib_destroy_cm_id(ch->cm_id);
2114
Bart Van Asschea42d9852011-10-14 01:30:46 +00002115 srpt_destroy_ch_ib(ch);
2116
2117 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2118 ch->sport->sdev, ch->rq_size,
2119 ch->rsp_size, DMA_TO_DEVICE);
2120
2121 spin_lock_irq(&sdev->spinlock);
2122 list_del(&ch->list);
2123 spin_unlock_irq(&sdev->spinlock);
2124
Bart Van Asschea42d9852011-10-14 01:30:46 +00002125 if (ch->release_done)
2126 complete(ch->release_done);
2127
2128 wake_up(&sdev->ch_releaseQ);
2129
2130 kfree(ch);
2131}
2132
Bart Van Asschea42d9852011-10-14 01:30:46 +00002133/**
2134 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2135 *
2136 * Ownership of the cm_id is transferred to the target session if this
2137 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2138 */
2139static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2140 struct ib_cm_req_event_param *param,
2141 void *private_data)
2142{
2143 struct srpt_device *sdev = cm_id->context;
2144 struct srpt_port *sport = &sdev->port[param->port - 1];
2145 struct srp_login_req *req;
2146 struct srp_login_rsp *rsp;
2147 struct srp_login_rej *rej;
2148 struct ib_cm_rep_param *rep_param;
2149 struct srpt_rdma_ch *ch, *tmp_ch;
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002150 struct se_node_acl *se_acl;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002151 u32 it_iu_len;
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002152 int i, ret = 0;
2153 unsigned char *p;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002154
2155 WARN_ON_ONCE(irqs_disabled());
2156
2157 if (WARN_ON(!sdev || !private_data))
2158 return -EINVAL;
2159
2160 req = (struct srp_login_req *)private_data;
2161
2162 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2163
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002164 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2165 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2166 " (guid=0x%llx:0x%llx)\n",
2167 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2168 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2169 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2170 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2171 it_iu_len,
2172 param->port,
2173 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2174 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002175
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002176 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
2177 rej = kzalloc(sizeof(*rej), GFP_KERNEL);
2178 rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002179
2180 if (!rsp || !rej || !rep_param) {
2181 ret = -ENOMEM;
2182 goto out;
2183 }
2184
2185 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302186 rej->reason = cpu_to_be32(
2187 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002188 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002189 pr_err("rejected SRP_LOGIN_REQ because its"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002190 " length (%d bytes) is out of range (%d .. %d)\n",
2191 it_iu_len, 64, srp_max_req_size);
2192 goto reject;
2193 }
2194
2195 if (!sport->enabled) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302196 rej->reason = cpu_to_be32(
2197 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002198 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002199 pr_err("rejected SRP_LOGIN_REQ because the target port"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002200 " has not yet been enabled\n");
2201 goto reject;
2202 }
2203
2204 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2205 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2206
2207 spin_lock_irq(&sdev->spinlock);
2208
2209 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2210 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2211 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2212 && param->port == ch->sport->port
2213 && param->listen_id == ch->sport->sdev->cm_id
2214 && ch->cm_id) {
Bart Van Assche33912d72016-02-11 11:04:43 -08002215 if (ch->state != CH_CONNECTING
2216 && ch->state != CH_LIVE)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002217 continue;
2218
2219 /* found an existing channel */
2220 pr_debug("Found existing channel %s"
2221 " cm_id= %p state= %d\n",
Bart Van Assche33912d72016-02-11 11:04:43 -08002222 ch->sess_name, ch->cm_id, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002223
2224 __srpt_close_ch(ch);
2225
2226 rsp->rsp_flags =
2227 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2228 }
2229 }
2230
2231 spin_unlock_irq(&sdev->spinlock);
2232
2233 } else
2234 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2235
2236 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2237 || *(__be64 *)(req->target_port_id + 8) !=
2238 cpu_to_be64(srpt_service_guid)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302239 rej->reason = cpu_to_be32(
2240 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002241 ret = -ENOMEM;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002242 pr_err("rejected SRP_LOGIN_REQ because it"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002243 " has an invalid target port identifier.\n");
2244 goto reject;
2245 }
2246
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002247 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002248 if (!ch) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302249 rej->reason = cpu_to_be32(
2250 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002251 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002252 ret = -ENOMEM;
2253 goto reject;
2254 }
2255
2256 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2257 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2258 memcpy(ch->t_port_id, req->target_port_id, 16);
2259 ch->sport = &sdev->port[param->port - 1];
2260 ch->cm_id = cm_id;
2261 /*
2262 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2263 * for the SRP protocol to the command queue size.
2264 */
2265 ch->rq_size = SRPT_RQ_SIZE;
2266 spin_lock_init(&ch->spinlock);
2267 ch->state = CH_CONNECTING;
2268 INIT_LIST_HEAD(&ch->cmd_wait_list);
2269 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2270
2271 ch->ioctx_ring = (struct srpt_send_ioctx **)
2272 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2273 sizeof(*ch->ioctx_ring[0]),
2274 ch->rsp_size, DMA_TO_DEVICE);
2275 if (!ch->ioctx_ring)
2276 goto free_ch;
2277
2278 INIT_LIST_HEAD(&ch->free_list);
2279 for (i = 0; i < ch->rq_size; i++) {
2280 ch->ioctx_ring[i]->ch = ch;
2281 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2282 }
2283
2284 ret = srpt_create_ch_ib(ch);
2285 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302286 rej->reason = cpu_to_be32(
2287 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002288 pr_err("rejected SRP_LOGIN_REQ because creating"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002289 " a new RDMA channel failed.\n");
2290 goto free_ring;
2291 }
2292
2293 ret = srpt_ch_qp_rtr(ch, ch->qp);
2294 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302295 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002296 pr_err("rejected SRP_LOGIN_REQ because enabling"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002297 " RTR failed (error code = %d)\n", ret);
2298 goto destroy_ib;
2299 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002300
Bart Van Asschea42d9852011-10-14 01:30:46 +00002301 /*
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002302 * Use the initator port identifier as the session name, when
2303 * checking against se_node_acl->initiatorname[] this can be
2304 * with or without preceeding '0x'.
Bart Van Asschea42d9852011-10-14 01:30:46 +00002305 */
2306 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2307 be64_to_cpu(*(__be64 *)ch->i_port_id),
2308 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2309
2310 pr_debug("registering session %s\n", ch->sess_name);
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002311 p = &ch->sess_name[0];
Bart Van Asschea42d9852011-10-14 01:30:46 +00002312
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002313 ch->sess = transport_init_session(TARGET_PROT_NORMAL);
Dan Carpenter3af33632011-11-04 21:27:32 +03002314 if (IS_ERR(ch->sess)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302315 rej->reason = cpu_to_be32(
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002316 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002317 pr_debug("Failed to create session\n");
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002318 goto destroy_ib;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002319 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002320
2321try_again:
2322 se_acl = core_tpg_get_initiator_node_acl(&sport->port_tpg_1, p);
2323 if (!se_acl) {
2324 pr_info("Rejected login because no ACL has been"
2325 " configured yet for initiator %s.\n", ch->sess_name);
2326 /*
2327 * XXX: Hack to retry of ch->i_port_id without leading '0x'
2328 */
2329 if (p == &ch->sess_name[0]) {
2330 p += 2;
2331 goto try_again;
2332 }
2333 rej->reason = cpu_to_be32(
2334 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2335 transport_free_session(ch->sess);
2336 goto destroy_ib;
2337 }
2338 ch->sess->se_node_acl = se_acl;
2339
2340 transport_register_session(&sport->port_tpg_1, se_acl, ch->sess, ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002341
2342 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2343 ch->sess_name, ch->cm_id);
2344
2345 /* create srp_login_response */
2346 rsp->opcode = SRP_LOGIN_RSP;
2347 rsp->tag = req->tag;
2348 rsp->max_it_iu_len = req->req_it_iu_len;
2349 rsp->max_ti_iu_len = req->req_it_iu_len;
2350 ch->max_ti_iu_len = it_iu_len;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302351 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2352 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002353 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2354 atomic_set(&ch->req_lim, ch->rq_size);
2355 atomic_set(&ch->req_lim_delta, 0);
2356
2357 /* create cm reply */
2358 rep_param->qp_num = ch->qp->qp_num;
2359 rep_param->private_data = (void *)rsp;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002360 rep_param->private_data_len = sizeof(*rsp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002361 rep_param->rnr_retry_count = 7;
2362 rep_param->flow_control = 1;
2363 rep_param->failover_accepted = 0;
2364 rep_param->srq = 1;
2365 rep_param->responder_resources = 4;
2366 rep_param->initiator_depth = 4;
2367
2368 ret = ib_send_cm_rep(cm_id, rep_param);
2369 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002370 pr_err("sending SRP_LOGIN_REQ response failed"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002371 " (error code = %d)\n", ret);
2372 goto release_channel;
2373 }
2374
2375 spin_lock_irq(&sdev->spinlock);
2376 list_add_tail(&ch->list, &sdev->rch_list);
2377 spin_unlock_irq(&sdev->spinlock);
2378
2379 goto out;
2380
2381release_channel:
2382 srpt_set_ch_state(ch, CH_RELEASING);
2383 transport_deregister_session_configfs(ch->sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002384 transport_deregister_session(ch->sess);
2385 ch->sess = NULL;
2386
2387destroy_ib:
2388 srpt_destroy_ch_ib(ch);
2389
2390free_ring:
2391 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2392 ch->sport->sdev, ch->rq_size,
2393 ch->rsp_size, DMA_TO_DEVICE);
2394free_ch:
2395 kfree(ch);
2396
2397reject:
2398 rej->opcode = SRP_LOGIN_REJ;
2399 rej->tag = req->tag;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302400 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2401 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002402
2403 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002404 (void *)rej, sizeof(*rej));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002405
2406out:
2407 kfree(rep_param);
2408 kfree(rsp);
2409 kfree(rej);
2410
2411 return ret;
2412}
2413
2414static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2415{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002416 pr_info("Received IB REJ for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002417 srpt_drain_channel(cm_id);
2418}
2419
2420/**
2421 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2422 *
2423 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2424 * and that the recipient may begin transmitting (RTU = ready to use).
2425 */
2426static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2427{
2428 struct srpt_rdma_ch *ch;
2429 int ret;
2430
2431 ch = srpt_find_channel(cm_id->context, cm_id);
2432 BUG_ON(!ch);
2433
2434 if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
2435 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2436
2437 ret = srpt_ch_qp_rts(ch, ch->qp);
2438
2439 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2440 wait_list) {
2441 list_del(&ioctx->wait_list);
2442 srpt_handle_new_iu(ch, ioctx, NULL);
2443 }
2444 if (ret)
2445 srpt_close_ch(ch);
2446 }
2447}
2448
2449static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2450{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002451 pr_info("Received IB TimeWait exit for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002452 srpt_drain_channel(cm_id);
2453}
2454
2455static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2456{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002457 pr_info("Received IB REP error for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002458 srpt_drain_channel(cm_id);
2459}
2460
2461/**
2462 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2463 */
2464static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2465{
2466 struct srpt_rdma_ch *ch;
2467 unsigned long flags;
2468 bool send_drep = false;
2469
2470 ch = srpt_find_channel(cm_id->context, cm_id);
2471 BUG_ON(!ch);
2472
Bart Van Assche33912d72016-02-11 11:04:43 -08002473 pr_debug("cm_id= %p ch->state= %d\n", cm_id, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002474
2475 spin_lock_irqsave(&ch->spinlock, flags);
2476 switch (ch->state) {
2477 case CH_CONNECTING:
2478 case CH_LIVE:
2479 send_drep = true;
2480 ch->state = CH_DISCONNECTING;
2481 break;
2482 case CH_DISCONNECTING:
2483 case CH_DRAINING:
2484 case CH_RELEASING:
2485 WARN(true, "unexpected channel state %d\n", ch->state);
2486 break;
2487 }
2488 spin_unlock_irqrestore(&ch->spinlock, flags);
2489
2490 if (send_drep) {
2491 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002492 pr_err("Sending IB DREP failed.\n");
2493 pr_info("Received DREQ and sent DREP for session %s.\n",
2494 ch->sess_name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002495 }
2496}
2497
2498/**
2499 * srpt_cm_drep_recv() - Process reception of a DREP message.
2500 */
2501static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2502{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002503 pr_info("Received InfiniBand DREP message for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002504 srpt_drain_channel(cm_id);
2505}
2506
2507/**
2508 * srpt_cm_handler() - IB connection manager callback function.
2509 *
2510 * A non-zero return value will cause the caller destroy the CM ID.
2511 *
2512 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2513 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2514 * a non-zero value in any other case will trigger a race with the
2515 * ib_destroy_cm_id() call in srpt_release_channel().
2516 */
2517static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2518{
2519 int ret;
2520
2521 ret = 0;
2522 switch (event->event) {
2523 case IB_CM_REQ_RECEIVED:
2524 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2525 event->private_data);
2526 break;
2527 case IB_CM_REJ_RECEIVED:
2528 srpt_cm_rej_recv(cm_id);
2529 break;
2530 case IB_CM_RTU_RECEIVED:
2531 case IB_CM_USER_ESTABLISHED:
2532 srpt_cm_rtu_recv(cm_id);
2533 break;
2534 case IB_CM_DREQ_RECEIVED:
2535 srpt_cm_dreq_recv(cm_id);
2536 break;
2537 case IB_CM_DREP_RECEIVED:
2538 srpt_cm_drep_recv(cm_id);
2539 break;
2540 case IB_CM_TIMEWAIT_EXIT:
2541 srpt_cm_timewait_exit(cm_id);
2542 break;
2543 case IB_CM_REP_ERROR:
2544 srpt_cm_rep_error(cm_id);
2545 break;
2546 case IB_CM_DREQ_ERROR:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002547 pr_info("Received IB DREQ ERROR event.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002548 break;
2549 case IB_CM_MRA_RECEIVED:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002550 pr_info("Received IB MRA event\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002551 break;
2552 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002553 pr_err("received unrecognized IB CM event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002554 break;
2555 }
2556
2557 return ret;
2558}
2559
2560/**
2561 * srpt_perform_rdmas() - Perform IB RDMA.
2562 *
2563 * Returns zero upon success or a negative number upon failure.
2564 */
2565static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2566 struct srpt_send_ioctx *ioctx)
2567{
Bart Van Asschea42d9852011-10-14 01:30:46 +00002568 struct ib_send_wr *bad_wr;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002569 int sq_wr_avail, ret, i;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002570 enum dma_data_direction dir;
2571 const int n_rdma = ioctx->n_rdma;
2572
2573 dir = ioctx->cmd.data_direction;
2574 if (dir == DMA_TO_DEVICE) {
2575 /* write */
2576 ret = -ENOMEM;
2577 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2578 if (sq_wr_avail < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002579 pr_warn("IB send queue full (needed %d)\n",
2580 n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002581 goto out;
2582 }
2583 }
2584
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002585 for (i = 0; i < n_rdma; i++) {
2586 struct ib_send_wr *wr = &ioctx->rdma_wrs[i].wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002587
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002588 wr->opcode = (dir == DMA_FROM_DEVICE) ?
2589 IB_WR_RDMA_WRITE : IB_WR_RDMA_READ;
2590
2591 if (i == n_rdma - 1) {
2592 /* only get completion event for the last rdma read */
2593 if (dir == DMA_TO_DEVICE) {
2594 wr->send_flags = IB_SEND_SIGNALED;
2595 ioctx->rdma_cqe.done = srpt_rdma_read_done;
2596 } else {
2597 ioctx->rdma_cqe.done = srpt_rdma_write_done;
2598 }
2599 wr->wr_cqe = &ioctx->rdma_cqe;
2600 wr->next = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002601 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002602 wr->wr_cqe = NULL;
2603 wr->next = &ioctx->rdma_wrs[i + 1].wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002604 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00002605 }
2606
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002607 ret = ib_post_send(ch->qp, &ioctx->rdma_wrs->wr, &bad_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002608 if (ret)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002609 pr_err("%s[%d]: ib_post_send() returned %d for %d/%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002610 __func__, __LINE__, ret, i, n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002611out:
2612 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2613 atomic_add(n_rdma, &ch->sq_wr_avail);
2614 return ret;
2615}
2616
2617/**
2618 * srpt_xfer_data() - Start data transfer from initiator to target.
2619 */
2620static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2621 struct srpt_send_ioctx *ioctx)
2622{
2623 int ret;
2624
2625 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2626 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002627 pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002628 goto out;
2629 }
2630
2631 ret = srpt_perform_rdmas(ch, ioctx);
2632 if (ret) {
2633 if (ret == -EAGAIN || ret == -ENOMEM)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002634 pr_info("%s[%d] queue full -- ret=%d\n",
2635 __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002636 else
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002637 pr_err("%s[%d] fatal error -- ret=%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002638 __func__, __LINE__, ret);
2639 goto out_unmap;
2640 }
2641
2642out:
2643 return ret;
2644out_unmap:
2645 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2646 goto out;
2647}
2648
2649static int srpt_write_pending_status(struct se_cmd *se_cmd)
2650{
2651 struct srpt_send_ioctx *ioctx;
2652
2653 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2654 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2655}
2656
2657/*
2658 * srpt_write_pending() - Start data transfer from initiator to target (write).
2659 */
2660static int srpt_write_pending(struct se_cmd *se_cmd)
2661{
2662 struct srpt_rdma_ch *ch;
2663 struct srpt_send_ioctx *ioctx;
2664 enum srpt_command_state new_state;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002665 int ret;
2666
2667 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2668
2669 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2670 WARN_ON(new_state == SRPT_STATE_DONE);
2671
2672 ch = ioctx->ch;
2673 BUG_ON(!ch);
2674
Bart Van Assche33912d72016-02-11 11:04:43 -08002675 switch (ch->state) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00002676 case CH_CONNECTING:
Bart Van Assche33912d72016-02-11 11:04:43 -08002677 WARN(true, "unexpected channel state %d\n", ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002678 ret = -EINVAL;
2679 goto out;
2680 case CH_LIVE:
2681 break;
2682 case CH_DISCONNECTING:
2683 case CH_DRAINING:
2684 case CH_RELEASING:
2685 pr_debug("cmd with tag %lld: channel disconnecting\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002686 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002687 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2688 ret = -EINVAL;
2689 goto out;
2690 }
2691 ret = srpt_xfer_data(ch, ioctx);
2692
2693out:
2694 return ret;
2695}
2696
2697static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2698{
2699 switch (tcm_mgmt_status) {
2700 case TMR_FUNCTION_COMPLETE:
2701 return SRP_TSK_MGMT_SUCCESS;
2702 case TMR_FUNCTION_REJECTED:
2703 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2704 }
2705 return SRP_TSK_MGMT_FAILED;
2706}
2707
2708/**
2709 * srpt_queue_response() - Transmits the response to a SCSI command.
2710 *
2711 * Callback function called by the TCM core. Must not block since it can be
2712 * invoked on the context of the IB completion handler.
2713 */
Joern Engelb79fafa2013-07-03 11:22:17 -04002714static void srpt_queue_response(struct se_cmd *cmd)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002715{
2716 struct srpt_rdma_ch *ch;
2717 struct srpt_send_ioctx *ioctx;
2718 enum srpt_command_state state;
2719 unsigned long flags;
2720 int ret;
2721 enum dma_data_direction dir;
2722 int resp_len;
2723 u8 srp_tm_status;
2724
Bart Van Asschea42d9852011-10-14 01:30:46 +00002725 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2726 ch = ioctx->ch;
2727 BUG_ON(!ch);
2728
2729 spin_lock_irqsave(&ioctx->spinlock, flags);
2730 state = ioctx->state;
2731 switch (state) {
2732 case SRPT_STATE_NEW:
2733 case SRPT_STATE_DATA_IN:
2734 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2735 break;
2736 case SRPT_STATE_MGMT:
2737 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2738 break;
2739 default:
2740 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2741 ch, ioctx->ioctx.index, ioctx->state);
2742 break;
2743 }
2744 spin_unlock_irqrestore(&ioctx->spinlock, flags);
2745
2746 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
2747 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
2748 atomic_inc(&ch->req_lim_delta);
2749 srpt_abort_cmd(ioctx);
Joern Engelb79fafa2013-07-03 11:22:17 -04002750 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002751 }
2752
2753 dir = ioctx->cmd.data_direction;
2754
2755 /* For read commands, transfer the data to the initiator. */
2756 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
2757 !ioctx->queue_status_only) {
2758 ret = srpt_xfer_data(ch, ioctx);
2759 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002760 pr_err("xfer_data failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002761 ioctx->cmd.tag);
Joern Engelb79fafa2013-07-03 11:22:17 -04002762 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002763 }
2764 }
2765
2766 if (state != SRPT_STATE_MGMT)
Bart Van Assche649ee052015-04-14 13:26:44 +02002767 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002768 cmd->scsi_status);
2769 else {
2770 srp_tm_status
2771 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2772 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
Bart Van Assche649ee052015-04-14 13:26:44 +02002773 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002774 }
2775 ret = srpt_post_send(ch, ioctx, resp_len);
2776 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002777 pr_err("sending cmd response failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002778 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002779 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2780 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02002781 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002782 }
Joern Engelb79fafa2013-07-03 11:22:17 -04002783}
Bart Van Asschea42d9852011-10-14 01:30:46 +00002784
Joern Engelb79fafa2013-07-03 11:22:17 -04002785static int srpt_queue_data_in(struct se_cmd *cmd)
2786{
2787 srpt_queue_response(cmd);
2788 return 0;
2789}
2790
2791static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2792{
2793 srpt_queue_response(cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002794}
2795
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002796static void srpt_aborted_task(struct se_cmd *cmd)
2797{
2798 struct srpt_send_ioctx *ioctx = container_of(cmd,
2799 struct srpt_send_ioctx, cmd);
2800
2801 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
2802}
2803
Bart Van Asschea42d9852011-10-14 01:30:46 +00002804static int srpt_queue_status(struct se_cmd *cmd)
2805{
2806 struct srpt_send_ioctx *ioctx;
2807
2808 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2809 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2810 if (cmd->se_cmd_flags &
2811 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2812 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2813 ioctx->queue_status_only = true;
Joern Engelb79fafa2013-07-03 11:22:17 -04002814 srpt_queue_response(cmd);
2815 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002816}
2817
2818static void srpt_refresh_port_work(struct work_struct *work)
2819{
2820 struct srpt_port *sport = container_of(work, struct srpt_port, work);
2821
2822 srpt_refresh_port(sport);
2823}
2824
2825static int srpt_ch_list_empty(struct srpt_device *sdev)
2826{
2827 int res;
2828
2829 spin_lock_irq(&sdev->spinlock);
2830 res = list_empty(&sdev->rch_list);
2831 spin_unlock_irq(&sdev->spinlock);
2832
2833 return res;
2834}
2835
2836/**
2837 * srpt_release_sdev() - Free the channel resources associated with a target.
2838 */
2839static int srpt_release_sdev(struct srpt_device *sdev)
2840{
2841 struct srpt_rdma_ch *ch, *tmp_ch;
2842 int res;
2843
2844 WARN_ON_ONCE(irqs_disabled());
2845
2846 BUG_ON(!sdev);
2847
2848 spin_lock_irq(&sdev->spinlock);
2849 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
2850 __srpt_close_ch(ch);
2851 spin_unlock_irq(&sdev->spinlock);
2852
2853 res = wait_event_interruptible(sdev->ch_releaseQ,
2854 srpt_ch_list_empty(sdev));
2855 if (res)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002856 pr_err("%s: interrupted.\n", __func__);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002857
2858 return 0;
2859}
2860
2861static struct srpt_port *__srpt_lookup_port(const char *name)
2862{
2863 struct ib_device *dev;
2864 struct srpt_device *sdev;
2865 struct srpt_port *sport;
2866 int i;
2867
2868 list_for_each_entry(sdev, &srpt_dev_list, list) {
2869 dev = sdev->device;
2870 if (!dev)
2871 continue;
2872
2873 for (i = 0; i < dev->phys_port_cnt; i++) {
2874 sport = &sdev->port[i];
2875
2876 if (!strcmp(sport->port_guid, name))
2877 return sport;
2878 }
2879 }
2880
2881 return NULL;
2882}
2883
2884static struct srpt_port *srpt_lookup_port(const char *name)
2885{
2886 struct srpt_port *sport;
2887
2888 spin_lock(&srpt_dev_lock);
2889 sport = __srpt_lookup_port(name);
2890 spin_unlock(&srpt_dev_lock);
2891
2892 return sport;
2893}
2894
2895/**
2896 * srpt_add_one() - Infiniband device addition callback function.
2897 */
2898static void srpt_add_one(struct ib_device *device)
2899{
2900 struct srpt_device *sdev;
2901 struct srpt_port *sport;
2902 struct ib_srq_init_attr srq_attr;
2903 int i;
2904
2905 pr_debug("device = %p, device->dma_ops = %p\n", device,
2906 device->dma_ops);
2907
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002908 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002909 if (!sdev)
2910 goto err;
2911
2912 sdev->device = device;
2913 INIT_LIST_HEAD(&sdev->rch_list);
2914 init_waitqueue_head(&sdev->ch_releaseQ);
2915 spin_lock_init(&sdev->spinlock);
2916
Bart Van Asschea42d9852011-10-14 01:30:46 +00002917 sdev->pd = ib_alloc_pd(device);
2918 if (IS_ERR(sdev->pd))
2919 goto free_dev;
2920
Or Gerlitz4a061b22015-12-18 10:59:46 +02002921 sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002922
2923 srq_attr.event_handler = srpt_srq_event;
2924 srq_attr.srq_context = (void *)sdev;
2925 srq_attr.attr.max_wr = sdev->srq_size;
2926 srq_attr.attr.max_sge = 1;
2927 srq_attr.attr.srq_limit = 0;
Roland Dreier6f360332012-04-12 07:51:08 -07002928 srq_attr.srq_type = IB_SRQT_BASIC;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002929
2930 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
2931 if (IS_ERR(sdev->srq))
Jason Gunthorpe5a783952015-07-30 17:22:24 -06002932 goto err_pd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002933
2934 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
Or Gerlitz4a061b22015-12-18 10:59:46 +02002935 __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002936 device->name);
2937
2938 if (!srpt_service_guid)
2939 srpt_service_guid = be64_to_cpu(device->node_guid);
2940
2941 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
2942 if (IS_ERR(sdev->cm_id))
2943 goto err_srq;
2944
2945 /* print out target login information */
2946 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
2947 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
2948 srpt_service_guid, srpt_service_guid);
2949
2950 /*
2951 * We do not have a consistent service_id (ie. also id_ext of target_id)
2952 * to identify this target. We currently use the guid of the first HCA
2953 * in the system as service_id; therefore, the target_id will change
2954 * if this HCA is gone bad and replaced by different HCA
2955 */
Haggai Eran73fec7f2015-07-30 17:50:26 +03002956 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
Bart Van Asschea42d9852011-10-14 01:30:46 +00002957 goto err_cm;
2958
2959 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
2960 srpt_event_handler);
2961 if (ib_register_event_handler(&sdev->event_handler))
2962 goto err_cm;
2963
2964 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
2965 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
2966 sizeof(*sdev->ioctx_ring[0]),
2967 srp_max_req_size, DMA_FROM_DEVICE);
2968 if (!sdev->ioctx_ring)
2969 goto err_event;
2970
2971 for (i = 0; i < sdev->srq_size; ++i)
2972 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
2973
Roland Dreierf2250662012-02-02 12:55:58 -08002974 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002975
2976 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
2977 sport = &sdev->port[i - 1];
2978 sport->sdev = sdev;
2979 sport->port = i;
2980 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
2981 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
2982 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
2983 INIT_WORK(&sport->work, srpt_refresh_port_work);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002984
2985 if (srpt_refresh_port(sport)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002986 pr_err("MAD registration failed for %s-%d.\n",
Bart Van Asschef68cba4e92016-02-11 11:04:20 -08002987 sdev->device->name, i);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002988 goto err_ring;
2989 }
2990 snprintf(sport->port_guid, sizeof(sport->port_guid),
2991 "0x%016llx%016llx",
2992 be64_to_cpu(sport->gid.global.subnet_prefix),
2993 be64_to_cpu(sport->gid.global.interface_id));
2994 }
2995
2996 spin_lock(&srpt_dev_lock);
2997 list_add_tail(&sdev->list, &srpt_dev_list);
2998 spin_unlock(&srpt_dev_lock);
2999
3000out:
3001 ib_set_client_data(device, &srpt_client, sdev);
3002 pr_debug("added %s.\n", device->name);
3003 return;
3004
3005err_ring:
3006 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3007 sdev->srq_size, srp_max_req_size,
3008 DMA_FROM_DEVICE);
3009err_event:
3010 ib_unregister_event_handler(&sdev->event_handler);
3011err_cm:
3012 ib_destroy_cm_id(sdev->cm_id);
3013err_srq:
3014 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003015err_pd:
3016 ib_dealloc_pd(sdev->pd);
3017free_dev:
3018 kfree(sdev);
3019err:
3020 sdev = NULL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003021 pr_info("%s(%s) failed.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003022 goto out;
3023}
3024
3025/**
3026 * srpt_remove_one() - InfiniBand device removal callback function.
3027 */
Haggai Eran7c1eb452015-07-30 17:50:14 +03003028static void srpt_remove_one(struct ib_device *device, void *client_data)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003029{
Haggai Eran7c1eb452015-07-30 17:50:14 +03003030 struct srpt_device *sdev = client_data;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003031 int i;
3032
Bart Van Asschea42d9852011-10-14 01:30:46 +00003033 if (!sdev) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003034 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003035 return;
3036 }
3037
3038 srpt_unregister_mad_agent(sdev);
3039
3040 ib_unregister_event_handler(&sdev->event_handler);
3041
3042 /* Cancel any work queued by the just unregistered IB event handler. */
3043 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3044 cancel_work_sync(&sdev->port[i].work);
3045
3046 ib_destroy_cm_id(sdev->cm_id);
3047
3048 /*
3049 * Unregistering a target must happen after destroying sdev->cm_id
3050 * such that no new SRP_LOGIN_REQ information units can arrive while
3051 * destroying the target.
3052 */
3053 spin_lock(&srpt_dev_lock);
3054 list_del(&sdev->list);
3055 spin_unlock(&srpt_dev_lock);
3056 srpt_release_sdev(sdev);
3057
3058 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003059 ib_dealloc_pd(sdev->pd);
3060
3061 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3062 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
3063 sdev->ioctx_ring = NULL;
3064 kfree(sdev);
3065}
3066
3067static struct ib_client srpt_client = {
3068 .name = DRV_NAME,
3069 .add = srpt_add_one,
3070 .remove = srpt_remove_one
3071};
3072
3073static int srpt_check_true(struct se_portal_group *se_tpg)
3074{
3075 return 1;
3076}
3077
3078static int srpt_check_false(struct se_portal_group *se_tpg)
3079{
3080 return 0;
3081}
3082
3083static char *srpt_get_fabric_name(void)
3084{
3085 return "srpt";
3086}
3087
Bart Van Asschea42d9852011-10-14 01:30:46 +00003088static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3089{
3090 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3091
3092 return sport->port_guid;
3093}
3094
3095static u16 srpt_get_tag(struct se_portal_group *tpg)
3096{
3097 return 1;
3098}
3099
Bart Van Asschea42d9852011-10-14 01:30:46 +00003100static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3101{
3102 return 1;
3103}
3104
3105static void srpt_release_cmd(struct se_cmd *se_cmd)
3106{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08003107 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3108 struct srpt_send_ioctx, cmd);
3109 struct srpt_rdma_ch *ch = ioctx->ch;
3110 unsigned long flags;
3111
3112 WARN_ON(ioctx->state != SRPT_STATE_DONE);
3113 WARN_ON(ioctx->mapped_sg_count != 0);
3114
3115 if (ioctx->n_rbuf > 1) {
3116 kfree(ioctx->rbufs);
3117 ioctx->rbufs = NULL;
3118 ioctx->n_rbuf = 0;
3119 }
3120
3121 spin_lock_irqsave(&ch->spinlock, flags);
3122 list_add(&ioctx->free_list, &ch->free_list);
3123 spin_unlock_irqrestore(&ch->spinlock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003124}
3125
3126/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003127 * srpt_close_session() - Forcibly close a session.
3128 *
3129 * Callback function invoked by the TCM core to clean up sessions associated
3130 * with a node ACL when the user invokes
3131 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3132 */
3133static void srpt_close_session(struct se_session *se_sess)
3134{
3135 DECLARE_COMPLETION_ONSTACK(release_done);
3136 struct srpt_rdma_ch *ch;
3137 struct srpt_device *sdev;
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003138 unsigned long res;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003139
3140 ch = se_sess->fabric_sess_ptr;
3141 WARN_ON(ch->sess != se_sess);
3142
Bart Van Assche33912d72016-02-11 11:04:43 -08003143 pr_debug("ch %p state %d\n", ch, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003144
3145 sdev = ch->sport->sdev;
3146 spin_lock_irq(&sdev->spinlock);
3147 BUG_ON(ch->release_done);
3148 ch->release_done = &release_done;
3149 __srpt_close_ch(ch);
3150 spin_unlock_irq(&sdev->spinlock);
3151
3152 res = wait_for_completion_timeout(&release_done, 60 * HZ);
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003153 WARN_ON(res == 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003154}
3155
3156/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003157 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3158 *
3159 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3160 * This object represents an arbitrary integer used to uniquely identify a
3161 * particular attached remote initiator port to a particular SCSI target port
3162 * within a particular SCSI target device within a particular SCSI instance.
3163 */
3164static u32 srpt_sess_get_index(struct se_session *se_sess)
3165{
3166 return 0;
3167}
3168
3169static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3170{
3171}
3172
Bart Van Asschea42d9852011-10-14 01:30:46 +00003173/* Note: only used from inside debug printk's by the TCM core. */
3174static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3175{
3176 struct srpt_send_ioctx *ioctx;
3177
3178 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3179 return srpt_get_cmd_state(ioctx);
3180}
3181
Bart Van Asschea42d9852011-10-14 01:30:46 +00003182/**
3183 * srpt_parse_i_port_id() - Parse an initiator port ID.
3184 * @name: ASCII representation of a 128-bit initiator port ID.
3185 * @i_port_id: Binary 128-bit port ID.
3186 */
3187static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3188{
3189 const char *p;
3190 unsigned len, count, leading_zero_bytes;
3191 int ret, rc;
3192
3193 p = name;
Rasmus Villemoesb60459f2014-10-13 15:54:46 -07003194 if (strncasecmp(p, "0x", 2) == 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003195 p += 2;
3196 ret = -EINVAL;
3197 len = strlen(p);
3198 if (len % 2)
3199 goto out;
3200 count = min(len / 2, 16U);
3201 leading_zero_bytes = 16 - count;
3202 memset(i_port_id, 0, leading_zero_bytes);
3203 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3204 if (rc < 0)
3205 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3206 ret = 0;
3207out:
3208 return ret;
3209}
3210
3211/*
3212 * configfs callback function invoked for
3213 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3214 */
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003215static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003216{
Bart Van Asschea42d9852011-10-14 01:30:46 +00003217 u8 i_port_id[16];
3218
3219 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003220 pr_err("invalid initiator port ID %s\n", name);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003221 return -EINVAL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003222 }
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003223 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003224}
3225
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003226static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
3227 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003228{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003229 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003230 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3231
3232 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3233}
3234
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003235static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
3236 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003237{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003238 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003239 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3240 unsigned long val;
3241 int ret;
3242
Jingoo Han9d8abf42014-02-05 11:22:05 +09003243 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003244 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003245 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003246 return -EINVAL;
3247 }
3248 if (val > MAX_SRPT_RDMA_SIZE) {
3249 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3250 MAX_SRPT_RDMA_SIZE);
3251 return -EINVAL;
3252 }
3253 if (val < DEFAULT_MAX_RDMA_SIZE) {
3254 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3255 val, DEFAULT_MAX_RDMA_SIZE);
3256 return -EINVAL;
3257 }
3258 sport->port_attrib.srp_max_rdma_size = val;
3259
3260 return count;
3261}
3262
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003263static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
3264 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003265{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003266 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003267 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3268
3269 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3270}
3271
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003272static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
3273 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003274{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003275 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003276 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3277 unsigned long val;
3278 int ret;
3279
Jingoo Han9d8abf42014-02-05 11:22:05 +09003280 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003281 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003282 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003283 return -EINVAL;
3284 }
3285 if (val > MAX_SRPT_RSP_SIZE) {
3286 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3287 MAX_SRPT_RSP_SIZE);
3288 return -EINVAL;
3289 }
3290 if (val < MIN_MAX_RSP_SIZE) {
3291 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3292 MIN_MAX_RSP_SIZE);
3293 return -EINVAL;
3294 }
3295 sport->port_attrib.srp_max_rsp_size = val;
3296
3297 return count;
3298}
3299
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003300static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
3301 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003302{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003303 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003304 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3305
3306 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3307}
3308
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003309static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
3310 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003311{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003312 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003313 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3314 unsigned long val;
3315 int ret;
3316
Jingoo Han9d8abf42014-02-05 11:22:05 +09003317 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003318 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003319 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003320 return -EINVAL;
3321 }
3322 if (val > MAX_SRPT_SRQ_SIZE) {
3323 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3324 MAX_SRPT_SRQ_SIZE);
3325 return -EINVAL;
3326 }
3327 if (val < MIN_SRPT_SRQ_SIZE) {
3328 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3329 MIN_SRPT_SRQ_SIZE);
3330 return -EINVAL;
3331 }
3332 sport->port_attrib.srp_sq_size = val;
3333
3334 return count;
3335}
3336
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003337CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
3338CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
3339CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003340
3341static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003342 &srpt_tpg_attrib_attr_srp_max_rdma_size,
3343 &srpt_tpg_attrib_attr_srp_max_rsp_size,
3344 &srpt_tpg_attrib_attr_srp_sq_size,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003345 NULL,
3346};
3347
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003348static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003349{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003350 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003351 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3352
3353 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3354}
3355
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003356static ssize_t srpt_tpg_enable_store(struct config_item *item,
3357 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003358{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003359 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003360 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3361 unsigned long tmp;
3362 int ret;
3363
Jingoo Han9d8abf42014-02-05 11:22:05 +09003364 ret = kstrtoul(page, 0, &tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003365 if (ret < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003366 pr_err("Unable to extract srpt_tpg_store_enable\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003367 return -EINVAL;
3368 }
3369
3370 if ((tmp != 0) && (tmp != 1)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003371 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003372 return -EINVAL;
3373 }
3374 if (tmp == 1)
3375 sport->enabled = true;
3376 else
3377 sport->enabled = false;
3378
3379 return count;
3380}
3381
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003382CONFIGFS_ATTR(srpt_tpg_, enable);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003383
3384static struct configfs_attribute *srpt_tpg_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003385 &srpt_tpg_attr_enable,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003386 NULL,
3387};
3388
3389/**
3390 * configfs callback invoked for
3391 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3392 */
3393static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3394 struct config_group *group,
3395 const char *name)
3396{
3397 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3398 int res;
3399
3400 /* Initialize sport->port_wwn and sport->port_tpg_1 */
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07003401 res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003402 if (res)
3403 return ERR_PTR(res);
3404
3405 return &sport->port_tpg_1;
3406}
3407
3408/**
3409 * configfs callback invoked for
3410 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3411 */
3412static void srpt_drop_tpg(struct se_portal_group *tpg)
3413{
3414 struct srpt_port *sport = container_of(tpg,
3415 struct srpt_port, port_tpg_1);
3416
3417 sport->enabled = false;
3418 core_tpg_deregister(&sport->port_tpg_1);
3419}
3420
3421/**
3422 * configfs callback invoked for
3423 * mkdir /sys/kernel/config/target/$driver/$port
3424 */
3425static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3426 struct config_group *group,
3427 const char *name)
3428{
3429 struct srpt_port *sport;
3430 int ret;
3431
3432 sport = srpt_lookup_port(name);
3433 pr_debug("make_tport(%s)\n", name);
3434 ret = -EINVAL;
3435 if (!sport)
3436 goto err;
3437
3438 return &sport->port_wwn;
3439
3440err:
3441 return ERR_PTR(ret);
3442}
3443
3444/**
3445 * configfs callback invoked for
3446 * rmdir /sys/kernel/config/target/$driver/$port
3447 */
3448static void srpt_drop_tport(struct se_wwn *wwn)
3449{
3450 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3451
3452 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3453}
3454
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003455static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003456{
3457 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3458}
3459
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003460CONFIGFS_ATTR_RO(srpt_wwn_, version);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003461
3462static struct configfs_attribute *srpt_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003463 &srpt_wwn_attr_version,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003464 NULL,
3465};
3466
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003467static const struct target_core_fabric_ops srpt_template = {
3468 .module = THIS_MODULE,
3469 .name = "srpt",
Bart Van Asschea42d9852011-10-14 01:30:46 +00003470 .get_fabric_name = srpt_get_fabric_name,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003471 .tpg_get_wwn = srpt_get_fabric_wwn,
3472 .tpg_get_tag = srpt_get_tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003473 .tpg_check_demo_mode = srpt_check_false,
3474 .tpg_check_demo_mode_cache = srpt_check_true,
3475 .tpg_check_demo_mode_write_protect = srpt_check_true,
3476 .tpg_check_prod_mode_write_protect = srpt_check_false,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003477 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3478 .release_cmd = srpt_release_cmd,
3479 .check_stop_free = srpt_check_stop_free,
3480 .shutdown_session = srpt_shutdown_session,
3481 .close_session = srpt_close_session,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003482 .sess_get_index = srpt_sess_get_index,
3483 .sess_get_initiator_sid = NULL,
3484 .write_pending = srpt_write_pending,
3485 .write_pending_status = srpt_write_pending_status,
3486 .set_default_node_attributes = srpt_set_default_node_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003487 .get_cmd_state = srpt_get_tcm_cmd_state,
Joern Engelb79fafa2013-07-03 11:22:17 -04003488 .queue_data_in = srpt_queue_data_in,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003489 .queue_status = srpt_queue_status,
Joern Engelb79fafa2013-07-03 11:22:17 -04003490 .queue_tm_rsp = srpt_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003491 .aborted_task = srpt_aborted_task,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003492 /*
3493 * Setup function pointers for generic logic in
3494 * target_core_fabric_configfs.c
3495 */
3496 .fabric_make_wwn = srpt_make_tport,
3497 .fabric_drop_wwn = srpt_drop_tport,
3498 .fabric_make_tpg = srpt_make_tpg,
3499 .fabric_drop_tpg = srpt_drop_tpg,
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003500 .fabric_init_nodeacl = srpt_init_nodeacl,
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003501
3502 .tfc_wwn_attrs = srpt_wwn_attrs,
3503 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3504 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003505};
3506
3507/**
3508 * srpt_init_module() - Kernel module initialization.
3509 *
3510 * Note: Since ib_register_client() registers callback functions, and since at
3511 * least one of these callback functions (srpt_add_one()) calls target core
3512 * functions, this driver must be registered with the target core before
3513 * ib_register_client() is called.
3514 */
3515static int __init srpt_init_module(void)
3516{
3517 int ret;
3518
3519 ret = -EINVAL;
3520 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003521 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003522 " srp_max_req_size -- must be at least %d.\n",
3523 srp_max_req_size, MIN_MAX_REQ_SIZE);
3524 goto out;
3525 }
3526
3527 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3528 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003529 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003530 " srpt_srq_size -- must be in the range [%d..%d].\n",
3531 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3532 goto out;
3533 }
3534
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003535 ret = target_register_template(&srpt_template);
3536 if (ret)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003537 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003538
3539 ret = ib_register_client(&srpt_client);
3540 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003541 pr_err("couldn't register IB client\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003542 goto out_unregister_target;
3543 }
3544
3545 return 0;
3546
3547out_unregister_target:
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003548 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003549out:
3550 return ret;
3551}
3552
3553static void __exit srpt_cleanup_module(void)
3554{
3555 ib_unregister_client(&srpt_client);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003556 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003557}
3558
3559module_init(srpt_init_module);
3560module_exit(srpt_cleanup_module);