nvme.h 14 KB

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