ib_srpt.h 12.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*
 * Copyright (c) 2006 - 2009 Mellanox Technology Inc.  All rights reserved.
 * Copyright (C) 2009 - 2010 Bart Van Assche <bvanassche@acm.org>.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

#ifndef IB_SRPT_H
#define IB_SRPT_H

#include <linux/types.h>
#include <linux/list.h>
#include <linux/wait.h>

#include <rdma/ib_verbs.h>
#include <rdma/ib_sa.h>
#include <rdma/ib_cm.h>
45
#include <rdma/rw.h>
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

#include <scsi/srp.h>

#include "ib_dm_mad.h"

/*
 * The prefix the ServiceName field must start with in the device management
 * ServiceEntries attribute pair. See also the SRP specification.
 */
#define SRP_SERVICE_NAME_PREFIX		"SRP.T10:"

enum {
	/*
	 * SRP IOControllerProfile attributes for SRP target ports that have
	 * not been defined in <scsi/srp.h>. Source: section B.7, table B.7
	 * in the SRP specification.
	 */
	SRP_PROTOCOL = 0x0108,
	SRP_PROTOCOL_VERSION = 0x0001,
	SRP_IO_SUBCLASS = 0x609e,
	SRP_SEND_TO_IOC = 0x01,
	SRP_SEND_FROM_IOC = 0x02,
	SRP_RDMA_READ_FROM_IOC = 0x08,
	SRP_RDMA_WRITE_FROM_IOC = 0x20,

	/*
	 * srp_login_cmd.req_flags bitmasks. See also table 9 in the SRP
	 * specification.
	 */
	SRP_MTCH_ACTION = 0x03, /* MULTI-CHANNEL ACTION */
	SRP_LOSOLNT = 0x10, /* logout solicited notification */
	SRP_CRSOLNT = 0x20, /* credit request solicited notification */
	SRP_AESOLNT = 0x40, /* asynchronous event solicited notification */

	/*
	 * srp_cmd.sol_nt / srp_tsk_mgmt.sol_not bitmasks. See also tables
	 * 18 and 20 in the SRP specification.
	 */
	SRP_SCSOLNT = 0x02, /* SCSOLNT = successful solicited notification */
	SRP_UCSOLNT = 0x04, /* UCSOLNT = unsuccessful solicited notification */

	/*
	 * srp_rsp.sol_not / srp_t_logout.sol_not bitmasks. See also tables
	 * 16 and 22 in the SRP specification.
	 */
	SRP_SOLNT = 0x01, /* SOLNT = solicited notification */

	/* See also table 24 in the SRP specification. */
	SRP_TSK_MGMT_SUCCESS = 0x00,
	SRP_TSK_MGMT_FUNC_NOT_SUPP = 0x04,
	SRP_TSK_MGMT_FAILED = 0x05,

	/* See also table 21 in the SRP specification. */
	SRP_CMD_SIMPLE_Q = 0x0,
	SRP_CMD_HEAD_OF_Q = 0x1,
	SRP_CMD_ORDERED_Q = 0x2,
	SRP_CMD_ACA = 0x4,

	SRP_LOGIN_RSP_MULTICHAN_NO_CHAN = 0x0,
	SRP_LOGIN_RSP_MULTICHAN_TERMINATED = 0x1,
	SRP_LOGIN_RSP_MULTICHAN_MAINTAINED = 0x2,

	SRPT_DEF_SG_TABLESIZE = 128,
109 110 111 112 113
	/*
	 * An experimentally determined value that avoids that QP creation
	 * fails due to "swiotlb buffer is full" on systems using the swiotlb.
	 */
	SRPT_MAX_SG_PER_WQE = 16,
114 115 116

	MIN_SRPT_SQ_SIZE = 16,
	DEF_SRPT_SQ_SIZE = 4096,
117
	MAX_SRPT_RQ_SIZE = 128,
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	MIN_SRPT_SRQ_SIZE = 4,
	DEFAULT_SRPT_SRQ_SIZE = 4095,
	MAX_SRPT_SRQ_SIZE = 65535,
	MAX_SRPT_RDMA_SIZE = 1U << 24,
	MAX_SRPT_RSP_SIZE = 1024,

