nvme.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. struct nvme_ctrl *ctrl;
  90. };
  91. /*
  92. * Mark a bio as coming in through the mpath node.
  93. */
  94. #define REQ_NVME_MPATH REQ_DRV
  95. enum {
  96. NVME_REQ_CANCELLED = (1 << 0),
  97. NVME_REQ_USERCMD = (1 << 1),
  98. };
  99. static inline struct nvme_request *nvme_req(struct request *req)
  100. {
  101. return blk_mq_rq_to_pdu(req);
  102. }
  103. static inline u16 nvme_req_qid(struct request *req)
  104. {
  105. if (!req->rq_disk)
  106. return 0;
  107. return blk_mq_unique_tag_to_hwq(blk_mq_unique_tag(req)) + 1;
  108. }
  109. /* The below value is the specific amount of delay needed before checking
  110. * readiness in case of the PCI_DEVICE(0x1c58, 0x0003), which needs the
  111. * NVME_QUIRK_DELAY_BEFORE_CHK_RDY quirk enabled. The value (in ms) was
  112. * found empirically.
  113. */
  114. #define NVME_QUIRK_DELAY_AMOUNT 2300
  115. enum nvme_ctrl_state {
  116. NVME_CTRL_NEW,
  117. NVME_CTRL_LIVE,
  118. NVME_CTRL_ADMIN_ONLY, /* Only admin queue live */
  119. NVME_CTRL_RESETTING,
  120. NVME_CTRL_CONNECTING,
  121. NVME_CTRL_DELETING,
  122. NVME_CTRL_DEAD,
  123. };
  124. struct nvme_ctrl {
  125. enum nvme_ctrl_state state;
  126. bool identified;
  127. spinlock_t lock;
  128. const struct nvme_ctrl_ops *ops;
  129. struct request_queue *admin_q;
  130. struct request_queue *connect_q;
  131. struct device *dev;
  132. int instance;
  133. struct blk_mq_tag_set *tagset;
  134. struct blk_mq_tag_set *admin_tagset;
  135. struct list_head namespaces;
  136. struct rw_semaphore namespaces_rwsem;
  137. struct device ctrl_device;
  138. struct device *device; /* char device */
  139. struct cdev cdev;
  140. struct work_struct reset_work;
  141. struct work_struct delete_work;
  142. struct nvme_subsystem *subsys;
  143. struct list_head subsys_entry;
  144. struct opal_dev *opal_dev;
  145. char name[12];
  146. u16 cntlid;
  147. u32 ctrl_config;
  148. u16 mtfa;
  149. u32 queue_count;
  150. u64 cap;
  151. u32 page_size;
  152. u32 max_hw_sectors;
  153. u32 max_segments;
  154. u16 oncs;
  155. u16 oacs;
  156. u16 nssa;
  157. u16 nr_streams;
  158. u32 max_namespaces;
  159. atomic_t abort_limit;
  160. u8 vwc;
  161. u32 vs;
  162. u32 sgls;
  163. u16 kas;
  164. u8 npss;
  165. u8 apsta;
  166. u32 oaes;
  167. u32 aen_result;
  168. unsigned int shutdown_timeout;
  169. unsigned int kato;
  170. bool subsystem;
  171. unsigned long quirks;
  172. struct nvme_id_power_state psd[32];
  173. struct nvme_effects_log *effects;
  174. struct work_struct scan_work;
  175. struct work_struct async_event_work;
  176. struct delayed_work ka_work;
  177. struct nvme_command ka_cmd;
  178. struct work_struct fw_act_work;
  179. unsigned long events;
  180. #ifdef CONFIG_NVME_MULTIPATH
  181. /* asymmetric namespace access: */
  182. u8 anacap;
  183. u8 anatt;
  184. u32 anagrpmax;
  185. u32 nanagrpid;
  186. struct mutex ana_lock;
  187. struct nvme_ana_rsp_hdr *ana_log_buf;
  188. size_t ana_log_size;
  189. struct timer_list anatt_timer;
  190. struct work_struct ana_work;
  191. #endif
  192. /* Power saving configuration */
  193. u64 ps_max_latency_us;
  194. bool apst_enabled;
  195. /* PCIe only: */
  196. u32 hmpre;
  197. u32 hmmin;
  198. u32 hmminds;
  199. u16 hmmaxd;
  200. /* Fabrics only */
  201. u16 sqsize;
  202. u32 ioccsz;
  203. u32 iorcsz;
  204. u16 icdoff;
  205. u16 maxcmd;
  206. int nr_reconnects;
  207. struct nvmf_ctrl_options *opts;
  208. };
  209. struct nvme_subsystem {
  210. int instance;
  211. struct device dev;
  212. /*
  213. * Because we unregister the device on the last put we need
  214. * a separate refcount.
  215. */
  216. struct kref ref;
  217. struct list_head entry;
  218. struct mutex lock;
  219. struct list_head ctrls;
  220. struct list_head nsheads;
  221. char subnqn[NVMF_NQN_SIZE];
  222. char serial[20];
  223. char model[40];
  224. char firmware_rev[8];
  225. u8 cmic;
  226. u16 vendor_id;
  227. struct ida ns_ida;
  228. };
  229. /*
  230. * Container structure for uniqueue namespace identifiers.
  231. */
  232. struct nvme_ns_ids {
  233. u8 eui64[8];
  234. u8 nguid[16];
  235. uuid_t uuid;
  236. };
  237. /*
  238. * Anchor structure for namespaces. There is one for each namespace in a
  239. * NVMe subsystem that any of our controllers can see, and the namespace
  240. * structure for each controller is chained of it. For private namespaces
  241. * there is a 1:1 relation to our namespace structures, that is ->list
  242. * only ever has a single entry for private namespaces.
  243. */
  244. struct nvme_ns_head {
  245. struct list_head list;
  246. struct srcu_struct srcu;
  247. struct nvme_subsystem *subsys;
  248. unsigned ns_id;
  249. struct nvme_ns_ids ids;
  250. struct list_head entry;
  251. struct kref ref;
  252. int instance;
  253. #ifdef CONFIG_NVME_MULTIPATH
  254. struct gendisk *disk;
  255. struct bio_list requeue_list;
  256. spinlock_t requeue_lock;
  257. struct work_struct requeue_work;
  258. struct mutex lock;
  259. struct nvme_ns __rcu *current_path[];
  260. #endif
  261. };
  262. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  263. struct nvme_fault_inject {
  264. struct fault_attr attr;
  265. struct dentry *parent;
  266. bool dont_retry; /* DNR, do not retry */
  267. u16 status; /* status code */
  268. };
  269. #endif
  270. struct nvme_ns {
  271. struct list_head list;
  272. struct nvme_ctrl *ctrl;
  273. struct request_queue *queue;
  274. struct gendisk *disk;
  275. #ifdef CONFIG_NVME_MULTIPATH
  276. enum nvme_ana_state ana_state;
  277. u32 ana_grpid;
  278. #endif
  279. struct list_head siblings;
  280. struct nvm_dev *ndev;
  281. struct kref kref;
  282. struct nvme_ns_head *head;
  283. int lba_shift;
  284. u16 ms;
  285. u16 sgs;
  286. u32 sws;
  287. bool ext;
  288. u8 pi_type;
  289. unsigned long flags;
  290. #define NVME_NS_REMOVING 0
  291. #define NVME_NS_DEAD 1
  292. #define NVME_NS_ANA_PENDING 2
  293. u16 noiob;
  294. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  295. struct nvme_fault_inject fault_inject;
  296. #endif
  297. };
  298. struct nvme_ctrl_ops {
  299. const char *name;
  300. struct module *module;
  301. unsigned int flags;
  302. #define NVME_F_FABRICS (1 << 0)
  303. #define NVME_F_METADATA_SUPPORTED (1 << 1)
  304. int (*reg_read32)(struct nvme_ctrl *ctrl, u32 off, u32 *val);
  305. int (*reg_write32)(struct nvme_ctrl *ctrl, u32 off, u32 val);
  306. int (*reg_read64)(struct nvme_ctrl *ctrl, u32 off, u64 *val);
  307. void (*free_ctrl)(struct nvme_ctrl *ctrl);
  308. void (*submit_async_event)(struct nvme_ctrl *ctrl);
  309. void (*delete_ctrl)(struct nvme_ctrl *ctrl);
  310. int (*get_address)(struct nvme_ctrl *ctrl, char *buf, int size);
  311. void (*stop_ctrl)(struct nvme_ctrl *ctrl);
  312. };
  313. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  314. void nvme_fault_inject_init(struct nvme_ns *ns);
  315. void nvme_fault_inject_fini(struct nvme_ns *ns);
  316. void nvme_should_fail(struct request *req);
  317. #else
  318. static inline void nvme_fault_inject_init(struct nvme_ns *ns) {}
  319. static inline void nvme_fault_inject_fini(struct nvme_ns *ns) {}
  320. static inline void nvme_should_fail(struct request *req) {}
  321. #endif
  322. static inline bool nvme_ctrl_ready(struct nvme_ctrl *ctrl)
  323. {
  324. u32 val = 0;
  325. if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &val))
  326. return false;
  327. return val & NVME_CSTS_RDY;
  328. }
  329. static inline int nvme_reset_subsystem(struct nvme_ctrl *ctrl)
  330. {
  331. if (!ctrl->subsystem)
  332. return -ENOTTY;
  333. return ctrl->ops->reg_write32(ctrl, NVME_REG_NSSR, 0x4E564D65);
  334. }
  335. static inline u64 nvme_block_nr(struct nvme_ns *ns, sector_t sector)
  336. {
  337. return (sector >> (ns->lba_shift - 9));
  338. }
  339. static inline void nvme_end_request(struct request *req, __le16 status,
  340. union nvme_result result)
  341. {
  342. struct nvme_request *rq = nvme_req(req);
  343. rq->status = le16_to_cpu(status) >> 1;
  344. rq->result = result;
  345. /* inject error when permitted by fault injection framework */
  346. nvme_should_fail(req);
  347. blk_mq_complete_request(req);
  348. }
  349. static inline void nvme_get_ctrl(struct nvme_ctrl *ctrl)
  350. {
  351. get_device(ctrl->device);
  352. }
  353. static inline void nvme_put_ctrl(struct nvme_ctrl *ctrl)
  354. {
  355. put_device(ctrl->device);
  356. }
  357. void nvme_complete_rq(struct request *req);
  358. void nvme_cancel_request(struct request *req, void *data, bool reserved);
  359. bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
  360. enum nvme_ctrl_state new_state);
  361. int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap);
  362. int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap);
  363. int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl);
  364. int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
  365. const struct nvme_ctrl_ops *ops, unsigned long quirks);
  366. void nvme_uninit_ctrl(struct nvme_ctrl *ctrl);
  367. void nvme_start_ctrl(struct nvme_ctrl *ctrl);
  368. void nvme_stop_ctrl(struct nvme_ctrl *ctrl);
  369. void nvme_put_ctrl(struct nvme_ctrl *ctrl);
  370. int nvme_init_identify(struct nvme_ctrl *ctrl);
  371. void nvme_remove_namespaces(struct nvme_ctrl *ctrl);
  372. int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
  373. bool send);
  374. void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
  375. volatile union nvme_result *res);
  376. void nvme_stop_queues(struct nvme_ctrl *ctrl);
  377. void nvme_start_queues(struct nvme_ctrl *ctrl);
  378. void nvme_kill_queues(struct nvme_ctrl *ctrl);
  379. void nvme_unfreeze(struct nvme_ctrl *ctrl);
  380. void nvme_wait_freeze(struct nvme_ctrl *ctrl);
  381. void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout);
  382. void nvme_start_freeze(struct nvme_ctrl *ctrl);
  383. #define NVME_QID_ANY -1
  384. struct request *nvme_alloc_request(struct request_queue *q,
  385. struct nvme_command *cmd, blk_mq_req_flags_t flags, int qid);
  386. void nvme_cleanup_cmd(struct request *req);
  387. blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
  388. struct nvme_command *cmd);
  389. int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  390. void *buf, unsigned bufflen);
  391. int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  392. union nvme_result *result, void *buffer, unsigned bufflen,
  393. unsigned timeout, int qid, int at_head,
  394. blk_mq_req_flags_t flags);
  395. int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count);
  396. void nvme_stop_keep_alive(struct nvme_ctrl *ctrl);
  397. int nvme_reset_ctrl(struct nvme_ctrl *ctrl);
  398. int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl);
  399. int nvme_delete_ctrl(struct nvme_ctrl *ctrl);
  400. int nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl);
  401. int nvme_get_log(struct nvme_ctrl *ctrl, u32 nsid, u8 log_page, u8 lsp,
  402. void *log, size_t size, u64 offset);
  403. extern const struct attribute_group *nvme_ns_id_attr_groups[];
  404. extern const struct block_device_operations nvme_ns_head_ops;
  405. #ifdef CONFIG_NVME_MULTIPATH
  406. bool nvme_ctrl_use_ana(struct nvme_ctrl *ctrl);
  407. void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
  408. struct nvme_ctrl *ctrl, int *flags);
  409. void nvme_failover_req(struct request *req);
  410. void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl);
  411. int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl,struct nvme_ns_head *head);
  412. void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id);
  413. void nvme_mpath_remove_disk(struct nvme_ns_head *head);
  414. int nvme_mpath_init(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id);
  415. void nvme_mpath_uninit(struct nvme_ctrl *ctrl);
  416. void nvme_mpath_stop(struct nvme_ctrl *ctrl);
  417. void nvme_mpath_clear_current_path(struct nvme_ns *ns);
  418. struct nvme_ns *nvme_find_path(struct nvme_ns_head *head);
  419. static inline void nvme_mpath_check_last_path(struct nvme_ns *ns)
  420. {
  421. struct nvme_ns_head *head = ns->head;
  422. if (head->disk && list_empty(&head->list))
  423. kblockd_schedule_work(&head->requeue_work);
  424. }
  425. extern struct device_attribute dev_attr_ana_grpid;
  426. extern struct device_attribute dev_attr_ana_state;
  427. #else
  428. static inline bool nvme_ctrl_use_ana(struct nvme_ctrl *ctrl)
  429. {
  430. return false;
  431. }
  432. /*
  433. * Without the multipath code enabled, multiple controller per subsystems are
  434. * visible as devices and thus we cannot use the subsystem instance.
  435. */
  436. static inline void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
  437. struct nvme_ctrl *ctrl, int *flags)
  438. {
  439. sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
  440. }
  441. static inline void nvme_failover_req(struct request *req)
  442. {
  443. }
  444. static inline void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
  445. {
  446. }
  447. static inline int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl,
  448. struct nvme_ns_head *head)
  449. {
  450. return 0;
  451. }
  452. static inline void nvme_mpath_add_disk(struct nvme_ns *ns,
  453. struct nvme_id_ns *id)
  454. {
  455. }
  456. static inline void nvme_mpath_remove_disk(struct nvme_ns_head *head)
  457. {
  458. }
  459. static inline void nvme_mpath_clear_current_path(struct nvme_ns *ns)
  460. {
  461. }
  462. static inline void nvme_mpath_check_last_path(struct nvme_ns *ns)
  463. {
  464. }
  465. static inline int nvme_mpath_init(struct nvme_ctrl *ctrl,
  466. struct nvme_id_ctrl *id)
  467. {
  468. return 0;
  469. }
  470. static inline void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
  471. {
  472. }
  473. static inline void nvme_mpath_stop(struct nvme_ctrl *ctrl)
  474. {
  475. }
  476. #endif /* CONFIG_NVME_MULTIPATH */
  477. #ifdef CONFIG_NVM
  478. void nvme_nvm_update_nvm_info(struct nvme_ns *ns);
  479. int nvme_nvm_register(struct nvme_ns *ns, char *disk_name, int node);
  480. void nvme_nvm_unregister(struct nvme_ns *ns);
  481. extern const struct attribute_group nvme_nvm_attr_group;
  482. int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd, unsigned long arg);
  483. #else
  484. static inline void nvme_nvm_update_nvm_info(struct nvme_ns *ns) {};
  485. static inline int nvme_nvm_register(struct nvme_ns *ns, char *disk_name,
  486. int node)
  487. {
  488. return 0;
  489. }
  490. static inline void nvme_nvm_unregister(struct nvme_ns *ns) {};
  491. static inline int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd,
  492. unsigned long arg)
  493. {
  494. return -ENOTTY;
  495. }
  496. #endif /* CONFIG_NVM */
  497. static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev)
  498. {
  499. return dev_to_disk(dev)->private_data;
  500. }
  501. int __init nvme_core_init(void);
  502. void nvme_core_exit(void);
  503. #endif /* _NVME_H */