nvme.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. enum {
  21. /*
  22. * Driver internal status code for commands that were cancelled due
  23. * to timeouts or controller shutdown. The value is negative so
  24. * that it a) doesn't overlap with the unsigned hardware error codes,
  25. * and b) can easily be tested for.
  26. */
  27. NVME_SC_CANCELLED = -EINTR,
  28. };
  29. extern unsigned char nvme_io_timeout;
  30. #define NVME_IO_TIMEOUT (nvme_io_timeout * HZ)
  31. extern unsigned char admin_timeout;
  32. #define ADMIN_TIMEOUT (admin_timeout * HZ)
  33. extern unsigned char shutdown_timeout;
  34. #define SHUTDOWN_TIMEOUT (shutdown_timeout * HZ)
  35. #define NVME_DEFAULT_KATO 5
  36. #define NVME_KATO_GRACE 10
  37. extern unsigned int nvme_max_retries;
  38. enum {
  39. NVME_NS_LBA = 0,
  40. NVME_NS_LIGHTNVM = 1,
  41. };
  42. /*
  43. * List of workarounds for devices that required behavior not specified in
  44. * the standard.
  45. */
  46. enum nvme_quirks {
  47. /*
  48. * Prefers I/O aligned to a stripe size specified in a vendor
  49. * specific Identify field.
  50. */
  51. NVME_QUIRK_STRIPE_SIZE = (1 << 0),
  52. /*
  53. * The controller doesn't handle Identify value others than 0 or 1
  54. * correctly.
  55. */
  56. NVME_QUIRK_IDENTIFY_CNS = (1 << 1),
  57. /*
  58. * The controller deterministically returns O's on reads to discarded
  59. * logical blocks.
  60. */
  61. NVME_QUIRK_DISCARD_ZEROES = (1 << 2),
  62. /*
  63. * The controller needs a delay before starts checking the device
  64. * readiness, which is done by reading the NVME_CSTS_RDY bit.
  65. */
  66. NVME_QUIRK_DELAY_BEFORE_CHK_RDY = (1 << 3),
  67. };
  68. /*
  69. * Common request structure for NVMe passthrough. All drivers must have
  70. * this structure as the first member of their request-private data.
  71. */
  72. struct nvme_request {
  73. struct nvme_command *cmd;
  74. union nvme_result result;
  75. };
  76. static inline struct nvme_request *nvme_req(struct request *req)
  77. {
  78. return blk_mq_rq_to_pdu(req);
  79. }
  80. /* The below value is the specific amount of delay needed before checking
  81. * readiness in case of the PCI_DEVICE(0x1c58, 0x0003), which needs the
  82. * NVME_QUIRK_DELAY_BEFORE_CHK_RDY quirk enabled. The value (in ms) was
  83. * found empirically.
  84. */
  85. #define NVME_QUIRK_DELAY_AMOUNT 2000
  86. enum nvme_ctrl_state {
  87. NVME_CTRL_NEW,
  88. NVME_CTRL_LIVE,
  89. NVME_CTRL_RESETTING,
  90. NVME_CTRL_RECONNECTING,
  91. NVME_CTRL_DELETING,
  92. NVME_CTRL_DEAD,
  93. };
  94. struct nvme_ctrl {
  95. enum nvme_ctrl_state state;
  96. spinlock_t lock;
  97. const struct nvme_ctrl_ops *ops;
  98. struct request_queue *admin_q;
  99. struct request_queue *connect_q;
  100. struct device *dev;
  101. struct kref kref;
  102. int instance;
  103. struct blk_mq_tag_set *tagset;
  104. struct list_head namespaces;
  105. struct mutex namespaces_mutex;
  106. struct device *device; /* char device */
  107. struct list_head node;
  108. struct ida ns_ida;
  109. char name[12];
  110. char serial[20];
  111. char model[40];
  112. char firmware_rev[8];
  113. u16 cntlid;
  114. u32 ctrl_config;
  115. u32 page_size;
  116. u32 max_hw_sectors;
  117. u16 oncs;
  118. u16 vid;
  119. atomic_t abort_limit;
  120. u8 event_limit;
  121. u8 vwc;
  122. u32 vs;
  123. u32 sgls;
  124. u16 kas;
  125. unsigned int kato;
  126. bool subsystem;
  127. unsigned long quirks;
  128. struct work_struct scan_work;
  129. struct work_struct async_event_work;
  130. struct delayed_work ka_work;
  131. /* Fabrics only */
  132. u16 sqsize;
  133. u32 ioccsz;
  134. u32 iorcsz;
  135. u16 icdoff;
  136. u16 maxcmd;
  137. struct nvmf_ctrl_options *opts;
  138. };
  139. /*
  140. * An NVM Express namespace is equivalent to a SCSI LUN
  141. */
  142. struct nvme_ns {
  143. struct list_head list;
  144. struct nvme_ctrl *ctrl;
  145. struct request_queue *queue;
  146. struct gendisk *disk;
  147. struct nvm_dev *ndev;
  148. struct kref kref;
  149. int instance;
  150. u8 eui[8];
  151. u8 uuid[16];
  152. unsigned ns_id;
  153. int lba_shift;
  154. u16 ms;
  155. bool ext;
  156. u8 pi_type;
  157. unsigned long flags;
  158. #define NVME_NS_REMOVING 0
  159. #define NVME_NS_DEAD 1
  160. u64 mode_select_num_blocks;
  161. u32 mode_select_block_len;
  162. };
  163. struct nvme_ctrl_ops {
  164. const char *name;
  165. struct module *module;
  166. bool is_fabrics;
  167. int (*reg_read32)(struct nvme_ctrl *ctrl, u32 off, u32 *val);
  168. int (*reg_write32)(struct nvme_ctrl *ctrl, u32 off, u32 val);
  169. int (*reg_read64)(struct nvme_ctrl *ctrl, u32 off, u64 *val);
  170. int (*reset_ctrl)(struct nvme_ctrl *ctrl);
  171. void (*free_ctrl)(struct nvme_ctrl *ctrl);
  172. void (*submit_async_event)(struct nvme_ctrl *ctrl, int aer_idx);
  173. int (*delete_ctrl)(struct nvme_ctrl *ctrl);
  174. const char *(*get_subsysnqn)(struct nvme_ctrl *ctrl);
  175. int (*get_address)(struct nvme_ctrl *ctrl, char *buf, int size);
  176. };
  177. static inline bool nvme_ctrl_ready(struct nvme_ctrl *ctrl)
  178. {
  179. u32 val = 0;
  180. if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &val))
  181. return false;
  182. return val & NVME_CSTS_RDY;
  183. }
  184. static inline int nvme_reset_subsystem(struct nvme_ctrl *ctrl)
  185. {
  186. if (!ctrl->subsystem)
  187. return -ENOTTY;
  188. return ctrl->ops->reg_write32(ctrl, NVME_REG_NSSR, 0x4E564D65);
  189. }
  190. static inline u64 nvme_block_nr(struct nvme_ns *ns, sector_t sector)
  191. {
  192. return (sector >> (ns->lba_shift - 9));
  193. }
  194. static inline void nvme_cleanup_cmd(struct request *req)
  195. {
  196. if (req->rq_flags & RQF_SPECIAL_PAYLOAD) {
  197. kfree(page_address(req->special_vec.bv_page) +
  198. req->special_vec.bv_offset);
  199. }
  200. }
  201. static inline int nvme_error_status(u16 status)
  202. {
  203. switch (status & 0x7ff) {
  204. case NVME_SC_SUCCESS:
  205. return 0;
  206. case NVME_SC_CAP_EXCEEDED:
  207. return -ENOSPC;
  208. default:
  209. return -EIO;
  210. }
  211. }
  212. static inline bool nvme_req_needs_retry(struct request *req, u16 status)
  213. {
  214. return !(status & NVME_SC_DNR || blk_noretry_request(req)) &&
  215. (jiffies - req->start_time) < req->timeout &&
  216. req->retries < nvme_max_retries;
  217. }
  218. void nvme_cancel_request(struct request *req, void *data, bool reserved);
  219. bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
  220. enum nvme_ctrl_state new_state);
  221. int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap);
  222. int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap);
  223. int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl);
  224. int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
  225. const struct nvme_ctrl_ops *ops, unsigned long quirks);
  226. void nvme_uninit_ctrl(struct nvme_ctrl *ctrl);
  227. void nvme_put_ctrl(struct nvme_ctrl *ctrl);
  228. int nvme_init_identify(struct nvme_ctrl *ctrl);
  229. void nvme_queue_scan(struct nvme_ctrl *ctrl);
  230. void nvme_remove_namespaces(struct nvme_ctrl *ctrl);
  231. #define NVME_NR_AERS 1
  232. void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
  233. union nvme_result *res);
  234. void nvme_queue_async_events(struct nvme_ctrl *ctrl);
  235. void nvme_stop_queues(struct nvme_ctrl *ctrl);
  236. void nvme_start_queues(struct nvme_ctrl *ctrl);
  237. void nvme_kill_queues(struct nvme_ctrl *ctrl);
  238. #define NVME_QID_ANY -1
  239. struct request *nvme_alloc_request(struct request_queue *q,
  240. struct nvme_command *cmd, unsigned int flags, int qid);
  241. void nvme_requeue_req(struct request *req);
  242. int nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
  243. struct nvme_command *cmd);
  244. int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  245. void *buf, unsigned bufflen);
  246. int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  247. union nvme_result *result, void *buffer, unsigned bufflen,
  248. unsigned timeout, int qid, int at_head, int flags);
  249. int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
  250. void __user *ubuffer, unsigned bufflen, u32 *result,
  251. unsigned timeout);
  252. int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
  253. void __user *ubuffer, unsigned bufflen,
  254. void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
  255. u32 *result, unsigned timeout);
  256. int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id);
  257. int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
  258. struct nvme_id_ns **id);
  259. int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log);
  260. int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
  261. void *buffer, size_t buflen, u32 *result);
  262. int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
  263. void *buffer, size_t buflen, u32 *result);
  264. int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count);
  265. void nvme_start_keep_alive(struct nvme_ctrl *ctrl);
  266. void nvme_stop_keep_alive(struct nvme_ctrl *ctrl);
  267. struct sg_io_hdr;
  268. int nvme_sg_io(struct nvme_ns *ns, struct sg_io_hdr __user *u_hdr);
  269. int nvme_sg_io32(struct nvme_ns *ns, unsigned long arg);
  270. int nvme_sg_get_version_num(int __user *ip);
  271. #ifdef CONFIG_NVM
  272. int nvme_nvm_ns_supported(struct nvme_ns *ns, struct nvme_id_ns *id);
  273. int nvme_nvm_register(struct nvme_ns *ns, char *disk_name, int node);
  274. void nvme_nvm_unregister(struct nvme_ns *ns);
  275. int nvme_nvm_register_sysfs(struct nvme_ns *ns);
  276. void nvme_nvm_unregister_sysfs(struct nvme_ns *ns);
  277. #else
  278. static inline int nvme_nvm_register(struct nvme_ns *ns, char *disk_name,
  279. int node)
  280. {
  281. return 0;
  282. }
  283. static inline void nvme_nvm_unregister(struct nvme_ns *ns) {};
  284. static inline int nvme_nvm_register_sysfs(struct nvme_ns *ns)
  285. {
  286. return 0;
  287. }
  288. static inline void nvme_nvm_unregister_sysfs(struct nvme_ns *ns) {};
  289. static inline int nvme_nvm_ns_supported(struct nvme_ns *ns, struct nvme_id_ns *id)
  290. {
  291. return 0;
  292. }
  293. #endif /* CONFIG_NVM */
  294. static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev)
  295. {
  296. return dev_to_disk(dev)->private_data;
  297. }
  298. int __init nvme_core_init(void);
  299. void nvme_core_exit(void);
  300. #endif /* _NVME_H */