xref: /linux/drivers/infiniband/ulp/srpt/ib_srpt.h (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
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 
46 #include <scsi/srp.h>
47 
48 #include "ib_dm_mad.h"
49 
50 /*
51  * The prefix the ServiceName field must start with in the device management
52  * ServiceEntries attribute pair. See also the SRP specification.
53  */
54 #define SRP_SERVICE_NAME_PREFIX		"SRP.T10:"
55 
56 enum {
57 	/*
58 	 * SRP IOControllerProfile attributes for SRP target ports that have
59 	 * not been defined in <scsi/srp.h>. Source: section B.7, table B.7
60 	 * in the SRP specification.
61 	 */
62 	SRP_PROTOCOL = 0x0108,
63 	SRP_PROTOCOL_VERSION = 0x0001,
64 	SRP_IO_SUBCLASS = 0x609e,
65 	SRP_SEND_TO_IOC = 0x01,
66 	SRP_SEND_FROM_IOC = 0x02,
67 	SRP_RDMA_READ_FROM_IOC = 0x08,
68 	SRP_RDMA_WRITE_FROM_IOC = 0x20,
69 
70 	/*
71 	 * srp_login_cmd.req_flags bitmasks. See also table 9 in the SRP
72 	 * specification.
73 	 */
74 	SRP_MTCH_ACTION = 0x03, /* MULTI-CHANNEL ACTION */
75 	SRP_LOSOLNT = 0x10, /* logout solicited notification */
76 	SRP_CRSOLNT = 0x20, /* credit request solicited notification */
77 	SRP_AESOLNT = 0x40, /* asynchronous event solicited notification */
78 
79 	/*
80 	 * srp_cmd.sol_nt / srp_tsk_mgmt.sol_not bitmasks. See also tables
81 	 * 18 and 20 in the SRP specification.
82 	 */
83 	SRP_SCSOLNT = 0x02, /* SCSOLNT = successful solicited notification */
84 	SRP_UCSOLNT = 0x04, /* UCSOLNT = unsuccessful solicited notification */
85 
86 	/*
87 	 * srp_rsp.sol_not / srp_t_logout.sol_not bitmasks. See also tables
88 	 * 16 and 22 in the SRP specification.
89 	 */
90 	SRP_SOLNT = 0x01, /* SOLNT = solicited notification */
91 
92 	/* See also table 24 in the SRP specification. */
93 	SRP_TSK_MGMT_SUCCESS = 0x00,
94 	SRP_TSK_MGMT_FUNC_NOT_SUPP = 0x04,
95 	SRP_TSK_MGMT_FAILED = 0x05,
96 
97 	/* See also table 21 in the SRP specification. */
98 	SRP_CMD_SIMPLE_Q = 0x0,
99 	SRP_CMD_HEAD_OF_Q = 0x1,
100 	SRP_CMD_ORDERED_Q = 0x2,
101 	SRP_CMD_ACA = 0x4,
102 
103 	SRP_LOGIN_RSP_MULTICHAN_NO_CHAN = 0x0,
104 	SRP_LOGIN_RSP_MULTICHAN_TERMINATED = 0x1,
105 	SRP_LOGIN_RSP_MULTICHAN_MAINTAINED = 0x2,
106 
107 	SRPT_DEF_SG_TABLESIZE = 128,
108 	SRPT_DEF_SG_PER_WQE = 16,
109 
110 	MIN_SRPT_SQ_SIZE = 16,
111 	DEF_SRPT_SQ_SIZE = 4096,
112 	SRPT_RQ_SIZE = 128,
113 	MIN_SRPT_SRQ_SIZE = 4,
114 	DEFAULT_SRPT_SRQ_SIZE = 4095,
115 	MAX_SRPT_SRQ_SIZE = 65535,
116 	MAX_SRPT_RDMA_SIZE = 1U << 24,
117 	MAX_SRPT_RSP_SIZE = 1024,
118 
119 	MIN_MAX_REQ_SIZE = 996,
120 	DEFAULT_MAX_REQ_SIZE
121 		= sizeof(struct srp_cmd)/*48*/
122 		+ sizeof(struct srp_indirect_buf)/*20*/
123 		+ 128 * sizeof(struct srp_direct_buf)/*16*/,
124 
125 	MIN_MAX_RSP_SIZE = sizeof(struct srp_rsp)/*36*/ + 4,
126 	DEFAULT_MAX_RSP_SIZE = 256, /* leaves 220 bytes for sense data */
127 
128 	DEFAULT_MAX_RDMA_SIZE = 65536,
129 };
130 
131 enum srpt_opcode {
132 	SRPT_RECV,
133 	SRPT_SEND,
134 	SRPT_RDMA_MID,
135 	SRPT_RDMA_ABORT,
136 	SRPT_RDMA_READ_LAST,
137 	SRPT_RDMA_WRITE_LAST,
138 };
139 
140 static inline u64 encode_wr_id(u8 opcode, u32 idx)
141 {
142 	return ((u64)opcode << 32) | idx;
143 }
144 static inline enum srpt_opcode opcode_from_wr_id(u64 wr_id)
145 {
146 	return wr_id >> 32;
147 }
148 static inline u32 idx_from_wr_id(u64 wr_id)
149 {
150 	return (u32)wr_id;
151 }
152 
153 struct rdma_iu {
154 	u64		raddr;
155 	u32		rkey;
156 	struct ib_sge	*sge;
157 	u32		sge_cnt;
158 	int		mem_id;
159 };
160 
161 /**
162  * enum srpt_command_state - SCSI command state managed by SRPT.
163  * @SRPT_STATE_NEW:           New command arrived and is being processed.
164  * @SRPT_STATE_NEED_DATA:     Processing a write or bidir command and waiting
165  *                            for data arrival.
166  * @SRPT_STATE_DATA_IN:       Data for the write or bidir command arrived and is
167  *                            being processed.
168  * @SRPT_STATE_CMD_RSP_SENT:  SRP_RSP for SRP_CMD has been sent.
169  * @SRPT_STATE_MGMT:          Processing a SCSI task management command.
170  * @SRPT_STATE_MGMT_RSP_SENT: SRP_RSP for SRP_TSK_MGMT has been sent.
171  * @SRPT_STATE_DONE:          Command processing finished successfully, command
172  *                            processing has been aborted or command processing
173  *                            failed.
174  */
175 enum srpt_command_state {
176 	SRPT_STATE_NEW		 = 0,
177 	SRPT_STATE_NEED_DATA	 = 1,
178 	SRPT_STATE_DATA_IN	 = 2,
179 	SRPT_STATE_CMD_RSP_SENT	 = 3,
180 	SRPT_STATE_MGMT		 = 4,
181 	SRPT_STATE_MGMT_RSP_SENT = 5,
182 	SRPT_STATE_DONE		 = 6,
183 };
184 
185 /**
186  * struct srpt_ioctx - Shared SRPT I/O context information.
187  * @buf:   Pointer to the buffer.
188  * @dma:   DMA address of the buffer.
189  * @index: Index of the I/O context in its ioctx_ring array.
190  */
191 struct srpt_ioctx {
192 	void			*buf;
193 	dma_addr_t		dma;
194 	uint32_t		index;
195 };
196 
197 /**
198  * struct srpt_recv_ioctx - SRPT receive I/O context.
199  * @ioctx:     See above.
200  * @wait_list: Node for insertion in srpt_rdma_ch.cmd_wait_list.
201  */
202 struct srpt_recv_ioctx {
203 	struct srpt_ioctx	ioctx;
204 	struct list_head	wait_list;
205 };
206 
207 /**
208  * struct srpt_send_ioctx - SRPT send I/O context.
209  * @ioctx:       See above.
210  * @ch:          Channel pointer.
211  * @free_list:   Node in srpt_rdma_ch.free_list.
212  * @n_rbuf:      Number of data buffers in the received SRP command.
213  * @rbufs:       Pointer to SRP data buffer array.
214  * @single_rbuf: SRP data buffer if the command has only a single buffer.
215  * @sg:          Pointer to sg-list associated with this I/O context.
216  * @sg_cnt:      SG-list size.
217  * @mapped_sg_count: ib_dma_map_sg() return value.
218  * @n_rdma_ius:  Number of elements in the rdma_ius array.
219  * @rdma_ius:    Array with information about the RDMA mapping.
220  * @tag:         Tag of the received SRP information unit.
221  * @spinlock:    Protects 'state'.
222  * @state:       I/O context state.
223  * @rdma_aborted: If initiating a multipart RDMA transfer failed, whether
224  * 		 the already initiated transfers have finished.
225  * @cmd:         Target core command data structure.
226  * @sense_data:  SCSI sense data.
227  */
228 struct srpt_send_ioctx {
229 	struct srpt_ioctx	ioctx;
230 	struct srpt_rdma_ch	*ch;
231 	struct rdma_iu		*rdma_ius;
232 	struct srp_direct_buf	*rbufs;
233 	struct srp_direct_buf	single_rbuf;
234 	struct scatterlist	*sg;
235 	struct list_head	free_list;
236 	spinlock_t		spinlock;
237 	enum srpt_command_state	state;
238 	bool			rdma_aborted;
239 	struct se_cmd		cmd;
240 	struct completion	tx_done;
241 	int			sg_cnt;
242 	int			mapped_sg_count;
243 	u16			n_rdma_ius;
244 	u8			n_rdma;
245 	u8			n_rbuf;
246 	bool			queue_status_only;
247 	u8			sense_data[TRANSPORT_SENSE_BUFFER];
248 };
249 
250 /**
251  * enum rdma_ch_state - SRP channel state.
252  * @CH_CONNECTING:	 QP is in RTR state; waiting for RTU.
253  * @CH_LIVE:		 QP is in RTS state.
254  * @CH_DISCONNECTING:    DREQ has been received; waiting for DREP
255  *                       or DREQ has been send and waiting for DREP
256  *                       or .
257  * @CH_DRAINING:	 QP is in ERR state; waiting for last WQE event.
258  * @CH_RELEASING:	 Last WQE event has been received; releasing resources.
259  */
260 enum rdma_ch_state {
261 	CH_CONNECTING,
262 	CH_LIVE,
263 	CH_DISCONNECTING,
264 	CH_DRAINING,
265 	CH_RELEASING
266 };
267 
268 /**
269  * struct srpt_rdma_ch - RDMA channel.
270  * @wait_queue:    Allows the kernel thread to wait for more work.
271  * @thread:        Kernel thread that processes the IB queues associated with
272  *                 the channel.
273  * @cm_id:         IB CM ID associated with the channel.
274  * @qp:            IB queue pair used for communicating over this channel.
275  * @cq:            IB completion queue for this channel.
276  * @rq_size:       IB receive queue size.
277  * @rsp_size	   IB response message size in bytes.
278  * @sq_wr_avail:   number of work requests available in the send queue.
279  * @sport:         pointer to the information of the HCA port used by this
280  *                 channel.
281  * @i_port_id:     128-bit initiator port identifier copied from SRP_LOGIN_REQ.
282  * @t_port_id:     128-bit target port identifier copied from SRP_LOGIN_REQ.
283  * @max_ti_iu_len: maximum target-to-initiator information unit length.
284  * @req_lim:       request limit: maximum number of requests that may be sent
285  *                 by the initiator without having received a response.
286  * @req_lim_delta: Number of credits not yet sent back to the initiator.
287  * @spinlock:      Protects free_list and state.
288  * @free_list:     Head of list with free send I/O contexts.
289  * @state:         channel state. See also enum rdma_ch_state.
290  * @ioctx_ring:    Send ring.
291  * @wc:            IB work completion array for srpt_process_completion().
292  * @list:          Node for insertion in the srpt_device.rch_list list.
293  * @cmd_wait_list: List of SCSI commands that arrived before the RTU event. This
294  *                 list contains struct srpt_ioctx elements and is protected
295  *                 against concurrent modification by the cm_id spinlock.
296  * @sess:          Session information associated with this SRP channel.
297  * @sess_name:     Session name.
298  * @release_work:  Allows scheduling of srpt_release_channel().
299  * @release_done:  Enables waiting for srpt_release_channel() completion.
300  */
301 struct srpt_rdma_ch {
302 	wait_queue_head_t	wait_queue;
303 	struct task_struct	*thread;
304 	struct ib_cm_id		*cm_id;
305 	struct ib_qp		*qp;
306 	struct ib_cq		*cq;
307 	int			rq_size;
308 	u32			rsp_size;
309 	atomic_t		sq_wr_avail;
310 	struct srpt_port	*sport;
311 	u8			i_port_id[16];
312 	u8			t_port_id[16];
313 	int			max_ti_iu_len;
314 	atomic_t		req_lim;
315 	atomic_t		req_lim_delta;
316 	spinlock_t		spinlock;
317 	struct list_head	free_list;
318 	enum rdma_ch_state	state;
319 	struct srpt_send_ioctx	**ioctx_ring;
320 	struct ib_wc		wc[16];
321 	struct list_head	list;
322 	struct list_head	cmd_wait_list;
323 	struct se_session	*sess;
324 	u8			sess_name[36];
325 	struct work_struct	release_work;
326 	struct completion	*release_done;
327 	bool			in_shutdown;
328 };
329 
330 /**
331  * struct srpt_port_attib - Attributes for SRPT port
332  * @srp_max_rdma_size: Maximum size of SRP RDMA transfers for new connections.
333  * @srp_max_rsp_size: Maximum size of SRP response messages in bytes.
334  * @srp_sq_size: Shared receive queue (SRQ) size.
335  */
336 struct srpt_port_attrib {
337 	u32			srp_max_rdma_size;
338 	u32			srp_max_rsp_size;
339 	u32			srp_sq_size;
340 };
341 
342 /**
343  * struct srpt_port - Information associated by SRPT with a single IB port.
344  * @sdev:      backpointer to the HCA information.
345  * @mad_agent: per-port management datagram processing information.
346  * @enabled:   Whether or not this target port is enabled.
347  * @port_guid: ASCII representation of Port GUID
348  * @port:      one-based port number.
349  * @sm_lid:    cached value of the port's sm_lid.
350  * @lid:       cached value of the port's lid.
351  * @gid:       cached value of the port's gid.
352  * @port_acl_lock spinlock for port_acl_list:
353  * @work:      work structure for refreshing the aforementioned cached values.
354  * @port_tpg_1 Target portal group = 1 data.
355  * @port_wwn:  Target core WWN data.
356  * @port_acl_list: Head of the list with all node ACLs for this port.
357  */
358 struct srpt_port {
359 	struct srpt_device	*sdev;
360 	struct ib_mad_agent	*mad_agent;
361 	bool			enabled;
362 	u8			port_guid[64];
363 	u8			port;
364 	u16			sm_lid;
365 	u16			lid;
366 	union ib_gid		gid;
367 	spinlock_t		port_acl_lock;
368 	struct work_struct	work;
369 	struct se_portal_group	port_tpg_1;
370 	struct se_wwn		port_wwn;
371 	struct list_head	port_acl_list;
372 	struct srpt_port_attrib port_attrib;
373 };
374 
375 /**
376  * struct srpt_device - Information associated by SRPT with a single HCA.
377  * @device:        Backpointer to the struct ib_device managed by the IB core.
378  * @pd:            IB protection domain.
379  * @mr:            L_Key (local key) with write access to all local memory.
380  * @srq:           Per-HCA SRQ (shared receive queue).
381  * @cm_id:         Connection identifier.
382  * @dev_attr:      Attributes of the InfiniBand device as obtained during the
383  *                 ib_client.add() callback.
384  * @srq_size:      SRQ size.
385  * @ioctx_ring:    Per-HCA SRQ.
386  * @rch_list:      Per-device channel list -- see also srpt_rdma_ch.list.
387  * @ch_releaseQ:   Enables waiting for removal from rch_list.
388  * @spinlock:      Protects rch_list and tpg.
389  * @port:          Information about the ports owned by this HCA.
390  * @event_handler: Per-HCA asynchronous IB event handler.
391  * @list:          Node in srpt_dev_list.
392  */
393 struct srpt_device {
394 	struct ib_device	*device;
395 	struct ib_pd		*pd;
396 	struct ib_srq		*srq;
397 	struct ib_cm_id		*cm_id;
398 	struct ib_device_attr	dev_attr;
399 	int			srq_size;
400 	struct srpt_recv_ioctx	**ioctx_ring;
401 	struct list_head	rch_list;
402 	wait_queue_head_t	ch_releaseQ;
403 	spinlock_t		spinlock;
404 	struct srpt_port	port[2];
405 	struct ib_event_handler	event_handler;
406 	struct list_head	list;
407 };
408 
409 /**
410  * struct srpt_node_acl - Per-initiator ACL data (managed via configfs).
411  * @nacl:      Target core node ACL information.
412  * @i_port_id: 128-bit SRP initiator port ID.
413  * @sport:     port information.
414  * @list:      Element of the per-HCA ACL list.
415  */
416 struct srpt_node_acl {
417 	struct se_node_acl	nacl;
418 	u8			i_port_id[16];
419 	struct srpt_port	*sport;
420 	struct list_head	list;
421 };
422 
423 #endif				/* IB_SRPT_H */
424