ib_srpt.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. #ifndef IB_SRPT_H
  35. #define IB_SRPT_H
  36. #include <linux/version.h>
  37. #include <linux/types.h>
  38. #include <linux/list.h>
  39. #include <linux/wait.h>
  40. #include <rdma/ib_verbs.h>
  41. #include <rdma/ib_sa.h>
  42. #include <rdma/ib_cm.h>
  43. #include <scsi/srp.h>
  44. #include "ib_dm_mad.h"
  45. /*
  46. * The prefix the ServiceName field must start with in the device management
  47. * ServiceEntries attribute pair. See also the SRP specification.
  48. */
  49. #define SRP_SERVICE_NAME_PREFIX "SRP.T10:"
  50. enum {
  51. /*
  52. * SRP IOControllerProfile attributes for SRP target ports that have
  53. * not been defined in <scsi/srp.h>. Source: section B.7, table B.7
  54. * in the SRP specification.
  55. */
  56. SRP_PROTOCOL = 0x0108,
  57. SRP_PROTOCOL_VERSION = 0x0001,
  58. SRP_IO_SUBCLASS = 0x609e,
  59. SRP_SEND_TO_IOC = 0x01,
  60. SRP_SEND_FROM_IOC = 0x02,
  61. SRP_RDMA_READ_FROM_IOC = 0x08,
  62. SRP_RDMA_WRITE_FROM_IOC = 0x20,
  63. /*
  64. * srp_login_cmd.req_flags bitmasks. See also table 9 in the SRP
  65. * specification.
  66. */
  67. SRP_MTCH_ACTION = 0x03, /* MULTI-CHANNEL ACTION */
  68. SRP_LOSOLNT = 0x10, /* logout solicited notification */
  69. SRP_CRSOLNT = 0x20, /* credit request solicited notification */
  70. SRP_AESOLNT = 0x40, /* asynchronous event solicited notification */
  71. /*
  72. * srp_cmd.sol_nt / srp_tsk_mgmt.sol_not bitmasks. See also tables
  73. * 18 and 20 in the SRP specification.
  74. */
  75. SRP_SCSOLNT = 0x02, /* SCSOLNT = successful solicited notification */
  76. SRP_UCSOLNT = 0x04, /* UCSOLNT = unsuccessful solicited notification */
  77. /*
  78. * srp_rsp.sol_not / srp_t_logout.sol_not bitmasks. See also tables
  79. * 16 and 22 in the SRP specification.
  80. */
  81. SRP_SOLNT = 0x01, /* SOLNT = solicited notification */
  82. /* See also table 24 in the SRP specification. */
  83. SRP_TSK_MGMT_SUCCESS = 0x00,
  84. SRP_TSK_MGMT_FUNC_NOT_SUPP = 0x04,
  85. SRP_TSK_MGMT_FAILED = 0x05,
  86. /* See also table 21 in the SRP specification. */
  87. SRP_CMD_SIMPLE_Q = 0x0,
  88. SRP_CMD_HEAD_OF_Q = 0x1,
  89. SRP_CMD_ORDERED_Q = 0x2,
  90. SRP_CMD_ACA = 0x4,
  91. SRP_LOGIN_RSP_MULTICHAN_NO_CHAN = 0x0,
  92. SRP_LOGIN_RSP_MULTICHAN_TERMINATED = 0x1,
  93. SRP_LOGIN_RSP_MULTICHAN_MAINTAINED = 0x2,
  94. SRPT_DEF_SG_TABLESIZE = 128,
  95. SRPT_DEF_SG_PER_WQE = 16,
  96. MIN_SRPT_SQ_SIZE = 16,
  97. DEF_SRPT_SQ_SIZE = 4096,
  98. SRPT_RQ_SIZE = 128,
  99. MIN_SRPT_SRQ_SIZE = 4,
  100. DEFAULT_SRPT_SRQ_SIZE = 4095,
  101. MAX_SRPT_SRQ_SIZE = 65535,
  102. MAX_SRPT_RDMA_SIZE = 1U << 24,
  103. MAX_SRPT_RSP_SIZE = 1024,
  104. MIN_MAX_REQ_SIZE = 996,
  105. DEFAULT_MAX_REQ_SIZE
  106. = sizeof(struct srp_cmd)/*48*/
  107. + sizeof(struct srp_indirect_buf)/*20*/
  108. + 128 * sizeof(struct srp_direct_buf)/*16*/,
  109. MIN_MAX_RSP_SIZE = sizeof(struct srp_rsp)/*36*/ + 4,
  110. DEFAULT_MAX_RSP_SIZE = 256, /* leaves 220 bytes for sense data */
  111. DEFAULT_MAX_RDMA_SIZE = 65536,
  112. };
  113. enum srpt_opcode {
  114. SRPT_RECV,
  115. SRPT_SEND,
  116. SRPT_RDMA_MID,
  117. SRPT_RDMA_ABORT,
  118. SRPT_RDMA_READ_LAST,
  119. SRPT_RDMA_WRITE_LAST,
  120. };
  121. static inline u64 encode_wr_id(u8 opcode, u32 idx)
  122. {
  123. return ((u64)opcode << 32) | idx;
  124. }
  125. static inline enum srpt_opcode opcode_from_wr_id(u64 wr_id)
  126. {
  127. return wr_id >> 32;
  128. }
  129. static inline u32 idx_from_wr_id(u64 wr_id)
  130. {
  131. return (u32)wr_id;
  132. }
  133. struct rdma_iu {
  134. u64 raddr;
  135. u32 rkey;
  136. struct ib_sge *sge;
  137. u32 sge_cnt;
  138. int mem_id;
  139. };
  140. /**
  141. * enum srpt_command_state - SCSI command state managed by SRPT.
  142. * @SRPT_STATE_NEW: New command arrived and is being processed.
  143. * @SRPT_STATE_NEED_DATA: Processing a write or bidir command and waiting
  144. * for data arrival.
  145. * @SRPT_STATE_DATA_IN: Data for the write or bidir command arrived and is
  146. * being processed.
  147. * @SRPT_STATE_CMD_RSP_SENT: SRP_RSP for SRP_CMD has been sent.
  148. * @SRPT_STATE_MGMT: Processing a SCSI task management command.
  149. * @SRPT_STATE_MGMT_RSP_SENT: SRP_RSP for SRP_TSK_MGMT has been sent.
  150. * @SRPT_STATE_DONE: Command processing finished successfully, command
  151. * processing has been aborted or command processing
  152. * failed.
  153. */
  154. enum srpt_command_state {
  155. SRPT_STATE_NEW = 0,
  156. SRPT_STATE_NEED_DATA = 1,
  157. SRPT_STATE_DATA_IN = 2,
  158. SRPT_STATE_CMD_RSP_SENT = 3,
  159. SRPT_STATE_MGMT = 4,
  160. SRPT_STATE_MGMT_RSP_SENT = 5,
  161. SRPT_STATE_DONE = 6,
  162. };
  163. /**
  164. * struct srpt_ioctx - Shared SRPT I/O context information.
  165. * @buf: Pointer to the buffer.
  166. * @dma: DMA address of the buffer.
  167. * @index: Index of the I/O context in its ioctx_ring array.
  168. */
  169. struct srpt_ioctx {
  170. void *buf;
  171. dma_addr_t dma;
  172. uint32_t index;
  173. };
  174. /**
  175. * struct srpt_recv_ioctx - SRPT receive I/O context.
  176. * @ioctx: See above.
  177. * @wait_list: Node for insertion in srpt_rdma_ch.cmd_wait_list.
  178. */
  179. struct srpt_recv_ioctx {
  180. struct srpt_ioctx ioctx;
  181. struct list_head wait_list;
  182. };
  183. /**
  184. * struct srpt_send_ioctx - SRPT send I/O context.
  185. * @ioctx: See above.
  186. * @ch: Channel pointer.
  187. * @free_list: Node in srpt_rdma_ch.free_list.
  188. * @n_rbuf: Number of data buffers in the received SRP command.
  189. * @rbufs: Pointer to SRP data buffer array.
  190. * @single_rbuf: SRP data buffer if the command has only a single buffer.
  191. * @sg: Pointer to sg-list associated with this I/O context.
  192. * @sg_cnt: SG-list size.
  193. * @mapped_sg_count: ib_dma_map_sg() return value.
  194. * @n_rdma_ius: Number of elements in the rdma_ius array.
  195. * @rdma_ius: Array with information about the RDMA mapping.
  196. * @tag: Tag of the received SRP information unit.
  197. * @spinlock: Protects 'state'.
  198. * @state: I/O context state.
  199. * @rdma_aborted: If initiating a multipart RDMA transfer failed, whether
  200. * the already initiated transfers have finished.
  201. * @cmd: Target core command data structure.
  202. * @sense_data: SCSI sense data.
  203. */
  204. struct srpt_send_ioctx {
  205. struct srpt_ioctx ioctx;
  206. struct srpt_rdma_ch *ch;
  207. struct kref kref;
  208. struct rdma_iu *rdma_ius;
  209. struct srp_direct_buf *rbufs;
  210. struct srp_direct_buf single_rbuf;
  211. struct scatterlist *sg;
  212. struct list_head free_list;
  213. spinlock_t spinlock;
  214. enum srpt_command_state state;
  215. bool rdma_aborted;
  216. struct se_cmd cmd;
  217. struct completion tx_done;
  218. u64 tag;
  219. int sg_cnt;
  220. int mapped_sg_count;
  221. u16 n_rdma_ius;
  222. u8 n_rdma;
  223. u8 n_rbuf;
  224. bool queue_status_only;
  225. u8 sense_data[SCSI_SENSE_BUFFERSIZE];
  226. };
  227. /**
  228. * enum rdma_ch_state - SRP channel state.
  229. * @CH_CONNECTING: QP is in RTR state; waiting for RTU.
  230. * @CH_LIVE: QP is in RTS state.
  231. * @CH_DISCONNECTING: DREQ has been received; waiting for DREP
  232. * or DREQ has been send and waiting for DREP
  233. * or .
  234. * @CH_DRAINING: QP is in ERR state; waiting for last WQE event.
  235. * @CH_RELEASING: Last WQE event has been received; releasing resources.
  236. */
  237. enum rdma_ch_state {
  238. CH_CONNECTING,
  239. CH_LIVE,
  240. CH_DISCONNECTING,
  241. CH_DRAINING,
  242. CH_RELEASING
  243. };
  244. /**
  245. * struct srpt_rdma_ch - RDMA channel.
  246. * @wait_queue: Allows the kernel thread to wait for more work.
  247. * @thread: Kernel thread that processes the IB queues associated with
  248. * the channel.
  249. * @cm_id: IB CM ID associated with the channel.
  250. * @qp: IB queue pair used for communicating over this channel.
  251. * @cq: IB completion queue for this channel.
  252. * @rq_size: IB receive queue size.
  253. * @rsp_size IB response message size in bytes.
  254. * @sq_wr_avail: number of work requests available in the send queue.
  255. * @sport: pointer to the information of the HCA port used by this
  256. * channel.
  257. * @i_port_id: 128-bit initiator port identifier copied from SRP_LOGIN_REQ.
  258. * @t_port_id: 128-bit target port identifier copied from SRP_LOGIN_REQ.
  259. * @max_ti_iu_len: maximum target-to-initiator information unit length.
  260. * @req_lim: request limit: maximum number of requests that may be sent
  261. * by the initiator without having received a response.
  262. * @req_lim_delta: Number of credits not yet sent back to the initiator.
  263. * @spinlock: Protects free_list and state.
  264. * @free_list: Head of list with free send I/O contexts.
  265. * @state: channel state. See also enum rdma_ch_state.
  266. * @ioctx_ring: Send ring.
  267. * @wc: IB work completion array for srpt_process_completion().
  268. * @list: Node for insertion in the srpt_device.rch_list list.
  269. * @cmd_wait_list: List of SCSI commands that arrived before the RTU event. This
  270. * list contains struct srpt_ioctx elements and is protected
  271. * against concurrent modification by the cm_id spinlock.
  272. * @sess: Session information associated with this SRP channel.
  273. * @sess_name: Session name.
  274. * @release_work: Allows scheduling of srpt_release_channel().
  275. * @release_done: Enables waiting for srpt_release_channel() completion.
  276. */
  277. struct srpt_rdma_ch {
  278. wait_queue_head_t wait_queue;
  279. struct task_struct *thread;
  280. struct ib_cm_id *cm_id;
  281. struct ib_qp *qp;
  282. struct ib_cq *cq;
  283. int rq_size;
  284. u32 rsp_size;
  285. atomic_t sq_wr_avail;
  286. struct srpt_port *sport;
  287. u8 i_port_id[16];
  288. u8 t_port_id[16];
  289. int max_ti_iu_len;
  290. atomic_t req_lim;
  291. atomic_t req_lim_delta;
  292. spinlock_t spinlock;
  293. struct list_head free_list;
  294. enum rdma_ch_state state;
  295. struct srpt_send_ioctx **ioctx_ring;
  296. struct ib_wc wc[16];
  297. struct list_head list;
  298. struct list_head cmd_wait_list;
  299. struct se_session *sess;
  300. u8 sess_name[36];
  301. struct work_struct release_work;
  302. struct completion *release_done;
  303. };
  304. /**
  305. * struct srpt_port_attib - Attributes for SRPT port
  306. * @srp_max_rdma_size: Maximum size of SRP RDMA transfers for new connections.
  307. * @srp_max_rsp_size: Maximum size of SRP response messages in bytes.
  308. * @srp_sq_size: Shared receive queue (SRQ) size.
  309. */
  310. struct srpt_port_attrib {
  311. u32 srp_max_rdma_size;
  312. u32 srp_max_rsp_size;
  313. u32 srp_sq_size;
  314. };
  315. /**
  316. * struct srpt_port - Information associated by SRPT with a single IB port.
  317. * @sdev: backpointer to the HCA information.
  318. * @mad_agent: per-port management datagram processing information.
  319. * @enabled: Whether or not this target port is enabled.
  320. * @port_guid: ASCII representation of Port GUID
  321. * @port: one-based port number.
  322. * @sm_lid: cached value of the port's sm_lid.
  323. * @lid: cached value of the port's lid.
  324. * @gid: cached value of the port's gid.
  325. * @port_acl_lock spinlock for port_acl_list:
  326. * @work: work structure for refreshing the aforementioned cached values.
  327. * @port_tpg_1 Target portal group = 1 data.
  328. * @port_wwn: Target core WWN data.
  329. * @port_acl_list: Head of the list with all node ACLs for this port.
  330. */
  331. struct srpt_port {
  332. struct srpt_device *sdev;
  333. struct ib_mad_agent *mad_agent;
  334. bool enabled;
  335. u8 port_guid[64];
  336. u8 port;
  337. u16 sm_lid;
  338. u16 lid;
  339. union ib_gid gid;
  340. spinlock_t port_acl_lock;
  341. struct work_struct work;
  342. struct se_portal_group port_tpg_1;
  343. struct se_wwn port_wwn;
  344. struct list_head port_acl_list;
  345. struct srpt_port_attrib port_attrib;
  346. };
  347. /**
  348. * struct srpt_device - Information associated by SRPT with a single HCA.
  349. * @device: Backpointer to the struct ib_device managed by the IB core.
  350. * @pd: IB protection domain.
  351. * @mr: L_Key (local key) with write access to all local memory.
  352. * @srq: Per-HCA SRQ (shared receive queue).
  353. * @cm_id: Connection identifier.
  354. * @dev_attr: Attributes of the InfiniBand device as obtained during the
  355. * ib_client.add() callback.
  356. * @srq_size: SRQ size.
  357. * @ioctx_ring: Per-HCA SRQ.
  358. * @rch_list: Per-device channel list -- see also srpt_rdma_ch.list.
  359. * @ch_releaseQ: Enables waiting for removal from rch_list.
  360. * @spinlock: Protects rch_list and tpg.
  361. * @port: Information about the ports owned by this HCA.
  362. * @event_handler: Per-HCA asynchronous IB event handler.
  363. * @list: Node in srpt_dev_list.
  364. */
  365. struct srpt_device {
  366. struct ib_device *device;
  367. struct ib_pd *pd;
  368. struct ib_mr *mr;
  369. struct ib_srq *srq;
  370. struct ib_cm_id *cm_id;
  371. struct ib_device_attr dev_attr;
  372. int srq_size;
  373. struct srpt_recv_ioctx **ioctx_ring;
  374. struct list_head rch_list;
  375. wait_queue_head_t ch_releaseQ;
  376. spinlock_t spinlock;
  377. struct srpt_port port[2];
  378. struct ib_event_handler event_handler;
  379. struct list_head list;
  380. };
  381. /**
  382. * struct srpt_node_acl - Per-initiator ACL data (managed via configfs).
  383. * @i_port_id: 128-bit SRP initiator port ID.
  384. * @sport: port information.
  385. * @nacl: Target core node ACL information.
  386. * @list: Element of the per-HCA ACL list.
  387. */
  388. struct srpt_node_acl {
  389. u8 i_port_id[16];
  390. struct srpt_port *sport;
  391. struct se_node_acl nacl;
  392. struct list_head list;
  393. };
  394. /*
  395. * SRP-releated SCSI persistent reservation definitions.
  396. *
  397. * See also SPC4r28, section 7.6.1 (Protocol specific parameters introduction).
  398. * See also SPC4r28, section 7.6.4.5 (TransportID for initiator ports using
  399. * SCSI over an RDMA interface).
  400. */
  401. enum {
  402. SCSI_TRANSPORTID_PROTOCOLID_SRP = 4,
  403. };
  404. struct spc_rdma_transport_id {
  405. uint8_t protocol_identifier;
  406. uint8_t reserved[7];
  407. uint8_t i_port_id[16];
  408. };
  409. #endif /* IB_SRPT_H */