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>
45 #include <rdma/rdma_cm.h>
50 #include "ib_dm_mad.h"
53 * The prefix the ServiceName field must start with in the device management
54 * ServiceEntries attribute pair. See also the SRP specification.
56 #define SRP_SERVICE_NAME_PREFIX "SRP.T10:"
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.
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,
75 * srp_login_cmd.req_flags bitmasks. See also table 9 in the SRP
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 */
84 * srp_cmd.sol_nt / srp_tsk_mgmt.sol_not bitmasks. See also tables
85 * 18 and 20 in the SRP specification.
87 SRP_SCSOLNT
= 0x02, /* SCSOLNT = successful solicited notification */
88 SRP_UCSOLNT
= 0x04, /* UCSOLNT = unsuccessful solicited notification */
91 * srp_rsp.sol_not / srp_t_logout.sol_not bitmasks. See also tables
92 * 16 and 22 in the SRP specification.
94 SRP_SOLNT
= 0x01, /* SOLNT = solicited notification */
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,
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,
107 SRP_LOGIN_RSP_MULTICHAN_NO_CHAN
= 0x0,
108 SRP_LOGIN_RSP_MULTICHAN_TERMINATED
= 0x1,
109 SRP_LOGIN_RSP_MULTICHAN_MAINTAINED
= 0x2,
111 SRPT_DEF_SG_TABLESIZE
= 128,
113 * An experimentally determined value that avoids that QP creation
114 * fails due to "swiotlb buffer is full" on systems using the swiotlb.
116 SRPT_MAX_SG_PER_WQE
= 16,
118 MIN_SRPT_SQ_SIZE
= 16,
119 DEF_SRPT_SQ_SIZE
= 4096,
120 MAX_SRPT_RQ_SIZE
= 128,
121 MIN_SRPT_SRQ_SIZE
= 4,
122 DEFAULT_SRPT_SRQ_SIZE
= 4095,
123 MAX_SRPT_SRQ_SIZE
= 65535,
124 MAX_SRPT_RDMA_SIZE
= 1U << 24,
125 MAX_SRPT_RSP_SIZE
= 1024,
127 MIN_MAX_REQ_SIZE
= 996,
129 = sizeof(struct srp_cmd
)/*48*/
130 + sizeof(struct srp_indirect_buf
)/*20*/
131 + 128 * sizeof(struct srp_direct_buf
)/*16*/,
133 MIN_MAX_RSP_SIZE
= sizeof(struct srp_rsp
)/*36*/ + 4,
134 DEFAULT_MAX_RSP_SIZE
= 256, /* leaves 220 bytes for sense data */
136 DEFAULT_MAX_RDMA_SIZE
= 65536,
140 * enum srpt_command_state - SCSI command state managed by SRPT
141 * @SRPT_STATE_NEW: New command arrived and is being processed.
142 * @SRPT_STATE_NEED_DATA: Processing a write or bidir command and waiting
144 * @SRPT_STATE_DATA_IN: Data for the write or bidir command arrived and is
146 * @SRPT_STATE_CMD_RSP_SENT: SRP_RSP for SRP_CMD has been sent.
147 * @SRPT_STATE_MGMT: Processing a SCSI task management command.
148 * @SRPT_STATE_MGMT_RSP_SENT: SRP_RSP for SRP_TSK_MGMT has been sent.
149 * @SRPT_STATE_DONE: Command processing finished successfully, command
150 * processing has been aborted or command processing
153 enum srpt_command_state
{
155 SRPT_STATE_NEED_DATA
= 1,
156 SRPT_STATE_DATA_IN
= 2,
157 SRPT_STATE_CMD_RSP_SENT
= 3,
159 SRPT_STATE_MGMT_RSP_SENT
= 5,
164 * struct srpt_ioctx - shared SRPT I/O context information
165 * @cqe: Completion queue element.
166 * @buf: Pointer to the buffer.
167 * @dma: DMA address of the buffer.
168 * @index: Index of the I/O context in its ioctx_ring array.
178 * struct srpt_recv_ioctx - SRPT receive I/O context
180 * @wait_list: Node for insertion in srpt_rdma_ch.cmd_wait_list.
182 struct srpt_recv_ioctx
{
183 struct srpt_ioctx ioctx
;
184 struct list_head wait_list
;
188 struct rdma_rw_ctx rw
;
189 struct scatterlist
*sg
;
194 * struct srpt_send_ioctx - SRPT send I/O context
196 * @ch: Channel pointer.
197 * @s_rw_ctx: @rw_ctxs points here if only a single rw_ctx is needed.
198 * @rw_ctxs: RDMA read/write contexts.
199 * @rdma_cqe: RDMA completion queue element.
200 * @free_list: Node in srpt_rdma_ch.free_list.
201 * @state: I/O context state.
202 * @cmd: Target core command data structure.
203 * @sense_data: SCSI sense data.
204 * @n_rdma: Number of work requests needed to transfer this ioctx.
205 * @n_rw_ctx: Size of rw_ctxs array.
206 * @queue_status_only: Send a SCSI status back to the initiator but no data.
207 * @sense_data: Sense data to be sent to the initiator.
209 struct srpt_send_ioctx
{
210 struct srpt_ioctx ioctx
;
211 struct srpt_rdma_ch
*ch
;
213 struct srpt_rw_ctx s_rw_ctx
;
214 struct srpt_rw_ctx
*rw_ctxs
;
216 struct ib_cqe rdma_cqe
;
217 struct list_head free_list
;
218 enum srpt_command_state state
;
222 bool queue_status_only
;
223 u8 sense_data
[TRANSPORT_SENSE_BUFFER
];
227 * enum rdma_ch_state - SRP channel state
228 * @CH_CONNECTING: QP is in RTR state; waiting for RTU.
229 * @CH_LIVE: QP is in RTS state.
230 * @CH_DISCONNECTING: DREQ has been sent and waiting for DREP or DREQ has
232 * @CH_DRAINING: DREP has been received or waiting for DREP timed out
233 * and last work request has been queued.
234 * @CH_DISCONNECTED: Last completion has been received.
245 * struct srpt_rdma_ch - RDMA channel
246 * @nexus: I_T nexus this channel is associated with.
247 * @qp: IB queue pair used for communicating over this channel.
248 * @cm_id: IB CM ID associated with the channel.
249 * @cq: IB completion queue for this channel.
250 * @zw_cqe: Zero-length write CQE.
252 * @kref: kref for this channel.
253 * @rq_size: IB receive queue size.
254 * @max_rsp_size: Maximum size of an RSP response message in bytes.
255 * @sq_wr_avail: number of work requests available in the send queue.
256 * @sport: pointer to the information of the HCA port used by this
258 * @max_ti_iu_len: maximum target-to-initiator information unit length.
259 * @req_lim: request limit: maximum number of requests that may be sent
260 * by the initiator without having received a response.
261 * @req_lim_delta: Number of credits not yet sent back to the initiator.
262 * @spinlock: Protects free_list and state.
263 * @free_list: Head of list with free send I/O contexts.
264 * @state: channel state. See also enum rdma_ch_state.
265 * @using_rdma_cm: Whether the RDMA/CM or IB/CM is used for this channel.
266 * @processing_wait_list: Whether or not cmd_wait_list is being processed.
267 * @ioctx_ring: Send ring.
268 * @ioctx_recv_ring: Receive I/O context ring.
269 * @list: Node in srpt_nexus.ch_list.
270 * @cmd_wait_list: List of SCSI commands that arrived before the RTU event. This
271 * list contains struct srpt_ioctx elements and is protected
272 * against concurrent modification by the cm_id spinlock.
273 * @pkey: P_Key of the IB partition for this SRP channel.
274 * @sess: Session information associated with this SRP channel.
275 * @sess_name: Session name.
276 * @release_work: Allows scheduling of srpt_release_channel().
278 struct srpt_rdma_ch
{
279 struct srpt_nexus
*nexus
;
283 struct ib_cm_id
*cm_id
;
286 struct rdma_cm_id
*cm_id
;
290 struct ib_cqe zw_cqe
;
295 atomic_t sq_wr_avail
;
296 struct srpt_port
*sport
;
299 atomic_t req_lim_delta
;
301 struct list_head free_list
;
302 enum rdma_ch_state state
;
303 struct srpt_send_ioctx
**ioctx_ring
;
304 struct srpt_recv_ioctx
**ioctx_recv_ring
;
305 struct list_head list
;
306 struct list_head cmd_wait_list
;
309 bool processing_wait_list
;
310 struct se_session
*sess
;
312 struct work_struct release_work
;
316 * struct srpt_nexus - I_T nexus
317 * @rcu: RCU head for this data structure.
318 * @entry: srpt_port.nexus_list list node.
319 * @ch_list: struct srpt_rdma_ch list. Protected by srpt_port.mutex.
320 * @i_port_id: 128-bit initiator port identifier copied from SRP_LOGIN_REQ.
321 * @t_port_id: 128-bit target port identifier copied from SRP_LOGIN_REQ.
325 struct list_head entry
;
326 struct list_head ch_list
;
332 * struct srpt_port_attib - attributes for SRPT port
333 * @srp_max_rdma_size: Maximum size of SRP RDMA transfers for new connections.
334 * @srp_max_rsp_size: Maximum size of SRP response messages in bytes.
335 * @srp_sq_size: Shared receive queue (SRQ) size.
336 * @use_srq: Whether or not to use SRQ.
338 struct srpt_port_attrib
{
339 u32 srp_max_rdma_size
;
340 u32 srp_max_rsp_size
;
346 * struct srpt_port - information associated by SRPT with a single IB port
347 * @sdev: backpointer to the HCA information.
348 * @mad_agent: per-port management datagram processing information.
349 * @enabled: Whether or not this target port is enabled.
350 * @port_guid: ASCII representation of Port GUID
351 * @port_gid: ASCII representation of Port GID
352 * @port: one-based port number.
353 * @sm_lid: cached value of the port's sm_lid.
354 * @lid: cached value of the port's lid.
355 * @gid: cached value of the port's gid.
356 * @port_acl_lock spinlock for port_acl_list:
357 * @work: work structure for refreshing the aforementioned cached values.
358 * @port_guid_tpg: TPG associated with target port GUID.
359 * @port_guid_wwn: WWN associated with target port GUID.
360 * @port_gid_tpg: TPG associated with target port GID.
361 * @port_gid_wwn: WWN associated with target port GID.
362 * @port_attrib: Port attributes that can be accessed through configfs.
363 * @ch_releaseQ: Enables waiting for removal from nexus_list.
364 * @mutex: Protects nexus_list.
365 * @nexus_list: Nexus list. See also srpt_nexus.entry.
368 struct srpt_device
*sdev
;
369 struct ib_mad_agent
*mad_agent
;
377 struct work_struct work
;
378 struct se_portal_group port_guid_tpg
;
379 struct se_wwn port_guid_wwn
;
380 struct se_portal_group port_gid_tpg
;
381 struct se_wwn port_gid_wwn
;
382 struct srpt_port_attrib port_attrib
;
383 wait_queue_head_t ch_releaseQ
;
385 struct list_head nexus_list
;
389 * struct srpt_device - information associated by SRPT with a single HCA
390 * @device: Backpointer to the struct ib_device managed by the IB core.
391 * @pd: IB protection domain.
392 * @lkey: L_Key (local key) with write access to all local memory.
393 * @srq: Per-HCA SRQ (shared receive queue).
394 * @cm_id: Connection identifier.
395 * @srq_size: SRQ size.
396 * @sdev_mutex: Serializes use_srq changes.
397 * @use_srq: Whether or not to use SRQ.
398 * @ioctx_ring: Per-HCA SRQ.
399 * @event_handler: Per-HCA asynchronous IB event handler.
400 * @list: Node in srpt_dev_list.
401 * @port: Information about the ports owned by this HCA.
404 struct ib_device
*device
;
408 struct ib_cm_id
*cm_id
;
410 struct mutex sdev_mutex
;
412 struct srpt_recv_ioctx
**ioctx_ring
;
413 struct ib_event_handler event_handler
;
414 struct list_head list
;
415 struct srpt_port port
[];
418 #endif /* IB_SRPT_H */