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