	MIN_MAX_REQ_SIZE = 996,
	DEFAULT_MAX_REQ_SIZE
		= sizeof(struct srp_cmd)/*48*/
		+ sizeof(struct srp_indirect_buf)/*20*/
		+ 128 * sizeof(struct srp_direct_buf)/*16*/,

	MIN_MAX_RSP_SIZE = sizeof(struct srp_rsp)/*36*/ + 4,
	DEFAULT_MAX_RSP_SIZE = 256, /* leaves 220 bytes for sense data */

	DEFAULT_MAX_RDMA_SIZE = 65536,
};

/**
137
 * enum srpt_command_state - SCSI command state managed by SRPT
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
 * @SRPT_STATE_NEW:           New command arrived and is being processed.
 * @SRPT_STATE_NEED_DATA:     Processing a write or bidir command and waiting
 *                            for data arrival.
 * @SRPT_STATE_DATA_IN:       Data for the write or bidir command arrived and is
 *                            being processed.
 * @SRPT_STATE_CMD_RSP_SENT:  SRP_RSP for SRP_CMD has been sent.
 * @SRPT_STATE_MGMT:          Processing a SCSI task management command.
 * @SRPT_STATE_MGMT_RSP_SENT: SRP_RSP for SRP_TSK_MGMT has been sent.
 * @SRPT_STATE_DONE:          Command processing finished successfully, command
 *                            processing has been aborted or command processing
 *                            failed.
 */
enum srpt_command_state {
	SRPT_STATE_NEW		 = 0,
	SRPT_STATE_NEED_DATA	 = 1,
	SRPT_STATE_DATA_IN	 = 2,
	SRPT_STATE_CMD_RSP_SENT	 = 3,
	SRPT_STATE_MGMT		 = 4,
	SRPT_STATE_MGMT_RSP_SENT = 5,
	SRPT_STATE_DONE		 = 6,
};

/**
161 162
 * struct srpt_ioctx - shared SRPT I/O context information
 * @cqe:   Completion queue element.
163 164 165 166 167
 * @buf:   Pointer to the buffer.
 * @dma:   DMA address of the buffer.
 * @index: Index of the I/O context in its ioctx_ring array.
 */
struct srpt_ioctx {
168
	struct ib_cqe		cqe;
169 170 171 172 173 174
	void			*buf;
	dma_addr_t		dma;
	uint32_t		index;
};

/**
175
 * struct srpt_recv_ioctx - SRPT receive I/O context
176 177 178 179 180 181 182
 * @ioctx:     See above.
 * @wait_list: Node for insertion in srpt_rdma_ch.cmd_wait_list.
 */
struct srpt_recv_ioctx {
	struct srpt_ioctx	ioctx;
	struct list_head	wait_list;
};
183 184 185 186 187 188
	
struct srpt_rw_ctx {
	struct rdma_rw_ctx	rw;
	struct scatterlist	*sg;
	unsigned int		nents;
};
189 190

/**
191
 * struct srpt_send_ioctx - SRPT send I/O context
192 193
 * @ioctx:       See above.
 * @ch:          Channel pointer.
194 195 196 197
 * @s_rw_ctx:    @rw_ctxs points here if only a single rw_ctx is needed.
 * @rw_ctxs:     RDMA read/write contexts.
 * @rdma_cqe:    RDMA completion queue element.
 * @free_list:   Node in srpt_rdma_ch.free_list.
198 199 200
 * @state:       I/O context state.
 * @cmd:         Target core command data structure.
 * @sense_data:  SCSI sense data.
201 202 203 204
 * @n_rdma:      Number of work requests needed to transfer this ioctx.
 * @n_rw_ctx:    Size of rw_ctxs array.
 * @queue_status_only: Send a SCSI status back to the initiator but no data.
 * @sense_data:  Sense data to be sent to the initiator.
205 206 207 208
 */
struct srpt_send_ioctx {
	struct srpt_ioctx	ioctx;
	struct srpt_rdma_ch	*ch;
209 210 211 212

