ib_srpt.h 15 KB

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