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