fabrics.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * NVMe over Fabrics common host code.
  4. * Copyright (c) 2015-2016 HGST, a Western Digital Company.
  5. */
  6. #ifndef _NVME_FABRICS_H
  7. #define _NVME_FABRICS_H 1
  8. #include <linux/in.h>
  9. #include <linux/inet.h>
  10. #define NVMF_MIN_QUEUE_SIZE 16
  11. #define NVMF_MAX_QUEUE_SIZE 1024
  12. #define NVMF_DEF_QUEUE_SIZE 128
  13. #define NVMF_DEF_RECONNECT_DELAY 10
  14. /* default to 600 seconds of reconnect attempts before giving up */
  15. #define NVMF_DEF_CTRL_LOSS_TMO 600
  16. /* default is -1: the fail fast mechanism is disabled */
  17. #define NVMF_DEF_FAIL_FAST_TMO -1
  18. /*
  19. * Define a host as seen by the target. We allocate one at boot, but also
  20. * allow the override it when creating controllers. This is both to provide
  21. * persistence of the Host NQN over multiple boots, and to allow using
  22. * multiple ones, for example in a container scenario. Because we must not
  23. * use different Host NQNs with the same Host ID we generate a Host ID and
  24. * use this structure to keep track of the relation between the two.
  25. */
  26. struct nvmf_host {
  27. struct kref ref;
  28. struct list_head list;
  29. char nqn[NVMF_NQN_SIZE];
  30. uuid_t id;
  31. };
  32. /**
  33. * enum nvmf_parsing_opts - used to define the sysfs parsing options used.
  34. */
  35. enum {
  36. NVMF_OPT_ERR = 0,
  37. NVMF_OPT_TRANSPORT = 1 << 0,
  38. NVMF_OPT_NQN = 1 << 1,
  39. NVMF_OPT_TRADDR = 1 << 2,
  40. NVMF_OPT_TRSVCID = 1 << 3,
  41. NVMF_OPT_QUEUE_SIZE = 1 << 4,
  42. NVMF_OPT_NR_IO_QUEUES = 1 << 5,
  43. NVMF_OPT_TL_RETRY_COUNT = 1 << 6,
  44. NVMF_OPT_KATO = 1 << 7,
  45. NVMF_OPT_HOSTNQN = 1 << 8,
  46. NVMF_OPT_RECONNECT_DELAY = 1 << 9,
  47. NVMF_OPT_HOST_TRADDR = 1 << 10,
  48. NVMF_OPT_CTRL_LOSS_TMO = 1 << 11,
  49. NVMF_OPT_HOST_ID = 1 << 12,
  50. NVMF_OPT_DUP_CONNECT = 1 << 13,
  51. NVMF_OPT_DISABLE_SQFLOW = 1 << 14,
  52. NVMF_OPT_HDR_DIGEST = 1 << 15,
  53. NVMF_OPT_DATA_DIGEST = 1 << 16,
  54. NVMF_OPT_NR_WRITE_QUEUES = 1 << 17,
  55. NVMF_OPT_NR_POLL_QUEUES = 1 << 18,
  56. NVMF_OPT_TOS = 1 << 19,
  57. NVMF_OPT_FAIL_FAST_TMO = 1 << 20,
  58. NVMF_OPT_HOST_IFACE = 1 << 21,
  59. NVMF_OPT_DISCOVERY = 1 << 22,
  60. NVMF_OPT_DHCHAP_SECRET = 1 << 23,
  61. NVMF_OPT_DHCHAP_CTRL_SECRET = 1 << 24,
  62. NVMF_OPT_TLS = 1 << 25,
  63. NVMF_OPT_KEYRING = 1 << 26,
  64. NVMF_OPT_TLS_KEY = 1 << 27,
  65. };
  66. /**
  67. * struct nvmf_ctrl_options - Used to hold the options specified
  68. * with the parsing opts enum.
  69. * @mask: Used by the fabrics library to parse through sysfs options
  70. * on adding a NVMe controller.
  71. * @max_reconnects: maximum number of allowed reconnect attempts before removing
  72. * the controller, (-1) means reconnect forever, zero means remove
  73. * immediately;
  74. * @transport: Holds the fabric transport "technology name" (for a lack of
  75. * better description) that will be used by an NVMe controller
  76. * being added.
  77. * @subsysnqn: Hold the fully qualified NQN subystem name (format defined
  78. * in the NVMe specification, "NVMe Qualified Names").
  79. * @traddr: The transport-specific TRADDR field for a port on the
  80. * subsystem which is adding a controller.
  81. * @trsvcid: The transport-specific TRSVCID field for a port on the
  82. * subsystem which is adding a controller.
  83. * @host_traddr: A transport-specific field identifying the NVME host port
  84. * to use for the connection to the controller.
  85. * @host_iface: A transport-specific field identifying the NVME host
  86. * interface to use for the connection to the controller.
  87. * @queue_size: Number of IO queue elements.
  88. * @nr_io_queues: Number of controller IO queues that will be established.
  89. * @reconnect_delay: Time between two consecutive reconnect attempts.
  90. * @discovery_nqn: indicates if the subsysnqn is the well-known discovery NQN.
  91. * @kato: Keep-alive timeout.
  92. * @host: Virtual NVMe host, contains the NQN and Host ID.
  93. * @dhchap_secret: DH-HMAC-CHAP secret
  94. * @dhchap_ctrl_secret: DH-HMAC-CHAP controller secret for bi-directional
  95. * authentication
  96. * @keyring: Keyring to use for key lookups
  97. * @tls_key: TLS key for encrypted connections (TCP)
  98. * @tls: Start TLS encrypted connections (TCP)
  99. * @disable_sqflow: disable controller sq flow control
  100. * @hdr_digest: generate/verify header digest (TCP)
  101. * @data_digest: generate/verify data digest (TCP)
  102. * @nr_write_queues: number of queues for write I/O
  103. * @nr_poll_queues: number of queues for polling I/O
  104. * @tos: type of service
  105. * @fast_io_fail_tmo: Fast I/O fail timeout in seconds
  106. */
  107. struct nvmf_ctrl_options {
  108. unsigned mask;
  109. int max_reconnects;
  110. char *transport;
  111. char *subsysnqn;
  112. char *traddr;
  113. char *trsvcid;
  114. char *host_traddr;
  115. char *host_iface;
  116. size_t queue_size;
  117. unsigned int nr_io_queues;
  118. unsigned int reconnect_delay;
  119. bool discovery_nqn;
  120. bool duplicate_connect;
  121. unsigned int kato;
  122. struct nvmf_host *host;
  123. char *dhchap_secret;
  124. char *dhchap_ctrl_secret;
  125. struct key *keyring;
  126. struct key *tls_key;
  127. bool tls;
  128. bool disable_sqflow;
  129. bool hdr_digest;
  130. bool data_digest;
  131. unsigned int nr_write_queues;
  132. unsigned int nr_poll_queues;
  133. int tos;
  134. int fast_io_fail_tmo;
  135. };
  136. /*
  137. * struct nvmf_transport_ops - used to register a specific
  138. * fabric implementation of NVMe fabrics.
  139. * @entry: Used by the fabrics library to add the new
  140. * registration entry to its linked-list internal tree.
  141. * @module: Transport module reference
  142. * @name: Name of the NVMe fabric driver implementation.
  143. * @required_opts: sysfs command-line options that must be specified
  144. * when adding a new NVMe controller.
  145. * @allowed_opts: sysfs command-line options that can be specified
  146. * when adding a new NVMe controller.
  147. * @create_ctrl(): function pointer that points to a non-NVMe
  148. * implementation-specific fabric technology
  149. * that would go into starting up that fabric
  150. * for the purpose of conneciton to an NVMe controller
  151. * using that fabric technology.
  152. *
  153. * Notes:
  154. * 1. At minimum, 'required_opts' and 'allowed_opts' should
  155. * be set to the same enum parsing options defined earlier.
  156. * 2. create_ctrl() must be defined (even if it does nothing)
  157. * 3. struct nvmf_transport_ops must be statically allocated in the
  158. * modules .bss section so that a pure module_get on @module
  159. * prevents the memory from beeing freed.
  160. */
  161. struct nvmf_transport_ops {
  162. struct list_head entry;
  163. struct module *module;
  164. const char *name;
  165. int required_opts;
  166. int allowed_opts;
  167. struct nvme_ctrl *(*create_ctrl)(struct device *dev,
  168. struct nvmf_ctrl_options *opts);
  169. };
  170. static inline bool
  171. nvmf_ctlr_matches_baseopts(struct nvme_ctrl *ctrl,
  172. struct nvmf_ctrl_options *opts)
  173. {
  174. enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
  175. if (state == NVME_CTRL_DELETING ||
  176. state == NVME_CTRL_DELETING_NOIO ||
  177. state == NVME_CTRL_DEAD ||
  178. strcmp(opts->subsysnqn, ctrl->opts->subsysnqn) ||
  179. strcmp(opts->host->nqn, ctrl->opts->host->nqn) ||
  180. !uuid_equal(&opts->host->id, &ctrl->opts->host->id))
  181. return false;
  182. return true;
  183. }
  184. static inline char *nvmf_ctrl_subsysnqn(struct nvme_ctrl *ctrl)
  185. {
  186. if (!ctrl->subsys ||
  187. !strcmp(ctrl->opts->subsysnqn, NVME_DISC_SUBSYS_NAME))
  188. return ctrl->opts->subsysnqn;
  189. return ctrl->subsys->subnqn;
  190. }
  191. static inline void nvmf_complete_timed_out_request(struct request *rq)
  192. {
  193. if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq)) {
  194. nvme_req(rq)->status = NVME_SC_HOST_ABORTED_CMD;
  195. blk_mq_complete_request(rq);
  196. }
  197. }
  198. static inline unsigned int nvmf_nr_io_queues(struct nvmf_ctrl_options *opts)
  199. {
  200. return min(opts->nr_io_queues, num_online_cpus()) +
  201. min(opts->nr_write_queues, num_online_cpus()) +
  202. min(opts->nr_poll_queues, num_online_cpus());
  203. }
  204. int nvmf_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val);
  205. int nvmf_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val);
  206. int nvmf_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val);
  207. int nvmf_subsystem_reset(struct nvme_ctrl *ctrl);
  208. int nvmf_connect_admin_queue(struct nvme_ctrl *ctrl);
  209. int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid);
  210. int nvmf_register_transport(struct nvmf_transport_ops *ops);
  211. void nvmf_unregister_transport(struct nvmf_transport_ops *ops);
  212. void nvmf_free_options(struct nvmf_ctrl_options *opts);
  213. int nvmf_get_address(struct nvme_ctrl *ctrl, char *buf, int size);
  214. bool nvmf_should_reconnect(struct nvme_ctrl *ctrl, int status);
  215. bool nvmf_ip_options_match(struct nvme_ctrl *ctrl,
  216. struct nvmf_ctrl_options *opts);
  217. void nvmf_set_io_queues(struct nvmf_ctrl_options *opts, u32 nr_io_queues,
  218. u32 io_queues[HCTX_MAX_TYPES]);
  219. void nvmf_map_queues(struct blk_mq_tag_set *set, struct nvme_ctrl *ctrl,
  220. u32 io_queues[HCTX_MAX_TYPES]);
  221. #endif /* _NVME_FABRICS_H */