2 * Copyright (c) 2006 - 2009 Mellanox Technology Inc. All rights reserved.
3 * Copyright (C) 2009 - 2010 Bart Van Assche <bvanassche@acm.org>.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
38 #include <linux/types.h>
39 #include <linux/list.h>
40 #include <linux/wait.h>
42 #include <rdma/ib_verbs.h>
43 #include <rdma/ib_sa.h>
44 #include <rdma/ib_cm.h>
48 #include "ib_dm_mad.h"
51 * The prefix the ServiceName field must start with in the device management
52 * ServiceEntries attribute pair. See also the SRP specification.
54 #define SRP_SERVICE_NAME_PREFIX "SRP.T10:"
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.
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,
71 * srp_login_cmd.req_flags bitmasks. See also table 9 in the SRP
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 */
80 * srp_cmd.sol_nt / srp_tsk_mgmt.sol_not bitmasks. See also tables
81 * 18 and 20 in the SRP specification.
83 SRP_SCSOLNT
= 0x02, /* SCSOLNT = successful solicited notification */
84 SRP_UCSOLNT
= 0x04, /* UCSOLNT = unsuccessful solicited notification */
87 * srp_rsp.sol_not / srp_t_logout.sol_not bitmasks. See also tables
88 * 16 and 22 in the SRP specification.
90 SRP_SOLNT
= 0x01, /* SOLNT = solicited notification */
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,
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,
103 SRP_LOGIN_RSP_MULTICHAN_NO_CHAN
= 0x0,
104 SRP_LOGIN_RSP_MULTICHAN_TERMINATED
= 0x1,
105 SRP_LOGIN_RSP_MULTICHAN_MAINTAINED
= 0x2,
107 SRPT_DEF_SG_TABLESIZE
= 128,
108 SRPT_DEF_SG_PER_WQE
= 16,
110 MIN_SRPT_SQ_SIZE
= 16,
111 DEF_SRPT_SQ_SIZE
= 4096,
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,
119 MIN_MAX_REQ_SIZE
= 996,
121 = sizeof(struct srp_cmd
)/*48*/
122 + sizeof(struct srp_indirect_buf
)/*20*/
123 + 128 * sizeof(struct srp_direct_buf
)/*16*/,
125 MIN_MAX_RSP_SIZE
= sizeof(struct srp_rsp
)/*36*/ + 4,
126 DEFAULT_MAX_RSP_SIZE
= 256, /* leaves 220 bytes for sense data */
128 DEFAULT_MAX_RDMA_SIZE
= 65536,
132 * enum srpt_command_state - SCSI command state managed by SRPT.
133 * @SRPT_STATE_NEW: New command arrived and is being processed.
134 * @SRPT_STATE_NEED_DATA: Processing a write or bidir command and waiting
136 * @SRPT_STATE_DATA_IN: Data for the write or bidir command arrived and is
138 * @SRPT_STATE_CMD_RSP_SENT: SRP_RSP for SRP_CMD has been sent.
139 * @SRPT_STATE_MGMT: Processing a SCSI task management command.
140 * @SRPT_STATE_MGMT_RSP_SENT: SRP_RSP for SRP_TSK_MGMT has been sent.
141 * @SRPT_STATE_DONE: Command processing finished successfully, command
142 * processing has been aborted or command processing
145 enum srpt_command_state
{
147 SRPT_STATE_NEED_DATA
= 1,
148 SRPT_STATE_DATA_IN
= 2,
149 SRPT_STATE_CMD_RSP_SENT
= 3,
151 SRPT_STATE_MGMT_RSP_SENT
= 5,
156 * struct srpt_ioctx - Shared SRPT I/O context information.
157 * @buf: Pointer to the buffer.
158 * @dma: DMA address of the buffer.
159 * @index: Index of the I/O context in its ioctx_ring array.
169 * struct srpt_recv_ioctx - SRPT receive I/O context.
171 * @wait_list: Node for insertion in srpt_rdma_ch.cmd_wait_list.
173 struct srpt_recv_ioctx
{
174 struct srpt_ioctx ioctx
;
175 struct list_head wait_list
;
179 * struct srpt_send_ioctx - SRPT send I/O context.
181 * @ch: Channel pointer.
182 * @free_list: Node in srpt_rdma_ch.free_list.
183 * @n_rbuf: Number of data buffers in the received SRP command.
184 * @rbufs: Pointer to SRP data buffer array.
185 * @single_rbuf: SRP data buffer if the command has only a single buffer.
186 * @sg: Pointer to sg-list associated with this I/O context.
187 * @sg_cnt: SG-list size.
188 * @mapped_sg_count: ib_dma_map_sg() return value.
189 * @n_rdma_wrs: Number of elements in the rdma_wrs array.
190 * @rdma_wrs: Array with information about the RDMA mapping.
191 * @tag: Tag of the received SRP information unit.
192 * @spinlock: Protects 'state'.
193 * @state: I/O context state.
194 * @cmd: Target core command data structure.
195 * @sense_data: SCSI sense data.
197 struct srpt_send_ioctx
{
198 struct srpt_ioctx ioctx
;
199 struct srpt_rdma_ch
*ch
;
200 struct ib_rdma_wr
*rdma_wrs
;
201 struct ib_cqe rdma_cqe
;
202 struct srp_direct_buf
*rbufs
;
203 struct srp_direct_buf single_rbuf
;
204 struct scatterlist
*sg
;
205 struct list_head free_list
;
207 enum srpt_command_state state
;
209 struct completion tx_done
;
215 bool queue_status_only
;
216 u8 sense_data
[TRANSPORT_SENSE_BUFFER
];
220 * enum rdma_ch_state - SRP channel state.
221 * @CH_CONNECTING: QP is in RTR state; waiting for RTU.
222 * @CH_LIVE: QP is in RTS state.
223 * @CH_DISCONNECTING: DREQ has been sent and waiting for DREP or DREQ has
225 * @CH_DRAINING: DREP has been received or waiting for DREP timed out
226 * and last work request has been queued.
227 * @CH_DISCONNECTED: Last completion has been received.
238 * struct srpt_rdma_ch - RDMA channel.
239 * @cm_id: IB CM ID associated with the channel.
240 * @qp: IB queue pair used for communicating over this channel.
241 * @cq: IB completion queue for this channel.
242 * @rq_size: IB receive queue size.
243 * @rsp_size IB response message size in bytes.
244 * @sq_wr_avail: number of work requests available in the send queue.
245 * @sport: pointer to the information of the HCA port used by this
247 * @i_port_id: 128-bit initiator port identifier copied from SRP_LOGIN_REQ.
248 * @t_port_id: 128-bit target port identifier copied from SRP_LOGIN_REQ.
249 * @max_ti_iu_len: maximum target-to-initiator information unit length.
250 * @req_lim: request limit: maximum number of requests that may be sent
251 * by the initiator without having received a response.
252 * @req_lim_delta: Number of credits not yet sent back to the initiator.
253 * @spinlock: Protects free_list and state.
254 * @free_list: Head of list with free send I/O contexts.
255 * @state: channel state. See also enum rdma_ch_state.
256 * @ioctx_ring: Send ring.
257 * @list: Node for insertion in the srpt_device.rch_list list.
258 * @cmd_wait_list: List of SCSI commands that arrived before the RTU event. This
259 * list contains struct srpt_ioctx elements and is protected
260 * against concurrent modification by the cm_id spinlock.
261 * @sess: Session information associated with this SRP channel.
262 * @sess_name: Session name.
263 * @release_work: Allows scheduling of srpt_release_channel().
264 * @release_done: Enables waiting for srpt_release_channel() completion.
266 struct srpt_rdma_ch
{
267 struct ib_cm_id
*cm_id
;
270 struct ib_cqe zw_cqe
;
274 atomic_t sq_wr_avail
;
275 struct srpt_port
*sport
;
280 atomic_t req_lim_delta
;
282 struct list_head free_list
;
283 enum rdma_ch_state state
;
284 struct srpt_send_ioctx
**ioctx_ring
;
285 struct list_head list
;
286 struct list_head cmd_wait_list
;
287 struct se_session
*sess
;
289 struct work_struct release_work
;
290 struct completion
*release_done
;
294 * struct srpt_port_attib - Attributes for SRPT port
295 * @srp_max_rdma_size: Maximum size of SRP RDMA transfers for new connections.
296 * @srp_max_rsp_size: Maximum size of SRP response messages in bytes.
297 * @srp_sq_size: Shared receive queue (SRQ) size.
299 struct srpt_port_attrib
{
300 u32 srp_max_rdma_size
;
301 u32 srp_max_rsp_size
;
306 * struct srpt_port - Information associated by SRPT with a single IB port.
307 * @sdev: backpointer to the HCA information.
308 * @mad_agent: per-port management datagram processing information.
309 * @enabled: Whether or not this target port is enabled.
310 * @port_guid: ASCII representation of Port GUID
311 * @port: one-based port number.
312 * @sm_lid: cached value of the port's sm_lid.
313 * @lid: cached value of the port's lid.
314 * @gid: cached value of the port's gid.
315 * @port_acl_lock spinlock for port_acl_list:
316 * @work: work structure for refreshing the aforementioned cached values.
317 * @port_tpg_1 Target portal group = 1 data.
318 * @port_wwn: Target core WWN data.
319 * @port_acl_list: Head of the list with all node ACLs for this port.
322 struct srpt_device
*sdev
;
323 struct ib_mad_agent
*mad_agent
;
330 struct work_struct work
;
331 struct se_portal_group port_tpg_1
;
332 struct se_wwn port_wwn
;
333 struct srpt_port_attrib port_attrib
;
337 * struct srpt_device - Information associated by SRPT with a single HCA.
338 * @device: Backpointer to the struct ib_device managed by the IB core.
339 * @pd: IB protection domain.
340 * @mr: L_Key (local key) with write access to all local memory.
341 * @srq: Per-HCA SRQ (shared receive queue).
342 * @cm_id: Connection identifier.
343 * @srq_size: SRQ size.
344 * @ioctx_ring: Per-HCA SRQ.
345 * @rch_list: Per-device channel list -- see also srpt_rdma_ch.list.
346 * @ch_releaseQ: Enables waiting for removal from rch_list.
347 * @mutex: Protects rch_list.
348 * @port: Information about the ports owned by this HCA.
349 * @event_handler: Per-HCA asynchronous IB event handler.
350 * @list: Node in srpt_dev_list.
353 struct ib_device
*device
;
356 struct ib_cm_id
*cm_id
;
358 struct srpt_recv_ioctx
**ioctx_ring
;
359 struct list_head rch_list
;
360 wait_queue_head_t ch_releaseQ
;
362 struct srpt_port port
[2];
363 struct ib_event_handler event_handler
;
364 struct list_head list
;
367 #endif /* IB_SRPT_H */