fabrics.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. * Reserved one command for internal usage. This command is used for sending
  20. * the connect command, as well as for the keep alive command on the admin
  21. * queue once live.
  22. */
  23. #define NVMF_RESERVED_TAGS 1
  24. /*
  25. * Define a host as seen by the target. We allocate one at boot, but also
  26. * allow the override it when creating controllers. This is both to provide
  27. * persistence of the Host NQN over multiple boots, and to allow using
  28. * multiple ones, for example in a container scenario. Because we must not
  29. * use different Host NQNs with the same Host ID we generate a Host ID and
  30. * use this structure to keep track of the relation between the two.
  31. */
  32. struct nvmf_host {
  33. struct kref ref;
  34. struct list_head list;
  35. char nqn[NVMF_NQN_SIZE];
  36. uuid_t id;
  37. };
  38. /**
  39. * enum nvmf_parsing_opts - used to define the sysfs parsing options used.
  40. */
  41. enum {
  42. NVMF_OPT_ERR = 0,
  43. NVMF_OPT_TRANSPORT = 1 << 0,
  44. NVMF_OPT_NQN = 1 << 1,
  45. NVMF_OPT_TRADDR = 1 << 2,
  46. NVMF_OPT_TRSVCID = 1 << 3,
  47. NVMF_OPT_QUEUE_SIZE = 1 << 4,
  48. NVMF_OPT_NR_IO_QUEUES = 1 << 5,
  49. NVMF_OPT_TL_RETRY_COUNT = 1 << 6,
  50. NVMF_OPT_KATO = 1 << 7,
  51. NVMF_OPT_HOSTNQN = 1 << 8,
  52. NVMF_OPT_RECONNECT_DELAY = 1 << 9,
  53. NVMF_OPT_HOST_TRADDR = 1 << 10,
  54. NVMF_OPT_CTRL_LOSS_TMO = 1 << 11,
  55. NVMF_OPT_HOST_ID = 1 << 12,
  56. NVMF_OPT_DUP_CONNECT = 1 << 13,
  57. NVMF_OPT_DISABLE_SQFLOW = 1 << 14,
  58. NVMF_OPT_HDR_DIGEST = 1 << 15,
  59. NVMF_OPT_DATA_DIGEST = 1 << 16,
  60. NVMF_OPT_NR_WRITE_QUEUES = 1 << 17,
  61. NVMF_OPT_NR_POLL_QUEUES = 1 << 18,
  62. NVMF_OPT_TOS = 1 << 19,
  63. NVMF_OPT_FAIL_FAST_TMO = 1 << 20,
  64. NVMF_OPT_HOST_IFACE = 1 << 21,
  65. NVMF_OPT_DISCOVERY = 1 << 22,
  66. NVMF_OPT_DHCHAP_SECRET = 1 << 23,
  67. NVMF_OPT_DHCHAP_CTRL_SECRET = 1 << 24,
  68. };
  69. /**
  70. * struct nvmf_ctrl_options - Used to hold the options specified
  71. * with the parsing opts enum.
  72. * @mask: Used by the fabrics library to parse through sysfs options
  73. * on adding a NVMe controller.
  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. * @max_reconnects: maximum number of allowed reconnect attempts before removing
  94. * the controller, (-1) means reconnect forever, zero means remove
  95. * immediately;
  96. * @dhchap_secret: DH-HMAC-CHAP secret
  97. * @dhchap_ctrl_secret: DH-HMAC-CHAP controller secret for bi-directional
  98. * authentication
  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. char *transport;
  110. char *subsysnqn;
  111. char *traddr;
  112. char *trsvcid;
  113. char *host_traddr;
  114. char *host_iface;
  115. size_t queue_size;
  116. unsigned int nr_io_queues;
  117. unsigned int reconnect_delay;
  118. bool discovery_nqn;
  119. bool duplicate_connect;
  120. unsigned int kato;
  121. struct nvmf_host *host;
  122. int max_reconnects;
  123. char *dhchap_secret;
  124. char *dhchap_ctrl_secret;
  125. bool disable_sqflow;
  126. bool hdr_digest;
  127. bool data_digest;
  128. unsigned int nr_write_queues;
  129. unsigned int nr_poll_queues;
  130. int tos;
  131. int fast_io_fail_tmo;
  132. };
  133. /*
  134. * struct nvmf_transport_ops - used to register a specific
  135. * fabric implementation of NVMe fabrics.
  136. * @entry: Used by the fabrics library to add the new
  137. * registration entry to its linked-list internal tree.
  138. * @module: Transport module reference
  139. * @name: Name of the NVMe fabric driver implementation.
  140. * @required_opts: sysfs command-line options that must be specified
  141. * when adding a new NVMe controller.
  142. * @allowed_opts: sysfs command-line options that can be specified
  143. * when adding a new NVMe controller.
  144. * @create_ctrl(): function pointer that points to a non-NVMe
  145. * implementation-specific fabric technology
  146. * that would go into starting up that fabric
  147. * for the purpose of conneciton to an NVMe controller
  148. * using that fabric technology.
  149. *
  150. * Notes:
  151. * 1. At minimum, 'required_opts' and 'allowed_opts' should
  152. * be set to the same enum parsing options defined earlier.
  153. * 2. create_ctrl() must be defined (even if it does nothing)
  154. * 3. struct nvmf_transport_ops must be statically allocated in the
  155. * modules .bss section so that a pure module_get on @module
  156. * prevents the memory from beeing freed.
  157. */
  158. struct nvmf_transport_ops {
  159. struct list_head entry;
  160. struct module *module;
  161. const char *name;
  162. int required_opts;
  163. int allowed_opts;
  164. struct nvme_ctrl *(*create_ctrl)(struct device *dev,
  165. struct nvmf_ctrl_options *opts);
  166. };
  167. static inline bool
  168. nvmf_ctlr_matches_baseopts(struct nvme_ctrl *ctrl,
  169. struct nvmf_ctrl_options *opts)
  170. {
  171. if (ctrl->state == NVME_CTRL_DELETING ||
  172. ctrl->state == NVME_CTRL_DELETING_NOIO ||
  173. ctrl->state == NVME_CTRL_DEAD ||
  174. strcmp(opts->subsysnqn, ctrl->opts->subsysnqn) ||
  175. strcmp(opts->host->nqn, ctrl->opts->host->nqn) ||
  176. memcmp(&opts->host->id, &ctrl->opts->host->id, sizeof(uuid_t)))
  177. return false;
  178. return true;
  179. }
  180. static inline char *nvmf_ctrl_subsysnqn(struct nvme_ctrl *ctrl)
  181. {
  182. if (!ctrl->subsys ||
  183. !strcmp(ctrl->opts->subsysnqn, NVME_DISC_SUBSYS_NAME))
  184. return ctrl->opts->subsysnqn;
  185. return ctrl->subsys->subnqn;
  186. }
  187. static inline void nvmf_complete_timed_out_request(struct request *rq)
  188. {
  189. if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq)) {
  190. nvme_req(rq)->status = NVME_SC_HOST_ABORTED_CMD;
  191. blk_mq_complete_request(rq);
  192. }
  193. }
  194. int nvmf_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val);
  195. int nvmf_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val);
  196. int nvmf_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val);
  197. int nvmf_connect_admin_queue(struct nvme_ctrl *ctrl);
  198. int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid);
  199. int nvmf_register_transport(struct nvmf_transport_ops *ops);
  200. void nvmf_unregister_transport(struct nvmf_transport_ops *ops);
  201. void nvmf_free_options(struct nvmf_ctrl_options *opts);
  202. int nvmf_get_address(struct nvme_ctrl *ctrl, char *buf, int size);
  203. bool nvmf_should_reconnect(struct nvme_ctrl *ctrl);
  204. bool nvmf_ip_options_match(struct nvme_ctrl *ctrl,
  205. struct nvmf_ctrl_options *opts);
  206. #endif /* _NVME_FABRICS_H */