blob: 2b73d43cd6919a33c6b491467ea02a5d7af8202f [file] [log] [blame]
Bart Van Asschea42d9852011-10-14 01:30:46 +00001/*
2 * Copyright (c) 2006 - 2009 Mellanox Technology Inc. All rights reserved.
3 * Copyright (C) 2008 - 2011 Bart Van Assche <bvanassche@acm.org>.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 *
33 */
34
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/slab.h>
38#include <linux/err.h>
39#include <linux/ctype.h>
40#include <linux/kthread.h>
41#include <linux/string.h>
42#include <linux/delay.h>
43#include <linux/atomic.h>
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;
Roland Dreier486d8b92012-02-02 12:55:58 -080072static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
73static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
Bart Van Asschea42d9852011-10-14 01:30:46 +000074
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);
Jesper Juhl715252d2012-02-04 23:49:40 +0100690 ring = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000691out:
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{
1081 struct se_cmd *cmd;
1082 struct scatterlist *sg, *sg_orig;
1083 int sg_cnt;
1084 enum dma_data_direction dir;
1085 struct rdma_iu *riu;
1086 struct srp_direct_buf *db;
1087 dma_addr_t dma_addr;
1088 struct ib_sge *sge;
1089 u64 raddr;
1090 u32 rsize;
1091 u32 tsize;
1092 u32 dma_len;
1093 int count, nrdma;
1094 int i, j, k;
1095
1096 BUG_ON(!ch);
1097 BUG_ON(!ioctx);
1098 cmd = &ioctx->cmd;
1099 dir = cmd->data_direction;
1100 BUG_ON(dir == DMA_NONE);
1101
1102 transport_do_task_sg_chain(cmd);
1103 ioctx->sg = sg = sg_orig = cmd->t_tasks_sg_chained;
1104 ioctx->sg_cnt = sg_cnt = cmd->t_tasks_sg_chained_no;
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;
1128 dma_len = sg_dma_len(&sg[0]);
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);
1159 dma_len = sg_dma_len(sg);
1160 }
1161 }
1162 } else {
1163 tsize -= rsize;
1164 dma_len -= rsize;
1165 rsize = 0;
1166 }
1167
1168 ++riu->sge_cnt;
1169
1170 if (rsize > 0 && riu->sge_cnt == SRPT_DEF_SG_PER_WQE) {
1171 ++ioctx->n_rdma;
1172 riu->sge =
1173 kmalloc(riu->sge_cnt * sizeof *riu->sge,
1174 GFP_KERNEL);
1175 if (!riu->sge)
1176 goto free_mem;
1177
1178 ++riu;
1179 riu->sge_cnt = 0;
1180 riu->raddr = raddr;
1181 riu->rkey = be32_to_cpu(db->key);
1182 }
1183 }
1184
1185 ++ioctx->n_rdma;
1186 riu->sge = kmalloc(riu->sge_cnt * sizeof *riu->sge,
1187 GFP_KERNEL);
1188 if (!riu->sge)
1189 goto free_mem;
1190 }
1191
1192 db = ioctx->rbufs;
1193 tsize = cmd->data_length;
1194 riu = ioctx->rdma_ius;
1195 sg = sg_orig;
1196 dma_len = sg_dma_len(&sg[0]);
1197 dma_addr = sg_dma_address(&sg[0]);
1198
1199 /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1200 for (i = 0, j = 0;
1201 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1202 rsize = be32_to_cpu(db->len);
1203 sge = riu->sge;
1204 k = 0;
1205
1206 while (rsize > 0 && tsize > 0) {
1207 sge->addr = dma_addr;
1208 sge->lkey = ch->sport->sdev->mr->lkey;
1209
1210 if (rsize >= dma_len) {
1211 sge->length =
1212 (tsize < dma_len) ? tsize : dma_len;
1213 tsize -= dma_len;
1214 rsize -= dma_len;
1215
1216 if (tsize > 0) {
1217 ++j;
1218 if (j < count) {
1219 sg = sg_next(sg);
1220 dma_len = sg_dma_len(sg);
1221 dma_addr = sg_dma_address(sg);
1222 }
1223 }
1224 } else {
1225 sge->length = (tsize < rsize) ? tsize : rsize;
1226 tsize -= rsize;
1227 dma_len -= rsize;
1228 dma_addr += rsize;
1229 rsize = 0;
1230 }
1231
1232 ++k;
1233 if (k == riu->sge_cnt && rsize > 0 && tsize > 0) {
1234 ++riu;
1235 sge = riu->sge;
1236 k = 0;
1237 } else if (rsize > 0 && tsize > 0)
1238 ++sge;
1239 }
1240 }
1241
1242 return 0;
1243
1244free_mem:
1245 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1246
1247 return -ENOMEM;
1248}
1249
1250/**
1251 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1252 */
1253static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1254{
1255 struct srpt_send_ioctx *ioctx;
1256 unsigned long flags;
1257
1258 BUG_ON(!ch);
1259
1260 ioctx = NULL;
1261 spin_lock_irqsave(&ch->spinlock, flags);
1262 if (!list_empty(&ch->free_list)) {
1263 ioctx = list_first_entry(&ch->free_list,
1264 struct srpt_send_ioctx, free_list);
1265 list_del(&ioctx->free_list);
1266 }
1267 spin_unlock_irqrestore(&ch->spinlock, flags);
1268
1269 if (!ioctx)
1270 return ioctx;
1271
1272 BUG_ON(ioctx->ch != ch);
1273 kref_init(&ioctx->kref);
1274 spin_lock_init(&ioctx->spinlock);
1275 ioctx->state = SRPT_STATE_NEW;
1276 ioctx->n_rbuf = 0;
1277 ioctx->rbufs = NULL;
1278 ioctx->n_rdma = 0;
1279 ioctx->n_rdma_ius = 0;
1280 ioctx->rdma_ius = NULL;
1281 ioctx->mapped_sg_count = 0;
1282 init_completion(&ioctx->tx_done);
1283 ioctx->queue_status_only = false;
1284 /*
1285 * transport_init_se_cmd() does not initialize all fields, so do it
1286 * here.
1287 */
1288 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1289 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1290
1291 return ioctx;
1292}
1293
1294/**
1295 * srpt_put_send_ioctx() - Free up resources.
1296 */
1297static void srpt_put_send_ioctx(struct srpt_send_ioctx *ioctx)
1298{
1299 struct srpt_rdma_ch *ch;
1300 unsigned long flags;
1301
1302 BUG_ON(!ioctx);
1303 ch = ioctx->ch;
1304 BUG_ON(!ch);
1305
1306 WARN_ON(srpt_get_cmd_state(ioctx) != SRPT_STATE_DONE);
1307
1308 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
1309 transport_generic_free_cmd(&ioctx->cmd, 0);
1310
1311 if (ioctx->n_rbuf > 1) {
1312 kfree(ioctx->rbufs);
1313 ioctx->rbufs = NULL;
1314 ioctx->n_rbuf = 0;
1315 }
1316
1317 spin_lock_irqsave(&ch->spinlock, flags);
1318 list_add(&ioctx->free_list, &ch->free_list);
1319 spin_unlock_irqrestore(&ch->spinlock, flags);
1320}
1321
1322static void srpt_put_send_ioctx_kref(struct kref *kref)
1323{
1324 srpt_put_send_ioctx(container_of(kref, struct srpt_send_ioctx, kref));
1325}
1326
1327/**
1328 * srpt_abort_cmd() - Abort a SCSI command.
1329 * @ioctx: I/O context associated with the SCSI command.
1330 * @context: Preferred execution context.
1331 */
1332static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1333{
1334 enum srpt_command_state state;
1335 unsigned long flags;
1336
1337 BUG_ON(!ioctx);
1338
1339 /*
1340 * If the command is in a state where the target core is waiting for
1341 * the ib_srpt driver, change the state to the next state. Changing
1342 * the state of the command from SRPT_STATE_NEED_DATA to
1343 * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
1344 * function a second time.
1345 */
1346
1347 spin_lock_irqsave(&ioctx->spinlock, flags);
1348 state = ioctx->state;
1349 switch (state) {
1350 case SRPT_STATE_NEED_DATA:
1351 ioctx->state = SRPT_STATE_DATA_IN;
1352 break;
1353 case SRPT_STATE_DATA_IN:
1354 case SRPT_STATE_CMD_RSP_SENT:
1355 case SRPT_STATE_MGMT_RSP_SENT:
1356 ioctx->state = SRPT_STATE_DONE;
1357 break;
1358 default:
1359 break;
1360 }
1361 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1362
1363 if (state == SRPT_STATE_DONE)
1364 goto out;
1365
1366 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
1367 ioctx->tag);
1368
1369 switch (state) {
1370 case SRPT_STATE_NEW:
1371 case SRPT_STATE_DATA_IN:
1372 case SRPT_STATE_MGMT:
1373 /*
1374 * Do nothing - defer abort processing until
1375 * srpt_queue_response() is invoked.
1376 */
1377 WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
1378 break;
1379 case SRPT_STATE_NEED_DATA:
1380 /* DMA_TO_DEVICE (write) - RDMA read error. */
1381 atomic_set(&ioctx->cmd.transport_lun_stop, 1);
1382 transport_generic_handle_data(&ioctx->cmd);
1383 break;
1384 case SRPT_STATE_CMD_RSP_SENT:
1385 /*
1386 * SRP_RSP sending failed or the SRP_RSP send completion has
1387 * not been received in time.
1388 */
1389 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
1390 atomic_set(&ioctx->cmd.transport_lun_stop, 1);
1391 kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
1392 break;
1393 case SRPT_STATE_MGMT_RSP_SENT:
1394 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1395 kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
1396 break;
1397 default:
1398 WARN_ON("ERROR: unexpected command state");
1399 break;
1400 }
1401
1402out:
1403 return state;
1404}
1405
1406/**
1407 * srpt_handle_send_err_comp() - Process an IB_WC_SEND error completion.
1408 */
1409static void srpt_handle_send_err_comp(struct srpt_rdma_ch *ch, u64 wr_id)
1410{
1411 struct srpt_send_ioctx *ioctx;
1412 enum srpt_command_state state;
1413 struct se_cmd *cmd;
1414 u32 index;
1415
1416 atomic_inc(&ch->sq_wr_avail);
1417
1418 index = idx_from_wr_id(wr_id);
1419 ioctx = ch->ioctx_ring[index];
1420 state = srpt_get_cmd_state(ioctx);
1421 cmd = &ioctx->cmd;
1422
1423 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1424 && state != SRPT_STATE_MGMT_RSP_SENT
1425 && state != SRPT_STATE_NEED_DATA
1426 && state != SRPT_STATE_DONE);
1427
1428 /* If SRP_RSP sending failed, undo the ch->req_lim change. */
1429 if (state == SRPT_STATE_CMD_RSP_SENT
1430 || state == SRPT_STATE_MGMT_RSP_SENT)
1431 atomic_dec(&ch->req_lim);
1432
1433 srpt_abort_cmd(ioctx);
1434}
1435
1436/**
1437 * srpt_handle_send_comp() - Process an IB send completion notification.
1438 */
1439static void srpt_handle_send_comp(struct srpt_rdma_ch *ch,
1440 struct srpt_send_ioctx *ioctx)
1441{
1442 enum srpt_command_state state;
1443
1444 atomic_inc(&ch->sq_wr_avail);
1445
1446 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1447
1448 if (WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1449 && state != SRPT_STATE_MGMT_RSP_SENT
1450 && state != SRPT_STATE_DONE))
1451 pr_debug("state = %d\n", state);
1452
1453 if (state != SRPT_STATE_DONE)
1454 kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
1455 else
1456 printk(KERN_ERR "IB completion has been received too late for"
1457 " wr_id = %u.\n", ioctx->ioctx.index);
1458}
1459
1460/**
1461 * srpt_handle_rdma_comp() - Process an IB RDMA completion notification.
1462 *
1463 * Note: transport_generic_handle_data() is asynchronous so unmapping the
1464 * data that has been transferred via IB RDMA must be postponed until the
1465 * check_stop_free() callback.
1466 */
1467static void srpt_handle_rdma_comp(struct srpt_rdma_ch *ch,
1468 struct srpt_send_ioctx *ioctx,
1469 enum srpt_opcode opcode)
1470{
1471 WARN_ON(ioctx->n_rdma <= 0);
1472 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1473
1474 if (opcode == SRPT_RDMA_READ_LAST) {
1475 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1476 SRPT_STATE_DATA_IN))
1477 transport_generic_handle_data(&ioctx->cmd);
1478 else
1479 printk(KERN_ERR "%s[%d]: wrong state = %d\n", __func__,
1480 __LINE__, srpt_get_cmd_state(ioctx));
1481 } else if (opcode == SRPT_RDMA_ABORT) {
1482 ioctx->rdma_aborted = true;
1483 } else {
1484 WARN(true, "unexpected opcode %d\n", opcode);
1485 }
1486}
1487
1488/**
1489 * srpt_handle_rdma_err_comp() - Process an IB RDMA error completion.
1490 */
1491static void srpt_handle_rdma_err_comp(struct srpt_rdma_ch *ch,
1492 struct srpt_send_ioctx *ioctx,
1493 enum srpt_opcode opcode)
1494{
1495 struct se_cmd *cmd;
1496 enum srpt_command_state state;
1497
1498 cmd = &ioctx->cmd;
1499 state = srpt_get_cmd_state(ioctx);
1500 switch (opcode) {
1501 case SRPT_RDMA_READ_LAST:
1502 if (ioctx->n_rdma <= 0) {
1503 printk(KERN_ERR "Received invalid RDMA read"
1504 " error completion with idx %d\n",
1505 ioctx->ioctx.index);
1506 break;
1507 }
1508 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1509 if (state == SRPT_STATE_NEED_DATA)
1510 srpt_abort_cmd(ioctx);
1511 else
1512 printk(KERN_ERR "%s[%d]: wrong state = %d\n",
1513 __func__, __LINE__, state);
1514 break;
1515 case SRPT_RDMA_WRITE_LAST:
1516 atomic_set(&ioctx->cmd.transport_lun_stop, 1);
1517 break;
1518 default:
1519 printk(KERN_ERR "%s[%d]: opcode = %u\n", __func__,
1520 __LINE__, opcode);
1521 break;
1522 }
1523}
1524
1525/**
1526 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1527 * @ch: RDMA channel through which the request has been received.
1528 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1529 * be built in the buffer ioctx->buf points at and hence this function will
1530 * overwrite the request data.
1531 * @tag: tag of the request for which this response is being generated.
1532 * @status: value for the STATUS field of the SRP_RSP information unit.
1533 *
1534 * Returns the size in bytes of the SRP_RSP response.
1535 *
1536 * An SRP_RSP response contains a SCSI status or service response. See also
1537 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1538 * response. See also SPC-2 for more information about sense data.
1539 */
1540static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1541 struct srpt_send_ioctx *ioctx, u64 tag,
1542 int status)
1543{
1544 struct srp_rsp *srp_rsp;
1545 const u8 *sense_data;
1546 int sense_data_len, max_sense_len;
1547
1548 /*
1549 * The lowest bit of all SAM-3 status codes is zero (see also
1550 * paragraph 5.3 in SAM-3).
1551 */
1552 WARN_ON(status & 1);
1553
1554 srp_rsp = ioctx->ioctx.buf;
1555 BUG_ON(!srp_rsp);
1556
1557 sense_data = ioctx->sense_data;
1558 sense_data_len = ioctx->cmd.scsi_sense_length;
1559 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1560
1561 memset(srp_rsp, 0, sizeof *srp_rsp);
1562 srp_rsp->opcode = SRP_RSP;
1563 srp_rsp->req_lim_delta =
1564 __constant_cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1565 srp_rsp->tag = tag;
1566 srp_rsp->status = status;
1567
1568 if (sense_data_len) {
1569 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1570 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1571 if (sense_data_len > max_sense_len) {
1572 printk(KERN_WARNING "truncated sense data from %d to %d"
1573 " bytes\n", sense_data_len, max_sense_len);
1574 sense_data_len = max_sense_len;
1575 }
1576
1577 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1578 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1579 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1580 }
1581
1582 return sizeof(*srp_rsp) + sense_data_len;
1583}
1584
1585/**
1586 * srpt_build_tskmgmt_rsp() - Build a task management response.
1587 * @ch: RDMA channel through which the request has been received.
1588 * @ioctx: I/O context in which the SRP_RSP response will be built.
1589 * @rsp_code: RSP_CODE that will be stored in the response.
1590 * @tag: Tag of the request for which this response is being generated.
1591 *
1592 * Returns the size in bytes of the SRP_RSP response.
1593 *
1594 * An SRP_RSP response contains a SCSI status or service response. See also
1595 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1596 * response.
1597 */
1598static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1599 struct srpt_send_ioctx *ioctx,
1600 u8 rsp_code, u64 tag)
1601{
1602 struct srp_rsp *srp_rsp;
1603 int resp_data_len;
1604 int resp_len;
1605
1606 resp_data_len = (rsp_code == SRP_TSK_MGMT_SUCCESS) ? 0 : 4;
1607 resp_len = sizeof(*srp_rsp) + resp_data_len;
1608
1609 srp_rsp = ioctx->ioctx.buf;
1610 BUG_ON(!srp_rsp);
1611 memset(srp_rsp, 0, sizeof *srp_rsp);
1612
1613 srp_rsp->opcode = SRP_RSP;
1614 srp_rsp->req_lim_delta = __constant_cpu_to_be32(1
1615 + atomic_xchg(&ch->req_lim_delta, 0));
1616 srp_rsp->tag = tag;
1617
1618 if (rsp_code != SRP_TSK_MGMT_SUCCESS) {
1619 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1620 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1621 srp_rsp->data[3] = rsp_code;
1622 }
1623
1624 return resp_len;
1625}
1626
1627#define NO_SUCH_LUN ((uint64_t)-1LL)
1628
1629/*
1630 * SCSI LUN addressing method. See also SAM-2 and the section about
1631 * eight byte LUNs.
1632 */
1633enum scsi_lun_addr_method {
1634 SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
1635 SCSI_LUN_ADDR_METHOD_FLAT = 1,
1636 SCSI_LUN_ADDR_METHOD_LUN = 2,
1637 SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
1638};
1639
1640/*
1641 * srpt_unpack_lun() - Convert from network LUN to linear LUN.
1642 *
1643 * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
1644 * order (big endian) to a linear LUN. Supports three LUN addressing methods:
1645 * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
1646 */
1647static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
1648{
1649 uint64_t res = NO_SUCH_LUN;
1650 int addressing_method;
1651
1652 if (unlikely(len < 2)) {
1653 printk(KERN_ERR "Illegal LUN length %d, expected 2 bytes or "
1654 "more", len);
1655 goto out;
1656 }
1657
1658 switch (len) {
1659 case 8:
1660 if ((*((__be64 *)lun) &
1661 __constant_cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
1662 goto out_err;
1663 break;
1664 case 4:
1665 if (*((__be16 *)&lun[2]) != 0)
1666 goto out_err;
1667 break;
1668 case 6:
1669 if (*((__be32 *)&lun[2]) != 0)
1670 goto out_err;
1671 break;
1672 case 2:
1673 break;
1674 default:
1675 goto out_err;
1676 }
1677
1678 addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
1679 switch (addressing_method) {
1680 case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
1681 case SCSI_LUN_ADDR_METHOD_FLAT:
1682 case SCSI_LUN_ADDR_METHOD_LUN:
1683 res = *(lun + 1) | (((*lun) & 0x3f) << 8);
1684 break;
1685
1686 case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
1687 default:
1688 printk(KERN_ERR "Unimplemented LUN addressing method %u",
1689 addressing_method);
1690 break;
1691 }
1692
1693out:
1694 return res;
1695
1696out_err:
1697 printk(KERN_ERR "Support for multi-level LUNs has not yet been"
1698 " implemented");
1699 goto out;
1700}
1701
1702static int srpt_check_stop_free(struct se_cmd *cmd)
1703{
1704 struct srpt_send_ioctx *ioctx;
1705
1706 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
1707 return kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
1708}
1709
1710/**
1711 * srpt_handle_cmd() - Process SRP_CMD.
1712 */
1713static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
1714 struct srpt_recv_ioctx *recv_ioctx,
1715 struct srpt_send_ioctx *send_ioctx)
1716{
1717 struct se_cmd *cmd;
1718 struct srp_cmd *srp_cmd;
1719 uint64_t unpacked_lun;
1720 u64 data_len;
1721 enum dma_data_direction dir;
1722 int ret;
1723
1724 BUG_ON(!send_ioctx);
1725
1726 srp_cmd = recv_ioctx->ioctx.buf;
1727 kref_get(&send_ioctx->kref);
1728 cmd = &send_ioctx->cmd;
1729 send_ioctx->tag = srp_cmd->tag;
1730
1731 switch (srp_cmd->task_attr) {
1732 case SRP_CMD_SIMPLE_Q:
1733 cmd->sam_task_attr = MSG_SIMPLE_TAG;
1734 break;
1735 case SRP_CMD_ORDERED_Q:
1736 default:
1737 cmd->sam_task_attr = MSG_ORDERED_TAG;
1738 break;
1739 case SRP_CMD_HEAD_OF_Q:
1740 cmd->sam_task_attr = MSG_HEAD_TAG;
1741 break;
1742 case SRP_CMD_ACA:
1743 cmd->sam_task_attr = MSG_ACA_TAG;
1744 break;
1745 }
1746
1747 ret = srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len);
1748 if (ret) {
1749 printk(KERN_ERR "0x%llx: parsing SRP descriptor table failed.\n",
1750 srp_cmd->tag);
1751 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1752 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1753 goto send_sense;
1754 }
1755
1756 cmd->data_length = data_len;
1757 cmd->data_direction = dir;
1758 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
1759 sizeof(srp_cmd->lun));
1760 if (transport_lookup_cmd_lun(cmd, unpacked_lun) < 0)
1761 goto send_sense;
1762 ret = transport_generic_allocate_tasks(cmd, srp_cmd->cdb);
1763 if (cmd->se_cmd_flags & SCF_SCSI_RESERVATION_CONFLICT)
1764 srpt_queue_status(cmd);
1765 else if (cmd->se_cmd_flags & SCF_SCSI_CDB_EXCEPTION)
1766 goto send_sense;
1767 else
1768 WARN_ON_ONCE(ret);
1769
1770 transport_handle_cdb_direct(cmd);
1771 return 0;
1772
1773send_sense:
1774 transport_send_check_condition_and_sense(cmd, cmd->scsi_sense_reason,
1775 0);
1776 return -1;
1777}
1778
1779/**
1780 * srpt_rx_mgmt_fn_tag() - Process a task management function by tag.
1781 * @ch: RDMA channel of the task management request.
1782 * @fn: Task management function to perform.
1783 * @req_tag: Tag of the SRP task management request.
1784 * @mgmt_ioctx: I/O context of the task management request.
1785 *
1786 * Returns zero if the target core will process the task management
1787 * request asynchronously.
1788 *
1789 * Note: It is assumed that the initiator serializes tag-based task management
1790 * requests.
1791 */
1792static int srpt_rx_mgmt_fn_tag(struct srpt_send_ioctx *ioctx, u64 tag)
1793{
1794 struct srpt_device *sdev;
1795 struct srpt_rdma_ch *ch;
1796 struct srpt_send_ioctx *target;
1797 int ret, i;
1798
1799 ret = -EINVAL;
1800 ch = ioctx->ch;
1801 BUG_ON(!ch);
1802 BUG_ON(!ch->sport);
1803 sdev = ch->sport->sdev;
1804 BUG_ON(!sdev);
1805 spin_lock_irq(&sdev->spinlock);
1806 for (i = 0; i < ch->rq_size; ++i) {
1807 target = ch->ioctx_ring[i];
1808 if (target->cmd.se_lun == ioctx->cmd.se_lun &&
1809 target->tag == tag &&
1810 srpt_get_cmd_state(target) != SRPT_STATE_DONE) {
1811 ret = 0;
1812 /* now let the target core abort &target->cmd; */
1813 break;
1814 }
1815 }
1816 spin_unlock_irq(&sdev->spinlock);
1817 return ret;
1818}
1819
1820static int srp_tmr_to_tcm(int fn)
1821{
1822 switch (fn) {
1823 case SRP_TSK_ABORT_TASK:
1824 return TMR_ABORT_TASK;
1825 case SRP_TSK_ABORT_TASK_SET:
1826 return TMR_ABORT_TASK_SET;
1827 case SRP_TSK_CLEAR_TASK_SET:
1828 return TMR_CLEAR_TASK_SET;
1829 case SRP_TSK_LUN_RESET:
1830 return TMR_LUN_RESET;
1831 case SRP_TSK_CLEAR_ACA:
1832 return TMR_CLEAR_ACA;
1833 default:
1834 return -1;
1835 }
1836}
1837
1838/**
1839 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1840 *
1841 * Returns 0 if and only if the request will be processed by the target core.
1842 *
1843 * For more information about SRP_TSK_MGMT information units, see also section
1844 * 6.7 in the SRP r16a document.
1845 */
1846static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1847 struct srpt_recv_ioctx *recv_ioctx,
1848 struct srpt_send_ioctx *send_ioctx)
1849{
1850 struct srp_tsk_mgmt *srp_tsk;
1851 struct se_cmd *cmd;
1852 uint64_t unpacked_lun;
1853 int tcm_tmr;
1854 int res;
1855
1856 BUG_ON(!send_ioctx);
1857
1858 srp_tsk = recv_ioctx->ioctx.buf;
1859 cmd = &send_ioctx->cmd;
1860
1861 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1862 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1863 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1864
1865 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
1866 send_ioctx->tag = srp_tsk->tag;
1867 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1868 if (tcm_tmr < 0) {
1869 send_ioctx->cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1870 send_ioctx->cmd.se_tmr_req->response =
1871 TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
1872 goto process_tmr;
1873 }
1874 cmd->se_tmr_req = core_tmr_alloc_req(cmd, NULL, tcm_tmr, GFP_KERNEL);
1875 if (!cmd->se_tmr_req) {
1876 send_ioctx->cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1877 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
1878 goto process_tmr;
1879 }
1880
1881 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
1882 sizeof(srp_tsk->lun));
1883 res = transport_lookup_tmr_lun(&send_ioctx->cmd, unpacked_lun);
1884 if (res) {
1885 pr_debug("rejecting TMR for LUN %lld\n", unpacked_lun);
1886 send_ioctx->cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1887 send_ioctx->cmd.se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
1888 goto process_tmr;
1889 }
1890
1891 if (srp_tsk->tsk_mgmt_func == SRP_TSK_ABORT_TASK)
1892 srpt_rx_mgmt_fn_tag(send_ioctx, srp_tsk->task_tag);
1893
1894process_tmr:
1895 kref_get(&send_ioctx->kref);
1896 if (!(send_ioctx->cmd.se_cmd_flags & SCF_SCSI_CDB_EXCEPTION))
1897 transport_generic_handle_tmr(&send_ioctx->cmd);
1898 else
1899 transport_send_check_condition_and_sense(cmd,
1900 cmd->scsi_sense_reason, 0);
1901
1902}
1903
1904/**
1905 * srpt_handle_new_iu() - Process a newly received information unit.
1906 * @ch: RDMA channel through which the information unit has been received.
1907 * @ioctx: SRPT I/O context associated with the information unit.
1908 */
1909static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1910 struct srpt_recv_ioctx *recv_ioctx,
1911 struct srpt_send_ioctx *send_ioctx)
1912{
1913 struct srp_cmd *srp_cmd;
1914 enum rdma_ch_state ch_state;
1915
1916 BUG_ON(!ch);
1917 BUG_ON(!recv_ioctx);
1918
1919 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1920 recv_ioctx->ioctx.dma, srp_max_req_size,
1921 DMA_FROM_DEVICE);
1922
1923 ch_state = srpt_get_ch_state(ch);
1924 if (unlikely(ch_state == CH_CONNECTING)) {
1925 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1926 goto out;
1927 }
1928
1929 if (unlikely(ch_state != CH_LIVE))
1930 goto out;
1931
1932 srp_cmd = recv_ioctx->ioctx.buf;
1933 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1934 if (!send_ioctx)
1935 send_ioctx = srpt_get_send_ioctx(ch);
1936 if (unlikely(!send_ioctx)) {
1937 list_add_tail(&recv_ioctx->wait_list,
1938 &ch->cmd_wait_list);
1939 goto out;
1940 }
1941 }
1942
1943 transport_init_se_cmd(&send_ioctx->cmd, &srpt_target->tf_ops, ch->sess,
1944 0, DMA_NONE, MSG_SIMPLE_TAG,
1945 send_ioctx->sense_data);
1946
1947 switch (srp_cmd->opcode) {
1948 case SRP_CMD:
1949 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1950 break;
1951 case SRP_TSK_MGMT:
1952 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1953 break;
1954 case SRP_I_LOGOUT:
1955 printk(KERN_ERR "Not yet implemented: SRP_I_LOGOUT\n");
1956 break;
1957 case SRP_CRED_RSP:
1958 pr_debug("received SRP_CRED_RSP\n");
1959 break;
1960 case SRP_AER_RSP:
1961 pr_debug("received SRP_AER_RSP\n");
1962 break;
1963 case SRP_RSP:
1964 printk(KERN_ERR "Received SRP_RSP\n");
1965 break;
1966 default:
1967 printk(KERN_ERR "received IU with unknown opcode 0x%x\n",
1968 srp_cmd->opcode);
1969 break;
1970 }
1971
1972 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1973out:
1974 return;
1975}
1976
1977static void srpt_process_rcv_completion(struct ib_cq *cq,
1978 struct srpt_rdma_ch *ch,
1979 struct ib_wc *wc)
1980{
1981 struct srpt_device *sdev = ch->sport->sdev;
1982 struct srpt_recv_ioctx *ioctx;
1983 u32 index;
1984
1985 index = idx_from_wr_id(wc->wr_id);
1986 if (wc->status == IB_WC_SUCCESS) {
1987 int req_lim;
1988
1989 req_lim = atomic_dec_return(&ch->req_lim);
1990 if (unlikely(req_lim < 0))
1991 printk(KERN_ERR "req_lim = %d < 0\n", req_lim);
1992 ioctx = sdev->ioctx_ring[index];
1993 srpt_handle_new_iu(ch, ioctx, NULL);
1994 } else {
1995 printk(KERN_INFO "receiving failed for idx %u with status %d\n",
1996 index, wc->status);
1997 }
1998}
1999
2000/**
2001 * srpt_process_send_completion() - Process an IB send completion.
2002 *
2003 * Note: Although this has not yet been observed during tests, at least in
2004 * theory it is possible that the srpt_get_send_ioctx() call invoked by
2005 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
2006 * value in each response is set to one, and it is possible that this response
2007 * makes the initiator send a new request before the send completion for that
2008 * response has been processed. This could e.g. happen if the call to
2009 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
2010 * if IB retransmission causes generation of the send completion to be
2011 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
2012 * are queued on cmd_wait_list. The code below processes these delayed
2013 * requests one at a time.
2014 */
2015static void srpt_process_send_completion(struct ib_cq *cq,
2016 struct srpt_rdma_ch *ch,
2017 struct ib_wc *wc)
2018{
2019 struct srpt_send_ioctx *send_ioctx;
2020 uint32_t index;
2021 enum srpt_opcode opcode;
2022
2023 index = idx_from_wr_id(wc->wr_id);
2024 opcode = opcode_from_wr_id(wc->wr_id);
2025 send_ioctx = ch->ioctx_ring[index];
2026 if (wc->status == IB_WC_SUCCESS) {
2027 if (opcode == SRPT_SEND)
2028 srpt_handle_send_comp(ch, send_ioctx);
2029 else {
2030 WARN_ON(opcode != SRPT_RDMA_ABORT &&
2031 wc->opcode != IB_WC_RDMA_READ);
2032 srpt_handle_rdma_comp(ch, send_ioctx, opcode);
2033 }
2034 } else {
2035 if (opcode == SRPT_SEND) {
2036 printk(KERN_INFO "sending response for idx %u failed"
2037 " with status %d\n", index, wc->status);
2038 srpt_handle_send_err_comp(ch, wc->wr_id);
2039 } else if (opcode != SRPT_RDMA_MID) {
2040 printk(KERN_INFO "RDMA t %d for idx %u failed with"
2041 " status %d", opcode, index, wc->status);
2042 srpt_handle_rdma_err_comp(ch, send_ioctx, opcode);
2043 }
2044 }
2045
2046 while (unlikely(opcode == SRPT_SEND
2047 && !list_empty(&ch->cmd_wait_list)
2048 && srpt_get_ch_state(ch) == CH_LIVE
2049 && (send_ioctx = srpt_get_send_ioctx(ch)) != NULL)) {
2050 struct srpt_recv_ioctx *recv_ioctx;
2051
2052 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
2053 struct srpt_recv_ioctx,
2054 wait_list);
2055 list_del(&recv_ioctx->wait_list);
2056 srpt_handle_new_iu(ch, recv_ioctx, send_ioctx);
2057 }
2058}
2059
2060static void srpt_process_completion(struct ib_cq *cq, struct srpt_rdma_ch *ch)
2061{
2062 struct ib_wc *const wc = ch->wc;
2063 int i, n;
2064
2065 WARN_ON(cq != ch->cq);
2066
2067 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
2068 while ((n = ib_poll_cq(cq, ARRAY_SIZE(ch->wc), wc)) > 0) {
2069 for (i = 0; i < n; i++) {
2070 if (opcode_from_wr_id(wc[i].wr_id) == SRPT_RECV)
2071 srpt_process_rcv_completion(cq, ch, &wc[i]);
2072 else
2073 srpt_process_send_completion(cq, ch, &wc[i]);
2074 }
2075 }
2076}
2077
2078/**
2079 * srpt_completion() - IB completion queue callback function.
2080 *
2081 * Notes:
2082 * - It is guaranteed that a completion handler will never be invoked
2083 * concurrently on two different CPUs for the same completion queue. See also
2084 * Documentation/infiniband/core_locking.txt and the implementation of
2085 * handle_edge_irq() in kernel/irq/chip.c.
2086 * - When threaded IRQs are enabled, completion handlers are invoked in thread
2087 * context instead of interrupt context.
2088 */
2089static void srpt_completion(struct ib_cq *cq, void *ctx)
2090{
2091 struct srpt_rdma_ch *ch = ctx;
2092
2093 wake_up_interruptible(&ch->wait_queue);
2094}
2095
2096static int srpt_compl_thread(void *arg)
2097{
2098 struct srpt_rdma_ch *ch;
2099
2100 /* Hibernation / freezing of the SRPT kernel thread is not supported. */
2101 current->flags |= PF_NOFREEZE;
2102
2103 ch = arg;
2104 BUG_ON(!ch);
2105 printk(KERN_INFO "Session %s: kernel thread %s (PID %d) started\n",
2106 ch->sess_name, ch->thread->comm, current->pid);
2107 while (!kthread_should_stop()) {
2108 wait_event_interruptible(ch->wait_queue,
2109 (srpt_process_completion(ch->cq, ch),
2110 kthread_should_stop()));
2111 }
2112 printk(KERN_INFO "Session %s: kernel thread %s (PID %d) stopped\n",
2113 ch->sess_name, ch->thread->comm, current->pid);
2114 return 0;
2115}
2116
2117/**
2118 * srpt_create_ch_ib() - Create receive and send completion queues.
2119 */
2120static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
2121{
2122 struct ib_qp_init_attr *qp_init;
2123 struct srpt_port *sport = ch->sport;
2124 struct srpt_device *sdev = sport->sdev;
2125 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
2126 int ret;
2127
2128 WARN_ON(ch->rq_size < 1);
2129
2130 ret = -ENOMEM;
2131 qp_init = kzalloc(sizeof *qp_init, GFP_KERNEL);
2132 if (!qp_init)
2133 goto out;
2134
2135 ch->cq = ib_create_cq(sdev->device, srpt_completion, NULL, ch,
2136 ch->rq_size + srp_sq_size, 0);
2137 if (IS_ERR(ch->cq)) {
2138 ret = PTR_ERR(ch->cq);
2139 printk(KERN_ERR "failed to create CQ cqe= %d ret= %d\n",
2140 ch->rq_size + srp_sq_size, ret);
2141 goto out;
2142 }
2143
2144 qp_init->qp_context = (void *)ch;
2145 qp_init->event_handler
2146 = (void(*)(struct ib_event *, void*))srpt_qp_event;
2147 qp_init->send_cq = ch->cq;
2148 qp_init->recv_cq = ch->cq;
2149 qp_init->srq = sdev->srq;
2150 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
2151 qp_init->qp_type = IB_QPT_RC;
2152 qp_init->cap.max_send_wr = srp_sq_size;
2153 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
2154
2155 ch->qp = ib_create_qp(sdev->pd, qp_init);
2156 if (IS_ERR(ch->qp)) {
2157 ret = PTR_ERR(ch->qp);
2158 printk(KERN_ERR "failed to create_qp ret= %d\n", ret);
2159 goto err_destroy_cq;
2160 }
2161
2162 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
2163
2164 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
2165 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
2166 qp_init->cap.max_send_wr, ch->cm_id);
2167
2168 ret = srpt_init_ch_qp(ch, ch->qp);
2169 if (ret)
2170 goto err_destroy_qp;
2171
2172 init_waitqueue_head(&ch->wait_queue);
2173
2174 pr_debug("creating thread for session %s\n", ch->sess_name);
2175
2176 ch->thread = kthread_run(srpt_compl_thread, ch, "ib_srpt_compl");
2177 if (IS_ERR(ch->thread)) {
2178 printk(KERN_ERR "failed to create kernel thread %ld\n",
2179 PTR_ERR(ch->thread));
2180 ch->thread = NULL;
2181 goto err_destroy_qp;
2182 }
2183
2184out:
2185 kfree(qp_init);
2186 return ret;
2187
2188err_destroy_qp:
2189 ib_destroy_qp(ch->qp);
2190err_destroy_cq:
2191 ib_destroy_cq(ch->cq);
2192 goto out;
2193}
2194
2195static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
2196{
2197 if (ch->thread)
2198 kthread_stop(ch->thread);
2199
2200 ib_destroy_qp(ch->qp);
2201 ib_destroy_cq(ch->cq);
2202}
2203
2204/**
2205 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
2206 *
2207 * Reset the QP and make sure all resources associated with the channel will
2208 * be deallocated at an appropriate time.
2209 *
2210 * Note: The caller must hold ch->sport->sdev->spinlock.
2211 */
2212static void __srpt_close_ch(struct srpt_rdma_ch *ch)
2213{
2214 struct srpt_device *sdev;
2215 enum rdma_ch_state prev_state;
2216 unsigned long flags;
2217
2218 sdev = ch->sport->sdev;
2219
2220 spin_lock_irqsave(&ch->spinlock, flags);
2221 prev_state = ch->state;
2222 switch (prev_state) {
2223 case CH_CONNECTING:
2224 case CH_LIVE:
2225 ch->state = CH_DISCONNECTING;
2226 break;
2227 default:
2228 break;
2229 }
2230 spin_unlock_irqrestore(&ch->spinlock, flags);
2231
2232 switch (prev_state) {
2233 case CH_CONNECTING:
2234 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
2235 NULL, 0);
2236 /* fall through */
2237 case CH_LIVE:
2238 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
2239 printk(KERN_ERR "sending CM DREQ failed.\n");
2240 break;
2241 case CH_DISCONNECTING:
2242 break;
2243 case CH_DRAINING:
2244 case CH_RELEASING:
2245 break;
2246 }
2247}
2248
2249/**
2250 * srpt_close_ch() - Close an RDMA channel.
2251 */
2252static void srpt_close_ch(struct srpt_rdma_ch *ch)
2253{
2254 struct srpt_device *sdev;
2255
2256 sdev = ch->sport->sdev;
2257 spin_lock_irq(&sdev->spinlock);
2258 __srpt_close_ch(ch);
2259 spin_unlock_irq(&sdev->spinlock);
2260}
2261
2262/**
2263 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
2264 * @cm_id: Pointer to the CM ID of the channel to be drained.
2265 *
2266 * Note: Must be called from inside srpt_cm_handler to avoid a race between
2267 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
2268 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
2269 * waits until all target sessions for the associated IB device have been
2270 * unregistered and target session registration involves a call to
2271 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
2272 * this function has finished).
2273 */
2274static void srpt_drain_channel(struct ib_cm_id *cm_id)
2275{
2276 struct srpt_device *sdev;
2277 struct srpt_rdma_ch *ch;
2278 int ret;
2279 bool do_reset = false;
2280
2281 WARN_ON_ONCE(irqs_disabled());
2282
2283 sdev = cm_id->context;
2284 BUG_ON(!sdev);
2285 spin_lock_irq(&sdev->spinlock);
2286 list_for_each_entry(ch, &sdev->rch_list, list) {
2287 if (ch->cm_id == cm_id) {
2288 do_reset = srpt_test_and_set_ch_state(ch,
2289 CH_CONNECTING, CH_DRAINING) ||
2290 srpt_test_and_set_ch_state(ch,
2291 CH_LIVE, CH_DRAINING) ||
2292 srpt_test_and_set_ch_state(ch,
2293 CH_DISCONNECTING, CH_DRAINING);
2294 break;
2295 }
2296 }
2297 spin_unlock_irq(&sdev->spinlock);
2298
2299 if (do_reset) {
2300 ret = srpt_ch_qp_err(ch);
2301 if (ret < 0)
2302 printk(KERN_ERR "Setting queue pair in error state"
2303 " failed: %d\n", ret);
2304 }
2305}
2306
2307/**
2308 * srpt_find_channel() - Look up an RDMA channel.
2309 * @cm_id: Pointer to the CM ID of the channel to be looked up.
2310 *
2311 * Return NULL if no matching RDMA channel has been found.
2312 */
2313static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
2314 struct ib_cm_id *cm_id)
2315{
2316 struct srpt_rdma_ch *ch;
2317 bool found;
2318
2319 WARN_ON_ONCE(irqs_disabled());
2320 BUG_ON(!sdev);
2321
2322 found = false;
2323 spin_lock_irq(&sdev->spinlock);
2324 list_for_each_entry(ch, &sdev->rch_list, list) {
2325 if (ch->cm_id == cm_id) {
2326 found = true;
2327 break;
2328 }
2329 }
2330 spin_unlock_irq(&sdev->spinlock);
2331
2332 return found ? ch : NULL;
2333}
2334
2335/**
2336 * srpt_release_channel() - Release channel resources.
2337 *
2338 * Schedules the actual release because:
2339 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
2340 * trigger a deadlock.
2341 * - It is not safe to call TCM transport_* functions from interrupt context.
2342 */
2343static void srpt_release_channel(struct srpt_rdma_ch *ch)
2344{
2345 schedule_work(&ch->release_work);
2346}
2347
2348static void srpt_release_channel_work(struct work_struct *w)
2349{
2350 struct srpt_rdma_ch *ch;
2351 struct srpt_device *sdev;
2352
2353 ch = container_of(w, struct srpt_rdma_ch, release_work);
2354 pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2355 ch->release_done);
2356
2357 sdev = ch->sport->sdev;
2358 BUG_ON(!sdev);
2359
2360 transport_deregister_session_configfs(ch->sess);
2361 transport_deregister_session(ch->sess);
2362 ch->sess = NULL;
2363
2364 srpt_destroy_ch_ib(ch);
2365
2366 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2367 ch->sport->sdev, ch->rq_size,
2368 ch->rsp_size, DMA_TO_DEVICE);
2369
2370 spin_lock_irq(&sdev->spinlock);
2371 list_del(&ch->list);
2372 spin_unlock_irq(&sdev->spinlock);
2373
2374 ib_destroy_cm_id(ch->cm_id);
2375
2376 if (ch->release_done)
2377 complete(ch->release_done);
2378
2379 wake_up(&sdev->ch_releaseQ);
2380
2381 kfree(ch);
2382}
2383
2384static struct srpt_node_acl *__srpt_lookup_acl(struct srpt_port *sport,
2385 u8 i_port_id[16])
2386{
2387 struct srpt_node_acl *nacl;
2388
2389 list_for_each_entry(nacl, &sport->port_acl_list, list)
2390 if (memcmp(nacl->i_port_id, i_port_id,
2391 sizeof(nacl->i_port_id)) == 0)
2392 return nacl;
2393
2394 return NULL;
2395}
2396
2397static struct srpt_node_acl *srpt_lookup_acl(struct srpt_port *sport,
2398 u8 i_port_id[16])
2399{
2400 struct srpt_node_acl *nacl;
2401
2402 spin_lock_irq(&sport->port_acl_lock);
2403 nacl = __srpt_lookup_acl(sport, i_port_id);
2404 spin_unlock_irq(&sport->port_acl_lock);
2405
2406 return nacl;
2407}
2408
2409/**
2410 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2411 *
2412 * Ownership of the cm_id is transferred to the target session if this
2413 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2414 */
2415static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2416 struct ib_cm_req_event_param *param,
2417 void *private_data)
2418{
2419 struct srpt_device *sdev = cm_id->context;
2420 struct srpt_port *sport = &sdev->port[param->port - 1];
2421 struct srp_login_req *req;
2422 struct srp_login_rsp *rsp;
2423 struct srp_login_rej *rej;
2424 struct ib_cm_rep_param *rep_param;
2425 struct srpt_rdma_ch *ch, *tmp_ch;
2426 struct srpt_node_acl *nacl;
2427 u32 it_iu_len;
2428 int i;
2429 int ret = 0;
2430
2431 WARN_ON_ONCE(irqs_disabled());
2432
2433 if (WARN_ON(!sdev || !private_data))
2434 return -EINVAL;
2435
2436 req = (struct srp_login_req *)private_data;
2437
2438 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2439
2440 printk(KERN_INFO "Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2441 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2442 " (guid=0x%llx:0x%llx)\n",
2443 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2444 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2445 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2446 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2447 it_iu_len,
2448 param->port,
2449 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2450 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
2451
2452 rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
2453 rej = kzalloc(sizeof *rej, GFP_KERNEL);
2454 rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
2455
2456 if (!rsp || !rej || !rep_param) {
2457 ret = -ENOMEM;
2458 goto out;
2459 }
2460
2461 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
2462 rej->reason = __constant_cpu_to_be32(
2463 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
2464 ret = -EINVAL;
2465 printk(KERN_ERR "rejected SRP_LOGIN_REQ because its"
2466 " length (%d bytes) is out of range (%d .. %d)\n",
2467 it_iu_len, 64, srp_max_req_size);
2468 goto reject;
2469 }
2470
2471 if (!sport->enabled) {
2472 rej->reason = __constant_cpu_to_be32(
2473 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2474 ret = -EINVAL;
2475 printk(KERN_ERR "rejected SRP_LOGIN_REQ because the target port"
2476 " has not yet been enabled\n");
2477 goto reject;
2478 }
2479
2480 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2481 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2482
2483 spin_lock_irq(&sdev->spinlock);
2484
2485 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2486 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2487 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2488 && param->port == ch->sport->port
2489 && param->listen_id == ch->sport->sdev->cm_id
2490 && ch->cm_id) {
2491 enum rdma_ch_state ch_state;
2492
2493 ch_state = srpt_get_ch_state(ch);
2494 if (ch_state != CH_CONNECTING
2495 && ch_state != CH_LIVE)
2496 continue;
2497
2498 /* found an existing channel */
2499 pr_debug("Found existing channel %s"
2500 " cm_id= %p state= %d\n",
2501 ch->sess_name, ch->cm_id, ch_state);
2502
2503 __srpt_close_ch(ch);
2504
2505 rsp->rsp_flags =
2506 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2507 }
2508 }
2509
2510 spin_unlock_irq(&sdev->spinlock);
2511
2512 } else
2513 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2514
2515 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2516 || *(__be64 *)(req->target_port_id + 8) !=
2517 cpu_to_be64(srpt_service_guid)) {
2518 rej->reason = __constant_cpu_to_be32(
2519 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
2520 ret = -ENOMEM;
2521 printk(KERN_ERR "rejected SRP_LOGIN_REQ because it"
2522 " has an invalid target port identifier.\n");
2523 goto reject;
2524 }
2525
2526 ch = kzalloc(sizeof *ch, GFP_KERNEL);
2527 if (!ch) {
2528 rej->reason = __constant_cpu_to_be32(
2529 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2530 printk(KERN_ERR "rejected SRP_LOGIN_REQ because no memory.\n");
2531 ret = -ENOMEM;
2532 goto reject;
2533 }
2534
2535 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2536 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2537 memcpy(ch->t_port_id, req->target_port_id, 16);
2538 ch->sport = &sdev->port[param->port - 1];
2539 ch->cm_id = cm_id;
2540 /*
2541 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2542 * for the SRP protocol to the command queue size.
2543 */
2544 ch->rq_size = SRPT_RQ_SIZE;
2545 spin_lock_init(&ch->spinlock);
2546 ch->state = CH_CONNECTING;
2547 INIT_LIST_HEAD(&ch->cmd_wait_list);
2548 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2549
2550 ch->ioctx_ring = (struct srpt_send_ioctx **)
2551 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2552 sizeof(*ch->ioctx_ring[0]),
2553 ch->rsp_size, DMA_TO_DEVICE);
2554 if (!ch->ioctx_ring)
2555 goto free_ch;
2556
2557 INIT_LIST_HEAD(&ch->free_list);
2558 for (i = 0; i < ch->rq_size; i++) {
2559 ch->ioctx_ring[i]->ch = ch;
2560 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2561 }
2562
2563 ret = srpt_create_ch_ib(ch);
2564 if (ret) {
2565 rej->reason = __constant_cpu_to_be32(
2566 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2567 printk(KERN_ERR "rejected SRP_LOGIN_REQ because creating"
2568 " a new RDMA channel failed.\n");
2569 goto free_ring;
2570 }
2571
2572 ret = srpt_ch_qp_rtr(ch, ch->qp);
2573 if (ret) {
2574 rej->reason = __constant_cpu_to_be32(
2575 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2576 printk(KERN_ERR "rejected SRP_LOGIN_REQ because enabling"
2577 " RTR failed (error code = %d)\n", ret);
2578 goto destroy_ib;
2579 }
2580 /*
2581 * Use the initator port identifier as the session name.
2582 */
2583 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2584 be64_to_cpu(*(__be64 *)ch->i_port_id),
2585 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2586
2587 pr_debug("registering session %s\n", ch->sess_name);
2588
2589 nacl = srpt_lookup_acl(sport, ch->i_port_id);
2590 if (!nacl) {
2591 printk(KERN_INFO "Rejected login because no ACL has been"
2592 " configured yet for initiator %s.\n", ch->sess_name);
2593 rej->reason = __constant_cpu_to_be32(
2594 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2595 goto destroy_ib;
2596 }
2597
2598 ch->sess = transport_init_session();
Dan Carpenter3af33632011-11-04 21:27:32 +03002599 if (IS_ERR(ch->sess)) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00002600 rej->reason = __constant_cpu_to_be32(
2601 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2602 pr_debug("Failed to create session\n");
2603 goto deregister_session;
2604 }
2605 ch->sess->se_node_acl = &nacl->nacl;
2606 transport_register_session(&sport->port_tpg_1, &nacl->nacl, ch->sess, ch);
2607
2608 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2609 ch->sess_name, ch->cm_id);
2610
2611 /* create srp_login_response */
2612 rsp->opcode = SRP_LOGIN_RSP;
2613 rsp->tag = req->tag;
2614 rsp->max_it_iu_len = req->req_it_iu_len;
2615 rsp->max_ti_iu_len = req->req_it_iu_len;
2616 ch->max_ti_iu_len = it_iu_len;
2617 rsp->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2618 | SRP_BUF_FORMAT_INDIRECT);
2619 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2620 atomic_set(&ch->req_lim, ch->rq_size);
2621 atomic_set(&ch->req_lim_delta, 0);
2622
2623 /* create cm reply */
2624 rep_param->qp_num = ch->qp->qp_num;
2625 rep_param->private_data = (void *)rsp;
2626 rep_param->private_data_len = sizeof *rsp;
2627 rep_param->rnr_retry_count = 7;
2628 rep_param->flow_control = 1;
2629 rep_param->failover_accepted = 0;
2630 rep_param->srq = 1;
2631 rep_param->responder_resources = 4;
2632 rep_param->initiator_depth = 4;
2633
2634 ret = ib_send_cm_rep(cm_id, rep_param);
2635 if (ret) {
2636 printk(KERN_ERR "sending SRP_LOGIN_REQ response failed"
2637 " (error code = %d)\n", ret);
2638 goto release_channel;
2639 }
2640
2641 spin_lock_irq(&sdev->spinlock);
2642 list_add_tail(&ch->list, &sdev->rch_list);
2643 spin_unlock_irq(&sdev->spinlock);
2644
2645 goto out;
2646
2647release_channel:
2648 srpt_set_ch_state(ch, CH_RELEASING);
2649 transport_deregister_session_configfs(ch->sess);
2650
2651deregister_session:
2652 transport_deregister_session(ch->sess);
2653 ch->sess = NULL;
2654
2655destroy_ib:
2656 srpt_destroy_ch_ib(ch);
2657
2658free_ring:
2659 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2660 ch->sport->sdev, ch->rq_size,
2661 ch->rsp_size, DMA_TO_DEVICE);
2662free_ch:
2663 kfree(ch);
2664
2665reject:
2666 rej->opcode = SRP_LOGIN_REJ;
2667 rej->tag = req->tag;
2668 rej->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2669 | SRP_BUF_FORMAT_INDIRECT);
2670
2671 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2672 (void *)rej, sizeof *rej);
2673
2674out:
2675 kfree(rep_param);
2676 kfree(rsp);
2677 kfree(rej);
2678
2679 return ret;
2680}
2681
2682static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2683{
2684 printk(KERN_INFO "Received IB REJ for cm_id %p.\n", cm_id);
2685 srpt_drain_channel(cm_id);
2686}
2687
2688/**
2689 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2690 *
2691 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2692 * and that the recipient may begin transmitting (RTU = ready to use).
2693 */
2694static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2695{
2696 struct srpt_rdma_ch *ch;
2697 int ret;
2698
2699 ch = srpt_find_channel(cm_id->context, cm_id);
2700 BUG_ON(!ch);
2701
2702 if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
2703 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2704
2705 ret = srpt_ch_qp_rts(ch, ch->qp);
2706
2707 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2708 wait_list) {
2709 list_del(&ioctx->wait_list);
2710 srpt_handle_new_iu(ch, ioctx, NULL);
2711 }
2712 if (ret)
2713 srpt_close_ch(ch);
2714 }
2715}
2716
2717static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2718{
2719 printk(KERN_INFO "Received IB TimeWait exit for cm_id %p.\n", cm_id);
2720 srpt_drain_channel(cm_id);
2721}
2722
2723static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2724{
2725 printk(KERN_INFO "Received IB REP error for cm_id %p.\n", cm_id);
2726 srpt_drain_channel(cm_id);
2727}
2728
2729/**
2730 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2731 */
2732static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2733{
2734 struct srpt_rdma_ch *ch;
2735 unsigned long flags;
2736 bool send_drep = false;
2737
2738 ch = srpt_find_channel(cm_id->context, cm_id);
2739 BUG_ON(!ch);
2740
2741 pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
2742
2743 spin_lock_irqsave(&ch->spinlock, flags);
2744 switch (ch->state) {
2745 case CH_CONNECTING:
2746 case CH_LIVE:
2747 send_drep = true;
2748 ch->state = CH_DISCONNECTING;
2749 break;
2750 case CH_DISCONNECTING:
2751 case CH_DRAINING:
2752 case CH_RELEASING:
2753 WARN(true, "unexpected channel state %d\n", ch->state);
2754 break;
2755 }
2756 spin_unlock_irqrestore(&ch->spinlock, flags);
2757
2758 if (send_drep) {
2759 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
2760 printk(KERN_ERR "Sending IB DREP failed.\n");
2761 printk(KERN_INFO "Received DREQ and sent DREP for session %s.\n",
2762 ch->sess_name);
2763 }
2764}
2765
2766/**
2767 * srpt_cm_drep_recv() - Process reception of a DREP message.
2768 */
2769static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2770{
2771 printk(KERN_INFO "Received InfiniBand DREP message for cm_id %p.\n",
2772 cm_id);
2773 srpt_drain_channel(cm_id);
2774}
2775
2776/**
2777 * srpt_cm_handler() - IB connection manager callback function.
2778 *
2779 * A non-zero return value will cause the caller destroy the CM ID.
2780 *
2781 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2782 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2783 * a non-zero value in any other case will trigger a race with the
2784 * ib_destroy_cm_id() call in srpt_release_channel().
2785 */
2786static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2787{
2788 int ret;
2789
2790 ret = 0;
2791 switch (event->event) {
2792 case IB_CM_REQ_RECEIVED:
2793 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2794 event->private_data);
2795 break;
2796 case IB_CM_REJ_RECEIVED:
2797 srpt_cm_rej_recv(cm_id);
2798 break;
2799 case IB_CM_RTU_RECEIVED:
2800 case IB_CM_USER_ESTABLISHED:
2801 srpt_cm_rtu_recv(cm_id);
2802 break;
2803 case IB_CM_DREQ_RECEIVED:
2804 srpt_cm_dreq_recv(cm_id);
2805 break;
2806 case IB_CM_DREP_RECEIVED:
2807 srpt_cm_drep_recv(cm_id);
2808 break;
2809 case IB_CM_TIMEWAIT_EXIT:
2810 srpt_cm_timewait_exit(cm_id);
2811 break;
2812 case IB_CM_REP_ERROR:
2813 srpt_cm_rep_error(cm_id);
2814 break;
2815 case IB_CM_DREQ_ERROR:
2816 printk(KERN_INFO "Received IB DREQ ERROR event.\n");
2817 break;
2818 case IB_CM_MRA_RECEIVED:
2819 printk(KERN_INFO "Received IB MRA event\n");
2820 break;
2821 default:
2822 printk(KERN_ERR "received unrecognized IB CM event %d\n",
2823 event->event);
2824 break;
2825 }
2826
2827 return ret;
2828}
2829
2830/**
2831 * srpt_perform_rdmas() - Perform IB RDMA.
2832 *
2833 * Returns zero upon success or a negative number upon failure.
2834 */
2835static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2836 struct srpt_send_ioctx *ioctx)
2837{
2838 struct ib_send_wr wr;
2839 struct ib_send_wr *bad_wr;
2840 struct rdma_iu *riu;
2841 int i;
2842 int ret;
2843 int sq_wr_avail;
2844 enum dma_data_direction dir;
2845 const int n_rdma = ioctx->n_rdma;
2846
2847 dir = ioctx->cmd.data_direction;
2848 if (dir == DMA_TO_DEVICE) {
2849 /* write */
2850 ret = -ENOMEM;
2851 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2852 if (sq_wr_avail < 0) {
2853 printk(KERN_WARNING "IB send queue full (needed %d)\n",
2854 n_rdma);
2855 goto out;
2856 }
2857 }
2858
2859 ioctx->rdma_aborted = false;
2860 ret = 0;
2861 riu = ioctx->rdma_ius;
2862 memset(&wr, 0, sizeof wr);
2863
2864 for (i = 0; i < n_rdma; ++i, ++riu) {
2865 if (dir == DMA_FROM_DEVICE) {
2866 wr.opcode = IB_WR_RDMA_WRITE;
2867 wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2868 SRPT_RDMA_WRITE_LAST :
2869 SRPT_RDMA_MID,
2870 ioctx->ioctx.index);
2871 } else {
2872 wr.opcode = IB_WR_RDMA_READ;
2873 wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2874 SRPT_RDMA_READ_LAST :
2875 SRPT_RDMA_MID,
2876 ioctx->ioctx.index);
2877 }
2878 wr.next = NULL;
2879 wr.wr.rdma.remote_addr = riu->raddr;
2880 wr.wr.rdma.rkey = riu->rkey;
2881 wr.num_sge = riu->sge_cnt;
2882 wr.sg_list = riu->sge;
2883
2884 /* only get completion event for the last rdma write */
2885 if (i == (n_rdma - 1) && dir == DMA_TO_DEVICE)
2886 wr.send_flags = IB_SEND_SIGNALED;
2887
2888 ret = ib_post_send(ch->qp, &wr, &bad_wr);
2889 if (ret)
2890 break;
2891 }
2892
2893 if (ret)
2894 printk(KERN_ERR "%s[%d]: ib_post_send() returned %d for %d/%d",
2895 __func__, __LINE__, ret, i, n_rdma);
2896 if (ret && i > 0) {
2897 wr.num_sge = 0;
2898 wr.wr_id = encode_wr_id(SRPT_RDMA_ABORT, ioctx->ioctx.index);
2899 wr.send_flags = IB_SEND_SIGNALED;
2900 while (ch->state == CH_LIVE &&
2901 ib_post_send(ch->qp, &wr, &bad_wr) != 0) {
2902 printk(KERN_INFO "Trying to abort failed RDMA transfer [%d]",
2903 ioctx->ioctx.index);
2904 msleep(1000);
2905 }
2906 while (ch->state != CH_RELEASING && !ioctx->rdma_aborted) {
2907 printk(KERN_INFO "Waiting until RDMA abort finished [%d]",
2908 ioctx->ioctx.index);
2909 msleep(1000);
2910 }
2911 }
2912out:
2913 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2914 atomic_add(n_rdma, &ch->sq_wr_avail);
2915 return ret;
2916}
2917
2918/**
2919 * srpt_xfer_data() - Start data transfer from initiator to target.
2920 */
2921static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2922 struct srpt_send_ioctx *ioctx)
2923{
2924 int ret;
2925
2926 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2927 if (ret) {
2928 printk(KERN_ERR "%s[%d] ret=%d\n", __func__, __LINE__, ret);
2929 goto out;
2930 }
2931
2932 ret = srpt_perform_rdmas(ch, ioctx);
2933 if (ret) {
2934 if (ret == -EAGAIN || ret == -ENOMEM)
2935 printk(KERN_INFO "%s[%d] queue full -- ret=%d\n",
2936 __func__, __LINE__, ret);
2937 else
2938 printk(KERN_ERR "%s[%d] fatal error -- ret=%d\n",
2939 __func__, __LINE__, ret);
2940 goto out_unmap;
2941 }
2942
2943out:
2944 return ret;
2945out_unmap:
2946 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2947 goto out;
2948}
2949
2950static int srpt_write_pending_status(struct se_cmd *se_cmd)
2951{
2952 struct srpt_send_ioctx *ioctx;
2953
2954 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2955 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2956}
2957
2958/*
2959 * srpt_write_pending() - Start data transfer from initiator to target (write).
2960 */
2961static int srpt_write_pending(struct se_cmd *se_cmd)
2962{
2963 struct srpt_rdma_ch *ch;
2964 struct srpt_send_ioctx *ioctx;
2965 enum srpt_command_state new_state;
2966 enum rdma_ch_state ch_state;
2967 int ret;
2968
2969 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2970
2971 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2972 WARN_ON(new_state == SRPT_STATE_DONE);
2973
2974 ch = ioctx->ch;
2975 BUG_ON(!ch);
2976
2977 ch_state = srpt_get_ch_state(ch);
2978 switch (ch_state) {
2979 case CH_CONNECTING:
2980 WARN(true, "unexpected channel state %d\n", ch_state);
2981 ret = -EINVAL;
2982 goto out;
2983 case CH_LIVE:
2984 break;
2985 case CH_DISCONNECTING:
2986 case CH_DRAINING:
2987 case CH_RELEASING:
2988 pr_debug("cmd with tag %lld: channel disconnecting\n",
2989 ioctx->tag);
2990 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2991 ret = -EINVAL;
2992 goto out;
2993 }
2994 ret = srpt_xfer_data(ch, ioctx);
2995
2996out:
2997 return ret;
2998}
2999
3000static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
3001{
3002 switch (tcm_mgmt_status) {
3003 case TMR_FUNCTION_COMPLETE:
3004 return SRP_TSK_MGMT_SUCCESS;
3005 case TMR_FUNCTION_REJECTED:
3006 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
3007 }
3008 return SRP_TSK_MGMT_FAILED;
3009}
3010
3011/**
3012 * srpt_queue_response() - Transmits the response to a SCSI command.
3013 *
3014 * Callback function called by the TCM core. Must not block since it can be
3015 * invoked on the context of the IB completion handler.
3016 */
3017static int srpt_queue_response(struct se_cmd *cmd)
3018{
3019 struct srpt_rdma_ch *ch;
3020 struct srpt_send_ioctx *ioctx;
3021 enum srpt_command_state state;
3022 unsigned long flags;
3023 int ret;
3024 enum dma_data_direction dir;
3025 int resp_len;
3026 u8 srp_tm_status;
3027
3028 ret = 0;
3029
3030 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3031 ch = ioctx->ch;
3032 BUG_ON(!ch);
3033
3034 spin_lock_irqsave(&ioctx->spinlock, flags);
3035 state = ioctx->state;
3036 switch (state) {
3037 case SRPT_STATE_NEW:
3038 case SRPT_STATE_DATA_IN:
3039 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
3040 break;
3041 case SRPT_STATE_MGMT:
3042 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
3043 break;
3044 default:
3045 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
3046 ch, ioctx->ioctx.index, ioctx->state);
3047 break;
3048 }
3049 spin_unlock_irqrestore(&ioctx->spinlock, flags);
3050
3051 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
3052 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
3053 atomic_inc(&ch->req_lim_delta);
3054 srpt_abort_cmd(ioctx);
3055 goto out;
3056 }
3057
3058 dir = ioctx->cmd.data_direction;
3059
3060 /* For read commands, transfer the data to the initiator. */
3061 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
3062 !ioctx->queue_status_only) {
3063 ret = srpt_xfer_data(ch, ioctx);
3064 if (ret) {
3065 printk(KERN_ERR "xfer_data failed for tag %llu\n",
3066 ioctx->tag);
3067 goto out;
3068 }
3069 }
3070
3071 if (state != SRPT_STATE_MGMT)
3072 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->tag,
3073 cmd->scsi_status);
3074 else {
3075 srp_tm_status
3076 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
3077 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
3078 ioctx->tag);
3079 }
3080 ret = srpt_post_send(ch, ioctx, resp_len);
3081 if (ret) {
3082 printk(KERN_ERR "sending cmd response failed for tag %llu\n",
3083 ioctx->tag);
3084 srpt_unmap_sg_to_ib_sge(ch, ioctx);
3085 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
3086 kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
3087 }
3088
3089out:
3090 return ret;
3091}
3092
3093static int srpt_queue_status(struct se_cmd *cmd)
3094{
3095 struct srpt_send_ioctx *ioctx;
3096
3097 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3098 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
3099 if (cmd->se_cmd_flags &
3100 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
3101 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
3102 ioctx->queue_status_only = true;
3103 return srpt_queue_response(cmd);
3104}
3105
3106static void srpt_refresh_port_work(struct work_struct *work)
3107{
3108 struct srpt_port *sport = container_of(work, struct srpt_port, work);
3109
3110 srpt_refresh_port(sport);
3111}
3112
3113static int srpt_ch_list_empty(struct srpt_device *sdev)
3114{
3115 int res;
3116
3117 spin_lock_irq(&sdev->spinlock);
3118 res = list_empty(&sdev->rch_list);
3119 spin_unlock_irq(&sdev->spinlock);
3120
3121 return res;
3122}
3123
3124/**
3125 * srpt_release_sdev() - Free the channel resources associated with a target.
3126 */
3127static int srpt_release_sdev(struct srpt_device *sdev)
3128{
3129 struct srpt_rdma_ch *ch, *tmp_ch;
3130 int res;
3131
3132 WARN_ON_ONCE(irqs_disabled());
3133
3134 BUG_ON(!sdev);
3135
3136 spin_lock_irq(&sdev->spinlock);
3137 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
3138 __srpt_close_ch(ch);
3139 spin_unlock_irq(&sdev->spinlock);
3140
3141 res = wait_event_interruptible(sdev->ch_releaseQ,
3142 srpt_ch_list_empty(sdev));
3143 if (res)
3144 printk(KERN_ERR "%s: interrupted.\n", __func__);
3145
3146 return 0;
3147}
3148
3149static struct srpt_port *__srpt_lookup_port(const char *name)
3150{
3151 struct ib_device *dev;
3152 struct srpt_device *sdev;
3153 struct srpt_port *sport;
3154 int i;
3155
3156 list_for_each_entry(sdev, &srpt_dev_list, list) {
3157 dev = sdev->device;
3158 if (!dev)
3159 continue;
3160
3161 for (i = 0; i < dev->phys_port_cnt; i++) {
3162 sport = &sdev->port[i];
3163
3164 if (!strcmp(sport->port_guid, name))
3165 return sport;
3166 }
3167 }
3168
3169 return NULL;
3170}
3171
3172static struct srpt_port *srpt_lookup_port(const char *name)
3173{
3174 struct srpt_port *sport;
3175
3176 spin_lock(&srpt_dev_lock);
3177 sport = __srpt_lookup_port(name);
3178 spin_unlock(&srpt_dev_lock);
3179
3180 return sport;
3181}
3182
3183/**
3184 * srpt_add_one() - Infiniband device addition callback function.
3185 */
3186static void srpt_add_one(struct ib_device *device)
3187{
3188 struct srpt_device *sdev;
3189 struct srpt_port *sport;
3190 struct ib_srq_init_attr srq_attr;
3191 int i;
3192
3193 pr_debug("device = %p, device->dma_ops = %p\n", device,
3194 device->dma_ops);
3195
3196 sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
3197 if (!sdev)
3198 goto err;
3199
3200 sdev->device = device;
3201 INIT_LIST_HEAD(&sdev->rch_list);
3202 init_waitqueue_head(&sdev->ch_releaseQ);
3203 spin_lock_init(&sdev->spinlock);
3204
3205 if (ib_query_device(device, &sdev->dev_attr))
3206 goto free_dev;
3207
3208 sdev->pd = ib_alloc_pd(device);
3209 if (IS_ERR(sdev->pd))
3210 goto free_dev;
3211
3212 sdev->mr = ib_get_dma_mr(sdev->pd, IB_ACCESS_LOCAL_WRITE);
3213 if (IS_ERR(sdev->mr))
3214 goto err_pd;
3215
3216 sdev->srq_size = min(srpt_srq_size, sdev->dev_attr.max_srq_wr);
3217
3218 srq_attr.event_handler = srpt_srq_event;
3219 srq_attr.srq_context = (void *)sdev;
3220 srq_attr.attr.max_wr = sdev->srq_size;
3221 srq_attr.attr.max_sge = 1;
3222 srq_attr.attr.srq_limit = 0;
3223
3224 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
3225 if (IS_ERR(sdev->srq))
3226 goto err_mr;
3227
3228 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
3229 __func__, sdev->srq_size, sdev->dev_attr.max_srq_wr,
3230 device->name);
3231
3232 if (!srpt_service_guid)
3233 srpt_service_guid = be64_to_cpu(device->node_guid);
3234
3235 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
3236 if (IS_ERR(sdev->cm_id))
3237 goto err_srq;
3238
3239 /* print out target login information */
3240 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
3241 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
3242 srpt_service_guid, srpt_service_guid);
3243
3244 /*
3245 * We do not have a consistent service_id (ie. also id_ext of target_id)
3246 * to identify this target. We currently use the guid of the first HCA
3247 * in the system as service_id; therefore, the target_id will change
3248 * if this HCA is gone bad and replaced by different HCA
3249 */
3250 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0, NULL))
3251 goto err_cm;
3252
3253 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
3254 srpt_event_handler);
3255 if (ib_register_event_handler(&sdev->event_handler))
3256 goto err_cm;
3257
3258 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
3259 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
3260 sizeof(*sdev->ioctx_ring[0]),
3261 srp_max_req_size, DMA_FROM_DEVICE);
3262 if (!sdev->ioctx_ring)
3263 goto err_event;
3264
3265 for (i = 0; i < sdev->srq_size; ++i)
3266 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
3267
Roland Dreierf2250662012-02-02 12:55:58 -08003268 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
Bart Van Asschea42d9852011-10-14 01:30:46 +00003269
3270 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3271 sport = &sdev->port[i - 1];
3272 sport->sdev = sdev;
3273 sport->port = i;
3274 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3275 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3276 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3277 INIT_WORK(&sport->work, srpt_refresh_port_work);
3278 INIT_LIST_HEAD(&sport->port_acl_list);
3279 spin_lock_init(&sport->port_acl_lock);
3280
3281 if (srpt_refresh_port(sport)) {
3282 printk(KERN_ERR "MAD registration failed for %s-%d.\n",
3283 srpt_sdev_name(sdev), i);
3284 goto err_ring;
3285 }
3286 snprintf(sport->port_guid, sizeof(sport->port_guid),
3287 "0x%016llx%016llx",
3288 be64_to_cpu(sport->gid.global.subnet_prefix),
3289 be64_to_cpu(sport->gid.global.interface_id));
3290 }
3291
3292 spin_lock(&srpt_dev_lock);
3293 list_add_tail(&sdev->list, &srpt_dev_list);
3294 spin_unlock(&srpt_dev_lock);
3295
3296out:
3297 ib_set_client_data(device, &srpt_client, sdev);
3298 pr_debug("added %s.\n", device->name);
3299 return;
3300
3301err_ring:
3302 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3303 sdev->srq_size, srp_max_req_size,
3304 DMA_FROM_DEVICE);
3305err_event:
3306 ib_unregister_event_handler(&sdev->event_handler);
3307err_cm:
3308 ib_destroy_cm_id(sdev->cm_id);
3309err_srq:
3310 ib_destroy_srq(sdev->srq);
3311err_mr:
3312 ib_dereg_mr(sdev->mr);
3313err_pd:
3314 ib_dealloc_pd(sdev->pd);
3315free_dev:
3316 kfree(sdev);
3317err:
3318 sdev = NULL;
3319 printk(KERN_INFO "%s(%s) failed.\n", __func__, device->name);
3320 goto out;
3321}
3322
3323/**
3324 * srpt_remove_one() - InfiniBand device removal callback function.
3325 */
3326static void srpt_remove_one(struct ib_device *device)
3327{
3328 struct srpt_device *sdev;
3329 int i;
3330
3331 sdev = ib_get_client_data(device, &srpt_client);
3332 if (!sdev) {
3333 printk(KERN_INFO "%s(%s): nothing to do.\n", __func__,
3334 device->name);
3335 return;
3336 }
3337
3338 srpt_unregister_mad_agent(sdev);
3339
3340 ib_unregister_event_handler(&sdev->event_handler);
3341
3342 /* Cancel any work queued by the just unregistered IB event handler. */
3343 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3344 cancel_work_sync(&sdev->port[i].work);
3345
3346 ib_destroy_cm_id(sdev->cm_id);
3347
3348 /*
3349 * Unregistering a target must happen after destroying sdev->cm_id
3350 * such that no new SRP_LOGIN_REQ information units can arrive while
3351 * destroying the target.
3352 */
3353 spin_lock(&srpt_dev_lock);
3354 list_del(&sdev->list);
3355 spin_unlock(&srpt_dev_lock);
3356 srpt_release_sdev(sdev);
3357
3358 ib_destroy_srq(sdev->srq);
3359 ib_dereg_mr(sdev->mr);
3360 ib_dealloc_pd(sdev->pd);
3361
3362 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3363 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
3364 sdev->ioctx_ring = NULL;
3365 kfree(sdev);
3366}
3367
3368static struct ib_client srpt_client = {
3369 .name = DRV_NAME,
3370 .add = srpt_add_one,
3371 .remove = srpt_remove_one
3372};
3373
3374static int srpt_check_true(struct se_portal_group *se_tpg)
3375{
3376 return 1;
3377}
3378
3379static int srpt_check_false(struct se_portal_group *se_tpg)
3380{
3381 return 0;
3382}
3383
3384static char *srpt_get_fabric_name(void)
3385{
3386 return "srpt";
3387}
3388
3389static u8 srpt_get_fabric_proto_ident(struct se_portal_group *se_tpg)
3390{
3391 return SCSI_TRANSPORTID_PROTOCOLID_SRP;
3392}
3393
3394static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3395{
3396 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3397
3398 return sport->port_guid;
3399}
3400
3401static u16 srpt_get_tag(struct se_portal_group *tpg)
3402{
3403 return 1;
3404}
3405
3406static u32 srpt_get_default_depth(struct se_portal_group *se_tpg)
3407{
3408 return 1;
3409}
3410
3411static u32 srpt_get_pr_transport_id(struct se_portal_group *se_tpg,
3412 struct se_node_acl *se_nacl,
3413 struct t10_pr_registration *pr_reg,
3414 int *format_code, unsigned char *buf)
3415{
3416 struct srpt_node_acl *nacl;
3417 struct spc_rdma_transport_id *tr_id;
3418
3419 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3420 tr_id = (void *)buf;
3421 tr_id->protocol_identifier = SCSI_TRANSPORTID_PROTOCOLID_SRP;
3422 memcpy(tr_id->i_port_id, nacl->i_port_id, sizeof(tr_id->i_port_id));
3423 return sizeof(*tr_id);
3424}
3425
3426static u32 srpt_get_pr_transport_id_len(struct se_portal_group *se_tpg,
3427 struct se_node_acl *se_nacl,
3428 struct t10_pr_registration *pr_reg,
3429 int *format_code)
3430{
3431 *format_code = 0;
3432 return sizeof(struct spc_rdma_transport_id);
3433}
3434
3435static char *srpt_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
3436 const char *buf, u32 *out_tid_len,
3437 char **port_nexus_ptr)
3438{
3439 struct spc_rdma_transport_id *tr_id;
3440
3441 *port_nexus_ptr = NULL;
3442 *out_tid_len = sizeof(struct spc_rdma_transport_id);
3443 tr_id = (void *)buf;
3444 return (char *)tr_id->i_port_id;
3445}
3446
3447static struct se_node_acl *srpt_alloc_fabric_acl(struct se_portal_group *se_tpg)
3448{
3449 struct srpt_node_acl *nacl;
3450
3451 nacl = kzalloc(sizeof(struct srpt_node_acl), GFP_KERNEL);
3452 if (!nacl) {
3453 printk(KERN_ERR "Unable to alocate struct srpt_node_acl\n");
3454 return NULL;
3455 }
3456
3457 return &nacl->nacl;
3458}
3459
3460static void srpt_release_fabric_acl(struct se_portal_group *se_tpg,
3461 struct se_node_acl *se_nacl)
3462{
3463 struct srpt_node_acl *nacl;
3464
3465 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3466 kfree(nacl);
3467}
3468
3469static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3470{
3471 return 1;
3472}
3473
3474static void srpt_release_cmd(struct se_cmd *se_cmd)
3475{
3476}
3477
3478/**
3479 * srpt_shutdown_session() - Whether or not a session may be shut down.
3480 */
3481static int srpt_shutdown_session(struct se_session *se_sess)
3482{
3483 return true;
3484}
3485
3486/**
3487 * srpt_close_session() - Forcibly close a session.
3488 *
3489 * Callback function invoked by the TCM core to clean up sessions associated
3490 * with a node ACL when the user invokes
3491 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3492 */
3493static void srpt_close_session(struct se_session *se_sess)
3494{
3495 DECLARE_COMPLETION_ONSTACK(release_done);
3496 struct srpt_rdma_ch *ch;
3497 struct srpt_device *sdev;
3498 int res;
3499
3500 ch = se_sess->fabric_sess_ptr;
3501 WARN_ON(ch->sess != se_sess);
3502
3503 pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
3504
3505 sdev = ch->sport->sdev;
3506 spin_lock_irq(&sdev->spinlock);
3507 BUG_ON(ch->release_done);
3508 ch->release_done = &release_done;
3509 __srpt_close_ch(ch);
3510 spin_unlock_irq(&sdev->spinlock);
3511
3512 res = wait_for_completion_timeout(&release_done, 60 * HZ);
3513 WARN_ON(res <= 0);
3514}
3515
3516/**
3517 * To do: Find out whether stop_session() has a meaning for transports
3518 * other than iSCSI.
3519 */
3520static void srpt_stop_session(struct se_session *se_sess, int sess_sleep,
3521 int conn_sleep)
3522{
3523}
3524
3525static void srpt_reset_nexus(struct se_session *sess)
3526{
3527 printk(KERN_ERR "This is the SRP protocol, not iSCSI\n");
3528}
3529
3530static int srpt_sess_logged_in(struct se_session *se_sess)
3531{
3532 return true;
3533}
3534
3535/**
3536 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3537 *
3538 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3539 * This object represents an arbitrary integer used to uniquely identify a
3540 * particular attached remote initiator port to a particular SCSI target port
3541 * within a particular SCSI target device within a particular SCSI instance.
3542 */
3543static u32 srpt_sess_get_index(struct se_session *se_sess)
3544{
3545 return 0;
3546}
3547
3548static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3549{
3550}
3551
3552static u32 srpt_get_task_tag(struct se_cmd *se_cmd)
3553{
3554 struct srpt_send_ioctx *ioctx;
3555
3556 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3557 return ioctx->tag;
3558}
3559
3560/* Note: only used from inside debug printk's by the TCM core. */
3561static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3562{
3563 struct srpt_send_ioctx *ioctx;
3564
3565 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3566 return srpt_get_cmd_state(ioctx);
3567}
3568
3569static u16 srpt_set_fabric_sense_len(struct se_cmd *cmd, u32 sense_length)
3570{
3571 return 0;
3572}
3573
3574static u16 srpt_get_fabric_sense_len(void)
3575{
3576 return 0;
3577}
3578
3579static int srpt_is_state_remove(struct se_cmd *se_cmd)
3580{
3581 return 0;
3582}
3583
3584/**
3585 * srpt_parse_i_port_id() - Parse an initiator port ID.
3586 * @name: ASCII representation of a 128-bit initiator port ID.
3587 * @i_port_id: Binary 128-bit port ID.
3588 */
3589static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3590{
3591 const char *p;
3592 unsigned len, count, leading_zero_bytes;
3593 int ret, rc;
3594
3595 p = name;
3596 if (strnicmp(p, "0x", 2) == 0)
3597 p += 2;
3598 ret = -EINVAL;
3599 len = strlen(p);
3600 if (len % 2)
3601 goto out;
3602 count = min(len / 2, 16U);
3603 leading_zero_bytes = 16 - count;
3604 memset(i_port_id, 0, leading_zero_bytes);
3605 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3606 if (rc < 0)
3607 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3608 ret = 0;
3609out:
3610 return ret;
3611}
3612
3613/*
3614 * configfs callback function invoked for
3615 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3616 */
3617static struct se_node_acl *srpt_make_nodeacl(struct se_portal_group *tpg,
3618 struct config_group *group,
3619 const char *name)
3620{
3621 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3622 struct se_node_acl *se_nacl, *se_nacl_new;
3623 struct srpt_node_acl *nacl;
3624 int ret = 0;
3625 u32 nexus_depth = 1;
3626 u8 i_port_id[16];
3627
3628 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
3629 printk(KERN_ERR "invalid initiator port ID %s\n", name);
3630 ret = -EINVAL;
3631 goto err;
3632 }
3633
3634 se_nacl_new = srpt_alloc_fabric_acl(tpg);
3635 if (!se_nacl_new) {
3636 ret = -ENOMEM;
3637 goto err;
3638 }
3639 /*
3640 * nacl_new may be released by core_tpg_add_initiator_node_acl()
3641 * when converting a node ACL from demo mode to explict
3642 */
3643 se_nacl = core_tpg_add_initiator_node_acl(tpg, se_nacl_new, name,
3644 nexus_depth);
3645 if (IS_ERR(se_nacl)) {
3646 ret = PTR_ERR(se_nacl);
3647 goto err;
3648 }
3649 /* Locate our struct srpt_node_acl and set sdev and i_port_id. */
3650 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3651 memcpy(&nacl->i_port_id[0], &i_port_id[0], 16);
3652 nacl->sport = sport;
3653
3654 spin_lock_irq(&sport->port_acl_lock);
3655 list_add_tail(&nacl->list, &sport->port_acl_list);
3656 spin_unlock_irq(&sport->port_acl_lock);
3657
3658 return se_nacl;
3659err:
3660 return ERR_PTR(ret);
3661}
3662
3663/*
3664 * configfs callback function invoked for
3665 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3666 */
3667static void srpt_drop_nodeacl(struct se_node_acl *se_nacl)
3668{
3669 struct srpt_node_acl *nacl;
3670 struct srpt_device *sdev;
3671 struct srpt_port *sport;
3672
3673 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3674 sport = nacl->sport;
3675 sdev = sport->sdev;
3676 spin_lock_irq(&sport->port_acl_lock);
3677 list_del(&nacl->list);
3678 spin_unlock_irq(&sport->port_acl_lock);
3679 core_tpg_del_initiator_node_acl(&sport->port_tpg_1, se_nacl, 1);
3680 srpt_release_fabric_acl(NULL, se_nacl);
3681}
3682
3683static ssize_t srpt_tpg_attrib_show_srp_max_rdma_size(
3684 struct se_portal_group *se_tpg,
3685 char *page)
3686{
3687 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3688
3689 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3690}
3691
3692static ssize_t srpt_tpg_attrib_store_srp_max_rdma_size(
3693 struct se_portal_group *se_tpg,
3694 const char *page,
3695 size_t count)
3696{
3697 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3698 unsigned long val;
3699 int ret;
3700
3701 ret = strict_strtoul(page, 0, &val);
3702 if (ret < 0) {
3703 pr_err("strict_strtoul() failed with ret: %d\n", ret);
3704 return -EINVAL;
3705 }
3706 if (val > MAX_SRPT_RDMA_SIZE) {
3707 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3708 MAX_SRPT_RDMA_SIZE);
3709 return -EINVAL;
3710 }
3711 if (val < DEFAULT_MAX_RDMA_SIZE) {
3712 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3713 val, DEFAULT_MAX_RDMA_SIZE);
3714 return -EINVAL;
3715 }
3716 sport->port_attrib.srp_max_rdma_size = val;
3717
3718 return count;
3719}
3720
3721TF_TPG_ATTRIB_ATTR(srpt, srp_max_rdma_size, S_IRUGO | S_IWUSR);
3722
3723static ssize_t srpt_tpg_attrib_show_srp_max_rsp_size(
3724 struct se_portal_group *se_tpg,
3725 char *page)
3726{
3727 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3728
3729 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3730}
3731
3732static ssize_t srpt_tpg_attrib_store_srp_max_rsp_size(
3733 struct se_portal_group *se_tpg,
3734 const char *page,
3735 size_t count)
3736{
3737 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3738 unsigned long val;
3739 int ret;
3740
3741 ret = strict_strtoul(page, 0, &val);
3742 if (ret < 0) {
3743 pr_err("strict_strtoul() failed with ret: %d\n", ret);
3744 return -EINVAL;
3745 }
3746 if (val > MAX_SRPT_RSP_SIZE) {
3747 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3748 MAX_SRPT_RSP_SIZE);
3749 return -EINVAL;
3750 }
3751 if (val < MIN_MAX_RSP_SIZE) {
3752 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3753 MIN_MAX_RSP_SIZE);
3754 return -EINVAL;
3755 }
3756 sport->port_attrib.srp_max_rsp_size = val;
3757
3758 return count;
3759}
3760
3761TF_TPG_ATTRIB_ATTR(srpt, srp_max_rsp_size, S_IRUGO | S_IWUSR);
3762
3763static ssize_t srpt_tpg_attrib_show_srp_sq_size(
3764 struct se_portal_group *se_tpg,
3765 char *page)
3766{
3767 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3768
3769 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3770}
3771
3772static ssize_t srpt_tpg_attrib_store_srp_sq_size(
3773 struct se_portal_group *se_tpg,
3774 const char *page,
3775 size_t count)
3776{
3777 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3778 unsigned long val;
3779 int ret;
3780
3781 ret = strict_strtoul(page, 0, &val);
3782 if (ret < 0) {
3783 pr_err("strict_strtoul() failed with ret: %d\n", ret);
3784 return -EINVAL;
3785 }
3786 if (val > MAX_SRPT_SRQ_SIZE) {
3787 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3788 MAX_SRPT_SRQ_SIZE);
3789 return -EINVAL;
3790 }
3791 if (val < MIN_SRPT_SRQ_SIZE) {
3792 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3793 MIN_SRPT_SRQ_SIZE);
3794 return -EINVAL;
3795 }
3796 sport->port_attrib.srp_sq_size = val;
3797
3798 return count;
3799}
3800
3801TF_TPG_ATTRIB_ATTR(srpt, srp_sq_size, S_IRUGO | S_IWUSR);
3802
3803static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
3804 &srpt_tpg_attrib_srp_max_rdma_size.attr,
3805 &srpt_tpg_attrib_srp_max_rsp_size.attr,
3806 &srpt_tpg_attrib_srp_sq_size.attr,
3807 NULL,
3808};
3809
3810static ssize_t srpt_tpg_show_enable(
3811 struct se_portal_group *se_tpg,
3812 char *page)
3813{
3814 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3815
3816 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3817}
3818
3819static ssize_t srpt_tpg_store_enable(
3820 struct se_portal_group *se_tpg,
3821 const char *page,
3822 size_t count)
3823{
3824 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3825 unsigned long tmp;
3826 int ret;
3827
3828 ret = strict_strtoul(page, 0, &tmp);
3829 if (ret < 0) {
3830 printk(KERN_ERR "Unable to extract srpt_tpg_store_enable\n");
3831 return -EINVAL;
3832 }
3833
3834 if ((tmp != 0) && (tmp != 1)) {
3835 printk(KERN_ERR "Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
3836 return -EINVAL;
3837 }
3838 if (tmp == 1)
3839 sport->enabled = true;
3840 else
3841 sport->enabled = false;
3842
3843 return count;
3844}
3845
3846TF_TPG_BASE_ATTR(srpt, enable, S_IRUGO | S_IWUSR);
3847
3848static struct configfs_attribute *srpt_tpg_attrs[] = {
3849 &srpt_tpg_enable.attr,
3850 NULL,
3851};
3852
3853/**
3854 * configfs callback invoked for
3855 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3856 */
3857static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3858 struct config_group *group,
3859 const char *name)
3860{
3861 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3862 int res;
3863
3864 /* Initialize sport->port_wwn and sport->port_tpg_1 */
3865 res = core_tpg_register(&srpt_target->tf_ops, &sport->port_wwn,
3866 &sport->port_tpg_1, sport, TRANSPORT_TPG_TYPE_NORMAL);
3867 if (res)
3868 return ERR_PTR(res);
3869
3870 return &sport->port_tpg_1;
3871}
3872
3873/**
3874 * configfs callback invoked for
3875 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3876 */
3877static void srpt_drop_tpg(struct se_portal_group *tpg)
3878{
3879 struct srpt_port *sport = container_of(tpg,
3880 struct srpt_port, port_tpg_1);
3881
3882 sport->enabled = false;
3883 core_tpg_deregister(&sport->port_tpg_1);
3884}
3885
3886/**
3887 * configfs callback invoked for
3888 * mkdir /sys/kernel/config/target/$driver/$port
3889 */
3890static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3891 struct config_group *group,
3892 const char *name)
3893{
3894 struct srpt_port *sport;
3895 int ret;
3896
3897 sport = srpt_lookup_port(name);
3898 pr_debug("make_tport(%s)\n", name);
3899 ret = -EINVAL;
3900 if (!sport)
3901 goto err;
3902
3903 return &sport->port_wwn;
3904
3905err:
3906 return ERR_PTR(ret);
3907}
3908
3909/**
3910 * configfs callback invoked for
3911 * rmdir /sys/kernel/config/target/$driver/$port
3912 */
3913static void srpt_drop_tport(struct se_wwn *wwn)
3914{
3915 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3916
3917 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3918}
3919
3920static ssize_t srpt_wwn_show_attr_version(struct target_fabric_configfs *tf,
3921 char *buf)
3922{
3923 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3924}
3925
3926TF_WWN_ATTR_RO(srpt, version);
3927
3928static struct configfs_attribute *srpt_wwn_attrs[] = {
3929 &srpt_wwn_version.attr,
3930 NULL,
3931};
3932
3933static struct target_core_fabric_ops srpt_template = {
3934 .get_fabric_name = srpt_get_fabric_name,
3935 .get_fabric_proto_ident = srpt_get_fabric_proto_ident,
3936 .tpg_get_wwn = srpt_get_fabric_wwn,
3937 .tpg_get_tag = srpt_get_tag,
3938 .tpg_get_default_depth = srpt_get_default_depth,
3939 .tpg_get_pr_transport_id = srpt_get_pr_transport_id,
3940 .tpg_get_pr_transport_id_len = srpt_get_pr_transport_id_len,
3941 .tpg_parse_pr_out_transport_id = srpt_parse_pr_out_transport_id,
3942 .tpg_check_demo_mode = srpt_check_false,
3943 .tpg_check_demo_mode_cache = srpt_check_true,
3944 .tpg_check_demo_mode_write_protect = srpt_check_true,
3945 .tpg_check_prod_mode_write_protect = srpt_check_false,
3946 .tpg_alloc_fabric_acl = srpt_alloc_fabric_acl,
3947 .tpg_release_fabric_acl = srpt_release_fabric_acl,
3948 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3949 .release_cmd = srpt_release_cmd,
3950 .check_stop_free = srpt_check_stop_free,
3951 .shutdown_session = srpt_shutdown_session,
3952 .close_session = srpt_close_session,
3953 .stop_session = srpt_stop_session,
3954 .fall_back_to_erl0 = srpt_reset_nexus,
3955 .sess_logged_in = srpt_sess_logged_in,
3956 .sess_get_index = srpt_sess_get_index,
3957 .sess_get_initiator_sid = NULL,
3958 .write_pending = srpt_write_pending,
3959 .write_pending_status = srpt_write_pending_status,
3960 .set_default_node_attributes = srpt_set_default_node_attrs,
3961 .get_task_tag = srpt_get_task_tag,
3962 .get_cmd_state = srpt_get_tcm_cmd_state,
3963 .queue_data_in = srpt_queue_response,
3964 .queue_status = srpt_queue_status,
3965 .queue_tm_rsp = srpt_queue_response,
3966 .get_fabric_sense_len = srpt_get_fabric_sense_len,
3967 .set_fabric_sense_len = srpt_set_fabric_sense_len,
3968 .is_state_remove = srpt_is_state_remove,
3969 /*
3970 * Setup function pointers for generic logic in
3971 * target_core_fabric_configfs.c
3972 */
3973 .fabric_make_wwn = srpt_make_tport,
3974 .fabric_drop_wwn = srpt_drop_tport,
3975 .fabric_make_tpg = srpt_make_tpg,
3976 .fabric_drop_tpg = srpt_drop_tpg,
3977 .fabric_post_link = NULL,
3978 .fabric_pre_unlink = NULL,
3979 .fabric_make_np = NULL,
3980 .fabric_drop_np = NULL,
3981 .fabric_make_nodeacl = srpt_make_nodeacl,
3982 .fabric_drop_nodeacl = srpt_drop_nodeacl,
3983};
3984
3985/**
3986 * srpt_init_module() - Kernel module initialization.
3987 *
3988 * Note: Since ib_register_client() registers callback functions, and since at
3989 * least one of these callback functions (srpt_add_one()) calls target core
3990 * functions, this driver must be registered with the target core before
3991 * ib_register_client() is called.
3992 */
3993static int __init srpt_init_module(void)
3994{
3995 int ret;
3996
3997 ret = -EINVAL;
3998 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
3999 printk(KERN_ERR "invalid value %d for kernel module parameter"
4000 " srp_max_req_size -- must be at least %d.\n",
4001 srp_max_req_size, MIN_MAX_REQ_SIZE);
4002 goto out;
4003 }
4004
4005 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
4006 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
4007 printk(KERN_ERR "invalid value %d for kernel module parameter"
4008 " srpt_srq_size -- must be in the range [%d..%d].\n",
4009 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
4010 goto out;
4011 }
4012
Bart Van Asschea42d9852011-10-14 01:30:46 +00004013 srpt_target = target_fabric_configfs_init(THIS_MODULE, "srpt");
Dan Carpenter3af33632011-11-04 21:27:32 +03004014 if (IS_ERR(srpt_target)) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00004015 printk(KERN_ERR "couldn't register\n");
Dan Carpenter3af33632011-11-04 21:27:32 +03004016 ret = PTR_ERR(srpt_target);
Bart Van Asschea42d9852011-10-14 01:30:46 +00004017 goto out;
4018 }
4019
4020 srpt_target->tf_ops = srpt_template;
4021
4022 /* Enable SG chaining */
4023 srpt_target->tf_ops.task_sg_chaining = true;
4024
4025 /*
4026 * Set up default attribute lists.
4027 */
4028 srpt_target->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = srpt_wwn_attrs;
4029 srpt_target->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = srpt_tpg_attrs;
4030 srpt_target->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = srpt_tpg_attrib_attrs;
4031 srpt_target->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
4032 srpt_target->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
4033 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
4034 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
4035 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
4036 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
4037
4038 ret = target_fabric_configfs_register(srpt_target);
4039 if (ret < 0) {
4040 printk(KERN_ERR "couldn't register\n");
4041 goto out_free_target;
4042 }
4043
4044 ret = ib_register_client(&srpt_client);
4045 if (ret) {
4046 printk(KERN_ERR "couldn't register IB client\n");
4047 goto out_unregister_target;
4048 }
4049
4050 return 0;
4051
4052out_unregister_target:
4053 target_fabric_configfs_deregister(srpt_target);
4054 srpt_target = NULL;
4055out_free_target:
4056 if (srpt_target)
4057 target_fabric_configfs_free(srpt_target);
4058out:
4059 return ret;
4060}
4061
4062static void __exit srpt_cleanup_module(void)
4063{
4064 ib_unregister_client(&srpt_client);
4065 target_fabric_configfs_deregister(srpt_target);
4066 srpt_target = NULL;
4067}
4068
4069module_init(srpt_init_module);
4070module_exit(srpt_cleanup_module);