nvmet.h 21 KB

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