nvme.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. /* The below value is the specific amount of delay needed before checking
  69. * readiness in case of the PCI_DEVICE(0x1c58, 0x0003), which needs the
  70. * NVME_QUIRK_DELAY_BEFORE_CHK_RDY quirk enabled. The value (in ms) was
  71. * found empirically.
  72. */
  73. #define NVME_QUIRK_DELAY_AMOUNT 2000
  74. enum nvme_ctrl_state {
  75. NVME_CTRL_NEW,
  76. NVME_CTRL_LIVE,
  77. NVME_CTRL_RESETTING,
  78. NVME_CTRL_RECONNECTING,
  79. NVME_CTRL_DELETING,
  80. NVME_CTRL_DEAD,
  81. };
  82. struct nvme_ctrl {
  83. enum nvme_ctrl_state state;
  84. spinlock_t lock;
  85. const struct nvme_ctrl_ops *ops;
  86. struct request_queue *admin_q;
  87. struct request_queue *connect_q;
  88. struct device *dev;
  89. struct kref kref;
  90. int instance;
  91. struct blk_mq_tag_set *tagset;
  92. struct list_head namespaces;
  93. struct mutex namespaces_mutex;
  94. struct device *device; /* char device */
  95. struct list_head node;
  96. struct ida ns_ida;
  97. char name[12];
  98. char serial[20];
  99. char model[40];
  100. char firmware_rev[8];
  101. u16 cntlid;
  102. u32 ctrl_config;
  103. u32 page_size;
  104. u32 max_hw_sectors;
  105. u32 stripe_size;
  106. u16 oncs;
  107. u16 vid;
  108. atomic_t abort_limit;
  109. u8 event_limit;
  110. u8 vwc;
  111. u32 vs;
  112. u32 sgls;
  113. u16 kas;
  114. unsigned int kato;
  115. bool subsystem;
  116. unsigned long quirks;
  117. struct work_struct scan_work;
  118. struct work_struct async_event_work;
  119. struct delayed_work ka_work;
  120. /* Fabrics only */
  121. u16 sqsize;
  122. u32 ioccsz;
  123. u32 iorcsz;
  124. u16 icdoff;
  125. u16 maxcmd;
  126. struct nvmf_ctrl_options *opts;
  127. };
  128. /*
  129. * An NVM Express namespace is equivalent to a SCSI LUN
  130. */
  131. struct nvme_ns {
  132. struct list_head list;
  133. struct nvme_ctrl *ctrl;
  134. struct request_queue *queue;
  135. struct gendisk *disk;
  136. struct nvm_dev *ndev;
  137. struct kref kref;
  138. int instance;
  139. u8 eui[8];
  140. u8 uuid[16];
  141. unsigned ns_id;
  142. int lba_shift;
  143. u16 ms;
  144. bool ext;
  145. u8 pi_type;
  146. unsigned long flags;
  147. #define NVME_NS_REMOVING 0
  148. #define NVME_NS_DEAD 1
  149. u64 mode_select_num_blocks;
  150. u32 mode_select_block_len;
  151. };
  152. struct nvme_ctrl_ops {
  153. const char *name;
  154. struct module *module;
  155. bool is_fabrics;
  156. int (*reg_read32)(struct nvme_ctrl *ctrl, u32 off, u32 *val);
  157. int (*reg_write32)(struct nvme_ctrl *ctrl, u32 off, u32 val);
  158. int (*reg_read64)(struct nvme_ctrl *ctrl, u32 off, u64 *val);
  159. int (*reset_ctrl)(struct nvme_ctrl *ctrl);
  160. void (*free_ctrl)(struct nvme_ctrl *ctrl);
  161. void (*post_scan)(struct nvme_ctrl *ctrl);
  162. void (*submit_async_event)(struct nvme_ctrl *ctrl, int aer_idx);
  163. int (*delete_ctrl)(struct nvme_ctrl *ctrl);
  164. const char *(*get_subsysnqn)(struct nvme_ctrl *ctrl);
  165. int (*get_address)(struct nvme_ctrl *ctrl, char *buf, int size);
  166. };
  167. static inline bool nvme_ctrl_ready(struct nvme_ctrl *ctrl)
  168. {
  169. u32 val = 0;
  170. if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &val))
  171. return false;
  172. return val & NVME_CSTS_RDY;
  173. }
  174. static inline int nvme_reset_subsystem(struct nvme_ctrl *ctrl)
  175. {
  176. if (!ctrl->subsystem)
  177. return -ENOTTY;
  178. return ctrl->ops->reg_write32(ctrl, NVME_REG_NSSR, 0x4E564D65);
  179. }
  180. static inline u64 nvme_block_nr(struct nvme_ns *ns, sector_t sector)
  181. {
  182. return (sector >> (ns->lba_shift - 9));
  183. }
  184. static inline unsigned nvme_map_len(struct request *rq)
  185. {
  186. if (req_op(rq) == REQ_OP_DISCARD)
  187. return sizeof(struct nvme_dsm_range);
  188. else
  189. return blk_rq_bytes(rq);
  190. }
  191. static inline void nvme_cleanup_cmd(struct request *req)
  192. {
  193. if (req_op(req) == REQ_OP_DISCARD)
  194. kfree(req->completion_data);
  195. }
  196. static inline int nvme_error_status(u16 status)
  197. {
  198. switch (status & 0x7ff) {
  199. case NVME_SC_SUCCESS:
  200. return 0;
  201. case NVME_SC_CAP_EXCEEDED:
  202. return -ENOSPC;
  203. default:
  204. return -EIO;
  205. }
  206. }
  207. static inline bool nvme_req_needs_retry(struct request *req, u16 status)
  208. {
  209. return !(status & NVME_SC_DNR || blk_noretry_request(req)) &&
  210. (jiffies - req->start_time) < req->timeout &&
  211. req->retries < nvme_max_retries;
  212. }
  213. void nvme_cancel_request(struct request *req, void *data, bool reserved);
  214. bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
  215. enum nvme_ctrl_state new_state);
  216. int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap);
  217. int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap);
  218. int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl);
  219. int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
  220. const struct nvme_ctrl_ops *ops, unsigned long quirks);
  221. void nvme_uninit_ctrl(struct nvme_ctrl *ctrl);
  222. void nvme_put_ctrl(struct nvme_ctrl *ctrl);
  223. int nvme_init_identify(struct nvme_ctrl *ctrl);
  224. void nvme_queue_scan(struct nvme_ctrl *ctrl);
  225. void nvme_remove_namespaces(struct nvme_ctrl *ctrl);
  226. #define NVME_NR_AERS 1
  227. void nvme_complete_async_event(struct nvme_ctrl *ctrl,
  228. struct nvme_completion *cqe);
  229. void nvme_queue_async_events(struct nvme_ctrl *ctrl);
  230. void nvme_stop_queues(struct nvme_ctrl *ctrl);
  231. void nvme_start_queues(struct nvme_ctrl *ctrl);
  232. void nvme_kill_queues(struct nvme_ctrl *ctrl);
  233. #define NVME_QID_ANY -1
  234. struct request *nvme_alloc_request(struct request_queue *q,
  235. struct nvme_command *cmd, unsigned int flags, int qid);
  236. void nvme_requeue_req(struct request *req);
  237. int nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
  238. struct nvme_command *cmd);
  239. int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  240. void *buf, unsigned bufflen);
  241. int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  242. struct nvme_completion *cqe, void *buffer, unsigned bufflen,
  243. unsigned timeout, int qid, int at_head, int flags);
  244. int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
  245. void __user *ubuffer, unsigned bufflen, u32 *result,
  246. unsigned timeout);
  247. int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
  248. void __user *ubuffer, unsigned bufflen,
  249. void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
  250. u32 *result, unsigned timeout);
  251. int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id);
  252. int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
  253. struct nvme_id_ns **id);
  254. int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log);
  255. int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
  256. void *buffer, size_t buflen, u32 *result);
  257. int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
  258. void *buffer, size_t buflen, u32 *result);
  259. int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count);
  260. void nvme_start_keep_alive(struct nvme_ctrl *ctrl);
  261. void nvme_stop_keep_alive(struct nvme_ctrl *ctrl);
  262. struct sg_io_hdr;
  263. int nvme_sg_io(struct nvme_ns *ns, struct sg_io_hdr __user *u_hdr);
  264. int nvme_sg_io32(struct nvme_ns *ns, unsigned long arg);
  265. int nvme_sg_get_version_num(int __user *ip);
  266. #ifdef CONFIG_NVM
  267. int nvme_nvm_ns_supported(struct nvme_ns *ns, struct nvme_id_ns *id);
  268. int nvme_nvm_register(struct nvme_ns *ns, char *disk_name, int node,
  269. const struct attribute_group *attrs);
  270. void nvme_nvm_unregister(struct nvme_ns *ns);
  271. static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev)
  272. {
  273. if (dev->type->devnode)
  274. return dev_to_disk(dev)->private_data;
  275. return (container_of(dev, struct nvm_dev, dev))->private_data;
  276. }
  277. #else
  278. static inline int nvme_nvm_register(struct nvme_ns *ns, char *disk_name,
  279. int node,
  280. const struct attribute_group *attrs)
  281. {
  282. return 0;
  283. }
  284. static inline void nvme_nvm_unregister(struct nvme_ns *ns) {};
  285. static inline int nvme_nvm_ns_supported(struct nvme_ns *ns, struct nvme_id_ns *id)
  286. {
  287. return 0;
  288. }
  289. static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev)
  290. {
  291. return dev_to_disk(dev)->private_data;
  292. }
  293. #endif /* CONFIG_NVM */
  294. int __init nvme_core_init(void);
  295. void nvme_core_exit(void);
  296. #endif /* _NVME_H */