Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1 | /* |
| 2 | * NVM Express device driver |
| 3 | * Copyright (c) 2011, Intel Corporation. |
| 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. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/nvme.h> |
| 20 | #include <linux/bio.h> |
Matthew Wilcox | 8de0553 | 2011-05-12 13:50:28 -0400 | [diff] [blame] | 21 | #include <linux/bitops.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 22 | #include <linux/blkdev.h> |
Matthew Wilcox | fd63e9ce | 2011-05-06 08:37:54 -0400 | [diff] [blame] | 23 | #include <linux/delay.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 24 | #include <linux/errno.h> |
| 25 | #include <linux/fs.h> |
| 26 | #include <linux/genhd.h> |
Matthew Wilcox | 5aff938 | 2011-05-06 08:45:47 -0400 | [diff] [blame] | 27 | #include <linux/idr.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 28 | #include <linux/init.h> |
| 29 | #include <linux/interrupt.h> |
| 30 | #include <linux/io.h> |
| 31 | #include <linux/kdev_t.h> |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 32 | #include <linux/kthread.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 33 | #include <linux/kernel.h> |
| 34 | #include <linux/mm.h> |
| 35 | #include <linux/module.h> |
| 36 | #include <linux/moduleparam.h> |
| 37 | #include <linux/pci.h> |
Matthew Wilcox | be7b627 | 2011-02-06 07:53:23 -0500 | [diff] [blame] | 38 | #include <linux/poison.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 39 | #include <linux/sched.h> |
| 40 | #include <linux/slab.h> |
| 41 | #include <linux/types.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 42 | |
Hitoshi Mitake | 797a796 | 2012-02-07 11:45:33 +0900 | [diff] [blame] | 43 | #include <asm-generic/io-64-nonatomic-lo-hi.h> |
| 44 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 45 | #define NVME_Q_DEPTH 1024 |
| 46 | #define SQ_SIZE(depth) (depth * sizeof(struct nvme_command)) |
| 47 | #define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion)) |
| 48 | #define NVME_MINORS 64 |
Matthew Wilcox | ff976d7 | 2011-12-20 13:53:01 -0500 | [diff] [blame] | 49 | #define NVME_IO_TIMEOUT (5 * HZ) |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 50 | #define ADMIN_TIMEOUT (60 * HZ) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 51 | |
| 52 | static int nvme_major; |
| 53 | module_param(nvme_major, int, 0); |
| 54 | |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 55 | static int use_threaded_interrupts; |
| 56 | module_param(use_threaded_interrupts, int, 0); |
| 57 | |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 58 | static DEFINE_SPINLOCK(dev_list_lock); |
| 59 | static LIST_HEAD(dev_list); |
| 60 | static struct task_struct *nvme_thread; |
| 61 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 62 | /* |
| 63 | * Represents an NVM Express device. Each nvme_dev is a PCI function. |
| 64 | */ |
| 65 | struct nvme_dev { |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 66 | struct list_head node; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 67 | struct nvme_queue **queues; |
| 68 | u32 __iomem *dbs; |
| 69 | struct pci_dev *pci_dev; |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 70 | struct dma_pool *prp_page_pool; |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 71 | struct dma_pool *prp_small_pool; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 72 | int instance; |
| 73 | int queue_count; |
Matthew Wilcox | f1938f6 | 2011-10-20 17:00:41 -0400 | [diff] [blame] | 74 | int db_stride; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 75 | u32 ctrl_config; |
| 76 | struct msix_entry *entry; |
| 77 | struct nvme_bar __iomem *bar; |
| 78 | struct list_head namespaces; |
Matthew Wilcox | 5181423 | 2011-02-01 16:18:08 -0500 | [diff] [blame] | 79 | char serial[20]; |
| 80 | char model[40]; |
| 81 | char firmware_rev[8]; |
Keith Busch | 8fc23e0 | 2012-07-26 11:29:57 -0600 | [diff] [blame] | 82 | u32 max_hw_sectors; |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame^] | 83 | u16 oncs; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /* |
| 87 | * An NVM Express namespace is equivalent to a SCSI LUN |
| 88 | */ |
| 89 | struct nvme_ns { |
| 90 | struct list_head list; |
| 91 | |
| 92 | struct nvme_dev *dev; |
| 93 | struct request_queue *queue; |
| 94 | struct gendisk *disk; |
| 95 | |
| 96 | int ns_id; |
| 97 | int lba_shift; |
| 98 | }; |
| 99 | |
| 100 | /* |
| 101 | * An NVM Express queue. Each device has at least two (one for admin |
| 102 | * commands and one for I/O commands). |
| 103 | */ |
| 104 | struct nvme_queue { |
| 105 | struct device *q_dmadev; |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 106 | struct nvme_dev *dev; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 107 | spinlock_t q_lock; |
| 108 | struct nvme_command *sq_cmds; |
| 109 | volatile struct nvme_completion *cqes; |
| 110 | dma_addr_t sq_dma_addr; |
| 111 | dma_addr_t cq_dma_addr; |
| 112 | wait_queue_head_t sq_full; |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 113 | wait_queue_t sq_cong_wait; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 114 | struct bio_list sq_cong; |
| 115 | u32 __iomem *q_db; |
| 116 | u16 q_depth; |
| 117 | u16 cq_vector; |
| 118 | u16 sq_head; |
| 119 | u16 sq_tail; |
| 120 | u16 cq_head; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 121 | u16 cq_phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 122 | unsigned long cmdid_data[]; |
| 123 | }; |
| 124 | |
| 125 | /* |
| 126 | * Check we didin't inadvertently grow the command struct |
| 127 | */ |
| 128 | static inline void _nvme_check_size(void) |
| 129 | { |
| 130 | BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64); |
| 131 | BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64); |
| 132 | BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64); |
| 133 | BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64); |
| 134 | BUILD_BUG_ON(sizeof(struct nvme_features) != 64); |
| 135 | BUILD_BUG_ON(sizeof(struct nvme_command) != 64); |
| 136 | BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096); |
| 137 | BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096); |
| 138 | BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64); |
Keith Busch | 6ecec74 | 2012-09-26 12:49:27 -0600 | [diff] [blame] | 139 | BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 140 | } |
| 141 | |
Matthew Wilcox | 5c1281a | 2011-12-20 11:54:53 -0500 | [diff] [blame] | 142 | typedef void (*nvme_completion_fn)(struct nvme_dev *, void *, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 143 | struct nvme_completion *); |
| 144 | |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 145 | struct nvme_cmd_info { |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 146 | nvme_completion_fn fn; |
| 147 | void *ctx; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 148 | unsigned long timeout; |
| 149 | }; |
| 150 | |
| 151 | static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq) |
| 152 | { |
| 153 | return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)]; |
| 154 | } |
| 155 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 156 | /** |
Matthew Wilcox | 714a7a2 | 2011-03-16 16:28:24 -0400 | [diff] [blame] | 157 | * alloc_cmdid() - Allocate a Command ID |
| 158 | * @nvmeq: The queue that will be used for this command |
| 159 | * @ctx: A pointer that will be passed to the handler |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 160 | * @handler: The function to call on completion |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 161 | * |
| 162 | * Allocate a Command ID for a queue. The data passed in will |
| 163 | * be passed to the completion handler. This is implemented by using |
| 164 | * the bottom two bits of the ctx pointer to store the handler ID. |
| 165 | * Passing in a pointer that's not 4-byte aligned will cause a BUG. |
| 166 | * We can change this if it becomes a problem. |
Matthew Wilcox | 184d294 | 2011-05-11 21:36:38 -0400 | [diff] [blame] | 167 | * |
| 168 | * May be called with local interrupts disabled and the q_lock held, |
| 169 | * or with interrupts enabled and no locks held. |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 170 | */ |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 171 | static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx, |
| 172 | nvme_completion_fn handler, unsigned timeout) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 173 | { |
Matthew Wilcox | e6d15f7 | 2011-02-24 08:49:41 -0500 | [diff] [blame] | 174 | int depth = nvmeq->q_depth - 1; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 175 | struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 176 | int cmdid; |
| 177 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 178 | do { |
| 179 | cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth); |
| 180 | if (cmdid >= depth) |
| 181 | return -EBUSY; |
| 182 | } while (test_and_set_bit(cmdid, nvmeq->cmdid_data)); |
| 183 | |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 184 | info[cmdid].fn = handler; |
| 185 | info[cmdid].ctx = ctx; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 186 | info[cmdid].timeout = jiffies + timeout; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 187 | return cmdid; |
| 188 | } |
| 189 | |
| 190 | static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 191 | nvme_completion_fn handler, unsigned timeout) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 192 | { |
| 193 | int cmdid; |
| 194 | wait_event_killable(nvmeq->sq_full, |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 195 | (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 196 | return (cmdid < 0) ? -EINTR : cmdid; |
| 197 | } |
| 198 | |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 199 | /* Special values must be less than 0x1000 */ |
| 200 | #define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA) |
Matthew Wilcox | d2d8703 | 2011-02-07 15:55:59 -0500 | [diff] [blame] | 201 | #define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE) |
| 202 | #define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE) |
| 203 | #define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE) |
Matthew Wilcox | 00df5cb | 2011-02-22 14:18:30 -0500 | [diff] [blame] | 204 | #define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE) |
Matthew Wilcox | be7b627 | 2011-02-06 07:53:23 -0500 | [diff] [blame] | 205 | |
Matthew Wilcox | 5c1281a | 2011-12-20 11:54:53 -0500 | [diff] [blame] | 206 | static void special_completion(struct nvme_dev *dev, void *ctx, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 207 | struct nvme_completion *cqe) |
| 208 | { |
| 209 | if (ctx == CMD_CTX_CANCELLED) |
| 210 | return; |
| 211 | if (ctx == CMD_CTX_FLUSH) |
| 212 | return; |
| 213 | if (ctx == CMD_CTX_COMPLETED) { |
Matthew Wilcox | 5c1281a | 2011-12-20 11:54:53 -0500 | [diff] [blame] | 214 | dev_warn(&dev->pci_dev->dev, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 215 | "completed id %d twice on queue %d\n", |
| 216 | cqe->command_id, le16_to_cpup(&cqe->sq_id)); |
| 217 | return; |
| 218 | } |
| 219 | if (ctx == CMD_CTX_INVALID) { |
Matthew Wilcox | 5c1281a | 2011-12-20 11:54:53 -0500 | [diff] [blame] | 220 | dev_warn(&dev->pci_dev->dev, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 221 | "invalid id %d completed on queue %d\n", |
| 222 | cqe->command_id, le16_to_cpup(&cqe->sq_id)); |
| 223 | return; |
| 224 | } |
| 225 | |
Matthew Wilcox | 5c1281a | 2011-12-20 11:54:53 -0500 | [diff] [blame] | 226 | dev_warn(&dev->pci_dev->dev, "Unknown special completion %p\n", ctx); |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 227 | } |
| 228 | |
Matthew Wilcox | 184d294 | 2011-05-11 21:36:38 -0400 | [diff] [blame] | 229 | /* |
| 230 | * Called with local interrupts disabled and the q_lock held. May not sleep. |
| 231 | */ |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 232 | static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid, |
| 233 | nvme_completion_fn *fn) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 234 | { |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 235 | void *ctx; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 236 | struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 237 | |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 238 | if (cmdid >= nvmeq->q_depth) { |
| 239 | *fn = special_completion; |
Matthew Wilcox | 48e3d39 | 2011-02-06 08:51:15 -0500 | [diff] [blame] | 240 | return CMD_CTX_INVALID; |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 241 | } |
Keith Busch | 859361a | 2012-08-02 14:05:59 -0600 | [diff] [blame] | 242 | if (fn) |
| 243 | *fn = info[cmdid].fn; |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 244 | ctx = info[cmdid].ctx; |
| 245 | info[cmdid].fn = special_completion; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 246 | info[cmdid].ctx = CMD_CTX_COMPLETED; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 247 | clear_bit(cmdid, nvmeq->cmdid_data); |
| 248 | wake_up(&nvmeq->sq_full); |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 249 | return ctx; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 250 | } |
| 251 | |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 252 | static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid, |
| 253 | nvme_completion_fn *fn) |
Matthew Wilcox | 3c0cf13 | 2011-02-04 16:03:56 -0500 | [diff] [blame] | 254 | { |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 255 | void *ctx; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 256 | struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 257 | if (fn) |
| 258 | *fn = info[cmdid].fn; |
| 259 | ctx = info[cmdid].ctx; |
| 260 | info[cmdid].fn = special_completion; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 261 | info[cmdid].ctx = CMD_CTX_CANCELLED; |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 262 | return ctx; |
Matthew Wilcox | 3c0cf13 | 2011-02-04 16:03:56 -0500 | [diff] [blame] | 263 | } |
| 264 | |
Matthew Wilcox | 040a93b | 2011-12-20 11:04:12 -0500 | [diff] [blame] | 265 | static struct nvme_queue *get_nvmeq(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 266 | { |
Matthew Wilcox | 040a93b | 2011-12-20 11:04:12 -0500 | [diff] [blame] | 267 | return dev->queues[get_cpu() + 1]; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static void put_nvmeq(struct nvme_queue *nvmeq) |
| 271 | { |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 272 | put_cpu(); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /** |
Matthew Wilcox | 714a7a2 | 2011-03-16 16:28:24 -0400 | [diff] [blame] | 276 | * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 277 | * @nvmeq: The queue to use |
| 278 | * @cmd: The command to send |
| 279 | * |
| 280 | * Safe to use from interrupt context |
| 281 | */ |
| 282 | static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd) |
| 283 | { |
| 284 | unsigned long flags; |
| 285 | u16 tail; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 286 | spin_lock_irqsave(&nvmeq->q_lock, flags); |
| 287 | tail = nvmeq->sq_tail; |
| 288 | memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd)); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 289 | if (++tail == nvmeq->q_depth) |
| 290 | tail = 0; |
Matthew Wilcox | 7547881 | 2011-02-16 09:59:59 -0500 | [diff] [blame] | 291 | writel(tail, nvmeq->q_db); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 292 | nvmeq->sq_tail = tail; |
| 293 | spin_unlock_irqrestore(&nvmeq->q_lock, flags); |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 298 | /* |
| 299 | * The nvme_iod describes the data in an I/O, including the list of PRP |
| 300 | * entries. You can't see it in this data structure because C doesn't let |
| 301 | * me express that. Use nvme_alloc_iod to ensure there's enough space |
| 302 | * allocated to store the PRP list. |
| 303 | */ |
| 304 | struct nvme_iod { |
| 305 | void *private; /* For the use of the submitter of the I/O */ |
| 306 | int npages; /* In the PRP list. 0 means small pool in use */ |
| 307 | int offset; /* Of PRP list */ |
| 308 | int nents; /* Used in scatterlist */ |
| 309 | int length; /* Of data, in bytes */ |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 310 | dma_addr_t first_dma; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 311 | struct scatterlist sg[0]; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 312 | }; |
| 313 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 314 | static __le64 **iod_list(struct nvme_iod *iod) |
| 315 | { |
| 316 | return ((void *)iod) + iod->offset; |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Will slightly overestimate the number of pages needed. This is OK |
| 321 | * as it only leads to a small amount of wasted memory for the lifetime of |
| 322 | * the I/O. |
| 323 | */ |
| 324 | static int nvme_npages(unsigned size) |
| 325 | { |
| 326 | unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE); |
| 327 | return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8); |
| 328 | } |
| 329 | |
| 330 | static struct nvme_iod * |
| 331 | nvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp) |
| 332 | { |
| 333 | struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) + |
| 334 | sizeof(__le64 *) * nvme_npages(nbytes) + |
| 335 | sizeof(struct scatterlist) * nseg, gfp); |
| 336 | |
| 337 | if (iod) { |
| 338 | iod->offset = offsetof(struct nvme_iod, sg[nseg]); |
| 339 | iod->npages = -1; |
| 340 | iod->length = nbytes; |
Keith Busch | 2b19603 | 2012-11-06 11:59:23 -0700 | [diff] [blame] | 341 | iod->nents = 0; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | return iod; |
| 345 | } |
| 346 | |
| 347 | static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod) |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 348 | { |
| 349 | const int last_prp = PAGE_SIZE / 8 - 1; |
| 350 | int i; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 351 | __le64 **list = iod_list(iod); |
| 352 | dma_addr_t prp_dma = iod->first_dma; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 353 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 354 | if (iod->npages == 0) |
| 355 | dma_pool_free(dev->prp_small_pool, list[0], prp_dma); |
| 356 | for (i = 0; i < iod->npages; i++) { |
| 357 | __le64 *prp_list = list[i]; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 358 | dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]); |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 359 | dma_pool_free(dev->prp_page_pool, prp_list, prp_dma); |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 360 | prp_dma = next_prp_dma; |
| 361 | } |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 362 | kfree(iod); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 363 | } |
| 364 | |
Matthew Wilcox | 5c1281a | 2011-12-20 11:54:53 -0500 | [diff] [blame] | 365 | static void requeue_bio(struct nvme_dev *dev, struct bio *bio) |
| 366 | { |
| 367 | struct nvme_queue *nvmeq = get_nvmeq(dev); |
| 368 | if (bio_list_empty(&nvmeq->sq_cong)) |
| 369 | add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait); |
| 370 | bio_list_add(&nvmeq->sq_cong, bio); |
| 371 | put_nvmeq(nvmeq); |
| 372 | wake_up_process(nvme_thread); |
| 373 | } |
| 374 | |
| 375 | static void bio_completion(struct nvme_dev *dev, void *ctx, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 376 | struct nvme_completion *cqe) |
| 377 | { |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 378 | struct nvme_iod *iod = ctx; |
| 379 | struct bio *bio = iod->private; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 380 | u16 status = le16_to_cpup(&cqe->status) >> 1; |
| 381 | |
Keith Busch | 2b19603 | 2012-11-06 11:59:23 -0700 | [diff] [blame] | 382 | if (iod->nents) |
| 383 | dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 384 | bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 385 | nvme_free_iod(dev, iod); |
Matthew Wilcox | 09a58f5 | 2011-04-28 23:09:09 -0700 | [diff] [blame] | 386 | if (status) { |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 387 | bio_endio(bio, -EIO); |
Matthew Wilcox | 09a58f5 | 2011-04-28 23:09:09 -0700 | [diff] [blame] | 388 | } else if (bio->bi_vcnt > bio->bi_idx) { |
Matthew Wilcox | 5c1281a | 2011-12-20 11:54:53 -0500 | [diff] [blame] | 389 | requeue_bio(dev, bio); |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 390 | } else { |
| 391 | bio_endio(bio, 0); |
| 392 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 393 | } |
| 394 | |
Matthew Wilcox | 184d294 | 2011-05-11 21:36:38 -0400 | [diff] [blame] | 395 | /* length is in bytes. gfp flags indicates whether we may sleep. */ |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 396 | static int nvme_setup_prps(struct nvme_dev *dev, |
| 397 | struct nvme_common_command *cmd, struct nvme_iod *iod, |
| 398 | int total_len, gfp_t gfp) |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 399 | { |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 400 | struct dma_pool *pool; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 401 | int length = total_len; |
| 402 | struct scatterlist *sg = iod->sg; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 403 | int dma_len = sg_dma_len(sg); |
| 404 | u64 dma_addr = sg_dma_address(sg); |
| 405 | int offset = offset_in_page(dma_addr); |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 406 | __le64 *prp_list; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 407 | __le64 **list = iod_list(iod); |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 408 | dma_addr_t prp_dma; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 409 | int nprps, i; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 410 | |
| 411 | cmd->prp1 = cpu_to_le64(dma_addr); |
| 412 | length -= (PAGE_SIZE - offset); |
| 413 | if (length <= 0) |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 414 | return total_len; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 415 | |
| 416 | dma_len -= (PAGE_SIZE - offset); |
| 417 | if (dma_len) { |
| 418 | dma_addr += (PAGE_SIZE - offset); |
| 419 | } else { |
| 420 | sg = sg_next(sg); |
| 421 | dma_addr = sg_dma_address(sg); |
| 422 | dma_len = sg_dma_len(sg); |
| 423 | } |
| 424 | |
| 425 | if (length <= PAGE_SIZE) { |
| 426 | cmd->prp2 = cpu_to_le64(dma_addr); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 427 | return total_len; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 428 | } |
| 429 | |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 430 | nprps = DIV_ROUND_UP(length, PAGE_SIZE); |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 431 | if (nprps <= (256 / 8)) { |
| 432 | pool = dev->prp_small_pool; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 433 | iod->npages = 0; |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 434 | } else { |
| 435 | pool = dev->prp_page_pool; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 436 | iod->npages = 1; |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 437 | } |
| 438 | |
Matthew Wilcox | b77954c | 2011-05-12 13:51:41 -0400 | [diff] [blame] | 439 | prp_list = dma_pool_alloc(pool, gfp, &prp_dma); |
| 440 | if (!prp_list) { |
| 441 | cmd->prp2 = cpu_to_le64(dma_addr); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 442 | iod->npages = -1; |
| 443 | return (total_len - length) + PAGE_SIZE; |
Matthew Wilcox | b77954c | 2011-05-12 13:51:41 -0400 | [diff] [blame] | 444 | } |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 445 | list[0] = prp_list; |
| 446 | iod->first_dma = prp_dma; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 447 | cmd->prp2 = cpu_to_le64(prp_dma); |
| 448 | i = 0; |
| 449 | for (;;) { |
Matthew Wilcox | 7523d83 | 2011-03-16 16:43:40 -0400 | [diff] [blame] | 450 | if (i == PAGE_SIZE / 8) { |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 451 | __le64 *old_prp_list = prp_list; |
Matthew Wilcox | b77954c | 2011-05-12 13:51:41 -0400 | [diff] [blame] | 452 | prp_list = dma_pool_alloc(pool, gfp, &prp_dma); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 453 | if (!prp_list) |
| 454 | return total_len - length; |
| 455 | list[iod->npages++] = prp_list; |
Matthew Wilcox | 7523d83 | 2011-03-16 16:43:40 -0400 | [diff] [blame] | 456 | prp_list[0] = old_prp_list[i - 1]; |
| 457 | old_prp_list[i - 1] = cpu_to_le64(prp_dma); |
| 458 | i = 1; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 459 | } |
| 460 | prp_list[i++] = cpu_to_le64(dma_addr); |
| 461 | dma_len -= PAGE_SIZE; |
| 462 | dma_addr += PAGE_SIZE; |
| 463 | length -= PAGE_SIZE; |
| 464 | if (length <= 0) |
| 465 | break; |
| 466 | if (dma_len > 0) |
| 467 | continue; |
| 468 | BUG_ON(dma_len < 0); |
| 469 | sg = sg_next(sg); |
| 470 | dma_addr = sg_dma_address(sg); |
| 471 | dma_len = sg_dma_len(sg); |
| 472 | } |
| 473 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 474 | return total_len; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 475 | } |
| 476 | |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 477 | /* NVMe scatterlists require no holes in the virtual address */ |
| 478 | #define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \ |
| 479 | (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE)) |
| 480 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 481 | static int nvme_map_bio(struct device *dev, struct nvme_iod *iod, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 482 | struct bio *bio, enum dma_data_direction dma_dir, int psegs) |
| 483 | { |
Matthew Wilcox | 7683084 | 2011-02-10 13:55:39 -0500 | [diff] [blame] | 484 | struct bio_vec *bvec, *bvprv = NULL; |
| 485 | struct scatterlist *sg = NULL; |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 486 | int i, old_idx, length = 0, nsegs = 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 487 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 488 | sg_init_table(iod->sg, psegs); |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 489 | old_idx = bio->bi_idx; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 490 | bio_for_each_segment(bvec, bio, i) { |
Matthew Wilcox | 7683084 | 2011-02-10 13:55:39 -0500 | [diff] [blame] | 491 | if (bvprv && BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) { |
| 492 | sg->length += bvec->bv_len; |
| 493 | } else { |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 494 | if (bvprv && BIOVEC_NOT_VIRT_MERGEABLE(bvprv, bvec)) |
| 495 | break; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 496 | sg = sg ? sg + 1 : iod->sg; |
Matthew Wilcox | 7683084 | 2011-02-10 13:55:39 -0500 | [diff] [blame] | 497 | sg_set_page(sg, bvec->bv_page, bvec->bv_len, |
| 498 | bvec->bv_offset); |
| 499 | nsegs++; |
| 500 | } |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 501 | length += bvec->bv_len; |
Matthew Wilcox | 7683084 | 2011-02-10 13:55:39 -0500 | [diff] [blame] | 502 | bvprv = bvec; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 503 | } |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 504 | bio->bi_idx = i; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 505 | iod->nents = nsegs; |
Matthew Wilcox | 7683084 | 2011-02-10 13:55:39 -0500 | [diff] [blame] | 506 | sg_mark_end(sg); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 507 | if (dma_map_sg(dev, iod->sg, iod->nents, dma_dir) == 0) { |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 508 | bio->bi_idx = old_idx; |
| 509 | return -ENOMEM; |
| 510 | } |
| 511 | return length; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 512 | } |
| 513 | |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame^] | 514 | /* |
| 515 | * We reuse the small pool to allocate the 16-byte range here as it is not |
| 516 | * worth having a special pool for these or additional cases to handle freeing |
| 517 | * the iod. |
| 518 | */ |
| 519 | static int nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns, |
| 520 | struct bio *bio, struct nvme_iod *iod, int cmdid) |
| 521 | { |
| 522 | struct nvme_dsm_range *range; |
| 523 | struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; |
| 524 | |
| 525 | range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC, |
| 526 | &iod->first_dma); |
| 527 | if (!range) |
| 528 | return -ENOMEM; |
| 529 | |
| 530 | iod_list(iod)[0] = (__le64 *)range; |
| 531 | iod->npages = 0; |
| 532 | |
| 533 | range->cattr = cpu_to_le32(0); |
| 534 | range->nlb = cpu_to_le32(bio->bi_size >> ns->lba_shift); |
| 535 | range->slba = cpu_to_le64(bio->bi_sector >> (ns->lba_shift - 9)); |
| 536 | |
| 537 | memset(cmnd, 0, sizeof(*cmnd)); |
| 538 | cmnd->dsm.opcode = nvme_cmd_dsm; |
| 539 | cmnd->dsm.command_id = cmdid; |
| 540 | cmnd->dsm.nsid = cpu_to_le32(ns->ns_id); |
| 541 | cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma); |
| 542 | cmnd->dsm.nr = 0; |
| 543 | cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD); |
| 544 | |
| 545 | if (++nvmeq->sq_tail == nvmeq->q_depth) |
| 546 | nvmeq->sq_tail = 0; |
| 547 | writel(nvmeq->sq_tail, nvmeq->q_db); |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
Matthew Wilcox | 00df5cb | 2011-02-22 14:18:30 -0500 | [diff] [blame] | 552 | static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns, |
| 553 | int cmdid) |
| 554 | { |
| 555 | struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; |
| 556 | |
| 557 | memset(cmnd, 0, sizeof(*cmnd)); |
| 558 | cmnd->common.opcode = nvme_cmd_flush; |
| 559 | cmnd->common.command_id = cmdid; |
| 560 | cmnd->common.nsid = cpu_to_le32(ns->ns_id); |
| 561 | |
| 562 | if (++nvmeq->sq_tail == nvmeq->q_depth) |
| 563 | nvmeq->sq_tail = 0; |
| 564 | writel(nvmeq->sq_tail, nvmeq->q_db); |
| 565 | |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | static int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns) |
| 570 | { |
| 571 | int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH, |
Matthew Wilcox | ff976d7 | 2011-12-20 13:53:01 -0500 | [diff] [blame] | 572 | special_completion, NVME_IO_TIMEOUT); |
Matthew Wilcox | 00df5cb | 2011-02-22 14:18:30 -0500 | [diff] [blame] | 573 | if (unlikely(cmdid < 0)) |
| 574 | return cmdid; |
| 575 | |
| 576 | return nvme_submit_flush(nvmeq, ns, cmdid); |
| 577 | } |
| 578 | |
Matthew Wilcox | 184d294 | 2011-05-11 21:36:38 -0400 | [diff] [blame] | 579 | /* |
| 580 | * Called with local interrupts disabled and the q_lock held. May not sleep. |
| 581 | */ |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 582 | static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns, |
| 583 | struct bio *bio) |
| 584 | { |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 585 | struct nvme_command *cmnd; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 586 | struct nvme_iod *iod; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 587 | enum dma_data_direction dma_dir; |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 588 | int cmdid, length, result = -ENOMEM; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 589 | u16 control; |
| 590 | u32 dsmgmt; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 591 | int psegs = bio_phys_segments(ns->queue, bio); |
| 592 | |
Matthew Wilcox | 00df5cb | 2011-02-22 14:18:30 -0500 | [diff] [blame] | 593 | if ((bio->bi_rw & REQ_FLUSH) && psegs) { |
| 594 | result = nvme_submit_flush_data(nvmeq, ns); |
| 595 | if (result) |
| 596 | return result; |
| 597 | } |
| 598 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 599 | iod = nvme_alloc_iod(psegs, bio->bi_size, GFP_ATOMIC); |
| 600 | if (!iod) |
Matthew Wilcox | eeee322 | 2011-02-14 15:55:33 -0500 | [diff] [blame] | 601 | goto nomem; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 602 | iod->private = bio; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 603 | |
Matthew Wilcox | eeee322 | 2011-02-14 15:55:33 -0500 | [diff] [blame] | 604 | result = -EBUSY; |
Matthew Wilcox | ff976d7 | 2011-12-20 13:53:01 -0500 | [diff] [blame] | 605 | cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 606 | if (unlikely(cmdid < 0)) |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 607 | goto free_iod; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 608 | |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame^] | 609 | if (bio->bi_rw & REQ_DISCARD) { |
| 610 | result = nvme_submit_discard(nvmeq, ns, bio, iod, cmdid); |
| 611 | if (result) |
| 612 | goto free_cmdid; |
| 613 | return result; |
| 614 | } |
Matthew Wilcox | 00df5cb | 2011-02-22 14:18:30 -0500 | [diff] [blame] | 615 | if ((bio->bi_rw & REQ_FLUSH) && !psegs) |
| 616 | return nvme_submit_flush(nvmeq, ns, cmdid); |
| 617 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 618 | control = 0; |
| 619 | if (bio->bi_rw & REQ_FUA) |
| 620 | control |= NVME_RW_FUA; |
| 621 | if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD)) |
| 622 | control |= NVME_RW_LR; |
| 623 | |
| 624 | dsmgmt = 0; |
| 625 | if (bio->bi_rw & REQ_RAHEAD) |
| 626 | dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH; |
| 627 | |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 628 | cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 629 | |
Matthew Wilcox | b8deb62 | 2011-01-26 10:08:25 -0500 | [diff] [blame] | 630 | memset(cmnd, 0, sizeof(*cmnd)); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 631 | if (bio_data_dir(bio)) { |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 632 | cmnd->rw.opcode = nvme_cmd_write; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 633 | dma_dir = DMA_TO_DEVICE; |
| 634 | } else { |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 635 | cmnd->rw.opcode = nvme_cmd_read; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 636 | dma_dir = DMA_FROM_DEVICE; |
| 637 | } |
| 638 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 639 | result = nvme_map_bio(nvmeq->q_dmadev, iod, bio, dma_dir, psegs); |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 640 | if (result < 0) |
Keith Busch | 859361a | 2012-08-02 14:05:59 -0600 | [diff] [blame] | 641 | goto free_cmdid; |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 642 | length = result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 643 | |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 644 | cmnd->rw.command_id = cmdid; |
| 645 | cmnd->rw.nsid = cpu_to_le32(ns->ns_id); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 646 | length = nvme_setup_prps(nvmeq->dev, &cmnd->common, iod, length, |
| 647 | GFP_ATOMIC); |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 648 | cmnd->rw.slba = cpu_to_le64(bio->bi_sector >> (ns->lba_shift - 9)); |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 649 | cmnd->rw.length = cpu_to_le16((length >> ns->lba_shift) - 1); |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 650 | cmnd->rw.control = cpu_to_le16(control); |
| 651 | cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 652 | |
Matthew Wilcox | d8ee9d6 | 2011-02-24 08:46:00 -0500 | [diff] [blame] | 653 | bio->bi_sector += length >> 9; |
| 654 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 655 | if (++nvmeq->sq_tail == nvmeq->q_depth) |
| 656 | nvmeq->sq_tail = 0; |
Matthew Wilcox | 7547881 | 2011-02-16 09:59:59 -0500 | [diff] [blame] | 657 | writel(nvmeq->sq_tail, nvmeq->q_db); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 658 | |
Matthew Wilcox | 1974b1a | 2011-02-10 12:01:09 -0500 | [diff] [blame] | 659 | return 0; |
| 660 | |
Keith Busch | 859361a | 2012-08-02 14:05:59 -0600 | [diff] [blame] | 661 | free_cmdid: |
| 662 | free_cmdid(nvmeq, cmdid, NULL); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 663 | free_iod: |
| 664 | nvme_free_iod(nvmeq->dev, iod); |
Matthew Wilcox | eeee322 | 2011-02-14 15:55:33 -0500 | [diff] [blame] | 665 | nomem: |
| 666 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 667 | } |
| 668 | |
Linus Torvalds | 93c3d65 | 2012-01-18 15:41:27 -0800 | [diff] [blame] | 669 | static void nvme_make_request(struct request_queue *q, struct bio *bio) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 670 | { |
| 671 | struct nvme_ns *ns = q->queuedata; |
Matthew Wilcox | 040a93b | 2011-12-20 11:04:12 -0500 | [diff] [blame] | 672 | struct nvme_queue *nvmeq = get_nvmeq(ns->dev); |
Matthew Wilcox | eeee322 | 2011-02-14 15:55:33 -0500 | [diff] [blame] | 673 | int result = -EBUSY; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 674 | |
Matthew Wilcox | eeee322 | 2011-02-14 15:55:33 -0500 | [diff] [blame] | 675 | spin_lock_irq(&nvmeq->q_lock); |
| 676 | if (bio_list_empty(&nvmeq->sq_cong)) |
| 677 | result = nvme_submit_bio_queue(nvmeq, ns, bio); |
| 678 | if (unlikely(result)) { |
| 679 | if (bio_list_empty(&nvmeq->sq_cong)) |
| 680 | add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 681 | bio_list_add(&nvmeq->sq_cong, bio); |
| 682 | } |
Matthew Wilcox | eeee322 | 2011-02-14 15:55:33 -0500 | [diff] [blame] | 683 | |
| 684 | spin_unlock_irq(&nvmeq->q_lock); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 685 | put_nvmeq(nvmeq); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 686 | } |
| 687 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 688 | static irqreturn_t nvme_process_cq(struct nvme_queue *nvmeq) |
| 689 | { |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 690 | u16 head, phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 691 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 692 | head = nvmeq->cq_head; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 693 | phase = nvmeq->cq_phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 694 | |
| 695 | for (;;) { |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 696 | void *ctx; |
| 697 | nvme_completion_fn fn; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 698 | struct nvme_completion cqe = nvmeq->cqes[head]; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 699 | if ((le16_to_cpu(cqe.status) & 1) != phase) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 700 | break; |
| 701 | nvmeq->sq_head = le16_to_cpu(cqe.sq_head); |
| 702 | if (++head == nvmeq->q_depth) { |
| 703 | head = 0; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 704 | phase = !phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 705 | } |
| 706 | |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 707 | ctx = free_cmdid(nvmeq, cqe.command_id, &fn); |
Matthew Wilcox | 5c1281a | 2011-12-20 11:54:53 -0500 | [diff] [blame] | 708 | fn(nvmeq->dev, ctx, &cqe); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | /* If the controller ignores the cq head doorbell and continuously |
| 712 | * writes to the queue, it is theoretically possible to wrap around |
| 713 | * the queue twice and mistakenly return IRQ_NONE. Linux only |
| 714 | * requires that 0.1% of your interrupts are handled, so this isn't |
| 715 | * a big problem. |
| 716 | */ |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 717 | if (head == nvmeq->cq_head && phase == nvmeq->cq_phase) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 718 | return IRQ_NONE; |
| 719 | |
Matthew Wilcox | f1938f6 | 2011-10-20 17:00:41 -0400 | [diff] [blame] | 720 | writel(head, nvmeq->q_db + (1 << nvmeq->dev->db_stride)); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 721 | nvmeq->cq_head = head; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 722 | nvmeq->cq_phase = phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 723 | |
| 724 | return IRQ_HANDLED; |
| 725 | } |
| 726 | |
| 727 | static irqreturn_t nvme_irq(int irq, void *data) |
| 728 | { |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 729 | irqreturn_t result; |
| 730 | struct nvme_queue *nvmeq = data; |
| 731 | spin_lock(&nvmeq->q_lock); |
| 732 | result = nvme_process_cq(nvmeq); |
| 733 | spin_unlock(&nvmeq->q_lock); |
| 734 | return result; |
| 735 | } |
| 736 | |
| 737 | static irqreturn_t nvme_irq_check(int irq, void *data) |
| 738 | { |
| 739 | struct nvme_queue *nvmeq = data; |
| 740 | struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head]; |
| 741 | if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase) |
| 742 | return IRQ_NONE; |
| 743 | return IRQ_WAKE_THREAD; |
| 744 | } |
| 745 | |
Matthew Wilcox | 3c0cf13 | 2011-02-04 16:03:56 -0500 | [diff] [blame] | 746 | static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid) |
| 747 | { |
| 748 | spin_lock_irq(&nvmeq->q_lock); |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 749 | cancel_cmdid(nvmeq, cmdid, NULL); |
Matthew Wilcox | 3c0cf13 | 2011-02-04 16:03:56 -0500 | [diff] [blame] | 750 | spin_unlock_irq(&nvmeq->q_lock); |
| 751 | } |
| 752 | |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 753 | struct sync_cmd_info { |
| 754 | struct task_struct *task; |
| 755 | u32 result; |
| 756 | int status; |
| 757 | }; |
| 758 | |
Matthew Wilcox | 5c1281a | 2011-12-20 11:54:53 -0500 | [diff] [blame] | 759 | static void sync_completion(struct nvme_dev *dev, void *ctx, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 760 | struct nvme_completion *cqe) |
| 761 | { |
| 762 | struct sync_cmd_info *cmdinfo = ctx; |
| 763 | cmdinfo->result = le32_to_cpup(&cqe->result); |
| 764 | cmdinfo->status = le16_to_cpup(&cqe->status) >> 1; |
| 765 | wake_up_process(cmdinfo->task); |
| 766 | } |
| 767 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 768 | /* |
| 769 | * Returns 0 on success. If the result is negative, it's a Linux error code; |
| 770 | * if the result is positive, it's an NVM Express status code |
| 771 | */ |
Matthew Wilcox | 3c0cf13 | 2011-02-04 16:03:56 -0500 | [diff] [blame] | 772 | static int nvme_submit_sync_cmd(struct nvme_queue *nvmeq, |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 773 | struct nvme_command *cmd, u32 *result, unsigned timeout) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 774 | { |
| 775 | int cmdid; |
| 776 | struct sync_cmd_info cmdinfo; |
| 777 | |
| 778 | cmdinfo.task = current; |
| 779 | cmdinfo.status = -EINTR; |
| 780 | |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 781 | cmdid = alloc_cmdid_killable(nvmeq, &cmdinfo, sync_completion, |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 782 | timeout); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 783 | if (cmdid < 0) |
| 784 | return cmdid; |
| 785 | cmd->common.command_id = cmdid; |
| 786 | |
Matthew Wilcox | 3c0cf13 | 2011-02-04 16:03:56 -0500 | [diff] [blame] | 787 | set_current_state(TASK_KILLABLE); |
| 788 | nvme_submit_cmd(nvmeq, cmd); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 789 | schedule(); |
| 790 | |
Matthew Wilcox | 3c0cf13 | 2011-02-04 16:03:56 -0500 | [diff] [blame] | 791 | if (cmdinfo.status == -EINTR) { |
| 792 | nvme_abort_command(nvmeq, cmdid); |
| 793 | return -EINTR; |
| 794 | } |
| 795 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 796 | if (result) |
| 797 | *result = cmdinfo.result; |
| 798 | |
| 799 | return cmdinfo.status; |
| 800 | } |
| 801 | |
| 802 | static int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd, |
| 803 | u32 *result) |
| 804 | { |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 805 | return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id) |
| 809 | { |
| 810 | int status; |
| 811 | struct nvme_command c; |
| 812 | |
| 813 | memset(&c, 0, sizeof(c)); |
| 814 | c.delete_queue.opcode = opcode; |
| 815 | c.delete_queue.qid = cpu_to_le16(id); |
| 816 | |
| 817 | status = nvme_submit_admin_cmd(dev, &c, NULL); |
| 818 | if (status) |
| 819 | return -EIO; |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid, |
| 824 | struct nvme_queue *nvmeq) |
| 825 | { |
| 826 | int status; |
| 827 | struct nvme_command c; |
| 828 | int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED; |
| 829 | |
| 830 | memset(&c, 0, sizeof(c)); |
| 831 | c.create_cq.opcode = nvme_admin_create_cq; |
| 832 | c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr); |
| 833 | c.create_cq.cqid = cpu_to_le16(qid); |
| 834 | c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1); |
| 835 | c.create_cq.cq_flags = cpu_to_le16(flags); |
| 836 | c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector); |
| 837 | |
| 838 | status = nvme_submit_admin_cmd(dev, &c, NULL); |
| 839 | if (status) |
| 840 | return -EIO; |
| 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid, |
| 845 | struct nvme_queue *nvmeq) |
| 846 | { |
| 847 | int status; |
| 848 | struct nvme_command c; |
| 849 | int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM; |
| 850 | |
| 851 | memset(&c, 0, sizeof(c)); |
| 852 | c.create_sq.opcode = nvme_admin_create_sq; |
| 853 | c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr); |
| 854 | c.create_sq.sqid = cpu_to_le16(qid); |
| 855 | c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1); |
| 856 | c.create_sq.sq_flags = cpu_to_le16(flags); |
| 857 | c.create_sq.cqid = cpu_to_le16(qid); |
| 858 | |
| 859 | status = nvme_submit_admin_cmd(dev, &c, NULL); |
| 860 | if (status) |
| 861 | return -EIO; |
| 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid) |
| 866 | { |
| 867 | return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid); |
| 868 | } |
| 869 | |
| 870 | static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid) |
| 871 | { |
| 872 | return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid); |
| 873 | } |
| 874 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 875 | static int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns, |
| 876 | dma_addr_t dma_addr) |
| 877 | { |
| 878 | struct nvme_command c; |
| 879 | |
| 880 | memset(&c, 0, sizeof(c)); |
| 881 | c.identify.opcode = nvme_admin_identify; |
| 882 | c.identify.nsid = cpu_to_le32(nsid); |
| 883 | c.identify.prp1 = cpu_to_le64(dma_addr); |
| 884 | c.identify.cns = cpu_to_le32(cns); |
| 885 | |
| 886 | return nvme_submit_admin_cmd(dev, &c, NULL); |
| 887 | } |
| 888 | |
Keith Busch | 08df1e0 | 2012-09-21 10:52:13 -0600 | [diff] [blame] | 889 | static int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid, |
| 890 | dma_addr_t dma_addr, u32 *result) |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 891 | { |
| 892 | struct nvme_command c; |
| 893 | |
| 894 | memset(&c, 0, sizeof(c)); |
| 895 | c.features.opcode = nvme_admin_get_features; |
Keith Busch | a42cecc | 2012-07-25 16:06:38 -0600 | [diff] [blame] | 896 | c.features.nsid = cpu_to_le32(nsid); |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 897 | c.features.prp1 = cpu_to_le64(dma_addr); |
| 898 | c.features.fid = cpu_to_le32(fid); |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 899 | |
Keith Busch | 08df1e0 | 2012-09-21 10:52:13 -0600 | [diff] [blame] | 900 | return nvme_submit_admin_cmd(dev, &c, result); |
Matthew Wilcox | df34813 | 2012-01-11 07:29:56 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | static int nvme_set_features(struct nvme_dev *dev, unsigned fid, |
| 904 | unsigned dword11, dma_addr_t dma_addr, u32 *result) |
| 905 | { |
| 906 | struct nvme_command c; |
| 907 | |
| 908 | memset(&c, 0, sizeof(c)); |
| 909 | c.features.opcode = nvme_admin_set_features; |
| 910 | c.features.prp1 = cpu_to_le64(dma_addr); |
| 911 | c.features.fid = cpu_to_le32(fid); |
| 912 | c.features.dword11 = cpu_to_le32(dword11); |
| 913 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 914 | return nvme_submit_admin_cmd(dev, &c, result); |
| 915 | } |
| 916 | |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 917 | /** |
| 918 | * nvme_cancel_ios - Cancel outstanding I/Os |
| 919 | * @queue: The queue to cancel I/Os on |
| 920 | * @timeout: True to only cancel I/Os which have timed out |
| 921 | */ |
| 922 | static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout) |
| 923 | { |
| 924 | int depth = nvmeq->q_depth - 1; |
| 925 | struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); |
| 926 | unsigned long now = jiffies; |
| 927 | int cmdid; |
| 928 | |
| 929 | for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) { |
| 930 | void *ctx; |
| 931 | nvme_completion_fn fn; |
| 932 | static struct nvme_completion cqe = { |
| 933 | .status = cpu_to_le16(NVME_SC_ABORT_REQ) << 1, |
| 934 | }; |
| 935 | |
| 936 | if (timeout && !time_after(now, info[cmdid].timeout)) |
| 937 | continue; |
| 938 | dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d\n", cmdid); |
| 939 | ctx = cancel_cmdid(nvmeq, cmdid, &fn); |
| 940 | fn(nvmeq->dev, ctx, &cqe); |
| 941 | } |
| 942 | } |
| 943 | |
Matthew Wilcox | 9e86677 | 2012-08-03 13:55:56 -0400 | [diff] [blame] | 944 | static void nvme_free_queue_mem(struct nvme_queue *nvmeq) |
| 945 | { |
| 946 | dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth), |
| 947 | (void *)nvmeq->cqes, nvmeq->cq_dma_addr); |
| 948 | dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth), |
| 949 | nvmeq->sq_cmds, nvmeq->sq_dma_addr); |
| 950 | kfree(nvmeq); |
| 951 | } |
| 952 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 953 | static void nvme_free_queue(struct nvme_dev *dev, int qid) |
| 954 | { |
| 955 | struct nvme_queue *nvmeq = dev->queues[qid]; |
Matthew Wilcox | aba2080 | 2011-03-27 08:52:06 -0400 | [diff] [blame] | 956 | int vector = dev->entry[nvmeq->cq_vector].vector; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 957 | |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 958 | spin_lock_irq(&nvmeq->q_lock); |
| 959 | nvme_cancel_ios(nvmeq, false); |
Keith Busch | 3295874 | 2012-08-20 14:57:49 -0600 | [diff] [blame] | 960 | while (bio_list_peek(&nvmeq->sq_cong)) { |
| 961 | struct bio *bio = bio_list_pop(&nvmeq->sq_cong); |
| 962 | bio_endio(bio, -EIO); |
| 963 | } |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 964 | spin_unlock_irq(&nvmeq->q_lock); |
| 965 | |
Matthew Wilcox | aba2080 | 2011-03-27 08:52:06 -0400 | [diff] [blame] | 966 | irq_set_affinity_hint(vector, NULL); |
| 967 | free_irq(vector, nvmeq); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 968 | |
| 969 | /* Don't tell the adapter to delete the admin queue */ |
| 970 | if (qid) { |
| 971 | adapter_delete_sq(dev, qid); |
| 972 | adapter_delete_cq(dev, qid); |
| 973 | } |
| 974 | |
Matthew Wilcox | 9e86677 | 2012-08-03 13:55:56 -0400 | [diff] [blame] | 975 | nvme_free_queue_mem(nvmeq); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid, |
| 979 | int depth, int vector) |
| 980 | { |
| 981 | struct device *dmadev = &dev->pci_dev->dev; |
Keith Busch | a0cadb8 | 2012-07-27 13:57:23 -0400 | [diff] [blame] | 982 | unsigned extra = DIV_ROUND_UP(depth, 8) + (depth * |
| 983 | sizeof(struct nvme_cmd_info)); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 984 | struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL); |
| 985 | if (!nvmeq) |
| 986 | return NULL; |
| 987 | |
| 988 | nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth), |
| 989 | &nvmeq->cq_dma_addr, GFP_KERNEL); |
| 990 | if (!nvmeq->cqes) |
| 991 | goto free_nvmeq; |
| 992 | memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth)); |
| 993 | |
| 994 | nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth), |
| 995 | &nvmeq->sq_dma_addr, GFP_KERNEL); |
| 996 | if (!nvmeq->sq_cmds) |
| 997 | goto free_cqdma; |
| 998 | |
| 999 | nvmeq->q_dmadev = dmadev; |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 1000 | nvmeq->dev = dev; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1001 | spin_lock_init(&nvmeq->q_lock); |
| 1002 | nvmeq->cq_head = 0; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 1003 | nvmeq->cq_phase = 1; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1004 | init_waitqueue_head(&nvmeq->sq_full); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1005 | init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1006 | bio_list_init(&nvmeq->sq_cong); |
Matthew Wilcox | f1938f6 | 2011-10-20 17:00:41 -0400 | [diff] [blame] | 1007 | nvmeq->q_db = &dev->dbs[qid << (dev->db_stride + 1)]; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1008 | nvmeq->q_depth = depth; |
| 1009 | nvmeq->cq_vector = vector; |
| 1010 | |
| 1011 | return nvmeq; |
| 1012 | |
| 1013 | free_cqdma: |
| 1014 | dma_free_coherent(dmadev, CQ_SIZE(nvmeq->q_depth), (void *)nvmeq->cqes, |
| 1015 | nvmeq->cq_dma_addr); |
| 1016 | free_nvmeq: |
| 1017 | kfree(nvmeq); |
| 1018 | return NULL; |
| 1019 | } |
| 1020 | |
Matthew Wilcox | 3001082 | 2011-01-20 09:10:15 -0500 | [diff] [blame] | 1021 | static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq, |
| 1022 | const char *name) |
| 1023 | { |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 1024 | if (use_threaded_interrupts) |
| 1025 | return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector, |
Matthew Wilcox | ec6ce61 | 2011-02-06 09:01:00 -0500 | [diff] [blame] | 1026 | nvme_irq_check, nvme_irq, |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 1027 | IRQF_DISABLED | IRQF_SHARED, |
| 1028 | name, nvmeq); |
Matthew Wilcox | 3001082 | 2011-01-20 09:10:15 -0500 | [diff] [blame] | 1029 | return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq, |
| 1030 | IRQF_DISABLED | IRQF_SHARED, name, nvmeq); |
| 1031 | } |
| 1032 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1033 | static struct nvme_queue *nvme_create_queue(struct nvme_dev *dev, int qid, |
| 1034 | int cq_size, int vector) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1035 | { |
| 1036 | int result; |
| 1037 | struct nvme_queue *nvmeq = nvme_alloc_queue(dev, qid, cq_size, vector); |
| 1038 | |
Matthew Wilcox | 3f85d50 | 2011-02-01 08:39:04 -0500 | [diff] [blame] | 1039 | if (!nvmeq) |
Matthew Wilcox | 6f0f544 | 2011-05-11 13:30:59 -0700 | [diff] [blame] | 1040 | return ERR_PTR(-ENOMEM); |
Matthew Wilcox | 3f85d50 | 2011-02-01 08:39:04 -0500 | [diff] [blame] | 1041 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1042 | result = adapter_alloc_cq(dev, qid, nvmeq); |
| 1043 | if (result < 0) |
| 1044 | goto free_nvmeq; |
| 1045 | |
| 1046 | result = adapter_alloc_sq(dev, qid, nvmeq); |
| 1047 | if (result < 0) |
| 1048 | goto release_cq; |
| 1049 | |
Matthew Wilcox | 3001082 | 2011-01-20 09:10:15 -0500 | [diff] [blame] | 1050 | result = queue_request_irq(dev, nvmeq, "nvme"); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1051 | if (result < 0) |
| 1052 | goto release_sq; |
| 1053 | |
| 1054 | return nvmeq; |
| 1055 | |
| 1056 | release_sq: |
| 1057 | adapter_delete_sq(dev, qid); |
| 1058 | release_cq: |
| 1059 | adapter_delete_cq(dev, qid); |
| 1060 | free_nvmeq: |
| 1061 | dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth), |
| 1062 | (void *)nvmeq->cqes, nvmeq->cq_dma_addr); |
| 1063 | dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth), |
| 1064 | nvmeq->sq_cmds, nvmeq->sq_dma_addr); |
| 1065 | kfree(nvmeq); |
Matthew Wilcox | 6f0f544 | 2011-05-11 13:30:59 -0700 | [diff] [blame] | 1066 | return ERR_PTR(result); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1067 | } |
| 1068 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1069 | static int nvme_configure_admin_queue(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1070 | { |
Matthew Wilcox | 9e86677 | 2012-08-03 13:55:56 -0400 | [diff] [blame] | 1071 | int result = 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1072 | u32 aqa; |
Matthew Wilcox | 22605f9 | 2011-04-19 15:04:20 -0400 | [diff] [blame] | 1073 | u64 cap; |
| 1074 | unsigned long timeout; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1075 | struct nvme_queue *nvmeq; |
| 1076 | |
| 1077 | dev->dbs = ((void __iomem *)dev->bar) + 4096; |
| 1078 | |
| 1079 | nvmeq = nvme_alloc_queue(dev, 0, 64, 0); |
Matthew Wilcox | 3f85d50 | 2011-02-01 08:39:04 -0500 | [diff] [blame] | 1080 | if (!nvmeq) |
| 1081 | return -ENOMEM; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1082 | |
| 1083 | aqa = nvmeq->q_depth - 1; |
| 1084 | aqa |= aqa << 16; |
| 1085 | |
| 1086 | dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM; |
| 1087 | dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT; |
| 1088 | dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE; |
Matthew Wilcox | 7f53f9d | 2011-03-22 15:55:45 -0400 | [diff] [blame] | 1089 | dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1090 | |
Shane Michael Matthews | 5911f20 | 2011-02-01 11:31:55 -0500 | [diff] [blame] | 1091 | writel(0, &dev->bar->cc); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1092 | writel(aqa, &dev->bar->aqa); |
| 1093 | writeq(nvmeq->sq_dma_addr, &dev->bar->asq); |
| 1094 | writeq(nvmeq->cq_dma_addr, &dev->bar->acq); |
| 1095 | writel(dev->ctrl_config, &dev->bar->cc); |
| 1096 | |
Matthew Wilcox | 22605f9 | 2011-04-19 15:04:20 -0400 | [diff] [blame] | 1097 | cap = readq(&dev->bar->cap); |
| 1098 | timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies; |
Matthew Wilcox | f1938f6 | 2011-10-20 17:00:41 -0400 | [diff] [blame] | 1099 | dev->db_stride = NVME_CAP_STRIDE(cap); |
Matthew Wilcox | 22605f9 | 2011-04-19 15:04:20 -0400 | [diff] [blame] | 1100 | |
Matthew Wilcox | 9e86677 | 2012-08-03 13:55:56 -0400 | [diff] [blame] | 1101 | while (!result && !(readl(&dev->bar->csts) & NVME_CSTS_RDY)) { |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1102 | msleep(100); |
| 1103 | if (fatal_signal_pending(current)) |
Matthew Wilcox | 9e86677 | 2012-08-03 13:55:56 -0400 | [diff] [blame] | 1104 | result = -EINTR; |
Matthew Wilcox | 22605f9 | 2011-04-19 15:04:20 -0400 | [diff] [blame] | 1105 | if (time_after(jiffies, timeout)) { |
| 1106 | dev_err(&dev->pci_dev->dev, |
| 1107 | "Device not ready; aborting initialisation\n"); |
Matthew Wilcox | 9e86677 | 2012-08-03 13:55:56 -0400 | [diff] [blame] | 1108 | result = -ENODEV; |
Matthew Wilcox | 22605f9 | 2011-04-19 15:04:20 -0400 | [diff] [blame] | 1109 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1110 | } |
| 1111 | |
Matthew Wilcox | 9e86677 | 2012-08-03 13:55:56 -0400 | [diff] [blame] | 1112 | if (result) { |
| 1113 | nvme_free_queue_mem(nvmeq); |
| 1114 | return result; |
| 1115 | } |
| 1116 | |
Matthew Wilcox | 3001082 | 2011-01-20 09:10:15 -0500 | [diff] [blame] | 1117 | result = queue_request_irq(dev, nvmeq, "nvme admin"); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1118 | dev->queues[0] = nvmeq; |
| 1119 | return result; |
| 1120 | } |
| 1121 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1122 | static struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write, |
| 1123 | unsigned long addr, unsigned length) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1124 | { |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 1125 | int i, err, count, nents, offset; |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1126 | struct scatterlist *sg; |
| 1127 | struct page **pages; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1128 | struct nvme_iod *iod; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1129 | |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 1130 | if (addr & 3) |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1131 | return ERR_PTR(-EINVAL); |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1132 | if (!length) |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1133 | return ERR_PTR(-EINVAL); |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1134 | |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 1135 | offset = offset_in_page(addr); |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1136 | count = DIV_ROUND_UP(offset + length, PAGE_SIZE); |
| 1137 | pages = kcalloc(count, sizeof(*pages), GFP_KERNEL); |
Dan Carpenter | 22fff82 | 2012-01-20 07:55:30 -0500 | [diff] [blame] | 1138 | if (!pages) |
| 1139 | return ERR_PTR(-ENOMEM); |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 1140 | |
| 1141 | err = get_user_pages_fast(addr, count, 1, pages); |
| 1142 | if (err < count) { |
| 1143 | count = err; |
| 1144 | err = -EFAULT; |
| 1145 | goto put_pages; |
| 1146 | } |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1147 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1148 | iod = nvme_alloc_iod(count, length, GFP_KERNEL); |
| 1149 | sg = iod->sg; |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 1150 | sg_init_table(sg, count); |
Matthew Wilcox | d0ba1e4 | 2011-09-13 17:01:39 -0400 | [diff] [blame] | 1151 | for (i = 0; i < count; i++) { |
| 1152 | sg_set_page(&sg[i], pages[i], |
| 1153 | min_t(int, length, PAGE_SIZE - offset), offset); |
| 1154 | length -= (PAGE_SIZE - offset); |
| 1155 | offset = 0; |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1156 | } |
Matthew Wilcox | fe304c4 | 2012-01-06 13:49:25 -0700 | [diff] [blame] | 1157 | sg_mark_end(&sg[i - 1]); |
Matthew Wilcox | 1c2ad9f | 2012-01-06 13:52:56 -0700 | [diff] [blame] | 1158 | iod->nents = count; |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1159 | |
| 1160 | err = -ENOMEM; |
| 1161 | nents = dma_map_sg(&dev->pci_dev->dev, sg, count, |
| 1162 | write ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 1163 | if (!nents) |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1164 | goto free_iod; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1165 | |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1166 | kfree(pages); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1167 | return iod; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1168 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1169 | free_iod: |
| 1170 | kfree(iod); |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 1171 | put_pages: |
| 1172 | for (i = 0; i < count; i++) |
| 1173 | put_page(pages[i]); |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1174 | kfree(pages); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1175 | return ERR_PTR(err); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1176 | } |
| 1177 | |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1178 | static void nvme_unmap_user_pages(struct nvme_dev *dev, int write, |
Matthew Wilcox | 1c2ad9f | 2012-01-06 13:52:56 -0700 | [diff] [blame] | 1179 | struct nvme_iod *iod) |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1180 | { |
Matthew Wilcox | 1c2ad9f | 2012-01-06 13:52:56 -0700 | [diff] [blame] | 1181 | int i; |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1182 | |
Matthew Wilcox | 1c2ad9f | 2012-01-06 13:52:56 -0700 | [diff] [blame] | 1183 | dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents, |
| 1184 | write ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1185 | |
Matthew Wilcox | 1c2ad9f | 2012-01-06 13:52:56 -0700 | [diff] [blame] | 1186 | for (i = 0; i < iod->nents; i++) |
| 1187 | put_page(sg_page(&iod->sg[i])); |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1188 | } |
| 1189 | |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1190 | static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) |
| 1191 | { |
| 1192 | struct nvme_dev *dev = ns->dev; |
| 1193 | struct nvme_queue *nvmeq; |
| 1194 | struct nvme_user_io io; |
| 1195 | struct nvme_command c; |
| 1196 | unsigned length; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1197 | int status; |
| 1198 | struct nvme_iod *iod; |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1199 | |
| 1200 | if (copy_from_user(&io, uio, sizeof(io))) |
| 1201 | return -EFAULT; |
Matthew Wilcox | 6c7d494 | 2011-03-21 09:48:57 -0400 | [diff] [blame] | 1202 | length = (io.nblocks + 1) << ns->lba_shift; |
| 1203 | |
| 1204 | switch (io.opcode) { |
| 1205 | case nvme_cmd_write: |
| 1206 | case nvme_cmd_read: |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1207 | case nvme_cmd_compare: |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1208 | iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length); |
Matthew Wilcox | 6413214 | 2011-08-09 12:56:37 -0400 | [diff] [blame] | 1209 | break; |
Matthew Wilcox | 6c7d494 | 2011-03-21 09:48:57 -0400 | [diff] [blame] | 1210 | default: |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1211 | return -EINVAL; |
Matthew Wilcox | 6c7d494 | 2011-03-21 09:48:57 -0400 | [diff] [blame] | 1212 | } |
| 1213 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1214 | if (IS_ERR(iod)) |
| 1215 | return PTR_ERR(iod); |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1216 | |
| 1217 | memset(&c, 0, sizeof(c)); |
| 1218 | c.rw.opcode = io.opcode; |
| 1219 | c.rw.flags = io.flags; |
Matthew Wilcox | 6c7d494 | 2011-03-21 09:48:57 -0400 | [diff] [blame] | 1220 | c.rw.nsid = cpu_to_le32(ns->ns_id); |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1221 | c.rw.slba = cpu_to_le64(io.slba); |
Matthew Wilcox | 6c7d494 | 2011-03-21 09:48:57 -0400 | [diff] [blame] | 1222 | c.rw.length = cpu_to_le16(io.nblocks); |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1223 | c.rw.control = cpu_to_le16(io.control); |
| 1224 | c.rw.dsmgmt = cpu_to_le16(io.dsmgmt); |
Matthew Wilcox | 6c7d494 | 2011-03-21 09:48:57 -0400 | [diff] [blame] | 1225 | c.rw.reftag = io.reftag; |
| 1226 | c.rw.apptag = io.apptag; |
| 1227 | c.rw.appmask = io.appmask; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 1228 | /* XXX: metadata */ |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1229 | length = nvme_setup_prps(dev, &c.common, iod, length, GFP_KERNEL); |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 1230 | |
Matthew Wilcox | 040a93b | 2011-12-20 11:04:12 -0500 | [diff] [blame] | 1231 | nvmeq = get_nvmeq(dev); |
Matthew Wilcox | fa92282 | 2011-03-16 16:29:00 -0400 | [diff] [blame] | 1232 | /* |
| 1233 | * Since nvme_submit_sync_cmd sleeps, we can't keep preemption |
Matthew Wilcox | b1ad37e | 2011-02-04 16:14:30 -0500 | [diff] [blame] | 1234 | * disabled. We may be preempted at any point, and be rescheduled |
| 1235 | * to a different CPU. That will cause cacheline bouncing, but no |
| 1236 | * additional races since q_lock already protects against other CPUs. |
| 1237 | */ |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1238 | put_nvmeq(nvmeq); |
Matthew Wilcox | b77954c | 2011-05-12 13:51:41 -0400 | [diff] [blame] | 1239 | if (length != (io.nblocks + 1) << ns->lba_shift) |
| 1240 | status = -ENOMEM; |
| 1241 | else |
Matthew Wilcox | ff976d7 | 2011-12-20 13:53:01 -0500 | [diff] [blame] | 1242 | status = nvme_submit_sync_cmd(nvmeq, &c, NULL, NVME_IO_TIMEOUT); |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1243 | |
Matthew Wilcox | 1c2ad9f | 2012-01-06 13:52:56 -0700 | [diff] [blame] | 1244 | nvme_unmap_user_pages(dev, io.opcode & 1, iod); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1245 | nvme_free_iod(dev, iod); |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1246 | return status; |
| 1247 | } |
| 1248 | |
Keith Busch | 50af8ba | 2012-07-25 16:07:55 -0600 | [diff] [blame] | 1249 | static int nvme_user_admin_cmd(struct nvme_dev *dev, |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1250 | struct nvme_admin_cmd __user *ucmd) |
Matthew Wilcox | 6ee44cd | 2011-02-03 10:58:26 -0500 | [diff] [blame] | 1251 | { |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1252 | struct nvme_admin_cmd cmd; |
Matthew Wilcox | 6ee44cd | 2011-02-03 10:58:26 -0500 | [diff] [blame] | 1253 | struct nvme_command c; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1254 | int status, length; |
Keith Busch | c7d36ab | 2012-07-27 11:53:28 -0600 | [diff] [blame] | 1255 | struct nvme_iod *uninitialized_var(iod); |
Matthew Wilcox | 6ee44cd | 2011-02-03 10:58:26 -0500 | [diff] [blame] | 1256 | |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1257 | if (!capable(CAP_SYS_ADMIN)) |
| 1258 | return -EACCES; |
| 1259 | if (copy_from_user(&cmd, ucmd, sizeof(cmd))) |
Matthew Wilcox | 6ee44cd | 2011-02-03 10:58:26 -0500 | [diff] [blame] | 1260 | return -EFAULT; |
Matthew Wilcox | 6ee44cd | 2011-02-03 10:58:26 -0500 | [diff] [blame] | 1261 | |
| 1262 | memset(&c, 0, sizeof(c)); |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1263 | c.common.opcode = cmd.opcode; |
| 1264 | c.common.flags = cmd.flags; |
| 1265 | c.common.nsid = cpu_to_le32(cmd.nsid); |
| 1266 | c.common.cdw2[0] = cpu_to_le32(cmd.cdw2); |
| 1267 | c.common.cdw2[1] = cpu_to_le32(cmd.cdw3); |
| 1268 | c.common.cdw10[0] = cpu_to_le32(cmd.cdw10); |
| 1269 | c.common.cdw10[1] = cpu_to_le32(cmd.cdw11); |
| 1270 | c.common.cdw10[2] = cpu_to_le32(cmd.cdw12); |
| 1271 | c.common.cdw10[3] = cpu_to_le32(cmd.cdw13); |
| 1272 | c.common.cdw10[4] = cpu_to_le32(cmd.cdw14); |
| 1273 | c.common.cdw10[5] = cpu_to_le32(cmd.cdw15); |
| 1274 | |
| 1275 | length = cmd.data_len; |
| 1276 | if (cmd.data_len) { |
Matthew Wilcox | 4974218 | 2012-01-06 13:42:45 -0700 | [diff] [blame] | 1277 | iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr, |
| 1278 | length); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1279 | if (IS_ERR(iod)) |
| 1280 | return PTR_ERR(iod); |
| 1281 | length = nvme_setup_prps(dev, &c.common, iod, length, |
| 1282 | GFP_KERNEL); |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | if (length != cmd.data_len) |
Matthew Wilcox | b77954c | 2011-05-12 13:51:41 -0400 | [diff] [blame] | 1286 | status = -ENOMEM; |
| 1287 | else |
Keith Busch | f4f117f | 2012-09-21 10:49:05 -0600 | [diff] [blame] | 1288 | status = nvme_submit_admin_cmd(dev, &c, &cmd.result); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1289 | |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1290 | if (cmd.data_len) { |
Matthew Wilcox | 1c2ad9f | 2012-01-06 13:52:56 -0700 | [diff] [blame] | 1291 | nvme_unmap_user_pages(dev, cmd.opcode & 1, iod); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1292 | nvme_free_iod(dev, iod); |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1293 | } |
Keith Busch | f4f117f | 2012-09-21 10:49:05 -0600 | [diff] [blame] | 1294 | |
| 1295 | if (!status && copy_to_user(&ucmd->result, &cmd.result, |
| 1296 | sizeof(cmd.result))) |
| 1297 | status = -EFAULT; |
| 1298 | |
Matthew Wilcox | 6ee44cd | 2011-02-03 10:58:26 -0500 | [diff] [blame] | 1299 | return status; |
| 1300 | } |
| 1301 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1302 | static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, |
| 1303 | unsigned long arg) |
| 1304 | { |
| 1305 | struct nvme_ns *ns = bdev->bd_disk->private_data; |
| 1306 | |
| 1307 | switch (cmd) { |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1308 | case NVME_IOCTL_ID: |
| 1309 | return ns->ns_id; |
| 1310 | case NVME_IOCTL_ADMIN_CMD: |
Keith Busch | 50af8ba | 2012-07-25 16:07:55 -0600 | [diff] [blame] | 1311 | return nvme_user_admin_cmd(ns->dev, (void __user *)arg); |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1312 | case NVME_IOCTL_SUBMIT_IO: |
| 1313 | return nvme_submit_io(ns, (void __user *)arg); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1314 | default: |
| 1315 | return -ENOTTY; |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | static const struct block_device_operations nvme_fops = { |
| 1320 | .owner = THIS_MODULE, |
| 1321 | .ioctl = nvme_ioctl, |
Matthew Wilcox | 4948168 | 2011-03-19 14:55:38 -0400 | [diff] [blame] | 1322 | .compat_ioctl = nvme_ioctl, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1323 | }; |
| 1324 | |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1325 | static void nvme_resubmit_bios(struct nvme_queue *nvmeq) |
| 1326 | { |
| 1327 | while (bio_list_peek(&nvmeq->sq_cong)) { |
| 1328 | struct bio *bio = bio_list_pop(&nvmeq->sq_cong); |
| 1329 | struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data; |
| 1330 | if (nvme_submit_bio_queue(nvmeq, ns, bio)) { |
| 1331 | bio_list_add_head(&nvmeq->sq_cong, bio); |
| 1332 | break; |
| 1333 | } |
Matthew Wilcox | 3cb967c | 2011-03-16 16:45:49 -0400 | [diff] [blame] | 1334 | if (bio_list_empty(&nvmeq->sq_cong)) |
| 1335 | remove_wait_queue(&nvmeq->sq_full, |
| 1336 | &nvmeq->sq_cong_wait); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | static int nvme_kthread(void *data) |
| 1341 | { |
| 1342 | struct nvme_dev *dev; |
| 1343 | |
| 1344 | while (!kthread_should_stop()) { |
| 1345 | __set_current_state(TASK_RUNNING); |
| 1346 | spin_lock(&dev_list_lock); |
| 1347 | list_for_each_entry(dev, &dev_list, node) { |
| 1348 | int i; |
| 1349 | for (i = 0; i < dev->queue_count; i++) { |
| 1350 | struct nvme_queue *nvmeq = dev->queues[i]; |
Matthew Wilcox | 740216f | 2011-02-15 16:28:20 -0500 | [diff] [blame] | 1351 | if (!nvmeq) |
| 1352 | continue; |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1353 | spin_lock_irq(&nvmeq->q_lock); |
| 1354 | if (nvme_process_cq(nvmeq)) |
| 1355 | printk("process_cq did something\n"); |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1356 | nvme_cancel_ios(nvmeq, true); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1357 | nvme_resubmit_bios(nvmeq); |
| 1358 | spin_unlock_irq(&nvmeq->q_lock); |
| 1359 | } |
| 1360 | } |
| 1361 | spin_unlock(&dev_list_lock); |
| 1362 | set_current_state(TASK_INTERRUPTIBLE); |
| 1363 | schedule_timeout(HZ); |
| 1364 | } |
| 1365 | return 0; |
| 1366 | } |
| 1367 | |
Matthew Wilcox | 5aff938 | 2011-05-06 08:45:47 -0400 | [diff] [blame] | 1368 | static DEFINE_IDA(nvme_index_ida); |
| 1369 | |
| 1370 | static int nvme_get_ns_idx(void) |
| 1371 | { |
| 1372 | int index, error; |
| 1373 | |
| 1374 | do { |
| 1375 | if (!ida_pre_get(&nvme_index_ida, GFP_KERNEL)) |
| 1376 | return -1; |
| 1377 | |
| 1378 | spin_lock(&dev_list_lock); |
| 1379 | error = ida_get_new(&nvme_index_ida, &index); |
| 1380 | spin_unlock(&dev_list_lock); |
| 1381 | } while (error == -EAGAIN); |
| 1382 | |
| 1383 | if (error) |
| 1384 | index = -1; |
| 1385 | return index; |
| 1386 | } |
| 1387 | |
| 1388 | static void nvme_put_ns_idx(int index) |
| 1389 | { |
| 1390 | spin_lock(&dev_list_lock); |
| 1391 | ida_remove(&nvme_index_ida, index); |
| 1392 | spin_unlock(&dev_list_lock); |
| 1393 | } |
| 1394 | |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame^] | 1395 | static void nvme_config_discard(struct nvme_ns *ns) |
| 1396 | { |
| 1397 | u32 logical_block_size = queue_logical_block_size(ns->queue); |
| 1398 | ns->queue->limits.discard_zeroes_data = 0; |
| 1399 | ns->queue->limits.discard_alignment = logical_block_size; |
| 1400 | ns->queue->limits.discard_granularity = logical_block_size; |
| 1401 | ns->queue->limits.max_discard_sectors = 0xffffffff; |
| 1402 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue); |
| 1403 | } |
| 1404 | |
Matthew Wilcox | 5aff938 | 2011-05-06 08:45:47 -0400 | [diff] [blame] | 1405 | static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, int nsid, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1406 | struct nvme_id_ns *id, struct nvme_lba_range_type *rt) |
| 1407 | { |
| 1408 | struct nvme_ns *ns; |
| 1409 | struct gendisk *disk; |
| 1410 | int lbaf; |
| 1411 | |
| 1412 | if (rt->attributes & NVME_LBART_ATTRIB_HIDE) |
| 1413 | return NULL; |
| 1414 | |
| 1415 | ns = kzalloc(sizeof(*ns), GFP_KERNEL); |
| 1416 | if (!ns) |
| 1417 | return NULL; |
| 1418 | ns->queue = blk_alloc_queue(GFP_KERNEL); |
| 1419 | if (!ns->queue) |
| 1420 | goto out_free_ns; |
Matthew Wilcox | 4eeb921 | 2012-01-10 14:35:08 -0700 | [diff] [blame] | 1421 | ns->queue->queue_flags = QUEUE_FLAG_DEFAULT; |
| 1422 | queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue); |
| 1423 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1424 | blk_queue_make_request(ns->queue, nvme_make_request); |
| 1425 | ns->dev = dev; |
| 1426 | ns->queue->queuedata = ns; |
| 1427 | |
| 1428 | disk = alloc_disk(NVME_MINORS); |
| 1429 | if (!disk) |
| 1430 | goto out_free_queue; |
Matthew Wilcox | 5aff938 | 2011-05-06 08:45:47 -0400 | [diff] [blame] | 1431 | ns->ns_id = nsid; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1432 | ns->disk = disk; |
| 1433 | lbaf = id->flbas & 0xf; |
| 1434 | ns->lba_shift = id->lbaf[lbaf].ds; |
Keith Busch | e9ef463 | 2012-07-24 15:01:04 -0600 | [diff] [blame] | 1435 | blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift); |
Keith Busch | 8fc23e0 | 2012-07-26 11:29:57 -0600 | [diff] [blame] | 1436 | if (dev->max_hw_sectors) |
| 1437 | blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1438 | |
| 1439 | disk->major = nvme_major; |
| 1440 | disk->minors = NVME_MINORS; |
Matthew Wilcox | 5aff938 | 2011-05-06 08:45:47 -0400 | [diff] [blame] | 1441 | disk->first_minor = NVME_MINORS * nvme_get_ns_idx(); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1442 | disk->fops = &nvme_fops; |
| 1443 | disk->private_data = ns; |
| 1444 | disk->queue = ns->queue; |
Matthew Wilcox | 388f037 | 2011-02-01 12:49:38 -0500 | [diff] [blame] | 1445 | disk->driverfs_dev = &dev->pci_dev->dev; |
Matthew Wilcox | 5aff938 | 2011-05-06 08:45:47 -0400 | [diff] [blame] | 1446 | sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1447 | set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9)); |
| 1448 | |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame^] | 1449 | if (dev->oncs & NVME_CTRL_ONCS_DSM) |
| 1450 | nvme_config_discard(ns); |
| 1451 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1452 | return ns; |
| 1453 | |
| 1454 | out_free_queue: |
| 1455 | blk_cleanup_queue(ns->queue); |
| 1456 | out_free_ns: |
| 1457 | kfree(ns); |
| 1458 | return NULL; |
| 1459 | } |
| 1460 | |
| 1461 | static void nvme_ns_free(struct nvme_ns *ns) |
| 1462 | { |
Matthew Wilcox | 5aff938 | 2011-05-06 08:45:47 -0400 | [diff] [blame] | 1463 | int index = ns->disk->first_minor / NVME_MINORS; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1464 | put_disk(ns->disk); |
Matthew Wilcox | 5aff938 | 2011-05-06 08:45:47 -0400 | [diff] [blame] | 1465 | nvme_put_ns_idx(index); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1466 | blk_cleanup_queue(ns->queue); |
| 1467 | kfree(ns); |
| 1468 | } |
| 1469 | |
Matthew Wilcox | b3b0681 | 2011-01-20 09:14:34 -0500 | [diff] [blame] | 1470 | static int set_queue_count(struct nvme_dev *dev, int count) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1471 | { |
| 1472 | int status; |
| 1473 | u32 result; |
Matthew Wilcox | b3b0681 | 2011-01-20 09:14:34 -0500 | [diff] [blame] | 1474 | u32 q_count = (count - 1) | ((count - 1) << 16); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1475 | |
Matthew Wilcox | df34813 | 2012-01-11 07:29:56 -0700 | [diff] [blame] | 1476 | status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0, |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1477 | &result); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1478 | if (status) |
| 1479 | return -EIO; |
| 1480 | return min(result & 0xffff, result >> 16) + 1; |
| 1481 | } |
| 1482 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1483 | static int nvme_setup_io_queues(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1484 | { |
Keith Busch | a0cadb8 | 2012-07-27 13:57:23 -0400 | [diff] [blame] | 1485 | int result, cpu, i, nr_io_queues, db_bar_size, q_depth; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1486 | |
Matthew Wilcox | b348b7d | 2011-02-15 16:16:02 -0500 | [diff] [blame] | 1487 | nr_io_queues = num_online_cpus(); |
| 1488 | result = set_queue_count(dev, nr_io_queues); |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 1489 | if (result < 0) |
| 1490 | return result; |
Matthew Wilcox | b348b7d | 2011-02-15 16:16:02 -0500 | [diff] [blame] | 1491 | if (result < nr_io_queues) |
| 1492 | nr_io_queues = result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1493 | |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 1494 | /* Deregister the admin queue's interrupt */ |
| 1495 | free_irq(dev->entry[0].vector, dev->queues[0]); |
| 1496 | |
Matthew Wilcox | f1938f6 | 2011-10-20 17:00:41 -0400 | [diff] [blame] | 1497 | db_bar_size = 4096 + ((nr_io_queues + 1) << (dev->db_stride + 3)); |
| 1498 | if (db_bar_size > 8192) { |
| 1499 | iounmap(dev->bar); |
| 1500 | dev->bar = ioremap(pci_resource_start(dev->pci_dev, 0), |
| 1501 | db_bar_size); |
| 1502 | dev->dbs = ((void __iomem *)dev->bar) + 4096; |
| 1503 | dev->queues[0]->q_db = dev->dbs; |
| 1504 | } |
| 1505 | |
Matthew Wilcox | b348b7d | 2011-02-15 16:16:02 -0500 | [diff] [blame] | 1506 | for (i = 0; i < nr_io_queues; i++) |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 1507 | dev->entry[i].entry = i; |
| 1508 | for (;;) { |
Matthew Wilcox | b348b7d | 2011-02-15 16:16:02 -0500 | [diff] [blame] | 1509 | result = pci_enable_msix(dev->pci_dev, dev->entry, |
| 1510 | nr_io_queues); |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 1511 | if (result == 0) { |
| 1512 | break; |
| 1513 | } else if (result > 0) { |
Matthew Wilcox | b348b7d | 2011-02-15 16:16:02 -0500 | [diff] [blame] | 1514 | nr_io_queues = result; |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 1515 | continue; |
| 1516 | } else { |
Matthew Wilcox | b348b7d | 2011-02-15 16:16:02 -0500 | [diff] [blame] | 1517 | nr_io_queues = 1; |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 1518 | break; |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | result = queue_request_irq(dev, dev->queues[0], "nvme admin"); |
| 1523 | /* XXX: handle failure here */ |
| 1524 | |
| 1525 | cpu = cpumask_first(cpu_online_mask); |
Matthew Wilcox | b348b7d | 2011-02-15 16:16:02 -0500 | [diff] [blame] | 1526 | for (i = 0; i < nr_io_queues; i++) { |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 1527 | irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu)); |
| 1528 | cpu = cpumask_next(cpu, cpu_online_mask); |
| 1529 | } |
| 1530 | |
Keith Busch | a0cadb8 | 2012-07-27 13:57:23 -0400 | [diff] [blame] | 1531 | q_depth = min_t(int, NVME_CAP_MQES(readq(&dev->bar->cap)) + 1, |
| 1532 | NVME_Q_DEPTH); |
Matthew Wilcox | b348b7d | 2011-02-15 16:16:02 -0500 | [diff] [blame] | 1533 | for (i = 0; i < nr_io_queues; i++) { |
Keith Busch | a0cadb8 | 2012-07-27 13:57:23 -0400 | [diff] [blame] | 1534 | dev->queues[i + 1] = nvme_create_queue(dev, i + 1, q_depth, i); |
Matthew Wilcox | 6f0f544 | 2011-05-11 13:30:59 -0700 | [diff] [blame] | 1535 | if (IS_ERR(dev->queues[i + 1])) |
| 1536 | return PTR_ERR(dev->queues[i + 1]); |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 1537 | dev->queue_count++; |
| 1538 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1539 | |
Matthew Wilcox | 9ecdc94 | 2011-03-16 16:52:19 -0400 | [diff] [blame] | 1540 | for (; i < num_possible_cpus(); i++) { |
| 1541 | int target = i % rounddown_pow_of_two(dev->queue_count - 1); |
| 1542 | dev->queues[i + 1] = dev->queues[target + 1]; |
| 1543 | } |
| 1544 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1545 | return 0; |
| 1546 | } |
| 1547 | |
| 1548 | static void nvme_free_queues(struct nvme_dev *dev) |
| 1549 | { |
| 1550 | int i; |
| 1551 | |
| 1552 | for (i = dev->queue_count - 1; i >= 0; i--) |
| 1553 | nvme_free_queue(dev, i); |
| 1554 | } |
| 1555 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1556 | static int nvme_dev_add(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1557 | { |
| 1558 | int res, nn, i; |
| 1559 | struct nvme_ns *ns, *next; |
Matthew Wilcox | 5181423 | 2011-02-01 16:18:08 -0500 | [diff] [blame] | 1560 | struct nvme_id_ctrl *ctrl; |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1561 | struct nvme_id_ns *id_ns; |
| 1562 | void *mem; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1563 | dma_addr_t dma_addr; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1564 | |
| 1565 | res = nvme_setup_io_queues(dev); |
| 1566 | if (res) |
| 1567 | return res; |
| 1568 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1569 | mem = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1570 | GFP_KERNEL); |
| 1571 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1572 | res = nvme_identify(dev, 0, 1, dma_addr); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1573 | if (res) { |
| 1574 | res = -EIO; |
| 1575 | goto out_free; |
| 1576 | } |
| 1577 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1578 | ctrl = mem; |
Matthew Wilcox | 5181423 | 2011-02-01 16:18:08 -0500 | [diff] [blame] | 1579 | nn = le32_to_cpup(&ctrl->nn); |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame^] | 1580 | dev->oncs = le16_to_cpup(&ctrl->oncs); |
Matthew Wilcox | 5181423 | 2011-02-01 16:18:08 -0500 | [diff] [blame] | 1581 | memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn)); |
| 1582 | memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn)); |
| 1583 | memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr)); |
Keith Busch | 8fc23e0 | 2012-07-26 11:29:57 -0600 | [diff] [blame] | 1584 | if (ctrl->mdts) { |
| 1585 | int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12; |
| 1586 | dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9); |
| 1587 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1588 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1589 | id_ns = mem; |
Matthew Wilcox | 2b2c189 | 2011-10-07 13:10:13 -0400 | [diff] [blame] | 1590 | for (i = 1; i <= nn; i++) { |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1591 | res = nvme_identify(dev, i, 0, dma_addr); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1592 | if (res) |
| 1593 | continue; |
| 1594 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1595 | if (id_ns->ncap == 0) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1596 | continue; |
| 1597 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1598 | res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i, |
Keith Busch | 08df1e0 | 2012-09-21 10:52:13 -0600 | [diff] [blame] | 1599 | dma_addr + 4096, NULL); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1600 | if (res) |
Keith Busch | 1220903 | 2013-01-31 14:40:38 -0700 | [diff] [blame] | 1601 | memset(mem + 4096, 0, 4096); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1602 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1603 | ns = nvme_alloc_ns(dev, i, mem, mem + 4096); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1604 | if (ns) |
| 1605 | list_add_tail(&ns->list, &dev->namespaces); |
| 1606 | } |
| 1607 | list_for_each_entry(ns, &dev->namespaces, list) |
| 1608 | add_disk(ns->disk); |
| 1609 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1610 | goto out; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1611 | |
| 1612 | out_free: |
| 1613 | list_for_each_entry_safe(ns, next, &dev->namespaces, list) { |
| 1614 | list_del(&ns->list); |
| 1615 | nvme_ns_free(ns); |
| 1616 | } |
| 1617 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1618 | out: |
Matthew Wilcox | 684f5c2 | 2011-09-19 17:14:53 -0400 | [diff] [blame] | 1619 | dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1620 | return res; |
| 1621 | } |
| 1622 | |
| 1623 | static int nvme_dev_remove(struct nvme_dev *dev) |
| 1624 | { |
| 1625 | struct nvme_ns *ns, *next; |
| 1626 | |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1627 | spin_lock(&dev_list_lock); |
| 1628 | list_del(&dev->node); |
| 1629 | spin_unlock(&dev_list_lock); |
| 1630 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1631 | list_for_each_entry_safe(ns, next, &dev->namespaces, list) { |
| 1632 | list_del(&ns->list); |
| 1633 | del_gendisk(ns->disk); |
| 1634 | nvme_ns_free(ns); |
| 1635 | } |
| 1636 | |
| 1637 | nvme_free_queues(dev); |
| 1638 | |
| 1639 | return 0; |
| 1640 | } |
| 1641 | |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 1642 | static int nvme_setup_prp_pools(struct nvme_dev *dev) |
| 1643 | { |
| 1644 | struct device *dmadev = &dev->pci_dev->dev; |
| 1645 | dev->prp_page_pool = dma_pool_create("prp list page", dmadev, |
| 1646 | PAGE_SIZE, PAGE_SIZE, 0); |
| 1647 | if (!dev->prp_page_pool) |
| 1648 | return -ENOMEM; |
| 1649 | |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 1650 | /* Optimisation for I/Os between 4k and 128k */ |
| 1651 | dev->prp_small_pool = dma_pool_create("prp list 256", dmadev, |
| 1652 | 256, 256, 0); |
| 1653 | if (!dev->prp_small_pool) { |
| 1654 | dma_pool_destroy(dev->prp_page_pool); |
| 1655 | return -ENOMEM; |
| 1656 | } |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 1657 | return 0; |
| 1658 | } |
| 1659 | |
| 1660 | static void nvme_release_prp_pools(struct nvme_dev *dev) |
| 1661 | { |
| 1662 | dma_pool_destroy(dev->prp_page_pool); |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 1663 | dma_pool_destroy(dev->prp_small_pool); |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 1664 | } |
| 1665 | |
Quoc-Son Anh | cd58ad7 | 2012-02-21 16:50:53 -0700 | [diff] [blame] | 1666 | static DEFINE_IDA(nvme_instance_ida); |
| 1667 | |
| 1668 | static int nvme_set_instance(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1669 | { |
Quoc-Son Anh | cd58ad7 | 2012-02-21 16:50:53 -0700 | [diff] [blame] | 1670 | int instance, error; |
| 1671 | |
| 1672 | do { |
| 1673 | if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL)) |
| 1674 | return -ENODEV; |
| 1675 | |
| 1676 | spin_lock(&dev_list_lock); |
| 1677 | error = ida_get_new(&nvme_instance_ida, &instance); |
| 1678 | spin_unlock(&dev_list_lock); |
| 1679 | } while (error == -EAGAIN); |
| 1680 | |
| 1681 | if (error) |
| 1682 | return -ENODEV; |
| 1683 | |
| 1684 | dev->instance = instance; |
| 1685 | return 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | static void nvme_release_instance(struct nvme_dev *dev) |
| 1689 | { |
Quoc-Son Anh | cd58ad7 | 2012-02-21 16:50:53 -0700 | [diff] [blame] | 1690 | spin_lock(&dev_list_lock); |
| 1691 | ida_remove(&nvme_instance_ida, dev->instance); |
| 1692 | spin_unlock(&dev_list_lock); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1693 | } |
| 1694 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1695 | static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1696 | { |
Matthew Wilcox | 574e8b9 | 2011-02-01 16:24:35 -0500 | [diff] [blame] | 1697 | int bars, result = -ENOMEM; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1698 | struct nvme_dev *dev; |
| 1699 | |
| 1700 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 1701 | if (!dev) |
| 1702 | return -ENOMEM; |
| 1703 | dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry), |
| 1704 | GFP_KERNEL); |
| 1705 | if (!dev->entry) |
| 1706 | goto free; |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 1707 | dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *), |
| 1708 | GFP_KERNEL); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1709 | if (!dev->queues) |
| 1710 | goto free; |
| 1711 | |
Shane Michael Matthews | 0ee5a7d | 2011-02-01 08:49:30 -0500 | [diff] [blame] | 1712 | if (pci_enable_device_mem(pdev)) |
| 1713 | goto free; |
Matthew Wilcox | f64d336 | 2011-02-01 09:01:59 -0500 | [diff] [blame] | 1714 | pci_set_master(pdev); |
Matthew Wilcox | 574e8b9 | 2011-02-01 16:24:35 -0500 | [diff] [blame] | 1715 | bars = pci_select_bars(pdev, IORESOURCE_MEM); |
| 1716 | if (pci_request_selected_regions(pdev, bars, "nvme")) |
| 1717 | goto disable; |
Shane Michael Matthews | 0ee5a7d | 2011-02-01 08:49:30 -0500 | [diff] [blame] | 1718 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1719 | INIT_LIST_HEAD(&dev->namespaces); |
| 1720 | dev->pci_dev = pdev; |
| 1721 | pci_set_drvdata(pdev, dev); |
Matthew Wilcox | 2930353 | 2011-02-01 16:23:39 -0500 | [diff] [blame] | 1722 | dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)); |
| 1723 | dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64)); |
Quoc-Son Anh | cd58ad7 | 2012-02-21 16:50:53 -0700 | [diff] [blame] | 1724 | result = nvme_set_instance(dev); |
| 1725 | if (result) |
| 1726 | goto disable; |
| 1727 | |
Matthew Wilcox | 53c9577 | 2011-01-20 13:42:34 -0500 | [diff] [blame] | 1728 | dev->entry[0].vector = pdev->irq; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1729 | |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 1730 | result = nvme_setup_prp_pools(dev); |
| 1731 | if (result) |
| 1732 | goto disable_msix; |
| 1733 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1734 | dev->bar = ioremap(pci_resource_start(pdev, 0), 8192); |
| 1735 | if (!dev->bar) { |
| 1736 | result = -ENOMEM; |
Matthew Wilcox | 574e8b9 | 2011-02-01 16:24:35 -0500 | [diff] [blame] | 1737 | goto disable_msix; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | result = nvme_configure_admin_queue(dev); |
| 1741 | if (result) |
| 1742 | goto unmap; |
| 1743 | dev->queue_count++; |
| 1744 | |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1745 | spin_lock(&dev_list_lock); |
| 1746 | list_add(&dev->node, &dev_list); |
| 1747 | spin_unlock(&dev_list_lock); |
| 1748 | |
Matthew Wilcox | 740216f | 2011-02-15 16:28:20 -0500 | [diff] [blame] | 1749 | result = nvme_dev_add(dev); |
| 1750 | if (result) |
| 1751 | goto delete; |
| 1752 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1753 | return 0; |
| 1754 | |
| 1755 | delete: |
Matthew Wilcox | 740216f | 2011-02-15 16:28:20 -0500 | [diff] [blame] | 1756 | spin_lock(&dev_list_lock); |
| 1757 | list_del(&dev->node); |
| 1758 | spin_unlock(&dev_list_lock); |
| 1759 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1760 | nvme_free_queues(dev); |
| 1761 | unmap: |
| 1762 | iounmap(dev->bar); |
Matthew Wilcox | 574e8b9 | 2011-02-01 16:24:35 -0500 | [diff] [blame] | 1763 | disable_msix: |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1764 | pci_disable_msix(pdev); |
| 1765 | nvme_release_instance(dev); |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 1766 | nvme_release_prp_pools(dev); |
Matthew Wilcox | 574e8b9 | 2011-02-01 16:24:35 -0500 | [diff] [blame] | 1767 | disable: |
Shane Michael Matthews | 0ee5a7d | 2011-02-01 08:49:30 -0500 | [diff] [blame] | 1768 | pci_disable_device(pdev); |
Matthew Wilcox | 574e8b9 | 2011-02-01 16:24:35 -0500 | [diff] [blame] | 1769 | pci_release_regions(pdev); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1770 | free: |
| 1771 | kfree(dev->queues); |
| 1772 | kfree(dev->entry); |
| 1773 | kfree(dev); |
| 1774 | return result; |
| 1775 | } |
| 1776 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1777 | static void nvme_remove(struct pci_dev *pdev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1778 | { |
| 1779 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
| 1780 | nvme_dev_remove(dev); |
| 1781 | pci_disable_msix(pdev); |
| 1782 | iounmap(dev->bar); |
| 1783 | nvme_release_instance(dev); |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 1784 | nvme_release_prp_pools(dev); |
Shane Michael Matthews | 0ee5a7d | 2011-02-01 08:49:30 -0500 | [diff] [blame] | 1785 | pci_disable_device(pdev); |
Matthew Wilcox | 574e8b9 | 2011-02-01 16:24:35 -0500 | [diff] [blame] | 1786 | pci_release_regions(pdev); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1787 | kfree(dev->queues); |
| 1788 | kfree(dev->entry); |
| 1789 | kfree(dev); |
| 1790 | } |
| 1791 | |
| 1792 | /* These functions are yet to be implemented */ |
| 1793 | #define nvme_error_detected NULL |
| 1794 | #define nvme_dump_registers NULL |
| 1795 | #define nvme_link_reset NULL |
| 1796 | #define nvme_slot_reset NULL |
| 1797 | #define nvme_error_resume NULL |
| 1798 | #define nvme_suspend NULL |
| 1799 | #define nvme_resume NULL |
| 1800 | |
Stephen Hemminger | 1d35203 | 2012-09-07 09:33:17 -0700 | [diff] [blame] | 1801 | static const struct pci_error_handlers nvme_err_handler = { |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1802 | .error_detected = nvme_error_detected, |
| 1803 | .mmio_enabled = nvme_dump_registers, |
| 1804 | .link_reset = nvme_link_reset, |
| 1805 | .slot_reset = nvme_slot_reset, |
| 1806 | .resume = nvme_error_resume, |
| 1807 | }; |
| 1808 | |
| 1809 | /* Move to pci_ids.h later */ |
| 1810 | #define PCI_CLASS_STORAGE_EXPRESS 0x010802 |
| 1811 | |
| 1812 | static DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = { |
| 1813 | { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) }, |
| 1814 | { 0, } |
| 1815 | }; |
| 1816 | MODULE_DEVICE_TABLE(pci, nvme_id_table); |
| 1817 | |
| 1818 | static struct pci_driver nvme_driver = { |
| 1819 | .name = "nvme", |
| 1820 | .id_table = nvme_id_table, |
| 1821 | .probe = nvme_probe, |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1822 | .remove = nvme_remove, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1823 | .suspend = nvme_suspend, |
| 1824 | .resume = nvme_resume, |
| 1825 | .err_handler = &nvme_err_handler, |
| 1826 | }; |
| 1827 | |
| 1828 | static int __init nvme_init(void) |
| 1829 | { |
Matthew Wilcox | 0ac1314 | 2012-07-31 13:31:15 -0400 | [diff] [blame] | 1830 | int result; |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1831 | |
| 1832 | nvme_thread = kthread_run(nvme_kthread, NULL, "nvme"); |
| 1833 | if (IS_ERR(nvme_thread)) |
| 1834 | return PTR_ERR(nvme_thread); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1835 | |
Keith Busch | 5c42ea1 | 2012-07-25 16:05:18 -0600 | [diff] [blame] | 1836 | result = register_blkdev(nvme_major, "nvme"); |
| 1837 | if (result < 0) |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1838 | goto kill_kthread; |
Keith Busch | 5c42ea1 | 2012-07-25 16:05:18 -0600 | [diff] [blame] | 1839 | else if (result > 0) |
Matthew Wilcox | 0ac1314 | 2012-07-31 13:31:15 -0400 | [diff] [blame] | 1840 | nvme_major = result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1841 | |
| 1842 | result = pci_register_driver(&nvme_driver); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1843 | if (result) |
| 1844 | goto unregister_blkdev; |
| 1845 | return 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1846 | |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1847 | unregister_blkdev: |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1848 | unregister_blkdev(nvme_major, "nvme"); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1849 | kill_kthread: |
| 1850 | kthread_stop(nvme_thread); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1851 | return result; |
| 1852 | } |
| 1853 | |
| 1854 | static void __exit nvme_exit(void) |
| 1855 | { |
| 1856 | pci_unregister_driver(&nvme_driver); |
| 1857 | unregister_blkdev(nvme_major, "nvme"); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1858 | kthread_stop(nvme_thread); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>"); |
| 1862 | MODULE_LICENSE("GPL"); |
Matthew Wilcox | 366e821 | 2012-01-10 16:30:15 -0500 | [diff] [blame] | 1863 | MODULE_VERSION("0.8"); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1864 | module_init(nvme_init); |
| 1865 | module_exit(nvme_exit); |