nvmet.h 17 KB

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