blob: babc15b1795ddf9f1189dd69938593950bcf5db8 [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
99/**
100 * opposite_dma_dir() - Swap DMA_TO_DEVICE and DMA_FROM_DEVICE.
101 */
102static inline
103enum dma_data_direction opposite_dma_dir(enum dma_data_direction dir)
104{
105 switch (dir) {
106 case DMA_TO_DEVICE: return DMA_FROM_DEVICE;
107 case DMA_FROM_DEVICE: return DMA_TO_DEVICE;
108 default: return dir;
109 }
110}
111
Bart Van Asschea42d9852011-10-14 01:30:46 +0000112static enum rdma_ch_state srpt_get_ch_state(struct srpt_rdma_ch *ch)
113{
114 unsigned long flags;
115 enum rdma_ch_state state;
116
117 spin_lock_irqsave(&ch->spinlock, flags);
118 state = ch->state;
119 spin_unlock_irqrestore(&ch->spinlock, flags);
120 return state;
121}
122
123static enum rdma_ch_state
124srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new_state)
125{
126 unsigned long flags;
127 enum rdma_ch_state prev;
128
129 spin_lock_irqsave(&ch->spinlock, flags);
130 prev = ch->state;
131 ch->state = new_state;
132 spin_unlock_irqrestore(&ch->spinlock, flags);
133 return prev;
134}
135
136/**
137 * srpt_test_and_set_ch_state() - Test and set the channel state.
138 *
139 * Returns true if and only if the channel state has been set to the new state.
140 */
141static bool
142srpt_test_and_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state old,
143 enum rdma_ch_state new)
144{
145 unsigned long flags;
146 enum rdma_ch_state prev;
147
148 spin_lock_irqsave(&ch->spinlock, flags);
149 prev = ch->state;
150 if (prev == old)
151 ch->state = new;
152 spin_unlock_irqrestore(&ch->spinlock, flags);
153 return prev == old;
154}
155
156/**
157 * srpt_event_handler() - Asynchronous IB event callback function.
158 *
159 * Callback function called by the InfiniBand core when an asynchronous IB
160 * event occurs. This callback may occur in interrupt context. See also
161 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
162 * Architecture Specification.
163 */
164static void srpt_event_handler(struct ib_event_handler *handler,
165 struct ib_event *event)
166{
167 struct srpt_device *sdev;
168 struct srpt_port *sport;
169
170 sdev = ib_get_client_data(event->device, &srpt_client);
171 if (!sdev || sdev->device != event->device)
172 return;
173
174 pr_debug("ASYNC event= %d on device= %s\n", event->event,
Bart Van Asschef68cba4e92016-02-11 11:04:20 -0800175 sdev->device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000176
177 switch (event->event) {
178 case IB_EVENT_PORT_ERR:
179 if (event->element.port_num <= sdev->device->phys_port_cnt) {
180 sport = &sdev->port[event->element.port_num - 1];
181 sport->lid = 0;
182 sport->sm_lid = 0;
183 }
184 break;
185 case IB_EVENT_PORT_ACTIVE:
186 case IB_EVENT_LID_CHANGE:
187 case IB_EVENT_PKEY_CHANGE:
188 case IB_EVENT_SM_CHANGE:
189 case IB_EVENT_CLIENT_REREGISTER:
Doug Ledford2aa1cf62014-08-12 19:20:10 -0400190 case IB_EVENT_GID_CHANGE:
Bart Van Asschea42d9852011-10-14 01:30:46 +0000191 /* Refresh port data asynchronously. */
192 if (event->element.port_num <= sdev->device->phys_port_cnt) {
193 sport = &sdev->port[event->element.port_num - 1];
194 if (!sport->lid && !sport->sm_lid)
195 schedule_work(&sport->work);
196 }
197 break;
198 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400199 pr_err("received unrecognized IB event %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +0000200 event->event);
201 break;
202 }
203}
204
205/**
206 * srpt_srq_event() - SRQ event callback function.
207 */
208static void srpt_srq_event(struct ib_event *event, void *ctx)
209{
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400210 pr_info("SRQ event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000211}
212
213/**
214 * srpt_qp_event() - QP event callback function.
215 */
216static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
217{
218 pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
219 event->event, ch->cm_id, ch->sess_name, srpt_get_ch_state(ch));
220
221 switch (event->event) {
222 case IB_EVENT_COMM_EST:
223 ib_cm_notify(ch->cm_id, event->event);
224 break;
225 case IB_EVENT_QP_LAST_WQE_REACHED:
226 if (srpt_test_and_set_ch_state(ch, CH_DRAINING,
227 CH_RELEASING))
228 srpt_release_channel(ch);
229 else
230 pr_debug("%s: state %d - ignored LAST_WQE.\n",
231 ch->sess_name, srpt_get_ch_state(ch));
232 break;
233 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400234 pr_err("received unrecognized IB QP event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000235 break;
236 }
237}
238
239/**
240 * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
241 *
242 * @slot: one-based slot number.
243 * @value: four-bit value.
244 *
245 * Copies the lowest four bits of value in element slot of the array of four
246 * bit elements called c_list (controller list). The index slot is one-based.
247 */
248static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
249{
250 u16 id;
251 u8 tmp;
252
253 id = (slot - 1) / 2;
254 if (slot & 0x1) {
255 tmp = c_list[id] & 0xf;
256 c_list[id] = (value << 4) | tmp;
257 } else {
258 tmp = c_list[id] & 0xf0;
259 c_list[id] = (value & 0xf) | tmp;
260 }
261}
262
263/**
264 * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
265 *
266 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
267 * Specification.
268 */
269static void srpt_get_class_port_info(struct ib_dm_mad *mad)
270{
271 struct ib_class_port_info *cif;
272
273 cif = (struct ib_class_port_info *)mad->data;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800274 memset(cif, 0, sizeof(*cif));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000275 cif->base_version = 1;
276 cif->class_version = 1;
277 cif->resp_time_value = 20;
278
279 mad->mad_hdr.status = 0;
280}
281
282/**
283 * srpt_get_iou() - Write IOUnitInfo to a management datagram.
284 *
285 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
286 * Specification. See also section B.7, table B.6 in the SRP r16a document.
287 */
288static void srpt_get_iou(struct ib_dm_mad *mad)
289{
290 struct ib_dm_iou_info *ioui;
291 u8 slot;
292 int i;
293
294 ioui = (struct ib_dm_iou_info *)mad->data;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530295 ioui->change_id = cpu_to_be16(1);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000296 ioui->max_controllers = 16;
297
298 /* set present for slot 1 and empty for the rest */
299 srpt_set_ioc(ioui->controller_list, 1, 1);
300 for (i = 1, slot = 2; i < 16; i++, slot++)
301 srpt_set_ioc(ioui->controller_list, slot, 0);
302
303 mad->mad_hdr.status = 0;
304}
305
306/**
307 * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
308 *
309 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
310 * Architecture Specification. See also section B.7, table B.7 in the SRP
311 * r16a document.
312 */
313static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
314 struct ib_dm_mad *mad)
315{
316 struct srpt_device *sdev = sport->sdev;
317 struct ib_dm_ioc_profile *iocp;
318
319 iocp = (struct ib_dm_ioc_profile *)mad->data;
320
321 if (!slot || slot > 16) {
322 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530323 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000324 return;
325 }
326
327 if (slot > 2) {
328 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530329 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000330 return;
331 }
332
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800333 memset(iocp, 0, sizeof(*iocp));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000334 strcpy(iocp->id_string, SRPT_ID_STRING);
335 iocp->guid = cpu_to_be64(srpt_service_guid);
Or Gerlitz4a061b22015-12-18 10:59:46 +0200336 iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
337 iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
338 iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
339 iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000340 iocp->subsys_device_id = 0x0;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530341 iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
342 iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
343 iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
344 iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000345 iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
346 iocp->rdma_read_depth = 4;
347 iocp->send_size = cpu_to_be32(srp_max_req_size);
348 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
349 1U << 24));
350 iocp->num_svc_entries = 1;
351 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
352 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
353
354 mad->mad_hdr.status = 0;
355}
356
357/**
358 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
359 *
360 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
361 * Specification. See also section B.7, table B.8 in the SRP r16a document.
362 */
363static void srpt_get_svc_entries(u64 ioc_guid,
364 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
365{
366 struct ib_dm_svc_entries *svc_entries;
367
368 WARN_ON(!ioc_guid);
369
370 if (!slot || slot > 16) {
371 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530372 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000373 return;
374 }
375
376 if (slot > 2 || lo > hi || hi > 1) {
377 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530378 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000379 return;
380 }
381
382 svc_entries = (struct ib_dm_svc_entries *)mad->data;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800383 memset(svc_entries, 0, sizeof(*svc_entries));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000384 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
385 snprintf(svc_entries->service_entries[0].name,
386 sizeof(svc_entries->service_entries[0].name),
387 "%s%016llx",
388 SRP_SERVICE_NAME_PREFIX,
389 ioc_guid);
390
391 mad->mad_hdr.status = 0;
392}
393
394/**
395 * srpt_mgmt_method_get() - Process a received management datagram.
396 * @sp: source port through which the MAD has been received.
397 * @rq_mad: received MAD.
398 * @rsp_mad: response MAD.
399 */
400static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
401 struct ib_dm_mad *rsp_mad)
402{
403 u16 attr_id;
404 u32 slot;
405 u8 hi, lo;
406
407 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
408 switch (attr_id) {
409 case DM_ATTR_CLASS_PORT_INFO:
410 srpt_get_class_port_info(rsp_mad);
411 break;
412 case DM_ATTR_IOU_INFO:
413 srpt_get_iou(rsp_mad);
414 break;
415 case DM_ATTR_IOC_PROFILE:
416 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
417 srpt_get_ioc(sp, slot, rsp_mad);
418 break;
419 case DM_ATTR_SVC_ENTRIES:
420 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
421 hi = (u8) ((slot >> 8) & 0xff);
422 lo = (u8) (slot & 0xff);
423 slot = (u16) ((slot >> 16) & 0xffff);
424 srpt_get_svc_entries(srpt_service_guid,
425 slot, hi, lo, rsp_mad);
426 break;
427 default:
428 rsp_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530429 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000430 break;
431 }
432}
433
434/**
435 * srpt_mad_send_handler() - Post MAD-send callback function.
436 */
437static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
438 struct ib_mad_send_wc *mad_wc)
439{
440 ib_destroy_ah(mad_wc->send_buf->ah);
441 ib_free_send_mad(mad_wc->send_buf);
442}
443
444/**
445 * srpt_mad_recv_handler() - MAD reception callback function.
446 */
447static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
Christoph Hellwigca281262016-01-04 14:15:58 +0100448 struct ib_mad_send_buf *send_buf,
Bart Van Asschea42d9852011-10-14 01:30:46 +0000449 struct ib_mad_recv_wc *mad_wc)
450{
451 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
452 struct ib_ah *ah;
453 struct ib_mad_send_buf *rsp;
454 struct ib_dm_mad *dm_mad;
455
456 if (!mad_wc || !mad_wc->recv_buf.mad)
457 return;
458
459 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
460 mad_wc->recv_buf.grh, mad_agent->port_num);
461 if (IS_ERR(ah))
462 goto err;
463
464 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
465
466 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
467 mad_wc->wc->pkey_index, 0,
468 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
Ira Weinyda2dfaa2015-06-06 14:38:28 -0400469 GFP_KERNEL,
470 IB_MGMT_BASE_VERSION);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000471 if (IS_ERR(rsp))
472 goto err_rsp;
473
474 rsp->ah = ah;
475
476 dm_mad = rsp->mad;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800477 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000478 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
479 dm_mad->mad_hdr.status = 0;
480
481 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
482 case IB_MGMT_METHOD_GET:
483 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
484 break;
485 case IB_MGMT_METHOD_SET:
486 dm_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530487 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000488 break;
489 default:
490 dm_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530491 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000492 break;
493 }
494
495 if (!ib_post_send_mad(rsp, NULL)) {
496 ib_free_recv_mad(mad_wc);
497 /* will destroy_ah & free_send_mad in send completion */
498 return;
499 }
500
501 ib_free_send_mad(rsp);
502
503err_rsp:
504 ib_destroy_ah(ah);
505err:
506 ib_free_recv_mad(mad_wc);
507}
508
509/**
510 * srpt_refresh_port() - Configure a HCA port.
511 *
512 * Enable InfiniBand management datagram processing, update the cached sm_lid,
513 * lid and gid values, and register a callback function for processing MADs
514 * on the specified port.
515 *
516 * Note: It is safe to call this function more than once for the same port.
517 */
518static int srpt_refresh_port(struct srpt_port *sport)
519{
520 struct ib_mad_reg_req reg_req;
521 struct ib_port_modify port_modify;
522 struct ib_port_attr port_attr;
523 int ret;
524
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800525 memset(&port_modify, 0, sizeof(port_modify));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000526 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
527 port_modify.clr_port_cap_mask = 0;
528
529 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
530 if (ret)
531 goto err_mod_port;
532
533 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
534 if (ret)
535 goto err_query_port;
536
537 sport->sm_lid = port_attr.sm_lid;
538 sport->lid = port_attr.lid;
539
Matan Barak55ee3ab2015-10-15 18:38:45 +0300540 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid,
541 NULL);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000542 if (ret)
543 goto err_query_port;
544
545 if (!sport->mad_agent) {
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800546 memset(&reg_req, 0, sizeof(reg_req));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000547 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
548 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
549 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
550 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
551
552 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
553 sport->port,
554 IB_QPT_GSI,
555 &reg_req, 0,
556 srpt_mad_send_handler,
557 srpt_mad_recv_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -0400558 sport, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000559 if (IS_ERR(sport->mad_agent)) {
560 ret = PTR_ERR(sport->mad_agent);
561 sport->mad_agent = NULL;
562 goto err_query_port;
563 }
564 }
565
566 return 0;
567
568err_query_port:
569
570 port_modify.set_port_cap_mask = 0;
571 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
572 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
573
574err_mod_port:
575
576 return ret;
577}
578
579/**
580 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
581 *
582 * Note: It is safe to call this function more than once for the same device.
583 */
584static void srpt_unregister_mad_agent(struct srpt_device *sdev)
585{
586 struct ib_port_modify port_modify = {
587 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
588 };
589 struct srpt_port *sport;
590 int i;
591
592 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
593 sport = &sdev->port[i - 1];
594 WARN_ON(sport->port != i);
595 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400596 pr_err("disabling MAD processing failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000597 if (sport->mad_agent) {
598 ib_unregister_mad_agent(sport->mad_agent);
599 sport->mad_agent = NULL;
600 }
601 }
602}
603
604/**
605 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
606 */
607static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
608 int ioctx_size, int dma_size,
609 enum dma_data_direction dir)
610{
611 struct srpt_ioctx *ioctx;
612
613 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
614 if (!ioctx)
615 goto err;
616
617 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
618 if (!ioctx->buf)
619 goto err_free_ioctx;
620
621 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
622 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
623 goto err_free_buf;
624
625 return ioctx;
626
627err_free_buf:
628 kfree(ioctx->buf);
629err_free_ioctx:
630 kfree(ioctx);
631err:
632 return NULL;
633}
634
635/**
636 * srpt_free_ioctx() - Free an SRPT I/O context structure.
637 */
638static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
639 int dma_size, enum dma_data_direction dir)
640{
641 if (!ioctx)
642 return;
643
644 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
645 kfree(ioctx->buf);
646 kfree(ioctx);
647}
648
649/**
650 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
651 * @sdev: Device to allocate the I/O context ring for.
652 * @ring_size: Number of elements in the I/O context ring.
653 * @ioctx_size: I/O context size.
654 * @dma_size: DMA buffer size.
655 * @dir: DMA data direction.
656 */
657static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
658 int ring_size, int ioctx_size,
659 int dma_size, enum dma_data_direction dir)
660{
661 struct srpt_ioctx **ring;
662 int i;
663
664 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
665 && ioctx_size != sizeof(struct srpt_send_ioctx));
666
667 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
668 if (!ring)
669 goto out;
670 for (i = 0; i < ring_size; ++i) {
671 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
672 if (!ring[i])
673 goto err;
674 ring[i]->index = i;
675 }
676 goto out;
677
678err:
679 while (--i >= 0)
680 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
681 kfree(ring);
Jesper Juhl715252d2012-02-04 23:49:40 +0100682 ring = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000683out:
684 return ring;
685}
686
687/**
688 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
689 */
690static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
691 struct srpt_device *sdev, int ring_size,
692 int dma_size, enum dma_data_direction dir)
693{
694 int i;
695
696 for (i = 0; i < ring_size; ++i)
697 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
698 kfree(ioctx_ring);
699}
700
701/**
702 * srpt_get_cmd_state() - Get the state of a SCSI command.
703 */
704static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
705{
706 enum srpt_command_state state;
707 unsigned long flags;
708
709 BUG_ON(!ioctx);
710
711 spin_lock_irqsave(&ioctx->spinlock, flags);
712 state = ioctx->state;
713 spin_unlock_irqrestore(&ioctx->spinlock, flags);
714 return state;
715}
716
717/**
718 * srpt_set_cmd_state() - Set the state of a SCSI command.
719 *
720 * Does not modify the state of aborted commands. Returns the previous command
721 * state.
722 */
723static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
724 enum srpt_command_state new)
725{
726 enum srpt_command_state previous;
727 unsigned long flags;
728
729 BUG_ON(!ioctx);
730
731 spin_lock_irqsave(&ioctx->spinlock, flags);
732 previous = ioctx->state;
733 if (previous != SRPT_STATE_DONE)
734 ioctx->state = new;
735 spin_unlock_irqrestore(&ioctx->spinlock, flags);
736
737 return previous;
738}
739
740/**
741 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
742 *
743 * Returns true if and only if the previous command state was equal to 'old'.
744 */
745static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
746 enum srpt_command_state old,
747 enum srpt_command_state new)
748{
749 enum srpt_command_state previous;
750 unsigned long flags;
751
752 WARN_ON(!ioctx);
753 WARN_ON(old == SRPT_STATE_DONE);
754 WARN_ON(new == SRPT_STATE_NEW);
755
756 spin_lock_irqsave(&ioctx->spinlock, flags);
757 previous = ioctx->state;
758 if (previous == old)
759 ioctx->state = new;
760 spin_unlock_irqrestore(&ioctx->spinlock, flags);
761 return previous == old;
762}
763
764/**
765 * srpt_post_recv() - Post an IB receive request.
766 */
767static int srpt_post_recv(struct srpt_device *sdev,
768 struct srpt_recv_ioctx *ioctx)
769{
770 struct ib_sge list;
771 struct ib_recv_wr wr, *bad_wr;
772
773 BUG_ON(!sdev);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000774 list.addr = ioctx->ioctx.dma;
775 list.length = srp_max_req_size;
Jason Gunthorpe5a783952015-07-30 17:22:24 -0600776 list.lkey = sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000777
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200778 ioctx->ioctx.cqe.done = srpt_recv_done;
779 wr.wr_cqe = &ioctx->ioctx.cqe;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000780 wr.next = NULL;
781 wr.sg_list = &list;
782 wr.num_sge = 1;
783
784 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
785}
786
787/**
788 * srpt_post_send() - Post an IB send request.
789 *
790 * Returns zero upon success and a non-zero value upon failure.
791 */
792static int srpt_post_send(struct srpt_rdma_ch *ch,
793 struct srpt_send_ioctx *ioctx, int len)
794{
795 struct ib_sge list;
796 struct ib_send_wr wr, *bad_wr;
797 struct srpt_device *sdev = ch->sport->sdev;
798 int ret;
799
800 atomic_inc(&ch->req_lim);
801
802 ret = -ENOMEM;
803 if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400804 pr_warn("IB send queue full (needed 1)\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000805 goto out;
806 }
807
808 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
809 DMA_TO_DEVICE);
810
811 list.addr = ioctx->ioctx.dma;
812 list.length = len;
Jason Gunthorpe5a783952015-07-30 17:22:24 -0600813 list.lkey = sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000814
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200815 ioctx->ioctx.cqe.done = srpt_send_done;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000816 wr.next = NULL;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200817 wr.wr_cqe = &ioctx->ioctx.cqe;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000818 wr.sg_list = &list;
819 wr.num_sge = 1;
820 wr.opcode = IB_WR_SEND;
821 wr.send_flags = IB_SEND_SIGNALED;
822
823 ret = ib_post_send(ch->qp, &wr, &bad_wr);
824
825out:
826 if (ret < 0) {
827 atomic_inc(&ch->sq_wr_avail);
828 atomic_dec(&ch->req_lim);
829 }
830 return ret;
831}
832
833/**
834 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
835 * @ioctx: Pointer to the I/O context associated with the request.
836 * @srp_cmd: Pointer to the SRP_CMD request data.
837 * @dir: Pointer to the variable to which the transfer direction will be
838 * written.
839 * @data_len: Pointer to the variable to which the total data length of all
840 * descriptors in the SRP_CMD request will be written.
841 *
842 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
843 *
844 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
845 * -ENOMEM when memory allocation fails and zero upon success.
846 */
847static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
848 struct srp_cmd *srp_cmd,
849 enum dma_data_direction *dir, u64 *data_len)
850{
851 struct srp_indirect_buf *idb;
852 struct srp_direct_buf *db;
853 unsigned add_cdb_offset;
854 int ret;
855
856 /*
857 * The pointer computations below will only be compiled correctly
858 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
859 * whether srp_cmd::add_data has been declared as a byte pointer.
860 */
861 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
862 && !__same_type(srp_cmd->add_data[0], (u8)0));
863
864 BUG_ON(!dir);
865 BUG_ON(!data_len);
866
867 ret = 0;
868 *data_len = 0;
869
870 /*
871 * The lower four bits of the buffer format field contain the DATA-IN
872 * buffer descriptor format, and the highest four bits contain the
873 * DATA-OUT buffer descriptor format.
874 */
875 *dir = DMA_NONE;
876 if (srp_cmd->buf_fmt & 0xf)
877 /* DATA-IN: transfer data from target to initiator (read). */
878 *dir = DMA_FROM_DEVICE;
879 else if (srp_cmd->buf_fmt >> 4)
880 /* DATA-OUT: transfer data from initiator to target (write). */
881 *dir = DMA_TO_DEVICE;
882
883 /*
884 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
885 * CDB LENGTH' field are reserved and the size in bytes of this field
886 * is four times the value specified in bits 3..7. Hence the "& ~3".
887 */
888 add_cdb_offset = srp_cmd->add_cdb_len & ~3;
889 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
890 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
891 ioctx->n_rbuf = 1;
892 ioctx->rbufs = &ioctx->single_rbuf;
893
894 db = (struct srp_direct_buf *)(srp_cmd->add_data
895 + add_cdb_offset);
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800896 memcpy(ioctx->rbufs, db, sizeof(*db));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000897 *data_len = be32_to_cpu(db->len);
898 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
899 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
900 idb = (struct srp_indirect_buf *)(srp_cmd->add_data
901 + add_cdb_offset);
902
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800903 ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof(*db);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000904
905 if (ioctx->n_rbuf >
906 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400907 pr_err("received unsupported SRP_CMD request"
Bart Van Asschea42d9852011-10-14 01:30:46 +0000908 " type (%u out + %u in != %u / %zu)\n",
909 srp_cmd->data_out_desc_cnt,
910 srp_cmd->data_in_desc_cnt,
911 be32_to_cpu(idb->table_desc.len),
912 sizeof(*db));
913 ioctx->n_rbuf = 0;
914 ret = -EINVAL;
915 goto out;
916 }
917
918 if (ioctx->n_rbuf == 1)
919 ioctx->rbufs = &ioctx->single_rbuf;
920 else {
921 ioctx->rbufs =
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800922 kmalloc(ioctx->n_rbuf * sizeof(*db), GFP_ATOMIC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000923 if (!ioctx->rbufs) {
924 ioctx->n_rbuf = 0;
925 ret = -ENOMEM;
926 goto out;
927 }
928 }
929
930 db = idb->desc_list;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800931 memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof(*db));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000932 *data_len = be32_to_cpu(idb->len);
933 }
934out:
935 return ret;
936}
937
938/**
939 * srpt_init_ch_qp() - Initialize queue pair attributes.
940 *
941 * Initialized the attributes of queue pair 'qp' by allowing local write,
942 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
943 */
944static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
945{
946 struct ib_qp_attr *attr;
947 int ret;
948
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800949 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000950 if (!attr)
951 return -ENOMEM;
952
953 attr->qp_state = IB_QPS_INIT;
954 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
955 IB_ACCESS_REMOTE_WRITE;
956 attr->port_num = ch->sport->port;
957 attr->pkey_index = 0;
958
959 ret = ib_modify_qp(qp, attr,
960 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
961 IB_QP_PKEY_INDEX);
962
963 kfree(attr);
964 return ret;
965}
966
967/**
968 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
969 * @ch: channel of the queue pair.
970 * @qp: queue pair to change the state of.
971 *
972 * Returns zero upon success and a negative value upon failure.
973 *
974 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
975 * If this structure ever becomes larger, it might be necessary to allocate
976 * it dynamically instead of on the stack.
977 */
978static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
979{
980 struct ib_qp_attr qp_attr;
981 int attr_mask;
982 int ret;
983
984 qp_attr.qp_state = IB_QPS_RTR;
985 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
986 if (ret)
987 goto out;
988
989 qp_attr.max_dest_rd_atomic = 4;
990
991 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
992
993out:
994 return ret;
995}
996
997/**
998 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
999 * @ch: channel of the queue pair.
1000 * @qp: queue pair to change the state of.
1001 *
1002 * Returns zero upon success and a negative value upon failure.
1003 *
1004 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1005 * If this structure ever becomes larger, it might be necessary to allocate
1006 * it dynamically instead of on the stack.
1007 */
1008static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1009{
1010 struct ib_qp_attr qp_attr;
1011 int attr_mask;
1012 int ret;
1013
1014 qp_attr.qp_state = IB_QPS_RTS;
1015 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1016 if (ret)
1017 goto out;
1018
1019 qp_attr.max_rd_atomic = 4;
1020
1021 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1022
1023out:
1024 return ret;
1025}
1026
1027/**
1028 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1029 */
1030static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1031{
1032 struct ib_qp_attr qp_attr;
1033
1034 qp_attr.qp_state = IB_QPS_ERR;
1035 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1036}
1037
1038/**
1039 * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
1040 */
1041static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1042 struct srpt_send_ioctx *ioctx)
1043{
1044 struct scatterlist *sg;
1045 enum dma_data_direction dir;
1046
1047 BUG_ON(!ch);
1048 BUG_ON(!ioctx);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001049 BUG_ON(ioctx->n_rdma && !ioctx->rdma_wrs);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001050
1051 while (ioctx->n_rdma)
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001052 kfree(ioctx->rdma_wrs[--ioctx->n_rdma].wr.sg_list);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001053
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001054 kfree(ioctx->rdma_wrs);
1055 ioctx->rdma_wrs = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001056
1057 if (ioctx->mapped_sg_count) {
1058 sg = ioctx->sg;
1059 WARN_ON(!sg);
1060 dir = ioctx->cmd.data_direction;
1061 BUG_ON(dir == DMA_NONE);
1062 ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
1063 opposite_dma_dir(dir));
1064 ioctx->mapped_sg_count = 0;
1065 }
1066}
1067
1068/**
1069 * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
1070 */
1071static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1072 struct srpt_send_ioctx *ioctx)
1073{
Mike Marciniszynb0768082014-04-07 13:58:35 -04001074 struct ib_device *dev = ch->sport->sdev->device;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001075 struct se_cmd *cmd;
1076 struct scatterlist *sg, *sg_orig;
1077 int sg_cnt;
1078 enum dma_data_direction dir;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001079 struct ib_rdma_wr *riu;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001080 struct srp_direct_buf *db;
1081 dma_addr_t dma_addr;
1082 struct ib_sge *sge;
1083 u64 raddr;
1084 u32 rsize;
1085 u32 tsize;
1086 u32 dma_len;
1087 int count, nrdma;
1088 int i, j, k;
1089
1090 BUG_ON(!ch);
1091 BUG_ON(!ioctx);
1092 cmd = &ioctx->cmd;
1093 dir = cmd->data_direction;
1094 BUG_ON(dir == DMA_NONE);
1095
Roland Dreier6f9e7f02012-03-30 11:29:12 -07001096 ioctx->sg = sg = sg_orig = cmd->t_data_sg;
1097 ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001098
1099 count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
1100 opposite_dma_dir(dir));
1101 if (unlikely(!count))
1102 return -EAGAIN;
1103
1104 ioctx->mapped_sg_count = count;
1105
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001106 if (ioctx->rdma_wrs && ioctx->n_rdma_wrs)
1107 nrdma = ioctx->n_rdma_wrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001108 else {
1109 nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
1110 + ioctx->n_rbuf;
1111
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001112 ioctx->rdma_wrs = kcalloc(nrdma, sizeof(*ioctx->rdma_wrs),
1113 GFP_KERNEL);
1114 if (!ioctx->rdma_wrs)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001115 goto free_mem;
1116
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001117 ioctx->n_rdma_wrs = nrdma;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001118 }
1119
1120 db = ioctx->rbufs;
1121 tsize = cmd->data_length;
Mike Marciniszynb0768082014-04-07 13:58:35 -04001122 dma_len = ib_sg_dma_len(dev, &sg[0]);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001123 riu = ioctx->rdma_wrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001124
1125 /*
1126 * For each remote desc - calculate the #ib_sge.
1127 * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1128 * each remote desc rdma_iu is required a rdma wr;
1129 * else
1130 * we need to allocate extra rdma_iu to carry extra #ib_sge in
1131 * another rdma wr
1132 */
1133 for (i = 0, j = 0;
1134 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1135 rsize = be32_to_cpu(db->len);
1136 raddr = be64_to_cpu(db->va);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001137 riu->remote_addr = raddr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001138 riu->rkey = be32_to_cpu(db->key);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001139 riu->wr.num_sge = 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001140
1141 /* calculate how many sge required for this remote_buf */
1142 while (rsize > 0 && tsize > 0) {
1143
1144 if (rsize >= dma_len) {
1145 tsize -= dma_len;
1146 rsize -= dma_len;
1147 raddr += dma_len;
1148
1149 if (tsize > 0) {
1150 ++j;
1151 if (j < count) {
1152 sg = sg_next(sg);
Mike Marciniszynb0768082014-04-07 13:58:35 -04001153 dma_len = ib_sg_dma_len(
1154 dev, sg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001155 }
1156 }
1157 } else {
1158 tsize -= rsize;
1159 dma_len -= rsize;
1160 rsize = 0;
1161 }
1162
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001163 ++riu->wr.num_sge;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001164
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001165 if (rsize > 0 &&
1166 riu->wr.num_sge == SRPT_DEF_SG_PER_WQE) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001167 ++ioctx->n_rdma;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001168 riu->wr.sg_list = kmalloc_array(riu->wr.num_sge,
1169 sizeof(*riu->wr.sg_list),
1170 GFP_KERNEL);
1171 if (!riu->wr.sg_list)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001172 goto free_mem;
1173
1174 ++riu;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001175 riu->wr.num_sge = 0;
1176 riu->remote_addr = raddr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001177 riu->rkey = be32_to_cpu(db->key);
1178 }
1179 }
1180
1181 ++ioctx->n_rdma;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001182 riu->wr.sg_list = kmalloc_array(riu->wr.num_sge,
1183 sizeof(*riu->wr.sg_list),
1184 GFP_KERNEL);
1185 if (!riu->wr.sg_list)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001186 goto free_mem;
1187 }
1188
1189 db = ioctx->rbufs;
1190 tsize = cmd->data_length;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001191 riu = ioctx->rdma_wrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001192 sg = sg_orig;
Mike Marciniszynb0768082014-04-07 13:58:35 -04001193 dma_len = ib_sg_dma_len(dev, &sg[0]);
1194 dma_addr = ib_sg_dma_address(dev, &sg[0]);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001195
1196 /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1197 for (i = 0, j = 0;
1198 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1199 rsize = be32_to_cpu(db->len);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001200 sge = riu->wr.sg_list;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001201 k = 0;
1202
1203 while (rsize > 0 && tsize > 0) {
1204 sge->addr = dma_addr;
Jason Gunthorpe5a783952015-07-30 17:22:24 -06001205 sge->lkey = ch->sport->sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001206
1207 if (rsize >= dma_len) {
1208 sge->length =
1209 (tsize < dma_len) ? tsize : dma_len;
1210 tsize -= dma_len;
1211 rsize -= dma_len;
1212
1213 if (tsize > 0) {
1214 ++j;
1215 if (j < count) {
1216 sg = sg_next(sg);
Mike Marciniszynb0768082014-04-07 13:58:35 -04001217 dma_len = ib_sg_dma_len(
1218 dev, sg);
1219 dma_addr = ib_sg_dma_address(
1220 dev, sg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001221 }
1222 }
1223 } else {
1224 sge->length = (tsize < rsize) ? tsize : rsize;
1225 tsize -= rsize;
1226 dma_len -= rsize;
1227 dma_addr += rsize;
1228 rsize = 0;
1229 }
1230
1231 ++k;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001232 if (k == riu->wr.num_sge && rsize > 0 && tsize > 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001233 ++riu;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001234 sge = riu->wr.sg_list;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001235 k = 0;
1236 } else if (rsize > 0 && tsize > 0)
1237 ++sge;
1238 }
1239 }
1240
1241 return 0;
1242
1243free_mem:
1244 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1245
1246 return -ENOMEM;
1247}
1248
1249/**
1250 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1251 */
1252static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1253{
1254 struct srpt_send_ioctx *ioctx;
1255 unsigned long flags;
1256
1257 BUG_ON(!ch);
1258
1259 ioctx = NULL;
1260 spin_lock_irqsave(&ch->spinlock, flags);
1261 if (!list_empty(&ch->free_list)) {
1262 ioctx = list_first_entry(&ch->free_list,
1263 struct srpt_send_ioctx, free_list);
1264 list_del(&ioctx->free_list);
1265 }
1266 spin_unlock_irqrestore(&ch->spinlock, flags);
1267
1268 if (!ioctx)
1269 return ioctx;
1270
1271 BUG_ON(ioctx->ch != ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001272 spin_lock_init(&ioctx->spinlock);
1273 ioctx->state = SRPT_STATE_NEW;
1274 ioctx->n_rbuf = 0;
1275 ioctx->rbufs = NULL;
1276 ioctx->n_rdma = 0;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001277 ioctx->n_rdma_wrs = 0;
1278 ioctx->rdma_wrs = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001279 ioctx->mapped_sg_count = 0;
1280 init_completion(&ioctx->tx_done);
1281 ioctx->queue_status_only = false;
1282 /*
1283 * transport_init_se_cmd() does not initialize all fields, so do it
1284 * here.
1285 */
1286 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1287 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1288
1289 return ioctx;
1290}
1291
1292/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001293 * srpt_abort_cmd() - Abort a SCSI command.
1294 * @ioctx: I/O context associated with the SCSI command.
1295 * @context: Preferred execution context.
1296 */
1297static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1298{
1299 enum srpt_command_state state;
1300 unsigned long flags;
1301
1302 BUG_ON(!ioctx);
1303
1304 /*
1305 * If the command is in a state where the target core is waiting for
1306 * the ib_srpt driver, change the state to the next state. Changing
1307 * the state of the command from SRPT_STATE_NEED_DATA to
1308 * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
1309 * function a second time.
1310 */
1311
1312 spin_lock_irqsave(&ioctx->spinlock, flags);
1313 state = ioctx->state;
1314 switch (state) {
1315 case SRPT_STATE_NEED_DATA:
1316 ioctx->state = SRPT_STATE_DATA_IN;
1317 break;
1318 case SRPT_STATE_DATA_IN:
1319 case SRPT_STATE_CMD_RSP_SENT:
1320 case SRPT_STATE_MGMT_RSP_SENT:
1321 ioctx->state = SRPT_STATE_DONE;
1322 break;
1323 default:
1324 break;
1325 }
1326 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1327
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001328 if (state == SRPT_STATE_DONE) {
1329 struct srpt_rdma_ch *ch = ioctx->ch;
1330
1331 BUG_ON(ch->sess == NULL);
1332
Bart Van Asscheafc16602015-04-27 13:52:36 +02001333 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001334 goto out;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001335 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001336
1337 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
Bart Van Assche649ee052015-04-14 13:26:44 +02001338 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001339
1340 switch (state) {
1341 case SRPT_STATE_NEW:
1342 case SRPT_STATE_DATA_IN:
1343 case SRPT_STATE_MGMT:
1344 /*
1345 * Do nothing - defer abort processing until
1346 * srpt_queue_response() is invoked.
1347 */
1348 WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
1349 break;
1350 case SRPT_STATE_NEED_DATA:
1351 /* DMA_TO_DEVICE (write) - RDMA read error. */
Christoph Hellwige672a472012-07-08 15:58:43 -04001352
1353 /* XXX(hch): this is a horrible layering violation.. */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001354 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
Christoph Hellwige672a472012-07-08 15:58:43 -04001355 ioctx->cmd.transport_state &= ~CMD_T_ACTIVE;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001356 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001357 break;
1358 case SRPT_STATE_CMD_RSP_SENT:
1359 /*
1360 * SRP_RSP sending failed or the SRP_RSP send completion has
1361 * not been received in time.
1362 */
1363 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001364 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001365 break;
1366 case SRPT_STATE_MGMT_RSP_SENT:
1367 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001368 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001369 break;
1370 default:
Grant Grundler532ec6f2013-03-26 21:48:28 +00001371 WARN(1, "Unexpected command state (%d)", state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001372 break;
1373 }
1374
1375out:
1376 return state;
1377}
1378
1379/**
Christoph Hellwige672a472012-07-08 15:58:43 -04001380 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1381 * the data that has been transferred via IB RDMA had to be postponed until the
Masanari Iida142ad5d2012-08-10 00:07:58 +00001382 * check_stop_free() callback. None of this is necessary anymore and needs to
Christoph Hellwige672a472012-07-08 15:58:43 -04001383 * be cleaned up.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001384 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001385static void srpt_rdma_read_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_rdma_ch *ch = cq->cq_context;
1388 struct srpt_send_ioctx *ioctx =
Bart Van Assche19f57292015-12-31 09:56:43 +01001389 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001390
Bart Van Asschea42d9852011-10-14 01:30:46 +00001391 WARN_ON(ioctx->n_rdma <= 0);
1392 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1393
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001394 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1395 pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
1396 ioctx, wc->status);
1397 srpt_abort_cmd(ioctx);
1398 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001399 }
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001400
1401 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1402 SRPT_STATE_DATA_IN))
1403 target_execute_cmd(&ioctx->cmd);
1404 else
1405 pr_err("%s[%d]: wrong state = %d\n", __func__,
1406 __LINE__, srpt_get_cmd_state(ioctx));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001407}
1408
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001409static void srpt_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001410{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001411 struct srpt_send_ioctx *ioctx =
Bart Van Assche19f57292015-12-31 09:56:43 +01001412 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001413
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001414 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1415 pr_info("RDMA_WRITE for ioctx 0x%p failed with status %d\n",
1416 ioctx, wc->status);
1417 srpt_abort_cmd(ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001418 }
1419}
1420
1421/**
1422 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1423 * @ch: RDMA channel through which the request has been received.
1424 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1425 * be built in the buffer ioctx->buf points at and hence this function will
1426 * overwrite the request data.
1427 * @tag: tag of the request for which this response is being generated.
1428 * @status: value for the STATUS field of the SRP_RSP information unit.
1429 *
1430 * Returns the size in bytes of the SRP_RSP response.
1431 *
1432 * An SRP_RSP response contains a SCSI status or service response. See also
1433 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1434 * response. See also SPC-2 for more information about sense data.
1435 */
1436static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1437 struct srpt_send_ioctx *ioctx, u64 tag,
1438 int status)
1439{
1440 struct srp_rsp *srp_rsp;
1441 const u8 *sense_data;
1442 int sense_data_len, max_sense_len;
1443
1444 /*
1445 * The lowest bit of all SAM-3 status codes is zero (see also
1446 * paragraph 5.3 in SAM-3).
1447 */
1448 WARN_ON(status & 1);
1449
1450 srp_rsp = ioctx->ioctx.buf;
1451 BUG_ON(!srp_rsp);
1452
1453 sense_data = ioctx->sense_data;
1454 sense_data_len = ioctx->cmd.scsi_sense_length;
1455 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1456
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08001457 memset(srp_rsp, 0, sizeof(*srp_rsp));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001458 srp_rsp->opcode = SRP_RSP;
1459 srp_rsp->req_lim_delta =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301460 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001461 srp_rsp->tag = tag;
1462 srp_rsp->status = status;
1463
1464 if (sense_data_len) {
1465 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1466 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1467 if (sense_data_len > max_sense_len) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001468 pr_warn("truncated sense data from %d to %d"
1469 " bytes\n", sense_data_len, max_sense_len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001470 sense_data_len = max_sense_len;
1471 }
1472
1473 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1474 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1475 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1476 }
1477
1478 return sizeof(*srp_rsp) + sense_data_len;
1479}
1480
1481/**
1482 * srpt_build_tskmgmt_rsp() - Build a task management response.
1483 * @ch: RDMA channel through which the request has been received.
1484 * @ioctx: I/O context in which the SRP_RSP response will be built.
1485 * @rsp_code: RSP_CODE that will be stored in the response.
1486 * @tag: Tag of the request for which this response is being generated.
1487 *
1488 * Returns the size in bytes of the SRP_RSP response.
1489 *
1490 * An SRP_RSP response contains a SCSI status or service response. See also
1491 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1492 * response.
1493 */
1494static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1495 struct srpt_send_ioctx *ioctx,
1496 u8 rsp_code, u64 tag)
1497{
1498 struct srp_rsp *srp_rsp;
1499 int resp_data_len;
1500 int resp_len;
1501
Jack Wangc807f642013-09-30 10:09:05 +02001502 resp_data_len = 4;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001503 resp_len = sizeof(*srp_rsp) + resp_data_len;
1504
1505 srp_rsp = ioctx->ioctx.buf;
1506 BUG_ON(!srp_rsp);
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08001507 memset(srp_rsp, 0, sizeof(*srp_rsp));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001508
1509 srp_rsp->opcode = SRP_RSP;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301510 srp_rsp->req_lim_delta =
1511 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001512 srp_rsp->tag = tag;
1513
Jack Wangc807f642013-09-30 10:09:05 +02001514 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1515 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1516 srp_rsp->data[3] = rsp_code;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001517
1518 return resp_len;
1519}
1520
1521#define NO_SUCH_LUN ((uint64_t)-1LL)
1522
1523/*
1524 * SCSI LUN addressing method. See also SAM-2 and the section about
1525 * eight byte LUNs.
1526 */
1527enum scsi_lun_addr_method {
1528 SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
1529 SCSI_LUN_ADDR_METHOD_FLAT = 1,
1530 SCSI_LUN_ADDR_METHOD_LUN = 2,
1531 SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
1532};
1533
1534/*
1535 * srpt_unpack_lun() - Convert from network LUN to linear LUN.
1536 *
1537 * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
1538 * order (big endian) to a linear LUN. Supports three LUN addressing methods:
1539 * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
1540 */
1541static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
1542{
1543 uint64_t res = NO_SUCH_LUN;
1544 int addressing_method;
1545
1546 if (unlikely(len < 2)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001547 pr_err("Illegal LUN length %d, expected 2 bytes or more\n",
1548 len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001549 goto out;
1550 }
1551
1552 switch (len) {
1553 case 8:
1554 if ((*((__be64 *)lun) &
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301555 cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001556 goto out_err;
1557 break;
1558 case 4:
1559 if (*((__be16 *)&lun[2]) != 0)
1560 goto out_err;
1561 break;
1562 case 6:
1563 if (*((__be32 *)&lun[2]) != 0)
1564 goto out_err;
1565 break;
1566 case 2:
1567 break;
1568 default:
1569 goto out_err;
1570 }
1571
1572 addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
1573 switch (addressing_method) {
1574 case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
1575 case SCSI_LUN_ADDR_METHOD_FLAT:
1576 case SCSI_LUN_ADDR_METHOD_LUN:
1577 res = *(lun + 1) | (((*lun) & 0x3f) << 8);
1578 break;
1579
1580 case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
1581 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001582 pr_err("Unimplemented LUN addressing method %u\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001583 addressing_method);
1584 break;
1585 }
1586
1587out:
1588 return res;
1589
1590out_err:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001591 pr_err("Support for multi-level LUNs has not yet been implemented\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001592 goto out;
1593}
1594
1595static int srpt_check_stop_free(struct se_cmd *cmd)
1596{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001597 struct srpt_send_ioctx *ioctx = container_of(cmd,
1598 struct srpt_send_ioctx, cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001599
Bart Van Asscheafc16602015-04-27 13:52:36 +02001600 return target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001601}
1602
1603/**
1604 * srpt_handle_cmd() - Process SRP_CMD.
1605 */
1606static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
1607 struct srpt_recv_ioctx *recv_ioctx,
1608 struct srpt_send_ioctx *send_ioctx)
1609{
1610 struct se_cmd *cmd;
1611 struct srp_cmd *srp_cmd;
1612 uint64_t unpacked_lun;
1613 u64 data_len;
1614 enum dma_data_direction dir;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001615 sense_reason_t ret;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001616 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001617
1618 BUG_ON(!send_ioctx);
1619
1620 srp_cmd = recv_ioctx->ioctx.buf;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001621 cmd = &send_ioctx->cmd;
Bart Van Assche649ee052015-04-14 13:26:44 +02001622 cmd->tag = srp_cmd->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001623
1624 switch (srp_cmd->task_attr) {
1625 case SRP_CMD_SIMPLE_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001626 cmd->sam_task_attr = TCM_SIMPLE_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001627 break;
1628 case SRP_CMD_ORDERED_Q:
1629 default:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001630 cmd->sam_task_attr = TCM_ORDERED_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001631 break;
1632 case SRP_CMD_HEAD_OF_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001633 cmd->sam_task_attr = TCM_HEAD_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001634 break;
1635 case SRP_CMD_ACA:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001636 cmd->sam_task_attr = TCM_ACA_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001637 break;
1638 }
1639
Christoph Hellwigde103c92012-11-06 12:24:09 -08001640 if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001641 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001642 srp_cmd->tag);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001643 ret = TCM_INVALID_CDB_FIELD;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001644 goto send_sense;
1645 }
1646
Bart Van Asschea42d9852011-10-14 01:30:46 +00001647 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
1648 sizeof(srp_cmd->lun));
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001649 rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
1650 &send_ioctx->sense_data[0], unpacked_lun, data_len,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001651 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001652 if (rc != 0) {
1653 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001654 goto send_sense;
Nicholas Bellinger187e70a2012-03-17 20:12:36 -07001655 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001656 return 0;
1657
1658send_sense:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001659 transport_send_check_condition_and_sense(cmd, ret, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001660 return -1;
1661}
1662
Bart Van Asschea42d9852011-10-14 01:30:46 +00001663static int srp_tmr_to_tcm(int fn)
1664{
1665 switch (fn) {
1666 case SRP_TSK_ABORT_TASK:
1667 return TMR_ABORT_TASK;
1668 case SRP_TSK_ABORT_TASK_SET:
1669 return TMR_ABORT_TASK_SET;
1670 case SRP_TSK_CLEAR_TASK_SET:
1671 return TMR_CLEAR_TASK_SET;
1672 case SRP_TSK_LUN_RESET:
1673 return TMR_LUN_RESET;
1674 case SRP_TSK_CLEAR_ACA:
1675 return TMR_CLEAR_ACA;
1676 default:
1677 return -1;
1678 }
1679}
1680
1681/**
1682 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1683 *
1684 * Returns 0 if and only if the request will be processed by the target core.
1685 *
1686 * For more information about SRP_TSK_MGMT information units, see also section
1687 * 6.7 in the SRP r16a document.
1688 */
1689static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1690 struct srpt_recv_ioctx *recv_ioctx,
1691 struct srpt_send_ioctx *send_ioctx)
1692{
1693 struct srp_tsk_mgmt *srp_tsk;
1694 struct se_cmd *cmd;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001695 struct se_session *sess = ch->sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001696 uint64_t unpacked_lun;
1697 int tcm_tmr;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001698 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001699
1700 BUG_ON(!send_ioctx);
1701
1702 srp_tsk = recv_ioctx->ioctx.buf;
1703 cmd = &send_ioctx->cmd;
1704
1705 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1706 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1707 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1708
1709 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
Bart Van Assche649ee052015-04-14 13:26:44 +02001710 send_ioctx->cmd.tag = srp_tsk->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001711 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001712 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
1713 sizeof(srp_tsk->lun));
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001714 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, unpacked_lun,
Bart Van Assche51093252016-02-11 11:03:09 -08001715 srp_tsk, tcm_tmr, GFP_KERNEL, srp_tsk->task_tag,
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001716 TARGET_SCF_ACK_KREF);
1717 if (rc != 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001718 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001719 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001720 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001721 return;
1722fail:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001723 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001724}
1725
1726/**
1727 * srpt_handle_new_iu() - Process a newly received information unit.
1728 * @ch: RDMA channel through which the information unit has been received.
1729 * @ioctx: SRPT I/O context associated with the information unit.
1730 */
1731static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1732 struct srpt_recv_ioctx *recv_ioctx,
1733 struct srpt_send_ioctx *send_ioctx)
1734{
1735 struct srp_cmd *srp_cmd;
1736 enum rdma_ch_state ch_state;
1737
1738 BUG_ON(!ch);
1739 BUG_ON(!recv_ioctx);
1740
1741 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1742 recv_ioctx->ioctx.dma, srp_max_req_size,
1743 DMA_FROM_DEVICE);
1744
1745 ch_state = srpt_get_ch_state(ch);
1746 if (unlikely(ch_state == CH_CONNECTING)) {
1747 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1748 goto out;
1749 }
1750
1751 if (unlikely(ch_state != CH_LIVE))
1752 goto out;
1753
1754 srp_cmd = recv_ioctx->ioctx.buf;
1755 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1756 if (!send_ioctx)
1757 send_ioctx = srpt_get_send_ioctx(ch);
1758 if (unlikely(!send_ioctx)) {
1759 list_add_tail(&recv_ioctx->wait_list,
1760 &ch->cmd_wait_list);
1761 goto out;
1762 }
1763 }
1764
Bart Van Asschea42d9852011-10-14 01:30:46 +00001765 switch (srp_cmd->opcode) {
1766 case SRP_CMD:
1767 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1768 break;
1769 case SRP_TSK_MGMT:
1770 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1771 break;
1772 case SRP_I_LOGOUT:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001773 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001774 break;
1775 case SRP_CRED_RSP:
1776 pr_debug("received SRP_CRED_RSP\n");
1777 break;
1778 case SRP_AER_RSP:
1779 pr_debug("received SRP_AER_RSP\n");
1780 break;
1781 case SRP_RSP:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001782 pr_err("Received SRP_RSP\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001783 break;
1784 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001785 pr_err("received IU with unknown opcode 0x%x\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001786 srp_cmd->opcode);
1787 break;
1788 }
1789
1790 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1791out:
1792 return;
1793}
1794
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001795static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001796{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001797 struct srpt_rdma_ch *ch = cq->cq_context;
1798 struct srpt_recv_ioctx *ioctx =
1799 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001800
Bart Van Asschea42d9852011-10-14 01:30:46 +00001801 if (wc->status == IB_WC_SUCCESS) {
1802 int req_lim;
1803
1804 req_lim = atomic_dec_return(&ch->req_lim);
1805 if (unlikely(req_lim < 0))
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001806 pr_err("req_lim = %d < 0\n", req_lim);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001807 srpt_handle_new_iu(ch, ioctx, NULL);
1808 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001809 pr_info("receiving failed for ioctx %p with status %d\n",
1810 ioctx, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001811 }
1812}
1813
1814/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001815 * Note: Although this has not yet been observed during tests, at least in
1816 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1817 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1818 * value in each response is set to one, and it is possible that this response
1819 * makes the initiator send a new request before the send completion for that
1820 * response has been processed. This could e.g. happen if the call to
1821 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1822 * if IB retransmission causes generation of the send completion to be
1823 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1824 * are queued on cmd_wait_list. The code below processes these delayed
1825 * requests one at a time.
1826 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001827static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001828{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001829 struct srpt_rdma_ch *ch = cq->cq_context;
1830 struct srpt_send_ioctx *ioctx =
1831 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1832 enum srpt_command_state state;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001833
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001834 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1835
1836 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1837 state != SRPT_STATE_MGMT_RSP_SENT);
1838
1839 atomic_inc(&ch->sq_wr_avail);
1840
1841 if (wc->status != IB_WC_SUCCESS) {
1842 pr_info("sending response for ioctx 0x%p failed"
1843 " with status %d\n", ioctx, wc->status);
1844
1845 atomic_dec(&ch->req_lim);
1846 srpt_abort_cmd(ioctx);
1847 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001848 }
1849
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001850 if (state != SRPT_STATE_DONE) {
1851 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1852 transport_generic_free_cmd(&ioctx->cmd, 0);
1853 } else {
1854 pr_err("IB completion has been received too late for"
1855 " wr_id = %u.\n", ioctx->ioctx.index);
1856 }
1857
1858out:
1859 while (!list_empty(&ch->cmd_wait_list) &&
1860 srpt_get_ch_state(ch) == CH_LIVE &&
1861 (ioctx = srpt_get_send_ioctx(ch)) != NULL) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001862 struct srpt_recv_ioctx *recv_ioctx;
1863
1864 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
1865 struct srpt_recv_ioctx,
1866 wait_list);
1867 list_del(&recv_ioctx->wait_list);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001868 srpt_handle_new_iu(ch, recv_ioctx, ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001869 }
1870}
1871
Bart Van Asschea42d9852011-10-14 01:30:46 +00001872/**
1873 * srpt_create_ch_ib() - Create receive and send completion queues.
1874 */
1875static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1876{
1877 struct ib_qp_init_attr *qp_init;
1878 struct srpt_port *sport = ch->sport;
1879 struct srpt_device *sdev = sport->sdev;
1880 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
1881 int ret;
1882
1883 WARN_ON(ch->rq_size < 1);
1884
1885 ret = -ENOMEM;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08001886 qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001887 if (!qp_init)
1888 goto out;
1889
Bart Van Asscheab477c12014-10-19 18:05:33 +03001890retry:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001891 ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size,
1892 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001893 if (IS_ERR(ch->cq)) {
1894 ret = PTR_ERR(ch->cq);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001895 pr_err("failed to create CQ cqe= %d ret= %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001896 ch->rq_size + srp_sq_size, ret);
1897 goto out;
1898 }
1899
1900 qp_init->qp_context = (void *)ch;
1901 qp_init->event_handler
1902 = (void(*)(struct ib_event *, void*))srpt_qp_event;
1903 qp_init->send_cq = ch->cq;
1904 qp_init->recv_cq = ch->cq;
1905 qp_init->srq = sdev->srq;
1906 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1907 qp_init->qp_type = IB_QPT_RC;
1908 qp_init->cap.max_send_wr = srp_sq_size;
1909 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
1910
1911 ch->qp = ib_create_qp(sdev->pd, qp_init);
1912 if (IS_ERR(ch->qp)) {
1913 ret = PTR_ERR(ch->qp);
Bart Van Asscheab477c12014-10-19 18:05:33 +03001914 if (ret == -ENOMEM) {
1915 srp_sq_size /= 2;
1916 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
1917 ib_destroy_cq(ch->cq);
1918 goto retry;
1919 }
1920 }
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001921 pr_err("failed to create_qp ret= %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001922 goto err_destroy_cq;
1923 }
1924
1925 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1926
1927 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
1928 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1929 qp_init->cap.max_send_wr, ch->cm_id);
1930
1931 ret = srpt_init_ch_qp(ch, ch->qp);
1932 if (ret)
1933 goto err_destroy_qp;
1934
Bart Van Asschea42d9852011-10-14 01:30:46 +00001935out:
1936 kfree(qp_init);
1937 return ret;
1938
1939err_destroy_qp:
1940 ib_destroy_qp(ch->qp);
1941err_destroy_cq:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001942 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001943 goto out;
1944}
1945
1946static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
1947{
Bart Van Asschea42d9852011-10-14 01:30:46 +00001948 ib_destroy_qp(ch->qp);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001949 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001950}
1951
1952/**
1953 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
1954 *
1955 * Reset the QP and make sure all resources associated with the channel will
1956 * be deallocated at an appropriate time.
1957 *
1958 * Note: The caller must hold ch->sport->sdev->spinlock.
1959 */
1960static void __srpt_close_ch(struct srpt_rdma_ch *ch)
1961{
Bart Van Asschea42d9852011-10-14 01:30:46 +00001962 enum rdma_ch_state prev_state;
1963 unsigned long flags;
1964
Bart Van Asschea42d9852011-10-14 01:30:46 +00001965 spin_lock_irqsave(&ch->spinlock, flags);
1966 prev_state = ch->state;
1967 switch (prev_state) {
1968 case CH_CONNECTING:
1969 case CH_LIVE:
1970 ch->state = CH_DISCONNECTING;
1971 break;
1972 default:
1973 break;
1974 }
1975 spin_unlock_irqrestore(&ch->spinlock, flags);
1976
1977 switch (prev_state) {
1978 case CH_CONNECTING:
1979 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
1980 NULL, 0);
1981 /* fall through */
1982 case CH_LIVE:
1983 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001984 pr_err("sending CM DREQ failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001985 break;
1986 case CH_DISCONNECTING:
1987 break;
1988 case CH_DRAINING:
1989 case CH_RELEASING:
1990 break;
1991 }
1992}
1993
1994/**
1995 * srpt_close_ch() - Close an RDMA channel.
1996 */
1997static void srpt_close_ch(struct srpt_rdma_ch *ch)
1998{
1999 struct srpt_device *sdev;
2000
2001 sdev = ch->sport->sdev;
2002 spin_lock_irq(&sdev->spinlock);
2003 __srpt_close_ch(ch);
2004 spin_unlock_irq(&sdev->spinlock);
2005}
2006
2007/**
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07002008 * srpt_shutdown_session() - Whether or not a session may be shut down.
2009 */
2010static int srpt_shutdown_session(struct se_session *se_sess)
2011{
2012 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
2013 unsigned long flags;
2014
2015 spin_lock_irqsave(&ch->spinlock, flags);
2016 if (ch->in_shutdown) {
2017 spin_unlock_irqrestore(&ch->spinlock, flags);
2018 return true;
2019 }
2020
2021 ch->in_shutdown = true;
2022 target_sess_cmd_list_set_waiting(se_sess);
2023 spin_unlock_irqrestore(&ch->spinlock, flags);
2024
2025 return true;
2026}
2027
2028/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00002029 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
2030 * @cm_id: Pointer to the CM ID of the channel to be drained.
2031 *
2032 * Note: Must be called from inside srpt_cm_handler to avoid a race between
2033 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
2034 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
2035 * waits until all target sessions for the associated IB device have been
2036 * unregistered and target session registration involves a call to
2037 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
2038 * this function has finished).
2039 */
2040static void srpt_drain_channel(struct ib_cm_id *cm_id)
2041{
2042 struct srpt_device *sdev;
2043 struct srpt_rdma_ch *ch;
2044 int ret;
2045 bool do_reset = false;
2046
2047 WARN_ON_ONCE(irqs_disabled());
2048
2049 sdev = cm_id->context;
2050 BUG_ON(!sdev);
2051 spin_lock_irq(&sdev->spinlock);
2052 list_for_each_entry(ch, &sdev->rch_list, list) {
2053 if (ch->cm_id == cm_id) {
2054 do_reset = srpt_test_and_set_ch_state(ch,
2055 CH_CONNECTING, CH_DRAINING) ||
2056 srpt_test_and_set_ch_state(ch,
2057 CH_LIVE, CH_DRAINING) ||
2058 srpt_test_and_set_ch_state(ch,
2059 CH_DISCONNECTING, CH_DRAINING);
2060 break;
2061 }
2062 }
2063 spin_unlock_irq(&sdev->spinlock);
2064
2065 if (do_reset) {
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07002066 if (ch->sess)
2067 srpt_shutdown_session(ch->sess);
2068
Bart Van Asschea42d9852011-10-14 01:30:46 +00002069 ret = srpt_ch_qp_err(ch);
2070 if (ret < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002071 pr_err("Setting queue pair in error state"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002072 " failed: %d\n", ret);
2073 }
2074}
2075
2076/**
2077 * srpt_find_channel() - Look up an RDMA channel.
2078 * @cm_id: Pointer to the CM ID of the channel to be looked up.
2079 *
2080 * Return NULL if no matching RDMA channel has been found.
2081 */
2082static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
2083 struct ib_cm_id *cm_id)
2084{
2085 struct srpt_rdma_ch *ch;
2086 bool found;
2087
2088 WARN_ON_ONCE(irqs_disabled());
2089 BUG_ON(!sdev);
2090
2091 found = false;
2092 spin_lock_irq(&sdev->spinlock);
2093 list_for_each_entry(ch, &sdev->rch_list, list) {
2094 if (ch->cm_id == cm_id) {
2095 found = true;
2096 break;
2097 }
2098 }
2099 spin_unlock_irq(&sdev->spinlock);
2100
2101 return found ? ch : NULL;
2102}
2103
2104/**
2105 * srpt_release_channel() - Release channel resources.
2106 *
2107 * Schedules the actual release because:
2108 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
2109 * trigger a deadlock.
2110 * - It is not safe to call TCM transport_* functions from interrupt context.
2111 */
2112static void srpt_release_channel(struct srpt_rdma_ch *ch)
2113{
2114 schedule_work(&ch->release_work);
2115}
2116
2117static void srpt_release_channel_work(struct work_struct *w)
2118{
2119 struct srpt_rdma_ch *ch;
2120 struct srpt_device *sdev;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002121 struct se_session *se_sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002122
2123 ch = container_of(w, struct srpt_rdma_ch, release_work);
2124 pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2125 ch->release_done);
2126
2127 sdev = ch->sport->sdev;
2128 BUG_ON(!sdev);
2129
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002130 se_sess = ch->sess;
2131 BUG_ON(!se_sess);
2132
Joern Engelbe646c2d2013-05-15 00:44:07 -07002133 target_wait_for_sess_cmds(se_sess);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002134
2135 transport_deregister_session_configfs(se_sess);
2136 transport_deregister_session(se_sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002137 ch->sess = NULL;
2138
Nicholas Bellinger0b41d6c2013-09-18 12:48:27 -07002139 ib_destroy_cm_id(ch->cm_id);
2140
Bart Van Asschea42d9852011-10-14 01:30:46 +00002141 srpt_destroy_ch_ib(ch);
2142
2143 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2144 ch->sport->sdev, ch->rq_size,
2145 ch->rsp_size, DMA_TO_DEVICE);
2146
2147 spin_lock_irq(&sdev->spinlock);
2148 list_del(&ch->list);
2149 spin_unlock_irq(&sdev->spinlock);
2150
Bart Van Asschea42d9852011-10-14 01:30:46 +00002151 if (ch->release_done)
2152 complete(ch->release_done);
2153
2154 wake_up(&sdev->ch_releaseQ);
2155
2156 kfree(ch);
2157}
2158
Bart Van Asschea42d9852011-10-14 01:30:46 +00002159/**
2160 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2161 *
2162 * Ownership of the cm_id is transferred to the target session if this
2163 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2164 */
2165static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2166 struct ib_cm_req_event_param *param,
2167 void *private_data)
2168{
2169 struct srpt_device *sdev = cm_id->context;
2170 struct srpt_port *sport = &sdev->port[param->port - 1];
2171 struct srp_login_req *req;
2172 struct srp_login_rsp *rsp;
2173 struct srp_login_rej *rej;
2174 struct ib_cm_rep_param *rep_param;
2175 struct srpt_rdma_ch *ch, *tmp_ch;
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002176 struct se_node_acl *se_acl;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002177 u32 it_iu_len;
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002178 int i, ret = 0;
2179 unsigned char *p;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002180
2181 WARN_ON_ONCE(irqs_disabled());
2182
2183 if (WARN_ON(!sdev || !private_data))
2184 return -EINVAL;
2185
2186 req = (struct srp_login_req *)private_data;
2187
2188 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2189
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002190 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2191 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2192 " (guid=0x%llx:0x%llx)\n",
2193 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2194 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2195 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2196 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2197 it_iu_len,
2198 param->port,
2199 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2200 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002201
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08002202 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
2203 rej = kzalloc(sizeof(*rej), GFP_KERNEL);
2204 rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002205
2206 if (!rsp || !rej || !rep_param) {
2207 ret = -ENOMEM;
2208 goto out;
2209 }
2210
2211 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302212 rej->reason = cpu_to_be32(
2213 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002214 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002215 pr_err("rejected SRP_LOGIN_REQ because its"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002216 " length (%d bytes) is out of range (%d .. %d)\n",
2217 it_iu_len, 64, srp_max_req_size);
2218 goto reject;
2219 }
2220
2221 if (!sport->enabled) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302222 rej->reason = cpu_to_be32(
2223 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002224 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002225 pr_err("rejected SRP_LOGIN_REQ because the target port"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002226 " has not yet been enabled\n");
2227 goto reject;
2228 }
2229
2230 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2231 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2232
2233 spin_lock_irq(&sdev->spinlock);
2234
2235 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2236 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2237 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2238 && param->port == ch->sport->port
2239 && param->listen_id == ch->sport->sdev->cm_id
2240 && ch->cm_id) {
2241 enum rdma_ch_state ch_state;
2242
2243 ch_state = srpt_get_ch_state(ch);
2244 if (ch_state != CH_CONNECTING
2245 && ch_state != CH_LIVE)
2246 continue;
2247
2248 /* found an existing channel */
2249 pr_debug("Found existing channel %s"
2250 " cm_id= %p state= %d\n",
2251 ch->sess_name, ch->cm_id, ch_state);
2252
2253 __srpt_close_ch(ch);
2254
2255 rsp->rsp_flags =
2256 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2257 }
2258 }
2259
2260 spin_unlock_irq(&sdev->spinlock);
2261
2262 } else
2263 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2264
2265 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2266 || *(__be64 *)(req->target_port_id + 8) !=
2267 cpu_to_be64(srpt_service_guid)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302268 rej->reason = cpu_to_be32(
2269 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002270 ret = -ENOMEM;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002271 pr_err("rejected SRP_LOGIN_REQ because it"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002272 " has an invalid target port identifier.\n");
2273 goto reject;
2274 }
2275
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08002276 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002277 if (!ch) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302278 rej->reason = cpu_to_be32(
2279 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002280 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002281 ret = -ENOMEM;
2282 goto reject;
2283 }
2284
2285 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2286 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2287 memcpy(ch->t_port_id, req->target_port_id, 16);
2288 ch->sport = &sdev->port[param->port - 1];
2289 ch->cm_id = cm_id;
2290 /*
2291 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2292 * for the SRP protocol to the command queue size.
2293 */
2294 ch->rq_size = SRPT_RQ_SIZE;
2295 spin_lock_init(&ch->spinlock);
2296 ch->state = CH_CONNECTING;
2297 INIT_LIST_HEAD(&ch->cmd_wait_list);
2298 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2299
2300 ch->ioctx_ring = (struct srpt_send_ioctx **)
2301 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2302 sizeof(*ch->ioctx_ring[0]),
2303 ch->rsp_size, DMA_TO_DEVICE);
2304 if (!ch->ioctx_ring)
2305 goto free_ch;
2306
2307 INIT_LIST_HEAD(&ch->free_list);
2308 for (i = 0; i < ch->rq_size; i++) {
2309 ch->ioctx_ring[i]->ch = ch;
2310 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2311 }
2312
2313 ret = srpt_create_ch_ib(ch);
2314 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302315 rej->reason = cpu_to_be32(
2316 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002317 pr_err("rejected SRP_LOGIN_REQ because creating"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002318 " a new RDMA channel failed.\n");
2319 goto free_ring;
2320 }
2321
2322 ret = srpt_ch_qp_rtr(ch, ch->qp);
2323 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302324 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002325 pr_err("rejected SRP_LOGIN_REQ because enabling"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002326 " RTR failed (error code = %d)\n", ret);
2327 goto destroy_ib;
2328 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002329
Bart Van Asschea42d9852011-10-14 01:30:46 +00002330 /*
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002331 * Use the initator port identifier as the session name, when
2332 * checking against se_node_acl->initiatorname[] this can be
2333 * with or without preceeding '0x'.
Bart Van Asschea42d9852011-10-14 01:30:46 +00002334 */
2335 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2336 be64_to_cpu(*(__be64 *)ch->i_port_id),
2337 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2338
2339 pr_debug("registering session %s\n", ch->sess_name);
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002340 p = &ch->sess_name[0];
Bart Van Asschea42d9852011-10-14 01:30:46 +00002341
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002342 ch->sess = transport_init_session(TARGET_PROT_NORMAL);
Dan Carpenter3af33632011-11-04 21:27:32 +03002343 if (IS_ERR(ch->sess)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302344 rej->reason = cpu_to_be32(
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002345 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002346 pr_debug("Failed to create session\n");
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002347 goto destroy_ib;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002348 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002349
2350try_again:
2351 se_acl = core_tpg_get_initiator_node_acl(&sport->port_tpg_1, p);
2352 if (!se_acl) {
2353 pr_info("Rejected login because no ACL has been"
2354 " configured yet for initiator %s.\n", ch->sess_name);
2355 /*
2356 * XXX: Hack to retry of ch->i_port_id without leading '0x'
2357 */
2358 if (p == &ch->sess_name[0]) {
2359 p += 2;
2360 goto try_again;
2361 }
2362 rej->reason = cpu_to_be32(
2363 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2364 transport_free_session(ch->sess);
2365 goto destroy_ib;
2366 }
2367 ch->sess->se_node_acl = se_acl;
2368
2369 transport_register_session(&sport->port_tpg_1, se_acl, ch->sess, ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002370
2371 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2372 ch->sess_name, ch->cm_id);
2373
2374 /* create srp_login_response */
2375 rsp->opcode = SRP_LOGIN_RSP;
2376 rsp->tag = req->tag;
2377 rsp->max_it_iu_len = req->req_it_iu_len;
2378 rsp->max_ti_iu_len = req->req_it_iu_len;
2379 ch->max_ti_iu_len = it_iu_len;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302380 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2381 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002382 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2383 atomic_set(&ch->req_lim, ch->rq_size);
2384 atomic_set(&ch->req_lim_delta, 0);
2385
2386 /* create cm reply */
2387 rep_param->qp_num = ch->qp->qp_num;
2388 rep_param->private_data = (void *)rsp;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08002389 rep_param->private_data_len = sizeof(*rsp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002390 rep_param->rnr_retry_count = 7;
2391 rep_param->flow_control = 1;
2392 rep_param->failover_accepted = 0;
2393 rep_param->srq = 1;
2394 rep_param->responder_resources = 4;
2395 rep_param->initiator_depth = 4;
2396
2397 ret = ib_send_cm_rep(cm_id, rep_param);
2398 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002399 pr_err("sending SRP_LOGIN_REQ response failed"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002400 " (error code = %d)\n", ret);
2401 goto release_channel;
2402 }
2403
2404 spin_lock_irq(&sdev->spinlock);
2405 list_add_tail(&ch->list, &sdev->rch_list);
2406 spin_unlock_irq(&sdev->spinlock);
2407
2408 goto out;
2409
2410release_channel:
2411 srpt_set_ch_state(ch, CH_RELEASING);
2412 transport_deregister_session_configfs(ch->sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002413 transport_deregister_session(ch->sess);
2414 ch->sess = NULL;
2415
2416destroy_ib:
2417 srpt_destroy_ch_ib(ch);
2418
2419free_ring:
2420 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2421 ch->sport->sdev, ch->rq_size,
2422 ch->rsp_size, DMA_TO_DEVICE);
2423free_ch:
2424 kfree(ch);
2425
2426reject:
2427 rej->opcode = SRP_LOGIN_REJ;
2428 rej->tag = req->tag;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302429 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2430 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002431
2432 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08002433 (void *)rej, sizeof(*rej));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002434
2435out:
2436 kfree(rep_param);
2437 kfree(rsp);
2438 kfree(rej);
2439
2440 return ret;
2441}
2442
2443static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2444{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002445 pr_info("Received IB REJ for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002446 srpt_drain_channel(cm_id);
2447}
2448
2449/**
2450 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2451 *
2452 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2453 * and that the recipient may begin transmitting (RTU = ready to use).
2454 */
2455static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2456{
2457 struct srpt_rdma_ch *ch;
2458 int ret;
2459
2460 ch = srpt_find_channel(cm_id->context, cm_id);
2461 BUG_ON(!ch);
2462
2463 if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
2464 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2465
2466 ret = srpt_ch_qp_rts(ch, ch->qp);
2467
2468 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2469 wait_list) {
2470 list_del(&ioctx->wait_list);
2471 srpt_handle_new_iu(ch, ioctx, NULL);
2472 }
2473 if (ret)
2474 srpt_close_ch(ch);
2475 }
2476}
2477
2478static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2479{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002480 pr_info("Received IB TimeWait exit for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002481 srpt_drain_channel(cm_id);
2482}
2483
2484static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2485{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002486 pr_info("Received IB REP error for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002487 srpt_drain_channel(cm_id);
2488}
2489
2490/**
2491 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2492 */
2493static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2494{
2495 struct srpt_rdma_ch *ch;
2496 unsigned long flags;
2497 bool send_drep = false;
2498
2499 ch = srpt_find_channel(cm_id->context, cm_id);
2500 BUG_ON(!ch);
2501
2502 pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
2503
2504 spin_lock_irqsave(&ch->spinlock, flags);
2505 switch (ch->state) {
2506 case CH_CONNECTING:
2507 case CH_LIVE:
2508 send_drep = true;
2509 ch->state = CH_DISCONNECTING;
2510 break;
2511 case CH_DISCONNECTING:
2512 case CH_DRAINING:
2513 case CH_RELEASING:
2514 WARN(true, "unexpected channel state %d\n", ch->state);
2515 break;
2516 }
2517 spin_unlock_irqrestore(&ch->spinlock, flags);
2518
2519 if (send_drep) {
2520 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002521 pr_err("Sending IB DREP failed.\n");
2522 pr_info("Received DREQ and sent DREP for session %s.\n",
2523 ch->sess_name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002524 }
2525}
2526
2527/**
2528 * srpt_cm_drep_recv() - Process reception of a DREP message.
2529 */
2530static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2531{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002532 pr_info("Received InfiniBand DREP message for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002533 srpt_drain_channel(cm_id);
2534}
2535
2536/**
2537 * srpt_cm_handler() - IB connection manager callback function.
2538 *
2539 * A non-zero return value will cause the caller destroy the CM ID.
2540 *
2541 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2542 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2543 * a non-zero value in any other case will trigger a race with the
2544 * ib_destroy_cm_id() call in srpt_release_channel().
2545 */
2546static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2547{
2548 int ret;
2549
2550 ret = 0;
2551 switch (event->event) {
2552 case IB_CM_REQ_RECEIVED:
2553 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2554 event->private_data);
2555 break;
2556 case IB_CM_REJ_RECEIVED:
2557 srpt_cm_rej_recv(cm_id);
2558 break;
2559 case IB_CM_RTU_RECEIVED:
2560 case IB_CM_USER_ESTABLISHED:
2561 srpt_cm_rtu_recv(cm_id);
2562 break;
2563 case IB_CM_DREQ_RECEIVED:
2564 srpt_cm_dreq_recv(cm_id);
2565 break;
2566 case IB_CM_DREP_RECEIVED:
2567 srpt_cm_drep_recv(cm_id);
2568 break;
2569 case IB_CM_TIMEWAIT_EXIT:
2570 srpt_cm_timewait_exit(cm_id);
2571 break;
2572 case IB_CM_REP_ERROR:
2573 srpt_cm_rep_error(cm_id);
2574 break;
2575 case IB_CM_DREQ_ERROR:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002576 pr_info("Received IB DREQ ERROR event.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002577 break;
2578 case IB_CM_MRA_RECEIVED:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002579 pr_info("Received IB MRA event\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002580 break;
2581 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002582 pr_err("received unrecognized IB CM event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002583 break;
2584 }
2585
2586 return ret;
2587}
2588
2589/**
2590 * srpt_perform_rdmas() - Perform IB RDMA.
2591 *
2592 * Returns zero upon success or a negative number upon failure.
2593 */
2594static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2595 struct srpt_send_ioctx *ioctx)
2596{
Bart Van Asschea42d9852011-10-14 01:30:46 +00002597 struct ib_send_wr *bad_wr;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002598 int sq_wr_avail, ret, i;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002599 enum dma_data_direction dir;
2600 const int n_rdma = ioctx->n_rdma;
2601
2602 dir = ioctx->cmd.data_direction;
2603 if (dir == DMA_TO_DEVICE) {
2604 /* write */
2605 ret = -ENOMEM;
2606 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2607 if (sq_wr_avail < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002608 pr_warn("IB send queue full (needed %d)\n",
2609 n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002610 goto out;
2611 }
2612 }
2613
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002614 for (i = 0; i < n_rdma; i++) {
2615 struct ib_send_wr *wr = &ioctx->rdma_wrs[i].wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002616
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002617 wr->opcode = (dir == DMA_FROM_DEVICE) ?
2618 IB_WR_RDMA_WRITE : IB_WR_RDMA_READ;
2619
2620 if (i == n_rdma - 1) {
2621 /* only get completion event for the last rdma read */
2622 if (dir == DMA_TO_DEVICE) {
2623 wr->send_flags = IB_SEND_SIGNALED;
2624 ioctx->rdma_cqe.done = srpt_rdma_read_done;
2625 } else {
2626 ioctx->rdma_cqe.done = srpt_rdma_write_done;
2627 }
2628 wr->wr_cqe = &ioctx->rdma_cqe;
2629 wr->next = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002630 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002631 wr->wr_cqe = NULL;
2632 wr->next = &ioctx->rdma_wrs[i + 1].wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002633 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00002634 }
2635
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002636 ret = ib_post_send(ch->qp, &ioctx->rdma_wrs->wr, &bad_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002637 if (ret)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002638 pr_err("%s[%d]: ib_post_send() returned %d for %d/%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002639 __func__, __LINE__, ret, i, n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002640out:
2641 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2642 atomic_add(n_rdma, &ch->sq_wr_avail);
2643 return ret;
2644}
2645
2646/**
2647 * srpt_xfer_data() - Start data transfer from initiator to target.
2648 */
2649static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2650 struct srpt_send_ioctx *ioctx)
2651{
2652 int ret;
2653
2654 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2655 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002656 pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002657 goto out;
2658 }
2659
2660 ret = srpt_perform_rdmas(ch, ioctx);
2661 if (ret) {
2662 if (ret == -EAGAIN || ret == -ENOMEM)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002663 pr_info("%s[%d] queue full -- ret=%d\n",
2664 __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002665 else
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002666 pr_err("%s[%d] fatal error -- ret=%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002667 __func__, __LINE__, ret);
2668 goto out_unmap;
2669 }
2670
2671out:
2672 return ret;
2673out_unmap:
2674 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2675 goto out;
2676}
2677
2678static int srpt_write_pending_status(struct se_cmd *se_cmd)
2679{
2680 struct srpt_send_ioctx *ioctx;
2681
2682 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2683 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2684}
2685
2686/*
2687 * srpt_write_pending() - Start data transfer from initiator to target (write).
2688 */
2689static int srpt_write_pending(struct se_cmd *se_cmd)
2690{
2691 struct srpt_rdma_ch *ch;
2692 struct srpt_send_ioctx *ioctx;
2693 enum srpt_command_state new_state;
2694 enum rdma_ch_state ch_state;
2695 int ret;
2696
2697 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2698
2699 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2700 WARN_ON(new_state == SRPT_STATE_DONE);
2701
2702 ch = ioctx->ch;
2703 BUG_ON(!ch);
2704
2705 ch_state = srpt_get_ch_state(ch);
2706 switch (ch_state) {
2707 case CH_CONNECTING:
2708 WARN(true, "unexpected channel state %d\n", ch_state);
2709 ret = -EINVAL;
2710 goto out;
2711 case CH_LIVE:
2712 break;
2713 case CH_DISCONNECTING:
2714 case CH_DRAINING:
2715 case CH_RELEASING:
2716 pr_debug("cmd with tag %lld: channel disconnecting\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002717 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002718 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2719 ret = -EINVAL;
2720 goto out;
2721 }
2722 ret = srpt_xfer_data(ch, ioctx);
2723
2724out:
2725 return ret;
2726}
2727
2728static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2729{
2730 switch (tcm_mgmt_status) {
2731 case TMR_FUNCTION_COMPLETE:
2732 return SRP_TSK_MGMT_SUCCESS;
2733 case TMR_FUNCTION_REJECTED:
2734 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2735 }
2736 return SRP_TSK_MGMT_FAILED;
2737}
2738
2739/**
2740 * srpt_queue_response() - Transmits the response to a SCSI command.
2741 *
2742 * Callback function called by the TCM core. Must not block since it can be
2743 * invoked on the context of the IB completion handler.
2744 */
Joern Engelb79fafa2013-07-03 11:22:17 -04002745static void srpt_queue_response(struct se_cmd *cmd)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002746{
2747 struct srpt_rdma_ch *ch;
2748 struct srpt_send_ioctx *ioctx;
2749 enum srpt_command_state state;
2750 unsigned long flags;
2751 int ret;
2752 enum dma_data_direction dir;
2753 int resp_len;
2754 u8 srp_tm_status;
2755
Bart Van Asschea42d9852011-10-14 01:30:46 +00002756 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2757 ch = ioctx->ch;
2758 BUG_ON(!ch);
2759
2760 spin_lock_irqsave(&ioctx->spinlock, flags);
2761 state = ioctx->state;
2762 switch (state) {
2763 case SRPT_STATE_NEW:
2764 case SRPT_STATE_DATA_IN:
2765 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2766 break;
2767 case SRPT_STATE_MGMT:
2768 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2769 break;
2770 default:
2771 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2772 ch, ioctx->ioctx.index, ioctx->state);
2773 break;
2774 }
2775 spin_unlock_irqrestore(&ioctx->spinlock, flags);
2776
2777 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
2778 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
2779 atomic_inc(&ch->req_lim_delta);
2780 srpt_abort_cmd(ioctx);
Joern Engelb79fafa2013-07-03 11:22:17 -04002781 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002782 }
2783
2784 dir = ioctx->cmd.data_direction;
2785
2786 /* For read commands, transfer the data to the initiator. */
2787 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
2788 !ioctx->queue_status_only) {
2789 ret = srpt_xfer_data(ch, ioctx);
2790 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002791 pr_err("xfer_data failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002792 ioctx->cmd.tag);
Joern Engelb79fafa2013-07-03 11:22:17 -04002793 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002794 }
2795 }
2796
2797 if (state != SRPT_STATE_MGMT)
Bart Van Assche649ee052015-04-14 13:26:44 +02002798 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002799 cmd->scsi_status);
2800 else {
2801 srp_tm_status
2802 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2803 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
Bart Van Assche649ee052015-04-14 13:26:44 +02002804 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002805 }
2806 ret = srpt_post_send(ch, ioctx, resp_len);
2807 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002808 pr_err("sending cmd response failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002809 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002810 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2811 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02002812 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002813 }
Joern Engelb79fafa2013-07-03 11:22:17 -04002814}
Bart Van Asschea42d9852011-10-14 01:30:46 +00002815
Joern Engelb79fafa2013-07-03 11:22:17 -04002816static int srpt_queue_data_in(struct se_cmd *cmd)
2817{
2818 srpt_queue_response(cmd);
2819 return 0;
2820}
2821
2822static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2823{
2824 srpt_queue_response(cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002825}
2826
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002827static void srpt_aborted_task(struct se_cmd *cmd)
2828{
2829 struct srpt_send_ioctx *ioctx = container_of(cmd,
2830 struct srpt_send_ioctx, cmd);
2831
2832 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
2833}
2834
Bart Van Asschea42d9852011-10-14 01:30:46 +00002835static int srpt_queue_status(struct se_cmd *cmd)
2836{
2837 struct srpt_send_ioctx *ioctx;
2838
2839 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2840 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2841 if (cmd->se_cmd_flags &
2842 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2843 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2844 ioctx->queue_status_only = true;
Joern Engelb79fafa2013-07-03 11:22:17 -04002845 srpt_queue_response(cmd);
2846 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002847}
2848
2849static void srpt_refresh_port_work(struct work_struct *work)
2850{
2851 struct srpt_port *sport = container_of(work, struct srpt_port, work);
2852
2853 srpt_refresh_port(sport);
2854}
2855
2856static int srpt_ch_list_empty(struct srpt_device *sdev)
2857{
2858 int res;
2859
2860 spin_lock_irq(&sdev->spinlock);
2861 res = list_empty(&sdev->rch_list);
2862 spin_unlock_irq(&sdev->spinlock);
2863
2864 return res;
2865}
2866
2867/**
2868 * srpt_release_sdev() - Free the channel resources associated with a target.
2869 */
2870static int srpt_release_sdev(struct srpt_device *sdev)
2871{
2872 struct srpt_rdma_ch *ch, *tmp_ch;
2873 int res;
2874
2875 WARN_ON_ONCE(irqs_disabled());
2876
2877 BUG_ON(!sdev);
2878
2879 spin_lock_irq(&sdev->spinlock);
2880 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
2881 __srpt_close_ch(ch);
2882 spin_unlock_irq(&sdev->spinlock);
2883
2884 res = wait_event_interruptible(sdev->ch_releaseQ,
2885 srpt_ch_list_empty(sdev));
2886 if (res)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002887 pr_err("%s: interrupted.\n", __func__);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002888
2889 return 0;
2890}
2891
2892static struct srpt_port *__srpt_lookup_port(const char *name)
2893{
2894 struct ib_device *dev;
2895 struct srpt_device *sdev;
2896 struct srpt_port *sport;
2897 int i;
2898
2899 list_for_each_entry(sdev, &srpt_dev_list, list) {
2900 dev = sdev->device;
2901 if (!dev)
2902 continue;
2903
2904 for (i = 0; i < dev->phys_port_cnt; i++) {
2905 sport = &sdev->port[i];
2906
2907 if (!strcmp(sport->port_guid, name))
2908 return sport;
2909 }
2910 }
2911
2912 return NULL;
2913}
2914
2915static struct srpt_port *srpt_lookup_port(const char *name)
2916{
2917 struct srpt_port *sport;
2918
2919 spin_lock(&srpt_dev_lock);
2920 sport = __srpt_lookup_port(name);
2921 spin_unlock(&srpt_dev_lock);
2922
2923 return sport;
2924}
2925
2926/**
2927 * srpt_add_one() - Infiniband device addition callback function.
2928 */
2929static void srpt_add_one(struct ib_device *device)
2930{
2931 struct srpt_device *sdev;
2932 struct srpt_port *sport;
2933 struct ib_srq_init_attr srq_attr;
2934 int i;
2935
2936 pr_debug("device = %p, device->dma_ops = %p\n", device,
2937 device->dma_ops);
2938
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08002939 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002940 if (!sdev)
2941 goto err;
2942
2943 sdev->device = device;
2944 INIT_LIST_HEAD(&sdev->rch_list);
2945 init_waitqueue_head(&sdev->ch_releaseQ);
2946 spin_lock_init(&sdev->spinlock);
2947
Bart Van Asschea42d9852011-10-14 01:30:46 +00002948 sdev->pd = ib_alloc_pd(device);
2949 if (IS_ERR(sdev->pd))
2950 goto free_dev;
2951
Or Gerlitz4a061b22015-12-18 10:59:46 +02002952 sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002953
2954 srq_attr.event_handler = srpt_srq_event;
2955 srq_attr.srq_context = (void *)sdev;
2956 srq_attr.attr.max_wr = sdev->srq_size;
2957 srq_attr.attr.max_sge = 1;
2958 srq_attr.attr.srq_limit = 0;
Roland Dreier6f360332012-04-12 07:51:08 -07002959 srq_attr.srq_type = IB_SRQT_BASIC;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002960
2961 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
2962 if (IS_ERR(sdev->srq))
Jason Gunthorpe5a783952015-07-30 17:22:24 -06002963 goto err_pd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002964
2965 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
Or Gerlitz4a061b22015-12-18 10:59:46 +02002966 __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002967 device->name);
2968
2969 if (!srpt_service_guid)
2970 srpt_service_guid = be64_to_cpu(device->node_guid);
2971
2972 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
2973 if (IS_ERR(sdev->cm_id))
2974 goto err_srq;
2975
2976 /* print out target login information */
2977 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
2978 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
2979 srpt_service_guid, srpt_service_guid);
2980
2981 /*
2982 * We do not have a consistent service_id (ie. also id_ext of target_id)
2983 * to identify this target. We currently use the guid of the first HCA
2984 * in the system as service_id; therefore, the target_id will change
2985 * if this HCA is gone bad and replaced by different HCA
2986 */
Haggai Eran73fec7f2015-07-30 17:50:26 +03002987 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
Bart Van Asschea42d9852011-10-14 01:30:46 +00002988 goto err_cm;
2989
2990 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
2991 srpt_event_handler);
2992 if (ib_register_event_handler(&sdev->event_handler))
2993 goto err_cm;
2994
2995 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
2996 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
2997 sizeof(*sdev->ioctx_ring[0]),
2998 srp_max_req_size, DMA_FROM_DEVICE);
2999 if (!sdev->ioctx_ring)
3000 goto err_event;
3001
3002 for (i = 0; i < sdev->srq_size; ++i)
3003 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
3004
Roland Dreierf2250662012-02-02 12:55:58 -08003005 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
Bart Van Asschea42d9852011-10-14 01:30:46 +00003006
3007 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3008 sport = &sdev->port[i - 1];
3009 sport->sdev = sdev;
3010 sport->port = i;
3011 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3012 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3013 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3014 INIT_WORK(&sport->work, srpt_refresh_port_work);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003015
3016 if (srpt_refresh_port(sport)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003017 pr_err("MAD registration failed for %s-%d.\n",
Bart Van Asschef68cba4e92016-02-11 11:04:20 -08003018 sdev->device->name, i);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003019 goto err_ring;
3020 }
3021 snprintf(sport->port_guid, sizeof(sport->port_guid),
3022 "0x%016llx%016llx",
3023 be64_to_cpu(sport->gid.global.subnet_prefix),
3024 be64_to_cpu(sport->gid.global.interface_id));
3025 }
3026
3027 spin_lock(&srpt_dev_lock);
3028 list_add_tail(&sdev->list, &srpt_dev_list);
3029 spin_unlock(&srpt_dev_lock);
3030
3031out:
3032 ib_set_client_data(device, &srpt_client, sdev);
3033 pr_debug("added %s.\n", device->name);
3034 return;
3035
3036err_ring:
3037 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3038 sdev->srq_size, srp_max_req_size,
3039 DMA_FROM_DEVICE);
3040err_event:
3041 ib_unregister_event_handler(&sdev->event_handler);
3042err_cm:
3043 ib_destroy_cm_id(sdev->cm_id);
3044err_srq:
3045 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003046err_pd:
3047 ib_dealloc_pd(sdev->pd);
3048free_dev:
3049 kfree(sdev);
3050err:
3051 sdev = NULL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003052 pr_info("%s(%s) failed.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003053 goto out;
3054}
3055
3056/**
3057 * srpt_remove_one() - InfiniBand device removal callback function.
3058 */
Haggai Eran7c1eb452015-07-30 17:50:14 +03003059static void srpt_remove_one(struct ib_device *device, void *client_data)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003060{
Haggai Eran7c1eb452015-07-30 17:50:14 +03003061 struct srpt_device *sdev = client_data;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003062 int i;
3063
Bart Van Asschea42d9852011-10-14 01:30:46 +00003064 if (!sdev) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003065 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003066 return;
3067 }
3068
3069 srpt_unregister_mad_agent(sdev);
3070
3071 ib_unregister_event_handler(&sdev->event_handler);
3072
3073 /* Cancel any work queued by the just unregistered IB event handler. */
3074 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3075 cancel_work_sync(&sdev->port[i].work);
3076
3077 ib_destroy_cm_id(sdev->cm_id);
3078
3079 /*
3080 * Unregistering a target must happen after destroying sdev->cm_id
3081 * such that no new SRP_LOGIN_REQ information units can arrive while
3082 * destroying the target.
3083 */
3084 spin_lock(&srpt_dev_lock);
3085 list_del(&sdev->list);
3086 spin_unlock(&srpt_dev_lock);
3087 srpt_release_sdev(sdev);
3088
3089 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003090 ib_dealloc_pd(sdev->pd);
3091
3092 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3093 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
3094 sdev->ioctx_ring = NULL;
3095 kfree(sdev);
3096}
3097
3098static struct ib_client srpt_client = {
3099 .name = DRV_NAME,
3100 .add = srpt_add_one,
3101 .remove = srpt_remove_one
3102};
3103
3104static int srpt_check_true(struct se_portal_group *se_tpg)
3105{
3106 return 1;
3107}
3108
3109static int srpt_check_false(struct se_portal_group *se_tpg)
3110{
3111 return 0;
3112}
3113
3114static char *srpt_get_fabric_name(void)
3115{
3116 return "srpt";
3117}
3118
Bart Van Asschea42d9852011-10-14 01:30:46 +00003119static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3120{
3121 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3122
3123 return sport->port_guid;
3124}
3125
3126static u16 srpt_get_tag(struct se_portal_group *tpg)
3127{
3128 return 1;
3129}
3130
Bart Van Asschea42d9852011-10-14 01:30:46 +00003131static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3132{
3133 return 1;
3134}
3135
3136static void srpt_release_cmd(struct se_cmd *se_cmd)
3137{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08003138 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3139 struct srpt_send_ioctx, cmd);
3140 struct srpt_rdma_ch *ch = ioctx->ch;
3141 unsigned long flags;
3142
3143 WARN_ON(ioctx->state != SRPT_STATE_DONE);
3144 WARN_ON(ioctx->mapped_sg_count != 0);
3145
3146 if (ioctx->n_rbuf > 1) {
3147 kfree(ioctx->rbufs);
3148 ioctx->rbufs = NULL;
3149 ioctx->n_rbuf = 0;
3150 }
3151
3152 spin_lock_irqsave(&ch->spinlock, flags);
3153 list_add(&ioctx->free_list, &ch->free_list);
3154 spin_unlock_irqrestore(&ch->spinlock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003155}
3156
3157/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003158 * srpt_close_session() - Forcibly close a session.
3159 *
3160 * Callback function invoked by the TCM core to clean up sessions associated
3161 * with a node ACL when the user invokes
3162 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3163 */
3164static void srpt_close_session(struct se_session *se_sess)
3165{
3166 DECLARE_COMPLETION_ONSTACK(release_done);
3167 struct srpt_rdma_ch *ch;
3168 struct srpt_device *sdev;
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003169 unsigned long res;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003170
3171 ch = se_sess->fabric_sess_ptr;
3172 WARN_ON(ch->sess != se_sess);
3173
3174 pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
3175
3176 sdev = ch->sport->sdev;
3177 spin_lock_irq(&sdev->spinlock);
3178 BUG_ON(ch->release_done);
3179 ch->release_done = &release_done;
3180 __srpt_close_ch(ch);
3181 spin_unlock_irq(&sdev->spinlock);
3182
3183 res = wait_for_completion_timeout(&release_done, 60 * HZ);
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003184 WARN_ON(res == 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003185}
3186
3187/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003188 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3189 *
3190 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3191 * This object represents an arbitrary integer used to uniquely identify a
3192 * particular attached remote initiator port to a particular SCSI target port
3193 * within a particular SCSI target device within a particular SCSI instance.
3194 */
3195static u32 srpt_sess_get_index(struct se_session *se_sess)
3196{
3197 return 0;
3198}
3199
3200static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3201{
3202}
3203
Bart Van Asschea42d9852011-10-14 01:30:46 +00003204/* Note: only used from inside debug printk's by the TCM core. */
3205static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3206{
3207 struct srpt_send_ioctx *ioctx;
3208
3209 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3210 return srpt_get_cmd_state(ioctx);
3211}
3212
Bart Van Asschea42d9852011-10-14 01:30:46 +00003213/**
3214 * srpt_parse_i_port_id() - Parse an initiator port ID.
3215 * @name: ASCII representation of a 128-bit initiator port ID.
3216 * @i_port_id: Binary 128-bit port ID.
3217 */
3218static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3219{
3220 const char *p;
3221 unsigned len, count, leading_zero_bytes;
3222 int ret, rc;
3223
3224 p = name;
Rasmus Villemoesb60459f2014-10-13 15:54:46 -07003225 if (strncasecmp(p, "0x", 2) == 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003226 p += 2;
3227 ret = -EINVAL;
3228 len = strlen(p);
3229 if (len % 2)
3230 goto out;
3231 count = min(len / 2, 16U);
3232 leading_zero_bytes = 16 - count;
3233 memset(i_port_id, 0, leading_zero_bytes);
3234 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3235 if (rc < 0)
3236 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3237 ret = 0;
3238out:
3239 return ret;
3240}
3241
3242/*
3243 * configfs callback function invoked for
3244 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3245 */
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003246static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003247{
Bart Van Asschea42d9852011-10-14 01:30:46 +00003248 u8 i_port_id[16];
3249
3250 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003251 pr_err("invalid initiator port ID %s\n", name);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003252 return -EINVAL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003253 }
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003254 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003255}
3256
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003257static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
3258 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003259{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003260 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003261 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3262
3263 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3264}
3265
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003266static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
3267 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003268{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003269 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003270 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3271 unsigned long val;
3272 int ret;
3273
Jingoo Han9d8abf42014-02-05 11:22:05 +09003274 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003275 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003276 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003277 return -EINVAL;
3278 }
3279 if (val > MAX_SRPT_RDMA_SIZE) {
3280 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3281 MAX_SRPT_RDMA_SIZE);
3282 return -EINVAL;
3283 }
3284 if (val < DEFAULT_MAX_RDMA_SIZE) {
3285 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3286 val, DEFAULT_MAX_RDMA_SIZE);
3287 return -EINVAL;
3288 }
3289 sport->port_attrib.srp_max_rdma_size = val;
3290
3291 return count;
3292}
3293
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003294static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
3295 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003296{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003297 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003298 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3299
3300 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3301}
3302
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003303static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
3304 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003305{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003306 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003307 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3308 unsigned long val;
3309 int ret;
3310
Jingoo Han9d8abf42014-02-05 11:22:05 +09003311 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003312 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003313 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003314 return -EINVAL;
3315 }
3316 if (val > MAX_SRPT_RSP_SIZE) {
3317 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3318 MAX_SRPT_RSP_SIZE);
3319 return -EINVAL;
3320 }
3321 if (val < MIN_MAX_RSP_SIZE) {
3322 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3323 MIN_MAX_RSP_SIZE);
3324 return -EINVAL;
3325 }
3326 sport->port_attrib.srp_max_rsp_size = val;
3327
3328 return count;
3329}
3330
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003331static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
3332 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003333{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003334 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003335 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3336
3337 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3338}
3339
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003340static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
3341 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003342{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003343 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003344 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3345 unsigned long val;
3346 int ret;
3347
Jingoo Han9d8abf42014-02-05 11:22:05 +09003348 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003349 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003350 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003351 return -EINVAL;
3352 }
3353 if (val > MAX_SRPT_SRQ_SIZE) {
3354 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3355 MAX_SRPT_SRQ_SIZE);
3356 return -EINVAL;
3357 }
3358 if (val < MIN_SRPT_SRQ_SIZE) {
3359 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3360 MIN_SRPT_SRQ_SIZE);
3361 return -EINVAL;
3362 }
3363 sport->port_attrib.srp_sq_size = val;
3364
3365 return count;
3366}
3367
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003368CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
3369CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
3370CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003371
3372static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003373 &srpt_tpg_attrib_attr_srp_max_rdma_size,
3374 &srpt_tpg_attrib_attr_srp_max_rsp_size,
3375 &srpt_tpg_attrib_attr_srp_sq_size,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003376 NULL,
3377};
3378
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003379static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003380{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003381 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003382 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3383
3384 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3385}
3386
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003387static ssize_t srpt_tpg_enable_store(struct config_item *item,
3388 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003389{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003390 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003391 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3392 unsigned long tmp;
3393 int ret;
3394
Jingoo Han9d8abf42014-02-05 11:22:05 +09003395 ret = kstrtoul(page, 0, &tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003396 if (ret < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003397 pr_err("Unable to extract srpt_tpg_store_enable\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003398 return -EINVAL;
3399 }
3400
3401 if ((tmp != 0) && (tmp != 1)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003402 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003403 return -EINVAL;
3404 }
3405 if (tmp == 1)
3406 sport->enabled = true;
3407 else
3408 sport->enabled = false;
3409
3410 return count;
3411}
3412
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003413CONFIGFS_ATTR(srpt_tpg_, enable);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003414
3415static struct configfs_attribute *srpt_tpg_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003416 &srpt_tpg_attr_enable,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003417 NULL,
3418};
3419
3420/**
3421 * configfs callback invoked for
3422 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3423 */
3424static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3425 struct config_group *group,
3426 const char *name)
3427{
3428 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3429 int res;
3430
3431 /* Initialize sport->port_wwn and sport->port_tpg_1 */
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07003432 res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003433 if (res)
3434 return ERR_PTR(res);
3435
3436 return &sport->port_tpg_1;
3437}
3438
3439/**
3440 * configfs callback invoked for
3441 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3442 */
3443static void srpt_drop_tpg(struct se_portal_group *tpg)
3444{
3445 struct srpt_port *sport = container_of(tpg,
3446 struct srpt_port, port_tpg_1);
3447
3448 sport->enabled = false;
3449 core_tpg_deregister(&sport->port_tpg_1);
3450}
3451
3452/**
3453 * configfs callback invoked for
3454 * mkdir /sys/kernel/config/target/$driver/$port
3455 */
3456static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3457 struct config_group *group,
3458 const char *name)
3459{
3460 struct srpt_port *sport;
3461 int ret;
3462
3463 sport = srpt_lookup_port(name);
3464 pr_debug("make_tport(%s)\n", name);
3465 ret = -EINVAL;
3466 if (!sport)
3467 goto err;
3468
3469 return &sport->port_wwn;
3470
3471err:
3472 return ERR_PTR(ret);
3473}
3474
3475/**
3476 * configfs callback invoked for
3477 * rmdir /sys/kernel/config/target/$driver/$port
3478 */
3479static void srpt_drop_tport(struct se_wwn *wwn)
3480{
3481 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3482
3483 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3484}
3485
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003486static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003487{
3488 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3489}
3490
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003491CONFIGFS_ATTR_RO(srpt_wwn_, version);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003492
3493static struct configfs_attribute *srpt_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003494 &srpt_wwn_attr_version,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003495 NULL,
3496};
3497
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003498static const struct target_core_fabric_ops srpt_template = {
3499 .module = THIS_MODULE,
3500 .name = "srpt",
Bart Van Asschea42d9852011-10-14 01:30:46 +00003501 .get_fabric_name = srpt_get_fabric_name,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003502 .tpg_get_wwn = srpt_get_fabric_wwn,
3503 .tpg_get_tag = srpt_get_tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003504 .tpg_check_demo_mode = srpt_check_false,
3505 .tpg_check_demo_mode_cache = srpt_check_true,
3506 .tpg_check_demo_mode_write_protect = srpt_check_true,
3507 .tpg_check_prod_mode_write_protect = srpt_check_false,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003508 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3509 .release_cmd = srpt_release_cmd,
3510 .check_stop_free = srpt_check_stop_free,
3511 .shutdown_session = srpt_shutdown_session,
3512 .close_session = srpt_close_session,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003513 .sess_get_index = srpt_sess_get_index,
3514 .sess_get_initiator_sid = NULL,
3515 .write_pending = srpt_write_pending,
3516 .write_pending_status = srpt_write_pending_status,
3517 .set_default_node_attributes = srpt_set_default_node_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003518 .get_cmd_state = srpt_get_tcm_cmd_state,
Joern Engelb79fafa2013-07-03 11:22:17 -04003519 .queue_data_in = srpt_queue_data_in,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003520 .queue_status = srpt_queue_status,
Joern Engelb79fafa2013-07-03 11:22:17 -04003521 .queue_tm_rsp = srpt_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003522 .aborted_task = srpt_aborted_task,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003523 /*
3524 * Setup function pointers for generic logic in
3525 * target_core_fabric_configfs.c
3526 */
3527 .fabric_make_wwn = srpt_make_tport,
3528 .fabric_drop_wwn = srpt_drop_tport,
3529 .fabric_make_tpg = srpt_make_tpg,
3530 .fabric_drop_tpg = srpt_drop_tpg,
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003531 .fabric_init_nodeacl = srpt_init_nodeacl,
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003532
3533 .tfc_wwn_attrs = srpt_wwn_attrs,
3534 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3535 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003536};
3537
3538/**
3539 * srpt_init_module() - Kernel module initialization.
3540 *
3541 * Note: Since ib_register_client() registers callback functions, and since at
3542 * least one of these callback functions (srpt_add_one()) calls target core
3543 * functions, this driver must be registered with the target core before
3544 * ib_register_client() is called.
3545 */
3546static int __init srpt_init_module(void)
3547{
3548 int ret;
3549
3550 ret = -EINVAL;
3551 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003552 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003553 " srp_max_req_size -- must be at least %d.\n",
3554 srp_max_req_size, MIN_MAX_REQ_SIZE);
3555 goto out;
3556 }
3557
3558 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3559 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003560 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003561 " srpt_srq_size -- must be in the range [%d..%d].\n",
3562 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3563 goto out;
3564 }
3565
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003566 ret = target_register_template(&srpt_template);
3567 if (ret)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003568 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003569
3570 ret = ib_register_client(&srpt_client);
3571 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003572 pr_err("couldn't register IB client\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003573 goto out_unregister_target;
3574 }
3575
3576 return 0;
3577
3578out_unregister_target:
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003579 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003580out:
3581 return ret;
3582}
3583
3584static void __exit srpt_cleanup_module(void)
3585{
3586 ib_unregister_client(&srpt_client);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003587 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003588}
3589
3590module_init(srpt_init_module);
3591module_exit(srpt_cleanup_module);