nvme.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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/pci.h>
  17. #include <linux/kref.h>
  18. #include <linux/blk-mq.h>
  19. #include <linux/lightnvm.h>
  20. #include <linux/sed-opal.h>
  21. extern unsigned char nvme_io_timeout;
  22. #define NVME_IO_TIMEOUT (nvme_io_timeout * HZ)
  23. extern unsigned char admin_timeout;
  24. #define ADMIN_TIMEOUT (admin_timeout * HZ)
  25. #define NVME_DEFAULT_KATO 5
  26. #define NVME_KATO_GRACE 10
  27. extern struct workqueue_struct *nvme_wq;
  28. enum {
  29. NVME_NS_LBA = 0,
  30. NVME_NS_LIGHTNVM = 1,
  31. };
  32. /*
  33. * List of workarounds for devices that required behavior not specified in
  34. * the standard.
  35. */
  36. enum nvme_quirks {
  37. /*
  38. * Prefers I/O aligned to a stripe size specified in a vendor
  39. * specific Identify field.
  40. */
  41. NVME_QUIRK_STRIPE_SIZE = (1 << 0),
  42. /*
  43. * The controller doesn't handle Identify value others than 0 or 1
  44. * correctly.
  45. */
  46. NVME_QUIRK_IDENTIFY_CNS = (1 << 1),
  47. /*
  48. * The controller deterministically returns O's on reads to
  49. * logical blocks that deallocate was called on.
  50. */
  51. NVME_QUIRK_DEALLOCATE_ZEROES = (1 << 2),
  52. /*
  53. * The controller needs a delay before starts checking the device
  54. * readiness, which is done by reading the NVME_CSTS_RDY bit.
  55. */
  56. NVME_QUIRK_DELAY_BEFORE_CHK_RDY = (1 << 3),
  57. /*
  58. * APST should not be used.
  59. */
  60. NVME_QUIRK_NO_APST = (1 << 4),
  61. /*
  62. * The deepest sleep state should not be used.
  63. */
  64. NVME_QUIRK_NO_DEEPEST_PS = (1 << 5),
  65. /*
  66. * Supports the LighNVM command set if indicated in vs[1].
  67. */
  68. NVME_QUIRK_LIGHTNVM = (1 << 6),
  69. };
  70. /*
  71. * Common request structure for NVMe passthrough. All drivers must have
  72. * this structure as the first member of their request-private data.
  73. */
  74. struct nvme_request {
  75. struct nvme_command *cmd;
  76. union nvme_result result;
  77. u8 retries;
  78. u8 flags;
  79. u16 status;
  80. };
  81. enum {
  82. NVME_REQ_CANCELLED = (1 << 0),
  83. };
  84. static inline struct nvme_request *nvme_req(struct request *req)
  85. {
  86. return blk_mq_rq_to_pdu(req);
  87. }
  88. /* The below value is the specific amount of delay needed before checking
  89. * readiness in case of the PCI_DEVICE(0x1c58, 0x0003), which needs the
  90. * NVME_QUIRK_DELAY_BEFORE_CHK_RDY quirk enabled. The value (in ms) was
  91. * found empirically.
  92. */
  93. #define NVME_QUIRK_DELAY_AMOUNT 2000
  94. enum nvme_ctrl_state {
  95. NVME_CTRL_NEW,
  96. NVME_CTRL_LIVE,
  97. NVME_CTRL_RESETTING,
  98. NVME_CTRL_RECONNECTING,
  99. NVME_CTRL_DELETING,
  100. NVME_CTRL_DEAD,
  101. };
  102. struct nvme_ctrl {
  103. enum nvme_ctrl_state state;
  104. bool identified;
  105. spinlock_t lock;
  106. const struct nvme_ctrl_ops *ops;
  107. struct request_queue *admin_q;
  108. struct request_queue *connect_q;
  109. struct device *dev;
  110. struct kref kref;
  111. int instance;
  112. struct blk_mq_tag_set *tagset;
  113. struct blk_mq_tag_set *admin_tagset;
  114. struct list_head namespaces;
  115. struct mutex namespaces_mutex;
  116. struct device *device; /* char device */
  117. struct list_head node;
  118. struct ida ns_ida;
  119. struct work_struct reset_work;
  120. struct opal_dev *opal_dev;
  121. char name[12];
  122. char serial[20];
  123. char model[40];
  124. char firmware_rev[8];
  125. char subnqn[NVMF_NQN_SIZE];
  126. u16 cntlid;
  127. u32 ctrl_config;
  128. u16 mtfa;
  129. u32 queue_count;
  130. u64 cap;
  131. u32 page_size;
  132. u32 max_hw_sectors;
  133. u16 oncs;
  134. u16 vid;
  135. u16 oacs;
  136. u16 nssa;
  137. u16 nr_streams;
  138. atomic_t abort_limit;
  139. u8 event_limit;
  140. u8 vwc;
  141. u32 vs;
  142. u32 sgls;
  143. u16 kas;
  144. u8 npss;
  145. u8 apsta;
  146. unsigned int shutdown_timeout;
  147. unsigned int kato;
  148. bool subsystem;
  149. unsigned long quirks;
  150. struct nvme_id_power_state psd[32];
  151. struct work_struct scan_work;
  152. struct work_struct async_event_work;
  153. struct delayed_work ka_work;
  154. struct work_struct fw_act_work;
  155. /* Power saving configuration */
  156. u64 ps_max_latency_us;
  157. bool apst_enabled;
  158. /* PCIe only: */
  159. u32 hmpre;
  160. u32 hmmin;
  161. u32 hmminds;
  162. u16 hmmaxd;
  163. /* Fabrics only */
  164. u16 sqsize;
  165. u32 ioccsz;
  166. u32 iorcsz;
  167. u16 icdoff;
  168. u16 maxcmd;
  169. int nr_reconnects;
  170. struct nvmf_ctrl_options *opts;
  171. };
  172. struct nvme_ns {
  173. struct list_head list;
  174. struct nvme_ctrl *ctrl;
  175. struct request_queue *queue;
  176. struct gendisk *disk;
  177. struct nvm_dev *ndev;
  178. struct kref kref;
  179. int instance;
  180. u8 eui[8];
  181. u8 nguid[16];
  182. uuid_t uuid;
  183. unsigned ns_id;
  184. int lba_shift;
  185. u16 ms;
  186. u16 sgs;
  187. u32 sws;
  188. bool ext;
  189. u8 pi_type;
  190. unsigned long flags;
  191. #define NVME_NS_REMOVING 0
  192. #define NVME_NS_DEAD 1
  193. u16 noiob;
  194. };
  195. struct nvme_ctrl_ops {
  196. const char *name;
  197. struct module *module;
  198. unsigned int flags;
  199. #define NVME_F_FABRICS (1 << 0)
  200. #define NVME_F_METADATA_SUPPORTED (1 << 1)
  201. int (*reg_read32)(struct nvme_ctrl *ctrl, u32 off, u32 *val);
  202. int (*reg_write32)(struct nvme_ctrl *ctrl, u32 off, u32 val);
  203. int (*reg_read64)(struct nvme_ctrl *ctrl, u32 off, u64 *val);
  204. void (*free_ctrl)(struct nvme_ctrl *ctrl);
  205. void (*submit_async_event)(struct nvme_ctrl *ctrl, int aer_idx);
  206. int (*delete_ctrl)(struct nvme_ctrl *ctrl);
  207. int (*get_address)(struct nvme_ctrl *ctrl, char *buf, int size);
  208. };
  209. static inline bool nvme_ctrl_ready(struct nvme_ctrl *ctrl)
  210. {
  211. u32 val = 0;
  212. if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &val))
  213. return false;
  214. return val & NVME_CSTS_RDY;
  215. }
  216. static inline int nvme_reset_subsystem(struct nvme_ctrl *ctrl)
  217. {
  218. if (!ctrl->subsystem)
  219. return -ENOTTY;
  220. return ctrl->ops->reg_write32(ctrl, NVME_REG_NSSR, 0x4E564D65);
  221. }
  222. static inline u64 nvme_block_nr(struct nvme_ns *ns, sector_t sector)
  223. {
  224. return (sector >> (ns->lba_shift - 9));
  225. }
  226. static inline void nvme_cleanup_cmd(struct request *req)
  227. {
  228. if (req->rq_flags & RQF_SPECIAL_PAYLOAD) {
  229. kfree(page_address(req->special_vec.bv_page) +
  230. req->special_vec.bv_offset);
  231. }
  232. }
  233. static inline void nvme_end_request(struct request *req, __le16 status,
  234. union nvme_result result)
  235. {
  236. struct nvme_request *rq = nvme_req(req);
  237. rq->status = le16_to_cpu(status) >> 1;
  238. rq->result = result;
  239. blk_mq_complete_request(req);
  240. }
  241. void nvme_complete_rq(struct request *req);
  242. void nvme_cancel_request(struct request *req, void *data, bool reserved);
  243. bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
  244. enum nvme_ctrl_state new_state);
  245. int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap);
  246. int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap);
  247. int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl);
  248. int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
  249. const struct nvme_ctrl_ops *ops, unsigned long quirks);
  250. void nvme_uninit_ctrl(struct nvme_ctrl *ctrl);
  251. void nvme_start_ctrl(struct nvme_ctrl *ctrl);
  252. void nvme_stop_ctrl(struct nvme_ctrl *ctrl);
  253. void nvme_put_ctrl(struct nvme_ctrl *ctrl);
  254. int nvme_init_identify(struct nvme_ctrl *ctrl);
  255. void nvme_queue_scan(struct nvme_ctrl *ctrl);
  256. void nvme_remove_namespaces(struct nvme_ctrl *ctrl);
  257. int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
  258. bool send);
  259. #define NVME_NR_AERS 1
  260. void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
  261. union nvme_result *res);
  262. void nvme_queue_async_events(struct nvme_ctrl *ctrl);
  263. void nvme_stop_queues(struct nvme_ctrl *ctrl);
  264. void nvme_start_queues(struct nvme_ctrl *ctrl);
  265. void nvme_kill_queues(struct nvme_ctrl *ctrl);
  266. void nvme_unfreeze(struct nvme_ctrl *ctrl);
  267. void nvme_wait_freeze(struct nvme_ctrl *ctrl);
  268. void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout);
  269. void nvme_start_freeze(struct nvme_ctrl *ctrl);
  270. #define NVME_QID_ANY -1
  271. struct request *nvme_alloc_request(struct request_queue *q,
  272. struct nvme_command *cmd, unsigned int flags, int qid);
  273. blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
  274. struct nvme_command *cmd);
  275. int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  276. void *buf, unsigned bufflen);
  277. int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  278. union nvme_result *result, void *buffer, unsigned bufflen,
  279. unsigned timeout, int qid, int at_head, int flags);
  280. int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count);
  281. void nvme_start_keep_alive(struct nvme_ctrl *ctrl);
  282. void nvme_stop_keep_alive(struct nvme_ctrl *ctrl);
  283. int nvme_reset_ctrl(struct nvme_ctrl *ctrl);
  284. #ifdef CONFIG_NVM
  285. int nvme_nvm_register(struct nvme_ns *ns, char *disk_name, int node);
  286. void nvme_nvm_unregister(struct nvme_ns *ns);
  287. int nvme_nvm_register_sysfs(struct nvme_ns *ns);
  288. void nvme_nvm_unregister_sysfs(struct nvme_ns *ns);
  289. int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd, unsigned long arg);
  290. #else
  291. static inline int nvme_nvm_register(struct nvme_ns *ns, char *disk_name,
  292. int node)
  293. {
  294. return 0;
  295. }
  296. static inline void nvme_nvm_unregister(struct nvme_ns *ns) {};
  297. static inline int nvme_nvm_register_sysfs(struct nvme_ns *ns)
  298. {
  299. return 0;
  300. }
  301. static inline void nvme_nvm_unregister_sysfs(struct nvme_ns *ns) {};
  302. static inline int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd,
  303. unsigned long arg)
  304. {
  305. return -ENOTTY;
  306. }
  307. #endif /* CONFIG_NVM */
  308. static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev)
  309. {
  310. return dev_to_disk(dev)->private_data;
  311. }
  312. int __init nvme_core_init(void);
  313. void nvme_core_exit(void);
  314. #endif /* _NVME_H */