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> |
Matthew Wilcox | 42c7768 | 2013-06-25 15:14:56 -0400 | [diff] [blame] | 20 | #include <linux/kref.h> |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 21 | #include <linux/blk-mq.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 */ |
Jon Derrick | 81f03fe | 2015-08-10 15:20:41 -0600 | [diff] [blame] | 31 | __u32 nssr; /* Subsystem Reset */ |
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 */ |
Jon Derrick | 8ffaadf | 2015-07-20 10:14:09 -0600 | [diff] [blame] | 35 | __u32 cmbloc; /* Controller Memory Buffer Location */ |
| 36 | __u32 cmbsz; /* Controller Memory Buffer Size */ |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 37 | }; |
| 38 | |
Keith Busch | a0cadb8 | 2012-07-27 13:57:23 -0400 | [diff] [blame] | 39 | #define NVME_CAP_MQES(cap) ((cap) & 0xffff) |
Matthew Wilcox | 22605f9 | 2011-04-19 15:04:20 -0400 | [diff] [blame] | 40 | #define NVME_CAP_TIMEOUT(cap) (((cap) >> 24) & 0xff) |
Matthew Wilcox | f1938f6 | 2011-10-20 17:00:41 -0400 | [diff] [blame] | 41 | #define NVME_CAP_STRIDE(cap) (((cap) >> 32) & 0xf) |
Keith Busch | dfbac8c | 2015-08-10 15:20:40 -0600 | [diff] [blame] | 42 | #define NVME_CAP_NSSRC(cap) (((cap) >> 36) & 0x1) |
Keith Busch | 8fc23e0 | 2012-07-26 11:29:57 -0600 | [diff] [blame] | 43 | #define NVME_CAP_MPSMIN(cap) (((cap) >> 48) & 0xf) |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 44 | #define NVME_CAP_MPSMAX(cap) (((cap) >> 52) & 0xf) |
Matthew Wilcox | 22605f9 | 2011-04-19 15:04:20 -0400 | [diff] [blame] | 45 | |
Jon Derrick | 8ffaadf | 2015-07-20 10:14:09 -0600 | [diff] [blame] | 46 | #define NVME_CMB_BIR(cmbloc) ((cmbloc) & 0x7) |
| 47 | #define NVME_CMB_OFST(cmbloc) (((cmbloc) >> 12) & 0xfffff) |
| 48 | #define NVME_CMB_SZ(cmbsz) (((cmbsz) >> 12) & 0xfffff) |
| 49 | #define NVME_CMB_SZU(cmbsz) (((cmbsz) >> 8) & 0xf) |
| 50 | |
| 51 | #define NVME_CMB_WDS(cmbsz) ((cmbsz) & 0x10) |
| 52 | #define NVME_CMB_RDS(cmbsz) ((cmbsz) & 0x8) |
| 53 | #define NVME_CMB_LISTS(cmbsz) ((cmbsz) & 0x4) |
| 54 | #define NVME_CMB_CQS(cmbsz) ((cmbsz) & 0x2) |
| 55 | #define NVME_CMB_SQS(cmbsz) ((cmbsz) & 0x1) |
| 56 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 57 | enum { |
| 58 | NVME_CC_ENABLE = 1 << 0, |
| 59 | NVME_CC_CSS_NVM = 0 << 4, |
| 60 | NVME_CC_MPS_SHIFT = 7, |
| 61 | NVME_CC_ARB_RR = 0 << 11, |
| 62 | NVME_CC_ARB_WRRU = 1 << 11, |
Matthew Wilcox | 7f53f9d | 2011-03-22 15:55:45 -0400 | [diff] [blame] | 63 | NVME_CC_ARB_VS = 7 << 11, |
| 64 | NVME_CC_SHN_NONE = 0 << 14, |
| 65 | NVME_CC_SHN_NORMAL = 1 << 14, |
| 66 | NVME_CC_SHN_ABRUPT = 2 << 14, |
Keith Busch | 1894d8f | 2013-07-15 15:02:22 -0600 | [diff] [blame] | 67 | NVME_CC_SHN_MASK = 3 << 14, |
Matthew Wilcox | 7f53f9d | 2011-03-22 15:55:45 -0400 | [diff] [blame] | 68 | NVME_CC_IOSQES = 6 << 16, |
| 69 | NVME_CC_IOCQES = 4 << 20, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 70 | NVME_CSTS_RDY = 1 << 0, |
| 71 | NVME_CSTS_CFS = 1 << 1, |
Keith Busch | dfbac8c | 2015-08-10 15:20:40 -0600 | [diff] [blame] | 72 | NVME_CSTS_NSSRO = 1 << 4, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 73 | NVME_CSTS_SHST_NORMAL = 0 << 2, |
| 74 | NVME_CSTS_SHST_OCCUR = 1 << 2, |
| 75 | NVME_CSTS_SHST_CMPLT = 2 << 2, |
Keith Busch | 1894d8f | 2013-07-15 15:02:22 -0600 | [diff] [blame] | 76 | NVME_CSTS_SHST_MASK = 3 << 2, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 77 | }; |
| 78 | |
Matthew Wilcox | bd67608 | 2014-06-03 23:04:30 -0400 | [diff] [blame] | 79 | extern unsigned char nvme_io_timeout; |
| 80 | #define NVME_IO_TIMEOUT (nvme_io_timeout * HZ) |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * Represents an NVM Express device. Each nvme_dev is a PCI function. |
| 84 | */ |
| 85 | struct nvme_dev { |
| 86 | struct list_head node; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 87 | struct nvme_queue **queues; |
| 88 | struct request_queue *admin_q; |
| 89 | struct blk_mq_tag_set tagset; |
| 90 | struct blk_mq_tag_set admin_tagset; |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 91 | u32 __iomem *dbs; |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 92 | struct device *dev; |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 93 | struct dma_pool *prp_page_pool; |
| 94 | struct dma_pool *prp_small_pool; |
| 95 | int instance; |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 96 | unsigned queue_count; |
| 97 | unsigned online_queues; |
| 98 | unsigned max_qid; |
| 99 | int q_depth; |
Haiyan Hu | b80d5cc | 2013-09-10 11:25:37 +0800 | [diff] [blame] | 100 | u32 db_stride; |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 101 | u32 ctrl_config; |
| 102 | struct msix_entry *entry; |
| 103 | struct nvme_bar __iomem *bar; |
| 104 | struct list_head namespaces; |
Keith Busch | 5e82e95 | 2013-02-19 10:17:58 -0700 | [diff] [blame] | 105 | struct kref kref; |
Keith Busch | b3fffde | 2015-02-03 11:21:42 -0700 | [diff] [blame] | 106 | struct device *device; |
Tejun Heo | 9ca9737 | 2014-03-07 10:24:49 -0500 | [diff] [blame] | 107 | work_func_t reset_workfn; |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 108 | struct work_struct reset_work; |
Keith Busch | 2e1d844 | 2015-02-12 15:33:00 -0700 | [diff] [blame] | 109 | struct work_struct probe_work; |
Keith Busch | a5768aa | 2015-06-01 14:28:14 -0600 | [diff] [blame] | 110 | struct work_struct scan_work; |
Keith Busch | 5e82e95 | 2013-02-19 10:17:58 -0700 | [diff] [blame] | 111 | char name[12]; |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 112 | char serial[20]; |
| 113 | char model[40]; |
| 114 | char firmware_rev[8]; |
Keith Busch | dfbac8c | 2015-08-10 15:20:40 -0600 | [diff] [blame] | 115 | bool subsystem; |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 116 | u32 max_hw_sectors; |
Keith Busch | 159b67d | 2013-04-09 17:13:20 -0600 | [diff] [blame] | 117 | u32 stripe_size; |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 118 | u32 page_size; |
Jon Derrick | 8ffaadf | 2015-07-20 10:14:09 -0600 | [diff] [blame] | 119 | void __iomem *cmb; |
| 120 | dma_addr_t cmb_dma_addr; |
| 121 | u64 cmb_size; |
| 122 | u32 cmbsz; |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 123 | u16 oncs; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 124 | u16 abort_limit; |
Keith Busch | 6fccf93 | 2014-06-18 13:58:57 -0600 | [diff] [blame] | 125 | u8 event_limit; |
Keith Busch | a7d2ce2 | 2014-04-29 11:41:28 -0600 | [diff] [blame] | 126 | u8 vwc; |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | /* |
| 130 | * An NVM Express namespace is equivalent to a SCSI LUN |
| 131 | */ |
| 132 | struct nvme_ns { |
| 133 | struct list_head list; |
| 134 | |
| 135 | struct nvme_dev *dev; |
| 136 | struct request_queue *queue; |
| 137 | struct gendisk *disk; |
| 138 | |
Matthew Wilcox | c3bfe71 | 2013-07-08 17:26:25 -0400 | [diff] [blame] | 139 | unsigned ns_id; |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 140 | int lba_shift; |
Keith Busch | a67a951 | 2015-04-07 16:57:19 -0600 | [diff] [blame] | 141 | u16 ms; |
| 142 | bool ext; |
| 143 | u8 pi_type; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 144 | u64 mode_select_num_blocks; |
| 145 | u32 mode_select_block_len; |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | /* |
| 149 | * The nvme_iod describes the data in an I/O, including the list of PRP |
| 150 | * entries. You can't see it in this data structure because C doesn't let |
| 151 | * me express that. Use nvme_alloc_iod to ensure there's enough space |
| 152 | * allocated to store the PRP list. |
| 153 | */ |
| 154 | struct nvme_iod { |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 155 | unsigned long private; /* For the use of the submitter of the I/O */ |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 156 | int npages; /* In the PRP list. 0 means small pool in use */ |
| 157 | int offset; /* Of PRP list */ |
| 158 | int nents; /* Used in scatterlist */ |
| 159 | int length; /* Of data, in bytes */ |
| 160 | dma_addr_t first_dma; |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 161 | struct scatterlist meta_sg[1]; /* metadata requires single contiguous buffer */ |
Vishal Verma | 13c3b0f | 2013-03-04 18:40:57 -0700 | [diff] [blame] | 162 | struct scatterlist sg[0]; |
| 163 | }; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 164 | |
Matthew Wilcox | 063cc6d | 2013-03-27 21:28:22 -0400 | [diff] [blame] | 165 | static inline u64 nvme_block_nr(struct nvme_ns *ns, sector_t sector) |
| 166 | { |
| 167 | return (sector >> (ns->lba_shift - 9)); |
| 168 | } |
| 169 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 170 | int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, |
| 171 | void *buf, unsigned bufflen); |
| 172 | int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, |
| 173 | void *buffer, void __user *ubuffer, unsigned bufflen, |
| 174 | u32 *result, unsigned timeout); |
| 175 | int nvme_identify_ctrl(struct nvme_dev *dev, struct nvme_id_ctrl **id); |
| 176 | int nvme_identify_ns(struct nvme_dev *dev, unsigned nsid, |
| 177 | struct nvme_id_ns **id); |
| 178 | int nvme_get_log_page(struct nvme_dev *dev, struct nvme_smart_log **log); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 179 | int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid, |
| 180 | dma_addr_t dma_addr, u32 *result); |
| 181 | int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11, |
| 182 | dma_addr_t dma_addr, u32 *result); |
| 183 | |
| 184 | struct sg_io_hdr; |
| 185 | |
| 186 | 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] | 187 | int nvme_sg_io32(struct nvme_ns *ns, unsigned long arg); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 188 | int nvme_sg_get_version_num(int __user *ip); |
| 189 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 190 | #endif /* _LINUX_NVME_H */ |