IB/srpt: Preparations for adding RDMA/CM support
[linux-2.6-block.git] / drivers / infiniband / ulp / srpt / ib_srpt.c
CommitLineData
a42d985b
BVA
1/*
2 * Copyright (c) 2006 - 2009 Mellanox Technology Inc. All rights reserved.
3 * Copyright (C) 2008 - 2011 Bart Van Assche <bvanassche@acm.org>.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 *
33 */
34
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/slab.h>
38#include <linux/err.h>
39#include <linux/ctype.h>
40#include <linux/kthread.h>
41#include <linux/string.h>
42#include <linux/delay.h>
43#include <linux/atomic.h>
c5efb621 44#include <rdma/ib_cache.h>
ba929992 45#include <scsi/scsi_proto.h>
a42d985b 46#include <scsi/scsi_tcq.h>
a42d985b 47#include <target/target_core_base.h>
a42d985b 48#include <target/target_core_fabric.h>
a42d985b
BVA
49#include "ib_srpt.h"
50
51/* Name of this kernel module. */
52#define DRV_NAME "ib_srpt"
53#define DRV_VERSION "2.0.0"
54#define DRV_RELDATE "2011-02-14"
55
56#define SRPT_ID_STRING "Linux SRP target"
57
58#undef pr_fmt
59#define pr_fmt(fmt) DRV_NAME " " fmt
60
61MODULE_AUTHOR("Vu Pham and Bart Van Assche");
62MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
63 "v" DRV_VERSION " (" DRV_RELDATE ")");
64MODULE_LICENSE("Dual BSD/GPL");
65
66/*
67 * Global Variables
68 */
69
70static u64 srpt_service_guid;
486d8b9f
RD
71static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
72static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
a42d985b
BVA
73
74static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
75module_param(srp_max_req_size, int, 0444);
76MODULE_PARM_DESC(srp_max_req_size,
77 "Maximum size of SRP request messages in bytes.");
78
79static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
80module_param(srpt_srq_size, int, 0444);
81MODULE_PARM_DESC(srpt_srq_size,
82 "Shared receive queue (SRQ) size.");
83
e4dca7b7 84static int srpt_get_u64_x(char *buffer, const struct kernel_param *kp)
a42d985b
BVA
85{
86 return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
87}
88module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
89 0444);
90MODULE_PARM_DESC(srpt_service_guid,
91 "Using this value for ioc_guid, id_ext, and cm_listen_id"
92 " instead of using the node_guid of the first HCA.");
93
94static struct ib_client srpt_client;
2c7f37ff 95static void srpt_release_cmd(struct se_cmd *se_cmd);
aaf45bd8 96static void srpt_free_ch(struct kref *kref);
a42d985b 97static int srpt_queue_status(struct se_cmd *cmd);
59fae4de
CH
98static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc);
99static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc);
387add46 100static void srpt_process_wait_list(struct srpt_rdma_ch *ch);
a42d985b 101
f130c220
BVA
102/*
103 * The only allowed channel state changes are those that change the channel
104 * state into a state with a higher numerical value. Hence the new > prev test.
a42d985b 105 */
f130c220 106static bool srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new)
a42d985b
BVA
107{
108 unsigned long flags;
109 enum rdma_ch_state prev;
f130c220 110 bool changed = false;
a42d985b
BVA
111
112 spin_lock_irqsave(&ch->spinlock, flags);
113 prev = ch->state;
f130c220 114 if (new > prev) {
a42d985b 115 ch->state = new;
f130c220
BVA
116 changed = true;
117 }
a42d985b 118 spin_unlock_irqrestore(&ch->spinlock, flags);
f130c220
BVA
119
120 return changed;
a42d985b
BVA
121}
122
123/**
10eac19b
BVA
124 * srpt_event_handler - asynchronous IB event callback function
125 * @handler: IB event handler registered by ib_register_event_handler().
126 * @event: Description of the event that occurred.
a42d985b
BVA
127 *
128 * Callback function called by the InfiniBand core when an asynchronous IB
129 * event occurs. This callback may occur in interrupt context. See also
130 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
131 * Architecture Specification.
132 */
133static void srpt_event_handler(struct ib_event_handler *handler,
134 struct ib_event *event)
135{
136 struct srpt_device *sdev;
137 struct srpt_port *sport;
5f1141b1 138 u8 port_num;
a42d985b
BVA
139
140 sdev = ib_get_client_data(event->device, &srpt_client);
141 if (!sdev || sdev->device != event->device)
142 return;
143
144 pr_debug("ASYNC event= %d on device= %s\n", event->event,
f68cba4e 145 sdev->device->name);
a42d985b
BVA
146
147 switch (event->event) {
148 case IB_EVENT_PORT_ERR:
5f1141b1
BVA
149 port_num = event->element.port_num - 1;
150 if (port_num < sdev->device->phys_port_cnt) {
151 sport = &sdev->port[port_num];
a42d985b
BVA
152 sport->lid = 0;
153 sport->sm_lid = 0;
5f1141b1
BVA
154 } else {
155 WARN(true, "event %d: port_num %d out of range 1..%d\n",
156 event->event, port_num + 1,
157 sdev->device->phys_port_cnt);
a42d985b
BVA
158 }
159 break;
160 case IB_EVENT_PORT_ACTIVE:
161 case IB_EVENT_LID_CHANGE:
162 case IB_EVENT_PKEY_CHANGE:
163 case IB_EVENT_SM_CHANGE:
164 case IB_EVENT_CLIENT_REREGISTER:
2aa1cf64 165 case IB_EVENT_GID_CHANGE:
a42d985b 166 /* Refresh port data asynchronously. */
5f1141b1
BVA
167 port_num = event->element.port_num - 1;
168 if (port_num < sdev->device->phys_port_cnt) {
169 sport = &sdev->port[port_num];
a42d985b
BVA
170 if (!sport->lid && !sport->sm_lid)
171 schedule_work(&sport->work);
5f1141b1
BVA
172 } else {
173 WARN(true, "event %d: port_num %d out of range 1..%d\n",
174 event->event, port_num + 1,
175 sdev->device->phys_port_cnt);
a42d985b
BVA
176 }
177 break;
178 default:
5f1141b1 179 pr_err("received unrecognized IB event %d\n", event->event);
a42d985b
BVA
180 break;
181 }
182}
183
184/**
10eac19b
BVA
185 * srpt_srq_event - SRQ event callback function
186 * @event: Description of the event that occurred.
187 * @ctx: Context pointer specified at SRQ creation time.
a42d985b
BVA
188 */
189static void srpt_srq_event(struct ib_event *event, void *ctx)
190{
d9f45ae6 191 pr_debug("SRQ event %d\n", event->event);
a42d985b
BVA
192}
193
aaf45bd8
BVA
194static const char *get_ch_state_name(enum rdma_ch_state s)
195{
196 switch (s) {
197 case CH_CONNECTING:
198 return "connecting";
199 case CH_LIVE:
200 return "live";
201 case CH_DISCONNECTING:
202 return "disconnecting";
203 case CH_DRAINING:
204 return "draining";
205 case CH_DISCONNECTED:
206 return "disconnected";
207 }
208 return "???";
209}
210
a42d985b 211/**
10eac19b
BVA
212 * srpt_qp_event - QP event callback function
213 * @event: Description of the event that occurred.
214 * @ch: SRPT RDMA channel.
a42d985b
BVA
215 */
216static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
217{
090fa24b
BVA
218 pr_debug("QP event %d on ch=%p sess_name=%s state=%d\n",
219 event->event, ch, ch->sess_name, ch->state);
a42d985b
BVA
220
221 switch (event->event) {
222 case IB_EVENT_COMM_EST:
090fa24b 223 ib_cm_notify(ch->ib_cm.cm_id, event->event);
a42d985b
BVA
224 break;
225 case IB_EVENT_QP_LAST_WQE_REACHED:
aaf45bd8
BVA
226 pr_debug("%s-%d, state %s: received Last WQE event.\n",
227 ch->sess_name, ch->qp->qp_num,
228 get_ch_state_name(ch->state));
a42d985b
BVA
229 break;
230 default:
9f5d32af 231 pr_err("received unrecognized IB QP event %d\n", event->event);
a42d985b
BVA
232 break;
233 }
234}
235
236/**
10eac19b
BVA
237 * srpt_set_ioc - initialize a IOUnitInfo structure
238 * @c_list: controller list.
a42d985b
BVA
239 * @slot: one-based slot number.
240 * @value: four-bit value.
241 *
242 * Copies the lowest four bits of value in element slot of the array of four
243 * bit elements called c_list (controller list). The index slot is one-based.
244 */
245static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
246{
247 u16 id;
248 u8 tmp;
249
250 id = (slot - 1) / 2;
251 if (slot & 0x1) {
252 tmp = c_list[id] & 0xf;
253 c_list[id] = (value << 4) | tmp;
254 } else {
255 tmp = c_list[id] & 0xf0;
256 c_list[id] = (value & 0xf) | tmp;
257 }
258}
259
260/**
10eac19b
BVA
261 * srpt_get_class_port_info - copy ClassPortInfo to a management datagram
262 * @mad: Datagram that will be sent as response to DM_ATTR_CLASS_PORT_INFO.
a42d985b
BVA
263 *
264 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
265 * Specification.
266 */
267static void srpt_get_class_port_info(struct ib_dm_mad *mad)
268{
269 struct ib_class_port_info *cif;
270
271 cif = (struct ib_class_port_info *)mad->data;
9d2aa2b4 272 memset(cif, 0, sizeof(*cif));
a42d985b
BVA
273 cif->base_version = 1;
274 cif->class_version = 1;
a42d985b 275
507f6afa 276 ib_set_cpi_resp_time(cif, 20);
a42d985b
BVA
277 mad->mad_hdr.status = 0;
278}
279
280/**
10eac19b
BVA
281 * srpt_get_iou - write IOUnitInfo to a management datagram
282 * @mad: Datagram that will be sent as response to DM_ATTR_IOU_INFO.
a42d985b
BVA
283 *
284 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
285 * Specification. See also section B.7, table B.6 in the SRP r16a document.
286 */
287static void srpt_get_iou(struct ib_dm_mad *mad)
288{
289 struct ib_dm_iou_info *ioui;
290 u8 slot;
291 int i;
292
293 ioui = (struct ib_dm_iou_info *)mad->data;
b356c1c1 294 ioui->change_id = cpu_to_be16(1);
a42d985b
BVA
295 ioui->max_controllers = 16;
296
297 /* set present for slot 1 and empty for the rest */
298 srpt_set_ioc(ioui->controller_list, 1, 1);
299 for (i = 1, slot = 2; i < 16; i++, slot++)
300 srpt_set_ioc(ioui->controller_list, slot, 0);
301
302 mad->mad_hdr.status = 0;
303}
304
305/**
10eac19b
BVA
306 * srpt_get_ioc - write IOControllerprofile to a management datagram
307 * @sport: HCA port through which the MAD has been received.
308 * @slot: Slot number specified in DM_ATTR_IOC_PROFILE query.
309 * @mad: Datagram that will be sent as response to DM_ATTR_IOC_PROFILE.
a42d985b
BVA
310 *
311 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
312 * Architecture Specification. See also section B.7, table B.7 in the SRP
313 * r16a document.
314 */
315static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
316 struct ib_dm_mad *mad)
317{
318 struct srpt_device *sdev = sport->sdev;
319 struct ib_dm_ioc_profile *iocp;
dea26209 320 int send_queue_depth;
a42d985b
BVA
321
322 iocp = (struct ib_dm_ioc_profile *)mad->data;
323
324 if (!slot || slot > 16) {
325 mad->mad_hdr.status
b356c1c1 326 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
a42d985b
BVA
327 return;
328 }
329
330 if (slot > 2) {
331 mad->mad_hdr.status
b356c1c1 332 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
a42d985b
BVA
333 return;
334 }
335
dea26209
BVA
336 if (sdev->use_srq)
337 send_queue_depth = sdev->srq_size;
338 else
ed262287 339 send_queue_depth = min(MAX_SRPT_RQ_SIZE,
dea26209
BVA
340 sdev->device->attrs.max_qp_wr);
341
9d2aa2b4 342 memset(iocp, 0, sizeof(*iocp));
a42d985b
BVA
343 strcpy(iocp->id_string, SRPT_ID_STRING);
344 iocp->guid = cpu_to_be64(srpt_service_guid);
4a061b28
OG
345 iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
346 iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
347 iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
348 iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
a42d985b 349 iocp->subsys_device_id = 0x0;
b356c1c1
VT
350 iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
351 iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
352 iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
353 iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
dea26209 354 iocp->send_queue_depth = cpu_to_be16(send_queue_depth);
a42d985b
BVA
355 iocp->rdma_read_depth = 4;
356 iocp->send_size = cpu_to_be32(srp_max_req_size);
357 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
358 1U << 24));
359 iocp->num_svc_entries = 1;
360 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
361 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
362
363 mad->mad_hdr.status = 0;
364}
365
366/**
10eac19b
BVA
367 * srpt_get_svc_entries - write ServiceEntries to a management datagram
368 * @ioc_guid: I/O controller GUID to use in reply.
369 * @slot: I/O controller number.
370 * @hi: End of the range of service entries to be specified in the reply.
371 * @lo: Start of the range of service entries to be specified in the reply..
372 * @mad: Datagram that will be sent as response to DM_ATTR_SVC_ENTRIES.
a42d985b
BVA
373 *
374 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
375 * Specification. See also section B.7, table B.8 in the SRP r16a document.
376 */
377static void srpt_get_svc_entries(u64 ioc_guid,
378 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
379{
380 struct ib_dm_svc_entries *svc_entries;
381
382 WARN_ON(!ioc_guid);
383
384 if (!slot || slot > 16) {
385 mad->mad_hdr.status
b356c1c1 386 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
a42d985b
BVA
387 return;
388 }
389
390 if (slot > 2 || lo > hi || hi > 1) {
391 mad->mad_hdr.status
b356c1c1 392 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
a42d985b
BVA
393 return;
394 }
395
396 svc_entries = (struct ib_dm_svc_entries *)mad->data;
9d2aa2b4 397 memset(svc_entries, 0, sizeof(*svc_entries));
a42d985b
BVA
398 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
399 snprintf(svc_entries->service_entries[0].name,
400 sizeof(svc_entries->service_entries[0].name),
401 "%s%016llx",
402 SRP_SERVICE_NAME_PREFIX,
403 ioc_guid);
404
405 mad->mad_hdr.status = 0;
406}
407
408/**
10eac19b
BVA
409 * srpt_mgmt_method_get - process a received management datagram
410 * @sp: HCA port through which the MAD has been received.
a42d985b
BVA
411 * @rq_mad: received MAD.
412 * @rsp_mad: response MAD.
413 */
414static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
415 struct ib_dm_mad *rsp_mad)
416{
417 u16 attr_id;
418 u32 slot;
419 u8 hi, lo;
420
421 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
422 switch (attr_id) {
423 case DM_ATTR_CLASS_PORT_INFO:
424 srpt_get_class_port_info(rsp_mad);
425 break;
426 case DM_ATTR_IOU_INFO:
427 srpt_get_iou(rsp_mad);
428 break;
429 case DM_ATTR_IOC_PROFILE:
430 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
431 srpt_get_ioc(sp, slot, rsp_mad);
432 break;
433 case DM_ATTR_SVC_ENTRIES:
434 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
435 hi = (u8) ((slot >> 8) & 0xff);
436 lo = (u8) (slot & 0xff);
437 slot = (u16) ((slot >> 16) & 0xffff);
438 srpt_get_svc_entries(srpt_service_guid,
439 slot, hi, lo, rsp_mad);
440 break;
441 default:
442 rsp_mad->mad_hdr.status =
b356c1c1 443 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
a42d985b
BVA
444 break;
445 }
446}
447
448/**
10eac19b
BVA
449 * srpt_mad_send_handler - MAD send completion callback
450 * @mad_agent: Return value of ib_register_mad_agent().
451 * @mad_wc: Work completion reporting that the MAD has been sent.
a42d985b
BVA
452 */
453static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
454 struct ib_mad_send_wc *mad_wc)
455{
36523159 456 rdma_destroy_ah(mad_wc->send_buf->ah);
a42d985b
BVA
457 ib_free_send_mad(mad_wc->send_buf);
458}
459
460/**
10eac19b
BVA
461 * srpt_mad_recv_handler - MAD reception callback function
462 * @mad_agent: Return value of ib_register_mad_agent().
463 * @send_buf: Not used.
464 * @mad_wc: Work completion reporting that a MAD has been received.
a42d985b
BVA
465 */
466static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
ca281265 467 struct ib_mad_send_buf *send_buf,
a42d985b
BVA
468 struct ib_mad_recv_wc *mad_wc)
469{
470 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
471 struct ib_ah *ah;
472 struct ib_mad_send_buf *rsp;
473 struct ib_dm_mad *dm_mad;
474
475 if (!mad_wc || !mad_wc->recv_buf.mad)
476 return;
477
478 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
479 mad_wc->recv_buf.grh, mad_agent->port_num);
480 if (IS_ERR(ah))
481 goto err;
482
483 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
484
485 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
486 mad_wc->wc->pkey_index, 0,
487 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
da2dfaa3
IW
488 GFP_KERNEL,
489 IB_MGMT_BASE_VERSION);
a42d985b
BVA
490 if (IS_ERR(rsp))
491 goto err_rsp;
492
493 rsp->ah = ah;
494
495 dm_mad = rsp->mad;
9d2aa2b4 496 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
a42d985b
BVA
497 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
498 dm_mad->mad_hdr.status = 0;
499
500 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
501 case IB_MGMT_METHOD_GET:
502 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
503 break;
504 case IB_MGMT_METHOD_SET:
505 dm_mad->mad_hdr.status =
b356c1c1 506 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
a42d985b
BVA
507 break;
508 default:
509 dm_mad->mad_hdr.status =
b356c1c1 510 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
a42d985b
BVA
511 break;
512 }
513
514 if (!ib_post_send_mad(rsp, NULL)) {
515 ib_free_recv_mad(mad_wc);
516 /* will destroy_ah & free_send_mad in send completion */
517 return;
518 }
519
520 ib_free_send_mad(rsp);
521
522err_rsp:
36523159 523 rdma_destroy_ah(ah);
a42d985b
BVA
524err:
525 ib_free_recv_mad(mad_wc);
526}
527
b14cb744
BVA
528static int srpt_format_guid(char *buf, unsigned int size, const __be64 *guid)
529{
530 const __be16 *g = (const __be16 *)guid;
531
532 return snprintf(buf, size, "%04x:%04x:%04x:%04x",
533 be16_to_cpu(g[0]), be16_to_cpu(g[1]),
534 be16_to_cpu(g[2]), be16_to_cpu(g[3]));
535}
536
a42d985b 537/**
10eac19b
BVA
538 * srpt_refresh_port - configure a HCA port
539 * @sport: SRPT HCA port.
a42d985b
BVA
540 *
541 * Enable InfiniBand management datagram processing, update the cached sm_lid,
542 * lid and gid values, and register a callback function for processing MADs
543 * on the specified port.
544 *
545 * Note: It is safe to call this function more than once for the same port.
546 */
547static int srpt_refresh_port(struct srpt_port *sport)
548{
549 struct ib_mad_reg_req reg_req;
550 struct ib_port_modify port_modify;
551 struct ib_port_attr port_attr;
552 int ret;
553
9d2aa2b4 554 memset(&port_modify, 0, sizeof(port_modify));
a42d985b
BVA
555 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
556 port_modify.clr_port_cap_mask = 0;
557
558 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
559 if (ret)
560 goto err_mod_port;
561
562 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
563 if (ret)
564 goto err_query_port;
565
566 sport->sm_lid = port_attr.sm_lid;
567 sport->lid = port_attr.lid;
568
55ee3ab2
MB
569 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid,
570 NULL);
a42d985b
BVA
571 if (ret)
572 goto err_query_port;
573
2bce1a6d 574 sport->port_guid_wwn.priv = sport;
b14cb744
BVA
575 srpt_format_guid(sport->port_guid, sizeof(sport->port_guid),
576 &sport->gid.global.interface_id);
2bce1a6d
BVA
577 sport->port_gid_wwn.priv = sport;
578 snprintf(sport->port_gid, sizeof(sport->port_gid),
579 "0x%016llx%016llx",
580 be64_to_cpu(sport->gid.global.subnet_prefix),
581 be64_to_cpu(sport->gid.global.interface_id));
716b076b 582
a42d985b 583 if (!sport->mad_agent) {
9d2aa2b4 584 memset(&reg_req, 0, sizeof(reg_req));
a42d985b
BVA
585 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
586 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
587 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
588 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
589
590 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
591 sport->port,
592 IB_QPT_GSI,
593 &reg_req, 0,
594 srpt_mad_send_handler,
595 srpt_mad_recv_handler,
0f29b46d 596 sport, 0);
a42d985b
BVA
597 if (IS_ERR(sport->mad_agent)) {
598 ret = PTR_ERR(sport->mad_agent);
599 sport->mad_agent = NULL;
600 goto err_query_port;
601 }
602 }
603
604 return 0;
605
606err_query_port:
607
608 port_modify.set_port_cap_mask = 0;
609 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
610 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
611
612err_mod_port:
613
614 return ret;
615}
616
617/**
10eac19b
BVA
618 * srpt_unregister_mad_agent - unregister MAD callback functions
619 * @sdev: SRPT HCA pointer.
a42d985b
BVA
620 *
621 * Note: It is safe to call this function more than once for the same device.
622 */
623static void srpt_unregister_mad_agent(struct srpt_device *sdev)
624{
625 struct ib_port_modify port_modify = {
626 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
627 };
628 struct srpt_port *sport;
629 int i;
630
631 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
632 sport = &sdev->port[i - 1];
633 WARN_ON(sport->port != i);
634 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
9f5d32af 635 pr_err("disabling MAD processing failed.\n");
a42d985b
BVA
636 if (sport->mad_agent) {
637 ib_unregister_mad_agent(sport->mad_agent);
638 sport->mad_agent = NULL;
639 }
640 }
641}
642
643/**
10eac19b
BVA
644 * srpt_alloc_ioctx - allocate a SRPT I/O context structure
645 * @sdev: SRPT HCA pointer.
646 * @ioctx_size: I/O context size.
647 * @dma_size: Size of I/O context DMA buffer.
648 * @dir: DMA data direction.
a42d985b
BVA
649 */
650static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
651 int ioctx_size, int dma_size,
652 enum dma_data_direction dir)
653{
654 struct srpt_ioctx *ioctx;
655
656 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
657 if (!ioctx)
658 goto err;
659
660 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
661 if (!ioctx->buf)
662 goto err_free_ioctx;
663
664 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
665 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
666 goto err_free_buf;
667
668 return ioctx;
669
670err_free_buf:
671 kfree(ioctx->buf);
672err_free_ioctx:
673 kfree(ioctx);
674err:
675 return NULL;
676}
677
678/**
10eac19b
BVA
679 * srpt_free_ioctx - free a SRPT I/O context structure
680 * @sdev: SRPT HCA pointer.
681 * @ioctx: I/O context pointer.
682 * @dma_size: Size of I/O context DMA buffer.
683 * @dir: DMA data direction.
a42d985b
BVA
684 */
685static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
686 int dma_size, enum dma_data_direction dir)
687{
688 if (!ioctx)
689 return;
690
691 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
692 kfree(ioctx->buf);
693 kfree(ioctx);
694}
695
696/**
10eac19b 697 * srpt_alloc_ioctx_ring - allocate a ring of SRPT I/O context structures
a42d985b
BVA
698 * @sdev: Device to allocate the I/O context ring for.
699 * @ring_size: Number of elements in the I/O context ring.
700 * @ioctx_size: I/O context size.
701 * @dma_size: DMA buffer size.
702 * @dir: DMA data direction.
703 */
704static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
705 int ring_size, int ioctx_size,
706 int dma_size, enum dma_data_direction dir)
707{
708 struct srpt_ioctx **ring;
709 int i;
710
711 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
712 && ioctx_size != sizeof(struct srpt_send_ioctx));
713
714 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
715 if (!ring)
716 goto out;
717 for (i = 0; i < ring_size; ++i) {
718 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
719 if (!ring[i])
720 goto err;
721 ring[i]->index = i;
722 }
723 goto out;
724
725err:
726 while (--i >= 0)
727 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
728 kfree(ring);
715252d4 729 ring = NULL;
a42d985b
BVA
730out:
731 return ring;
732}
733
734/**
10eac19b
BVA
735 * srpt_free_ioctx_ring - free the ring of SRPT I/O context structures
736 * @ioctx_ring: I/O context ring to be freed.
737 * @sdev: SRPT HCA pointer.
738 * @ring_size: Number of ring elements.
739 * @dma_size: Size of I/O context DMA buffer.
740 * @dir: DMA data direction.
a42d985b
BVA
741 */
742static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
743 struct srpt_device *sdev, int ring_size,
744 int dma_size, enum dma_data_direction dir)
745{
746 int i;
747
dea26209
BVA
748 if (!ioctx_ring)
749 return;
750
a42d985b
BVA
751 for (i = 0; i < ring_size; ++i)
752 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
753 kfree(ioctx_ring);
754}
755
a42d985b 756/**
10eac19b
BVA
757 * srpt_set_cmd_state - set the state of a SCSI command
758 * @ioctx: Send I/O context.
759 * @new: New I/O context state.
a42d985b
BVA
760 *
761 * Does not modify the state of aborted commands. Returns the previous command
762 * state.
763 */
764static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
765 enum srpt_command_state new)
766{
767 enum srpt_command_state previous;
a42d985b 768
a42d985b
BVA
769 previous = ioctx->state;
770 if (previous != SRPT_STATE_DONE)
771 ioctx->state = new;
a42d985b
BVA
772
773 return previous;
774}
775
776/**
10eac19b
BVA
777 * srpt_test_and_set_cmd_state - test and set the state of a command
778 * @ioctx: Send I/O context.
779 * @old: Current I/O context state.
780 * @new: New I/O context state.
a42d985b
BVA
781 *
782 * Returns true if and only if the previous command state was equal to 'old'.
783 */
784static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
785 enum srpt_command_state old,
786 enum srpt_command_state new)
787{
788 enum srpt_command_state previous;
a42d985b
BVA
789
790 WARN_ON(!ioctx);
791 WARN_ON(old == SRPT_STATE_DONE);
792 WARN_ON(new == SRPT_STATE_NEW);
793
a42d985b
BVA
794 previous = ioctx->state;
795 if (previous == old)
796 ioctx->state = new;
2d67017c 797
a42d985b
BVA
798 return previous == old;
799}
800
801/**
10eac19b
BVA
802 * srpt_post_recv - post an IB receive request
803 * @sdev: SRPT HCA pointer.
804 * @ch: SRPT RDMA channel.
805 * @ioctx: Receive I/O context pointer.
a42d985b 806 */
dea26209 807static int srpt_post_recv(struct srpt_device *sdev, struct srpt_rdma_ch *ch,
a42d985b
BVA
808 struct srpt_recv_ioctx *ioctx)
809{
810 struct ib_sge list;
811 struct ib_recv_wr wr, *bad_wr;
812
813 BUG_ON(!sdev);
a42d985b
BVA
814 list.addr = ioctx->ioctx.dma;
815 list.length = srp_max_req_size;
74333f12 816 list.lkey = sdev->lkey;
a42d985b 817
59fae4de
CH
818 ioctx->ioctx.cqe.done = srpt_recv_done;
819 wr.wr_cqe = &ioctx->ioctx.cqe;
a42d985b
BVA
820 wr.next = NULL;
821 wr.sg_list = &list;
822 wr.num_sge = 1;
823
dea26209
BVA
824 if (sdev->use_srq)
825 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
826 else
827 return ib_post_recv(ch->qp, &wr, &bad_wr);
a42d985b
BVA
828}
829
aaf45bd8 830/**
10eac19b
BVA
831 * srpt_zerolength_write - perform a zero-length RDMA write
832 * @ch: SRPT RDMA channel.
aaf45bd8
BVA
833 *
834 * A quote from the InfiniBand specification: C9-88: For an HCA responder
835 * using Reliable Connection service, for each zero-length RDMA READ or WRITE
836 * request, the R_Key shall not be validated, even if the request includes
837 * Immediate data.
838 */
839static int srpt_zerolength_write(struct srpt_rdma_ch *ch)
840{
841 struct ib_send_wr wr, *bad_wr;
842
63d370a6
BVA
843 pr_debug("%s-%d: queued zerolength write\n", ch->sess_name,
844 ch->qp->qp_num);
845
aaf45bd8
BVA
846 memset(&wr, 0, sizeof(wr));
847 wr.opcode = IB_WR_RDMA_WRITE;
848 wr.wr_cqe = &ch->zw_cqe;
849 wr.send_flags = IB_SEND_SIGNALED;
850 return ib_post_send(ch->qp, &wr, &bad_wr);
851}
852
853static void srpt_zerolength_write_done(struct ib_cq *cq, struct ib_wc *wc)
854{
855 struct srpt_rdma_ch *ch = cq->cq_context;
856
63d370a6
BVA
857 pr_debug("%s-%d wc->status %d\n", ch->sess_name, ch->qp->qp_num,
858 wc->status);
859
387add46
BVA
860 if (wc->status == IB_WC_SUCCESS) {
861 srpt_process_wait_list(ch);
862 } else {
863 if (srpt_set_ch_state(ch, CH_DISCONNECTED))
864 schedule_work(&ch->release_work);
865 else
ea51d2e1
BVA
866 pr_debug("%s-%d: already disconnected.\n",
867 ch->sess_name, ch->qp->qp_num);
387add46 868 }
aaf45bd8
BVA
869}
870
b99f8e4d
CH
871static int srpt_alloc_rw_ctxs(struct srpt_send_ioctx *ioctx,
872 struct srp_direct_buf *db, int nbufs, struct scatterlist **sg,
873 unsigned *sg_cnt)
874{
875 enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
876 struct srpt_rdma_ch *ch = ioctx->ch;
877 struct scatterlist *prev = NULL;
878 unsigned prev_nents;
879 int ret, i;
880
881 if (nbufs == 1) {
882 ioctx->rw_ctxs = &ioctx->s_rw_ctx;
883 } else {
884 ioctx->rw_ctxs = kmalloc_array(nbufs, sizeof(*ioctx->rw_ctxs),
885 GFP_KERNEL);
886 if (!ioctx->rw_ctxs)
887 return -ENOMEM;
888 }
889
890 for (i = ioctx->n_rw_ctx; i < nbufs; i++, db++) {
891 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
892 u64 remote_addr = be64_to_cpu(db->va);
893 u32 size = be32_to_cpu(db->len);
894 u32 rkey = be32_to_cpu(db->key);
895
896 ret = target_alloc_sgl(&ctx->sg, &ctx->nents, size, false,
897 i < nbufs - 1);
898 if (ret)
899 goto unwind;
900
901 ret = rdma_rw_ctx_init(&ctx->rw, ch->qp, ch->sport->port,
902 ctx->sg, ctx->nents, 0, remote_addr, rkey, dir);
903 if (ret < 0) {
904 target_free_sgl(ctx->sg, ctx->nents);
905 goto unwind;
906 }
907
908 ioctx->n_rdma += ret;
909 ioctx->n_rw_ctx++;
910
911 if (prev) {
912 sg_unmark_end(&prev[prev_nents - 1]);
913 sg_chain(prev, prev_nents + 1, ctx->sg);
914 } else {
915 *sg = ctx->sg;
916 }
917
918 prev = ctx->sg;
919 prev_nents = ctx->nents;
920
921 *sg_cnt += ctx->nents;
922 }
923
924 return 0;
925
926unwind:
927 while (--i >= 0) {
928 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
929
930 rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
931 ctx->sg, ctx->nents, dir);
932 target_free_sgl(ctx->sg, ctx->nents);
933 }
934 if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
935 kfree(ioctx->rw_ctxs);
936 return ret;
937}
938
939static void srpt_free_rw_ctxs(struct srpt_rdma_ch *ch,
940 struct srpt_send_ioctx *ioctx)
941{
942 enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
943 int i;
944
945 for (i = 0; i < ioctx->n_rw_ctx; i++) {
946 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
947
948 rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
949 ctx->sg, ctx->nents, dir);
950 target_free_sgl(ctx->sg, ctx->nents);
951 }
952
953 if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
954 kfree(ioctx->rw_ctxs);
955}
956
957static inline void *srpt_get_desc_buf(struct srp_cmd *srp_cmd)
958{
959 /*
960 * The pointer computations below will only be compiled correctly
961 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
962 * whether srp_cmd::add_data has been declared as a byte pointer.
963 */
964 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0) &&
965 !__same_type(srp_cmd->add_data[0], (u8)0));
966
967 /*
968 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
969 * CDB LENGTH' field are reserved and the size in bytes of this field
970 * is four times the value specified in bits 3..7. Hence the "& ~3".
971 */
972 return srp_cmd->add_data + (srp_cmd->add_cdb_len & ~3);
973}
974
a42d985b 975/**
10eac19b 976 * srpt_get_desc_tbl - parse the data descriptors of a SRP_CMD request
a42d985b
BVA
977 * @ioctx: Pointer to the I/O context associated with the request.
978 * @srp_cmd: Pointer to the SRP_CMD request data.
979 * @dir: Pointer to the variable to which the transfer direction will be
980 * written.
10eac19b
BVA
981 * @sg: [out] scatterlist allocated for the parsed SRP_CMD.
982 * @sg_cnt: [out] length of @sg.
a42d985b
BVA
983 * @data_len: Pointer to the variable to which the total data length of all
984 * descriptors in the SRP_CMD request will be written.
985 *
986 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
987 *
988 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
989 * -ENOMEM when memory allocation fails and zero upon success.
990 */
991static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
b99f8e4d
CH
992 struct srp_cmd *srp_cmd, enum dma_data_direction *dir,
993 struct scatterlist **sg, unsigned *sg_cnt, u64 *data_len)
a42d985b 994{
a42d985b
BVA
995 BUG_ON(!dir);
996 BUG_ON(!data_len);
997
a42d985b
BVA
998 /*
999 * The lower four bits of the buffer format field contain the DATA-IN
1000 * buffer descriptor format, and the highest four bits contain the
1001 * DATA-OUT buffer descriptor format.
1002 */
a42d985b
BVA
1003 if (srp_cmd->buf_fmt & 0xf)
1004 /* DATA-IN: transfer data from target to initiator (read). */
1005 *dir = DMA_FROM_DEVICE;
1006 else if (srp_cmd->buf_fmt >> 4)
1007 /* DATA-OUT: transfer data from initiator to target (write). */
1008 *dir = DMA_TO_DEVICE;
b99f8e4d
CH
1009 else
1010 *dir = DMA_NONE;
1011
1012 /* initialize data_direction early as srpt_alloc_rw_ctxs needs it */
1013 ioctx->cmd.data_direction = *dir;
a42d985b 1014
a42d985b
BVA
1015 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
1016 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
b99f8e4d 1017 struct srp_direct_buf *db = srpt_get_desc_buf(srp_cmd);
a42d985b 1018
a42d985b 1019 *data_len = be32_to_cpu(db->len);
b99f8e4d 1020 return srpt_alloc_rw_ctxs(ioctx, db, 1, sg, sg_cnt);
a42d985b
BVA
1021 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
1022 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
b99f8e4d
CH
1023 struct srp_indirect_buf *idb = srpt_get_desc_buf(srp_cmd);
1024 int nbufs = be32_to_cpu(idb->table_desc.len) /
1025 sizeof(struct srp_direct_buf);
a42d985b 1026
b99f8e4d 1027 if (nbufs >
a42d985b 1028 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
9f5d32af 1029 pr_err("received unsupported SRP_CMD request"
a42d985b
BVA
1030 " type (%u out + %u in != %u / %zu)\n",
1031 srp_cmd->data_out_desc_cnt,
1032 srp_cmd->data_in_desc_cnt,
1033 be32_to_cpu(idb->table_desc.len),
b99f8e4d
CH
1034 sizeof(struct srp_direct_buf));
1035 return -EINVAL;
a42d985b
BVA
1036 }
1037
a42d985b 1038 *data_len = be32_to_cpu(idb->len);
b99f8e4d
CH
1039 return srpt_alloc_rw_ctxs(ioctx, idb->desc_list, nbufs,
1040 sg, sg_cnt);
1041 } else {
1042 *data_len = 0;
1043 return 0;
a42d985b 1044 }
a42d985b
BVA
1045}
1046
1047/**
10eac19b
BVA
1048 * srpt_init_ch_qp - initialize queue pair attributes
1049 * @ch: SRPT RDMA channel.
1050 * @qp: Queue pair pointer.
a42d985b
BVA
1051 *
1052 * Initialized the attributes of queue pair 'qp' by allowing local write,
1053 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
1054 */
1055static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1056{
1057 struct ib_qp_attr *attr;
1058 int ret;
1059
9d2aa2b4 1060 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
a42d985b
BVA
1061 if (!attr)
1062 return -ENOMEM;
1063
1064 attr->qp_state = IB_QPS_INIT;
bec40c26 1065 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE;
a42d985b 1066 attr->port_num = ch->sport->port;
c5efb621
BVA
1067
1068 ret = ib_find_cached_pkey(ch->sport->sdev->device, ch->sport->port,
1069 ch->pkey, &attr->pkey_index);
1070 if (ret < 0)
1071 pr_err("Translating pkey %#x failed (%d) - using index 0\n",
1072 ch->pkey, ret);
a42d985b
BVA
1073
1074 ret = ib_modify_qp(qp, attr,
1075 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
1076 IB_QP_PKEY_INDEX);
1077
1078 kfree(attr);
1079 return ret;
1080}
1081
1082/**
10eac19b 1083 * srpt_ch_qp_rtr - change the state of a channel to 'ready to receive' (RTR)
a42d985b
BVA
1084 * @ch: channel of the queue pair.
1085 * @qp: queue pair to change the state of.
1086 *
1087 * Returns zero upon success and a negative value upon failure.
1088 *
1089 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1090 * If this structure ever becomes larger, it might be necessary to allocate
1091 * it dynamically instead of on the stack.
1092 */
1093static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1094{
1095 struct ib_qp_attr qp_attr;
1096 int attr_mask;
1097 int ret;
1098
1099 qp_attr.qp_state = IB_QPS_RTR;
090fa24b 1100 ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask);
a42d985b
BVA
1101 if (ret)
1102 goto out;
1103
1104 qp_attr.max_dest_rd_atomic = 4;
1105
1106 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1107
1108out:
1109 return ret;
1110}
1111
1112/**
10eac19b 1113 * srpt_ch_qp_rts - change the state of a channel to 'ready to send' (RTS)
a42d985b
BVA
1114 * @ch: channel of the queue pair.
1115 * @qp: queue pair to change the state of.
1116 *
1117 * Returns zero upon success and a negative value upon failure.
1118 *
1119 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1120 * If this structure ever becomes larger, it might be necessary to allocate
1121 * it dynamically instead of on the stack.
1122 */
1123static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1124{
1125 struct ib_qp_attr qp_attr;
1126 int attr_mask;
1127 int ret;
1128
1129 qp_attr.qp_state = IB_QPS_RTS;
090fa24b 1130 ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask);
a42d985b
BVA
1131 if (ret)
1132 goto out;
1133
1134 qp_attr.max_rd_atomic = 4;
1135
1136 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1137
1138out:
1139 return ret;
1140}
1141
1142/**
10eac19b
BVA
1143 * srpt_ch_qp_err - set the channel queue pair state to 'error'
1144 * @ch: SRPT RDMA channel.
a42d985b
BVA
1145 */
1146static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1147{
1148 struct ib_qp_attr qp_attr;
1149
1150 qp_attr.qp_state = IB_QPS_ERR;
1151 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1152}
1153
a42d985b 1154/**
10eac19b
BVA
1155 * srpt_get_send_ioctx - obtain an I/O context for sending to the initiator
1156 * @ch: SRPT RDMA channel.
a42d985b
BVA
1157 */
1158static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1159{
1160 struct srpt_send_ioctx *ioctx;
3c968887 1161 unsigned long flags;
a42d985b
BVA
1162
1163 BUG_ON(!ch);
1164
3c968887
BVA
1165 ioctx = NULL;
1166 spin_lock_irqsave(&ch->spinlock, flags);
1167 if (!list_empty(&ch->free_list)) {
1168 ioctx = list_first_entry(&ch->free_list,
1169 struct srpt_send_ioctx, free_list);
1170 list_del(&ioctx->free_list);
a42d985b 1171 }
3c968887
BVA
1172 spin_unlock_irqrestore(&ch->spinlock, flags);
1173
1174 if (!ioctx)
1175 return ioctx;
1176
1177 BUG_ON(ioctx->ch != ch);
a42d985b 1178 ioctx->state = SRPT_STATE_NEW;
3c968887 1179 ioctx->n_rdma = 0;
b99f8e4d 1180 ioctx->n_rw_ctx = 0;
3c968887
BVA
1181 ioctx->queue_status_only = false;
1182 /*
1183 * transport_init_se_cmd() does not initialize all fields, so do it
1184 * here.
1185 */
1186 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1187 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
a42d985b
BVA
1188
1189 return ioctx;
1190}
1191
a42d985b 1192/**
10eac19b 1193 * srpt_abort_cmd - abort a SCSI command
a42d985b 1194 * @ioctx: I/O context associated with the SCSI command.
a42d985b
BVA
1195 */
1196static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1197{
1198 enum srpt_command_state state;
a42d985b
BVA
1199
1200 BUG_ON(!ioctx);
1201
1202 /*
1203 * If the command is in a state where the target core is waiting for
49f40163 1204 * the ib_srpt driver, change the state to the next state.
a42d985b
BVA
1205 */
1206
a42d985b
BVA
1207 state = ioctx->state;
1208 switch (state) {
1209 case SRPT_STATE_NEED_DATA:
1210 ioctx->state = SRPT_STATE_DATA_IN;
1211 break;
a42d985b
BVA
1212 case SRPT_STATE_CMD_RSP_SENT:
1213 case SRPT_STATE_MGMT_RSP_SENT:
1214 ioctx->state = SRPT_STATE_DONE;
1215 break;
1216 default:
49f40163
BVA
1217 WARN_ONCE(true, "%s: unexpected I/O context state %d\n",
1218 __func__, state);
a42d985b
BVA
1219 break;
1220 }
a42d985b 1221
13fdd445
BVA
1222 pr_debug("Aborting cmd with state %d -> %d and tag %lld\n", state,
1223 ioctx->state, ioctx->cmd.tag);
a42d985b
BVA
1224
1225 switch (state) {
1226 case SRPT_STATE_NEW:
1227 case SRPT_STATE_DATA_IN:
1228 case SRPT_STATE_MGMT:
49f40163 1229 case SRPT_STATE_DONE:
a42d985b
BVA
1230 /*
1231 * Do nothing - defer abort processing until
1232 * srpt_queue_response() is invoked.
1233 */
a42d985b
BVA
1234 break;
1235 case SRPT_STATE_NEED_DATA:
49f40163
BVA
1236 pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag);
1237 transport_generic_request_failure(&ioctx->cmd,
1238 TCM_CHECK_CONDITION_ABORT_CMD);
a42d985b
BVA
1239 break;
1240 case SRPT_STATE_CMD_RSP_SENT:
1241 /*
1242 * SRP_RSP sending failed or the SRP_RSP send completion has
1243 * not been received in time.
1244 */
49f40163 1245 transport_generic_free_cmd(&ioctx->cmd, 0);
a42d985b
BVA
1246 break;
1247 case SRPT_STATE_MGMT_RSP_SENT:
49f40163 1248 transport_generic_free_cmd(&ioctx->cmd, 0);
a42d985b
BVA
1249 break;
1250 default:
532ec6f1 1251 WARN(1, "Unexpected command state (%d)", state);
a42d985b
BVA
1252 break;
1253 }
1254
a42d985b
BVA
1255 return state;
1256}
1257
1258/**
10eac19b
BVA
1259 * srpt_rdma_read_done - RDMA read completion callback
1260 * @cq: Completion queue.
1261 * @wc: Work completion.
1262 *
e672a47f
CH
1263 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1264 * the data that has been transferred via IB RDMA had to be postponed until the
142ad5db 1265 * check_stop_free() callback. None of this is necessary anymore and needs to
e672a47f 1266 * be cleaned up.
a42d985b 1267 */
59fae4de 1268static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
a42d985b 1269{
59fae4de
CH
1270 struct srpt_rdma_ch *ch = cq->cq_context;
1271 struct srpt_send_ioctx *ioctx =
19f57298 1272 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
59fae4de 1273
a42d985b
BVA
1274 WARN_ON(ioctx->n_rdma <= 0);
1275 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
b99f8e4d 1276 ioctx->n_rdma = 0;
a42d985b 1277
59fae4de
CH
1278 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1279 pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
1280 ioctx, wc->status);
1281 srpt_abort_cmd(ioctx);
1282 return;
a42d985b 1283 }
59fae4de
CH
1284
1285 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1286 SRPT_STATE_DATA_IN))
1287 target_execute_cmd(&ioctx->cmd);
1288 else
1289 pr_err("%s[%d]: wrong state = %d\n", __func__,
dd3bec86 1290 __LINE__, ioctx->state);
a42d985b
BVA
1291}
1292
a42d985b 1293/**
10eac19b 1294 * srpt_build_cmd_rsp - build a SRP_RSP response
a42d985b
BVA
1295 * @ch: RDMA channel through which the request has been received.
1296 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1297 * be built in the buffer ioctx->buf points at and hence this function will
1298 * overwrite the request data.
1299 * @tag: tag of the request for which this response is being generated.
1300 * @status: value for the STATUS field of the SRP_RSP information unit.
1301 *
1302 * Returns the size in bytes of the SRP_RSP response.
1303 *
1304 * An SRP_RSP response contains a SCSI status or service response. See also
1305 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1306 * response. See also SPC-2 for more information about sense data.
1307 */
1308static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1309 struct srpt_send_ioctx *ioctx, u64 tag,
1310 int status)
1311{
1312 struct srp_rsp *srp_rsp;
1313 const u8 *sense_data;
1314 int sense_data_len, max_sense_len;
1315
1316 /*
1317 * The lowest bit of all SAM-3 status codes is zero (see also
1318 * paragraph 5.3 in SAM-3).
1319 */
1320 WARN_ON(status & 1);
1321
1322 srp_rsp = ioctx->ioctx.buf;
1323 BUG_ON(!srp_rsp);
1324
1325 sense_data = ioctx->sense_data;
1326 sense_data_len = ioctx->cmd.scsi_sense_length;
1327 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1328
9d2aa2b4 1329 memset(srp_rsp, 0, sizeof(*srp_rsp));
a42d985b
BVA
1330 srp_rsp->opcode = SRP_RSP;
1331 srp_rsp->req_lim_delta =
b356c1c1 1332 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
a42d985b
BVA
1333 srp_rsp->tag = tag;
1334 srp_rsp->status = status;
1335
1336 if (sense_data_len) {
1337 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1338 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1339 if (sense_data_len > max_sense_len) {
9f5d32af
DL
1340 pr_warn("truncated sense data from %d to %d"
1341 " bytes\n", sense_data_len, max_sense_len);
a42d985b
BVA
1342 sense_data_len = max_sense_len;
1343 }
1344
1345 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1346 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1347 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1348 }
1349
1350 return sizeof(*srp_rsp) + sense_data_len;
1351}
1352
1353/**
10eac19b 1354 * srpt_build_tskmgmt_rsp - build a task management response
a42d985b
BVA
1355 * @ch: RDMA channel through which the request has been received.
1356 * @ioctx: I/O context in which the SRP_RSP response will be built.
1357 * @rsp_code: RSP_CODE that will be stored in the response.
1358 * @tag: Tag of the request for which this response is being generated.
1359 *
1360 * Returns the size in bytes of the SRP_RSP response.
1361 *
1362 * An SRP_RSP response contains a SCSI status or service response. See also
1363 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1364 * response.
1365 */
1366static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1367 struct srpt_send_ioctx *ioctx,
1368 u8 rsp_code, u64 tag)
1369{
1370 struct srp_rsp *srp_rsp;
1371 int resp_data_len;
1372 int resp_len;
1373
c807f643 1374 resp_data_len = 4;
a42d985b
BVA
1375 resp_len = sizeof(*srp_rsp) + resp_data_len;
1376
1377 srp_rsp = ioctx->ioctx.buf;
1378 BUG_ON(!srp_rsp);
9d2aa2b4 1379 memset(srp_rsp, 0, sizeof(*srp_rsp));
a42d985b
BVA
1380
1381 srp_rsp->opcode = SRP_RSP;
b356c1c1
VT
1382 srp_rsp->req_lim_delta =
1383 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
a42d985b
BVA
1384 srp_rsp->tag = tag;
1385
c807f643
JW
1386 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1387 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1388 srp_rsp->data[3] = rsp_code;
a42d985b
BVA
1389
1390 return resp_len;
1391}
1392
a42d985b
BVA
1393static int srpt_check_stop_free(struct se_cmd *cmd)
1394{
9474b043
NB
1395 struct srpt_send_ioctx *ioctx = container_of(cmd,
1396 struct srpt_send_ioctx, cmd);
a42d985b 1397
afc16604 1398 return target_put_sess_cmd(&ioctx->cmd);
a42d985b
BVA
1399}
1400
1401/**
10eac19b
BVA
1402 * srpt_handle_cmd - process a SRP_CMD information unit
1403 * @ch: SRPT RDMA channel.
1404 * @recv_ioctx: Receive I/O context.
1405 * @send_ioctx: Send I/O context.
a42d985b 1406 */
2c7f37ff
BVA
1407static void srpt_handle_cmd(struct srpt_rdma_ch *ch,
1408 struct srpt_recv_ioctx *recv_ioctx,
1409 struct srpt_send_ioctx *send_ioctx)
a42d985b
BVA
1410{
1411 struct se_cmd *cmd;
1412 struct srp_cmd *srp_cmd;
b99f8e4d
CH
1413 struct scatterlist *sg = NULL;
1414 unsigned sg_cnt = 0;
a42d985b
BVA
1415 u64 data_len;
1416 enum dma_data_direction dir;
9474b043 1417 int rc;
a42d985b
BVA
1418
1419 BUG_ON(!send_ioctx);
1420
1421 srp_cmd = recv_ioctx->ioctx.buf;
a42d985b 1422 cmd = &send_ioctx->cmd;
649ee054 1423 cmd->tag = srp_cmd->tag;
a42d985b
BVA
1424
1425 switch (srp_cmd->task_attr) {
1426 case SRP_CMD_SIMPLE_Q:
68d81f40 1427 cmd->sam_task_attr = TCM_SIMPLE_TAG;
a42d985b
BVA
1428 break;
1429 case SRP_CMD_ORDERED_Q:
1430 default:
68d81f40 1431 cmd->sam_task_attr = TCM_ORDERED_TAG;
a42d985b
BVA
1432 break;
1433 case SRP_CMD_HEAD_OF_Q:
68d81f40 1434 cmd->sam_task_attr = TCM_HEAD_TAG;
a42d985b
BVA
1435 break;
1436 case SRP_CMD_ACA:
68d81f40 1437 cmd->sam_task_attr = TCM_ACA_TAG;
a42d985b
BVA
1438 break;
1439 }
1440
b99f8e4d
CH
1441 rc = srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &sg, &sg_cnt,
1442 &data_len);
1443 if (rc) {
1444 if (rc != -EAGAIN) {
1445 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
1446 srp_cmd->tag);
1447 }
2c7f37ff 1448 goto release_ioctx;
a42d985b
BVA
1449 }
1450
b99f8e4d 1451 rc = target_submit_cmd_map_sgls(cmd, ch->sess, srp_cmd->cdb,
e1dd413c
BVA
1452 &send_ioctx->sense_data[0],
1453 scsilun_to_int(&srp_cmd->lun), data_len,
b99f8e4d
CH
1454 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF,
1455 sg, sg_cnt, NULL, 0, NULL, 0);
9474b043 1456 if (rc != 0) {
2c7f37ff
BVA
1457 pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc,
1458 srp_cmd->tag);
1459 goto release_ioctx;
187e70a5 1460 }
2c7f37ff 1461 return;
a42d985b 1462
2c7f37ff
BVA
1463release_ioctx:
1464 send_ioctx->state = SRPT_STATE_DONE;
1465 srpt_release_cmd(cmd);
a42d985b
BVA
1466}
1467
a42d985b
BVA
1468static int srp_tmr_to_tcm(int fn)
1469{
1470 switch (fn) {
1471 case SRP_TSK_ABORT_TASK:
1472 return TMR_ABORT_TASK;
1473 case SRP_TSK_ABORT_TASK_SET:
1474 return TMR_ABORT_TASK_SET;
1475 case SRP_TSK_CLEAR_TASK_SET:
1476 return TMR_CLEAR_TASK_SET;
1477 case SRP_TSK_LUN_RESET:
1478 return TMR_LUN_RESET;
1479 case SRP_TSK_CLEAR_ACA:
1480 return TMR_CLEAR_ACA;
1481 default:
1482 return -1;
1483 }
1484}
1485
1486/**
10eac19b
BVA
1487 * srpt_handle_tsk_mgmt - process a SRP_TSK_MGMT information unit
1488 * @ch: SRPT RDMA channel.
1489 * @recv_ioctx: Receive I/O context.
1490 * @send_ioctx: Send I/O context.
a42d985b
BVA
1491 *
1492 * Returns 0 if and only if the request will be processed by the target core.
1493 *
1494 * For more information about SRP_TSK_MGMT information units, see also section
1495 * 6.7 in the SRP r16a document.
1496 */
1497static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1498 struct srpt_recv_ioctx *recv_ioctx,
1499 struct srpt_send_ioctx *send_ioctx)
1500{
1501 struct srp_tsk_mgmt *srp_tsk;
1502 struct se_cmd *cmd;
3e4f5748 1503 struct se_session *sess = ch->sess;
a42d985b 1504 int tcm_tmr;
3e4f5748 1505 int rc;
a42d985b
BVA
1506
1507 BUG_ON(!send_ioctx);
1508
1509 srp_tsk = recv_ioctx->ioctx.buf;
1510 cmd = &send_ioctx->cmd;
1511
090fa24b
BVA
1512 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld ch %p sess %p\n",
1513 srp_tsk->tsk_mgmt_func, srp_tsk->task_tag, srp_tsk->tag, ch,
1514 ch->sess);
a42d985b
BVA
1515
1516 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
649ee054 1517 send_ioctx->cmd.tag = srp_tsk->tag;
a42d985b 1518 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
e1dd413c
BVA
1519 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL,
1520 scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr,
1521 GFP_KERNEL, srp_tsk->task_tag,
1522 TARGET_SCF_ACK_KREF);
3e4f5748
NB
1523 if (rc != 0) {
1524 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
de103c93 1525 goto fail;
a42d985b 1526 }
de103c93
CH
1527 return;
1528fail:
de103c93 1529 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
a42d985b
BVA
1530}
1531
1532/**
10eac19b 1533 * srpt_handle_new_iu - process a newly received information unit
a42d985b 1534 * @ch: RDMA channel through which the information unit has been received.
10eac19b 1535 * @recv_ioctx: Receive I/O context associated with the information unit.
a42d985b 1536 */
fcf58936
BVA
1537static bool
1538srpt_handle_new_iu(struct srpt_rdma_ch *ch, struct srpt_recv_ioctx *recv_ioctx)
a42d985b 1539{
fcf58936 1540 struct srpt_send_ioctx *send_ioctx = NULL;
a42d985b 1541 struct srp_cmd *srp_cmd;
fcf58936
BVA
1542 bool res = false;
1543 u8 opcode;
a42d985b
BVA
1544
1545 BUG_ON(!ch);
1546 BUG_ON(!recv_ioctx);
1547
fcf58936
BVA
1548 if (unlikely(ch->state == CH_CONNECTING))
1549 goto push;
1550
a42d985b
BVA
1551 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1552 recv_ioctx->ioctx.dma, srp_max_req_size,
1553 DMA_FROM_DEVICE);
1554
a42d985b 1555 srp_cmd = recv_ioctx->ioctx.buf;
fcf58936
BVA
1556 opcode = srp_cmd->opcode;
1557 if (opcode == SRP_CMD || opcode == SRP_TSK_MGMT) {
1558 send_ioctx = srpt_get_send_ioctx(ch);
b99f8e4d 1559 if (unlikely(!send_ioctx))
fcf58936 1560 goto push;
a42d985b
BVA
1561 }
1562
fcf58936
BVA
1563 if (!list_empty(&recv_ioctx->wait_list)) {
1564 WARN_ON_ONCE(!ch->processing_wait_list);
1565 list_del_init(&recv_ioctx->wait_list);
1566 }
1567
1568 switch (opcode) {
a42d985b
BVA
1569 case SRP_CMD:
1570 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1571 break;
1572 case SRP_TSK_MGMT:
1573 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1574 break;
1575 case SRP_I_LOGOUT:
9f5d32af 1576 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
a42d985b
BVA
1577 break;
1578 case SRP_CRED_RSP:
1579 pr_debug("received SRP_CRED_RSP\n");
1580 break;
1581 case SRP_AER_RSP:
1582 pr_debug("received SRP_AER_RSP\n");
1583 break;
1584 case SRP_RSP:
9f5d32af 1585 pr_err("Received SRP_RSP\n");
a42d985b
BVA
1586 break;
1587 default:
fcf58936 1588 pr_err("received IU with unknown opcode 0x%x\n", opcode);
a42d985b
BVA
1589 break;
1590 }
1591
dea26209 1592 srpt_post_recv(ch->sport->sdev, ch, recv_ioctx);
fcf58936 1593 res = true;
b99f8e4d 1594
fcf58936
BVA
1595out:
1596 return res;
1597
1598push:
1599 if (list_empty(&recv_ioctx->wait_list)) {
1600 WARN_ON_ONCE(ch->processing_wait_list);
1601 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1602 }
1603 goto out;
a42d985b
BVA
1604}
1605
59fae4de 1606static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
a42d985b 1607{
59fae4de
CH
1608 struct srpt_rdma_ch *ch = cq->cq_context;
1609 struct srpt_recv_ioctx *ioctx =
1610 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
a42d985b 1611
a42d985b
BVA
1612 if (wc->status == IB_WC_SUCCESS) {
1613 int req_lim;
1614
1615 req_lim = atomic_dec_return(&ch->req_lim);
1616 if (unlikely(req_lim < 0))
9f5d32af 1617 pr_err("req_lim = %d < 0\n", req_lim);
fcf58936 1618 srpt_handle_new_iu(ch, ioctx);
a42d985b 1619 } else {
3fa7f0e9
BVA
1620 pr_info_ratelimited("receiving failed for ioctx %p with status %d\n",
1621 ioctx, wc->status);
a42d985b
BVA
1622 }
1623}
1624
539b3248
BVA
1625/*
1626 * This function must be called from the context in which RDMA completions are
1627 * processed because it accesses the wait list without protection against
1628 * access from other threads.
1629 */
1630static void srpt_process_wait_list(struct srpt_rdma_ch *ch)
1631{
fcf58936 1632 struct srpt_recv_ioctx *recv_ioctx, *tmp;
539b3248 1633
fcf58936 1634 WARN_ON_ONCE(ch->state == CH_CONNECTING);
539b3248 1635
fcf58936
BVA
1636 if (list_empty(&ch->cmd_wait_list))
1637 return;
1638
1639 WARN_ON_ONCE(ch->processing_wait_list);
1640 ch->processing_wait_list = true;
1641 list_for_each_entry_safe(recv_ioctx, tmp, &ch->cmd_wait_list,
1642 wait_list) {
1643 if (!srpt_handle_new_iu(ch, recv_ioctx))
1644 break;
539b3248 1645 }
fcf58936 1646 ch->processing_wait_list = false;
539b3248
BVA
1647}
1648
a42d985b 1649/**
10eac19b
BVA
1650 * srpt_send_done - send completion callback
1651 * @cq: Completion queue.
1652 * @wc: Work completion.
1653 *
a42d985b
BVA
1654 * Note: Although this has not yet been observed during tests, at least in
1655 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1656 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1657 * value in each response is set to one, and it is possible that this response
1658 * makes the initiator send a new request before the send completion for that
1659 * response has been processed. This could e.g. happen if the call to
1660 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1661 * if IB retransmission causes generation of the send completion to be
1662 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1663 * are queued on cmd_wait_list. The code below processes these delayed
1664 * requests one at a time.
1665 */
59fae4de 1666static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
a42d985b 1667{
59fae4de
CH
1668 struct srpt_rdma_ch *ch = cq->cq_context;
1669 struct srpt_send_ioctx *ioctx =
1670 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1671 enum srpt_command_state state;
a42d985b 1672
59fae4de
CH
1673 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1674
1675 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1676 state != SRPT_STATE_MGMT_RSP_SENT);
1677
b99f8e4d 1678 atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
59fae4de 1679
49f40163 1680 if (wc->status != IB_WC_SUCCESS)
59fae4de
CH
1681 pr_info("sending response for ioctx 0x%p failed"
1682 " with status %d\n", ioctx, wc->status);
1683
59fae4de 1684 if (state != SRPT_STATE_DONE) {
59fae4de 1685 transport_generic_free_cmd(&ioctx->cmd, 0);
a42d985b 1686 } else {
59fae4de
CH
1687 pr_err("IB completion has been received too late for"
1688 " wr_id = %u.\n", ioctx->ioctx.index);
a42d985b
BVA
1689 }
1690
539b3248 1691 srpt_process_wait_list(ch);
a42d985b
BVA
1692}
1693
a42d985b 1694/**
10eac19b
BVA
1695 * srpt_create_ch_ib - create receive and send completion queues
1696 * @ch: SRPT RDMA channel.
a42d985b
BVA
1697 */
1698static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1699{
1700 struct ib_qp_init_attr *qp_init;
1701 struct srpt_port *sport = ch->sport;
1702 struct srpt_device *sdev = sport->sdev;
30c6d877 1703 const struct ib_device_attr *attrs = &sdev->device->attrs;
ed262287 1704 int sq_size = sport->port_attrib.srp_sq_size;
dea26209 1705 int i, ret;
a42d985b
BVA
1706
1707 WARN_ON(ch->rq_size < 1);
1708
1709 ret = -ENOMEM;
9d2aa2b4 1710 qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
a42d985b
BVA
1711 if (!qp_init)
1712 goto out;
1713
ab477c1f 1714retry:
ed262287 1715 ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + sq_size,
59fae4de 1716 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
a42d985b
BVA
1717 if (IS_ERR(ch->cq)) {
1718 ret = PTR_ERR(ch->cq);
9f5d32af 1719 pr_err("failed to create CQ cqe= %d ret= %d\n",
ed262287 1720 ch->rq_size + sq_size, ret);
a42d985b
BVA
1721 goto out;
1722 }
1723
1724 qp_init->qp_context = (void *)ch;
1725 qp_init->event_handler
1726 = (void(*)(struct ib_event *, void*))srpt_qp_event;
1727 qp_init->send_cq = ch->cq;
1728 qp_init->recv_cq = ch->cq;
a42d985b
BVA
1729 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1730 qp_init->qp_type = IB_QPT_RC;
b99f8e4d
CH
1731 /*
1732 * We divide up our send queue size into half SEND WRs to send the
1733 * completions, and half R/W contexts to actually do the RDMA
1734 * READ/WRITE transfers. Note that we need to allocate CQ slots for
1735 * both both, as RDMA contexts will also post completions for the
1736 * RDMA READ case.
1737 */
ed262287
BVA
1738 qp_init->cap.max_send_wr = min(sq_size / 2, attrs->max_qp_wr);
1739 qp_init->cap.max_rdma_ctxs = sq_size / 2;
30c6d877 1740 qp_init->cap.max_send_sge = min(attrs->max_sge, SRPT_MAX_SG_PER_WQE);
b99f8e4d 1741 qp_init->port_num = ch->sport->port;
dea26209
BVA
1742 if (sdev->use_srq) {
1743 qp_init->srq = sdev->srq;
1744 } else {
1745 qp_init->cap.max_recv_wr = ch->rq_size;
1746 qp_init->cap.max_recv_sge = qp_init->cap.max_send_sge;
1747 }
a42d985b
BVA
1748
1749 ch->qp = ib_create_qp(sdev->pd, qp_init);
1750 if (IS_ERR(ch->qp)) {
1751 ret = PTR_ERR(ch->qp);
ab477c1f 1752 if (ret == -ENOMEM) {
ed262287
BVA
1753 sq_size /= 2;
1754 if (sq_size >= MIN_SRPT_SQ_SIZE) {
ab477c1f
BVA
1755 ib_destroy_cq(ch->cq);
1756 goto retry;
1757 }
1758 }
9f5d32af 1759 pr_err("failed to create_qp ret= %d\n", ret);
a42d985b
BVA
1760 goto err_destroy_cq;
1761 }
1762
1763 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1764
090fa24b 1765 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d ch= %p\n",
a42d985b 1766 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
090fa24b 1767 qp_init->cap.max_send_wr, ch);
a42d985b
BVA
1768
1769 ret = srpt_init_ch_qp(ch, ch->qp);
1770 if (ret)
1771 goto err_destroy_qp;
1772
321e329b
MM
1773 if (!sdev->use_srq)
1774 for (i = 0; i < ch->rq_size; i++)
1775 srpt_post_recv(sdev, ch, ch->ioctx_recv_ring[i]);
1776
a42d985b
BVA
1777out:
1778 kfree(qp_init);
1779 return ret;
1780
1781err_destroy_qp:
1782 ib_destroy_qp(ch->qp);
1783err_destroy_cq:
59fae4de 1784 ib_free_cq(ch->cq);
a42d985b
BVA
1785 goto out;
1786}
1787
1788static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
1789{
a42d985b 1790 ib_destroy_qp(ch->qp);
59fae4de 1791 ib_free_cq(ch->cq);
a42d985b
BVA
1792}
1793
1794/**
10eac19b
BVA
1795 * srpt_close_ch - close a RDMA channel
1796 * @ch: SRPT RDMA channel.
a42d985b 1797 *
aaf45bd8
BVA
1798 * Make sure all resources associated with the channel will be deallocated at
1799 * an appropriate time.
a42d985b 1800 *
aaf45bd8
BVA
1801 * Returns true if and only if the channel state has been modified into
1802 * CH_DRAINING.
a42d985b 1803 */
aaf45bd8 1804static bool srpt_close_ch(struct srpt_rdma_ch *ch)
a42d985b 1805{
aaf45bd8 1806 int ret;
a42d985b 1807
aaf45bd8
BVA
1808 if (!srpt_set_ch_state(ch, CH_DRAINING)) {
1809 pr_debug("%s-%d: already closed\n", ch->sess_name,
1810 ch->qp->qp_num);
1811 return false;
a42d985b 1812 }
a42d985b 1813
aaf45bd8 1814 kref_get(&ch->kref);
a42d985b 1815
aaf45bd8
BVA
1816 ret = srpt_ch_qp_err(ch);
1817 if (ret < 0)
1818 pr_err("%s-%d: changing queue pair into error state failed: %d\n",
1819 ch->sess_name, ch->qp->qp_num, ret);
a42d985b 1820
aaf45bd8
BVA
1821 ret = srpt_zerolength_write(ch);
1822 if (ret < 0) {
1823 pr_err("%s-%d: queuing zero-length write failed: %d\n",
1824 ch->sess_name, ch->qp->qp_num, ret);
1825 if (srpt_set_ch_state(ch, CH_DISCONNECTED))
1826 schedule_work(&ch->release_work);
1827 else
1828 WARN_ON_ONCE(true);
1829 }
a42d985b 1830
aaf45bd8
BVA
1831 kref_put(&ch->kref, srpt_free_ch);
1832
1833 return true;
1d19f780
NB
1834}
1835
aaf45bd8
BVA
1836/*
1837 * Change the channel state into CH_DISCONNECTING. If a channel has not yet
1838 * reached the connected state, close it. If a channel is in the connected
1839 * state, send a DREQ. If a DREQ has been received, send a DREP. Note: it is
1840 * the responsibility of the caller to ensure that this function is not
1841 * invoked concurrently with the code that accepts a connection. This means
1842 * that this function must either be invoked from inside a CM callback
1843 * function or that it must be invoked with the srpt_port.mutex held.
a42d985b 1844 */
aaf45bd8 1845static int srpt_disconnect_ch(struct srpt_rdma_ch *ch)
a42d985b 1846{
a42d985b 1847 int ret;
a42d985b 1848
aaf45bd8
BVA
1849 if (!srpt_set_ch_state(ch, CH_DISCONNECTING))
1850 return -ENOTCONN;
a42d985b 1851
090fa24b 1852 ret = ib_send_cm_dreq(ch->ib_cm.cm_id, NULL, 0);
aaf45bd8 1853 if (ret < 0)
090fa24b 1854 ret = ib_send_cm_drep(ch->ib_cm.cm_id, NULL, 0);
a42d985b 1855
aaf45bd8
BVA
1856 if (ret < 0 && srpt_close_ch(ch))
1857 ret = 0;
1d19f780 1858
aaf45bd8
BVA
1859 return ret;
1860}
1861
ba60c84f 1862static bool srpt_ch_closed(struct srpt_port *sport, struct srpt_rdma_ch *ch)
44138344 1863{
a1125314 1864 struct srpt_nexus *nexus;
44138344
BVA
1865 struct srpt_rdma_ch *ch2;
1866 bool res = true;
1867
1868 rcu_read_lock();
a1125314
BVA
1869 list_for_each_entry(nexus, &sport->nexus_list, entry) {
1870 list_for_each_entry(ch2, &nexus->ch_list, list) {
1871 if (ch2 == ch) {
1872 res = false;
1873 goto done;
1874 }
44138344
BVA
1875 }
1876 }
a1125314 1877done:
44138344
BVA
1878 rcu_read_unlock();
1879
1880 return res;
1881}
1882
940874f8
BVA
1883/* Send DREQ and wait for DREP. */
1884static void srpt_disconnect_ch_sync(struct srpt_rdma_ch *ch)
aaf45bd8 1885{
ba60c84f 1886 struct srpt_port *sport = ch->sport;
01b3ee13
BVA
1887
1888 pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num,
1889 ch->state);
1890
940874f8
BVA
1891 mutex_lock(&sport->mutex);
1892 srpt_disconnect_ch(ch);
ba60c84f 1893 mutex_unlock(&sport->mutex);
01b3ee13 1894
ba60c84f 1895 while (wait_event_timeout(sport->ch_releaseQ, srpt_ch_closed(sport, ch),
44138344 1896 5 * HZ) == 0)
01b3ee13
BVA
1897 pr_info("%s(%s-%d state %d): still waiting ...\n", __func__,
1898 ch->sess_name, ch->qp->qp_num, ch->state);
1899
01b3ee13
BVA
1900}
1901
a1125314 1902static void __srpt_close_all_ch(struct srpt_port *sport)
aaf45bd8 1903{
a1125314 1904 struct srpt_nexus *nexus;
aaf45bd8
BVA
1905 struct srpt_rdma_ch *ch;
1906
ba60c84f 1907 lockdep_assert_held(&sport->mutex);
aaf45bd8 1908
a1125314
BVA
1909 list_for_each_entry(nexus, &sport->nexus_list, entry) {
1910 list_for_each_entry(ch, &nexus->ch_list, list) {
1911 if (srpt_disconnect_ch(ch) >= 0)
1912 pr_info("Closing channel %s-%d because target %s_%d has been disabled\n",
1913 ch->sess_name, ch->qp->qp_num,
1914 sport->sdev->device->name, sport->port);
1915 srpt_close_ch(ch);
1916 }
1917 }
1918}
1919
1920/*
1921 * Look up (i_port_id, t_port_id) in sport->nexus_list. Create an entry if
1922 * it does not yet exist.
1923 */
1924static struct srpt_nexus *srpt_get_nexus(struct srpt_port *sport,
1925 const u8 i_port_id[16],
1926 const u8 t_port_id[16])
1927{
1928 struct srpt_nexus *nexus = NULL, *tmp_nexus = NULL, *n;
8b6dc529 1929
a1125314
BVA
1930 for (;;) {
1931 mutex_lock(&sport->mutex);
1932 list_for_each_entry(n, &sport->nexus_list, entry) {
1933 if (memcmp(n->i_port_id, i_port_id, 16) == 0 &&
1934 memcmp(n->t_port_id, t_port_id, 16) == 0) {
1935 nexus = n;
1936 break;
1937 }
1938 }
1939 if (!nexus && tmp_nexus) {
1940 list_add_tail_rcu(&tmp_nexus->entry,
1941 &sport->nexus_list);
1942 swap(nexus, tmp_nexus);
1943 }
1944 mutex_unlock(&sport->mutex);
1945
1946 if (nexus)
1947 break;
1948 tmp_nexus = kzalloc(sizeof(*nexus), GFP_KERNEL);
1949 if (!tmp_nexus) {
1950 nexus = ERR_PTR(-ENOMEM);
1951 break;
8b6dc529 1952 }
a1125314
BVA
1953 init_rcu_head(&tmp_nexus->rcu);
1954 INIT_LIST_HEAD(&tmp_nexus->ch_list);
1955 memcpy(tmp_nexus->i_port_id, i_port_id, 16);
1956 memcpy(tmp_nexus->t_port_id, t_port_id, 16);
a42d985b 1957 }
8b6dc529 1958
a1125314
BVA
1959 kfree(tmp_nexus);
1960
1961 return nexus;
1962}
1963
1964static void srpt_set_enabled(struct srpt_port *sport, bool enabled)
1965 __must_hold(&sport->mutex)
1966{
1967 lockdep_assert_held(&sport->mutex);
1968
1969 if (sport->enabled == enabled)
1970 return;
1971 sport->enabled = enabled;
1972 if (!enabled)
1973 __srpt_close_all_ch(sport);
a42d985b
BVA
1974}
1975
aaf45bd8
BVA
1976static void srpt_free_ch(struct kref *kref)
1977{
1978 struct srpt_rdma_ch *ch = container_of(kref, struct srpt_rdma_ch, kref);
1979
795bc112 1980 kfree_rcu(ch, rcu);
a42d985b
BVA
1981}
1982
1983static void srpt_release_channel_work(struct work_struct *w)
1984{
1985 struct srpt_rdma_ch *ch;
1986 struct srpt_device *sdev;
ba60c84f 1987 struct srpt_port *sport;
9474b043 1988 struct se_session *se_sess;
a42d985b
BVA
1989
1990 ch = container_of(w, struct srpt_rdma_ch, release_work);
44138344 1991 pr_debug("%s-%d\n", ch->sess_name, ch->qp->qp_num);
a42d985b
BVA
1992
1993 sdev = ch->sport->sdev;
1994 BUG_ON(!sdev);
1995
9474b043
NB
1996 se_sess = ch->sess;
1997 BUG_ON(!se_sess);
1998
88936259 1999 target_sess_cmd_list_set_waiting(se_sess);
be646c2d 2000 target_wait_for_sess_cmds(se_sess);
9474b043
NB
2001
2002 transport_deregister_session_configfs(se_sess);
2003 transport_deregister_session(se_sess);
a42d985b
BVA
2004 ch->sess = NULL;
2005
090fa24b 2006 ib_destroy_cm_id(ch->ib_cm.cm_id);
0b41d6ca 2007
a42d985b
BVA
2008 srpt_destroy_ch_ib(ch);
2009
2010 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2011 ch->sport->sdev, ch->rq_size,
ed262287 2012 ch->max_rsp_size, DMA_TO_DEVICE);
a42d985b 2013
dea26209
BVA
2014 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring,
2015 sdev, ch->rq_size,
2016 srp_max_req_size, DMA_FROM_DEVICE);
2017
ba60c84f
BVA
2018 sport = ch->sport;
2019 mutex_lock(&sport->mutex);
795bc112 2020 list_del_rcu(&ch->list);
ba60c84f 2021 mutex_unlock(&sport->mutex);
795bc112 2022
ba60c84f 2023 wake_up(&sport->ch_releaseQ);
a42d985b 2024
aaf45bd8 2025 kref_put(&ch->kref, srpt_free_ch);
a42d985b
BVA
2026}
2027
a42d985b 2028/**
10eac19b
BVA
2029 * srpt_cm_req_recv - process the event IB_CM_REQ_RECEIVED
2030 * @cm_id: IB/CM connection identifier.
2031 * @param: IB/CM REQ parameters.
2032 * @private_data: IB/CM REQ private data.
a42d985b
BVA
2033 *
2034 * Ownership of the cm_id is transferred to the target session if this
2035 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2036 */
2037static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2038 struct ib_cm_req_event_param *param,
2039 void *private_data)
2040{
2041 struct srpt_device *sdev = cm_id->context;
2042 struct srpt_port *sport = &sdev->port[param->port - 1];
a1125314 2043 struct srpt_nexus *nexus;
a42d985b 2044 struct srp_login_req *req;
a1125314
BVA
2045 struct srp_login_rsp *rsp = NULL;
2046 struct srp_login_rej *rej = NULL;
2047 struct ib_cm_rep_param *rep_param = NULL;
2048 struct srpt_rdma_ch *ch;
2dc98f09 2049 char i_port_id[36];
a42d985b 2050 u32 it_iu_len;
db7683d7 2051 int i, ret;
a42d985b
BVA
2052
2053 WARN_ON_ONCE(irqs_disabled());
2054
2055 if (WARN_ON(!sdev || !private_data))
2056 return -EINVAL;
2057
2058 req = (struct srp_login_req *)private_data;
2059
2060 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2061
c5efb621 2062 pr_info("Received SRP_LOGIN_REQ with i_port_id %pI6, t_port_id %pI6 and it_iu_len %d on port %d (guid=%pI6); pkey %#04x\n",
b185d3f8 2063 req->initiator_port_id, req->target_port_id, it_iu_len,
c5efb621
BVA
2064 param->port, &sport->gid,
2065 be16_to_cpu(param->primary_path->pkey));
a42d985b 2066
a1125314
BVA
2067 nexus = srpt_get_nexus(sport, req->initiator_port_id,
2068 req->target_port_id);
2069 if (IS_ERR(nexus)) {
2070 ret = PTR_ERR(nexus);
2071 goto out;
2072 }
2073
db7683d7 2074 ret = -ENOMEM;
9d2aa2b4
BVA
2075 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
2076 rej = kzalloc(sizeof(*rej), GFP_KERNEL);
2077 rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
db7683d7 2078 if (!rsp || !rej || !rep_param)
a42d985b 2079 goto out;
a42d985b 2080
db7683d7 2081 ret = -EINVAL;
a42d985b 2082 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
b356c1c1 2083 rej->reason = cpu_to_be32(
db7683d7
BVA
2084 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
2085 pr_err("rejected SRP_LOGIN_REQ because its length (%d bytes) is out of range (%d .. %d)\n",
a42d985b
BVA
2086 it_iu_len, 64, srp_max_req_size);
2087 goto reject;
2088 }
2089
2090 if (!sport->enabled) {
db7683d7
BVA
2091 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2092 pr_info("rejected SRP_LOGIN_REQ because target port %s_%d has not yet been enabled\n",
2093 sport->sdev->device->name, param->port);
a42d985b
BVA
2094 goto reject;
2095 }
2096
a42d985b
BVA
2097 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2098 || *(__be64 *)(req->target_port_id + 8) !=
2099 cpu_to_be64(srpt_service_guid)) {
b356c1c1 2100 rej->reason = cpu_to_be32(
db7683d7
BVA
2101 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
2102 pr_err("rejected SRP_LOGIN_REQ because it has an invalid target port identifier.\n");
a42d985b
BVA
2103 goto reject;
2104 }
2105
db7683d7 2106 ret = -ENOMEM;
9d2aa2b4 2107 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
a42d985b 2108 if (!ch) {
db7683d7
BVA
2109 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2110 pr_err("rejected SRP_LOGIN_REQ because out of memory.\n");
a42d985b
BVA
2111 goto reject;
2112 }
2113
795bc112 2114 init_rcu_head(&ch->rcu);
aaf45bd8 2115 kref_init(&ch->kref);
c5efb621 2116 ch->pkey = be16_to_cpu(param->primary_path->pkey);
a1125314 2117 ch->nexus = nexus;
aaf45bd8 2118 ch->zw_cqe.done = srpt_zerolength_write_done;
a42d985b 2119 INIT_WORK(&ch->release_work, srpt_release_channel_work);
a42d985b 2120 ch->sport = &sdev->port[param->port - 1];
090fa24b 2121 ch->ib_cm.cm_id = cm_id;
2739b592 2122 cm_id->context = ch;
a42d985b 2123 /*
7a01d05c
BVA
2124 * ch->rq_size should be at least as large as the initiator queue
2125 * depth to avoid that the initiator driver has to report QUEUE_FULL
2126 * to the SCSI mid-layer.
a42d985b 2127 */
ed262287 2128 ch->rq_size = min(MAX_SRPT_RQ_SIZE, sdev->device->attrs.max_qp_wr);
a42d985b
BVA
2129 spin_lock_init(&ch->spinlock);
2130 ch->state = CH_CONNECTING;
2131 INIT_LIST_HEAD(&ch->cmd_wait_list);
ed262287 2132 ch->max_rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
a42d985b
BVA
2133
2134 ch->ioctx_ring = (struct srpt_send_ioctx **)
2135 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2136 sizeof(*ch->ioctx_ring[0]),
ed262287 2137 ch->max_rsp_size, DMA_TO_DEVICE);
db7683d7
BVA
2138 if (!ch->ioctx_ring) {
2139 pr_err("rejected SRP_LOGIN_REQ because creating a new QP SQ ring failed.\n");
2140 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
a42d985b 2141 goto free_ch;
db7683d7 2142 }
a42d985b 2143
3c968887
BVA
2144 INIT_LIST_HEAD(&ch->free_list);
2145 for (i = 0; i < ch->rq_size; i++) {
2146 ch->ioctx_ring[i]->ch = ch;
2147 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2148 }
dea26209
BVA
2149 if (!sdev->use_srq) {
2150 ch->ioctx_recv_ring = (struct srpt_recv_ioctx **)
2151 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2152 sizeof(*ch->ioctx_recv_ring[0]),
2153 srp_max_req_size,
2154 DMA_FROM_DEVICE);
2155 if (!ch->ioctx_recv_ring) {
2156 pr_err("rejected SRP_LOGIN_REQ because creating a new QP RQ ring failed.\n");
2157 rej->reason =
2158 cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2159 goto free_ring;
2160 }
fcf58936
BVA
2161 for (i = 0; i < ch->rq_size; i++)
2162 INIT_LIST_HEAD(&ch->ioctx_recv_ring[i]->wait_list);
dea26209 2163 }
3c968887 2164
a42d985b 2165 ret = srpt_create_ch_ib(ch);
a42d985b 2166 if (ret) {
b356c1c1 2167 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
db7683d7
BVA
2168 pr_err("rejected SRP_LOGIN_REQ because creating a new RDMA channel failed.\n");
2169 goto free_recv_ring;
a42d985b 2170 }
f246c941 2171
2dc98f09 2172 srpt_format_guid(ch->sess_name, sizeof(ch->sess_name),
b14cb744 2173 &param->primary_path->dgid.global.interface_id);
2dc98f09 2174 snprintf(i_port_id, sizeof(i_port_id), "0x%016llx%016llx",
a1125314
BVA
2175 be64_to_cpu(*(__be64 *)nexus->i_port_id),
2176 be64_to_cpu(*(__be64 *)(nexus->i_port_id + 8)));
a42d985b
BVA
2177
2178 pr_debug("registering session %s\n", ch->sess_name);
2179
2bce1a6d
BVA
2180 if (sport->port_guid_tpg.se_tpg_wwn)
2181 ch->sess = target_alloc_session(&sport->port_guid_tpg, 0, 0,
2182 TARGET_PROT_NORMAL,
2dc98f09 2183 ch->sess_name, ch, NULL);
2bce1a6d
BVA
2184 if (sport->port_gid_tpg.se_tpg_wwn && IS_ERR_OR_NULL(ch->sess))
2185 ch->sess = target_alloc_session(&sport->port_gid_tpg, 0, 0,
2dc98f09 2186 TARGET_PROT_NORMAL, i_port_id, ch,
0d38c240
BVA
2187 NULL);
2188 /* Retry without leading "0x" */
2bce1a6d
BVA
2189 if (sport->port_gid_tpg.se_tpg_wwn && IS_ERR_OR_NULL(ch->sess))
2190 ch->sess = target_alloc_session(&sport->port_gid_tpg, 0, 0,
0d38c240 2191 TARGET_PROT_NORMAL,
2dc98f09 2192 i_port_id + 2, ch, NULL);
2bce1a6d 2193 if (IS_ERR_OR_NULL(ch->sess)) {
db7683d7
BVA
2194 ret = PTR_ERR(ch->sess);
2195 pr_info("Rejected login for initiator %s: ret = %d.\n",
2196 ch->sess_name, ret);
2197 rej->reason = cpu_to_be32(ret == -ENOMEM ?
b42057ab 2198 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES :
f246c941 2199 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
db7683d7
BVA
2200 goto reject;
2201 }
2202
2203 mutex_lock(&sport->mutex);
2204
2205 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2206 struct srpt_rdma_ch *ch2;
2207
2208 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2209
2210 list_for_each_entry(ch2, &nexus->ch_list, list) {
2211 if (srpt_disconnect_ch(ch2) < 0)
2212 continue;
2213 pr_info("Relogin - closed existing channel %s\n",
2214 ch2->sess_name);
2215 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2216 }
2217 } else {
2218 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2219 }
2220
2221 list_add_tail_rcu(&ch->list, &nexus->ch_list);
2222
2223 if (!sport->enabled) {
2224 rej->reason = cpu_to_be32(
2225 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2226 pr_info("rejected SRP_LOGIN_REQ because target %s_%d is not enabled\n",
2227 sdev->device->name, param->port);
2228 mutex_unlock(&sport->mutex);
2229 goto reject;
2230 }
2231
2232 mutex_unlock(&sport->mutex);
2233
2234 ret = srpt_ch_qp_rtr(ch, ch->qp);
2235 if (ret) {
2236 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2237 pr_err("rejected SRP_LOGIN_REQ because enabling RTR failed (error code = %d)\n",
2238 ret);
f246c941 2239 goto destroy_ib;
a42d985b 2240 }
a42d985b 2241
090fa24b
BVA
2242 pr_debug("Establish connection sess=%p name=%s ch=%p\n", ch->sess,
2243 ch->sess_name, ch);
a42d985b
BVA
2244
2245 /* create srp_login_response */
2246 rsp->opcode = SRP_LOGIN_RSP;
2247 rsp->tag = req->tag;
2248 rsp->max_it_iu_len = req->req_it_iu_len;
2249 rsp->max_ti_iu_len = req->req_it_iu_len;
2250 ch->max_ti_iu_len = it_iu_len;
db7683d7
BVA
2251 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
2252 SRP_BUF_FORMAT_INDIRECT);
a42d985b
BVA
2253 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2254 atomic_set(&ch->req_lim, ch->rq_size);
2255 atomic_set(&ch->req_lim_delta, 0);
2256
2257 /* create cm reply */
2258 rep_param->qp_num = ch->qp->qp_num;
2259 rep_param->private_data = (void *)rsp;
9d2aa2b4 2260 rep_param->private_data_len = sizeof(*rsp);
a42d985b
BVA
2261 rep_param->rnr_retry_count = 7;
2262 rep_param->flow_control = 1;
2263 rep_param->failover_accepted = 0;
2264 rep_param->srq = 1;
2265 rep_param->responder_resources = 4;
2266 rep_param->initiator_depth = 4;
2267
db7683d7
BVA
2268 /*
2269 * Hold the sport mutex while accepting a connection to avoid that
2270 * srpt_disconnect_ch() is invoked concurrently with this code.
2271 */
ba60c84f 2272 mutex_lock(&sport->mutex);
db7683d7
BVA
2273 if (sport->enabled && ch->state == CH_CONNECTING)
2274 ret = ib_send_cm_rep(cm_id, rep_param);
2275 else
2276 ret = -EINVAL;
ba60c84f 2277 mutex_unlock(&sport->mutex);
a42d985b 2278
db7683d7
BVA
2279 switch (ret) {
2280 case 0:
2281 break;
2282 case -EINVAL:
2283 goto reject;
2284 default:
2285 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2286 pr_err("sending SRP_LOGIN_REQ response failed (error code = %d)\n",
2287 ret);
2288 goto reject;
2289 }
a42d985b 2290
db7683d7 2291 goto out;
a42d985b
BVA
2292
2293destroy_ib:
2294 srpt_destroy_ch_ib(ch);
2295
dea26209
BVA
2296free_recv_ring:
2297 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring,
2298 ch->sport->sdev, ch->rq_size,
2299 srp_max_req_size, DMA_FROM_DEVICE);
2300
a42d985b
BVA
2301free_ring:
2302 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2303 ch->sport->sdev, ch->rq_size,
ed262287 2304 ch->max_rsp_size, DMA_TO_DEVICE);
a42d985b 2305free_ch:
db7683d7 2306 cm_id->context = NULL;
a42d985b 2307 kfree(ch);
db7683d7
BVA
2308 ch = NULL;
2309
2310 WARN_ON_ONCE(ret == 0);
a42d985b
BVA
2311
2312reject:
db7683d7 2313 pr_info("Rejecting login with reason %#x\n", be32_to_cpu(rej->reason));
a42d985b
BVA
2314 rej->opcode = SRP_LOGIN_REJ;
2315 rej->tag = req->tag;
db7683d7
BVA
2316 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
2317 SRP_BUF_FORMAT_INDIRECT);
a42d985b
BVA
2318
2319 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
9d2aa2b4 2320 (void *)rej, sizeof(*rej));
a42d985b
BVA
2321
2322out:
2323 kfree(rep_param);
2324 kfree(rsp);
2325 kfree(rej);
2326
2327 return ret;
2328}
2329
2739b592
BVA
2330static void srpt_cm_rej_recv(struct srpt_rdma_ch *ch,
2331 enum ib_cm_rej_reason reason,
2332 const u8 *private_data,
2333 u8 private_data_len)
a42d985b 2334{
c13c90ea
BVA
2335 char *priv = NULL;
2336 int i;
2337
2338 if (private_data_len && (priv = kmalloc(private_data_len * 3 + 1,
2339 GFP_KERNEL))) {
2340 for (i = 0; i < private_data_len; i++)
2341 sprintf(priv + 3 * i, " %02x", private_data[i]);
2342 }
2343 pr_info("Received CM REJ for ch %s-%d; reason %d%s%s.\n",
2344 ch->sess_name, ch->qp->qp_num, reason, private_data_len ?
2345 "; private data" : "", priv ? priv : " (?)");
2346 kfree(priv);
a42d985b
BVA
2347}
2348
2349/**
10eac19b
BVA
2350 * srpt_cm_rtu_recv - process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event
2351 * @ch: SRPT RDMA channel.
a42d985b
BVA
2352 *
2353 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2354 * and that the recipient may begin transmitting (RTU = ready to use).
2355 */
2739b592 2356static void srpt_cm_rtu_recv(struct srpt_rdma_ch *ch)
a42d985b 2357{
a42d985b
BVA
2358 int ret;
2359
db7683d7
BVA
2360 ret = srpt_ch_qp_rts(ch, ch->qp);
2361 if (ret < 0) {
2362 pr_err("%s-%d: QP transition to RTS failed\n", ch->sess_name,
2363 ch->qp->qp_num);
2364 srpt_close_ch(ch);
2365 return;
a42d985b 2366 }
db7683d7 2367
db7683d7
BVA
2368 /*
2369 * Note: calling srpt_close_ch() if the transition to the LIVE state
2370 * fails is not necessary since that means that that function has
2371 * already been invoked from another thread.
2372 */
e28a547d 2373 if (!srpt_set_ch_state(ch, CH_LIVE)) {
db7683d7
BVA
2374 pr_err("%s-%d: channel transition to LIVE state failed\n",
2375 ch->sess_name, ch->qp->qp_num);
e28a547d
BVA
2376 return;
2377 }
2378
2379 /* Trigger wait list processing. */
2380 ret = srpt_zerolength_write(ch);
2381 WARN_ONCE(ret < 0, "%d\n", ret);
a42d985b
BVA
2382}
2383
a42d985b 2384/**
10eac19b
BVA
2385 * srpt_cm_handler - IB connection manager callback function
2386 * @cm_id: IB/CM connection identifier.
2387 * @event: IB/CM event.
a42d985b
BVA
2388 *
2389 * A non-zero return value will cause the caller destroy the CM ID.
2390 *
2391 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2392 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2393 * a non-zero value in any other case will trigger a race with the
2394 * ib_destroy_cm_id() call in srpt_release_channel().
2395 */
2396static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2397{
2739b592 2398 struct srpt_rdma_ch *ch = cm_id->context;
a42d985b
BVA
2399 int ret;
2400
2401 ret = 0;
2402 switch (event->event) {
2403 case IB_CM_REQ_RECEIVED:
2404 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2405 event->private_data);
2406 break;
2407 case IB_CM_REJ_RECEIVED:
2739b592
BVA
2408 srpt_cm_rej_recv(ch, event->param.rej_rcvd.reason,
2409 event->private_data,
2410 IB_CM_REJ_PRIVATE_DATA_SIZE);
a42d985b
BVA
2411 break;
2412 case IB_CM_RTU_RECEIVED:
2413 case IB_CM_USER_ESTABLISHED:
2739b592 2414 srpt_cm_rtu_recv(ch);
a42d985b
BVA
2415 break;
2416 case IB_CM_DREQ_RECEIVED:
aaf45bd8 2417 srpt_disconnect_ch(ch);
a42d985b
BVA
2418 break;
2419 case IB_CM_DREP_RECEIVED:
2739b592
BVA
2420 pr_info("Received CM DREP message for ch %s-%d.\n",
2421 ch->sess_name, ch->qp->qp_num);
aaf45bd8 2422 srpt_close_ch(ch);
a42d985b
BVA
2423 break;
2424 case IB_CM_TIMEWAIT_EXIT:
2739b592
BVA
2425 pr_info("Received CM TimeWait exit for ch %s-%d.\n",
2426 ch->sess_name, ch->qp->qp_num);
aaf45bd8 2427 srpt_close_ch(ch);
a42d985b
BVA
2428 break;
2429 case IB_CM_REP_ERROR:
2739b592
BVA
2430 pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
2431 ch->qp->qp_num);
a42d985b
BVA
2432 break;
2433 case IB_CM_DREQ_ERROR:
1e20a2a5 2434 pr_info("Received CM DREQ ERROR event.\n");
a42d985b
BVA
2435 break;
2436 case IB_CM_MRA_RECEIVED:
1e20a2a5 2437 pr_info("Received CM MRA event\n");
a42d985b
BVA
2438 break;
2439 default:
1e20a2a5 2440 pr_err("received unrecognized CM event %d\n", event->event);
a42d985b
BVA
2441 break;
2442 }
2443
2444 return ret;
2445}
2446
a42d985b
BVA
2447static int srpt_write_pending_status(struct se_cmd *se_cmd)
2448{
2449 struct srpt_send_ioctx *ioctx;
2450
2451 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
dd3bec86 2452 return ioctx->state == SRPT_STATE_NEED_DATA;
a42d985b
BVA
2453}
2454
2455/*
10eac19b 2456 * srpt_write_pending - Start data transfer from initiator to target (write).
a42d985b
BVA
2457 */
2458static int srpt_write_pending(struct se_cmd *se_cmd)
2459{
fc3af58d
BVA
2460 struct srpt_send_ioctx *ioctx =
2461 container_of(se_cmd, struct srpt_send_ioctx, cmd);
2462 struct srpt_rdma_ch *ch = ioctx->ch;
b99f8e4d
CH
2463 struct ib_send_wr *first_wr = NULL, *bad_wr;
2464 struct ib_cqe *cqe = &ioctx->rdma_cqe;
a42d985b 2465 enum srpt_command_state new_state;
b99f8e4d 2466 int ret, i;
a42d985b
BVA
2467
2468 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2469 WARN_ON(new_state == SRPT_STATE_DONE);
b99f8e4d
CH
2470
2471 if (atomic_sub_return(ioctx->n_rdma, &ch->sq_wr_avail) < 0) {
2472 pr_warn("%s: IB send queue full (needed %d)\n",
2473 __func__, ioctx->n_rdma);
2474 ret = -ENOMEM;
2475 goto out_undo;
2476 }
2477
2478 cqe->done = srpt_rdma_read_done;
2479 for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
2480 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
2481
2482 first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, ch->sport->port,
2483 cqe, first_wr);
2484 cqe = NULL;
2485 }
dcc9881e 2486
b99f8e4d
CH
2487 ret = ib_post_send(ch->qp, first_wr, &bad_wr);
2488 if (ret) {
2489 pr_err("%s: ib_post_send() returned %d for %d (avail: %d)\n",
2490 __func__, ret, ioctx->n_rdma,
2491 atomic_read(&ch->sq_wr_avail));
2492 goto out_undo;
2493 }
2494
2495 return 0;
2496out_undo:
2497 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
2498 return ret;
a42d985b
BVA
2499}
2500
2501static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2502{
2503 switch (tcm_mgmt_status) {
2504 case TMR_FUNCTION_COMPLETE:
2505 return SRP_TSK_MGMT_SUCCESS;
2506 case TMR_FUNCTION_REJECTED:
2507 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2508 }
2509 return SRP_TSK_MGMT_FAILED;
2510}
2511
2512/**
10eac19b
BVA
2513 * srpt_queue_response - transmit the response to a SCSI command
2514 * @cmd: SCSI target command.
a42d985b
BVA
2515 *
2516 * Callback function called by the TCM core. Must not block since it can be
2517 * invoked on the context of the IB completion handler.
2518 */
b79fafac 2519static void srpt_queue_response(struct se_cmd *cmd)
a42d985b 2520{
b99f8e4d
CH
2521 struct srpt_send_ioctx *ioctx =
2522 container_of(cmd, struct srpt_send_ioctx, cmd);
2523 struct srpt_rdma_ch *ch = ioctx->ch;
2524 struct srpt_device *sdev = ch->sport->sdev;
10fce586 2525 struct ib_send_wr send_wr, *first_wr = &send_wr, *bad_wr;
b99f8e4d 2526 struct ib_sge sge;
a42d985b 2527 enum srpt_command_state state;
b99f8e4d 2528 int resp_len, ret, i;
a42d985b
BVA
2529 u8 srp_tm_status;
2530
a42d985b
BVA
2531 BUG_ON(!ch);
2532
a42d985b
BVA
2533 state = ioctx->state;
2534 switch (state) {
2535 case SRPT_STATE_NEW:
2536 case SRPT_STATE_DATA_IN:
2537 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2538 break;
2539 case SRPT_STATE_MGMT:
2540 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2541 break;
2542 default:
2543 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2544 ch, ioctx->ioctx.index, ioctx->state);
2545 break;
2546 }
a42d985b 2547
55d69427 2548 if (unlikely(WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT)))
b79fafac 2549 return;
a42d985b 2550
a42d985b 2551 /* For read commands, transfer the data to the initiator. */
b99f8e4d
CH
2552 if (ioctx->cmd.data_direction == DMA_FROM_DEVICE &&
2553 ioctx->cmd.data_length &&
a42d985b 2554 !ioctx->queue_status_only) {
b99f8e4d
CH
2555 for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
2556 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
2557
2558 first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp,
10fce586 2559 ch->sport->port, NULL, first_wr);
a42d985b
BVA
2560 }
2561 }
2562
2563 if (state != SRPT_STATE_MGMT)
649ee054 2564 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
a42d985b
BVA
2565 cmd->scsi_status);
2566 else {
2567 srp_tm_status
2568 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2569 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
649ee054 2570 ioctx->cmd.tag);
a42d985b 2571 }
b99f8e4d
CH
2572
2573 atomic_inc(&ch->req_lim);
2574
2575 if (unlikely(atomic_sub_return(1 + ioctx->n_rdma,
2576 &ch->sq_wr_avail) < 0)) {
2577 pr_warn("%s: IB send queue full (needed %d)\n",
2578 __func__, ioctx->n_rdma);
2579 ret = -ENOMEM;
2580 goto out;
2581 }
2582
2583 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, resp_len,
2584 DMA_TO_DEVICE);
2585
2586 sge.addr = ioctx->ioctx.dma;
2587 sge.length = resp_len;
74333f12 2588 sge.lkey = sdev->lkey;
b99f8e4d
CH
2589
2590 ioctx->ioctx.cqe.done = srpt_send_done;
2591 send_wr.next = NULL;
2592 send_wr.wr_cqe = &ioctx->ioctx.cqe;
2593 send_wr.sg_list = &sge;
2594 send_wr.num_sge = 1;
2595 send_wr.opcode = IB_WR_SEND;
2596 send_wr.send_flags = IB_SEND_SIGNALED;
2597
2598 ret = ib_post_send(ch->qp, first_wr, &bad_wr);
2599 if (ret < 0) {
2600 pr_err("%s: sending cmd response failed for tag %llu (%d)\n",
2601 __func__, ioctx->cmd.tag, ret);
2602 goto out;
a42d985b 2603 }
b99f8e4d
CH
2604
2605 return;
2606
2607out:
2608 atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
2609 atomic_dec(&ch->req_lim);
2610 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
2611 target_put_sess_cmd(&ioctx->cmd);
b79fafac 2612}
a42d985b 2613
b79fafac
JE
2614static int srpt_queue_data_in(struct se_cmd *cmd)
2615{
2616 srpt_queue_response(cmd);
2617 return 0;
2618}
2619
2620static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2621{
2622 srpt_queue_response(cmd);
a42d985b
BVA
2623}
2624
131e6abc
NB
2625static void srpt_aborted_task(struct se_cmd *cmd)
2626{
131e6abc
NB
2627}
2628
a42d985b
BVA
2629static int srpt_queue_status(struct se_cmd *cmd)
2630{
2631 struct srpt_send_ioctx *ioctx;
2632
2633 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2634 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2635 if (cmd->se_cmd_flags &
2636 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2637 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2638 ioctx->queue_status_only = true;
b79fafac
JE
2639 srpt_queue_response(cmd);
2640 return 0;
a42d985b
BVA
2641}
2642
2643static void srpt_refresh_port_work(struct work_struct *work)
2644{
2645 struct srpt_port *sport = container_of(work, struct srpt_port, work);
2646
2647 srpt_refresh_port(sport);
2648}
2649
a1125314
BVA
2650static bool srpt_ch_list_empty(struct srpt_port *sport)
2651{
2652 struct srpt_nexus *nexus;
2653 bool res = true;
2654
2655 rcu_read_lock();
2656 list_for_each_entry(nexus, &sport->nexus_list, entry)
2657 if (!list_empty(&nexus->ch_list))
2658 res = false;
2659 rcu_read_unlock();
2660
2661 return res;
2662}
2663
a42d985b 2664/**
ba60c84f
BVA
2665 * srpt_release_sport - disable login and wait for associated channels
2666 * @sport: SRPT HCA port.
a42d985b 2667 */
ba60c84f 2668static int srpt_release_sport(struct srpt_port *sport)
a42d985b 2669{
a1125314
BVA
2670 struct srpt_nexus *nexus, *next_n;
2671 struct srpt_rdma_ch *ch;
a42d985b
BVA
2672
2673 WARN_ON_ONCE(irqs_disabled());
2674
ba60c84f
BVA
2675 mutex_lock(&sport->mutex);
2676 srpt_set_enabled(sport, false);
2677 mutex_unlock(&sport->mutex);
a42d985b 2678
a1125314
BVA
2679 while (wait_event_timeout(sport->ch_releaseQ,
2680 srpt_ch_list_empty(sport), 5 * HZ) <= 0) {
2681 pr_info("%s_%d: waiting for session unregistration ...\n",
2682 sport->sdev->device->name, sport->port);
2683 rcu_read_lock();
2684 list_for_each_entry(nexus, &sport->nexus_list, entry) {
2685 list_for_each_entry(ch, &nexus->ch_list, list) {
2686 pr_info("%s-%d: state %s\n",
2687 ch->sess_name, ch->qp->qp_num,
2688 get_ch_state_name(ch->state));
2689 }
2690 }
2691 rcu_read_unlock();
2692 }
2693
2694 mutex_lock(&sport->mutex);
2695 list_for_each_entry_safe(nexus, next_n, &sport->nexus_list, entry) {
2696 list_del(&nexus->entry);
2697 kfree_rcu(nexus, rcu);
2698 }
2699 mutex_unlock(&sport->mutex);
a42d985b
BVA
2700
2701 return 0;
2702}
2703
2bce1a6d 2704static struct se_wwn *__srpt_lookup_wwn(const char *name)
a42d985b
BVA
2705{
2706 struct ib_device *dev;
2707 struct srpt_device *sdev;
2708 struct srpt_port *sport;
2709 int i;
2710
2711 list_for_each_entry(sdev, &srpt_dev_list, list) {
2712 dev = sdev->device;
2713 if (!dev)
2714 continue;
2715
2716 for (i = 0; i < dev->phys_port_cnt; i++) {
2717 sport = &sdev->port[i];
2718
2bce1a6d
BVA
2719 if (strcmp(sport->port_guid, name) == 0)
2720 return &sport->port_guid_wwn;
2721 if (strcmp(sport->port_gid, name) == 0)
2722 return &sport->port_gid_wwn;
a42d985b
BVA
2723 }
2724 }
2725
2726 return NULL;
2727}
2728
2bce1a6d 2729static struct se_wwn *srpt_lookup_wwn(const char *name)
a42d985b 2730{
2bce1a6d 2731 struct se_wwn *wwn;
a42d985b
BVA
2732
2733 spin_lock(&srpt_dev_lock);
2bce1a6d 2734 wwn = __srpt_lookup_wwn(name);
a42d985b
BVA
2735 spin_unlock(&srpt_dev_lock);
2736
2bce1a6d 2737 return wwn;
a42d985b
BVA
2738}
2739
c76d7d64
BVA
2740static void srpt_free_srq(struct srpt_device *sdev)
2741{
2742 if (!sdev->srq)
2743 return;
2744
2745 ib_destroy_srq(sdev->srq);
2746 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2747 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
2748 sdev->srq = NULL;
2749}
2750
2751static int srpt_alloc_srq(struct srpt_device *sdev)
2752{
2753 struct ib_srq_init_attr srq_attr = {
2754 .event_handler = srpt_srq_event,
2755 .srq_context = (void *)sdev,
2756 .attr.max_wr = sdev->srq_size,
2757 .attr.max_sge = 1,
2758 .srq_type = IB_SRQT_BASIC,
2759 };
2760 struct ib_device *device = sdev->device;
2761 struct ib_srq *srq;
2762 int i;
2763
2764 WARN_ON_ONCE(sdev->srq);
2765 srq = ib_create_srq(sdev->pd, &srq_attr);
2766 if (IS_ERR(srq)) {
2767 pr_debug("ib_create_srq() failed: %ld\n", PTR_ERR(srq));
2768 return PTR_ERR(srq);
2769 }
2770
2771 pr_debug("create SRQ #wr= %d max_allow=%d dev= %s\n", sdev->srq_size,
2772 sdev->device->attrs.max_srq_wr, device->name);
2773
2774 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
2775 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
2776 sizeof(*sdev->ioctx_ring[0]),
2777 srp_max_req_size, DMA_FROM_DEVICE);
2778 if (!sdev->ioctx_ring) {
2779 ib_destroy_srq(srq);
2780 return -ENOMEM;
2781 }
2782
2783 sdev->use_srq = true;
2784 sdev->srq = srq;
2785
fcf58936
BVA
2786 for (i = 0; i < sdev->srq_size; ++i) {
2787 INIT_LIST_HEAD(&sdev->ioctx_ring[i]->wait_list);
c76d7d64 2788 srpt_post_recv(sdev, NULL, sdev->ioctx_ring[i]);
fcf58936 2789 }
c76d7d64
BVA
2790
2791 return 0;
2792}
2793
2794static int srpt_use_srq(struct srpt_device *sdev, bool use_srq)
2795{
2796 struct ib_device *device = sdev->device;
2797 int ret = 0;
2798
2799 if (!use_srq) {
2800 srpt_free_srq(sdev);
2801 sdev->use_srq = false;
2802 } else if (use_srq && !sdev->srq) {
2803 ret = srpt_alloc_srq(sdev);
2804 }
2805 pr_debug("%s(%s): use_srq = %d; ret = %d\n", __func__, device->name,
2806 sdev->use_srq, ret);
2807 return ret;
2808}
2809
a42d985b 2810/**
10eac19b
BVA
2811 * srpt_add_one - InfiniBand device addition callback function
2812 * @device: Describes a HCA.
a42d985b
BVA
2813 */
2814static void srpt_add_one(struct ib_device *device)
2815{
2816 struct srpt_device *sdev;
2817 struct srpt_port *sport;
a42d985b
BVA
2818 int i;
2819
e3dfa60c 2820 pr_debug("device = %p\n", device);
a42d985b 2821
9d2aa2b4 2822 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
a42d985b
BVA
2823 if (!sdev)
2824 goto err;
2825
2826 sdev->device = device;
ba60c84f 2827 mutex_init(&sdev->sdev_mutex);
a42d985b 2828
ed082d36 2829 sdev->pd = ib_alloc_pd(device, 0);
a42d985b
BVA
2830 if (IS_ERR(sdev->pd))
2831 goto free_dev;
2832
74333f12 2833 sdev->lkey = sdev->pd->local_dma_lkey;
a42d985b 2834
4a061b28 2835 sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
a42d985b 2836
c76d7d64 2837 srpt_use_srq(sdev, sdev->port[0].port_attrib.use_srq);
a42d985b
BVA
2838
2839 if (!srpt_service_guid)
2840 srpt_service_guid = be64_to_cpu(device->node_guid);
2841
2842 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
2843 if (IS_ERR(sdev->cm_id))
dea26209 2844 goto err_ring;
a42d985b
BVA
2845
2846 /* print out target login information */
2847 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
2848 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
2849 srpt_service_guid, srpt_service_guid);
2850
2851 /*
2852 * We do not have a consistent service_id (ie. also id_ext of target_id)
2853 * to identify this target. We currently use the guid of the first HCA
2854 * in the system as service_id; therefore, the target_id will change
2855 * if this HCA is gone bad and replaced by different HCA
2856 */
73fec7fd 2857 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
a42d985b
BVA
2858 goto err_cm;
2859
2860 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
2861 srpt_event_handler);
dcc9881e 2862 ib_register_event_handler(&sdev->event_handler);
a42d985b 2863
f225066b 2864 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
a42d985b
BVA
2865
2866 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
2867 sport = &sdev->port[i - 1];
a1125314 2868 INIT_LIST_HEAD(&sport->nexus_list);
ba60c84f
BVA
2869 init_waitqueue_head(&sport->ch_releaseQ);
2870 mutex_init(&sport->mutex);
a42d985b
BVA
2871 sport->sdev = sdev;
2872 sport->port = i;
2873 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
2874 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
2875 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
dea26209 2876 sport->port_attrib.use_srq = false;
a42d985b 2877 INIT_WORK(&sport->work, srpt_refresh_port_work);
a42d985b
BVA
2878
2879 if (srpt_refresh_port(sport)) {
9f5d32af 2880 pr_err("MAD registration failed for %s-%d.\n",
f68cba4e 2881 sdev->device->name, i);
dea26209 2882 goto err_event;
a42d985b 2883 }
a42d985b
BVA
2884 }
2885
2886 spin_lock(&srpt_dev_lock);
2887 list_add_tail(&sdev->list, &srpt_dev_list);
2888 spin_unlock(&srpt_dev_lock);
2889
2890out:
2891 ib_set_client_data(device, &srpt_client, sdev);
2892 pr_debug("added %s.\n", device->name);
2893 return;
2894
a42d985b
BVA
2895err_event:
2896 ib_unregister_event_handler(&sdev->event_handler);
2897err_cm:
2898 ib_destroy_cm_id(sdev->cm_id);
dea26209 2899err_ring:
c76d7d64 2900 srpt_free_srq(sdev);
a42d985b
BVA
2901 ib_dealloc_pd(sdev->pd);
2902free_dev:
2903 kfree(sdev);
2904err:
2905 sdev = NULL;
9f5d32af 2906 pr_info("%s(%s) failed.\n", __func__, device->name);
a42d985b
BVA
2907 goto out;
2908}
2909
2910/**
10eac19b
BVA
2911 * srpt_remove_one - InfiniBand device removal callback function
2912 * @device: Describes a HCA.
2913 * @client_data: The value passed as the third argument to ib_set_client_data().
a42d985b 2914 */
7c1eb45a 2915static void srpt_remove_one(struct ib_device *device, void *client_data)
a42d985b 2916{
7c1eb45a 2917 struct srpt_device *sdev = client_data;
a42d985b
BVA
2918 int i;
2919
a42d985b 2920 if (!sdev) {
9f5d32af 2921 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
a42d985b
BVA
2922 return;
2923 }
2924
2925 srpt_unregister_mad_agent(sdev);
2926
2927 ib_unregister_event_handler(&sdev->event_handler);
2928
2929 /* Cancel any work queued by the just unregistered IB event handler. */
2930 for (i = 0; i < sdev->device->phys_port_cnt; i++)
2931 cancel_work_sync(&sdev->port[i].work);
2932
2933 ib_destroy_cm_id(sdev->cm_id);
2934
2935 /*
2936 * Unregistering a target must happen after destroying sdev->cm_id
2937 * such that no new SRP_LOGIN_REQ information units can arrive while
2938 * destroying the target.
2939 */
2940 spin_lock(&srpt_dev_lock);
2941 list_del(&sdev->list);
2942 spin_unlock(&srpt_dev_lock);
ba60c84f
BVA
2943
2944 for (i = 0; i < sdev->device->phys_port_cnt; i++)
2945 srpt_release_sport(&sdev->port[i]);
a42d985b 2946
c76d7d64
BVA
2947 srpt_free_srq(sdev);
2948
a42d985b
BVA
2949 ib_dealloc_pd(sdev->pd);
2950
a42d985b
BVA
2951 kfree(sdev);
2952}
2953
2954static struct ib_client srpt_client = {
2955 .name = DRV_NAME,
2956 .add = srpt_add_one,
2957 .remove = srpt_remove_one
2958};
2959
2960static int srpt_check_true(struct se_portal_group *se_tpg)
2961{
2962 return 1;
2963}
2964
2965static int srpt_check_false(struct se_portal_group *se_tpg)
2966{
2967 return 0;
2968}
2969
2970static char *srpt_get_fabric_name(void)
2971{
2972 return "srpt";
2973}
2974
2bce1a6d
BVA
2975static struct srpt_port *srpt_tpg_to_sport(struct se_portal_group *tpg)
2976{
2977 return tpg->se_tpg_wwn->priv;
2978}
2979
a42d985b
BVA
2980static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
2981{
2bce1a6d 2982 struct srpt_port *sport = srpt_tpg_to_sport(tpg);
a42d985b 2983
2bce1a6d
BVA
2984 WARN_ON_ONCE(tpg != &sport->port_guid_tpg &&
2985 tpg != &sport->port_gid_tpg);
2986 return tpg == &sport->port_guid_tpg ? sport->port_guid :
2987 sport->port_gid;
a42d985b
BVA
2988}
2989
2990static u16 srpt_get_tag(struct se_portal_group *tpg)
2991{
2992 return 1;
2993}
2994
a42d985b
BVA
2995static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
2996{
2997 return 1;
2998}
2999
3000static void srpt_release_cmd(struct se_cmd *se_cmd)
3001{
9474b043
NB
3002 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3003 struct srpt_send_ioctx, cmd);
3004 struct srpt_rdma_ch *ch = ioctx->ch;
3c968887 3005 unsigned long flags;
9474b043 3006
bd2c52d7
BVA
3007 WARN_ON_ONCE(ioctx->state != SRPT_STATE_DONE &&
3008 !(ioctx->cmd.transport_state & CMD_T_ABORTED));
9474b043 3009
b99f8e4d
CH
3010 if (ioctx->n_rw_ctx) {
3011 srpt_free_rw_ctxs(ch, ioctx);
3012 ioctx->n_rw_ctx = 0;
9474b043
NB
3013 }
3014
3c968887
BVA
3015 spin_lock_irqsave(&ch->spinlock, flags);
3016 list_add(&ioctx->free_list, &ch->free_list);
3017 spin_unlock_irqrestore(&ch->spinlock, flags);
a42d985b
BVA
3018}
3019
a42d985b 3020/**
10eac19b
BVA
3021 * srpt_close_session - forcibly close a session
3022 * @se_sess: SCSI target session.
a42d985b
BVA
3023 *
3024 * Callback function invoked by the TCM core to clean up sessions associated
3025 * with a node ACL when the user invokes
3026 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3027 */
3028static void srpt_close_session(struct se_session *se_sess)
3029{
f108f0f6 3030 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
a42d985b 3031
01b3ee13 3032 srpt_disconnect_ch_sync(ch);
a42d985b
BVA
3033}
3034
a42d985b 3035/**
10eac19b
BVA
3036 * srpt_sess_get_index - return the value of scsiAttIntrPortIndex (SCSI-MIB)
3037 * @se_sess: SCSI target session.
a42d985b
BVA
3038 *
3039 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3040 * This object represents an arbitrary integer used to uniquely identify a
3041 * particular attached remote initiator port to a particular SCSI target port
3042 * within a particular SCSI target device within a particular SCSI instance.
3043 */
3044static u32 srpt_sess_get_index(struct se_session *se_sess)
3045{
3046 return 0;
3047}
3048
3049static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3050{
3051}
3052
a42d985b
BVA
3053/* Note: only used from inside debug printk's by the TCM core. */
3054static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3055{
3056 struct srpt_send_ioctx *ioctx;
3057
3058 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
dd3bec86 3059 return ioctx->state;
a42d985b
BVA
3060}
3061
2bce1a6d
BVA
3062static int srpt_parse_guid(u64 *guid, const char *name)
3063{
3064 u16 w[4];
3065 int ret = -EINVAL;
3066
3067 if (sscanf(name, "%hx:%hx:%hx:%hx", &w[0], &w[1], &w[2], &w[3]) != 4)
3068 goto out;
3069 *guid = get_unaligned_be64(w);
3070 ret = 0;
3071out:
3072 return ret;
3073}
3074
a42d985b 3075/**
10eac19b 3076 * srpt_parse_i_port_id - parse an initiator port ID
a42d985b
BVA
3077 * @name: ASCII representation of a 128-bit initiator port ID.
3078 * @i_port_id: Binary 128-bit port ID.
3079 */
3080static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3081{
3082 const char *p;
3083 unsigned len, count, leading_zero_bytes;
c70ca389 3084 int ret;
a42d985b
BVA
3085
3086 p = name;
b60459f0 3087 if (strncasecmp(p, "0x", 2) == 0)
a42d985b
BVA
3088 p += 2;
3089 ret = -EINVAL;
3090 len = strlen(p);
3091 if (len % 2)
3092 goto out;
3093 count = min(len / 2, 16U);
3094 leading_zero_bytes = 16 - count;
3095 memset(i_port_id, 0, leading_zero_bytes);
c70ca389
BVA
3096 ret = hex2bin(i_port_id + leading_zero_bytes, p, count);
3097 if (ret < 0)
3098 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", ret);
a42d985b
BVA
3099out:
3100 return ret;
3101}
3102
3103/*
3104 * configfs callback function invoked for
3105 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3106 */
c7d6a803 3107static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
a42d985b 3108{
2bce1a6d 3109 u64 guid;
a42d985b 3110 u8 i_port_id[16];
2bce1a6d 3111 int ret;
a42d985b 3112
2bce1a6d
BVA
3113 ret = srpt_parse_guid(&guid, name);
3114 if (ret < 0)
3115 ret = srpt_parse_i_port_id(i_port_id, name);
3116 if (ret < 0)
9f5d32af 3117 pr_err("invalid initiator port ID %s\n", name);
2bce1a6d 3118 return ret;
a42d985b
BVA
3119}
3120
2eafd729
CH
3121static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
3122 char *page)
a42d985b 3123{
2eafd729 3124 struct se_portal_group *se_tpg = attrib_to_tpg(item);
2bce1a6d 3125 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
a42d985b
BVA
3126
3127 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3128}
3129
2eafd729
CH
3130static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
3131 const char *page, size_t count)
a42d985b 3132{
2eafd729 3133 struct se_portal_group *se_tpg = attrib_to_tpg(item);
2bce1a6d 3134 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
a42d985b
BVA
3135 unsigned long val;
3136 int ret;
3137
9d8abf45 3138 ret = kstrtoul(page, 0, &val);
a42d985b 3139 if (ret < 0) {
9d8abf45 3140 pr_err("kstrtoul() failed with ret: %d\n", ret);
a42d985b
BVA
3141 return -EINVAL;
3142 }
3143 if (val > MAX_SRPT_RDMA_SIZE) {
3144 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3145 MAX_SRPT_RDMA_SIZE);
3146 return -EINVAL;
3147 }
3148 if (val < DEFAULT_MAX_RDMA_SIZE) {
3149 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3150 val, DEFAULT_MAX_RDMA_SIZE);
3151 return -EINVAL;
3152 }
3153 sport->port_attrib.srp_max_rdma_size = val;
3154
3155 return count;
3156}
3157
2eafd729
CH
3158static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
3159 char *page)
a42d985b 3160{
2eafd729 3161 struct se_portal_group *se_tpg = attrib_to_tpg(item);
2bce1a6d 3162 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
a42d985b
BVA
3163
3164 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3165}
3166
2eafd729
CH
3167static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
3168 const char *page, size_t count)
a42d985b 3169{
2eafd729 3170 struct se_portal_group *se_tpg = attrib_to_tpg(item);
2bce1a6d 3171 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
a42d985b
BVA
3172 unsigned long val;
3173 int ret;
3174
9d8abf45 3175 ret = kstrtoul(page, 0, &val);
a42d985b 3176 if (ret < 0) {
9d8abf45 3177 pr_err("kstrtoul() failed with ret: %d\n", ret);
a42d985b
BVA
3178 return -EINVAL;
3179 }
3180 if (val > MAX_SRPT_RSP_SIZE) {
3181 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3182 MAX_SRPT_RSP_SIZE);
3183 return -EINVAL;
3184 }
3185 if (val < MIN_MAX_RSP_SIZE) {
3186 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3187 MIN_MAX_RSP_SIZE);
3188 return -EINVAL;
3189 }
3190 sport->port_attrib.srp_max_rsp_size = val;
3191
3192 return count;
3193}
3194
2eafd729
CH
3195static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
3196 char *page)
a42d985b 3197{
2eafd729 3198 struct se_portal_group *se_tpg = attrib_to_tpg(item);
2bce1a6d 3199 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
a42d985b
BVA
3200
3201 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3202}
3203
2eafd729
CH
3204static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
3205 const char *page, size_t count)
a42d985b 3206{
2eafd729 3207 struct se_portal_group *se_tpg = attrib_to_tpg(item);
2bce1a6d 3208 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
a42d985b
BVA
3209 unsigned long val;
3210 int ret;
3211
9d8abf45 3212 ret = kstrtoul(page, 0, &val);
a42d985b 3213 if (ret < 0) {
9d8abf45 3214 pr_err("kstrtoul() failed with ret: %d\n", ret);
a42d985b
BVA
3215 return -EINVAL;
3216 }
3217 if (val > MAX_SRPT_SRQ_SIZE) {
3218 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3219 MAX_SRPT_SRQ_SIZE);
3220 return -EINVAL;
3221 }
3222 if (val < MIN_SRPT_SRQ_SIZE) {
3223 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3224 MIN_SRPT_SRQ_SIZE);
3225 return -EINVAL;
3226 }
3227 sport->port_attrib.srp_sq_size = val;
3228
3229 return count;
3230}
3231
dea26209
BVA
3232static ssize_t srpt_tpg_attrib_use_srq_show(struct config_item *item,
3233 char *page)
3234{
3235 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3236 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3237
3238 return sprintf(page, "%d\n", sport->port_attrib.use_srq);
3239}
3240
3241static ssize_t srpt_tpg_attrib_use_srq_store(struct config_item *item,
3242 const char *page, size_t count)
3243{
3244 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3245 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
57b0c460 3246 struct srpt_device *sdev = sport->sdev;
dea26209 3247 unsigned long val;
57b0c460 3248 bool enabled;
dea26209
BVA
3249 int ret;
3250
3251 ret = kstrtoul(page, 0, &val);
3252 if (ret < 0)
3253 return ret;
3254 if (val != !!val)
3255 return -EINVAL;
57b0c460 3256
ba60c84f 3257 ret = mutex_lock_interruptible(&sdev->sdev_mutex);
57b0c460
BVA
3258 if (ret < 0)
3259 return ret;
ba60c84f
BVA
3260 ret = mutex_lock_interruptible(&sport->mutex);
3261 if (ret < 0)
3262 goto unlock_sdev;
57b0c460
BVA
3263 enabled = sport->enabled;
3264 /* Log out all initiator systems before changing 'use_srq'. */
3265 srpt_set_enabled(sport, false);
dea26209 3266 sport->port_attrib.use_srq = val;
57b0c460
BVA
3267 srpt_use_srq(sdev, sport->port_attrib.use_srq);
3268 srpt_set_enabled(sport, enabled);
ba60c84f
BVA
3269 ret = count;
3270 mutex_unlock(&sport->mutex);
3271unlock_sdev:
3272 mutex_unlock(&sdev->sdev_mutex);
dea26209 3273
ba60c84f 3274 return ret;
dea26209
BVA
3275}
3276
2eafd729
CH
3277CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
3278CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
3279CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
dea26209 3280CONFIGFS_ATTR(srpt_tpg_attrib_, use_srq);
a42d985b
BVA
3281
3282static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
2eafd729
CH
3283 &srpt_tpg_attrib_attr_srp_max_rdma_size,
3284 &srpt_tpg_attrib_attr_srp_max_rsp_size,
3285 &srpt_tpg_attrib_attr_srp_sq_size,
dea26209 3286 &srpt_tpg_attrib_attr_use_srq,
a42d985b
BVA
3287 NULL,
3288};
3289
2eafd729 3290static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
a42d985b 3291{
2eafd729 3292 struct se_portal_group *se_tpg = to_tpg(item);
2bce1a6d 3293 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
a42d985b
BVA
3294
3295 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3296}
3297
2eafd729
CH
3298static ssize_t srpt_tpg_enable_store(struct config_item *item,
3299 const char *page, size_t count)
a42d985b 3300{
2eafd729 3301 struct se_portal_group *se_tpg = to_tpg(item);
2bce1a6d 3302 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
a42d985b
BVA
3303 unsigned long tmp;
3304 int ret;
3305
9d8abf45 3306 ret = kstrtoul(page, 0, &tmp);
a42d985b 3307 if (ret < 0) {
9f5d32af 3308 pr_err("Unable to extract srpt_tpg_store_enable\n");
a42d985b
BVA
3309 return -EINVAL;
3310 }
3311
3312 if ((tmp != 0) && (tmp != 1)) {
9f5d32af 3313 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
a42d985b
BVA
3314 return -EINVAL;
3315 }
a42d985b 3316
ba60c84f 3317 mutex_lock(&sport->mutex);
8b6dc529 3318 srpt_set_enabled(sport, tmp);
ba60c84f 3319 mutex_unlock(&sport->mutex);
043a6806 3320
a42d985b
BVA
3321 return count;
3322}
3323
2eafd729 3324CONFIGFS_ATTR(srpt_tpg_, enable);
a42d985b
BVA
3325
3326static struct configfs_attribute *srpt_tpg_attrs[] = {
2eafd729 3327 &srpt_tpg_attr_enable,
a42d985b
BVA
3328 NULL,
3329};
3330
3331/**
10eac19b
BVA
3332 * srpt_make_tpg - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port/$tpg
3333 * @wwn: Corresponds to $driver/$port.
3334 * @group: Not used.
3335 * @name: $tpg.
a42d985b
BVA
3336 */
3337static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3338 struct config_group *group,
3339 const char *name)
3340{
2bce1a6d
BVA
3341 struct srpt_port *sport = wwn->priv;
3342 static struct se_portal_group *tpg;
a42d985b
BVA
3343 int res;
3344
2bce1a6d
BVA
3345 WARN_ON_ONCE(wwn != &sport->port_guid_wwn &&
3346 wwn != &sport->port_gid_wwn);
3347 tpg = wwn == &sport->port_guid_wwn ? &sport->port_guid_tpg :
3348 &sport->port_gid_tpg;
3349 res = core_tpg_register(wwn, tpg, SCSI_PROTOCOL_SRP);
a42d985b
BVA
3350 if (res)
3351 return ERR_PTR(res);
3352
2bce1a6d 3353 return tpg;
a42d985b
BVA
3354}
3355
3356/**
10eac19b
BVA
3357 * srpt_drop_tpg - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port/$tpg
3358 * @tpg: Target portal group to deregister.
a42d985b
BVA
3359 */
3360static void srpt_drop_tpg(struct se_portal_group *tpg)
3361{
2bce1a6d 3362 struct srpt_port *sport = srpt_tpg_to_sport(tpg);
a42d985b
BVA
3363
3364 sport->enabled = false;
2bce1a6d 3365 core_tpg_deregister(tpg);
a42d985b
BVA
3366}
3367
3368/**
10eac19b
BVA
3369 * srpt_make_tport - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port
3370 * @tf: Not used.
3371 * @group: Not used.
3372 * @name: $port.
a42d985b
BVA
3373 */
3374static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3375 struct config_group *group,
3376 const char *name)
3377{
2bce1a6d 3378 return srpt_lookup_wwn(name) ? : ERR_PTR(-EINVAL);
a42d985b
BVA
3379}
3380
3381/**
10eac19b
BVA
3382 * srpt_drop_tport - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port
3383 * @wwn: $port.
a42d985b
BVA
3384 */
3385static void srpt_drop_tport(struct se_wwn *wwn)
3386{
a42d985b
BVA
3387}
3388
2eafd729 3389static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
a42d985b
BVA
3390{
3391 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3392}
3393
2eafd729 3394CONFIGFS_ATTR_RO(srpt_wwn_, version);
a42d985b
BVA
3395
3396static struct configfs_attribute *srpt_wwn_attrs[] = {
2eafd729 3397 &srpt_wwn_attr_version,
a42d985b
BVA
3398 NULL,
3399};
3400
9ac8928e
CH
3401static const struct target_core_fabric_ops srpt_template = {
3402 .module = THIS_MODULE,
3403 .name = "srpt",
a42d985b 3404 .get_fabric_name = srpt_get_fabric_name,
a42d985b
BVA
3405 .tpg_get_wwn = srpt_get_fabric_wwn,
3406 .tpg_get_tag = srpt_get_tag,
a42d985b
BVA
3407 .tpg_check_demo_mode = srpt_check_false,
3408 .tpg_check_demo_mode_cache = srpt_check_true,
3409 .tpg_check_demo_mode_write_protect = srpt_check_true,
3410 .tpg_check_prod_mode_write_protect = srpt_check_false,
a42d985b
BVA
3411 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3412 .release_cmd = srpt_release_cmd,
3413 .check_stop_free = srpt_check_stop_free,
a42d985b 3414 .close_session = srpt_close_session,
a42d985b
BVA
3415 .sess_get_index = srpt_sess_get_index,
3416 .sess_get_initiator_sid = NULL,
3417 .write_pending = srpt_write_pending,
3418 .write_pending_status = srpt_write_pending_status,
3419 .set_default_node_attributes = srpt_set_default_node_attrs,
a42d985b 3420 .get_cmd_state = srpt_get_tcm_cmd_state,
b79fafac 3421 .queue_data_in = srpt_queue_data_in,
a42d985b 3422 .queue_status = srpt_queue_status,
b79fafac 3423 .queue_tm_rsp = srpt_queue_tm_rsp,
131e6abc 3424 .aborted_task = srpt_aborted_task,
a42d985b
BVA
3425 /*
3426 * Setup function pointers for generic logic in
3427 * target_core_fabric_configfs.c
3428 */
3429 .fabric_make_wwn = srpt_make_tport,
3430 .fabric_drop_wwn = srpt_drop_tport,
3431 .fabric_make_tpg = srpt_make_tpg,
3432 .fabric_drop_tpg = srpt_drop_tpg,
c7d6a803 3433 .fabric_init_nodeacl = srpt_init_nodeacl,
9ac8928e
CH
3434
3435 .tfc_wwn_attrs = srpt_wwn_attrs,
3436 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3437 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
a42d985b
BVA
3438};
3439
3440/**
10eac19b 3441 * srpt_init_module - kernel module initialization
a42d985b
BVA
3442 *
3443 * Note: Since ib_register_client() registers callback functions, and since at
3444 * least one of these callback functions (srpt_add_one()) calls target core
3445 * functions, this driver must be registered with the target core before
3446 * ib_register_client() is called.
3447 */
3448static int __init srpt_init_module(void)
3449{
3450 int ret;
3451
3452 ret = -EINVAL;
3453 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
9f5d32af 3454 pr_err("invalid value %d for kernel module parameter"
a42d985b
BVA
3455 " srp_max_req_size -- must be at least %d.\n",
3456 srp_max_req_size, MIN_MAX_REQ_SIZE);
3457 goto out;
3458 }
3459
3460 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3461 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
9f5d32af 3462 pr_err("invalid value %d for kernel module parameter"
a42d985b
BVA
3463 " srpt_srq_size -- must be in the range [%d..%d].\n",
3464 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3465 goto out;
3466 }
3467
9ac8928e
CH
3468 ret = target_register_template(&srpt_template);
3469 if (ret)
a42d985b 3470 goto out;
a42d985b
BVA
3471
3472 ret = ib_register_client(&srpt_client);
3473 if (ret) {
9f5d32af 3474 pr_err("couldn't register IB client\n");
a42d985b
BVA
3475 goto out_unregister_target;
3476 }
3477
3478 return 0;
3479
3480out_unregister_target:
9ac8928e 3481 target_unregister_template(&srpt_template);
a42d985b
BVA
3482out:
3483 return ret;
3484}
3485
3486static void __exit srpt_cleanup_module(void)
3487{
3488 ib_unregister_client(&srpt_client);
9ac8928e 3489 target_unregister_template(&srpt_template);
a42d985b
BVA
3490}
3491
3492module_init(srpt_init_module);
3493module_exit(srpt_cleanup_module);