Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Definitions for the NVM Express interface |
Matthew Wilcox | 8757ad6 | 2014-04-11 10:37:39 -0400 | [diff] [blame] | 3 | * Copyright (c) 2011-2014, Intel Corporation. |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef _LINUX_NVME_H |
| 16 | #define _LINUX_NVME_H |
| 17 | |
Matthew Wilcox | 42c7768 | 2013-06-25 15:14:56 -0400 | [diff] [blame] | 18 | #include <uapi/linux/nvme.h> |
| 19 | #include <linux/pci.h> |
| 20 | #include <linux/miscdevice.h> |
| 21 | #include <linux/kref.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 22 | |
| 23 | struct nvme_bar { |
| 24 | __u64 cap; /* Controller Capabilities */ |
| 25 | __u32 vs; /* Version */ |
Matthew Wilcox | 897cfe1 | 2011-02-14 12:20:15 -0500 | [diff] [blame] | 26 | __u32 intms; /* Interrupt Mask Set */ |
| 27 | __u32 intmc; /* Interrupt Mask Clear */ |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 28 | __u32 cc; /* Controller Configuration */ |
Matthew Wilcox | 897cfe1 | 2011-02-14 12:20:15 -0500 | [diff] [blame] | 29 | __u32 rsvd1; /* Reserved */ |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 30 | __u32 csts; /* Controller Status */ |
Matthew Wilcox | 897cfe1 | 2011-02-14 12:20:15 -0500 | [diff] [blame] | 31 | __u32 rsvd2; /* Reserved */ |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 32 | __u32 aqa; /* Admin Queue Attributes */ |
| 33 | __u64 asq; /* Admin SQ Base Address */ |
| 34 | __u64 acq; /* Admin CQ Base Address */ |
| 35 | }; |
| 36 | |
Keith Busch | a0cadb8 | 2012-07-27 13:57:23 -0400 | [diff] [blame] | 37 | #define NVME_CAP_MQES(cap) ((cap) & 0xffff) |
Matthew Wilcox | 22605f9 | 2011-04-19 15:04:20 -0400 | [diff] [blame] | 38 | #define NVME_CAP_TIMEOUT(cap) (((cap) >> 24) & 0xff) |
Matthew Wilcox | f1938f6 | 2011-10-20 17:00:41 -0400 | [diff] [blame] | 39 | #define NVME_CAP_STRIDE(cap) (((cap) >> 32) & 0xf) |
Keith Busch | 8fc23e0 | 2012-07-26 11:29:57 -0600 | [diff] [blame] | 40 | #define NVME_CAP_MPSMIN(cap) (((cap) >> 48) & 0xf) |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame^] | 41 | #define NVME_CAP_MPSMAX(cap) (((cap) >> 52) & 0xf) |
Matthew Wilcox | 22605f9 | 2011-04-19 15:04:20 -0400 | [diff] [blame] | 42 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 43 | enum { |
| 44 | NVME_CC_ENABLE = 1 << 0, |
| 45 | NVME_CC_CSS_NVM = 0 << 4, |
| 46 | NVME_CC_MPS_SHIFT = 7, |
| 47 | NVME_CC_ARB_RR = 0 << 11, |
| 48 | NVME_CC_ARB_WRRU = 1 << 11, |
Matthew Wilcox | 7f53f9d | 2011-03-22 15:55:45 -0400 | [diff] [blame] | 49 | NVME_CC_ARB_VS = 7 << 11, |
| 50 | NVME_CC_SHN_NONE = 0 << 14, |
| 51 | NVME_CC_SHN_NORMAL = 1 << 14, |
| 52 | NVME_CC_SHN_ABRUPT = 2 << 14, |
Keith Busch | 1894d8f | 2013-07-15 15:02:22 -0600 | [diff] [blame] | 53 | NVME_CC_SHN_MASK = 3 << 14, |
Matthew Wilcox | 7f53f9d | 2011-03-22 15:55:45 -0400 | [diff] [blame] | 54 | NVME_CC_IOSQES = 6 << 16, |
| 55 | NVME_CC_IOCQES = 4 << 20, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 56 | NVME_CSTS_RDY = 1 << 0, |
| 57 | NVME_CSTS_CFS = 1 << 1, |
| 58 | NVME_CSTS_SHST_NORMAL = 0 << 2, |
| 59 | NVME_CSTS_SHST_OCCUR = 1 << 2, |
| 60 | NVME_CSTS_SHST_CMPLT = 2 << 2, |
Keith Busch | 1894d8f | 2013-07-15 15:02:22 -0600 | [diff] [blame] | 61 | NVME_CSTS_SHST_MASK = 3 << 2, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | #define NVME_VS(major, minor) (major << 16 | minor) |
| 65 | |
Matthew Wilcox | bd67608 | 2014-06-03 23:04:30 -0400 | [diff] [blame] | 66 | extern unsigned char nvme_io_timeout; |
| 67 | #define NVME_IO_TIMEOUT (nvme_io_timeout * HZ) |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * Represents an NVM Express device. Each nvme_dev is a PCI function. |
| 71 | */ |
| 72 | struct nvme_dev { |
| 73 | struct list_head node; |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 74 | struct nvme_queue __rcu **queues; |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 75 | unsigned short __percpu *io_queue; |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 76 | u32 __iomem *dbs; |
| 77 | struct pci_dev *pci_dev; |
| 78 | struct dma_pool *prp_page_pool; |
| 79 | struct dma_pool *prp_small_pool; |
| 80 | int instance; |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 81 | unsigned queue_count; |
| 82 | unsigned online_queues; |
| 83 | unsigned max_qid; |
| 84 | int q_depth; |
Haiyan Hu | b80d5cc | 2013-09-10 11:25:37 +0800 | [diff] [blame] | 85 | u32 db_stride; |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 86 | u32 ctrl_config; |
| 87 | struct msix_entry *entry; |
| 88 | struct nvme_bar __iomem *bar; |
| 89 | struct list_head namespaces; |
Keith Busch | 5e82e95 | 2013-02-19 10:17:58 -0700 | [diff] [blame] | 90 | struct kref kref; |
| 91 | struct miscdevice miscdev; |
Tejun Heo | 9ca9737 | 2014-03-07 10:24:49 -0500 | [diff] [blame] | 92 | work_func_t reset_workfn; |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 93 | struct work_struct reset_work; |
Keith Busch | f3db22f | 2014-06-11 11:51:35 -0600 | [diff] [blame] | 94 | struct work_struct cpu_work; |
Keith Busch | 5e82e95 | 2013-02-19 10:17:58 -0700 | [diff] [blame] | 95 | char name[12]; |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 96 | char serial[20]; |
| 97 | char model[40]; |
| 98 | char firmware_rev[8]; |
| 99 | u32 max_hw_sectors; |
Keith Busch | 159b67d | 2013-04-09 17:13:20 -0600 | [diff] [blame] | 100 | u32 stripe_size; |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame^] | 101 | u32 page_size; |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 102 | u16 oncs; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 103 | u16 abort_limit; |
Keith Busch | 6fccf93 | 2014-06-18 13:58:57 -0600 | [diff] [blame] | 104 | u8 event_limit; |
Keith Busch | a7d2ce2 | 2014-04-29 11:41:28 -0600 | [diff] [blame] | 105 | u8 vwc; |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 106 | u8 initialized; |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | /* |
| 110 | * An NVM Express namespace is equivalent to a SCSI LUN |
| 111 | */ |
| 112 | struct nvme_ns { |
| 113 | struct list_head list; |
| 114 | |
| 115 | struct nvme_dev *dev; |
| 116 | struct request_queue *queue; |
| 117 | struct gendisk *disk; |
| 118 | |
Matthew Wilcox | c3bfe71 | 2013-07-08 17:26:25 -0400 | [diff] [blame] | 119 | unsigned ns_id; |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 120 | int lba_shift; |
Keith Busch | f410c68 | 2013-04-23 17:23:59 -0600 | [diff] [blame] | 121 | int ms; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 122 | u64 mode_select_num_blocks; |
| 123 | u32 mode_select_block_len; |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | /* |
| 127 | * The nvme_iod describes the data in an I/O, including the list of PRP |
| 128 | * entries. You can't see it in this data structure because C doesn't let |
| 129 | * me express that. Use nvme_alloc_iod to ensure there's enough space |
| 130 | * allocated to store the PRP list. |
| 131 | */ |
| 132 | struct nvme_iod { |
| 133 | void *private; /* For the use of the submitter of the I/O */ |
| 134 | int npages; /* In the PRP list. 0 means small pool in use */ |
| 135 | int offset; /* Of PRP list */ |
| 136 | int nents; /* Used in scatterlist */ |
| 137 | int length; /* Of data, in bytes */ |
Keith Busch | 6198221 | 2013-05-29 15:59:39 -0600 | [diff] [blame] | 138 | unsigned long start_time; |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 139 | dma_addr_t first_dma; |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 140 | struct list_head node; |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 141 | struct scatterlist sg[0]; |
| 142 | }; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 143 | |
Matthew Wilcox | 063cc6d | 2013-03-27 21:28:22 -0400 | [diff] [blame] | 144 | static inline u64 nvme_block_nr(struct nvme_ns *ns, sector_t sector) |
| 145 | { |
| 146 | return (sector >> (ns->lba_shift - 9)); |
| 147 | } |
| 148 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 149 | /** |
| 150 | * nvme_free_iod - frees an nvme_iod |
| 151 | * @dev: The device that the I/O was submitted to |
| 152 | * @iod: The memory to free |
| 153 | */ |
| 154 | void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod); |
| 155 | |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 156 | int nvme_setup_prps(struct nvme_dev *, struct nvme_iod *, int , gfp_t); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 157 | struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write, |
| 158 | unsigned long addr, unsigned length); |
| 159 | void nvme_unmap_user_pages(struct nvme_dev *dev, int write, |
| 160 | struct nvme_iod *iod); |
Keith Busch | 4f5099a | 2014-03-03 16:39:13 -0700 | [diff] [blame] | 161 | int nvme_submit_io_cmd(struct nvme_dev *, struct nvme_command *, u32 *); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 162 | int nvme_submit_admin_cmd(struct nvme_dev *, struct nvme_command *, |
| 163 | u32 *result); |
| 164 | int nvme_identify(struct nvme_dev *, unsigned nsid, unsigned cns, |
| 165 | dma_addr_t dma_addr); |
| 166 | int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid, |
| 167 | dma_addr_t dma_addr, u32 *result); |
| 168 | int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11, |
| 169 | dma_addr_t dma_addr, u32 *result); |
| 170 | |
| 171 | struct sg_io_hdr; |
| 172 | |
| 173 | int nvme_sg_io(struct nvme_ns *ns, struct sg_io_hdr __user *u_hdr); |
Keith Busch | 320a382 | 2013-10-23 13:07:34 -0600 | [diff] [blame] | 174 | int nvme_sg_io32(struct nvme_ns *ns, unsigned long arg); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 175 | int nvme_sg_get_version_num(int __user *ip); |
| 176 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 177 | #endif /* _LINUX_NVME_H */ |