]> git.ipfire.org Git - people/arne_f/kernel.git/blame - drivers/infiniband/ulp/srpt/ib_srpt.c
target: Add TFO->abort_task for aborted task resources release
[people/arne_f/kernel.git] / drivers / infiniband / ulp / srpt / ib_srpt.c
CommitLineData
a42d985b
BVA
1/*
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>
44#include <scsi/scsi_tcq.h>
45#include <target/configfs_macros.h>
46#include <target/target_core_base.h>
47#include <target/target_core_fabric_configfs.h>
48#include <target/target_core_fabric.h>
49#include <target/target_core_configfs.h>
50#include "ib_srpt.h"
51
52/* Name of this kernel module. */
53#define DRV_NAME "ib_srpt"
54#define DRV_VERSION "2.0.0"
55#define DRV_RELDATE "2011-02-14"
56
57#define SRPT_ID_STRING "Linux SRP target"
58
59#undef pr_fmt
60#define pr_fmt(fmt) DRV_NAME " " fmt
61
62MODULE_AUTHOR("Vu Pham and Bart Van Assche");
63MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
64 "v" DRV_VERSION " (" DRV_RELDATE ")");
65MODULE_LICENSE("Dual BSD/GPL");
66
67/*
68 * Global Variables
69 */
70
71static u64 srpt_service_guid;
486d8b9f
RD
72static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
73static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
a42d985b
BVA
74
75static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
76module_param(srp_max_req_size, int, 0444);
77MODULE_PARM_DESC(srp_max_req_size,
78 "Maximum size of SRP request messages in bytes.");
79
80static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
81module_param(srpt_srq_size, int, 0444);
82MODULE_PARM_DESC(srpt_srq_size,
83 "Shared receive queue (SRQ) size.");
84
85static int srpt_get_u64_x(char *buffer, struct kernel_param *kp)
86{
87 return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
88}
89module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
90 0444);
91MODULE_PARM_DESC(srpt_service_guid,
92 "Using this value for ioc_guid, id_ext, and cm_listen_id"
93 " instead of using the node_guid of the first HCA.");
94
95static struct ib_client srpt_client;
96static struct target_fabric_configfs *srpt_target;
97static void srpt_release_channel(struct srpt_rdma_ch *ch);
98static int srpt_queue_status(struct se_cmd *cmd);
99
100/**
101 * opposite_dma_dir() - Swap DMA_TO_DEVICE and DMA_FROM_DEVICE.
102 */
103static inline
104enum dma_data_direction opposite_dma_dir(enum dma_data_direction dir)
105{
106 switch (dir) {
107 case DMA_TO_DEVICE: return DMA_FROM_DEVICE;
108 case DMA_FROM_DEVICE: return DMA_TO_DEVICE;
109 default: return dir;
110 }
111}
112
113/**
114 * srpt_sdev_name() - Return the name associated with the HCA.
115 *
116 * Examples are ib0, ib1, ...
117 */
118static inline const char *srpt_sdev_name(struct srpt_device *sdev)
119{
120 return sdev->device->name;
121}
122
123static enum rdma_ch_state srpt_get_ch_state(struct srpt_rdma_ch *ch)
124{
125 unsigned long flags;
126 enum rdma_ch_state state;
127
128 spin_lock_irqsave(&ch->spinlock, flags);
129 state = ch->state;
130 spin_unlock_irqrestore(&ch->spinlock, flags);
131 return state;
132}
133
134static enum rdma_ch_state
135srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new_state)
136{
137 unsigned long flags;
138 enum rdma_ch_state prev;
139
140 spin_lock_irqsave(&ch->spinlock, flags);
141 prev = ch->state;
142 ch->state = new_state;
143 spin_unlock_irqrestore(&ch->spinlock, flags);
144 return prev;
145}
146
147/**
148 * srpt_test_and_set_ch_state() - Test and set the channel state.
149 *
150 * Returns true if and only if the channel state has been set to the new state.
151 */
152static bool
153srpt_test_and_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state old,
154 enum rdma_ch_state new)
155{
156 unsigned long flags;
157 enum rdma_ch_state prev;
158
159 spin_lock_irqsave(&ch->spinlock, flags);
160 prev = ch->state;
161 if (prev == old)
162 ch->state = new;
163 spin_unlock_irqrestore(&ch->spinlock, flags);
164 return prev == old;
165}
166
167/**
168 * srpt_event_handler() - Asynchronous IB event callback function.
169 *
170 * Callback function called by the InfiniBand core when an asynchronous IB
171 * event occurs. This callback may occur in interrupt context. See also
172 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
173 * Architecture Specification.
174 */
175static void srpt_event_handler(struct ib_event_handler *handler,
176 struct ib_event *event)
177{
178 struct srpt_device *sdev;
179 struct srpt_port *sport;
180
181 sdev = ib_get_client_data(event->device, &srpt_client);
182 if (!sdev || sdev->device != event->device)
183 return;
184
185 pr_debug("ASYNC event= %d on device= %s\n", event->event,
186 srpt_sdev_name(sdev));
187
188 switch (event->event) {
189 case IB_EVENT_PORT_ERR:
190 if (event->element.port_num <= sdev->device->phys_port_cnt) {
191 sport = &sdev->port[event->element.port_num - 1];
192 sport->lid = 0;
193 sport->sm_lid = 0;
194 }
195 break;
196 case IB_EVENT_PORT_ACTIVE:
197 case IB_EVENT_LID_CHANGE:
198 case IB_EVENT_PKEY_CHANGE:
199 case IB_EVENT_SM_CHANGE:
200 case IB_EVENT_CLIENT_REREGISTER:
201 /* Refresh port data asynchronously. */
202 if (event->element.port_num <= sdev->device->phys_port_cnt) {
203 sport = &sdev->port[event->element.port_num - 1];
204 if (!sport->lid && !sport->sm_lid)
205 schedule_work(&sport->work);
206 }
207 break;
208 default:
209 printk(KERN_ERR "received unrecognized IB event %d\n",
210 event->event);
211 break;
212 }
213}
214
215/**
216 * srpt_srq_event() - SRQ event callback function.
217 */
218static void srpt_srq_event(struct ib_event *event, void *ctx)
219{
220 printk(KERN_INFO "SRQ event %d\n", event->event);
221}
222
223/**
224 * srpt_qp_event() - QP event callback function.
225 */
226static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
227{
228 pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
229 event->event, ch->cm_id, ch->sess_name, srpt_get_ch_state(ch));
230
231 switch (event->event) {
232 case IB_EVENT_COMM_EST:
233 ib_cm_notify(ch->cm_id, event->event);
234 break;
235 case IB_EVENT_QP_LAST_WQE_REACHED:
236 if (srpt_test_and_set_ch_state(ch, CH_DRAINING,
237 CH_RELEASING))
238 srpt_release_channel(ch);
239 else
240 pr_debug("%s: state %d - ignored LAST_WQE.\n",
241 ch->sess_name, srpt_get_ch_state(ch));
242 break;
243 default:
244 printk(KERN_ERR "received unrecognized IB QP event %d\n",
245 event->event);
246 break;
247 }
248}
249
250/**
251 * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
252 *
253 * @slot: one-based slot number.
254 * @value: four-bit value.
255 *
256 * Copies the lowest four bits of value in element slot of the array of four
257 * bit elements called c_list (controller list). The index slot is one-based.
258 */
259static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
260{
261 u16 id;
262 u8 tmp;
263
264 id = (slot - 1) / 2;
265 if (slot & 0x1) {
266 tmp = c_list[id] & 0xf;
267 c_list[id] = (value << 4) | tmp;
268 } else {
269 tmp = c_list[id] & 0xf0;
270 c_list[id] = (value & 0xf) | tmp;
271 }
272}
273
274/**
275 * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
276 *
277 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
278 * Specification.
279 */
280static void srpt_get_class_port_info(struct ib_dm_mad *mad)
281{
282 struct ib_class_port_info *cif;
283
284 cif = (struct ib_class_port_info *)mad->data;
285 memset(cif, 0, sizeof *cif);
286 cif->base_version = 1;
287 cif->class_version = 1;
288 cif->resp_time_value = 20;
289
290 mad->mad_hdr.status = 0;
291}
292
293/**
294 * srpt_get_iou() - Write IOUnitInfo to a management datagram.
295 *
296 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
297 * Specification. See also section B.7, table B.6 in the SRP r16a document.
298 */
299static void srpt_get_iou(struct ib_dm_mad *mad)
300{
301 struct ib_dm_iou_info *ioui;
302 u8 slot;
303 int i;
304
305 ioui = (struct ib_dm_iou_info *)mad->data;
306 ioui->change_id = __constant_cpu_to_be16(1);
307 ioui->max_controllers = 16;
308
309 /* set present for slot 1 and empty for the rest */
310 srpt_set_ioc(ioui->controller_list, 1, 1);
311 for (i = 1, slot = 2; i < 16; i++, slot++)
312 srpt_set_ioc(ioui->controller_list, slot, 0);
313
314 mad->mad_hdr.status = 0;
315}
316
317/**
318 * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
319 *
320 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
321 * Architecture Specification. See also section B.7, table B.7 in the SRP
322 * r16a document.
323 */
324static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
325 struct ib_dm_mad *mad)
326{
327 struct srpt_device *sdev = sport->sdev;
328 struct ib_dm_ioc_profile *iocp;
329
330 iocp = (struct ib_dm_ioc_profile *)mad->data;
331
332 if (!slot || slot > 16) {
333 mad->mad_hdr.status
334 = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
335 return;
336 }
337
338 if (slot > 2) {
339 mad->mad_hdr.status
340 = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
341 return;
342 }
343
344 memset(iocp, 0, sizeof *iocp);
345 strcpy(iocp->id_string, SRPT_ID_STRING);
346 iocp->guid = cpu_to_be64(srpt_service_guid);
347 iocp->vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
348 iocp->device_id = cpu_to_be32(sdev->dev_attr.vendor_part_id);
349 iocp->device_version = cpu_to_be16(sdev->dev_attr.hw_ver);
350 iocp->subsys_vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
351 iocp->subsys_device_id = 0x0;
352 iocp->io_class = __constant_cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
353 iocp->io_subclass = __constant_cpu_to_be16(SRP_IO_SUBCLASS);
354 iocp->protocol = __constant_cpu_to_be16(SRP_PROTOCOL);
355 iocp->protocol_version = __constant_cpu_to_be16(SRP_PROTOCOL_VERSION);
356 iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
357 iocp->rdma_read_depth = 4;
358 iocp->send_size = cpu_to_be32(srp_max_req_size);
359 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
360 1U << 24));
361 iocp->num_svc_entries = 1;
362 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
363 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
364
365 mad->mad_hdr.status = 0;
366}
367
368/**
369 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
370 *
371 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
372 * Specification. See also section B.7, table B.8 in the SRP r16a document.
373 */
374static void srpt_get_svc_entries(u64 ioc_guid,
375 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
376{
377 struct ib_dm_svc_entries *svc_entries;
378
379 WARN_ON(!ioc_guid);
380
381 if (!slot || slot > 16) {
382 mad->mad_hdr.status
383 = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
384 return;
385 }
386
387 if (slot > 2 || lo > hi || hi > 1) {
388 mad->mad_hdr.status
389 = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
390 return;
391 }
392
393 svc_entries = (struct ib_dm_svc_entries *)mad->data;
394 memset(svc_entries, 0, sizeof *svc_entries);
395 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
396 snprintf(svc_entries->service_entries[0].name,
397 sizeof(svc_entries->service_entries[0].name),
398 "%s%016llx",
399 SRP_SERVICE_NAME_PREFIX,
400 ioc_guid);
401
402 mad->mad_hdr.status = 0;
403}
404
405/**
406 * srpt_mgmt_method_get() - Process a received management datagram.
407 * @sp: source port through which the MAD has been received.
408 * @rq_mad: received MAD.
409 * @rsp_mad: response MAD.
410 */
411static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
412 struct ib_dm_mad *rsp_mad)
413{
414 u16 attr_id;
415 u32 slot;
416 u8 hi, lo;
417
418 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
419 switch (attr_id) {
420 case DM_ATTR_CLASS_PORT_INFO:
421 srpt_get_class_port_info(rsp_mad);
422 break;
423 case DM_ATTR_IOU_INFO:
424 srpt_get_iou(rsp_mad);
425 break;
426 case DM_ATTR_IOC_PROFILE:
427 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
428 srpt_get_ioc(sp, slot, rsp_mad);
429 break;
430 case DM_ATTR_SVC_ENTRIES:
431 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
432 hi = (u8) ((slot >> 8) & 0xff);
433 lo = (u8) (slot & 0xff);
434 slot = (u16) ((slot >> 16) & 0xffff);
435 srpt_get_svc_entries(srpt_service_guid,
436 slot, hi, lo, rsp_mad);
437 break;
438 default:
439 rsp_mad->mad_hdr.status =
440 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
441 break;
442 }
443}
444
445/**
446 * srpt_mad_send_handler() - Post MAD-send callback function.
447 */
448static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
449 struct ib_mad_send_wc *mad_wc)
450{
451 ib_destroy_ah(mad_wc->send_buf->ah);
452 ib_free_send_mad(mad_wc->send_buf);
453}
454
455/**
456 * srpt_mad_recv_handler() - MAD reception callback function.
457 */
458static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
459 struct ib_mad_recv_wc *mad_wc)
460{
461 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
462 struct ib_ah *ah;
463 struct ib_mad_send_buf *rsp;
464 struct ib_dm_mad *dm_mad;
465
466 if (!mad_wc || !mad_wc->recv_buf.mad)
467 return;
468
469 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
470 mad_wc->recv_buf.grh, mad_agent->port_num);
471 if (IS_ERR(ah))
472 goto err;
473
474 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
475
476 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
477 mad_wc->wc->pkey_index, 0,
478 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
479 GFP_KERNEL);
480 if (IS_ERR(rsp))
481 goto err_rsp;
482
483 rsp->ah = ah;
484
485 dm_mad = rsp->mad;
486 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof *dm_mad);
487 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
488 dm_mad->mad_hdr.status = 0;
489
490 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
491 case IB_MGMT_METHOD_GET:
492 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
493 break;
494 case IB_MGMT_METHOD_SET:
495 dm_mad->mad_hdr.status =
496 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
497 break;
498 default:
499 dm_mad->mad_hdr.status =
500 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
501 break;
502 }
503
504 if (!ib_post_send_mad(rsp, NULL)) {
505 ib_free_recv_mad(mad_wc);
506 /* will destroy_ah & free_send_mad in send completion */
507 return;
508 }
509
510 ib_free_send_mad(rsp);
511
512err_rsp:
513 ib_destroy_ah(ah);
514err:
515 ib_free_recv_mad(mad_wc);
516}
517
518/**
519 * srpt_refresh_port() - Configure a HCA port.
520 *
521 * Enable InfiniBand management datagram processing, update the cached sm_lid,
522 * lid and gid values, and register a callback function for processing MADs
523 * on the specified port.
524 *
525 * Note: It is safe to call this function more than once for the same port.
526 */
527static int srpt_refresh_port(struct srpt_port *sport)
528{
529 struct ib_mad_reg_req reg_req;
530 struct ib_port_modify port_modify;
531 struct ib_port_attr port_attr;
532 int ret;
533
534 memset(&port_modify, 0, sizeof port_modify);
535 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
536 port_modify.clr_port_cap_mask = 0;
537
538 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
539 if (ret)
540 goto err_mod_port;
541
542 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
543 if (ret)
544 goto err_query_port;
545
546 sport->sm_lid = port_attr.sm_lid;
547 sport->lid = port_attr.lid;
548
549 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid);
550 if (ret)
551 goto err_query_port;
552
553 if (!sport->mad_agent) {
554 memset(&reg_req, 0, sizeof reg_req);
555 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
556 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
557 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
558 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
559
560 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
561 sport->port,
562 IB_QPT_GSI,
563 &reg_req, 0,
564 srpt_mad_send_handler,
565 srpt_mad_recv_handler,
566 sport);
567 if (IS_ERR(sport->mad_agent)) {
568 ret = PTR_ERR(sport->mad_agent);
569 sport->mad_agent = NULL;
570 goto err_query_port;
571 }
572 }
573
574 return 0;
575
576err_query_port:
577
578 port_modify.set_port_cap_mask = 0;
579 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
580 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
581
582err_mod_port:
583
584 return ret;
585}
586
587/**
588 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
589 *
590 * Note: It is safe to call this function more than once for the same device.
591 */
592static void srpt_unregister_mad_agent(struct srpt_device *sdev)
593{
594 struct ib_port_modify port_modify = {
595 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
596 };
597 struct srpt_port *sport;
598 int i;
599
600 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
601 sport = &sdev->port[i - 1];
602 WARN_ON(sport->port != i);
603 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
604 printk(KERN_ERR "disabling MAD processing failed.\n");
605 if (sport->mad_agent) {
606 ib_unregister_mad_agent(sport->mad_agent);
607 sport->mad_agent = NULL;
608 }
609 }
610}
611
612/**
613 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
614 */
615static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
616 int ioctx_size, int dma_size,
617 enum dma_data_direction dir)
618{
619 struct srpt_ioctx *ioctx;
620
621 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
622 if (!ioctx)
623 goto err;
624
625 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
626 if (!ioctx->buf)
627 goto err_free_ioctx;
628
629 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
630 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
631 goto err_free_buf;
632
633 return ioctx;
634
635err_free_buf:
636 kfree(ioctx->buf);
637err_free_ioctx:
638 kfree(ioctx);
639err:
640 return NULL;
641}
642
643/**
644 * srpt_free_ioctx() - Free an SRPT I/O context structure.
645 */
646static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
647 int dma_size, enum dma_data_direction dir)
648{
649 if (!ioctx)
650 return;
651
652 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
653 kfree(ioctx->buf);
654 kfree(ioctx);
655}
656
657/**
658 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
659 * @sdev: Device to allocate the I/O context ring for.
660 * @ring_size: Number of elements in the I/O context ring.
661 * @ioctx_size: I/O context size.
662 * @dma_size: DMA buffer size.
663 * @dir: DMA data direction.
664 */
665static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
666 int ring_size, int ioctx_size,
667 int dma_size, enum dma_data_direction dir)
668{
669 struct srpt_ioctx **ring;
670 int i;
671
672 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
673 && ioctx_size != sizeof(struct srpt_send_ioctx));
674
675 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
676 if (!ring)
677 goto out;
678 for (i = 0; i < ring_size; ++i) {
679 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
680 if (!ring[i])
681 goto err;
682 ring[i]->index = i;
683 }
684 goto out;
685
686err:
687 while (--i >= 0)
688 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
689 kfree(ring);
715252d4 690 ring = NULL;
a42d985b
BVA
691out:
692 return ring;
693}
694
695/**
696 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
697 */
698static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
699 struct srpt_device *sdev, int ring_size,
700 int dma_size, enum dma_data_direction dir)
701{
702 int i;
703
704 for (i = 0; i < ring_size; ++i)
705 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
706 kfree(ioctx_ring);
707}
708
709/**
710 * srpt_get_cmd_state() - Get the state of a SCSI command.
711 */
712static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
713{
714 enum srpt_command_state state;
715 unsigned long flags;
716
717 BUG_ON(!ioctx);
718
719 spin_lock_irqsave(&ioctx->spinlock, flags);
720 state = ioctx->state;
721 spin_unlock_irqrestore(&ioctx->spinlock, flags);
722 return state;
723}
724
725/**
726 * srpt_set_cmd_state() - Set the state of a SCSI command.
727 *
728 * Does not modify the state of aborted commands. Returns the previous command
729 * state.
730 */
731static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
732 enum srpt_command_state new)
733{
734 enum srpt_command_state previous;
735 unsigned long flags;
736
737 BUG_ON(!ioctx);
738
739 spin_lock_irqsave(&ioctx->spinlock, flags);
740 previous = ioctx->state;
741 if (previous != SRPT_STATE_DONE)
742 ioctx->state = new;
743 spin_unlock_irqrestore(&ioctx->spinlock, flags);
744
745 return previous;
746}
747
748/**
749 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
750 *
751 * Returns true if and only if the previous command state was equal to 'old'.
752 */
753static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
754 enum srpt_command_state old,
755 enum srpt_command_state new)
756{
757 enum srpt_command_state previous;
758 unsigned long flags;
759
760 WARN_ON(!ioctx);
761 WARN_ON(old == SRPT_STATE_DONE);
762 WARN_ON(new == SRPT_STATE_NEW);
763
764 spin_lock_irqsave(&ioctx->spinlock, flags);
765 previous = ioctx->state;
766 if (previous == old)
767 ioctx->state = new;
768 spin_unlock_irqrestore(&ioctx->spinlock, flags);
769 return previous == old;
770}
771
772/**
773 * srpt_post_recv() - Post an IB receive request.
774 */
775static int srpt_post_recv(struct srpt_device *sdev,
776 struct srpt_recv_ioctx *ioctx)
777{
778 struct ib_sge list;
779 struct ib_recv_wr wr, *bad_wr;
780
781 BUG_ON(!sdev);
782 wr.wr_id = encode_wr_id(SRPT_RECV, ioctx->ioctx.index);
783
784 list.addr = ioctx->ioctx.dma;
785 list.length = srp_max_req_size;
786 list.lkey = sdev->mr->lkey;
787
788 wr.next = NULL;
789 wr.sg_list = &list;
790 wr.num_sge = 1;
791
792 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
793}
794
795/**
796 * srpt_post_send() - Post an IB send request.
797 *
798 * Returns zero upon success and a non-zero value upon failure.
799 */
800static int srpt_post_send(struct srpt_rdma_ch *ch,
801 struct srpt_send_ioctx *ioctx, int len)
802{
803 struct ib_sge list;
804 struct ib_send_wr wr, *bad_wr;
805 struct srpt_device *sdev = ch->sport->sdev;
806 int ret;
807
808 atomic_inc(&ch->req_lim);
809
810 ret = -ENOMEM;
811 if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
812 printk(KERN_WARNING "IB send queue full (needed 1)\n");
813 goto out;
814 }
815
816 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
817 DMA_TO_DEVICE);
818
819 list.addr = ioctx->ioctx.dma;
820 list.length = len;
821 list.lkey = sdev->mr->lkey;
822
823 wr.next = NULL;
824 wr.wr_id = encode_wr_id(SRPT_SEND, ioctx->ioctx.index);
825 wr.sg_list = &list;
826 wr.num_sge = 1;
827 wr.opcode = IB_WR_SEND;
828 wr.send_flags = IB_SEND_SIGNALED;
829
830 ret = ib_post_send(ch->qp, &wr, &bad_wr);
831
832out:
833 if (ret < 0) {
834 atomic_inc(&ch->sq_wr_avail);
835 atomic_dec(&ch->req_lim);
836 }
837 return ret;
838}
839
840/**
841 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
842 * @ioctx: Pointer to the I/O context associated with the request.
843 * @srp_cmd: Pointer to the SRP_CMD request data.
844 * @dir: Pointer to the variable to which the transfer direction will be
845 * written.
846 * @data_len: Pointer to the variable to which the total data length of all
847 * descriptors in the SRP_CMD request will be written.
848 *
849 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
850 *
851 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
852 * -ENOMEM when memory allocation fails and zero upon success.
853 */
854static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
855 struct srp_cmd *srp_cmd,
856 enum dma_data_direction *dir, u64 *data_len)
857{
858 struct srp_indirect_buf *idb;
859 struct srp_direct_buf *db;
860 unsigned add_cdb_offset;
861 int ret;
862
863 /*
864 * The pointer computations below will only be compiled correctly
865 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
866 * whether srp_cmd::add_data has been declared as a byte pointer.
867 */
868 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
869 && !__same_type(srp_cmd->add_data[0], (u8)0));
870
871 BUG_ON(!dir);
872 BUG_ON(!data_len);
873
874 ret = 0;
875 *data_len = 0;
876
877 /*
878 * The lower four bits of the buffer format field contain the DATA-IN
879 * buffer descriptor format, and the highest four bits contain the
880 * DATA-OUT buffer descriptor format.
881 */
882 *dir = DMA_NONE;
883 if (srp_cmd->buf_fmt & 0xf)
884 /* DATA-IN: transfer data from target to initiator (read). */
885 *dir = DMA_FROM_DEVICE;
886 else if (srp_cmd->buf_fmt >> 4)
887 /* DATA-OUT: transfer data from initiator to target (write). */
888 *dir = DMA_TO_DEVICE;
889
890 /*
891 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
892 * CDB LENGTH' field are reserved and the size in bytes of this field
893 * is four times the value specified in bits 3..7. Hence the "& ~3".
894 */
895 add_cdb_offset = srp_cmd->add_cdb_len & ~3;
896 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
897 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
898 ioctx->n_rbuf = 1;
899 ioctx->rbufs = &ioctx->single_rbuf;
900
901 db = (struct srp_direct_buf *)(srp_cmd->add_data
902 + add_cdb_offset);
903 memcpy(ioctx->rbufs, db, sizeof *db);
904 *data_len = be32_to_cpu(db->len);
905 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
906 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
907 idb = (struct srp_indirect_buf *)(srp_cmd->add_data
908 + add_cdb_offset);
909
910 ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof *db;
911
912 if (ioctx->n_rbuf >
913 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
914 printk(KERN_ERR "received unsupported SRP_CMD request"
915 " type (%u out + %u in != %u / %zu)\n",
916 srp_cmd->data_out_desc_cnt,
917 srp_cmd->data_in_desc_cnt,
918 be32_to_cpu(idb->table_desc.len),
919 sizeof(*db));
920 ioctx->n_rbuf = 0;
921 ret = -EINVAL;
922 goto out;
923 }
924
925 if (ioctx->n_rbuf == 1)
926 ioctx->rbufs = &ioctx->single_rbuf;
927 else {
928 ioctx->rbufs =
929 kmalloc(ioctx->n_rbuf * sizeof *db, GFP_ATOMIC);
930 if (!ioctx->rbufs) {
931 ioctx->n_rbuf = 0;
932 ret = -ENOMEM;
933 goto out;
934 }
935 }
936
937 db = idb->desc_list;
938 memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof *db);
939 *data_len = be32_to_cpu(idb->len);
940 }
941out:
942 return ret;
943}
944
945/**
946 * srpt_init_ch_qp() - Initialize queue pair attributes.
947 *
948 * Initialized the attributes of queue pair 'qp' by allowing local write,
949 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
950 */
951static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
952{
953 struct ib_qp_attr *attr;
954 int ret;
955
956 attr = kzalloc(sizeof *attr, GFP_KERNEL);
957 if (!attr)
958 return -ENOMEM;
959
960 attr->qp_state = IB_QPS_INIT;
961 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
962 IB_ACCESS_REMOTE_WRITE;
963 attr->port_num = ch->sport->port;
964 attr->pkey_index = 0;
965
966 ret = ib_modify_qp(qp, attr,
967 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
968 IB_QP_PKEY_INDEX);
969
970 kfree(attr);
971 return ret;
972}
973
974/**
975 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
976 * @ch: channel of the queue pair.
977 * @qp: queue pair to change the state of.
978 *
979 * Returns zero upon success and a negative value upon failure.
980 *
981 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
982 * If this structure ever becomes larger, it might be necessary to allocate
983 * it dynamically instead of on the stack.
984 */
985static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
986{
987 struct ib_qp_attr qp_attr;
988 int attr_mask;
989 int ret;
990
991 qp_attr.qp_state = IB_QPS_RTR;
992 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
993 if (ret)
994 goto out;
995
996 qp_attr.max_dest_rd_atomic = 4;
997
998 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
999
1000out:
1001 return ret;
1002}
1003
1004/**
1005 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
1006 * @ch: channel of the queue pair.
1007 * @qp: queue pair to change the state of.
1008 *
1009 * Returns zero upon success and a negative value upon failure.
1010 *
1011 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1012 * If this structure ever becomes larger, it might be necessary to allocate
1013 * it dynamically instead of on the stack.
1014 */
1015static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1016{
1017 struct ib_qp_attr qp_attr;
1018 int attr_mask;
1019 int ret;
1020
1021 qp_attr.qp_state = IB_QPS_RTS;
1022 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1023 if (ret)
1024 goto out;
1025
1026 qp_attr.max_rd_atomic = 4;
1027
1028 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1029
1030out:
1031 return ret;
1032}
1033
1034/**
1035 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1036 */
1037static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1038{
1039 struct ib_qp_attr qp_attr;
1040
1041 qp_attr.qp_state = IB_QPS_ERR;
1042 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1043}
1044
1045/**
1046 * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
1047 */
1048static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1049 struct srpt_send_ioctx *ioctx)
1050{
1051 struct scatterlist *sg;
1052 enum dma_data_direction dir;
1053
1054 BUG_ON(!ch);
1055 BUG_ON(!ioctx);
1056 BUG_ON(ioctx->n_rdma && !ioctx->rdma_ius);
1057
1058 while (ioctx->n_rdma)
1059 kfree(ioctx->rdma_ius[--ioctx->n_rdma].sge);
1060
1061 kfree(ioctx->rdma_ius);
1062 ioctx->rdma_ius = NULL;
1063
1064 if (ioctx->mapped_sg_count) {
1065 sg = ioctx->sg;
1066 WARN_ON(!sg);
1067 dir = ioctx->cmd.data_direction;
1068 BUG_ON(dir == DMA_NONE);
1069 ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
1070 opposite_dma_dir(dir));
1071 ioctx->mapped_sg_count = 0;
1072 }
1073}
1074
1075/**
1076 * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
1077 */
1078static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1079 struct srpt_send_ioctx *ioctx)
1080{
c2421ce1 1081 struct ib_device *dev = ch->sport->sdev->device;
a42d985b
BVA
1082 struct se_cmd *cmd;
1083 struct scatterlist *sg, *sg_orig;
1084 int sg_cnt;
1085 enum dma_data_direction dir;
1086 struct rdma_iu *riu;
1087 struct srp_direct_buf *db;
1088 dma_addr_t dma_addr;
1089 struct ib_sge *sge;
1090 u64 raddr;
1091 u32 rsize;
1092 u32 tsize;
1093 u32 dma_len;
1094 int count, nrdma;
1095 int i, j, k;
1096
1097 BUG_ON(!ch);
1098 BUG_ON(!ioctx);
1099 cmd = &ioctx->cmd;
1100 dir = cmd->data_direction;
1101 BUG_ON(dir == DMA_NONE);
1102
6f9e7f01
RD
1103 ioctx->sg = sg = sg_orig = cmd->t_data_sg;
1104 ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
a42d985b
BVA
1105
1106 count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
1107 opposite_dma_dir(dir));
1108 if (unlikely(!count))
1109 return -EAGAIN;
1110
1111 ioctx->mapped_sg_count = count;
1112
1113 if (ioctx->rdma_ius && ioctx->n_rdma_ius)
1114 nrdma = ioctx->n_rdma_ius;
1115 else {
1116 nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
1117 + ioctx->n_rbuf;
1118
1119 ioctx->rdma_ius = kzalloc(nrdma * sizeof *riu, GFP_KERNEL);
1120 if (!ioctx->rdma_ius)
1121 goto free_mem;
1122
1123 ioctx->n_rdma_ius = nrdma;
1124 }
1125
1126 db = ioctx->rbufs;
1127 tsize = cmd->data_length;
c2421ce1 1128 dma_len = ib_sg_dma_len(dev, &sg[0]);
a42d985b
BVA
1129 riu = ioctx->rdma_ius;
1130
1131 /*
1132 * For each remote desc - calculate the #ib_sge.
1133 * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1134 * each remote desc rdma_iu is required a rdma wr;
1135 * else
1136 * we need to allocate extra rdma_iu to carry extra #ib_sge in
1137 * another rdma wr
1138 */
1139 for (i = 0, j = 0;
1140 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1141 rsize = be32_to_cpu(db->len);
1142 raddr = be64_to_cpu(db->va);
1143 riu->raddr = raddr;
1144 riu->rkey = be32_to_cpu(db->key);
1145 riu->sge_cnt = 0;
1146
1147 /* calculate how many sge required for this remote_buf */
1148 while (rsize > 0 && tsize > 0) {
1149
1150 if (rsize >= dma_len) {
1151 tsize -= dma_len;
1152 rsize -= dma_len;
1153 raddr += dma_len;
1154
1155 if (tsize > 0) {
1156 ++j;
1157 if (j < count) {
1158 sg = sg_next(sg);
c2421ce1
MM
1159 dma_len = ib_sg_dma_len(
1160 dev, sg);
a42d985b
BVA
1161 }
1162 }
1163 } else {
1164 tsize -= rsize;
1165 dma_len -= rsize;
1166 rsize = 0;
1167 }
1168
1169 ++riu->sge_cnt;
1170
1171 if (rsize > 0 && riu->sge_cnt == SRPT_DEF_SG_PER_WQE) {
1172 ++ioctx->n_rdma;
1173 riu->sge =
1174 kmalloc(riu->sge_cnt * sizeof *riu->sge,
1175 GFP_KERNEL);
1176 if (!riu->sge)
1177 goto free_mem;
1178
1179 ++riu;
1180 riu->sge_cnt = 0;
1181 riu->raddr = raddr;
1182 riu->rkey = be32_to_cpu(db->key);
1183 }
1184 }
1185
1186 ++ioctx->n_rdma;
1187 riu->sge = kmalloc(riu->sge_cnt * sizeof *riu->sge,
1188 GFP_KERNEL);
1189 if (!riu->sge)
1190 goto free_mem;
1191 }
1192
1193 db = ioctx->rbufs;
1194 tsize = cmd->data_length;
1195 riu = ioctx->rdma_ius;
1196 sg = sg_orig;
c2421ce1
MM
1197 dma_len = ib_sg_dma_len(dev, &sg[0]);
1198 dma_addr = ib_sg_dma_address(dev, &sg[0]);
a42d985b
BVA
1199
1200 /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1201 for (i = 0, j = 0;
1202 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1203 rsize = be32_to_cpu(db->len);
1204 sge = riu->sge;
1205 k = 0;
1206
1207 while (rsize > 0 && tsize > 0) {
1208 sge->addr = dma_addr;
1209 sge->lkey = ch->sport->sdev->mr->lkey;
1210
1211 if (rsize >= dma_len) {
1212 sge->length =
1213 (tsize < dma_len) ? tsize : dma_len;
1214 tsize -= dma_len;
1215 rsize -= dma_len;
1216
1217 if (tsize > 0) {
1218 ++j;
1219 if (j < count) {
1220 sg = sg_next(sg);
c2421ce1
MM
1221 dma_len = ib_sg_dma_len(
1222 dev, sg);
1223 dma_addr = ib_sg_dma_address(
1224 dev, sg);
a42d985b
BVA
1225 }
1226 }
1227 } else {
1228 sge->length = (tsize < rsize) ? tsize : rsize;
1229 tsize -= rsize;
1230 dma_len -= rsize;
1231 dma_addr += rsize;
1232 rsize = 0;
1233 }
1234
1235 ++k;
1236 if (k == riu->sge_cnt && rsize > 0 && tsize > 0) {
1237 ++riu;
1238 sge = riu->sge;
1239 k = 0;
1240 } else if (rsize > 0 && tsize > 0)
1241 ++sge;
1242 }
1243 }
1244
1245 return 0;
1246
1247free_mem:
1248 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1249
1250 return -ENOMEM;
1251}
1252
1253/**
1254 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1255 */
1256static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1257{
1258 struct srpt_send_ioctx *ioctx;
1259 unsigned long flags;
1260
1261 BUG_ON(!ch);
1262
1263 ioctx = NULL;
1264 spin_lock_irqsave(&ch->spinlock, flags);
1265 if (!list_empty(&ch->free_list)) {
1266 ioctx = list_first_entry(&ch->free_list,
1267 struct srpt_send_ioctx, free_list);
1268 list_del(&ioctx->free_list);
1269 }
1270 spin_unlock_irqrestore(&ch->spinlock, flags);
1271
1272 if (!ioctx)
1273 return ioctx;
1274
1275 BUG_ON(ioctx->ch != ch);
a42d985b
BVA
1276 spin_lock_init(&ioctx->spinlock);
1277 ioctx->state = SRPT_STATE_NEW;
1278 ioctx->n_rbuf = 0;
1279 ioctx->rbufs = NULL;
1280 ioctx->n_rdma = 0;
1281 ioctx->n_rdma_ius = 0;
1282 ioctx->rdma_ius = NULL;
1283 ioctx->mapped_sg_count = 0;
1284 init_completion(&ioctx->tx_done);
1285 ioctx->queue_status_only = false;
1286 /*
1287 * transport_init_se_cmd() does not initialize all fields, so do it
1288 * here.
1289 */
1290 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1291 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1292
1293 return ioctx;
1294}
1295
a42d985b
BVA
1296/**
1297 * srpt_abort_cmd() - Abort a SCSI command.
1298 * @ioctx: I/O context associated with the SCSI command.
1299 * @context: Preferred execution context.
1300 */
1301static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1302{
1303 enum srpt_command_state state;
1304 unsigned long flags;
1305
1306 BUG_ON(!ioctx);
1307
1308 /*
1309 * If the command is in a state where the target core is waiting for
1310 * the ib_srpt driver, change the state to the next state. Changing
1311 * the state of the command from SRPT_STATE_NEED_DATA to
1312 * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
1313 * function a second time.
1314 */
1315
1316 spin_lock_irqsave(&ioctx->spinlock, flags);
1317 state = ioctx->state;
1318 switch (state) {
1319 case SRPT_STATE_NEED_DATA:
1320 ioctx->state = SRPT_STATE_DATA_IN;
1321 break;
1322 case SRPT_STATE_DATA_IN:
1323 case SRPT_STATE_CMD_RSP_SENT:
1324 case SRPT_STATE_MGMT_RSP_SENT:
1325 ioctx->state = SRPT_STATE_DONE;
1326 break;
1327 default:
1328 break;
1329 }
1330 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1331
9474b043
NB
1332 if (state == SRPT_STATE_DONE) {
1333 struct srpt_rdma_ch *ch = ioctx->ch;
1334
1335 BUG_ON(ch->sess == NULL);
1336
1337 target_put_sess_cmd(ch->sess, &ioctx->cmd);
a42d985b 1338 goto out;
9474b043 1339 }
a42d985b
BVA
1340
1341 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
1342 ioctx->tag);
1343
1344 switch (state) {
1345 case SRPT_STATE_NEW:
1346 case SRPT_STATE_DATA_IN:
1347 case SRPT_STATE_MGMT:
1348 /*
1349 * Do nothing - defer abort processing until
1350 * srpt_queue_response() is invoked.
1351 */
1352 WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
1353 break;
1354 case SRPT_STATE_NEED_DATA:
1355 /* DMA_TO_DEVICE (write) - RDMA read error. */
e672a47f
CH
1356
1357 /* XXX(hch): this is a horrible layering violation.. */
7d680f3b 1358 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
e672a47f 1359 ioctx->cmd.transport_state &= ~CMD_T_ACTIVE;
7d680f3b 1360 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
a42d985b
BVA
1361 break;
1362 case SRPT_STATE_CMD_RSP_SENT:
1363 /*
1364 * SRP_RSP sending failed or the SRP_RSP send completion has
1365 * not been received in time.
1366 */
1367 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
9474b043 1368 target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
a42d985b
BVA
1369 break;
1370 case SRPT_STATE_MGMT_RSP_SENT:
1371 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
9474b043 1372 target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
a42d985b
BVA
1373 break;
1374 default:
532ec6f1 1375 WARN(1, "Unexpected command state (%d)", state);
a42d985b
BVA
1376 break;
1377 }
1378
1379out:
1380 return state;
1381}
1382
1383/**
1384 * srpt_handle_send_err_comp() - Process an IB_WC_SEND error completion.
1385 */
1386static void srpt_handle_send_err_comp(struct srpt_rdma_ch *ch, u64 wr_id)
1387{
1388 struct srpt_send_ioctx *ioctx;
1389 enum srpt_command_state state;
1390 struct se_cmd *cmd;
1391 u32 index;
1392
1393 atomic_inc(&ch->sq_wr_avail);
1394
1395 index = idx_from_wr_id(wr_id);
1396 ioctx = ch->ioctx_ring[index];
1397 state = srpt_get_cmd_state(ioctx);
1398 cmd = &ioctx->cmd;
1399
1400 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1401 && state != SRPT_STATE_MGMT_RSP_SENT
1402 && state != SRPT_STATE_NEED_DATA
1403 && state != SRPT_STATE_DONE);
1404
1405 /* If SRP_RSP sending failed, undo the ch->req_lim change. */
1406 if (state == SRPT_STATE_CMD_RSP_SENT
1407 || state == SRPT_STATE_MGMT_RSP_SENT)
1408 atomic_dec(&ch->req_lim);
1409
1410 srpt_abort_cmd(ioctx);
1411}
1412
1413/**
1414 * srpt_handle_send_comp() - Process an IB send completion notification.
1415 */
1416static void srpt_handle_send_comp(struct srpt_rdma_ch *ch,
1417 struct srpt_send_ioctx *ioctx)
1418{
1419 enum srpt_command_state state;
1420
1421 atomic_inc(&ch->sq_wr_avail);
1422
1423 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1424
1425 if (WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1426 && state != SRPT_STATE_MGMT_RSP_SENT
1427 && state != SRPT_STATE_DONE))
1428 pr_debug("state = %d\n", state);
1429
9474b043
NB
1430 if (state != SRPT_STATE_DONE) {
1431 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1432 transport_generic_free_cmd(&ioctx->cmd, 0);
1433 } else {
a42d985b
BVA
1434 printk(KERN_ERR "IB completion has been received too late for"
1435 " wr_id = %u.\n", ioctx->ioctx.index);
9474b043 1436 }
a42d985b
BVA
1437}
1438
1439/**
1440 * srpt_handle_rdma_comp() - Process an IB RDMA completion notification.
1441 *
e672a47f
CH
1442 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1443 * the data that has been transferred via IB RDMA had to be postponed until the
142ad5db 1444 * check_stop_free() callback. None of this is necessary anymore and needs to
e672a47f 1445 * be cleaned up.
a42d985b
BVA
1446 */
1447static void srpt_handle_rdma_comp(struct srpt_rdma_ch *ch,
1448 struct srpt_send_ioctx *ioctx,
1449 enum srpt_opcode opcode)
1450{
1451 WARN_ON(ioctx->n_rdma <= 0);
1452 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1453
1454 if (opcode == SRPT_RDMA_READ_LAST) {
1455 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1456 SRPT_STATE_DATA_IN))
e672a47f 1457 target_execute_cmd(&ioctx->cmd);
a42d985b
BVA
1458 else
1459 printk(KERN_ERR "%s[%d]: wrong state = %d\n", __func__,
1460 __LINE__, srpt_get_cmd_state(ioctx));
1461 } else if (opcode == SRPT_RDMA_ABORT) {
1462 ioctx->rdma_aborted = true;
1463 } else {
1464 WARN(true, "unexpected opcode %d\n", opcode);
1465 }
1466}
1467
1468/**
1469 * srpt_handle_rdma_err_comp() - Process an IB RDMA error completion.
1470 */
1471static void srpt_handle_rdma_err_comp(struct srpt_rdma_ch *ch,
1472 struct srpt_send_ioctx *ioctx,
1473 enum srpt_opcode opcode)
1474{
1475 struct se_cmd *cmd;
1476 enum srpt_command_state state;
1477
1478 cmd = &ioctx->cmd;
1479 state = srpt_get_cmd_state(ioctx);
1480 switch (opcode) {
1481 case SRPT_RDMA_READ_LAST:
1482 if (ioctx->n_rdma <= 0) {
1483 printk(KERN_ERR "Received invalid RDMA read"
1484 " error completion with idx %d\n",
1485 ioctx->ioctx.index);
1486 break;
1487 }
1488 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1489 if (state == SRPT_STATE_NEED_DATA)
1490 srpt_abort_cmd(ioctx);
1491 else
1492 printk(KERN_ERR "%s[%d]: wrong state = %d\n",
1493 __func__, __LINE__, state);
1494 break;
1495 case SRPT_RDMA_WRITE_LAST:
a42d985b
BVA
1496 break;
1497 default:
1498 printk(KERN_ERR "%s[%d]: opcode = %u\n", __func__,
1499 __LINE__, opcode);
1500 break;
1501 }
1502}
1503
1504/**
1505 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1506 * @ch: RDMA channel through which the request has been received.
1507 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1508 * be built in the buffer ioctx->buf points at and hence this function will
1509 * overwrite the request data.
1510 * @tag: tag of the request for which this response is being generated.
1511 * @status: value for the STATUS field of the SRP_RSP information unit.
1512 *
1513 * Returns the size in bytes of the SRP_RSP response.
1514 *
1515 * An SRP_RSP response contains a SCSI status or service response. See also
1516 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1517 * response. See also SPC-2 for more information about sense data.
1518 */
1519static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1520 struct srpt_send_ioctx *ioctx, u64 tag,
1521 int status)
1522{
1523 struct srp_rsp *srp_rsp;
1524 const u8 *sense_data;
1525 int sense_data_len, max_sense_len;
1526
1527 /*
1528 * The lowest bit of all SAM-3 status codes is zero (see also
1529 * paragraph 5.3 in SAM-3).
1530 */
1531 WARN_ON(status & 1);
1532
1533 srp_rsp = ioctx->ioctx.buf;
1534 BUG_ON(!srp_rsp);
1535
1536 sense_data = ioctx->sense_data;
1537 sense_data_len = ioctx->cmd.scsi_sense_length;
1538 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1539
1540 memset(srp_rsp, 0, sizeof *srp_rsp);
1541 srp_rsp->opcode = SRP_RSP;
1542 srp_rsp->req_lim_delta =
1543 __constant_cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1544 srp_rsp->tag = tag;
1545 srp_rsp->status = status;
1546
1547 if (sense_data_len) {
1548 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1549 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1550 if (sense_data_len > max_sense_len) {
1551 printk(KERN_WARNING "truncated sense data from %d to %d"
1552 " bytes\n", sense_data_len, max_sense_len);
1553 sense_data_len = max_sense_len;
1554 }
1555
1556 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1557 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1558 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1559 }
1560
1561 return sizeof(*srp_rsp) + sense_data_len;
1562}
1563
1564/**
1565 * srpt_build_tskmgmt_rsp() - Build a task management response.
1566 * @ch: RDMA channel through which the request has been received.
1567 * @ioctx: I/O context in which the SRP_RSP response will be built.
1568 * @rsp_code: RSP_CODE that will be stored in the response.
1569 * @tag: Tag of the request for which this response is being generated.
1570 *
1571 * Returns the size in bytes of the SRP_RSP response.
1572 *
1573 * An SRP_RSP response contains a SCSI status or service response. See also
1574 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1575 * response.
1576 */
1577static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1578 struct srpt_send_ioctx *ioctx,
1579 u8 rsp_code, u64 tag)
1580{
1581 struct srp_rsp *srp_rsp;
1582 int resp_data_len;
1583 int resp_len;
1584
c807f643 1585 resp_data_len = 4;
a42d985b
BVA
1586 resp_len = sizeof(*srp_rsp) + resp_data_len;
1587
1588 srp_rsp = ioctx->ioctx.buf;
1589 BUG_ON(!srp_rsp);
1590 memset(srp_rsp, 0, sizeof *srp_rsp);
1591
1592 srp_rsp->opcode = SRP_RSP;
1593 srp_rsp->req_lim_delta = __constant_cpu_to_be32(1
1594 + atomic_xchg(&ch->req_lim_delta, 0));
1595 srp_rsp->tag = tag;
1596
c807f643
JW
1597 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1598 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1599 srp_rsp->data[3] = rsp_code;
a42d985b
BVA
1600
1601 return resp_len;
1602}
1603
1604#define NO_SUCH_LUN ((uint64_t)-1LL)
1605
1606/*
1607 * SCSI LUN addressing method. See also SAM-2 and the section about
1608 * eight byte LUNs.
1609 */
1610enum scsi_lun_addr_method {
1611 SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
1612 SCSI_LUN_ADDR_METHOD_FLAT = 1,
1613 SCSI_LUN_ADDR_METHOD_LUN = 2,
1614 SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
1615};
1616
1617/*
1618 * srpt_unpack_lun() - Convert from network LUN to linear LUN.
1619 *
1620 * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
1621 * order (big endian) to a linear LUN. Supports three LUN addressing methods:
1622 * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
1623 */
1624static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
1625{
1626 uint64_t res = NO_SUCH_LUN;
1627 int addressing_method;
1628
1629 if (unlikely(len < 2)) {
1630 printk(KERN_ERR "Illegal LUN length %d, expected 2 bytes or "
1631 "more", len);
1632 goto out;
1633 }
1634
1635 switch (len) {
1636 case 8:
1637 if ((*((__be64 *)lun) &
1638 __constant_cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
1639 goto out_err;
1640 break;
1641 case 4:
1642 if (*((__be16 *)&lun[2]) != 0)
1643 goto out_err;
1644 break;
1645 case 6:
1646 if (*((__be32 *)&lun[2]) != 0)
1647 goto out_err;
1648 break;
1649 case 2:
1650 break;
1651 default:
1652 goto out_err;
1653 }
1654
1655 addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
1656 switch (addressing_method) {
1657 case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
1658 case SCSI_LUN_ADDR_METHOD_FLAT:
1659 case SCSI_LUN_ADDR_METHOD_LUN:
1660 res = *(lun + 1) | (((*lun) & 0x3f) << 8);
1661 break;
1662
1663 case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
1664 default:
1665 printk(KERN_ERR "Unimplemented LUN addressing method %u",
1666 addressing_method);
1667 break;
1668 }
1669
1670out:
1671 return res;
1672
1673out_err:
1674 printk(KERN_ERR "Support for multi-level LUNs has not yet been"
1675 " implemented");
1676 goto out;
1677}
1678
1679static int srpt_check_stop_free(struct se_cmd *cmd)
1680{
9474b043
NB
1681 struct srpt_send_ioctx *ioctx = container_of(cmd,
1682 struct srpt_send_ioctx, cmd);
a42d985b 1683
9474b043 1684 return target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
a42d985b
BVA
1685}
1686
1687/**
1688 * srpt_handle_cmd() - Process SRP_CMD.
1689 */
1690static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
1691 struct srpt_recv_ioctx *recv_ioctx,
1692 struct srpt_send_ioctx *send_ioctx)
1693{
1694 struct se_cmd *cmd;
1695 struct srp_cmd *srp_cmd;
1696 uint64_t unpacked_lun;
1697 u64 data_len;
1698 enum dma_data_direction dir;
de103c93 1699 sense_reason_t ret;
9474b043 1700 int rc;
a42d985b
BVA
1701
1702 BUG_ON(!send_ioctx);
1703
1704 srp_cmd = recv_ioctx->ioctx.buf;
a42d985b
BVA
1705 cmd = &send_ioctx->cmd;
1706 send_ioctx->tag = srp_cmd->tag;
1707
1708 switch (srp_cmd->task_attr) {
1709 case SRP_CMD_SIMPLE_Q:
1710 cmd->sam_task_attr = MSG_SIMPLE_TAG;
1711 break;
1712 case SRP_CMD_ORDERED_Q:
1713 default:
1714 cmd->sam_task_attr = MSG_ORDERED_TAG;
1715 break;
1716 case SRP_CMD_HEAD_OF_Q:
1717 cmd->sam_task_attr = MSG_HEAD_TAG;
1718 break;
1719 case SRP_CMD_ACA:
1720 cmd->sam_task_attr = MSG_ACA_TAG;
1721 break;
1722 }
1723
de103c93 1724 if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
a42d985b
BVA
1725 printk(KERN_ERR "0x%llx: parsing SRP descriptor table failed.\n",
1726 srp_cmd->tag);
de103c93 1727 ret = TCM_INVALID_CDB_FIELD;
a42d985b
BVA
1728 goto send_sense;
1729 }
1730
a42d985b
BVA
1731 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
1732 sizeof(srp_cmd->lun));
9474b043
NB
1733 rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
1734 &send_ioctx->sense_data[0], unpacked_lun, data_len,
1735 MSG_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
1736 if (rc != 0) {
1737 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
a42d985b 1738 goto send_sense;
187e70a5 1739 }
a42d985b
BVA
1740 return 0;
1741
1742send_sense:
de103c93 1743 transport_send_check_condition_and_sense(cmd, ret, 0);
a42d985b
BVA
1744 return -1;
1745}
1746
1747/**
1748 * srpt_rx_mgmt_fn_tag() - Process a task management function by tag.
1749 * @ch: RDMA channel of the task management request.
1750 * @fn: Task management function to perform.
1751 * @req_tag: Tag of the SRP task management request.
1752 * @mgmt_ioctx: I/O context of the task management request.
1753 *
1754 * Returns zero if the target core will process the task management
1755 * request asynchronously.
1756 *
1757 * Note: It is assumed that the initiator serializes tag-based task management
1758 * requests.
1759 */
1760static int srpt_rx_mgmt_fn_tag(struct srpt_send_ioctx *ioctx, u64 tag)
1761{
1762 struct srpt_device *sdev;
1763 struct srpt_rdma_ch *ch;
1764 struct srpt_send_ioctx *target;
1765 int ret, i;
1766
1767 ret = -EINVAL;
1768 ch = ioctx->ch;
1769 BUG_ON(!ch);
1770 BUG_ON(!ch->sport);
1771 sdev = ch->sport->sdev;
1772 BUG_ON(!sdev);
1773 spin_lock_irq(&sdev->spinlock);
1774 for (i = 0; i < ch->rq_size; ++i) {
1775 target = ch->ioctx_ring[i];
1776 if (target->cmd.se_lun == ioctx->cmd.se_lun &&
1777 target->tag == tag &&
1778 srpt_get_cmd_state(target) != SRPT_STATE_DONE) {
1779 ret = 0;
1780 /* now let the target core abort &target->cmd; */
1781 break;
1782 }
1783 }
1784 spin_unlock_irq(&sdev->spinlock);
1785 return ret;
1786}
1787
1788static int srp_tmr_to_tcm(int fn)
1789{
1790 switch (fn) {
1791 case SRP_TSK_ABORT_TASK:
1792 return TMR_ABORT_TASK;
1793 case SRP_TSK_ABORT_TASK_SET:
1794 return TMR_ABORT_TASK_SET;
1795 case SRP_TSK_CLEAR_TASK_SET:
1796 return TMR_CLEAR_TASK_SET;
1797 case SRP_TSK_LUN_RESET:
1798 return TMR_LUN_RESET;
1799 case SRP_TSK_CLEAR_ACA:
1800 return TMR_CLEAR_ACA;
1801 default:
1802 return -1;
1803 }
1804}
1805
1806/**
1807 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1808 *
1809 * Returns 0 if and only if the request will be processed by the target core.
1810 *
1811 * For more information about SRP_TSK_MGMT information units, see also section
1812 * 6.7 in the SRP r16a document.
1813 */
1814static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1815 struct srpt_recv_ioctx *recv_ioctx,
1816 struct srpt_send_ioctx *send_ioctx)
1817{
1818 struct srp_tsk_mgmt *srp_tsk;
1819 struct se_cmd *cmd;
3e4f5748 1820 struct se_session *sess = ch->sess;
a42d985b 1821 uint64_t unpacked_lun;
3e4f5748 1822 uint32_t tag = 0;
a42d985b 1823 int tcm_tmr;
3e4f5748 1824 int rc;
a42d985b
BVA
1825
1826 BUG_ON(!send_ioctx);
1827
1828 srp_tsk = recv_ioctx->ioctx.buf;
1829 cmd = &send_ioctx->cmd;
1830
1831 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1832 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1833 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1834
1835 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
1836 send_ioctx->tag = srp_tsk->tag;
1837 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1838 if (tcm_tmr < 0) {
a42d985b
BVA
1839 send_ioctx->cmd.se_tmr_req->response =
1840 TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
de103c93 1841 goto fail;
a42d985b 1842 }
a42d985b
BVA
1843 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
1844 sizeof(srp_tsk->lun));
3e4f5748
NB
1845
1846 if (srp_tsk->tsk_mgmt_func == SRP_TSK_ABORT_TASK) {
1847 rc = srpt_rx_mgmt_fn_tag(send_ioctx, srp_tsk->task_tag);
1848 if (rc < 0) {
1849 send_ioctx->cmd.se_tmr_req->response =
1850 TMR_TASK_DOES_NOT_EXIST;
1851 goto fail;
1852 }
1853 tag = srp_tsk->task_tag;
1854 }
1855 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, unpacked_lun,
1856 srp_tsk, tcm_tmr, GFP_KERNEL, tag,
1857 TARGET_SCF_ACK_KREF);
1858 if (rc != 0) {
1859 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
de103c93 1860 goto fail;
a42d985b 1861 }
de103c93
CH
1862 return;
1863fail:
de103c93 1864 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
a42d985b
BVA
1865}
1866
1867/**
1868 * srpt_handle_new_iu() - Process a newly received information unit.
1869 * @ch: RDMA channel through which the information unit has been received.
1870 * @ioctx: SRPT I/O context associated with the information unit.
1871 */
1872static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1873 struct srpt_recv_ioctx *recv_ioctx,
1874 struct srpt_send_ioctx *send_ioctx)
1875{
1876 struct srp_cmd *srp_cmd;
1877 enum rdma_ch_state ch_state;
1878
1879 BUG_ON(!ch);
1880 BUG_ON(!recv_ioctx);
1881
1882 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1883 recv_ioctx->ioctx.dma, srp_max_req_size,
1884 DMA_FROM_DEVICE);
1885
1886 ch_state = srpt_get_ch_state(ch);
1887 if (unlikely(ch_state == CH_CONNECTING)) {
1888 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1889 goto out;
1890 }
1891
1892 if (unlikely(ch_state != CH_LIVE))
1893 goto out;
1894
1895 srp_cmd = recv_ioctx->ioctx.buf;
1896 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1897 if (!send_ioctx)
1898 send_ioctx = srpt_get_send_ioctx(ch);
1899 if (unlikely(!send_ioctx)) {
1900 list_add_tail(&recv_ioctx->wait_list,
1901 &ch->cmd_wait_list);
1902 goto out;
1903 }
1904 }
1905
a42d985b
BVA
1906 switch (srp_cmd->opcode) {
1907 case SRP_CMD:
1908 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1909 break;
1910 case SRP_TSK_MGMT:
1911 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1912 break;
1913 case SRP_I_LOGOUT:
1914 printk(KERN_ERR "Not yet implemented: SRP_I_LOGOUT\n");
1915 break;
1916 case SRP_CRED_RSP:
1917 pr_debug("received SRP_CRED_RSP\n");
1918 break;
1919 case SRP_AER_RSP:
1920 pr_debug("received SRP_AER_RSP\n");
1921 break;
1922 case SRP_RSP:
1923 printk(KERN_ERR "Received SRP_RSP\n");
1924 break;
1925 default:
1926 printk(KERN_ERR "received IU with unknown opcode 0x%x\n",
1927 srp_cmd->opcode);
1928 break;
1929 }
1930
1931 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1932out:
1933 return;
1934}
1935
1936static void srpt_process_rcv_completion(struct ib_cq *cq,
1937 struct srpt_rdma_ch *ch,
1938 struct ib_wc *wc)
1939{
1940 struct srpt_device *sdev = ch->sport->sdev;
1941 struct srpt_recv_ioctx *ioctx;
1942 u32 index;
1943
1944 index = idx_from_wr_id(wc->wr_id);
1945 if (wc->status == IB_WC_SUCCESS) {
1946 int req_lim;
1947
1948 req_lim = atomic_dec_return(&ch->req_lim);
1949 if (unlikely(req_lim < 0))
1950 printk(KERN_ERR "req_lim = %d < 0\n", req_lim);
1951 ioctx = sdev->ioctx_ring[index];
1952 srpt_handle_new_iu(ch, ioctx, NULL);
1953 } else {
1954 printk(KERN_INFO "receiving failed for idx %u with status %d\n",
1955 index, wc->status);
1956 }
1957}
1958
1959/**
1960 * srpt_process_send_completion() - Process an IB send completion.
1961 *
1962 * Note: Although this has not yet been observed during tests, at least in
1963 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1964 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1965 * value in each response is set to one, and it is possible that this response
1966 * makes the initiator send a new request before the send completion for that
1967 * response has been processed. This could e.g. happen if the call to
1968 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1969 * if IB retransmission causes generation of the send completion to be
1970 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1971 * are queued on cmd_wait_list. The code below processes these delayed
1972 * requests one at a time.
1973 */
1974static void srpt_process_send_completion(struct ib_cq *cq,
1975 struct srpt_rdma_ch *ch,
1976 struct ib_wc *wc)
1977{
1978 struct srpt_send_ioctx *send_ioctx;
1979 uint32_t index;
1980 enum srpt_opcode opcode;
1981
1982 index = idx_from_wr_id(wc->wr_id);
1983 opcode = opcode_from_wr_id(wc->wr_id);
1984 send_ioctx = ch->ioctx_ring[index];
1985 if (wc->status == IB_WC_SUCCESS) {
1986 if (opcode == SRPT_SEND)
1987 srpt_handle_send_comp(ch, send_ioctx);
1988 else {
1989 WARN_ON(opcode != SRPT_RDMA_ABORT &&
1990 wc->opcode != IB_WC_RDMA_READ);
1991 srpt_handle_rdma_comp(ch, send_ioctx, opcode);
1992 }
1993 } else {
1994 if (opcode == SRPT_SEND) {
1995 printk(KERN_INFO "sending response for idx %u failed"
1996 " with status %d\n", index, wc->status);
1997 srpt_handle_send_err_comp(ch, wc->wr_id);
1998 } else if (opcode != SRPT_RDMA_MID) {
1999 printk(KERN_INFO "RDMA t %d for idx %u failed with"
2000 " status %d", opcode, index, wc->status);
2001 srpt_handle_rdma_err_comp(ch, send_ioctx, opcode);
2002 }
2003 }
2004
2005 while (unlikely(opcode == SRPT_SEND
2006 && !list_empty(&ch->cmd_wait_list)
2007 && srpt_get_ch_state(ch) == CH_LIVE
2008 && (send_ioctx = srpt_get_send_ioctx(ch)) != NULL)) {
2009 struct srpt_recv_ioctx *recv_ioctx;
2010
2011 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
2012 struct srpt_recv_ioctx,
2013 wait_list);
2014 list_del(&recv_ioctx->wait_list);
2015 srpt_handle_new_iu(ch, recv_ioctx, send_ioctx);
2016 }
2017}
2018
2019static void srpt_process_completion(struct ib_cq *cq, struct srpt_rdma_ch *ch)
2020{
2021 struct ib_wc *const wc = ch->wc;
2022 int i, n;
2023
2024 WARN_ON(cq != ch->cq);
2025
2026 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
2027 while ((n = ib_poll_cq(cq, ARRAY_SIZE(ch->wc), wc)) > 0) {
2028 for (i = 0; i < n; i++) {
2029 if (opcode_from_wr_id(wc[i].wr_id) == SRPT_RECV)
2030 srpt_process_rcv_completion(cq, ch, &wc[i]);
2031 else
2032 srpt_process_send_completion(cq, ch, &wc[i]);
2033 }
2034 }
2035}
2036
2037/**
2038 * srpt_completion() - IB completion queue callback function.
2039 *
2040 * Notes:
2041 * - It is guaranteed that a completion handler will never be invoked
2042 * concurrently on two different CPUs for the same completion queue. See also
2043 * Documentation/infiniband/core_locking.txt and the implementation of
2044 * handle_edge_irq() in kernel/irq/chip.c.
2045 * - When threaded IRQs are enabled, completion handlers are invoked in thread
2046 * context instead of interrupt context.
2047 */
2048static void srpt_completion(struct ib_cq *cq, void *ctx)
2049{
2050 struct srpt_rdma_ch *ch = ctx;
2051
2052 wake_up_interruptible(&ch->wait_queue);
2053}
2054
2055static int srpt_compl_thread(void *arg)
2056{
2057 struct srpt_rdma_ch *ch;
2058
2059 /* Hibernation / freezing of the SRPT kernel thread is not supported. */
2060 current->flags |= PF_NOFREEZE;
2061
2062 ch = arg;
2063 BUG_ON(!ch);
2064 printk(KERN_INFO "Session %s: kernel thread %s (PID %d) started\n",
2065 ch->sess_name, ch->thread->comm, current->pid);
2066 while (!kthread_should_stop()) {
2067 wait_event_interruptible(ch->wait_queue,
2068 (srpt_process_completion(ch->cq, ch),
2069 kthread_should_stop()));
2070 }
2071 printk(KERN_INFO "Session %s: kernel thread %s (PID %d) stopped\n",
2072 ch->sess_name, ch->thread->comm, current->pid);
2073 return 0;
2074}
2075
2076/**
2077 * srpt_create_ch_ib() - Create receive and send completion queues.
2078 */
2079static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
2080{
2081 struct ib_qp_init_attr *qp_init;
2082 struct srpt_port *sport = ch->sport;
2083 struct srpt_device *sdev = sport->sdev;
2084 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
2085 int ret;
2086
2087 WARN_ON(ch->rq_size < 1);
2088
2089 ret = -ENOMEM;
2090 qp_init = kzalloc(sizeof *qp_init, GFP_KERNEL);
2091 if (!qp_init)
2092 goto out;
2093
a5d7c9df 2094retry:
a42d985b
BVA
2095 ch->cq = ib_create_cq(sdev->device, srpt_completion, NULL, ch,
2096 ch->rq_size + srp_sq_size, 0);
2097 if (IS_ERR(ch->cq)) {
2098 ret = PTR_ERR(ch->cq);
2099 printk(KERN_ERR "failed to create CQ cqe= %d ret= %d\n",
2100 ch->rq_size + srp_sq_size, ret);
2101 goto out;
2102 }
2103
2104 qp_init->qp_context = (void *)ch;
2105 qp_init->event_handler
2106 = (void(*)(struct ib_event *, void*))srpt_qp_event;
2107 qp_init->send_cq = ch->cq;
2108 qp_init->recv_cq = ch->cq;
2109 qp_init->srq = sdev->srq;
2110 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
2111 qp_init->qp_type = IB_QPT_RC;
2112 qp_init->cap.max_send_wr = srp_sq_size;
2113 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
2114
2115 ch->qp = ib_create_qp(sdev->pd, qp_init);
2116 if (IS_ERR(ch->qp)) {
2117 ret = PTR_ERR(ch->qp);
a5d7c9df
BVA
2118 if (ret == -ENOMEM) {
2119 srp_sq_size /= 2;
2120 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
2121 ib_destroy_cq(ch->cq);
2122 goto retry;
2123 }
2124 }
a42d985b
BVA
2125 printk(KERN_ERR "failed to create_qp ret= %d\n", ret);
2126 goto err_destroy_cq;
2127 }
2128
2129 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
2130
2131 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
2132 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
2133 qp_init->cap.max_send_wr, ch->cm_id);
2134
2135 ret = srpt_init_ch_qp(ch, ch->qp);
2136 if (ret)
2137 goto err_destroy_qp;
2138
2139 init_waitqueue_head(&ch->wait_queue);
2140
2141 pr_debug("creating thread for session %s\n", ch->sess_name);
2142
2143 ch->thread = kthread_run(srpt_compl_thread, ch, "ib_srpt_compl");
2144 if (IS_ERR(ch->thread)) {
2145 printk(KERN_ERR "failed to create kernel thread %ld\n",
2146 PTR_ERR(ch->thread));
2147 ch->thread = NULL;
2148 goto err_destroy_qp;
2149 }
2150
2151out:
2152 kfree(qp_init);
2153 return ret;
2154
2155err_destroy_qp:
2156 ib_destroy_qp(ch->qp);
2157err_destroy_cq:
2158 ib_destroy_cq(ch->cq);
2159 goto out;
2160}
2161
2162static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
2163{
2164 if (ch->thread)
2165 kthread_stop(ch->thread);
2166
2167 ib_destroy_qp(ch->qp);
2168 ib_destroy_cq(ch->cq);
2169}
2170
2171/**
2172 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
2173 *
2174 * Reset the QP and make sure all resources associated with the channel will
2175 * be deallocated at an appropriate time.
2176 *
2177 * Note: The caller must hold ch->sport->sdev->spinlock.
2178 */
2179static void __srpt_close_ch(struct srpt_rdma_ch *ch)
2180{
2181 struct srpt_device *sdev;
2182 enum rdma_ch_state prev_state;
2183 unsigned long flags;
2184
2185 sdev = ch->sport->sdev;
2186
2187 spin_lock_irqsave(&ch->spinlock, flags);
2188 prev_state = ch->state;
2189 switch (prev_state) {
2190 case CH_CONNECTING:
2191 case CH_LIVE:
2192 ch->state = CH_DISCONNECTING;
2193 break;
2194 default:
2195 break;
2196 }
2197 spin_unlock_irqrestore(&ch->spinlock, flags);
2198
2199 switch (prev_state) {
2200 case CH_CONNECTING:
2201 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
2202 NULL, 0);
2203 /* fall through */
2204 case CH_LIVE:
2205 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
2206 printk(KERN_ERR "sending CM DREQ failed.\n");
2207 break;
2208 case CH_DISCONNECTING:
2209 break;
2210 case CH_DRAINING:
2211 case CH_RELEASING:
2212 break;
2213 }
2214}
2215
2216/**
2217 * srpt_close_ch() - Close an RDMA channel.
2218 */
2219static void srpt_close_ch(struct srpt_rdma_ch *ch)
2220{
2221 struct srpt_device *sdev;
2222
2223 sdev = ch->sport->sdev;
2224 spin_lock_irq(&sdev->spinlock);
2225 __srpt_close_ch(ch);
2226 spin_unlock_irq(&sdev->spinlock);
2227}
2228
1d19f780
NB
2229/**
2230 * srpt_shutdown_session() - Whether or not a session may be shut down.
2231 */
2232static int srpt_shutdown_session(struct se_session *se_sess)
2233{
2234 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
2235 unsigned long flags;
2236
2237 spin_lock_irqsave(&ch->spinlock, flags);
2238 if (ch->in_shutdown) {
2239 spin_unlock_irqrestore(&ch->spinlock, flags);
2240 return true;
2241 }
2242
2243 ch->in_shutdown = true;
2244 target_sess_cmd_list_set_waiting(se_sess);
2245 spin_unlock_irqrestore(&ch->spinlock, flags);
2246
2247 return true;
2248}
2249
a42d985b
BVA
2250/**
2251 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
2252 * @cm_id: Pointer to the CM ID of the channel to be drained.
2253 *
2254 * Note: Must be called from inside srpt_cm_handler to avoid a race between
2255 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
2256 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
2257 * waits until all target sessions for the associated IB device have been
2258 * unregistered and target session registration involves a call to
2259 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
2260 * this function has finished).
2261 */
2262static void srpt_drain_channel(struct ib_cm_id *cm_id)
2263{
2264 struct srpt_device *sdev;
2265 struct srpt_rdma_ch *ch;
2266 int ret;
2267 bool do_reset = false;
2268
2269 WARN_ON_ONCE(irqs_disabled());
2270
2271 sdev = cm_id->context;
2272 BUG_ON(!sdev);
2273 spin_lock_irq(&sdev->spinlock);
2274 list_for_each_entry(ch, &sdev->rch_list, list) {
2275 if (ch->cm_id == cm_id) {
2276 do_reset = srpt_test_and_set_ch_state(ch,
2277 CH_CONNECTING, CH_DRAINING) ||
2278 srpt_test_and_set_ch_state(ch,
2279 CH_LIVE, CH_DRAINING) ||
2280 srpt_test_and_set_ch_state(ch,
2281 CH_DISCONNECTING, CH_DRAINING);
2282 break;
2283 }
2284 }
2285 spin_unlock_irq(&sdev->spinlock);
2286
2287 if (do_reset) {
1d19f780
NB
2288 if (ch->sess)
2289 srpt_shutdown_session(ch->sess);
2290
a42d985b
BVA
2291 ret = srpt_ch_qp_err(ch);
2292 if (ret < 0)
2293 printk(KERN_ERR "Setting queue pair in error state"
2294 " failed: %d\n", ret);
2295 }
2296}
2297
2298/**
2299 * srpt_find_channel() - Look up an RDMA channel.
2300 * @cm_id: Pointer to the CM ID of the channel to be looked up.
2301 *
2302 * Return NULL if no matching RDMA channel has been found.
2303 */
2304static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
2305 struct ib_cm_id *cm_id)
2306{
2307 struct srpt_rdma_ch *ch;
2308 bool found;
2309
2310 WARN_ON_ONCE(irqs_disabled());
2311 BUG_ON(!sdev);
2312
2313 found = false;
2314 spin_lock_irq(&sdev->spinlock);
2315 list_for_each_entry(ch, &sdev->rch_list, list) {
2316 if (ch->cm_id == cm_id) {
2317 found = true;
2318 break;
2319 }
2320 }
2321 spin_unlock_irq(&sdev->spinlock);
2322
2323 return found ? ch : NULL;
2324}
2325
2326/**
2327 * srpt_release_channel() - Release channel resources.
2328 *
2329 * Schedules the actual release because:
2330 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
2331 * trigger a deadlock.
2332 * - It is not safe to call TCM transport_* functions from interrupt context.
2333 */
2334static void srpt_release_channel(struct srpt_rdma_ch *ch)
2335{
2336 schedule_work(&ch->release_work);
2337}
2338
2339static void srpt_release_channel_work(struct work_struct *w)
2340{
2341 struct srpt_rdma_ch *ch;
2342 struct srpt_device *sdev;
9474b043 2343 struct se_session *se_sess;
a42d985b
BVA
2344
2345 ch = container_of(w, struct srpt_rdma_ch, release_work);
2346 pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2347 ch->release_done);
2348
2349 sdev = ch->sport->sdev;
2350 BUG_ON(!sdev);
2351
9474b043
NB
2352 se_sess = ch->sess;
2353 BUG_ON(!se_sess);
2354
be646c2d 2355 target_wait_for_sess_cmds(se_sess);
9474b043
NB
2356
2357 transport_deregister_session_configfs(se_sess);
2358 transport_deregister_session(se_sess);
a42d985b
BVA
2359 ch->sess = NULL;
2360
0b41d6ca
NB
2361 ib_destroy_cm_id(ch->cm_id);
2362
a42d985b
BVA
2363 srpt_destroy_ch_ib(ch);
2364
2365 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2366 ch->sport->sdev, ch->rq_size,
2367 ch->rsp_size, DMA_TO_DEVICE);
2368
2369 spin_lock_irq(&sdev->spinlock);
2370 list_del(&ch->list);
2371 spin_unlock_irq(&sdev->spinlock);
2372
a42d985b
BVA
2373 if (ch->release_done)
2374 complete(ch->release_done);
2375
2376 wake_up(&sdev->ch_releaseQ);
2377
2378 kfree(ch);
2379}
2380
2381static struct srpt_node_acl *__srpt_lookup_acl(struct srpt_port *sport,
2382 u8 i_port_id[16])
2383{
2384 struct srpt_node_acl *nacl;
2385
2386 list_for_each_entry(nacl, &sport->port_acl_list, list)
2387 if (memcmp(nacl->i_port_id, i_port_id,
2388 sizeof(nacl->i_port_id)) == 0)
2389 return nacl;
2390
2391 return NULL;
2392}
2393
2394static struct srpt_node_acl *srpt_lookup_acl(struct srpt_port *sport,
2395 u8 i_port_id[16])
2396{
2397 struct srpt_node_acl *nacl;
2398
2399 spin_lock_irq(&sport->port_acl_lock);
2400 nacl = __srpt_lookup_acl(sport, i_port_id);
2401 spin_unlock_irq(&sport->port_acl_lock);
2402
2403 return nacl;
2404}
2405
2406/**
2407 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2408 *
2409 * Ownership of the cm_id is transferred to the target session if this
2410 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2411 */
2412static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2413 struct ib_cm_req_event_param *param,
2414 void *private_data)
2415{
2416 struct srpt_device *sdev = cm_id->context;
2417 struct srpt_port *sport = &sdev->port[param->port - 1];
2418 struct srp_login_req *req;
2419 struct srp_login_rsp *rsp;
2420 struct srp_login_rej *rej;
2421 struct ib_cm_rep_param *rep_param;
2422 struct srpt_rdma_ch *ch, *tmp_ch;
2423 struct srpt_node_acl *nacl;
2424 u32 it_iu_len;
2425 int i;
2426 int ret = 0;
2427
2428 WARN_ON_ONCE(irqs_disabled());
2429
2430 if (WARN_ON(!sdev || !private_data))
2431 return -EINVAL;
2432
2433 req = (struct srp_login_req *)private_data;
2434
2435 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2436
2437 printk(KERN_INFO "Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2438 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2439 " (guid=0x%llx:0x%llx)\n",
2440 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2441 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2442 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2443 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2444 it_iu_len,
2445 param->port,
2446 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2447 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
2448
2449 rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
2450 rej = kzalloc(sizeof *rej, GFP_KERNEL);
2451 rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
2452
2453 if (!rsp || !rej || !rep_param) {
2454 ret = -ENOMEM;
2455 goto out;
2456 }
2457
2458 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
2459 rej->reason = __constant_cpu_to_be32(
2460 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
2461 ret = -EINVAL;
2462 printk(KERN_ERR "rejected SRP_LOGIN_REQ because its"
2463 " length (%d bytes) is out of range (%d .. %d)\n",
2464 it_iu_len, 64, srp_max_req_size);
2465 goto reject;
2466 }
2467
2468 if (!sport->enabled) {
2469 rej->reason = __constant_cpu_to_be32(
2470 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2471 ret = -EINVAL;
2472 printk(KERN_ERR "rejected SRP_LOGIN_REQ because the target port"
2473 " has not yet been enabled\n");
2474 goto reject;
2475 }
2476
2477 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2478 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2479
2480 spin_lock_irq(&sdev->spinlock);
2481
2482 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2483 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2484 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2485 && param->port == ch->sport->port
2486 && param->listen_id == ch->sport->sdev->cm_id
2487 && ch->cm_id) {
2488 enum rdma_ch_state ch_state;
2489
2490 ch_state = srpt_get_ch_state(ch);
2491 if (ch_state != CH_CONNECTING
2492 && ch_state != CH_LIVE)
2493 continue;
2494
2495 /* found an existing channel */
2496 pr_debug("Found existing channel %s"
2497 " cm_id= %p state= %d\n",
2498 ch->sess_name, ch->cm_id, ch_state);
2499
2500 __srpt_close_ch(ch);
2501
2502 rsp->rsp_flags =
2503 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2504 }
2505 }
2506
2507 spin_unlock_irq(&sdev->spinlock);
2508
2509 } else
2510 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2511
2512 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2513 || *(__be64 *)(req->target_port_id + 8) !=
2514 cpu_to_be64(srpt_service_guid)) {
2515 rej->reason = __constant_cpu_to_be32(
2516 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
2517 ret = -ENOMEM;
2518 printk(KERN_ERR "rejected SRP_LOGIN_REQ because it"
2519 " has an invalid target port identifier.\n");
2520 goto reject;
2521 }
2522
2523 ch = kzalloc(sizeof *ch, GFP_KERNEL);
2524 if (!ch) {
2525 rej->reason = __constant_cpu_to_be32(
2526 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2527 printk(KERN_ERR "rejected SRP_LOGIN_REQ because no memory.\n");
2528 ret = -ENOMEM;
2529 goto reject;
2530 }
2531
2532 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2533 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2534 memcpy(ch->t_port_id, req->target_port_id, 16);
2535 ch->sport = &sdev->port[param->port - 1];
2536 ch->cm_id = cm_id;
2537 /*
2538 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2539 * for the SRP protocol to the command queue size.
2540 */
2541 ch->rq_size = SRPT_RQ_SIZE;
2542 spin_lock_init(&ch->spinlock);
2543 ch->state = CH_CONNECTING;
2544 INIT_LIST_HEAD(&ch->cmd_wait_list);
2545 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2546
2547 ch->ioctx_ring = (struct srpt_send_ioctx **)
2548 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2549 sizeof(*ch->ioctx_ring[0]),
2550 ch->rsp_size, DMA_TO_DEVICE);
2551 if (!ch->ioctx_ring)
2552 goto free_ch;
2553
2554 INIT_LIST_HEAD(&ch->free_list);
2555 for (i = 0; i < ch->rq_size; i++) {
2556 ch->ioctx_ring[i]->ch = ch;
2557 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2558 }
2559
2560 ret = srpt_create_ch_ib(ch);
2561 if (ret) {
2562 rej->reason = __constant_cpu_to_be32(
2563 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2564 printk(KERN_ERR "rejected SRP_LOGIN_REQ because creating"
2565 " a new RDMA channel failed.\n");
2566 goto free_ring;
2567 }
2568
2569 ret = srpt_ch_qp_rtr(ch, ch->qp);
2570 if (ret) {
2571 rej->reason = __constant_cpu_to_be32(
2572 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2573 printk(KERN_ERR "rejected SRP_LOGIN_REQ because enabling"
2574 " RTR failed (error code = %d)\n", ret);
2575 goto destroy_ib;
2576 }
2577 /*
2578 * Use the initator port identifier as the session name.
2579 */
2580 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2581 be64_to_cpu(*(__be64 *)ch->i_port_id),
2582 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2583
2584 pr_debug("registering session %s\n", ch->sess_name);
2585
2586 nacl = srpt_lookup_acl(sport, ch->i_port_id);
2587 if (!nacl) {
2588 printk(KERN_INFO "Rejected login because no ACL has been"
2589 " configured yet for initiator %s.\n", ch->sess_name);
2590 rej->reason = __constant_cpu_to_be32(
2591 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2592 goto destroy_ib;
2593 }
2594
2595 ch->sess = transport_init_session();
3af33637 2596 if (IS_ERR(ch->sess)) {
a42d985b
BVA
2597 rej->reason = __constant_cpu_to_be32(
2598 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2599 pr_debug("Failed to create session\n");
2600 goto deregister_session;
2601 }
2602 ch->sess->se_node_acl = &nacl->nacl;
2603 transport_register_session(&sport->port_tpg_1, &nacl->nacl, ch->sess, ch);
2604
2605 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2606 ch->sess_name, ch->cm_id);
2607
2608 /* create srp_login_response */
2609 rsp->opcode = SRP_LOGIN_RSP;
2610 rsp->tag = req->tag;
2611 rsp->max_it_iu_len = req->req_it_iu_len;
2612 rsp->max_ti_iu_len = req->req_it_iu_len;
2613 ch->max_ti_iu_len = it_iu_len;
2614 rsp->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2615 | SRP_BUF_FORMAT_INDIRECT);
2616 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2617 atomic_set(&ch->req_lim, ch->rq_size);
2618 atomic_set(&ch->req_lim_delta, 0);
2619
2620 /* create cm reply */
2621 rep_param->qp_num = ch->qp->qp_num;
2622 rep_param->private_data = (void *)rsp;
2623 rep_param->private_data_len = sizeof *rsp;
2624 rep_param->rnr_retry_count = 7;
2625 rep_param->flow_control = 1;
2626 rep_param->failover_accepted = 0;
2627 rep_param->srq = 1;
2628 rep_param->responder_resources = 4;
2629 rep_param->initiator_depth = 4;
2630
2631 ret = ib_send_cm_rep(cm_id, rep_param);
2632 if (ret) {
2633 printk(KERN_ERR "sending SRP_LOGIN_REQ response failed"
2634 " (error code = %d)\n", ret);
2635 goto release_channel;
2636 }
2637
2638 spin_lock_irq(&sdev->spinlock);
2639 list_add_tail(&ch->list, &sdev->rch_list);
2640 spin_unlock_irq(&sdev->spinlock);
2641
2642 goto out;
2643
2644release_channel:
2645 srpt_set_ch_state(ch, CH_RELEASING);
2646 transport_deregister_session_configfs(ch->sess);
2647
2648deregister_session:
2649 transport_deregister_session(ch->sess);
2650 ch->sess = NULL;
2651
2652destroy_ib:
2653 srpt_destroy_ch_ib(ch);
2654
2655free_ring:
2656 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2657 ch->sport->sdev, ch->rq_size,
2658 ch->rsp_size, DMA_TO_DEVICE);
2659free_ch:
2660 kfree(ch);
2661
2662reject:
2663 rej->opcode = SRP_LOGIN_REJ;
2664 rej->tag = req->tag;
2665 rej->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2666 | SRP_BUF_FORMAT_INDIRECT);
2667
2668 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2669 (void *)rej, sizeof *rej);
2670
2671out:
2672 kfree(rep_param);
2673 kfree(rsp);
2674 kfree(rej);
2675
2676 return ret;
2677}
2678
2679static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2680{
2681 printk(KERN_INFO "Received IB REJ for cm_id %p.\n", cm_id);
2682 srpt_drain_channel(cm_id);
2683}
2684
2685/**
2686 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2687 *
2688 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2689 * and that the recipient may begin transmitting (RTU = ready to use).
2690 */
2691static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2692{
2693 struct srpt_rdma_ch *ch;
2694 int ret;
2695
2696 ch = srpt_find_channel(cm_id->context, cm_id);
2697 BUG_ON(!ch);
2698
2699 if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
2700 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2701
2702 ret = srpt_ch_qp_rts(ch, ch->qp);
2703
2704 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2705 wait_list) {
2706 list_del(&ioctx->wait_list);
2707 srpt_handle_new_iu(ch, ioctx, NULL);
2708 }
2709 if (ret)
2710 srpt_close_ch(ch);
2711 }
2712}
2713
2714static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2715{
2716 printk(KERN_INFO "Received IB TimeWait exit for cm_id %p.\n", cm_id);
2717 srpt_drain_channel(cm_id);
2718}
2719
2720static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2721{
2722 printk(KERN_INFO "Received IB REP error for cm_id %p.\n", cm_id);
2723 srpt_drain_channel(cm_id);
2724}
2725
2726/**
2727 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2728 */
2729static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2730{
2731 struct srpt_rdma_ch *ch;
2732 unsigned long flags;
2733 bool send_drep = false;
2734
2735 ch = srpt_find_channel(cm_id->context, cm_id);
2736 BUG_ON(!ch);
2737
2738 pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
2739
2740 spin_lock_irqsave(&ch->spinlock, flags);
2741 switch (ch->state) {
2742 case CH_CONNECTING:
2743 case CH_LIVE:
2744 send_drep = true;
2745 ch->state = CH_DISCONNECTING;
2746 break;
2747 case CH_DISCONNECTING:
2748 case CH_DRAINING:
2749 case CH_RELEASING:
2750 WARN(true, "unexpected channel state %d\n", ch->state);
2751 break;
2752 }
2753 spin_unlock_irqrestore(&ch->spinlock, flags);
2754
2755 if (send_drep) {
2756 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
2757 printk(KERN_ERR "Sending IB DREP failed.\n");
2758 printk(KERN_INFO "Received DREQ and sent DREP for session %s.\n",
2759 ch->sess_name);
2760 }
2761}
2762
2763/**
2764 * srpt_cm_drep_recv() - Process reception of a DREP message.
2765 */
2766static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2767{
2768 printk(KERN_INFO "Received InfiniBand DREP message for cm_id %p.\n",
2769 cm_id);
2770 srpt_drain_channel(cm_id);
2771}
2772
2773/**
2774 * srpt_cm_handler() - IB connection manager callback function.
2775 *
2776 * A non-zero return value will cause the caller destroy the CM ID.
2777 *
2778 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2779 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2780 * a non-zero value in any other case will trigger a race with the
2781 * ib_destroy_cm_id() call in srpt_release_channel().
2782 */
2783static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2784{
2785 int ret;
2786
2787 ret = 0;
2788 switch (event->event) {
2789 case IB_CM_REQ_RECEIVED:
2790 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2791 event->private_data);
2792 break;
2793 case IB_CM_REJ_RECEIVED:
2794 srpt_cm_rej_recv(cm_id);
2795 break;
2796 case IB_CM_RTU_RECEIVED:
2797 case IB_CM_USER_ESTABLISHED:
2798 srpt_cm_rtu_recv(cm_id);
2799 break;
2800 case IB_CM_DREQ_RECEIVED:
2801 srpt_cm_dreq_recv(cm_id);
2802 break;
2803 case IB_CM_DREP_RECEIVED:
2804 srpt_cm_drep_recv(cm_id);
2805 break;
2806 case IB_CM_TIMEWAIT_EXIT:
2807 srpt_cm_timewait_exit(cm_id);
2808 break;
2809 case IB_CM_REP_ERROR:
2810 srpt_cm_rep_error(cm_id);
2811 break;
2812 case IB_CM_DREQ_ERROR:
2813 printk(KERN_INFO "Received IB DREQ ERROR event.\n");
2814 break;
2815 case IB_CM_MRA_RECEIVED:
2816 printk(KERN_INFO "Received IB MRA event\n");
2817 break;
2818 default:
2819 printk(KERN_ERR "received unrecognized IB CM event %d\n",
2820 event->event);
2821 break;
2822 }
2823
2824 return ret;
2825}
2826
2827/**
2828 * srpt_perform_rdmas() - Perform IB RDMA.
2829 *
2830 * Returns zero upon success or a negative number upon failure.
2831 */
2832static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2833 struct srpt_send_ioctx *ioctx)
2834{
2835 struct ib_send_wr wr;
2836 struct ib_send_wr *bad_wr;
2837 struct rdma_iu *riu;
2838 int i;
2839 int ret;
2840 int sq_wr_avail;
2841 enum dma_data_direction dir;
2842 const int n_rdma = ioctx->n_rdma;
2843
2844 dir = ioctx->cmd.data_direction;
2845 if (dir == DMA_TO_DEVICE) {
2846 /* write */
2847 ret = -ENOMEM;
2848 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2849 if (sq_wr_avail < 0) {
2850 printk(KERN_WARNING "IB send queue full (needed %d)\n",
2851 n_rdma);
2852 goto out;
2853 }
2854 }
2855
2856 ioctx->rdma_aborted = false;
2857 ret = 0;
2858 riu = ioctx->rdma_ius;
2859 memset(&wr, 0, sizeof wr);
2860
2861 for (i = 0; i < n_rdma; ++i, ++riu) {
2862 if (dir == DMA_FROM_DEVICE) {
2863 wr.opcode = IB_WR_RDMA_WRITE;
2864 wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2865 SRPT_RDMA_WRITE_LAST :
2866 SRPT_RDMA_MID,
2867 ioctx->ioctx.index);
2868 } else {
2869 wr.opcode = IB_WR_RDMA_READ;
2870 wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2871 SRPT_RDMA_READ_LAST :
2872 SRPT_RDMA_MID,
2873 ioctx->ioctx.index);
2874 }
2875 wr.next = NULL;
2876 wr.wr.rdma.remote_addr = riu->raddr;
2877 wr.wr.rdma.rkey = riu->rkey;
2878 wr.num_sge = riu->sge_cnt;
2879 wr.sg_list = riu->sge;
2880
2881 /* only get completion event for the last rdma write */
2882 if (i == (n_rdma - 1) && dir == DMA_TO_DEVICE)
2883 wr.send_flags = IB_SEND_SIGNALED;
2884
2885 ret = ib_post_send(ch->qp, &wr, &bad_wr);
2886 if (ret)
2887 break;
2888 }
2889
2890 if (ret)
2891 printk(KERN_ERR "%s[%d]: ib_post_send() returned %d for %d/%d",
2892 __func__, __LINE__, ret, i, n_rdma);
2893 if (ret && i > 0) {
2894 wr.num_sge = 0;
2895 wr.wr_id = encode_wr_id(SRPT_RDMA_ABORT, ioctx->ioctx.index);
2896 wr.send_flags = IB_SEND_SIGNALED;
2897 while (ch->state == CH_LIVE &&
2898 ib_post_send(ch->qp, &wr, &bad_wr) != 0) {
2899 printk(KERN_INFO "Trying to abort failed RDMA transfer [%d]",
2900 ioctx->ioctx.index);
2901 msleep(1000);
2902 }
2903 while (ch->state != CH_RELEASING && !ioctx->rdma_aborted) {
2904 printk(KERN_INFO "Waiting until RDMA abort finished [%d]",
2905 ioctx->ioctx.index);
2906 msleep(1000);
2907 }
2908 }
2909out:
2910 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2911 atomic_add(n_rdma, &ch->sq_wr_avail);
2912 return ret;
2913}
2914
2915/**
2916 * srpt_xfer_data() - Start data transfer from initiator to target.
2917 */
2918static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2919 struct srpt_send_ioctx *ioctx)
2920{
2921 int ret;
2922
2923 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2924 if (ret) {
2925 printk(KERN_ERR "%s[%d] ret=%d\n", __func__, __LINE__, ret);
2926 goto out;
2927 }
2928
2929 ret = srpt_perform_rdmas(ch, ioctx);
2930 if (ret) {
2931 if (ret == -EAGAIN || ret == -ENOMEM)
2932 printk(KERN_INFO "%s[%d] queue full -- ret=%d\n",
2933 __func__, __LINE__, ret);
2934 else
2935 printk(KERN_ERR "%s[%d] fatal error -- ret=%d\n",
2936 __func__, __LINE__, ret);
2937 goto out_unmap;
2938 }
2939
2940out:
2941 return ret;
2942out_unmap:
2943 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2944 goto out;
2945}
2946
2947static int srpt_write_pending_status(struct se_cmd *se_cmd)
2948{
2949 struct srpt_send_ioctx *ioctx;
2950
2951 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2952 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2953}
2954
2955/*
2956 * srpt_write_pending() - Start data transfer from initiator to target (write).
2957 */
2958static int srpt_write_pending(struct se_cmd *se_cmd)
2959{
2960 struct srpt_rdma_ch *ch;
2961 struct srpt_send_ioctx *ioctx;
2962 enum srpt_command_state new_state;
2963 enum rdma_ch_state ch_state;
2964 int ret;
2965
2966 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2967
2968 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2969 WARN_ON(new_state == SRPT_STATE_DONE);
2970
2971 ch = ioctx->ch;
2972 BUG_ON(!ch);
2973
2974 ch_state = srpt_get_ch_state(ch);
2975 switch (ch_state) {
2976 case CH_CONNECTING:
2977 WARN(true, "unexpected channel state %d\n", ch_state);
2978 ret = -EINVAL;
2979 goto out;
2980 case CH_LIVE:
2981 break;
2982 case CH_DISCONNECTING:
2983 case CH_DRAINING:
2984 case CH_RELEASING:
2985 pr_debug("cmd with tag %lld: channel disconnecting\n",
2986 ioctx->tag);
2987 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2988 ret = -EINVAL;
2989 goto out;
2990 }
2991 ret = srpt_xfer_data(ch, ioctx);
2992
2993out:
2994 return ret;
2995}
2996
2997static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2998{
2999 switch (tcm_mgmt_status) {
3000 case TMR_FUNCTION_COMPLETE:
3001 return SRP_TSK_MGMT_SUCCESS;
3002 case TMR_FUNCTION_REJECTED:
3003 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
3004 }
3005 return SRP_TSK_MGMT_FAILED;
3006}
3007
3008/**
3009 * srpt_queue_response() - Transmits the response to a SCSI command.
3010 *
3011 * Callback function called by the TCM core. Must not block since it can be
3012 * invoked on the context of the IB completion handler.
3013 */
b79fafac 3014static void srpt_queue_response(struct se_cmd *cmd)
a42d985b
BVA
3015{
3016 struct srpt_rdma_ch *ch;
3017 struct srpt_send_ioctx *ioctx;
3018 enum srpt_command_state state;
3019 unsigned long flags;
3020 int ret;
3021 enum dma_data_direction dir;
3022 int resp_len;
3023 u8 srp_tm_status;
3024
a42d985b
BVA
3025 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3026 ch = ioctx->ch;
3027 BUG_ON(!ch);
3028
3029 spin_lock_irqsave(&ioctx->spinlock, flags);
3030 state = ioctx->state;
3031 switch (state) {
3032 case SRPT_STATE_NEW:
3033 case SRPT_STATE_DATA_IN:
3034 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
3035 break;
3036 case SRPT_STATE_MGMT:
3037 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
3038 break;
3039 default:
3040 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
3041 ch, ioctx->ioctx.index, ioctx->state);
3042 break;
3043 }
3044 spin_unlock_irqrestore(&ioctx->spinlock, flags);
3045
3046 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
3047 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
3048 atomic_inc(&ch->req_lim_delta);
3049 srpt_abort_cmd(ioctx);
b79fafac 3050 return;
a42d985b
BVA
3051 }
3052
3053 dir = ioctx->cmd.data_direction;
3054
3055 /* For read commands, transfer the data to the initiator. */
3056 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
3057 !ioctx->queue_status_only) {
3058 ret = srpt_xfer_data(ch, ioctx);
3059 if (ret) {
3060 printk(KERN_ERR "xfer_data failed for tag %llu\n",
3061 ioctx->tag);
b79fafac 3062 return;
a42d985b
BVA
3063 }
3064 }
3065
3066 if (state != SRPT_STATE_MGMT)
3067 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->tag,
3068 cmd->scsi_status);
3069 else {
3070 srp_tm_status
3071 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
3072 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
3073 ioctx->tag);
3074 }
3075 ret = srpt_post_send(ch, ioctx, resp_len);
3076 if (ret) {
3077 printk(KERN_ERR "sending cmd response failed for tag %llu\n",
3078 ioctx->tag);
3079 srpt_unmap_sg_to_ib_sge(ch, ioctx);
3080 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
9474b043 3081 target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
a42d985b 3082 }
b79fafac 3083}
a42d985b 3084
b79fafac
JE
3085static int srpt_queue_data_in(struct se_cmd *cmd)
3086{
3087 srpt_queue_response(cmd);
3088 return 0;
3089}
3090
3091static void srpt_queue_tm_rsp(struct se_cmd *cmd)
3092{
3093 srpt_queue_response(cmd);
a42d985b
BVA
3094}
3095
17b1c6c8
NB
3096static void srpt_aborted_task(struct se_cmd *cmd)
3097{
3098 struct srpt_send_ioctx *ioctx = container_of(cmd,
3099 struct srpt_send_ioctx, cmd);
3100
3101 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
3102}
3103
a42d985b
BVA
3104static int srpt_queue_status(struct se_cmd *cmd)
3105{
3106 struct srpt_send_ioctx *ioctx;
3107
3108 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3109 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
3110 if (cmd->se_cmd_flags &
3111 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
3112 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
3113 ioctx->queue_status_only = true;
b79fafac
JE
3114 srpt_queue_response(cmd);
3115 return 0;
a42d985b
BVA
3116}
3117
3118static void srpt_refresh_port_work(struct work_struct *work)
3119{
3120 struct srpt_port *sport = container_of(work, struct srpt_port, work);
3121
3122 srpt_refresh_port(sport);
3123}
3124
3125static int srpt_ch_list_empty(struct srpt_device *sdev)
3126{
3127 int res;
3128
3129 spin_lock_irq(&sdev->spinlock);
3130 res = list_empty(&sdev->rch_list);
3131 spin_unlock_irq(&sdev->spinlock);
3132
3133 return res;
3134}
3135
3136/**
3137 * srpt_release_sdev() - Free the channel resources associated with a target.
3138 */
3139static int srpt_release_sdev(struct srpt_device *sdev)
3140{
3141 struct srpt_rdma_ch *ch, *tmp_ch;
3142 int res;
3143
3144 WARN_ON_ONCE(irqs_disabled());
3145
3146 BUG_ON(!sdev);
3147
3148 spin_lock_irq(&sdev->spinlock);
3149 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
3150 __srpt_close_ch(ch);
3151 spin_unlock_irq(&sdev->spinlock);
3152
3153 res = wait_event_interruptible(sdev->ch_releaseQ,
3154 srpt_ch_list_empty(sdev));
3155 if (res)
3156 printk(KERN_ERR "%s: interrupted.\n", __func__);
3157
3158 return 0;
3159}
3160
3161static struct srpt_port *__srpt_lookup_port(const char *name)
3162{
3163 struct ib_device *dev;
3164 struct srpt_device *sdev;
3165 struct srpt_port *sport;
3166 int i;
3167
3168 list_for_each_entry(sdev, &srpt_dev_list, list) {
3169 dev = sdev->device;
3170 if (!dev)
3171 continue;
3172
3173 for (i = 0; i < dev->phys_port_cnt; i++) {
3174 sport = &sdev->port[i];
3175
3176 if (!strcmp(sport->port_guid, name))
3177 return sport;
3178 }
3179 }
3180
3181 return NULL;
3182}
3183
3184static struct srpt_port *srpt_lookup_port(const char *name)
3185{
3186 struct srpt_port *sport;
3187
3188 spin_lock(&srpt_dev_lock);
3189 sport = __srpt_lookup_port(name);
3190 spin_unlock(&srpt_dev_lock);
3191
3192 return sport;
3193}
3194
3195/**
3196 * srpt_add_one() - Infiniband device addition callback function.
3197 */
3198static void srpt_add_one(struct ib_device *device)
3199{
3200 struct srpt_device *sdev;
3201 struct srpt_port *sport;
3202 struct ib_srq_init_attr srq_attr;
3203 int i;
3204
3205 pr_debug("device = %p, device->dma_ops = %p\n", device,
3206 device->dma_ops);
3207
3208 sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
3209 if (!sdev)
3210 goto err;
3211
3212 sdev->device = device;
3213 INIT_LIST_HEAD(&sdev->rch_list);
3214 init_waitqueue_head(&sdev->ch_releaseQ);
3215 spin_lock_init(&sdev->spinlock);
3216
3217 if (ib_query_device(device, &sdev->dev_attr))
3218 goto free_dev;
3219
3220 sdev->pd = ib_alloc_pd(device);
3221 if (IS_ERR(sdev->pd))
3222 goto free_dev;
3223
3224 sdev->mr = ib_get_dma_mr(sdev->pd, IB_ACCESS_LOCAL_WRITE);
3225 if (IS_ERR(sdev->mr))
3226 goto err_pd;
3227
3228 sdev->srq_size = min(srpt_srq_size, sdev->dev_attr.max_srq_wr);
3229
3230 srq_attr.event_handler = srpt_srq_event;
3231 srq_attr.srq_context = (void *)sdev;
3232 srq_attr.attr.max_wr = sdev->srq_size;
3233 srq_attr.attr.max_sge = 1;
3234 srq_attr.attr.srq_limit = 0;
6f360336 3235 srq_attr.srq_type = IB_SRQT_BASIC;
a42d985b
BVA
3236
3237 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
3238 if (IS_ERR(sdev->srq))
3239 goto err_mr;
3240
3241 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
3242 __func__, sdev->srq_size, sdev->dev_attr.max_srq_wr,
3243 device->name);
3244
3245 if (!srpt_service_guid)
3246 srpt_service_guid = be64_to_cpu(device->node_guid);
3247
3248 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
3249 if (IS_ERR(sdev->cm_id))
3250 goto err_srq;
3251
3252 /* print out target login information */
3253 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
3254 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
3255 srpt_service_guid, srpt_service_guid);
3256
3257 /*
3258 * We do not have a consistent service_id (ie. also id_ext of target_id)
3259 * to identify this target. We currently use the guid of the first HCA
3260 * in the system as service_id; therefore, the target_id will change
3261 * if this HCA is gone bad and replaced by different HCA
3262 */
3263 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0, NULL))
3264 goto err_cm;
3265
3266 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
3267 srpt_event_handler);
3268 if (ib_register_event_handler(&sdev->event_handler))
3269 goto err_cm;
3270
3271 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
3272 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
3273 sizeof(*sdev->ioctx_ring[0]),
3274 srp_max_req_size, DMA_FROM_DEVICE);
3275 if (!sdev->ioctx_ring)
3276 goto err_event;
3277
3278 for (i = 0; i < sdev->srq_size; ++i)
3279 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
3280
f225066b 3281 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
a42d985b
BVA
3282
3283 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3284 sport = &sdev->port[i - 1];
3285 sport->sdev = sdev;
3286 sport->port = i;
3287 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3288 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3289 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3290 INIT_WORK(&sport->work, srpt_refresh_port_work);
3291 INIT_LIST_HEAD(&sport->port_acl_list);
3292 spin_lock_init(&sport->port_acl_lock);
3293
3294 if (srpt_refresh_port(sport)) {
3295 printk(KERN_ERR "MAD registration failed for %s-%d.\n",
3296 srpt_sdev_name(sdev), i);
3297 goto err_ring;
3298 }
3299 snprintf(sport->port_guid, sizeof(sport->port_guid),
3300 "0x%016llx%016llx",
3301 be64_to_cpu(sport->gid.global.subnet_prefix),
3302 be64_to_cpu(sport->gid.global.interface_id));
3303 }
3304
3305 spin_lock(&srpt_dev_lock);
3306 list_add_tail(&sdev->list, &srpt_dev_list);
3307 spin_unlock(&srpt_dev_lock);
3308
3309out:
3310 ib_set_client_data(device, &srpt_client, sdev);
3311 pr_debug("added %s.\n", device->name);
3312 return;
3313
3314err_ring:
3315 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3316 sdev->srq_size, srp_max_req_size,
3317 DMA_FROM_DEVICE);
3318err_event:
3319 ib_unregister_event_handler(&sdev->event_handler);
3320err_cm:
3321 ib_destroy_cm_id(sdev->cm_id);
3322err_srq:
3323 ib_destroy_srq(sdev->srq);
3324err_mr:
3325 ib_dereg_mr(sdev->mr);
3326err_pd:
3327 ib_dealloc_pd(sdev->pd);
3328free_dev:
3329 kfree(sdev);
3330err:
3331 sdev = NULL;
3332 printk(KERN_INFO "%s(%s) failed.\n", __func__, device->name);
3333 goto out;
3334}
3335
3336/**
3337 * srpt_remove_one() - InfiniBand device removal callback function.
3338 */
3339static void srpt_remove_one(struct ib_device *device)
3340{
3341 struct srpt_device *sdev;
3342 int i;
3343
3344 sdev = ib_get_client_data(device, &srpt_client);
3345 if (!sdev) {
3346 printk(KERN_INFO "%s(%s): nothing to do.\n", __func__,
3347 device->name);
3348 return;
3349 }
3350
3351 srpt_unregister_mad_agent(sdev);
3352
3353 ib_unregister_event_handler(&sdev->event_handler);
3354
3355 /* Cancel any work queued by the just unregistered IB event handler. */
3356 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3357 cancel_work_sync(&sdev->port[i].work);
3358
3359 ib_destroy_cm_id(sdev->cm_id);
3360
3361 /*
3362 * Unregistering a target must happen after destroying sdev->cm_id
3363 * such that no new SRP_LOGIN_REQ information units can arrive while
3364 * destroying the target.
3365 */
3366 spin_lock(&srpt_dev_lock);
3367 list_del(&sdev->list);
3368 spin_unlock(&srpt_dev_lock);
3369 srpt_release_sdev(sdev);
3370
3371 ib_destroy_srq(sdev->srq);
3372 ib_dereg_mr(sdev->mr);
3373 ib_dealloc_pd(sdev->pd);
3374
3375 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3376 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
3377 sdev->ioctx_ring = NULL;
3378 kfree(sdev);
3379}
3380
3381static struct ib_client srpt_client = {
3382 .name = DRV_NAME,
3383 .add = srpt_add_one,
3384 .remove = srpt_remove_one
3385};
3386
3387static int srpt_check_true(struct se_portal_group *se_tpg)
3388{
3389 return 1;
3390}
3391
3392static int srpt_check_false(struct se_portal_group *se_tpg)
3393{
3394 return 0;
3395}
3396
3397static char *srpt_get_fabric_name(void)
3398{
3399 return "srpt";
3400}
3401
3402static u8 srpt_get_fabric_proto_ident(struct se_portal_group *se_tpg)
3403{
3404 return SCSI_TRANSPORTID_PROTOCOLID_SRP;
3405}
3406
3407static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3408{
3409 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3410
3411 return sport->port_guid;
3412}
3413
3414static u16 srpt_get_tag(struct se_portal_group *tpg)
3415{
3416 return 1;
3417}
3418
3419static u32 srpt_get_default_depth(struct se_portal_group *se_tpg)
3420{
3421 return 1;
3422}
3423
3424static u32 srpt_get_pr_transport_id(struct se_portal_group *se_tpg,
3425 struct se_node_acl *se_nacl,
3426 struct t10_pr_registration *pr_reg,
3427 int *format_code, unsigned char *buf)
3428{
3429 struct srpt_node_acl *nacl;
3430 struct spc_rdma_transport_id *tr_id;
3431
3432 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3433 tr_id = (void *)buf;
3434 tr_id->protocol_identifier = SCSI_TRANSPORTID_PROTOCOLID_SRP;
3435 memcpy(tr_id->i_port_id, nacl->i_port_id, sizeof(tr_id->i_port_id));
3436 return sizeof(*tr_id);
3437}
3438
3439static u32 srpt_get_pr_transport_id_len(struct se_portal_group *se_tpg,
3440 struct se_node_acl *se_nacl,
3441 struct t10_pr_registration *pr_reg,
3442 int *format_code)
3443{
3444 *format_code = 0;
3445 return sizeof(struct spc_rdma_transport_id);
3446}
3447
3448static char *srpt_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
3449 const char *buf, u32 *out_tid_len,
3450 char **port_nexus_ptr)
3451{
3452 struct spc_rdma_transport_id *tr_id;
3453
3454 *port_nexus_ptr = NULL;
3455 *out_tid_len = sizeof(struct spc_rdma_transport_id);
3456 tr_id = (void *)buf;
3457 return (char *)tr_id->i_port_id;
3458}
3459
3460static struct se_node_acl *srpt_alloc_fabric_acl(struct se_portal_group *se_tpg)
3461{
3462 struct srpt_node_acl *nacl;
3463
3464 nacl = kzalloc(sizeof(struct srpt_node_acl), GFP_KERNEL);
3465 if (!nacl) {
7367d99b 3466 printk(KERN_ERR "Unable to allocate struct srpt_node_acl\n");
a42d985b
BVA
3467 return NULL;
3468 }
3469
3470 return &nacl->nacl;
3471}
3472
3473static void srpt_release_fabric_acl(struct se_portal_group *se_tpg,
3474 struct se_node_acl *se_nacl)
3475{
3476 struct srpt_node_acl *nacl;
3477
3478 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3479 kfree(nacl);
3480}
3481
3482static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3483{
3484 return 1;
3485}
3486
3487static void srpt_release_cmd(struct se_cmd *se_cmd)
3488{
9474b043
NB
3489 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3490 struct srpt_send_ioctx, cmd);
3491 struct srpt_rdma_ch *ch = ioctx->ch;
3492 unsigned long flags;
3493
3494 WARN_ON(ioctx->state != SRPT_STATE_DONE);
3495 WARN_ON(ioctx->mapped_sg_count != 0);
3496
3497 if (ioctx->n_rbuf > 1) {
3498 kfree(ioctx->rbufs);
3499 ioctx->rbufs = NULL;
3500 ioctx->n_rbuf = 0;
3501 }
3502
3503 spin_lock_irqsave(&ch->spinlock, flags);
3504 list_add(&ioctx->free_list, &ch->free_list);
3505 spin_unlock_irqrestore(&ch->spinlock, flags);
a42d985b
BVA
3506}
3507
a42d985b
BVA
3508/**
3509 * srpt_close_session() - Forcibly close a session.
3510 *
3511 * Callback function invoked by the TCM core to clean up sessions associated
3512 * with a node ACL when the user invokes
3513 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3514 */
3515static void srpt_close_session(struct se_session *se_sess)
3516{
3517 DECLARE_COMPLETION_ONSTACK(release_done);
3518 struct srpt_rdma_ch *ch;
3519 struct srpt_device *sdev;
3520 int res;
3521
3522 ch = se_sess->fabric_sess_ptr;
3523 WARN_ON(ch->sess != se_sess);
3524
3525 pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
3526
3527 sdev = ch->sport->sdev;
3528 spin_lock_irq(&sdev->spinlock);
3529 BUG_ON(ch->release_done);
3530 ch->release_done = &release_done;
3531 __srpt_close_ch(ch);
3532 spin_unlock_irq(&sdev->spinlock);
3533
3534 res = wait_for_completion_timeout(&release_done, 60 * HZ);
3535 WARN_ON(res <= 0);
3536}
3537
a42d985b
BVA
3538/**
3539 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3540 *
3541 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3542 * This object represents an arbitrary integer used to uniquely identify a
3543 * particular attached remote initiator port to a particular SCSI target port
3544 * within a particular SCSI target device within a particular SCSI instance.
3545 */
3546static u32 srpt_sess_get_index(struct se_session *se_sess)
3547{
3548 return 0;
3549}
3550
3551static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3552{
3553}
3554
3555static u32 srpt_get_task_tag(struct se_cmd *se_cmd)
3556{
3557 struct srpt_send_ioctx *ioctx;
3558
3559 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3560 return ioctx->tag;
3561}
3562
3563/* Note: only used from inside debug printk's by the TCM core. */
3564static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3565{
3566 struct srpt_send_ioctx *ioctx;
3567
3568 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3569 return srpt_get_cmd_state(ioctx);
3570}
3571
a42d985b
BVA
3572/**
3573 * srpt_parse_i_port_id() - Parse an initiator port ID.
3574 * @name: ASCII representation of a 128-bit initiator port ID.
3575 * @i_port_id: Binary 128-bit port ID.
3576 */
3577static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3578{
3579 const char *p;
3580 unsigned len, count, leading_zero_bytes;
3581 int ret, rc;
3582
3583 p = name;
3584 if (strnicmp(p, "0x", 2) == 0)
3585 p += 2;
3586 ret = -EINVAL;
3587 len = strlen(p);
3588 if (len % 2)
3589 goto out;
3590 count = min(len / 2, 16U);
3591 leading_zero_bytes = 16 - count;
3592 memset(i_port_id, 0, leading_zero_bytes);
3593 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3594 if (rc < 0)
3595 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3596 ret = 0;
3597out:
3598 return ret;
3599}
3600
3601/*
3602 * configfs callback function invoked for
3603 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3604 */
3605static struct se_node_acl *srpt_make_nodeacl(struct se_portal_group *tpg,
3606 struct config_group *group,
3607 const char *name)
3608{
3609 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3610 struct se_node_acl *se_nacl, *se_nacl_new;
3611 struct srpt_node_acl *nacl;
3612 int ret = 0;
3613 u32 nexus_depth = 1;
3614 u8 i_port_id[16];
3615
3616 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
3617 printk(KERN_ERR "invalid initiator port ID %s\n", name);
3618 ret = -EINVAL;
3619 goto err;
3620 }
3621
3622 se_nacl_new = srpt_alloc_fabric_acl(tpg);
3623 if (!se_nacl_new) {
3624 ret = -ENOMEM;
3625 goto err;
3626 }
3627 /*
3628 * nacl_new may be released by core_tpg_add_initiator_node_acl()
3629 * when converting a node ACL from demo mode to explict
3630 */
3631 se_nacl = core_tpg_add_initiator_node_acl(tpg, se_nacl_new, name,
3632 nexus_depth);
3633 if (IS_ERR(se_nacl)) {
3634 ret = PTR_ERR(se_nacl);
3635 goto err;
3636 }
3637 /* Locate our struct srpt_node_acl and set sdev and i_port_id. */
3638 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3639 memcpy(&nacl->i_port_id[0], &i_port_id[0], 16);
3640 nacl->sport = sport;
3641
3642 spin_lock_irq(&sport->port_acl_lock);
3643 list_add_tail(&nacl->list, &sport->port_acl_list);
3644 spin_unlock_irq(&sport->port_acl_lock);
3645
3646 return se_nacl;
3647err:
3648 return ERR_PTR(ret);
3649}
3650
3651/*
3652 * configfs callback function invoked for
3653 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3654 */
3655static void srpt_drop_nodeacl(struct se_node_acl *se_nacl)
3656{
3657 struct srpt_node_acl *nacl;
3658 struct srpt_device *sdev;
3659 struct srpt_port *sport;
3660
3661 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3662 sport = nacl->sport;
3663 sdev = sport->sdev;
3664 spin_lock_irq(&sport->port_acl_lock);
3665 list_del(&nacl->list);
3666 spin_unlock_irq(&sport->port_acl_lock);
3667 core_tpg_del_initiator_node_acl(&sport->port_tpg_1, se_nacl, 1);
3668 srpt_release_fabric_acl(NULL, se_nacl);
3669}
3670
3671static ssize_t srpt_tpg_attrib_show_srp_max_rdma_size(
3672 struct se_portal_group *se_tpg,
3673 char *page)
3674{
3675 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3676
3677 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3678}
3679
3680static ssize_t srpt_tpg_attrib_store_srp_max_rdma_size(
3681 struct se_portal_group *se_tpg,
3682 const char *page,
3683 size_t count)
3684{
3685 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3686 unsigned long val;
3687 int ret;
3688
9d8abf45 3689 ret = kstrtoul(page, 0, &val);
a42d985b 3690 if (ret < 0) {
9d8abf45 3691 pr_err("kstrtoul() failed with ret: %d\n", ret);
a42d985b
BVA
3692 return -EINVAL;
3693 }
3694 if (val > MAX_SRPT_RDMA_SIZE) {
3695 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3696 MAX_SRPT_RDMA_SIZE);
3697 return -EINVAL;
3698 }
3699 if (val < DEFAULT_MAX_RDMA_SIZE) {
3700 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3701 val, DEFAULT_MAX_RDMA_SIZE);
3702 return -EINVAL;
3703 }
3704 sport->port_attrib.srp_max_rdma_size = val;
3705
3706 return count;
3707}
3708
3709TF_TPG_ATTRIB_ATTR(srpt, srp_max_rdma_size, S_IRUGO | S_IWUSR);
3710
3711static ssize_t srpt_tpg_attrib_show_srp_max_rsp_size(
3712 struct se_portal_group *se_tpg,
3713 char *page)
3714{
3715 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3716
3717 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3718}
3719
3720static ssize_t srpt_tpg_attrib_store_srp_max_rsp_size(
3721 struct se_portal_group *se_tpg,
3722 const char *page,
3723 size_t count)
3724{
3725 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3726 unsigned long val;
3727 int ret;
3728
9d8abf45 3729 ret = kstrtoul(page, 0, &val);
a42d985b 3730 if (ret < 0) {
9d8abf45 3731 pr_err("kstrtoul() failed with ret: %d\n", ret);
a42d985b
BVA
3732 return -EINVAL;
3733 }
3734 if (val > MAX_SRPT_RSP_SIZE) {
3735 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3736 MAX_SRPT_RSP_SIZE);
3737 return -EINVAL;
3738 }
3739 if (val < MIN_MAX_RSP_SIZE) {
3740 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3741 MIN_MAX_RSP_SIZE);
3742 return -EINVAL;
3743 }
3744 sport->port_attrib.srp_max_rsp_size = val;
3745
3746 return count;
3747}
3748
3749TF_TPG_ATTRIB_ATTR(srpt, srp_max_rsp_size, S_IRUGO | S_IWUSR);
3750
3751static ssize_t srpt_tpg_attrib_show_srp_sq_size(
3752 struct se_portal_group *se_tpg,
3753 char *page)
3754{
3755 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3756
3757 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3758}
3759
3760static ssize_t srpt_tpg_attrib_store_srp_sq_size(
3761 struct se_portal_group *se_tpg,
3762 const char *page,
3763 size_t count)
3764{
3765 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3766 unsigned long val;
3767 int ret;
3768
9d8abf45 3769 ret = kstrtoul(page, 0, &val);
a42d985b 3770 if (ret < 0) {
9d8abf45 3771 pr_err("kstrtoul() failed with ret: %d\n", ret);
a42d985b
BVA
3772 return -EINVAL;
3773 }
3774 if (val > MAX_SRPT_SRQ_SIZE) {
3775 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3776 MAX_SRPT_SRQ_SIZE);
3777 return -EINVAL;
3778 }
3779 if (val < MIN_SRPT_SRQ_SIZE) {
3780 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3781 MIN_SRPT_SRQ_SIZE);
3782 return -EINVAL;
3783 }
3784 sport->port_attrib.srp_sq_size = val;
3785
3786 return count;
3787}
3788
3789TF_TPG_ATTRIB_ATTR(srpt, srp_sq_size, S_IRUGO | S_IWUSR);
3790
3791static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
3792 &srpt_tpg_attrib_srp_max_rdma_size.attr,
3793 &srpt_tpg_attrib_srp_max_rsp_size.attr,
3794 &srpt_tpg_attrib_srp_sq_size.attr,
3795 NULL,
3796};
3797
3798static ssize_t srpt_tpg_show_enable(
3799 struct se_portal_group *se_tpg,
3800 char *page)
3801{
3802 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3803
3804 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3805}
3806
3807static ssize_t srpt_tpg_store_enable(
3808 struct se_portal_group *se_tpg,
3809 const char *page,
3810 size_t count)
3811{
3812 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3813 unsigned long tmp;
3814 int ret;
3815
9d8abf45 3816 ret = kstrtoul(page, 0, &tmp);
a42d985b
BVA
3817 if (ret < 0) {
3818 printk(KERN_ERR "Unable to extract srpt_tpg_store_enable\n");
3819 return -EINVAL;
3820 }
3821
3822 if ((tmp != 0) && (tmp != 1)) {
3823 printk(KERN_ERR "Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
3824 return -EINVAL;
3825 }
3826 if (tmp == 1)
3827 sport->enabled = true;
3828 else
3829 sport->enabled = false;
3830
3831 return count;
3832}
3833
3834TF_TPG_BASE_ATTR(srpt, enable, S_IRUGO | S_IWUSR);
3835
3836static struct configfs_attribute *srpt_tpg_attrs[] = {
3837 &srpt_tpg_enable.attr,
3838 NULL,
3839};
3840
3841/**
3842 * configfs callback invoked for
3843 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3844 */
3845static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3846 struct config_group *group,
3847 const char *name)
3848{
3849 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3850 int res;
3851
3852 /* Initialize sport->port_wwn and sport->port_tpg_1 */
3853 res = core_tpg_register(&srpt_target->tf_ops, &sport->port_wwn,
3854 &sport->port_tpg_1, sport, TRANSPORT_TPG_TYPE_NORMAL);
3855 if (res)
3856 return ERR_PTR(res);
3857
3858 return &sport->port_tpg_1;
3859}
3860
3861/**
3862 * configfs callback invoked for
3863 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3864 */
3865static void srpt_drop_tpg(struct se_portal_group *tpg)
3866{
3867 struct srpt_port *sport = container_of(tpg,
3868 struct srpt_port, port_tpg_1);
3869
3870 sport->enabled = false;
3871 core_tpg_deregister(&sport->port_tpg_1);
3872}
3873
3874/**
3875 * configfs callback invoked for
3876 * mkdir /sys/kernel/config/target/$driver/$port
3877 */
3878static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3879 struct config_group *group,
3880 const char *name)
3881{
3882 struct srpt_port *sport;
3883 int ret;
3884
3885 sport = srpt_lookup_port(name);
3886 pr_debug("make_tport(%s)\n", name);
3887 ret = -EINVAL;
3888 if (!sport)
3889 goto err;
3890
3891 return &sport->port_wwn;
3892
3893err:
3894 return ERR_PTR(ret);
3895}
3896
3897/**
3898 * configfs callback invoked for
3899 * rmdir /sys/kernel/config/target/$driver/$port
3900 */
3901static void srpt_drop_tport(struct se_wwn *wwn)
3902{
3903 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3904
3905 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3906}
3907
3908static ssize_t srpt_wwn_show_attr_version(struct target_fabric_configfs *tf,
3909 char *buf)
3910{
3911 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3912}
3913
3914TF_WWN_ATTR_RO(srpt, version);
3915
3916static struct configfs_attribute *srpt_wwn_attrs[] = {
3917 &srpt_wwn_version.attr,
3918 NULL,
3919};
3920
3921static struct target_core_fabric_ops srpt_template = {
3922 .get_fabric_name = srpt_get_fabric_name,
3923 .get_fabric_proto_ident = srpt_get_fabric_proto_ident,
3924 .tpg_get_wwn = srpt_get_fabric_wwn,
3925 .tpg_get_tag = srpt_get_tag,
3926 .tpg_get_default_depth = srpt_get_default_depth,
3927 .tpg_get_pr_transport_id = srpt_get_pr_transport_id,
3928 .tpg_get_pr_transport_id_len = srpt_get_pr_transport_id_len,
3929 .tpg_parse_pr_out_transport_id = srpt_parse_pr_out_transport_id,
3930 .tpg_check_demo_mode = srpt_check_false,
3931 .tpg_check_demo_mode_cache = srpt_check_true,
3932 .tpg_check_demo_mode_write_protect = srpt_check_true,
3933 .tpg_check_prod_mode_write_protect = srpt_check_false,
3934 .tpg_alloc_fabric_acl = srpt_alloc_fabric_acl,
3935 .tpg_release_fabric_acl = srpt_release_fabric_acl,
3936 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3937 .release_cmd = srpt_release_cmd,
3938 .check_stop_free = srpt_check_stop_free,
3939 .shutdown_session = srpt_shutdown_session,
3940 .close_session = srpt_close_session,
a42d985b
BVA
3941 .sess_get_index = srpt_sess_get_index,
3942 .sess_get_initiator_sid = NULL,
3943 .write_pending = srpt_write_pending,
3944 .write_pending_status = srpt_write_pending_status,
3945 .set_default_node_attributes = srpt_set_default_node_attrs,
3946 .get_task_tag = srpt_get_task_tag,
3947 .get_cmd_state = srpt_get_tcm_cmd_state,
b79fafac 3948 .queue_data_in = srpt_queue_data_in,
a42d985b 3949 .queue_status = srpt_queue_status,
b79fafac 3950 .queue_tm_rsp = srpt_queue_tm_rsp,
17b1c6c8 3951 .aborted_task = srpt_aborted_task,
a42d985b
BVA
3952 /*
3953 * Setup function pointers for generic logic in
3954 * target_core_fabric_configfs.c
3955 */
3956 .fabric_make_wwn = srpt_make_tport,
3957 .fabric_drop_wwn = srpt_drop_tport,
3958 .fabric_make_tpg = srpt_make_tpg,
3959 .fabric_drop_tpg = srpt_drop_tpg,
3960 .fabric_post_link = NULL,
3961 .fabric_pre_unlink = NULL,
3962 .fabric_make_np = NULL,
3963 .fabric_drop_np = NULL,
3964 .fabric_make_nodeacl = srpt_make_nodeacl,
3965 .fabric_drop_nodeacl = srpt_drop_nodeacl,
3966};
3967
3968/**
3969 * srpt_init_module() - Kernel module initialization.
3970 *
3971 * Note: Since ib_register_client() registers callback functions, and since at
3972 * least one of these callback functions (srpt_add_one()) calls target core
3973 * functions, this driver must be registered with the target core before
3974 * ib_register_client() is called.
3975 */
3976static int __init srpt_init_module(void)
3977{
3978 int ret;
3979
3980 ret = -EINVAL;
3981 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
3982 printk(KERN_ERR "invalid value %d for kernel module parameter"
3983 " srp_max_req_size -- must be at least %d.\n",
3984 srp_max_req_size, MIN_MAX_REQ_SIZE);
3985 goto out;
3986 }
3987
3988 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3989 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
3990 printk(KERN_ERR "invalid value %d for kernel module parameter"
3991 " srpt_srq_size -- must be in the range [%d..%d].\n",
3992 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3993 goto out;
3994 }
3995
a42d985b 3996 srpt_target = target_fabric_configfs_init(THIS_MODULE, "srpt");
3af33637 3997 if (IS_ERR(srpt_target)) {
a42d985b 3998 printk(KERN_ERR "couldn't register\n");
3af33637 3999 ret = PTR_ERR(srpt_target);
a42d985b
BVA
4000 goto out;
4001 }
4002
4003 srpt_target->tf_ops = srpt_template;
4004
a42d985b
BVA
4005 /*
4006 * Set up default attribute lists.
4007 */
4008 srpt_target->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = srpt_wwn_attrs;
4009 srpt_target->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = srpt_tpg_attrs;
4010 srpt_target->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = srpt_tpg_attrib_attrs;
4011 srpt_target->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
4012 srpt_target->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
4013 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
4014 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
4015 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
4016 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
4017
4018 ret = target_fabric_configfs_register(srpt_target);
4019 if (ret < 0) {
4020 printk(KERN_ERR "couldn't register\n");
4021 goto out_free_target;
4022 }
4023
4024 ret = ib_register_client(&srpt_client);
4025 if (ret) {
4026 printk(KERN_ERR "couldn't register IB client\n");
4027 goto out_unregister_target;
4028 }
4029
4030 return 0;
4031
4032out_unregister_target:
4033 target_fabric_configfs_deregister(srpt_target);
4034 srpt_target = NULL;
4035out_free_target:
4036 if (srpt_target)
4037 target_fabric_configfs_free(srpt_target);
4038out:
4039 return ret;
4040}
4041
4042static void __exit srpt_cleanup_module(void)
4043{
4044 ib_unregister_client(&srpt_client);
4045 target_fabric_configfs_deregister(srpt_target);
4046 srpt_target = NULL;
4047}
4048
4049module_init(srpt_init_module);
4050module_exit(srpt_cleanup_module);