ib_srpt.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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/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. SRP_LOGIN_RSP_MULTICHAN_NO_CHAN = 0x0,
  94. SRP_LOGIN_RSP_MULTICHAN_TERMINATED = 0x1,
  95. SRP_LOGIN_RSP_MULTICHAN_MAINTAINED = 0x2,
  96. SRPT_DEF_SG_TABLESIZE = 128,
  97. /*
  98. * An experimentally determined value that avoids that QP creation
  99. * fails due to "swiotlb buffer is full" on systems using the swiotlb.
  100. */
  101. SRPT_MAX_SG_PER_WQE = 16,
  102. MIN_SRPT_SQ_SIZE = 16,
  103. DEF_SRPT_SQ_SIZE = 4096,
  104. MAX_SRPT_RQ_SIZE = 128,
  105. MIN_SRPT_SRQ_SIZE = 4,
  106. DEFAULT_SRPT_SRQ_SIZE = 4095,
  107. MAX_SRPT_SRQ_SIZE = 65535,
  108. MAX_SRPT_RDMA_SIZE = 1U << 24,
  109. MAX_SRPT_RSP_SIZE = 1024,
  110. MIN_MAX_REQ_SIZE = 996,
  111. DEFAULT_MAX_REQ_SIZE
  112. = sizeof(struct srp_cmd)/*48*/
  113. + sizeof(struct srp_indirect_buf)/*20*/
  114. + 128 * sizeof(struct srp_direct_buf)/*16*/,
  115. MIN_MAX_RSP_SIZE = sizeof(struct srp_rsp)/*36*/ + 4,
  116. DEFAULT_MAX_RSP_SIZE = 256, /* leaves 220 bytes for sense data */
  117. DEFAULT_MAX_RDMA_SIZE = 65536,
  118. };
  119. /**
  120. * enum srpt_command_state - SCSI command state managed by SRPT
  121. * @SRPT_STATE_NEW: New command arrived and is being processed.
  122. * @SRPT_STATE_NEED_DATA: Processing a write or bidir command and waiting
  123. * for data arrival.
  124. * @SRPT_STATE_DATA_IN: Data for the write or bidir command arrived and is
  125. * being processed.
  126. * @SRPT_STATE_CMD_RSP_SENT: SRP_RSP for SRP_CMD has been sent.
  127. * @SRPT_STATE_MGMT: Processing a SCSI task management command.
  128. * @SRPT_STATE_MGMT_RSP_SENT: SRP_RSP for SRP_TSK_MGMT has been sent.
  129. * @SRPT_STATE_DONE: Command processing finished successfully, command
  130. * processing has been aborted or command processing
  131. * failed.
  132. */
  133. enum srpt_command_state {
  134. SRPT_STATE_NEW = 0,
  135. SRPT_STATE_NEED_DATA = 1,
  136. SRPT_STATE_DATA_IN = 2,
  137. SRPT_STATE_CMD_RSP_SENT = 3,
  138. SRPT_STATE_MGMT = 4,
  139. SRPT_STATE_MGMT_RSP_SENT = 5,
  140. SRPT_STATE_DONE = 6,
  141. };
  142. /**
  143. * struct srpt_ioctx - shared SRPT I/O context information
  144. * @cqe: Completion queue element.
  145. * @buf: Pointer to the buffer.
  146. * @dma: DMA address of the buffer.
  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 index;
  154. };
  155. /**
  156. * struct srpt_recv_ioctx - SRPT receive I/O context
  157. * @ioctx: See above.
  158. * @wait_list: Node for insertion in srpt_rdma_ch.cmd_wait_list.
  159. */
  160. struct srpt_recv_ioctx {
  161. struct srpt_ioctx ioctx;
  162. struct list_head wait_list;
  163. };
  164. struct srpt_rw_ctx {
  165. struct rdma_rw_ctx rw;
  166. struct scatterlist *sg;
  167. unsigned int nents;
  168. };
  169. /**
  170. * struct srpt_send_ioctx - SRPT send I/O context
  171. * @ioctx: See above.
  172. * @ch: Channel pointer.
  173. * @s_rw_ctx: @rw_ctxs points here if only a single rw_ctx is needed.
  174. * @rw_ctxs: RDMA read/write contexts.
  175. * @rdma_cqe: RDMA completion queue element.
  176. * @free_list: Node in srpt_rdma_ch.free_list.
  177. * @state: I/O context state.
  178. * @cmd: Target core command data structure.
  179. * @sense_data: SCSI sense data.
  180. * @n_rdma: Number of work requests needed to transfer this ioctx.
  181. * @n_rw_ctx: Size of rw_ctxs array.
  182. * @queue_status_only: Send a SCSI status back to the initiator but no data.
  183. * @sense_data: Sense data to be sent to the initiator.
  184. */
  185. struct srpt_send_ioctx {
  186. struct srpt_ioctx ioctx;
  187. struct srpt_rdma_ch *ch;
  188. struct srpt_rw_ctx s_rw_ctx;
  189. struct srpt_rw_ctx *rw_ctxs;
  190. struct ib_cqe rdma_cqe;
  191. struct list_head free_list;
  192. enum srpt_command_state state;
  193. struct se_cmd cmd;
  194. u8 n_rdma;
  195. u8 n_rw_ctx;
  196. bool queue_status_only;
  197. u8 sense_data[TRANSPORT_SENSE_BUFFER];
  198. };
  199. /**
  200. * enum rdma_ch_state - SRP channel state
  201. * @CH_CONNECTING: QP is in RTR state; waiting for RTU.
  202. * @CH_LIVE: QP is in RTS state.
  203. * @CH_DISCONNECTING: DREQ has been sent and waiting for DREP or DREQ has
  204. * been received.
  205. * @CH_DRAINING: DREP has been received or waiting for DREP timed out
  206. * and last work request has been queued.
  207. * @CH_DISCONNECTED: Last completion has been received.
  208. */
  209. enum rdma_ch_state {
  210. CH_CONNECTING,
  211. CH_LIVE,
  212. CH_DISCONNECTING,
  213. CH_DRAINING,
  214. CH_DISCONNECTED,
  215. };
  216. /**
  217. * struct srpt_rdma_ch - RDMA channel
  218. * @nexus: I_T nexus this channel is associated with.
  219. * @qp: IB queue pair used for communicating over this channel.
  220. * @cm_id: IB CM ID associated with the channel.
  221. * @cq: IB completion queue for this channel.
  222. * @zw_cqe: Zero-length write CQE.
  223. * @rcu: RCU head.
  224. * @kref: kref for this channel.
  225. * @rq_size: IB receive queue size.
  226. * @max_rsp_size: Maximum size of an RSP response message in bytes.
  227. * @sq_wr_avail: number of work requests available in the send queue.
  228. * @sport: pointer to the information of the HCA port used by this
  229. * channel.
  230. * @max_ti_iu_len: maximum target-to-initiator information unit length.
  231. * @req_lim: request limit: maximum number of requests that may be sent
  232. * by the initiator without having received a response.
  233. * @req_lim_delta: Number of credits not yet sent back to the initiator.
  234. * @spinlock: Protects free_list and state.
  235. * @free_list: Head of list with free send I/O contexts.
  236. * @state: channel state. See also enum rdma_ch_state.
  237. * @using_rdma_cm: Whether the RDMA/CM or IB/CM is used for this channel.
  238. * @processing_wait_list: Whether or not cmd_wait_list is being processed.
  239. * @ioctx_ring: Send ring.
  240. * @ioctx_recv_ring: Receive I/O context ring.
  241. * @list: Node in srpt_nexus.ch_list.
  242. * @cmd_wait_list: List of SCSI commands that arrived before the RTU event. This
  243. * list contains struct srpt_ioctx elements and is protected
  244. * against concurrent modification by the cm_id spinlock.
  245. * @pkey: P_Key of the IB partition for this SRP channel.
  246. * @sess: Session information associated with this SRP channel.
  247. * @sess_name: Session name.
  248. * @release_work: Allows scheduling of srpt_release_channel().
  249. */
  250. struct srpt_rdma_ch {
  251. struct srpt_nexus *nexus;
  252. struct ib_qp *qp;
  253. union {
  254. struct {
  255. struct ib_cm_id *cm_id;
  256. } ib_cm;
  257. struct {
  258. struct rdma_cm_id *cm_id;
  259. } rdma_cm;
  260. };
  261. struct ib_cq *cq;
  262. struct ib_cqe zw_cqe;
  263. struct rcu_head rcu;
  264. struct kref kref;
  265. int rq_size;
  266. u32 max_rsp_size;
  267. atomic_t sq_wr_avail;
  268. struct srpt_port *sport;
  269. int max_ti_iu_len;
  270. atomic_t req_lim;
  271. atomic_t req_lim_delta;
  272. spinlock_t spinlock;
  273. struct list_head free_list;
  274. enum rdma_ch_state state;
  275. struct srpt_send_ioctx **ioctx_ring;
  276. struct srpt_recv_ioctx **ioctx_recv_ring;
  277. struct list_head list;
  278. struct list_head cmd_wait_list;
  279. uint16_t pkey;
  280. bool using_rdma_cm;
  281. bool processing_wait_list;
  282. struct se_session *sess;
  283. u8 sess_name[40];
  284. struct work_struct release_work;
  285. };
  286. /**
  287. * struct srpt_nexus - I_T nexus
  288. * @rcu: RCU head for this data structure.
  289. * @entry: srpt_port.nexus_list list node.
  290. * @ch_list: struct srpt_rdma_ch list. Protected by srpt_port.mutex.
  291. * @i_port_id: 128-bit initiator port identifier copied from SRP_LOGIN_REQ.
  292. * @t_port_id: 128-bit target port identifier copied from SRP_LOGIN_REQ.
  293. */
  294. struct srpt_nexus {
  295. struct rcu_head rcu;
  296. struct list_head entry;
  297. struct list_head ch_list;
  298. u8 i_port_id[16];
  299. u8 t_port_id[16];
  300. };
  301. /**
  302. * struct srpt_port_attib - attributes for SRPT port
  303. * @srp_max_rdma_size: Maximum size of SRP RDMA transfers for new connections.
  304. * @srp_max_rsp_size: Maximum size of SRP response messages in bytes.
  305. * @srp_sq_size: Shared receive queue (SRQ) size.
  306. * @use_srq: Whether or not to use SRQ.
  307. */
  308. struct srpt_port_attrib {
  309. u32 srp_max_rdma_size;
  310. u32 srp_max_rsp_size;
  311. u32 srp_sq_size;
  312. bool use_srq;
  313. };
  314. /**
  315. * struct srpt_port - information associated by SRPT with a single IB port
  316. * @sdev: backpointer to the HCA information.
  317. * @mad_agent: per-port management datagram processing information.
  318. * @enabled: Whether or not this target port is enabled.
  319. * @port_guid: ASCII representation of Port GUID
  320. * @port_gid: ASCII representation of Port GID
  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_guid_tpg: TPG associated with target port GUID.
  328. * @port_guid_wwn: WWN associated with target port GUID.
  329. * @port_gid_tpg: TPG associated with target port GID.
  330. * @port_gid_wwn: WWN associated with target port GID.
  331. * @port_attrib: Port attributes that can be accessed through configfs.
  332. * @ch_releaseQ: Enables waiting for removal from nexus_list.
  333. * @mutex: Protects nexus_list.
  334. * @nexus_list: Nexus list. See also srpt_nexus.entry.
  335. */
  336. struct srpt_port {
  337. struct srpt_device *sdev;
  338. struct ib_mad_agent *mad_agent;
  339. bool enabled;
  340. u8 port_guid[24];
  341. u8 port_gid[64];
  342. u8 port;
  343. u32 sm_lid;
  344. u32 lid;
  345. union ib_gid gid;
  346. struct work_struct work;
  347. struct se_portal_group port_guid_tpg;
  348. struct se_wwn port_guid_wwn;
  349. struct se_portal_group port_gid_tpg;
  350. struct se_wwn port_gid_wwn;
  351. struct srpt_port_attrib port_attrib;
  352. wait_queue_head_t ch_releaseQ;
  353. struct mutex mutex;
  354. struct list_head nexus_list;
  355. };
  356. /**
  357. * struct srpt_device - information associated by SRPT with a single HCA
  358. * @device: Backpointer to the struct ib_device managed by the IB core.
  359. * @pd: IB protection domain.
  360. * @lkey: L_Key (local key) with write access to all local memory.
  361. * @srq: Per-HCA SRQ (shared receive queue).
  362. * @cm_id: Connection identifier.
  363. * @srq_size: SRQ size.
  364. * @sdev_mutex: Serializes use_srq changes.
  365. * @use_srq: Whether or not to use SRQ.
  366. * @ioctx_ring: Per-HCA SRQ.
  367. * @port: Information about the ports owned by this HCA.
  368. * @event_handler: Per-HCA asynchronous IB event handler.
  369. * @list: Node in srpt_dev_list.
  370. */
  371. struct srpt_device {
  372. struct ib_device *device;
  373. struct ib_pd *pd;
  374. u32 lkey;
  375. struct ib_srq *srq;
  376. struct ib_cm_id *cm_id;
  377. int srq_size;
  378. struct mutex sdev_mutex;
  379. bool use_srq;
  380. struct srpt_recv_ioctx **ioctx_ring;
  381. struct srpt_port port[2];
  382. struct ib_event_handler event_handler;
  383. struct list_head list;
  384. };
  385. #endif /* IB_SRPT_H */