nvme.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2011-2014, Intel Corporation.
  4. */
  5. #ifndef _NVME_H
  6. #define _NVME_H
  7. #include <linux/nvme.h>
  8. #include <linux/cdev.h>
  9. #include <linux/pci.h>
  10. #include <linux/kref.h>
  11. #include <linux/blk-mq.h>
  12. #include <linux/lightnvm.h>
  13. #include <linux/sed-opal.h>
  14. #include <linux/fault-inject.h>
  15. #include <linux/rcupdate.h>
  16. #include <linux/wait.h>
  17. #include <linux/t10-pi.h>
  18. #include <trace/events/block.h>
  19. extern unsigned int nvme_io_timeout;
  20. #define NVME_IO_TIMEOUT (nvme_io_timeout * HZ)
  21. extern unsigned int admin_timeout;
  22. #define ADMIN_TIMEOUT (admin_timeout * HZ)
  23. #define NVME_DEFAULT_KATO 5
  24. #define NVME_KATO_GRACE 10
  25. #ifdef CONFIG_ARCH_NO_SG_CHAIN
  26. #define NVME_INLINE_SG_CNT 0
  27. #define NVME_INLINE_METADATA_SG_CNT 0
  28. #else
  29. #define NVME_INLINE_SG_CNT 2
  30. #define NVME_INLINE_METADATA_SG_CNT 1
  31. #endif
  32. /*
  33. * Default to a 4K page size, with the intention to update this
  34. * path in the future to accommodate architectures with differing
  35. * kernel and IO page sizes.
  36. */
  37. #define NVME_CTRL_PAGE_SHIFT 12
  38. #define NVME_CTRL_PAGE_SIZE (1 << NVME_CTRL_PAGE_SHIFT)
  39. extern struct workqueue_struct *nvme_wq;
  40. extern struct workqueue_struct *nvme_reset_wq;
  41. extern struct workqueue_struct *nvme_delete_wq;
  42. enum {
  43. NVME_NS_LBA = 0,
  44. NVME_NS_LIGHTNVM = 1,
  45. };
  46. /*
  47. * List of workarounds for devices that required behavior not specified in
  48. * the standard.
  49. */
  50. enum nvme_quirks {
  51. /*
  52. * Prefers I/O aligned to a stripe size specified in a vendor
  53. * specific Identify field.
  54. */
  55. NVME_QUIRK_STRIPE_SIZE = (1 << 0),
  56. /*
  57. * The controller doesn't handle Identify value others than 0 or 1
  58. * correctly.
  59. */
  60. NVME_QUIRK_IDENTIFY_CNS = (1 << 1),
  61. /*
  62. * The controller deterministically returns O's on reads to
  63. * logical blocks that deallocate was called on.
  64. */
  65. NVME_QUIRK_DEALLOCATE_ZEROES = (1 << 2),
  66. /*
  67. * The controller needs a delay before starts checking the device
  68. * readiness, which is done by reading the NVME_CSTS_RDY bit.
  69. */
  70. NVME_QUIRK_DELAY_BEFORE_CHK_RDY = (1 << 3),
  71. /*
  72. * APST should not be used.
  73. */
  74. NVME_QUIRK_NO_APST = (1 << 4),
  75. /*
  76. * The deepest sleep state should not be used.
  77. */
  78. NVME_QUIRK_NO_DEEPEST_PS = (1 << 5),
  79. /*
  80. * Supports the LighNVM command set if indicated in vs[1].
  81. */
  82. NVME_QUIRK_LIGHTNVM = (1 << 6),
  83. /*
  84. * Set MEDIUM priority on SQ creation
  85. */
  86. NVME_QUIRK_MEDIUM_PRIO_SQ = (1 << 7),
  87. /*
  88. * Ignore device provided subnqn.
  89. */
  90. NVME_QUIRK_IGNORE_DEV_SUBNQN = (1 << 8),
  91. /*
  92. * Broken Write Zeroes.
  93. */
  94. NVME_QUIRK_DISABLE_WRITE_ZEROES = (1 << 9),
  95. /*
  96. * Force simple suspend/resume path.
  97. */
  98. NVME_QUIRK_SIMPLE_SUSPEND = (1 << 10),
  99. /*
  100. * Use only one interrupt vector for all queues
  101. */
  102. NVME_QUIRK_SINGLE_VECTOR = (1 << 11),
  103. /*
  104. * Use non-standard 128 bytes SQEs.
  105. */
  106. NVME_QUIRK_128_BYTES_SQES = (1 << 12),
  107. /*
  108. * Prevent tag overlap between queues
  109. */
  110. NVME_QUIRK_SHARED_TAGS = (1 << 13),
  111. /*
  112. * Don't change the value of the temperature threshold feature
  113. */
  114. NVME_QUIRK_NO_TEMP_THRESH_CHANGE = (1 << 14),
  115. /*
  116. * The controller doesn't handle the Identify Namespace
  117. * Identification Descriptor list subcommand despite claiming
  118. * NVMe 1.3 compliance.
  119. */
  120. NVME_QUIRK_NO_NS_DESC_LIST = (1 << 15),
  121. /*
  122. * The controller requires the command_id value be be limited, so skip
  123. * encoding the generation sequence number.
  124. */
  125. NVME_QUIRK_SKIP_CID_GEN = (1 << 17),
  126. /*
  127. * Reports garbage in the namespace identifiers (eui64, nguid, uuid).
  128. */
  129. NVME_QUIRK_BOGUS_NID = (1 << 18),
  130. };
  131. /*
  132. * Common request structure for NVMe passthrough. All drivers must have
  133. * this structure as the first member of their request-private data.
  134. */
  135. struct nvme_request {
  136. struct nvme_command *cmd;
  137. union nvme_result result;
  138. u8 genctr;
  139. u8 retries;
  140. u8 flags;
  141. u16 status;
  142. struct nvme_ctrl *ctrl;
  143. };
  144. /*
  145. * Mark a bio as coming in through the mpath node.
  146. */
  147. #define REQ_NVME_MPATH REQ_DRV
  148. enum {
  149. NVME_REQ_CANCELLED = (1 << 0),
  150. NVME_REQ_USERCMD = (1 << 1),
  151. };
  152. static inline struct nvme_request *nvme_req(struct request *req)
  153. {
  154. return blk_mq_rq_to_pdu(req);
  155. }
  156. static inline u16 nvme_req_qid(struct request *req)
  157. {
  158. if (!req->q->queuedata)
  159. return 0;
  160. return blk_mq_unique_tag_to_hwq(blk_mq_unique_tag(req)) + 1;
  161. }
  162. /* The below value is the specific amount of delay needed before checking
  163. * readiness in case of the PCI_DEVICE(0x1c58, 0x0003), which needs the
  164. * NVME_QUIRK_DELAY_BEFORE_CHK_RDY quirk enabled. The value (in ms) was
  165. * found empirically.
  166. */
  167. #define NVME_QUIRK_DELAY_AMOUNT 2300
  168. /*
  169. * enum nvme_ctrl_state: Controller state
  170. *
  171. * @NVME_CTRL_NEW: New controller just allocated, initial state
  172. * @NVME_CTRL_LIVE: Controller is connected and I/O capable
  173. * @NVME_CTRL_RESETTING: Controller is resetting (or scheduled reset)
  174. * @NVME_CTRL_CONNECTING: Controller is disconnected, now connecting the
  175. * transport
  176. * @NVME_CTRL_DELETING: Controller is deleting (or scheduled deletion)
  177. * @NVME_CTRL_DELETING_NOIO: Controller is deleting and I/O is not
  178. * disabled/failed immediately. This state comes
  179. * after all async event processing took place and
  180. * before ns removal and the controller deletion
  181. * progress
  182. * @NVME_CTRL_DEAD: Controller is non-present/unresponsive during
  183. * shutdown or removal. In this case we forcibly
  184. * kill all inflight I/O as they have no chance to
  185. * complete
  186. */
  187. enum nvme_ctrl_state {
  188. NVME_CTRL_NEW,
  189. NVME_CTRL_LIVE,
  190. NVME_CTRL_RESETTING,
  191. NVME_CTRL_CONNECTING,
  192. NVME_CTRL_DELETING,
  193. NVME_CTRL_DELETING_NOIO,
  194. NVME_CTRL_DEAD,
  195. };
  196. struct nvme_fault_inject {
  197. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  198. struct fault_attr attr;
  199. struct dentry *parent;
  200. bool dont_retry; /* DNR, do not retry */
  201. u16 status; /* status code */
  202. #endif
  203. };
  204. struct nvme_ctrl {
  205. bool comp_seen;
  206. enum nvme_ctrl_state state;
  207. bool identified;
  208. spinlock_t lock;
  209. struct mutex scan_lock;
  210. const struct nvme_ctrl_ops *ops;
  211. struct request_queue *admin_q;
  212. struct request_queue *connect_q;
  213. struct request_queue *fabrics_q;
  214. struct device *dev;
  215. int instance;
  216. int numa_node;
  217. struct blk_mq_tag_set *tagset;
  218. struct blk_mq_tag_set *admin_tagset;
  219. struct list_head namespaces;
  220. struct rw_semaphore namespaces_rwsem;
  221. struct device ctrl_device;
  222. struct device *device; /* char device */
  223. struct cdev cdev;
  224. struct work_struct reset_work;
  225. struct work_struct delete_work;
  226. wait_queue_head_t state_wq;
  227. struct nvme_subsystem *subsys;
  228. struct list_head subsys_entry;
  229. struct opal_dev *opal_dev;
  230. char name[12];
  231. u16 cntlid;
  232. u32 ctrl_config;
  233. u16 mtfa;
  234. u32 queue_count;
  235. u64 cap;
  236. u32 max_hw_sectors;
  237. u32 max_segments;
  238. u32 max_integrity_segments;
  239. #ifdef CONFIG_BLK_DEV_ZONED
  240. u32 max_zone_append;
  241. #endif
  242. u16 crdt[3];
  243. u16 oncs;
  244. u16 oacs;
  245. u16 nssa;
  246. u16 nr_streams;
  247. u16 sqsize;
  248. u32 max_namespaces;
  249. atomic_t abort_limit;
  250. u8 vwc;
  251. u32 vs;
  252. u32 sgls;
  253. u16 kas;
  254. u8 npss;
  255. u8 apsta;
  256. u16 wctemp;
  257. u16 cctemp;
  258. u32 oaes;
  259. u32 aen_result;
  260. u32 ctratt;
  261. unsigned int shutdown_timeout;
  262. unsigned int kato;
  263. bool subsystem;
  264. unsigned long quirks;
  265. struct nvme_id_power_state psd[32];
  266. struct nvme_effects_log *effects;
  267. struct xarray cels;
  268. struct work_struct scan_work;
  269. struct work_struct async_event_work;
  270. struct delayed_work ka_work;
  271. struct nvme_command ka_cmd;
  272. struct work_struct fw_act_work;
  273. unsigned long events;
  274. #ifdef CONFIG_NVME_MULTIPATH
  275. /* asymmetric namespace access: */
  276. u8 anacap;
  277. u8 anatt;
  278. u32 anagrpmax;
  279. u32 nanagrpid;
  280. struct mutex ana_lock;
  281. struct nvme_ana_rsp_hdr *ana_log_buf;
  282. size_t ana_log_size;
  283. struct timer_list anatt_timer;
  284. struct work_struct ana_work;
  285. #endif
  286. /* Power saving configuration */
  287. u64 ps_max_latency_us;
  288. bool apst_enabled;
  289. /* PCIe only: */
  290. u32 hmpre;
  291. u32 hmmin;
  292. u32 hmminds;
  293. u16 hmmaxd;
  294. /* Fabrics only */
  295. u32 ioccsz;
  296. u32 iorcsz;
  297. u16 icdoff;
  298. u16 maxcmd;
  299. int nr_reconnects;
  300. struct nvmf_ctrl_options *opts;
  301. struct page *discard_page;
  302. unsigned long discard_page_busy;
  303. struct nvme_fault_inject fault_inject;
  304. };
  305. enum nvme_iopolicy {
  306. NVME_IOPOLICY_NUMA,
  307. NVME_IOPOLICY_RR,
  308. };
  309. struct nvme_subsystem {
  310. int instance;
  311. struct device dev;
  312. /*
  313. * Because we unregister the device on the last put we need
  314. * a separate refcount.
  315. */
  316. struct kref ref;
  317. struct list_head entry;
  318. struct mutex lock;
  319. struct list_head ctrls;
  320. struct list_head nsheads;
  321. char subnqn[NVMF_NQN_SIZE];
  322. char serial[20];
  323. char model[40];
  324. char firmware_rev[8];
  325. u8 cmic;
  326. u16 vendor_id;
  327. u16 awupf; /* 0's based awupf value. */
  328. struct ida ns_ida;
  329. #ifdef CONFIG_NVME_MULTIPATH
  330. enum nvme_iopolicy iopolicy;
  331. #endif
  332. };
  333. /*
  334. * Container structure for uniqueue namespace identifiers.
  335. */
  336. struct nvme_ns_ids {
  337. u8 eui64[8];
  338. u8 nguid[16];
  339. uuid_t uuid;
  340. u8 csi;
  341. };
  342. /*
  343. * Anchor structure for namespaces. There is one for each namespace in a
  344. * NVMe subsystem that any of our controllers can see, and the namespace
  345. * structure for each controller is chained of it. For private namespaces
  346. * there is a 1:1 relation to our namespace structures, that is ->list
  347. * only ever has a single entry for private namespaces.
  348. */
  349. struct nvme_ns_head {
  350. struct list_head list;
  351. struct srcu_struct srcu;
  352. struct nvme_subsystem *subsys;
  353. unsigned ns_id;
  354. struct nvme_ns_ids ids;
  355. struct list_head entry;
  356. struct kref ref;
  357. bool shared;
  358. int instance;
  359. struct nvme_effects_log *effects;
  360. #ifdef CONFIG_NVME_MULTIPATH
  361. struct gendisk *disk;
  362. struct bio_list requeue_list;
  363. spinlock_t requeue_lock;
  364. struct work_struct requeue_work;
  365. struct mutex lock;
  366. unsigned long flags;
  367. #define NVME_NSHEAD_DISK_LIVE 0
  368. struct nvme_ns __rcu *current_path[];
  369. #endif
  370. };
  371. enum nvme_ns_features {
  372. NVME_NS_EXT_LBAS = 1 << 0, /* support extended LBA format */
  373. NVME_NS_METADATA_SUPPORTED = 1 << 1, /* support getting generated md */
  374. };
  375. struct nvme_ns {
  376. struct list_head list;
  377. struct nvme_ctrl *ctrl;
  378. struct request_queue *queue;
  379. struct gendisk *disk;
  380. #ifdef CONFIG_NVME_MULTIPATH
  381. enum nvme_ana_state ana_state;
  382. u32 ana_grpid;
  383. #endif
  384. struct list_head siblings;
  385. struct nvm_dev *ndev;
  386. struct kref kref;
  387. struct nvme_ns_head *head;
  388. int lba_shift;
  389. u16 ms;
  390. u16 sgs;
  391. u32 sws;
  392. u8 pi_type;
  393. #ifdef CONFIG_BLK_DEV_ZONED
  394. u64 zsze;
  395. #endif
  396. unsigned long features;
  397. unsigned long flags;
  398. #define NVME_NS_REMOVING 0
  399. #define NVME_NS_DEAD 1
  400. #define NVME_NS_ANA_PENDING 2
  401. struct nvme_fault_inject fault_inject;
  402. };
  403. /* NVMe ns supports metadata actions by the controller (generate/strip) */
  404. static inline bool nvme_ns_has_pi(struct nvme_ns *ns)
  405. {
  406. return ns->pi_type && ns->ms == sizeof(struct t10_pi_tuple);
  407. }
  408. struct nvme_ctrl_ops {
  409. const char *name;
  410. struct module *module;
  411. unsigned int flags;
  412. #define NVME_F_FABRICS (1 << 0)
  413. #define NVME_F_METADATA_SUPPORTED (1 << 1)
  414. #define NVME_F_PCI_P2PDMA (1 << 2)
  415. int (*reg_read32)(struct nvme_ctrl *ctrl, u32 off, u32 *val);
  416. int (*reg_write32)(struct nvme_ctrl *ctrl, u32 off, u32 val);
  417. int (*reg_read64)(struct nvme_ctrl *ctrl, u32 off, u64 *val);
  418. void (*free_ctrl)(struct nvme_ctrl *ctrl);
  419. void (*submit_async_event)(struct nvme_ctrl *ctrl);
  420. void (*delete_ctrl)(struct nvme_ctrl *ctrl);
  421. int (*get_address)(struct nvme_ctrl *ctrl, char *buf, int size);
  422. };
  423. /*
  424. * nvme command_id is constructed as such:
  425. * | xxxx | xxxxxxxxxxxx |
  426. * gen request tag
  427. */
  428. #define nvme_genctr_mask(gen) (gen & 0xf)
  429. #define nvme_cid_install_genctr(gen) (nvme_genctr_mask(gen) << 12)
  430. #define nvme_genctr_from_cid(cid) ((cid & 0xf000) >> 12)
  431. #define nvme_tag_from_cid(cid) (cid & 0xfff)
  432. static inline u16 nvme_cid(struct request *rq)
  433. {
  434. return nvme_cid_install_genctr(nvme_req(rq)->genctr) | rq->tag;
  435. }
  436. static inline struct request *nvme_find_rq(struct blk_mq_tags *tags,
  437. u16 command_id)
  438. {
  439. u8 genctr = nvme_genctr_from_cid(command_id);
  440. u16 tag = nvme_tag_from_cid(command_id);
  441. struct request *rq;
  442. rq = blk_mq_tag_to_rq(tags, tag);
  443. if (unlikely(!rq)) {
  444. pr_err("could not locate request for tag %#x\n",
  445. tag);
  446. return NULL;
  447. }
  448. if (unlikely(nvme_genctr_mask(nvme_req(rq)->genctr) != genctr)) {
  449. dev_err(nvme_req(rq)->ctrl->device,
  450. "request %#x genctr mismatch (got %#x expected %#x)\n",
  451. tag, genctr, nvme_genctr_mask(nvme_req(rq)->genctr));
  452. return NULL;
  453. }
  454. return rq;
  455. }
  456. static inline struct request *nvme_cid_to_rq(struct blk_mq_tags *tags,
  457. u16 command_id)
  458. {
  459. return blk_mq_tag_to_rq(tags, nvme_tag_from_cid(command_id));
  460. }
  461. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  462. void nvme_fault_inject_init(struct nvme_fault_inject *fault_inj,
  463. const char *dev_name);
  464. void nvme_fault_inject_fini(struct nvme_fault_inject *fault_inject);
  465. void nvme_should_fail(struct request *req);
  466. #else
  467. static inline void nvme_fault_inject_init(struct nvme_fault_inject *fault_inj,
  468. const char *dev_name)
  469. {
  470. }
  471. static inline void nvme_fault_inject_fini(struct nvme_fault_inject *fault_inj)
  472. {
  473. }
  474. static inline void nvme_should_fail(struct request *req) {}
  475. #endif
  476. static inline int nvme_reset_subsystem(struct nvme_ctrl *ctrl)
  477. {
  478. if (!ctrl->subsystem)
  479. return -ENOTTY;
  480. return ctrl->ops->reg_write32(ctrl, NVME_REG_NSSR, 0x4E564D65);
  481. }
  482. /*
  483. * Convert a 512B sector number to a device logical block number.
  484. */
  485. static inline u64 nvme_sect_to_lba(struct nvme_ns *ns, sector_t sector)
  486. {
  487. return sector >> (ns->lba_shift - SECTOR_SHIFT);
  488. }
  489. /*
  490. * Convert a device logical block number to a 512B sector number.
  491. */
  492. static inline sector_t nvme_lba_to_sect(struct nvme_ns *ns, u64 lba)
  493. {
  494. return lba << (ns->lba_shift - SECTOR_SHIFT);
  495. }
  496. /*
  497. * Convert byte length to nvme's 0-based num dwords
  498. */
  499. static inline u32 nvme_bytes_to_numd(size_t len)
  500. {
  501. return (len >> 2) - 1;
  502. }
  503. static inline bool nvme_is_ana_error(u16 status)
  504. {
  505. switch (status & 0x7ff) {
  506. case NVME_SC_ANA_TRANSITION:
  507. case NVME_SC_ANA_INACCESSIBLE:
  508. case NVME_SC_ANA_PERSISTENT_LOSS:
  509. return true;
  510. default:
  511. return false;
  512. }
  513. }
  514. static inline bool nvme_is_path_error(u16 status)
  515. {
  516. /* check for a status code type of 'path related status' */
  517. return (status & 0x700) == 0x300;
  518. }
  519. /*
  520. * Fill in the status and result information from the CQE, and then figure out
  521. * if blk-mq will need to use IPI magic to complete the request, and if yes do
  522. * so. If not let the caller complete the request without an indirect function
  523. * call.
  524. */
  525. static inline bool nvme_try_complete_req(struct request *req, __le16 status,
  526. union nvme_result result)
  527. {
  528. struct nvme_request *rq = nvme_req(req);
  529. rq->status = le16_to_cpu(status) >> 1;
  530. rq->result = result;
  531. /* inject error when permitted by fault injection framework */
  532. nvme_should_fail(req);
  533. if (unlikely(blk_should_fake_timeout(req->q)))
  534. return true;
  535. return blk_mq_complete_request_remote(req);
  536. }
  537. static inline void nvme_get_ctrl(struct nvme_ctrl *ctrl)
  538. {
  539. get_device(ctrl->device);
  540. }
  541. static inline void nvme_put_ctrl(struct nvme_ctrl *ctrl)
  542. {
  543. put_device(ctrl->device);
  544. }
  545. static inline bool nvme_is_aen_req(u16 qid, __u16 command_id)
  546. {
  547. return !qid &&
  548. nvme_tag_from_cid(command_id) >= NVME_AQ_BLK_MQ_DEPTH;
  549. }
  550. void nvme_complete_rq(struct request *req);
  551. bool nvme_cancel_request(struct request *req, void *data, bool reserved);
  552. void nvme_cancel_tagset(struct nvme_ctrl *ctrl);
  553. void nvme_cancel_admin_tagset(struct nvme_ctrl *ctrl);
  554. bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
  555. enum nvme_ctrl_state new_state);
  556. bool nvme_wait_reset(struct nvme_ctrl *ctrl);
  557. int nvme_disable_ctrl(struct nvme_ctrl *ctrl);
  558. int nvme_enable_ctrl(struct nvme_ctrl *ctrl);
  559. int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl);
  560. int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
  561. const struct nvme_ctrl_ops *ops, unsigned long quirks);
  562. void nvme_uninit_ctrl(struct nvme_ctrl *ctrl);
  563. void nvme_start_ctrl(struct nvme_ctrl *ctrl);
  564. void nvme_stop_ctrl(struct nvme_ctrl *ctrl);
  565. int nvme_init_identify(struct nvme_ctrl *ctrl);
  566. void nvme_remove_namespaces(struct nvme_ctrl *ctrl);
  567. int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
  568. bool send);
  569. void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
  570. volatile union nvme_result *res);
  571. void nvme_stop_queues(struct nvme_ctrl *ctrl);
  572. void nvme_start_queues(struct nvme_ctrl *ctrl);
  573. void nvme_kill_queues(struct nvme_ctrl *ctrl);
  574. void nvme_sync_queues(struct nvme_ctrl *ctrl);
  575. void nvme_sync_io_queues(struct nvme_ctrl *ctrl);
  576. void nvme_unfreeze(struct nvme_ctrl *ctrl);
  577. void nvme_wait_freeze(struct nvme_ctrl *ctrl);
  578. int nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout);
  579. void nvme_start_freeze(struct nvme_ctrl *ctrl);
  580. #define NVME_QID_ANY -1
  581. struct request *nvme_alloc_request(struct request_queue *q,
  582. struct nvme_command *cmd, blk_mq_req_flags_t flags, int qid);
  583. void nvme_cleanup_cmd(struct request *req);
  584. blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
  585. struct nvme_command *cmd);
  586. int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  587. void *buf, unsigned bufflen);
  588. int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  589. union nvme_result *result, void *buffer, unsigned bufflen,
  590. unsigned timeout, int qid, int at_head,
  591. blk_mq_req_flags_t flags, bool poll);
  592. int nvme_set_features(struct nvme_ctrl *dev, unsigned int fid,
  593. unsigned int dword11, void *buffer, size_t buflen,
  594. u32 *result);
  595. int nvme_get_features(struct nvme_ctrl *dev, unsigned int fid,
  596. unsigned int dword11, void *buffer, size_t buflen,
  597. u32 *result);
  598. int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count);
  599. void nvme_stop_keep_alive(struct nvme_ctrl *ctrl);
  600. int nvme_reset_ctrl(struct nvme_ctrl *ctrl);
  601. int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl);
  602. int nvme_try_sched_reset(struct nvme_ctrl *ctrl);
  603. int nvme_delete_ctrl(struct nvme_ctrl *ctrl);
  604. int nvme_get_log(struct nvme_ctrl *ctrl, u32 nsid, u8 log_page, u8 lsp, u8 csi,
  605. void *log, size_t size, u64 offset);
  606. struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk,
  607. struct nvme_ns_head **head, int *srcu_idx);
  608. void nvme_put_ns_from_disk(struct nvme_ns_head *head, int idx);
  609. extern const struct attribute_group *nvme_ns_id_attr_groups[];
  610. extern const struct block_device_operations nvme_ns_head_ops;
  611. #ifdef CONFIG_NVME_MULTIPATH
  612. static inline bool nvme_ctrl_use_ana(struct nvme_ctrl *ctrl)
  613. {
  614. return ctrl->ana_log_buf != NULL;
  615. }
  616. void nvme_mpath_unfreeze(struct nvme_subsystem *subsys);
  617. void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys);
  618. void nvme_mpath_start_freeze(struct nvme_subsystem *subsys);
  619. void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
  620. struct nvme_ctrl *ctrl, int *flags);
  621. void nvme_failover_req(struct request *req);
  622. void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl);
  623. int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl,struct nvme_ns_head *head);
  624. void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id);
  625. void nvme_mpath_remove_disk(struct nvme_ns_head *head);
  626. int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id);
  627. void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl);
  628. void nvme_mpath_uninit(struct nvme_ctrl *ctrl);
  629. void nvme_mpath_stop(struct nvme_ctrl *ctrl);
  630. bool nvme_mpath_clear_current_path(struct nvme_ns *ns);
  631. void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl);
  632. struct nvme_ns *nvme_find_path(struct nvme_ns_head *head);
  633. blk_qc_t nvme_ns_head_submit_bio(struct bio *bio);
  634. static inline void nvme_mpath_check_last_path(struct nvme_ns *ns)
  635. {
  636. struct nvme_ns_head *head = ns->head;
  637. if (head->disk && list_empty(&head->list))
  638. kblockd_schedule_work(&head->requeue_work);
  639. }
  640. static inline void nvme_trace_bio_complete(struct request *req,
  641. blk_status_t status)
  642. {
  643. struct nvme_ns *ns = req->q->queuedata;
  644. if (req->cmd_flags & REQ_NVME_MPATH)
  645. trace_block_bio_complete(ns->head->disk->queue, req->bio);
  646. }
  647. extern struct device_attribute dev_attr_ana_grpid;
  648. extern struct device_attribute dev_attr_ana_state;
  649. extern struct device_attribute subsys_attr_iopolicy;
  650. #else
  651. static inline bool nvme_ctrl_use_ana(struct nvme_ctrl *ctrl)
  652. {
  653. return false;
  654. }
  655. /*
  656. * Without the multipath code enabled, multiple controller per subsystems are
  657. * visible as devices and thus we cannot use the subsystem instance.
  658. */
  659. static inline void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
  660. struct nvme_ctrl *ctrl, int *flags)
  661. {
  662. sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
  663. }
  664. static inline void nvme_failover_req(struct request *req)
  665. {
  666. }
  667. static inline void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
  668. {
  669. }
  670. static inline int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl,
  671. struct nvme_ns_head *head)
  672. {
  673. return 0;
  674. }
  675. static inline void nvme_mpath_add_disk(struct nvme_ns *ns,
  676. struct nvme_id_ns *id)
  677. {
  678. }
  679. static inline void nvme_mpath_remove_disk(struct nvme_ns_head *head)
  680. {
  681. }
  682. static inline bool nvme_mpath_clear_current_path(struct nvme_ns *ns)
  683. {
  684. return false;
  685. }
  686. static inline void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
  687. {
  688. }
  689. static inline void nvme_mpath_check_last_path(struct nvme_ns *ns)
  690. {
  691. }
  692. static inline void nvme_trace_bio_complete(struct request *req,
  693. blk_status_t status)
  694. {
  695. }
  696. static inline void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl)
  697. {
  698. }
  699. static inline int nvme_mpath_init_identify(struct nvme_ctrl *ctrl,
  700. struct nvme_id_ctrl *id)
  701. {
  702. if (ctrl->subsys->cmic & (1 << 3))
  703. dev_warn(ctrl->device,
  704. "Please enable CONFIG_NVME_MULTIPATH for full support of multi-port devices.\n");
  705. return 0;
  706. }
  707. static inline void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
  708. {
  709. }
  710. static inline void nvme_mpath_stop(struct nvme_ctrl *ctrl)
  711. {
  712. }
  713. static inline void nvme_mpath_unfreeze(struct nvme_subsystem *subsys)
  714. {
  715. }
  716. static inline void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys)
  717. {
  718. }
  719. static inline void nvme_mpath_start_freeze(struct nvme_subsystem *subsys)
  720. {
  721. }
  722. #endif /* CONFIG_NVME_MULTIPATH */
  723. int nvme_revalidate_zones(struct nvme_ns *ns);
  724. #ifdef CONFIG_BLK_DEV_ZONED
  725. int nvme_update_zone_info(struct nvme_ns *ns, unsigned lbaf);
  726. int nvme_report_zones(struct gendisk *disk, sector_t sector,
  727. unsigned int nr_zones, report_zones_cb cb, void *data);
  728. blk_status_t nvme_setup_zone_mgmt_send(struct nvme_ns *ns, struct request *req,
  729. struct nvme_command *cmnd,
  730. enum nvme_zone_mgmt_action action);
  731. #else
  732. #define nvme_report_zones NULL
  733. static inline blk_status_t nvme_setup_zone_mgmt_send(struct nvme_ns *ns,
  734. struct request *req, struct nvme_command *cmnd,
  735. enum nvme_zone_mgmt_action action)
  736. {
  737. return BLK_STS_NOTSUPP;
  738. }
  739. static inline int nvme_update_zone_info(struct nvme_ns *ns, unsigned lbaf)
  740. {
  741. dev_warn(ns->ctrl->device,
  742. "Please enable CONFIG_BLK_DEV_ZONED to support ZNS devices\n");
  743. return -EPROTONOSUPPORT;
  744. }
  745. #endif
  746. #ifdef CONFIG_NVM
  747. int nvme_nvm_register(struct nvme_ns *ns, char *disk_name, int node);
  748. void nvme_nvm_unregister(struct nvme_ns *ns);
  749. extern const struct attribute_group nvme_nvm_attr_group;
  750. int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd, unsigned long arg);
  751. #else
  752. static inline int nvme_nvm_register(struct nvme_ns *ns, char *disk_name,
  753. int node)
  754. {
  755. return 0;
  756. }
  757. static inline void nvme_nvm_unregister(struct nvme_ns *ns) {};
  758. static inline int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd,
  759. unsigned long arg)
  760. {
  761. return -ENOTTY;
  762. }
  763. #endif /* CONFIG_NVM */
  764. static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev)
  765. {
  766. return dev_to_disk(dev)->private_data;
  767. }
  768. #ifdef CONFIG_NVME_HWMON
  769. int nvme_hwmon_init(struct nvme_ctrl *ctrl);
  770. #else
  771. static inline int nvme_hwmon_init(struct nvme_ctrl *ctrl)
  772. {
  773. return 0;
  774. }
  775. #endif
  776. u32 nvme_command_effects(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
  777. u8 opcode);
  778. void nvme_execute_passthru_rq(struct request *rq);
  779. struct nvme_ctrl *nvme_ctrl_from_file(struct file *file);
  780. struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid);
  781. void nvme_put_ns(struct nvme_ns *ns);
  782. #endif /* _NVME_H */