nvme.h 9.2 KB

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