	struct srpt_rw_ctx	s_rw_ctx;
	struct srpt_rw_ctx	*rw_ctxs;

213
	struct ib_cqe		rdma_cqe;
214
	struct list_head	free_list;
215 216 217
	enum srpt_command_state	state;
	struct se_cmd		cmd;
	u8			n_rdma;
218
	u8			n_rw_ctx;
219
	bool			queue_status_only;
220
	u8			sense_data[TRANSPORT_SENSE_BUFFER];
221 222 223
};

/**
224
 * enum rdma_ch_state - SRP channel state
225 226 227 228 229 230 231
 * @CH_CONNECTING:    QP is in RTR state; waiting for RTU.
 * @CH_LIVE:	      QP is in RTS state.
 * @CH_DISCONNECTING: DREQ has been sent and waiting for DREP or DREQ has
 *                    been received.
 * @CH_DRAINING:      DREP has been received or waiting for DREP timed out
 *                    and last work request has been queued.
 * @CH_DISCONNECTED:  Last completion has been received.
232 233 234 235 236 237
 */
enum rdma_ch_state {
	CH_CONNECTING,
	CH_LIVE,
	CH_DISCONNECTING,
	CH_DRAINING,
238
	CH_DISCONNECTED,
239 240 241
};

/**
242
 * struct srpt_rdma_ch - RDMA channel
243 244 245
 * @cm_id:         IB CM ID associated with the channel.
 * @qp:            IB queue pair used for communicating over this channel.
 * @cq:            IB completion queue for this channel.
246
 * @zw_cqe:	   Zero-length write CQE.
247
 * @rcu:           RCU head.
248
 * @kref:	   kref for this channel.
249
 * @rq_size:       IB receive queue size.
250
 * @max_rsp_size:  Maximum size of an RSP response message in bytes.
251 252 253 254 255 256 257 258 259 260 261 262 263
 * @sq_wr_avail:   number of work requests available in the send queue.
 * @sport:         pointer to the information of the HCA port used by this
 *                 channel.
 * @i_port_id:     128-bit initiator port identifier copied from SRP_LOGIN_REQ.
 * @t_port_id:     128-bit target port identifier copied from SRP_LOGIN_REQ.
 * @max_ti_iu_len: maximum target-to-initiator information unit length.
 * @req_lim:       request limit: maximum number of requests that may be sent
 *                 by the initiator without having received a response.
 * @req_lim_delta: Number of credits not yet sent back to the initiator.
 * @spinlock:      Protects free_list and state.
 * @free_list:     Head of list with free send I/O contexts.
 * @state:         channel state. See also enum rdma_ch_state.
 * @ioctx_ring:    Send ring.
264
 * @ioctx_recv_ring: Receive I/O context ring.
265 266 267 268
 * @list:          Node for insertion in the srpt_device.rch_list list.
 * @cmd_wait_list: List of SCSI commands that arrived before the RTU event. This
 *                 list contains struct srpt_ioctx elements and is protected
 *                 against concurrent modification by the cm_id spinlock.
B
Bart Van Assche 已提交
269
 * @pkey:          P_Key of the IB partition for this SRP channel.
270 271
 * @sess:          Session information associated with this SRP channel.
 * @sess_name:     Session name.
272
 * @ini_guid:      Initiator port GUID.
273 274 275 276 277 278
 * @release_work:  Allows scheduling of srpt_release_channel().
 */
struct srpt_rdma_ch {
	struct ib_cm_id		*cm_id;
	struct ib_qp		*qp;
	struct ib_cq		*cq;
279
	struct ib_cqe		zw_cqe;
280
	struct rcu_head		rcu;
281
	struct kref		kref;
282
	int			rq_size;
283
	u32			max_rsp_size;
284 285 286 287 288 289 290 291 292 293 294
	atomic_t		sq_wr_avail;
	struct srpt_port	*sport;
	u8			i_port_id[16];
	u8			t_port_id[16];
	int			max_ti_iu_len;
	atomic_t		req_lim;
	atomic_t		req_lim_delta;
	spinlock_t		spinlock;
	struct list_head	free_list;
	enum rdma_ch_state	state;
	struct srpt_send_ioctx	**ioctx_ring;
295
	struct srpt_recv_ioctx	**ioctx_recv_ring;
296 297
	struct list_head	list;
	struct list_head	cmd_wait_list;
B
Bart Van Assche 已提交
298
	uint16_t		pkey;
299 300
	struct se_session	*sess;
	u8			sess_name[36];
301
	u8			ini_guid[24];
302 303 304 305
	struct work_struct	release_work;
};

