1 /*
2  * Copyright (c) 2006 - 2009 Mellanox Technology Inc.  All rights reserved.
3  * Copyright (C) 2009 - 2010 Bart Van Assche <bvanassche@acm.org>.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  *
33  */
34 
35 #ifndef IB_SRPT_H
36 #define IB_SRPT_H
37 
38 #include <linux/types.h>
39 #include <linux/list.h>
40 #include <linux/wait.h>
41 
42 #include <rdma/ib_verbs.h>
43 #include <rdma/ib_sa.h>
44 #include <rdma/ib_cm.h>
45 #include <rdma/rw.h>
46 
47 #include <scsi/srp.h>
48 
49 #include "ib_dm_mad.h"
50 
51 /*
52  * The prefix the ServiceName field must start with in the device management
53  * ServiceEntries attribute pair. See also the SRP specification.
54  */
55 #define SRP_SERVICE_NAME_PREFIX		"SRP.T10:"
56 
57 struct srpt_nexus;
58 
59 enum {
60 	/*
61 	 * SRP IOControllerProfile attributes for SRP target ports that have
62 	 * not been defined in <scsi/srp.h>. Source: section B.7, table B.7
63 	 * in the SRP specification.
64 	 */
65 	SRP_PROTOCOL = 0x0108,
66 	SRP_PROTOCOL_VERSION = 0x0001,
67 	SRP_IO_SUBCLASS = 0x609e,
68 	SRP_SEND_TO_IOC = 0x01,
69 	SRP_SEND_FROM_IOC = 0x02,
70 	SRP_RDMA_READ_FROM_IOC = 0x08,
71 	SRP_RDMA_WRITE_FROM_IOC = 0x20,
72 
73 	/*
74 	 * srp_login_cmd.req_flags bitmasks. See also table 9 in the SRP
75 	 * specification.
76 	 */
77 	SRP_MTCH_ACTION = 0x03, /* MULTI-CHANNEL ACTION */
78 	SRP_LOSOLNT = 0x10, /* logout solicited notification */
79 	SRP_CRSOLNT = 0x20, /* credit request solicited notification */
80 	SRP_AESOLNT = 0x40, /* asynchronous event solicited notification */
81 
82 	/*
83 	 * srp_cmd.sol_nt / srp_tsk_mgmt.sol_not bitmasks. See also tables
84 	 * 18 and 20 in the SRP specification.
85 	 */
86 	SRP_SCSOLNT = 0x02, /* SCSOLNT = successful solicited notification */
87 	SRP_UCSOLNT = 0x04, /* UCSOLNT = unsuccessful solicited notification */
88 
89 	/*
90 	 * srp_rsp.sol_not / srp_t_logout.sol_not bitmasks. See also tables
91 	 * 16 and 22 in the SRP specification.
92 	 */
93 	SRP_SOLNT = 0x01, /* SOLNT = solicited notification */
94 
95 	/* See also table 24 in the SRP specification. */
96 	SRP_TSK_MGMT_SUCCESS = 0x00,
97 	SRP_TSK_MGMT_FUNC_NOT_SUPP = 0x04,
98 	SRP_TSK_MGMT_FAILED = 0x05,
99 
100 	/* See also table 21 in the SRP specification. */
101 	SRP_CMD_SIMPLE_Q = 0x0,
102 	SRP_CMD_HEAD_OF_Q = 0x1,
103 	SRP_CMD_ORDERED_Q = 0x2,
104 	SRP_CMD_ACA = 0x4,
105 
106 	SRP_LOGIN_RSP_MULTICHAN_NO_CHAN = 0x0,
107 	SRP_LOGIN_RSP_MULTICHAN_TERMINATED = 0x1,
108 	SRP_LOGIN_RSP_MULTICHAN_MAINTAINED = 0x2,
109 
110 	SRPT_DEF_SG_TABLESIZE = 128,
111 	/*
112 	 * An experimentally determined value that avoids that QP creation
113 	 * fails due to "swiotlb buffer is full" on systems using the swiotlb.
114 	 */
115 	SRPT_MAX_SG_PER_WQE = 16,
116 
117 	MIN_SRPT_SQ_SIZE = 16,
118 	DEF_SRPT_SQ_SIZE = 4096,
119 	MAX_SRPT_RQ_SIZE = 128,
120 	MIN_SRPT_SRQ_SIZE = 4,
121 	DEFAULT_SRPT_SRQ_SIZE = 4095,
122 	MAX_SRPT_SRQ_SIZE = 65535,
123 	MAX_SRPT_RDMA_SIZE = 1U << 24,
124 	MAX_SRPT_RSP_SIZE = 1024,
125 
126 	MIN_MAX_REQ_SIZE = 996,
127 	DEFAULT_MAX_REQ_SIZE
128 		= sizeof(struct srp_cmd)/*48*/
129 		+ sizeof(struct srp_indirect_buf)/*20*/
130 		+ 128 * sizeof(struct srp_direct_buf)/*16*/,
131 
132 	MIN_MAX_RSP_SIZE = sizeof(struct srp_rsp)/*36*/ + 4,
133 	DEFAULT_MAX_RSP_SIZE = 256, /* leaves 220 bytes for sense data */
134 
135 	DEFAULT_MAX_RDMA_SIZE = 65536,
136 };
137 
138 /**
139  * enum srpt_command_state - SCSI command state managed by SRPT
140  * @SRPT_STATE_NEW:           New command arrived and is being processed.
141  * @SRPT_STATE_NEED_DATA:     Processing a write or bidir command and waiting
142  *                            for data arrival.
143  * @SRPT_STATE_DATA_IN:       Data for the write or bidir command arrived and is
144  *                            being processed.
145  * @SRPT_STATE_CMD_RSP_SENT:  SRP_RSP for SRP_CMD has been sent.
146  * @SRPT_STATE_MGMT:          Processing a SCSI task management command.
147  * @SRPT_STATE_MGMT_RSP_SENT: SRP_RSP for SRP_TSK_MGMT has been sent.
148  * @SRPT_STATE_DONE:          Command processing finished successfully, command
149  *                            processing has been aborted or command processing
150  *                            failed.
151  */
152 enum srpt_command_state {
153 	SRPT_STATE_NEW		 = 0,
154 	SRPT_STATE_NEED_DATA	 = 1,
155 	SRPT_STATE_DATA_IN	 = 2,
156 	SRPT_STATE_CMD_RSP_SENT	 = 3,
157 	SRPT_STATE_MGMT		 = 4,
158 	SRPT_STATE_MGMT_RSP_SENT = 5,
159 	SRPT_STATE_DONE		 = 6,
160 };
161 
162 /**
163  * struct srpt_ioctx - shared SRPT I/O context information
164  * @cqe:   Completion queue element.
165  * @buf:   Pointer to the buffer.
166  * @dma:   DMA address of the buffer.
167  * @index: Index of the I/O context in its ioctx_ring array.
168  */
169 struct srpt_ioctx {
170 	struct ib_cqe		cqe;
171 	void			*buf;
172 	dma_addr_t		dma;
173 	uint32_t		index;
174 };
175 
176 /**
177  * struct srpt_recv_ioctx - SRPT receive I/O context
178  * @ioctx:     See above.
179  * @wait_list: Node for insertion in srpt_rdma_ch.cmd_wait_list.
180  */
181 struct srpt_recv_ioctx {
182 	struct srpt_ioctx	ioctx;
183 	struct list_head	wait_list;
184 };
185 
186 struct srpt_rw_ctx {
187 	struct rdma_rw_ctx	rw;
188 	struct scatterlist	*sg;
189 	unsigned int		nents;
190 };
191 
192 /**
193  * struct srpt_send_ioctx - SRPT send I/O context
194  * @ioctx:       See above.
195  * @ch:          Channel pointer.
196  * @s_rw_ctx:    @rw_ctxs points here if only a single rw_ctx is needed.
197  * @rw_ctxs:     RDMA read/write contexts.
198  * @rdma_cqe:    RDMA completion queue element.
199  * @free_list:   Node in srpt_rdma_ch.free_list.
200  * @state:       I/O context state.
201  * @cmd:         Target core command data structure.
202  * @sense_data:  SCSI sense data.
203  * @n_rdma:      Number of work requests needed to transfer this ioctx.
204  * @n_rw_ctx:    Size of rw_ctxs array.
205  * @queue_status_only: Send a SCSI status back to the initiator but no data.
206  * @sense_data:  Sense data to be sent to the initiator.
207  */
208 struct srpt_send_ioctx {
209 	struct srpt_ioctx	ioctx;
210 	struct srpt_rdma_ch	*ch;
211 
212 	struct srpt_rw_ctx	s_rw_ctx;
213 	struct srpt_rw_ctx	*rw_ctxs;
214 
215 	struct ib_cqe		rdma_cqe;
216 	struct list_head	free_list;
217 	enum srpt_command_state	state;
218 	struct se_cmd		cmd;
219 	u8			n_rdma;
220 	u8			n_rw_ctx;
221 	bool			queue_status_only;
222 	u8			sense_data[TRANSPORT_SENSE_BUFFER];
223 };
224 
225 /**
226  * enum rdma_ch_state - SRP channel state
227  * @CH_CONNECTING:    QP is in RTR state; waiting for RTU.
228  * @CH_LIVE:	      QP is in RTS state.
229  * @CH_DISCONNECTING: DREQ has been sent and waiting for DREP or DREQ has
230  *                    been received.
231  * @CH_DRAINING:      DREP has been received or waiting for DREP timed out
232  *                    and last work request has been queued.
233  * @CH_DISCONNECTED:  Last completion has been received.
234  */
235 enum rdma_ch_state {
236 	CH_CONNECTING,
237 	CH_LIVE,
238 	CH_DISCONNECTING,
239 	CH_DRAINING,
240 	CH_DISCONNECTED,
241 };
242 
243 /**
244  * struct srpt_rdma_ch - RDMA channel
245  * @nexus:         I_T nexus this channel is associated with.
246  * @qp:            IB queue pair used for communicating over this channel.
247  * @cm_id:         IB CM ID associated with the channel.
248  * @cq:            IB completion queue for this channel.
249  * @zw_cqe:	   Zero-length write CQE.
250  * @rcu:           RCU head.
251  * @kref:	   kref for this channel.
252  * @rq_size:       IB receive queue size.
253  * @max_rsp_size:  Maximum size of an RSP response message in bytes.
254  * @sq_wr_avail:   number of work requests available in the send queue.
255  * @sport:         pointer to the information of the HCA port used by this
256  *                 channel.
257  * @max_ti_iu_len: maximum target-to-initiator information unit length.
258  * @req_lim:       request limit: maximum number of requests that may be sent
259  *                 by the initiator without having received a response.
260  * @req_lim_delta: Number of credits not yet sent back to the initiator.
261  * @spinlock:      Protects free_list and state.
262  * @free_list:     Head of list with free send I/O contexts.
263  * @state:         channel state. See also enum rdma_ch_state.
264  * @processing_wait_list: Whether or not cmd_wait_list is being processed.
265  * @ioctx_ring:    Send ring.
266  * @ioctx_recv_ring: Receive I/O context ring.
267  * @list:          Node in srpt_nexus.ch_list.
268  * @cmd_wait_list: List of SCSI commands that arrived before the RTU event. This
269  *                 list contains struct srpt_ioctx elements and is protected
270  *                 against concurrent modification by the cm_id spinlock.
271  * @pkey:          P_Key of the IB partition for this SRP channel.
272  * @sess:          Session information associated with this SRP channel.
273  * @sess_name:     Session name.
274  * @release_work:  Allows scheduling of srpt_release_channel().
275  */
276 struct srpt_rdma_ch {
277 	struct srpt_nexus	*nexus;
278 	struct ib_qp		*qp;
279 	union {
280 		struct {
281 			struct ib_cm_id		*cm_id;
282 		} ib_cm;
283 	};
284 	struct ib_cq		*cq;
285 	struct ib_cqe		zw_cqe;
286 	struct rcu_head		rcu;
287 	struct kref		kref;
288 	int			rq_size;
289 	u32			max_rsp_size;
290 	atomic_t		sq_wr_avail;
291 	struct srpt_port	*sport;
292 	int			max_ti_iu_len;
293 	atomic_t		req_lim;
294 	atomic_t		req_lim_delta;
295 	spinlock_t		spinlock;
296 	struct list_head	free_list;
297 	enum rdma_ch_state	state;
298 	struct srpt_send_ioctx	**ioctx_ring;
299 	struct srpt_recv_ioctx	**ioctx_recv_ring;
300 	struct list_head	list;
301 	struct list_head	cmd_wait_list;
302 	uint16_t		pkey;
303 	bool			processing_wait_list;
304 	struct se_session	*sess;
305 	u8			sess_name[24];
306 	struct work_struct	release_work;
307 };
308 
309 /**
310  * struct srpt_nexus - I_T nexus
311  * @rcu:       RCU head for this data structure.
312  * @entry:     srpt_port.nexus_list list node.
313  * @ch_list:   struct srpt_rdma_ch list. Protected by srpt_port.mutex.
314  * @i_port_id: 128-bit initiator port identifier copied from SRP_LOGIN_REQ.
315  * @t_port_id: 128-bit target port identifier copied from SRP_LOGIN_REQ.
316  */
317 struct srpt_nexus {
318 	struct rcu_head		rcu;
319 	struct list_head	entry;
320 	struct list_head	ch_list;
321 	u8			i_port_id[16];
322 	u8			t_port_id[16];
323 };
324 
325 /**
326  * struct srpt_port_attib - attributes for SRPT port
327  * @srp_max_rdma_size: Maximum size of SRP RDMA transfers for new connections.
328  * @srp_max_rsp_size: Maximum size of SRP response messages in bytes.
329  * @srp_sq_size: Shared receive queue (SRQ) size.
330  * @use_srq: Whether or not to use SRQ.
331  */
332 struct srpt_port_attrib {
333 	u32			srp_max_rdma_size;
334 	u32			srp_max_rsp_size;
335 	u32			srp_sq_size;
336 	bool			use_srq;
337 };
338 
339 /**
340  * struct srpt_port - information associated by SRPT with a single IB port
341  * @sdev:      backpointer to the HCA information.
342  * @mad_agent: per-port management datagram processing information.
343  * @enabled:   Whether or not this target port is enabled.
344  * @port_guid: ASCII representation of Port GUID
345  * @port_gid:  ASCII representation of Port GID
346  * @port:      one-based port number.
347  * @sm_lid:    cached value of the port's sm_lid.
348  * @lid:       cached value of the port's lid.
349  * @gid:       cached value of the port's gid.
350  * @port_acl_lock spinlock for port_acl_list:
351  * @work:      work structure for refreshing the aforementioned cached values.
352  * @port_guid_tpg: TPG associated with target port GUID.
353  * @port_guid_wwn: WWN associated with target port GUID.
354  * @port_gid_tpg:  TPG associated with target port GID.
355  * @port_gid_wwn:  WWN associated with target port GID.
356  * @port_attrib:   Port attributes that can be accessed through configfs.
357  * @ch_releaseQ:   Enables waiting for removal from nexus_list.
358  * @mutex:	   Protects nexus_list.
359  * @nexus_list:	   Nexus list. See also srpt_nexus.entry.
360  */
361 struct srpt_port {
362 	struct srpt_device	*sdev;
363 	struct ib_mad_agent	*mad_agent;
364 	bool			enabled;
365 	u8			port_guid[24];
366 	u8			port_gid[64];
367 	u8			port;
368 	u32			sm_lid;
369 	u32			lid;
370 	union ib_gid		gid;
371 	struct work_struct	work;
372 	struct se_portal_group	port_guid_tpg;
373 	struct se_wwn		port_guid_wwn;
374 	struct se_portal_group	port_gid_tpg;
375 	struct se_wwn		port_gid_wwn;
376 	struct srpt_port_attrib port_attrib;
377 	wait_queue_head_t	ch_releaseQ;
378 	struct mutex		mutex;
379 	struct list_head	nexus_list;
380 };
381 
382 /**
383  * struct srpt_device - information associated by SRPT with a single HCA
384  * @device:        Backpointer to the struct ib_device managed by the IB core.
385  * @pd:            IB protection domain.
386  * @lkey:          L_Key (local key) with write access to all local memory.
387  * @srq:           Per-HCA SRQ (shared receive queue).
388  * @cm_id:         Connection identifier.
389  * @srq_size:      SRQ size.
390  * @sdev_mutex:	   Serializes use_srq changes.
391  * @use_srq:       Whether or not to use SRQ.
392  * @ioctx_ring:    Per-HCA SRQ.
393  * @port:          Information about the ports owned by this HCA.
394  * @event_handler: Per-HCA asynchronous IB event handler.
395  * @list:          Node in srpt_dev_list.
396  */
397 struct srpt_device {
398 	struct ib_device	*device;
399 	struct ib_pd		*pd;
400 	u32			lkey;
401 	struct ib_srq		*srq;
402 	struct ib_cm_id		*cm_id;
403 	int			srq_size;
404 	struct mutex		sdev_mutex;
405 	bool			use_srq;
406 	struct srpt_recv_ioctx	**ioctx_ring;
407 	struct srpt_port	port[2];
408 	struct ib_event_handler	event_handler;
409 	struct list_head	list;
410 };
411 
412 #endif				/* IB_SRPT_H */
413