nvme.h 10.0 KB

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