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/rdma_cm.h>
46 #include <rdma/rw.h>
47 
48 #include <scsi/srp.h>
49 
50 #include "ib_dm_mad.h"
51 
52 /*
53  * The prefix the ServiceName field must start with in the device management
54  * ServiceEntries attribute pair. See also the SRP specification.
55  */
56 #define SRP_SERVICE_NAME_PREFIX		"SRP.T10:"
57 
58 struct srpt_nexus;
59 
60 enum {
61 	/*
62 	 * SRP IOControllerProfile attributes for SRP target ports that have
63 	 * not been defined in <scsi/srp.h>. Source: section B.7, table B.7
64 	 * in the SRP specification.
65 	 */
66 	SRP_PROTOCOL = 0x0108,
67 	SRP_PROTOCOL_VERSION = 0x0001,
68 	SRP_IO_SUBCLASS = 0x609e,
69 	SRP_SEND_TO_IOC = 0x01,
70 	SRP_SEND_FROM_IOC = 0x02,
71 	SRP_RDMA_READ_FROM_IOC = 0x08,
72 	SRP_RDMA_WRITE_FROM_IOC = 0x20,
73 
74 	/*
75 	 * srp_login_cmd.req_flags bitmasks. See also table 9 in the SRP
76 	 * specification.
77 	 */
78 	SRP_MTCH_ACTION = 0x03, /* MULTI-CHANNEL ACTION */
79 	SRP_LOSOLNT = 0x10, /* logout solicited notification */
80 	SRP_CRSOLNT = 0x20, /* credit request solicited notification */
81 	SRP_AESOLNT = 0x40, /* asynchronous event solicited notification */
82 
83 	/*
84 	 * srp_cmd.sol_nt / srp_tsk_mgmt.sol_not bitmasks. See also tables
85 	 * 18 and 20 in the SRP specification.
86 	 */
87 	SRP_SCSOLNT = 0x02, /* SCSOLNT = successful solicited notification */
88 	SRP_UCSOLNT = 0x04, /* UCSOLNT = unsuccessful solicited notification */
89 
90 	/*
91 	 * srp_rsp.sol_not / srp_t_logout.sol_not bitmasks. See also tables
92 	 * 16 and 22 in the SRP specification.
93 	 */
94 	SRP_SOLNT = 0x01, /* SOLNT = solicited notification */
95 
96 	/* See also table 24 in the SRP specification. */
97 	SRP_TSK_MGMT_SUCCESS = 0x00,
98 	SRP_TSK_MGMT_FUNC_NOT_SUPP = 0x04,
99 	SRP_TSK_MGMT_FAILED = 0x05,
100 
101 	/* See also table 21 in the SRP specification. */
102 	SRP_CMD_SIMPLE_Q = 0x0,
103 	SRP_CMD_HEAD_OF_Q = 0x1,
104 	SRP_CMD_ORDERED_Q = 0x2,
105 	SRP_CMD_ACA = 0x4,
106 
107 	SRPT_DEF_SG_TABLESIZE = 128,
108 
109 	MIN_SRPT_SQ_SIZE = 16,
110 	DEF_SRPT_SQ_SIZE = 4096,
111 	MAX_SRPT_RQ_SIZE = 128,
112 	MIN_SRPT_SRQ_SIZE = 4,
113 	DEFAULT_SRPT_SRQ_SIZE = 4095,
114 	MAX_SRPT_SRQ_SIZE = 65535,
115 	MAX_SRPT_RDMA_SIZE = 1U << 24,
116 	MAX_SRPT_RSP_SIZE = 1024,
117 
118 	SRP_MAX_ADD_CDB_LEN = 16,
119 	SRP_MAX_IMM_DATA_OFFSET = 80,
120 	SRP_MAX_IMM_DATA = 8 * 1024,
121 	MIN_MAX_REQ_SIZE = 996,
122 	DEFAULT_MAX_REQ_SIZE_1 = sizeof(struct srp_cmd)/*48*/ +
123 				 SRP_MAX_ADD_CDB_LEN +
124 				 sizeof(struct srp_indirect_buf)/*20*/ +
125 				 128 * sizeof(struct srp_direct_buf)/*16*/,
126 	DEFAULT_MAX_REQ_SIZE_2 = SRP_MAX_IMM_DATA_OFFSET +
127 				 sizeof(struct srp_imm_buf) + SRP_MAX_IMM_DATA,
128 	DEFAULT_MAX_REQ_SIZE = DEFAULT_MAX_REQ_SIZE_1 > DEFAULT_MAX_REQ_SIZE_2 ?
129 			       DEFAULT_MAX_REQ_SIZE_1 : DEFAULT_MAX_REQ_SIZE_2,
130 
131 	MIN_MAX_RSP_SIZE = sizeof(struct srp_rsp)/*36*/ + 4,
132 	DEFAULT_MAX_RSP_SIZE = 256, /* leaves 220 bytes for sense data */
133 
134 	DEFAULT_MAX_RDMA_SIZE = 65536,
135 };
136 
137 /**
138  * enum srpt_command_state - SCSI command state managed by SRPT
139  * @SRPT_STATE_NEW:           New command arrived and is being processed.
140  * @SRPT_STATE_NEED_DATA:     Processing a write or bidir command and waiting
141  *                            for data arrival.
142  * @SRPT_STATE_DATA_IN:       Data for the write or bidir command arrived and is
143  *                            being processed.
144  * @SRPT_STATE_CMD_RSP_SENT:  SRP_RSP for SRP_CMD has been sent.
145  * @SRPT_STATE_MGMT:          Processing a SCSI task management command.
146  * @SRPT_STATE_MGMT_RSP_SENT: SRP_RSP for SRP_TSK_MGMT has been sent.
147  * @SRPT_STATE_DONE:          Command processing finished successfully, command
148  *                            processing has been aborted or command processing
149  *                            failed.
150  */
151 enum srpt_command_state {
152 	SRPT_STATE_NEW		 = 0,
153 	SRPT_STATE_NEED_DATA	 = 1,
154 	SRPT_STATE_DATA_IN	 = 2,
155 	SRPT_STATE_CMD_RSP_SENT	 = 3,
156 	SRPT_STATE_MGMT		 = 4,
157 	SRPT_STATE_MGMT_RSP_SENT = 5,
158 	SRPT_STATE_DONE		 = 6,
159 };
160 
161 /**
162  * struct srpt_ioctx - shared SRPT I/O context information
163  * @cqe:   Completion queue element.
164  * @buf:   Pointer to the buffer.
165  * @dma:   DMA address of the buffer.
166  * @offset: Offset of the first byte in @buf and @dma that is actually used.
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		offset;
174 	uint32_t		index;
175 };
176 
177 /**
178  * struct srpt_recv_ioctx - SRPT receive I/O context
179  * @ioctx:     See above.
180  * @wait_list: Node for insertion in srpt_rdma_ch.cmd_wait_list.
181  * @byte_len:  Number of bytes in @ioctx.buf.
182  */
183 struct srpt_recv_ioctx {
184 	struct srpt_ioctx	ioctx;
185 	struct list_head	wait_list;
186 	int			byte_len;
187 };
188 
189 struct srpt_rw_ctx {
190 	struct rdma_rw_ctx	rw;
191 	struct scatterlist	*sg;
192 	unsigned int		nents;
193 };
194 
195 /**
196  * struct srpt_send_ioctx - SRPT send I/O context
197  * @ioctx:       See above.
198  * @ch:          Channel pointer.
199  * @recv_ioctx:  Receive I/O context associated with this send I/O context.
200  *		 Only used for processing immediate data.
201  * @s_rw_ctx:    @rw_ctxs points here if only a single rw_ctx is needed.
202  * @rw_ctxs:     RDMA read/write contexts.
203  * @imm_sg:      Scatterlist for immediate data.
204  * @rdma_cqe:    RDMA completion queue element.
205  * @state:       I/O context state.
206  * @cmd:         Target core command data structure.
207  * @sense_data:  SCSI sense data.
208  * @n_rdma:      Number of work requests needed to transfer this ioctx.
209  * @n_rw_ctx:    Size of rw_ctxs array.
210  * @queue_status_only: Send a SCSI status back to the initiator but no data.
211  * @sense_data:  Sense data to be sent to the initiator.
212  */
213 struct srpt_send_ioctx {
214 	struct srpt_ioctx	ioctx;
215 	struct srpt_rdma_ch	*ch;
216 	struct srpt_recv_ioctx	*recv_ioctx;
217 
218 	struct srpt_rw_ctx	s_rw_ctx;
219 	struct srpt_rw_ctx	*rw_ctxs;
220 
221 	struct scatterlist	imm_sg;
222 
223 	struct ib_cqe		rdma_cqe;
224 	enum srpt_command_state	state;
225 	struct se_cmd		cmd;
226 	u8			n_rdma;
227 	u8			n_rw_ctx;
228 	bool			queue_status_only;
229 	u8			sense_data[TRANSPORT_SENSE_BUFFER];
230 };
231 
232 /**
233  * enum rdma_ch_state - SRP channel state
234  * @CH_CONNECTING:    QP is in RTR state; waiting for RTU.
235  * @CH_LIVE:	      QP is in RTS state.
236  * @CH_DISCONNECTING: DREQ has been sent and waiting for DREP or DREQ has
237  *                    been received.
238  * @CH_DRAINING:      DREP has been received or waiting for DREP timed out
239  *                    and last work request has been queued.
240  * @CH_DISCONNECTED:  Last completion has been received.
241  */
242 enum rdma_ch_state {
243 	CH_CONNECTING,
244 	CH_LIVE,
245 	CH_DISCONNECTING,
246 	CH_DRAINING,
247 	CH_DISCONNECTED,
248 };
249 
250 /**
251  * struct srpt_rdma_ch - RDMA channel
252  * @nexus:         I_T nexus this channel is associated with.
253  * @qp:            IB queue pair used for communicating over this channel.
254  * @ib_cm:	   See below.
255  * @ib_cm.cm_id:   IB CM ID associated with the channel.
256  * @rdma_cm:	   See below.
257  * @rdma_cm.cm_id: RDMA CM ID associated with the channel.
258  * @cq:            IB completion queue for this channel.
259  * @zw_cqe:	   Zero-length write CQE.
260  * @rcu:           RCU head.
261  * @kref:	   kref for this channel.
262  * @closed:	   Completion object that will be signaled as soon as a new
263  *		   channel object with the same identity can be created.
264  * @rq_size:       IB receive queue size.
265  * @max_rsp_size:  Maximum size of an RSP response message in bytes.
266  * @sq_wr_avail:   number of work requests available in the send queue.
267  * @sport:         pointer to the information of the HCA port used by this
268  *                 channel.
269  * @max_ti_iu_len: maximum target-to-initiator information unit length.
270  * @req_lim:       request limit: maximum number of requests that may be sent
271  *                 by the initiator without having received a response.
272  * @req_lim_delta: Number of credits not yet sent back to the initiator.
273  * @imm_data_offset: Offset from start of SRP_CMD for immediate data.
274  * @spinlock:      Protects free_list and state.
275  * @state:         channel state. See also enum rdma_ch_state.
276  * @using_rdma_cm: Whether the RDMA/CM or IB/CM is used for this channel.
277  * @processing_wait_list: Whether or not cmd_wait_list is being processed.
278  * @rsp_buf_cache: kmem_cache for @ioctx_ring.
279  * @ioctx_ring:    Send ring.
280  * @req_buf_cache: kmem_cache for @ioctx_recv_ring.
281  * @ioctx_recv_ring: Receive I/O context ring.
282  * @list:          Node in srpt_nexus.ch_list.
283  * @cmd_wait_list: List of SCSI commands that arrived before the RTU event. This
284  *                 list contains struct srpt_ioctx elements and is protected
285  *                 against concurrent modification by the cm_id spinlock.
286  * @pkey:          P_Key of the IB partition for this SRP channel.
287  * @sess:          Session information associated with this SRP channel.
288  * @sess_name:     Session name.
289  * @release_work:  Allows scheduling of srpt_release_channel().
290  */
291 struct srpt_rdma_ch {
292 	struct srpt_nexus	*nexus;
293 	struct ib_qp		*qp;
294 	union {
295 		struct {
296 			struct ib_cm_id		*cm_id;
297 		} ib_cm;
298 		struct {
299 			struct rdma_cm_id	*cm_id;
300 		} rdma_cm;
301 	};
302 	struct ib_cq		*cq;
303 	u32			cq_size;
304 	struct ib_cqe		zw_cqe;
305 	struct rcu_head		rcu;
306 	struct kref		kref;
307 	struct completion	*closed;
308 	int			rq_size;
309 	u32			max_rsp_size;
310 	atomic_t		sq_wr_avail;
311 	struct srpt_port	*sport;
312 	int			max_ti_iu_len;
313 	atomic_t		req_lim;
314 	atomic_t		req_lim_delta;
315 	u16			imm_data_offset;
316 	spinlock_t		spinlock;
317 	enum rdma_ch_state	state;
318 	struct kmem_cache	*rsp_buf_cache;
319 	struct srpt_send_ioctx	**ioctx_ring;
320 	struct kmem_cache	*req_buf_cache;
321 	struct srpt_recv_ioctx	**ioctx_recv_ring;
322 	struct list_head	list;
323 	struct list_head	cmd_wait_list;
324 	uint16_t		pkey;
325 	bool			using_rdma_cm;
326 	bool			processing_wait_list;
327 	struct se_session	*sess;
328 	u8			sess_name[40];
329 	struct work_struct	release_work;
330 };
331 
332 /**
333  * struct srpt_nexus - I_T nexus
334  * @rcu:       RCU head for this data structure.
335  * @entry:     srpt_port.nexus_list list node.
336  * @ch_list:   struct srpt_rdma_ch list. Protected by srpt_port.mutex.
337  * @i_port_id: 128-bit initiator port identifier copied from SRP_LOGIN_REQ.
338  * @t_port_id: 128-bit target port identifier copied from SRP_LOGIN_REQ.
339  */
340 struct srpt_nexus {
341 	struct rcu_head		rcu;
342 	struct list_head	entry;
343 	struct list_head	ch_list;
344 	u8			i_port_id[16];
345 	u8			t_port_id[16];
346 };
347 
348 /**
349  * struct srpt_port_attib - attributes for SRPT port
350  * @srp_max_rdma_size: Maximum size of SRP RDMA transfers for new connections.
351  * @srp_max_rsp_size: Maximum size of SRP response messages in bytes.
352  * @srp_sq_size: Shared receive queue (SRQ) size.
353  * @use_srq: Whether or not to use SRQ.
354  */
355 struct srpt_port_attrib {
356 	u32			srp_max_rdma_size;
357 	u32			srp_max_rsp_size;
358 	u32			srp_sq_size;
359 	bool			use_srq;
360 };
361 
362 /**
363  * struct srpt_tpg - information about a single "target portal group"
364  * @entry:	Entry in @sport_id->tpg_list.
365  * @sport_id:	Port name this TPG is associated with.
366  * @tpg:	LIO TPG data structure.
367  *
368  * Zero or more target portal groups are associated with each port name
369  * (srpt_port_id). With each TPG an ACL list is associated.
370  */
371 struct srpt_tpg {
372 	struct list_head	entry;
373 	struct srpt_port_id	*sport_id;
374 	struct se_portal_group	tpg;
375 };
376 
377 /**
378  * struct srpt_port_id - information about an RDMA port name
379  * @mutex:	Protects @tpg_list changes.
380  * @tpg_list:	TPGs associated with the RDMA port name.
381  * @wwn:	WWN associated with the RDMA port name.
382  * @name:	ASCII representation of the port name.
383  *
384  * Multiple sysfs directories can be associated with a single RDMA port. This
385  * data structure represents a single (port, name) pair.
386  */
387 struct srpt_port_id {
388 	struct mutex		mutex;
389 	struct list_head	tpg_list;
390 	struct se_wwn		wwn;
391 	char			name[64];
392 };
393 
394 /**
395  * struct srpt_port - information associated by SRPT with a single IB port
396  * @sdev:      backpointer to the HCA information.
397  * @mad_agent: per-port management datagram processing information.
398  * @enabled:   Whether or not this target port is enabled.
399  * @port:      one-based port number.
400  * @sm_lid:    cached value of the port's sm_lid.
401  * @lid:       cached value of the port's lid.
402  * @gid:       cached value of the port's gid.
403  * @work:      work structure for refreshing the aforementioned cached values.
404  * @port_guid_id: target port GUID
405  * @port_gid_id: target port GID
406  * @port_attrib:   Port attributes that can be accessed through configfs.
407  * @refcount:	   Number of objects associated with this port.
408  * @freed_channels: Completion that will be signaled once @refcount becomes 0.
409  * @mutex:	   Protects nexus_list.
410  * @nexus_list:	   Nexus list. See also srpt_nexus.entry.
411  */
412 struct srpt_port {
413 	struct srpt_device	*sdev;
414 	struct ib_mad_agent	*mad_agent;
415 	bool			enabled;
416 	u8			port;
417 	u32			sm_lid;
418 	u32			lid;
419 	union ib_gid		gid;
420 	struct work_struct	work;
421 	struct srpt_port_id	port_guid_id;
422 	struct srpt_port_id	port_gid_id;
423 	struct srpt_port_attrib port_attrib;
424 	atomic_t		refcount;
425 	struct completion	*freed_channels;
426 	struct mutex		mutex;
427 	struct list_head	nexus_list;
428 };
429 
430 /**
431  * struct srpt_device - information associated by SRPT with a single HCA
432  * @device:        Backpointer to the struct ib_device managed by the IB core.
433  * @pd:            IB protection domain.
434  * @lkey:          L_Key (local key) with write access to all local memory.
435  * @srq:           Per-HCA SRQ (shared receive queue).
436  * @cm_id:         Connection identifier.
437  * @srq_size:      SRQ size.
438  * @sdev_mutex:	   Serializes use_srq changes.
439  * @use_srq:       Whether or not to use SRQ.
440  * @req_buf_cache: kmem_cache for @ioctx_ring buffers.
441  * @ioctx_ring:    Per-HCA SRQ.
442  * @event_handler: Per-HCA asynchronous IB event handler.
443  * @list:          Node in srpt_dev_list.
444  * @port:          Information about the ports owned by this HCA.
445  */
446 struct srpt_device {
447 	struct ib_device	*device;
448 	struct ib_pd		*pd;
449 	u32			lkey;
450 	struct ib_srq		*srq;
451 	struct ib_cm_id		*cm_id;
452 	int			srq_size;
453 	struct mutex		sdev_mutex;
454 	bool			use_srq;
455 	struct kmem_cache	*req_buf_cache;
456 	struct srpt_recv_ioctx	**ioctx_ring;
457 	struct ib_event_handler	event_handler;
458 	struct list_head	list;
459 	struct srpt_port        port[];
460 };
461 
462 #endif				/* IB_SRPT_H */
463