/**
306
 * struct srpt_port_attib - attributes for SRPT port
307 308 309
 * @srp_max_rdma_size: Maximum size of SRP RDMA transfers for new connections.
 * @srp_max_rsp_size: Maximum size of SRP response messages in bytes.
 * @srp_sq_size: Shared receive queue (SRQ) size.
310
 * @use_srq: Whether or not to use SRQ.
311 312 313 314 315
 */
struct srpt_port_attrib {
	u32			srp_max_rdma_size;
	u32			srp_max_rsp_size;
	u32			srp_sq_size;
316
	bool			use_srq;
317 318 319
};

/**
320
 * struct srpt_port - information associated by SRPT with a single IB port
321 322 323 324
 * @sdev:      backpointer to the HCA information.
 * @mad_agent: per-port management datagram processing information.
 * @enabled:   Whether or not this target port is enabled.
 * @port_guid: ASCII representation of Port GUID
325
 * @port_gid:  ASCII representation of Port GID
326 327 328 329 330 331
 * @port:      one-based port number.
 * @sm_lid:    cached value of the port's sm_lid.
 * @lid:       cached value of the port's lid.
 * @gid:       cached value of the port's gid.
 * @port_acl_lock spinlock for port_acl_list:
 * @work:      work structure for refreshing the aforementioned cached values.
332 333 334 335
 * @port_guid_tpg: TPG associated with target port GUID.
 * @port_guid_wwn: WWN associated with target port GUID.
 * @port_gid_tpg:  TPG associated with target port GID.
 * @port_gid_wwn:  WWN associated with target port GID.
336
 * @port_attrib:   Port attributes that can be accessed through configfs.
337 338 339 340 341
 */
struct srpt_port {
	struct srpt_device	*sdev;
	struct ib_mad_agent	*mad_agent;
	bool			enabled;
342 343
	u8			port_guid[24];
	u8			port_gid[64];
344
	u8			port;
345 346
	u32			sm_lid;
	u32			lid;
347 348
	union ib_gid		gid;
	struct work_struct	work;
349 350 351 352
	struct se_portal_group	port_guid_tpg;
	struct se_wwn		port_guid_wwn;
	struct se_portal_group	port_gid_tpg;
	struct se_wwn		port_gid_wwn;
353 354 355 356
	struct srpt_port_attrib port_attrib;
};

/**
357
 * struct srpt_device - information associated by SRPT with a single HCA
358 359
 * @device:        Backpointer to the struct ib_device managed by the IB core.
 * @pd:            IB protection domain.
B
Bart Van Assche 已提交
360
 * @lkey:          L_Key (local key) with write access to all local memory.
361 362 363
 * @srq:           Per-HCA SRQ (shared receive queue).
 * @cm_id:         Connection identifier.
 * @srq_size:      SRQ size.
364
 * @use_srq:       Whether or not to use SRQ.
365 366 367
 * @ioctx_ring:    Per-HCA SRQ.
 * @rch_list:      Per-device channel list -- see also srpt_rdma_ch.list.
 * @ch_releaseQ:   Enables waiting for removal from rch_list.
368
 * @mutex:         Protects rch_list.
369 370 371 372 373 374 375
 * @port:          Information about the ports owned by this HCA.
 * @event_handler: Per-HCA asynchronous IB event handler.
 * @list:          Node in srpt_dev_list.
 */
struct srpt_device {
	struct ib_device	*device;
	struct ib_pd		*pd;
B
Bart Van Assche 已提交
376
	u32			lkey;
377 378 379
	struct ib_srq		*srq;
	struct ib_cm_id		*cm_id;
	int			srq_size;
380
	bool			use_srq;
381 382 383
	struct srpt_recv_ioctx	**ioctx_ring;
	struct list_head	rch_list;
	wait_queue_head_t	ch_releaseQ;
384
	struct mutex		mutex;
385 386 387 388 389 390
	struct srpt_port	port[2];
	struct ib_event_handler	event_handler;
	struct list_head	list;
};

#endif				/* IB_SRPT_H */