nvme.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * Copyright (c) 2011-2014, Intel Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. */
  13. #ifndef _NVME_H
  14. #define _NVME_H
  15. #include <linux/nvme.h>
  16. #include <linux/cdev.h>
  17. #include <linux/pci.h>
  18. #include <linux/kref.h>
  19. #include <linux/blk-mq.h>
  20. #include <linux/lightnvm.h>
  21. #include <linux/sed-opal.h>
  22. #include <linux/fault-inject.h>
  23. extern unsigned int nvme_io_timeout;
  24. #define NVME_IO_TIMEOUT (nvme_io_timeout * HZ)
  25. extern unsigned int admin_timeout;
  26. #define ADMIN_TIMEOUT (admin_timeout * HZ)
  27. #define NVME_DEFAULT_KATO 5
  28. #define NVME_KATO_GRACE 10
  29. extern struct workqueue_struct *nvme_wq;
  30. extern struct workqueue_struct *nvme_reset_wq;
  31. extern struct workqueue_struct *nvme_delete_wq;
  32. enum {
  33. NVME_NS_LBA = 0,
  34. NVME_NS_LIGHTNVM = 1,
  35. };
  36. /*
  37. * List of workarounds for devices that required behavior not specified in
  38. * the standard.
  39. */
  40. enum nvme_quirks {
  41. /*
  42. * Prefers I/O aligned to a stripe size specified in a vendor
  43. * specific Identify field.
  44. */
  45. NVME_QUIRK_STRIPE_SIZE = (1 << 0),
  46. /*
  47. * The controller doesn't handle Identify value others than 0 or 1
  48. * correctly.
  49. */
  50. NVME_QUIRK_IDENTIFY_CNS = (1 << 1),
  51. /*
  52. * The controller deterministically returns O's on reads to
  53. * logical blocks that deallocate was called on.
  54. */
  55. NVME_QUIRK_DEALLOCATE_ZEROES = (1 << 2),
  56. /*
  57. * The controller needs a delay before starts checking the device
  58. * readiness, which is done by reading the NVME_CSTS_RDY bit.
  59. */
  60. NVME_QUIRK_DELAY_BEFORE_CHK_RDY = (1 << 3),
  61. /*
  62. * APST should not be used.
  63. */
  64. NVME_QUIRK_NO_APST = (1 << 4),
  65. /*
  66. * The deepest sleep state should not be used.
  67. */
  68. NVME_QUIRK_NO_DEEPEST_PS = (1 << 5),
  69. /*
  70. * Supports the LighNVM command set if indicated in vs[1].
  71. */
  72. NVME_QUIRK_LIGHTNVM = (1 << 6),
  73. };
  74. /*
  75. * Common request structure for NVMe passthrough. All drivers must have
  76. * this structure as the first member of their request-private data.
  77. */
  78. struct nvme_request {
  79. struct nvme_command *cmd;
  80. union nvme_result result;
  81. u8 retries;
  82. u8 flags;
  83. u16 status;
  84. };
  85. /*
  86. * Mark a bio as coming in through the mpath node.
  87. */
  88. #define REQ_NVME_MPATH REQ_DRV
  89. enum {
  90. NVME_REQ_CANCELLED = (1 << 0),
  91. NVME_REQ_USERCMD = (1 << 1),
  92. };
  93. static inline struct nvme_request *nvme_req(struct request *req)
  94. {
  95. return blk_mq_rq_to_pdu(req);
  96. }
  97. /* The below value is the specific amount of delay needed before checking
  98. * readiness in case of the PCI_DEVICE(0x1c58, 0x0003), which needs the
  99. * NVME_QUIRK_DELAY_BEFORE_CHK_RDY quirk enabled. The value (in ms) was
  100. * found empirically.
  101. */
  102. #define NVME_QUIRK_DELAY_AMOUNT 2300
  103. enum nvme_ctrl_state {
  104. NVME_CTRL_NEW,
  105. NVME_CTRL_LIVE,
  106. NVME_CTRL_ADMIN_ONLY, /* Only admin queue live */
  107. NVME_CTRL_RESETTING,
  108. NVME_CTRL_CONNECTING,
  109. NVME_CTRL_DELETING,
  110. NVME_CTRL_DEAD,
  111. };
  112. struct nvme_ctrl {
  113. enum nvme_ctrl_state state;
  114. bool identified;
  115. spinlock_t lock;
  116. const struct nvme_ctrl_ops *ops;
  117. struct request_queue *admin_q;
  118. struct request_queue *connect_q;
  119. struct device *dev;
  120. int instance;
  121. struct blk_mq_tag_set *tagset;
  122. struct blk_mq_tag_set *admin_tagset;
  123. struct list_head namespaces;
  124. struct rw_semaphore namespaces_rwsem;
  125. struct device ctrl_device;
  126. struct device *device; /* char device */
  127. struct cdev cdev;
  128. struct work_struct reset_work;
  129. struct work_struct delete_work;
  130. struct nvme_subsystem *subsys;
  131. struct list_head subsys_entry;
  132. struct opal_dev *opal_dev;
  133. char name[12];
  134. u16 cntlid;
  135. u32 ctrl_config;
  136. u16 mtfa;
  137. u32 queue_count;
  138. u64 cap;
  139. u32 page_size;
  140. u32 max_hw_sectors;
  141. u16 oncs;
  142. u16 oacs;
  143. u16 nssa;
  144. u16 nr_streams;
  145. atomic_t abort_limit;
  146. u8 vwc;
  147. u32 vs;
  148. u32 sgls;
  149. u16 kas;
  150. u8 npss;
  151. u8 apsta;
  152. u32 aen_result;
  153. unsigned int shutdown_timeout;
  154. unsigned int kato;
  155. bool subsystem;
  156. unsigned long quirks;
  157. struct nvme_id_power_state psd[32];
  158. struct nvme_effects_log *effects;
  159. struct work_struct scan_work;
  160. struct work_struct async_event_work;
  161. struct delayed_work ka_work;
  162. struct nvme_command ka_cmd;
  163. struct work_struct fw_act_work;
  164. /* Power saving configuration */
  165. u64 ps_max_latency_us;
  166. bool apst_enabled;
  167. /* PCIe only: */
  168. u32 hmpre;
  169. u32 hmmin;
  170. u32 hmminds;
  171. u16 hmmaxd;
  172. /* Fabrics only */
  173. u16 sqsize;
  174. u32 ioccsz;
  175. u32 iorcsz;
  176. u16 icdoff;
  177. u16 maxcmd;
  178. int nr_reconnects;
  179. struct nvmf_ctrl_options *opts;
  180. };
  181. struct nvme_subsystem {
  182. int instance;
  183. struct device dev;
  184. /*
  185. * Because we unregister the device on the last put we need
  186. * a separate refcount.
  187. */
  188. struct kref ref;
  189. struct list_head entry;
  190. struct mutex lock;
  191. struct list_head ctrls;
  192. struct list_head nsheads;
  193. char subnqn[NVMF_NQN_SIZE];
  194. char serial[20];
  195. char model[40];
  196. char firmware_rev[8];
  197. u8 cmic;
  198. u16 vendor_id;
  199. struct ida ns_ida;
  200. };
  201. /*
  202. * Container structure for uniqueue namespace identifiers.
  203. */
  204. struct nvme_ns_ids {
  205. u8 eui64[8];
  206. u8 nguid[16];
  207. uuid_t uuid;
  208. };
  209. /*
  210. * Anchor structure for namespaces. There is one for each namespace in a
  211. * NVMe subsystem that any of our controllers can see, and the namespace
  212. * structure for each controller is chained of it. For private namespaces
  213. * there is a 1:1 relation to our namespace structures, that is ->list
  214. * only ever has a single entry for private namespaces.
  215. */
  216. struct nvme_ns_head {
  217. #ifdef CONFIG_NVME_MULTIPATH
  218. struct gendisk *disk;
  219. struct nvme_ns __rcu *current_path;
  220. struct bio_list requeue_list;
  221. spinlock_t requeue_lock;
  222. struct work_struct requeue_work;
  223. #endif
  224. struct list_head list;
  225. struct srcu_struct srcu;
  226. struct nvme_subsystem *subsys;
  227. unsigned ns_id;
  228. struct nvme_ns_ids ids;
  229. struct list_head entry;
  230. struct kref ref;
  231. int instance;
  232. };
  233. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  234. struct nvme_fault_inject {
  235. struct fault_attr attr;
  236. struct dentry *parent;
  237. bool dont_retry; /* DNR, do not retry */
  238. u16 status; /* status code */
  239. };
  240. #endif
  241. struct nvme_ns {
  242. struct list_head list;
  243. struct nvme_ctrl *ctrl;
  244. struct request_queue *queue;
  245. struct gendisk *disk;
  246. struct list_head siblings;
  247. struct nvm_dev *ndev;
  248. struct kref kref;
  249. struct nvme_ns_head *head;
  250. int lba_shift;
  251. u16 ms;
  252. u16 sgs;
  253. u32 sws;
  254. bool ext;
  255. u8 pi_type;
  256. unsigned long flags;
  257. #define NVME_NS_REMOVING 0
  258. #define NVME_NS_DEAD 1
  259. u16 noiob;
  260. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  261. struct nvme_fault_inject fault_inject;
  262. #endif
  263. };
  264. struct nvme_ctrl_ops {
  265. const char *name;
  266. struct module *module;
  267. unsigned int flags;
  268. #define NVME_F_FABRICS (1 << 0)
  269. #define NVME_F_METADATA_SUPPORTED (1 << 1)
  270. int (*reg_read32)(struct nvme_ctrl *ctrl, u32 off, u32 *val);
  271. int (*reg_write32)(struct nvme_ctrl *ctrl, u32 off, u32 val);
  272. int (*reg_read64)(struct nvme_ctrl *ctrl, u32 off, u64 *val);
  273. void (*free_ctrl)(struct nvme_ctrl *ctrl);
  274. void (*submit_async_event)(struct nvme_ctrl *ctrl);
  275. void (*delete_ctrl)(struct nvme_ctrl *ctrl);
  276. int (*get_address)(struct nvme_ctrl *ctrl, char *buf, int size);
  277. int (*reinit_request)(void *data, struct request *rq);
  278. void (*stop_ctrl)(struct nvme_ctrl *ctrl);
  279. };
  280. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  281. void nvme_fault_inject_init(struct nvme_ns *ns);
  282. void nvme_fault_inject_fini(struct nvme_ns *ns);
  283. void nvme_should_fail(struct request *req);
  284. #else
  285. static inline void nvme_fault_inject_init(struct nvme_ns *ns) {}
  286. static inline void nvme_fault_inject_fini(struct nvme_ns *ns) {}
  287. static inline void nvme_should_fail(struct request *req) {}
  288. #endif
  289. static inline bool nvme_ctrl_ready(struct nvme_ctrl *ctrl)
  290. {
  291. u32 val = 0;
  292. if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &val))
  293. return false;
  294. return val & NVME_CSTS_RDY;
  295. }
  296. static inline int nvme_reset_subsystem(struct nvme_ctrl *ctrl)
  297. {
  298. if (!ctrl->subsystem)
  299. return -ENOTTY;
  300. return ctrl->ops->reg_write32(ctrl, NVME_REG_NSSR, 0x4E564D65);
  301. }
  302. static inline u64 nvme_block_nr(struct nvme_ns *ns, sector_t sector)
  303. {
  304. return (sector >> (ns->lba_shift - 9));
  305. }
  306. static inline void nvme_cleanup_cmd(struct request *req)
  307. {
  308. if (req->rq_flags & RQF_SPECIAL_PAYLOAD) {
  309. kfree(page_address(req->special_vec.bv_page) +
  310. req->special_vec.bv_offset);
  311. }
  312. }
  313. static inline void nvme_end_request(struct request *req, __le16 status,
  314. union nvme_result result)
  315. {
  316. struct nvme_request *rq = nvme_req(req);
  317. rq->status = le16_to_cpu(status) >> 1;
  318. rq->result = result;
  319. /* inject error when permitted by fault injection framework */
  320. nvme_should_fail(req);
  321. blk_mq_complete_request(req);
  322. }
  323. static inline void nvme_get_ctrl(struct nvme_ctrl *ctrl)
  324. {
  325. get_device(ctrl->device);
  326. }
  327. static inline void nvme_put_ctrl(struct nvme_ctrl *ctrl)
  328. {
  329. put_device(ctrl->device);
  330. }
  331. void nvme_complete_rq(struct request *req);
  332. void nvme_cancel_request(struct request *req, void *data, bool reserved);
  333. bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
  334. enum nvme_ctrl_state new_state);
  335. int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap);
  336. int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap);
  337. int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl);
  338. int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
  339. const struct nvme_ctrl_ops *ops, unsigned long quirks);
  340. void nvme_uninit_ctrl(struct nvme_ctrl *ctrl);
  341. void nvme_start_ctrl(struct nvme_ctrl *ctrl);
  342. void nvme_stop_ctrl(struct nvme_ctrl *ctrl);
  343. void nvme_put_ctrl(struct nvme_ctrl *ctrl);
  344. int nvme_init_identify(struct nvme_ctrl *ctrl);
  345. void nvme_queue_scan(struct nvme_ctrl *ctrl);
  346. void nvme_remove_namespaces(struct nvme_ctrl *ctrl);
  347. int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
  348. bool send);
  349. void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
  350. union nvme_result *res);
  351. void nvme_stop_queues(struct nvme_ctrl *ctrl);
  352. void nvme_start_queues(struct nvme_ctrl *ctrl);
  353. void nvme_kill_queues(struct nvme_ctrl *ctrl);
  354. void nvme_unfreeze(struct nvme_ctrl *ctrl);
  355. void nvme_wait_freeze(struct nvme_ctrl *ctrl);
  356. void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout);
  357. void nvme_start_freeze(struct nvme_ctrl *ctrl);
  358. int nvme_reinit_tagset(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set);
  359. #define NVME_QID_ANY -1
  360. struct request *nvme_alloc_request(struct request_queue *q,
  361. struct nvme_command *cmd, blk_mq_req_flags_t flags, int qid);
  362. blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
  363. struct nvme_command *cmd);
  364. int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  365. void *buf, unsigned bufflen);
  366. int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  367. union nvme_result *result, void *buffer, unsigned bufflen,
  368. unsigned timeout, int qid, int at_head,
  369. blk_mq_req_flags_t flags);
  370. int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count);
  371. void nvme_stop_keep_alive(struct nvme_ctrl *ctrl);
  372. int nvme_reset_ctrl(struct nvme_ctrl *ctrl);
  373. int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl);
  374. int nvme_delete_ctrl(struct nvme_ctrl *ctrl);
  375. int nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl);
  376. int nvme_get_log_ext(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
  377. u8 log_page, void *log, size_t size, u64 offset);
  378. extern const struct attribute_group nvme_ns_id_attr_group;
  379. extern const struct block_device_operations nvme_ns_head_ops;
  380. #ifdef CONFIG_NVME_MULTIPATH
  381. void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
  382. struct nvme_ctrl *ctrl, int *flags);
  383. void nvme_failover_req(struct request *req);
  384. bool nvme_req_needs_failover(struct request *req, blk_status_t error);
  385. void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl);
  386. int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl,struct nvme_ns_head *head);
  387. void nvme_mpath_add_disk(struct nvme_ns_head *head);
  388. void nvme_mpath_remove_disk(struct nvme_ns_head *head);
  389. static inline void nvme_mpath_clear_current_path(struct nvme_ns *ns)
  390. {
  391. struct nvme_ns_head *head = ns->head;
  392. if (head && ns == srcu_dereference(head->current_path, &head->srcu))
  393. rcu_assign_pointer(head->current_path, NULL);
  394. }
  395. struct nvme_ns *nvme_find_path(struct nvme_ns_head *head);
  396. static inline void nvme_mpath_check_last_path(struct nvme_ns *ns)
  397. {
  398. struct nvme_ns_head *head = ns->head;
  399. if (head->disk && list_empty(&head->list))
  400. kblockd_schedule_work(&head->requeue_work);
  401. }
  402. #else
  403. /*
  404. * Without the multipath code enabled, multiple controller per subsystems are
  405. * visible as devices and thus we cannot use the subsystem instance.
  406. */
  407. static inline void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
  408. struct nvme_ctrl *ctrl, int *flags)
  409. {
  410. sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
  411. }
  412. static inline void nvme_failover_req(struct request *req)
  413. {
  414. }
  415. static inline bool nvme_req_needs_failover(struct request *req,
  416. blk_status_t error)
  417. {
  418. return false;
  419. }
  420. static inline void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
  421. {
  422. }
  423. static inline int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl,
  424. struct nvme_ns_head *head)
  425. {
  426. return 0;
  427. }
  428. static inline void nvme_mpath_add_disk(struct nvme_ns_head *head)
  429. {
  430. }
  431. static inline void nvme_mpath_remove_disk(struct nvme_ns_head *head)
  432. {
  433. }
  434. static inline void nvme_mpath_clear_current_path(struct nvme_ns *ns)
  435. {
  436. }
  437. static inline void nvme_mpath_check_last_path(struct nvme_ns *ns)
  438. {
  439. }
  440. #endif /* CONFIG_NVME_MULTIPATH */
  441. #ifdef CONFIG_NVM
  442. void nvme_nvm_update_nvm_info(struct nvme_ns *ns);
  443. int nvme_nvm_register(struct nvme_ns *ns, char *disk_name, int node);
  444. void nvme_nvm_unregister(struct nvme_ns *ns);
  445. int nvme_nvm_register_sysfs(struct nvme_ns *ns);
  446. void nvme_nvm_unregister_sysfs(struct nvme_ns *ns);
  447. int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd, unsigned long arg);
  448. #else
  449. static inline void nvme_nvm_update_nvm_info(struct nvme_ns *ns) {};
  450. static inline int nvme_nvm_register(struct nvme_ns *ns, char *disk_name,
  451. int node)
  452. {
  453. return 0;
  454. }
  455. static inline void nvme_nvm_unregister(struct nvme_ns *ns) {};
  456. static inline int nvme_nvm_register_sysfs(struct nvme_ns *ns)
  457. {
  458. return 0;
  459. }
  460. static inline void nvme_nvm_unregister_sysfs(struct nvme_ns *ns) {};
  461. static inline int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd,
  462. unsigned long arg)
  463. {
  464. return -ENOTTY;
  465. }
  466. #endif /* CONFIG_NVM */
  467. static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev)
  468. {
  469. return dev_to_disk(dev)->private_data;
  470. }
  471. int __init nvme_core_init(void);
  472. void nvme_core_exit(void);
  473. #endif /* _NVME_H */