nvmet.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2015-2016 HGST, a Western Digital Company.
  4. */
  5. #ifndef _NVMET_H
  6. #define _NVMET_H
  7. #include <linux/dma-mapping.h>
  8. #include <linux/types.h>
  9. #include <linux/device.h>
  10. #include <linux/kref.h>
  11. #include <linux/percpu-refcount.h>
  12. #include <linux/list.h>
  13. #include <linux/mutex.h>
  14. #include <linux/uuid.h>
  15. #include <linux/nvme.h>
  16. #include <linux/configfs.h>
  17. #include <linux/rcupdate.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/radix-tree.h>
  20. #include <linux/t10-pi.h>
  21. #define NVMET_DEFAULT_VS NVME_VS(1, 3, 0)
  22. #define NVMET_ASYNC_EVENTS 4
  23. #define NVMET_ERROR_LOG_SLOTS 128
  24. #define NVMET_NO_ERROR_LOC ((u16)-1)
  25. #define NVMET_DEFAULT_CTRL_MODEL "Linux"
  26. #define NVMET_MN_MAX_SIZE 40
  27. #define NVMET_SN_MAX_SIZE 20
  28. #define NVMET_FR_MAX_SIZE 8
  29. /*
  30. * Supported optional AENs:
  31. */
  32. #define NVMET_AEN_CFG_OPTIONAL \
  33. (NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_ANA_CHANGE)
  34. #define NVMET_DISC_AEN_CFG_OPTIONAL \
  35. (NVME_AEN_CFG_DISC_CHANGE)
  36. /*
  37. * Plus mandatory SMART AENs (we'll never send them, but allow enabling them):
  38. */
  39. #define NVMET_AEN_CFG_ALL \
  40. (NVME_SMART_CRIT_SPARE | NVME_SMART_CRIT_TEMPERATURE | \
  41. NVME_SMART_CRIT_RELIABILITY | NVME_SMART_CRIT_MEDIA | \
  42. NVME_SMART_CRIT_VOLATILE_MEMORY | NVMET_AEN_CFG_OPTIONAL)
  43. /* Helper Macros when NVMe error is NVME_SC_CONNECT_INVALID_PARAM
  44. * The 16 bit shift is to set IATTR bit to 1, which means offending
  45. * offset starts in the data section of connect()
  46. */
  47. #define IPO_IATTR_CONNECT_DATA(x) \
  48. (cpu_to_le32((1 << 16) | (offsetof(struct nvmf_connect_data, x))))
  49. #define IPO_IATTR_CONNECT_SQE(x) \
  50. (cpu_to_le32(offsetof(struct nvmf_connect_command, x)))
  51. struct nvmet_ns {
  52. struct percpu_ref ref;
  53. struct file *bdev_file;
  54. struct block_device *bdev;
  55. struct file *file;
  56. bool readonly;
  57. u32 nsid;
  58. u32 blksize_shift;
  59. loff_t size;
  60. u8 nguid[16];
  61. uuid_t uuid;
  62. u32 anagrpid;
  63. bool buffered_io;
  64. bool enabled;
  65. struct nvmet_subsys *subsys;
  66. const char *device_path;
  67. struct config_group device_group;
  68. struct config_group group;
  69. struct completion disable_done;
  70. mempool_t *bvec_pool;
  71. struct pci_dev *p2p_dev;
  72. int use_p2pmem;
  73. int pi_type;
  74. int metadata_size;
  75. u8 csi;
  76. };
  77. static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item)
  78. {
  79. return container_of(to_config_group(item), struct nvmet_ns, group);
  80. }
  81. static inline struct device *nvmet_ns_dev(struct nvmet_ns *ns)
  82. {
  83. return ns->bdev ? disk_to_dev(ns->bdev->bd_disk) : NULL;
  84. }
  85. struct nvmet_cq {
  86. u16 qid;
  87. u16 size;
  88. };
  89. struct nvmet_sq {
  90. struct nvmet_ctrl *ctrl;
  91. struct percpu_ref ref;
  92. u16 qid;
  93. u16 size;
  94. u32 sqhd;
  95. bool sqhd_disabled;
  96. #ifdef CONFIG_NVME_TARGET_AUTH
  97. bool authenticated;
  98. struct delayed_work auth_expired_work;
  99. u16 dhchap_tid;
  100. u8 dhchap_status;
  101. u8 dhchap_step;
  102. u8 *dhchap_c1;
  103. u8 *dhchap_c2;
  104. u32 dhchap_s1;
  105. u32 dhchap_s2;
  106. u8 *dhchap_skey;
  107. int dhchap_skey_len;
  108. #endif
  109. struct completion free_done;
  110. struct completion confirm_done;
  111. };
  112. struct nvmet_ana_group {
  113. struct config_group group;
  114. struct nvmet_port *port;
  115. u32 grpid;
  116. };
  117. static inline struct nvmet_ana_group *to_ana_group(struct config_item *item)
  118. {
  119. return container_of(to_config_group(item), struct nvmet_ana_group,
  120. group);
  121. }
  122. /**
  123. * struct nvmet_port - Common structure to keep port
  124. * information for the target.
  125. * @entry: Entry into referrals or transport list.
  126. * @disc_addr: Address information is stored in a format defined
  127. * for a discovery log page entry.
  128. * @group: ConfigFS group for this element's folder.
  129. * @priv: Private data for the transport.
  130. */
  131. struct nvmet_port {
  132. struct list_head entry;
  133. struct nvmf_disc_rsp_page_entry disc_addr;
  134. struct config_group group;
  135. struct config_group subsys_group;
  136. struct list_head subsystems;
  137. struct config_group referrals_group;
  138. struct list_head referrals;
  139. struct list_head global_entry;
  140. struct config_group ana_groups_group;
  141. struct nvmet_ana_group ana_default_group;
  142. enum nvme_ana_state *ana_state;
  143. struct key *keyring;
  144. void *priv;
  145. bool enabled;
  146. int inline_data_size;
  147. int max_queue_size;
  148. const struct nvmet_fabrics_ops *tr_ops;
  149. bool pi_enable;
  150. };
  151. static inline struct nvmet_port *to_nvmet_port(struct config_item *item)
  152. {
  153. return container_of(to_config_group(item), struct nvmet_port,
  154. group);
  155. }
  156. static inline struct nvmet_port *ana_groups_to_port(
  157. struct config_item *item)
  158. {
  159. return container_of(to_config_group(item), struct nvmet_port,
  160. ana_groups_group);
  161. }
  162. static inline u8 nvmet_port_disc_addr_treq_secure_channel(struct nvmet_port *port)
  163. {
  164. return (port->disc_addr.treq & NVME_TREQ_SECURE_CHANNEL_MASK);
  165. }
  166. static inline bool nvmet_port_secure_channel_required(struct nvmet_port *port)
  167. {
  168. return nvmet_port_disc_addr_treq_secure_channel(port) == NVMF_TREQ_REQUIRED;
  169. }
  170. struct nvmet_ctrl {
  171. struct nvmet_subsys *subsys;
  172. struct nvmet_sq **sqs;
  173. bool reset_tbkas;
  174. struct mutex lock;
  175. u64 cap;
  176. u32 cc;
  177. u32 csts;
  178. uuid_t hostid;
  179. u16 cntlid;
  180. u32 kato;
  181. struct nvmet_port *port;
  182. u32 aen_enabled;
  183. unsigned long aen_masked;
  184. struct nvmet_req *async_event_cmds[NVMET_ASYNC_EVENTS];
  185. unsigned int nr_async_event_cmds;
  186. struct list_head async_events;
  187. struct work_struct async_event_work;
  188. struct list_head subsys_entry;
  189. struct kref ref;
  190. struct delayed_work ka_work;
  191. struct work_struct fatal_err_work;
  192. const struct nvmet_fabrics_ops *ops;
  193. __le32 *changed_ns_list;
  194. u32 nr_changed_ns;
  195. char subsysnqn[NVMF_NQN_FIELD_LEN];
  196. char hostnqn[NVMF_NQN_FIELD_LEN];
  197. struct device *p2p_client;
  198. struct radix_tree_root p2p_ns_map;
  199. #ifdef CONFIG_NVME_TARGET_DEBUGFS
  200. struct dentry *debugfs_dir;
  201. #endif
  202. spinlock_t error_lock;
  203. u64 err_counter;
  204. struct nvme_error_slot slots[NVMET_ERROR_LOG_SLOTS];
  205. bool pi_support;
  206. #ifdef CONFIG_NVME_TARGET_AUTH
  207. struct nvme_dhchap_key *host_key;
  208. struct nvme_dhchap_key *ctrl_key;
  209. u8 shash_id;
  210. struct crypto_kpp *dh_tfm;
  211. u8 dh_gid;
  212. u8 *dh_key;
  213. size_t dh_keysize;
  214. #endif
  215. };
  216. struct nvmet_subsys {
  217. enum nvme_subsys_type type;
  218. struct mutex lock;
  219. struct kref ref;
  220. struct xarray namespaces;
  221. unsigned int nr_namespaces;
  222. u32 max_nsid;
  223. u16 cntlid_min;
  224. u16 cntlid_max;
  225. struct list_head ctrls;
  226. struct list_head hosts;
  227. bool allow_any_host;
  228. #ifdef CONFIG_NVME_TARGET_DEBUGFS
  229. struct dentry *debugfs_dir;
  230. #endif
  231. u16 max_qid;
  232. u64 ver;
  233. char serial[NVMET_SN_MAX_SIZE];
  234. bool subsys_discovered;
  235. char *subsysnqn;
  236. bool pi_support;
  237. struct config_group group;
  238. struct config_group namespaces_group;
  239. struct config_group allowed_hosts_group;
  240. char *model_number;
  241. u32 ieee_oui;
  242. char *firmware_rev;
  243. #ifdef CONFIG_NVME_TARGET_PASSTHRU
  244. struct nvme_ctrl *passthru_ctrl;
  245. char *passthru_ctrl_path;
  246. struct config_group passthru_group;
  247. unsigned int admin_timeout;
  248. unsigned int io_timeout;
  249. unsigned int clear_ids;
  250. #endif /* CONFIG_NVME_TARGET_PASSTHRU */
  251. #ifdef CONFIG_BLK_DEV_ZONED
  252. u8 zasl;
  253. #endif /* CONFIG_BLK_DEV_ZONED */
  254. };
  255. static inline struct nvmet_subsys *to_subsys(struct config_item *item)
  256. {
  257. return container_of(to_config_group(item), struct nvmet_subsys, group);
  258. }
  259. static inline struct nvmet_subsys *namespaces_to_subsys(
  260. struct config_item *item)
  261. {
  262. return container_of(to_config_group(item), struct nvmet_subsys,
  263. namespaces_group);
  264. }
  265. struct nvmet_host {
  266. struct config_group group;
  267. u8 *dhchap_secret;
  268. u8 *dhchap_ctrl_secret;
  269. u8 dhchap_key_hash;
  270. u8 dhchap_ctrl_key_hash;
  271. u8 dhchap_hash_id;
  272. u8 dhchap_dhgroup_id;
  273. };
  274. static inline struct nvmet_host *to_host(struct config_item *item)
  275. {
  276. return container_of(to_config_group(item), struct nvmet_host, group);
  277. }
  278. static inline char *nvmet_host_name(struct nvmet_host *host)
  279. {
  280. return config_item_name(&host->group.cg_item);
  281. }
  282. struct nvmet_host_link {
  283. struct list_head entry;
  284. struct nvmet_host *host;
  285. };
  286. struct nvmet_subsys_link {
  287. struct list_head entry;
  288. struct nvmet_subsys *subsys;
  289. };
  290. struct nvmet_req;
  291. struct nvmet_fabrics_ops {
  292. struct module *owner;
  293. unsigned int type;
  294. unsigned int msdbd;
  295. unsigned int flags;
  296. #define NVMF_KEYED_SGLS (1 << 0)
  297. #define NVMF_METADATA_SUPPORTED (1 << 1)
  298. void (*queue_response)(struct nvmet_req *req);
  299. int (*add_port)(struct nvmet_port *port);
  300. void (*remove_port)(struct nvmet_port *port);
  301. void (*delete_ctrl)(struct nvmet_ctrl *ctrl);
  302. void (*disc_traddr)(struct nvmet_req *req,
  303. struct nvmet_port *port, char *traddr);
  304. ssize_t (*host_traddr)(struct nvmet_ctrl *ctrl,
  305. char *traddr, size_t traddr_len);
  306. u16 (*install_queue)(struct nvmet_sq *nvme_sq);
  307. void (*discovery_chg)(struct nvmet_port *port);
  308. u8 (*get_mdts)(const struct nvmet_ctrl *ctrl);
  309. u16 (*get_max_queue_size)(const struct nvmet_ctrl *ctrl);
  310. };
  311. #define NVMET_MAX_INLINE_BIOVEC 8
  312. #define NVMET_MAX_INLINE_DATA_LEN NVMET_MAX_INLINE_BIOVEC * PAGE_SIZE
  313. struct nvmet_req {
  314. struct nvme_command *cmd;
  315. struct nvme_completion *cqe;
  316. struct nvmet_sq *sq;
  317. struct nvmet_cq *cq;
  318. struct nvmet_ns *ns;
  319. struct scatterlist *sg;
  320. struct scatterlist *metadata_sg;
  321. struct bio_vec inline_bvec[NVMET_MAX_INLINE_BIOVEC];
  322. union {
  323. struct {
  324. struct bio inline_bio;
  325. } b;
  326. struct {
  327. bool mpool_alloc;
  328. struct kiocb iocb;
  329. struct bio_vec *bvec;
  330. struct work_struct work;
  331. } f;
  332. struct {
  333. struct bio inline_bio;
  334. struct request *rq;
  335. struct work_struct work;
  336. bool use_workqueue;
  337. } p;
  338. #ifdef CONFIG_BLK_DEV_ZONED
  339. struct {
  340. struct bio inline_bio;
  341. struct work_struct zmgmt_work;
  342. } z;
  343. #endif /* CONFIG_BLK_DEV_ZONED */
  344. };
  345. int sg_cnt;
  346. int metadata_sg_cnt;
  347. /* data length as parsed from the SGL descriptor: */
  348. size_t transfer_len;
  349. size_t metadata_len;
  350. struct nvmet_port *port;
  351. void (*execute)(struct nvmet_req *req);
  352. const struct nvmet_fabrics_ops *ops;
  353. struct pci_dev *p2p_dev;
  354. struct device *p2p_client;
  355. u16 error_loc;
  356. u64 error_slba;
  357. };
  358. #define NVMET_MAX_MPOOL_BVEC 16
  359. extern struct kmem_cache *nvmet_bvec_cache;
  360. extern struct workqueue_struct *buffered_io_wq;
  361. extern struct workqueue_struct *zbd_wq;
  362. extern struct workqueue_struct *nvmet_wq;
  363. static inline void nvmet_set_result(struct nvmet_req *req, u32 result)
  364. {
  365. req->cqe->result.u32 = cpu_to_le32(result);
  366. }
  367. /*
  368. * NVMe command writes actually are DMA reads for us on the target side.
  369. */
  370. static inline enum dma_data_direction
  371. nvmet_data_dir(struct nvmet_req *req)
  372. {
  373. return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  374. }
  375. struct nvmet_async_event {
  376. struct list_head entry;
  377. u8 event_type;
  378. u8 event_info;
  379. u8 log_page;
  380. };
  381. static inline void nvmet_clear_aen_bit(struct nvmet_req *req, u32 bn)
  382. {
  383. int rae = le32_to_cpu(req->cmd->common.cdw10) & 1 << 15;
  384. if (!rae)
  385. clear_bit(bn, &req->sq->ctrl->aen_masked);
  386. }
  387. static inline bool nvmet_aen_bit_disabled(struct nvmet_ctrl *ctrl, u32 bn)
  388. {
  389. if (!(READ_ONCE(ctrl->aen_enabled) & (1 << bn)))
  390. return true;
  391. return test_and_set_bit(bn, &ctrl->aen_masked);
  392. }
  393. void nvmet_get_feat_kato(struct nvmet_req *req);
  394. void nvmet_get_feat_async_event(struct nvmet_req *req);
  395. u16 nvmet_set_feat_kato(struct nvmet_req *req);
  396. u16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask);
  397. void nvmet_execute_async_event(struct nvmet_req *req);
  398. void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl);
  399. void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl);
  400. u16 nvmet_parse_connect_cmd(struct nvmet_req *req);
  401. void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id);
  402. u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req);
  403. u16 nvmet_file_parse_io_cmd(struct nvmet_req *req);
  404. u16 nvmet_bdev_zns_parse_io_cmd(struct nvmet_req *req);
  405. u16 nvmet_parse_admin_cmd(struct nvmet_req *req);
  406. u16 nvmet_parse_discovery_cmd(struct nvmet_req *req);
  407. u16 nvmet_parse_fabrics_admin_cmd(struct nvmet_req *req);
  408. u16 nvmet_parse_fabrics_io_cmd(struct nvmet_req *req);
  409. bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
  410. struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops);
  411. void nvmet_req_uninit(struct nvmet_req *req);
  412. bool nvmet_check_transfer_len(struct nvmet_req *req, size_t len);
  413. bool nvmet_check_data_len_lte(struct nvmet_req *req, size_t data_len);
  414. void nvmet_req_complete(struct nvmet_req *req, u16 status);
  415. int nvmet_req_alloc_sgls(struct nvmet_req *req);
  416. void nvmet_req_free_sgls(struct nvmet_req *req);
  417. void nvmet_execute_set_features(struct nvmet_req *req);
  418. void nvmet_execute_get_features(struct nvmet_req *req);
  419. void nvmet_execute_keep_alive(struct nvmet_req *req);
  420. void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid,
  421. u16 size);
  422. void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid,
  423. u16 size);
  424. void nvmet_sq_destroy(struct nvmet_sq *sq);
  425. int nvmet_sq_init(struct nvmet_sq *sq);
  426. void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl);
  427. void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new);
  428. u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
  429. struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp);
  430. struct nvmet_ctrl *nvmet_ctrl_find_get(const char *subsysnqn,
  431. const char *hostnqn, u16 cntlid,
  432. struct nvmet_req *req);
  433. void nvmet_ctrl_put(struct nvmet_ctrl *ctrl);
  434. u16 nvmet_check_ctrl_status(struct nvmet_req *req);
  435. ssize_t nvmet_ctrl_host_traddr(struct nvmet_ctrl *ctrl,
  436. char *traddr, size_t traddr_len);
  437. struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
  438. enum nvme_subsys_type type);
  439. void nvmet_subsys_put(struct nvmet_subsys *subsys);
  440. void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys);
  441. u16 nvmet_req_find_ns(struct nvmet_req *req);
  442. void nvmet_put_namespace(struct nvmet_ns *ns);
  443. int nvmet_ns_enable(struct nvmet_ns *ns);
  444. void nvmet_ns_disable(struct nvmet_ns *ns);
  445. struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
  446. void nvmet_ns_free(struct nvmet_ns *ns);
  447. void nvmet_send_ana_event(struct nvmet_subsys *subsys,
  448. struct nvmet_port *port);
  449. void nvmet_port_send_ana_event(struct nvmet_port *port);
  450. int nvmet_register_transport(const struct nvmet_fabrics_ops *ops);
  451. void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops);
  452. void nvmet_port_del_ctrls(struct nvmet_port *port,
  453. struct nvmet_subsys *subsys);
  454. int nvmet_enable_port(struct nvmet_port *port);
  455. void nvmet_disable_port(struct nvmet_port *port);
  456. void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port);
  457. void nvmet_referral_disable(struct nvmet_port *parent, struct nvmet_port *port);
  458. u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
  459. size_t len);
  460. u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf,
  461. size_t len);
  462. u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len);
  463. u32 nvmet_get_log_page_len(struct nvme_command *cmd);
  464. u64 nvmet_get_log_page_offset(struct nvme_command *cmd);
  465. extern struct list_head *nvmet_ports;
  466. void nvmet_port_disc_changed(struct nvmet_port *port,
  467. struct nvmet_subsys *subsys);
  468. void nvmet_subsys_disc_changed(struct nvmet_subsys *subsys,
  469. struct nvmet_host *host);
  470. void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
  471. u8 event_info, u8 log_page);
  472. bool nvmet_subsys_nsid_exists(struct nvmet_subsys *subsys, u32 nsid);
  473. #define NVMET_MIN_QUEUE_SIZE 16
  474. #define NVMET_MAX_QUEUE_SIZE 1024
  475. #define NVMET_NR_QUEUES 128
  476. #define NVMET_MAX_CMD(ctrl) (NVME_CAP_MQES(ctrl->cap) + 1)
  477. /*
  478. * Nice round number that makes a list of nsids fit into a page.
  479. * Should become tunable at some point in the future.
  480. */
  481. #define NVMET_MAX_NAMESPACES 1024
  482. /*
  483. * 0 is not a valid ANA group ID, so we start numbering at 1.
  484. *
  485. * ANA Group 1 exists without manual intervention, has namespaces assigned to it
  486. * by default, and is available in an optimized state through all ports.
  487. */
  488. #define NVMET_MAX_ANAGRPS 128
  489. #define NVMET_DEFAULT_ANA_GRPID 1
  490. #define NVMET_KAS 10
  491. #define NVMET_DISC_KATO_MS 120000
  492. int __init nvmet_init_configfs(void);
  493. void __exit nvmet_exit_configfs(void);
  494. int __init nvmet_init_discovery(void);
  495. void nvmet_exit_discovery(void);
  496. extern struct nvmet_subsys *nvmet_disc_subsys;
  497. extern struct rw_semaphore nvmet_config_sem;
  498. extern u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
  499. extern u64 nvmet_ana_chgcnt;
  500. extern struct rw_semaphore nvmet_ana_sem;
  501. bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn);
  502. int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
  503. int nvmet_file_ns_enable(struct nvmet_ns *ns);
  504. void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
  505. void nvmet_file_ns_disable(struct nvmet_ns *ns);
  506. u16 nvmet_bdev_flush(struct nvmet_req *req);
  507. u16 nvmet_file_flush(struct nvmet_req *req);
  508. void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
  509. void nvmet_bdev_ns_revalidate(struct nvmet_ns *ns);
  510. void nvmet_file_ns_revalidate(struct nvmet_ns *ns);
  511. bool nvmet_ns_revalidate(struct nvmet_ns *ns);
  512. u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts);
  513. bool nvmet_bdev_zns_enable(struct nvmet_ns *ns);
  514. void nvmet_execute_identify_ctrl_zns(struct nvmet_req *req);
  515. void nvmet_execute_identify_ns_zns(struct nvmet_req *req);
  516. void nvmet_bdev_execute_zone_mgmt_recv(struct nvmet_req *req);
  517. void nvmet_bdev_execute_zone_mgmt_send(struct nvmet_req *req);
  518. void nvmet_bdev_execute_zone_append(struct nvmet_req *req);
  519. static inline u32 nvmet_rw_data_len(struct nvmet_req *req)
  520. {
  521. return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) <<
  522. req->ns->blksize_shift;
  523. }
  524. static inline u32 nvmet_rw_metadata_len(struct nvmet_req *req)
  525. {
  526. if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
  527. return 0;
  528. return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) *
  529. req->ns->metadata_size;
  530. }
  531. static inline u32 nvmet_dsm_len(struct nvmet_req *req)
  532. {
  533. return (le32_to_cpu(req->cmd->dsm.nr) + 1) *
  534. sizeof(struct nvme_dsm_range);
  535. }
  536. static inline struct nvmet_subsys *nvmet_req_subsys(struct nvmet_req *req)
  537. {
  538. return req->sq->ctrl->subsys;
  539. }
  540. static inline bool nvmet_is_disc_subsys(struct nvmet_subsys *subsys)
  541. {
  542. return subsys->type != NVME_NQN_NVME;
  543. }
  544. #ifdef CONFIG_NVME_TARGET_PASSTHRU
  545. void nvmet_passthru_subsys_free(struct nvmet_subsys *subsys);
  546. int nvmet_passthru_ctrl_enable(struct nvmet_subsys *subsys);
  547. void nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys);
  548. u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req);
  549. u16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req);
  550. static inline bool nvmet_is_passthru_subsys(struct nvmet_subsys *subsys)
  551. {
  552. return subsys->passthru_ctrl;
  553. }
  554. #else /* CONFIG_NVME_TARGET_PASSTHRU */
  555. static inline void nvmet_passthru_subsys_free(struct nvmet_subsys *subsys)
  556. {
  557. }
  558. static inline void nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys)
  559. {
  560. }
  561. static inline u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req)
  562. {
  563. return 0;
  564. }
  565. static inline u16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req)
  566. {
  567. return 0;
  568. }
  569. static inline bool nvmet_is_passthru_subsys(struct nvmet_subsys *subsys)
  570. {
  571. return NULL;
  572. }
  573. #endif /* CONFIG_NVME_TARGET_PASSTHRU */
  574. static inline bool nvmet_is_passthru_req(struct nvmet_req *req)
  575. {
  576. return nvmet_is_passthru_subsys(nvmet_req_subsys(req));
  577. }
  578. void nvmet_passthrough_override_cap(struct nvmet_ctrl *ctrl);
  579. u16 errno_to_nvme_status(struct nvmet_req *req, int errno);
  580. u16 nvmet_report_invalid_opcode(struct nvmet_req *req);
  581. /* Convert a 32-bit number to a 16-bit 0's based number */
  582. static inline __le16 to0based(u32 a)
  583. {
  584. return cpu_to_le16(max(1U, min(1U << 16, a)) - 1);
  585. }
  586. static inline bool nvmet_ns_has_pi(struct nvmet_ns *ns)
  587. {
  588. if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
  589. return false;
  590. return ns->pi_type && ns->metadata_size == sizeof(struct t10_pi_tuple);
  591. }
  592. static inline __le64 nvmet_sect_to_lba(struct nvmet_ns *ns, sector_t sect)
  593. {
  594. return cpu_to_le64(sect >> (ns->blksize_shift - SECTOR_SHIFT));
  595. }
  596. static inline sector_t nvmet_lba_to_sect(struct nvmet_ns *ns, __le64 lba)
  597. {
  598. return le64_to_cpu(lba) << (ns->blksize_shift - SECTOR_SHIFT);
  599. }
  600. static inline bool nvmet_use_inline_bvec(struct nvmet_req *req)
  601. {
  602. return req->transfer_len <= NVMET_MAX_INLINE_DATA_LEN &&
  603. req->sg_cnt <= NVMET_MAX_INLINE_BIOVEC;
  604. }
  605. static inline void nvmet_req_bio_put(struct nvmet_req *req, struct bio *bio)
  606. {
  607. if (bio != &req->b.inline_bio)
  608. bio_put(bio);
  609. else
  610. bio_uninit(bio);
  611. }
  612. #ifdef CONFIG_NVME_TARGET_AUTH
  613. void nvmet_execute_auth_send(struct nvmet_req *req);
  614. void nvmet_execute_auth_receive(struct nvmet_req *req);
  615. int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
  616. bool set_ctrl);
  617. int nvmet_auth_set_host_hash(struct nvmet_host *host, const char *hash);
  618. u8 nvmet_setup_auth(struct nvmet_ctrl *ctrl);
  619. void nvmet_auth_sq_init(struct nvmet_sq *sq);
  620. void nvmet_destroy_auth(struct nvmet_ctrl *ctrl);
  621. void nvmet_auth_sq_free(struct nvmet_sq *sq);
  622. int nvmet_setup_dhgroup(struct nvmet_ctrl *ctrl, u8 dhgroup_id);
  623. bool nvmet_check_auth_status(struct nvmet_req *req);
  624. int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response,
  625. unsigned int hash_len);
  626. int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 *response,
  627. unsigned int hash_len);
  628. static inline bool nvmet_has_auth(struct nvmet_ctrl *ctrl)
  629. {
  630. return ctrl->host_key != NULL;
  631. }
  632. int nvmet_auth_ctrl_exponential(struct nvmet_req *req,
  633. u8 *buf, int buf_size);
  634. int nvmet_auth_ctrl_sesskey(struct nvmet_req *req,
  635. u8 *buf, int buf_size);
  636. #else
  637. static inline u8 nvmet_setup_auth(struct nvmet_ctrl *ctrl)
  638. {
  639. return 0;
  640. }
  641. static inline void nvmet_auth_sq_init(struct nvmet_sq *sq)
  642. {
  643. }
  644. static inline void nvmet_destroy_auth(struct nvmet_ctrl *ctrl) {};
  645. static inline void nvmet_auth_sq_free(struct nvmet_sq *sq) {};
  646. static inline bool nvmet_check_auth_status(struct nvmet_req *req)
  647. {
  648. return true;
  649. }
  650. static inline bool nvmet_has_auth(struct nvmet_ctrl *ctrl)
  651. {
  652. return false;
  653. }
  654. static inline const char *nvmet_dhchap_dhgroup_name(u8 dhgid) { return NULL; }
  655. #endif
  656. #endif /* _NVMET_H */