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