nvme.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. enum {
  20. /*
  21. * Driver internal status code for commands that were cancelled due
  22. * to timeouts or controller shutdown. The value is negative so
  23. * that it a) doesn't overlap with the unsigned hardware error codes,
  24. * and b) can easily be tested for.
  25. */
  26. NVME_SC_CANCELLED = -EINTR,
  27. };
  28. extern unsigned char nvme_io_timeout;
  29. #define NVME_IO_TIMEOUT (nvme_io_timeout * HZ)
  30. extern unsigned char admin_timeout;
  31. #define ADMIN_TIMEOUT (admin_timeout * HZ)
  32. extern unsigned char shutdown_timeout;
  33. #define SHUTDOWN_TIMEOUT (shutdown_timeout * HZ)
  34. enum {
  35. NVME_NS_LBA = 0,
  36. NVME_NS_LIGHTNVM = 1,
  37. };
  38. /*
  39. * List of workarounds for devices that required behavior not specified in
  40. * the standard.
  41. */
  42. enum nvme_quirks {
  43. /*
  44. * Prefers I/O aligned to a stripe size specified in a vendor
  45. * specific Identify field.
  46. */
  47. NVME_QUIRK_STRIPE_SIZE = (1 << 0),
  48. /*
  49. * The controller doesn't handle Identify value others than 0 or 1
  50. * correctly.
  51. */
  52. NVME_QUIRK_IDENTIFY_CNS = (1 << 1),
  53. /*
  54. * The controller deterministically returns O's on reads to discarded
  55. * logical blocks.
  56. */
  57. NVME_QUIRK_DISCARD_ZEROES = (1 << 2),
  58. };
  59. struct nvme_ctrl {
  60. const struct nvme_ctrl_ops *ops;
  61. struct request_queue *admin_q;
  62. struct device *dev;
  63. struct kref kref;
  64. int instance;
  65. struct blk_mq_tag_set *tagset;
  66. struct list_head namespaces;
  67. struct mutex namespaces_mutex;
  68. struct device *device; /* char device */
  69. struct list_head node;
  70. struct ida ns_ida;
  71. char name[12];
  72. char serial[20];
  73. char model[40];
  74. char firmware_rev[8];
  75. int cntlid;
  76. u32 ctrl_config;
  77. u32 page_size;
  78. u32 max_hw_sectors;
  79. u32 stripe_size;
  80. u16 oncs;
  81. u16 vid;
  82. atomic_t abort_limit;
  83. u8 event_limit;
  84. u8 vwc;
  85. u32 vs;
  86. bool subsystem;
  87. unsigned long quirks;
  88. };
  89. /*
  90. * An NVM Express namespace is equivalent to a SCSI LUN
  91. */
  92. struct nvme_ns {
  93. struct list_head list;
  94. struct nvme_ctrl *ctrl;
  95. struct request_queue *queue;
  96. struct gendisk *disk;
  97. struct kref kref;
  98. int instance;
  99. u8 eui[8];
  100. u8 uuid[16];
  101. unsigned ns_id;
  102. int lba_shift;
  103. u16 ms;
  104. bool ext;
  105. u8 pi_type;
  106. int type;
  107. unsigned long flags;
  108. #define NVME_NS_REMOVING 0
  109. #define NVME_NS_DEAD 1
  110. u64 mode_select_num_blocks;
  111. u32 mode_select_block_len;
  112. };
  113. struct nvme_ctrl_ops {
  114. struct module *module;
  115. int (*reg_read32)(struct nvme_ctrl *ctrl, u32 off, u32 *val);
  116. int (*reg_write32)(struct nvme_ctrl *ctrl, u32 off, u32 val);
  117. int (*reg_read64)(struct nvme_ctrl *ctrl, u32 off, u64 *val);
  118. bool (*io_incapable)(struct nvme_ctrl *ctrl);
  119. int (*reset_ctrl)(struct nvme_ctrl *ctrl);
  120. void (*free_ctrl)(struct nvme_ctrl *ctrl);
  121. };
  122. static inline bool nvme_ctrl_ready(struct nvme_ctrl *ctrl)
  123. {
  124. u32 val = 0;
  125. if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &val))
  126. return false;
  127. return val & NVME_CSTS_RDY;
  128. }
  129. static inline bool nvme_io_incapable(struct nvme_ctrl *ctrl)
  130. {
  131. u32 val = 0;
  132. if (ctrl->ops->io_incapable(ctrl))
  133. return true;
  134. if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &val))
  135. return true;
  136. return val & NVME_CSTS_CFS;
  137. }
  138. static inline int nvme_reset_subsystem(struct nvme_ctrl *ctrl)
  139. {
  140. if (!ctrl->subsystem)
  141. return -ENOTTY;
  142. return ctrl->ops->reg_write32(ctrl, NVME_REG_NSSR, 0x4E564D65);
  143. }
  144. static inline u64 nvme_block_nr(struct nvme_ns *ns, sector_t sector)
  145. {
  146. return (sector >> (ns->lba_shift - 9));
  147. }
  148. static inline unsigned nvme_map_len(struct request *rq)
  149. {
  150. if (rq->cmd_flags & REQ_DISCARD)
  151. return sizeof(struct nvme_dsm_range);
  152. else
  153. return blk_rq_bytes(rq);
  154. }
  155. static inline int nvme_error_status(u16 status)
  156. {
  157. switch (status & 0x7ff) {
  158. case NVME_SC_SUCCESS:
  159. return 0;
  160. case NVME_SC_CAP_EXCEEDED:
  161. return -ENOSPC;
  162. default:
  163. return -EIO;
  164. }
  165. }
  166. static inline bool nvme_req_needs_retry(struct request *req, u16 status)
  167. {
  168. return !(status & NVME_SC_DNR || blk_noretry_request(req)) &&
  169. (jiffies - req->start_time) < req->timeout;
  170. }
  171. int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap);
  172. int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap);
  173. int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl);
  174. int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
  175. const struct nvme_ctrl_ops *ops, unsigned long quirks);
  176. void nvme_uninit_ctrl(struct nvme_ctrl *ctrl);
  177. void nvme_put_ctrl(struct nvme_ctrl *ctrl);
  178. int nvme_init_identify(struct nvme_ctrl *ctrl);
  179. void nvme_scan_namespaces(struct nvme_ctrl *ctrl);
  180. void nvme_remove_namespaces(struct nvme_ctrl *ctrl);
  181. void nvme_stop_queues(struct nvme_ctrl *ctrl);
  182. void nvme_start_queues(struct nvme_ctrl *ctrl);
  183. void nvme_kill_queues(struct nvme_ctrl *ctrl);
  184. struct request *nvme_alloc_request(struct request_queue *q,
  185. struct nvme_command *cmd, unsigned int flags);
  186. void nvme_requeue_req(struct request *req);
  187. int nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
  188. struct nvme_command *cmd);
  189. int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  190. void *buf, unsigned bufflen);
  191. int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  192. struct nvme_completion *cqe, void *buffer, unsigned bufflen,
  193. unsigned timeout);
  194. int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
  195. void __user *ubuffer, unsigned bufflen, u32 *result,
  196. unsigned timeout);
  197. int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
  198. void __user *ubuffer, unsigned bufflen,
  199. void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
  200. u32 *result, unsigned timeout);
  201. int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id);
  202. int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
  203. struct nvme_id_ns **id);
  204. int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log);
  205. int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
  206. dma_addr_t dma_addr, u32 *result);
  207. int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
  208. dma_addr_t dma_addr, u32 *result);
  209. int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count);
  210. struct sg_io_hdr;
  211. int nvme_sg_io(struct nvme_ns *ns, struct sg_io_hdr __user *u_hdr);
  212. int nvme_sg_io32(struct nvme_ns *ns, unsigned long arg);
  213. int nvme_sg_get_version_num(int __user *ip);
  214. #ifdef CONFIG_NVM
  215. int nvme_nvm_ns_supported(struct nvme_ns *ns, struct nvme_id_ns *id);
  216. int nvme_nvm_register(struct request_queue *q, char *disk_name);
  217. void nvme_nvm_unregister(struct request_queue *q, char *disk_name);
  218. #else
  219. static inline int nvme_nvm_register(struct request_queue *q, char *disk_name)
  220. {
  221. return 0;
  222. }
  223. static inline void nvme_nvm_unregister(struct request_queue *q, char *disk_name) {};
  224. static inline int nvme_nvm_ns_supported(struct nvme_ns *ns, struct nvme_id_ns *id)
  225. {
  226. return 0;
  227. }
  228. #endif /* CONFIG_NVM */
  229. int __init nvme_core_init(void);
  230. void nvme_core_exit(void);
  231. #endif /* _NVME_H */