Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1 | /* |
| 2 | * NVM Express device driver |
Matthew Wilcox | 6eb0d69 | 2014-03-24 10:11:22 -0400 | [diff] [blame] | 3 | * Copyright (c) 2011-2014, Intel Corporation. |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 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> |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 23 | #include <linux/cpu.h> |
Matthew Wilcox | fd63e9ce | 2011-05-06 08:37:54 -0400 | [diff] [blame] | 24 | #include <linux/delay.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 25 | #include <linux/errno.h> |
| 26 | #include <linux/fs.h> |
| 27 | #include <linux/genhd.h> |
Matthew Wilcox | 5aff938 | 2011-05-06 08:45:47 -0400 | [diff] [blame] | 28 | #include <linux/idr.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 29 | #include <linux/init.h> |
| 30 | #include <linux/interrupt.h> |
| 31 | #include <linux/io.h> |
| 32 | #include <linux/kdev_t.h> |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 33 | #include <linux/kthread.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 34 | #include <linux/kernel.h> |
| 35 | #include <linux/mm.h> |
| 36 | #include <linux/module.h> |
| 37 | #include <linux/moduleparam.h> |
| 38 | #include <linux/pci.h> |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 39 | #include <linux/percpu.h> |
Matthew Wilcox | be7b627 | 2011-02-06 07:53:23 -0500 | [diff] [blame] | 40 | #include <linux/poison.h> |
Matthew Wilcox | c3bfe71 | 2013-07-08 17:26:25 -0400 | [diff] [blame] | 41 | #include <linux/ptrace.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 42 | #include <linux/sched.h> |
| 43 | #include <linux/slab.h> |
| 44 | #include <linux/types.h> |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 45 | #include <scsi/sg.h> |
Hitoshi Mitake | 797a796 | 2012-02-07 11:45:33 +0900 | [diff] [blame] | 46 | #include <asm-generic/io-64-nonatomic-lo-hi.h> |
| 47 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 48 | #define NVME_Q_DEPTH 1024 |
| 49 | #define SQ_SIZE(depth) (depth * sizeof(struct nvme_command)) |
| 50 | #define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion)) |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 51 | #define ADMIN_TIMEOUT (60 * HZ) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 52 | |
Keith Busch | b355084 | 2014-04-04 11:43:36 -0600 | [diff] [blame^] | 53 | unsigned char io_timeout = 30; |
| 54 | module_param(io_timeout, byte, 0644); |
| 55 | MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O"); |
| 56 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 57 | static int nvme_major; |
| 58 | module_param(nvme_major, int, 0); |
| 59 | |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 60 | static int use_threaded_interrupts; |
| 61 | module_param(use_threaded_interrupts, int, 0); |
| 62 | |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 63 | static DEFINE_SPINLOCK(dev_list_lock); |
| 64 | static LIST_HEAD(dev_list); |
| 65 | static struct task_struct *nvme_thread; |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 66 | static struct workqueue_struct *nvme_workq; |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 67 | |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 68 | static void nvme_reset_failed_dev(struct work_struct *ws); |
| 69 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 70 | struct async_cmd_info { |
| 71 | struct kthread_work work; |
| 72 | struct kthread_worker *worker; |
| 73 | u32 result; |
| 74 | int status; |
| 75 | void *ctx; |
| 76 | }; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 77 | |
| 78 | /* |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 79 | * An NVM Express queue. Each device has at least two (one for admin |
| 80 | * commands and one for I/O commands). |
| 81 | */ |
| 82 | struct nvme_queue { |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 83 | struct rcu_head r_head; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 84 | struct device *q_dmadev; |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 85 | struct nvme_dev *dev; |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 86 | char irqname[24]; /* nvme4294967295-65535\0 */ |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 87 | spinlock_t q_lock; |
| 88 | struct nvme_command *sq_cmds; |
| 89 | volatile struct nvme_completion *cqes; |
| 90 | dma_addr_t sq_dma_addr; |
| 91 | dma_addr_t cq_dma_addr; |
| 92 | wait_queue_head_t sq_full; |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 93 | wait_queue_t sq_cong_wait; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 94 | struct bio_list sq_cong; |
| 95 | u32 __iomem *q_db; |
| 96 | u16 q_depth; |
| 97 | u16 cq_vector; |
| 98 | u16 sq_head; |
| 99 | u16 sq_tail; |
| 100 | u16 cq_head; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 101 | u16 qid; |
Matthew Wilcox | e9539f4 | 2013-06-24 11:47:34 -0400 | [diff] [blame] | 102 | u8 cq_phase; |
| 103 | u8 cqe_seen; |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 104 | u8 q_suspended; |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 105 | cpumask_var_t cpu_mask; |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 106 | struct async_cmd_info cmdinfo; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 107 | unsigned long cmdid_data[]; |
| 108 | }; |
| 109 | |
| 110 | /* |
| 111 | * Check we didin't inadvertently grow the command struct |
| 112 | */ |
| 113 | static inline void _nvme_check_size(void) |
| 114 | { |
| 115 | BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64); |
| 116 | BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64); |
| 117 | BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64); |
| 118 | BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64); |
| 119 | BUILD_BUG_ON(sizeof(struct nvme_features) != 64); |
Vishal Verma | f8ebf84 | 2013-03-27 07:13:41 -0400 | [diff] [blame] | 120 | BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64); |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 121 | BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 122 | BUILD_BUG_ON(sizeof(struct nvme_command) != 64); |
| 123 | BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096); |
| 124 | BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096); |
| 125 | BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64); |
Keith Busch | 6ecec74 | 2012-09-26 12:49:27 -0600 | [diff] [blame] | 126 | BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 127 | } |
| 128 | |
Matthew Wilcox | 5c1281a | 2011-12-20 11:54:53 -0500 | [diff] [blame] | 129 | typedef void (*nvme_completion_fn)(struct nvme_dev *, void *, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 130 | struct nvme_completion *); |
| 131 | |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 132 | struct nvme_cmd_info { |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 133 | nvme_completion_fn fn; |
| 134 | void *ctx; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 135 | unsigned long timeout; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 136 | int aborted; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq) |
| 140 | { |
| 141 | return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)]; |
| 142 | } |
| 143 | |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 144 | static unsigned nvme_queue_extra(int depth) |
| 145 | { |
| 146 | return DIV_ROUND_UP(depth, 8) + (depth * sizeof(struct nvme_cmd_info)); |
| 147 | } |
| 148 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 149 | /** |
Matthew Wilcox | 714a7a2 | 2011-03-16 16:28:24 -0400 | [diff] [blame] | 150 | * alloc_cmdid() - Allocate a Command ID |
| 151 | * @nvmeq: The queue that will be used for this command |
| 152 | * @ctx: A pointer that will be passed to the handler |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 153 | * @handler: The function to call on completion |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 154 | * |
| 155 | * Allocate a Command ID for a queue. The data passed in will |
| 156 | * be passed to the completion handler. This is implemented by using |
| 157 | * the bottom two bits of the ctx pointer to store the handler ID. |
| 158 | * Passing in a pointer that's not 4-byte aligned will cause a BUG. |
| 159 | * We can change this if it becomes a problem. |
Matthew Wilcox | 184d294 | 2011-05-11 21:36:38 -0400 | [diff] [blame] | 160 | * |
| 161 | * May be called with local interrupts disabled and the q_lock held, |
| 162 | * or with interrupts enabled and no locks held. |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 163 | */ |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 164 | static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx, |
| 165 | nvme_completion_fn handler, unsigned timeout) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 166 | { |
Matthew Wilcox | e6d15f7 | 2011-02-24 08:49:41 -0500 | [diff] [blame] | 167 | int depth = nvmeq->q_depth - 1; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 168 | struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 169 | int cmdid; |
| 170 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 171 | do { |
| 172 | cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth); |
| 173 | if (cmdid >= depth) |
| 174 | return -EBUSY; |
| 175 | } while (test_and_set_bit(cmdid, nvmeq->cmdid_data)); |
| 176 | |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 177 | info[cmdid].fn = handler; |
| 178 | info[cmdid].ctx = ctx; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 179 | info[cmdid].timeout = jiffies + timeout; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 180 | info[cmdid].aborted = 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 181 | return cmdid; |
| 182 | } |
| 183 | |
| 184 | static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 185 | nvme_completion_fn handler, unsigned timeout) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 186 | { |
| 187 | int cmdid; |
| 188 | wait_event_killable(nvmeq->sq_full, |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 189 | (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 190 | return (cmdid < 0) ? -EINTR : cmdid; |
| 191 | } |
| 192 | |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 193 | /* Special values must be less than 0x1000 */ |
| 194 | #define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA) |
Matthew Wilcox | d2d8703 | 2011-02-07 15:55:59 -0500 | [diff] [blame] | 195 | #define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE) |
| 196 | #define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE) |
| 197 | #define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE) |
Matthew Wilcox | 00df5cb | 2011-02-22 14:18:30 -0500 | [diff] [blame] | 198 | #define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE) |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 199 | #define CMD_CTX_ABORT (0x31C + CMD_CTX_BASE) |
Matthew Wilcox | be7b627 | 2011-02-06 07:53:23 -0500 | [diff] [blame] | 200 | |
Matthew Wilcox | 5c1281a | 2011-12-20 11:54:53 -0500 | [diff] [blame] | 201 | static void special_completion(struct nvme_dev *dev, void *ctx, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 202 | struct nvme_completion *cqe) |
| 203 | { |
| 204 | if (ctx == CMD_CTX_CANCELLED) |
| 205 | return; |
| 206 | if (ctx == CMD_CTX_FLUSH) |
| 207 | return; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 208 | if (ctx == CMD_CTX_ABORT) { |
| 209 | ++dev->abort_limit; |
| 210 | return; |
| 211 | } |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 212 | if (ctx == CMD_CTX_COMPLETED) { |
Matthew Wilcox | 5c1281a | 2011-12-20 11:54:53 -0500 | [diff] [blame] | 213 | dev_warn(&dev->pci_dev->dev, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 214 | "completed id %d twice on queue %d\n", |
| 215 | cqe->command_id, le16_to_cpup(&cqe->sq_id)); |
| 216 | return; |
| 217 | } |
| 218 | if (ctx == CMD_CTX_INVALID) { |
Matthew Wilcox | 5c1281a | 2011-12-20 11:54:53 -0500 | [diff] [blame] | 219 | dev_warn(&dev->pci_dev->dev, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 220 | "invalid id %d completed on queue %d\n", |
| 221 | cqe->command_id, le16_to_cpup(&cqe->sq_id)); |
| 222 | return; |
| 223 | } |
| 224 | |
Matthew Wilcox | 5c1281a | 2011-12-20 11:54:53 -0500 | [diff] [blame] | 225 | dev_warn(&dev->pci_dev->dev, "Unknown special completion %p\n", ctx); |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 226 | } |
| 227 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 228 | static void async_completion(struct nvme_dev *dev, void *ctx, |
| 229 | struct nvme_completion *cqe) |
| 230 | { |
| 231 | struct async_cmd_info *cmdinfo = ctx; |
| 232 | cmdinfo->result = le32_to_cpup(&cqe->result); |
| 233 | cmdinfo->status = le16_to_cpup(&cqe->status) >> 1; |
| 234 | queue_kthread_work(cmdinfo->worker, &cmdinfo->work); |
| 235 | } |
| 236 | |
Matthew Wilcox | 184d294 | 2011-05-11 21:36:38 -0400 | [diff] [blame] | 237 | /* |
| 238 | * Called with local interrupts disabled and the q_lock held. May not sleep. |
| 239 | */ |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 240 | static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid, |
| 241 | nvme_completion_fn *fn) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 242 | { |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 243 | void *ctx; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 244 | struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 245 | |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 246 | if (cmdid >= nvmeq->q_depth) { |
| 247 | *fn = special_completion; |
Matthew Wilcox | 48e3d39 | 2011-02-06 08:51:15 -0500 | [diff] [blame] | 248 | return CMD_CTX_INVALID; |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 249 | } |
Keith Busch | 859361a | 2012-08-02 14:05:59 -0600 | [diff] [blame] | 250 | if (fn) |
| 251 | *fn = info[cmdid].fn; |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 252 | ctx = info[cmdid].ctx; |
| 253 | info[cmdid].fn = special_completion; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 254 | info[cmdid].ctx = CMD_CTX_COMPLETED; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 255 | clear_bit(cmdid, nvmeq->cmdid_data); |
| 256 | wake_up(&nvmeq->sq_full); |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 257 | return ctx; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 258 | } |
| 259 | |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 260 | static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid, |
| 261 | nvme_completion_fn *fn) |
Matthew Wilcox | 3c0cf13 | 2011-02-04 16:03:56 -0500 | [diff] [blame] | 262 | { |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 263 | void *ctx; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 264 | struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 265 | if (fn) |
| 266 | *fn = info[cmdid].fn; |
| 267 | ctx = info[cmdid].ctx; |
| 268 | info[cmdid].fn = special_completion; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 269 | info[cmdid].ctx = CMD_CTX_CANCELLED; |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 270 | return ctx; |
Matthew Wilcox | 3c0cf13 | 2011-02-04 16:03:56 -0500 | [diff] [blame] | 271 | } |
| 272 | |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 273 | static struct nvme_queue *raw_nvmeq(struct nvme_dev *dev, int qid) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 274 | { |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 275 | return rcu_dereference_raw(dev->queues[qid]); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 276 | } |
| 277 | |
Keith Busch | 4f5099a | 2014-03-03 16:39:13 -0700 | [diff] [blame] | 278 | static struct nvme_queue *get_nvmeq(struct nvme_dev *dev) __acquires(RCU) |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 279 | { |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 280 | unsigned queue_id = get_cpu_var(*dev->io_queue); |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 281 | rcu_read_lock(); |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 282 | return rcu_dereference(dev->queues[queue_id]); |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Keith Busch | 4f5099a | 2014-03-03 16:39:13 -0700 | [diff] [blame] | 285 | static void put_nvmeq(struct nvme_queue *nvmeq) __releases(RCU) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 286 | { |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 287 | rcu_read_unlock(); |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 288 | put_cpu_var(nvmeq->dev->io_queue); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 289 | } |
| 290 | |
Keith Busch | 4f5099a | 2014-03-03 16:39:13 -0700 | [diff] [blame] | 291 | static struct nvme_queue *lock_nvmeq(struct nvme_dev *dev, int q_idx) |
| 292 | __acquires(RCU) |
| 293 | { |
| 294 | rcu_read_lock(); |
| 295 | return rcu_dereference(dev->queues[q_idx]); |
| 296 | } |
| 297 | |
| 298 | static void unlock_nvmeq(struct nvme_queue *nvmeq) __releases(RCU) |
| 299 | { |
| 300 | rcu_read_unlock(); |
| 301 | } |
| 302 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 303 | /** |
Matthew Wilcox | 714a7a2 | 2011-03-16 16:28:24 -0400 | [diff] [blame] | 304 | * 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] | 305 | * @nvmeq: The queue to use |
| 306 | * @cmd: The command to send |
| 307 | * |
| 308 | * Safe to use from interrupt context |
| 309 | */ |
| 310 | static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd) |
| 311 | { |
| 312 | unsigned long flags; |
| 313 | u16 tail; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 314 | spin_lock_irqsave(&nvmeq->q_lock, flags); |
Keith Busch | 4f5099a | 2014-03-03 16:39:13 -0700 | [diff] [blame] | 315 | if (nvmeq->q_suspended) { |
| 316 | spin_unlock_irqrestore(&nvmeq->q_lock, flags); |
| 317 | return -EBUSY; |
| 318 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 319 | tail = nvmeq->sq_tail; |
| 320 | memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd)); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 321 | if (++tail == nvmeq->q_depth) |
| 322 | tail = 0; |
Matthew Wilcox | 7547881 | 2011-02-16 09:59:59 -0500 | [diff] [blame] | 323 | writel(tail, nvmeq->q_db); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 324 | nvmeq->sq_tail = tail; |
| 325 | spin_unlock_irqrestore(&nvmeq->q_lock, flags); |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 330 | static __le64 **iod_list(struct nvme_iod *iod) |
| 331 | { |
| 332 | return ((void *)iod) + iod->offset; |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Will slightly overestimate the number of pages needed. This is OK |
| 337 | * as it only leads to a small amount of wasted memory for the lifetime of |
| 338 | * the I/O. |
| 339 | */ |
| 340 | static int nvme_npages(unsigned size) |
| 341 | { |
| 342 | unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE); |
| 343 | return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8); |
| 344 | } |
| 345 | |
| 346 | static struct nvme_iod * |
| 347 | nvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp) |
| 348 | { |
| 349 | struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) + |
| 350 | sizeof(__le64 *) * nvme_npages(nbytes) + |
| 351 | sizeof(struct scatterlist) * nseg, gfp); |
| 352 | |
| 353 | if (iod) { |
| 354 | iod->offset = offsetof(struct nvme_iod, sg[nseg]); |
| 355 | iod->npages = -1; |
| 356 | iod->length = nbytes; |
Keith Busch | 2b19603 | 2012-11-06 11:59:23 -0700 | [diff] [blame] | 357 | iod->nents = 0; |
Keith Busch | 6198221 | 2013-05-29 15:59:39 -0600 | [diff] [blame] | 358 | iod->start_time = jiffies; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | return iod; |
| 362 | } |
| 363 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 364 | 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] | 365 | { |
| 366 | const int last_prp = PAGE_SIZE / 8 - 1; |
| 367 | int i; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 368 | __le64 **list = iod_list(iod); |
| 369 | dma_addr_t prp_dma = iod->first_dma; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 370 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 371 | if (iod->npages == 0) |
| 372 | dma_pool_free(dev->prp_small_pool, list[0], prp_dma); |
| 373 | for (i = 0; i < iod->npages; i++) { |
| 374 | __le64 *prp_list = list[i]; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 375 | 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] | 376 | dma_pool_free(dev->prp_page_pool, prp_list, prp_dma); |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 377 | prp_dma = next_prp_dma; |
| 378 | } |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 379 | kfree(iod); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 380 | } |
| 381 | |
Keith Busch | 6198221 | 2013-05-29 15:59:39 -0600 | [diff] [blame] | 382 | static void nvme_start_io_acct(struct bio *bio) |
| 383 | { |
| 384 | struct gendisk *disk = bio->bi_bdev->bd_disk; |
| 385 | const int rw = bio_data_dir(bio); |
| 386 | int cpu = part_stat_lock(); |
| 387 | part_round_stats(cpu, &disk->part0); |
| 388 | part_stat_inc(cpu, &disk->part0, ios[rw]); |
| 389 | part_stat_add(cpu, &disk->part0, sectors[rw], bio_sectors(bio)); |
| 390 | part_inc_in_flight(&disk->part0, rw); |
| 391 | part_stat_unlock(); |
| 392 | } |
| 393 | |
| 394 | static void nvme_end_io_acct(struct bio *bio, unsigned long start_time) |
| 395 | { |
| 396 | struct gendisk *disk = bio->bi_bdev->bd_disk; |
| 397 | const int rw = bio_data_dir(bio); |
| 398 | unsigned long duration = jiffies - start_time; |
| 399 | int cpu = part_stat_lock(); |
| 400 | part_stat_add(cpu, &disk->part0, ticks[rw], duration); |
| 401 | part_round_stats(cpu, &disk->part0); |
| 402 | part_dec_in_flight(&disk->part0, rw); |
| 403 | part_stat_unlock(); |
| 404 | } |
| 405 | |
Matthew Wilcox | 5c1281a | 2011-12-20 11:54:53 -0500 | [diff] [blame] | 406 | static void bio_completion(struct nvme_dev *dev, void *ctx, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 407 | struct nvme_completion *cqe) |
| 408 | { |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 409 | struct nvme_iod *iod = ctx; |
| 410 | struct bio *bio = iod->private; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 411 | u16 status = le16_to_cpup(&cqe->status) >> 1; |
| 412 | |
Keith Busch | 9e59d09 | 2013-08-08 10:25:38 -0600 | [diff] [blame] | 413 | if (iod->nents) { |
Keith Busch | 2b19603 | 2012-11-06 11:59:23 -0700 | [diff] [blame] | 414 | dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 415 | bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
Keith Busch | 9e59d09 | 2013-08-08 10:25:38 -0600 | [diff] [blame] | 416 | nvme_end_io_acct(bio, iod->start_time); |
| 417 | } |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 418 | nvme_free_iod(dev, iod); |
Keith Busch | 427e970 | 2013-04-09 11:59:32 -0600 | [diff] [blame] | 419 | if (status) |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 420 | bio_endio(bio, -EIO); |
Keith Busch | 427e970 | 2013-04-09 11:59:32 -0600 | [diff] [blame] | 421 | else |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 422 | bio_endio(bio, 0); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 423 | } |
| 424 | |
Matthew Wilcox | 184d294 | 2011-05-11 21:36:38 -0400 | [diff] [blame] | 425 | /* length is in bytes. gfp flags indicates whether we may sleep. */ |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 426 | int nvme_setup_prps(struct nvme_dev *dev, struct nvme_common_command *cmd, |
| 427 | struct nvme_iod *iod, int total_len, gfp_t gfp) |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 428 | { |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 429 | struct dma_pool *pool; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 430 | int length = total_len; |
| 431 | struct scatterlist *sg = iod->sg; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 432 | int dma_len = sg_dma_len(sg); |
| 433 | u64 dma_addr = sg_dma_address(sg); |
| 434 | int offset = offset_in_page(dma_addr); |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 435 | __le64 *prp_list; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 436 | __le64 **list = iod_list(iod); |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 437 | dma_addr_t prp_dma; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 438 | int nprps, i; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 439 | |
| 440 | cmd->prp1 = cpu_to_le64(dma_addr); |
| 441 | length -= (PAGE_SIZE - offset); |
| 442 | if (length <= 0) |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 443 | return total_len; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 444 | |
| 445 | dma_len -= (PAGE_SIZE - offset); |
| 446 | if (dma_len) { |
| 447 | dma_addr += (PAGE_SIZE - offset); |
| 448 | } else { |
| 449 | sg = sg_next(sg); |
| 450 | dma_addr = sg_dma_address(sg); |
| 451 | dma_len = sg_dma_len(sg); |
| 452 | } |
| 453 | |
| 454 | if (length <= PAGE_SIZE) { |
| 455 | cmd->prp2 = cpu_to_le64(dma_addr); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 456 | return total_len; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 457 | } |
| 458 | |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 459 | nprps = DIV_ROUND_UP(length, PAGE_SIZE); |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 460 | if (nprps <= (256 / 8)) { |
| 461 | pool = dev->prp_small_pool; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 462 | iod->npages = 0; |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 463 | } else { |
| 464 | pool = dev->prp_page_pool; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 465 | iod->npages = 1; |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 466 | } |
| 467 | |
Matthew Wilcox | b77954c | 2011-05-12 13:51:41 -0400 | [diff] [blame] | 468 | prp_list = dma_pool_alloc(pool, gfp, &prp_dma); |
| 469 | if (!prp_list) { |
| 470 | cmd->prp2 = cpu_to_le64(dma_addr); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 471 | iod->npages = -1; |
| 472 | return (total_len - length) + PAGE_SIZE; |
Matthew Wilcox | b77954c | 2011-05-12 13:51:41 -0400 | [diff] [blame] | 473 | } |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 474 | list[0] = prp_list; |
| 475 | iod->first_dma = prp_dma; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 476 | cmd->prp2 = cpu_to_le64(prp_dma); |
| 477 | i = 0; |
| 478 | for (;;) { |
Matthew Wilcox | 7523d83 | 2011-03-16 16:43:40 -0400 | [diff] [blame] | 479 | if (i == PAGE_SIZE / 8) { |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 480 | __le64 *old_prp_list = prp_list; |
Matthew Wilcox | b77954c | 2011-05-12 13:51:41 -0400 | [diff] [blame] | 481 | prp_list = dma_pool_alloc(pool, gfp, &prp_dma); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 482 | if (!prp_list) |
| 483 | return total_len - length; |
| 484 | list[iod->npages++] = prp_list; |
Matthew Wilcox | 7523d83 | 2011-03-16 16:43:40 -0400 | [diff] [blame] | 485 | prp_list[0] = old_prp_list[i - 1]; |
| 486 | old_prp_list[i - 1] = cpu_to_le64(prp_dma); |
| 487 | i = 1; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 488 | } |
| 489 | prp_list[i++] = cpu_to_le64(dma_addr); |
| 490 | dma_len -= PAGE_SIZE; |
| 491 | dma_addr += PAGE_SIZE; |
| 492 | length -= PAGE_SIZE; |
| 493 | if (length <= 0) |
| 494 | break; |
| 495 | if (dma_len > 0) |
| 496 | continue; |
| 497 | BUG_ON(dma_len < 0); |
| 498 | sg = sg_next(sg); |
| 499 | dma_addr = sg_dma_address(sg); |
| 500 | dma_len = sg_dma_len(sg); |
| 501 | } |
| 502 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 503 | return total_len; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 504 | } |
| 505 | |
Keith Busch | 427e970 | 2013-04-09 11:59:32 -0600 | [diff] [blame] | 506 | static int nvme_split_and_submit(struct bio *bio, struct nvme_queue *nvmeq, |
Kent Overstreet | 20d0189 | 2013-11-23 18:21:01 -0800 | [diff] [blame] | 507 | int len) |
Keith Busch | 427e970 | 2013-04-09 11:59:32 -0600 | [diff] [blame] | 508 | { |
Kent Overstreet | 20d0189 | 2013-11-23 18:21:01 -0800 | [diff] [blame] | 509 | struct bio *split = bio_split(bio, len >> 9, GFP_ATOMIC, NULL); |
| 510 | if (!split) |
Keith Busch | 427e970 | 2013-04-09 11:59:32 -0600 | [diff] [blame] | 511 | return -ENOMEM; |
| 512 | |
Kent Overstreet | 20d0189 | 2013-11-23 18:21:01 -0800 | [diff] [blame] | 513 | bio_chain(split, bio); |
| 514 | |
Keith Busch | 427e970 | 2013-04-09 11:59:32 -0600 | [diff] [blame] | 515 | if (bio_list_empty(&nvmeq->sq_cong)) |
| 516 | add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait); |
Kent Overstreet | 20d0189 | 2013-11-23 18:21:01 -0800 | [diff] [blame] | 517 | bio_list_add(&nvmeq->sq_cong, split); |
| 518 | bio_list_add(&nvmeq->sq_cong, bio); |
Keith Busch | 427e970 | 2013-04-09 11:59:32 -0600 | [diff] [blame] | 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 523 | /* NVMe scatterlists require no holes in the virtual address */ |
| 524 | #define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \ |
| 525 | (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE)) |
| 526 | |
Keith Busch | 427e970 | 2013-04-09 11:59:32 -0600 | [diff] [blame] | 527 | 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] | 528 | struct bio *bio, enum dma_data_direction dma_dir, int psegs) |
| 529 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 530 | struct bio_vec bvec, bvprv; |
| 531 | struct bvec_iter iter; |
Matthew Wilcox | 7683084 | 2011-02-10 13:55:39 -0500 | [diff] [blame] | 532 | struct scatterlist *sg = NULL; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 533 | int length = 0, nsegs = 0, split_len = bio->bi_iter.bi_size; |
| 534 | int first = 1; |
Keith Busch | 159b67d | 2013-04-09 17:13:20 -0600 | [diff] [blame] | 535 | |
| 536 | if (nvmeq->dev->stripe_size) |
| 537 | split_len = nvmeq->dev->stripe_size - |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 538 | ((bio->bi_iter.bi_sector << 9) & |
| 539 | (nvmeq->dev->stripe_size - 1)); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 540 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 541 | sg_init_table(iod->sg, psegs); |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 542 | bio_for_each_segment(bvec, bio, iter) { |
| 543 | if (!first && BIOVEC_PHYS_MERGEABLE(&bvprv, &bvec)) { |
| 544 | sg->length += bvec.bv_len; |
Matthew Wilcox | 7683084 | 2011-02-10 13:55:39 -0500 | [diff] [blame] | 545 | } else { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 546 | if (!first && BIOVEC_NOT_VIRT_MERGEABLE(&bvprv, &bvec)) |
| 547 | return nvme_split_and_submit(bio, nvmeq, |
Kent Overstreet | 20d0189 | 2013-11-23 18:21:01 -0800 | [diff] [blame] | 548 | length); |
Keith Busch | 427e970 | 2013-04-09 11:59:32 -0600 | [diff] [blame] | 549 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 550 | sg = sg ? sg + 1 : iod->sg; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 551 | sg_set_page(sg, bvec.bv_page, |
| 552 | bvec.bv_len, bvec.bv_offset); |
Matthew Wilcox | 7683084 | 2011-02-10 13:55:39 -0500 | [diff] [blame] | 553 | nsegs++; |
| 554 | } |
Keith Busch | 159b67d | 2013-04-09 17:13:20 -0600 | [diff] [blame] | 555 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 556 | if (split_len - length < bvec.bv_len) |
Kent Overstreet | 20d0189 | 2013-11-23 18:21:01 -0800 | [diff] [blame] | 557 | return nvme_split_and_submit(bio, nvmeq, split_len); |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 558 | length += bvec.bv_len; |
Matthew Wilcox | 7683084 | 2011-02-10 13:55:39 -0500 | [diff] [blame] | 559 | bvprv = bvec; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 560 | first = 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 561 | } |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 562 | iod->nents = nsegs; |
Matthew Wilcox | 7683084 | 2011-02-10 13:55:39 -0500 | [diff] [blame] | 563 | sg_mark_end(sg); |
Keith Busch | 427e970 | 2013-04-09 11:59:32 -0600 | [diff] [blame] | 564 | 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] | 565 | return -ENOMEM; |
Keith Busch | 427e970 | 2013-04-09 11:59:32 -0600 | [diff] [blame] | 566 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 567 | BUG_ON(length != bio->bi_iter.bi_size); |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 568 | return length; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 569 | } |
| 570 | |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame] | 571 | /* |
| 572 | * We reuse the small pool to allocate the 16-byte range here as it is not |
| 573 | * worth having a special pool for these or additional cases to handle freeing |
| 574 | * the iod. |
| 575 | */ |
| 576 | static int nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns, |
| 577 | struct bio *bio, struct nvme_iod *iod, int cmdid) |
| 578 | { |
| 579 | struct nvme_dsm_range *range; |
| 580 | struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; |
| 581 | |
| 582 | range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC, |
| 583 | &iod->first_dma); |
| 584 | if (!range) |
| 585 | return -ENOMEM; |
| 586 | |
| 587 | iod_list(iod)[0] = (__le64 *)range; |
| 588 | iod->npages = 0; |
| 589 | |
| 590 | range->cattr = cpu_to_le32(0); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 591 | range->nlb = cpu_to_le32(bio->bi_iter.bi_size >> ns->lba_shift); |
| 592 | range->slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector)); |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame] | 593 | |
| 594 | memset(cmnd, 0, sizeof(*cmnd)); |
| 595 | cmnd->dsm.opcode = nvme_cmd_dsm; |
| 596 | cmnd->dsm.command_id = cmdid; |
| 597 | cmnd->dsm.nsid = cpu_to_le32(ns->ns_id); |
| 598 | cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma); |
| 599 | cmnd->dsm.nr = 0; |
| 600 | cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD); |
| 601 | |
| 602 | if (++nvmeq->sq_tail == nvmeq->q_depth) |
| 603 | nvmeq->sq_tail = 0; |
| 604 | writel(nvmeq->sq_tail, nvmeq->q_db); |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
Matthew Wilcox | 00df5cb | 2011-02-22 14:18:30 -0500 | [diff] [blame] | 609 | static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns, |
| 610 | int cmdid) |
| 611 | { |
| 612 | struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; |
| 613 | |
| 614 | memset(cmnd, 0, sizeof(*cmnd)); |
| 615 | cmnd->common.opcode = nvme_cmd_flush; |
| 616 | cmnd->common.command_id = cmdid; |
| 617 | cmnd->common.nsid = cpu_to_le32(ns->ns_id); |
| 618 | |
| 619 | if (++nvmeq->sq_tail == nvmeq->q_depth) |
| 620 | nvmeq->sq_tail = 0; |
| 621 | writel(nvmeq->sq_tail, nvmeq->q_db); |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 626 | 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] | 627 | { |
| 628 | int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH, |
Matthew Wilcox | ff976d7 | 2011-12-20 13:53:01 -0500 | [diff] [blame] | 629 | special_completion, NVME_IO_TIMEOUT); |
Matthew Wilcox | 00df5cb | 2011-02-22 14:18:30 -0500 | [diff] [blame] | 630 | if (unlikely(cmdid < 0)) |
| 631 | return cmdid; |
| 632 | |
| 633 | return nvme_submit_flush(nvmeq, ns, cmdid); |
| 634 | } |
| 635 | |
Matthew Wilcox | 184d294 | 2011-05-11 21:36:38 -0400 | [diff] [blame] | 636 | /* |
| 637 | * Called with local interrupts disabled and the q_lock held. May not sleep. |
| 638 | */ |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 639 | static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns, |
| 640 | struct bio *bio) |
| 641 | { |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 642 | struct nvme_command *cmnd; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 643 | struct nvme_iod *iod; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 644 | enum dma_data_direction dma_dir; |
Wei Yongjun | 1287dab | 2013-05-13 22:29:04 +0800 | [diff] [blame] | 645 | int cmdid, length, result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 646 | u16 control; |
| 647 | u32 dsmgmt; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 648 | int psegs = bio_phys_segments(ns->queue, bio); |
| 649 | |
Matthew Wilcox | 00df5cb | 2011-02-22 14:18:30 -0500 | [diff] [blame] | 650 | if ((bio->bi_rw & REQ_FLUSH) && psegs) { |
| 651 | result = nvme_submit_flush_data(nvmeq, ns); |
| 652 | if (result) |
| 653 | return result; |
| 654 | } |
| 655 | |
Wei Yongjun | 1287dab | 2013-05-13 22:29:04 +0800 | [diff] [blame] | 656 | result = -ENOMEM; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 657 | iod = nvme_alloc_iod(psegs, bio->bi_iter.bi_size, GFP_ATOMIC); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 658 | if (!iod) |
Matthew Wilcox | eeee322 | 2011-02-14 15:55:33 -0500 | [diff] [blame] | 659 | goto nomem; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 660 | iod->private = bio; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 661 | |
Matthew Wilcox | eeee322 | 2011-02-14 15:55:33 -0500 | [diff] [blame] | 662 | result = -EBUSY; |
Matthew Wilcox | ff976d7 | 2011-12-20 13:53:01 -0500 | [diff] [blame] | 663 | cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 664 | if (unlikely(cmdid < 0)) |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 665 | goto free_iod; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 666 | |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame] | 667 | if (bio->bi_rw & REQ_DISCARD) { |
| 668 | result = nvme_submit_discard(nvmeq, ns, bio, iod, cmdid); |
| 669 | if (result) |
| 670 | goto free_cmdid; |
| 671 | return result; |
| 672 | } |
Matthew Wilcox | 00df5cb | 2011-02-22 14:18:30 -0500 | [diff] [blame] | 673 | if ((bio->bi_rw & REQ_FLUSH) && !psegs) |
| 674 | return nvme_submit_flush(nvmeq, ns, cmdid); |
| 675 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 676 | control = 0; |
| 677 | if (bio->bi_rw & REQ_FUA) |
| 678 | control |= NVME_RW_FUA; |
| 679 | if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD)) |
| 680 | control |= NVME_RW_LR; |
| 681 | |
| 682 | dsmgmt = 0; |
| 683 | if (bio->bi_rw & REQ_RAHEAD) |
| 684 | dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH; |
| 685 | |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 686 | cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 687 | |
Matthew Wilcox | b8deb62 | 2011-01-26 10:08:25 -0500 | [diff] [blame] | 688 | memset(cmnd, 0, sizeof(*cmnd)); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 689 | if (bio_data_dir(bio)) { |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 690 | cmnd->rw.opcode = nvme_cmd_write; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 691 | dma_dir = DMA_TO_DEVICE; |
| 692 | } else { |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 693 | cmnd->rw.opcode = nvme_cmd_read; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 694 | dma_dir = DMA_FROM_DEVICE; |
| 695 | } |
| 696 | |
Keith Busch | 427e970 | 2013-04-09 11:59:32 -0600 | [diff] [blame] | 697 | result = nvme_map_bio(nvmeq, iod, bio, dma_dir, psegs); |
| 698 | if (result <= 0) |
Keith Busch | 859361a | 2012-08-02 14:05:59 -0600 | [diff] [blame] | 699 | goto free_cmdid; |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 700 | length = result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 701 | |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 702 | cmnd->rw.command_id = cmdid; |
| 703 | cmnd->rw.nsid = cpu_to_le32(ns->ns_id); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 704 | length = nvme_setup_prps(nvmeq->dev, &cmnd->common, iod, length, |
| 705 | GFP_ATOMIC); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 706 | cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector)); |
Matthew Wilcox | 1ad2f89 | 2011-02-23 15:20:00 -0500 | [diff] [blame] | 707 | cmnd->rw.length = cpu_to_le16((length >> ns->lba_shift) - 1); |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 708 | cmnd->rw.control = cpu_to_le16(control); |
| 709 | cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 710 | |
Keith Busch | 6198221 | 2013-05-29 15:59:39 -0600 | [diff] [blame] | 711 | nvme_start_io_acct(bio); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 712 | if (++nvmeq->sq_tail == nvmeq->q_depth) |
| 713 | nvmeq->sq_tail = 0; |
Matthew Wilcox | 7547881 | 2011-02-16 09:59:59 -0500 | [diff] [blame] | 714 | writel(nvmeq->sq_tail, nvmeq->q_db); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 715 | |
Matthew Wilcox | 1974b1a | 2011-02-10 12:01:09 -0500 | [diff] [blame] | 716 | return 0; |
| 717 | |
Keith Busch | 859361a | 2012-08-02 14:05:59 -0600 | [diff] [blame] | 718 | free_cmdid: |
| 719 | free_cmdid(nvmeq, cmdid, NULL); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 720 | free_iod: |
| 721 | nvme_free_iod(nvmeq->dev, iod); |
Matthew Wilcox | eeee322 | 2011-02-14 15:55:33 -0500 | [diff] [blame] | 722 | nomem: |
| 723 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 724 | } |
| 725 | |
Matthew Wilcox | e9539f4 | 2013-06-24 11:47:34 -0400 | [diff] [blame] | 726 | static int nvme_process_cq(struct nvme_queue *nvmeq) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 727 | { |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 728 | u16 head, phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 729 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 730 | head = nvmeq->cq_head; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 731 | phase = nvmeq->cq_phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 732 | |
| 733 | for (;;) { |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 734 | void *ctx; |
| 735 | nvme_completion_fn fn; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 736 | struct nvme_completion cqe = nvmeq->cqes[head]; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 737 | if ((le16_to_cpu(cqe.status) & 1) != phase) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 738 | break; |
| 739 | nvmeq->sq_head = le16_to_cpu(cqe.sq_head); |
| 740 | if (++head == nvmeq->q_depth) { |
| 741 | head = 0; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 742 | phase = !phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 743 | } |
| 744 | |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 745 | ctx = free_cmdid(nvmeq, cqe.command_id, &fn); |
Matthew Wilcox | 5c1281a | 2011-12-20 11:54:53 -0500 | [diff] [blame] | 746 | fn(nvmeq->dev, ctx, &cqe); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | /* If the controller ignores the cq head doorbell and continuously |
| 750 | * writes to the queue, it is theoretically possible to wrap around |
| 751 | * the queue twice and mistakenly return IRQ_NONE. Linux only |
| 752 | * requires that 0.1% of your interrupts are handled, so this isn't |
| 753 | * a big problem. |
| 754 | */ |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 755 | if (head == nvmeq->cq_head && phase == nvmeq->cq_phase) |
Matthew Wilcox | e9539f4 | 2013-06-24 11:47:34 -0400 | [diff] [blame] | 756 | return 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 757 | |
Haiyan Hu | b80d5cc | 2013-09-10 11:25:37 +0800 | [diff] [blame] | 758 | writel(head, nvmeq->q_db + nvmeq->dev->db_stride); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 759 | nvmeq->cq_head = head; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 760 | nvmeq->cq_phase = phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 761 | |
Matthew Wilcox | e9539f4 | 2013-06-24 11:47:34 -0400 | [diff] [blame] | 762 | nvmeq->cqe_seen = 1; |
| 763 | return 1; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 764 | } |
| 765 | |
Matthew Wilcox | 7d82245 | 2013-06-24 12:03:57 -0400 | [diff] [blame] | 766 | static void nvme_make_request(struct request_queue *q, struct bio *bio) |
| 767 | { |
| 768 | struct nvme_ns *ns = q->queuedata; |
| 769 | struct nvme_queue *nvmeq = get_nvmeq(ns->dev); |
| 770 | int result = -EBUSY; |
| 771 | |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 772 | if (!nvmeq) { |
| 773 | put_nvmeq(NULL); |
| 774 | bio_endio(bio, -EIO); |
| 775 | return; |
| 776 | } |
| 777 | |
Matthew Wilcox | 7d82245 | 2013-06-24 12:03:57 -0400 | [diff] [blame] | 778 | spin_lock_irq(&nvmeq->q_lock); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 779 | if (!nvmeq->q_suspended && bio_list_empty(&nvmeq->sq_cong)) |
Matthew Wilcox | 7d82245 | 2013-06-24 12:03:57 -0400 | [diff] [blame] | 780 | result = nvme_submit_bio_queue(nvmeq, ns, bio); |
| 781 | if (unlikely(result)) { |
| 782 | if (bio_list_empty(&nvmeq->sq_cong)) |
| 783 | add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait); |
| 784 | bio_list_add(&nvmeq->sq_cong, bio); |
| 785 | } |
| 786 | |
| 787 | nvme_process_cq(nvmeq); |
| 788 | spin_unlock_irq(&nvmeq->q_lock); |
| 789 | put_nvmeq(nvmeq); |
| 790 | } |
| 791 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 792 | static irqreturn_t nvme_irq(int irq, void *data) |
| 793 | { |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 794 | irqreturn_t result; |
| 795 | struct nvme_queue *nvmeq = data; |
| 796 | spin_lock(&nvmeq->q_lock); |
Matthew Wilcox | e9539f4 | 2013-06-24 11:47:34 -0400 | [diff] [blame] | 797 | nvme_process_cq(nvmeq); |
| 798 | result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE; |
| 799 | nvmeq->cqe_seen = 0; |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 800 | spin_unlock(&nvmeq->q_lock); |
| 801 | return result; |
| 802 | } |
| 803 | |
| 804 | static irqreturn_t nvme_irq_check(int irq, void *data) |
| 805 | { |
| 806 | struct nvme_queue *nvmeq = data; |
| 807 | struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head]; |
| 808 | if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase) |
| 809 | return IRQ_NONE; |
| 810 | return IRQ_WAKE_THREAD; |
| 811 | } |
| 812 | |
Matthew Wilcox | 3c0cf13 | 2011-02-04 16:03:56 -0500 | [diff] [blame] | 813 | static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid) |
| 814 | { |
| 815 | spin_lock_irq(&nvmeq->q_lock); |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 816 | cancel_cmdid(nvmeq, cmdid, NULL); |
Matthew Wilcox | 3c0cf13 | 2011-02-04 16:03:56 -0500 | [diff] [blame] | 817 | spin_unlock_irq(&nvmeq->q_lock); |
| 818 | } |
| 819 | |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 820 | struct sync_cmd_info { |
| 821 | struct task_struct *task; |
| 822 | u32 result; |
| 823 | int status; |
| 824 | }; |
| 825 | |
Matthew Wilcox | 5c1281a | 2011-12-20 11:54:53 -0500 | [diff] [blame] | 826 | static void sync_completion(struct nvme_dev *dev, void *ctx, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 827 | struct nvme_completion *cqe) |
| 828 | { |
| 829 | struct sync_cmd_info *cmdinfo = ctx; |
| 830 | cmdinfo->result = le32_to_cpup(&cqe->result); |
| 831 | cmdinfo->status = le16_to_cpup(&cqe->status) >> 1; |
| 832 | wake_up_process(cmdinfo->task); |
| 833 | } |
| 834 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 835 | /* |
| 836 | * Returns 0 on success. If the result is negative, it's a Linux error code; |
| 837 | * if the result is positive, it's an NVM Express status code |
| 838 | */ |
Keith Busch | 4f5099a | 2014-03-03 16:39:13 -0700 | [diff] [blame] | 839 | static int nvme_submit_sync_cmd(struct nvme_dev *dev, int q_idx, |
| 840 | struct nvme_command *cmd, |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 841 | u32 *result, unsigned timeout) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 842 | { |
Keith Busch | 4f5099a | 2014-03-03 16:39:13 -0700 | [diff] [blame] | 843 | int cmdid, ret; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 844 | struct sync_cmd_info cmdinfo; |
Keith Busch | 4f5099a | 2014-03-03 16:39:13 -0700 | [diff] [blame] | 845 | struct nvme_queue *nvmeq; |
| 846 | |
| 847 | nvmeq = lock_nvmeq(dev, q_idx); |
| 848 | if (!nvmeq) { |
| 849 | unlock_nvmeq(nvmeq); |
| 850 | return -ENODEV; |
| 851 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 852 | |
| 853 | cmdinfo.task = current; |
| 854 | cmdinfo.status = -EINTR; |
| 855 | |
Keith Busch | 4f5099a | 2014-03-03 16:39:13 -0700 | [diff] [blame] | 856 | cmdid = alloc_cmdid(nvmeq, &cmdinfo, sync_completion, timeout); |
| 857 | if (cmdid < 0) { |
| 858 | unlock_nvmeq(nvmeq); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 859 | return cmdid; |
Keith Busch | 4f5099a | 2014-03-03 16:39:13 -0700 | [diff] [blame] | 860 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 861 | cmd->common.command_id = cmdid; |
| 862 | |
Matthew Wilcox | 3c0cf13 | 2011-02-04 16:03:56 -0500 | [diff] [blame] | 863 | set_current_state(TASK_KILLABLE); |
Keith Busch | 4f5099a | 2014-03-03 16:39:13 -0700 | [diff] [blame] | 864 | ret = nvme_submit_cmd(nvmeq, cmd); |
| 865 | if (ret) { |
| 866 | free_cmdid(nvmeq, cmdid, NULL); |
| 867 | unlock_nvmeq(nvmeq); |
| 868 | set_current_state(TASK_RUNNING); |
| 869 | return ret; |
| 870 | } |
| 871 | unlock_nvmeq(nvmeq); |
Keith Busch | 78f8d25 | 2013-04-19 14:11:06 -0600 | [diff] [blame] | 872 | schedule_timeout(timeout); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 873 | |
Matthew Wilcox | 3c0cf13 | 2011-02-04 16:03:56 -0500 | [diff] [blame] | 874 | if (cmdinfo.status == -EINTR) { |
Keith Busch | 4f5099a | 2014-03-03 16:39:13 -0700 | [diff] [blame] | 875 | nvmeq = lock_nvmeq(dev, q_idx); |
| 876 | if (nvmeq) |
| 877 | nvme_abort_command(nvmeq, cmdid); |
| 878 | unlock_nvmeq(nvmeq); |
Matthew Wilcox | 3c0cf13 | 2011-02-04 16:03:56 -0500 | [diff] [blame] | 879 | return -EINTR; |
| 880 | } |
| 881 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 882 | if (result) |
| 883 | *result = cmdinfo.result; |
| 884 | |
| 885 | return cmdinfo.status; |
| 886 | } |
| 887 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 888 | static int nvme_submit_async_cmd(struct nvme_queue *nvmeq, |
| 889 | struct nvme_command *cmd, |
| 890 | struct async_cmd_info *cmdinfo, unsigned timeout) |
| 891 | { |
| 892 | int cmdid; |
| 893 | |
| 894 | cmdid = alloc_cmdid_killable(nvmeq, cmdinfo, async_completion, timeout); |
| 895 | if (cmdid < 0) |
| 896 | return cmdid; |
| 897 | cmdinfo->status = -EINTR; |
| 898 | cmd->common.command_id = cmdid; |
Keith Busch | 4f5099a | 2014-03-03 16:39:13 -0700 | [diff] [blame] | 899 | return nvme_submit_cmd(nvmeq, cmd); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 902 | 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] | 903 | u32 *result) |
| 904 | { |
Keith Busch | 4f5099a | 2014-03-03 16:39:13 -0700 | [diff] [blame] | 905 | return nvme_submit_sync_cmd(dev, 0, cmd, result, ADMIN_TIMEOUT); |
| 906 | } |
| 907 | |
| 908 | int nvme_submit_io_cmd(struct nvme_dev *dev, struct nvme_command *cmd, |
| 909 | u32 *result) |
| 910 | { |
| 911 | return nvme_submit_sync_cmd(dev, smp_processor_id() + 1, cmd, result, |
| 912 | NVME_IO_TIMEOUT); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 913 | } |
| 914 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 915 | static int nvme_submit_admin_cmd_async(struct nvme_dev *dev, |
| 916 | struct nvme_command *cmd, struct async_cmd_info *cmdinfo) |
| 917 | { |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 918 | return nvme_submit_async_cmd(raw_nvmeq(dev, 0), cmd, cmdinfo, |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 919 | ADMIN_TIMEOUT); |
| 920 | } |
| 921 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 922 | static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id) |
| 923 | { |
| 924 | int status; |
| 925 | struct nvme_command c; |
| 926 | |
| 927 | memset(&c, 0, sizeof(c)); |
| 928 | c.delete_queue.opcode = opcode; |
| 929 | c.delete_queue.qid = cpu_to_le16(id); |
| 930 | |
| 931 | status = nvme_submit_admin_cmd(dev, &c, NULL); |
| 932 | if (status) |
| 933 | return -EIO; |
| 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid, |
| 938 | struct nvme_queue *nvmeq) |
| 939 | { |
| 940 | int status; |
| 941 | struct nvme_command c; |
| 942 | int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED; |
| 943 | |
| 944 | memset(&c, 0, sizeof(c)); |
| 945 | c.create_cq.opcode = nvme_admin_create_cq; |
| 946 | c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr); |
| 947 | c.create_cq.cqid = cpu_to_le16(qid); |
| 948 | c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1); |
| 949 | c.create_cq.cq_flags = cpu_to_le16(flags); |
| 950 | c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector); |
| 951 | |
| 952 | status = nvme_submit_admin_cmd(dev, &c, NULL); |
| 953 | if (status) |
| 954 | return -EIO; |
| 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid, |
| 959 | struct nvme_queue *nvmeq) |
| 960 | { |
| 961 | int status; |
| 962 | struct nvme_command c; |
| 963 | int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM; |
| 964 | |
| 965 | memset(&c, 0, sizeof(c)); |
| 966 | c.create_sq.opcode = nvme_admin_create_sq; |
| 967 | c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr); |
| 968 | c.create_sq.sqid = cpu_to_le16(qid); |
| 969 | c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1); |
| 970 | c.create_sq.sq_flags = cpu_to_le16(flags); |
| 971 | c.create_sq.cqid = cpu_to_le16(qid); |
| 972 | |
| 973 | status = nvme_submit_admin_cmd(dev, &c, NULL); |
| 974 | if (status) |
| 975 | return -EIO; |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid) |
| 980 | { |
| 981 | return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid); |
| 982 | } |
| 983 | |
| 984 | static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid) |
| 985 | { |
| 986 | return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid); |
| 987 | } |
| 988 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 989 | int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns, |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 990 | dma_addr_t dma_addr) |
| 991 | { |
| 992 | struct nvme_command c; |
| 993 | |
| 994 | memset(&c, 0, sizeof(c)); |
| 995 | c.identify.opcode = nvme_admin_identify; |
| 996 | c.identify.nsid = cpu_to_le32(nsid); |
| 997 | c.identify.prp1 = cpu_to_le64(dma_addr); |
| 998 | c.identify.cns = cpu_to_le32(cns); |
| 999 | |
| 1000 | return nvme_submit_admin_cmd(dev, &c, NULL); |
| 1001 | } |
| 1002 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1003 | int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid, |
Keith Busch | 08df1e0 | 2012-09-21 10:52:13 -0600 | [diff] [blame] | 1004 | dma_addr_t dma_addr, u32 *result) |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1005 | { |
| 1006 | struct nvme_command c; |
| 1007 | |
| 1008 | memset(&c, 0, sizeof(c)); |
| 1009 | c.features.opcode = nvme_admin_get_features; |
Keith Busch | a42cecc | 2012-07-25 16:06:38 -0600 | [diff] [blame] | 1010 | c.features.nsid = cpu_to_le32(nsid); |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1011 | c.features.prp1 = cpu_to_le64(dma_addr); |
| 1012 | c.features.fid = cpu_to_le32(fid); |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1013 | |
Keith Busch | 08df1e0 | 2012-09-21 10:52:13 -0600 | [diff] [blame] | 1014 | return nvme_submit_admin_cmd(dev, &c, result); |
Matthew Wilcox | df34813 | 2012-01-11 07:29:56 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1017 | int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11, |
| 1018 | dma_addr_t dma_addr, u32 *result) |
Matthew Wilcox | df34813 | 2012-01-11 07:29:56 -0700 | [diff] [blame] | 1019 | { |
| 1020 | struct nvme_command c; |
| 1021 | |
| 1022 | memset(&c, 0, sizeof(c)); |
| 1023 | c.features.opcode = nvme_admin_set_features; |
| 1024 | c.features.prp1 = cpu_to_le64(dma_addr); |
| 1025 | c.features.fid = cpu_to_le32(fid); |
| 1026 | c.features.dword11 = cpu_to_le32(dword11); |
| 1027 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1028 | return nvme_submit_admin_cmd(dev, &c, result); |
| 1029 | } |
| 1030 | |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1031 | /** |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1032 | * nvme_abort_cmd - Attempt aborting a command |
| 1033 | * @cmdid: Command id of a timed out IO |
| 1034 | * @queue: The queue with timed out IO |
| 1035 | * |
| 1036 | * Schedule controller reset if the command was already aborted once before and |
| 1037 | * still hasn't been returned to the driver, or if this is the admin queue. |
| 1038 | */ |
| 1039 | static void nvme_abort_cmd(int cmdid, struct nvme_queue *nvmeq) |
| 1040 | { |
| 1041 | int a_cmdid; |
| 1042 | struct nvme_command cmd; |
| 1043 | struct nvme_dev *dev = nvmeq->dev; |
| 1044 | struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 1045 | struct nvme_queue *adminq; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1046 | |
| 1047 | if (!nvmeq->qid || info[cmdid].aborted) { |
| 1048 | if (work_busy(&dev->reset_work)) |
| 1049 | return; |
| 1050 | list_del_init(&dev->node); |
| 1051 | dev_warn(&dev->pci_dev->dev, |
| 1052 | "I/O %d QID %d timeout, reset controller\n", cmdid, |
| 1053 | nvmeq->qid); |
Matthew Wilcox | bdfd70f | 2014-01-29 09:33:52 -0500 | [diff] [blame] | 1054 | PREPARE_WORK(&dev->reset_work, nvme_reset_failed_dev); |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1055 | queue_work(nvme_workq, &dev->reset_work); |
| 1056 | return; |
| 1057 | } |
| 1058 | |
| 1059 | if (!dev->abort_limit) |
| 1060 | return; |
| 1061 | |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 1062 | adminq = rcu_dereference(dev->queues[0]); |
| 1063 | a_cmdid = alloc_cmdid(adminq, CMD_CTX_ABORT, special_completion, |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1064 | ADMIN_TIMEOUT); |
| 1065 | if (a_cmdid < 0) |
| 1066 | return; |
| 1067 | |
| 1068 | memset(&cmd, 0, sizeof(cmd)); |
| 1069 | cmd.abort.opcode = nvme_admin_abort_cmd; |
| 1070 | cmd.abort.cid = cmdid; |
| 1071 | cmd.abort.sqid = cpu_to_le16(nvmeq->qid); |
| 1072 | cmd.abort.command_id = a_cmdid; |
| 1073 | |
| 1074 | --dev->abort_limit; |
| 1075 | info[cmdid].aborted = 1; |
| 1076 | info[cmdid].timeout = jiffies + ADMIN_TIMEOUT; |
| 1077 | |
| 1078 | dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", cmdid, |
| 1079 | nvmeq->qid); |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 1080 | nvme_submit_cmd(adminq, &cmd); |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | /** |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1084 | * nvme_cancel_ios - Cancel outstanding I/Os |
| 1085 | * @queue: The queue to cancel I/Os on |
| 1086 | * @timeout: True to only cancel I/Os which have timed out |
| 1087 | */ |
| 1088 | static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout) |
| 1089 | { |
| 1090 | int depth = nvmeq->q_depth - 1; |
| 1091 | struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); |
| 1092 | unsigned long now = jiffies; |
| 1093 | int cmdid; |
| 1094 | |
| 1095 | for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) { |
| 1096 | void *ctx; |
| 1097 | nvme_completion_fn fn; |
| 1098 | static struct nvme_completion cqe = { |
Matthew Wilcox | af2d9ca | 2013-04-16 15:18:30 -0400 | [diff] [blame] | 1099 | .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1), |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1100 | }; |
| 1101 | |
| 1102 | if (timeout && !time_after(now, info[cmdid].timeout)) |
| 1103 | continue; |
Keith Busch | 053ab702 | 2013-04-30 11:19:38 -0600 | [diff] [blame] | 1104 | if (info[cmdid].ctx == CMD_CTX_CANCELLED) |
| 1105 | continue; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1106 | if (timeout && nvmeq->dev->initialized) { |
| 1107 | nvme_abort_cmd(cmdid, nvmeq); |
| 1108 | continue; |
| 1109 | } |
| 1110 | dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n", cmdid, |
| 1111 | nvmeq->qid); |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1112 | ctx = cancel_cmdid(nvmeq, cmdid, &fn); |
| 1113 | fn(nvmeq->dev, ctx, &cqe); |
| 1114 | } |
| 1115 | } |
| 1116 | |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 1117 | static void nvme_free_queue(struct rcu_head *r) |
Matthew Wilcox | 9e86677 | 2012-08-03 13:55:56 -0400 | [diff] [blame] | 1118 | { |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 1119 | struct nvme_queue *nvmeq = container_of(r, struct nvme_queue, r_head); |
| 1120 | |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1121 | spin_lock_irq(&nvmeq->q_lock); |
| 1122 | while (bio_list_peek(&nvmeq->sq_cong)) { |
| 1123 | struct bio *bio = bio_list_pop(&nvmeq->sq_cong); |
| 1124 | bio_endio(bio, -EIO); |
| 1125 | } |
| 1126 | spin_unlock_irq(&nvmeq->q_lock); |
| 1127 | |
Matthew Wilcox | 9e86677 | 2012-08-03 13:55:56 -0400 | [diff] [blame] | 1128 | dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth), |
| 1129 | (void *)nvmeq->cqes, nvmeq->cq_dma_addr); |
| 1130 | dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth), |
| 1131 | nvmeq->sq_cmds, nvmeq->sq_dma_addr); |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1132 | if (nvmeq->qid) |
| 1133 | free_cpumask_var(nvmeq->cpu_mask); |
Matthew Wilcox | 9e86677 | 2012-08-03 13:55:56 -0400 | [diff] [blame] | 1134 | kfree(nvmeq); |
| 1135 | } |
| 1136 | |
Keith Busch | a1a5ef9 | 2013-12-16 13:50:00 -0500 | [diff] [blame] | 1137 | static void nvme_free_queues(struct nvme_dev *dev, int lowest) |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1138 | { |
| 1139 | int i; |
| 1140 | |
Keith Busch | a1a5ef9 | 2013-12-16 13:50:00 -0500 | [diff] [blame] | 1141 | for (i = dev->queue_count - 1; i >= lowest; i--) { |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 1142 | struct nvme_queue *nvmeq = raw_nvmeq(dev, i); |
| 1143 | rcu_assign_pointer(dev->queues[i], NULL); |
| 1144 | call_rcu(&nvmeq->r_head, nvme_free_queue); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1145 | dev->queue_count--; |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1146 | } |
| 1147 | } |
| 1148 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1149 | /** |
| 1150 | * nvme_suspend_queue - put queue into suspended state |
| 1151 | * @nvmeq - queue to suspend |
| 1152 | * |
| 1153 | * Returns 1 if already suspended, 0 otherwise. |
| 1154 | */ |
| 1155 | static int nvme_suspend_queue(struct nvme_queue *nvmeq) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1156 | { |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1157 | int vector = nvmeq->dev->entry[nvmeq->cq_vector].vector; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1158 | |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1159 | spin_lock_irq(&nvmeq->q_lock); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1160 | if (nvmeq->q_suspended) { |
| 1161 | spin_unlock_irq(&nvmeq->q_lock); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1162 | return 1; |
Keith Busch | 3295874 | 2012-08-20 14:57:49 -0600 | [diff] [blame] | 1163 | } |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1164 | nvmeq->q_suspended = 1; |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1165 | nvmeq->dev->online_queues--; |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1166 | spin_unlock_irq(&nvmeq->q_lock); |
| 1167 | |
Matthew Wilcox | aba2080 | 2011-03-27 08:52:06 -0400 | [diff] [blame] | 1168 | irq_set_affinity_hint(vector, NULL); |
| 1169 | free_irq(vector, nvmeq); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1170 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1171 | return 0; |
| 1172 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1173 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1174 | static void nvme_clear_queue(struct nvme_queue *nvmeq) |
| 1175 | { |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1176 | spin_lock_irq(&nvmeq->q_lock); |
| 1177 | nvme_process_cq(nvmeq); |
| 1178 | nvme_cancel_ios(nvmeq, false); |
| 1179 | spin_unlock_irq(&nvmeq->q_lock); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1180 | } |
| 1181 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1182 | static void nvme_disable_queue(struct nvme_dev *dev, int qid) |
| 1183 | { |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 1184 | struct nvme_queue *nvmeq = raw_nvmeq(dev, qid); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1185 | |
| 1186 | if (!nvmeq) |
| 1187 | return; |
| 1188 | if (nvme_suspend_queue(nvmeq)) |
| 1189 | return; |
| 1190 | |
Keith Busch | 0e53d18 | 2013-12-10 13:10:39 -0700 | [diff] [blame] | 1191 | /* Don't tell the adapter to delete the admin queue. |
| 1192 | * Don't tell a removed adapter to delete IO queues. */ |
| 1193 | if (qid && readl(&dev->bar->csts) != -1) { |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1194 | adapter_delete_sq(dev, qid); |
| 1195 | adapter_delete_cq(dev, qid); |
| 1196 | } |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1197 | nvme_clear_queue(nvmeq); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid, |
| 1201 | int depth, int vector) |
| 1202 | { |
| 1203 | struct device *dmadev = &dev->pci_dev->dev; |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1204 | unsigned extra = nvme_queue_extra(depth); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1205 | struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL); |
| 1206 | if (!nvmeq) |
| 1207 | return NULL; |
| 1208 | |
| 1209 | nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth), |
| 1210 | &nvmeq->cq_dma_addr, GFP_KERNEL); |
| 1211 | if (!nvmeq->cqes) |
| 1212 | goto free_nvmeq; |
| 1213 | memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth)); |
| 1214 | |
| 1215 | nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth), |
| 1216 | &nvmeq->sq_dma_addr, GFP_KERNEL); |
| 1217 | if (!nvmeq->sq_cmds) |
| 1218 | goto free_cqdma; |
| 1219 | |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1220 | if (qid && !zalloc_cpumask_var(&nvmeq->cpu_mask, GFP_KERNEL)) |
| 1221 | goto free_sqdma; |
| 1222 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1223 | nvmeq->q_dmadev = dmadev; |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 1224 | nvmeq->dev = dev; |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 1225 | snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d", |
| 1226 | dev->instance, qid); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1227 | spin_lock_init(&nvmeq->q_lock); |
| 1228 | nvmeq->cq_head = 0; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 1229 | nvmeq->cq_phase = 1; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1230 | init_waitqueue_head(&nvmeq->sq_full); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1231 | init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1232 | bio_list_init(&nvmeq->sq_cong); |
Haiyan Hu | b80d5cc | 2013-09-10 11:25:37 +0800 | [diff] [blame] | 1233 | nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride]; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1234 | nvmeq->q_depth = depth; |
| 1235 | nvmeq->cq_vector = vector; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1236 | nvmeq->qid = qid; |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1237 | nvmeq->q_suspended = 1; |
| 1238 | dev->queue_count++; |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 1239 | rcu_assign_pointer(dev->queues[qid], nvmeq); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1240 | |
| 1241 | return nvmeq; |
| 1242 | |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1243 | free_sqdma: |
| 1244 | dma_free_coherent(dmadev, SQ_SIZE(depth), (void *)nvmeq->sq_cmds, |
| 1245 | nvmeq->sq_dma_addr); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1246 | free_cqdma: |
Keith Busch | 68b8eca | 2013-05-01 13:07:47 -0600 | [diff] [blame] | 1247 | dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1248 | nvmeq->cq_dma_addr); |
| 1249 | free_nvmeq: |
| 1250 | kfree(nvmeq); |
| 1251 | return NULL; |
| 1252 | } |
| 1253 | |
Matthew Wilcox | 3001082 | 2011-01-20 09:10:15 -0500 | [diff] [blame] | 1254 | static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq, |
| 1255 | const char *name) |
| 1256 | { |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 1257 | if (use_threaded_interrupts) |
| 1258 | return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector, |
Michael Opdenacker | 481e5ba | 2013-10-12 06:23:29 +0200 | [diff] [blame] | 1259 | nvme_irq_check, nvme_irq, IRQF_SHARED, |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 1260 | name, nvmeq); |
Matthew Wilcox | 3001082 | 2011-01-20 09:10:15 -0500 | [diff] [blame] | 1261 | return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq, |
Michael Opdenacker | 481e5ba | 2013-10-12 06:23:29 +0200 | [diff] [blame] | 1262 | IRQF_SHARED, name, nvmeq); |
Matthew Wilcox | 3001082 | 2011-01-20 09:10:15 -0500 | [diff] [blame] | 1263 | } |
| 1264 | |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1265 | static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1266 | { |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1267 | struct nvme_dev *dev = nvmeq->dev; |
| 1268 | unsigned extra = nvme_queue_extra(nvmeq->q_depth); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1269 | |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1270 | nvmeq->sq_tail = 0; |
| 1271 | nvmeq->cq_head = 0; |
| 1272 | nvmeq->cq_phase = 1; |
Haiyan Hu | b80d5cc | 2013-09-10 11:25:37 +0800 | [diff] [blame] | 1273 | nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride]; |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1274 | memset(nvmeq->cmdid_data, 0, extra); |
| 1275 | memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth)); |
| 1276 | nvme_cancel_ios(nvmeq, false); |
| 1277 | nvmeq->q_suspended = 0; |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1278 | dev->online_queues++; |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | static int nvme_create_queue(struct nvme_queue *nvmeq, int qid) |
| 1282 | { |
| 1283 | struct nvme_dev *dev = nvmeq->dev; |
| 1284 | int result; |
Matthew Wilcox | 3f85d50 | 2011-02-01 08:39:04 -0500 | [diff] [blame] | 1285 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1286 | result = adapter_alloc_cq(dev, qid, nvmeq); |
| 1287 | if (result < 0) |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1288 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1289 | |
| 1290 | result = adapter_alloc_sq(dev, qid, nvmeq); |
| 1291 | if (result < 0) |
| 1292 | goto release_cq; |
| 1293 | |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 1294 | result = queue_request_irq(dev, nvmeq, nvmeq->irqname); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1295 | if (result < 0) |
| 1296 | goto release_sq; |
| 1297 | |
Matthew Wilcox | 0a8d44c | 2013-10-15 15:01:10 -0400 | [diff] [blame] | 1298 | spin_lock_irq(&nvmeq->q_lock); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1299 | nvme_init_queue(nvmeq, qid); |
Matthew Wilcox | 0a8d44c | 2013-10-15 15:01:10 -0400 | [diff] [blame] | 1300 | spin_unlock_irq(&nvmeq->q_lock); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1301 | |
| 1302 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1303 | |
| 1304 | release_sq: |
| 1305 | adapter_delete_sq(dev, qid); |
| 1306 | release_cq: |
| 1307 | adapter_delete_cq(dev, qid); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1308 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1309 | } |
| 1310 | |
Matthew Wilcox | ba47e38 | 2013-05-04 06:43:16 -0400 | [diff] [blame] | 1311 | static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled) |
| 1312 | { |
| 1313 | unsigned long timeout; |
| 1314 | u32 bit = enabled ? NVME_CSTS_RDY : 0; |
| 1315 | |
| 1316 | timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies; |
| 1317 | |
| 1318 | while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) { |
| 1319 | msleep(100); |
| 1320 | if (fatal_signal_pending(current)) |
| 1321 | return -EINTR; |
| 1322 | if (time_after(jiffies, timeout)) { |
| 1323 | dev_err(&dev->pci_dev->dev, |
| 1324 | "Device not ready; aborting initialisation\n"); |
| 1325 | return -ENODEV; |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
| 1332 | /* |
| 1333 | * If the device has been passed off to us in an enabled state, just clear |
| 1334 | * the enabled bit. The spec says we should set the 'shutdown notification |
| 1335 | * bits', but doing so may cause the device to complete commands to the |
| 1336 | * admin queue ... and we don't know what memory that might be pointing at! |
| 1337 | */ |
| 1338 | static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap) |
| 1339 | { |
Matthew Wilcox | 44af146 | 2013-05-04 06:43:17 -0400 | [diff] [blame] | 1340 | u32 cc = readl(&dev->bar->cc); |
| 1341 | |
| 1342 | if (cc & NVME_CC_ENABLE) |
| 1343 | writel(cc & ~NVME_CC_ENABLE, &dev->bar->cc); |
Matthew Wilcox | ba47e38 | 2013-05-04 06:43:16 -0400 | [diff] [blame] | 1344 | return nvme_wait_ready(dev, cap, false); |
| 1345 | } |
| 1346 | |
| 1347 | static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap) |
| 1348 | { |
| 1349 | return nvme_wait_ready(dev, cap, true); |
| 1350 | } |
| 1351 | |
Keith Busch | 1894d8f | 2013-07-15 15:02:22 -0600 | [diff] [blame] | 1352 | static int nvme_shutdown_ctrl(struct nvme_dev *dev) |
| 1353 | { |
| 1354 | unsigned long timeout; |
| 1355 | u32 cc; |
| 1356 | |
| 1357 | cc = (readl(&dev->bar->cc) & ~NVME_CC_SHN_MASK) | NVME_CC_SHN_NORMAL; |
| 1358 | writel(cc, &dev->bar->cc); |
| 1359 | |
| 1360 | timeout = 2 * HZ + jiffies; |
| 1361 | while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) != |
| 1362 | NVME_CSTS_SHST_CMPLT) { |
| 1363 | msleep(100); |
| 1364 | if (fatal_signal_pending(current)) |
| 1365 | return -EINTR; |
| 1366 | if (time_after(jiffies, timeout)) { |
| 1367 | dev_err(&dev->pci_dev->dev, |
| 1368 | "Device shutdown incomplete; abort shutdown\n"); |
| 1369 | return -ENODEV; |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | return 0; |
| 1374 | } |
| 1375 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1376 | static int nvme_configure_admin_queue(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1377 | { |
Matthew Wilcox | ba47e38 | 2013-05-04 06:43:16 -0400 | [diff] [blame] | 1378 | int result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1379 | u32 aqa; |
Matthew Wilcox | ba47e38 | 2013-05-04 06:43:16 -0400 | [diff] [blame] | 1380 | u64 cap = readq(&dev->bar->cap); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1381 | struct nvme_queue *nvmeq; |
| 1382 | |
Matthew Wilcox | ba47e38 | 2013-05-04 06:43:16 -0400 | [diff] [blame] | 1383 | result = nvme_disable_ctrl(dev, cap); |
| 1384 | if (result < 0) |
| 1385 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1386 | |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 1387 | nvmeq = raw_nvmeq(dev, 0); |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 1388 | if (!nvmeq) { |
| 1389 | nvmeq = nvme_alloc_queue(dev, 0, 64, 0); |
| 1390 | if (!nvmeq) |
| 1391 | return -ENOMEM; |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 1392 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1393 | |
| 1394 | aqa = nvmeq->q_depth - 1; |
| 1395 | aqa |= aqa << 16; |
| 1396 | |
| 1397 | dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM; |
| 1398 | dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT; |
| 1399 | dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE; |
Matthew Wilcox | 7f53f9d | 2011-03-22 15:55:45 -0400 | [diff] [blame] | 1400 | dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1401 | |
| 1402 | writel(aqa, &dev->bar->aqa); |
| 1403 | writeq(nvmeq->sq_dma_addr, &dev->bar->asq); |
| 1404 | writeq(nvmeq->cq_dma_addr, &dev->bar->acq); |
| 1405 | writel(dev->ctrl_config, &dev->bar->cc); |
| 1406 | |
Matthew Wilcox | ba47e38 | 2013-05-04 06:43:16 -0400 | [diff] [blame] | 1407 | result = nvme_enable_ctrl(dev, cap); |
Keith Busch | 025c557 | 2013-05-01 13:07:51 -0600 | [diff] [blame] | 1408 | if (result) |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 1409 | return result; |
Matthew Wilcox | 9e86677 | 2012-08-03 13:55:56 -0400 | [diff] [blame] | 1410 | |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 1411 | result = queue_request_irq(dev, nvmeq, nvmeq->irqname); |
Keith Busch | 025c557 | 2013-05-01 13:07:51 -0600 | [diff] [blame] | 1412 | if (result) |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 1413 | return result; |
Keith Busch | 025c557 | 2013-05-01 13:07:51 -0600 | [diff] [blame] | 1414 | |
Matthew Wilcox | 0a8d44c | 2013-10-15 15:01:10 -0400 | [diff] [blame] | 1415 | spin_lock_irq(&nvmeq->q_lock); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1416 | nvme_init_queue(nvmeq, 0); |
Matthew Wilcox | 0a8d44c | 2013-10-15 15:01:10 -0400 | [diff] [blame] | 1417 | spin_unlock_irq(&nvmeq->q_lock); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1418 | return result; |
| 1419 | } |
| 1420 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1421 | 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] | 1422 | unsigned long addr, unsigned length) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1423 | { |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 1424 | int i, err, count, nents, offset; |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1425 | struct scatterlist *sg; |
| 1426 | struct page **pages; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1427 | struct nvme_iod *iod; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1428 | |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 1429 | if (addr & 3) |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1430 | return ERR_PTR(-EINVAL); |
Dan Carpenter | 5460fc0 | 2013-05-13 17:59:50 +0300 | [diff] [blame] | 1431 | if (!length || length > INT_MAX - PAGE_SIZE) |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1432 | return ERR_PTR(-EINVAL); |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1433 | |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 1434 | offset = offset_in_page(addr); |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1435 | count = DIV_ROUND_UP(offset + length, PAGE_SIZE); |
| 1436 | pages = kcalloc(count, sizeof(*pages), GFP_KERNEL); |
Dan Carpenter | 22fff82 | 2012-01-20 07:55:30 -0500 | [diff] [blame] | 1437 | if (!pages) |
| 1438 | return ERR_PTR(-ENOMEM); |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 1439 | |
| 1440 | err = get_user_pages_fast(addr, count, 1, pages); |
| 1441 | if (err < count) { |
| 1442 | count = err; |
| 1443 | err = -EFAULT; |
| 1444 | goto put_pages; |
| 1445 | } |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1446 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1447 | iod = nvme_alloc_iod(count, length, GFP_KERNEL); |
| 1448 | sg = iod->sg; |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 1449 | sg_init_table(sg, count); |
Matthew Wilcox | d0ba1e4 | 2011-09-13 17:01:39 -0400 | [diff] [blame] | 1450 | for (i = 0; i < count; i++) { |
| 1451 | sg_set_page(&sg[i], pages[i], |
Dan Carpenter | 5460fc0 | 2013-05-13 17:59:50 +0300 | [diff] [blame] | 1452 | min_t(unsigned, length, PAGE_SIZE - offset), |
| 1453 | offset); |
Matthew Wilcox | d0ba1e4 | 2011-09-13 17:01:39 -0400 | [diff] [blame] | 1454 | length -= (PAGE_SIZE - offset); |
| 1455 | offset = 0; |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1456 | } |
Matthew Wilcox | fe304c4 | 2012-01-06 13:49:25 -0700 | [diff] [blame] | 1457 | sg_mark_end(&sg[i - 1]); |
Matthew Wilcox | 1c2ad9f | 2012-01-06 13:52:56 -0700 | [diff] [blame] | 1458 | iod->nents = count; |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1459 | |
| 1460 | err = -ENOMEM; |
| 1461 | nents = dma_map_sg(&dev->pci_dev->dev, sg, count, |
| 1462 | write ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 1463 | if (!nents) |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1464 | goto free_iod; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1465 | |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1466 | kfree(pages); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1467 | return iod; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1468 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1469 | free_iod: |
| 1470 | kfree(iod); |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 1471 | put_pages: |
| 1472 | for (i = 0; i < count; i++) |
| 1473 | put_page(pages[i]); |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1474 | kfree(pages); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1475 | return ERR_PTR(err); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1476 | } |
| 1477 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1478 | void nvme_unmap_user_pages(struct nvme_dev *dev, int write, |
Matthew Wilcox | 1c2ad9f | 2012-01-06 13:52:56 -0700 | [diff] [blame] | 1479 | struct nvme_iod *iod) |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1480 | { |
Matthew Wilcox | 1c2ad9f | 2012-01-06 13:52:56 -0700 | [diff] [blame] | 1481 | int i; |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1482 | |
Matthew Wilcox | 1c2ad9f | 2012-01-06 13:52:56 -0700 | [diff] [blame] | 1483 | dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents, |
| 1484 | write ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1485 | |
Matthew Wilcox | 1c2ad9f | 2012-01-06 13:52:56 -0700 | [diff] [blame] | 1486 | for (i = 0; i < iod->nents; i++) |
| 1487 | put_page(sg_page(&iod->sg[i])); |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame] | 1488 | } |
| 1489 | |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1490 | static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) |
| 1491 | { |
| 1492 | struct nvme_dev *dev = ns->dev; |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1493 | struct nvme_user_io io; |
| 1494 | struct nvme_command c; |
Keith Busch | f410c68 | 2013-04-23 17:23:59 -0600 | [diff] [blame] | 1495 | unsigned length, meta_len; |
| 1496 | int status, i; |
| 1497 | struct nvme_iod *iod, *meta_iod = NULL; |
| 1498 | dma_addr_t meta_dma_addr; |
| 1499 | void *meta, *uninitialized_var(meta_mem); |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1500 | |
| 1501 | if (copy_from_user(&io, uio, sizeof(io))) |
| 1502 | return -EFAULT; |
Matthew Wilcox | 6c7d494 | 2011-03-21 09:48:57 -0400 | [diff] [blame] | 1503 | length = (io.nblocks + 1) << ns->lba_shift; |
Keith Busch | f410c68 | 2013-04-23 17:23:59 -0600 | [diff] [blame] | 1504 | meta_len = (io.nblocks + 1) * ns->ms; |
| 1505 | |
| 1506 | if (meta_len && ((io.metadata & 3) || !io.metadata)) |
| 1507 | return -EINVAL; |
Matthew Wilcox | 6c7d494 | 2011-03-21 09:48:57 -0400 | [diff] [blame] | 1508 | |
| 1509 | switch (io.opcode) { |
| 1510 | case nvme_cmd_write: |
| 1511 | case nvme_cmd_read: |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1512 | case nvme_cmd_compare: |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1513 | iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length); |
Matthew Wilcox | 6413214 | 2011-08-09 12:56:37 -0400 | [diff] [blame] | 1514 | break; |
Matthew Wilcox | 6c7d494 | 2011-03-21 09:48:57 -0400 | [diff] [blame] | 1515 | default: |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1516 | return -EINVAL; |
Matthew Wilcox | 6c7d494 | 2011-03-21 09:48:57 -0400 | [diff] [blame] | 1517 | } |
| 1518 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1519 | if (IS_ERR(iod)) |
| 1520 | return PTR_ERR(iod); |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1521 | |
| 1522 | memset(&c, 0, sizeof(c)); |
| 1523 | c.rw.opcode = io.opcode; |
| 1524 | c.rw.flags = io.flags; |
Matthew Wilcox | 6c7d494 | 2011-03-21 09:48:57 -0400 | [diff] [blame] | 1525 | c.rw.nsid = cpu_to_le32(ns->ns_id); |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1526 | c.rw.slba = cpu_to_le64(io.slba); |
Matthew Wilcox | 6c7d494 | 2011-03-21 09:48:57 -0400 | [diff] [blame] | 1527 | c.rw.length = cpu_to_le16(io.nblocks); |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1528 | c.rw.control = cpu_to_le16(io.control); |
Matthew Wilcox | 1c9b526 | 2013-04-16 15:21:06 -0400 | [diff] [blame] | 1529 | c.rw.dsmgmt = cpu_to_le32(io.dsmgmt); |
| 1530 | c.rw.reftag = cpu_to_le32(io.reftag); |
| 1531 | c.rw.apptag = cpu_to_le16(io.apptag); |
| 1532 | c.rw.appmask = cpu_to_le16(io.appmask); |
Keith Busch | f410c68 | 2013-04-23 17:23:59 -0600 | [diff] [blame] | 1533 | |
| 1534 | if (meta_len) { |
Keith Busch | 1b56749 | 2013-07-18 12:13:51 -0600 | [diff] [blame] | 1535 | meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata, |
| 1536 | meta_len); |
Keith Busch | f410c68 | 2013-04-23 17:23:59 -0600 | [diff] [blame] | 1537 | if (IS_ERR(meta_iod)) { |
| 1538 | status = PTR_ERR(meta_iod); |
| 1539 | meta_iod = NULL; |
| 1540 | goto unmap; |
| 1541 | } |
| 1542 | |
| 1543 | meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len, |
| 1544 | &meta_dma_addr, GFP_KERNEL); |
| 1545 | if (!meta_mem) { |
| 1546 | status = -ENOMEM; |
| 1547 | goto unmap; |
| 1548 | } |
| 1549 | |
| 1550 | if (io.opcode & 1) { |
| 1551 | int meta_offset = 0; |
| 1552 | |
| 1553 | for (i = 0; i < meta_iod->nents; i++) { |
| 1554 | meta = kmap_atomic(sg_page(&meta_iod->sg[i])) + |
| 1555 | meta_iod->sg[i].offset; |
| 1556 | memcpy(meta_mem + meta_offset, meta, |
| 1557 | meta_iod->sg[i].length); |
| 1558 | kunmap_atomic(meta); |
| 1559 | meta_offset += meta_iod->sg[i].length; |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | c.rw.metadata = cpu_to_le64(meta_dma_addr); |
| 1564 | } |
| 1565 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1566 | length = nvme_setup_prps(dev, &c.common, iod, length, GFP_KERNEL); |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 1567 | |
Matthew Wilcox | b77954c | 2011-05-12 13:51:41 -0400 | [diff] [blame] | 1568 | if (length != (io.nblocks + 1) << ns->lba_shift) |
| 1569 | status = -ENOMEM; |
| 1570 | else |
Keith Busch | 4f5099a | 2014-03-03 16:39:13 -0700 | [diff] [blame] | 1571 | status = nvme_submit_io_cmd(dev, &c, NULL); |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1572 | |
Keith Busch | f410c68 | 2013-04-23 17:23:59 -0600 | [diff] [blame] | 1573 | if (meta_len) { |
| 1574 | if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) { |
| 1575 | int meta_offset = 0; |
| 1576 | |
| 1577 | for (i = 0; i < meta_iod->nents; i++) { |
| 1578 | meta = kmap_atomic(sg_page(&meta_iod->sg[i])) + |
| 1579 | meta_iod->sg[i].offset; |
| 1580 | memcpy(meta, meta_mem + meta_offset, |
| 1581 | meta_iod->sg[i].length); |
| 1582 | kunmap_atomic(meta); |
| 1583 | meta_offset += meta_iod->sg[i].length; |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem, |
| 1588 | meta_dma_addr); |
| 1589 | } |
| 1590 | |
| 1591 | unmap: |
Matthew Wilcox | 1c2ad9f | 2012-01-06 13:52:56 -0700 | [diff] [blame] | 1592 | nvme_unmap_user_pages(dev, io.opcode & 1, iod); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1593 | nvme_free_iod(dev, iod); |
Keith Busch | f410c68 | 2013-04-23 17:23:59 -0600 | [diff] [blame] | 1594 | |
| 1595 | if (meta_iod) { |
| 1596 | nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod); |
| 1597 | nvme_free_iod(dev, meta_iod); |
| 1598 | } |
| 1599 | |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1600 | return status; |
| 1601 | } |
| 1602 | |
Keith Busch | 50af8ba | 2012-07-25 16:07:55 -0600 | [diff] [blame] | 1603 | static int nvme_user_admin_cmd(struct nvme_dev *dev, |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1604 | struct nvme_admin_cmd __user *ucmd) |
Matthew Wilcox | 6ee44cd | 2011-02-03 10:58:26 -0500 | [diff] [blame] | 1605 | { |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1606 | struct nvme_admin_cmd cmd; |
Matthew Wilcox | 6ee44cd | 2011-02-03 10:58:26 -0500 | [diff] [blame] | 1607 | struct nvme_command c; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1608 | int status, length; |
Keith Busch | c7d36ab | 2012-07-27 11:53:28 -0600 | [diff] [blame] | 1609 | struct nvme_iod *uninitialized_var(iod); |
Keith Busch | 94f370c | 2013-05-09 14:01:38 -0600 | [diff] [blame] | 1610 | unsigned timeout; |
Matthew Wilcox | 6ee44cd | 2011-02-03 10:58:26 -0500 | [diff] [blame] | 1611 | |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1612 | if (!capable(CAP_SYS_ADMIN)) |
| 1613 | return -EACCES; |
| 1614 | if (copy_from_user(&cmd, ucmd, sizeof(cmd))) |
Matthew Wilcox | 6ee44cd | 2011-02-03 10:58:26 -0500 | [diff] [blame] | 1615 | return -EFAULT; |
Matthew Wilcox | 6ee44cd | 2011-02-03 10:58:26 -0500 | [diff] [blame] | 1616 | |
| 1617 | memset(&c, 0, sizeof(c)); |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1618 | c.common.opcode = cmd.opcode; |
| 1619 | c.common.flags = cmd.flags; |
| 1620 | c.common.nsid = cpu_to_le32(cmd.nsid); |
| 1621 | c.common.cdw2[0] = cpu_to_le32(cmd.cdw2); |
| 1622 | c.common.cdw2[1] = cpu_to_le32(cmd.cdw3); |
| 1623 | c.common.cdw10[0] = cpu_to_le32(cmd.cdw10); |
| 1624 | c.common.cdw10[1] = cpu_to_le32(cmd.cdw11); |
| 1625 | c.common.cdw10[2] = cpu_to_le32(cmd.cdw12); |
| 1626 | c.common.cdw10[3] = cpu_to_le32(cmd.cdw13); |
| 1627 | c.common.cdw10[4] = cpu_to_le32(cmd.cdw14); |
| 1628 | c.common.cdw10[5] = cpu_to_le32(cmd.cdw15); |
| 1629 | |
| 1630 | length = cmd.data_len; |
| 1631 | if (cmd.data_len) { |
Matthew Wilcox | 4974218 | 2012-01-06 13:42:45 -0700 | [diff] [blame] | 1632 | iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr, |
| 1633 | length); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1634 | if (IS_ERR(iod)) |
| 1635 | return PTR_ERR(iod); |
| 1636 | length = nvme_setup_prps(dev, &c.common, iod, length, |
| 1637 | GFP_KERNEL); |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1638 | } |
| 1639 | |
Keith Busch | 94f370c | 2013-05-09 14:01:38 -0600 | [diff] [blame] | 1640 | timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) : |
| 1641 | ADMIN_TIMEOUT; |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1642 | if (length != cmd.data_len) |
Matthew Wilcox | b77954c | 2011-05-12 13:51:41 -0400 | [diff] [blame] | 1643 | status = -ENOMEM; |
| 1644 | else |
Keith Busch | 4f5099a | 2014-03-03 16:39:13 -0700 | [diff] [blame] | 1645 | status = nvme_submit_sync_cmd(dev, 0, &c, &cmd.result, timeout); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1646 | |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1647 | if (cmd.data_len) { |
Matthew Wilcox | 1c2ad9f | 2012-01-06 13:52:56 -0700 | [diff] [blame] | 1648 | nvme_unmap_user_pages(dev, cmd.opcode & 1, iod); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 1649 | nvme_free_iod(dev, iod); |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1650 | } |
Keith Busch | f4f117f | 2012-09-21 10:49:05 -0600 | [diff] [blame] | 1651 | |
Chayan Biswas | cf90bc4 | 2013-05-22 22:34:49 +0000 | [diff] [blame] | 1652 | if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result, |
Keith Busch | f4f117f | 2012-09-21 10:49:05 -0600 | [diff] [blame] | 1653 | sizeof(cmd.result))) |
| 1654 | status = -EFAULT; |
| 1655 | |
Matthew Wilcox | 6ee44cd | 2011-02-03 10:58:26 -0500 | [diff] [blame] | 1656 | return status; |
| 1657 | } |
| 1658 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1659 | static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, |
| 1660 | unsigned long arg) |
| 1661 | { |
| 1662 | struct nvme_ns *ns = bdev->bd_disk->private_data; |
| 1663 | |
| 1664 | switch (cmd) { |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1665 | case NVME_IOCTL_ID: |
Matthew Wilcox | c3bfe71 | 2013-07-08 17:26:25 -0400 | [diff] [blame] | 1666 | force_successful_syscall_return(); |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1667 | return ns->ns_id; |
| 1668 | case NVME_IOCTL_ADMIN_CMD: |
Keith Busch | 50af8ba | 2012-07-25 16:07:55 -0600 | [diff] [blame] | 1669 | return nvme_user_admin_cmd(ns->dev, (void __user *)arg); |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1670 | case NVME_IOCTL_SUBMIT_IO: |
| 1671 | return nvme_submit_io(ns, (void __user *)arg); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1672 | case SG_GET_VERSION_NUM: |
| 1673 | return nvme_sg_get_version_num((void __user *)arg); |
| 1674 | case SG_IO: |
| 1675 | return nvme_sg_io(ns, (void __user *)arg); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1676 | default: |
| 1677 | return -ENOTTY; |
| 1678 | } |
| 1679 | } |
| 1680 | |
Keith Busch | 320a382 | 2013-10-23 13:07:34 -0600 | [diff] [blame] | 1681 | #ifdef CONFIG_COMPAT |
| 1682 | static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 1683 | unsigned int cmd, unsigned long arg) |
| 1684 | { |
| 1685 | struct nvme_ns *ns = bdev->bd_disk->private_data; |
| 1686 | |
| 1687 | switch (cmd) { |
| 1688 | case SG_IO: |
| 1689 | return nvme_sg_io32(ns, arg); |
| 1690 | } |
| 1691 | return nvme_ioctl(bdev, mode, cmd, arg); |
| 1692 | } |
| 1693 | #else |
| 1694 | #define nvme_compat_ioctl NULL |
| 1695 | #endif |
| 1696 | |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 1697 | static int nvme_open(struct block_device *bdev, fmode_t mode) |
| 1698 | { |
| 1699 | struct nvme_ns *ns = bdev->bd_disk->private_data; |
| 1700 | struct nvme_dev *dev = ns->dev; |
| 1701 | |
| 1702 | kref_get(&dev->kref); |
| 1703 | return 0; |
| 1704 | } |
| 1705 | |
| 1706 | static void nvme_free_dev(struct kref *kref); |
| 1707 | |
| 1708 | static void nvme_release(struct gendisk *disk, fmode_t mode) |
| 1709 | { |
| 1710 | struct nvme_ns *ns = disk->private_data; |
| 1711 | struct nvme_dev *dev = ns->dev; |
| 1712 | |
| 1713 | kref_put(&dev->kref, nvme_free_dev); |
| 1714 | } |
| 1715 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1716 | static const struct block_device_operations nvme_fops = { |
| 1717 | .owner = THIS_MODULE, |
| 1718 | .ioctl = nvme_ioctl, |
Keith Busch | 320a382 | 2013-10-23 13:07:34 -0600 | [diff] [blame] | 1719 | .compat_ioctl = nvme_compat_ioctl, |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 1720 | .open = nvme_open, |
| 1721 | .release = nvme_release, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1722 | }; |
| 1723 | |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1724 | static void nvme_resubmit_bios(struct nvme_queue *nvmeq) |
| 1725 | { |
| 1726 | while (bio_list_peek(&nvmeq->sq_cong)) { |
| 1727 | struct bio *bio = bio_list_pop(&nvmeq->sq_cong); |
| 1728 | struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data; |
Keith Busch | 427e970 | 2013-04-09 11:59:32 -0600 | [diff] [blame] | 1729 | |
Matthew Wilcox | 3cb967c | 2011-03-16 16:45:49 -0400 | [diff] [blame] | 1730 | if (bio_list_empty(&nvmeq->sq_cong)) |
| 1731 | remove_wait_queue(&nvmeq->sq_full, |
| 1732 | &nvmeq->sq_cong_wait); |
Keith Busch | 427e970 | 2013-04-09 11:59:32 -0600 | [diff] [blame] | 1733 | if (nvme_submit_bio_queue(nvmeq, ns, bio)) { |
| 1734 | if (bio_list_empty(&nvmeq->sq_cong)) |
| 1735 | add_wait_queue(&nvmeq->sq_full, |
| 1736 | &nvmeq->sq_cong_wait); |
| 1737 | bio_list_add_head(&nvmeq->sq_cong, bio); |
| 1738 | break; |
| 1739 | } |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | static int nvme_kthread(void *data) |
| 1744 | { |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 1745 | struct nvme_dev *dev, *next; |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1746 | |
| 1747 | while (!kthread_should_stop()) { |
Arjan van de Ven | 564a232 | 2013-05-01 16:38:23 -0400 | [diff] [blame] | 1748 | set_current_state(TASK_INTERRUPTIBLE); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1749 | spin_lock(&dev_list_lock); |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 1750 | list_for_each_entry_safe(dev, next, &dev_list, node) { |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1751 | int i; |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 1752 | if (readl(&dev->bar->csts) & NVME_CSTS_CFS && |
| 1753 | dev->initialized) { |
| 1754 | if (work_busy(&dev->reset_work)) |
| 1755 | continue; |
| 1756 | list_del_init(&dev->node); |
| 1757 | dev_warn(&dev->pci_dev->dev, |
| 1758 | "Failed status, reset controller\n"); |
Matthew Wilcox | bdfd70f | 2014-01-29 09:33:52 -0500 | [diff] [blame] | 1759 | PREPARE_WORK(&dev->reset_work, |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 1760 | nvme_reset_failed_dev); |
| 1761 | queue_work(nvme_workq, &dev->reset_work); |
| 1762 | continue; |
| 1763 | } |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 1764 | rcu_read_lock(); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1765 | for (i = 0; i < dev->queue_count; i++) { |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 1766 | struct nvme_queue *nvmeq = |
| 1767 | rcu_dereference(dev->queues[i]); |
Matthew Wilcox | 740216f | 2011-02-15 16:28:20 -0500 | [diff] [blame] | 1768 | if (!nvmeq) |
| 1769 | continue; |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1770 | spin_lock_irq(&nvmeq->q_lock); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1771 | if (nvmeq->q_suspended) |
| 1772 | goto unlock; |
Matthew Wilcox | bc57a0f | 2013-06-24 11:56:42 -0400 | [diff] [blame] | 1773 | nvme_process_cq(nvmeq); |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1774 | nvme_cancel_ios(nvmeq, true); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1775 | nvme_resubmit_bios(nvmeq); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1776 | unlock: |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1777 | spin_unlock_irq(&nvmeq->q_lock); |
| 1778 | } |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 1779 | rcu_read_unlock(); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1780 | } |
| 1781 | spin_unlock(&dev_list_lock); |
Arjan van de Ven | acb7aa0 | 2013-02-04 14:44:33 -0800 | [diff] [blame] | 1782 | schedule_timeout(round_jiffies_relative(HZ)); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1783 | } |
| 1784 | return 0; |
| 1785 | } |
| 1786 | |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame] | 1787 | static void nvme_config_discard(struct nvme_ns *ns) |
| 1788 | { |
| 1789 | u32 logical_block_size = queue_logical_block_size(ns->queue); |
| 1790 | ns->queue->limits.discard_zeroes_data = 0; |
| 1791 | ns->queue->limits.discard_alignment = logical_block_size; |
| 1792 | ns->queue->limits.discard_granularity = logical_block_size; |
| 1793 | ns->queue->limits.max_discard_sectors = 0xffffffff; |
| 1794 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue); |
| 1795 | } |
| 1796 | |
Matthew Wilcox | c3bfe71 | 2013-07-08 17:26:25 -0400 | [diff] [blame] | 1797 | static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1798 | struct nvme_id_ns *id, struct nvme_lba_range_type *rt) |
| 1799 | { |
| 1800 | struct nvme_ns *ns; |
| 1801 | struct gendisk *disk; |
| 1802 | int lbaf; |
| 1803 | |
| 1804 | if (rt->attributes & NVME_LBART_ATTRIB_HIDE) |
| 1805 | return NULL; |
| 1806 | |
| 1807 | ns = kzalloc(sizeof(*ns), GFP_KERNEL); |
| 1808 | if (!ns) |
| 1809 | return NULL; |
| 1810 | ns->queue = blk_alloc_queue(GFP_KERNEL); |
| 1811 | if (!ns->queue) |
| 1812 | goto out_free_ns; |
Matthew Wilcox | 4eeb921 | 2012-01-10 14:35:08 -0700 | [diff] [blame] | 1813 | ns->queue->queue_flags = QUEUE_FLAG_DEFAULT; |
| 1814 | queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue); |
| 1815 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1816 | blk_queue_make_request(ns->queue, nvme_make_request); |
| 1817 | ns->dev = dev; |
| 1818 | ns->queue->queuedata = ns; |
| 1819 | |
Matthew Wilcox | 469071a | 2013-12-09 12:58:46 -0500 | [diff] [blame] | 1820 | disk = alloc_disk(0); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1821 | if (!disk) |
| 1822 | goto out_free_queue; |
Matthew Wilcox | 5aff938 | 2011-05-06 08:45:47 -0400 | [diff] [blame] | 1823 | ns->ns_id = nsid; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1824 | ns->disk = disk; |
| 1825 | lbaf = id->flbas & 0xf; |
| 1826 | ns->lba_shift = id->lbaf[lbaf].ds; |
Keith Busch | f410c68 | 2013-04-23 17:23:59 -0600 | [diff] [blame] | 1827 | ns->ms = le16_to_cpu(id->lbaf[lbaf].ms); |
Keith Busch | e9ef463 | 2012-07-24 15:01:04 -0600 | [diff] [blame] | 1828 | blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift); |
Keith Busch | 8fc23e0 | 2012-07-26 11:29:57 -0600 | [diff] [blame] | 1829 | if (dev->max_hw_sectors) |
| 1830 | blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1831 | |
| 1832 | disk->major = nvme_major; |
Matthew Wilcox | 469071a | 2013-12-09 12:58:46 -0500 | [diff] [blame] | 1833 | disk->first_minor = 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1834 | disk->fops = &nvme_fops; |
| 1835 | disk->private_data = ns; |
| 1836 | disk->queue = ns->queue; |
Matthew Wilcox | 388f037 | 2011-02-01 12:49:38 -0500 | [diff] [blame] | 1837 | disk->driverfs_dev = &dev->pci_dev->dev; |
Matthew Wilcox | 469071a | 2013-12-09 12:58:46 -0500 | [diff] [blame] | 1838 | disk->flags = GENHD_FL_EXT_DEVT; |
Matthew Wilcox | 5aff938 | 2011-05-06 08:45:47 -0400 | [diff] [blame] | 1839 | sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1840 | set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9)); |
| 1841 | |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame] | 1842 | if (dev->oncs & NVME_CTRL_ONCS_DSM) |
| 1843 | nvme_config_discard(ns); |
| 1844 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1845 | return ns; |
| 1846 | |
| 1847 | out_free_queue: |
| 1848 | blk_cleanup_queue(ns->queue); |
| 1849 | out_free_ns: |
| 1850 | kfree(ns); |
| 1851 | return NULL; |
| 1852 | } |
| 1853 | |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1854 | static int nvme_find_closest_node(int node) |
| 1855 | { |
| 1856 | int n, val, min_val = INT_MAX, best_node = node; |
| 1857 | |
| 1858 | for_each_online_node(n) { |
| 1859 | if (n == node) |
| 1860 | continue; |
| 1861 | val = node_distance(node, n); |
| 1862 | if (val < min_val) { |
| 1863 | min_val = val; |
| 1864 | best_node = n; |
| 1865 | } |
| 1866 | } |
| 1867 | return best_node; |
| 1868 | } |
| 1869 | |
| 1870 | static void nvme_set_queue_cpus(cpumask_t *qmask, struct nvme_queue *nvmeq, |
| 1871 | int count) |
| 1872 | { |
| 1873 | int cpu; |
| 1874 | for_each_cpu(cpu, qmask) { |
| 1875 | if (cpumask_weight(nvmeq->cpu_mask) >= count) |
| 1876 | break; |
| 1877 | if (!cpumask_test_and_set_cpu(cpu, nvmeq->cpu_mask)) |
| 1878 | *per_cpu_ptr(nvmeq->dev->io_queue, cpu) = nvmeq->qid; |
| 1879 | } |
| 1880 | } |
| 1881 | |
| 1882 | static void nvme_add_cpus(cpumask_t *mask, const cpumask_t *unassigned_cpus, |
| 1883 | const cpumask_t *new_mask, struct nvme_queue *nvmeq, int cpus_per_queue) |
| 1884 | { |
| 1885 | int next_cpu; |
| 1886 | for_each_cpu(next_cpu, new_mask) { |
| 1887 | cpumask_or(mask, mask, get_cpu_mask(next_cpu)); |
| 1888 | cpumask_or(mask, mask, topology_thread_cpumask(next_cpu)); |
| 1889 | cpumask_and(mask, mask, unassigned_cpus); |
| 1890 | nvme_set_queue_cpus(mask, nvmeq, cpus_per_queue); |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | static void nvme_create_io_queues(struct nvme_dev *dev) |
| 1895 | { |
| 1896 | unsigned i, max; |
| 1897 | |
| 1898 | max = min(dev->max_qid, num_online_cpus()); |
| 1899 | for (i = dev->queue_count; i <= max; i++) |
| 1900 | if (!nvme_alloc_queue(dev, i, dev->q_depth, i - 1)) |
| 1901 | break; |
| 1902 | |
| 1903 | max = min(dev->queue_count - 1, num_online_cpus()); |
| 1904 | for (i = dev->online_queues; i <= max; i++) |
| 1905 | if (nvme_create_queue(raw_nvmeq(dev, i), i)) |
| 1906 | break; |
| 1907 | } |
| 1908 | |
| 1909 | /* |
| 1910 | * If there are fewer queues than online cpus, this will try to optimally |
| 1911 | * assign a queue to multiple cpus by grouping cpus that are "close" together: |
| 1912 | * thread siblings, core, socket, closest node, then whatever else is |
| 1913 | * available. |
| 1914 | */ |
| 1915 | static void nvme_assign_io_queues(struct nvme_dev *dev) |
| 1916 | { |
| 1917 | unsigned cpu, cpus_per_queue, queues, remainder, i; |
| 1918 | cpumask_var_t unassigned_cpus; |
| 1919 | |
| 1920 | nvme_create_io_queues(dev); |
| 1921 | |
| 1922 | queues = min(dev->online_queues - 1, num_online_cpus()); |
| 1923 | if (!queues) |
| 1924 | return; |
| 1925 | |
| 1926 | cpus_per_queue = num_online_cpus() / queues; |
| 1927 | remainder = queues - (num_online_cpus() - queues * cpus_per_queue); |
| 1928 | |
| 1929 | if (!alloc_cpumask_var(&unassigned_cpus, GFP_KERNEL)) |
| 1930 | return; |
| 1931 | |
| 1932 | cpumask_copy(unassigned_cpus, cpu_online_mask); |
| 1933 | cpu = cpumask_first(unassigned_cpus); |
| 1934 | for (i = 1; i <= queues; i++) { |
| 1935 | struct nvme_queue *nvmeq = lock_nvmeq(dev, i); |
| 1936 | cpumask_t mask; |
| 1937 | |
| 1938 | cpumask_clear(nvmeq->cpu_mask); |
| 1939 | if (!cpumask_weight(unassigned_cpus)) { |
| 1940 | unlock_nvmeq(nvmeq); |
| 1941 | break; |
| 1942 | } |
| 1943 | |
| 1944 | mask = *get_cpu_mask(cpu); |
| 1945 | nvme_set_queue_cpus(&mask, nvmeq, cpus_per_queue); |
| 1946 | if (cpus_weight(mask) < cpus_per_queue) |
| 1947 | nvme_add_cpus(&mask, unassigned_cpus, |
| 1948 | topology_thread_cpumask(cpu), |
| 1949 | nvmeq, cpus_per_queue); |
| 1950 | if (cpus_weight(mask) < cpus_per_queue) |
| 1951 | nvme_add_cpus(&mask, unassigned_cpus, |
| 1952 | topology_core_cpumask(cpu), |
| 1953 | nvmeq, cpus_per_queue); |
| 1954 | if (cpus_weight(mask) < cpus_per_queue) |
| 1955 | nvme_add_cpus(&mask, unassigned_cpus, |
| 1956 | cpumask_of_node(cpu_to_node(cpu)), |
| 1957 | nvmeq, cpus_per_queue); |
| 1958 | if (cpus_weight(mask) < cpus_per_queue) |
| 1959 | nvme_add_cpus(&mask, unassigned_cpus, |
| 1960 | cpumask_of_node( |
| 1961 | nvme_find_closest_node( |
| 1962 | cpu_to_node(cpu))), |
| 1963 | nvmeq, cpus_per_queue); |
| 1964 | if (cpus_weight(mask) < cpus_per_queue) |
| 1965 | nvme_add_cpus(&mask, unassigned_cpus, |
| 1966 | unassigned_cpus, |
| 1967 | nvmeq, cpus_per_queue); |
| 1968 | |
| 1969 | WARN(cpumask_weight(nvmeq->cpu_mask) != cpus_per_queue, |
| 1970 | "nvme%d qid:%d mis-matched queue-to-cpu assignment\n", |
| 1971 | dev->instance, i); |
| 1972 | |
| 1973 | irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector, |
| 1974 | nvmeq->cpu_mask); |
| 1975 | cpumask_andnot(unassigned_cpus, unassigned_cpus, |
| 1976 | nvmeq->cpu_mask); |
| 1977 | cpu = cpumask_next(cpu, unassigned_cpus); |
| 1978 | if (remainder && !--remainder) |
| 1979 | cpus_per_queue++; |
| 1980 | unlock_nvmeq(nvmeq); |
| 1981 | } |
| 1982 | WARN(cpumask_weight(unassigned_cpus), "nvme%d unassigned online cpus\n", |
| 1983 | dev->instance); |
| 1984 | i = 0; |
| 1985 | cpumask_andnot(unassigned_cpus, cpu_possible_mask, cpu_online_mask); |
| 1986 | for_each_cpu(cpu, unassigned_cpus) |
| 1987 | *per_cpu_ptr(dev->io_queue, cpu) = (i++ % queues) + 1; |
| 1988 | free_cpumask_var(unassigned_cpus); |
| 1989 | } |
| 1990 | |
Matthew Wilcox | b3b0681 | 2011-01-20 09:14:34 -0500 | [diff] [blame] | 1991 | static int set_queue_count(struct nvme_dev *dev, int count) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1992 | { |
| 1993 | int status; |
| 1994 | u32 result; |
Matthew Wilcox | b3b0681 | 2011-01-20 09:14:34 -0500 | [diff] [blame] | 1995 | u32 q_count = (count - 1) | ((count - 1) << 16); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1996 | |
Matthew Wilcox | df34813 | 2012-01-11 07:29:56 -0700 | [diff] [blame] | 1997 | status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0, |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1998 | &result); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1999 | if (status) |
Keith Busch | 7e03b12 | 2013-07-29 16:20:56 -0600 | [diff] [blame] | 2000 | return status < 0 ? -EIO : -EBUSY; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2001 | return min(result & 0xffff, result >> 16) + 1; |
| 2002 | } |
| 2003 | |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 2004 | static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues) |
| 2005 | { |
Haiyan Hu | b80d5cc | 2013-09-10 11:25:37 +0800 | [diff] [blame] | 2006 | return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride); |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 2007 | } |
| 2008 | |
Keith Busch | 33b1e95 | 2014-03-24 10:46:26 -0600 | [diff] [blame] | 2009 | static int nvme_cpu_notify(struct notifier_block *self, |
| 2010 | unsigned long action, void *hcpu) |
| 2011 | { |
| 2012 | struct nvme_dev *dev = container_of(self, struct nvme_dev, nb); |
| 2013 | switch (action) { |
| 2014 | case CPU_ONLINE: |
| 2015 | case CPU_DEAD: |
| 2016 | nvme_assign_io_queues(dev); |
| 2017 | break; |
| 2018 | } |
| 2019 | return NOTIFY_OK; |
| 2020 | } |
| 2021 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 2022 | static int nvme_setup_io_queues(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2023 | { |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 2024 | struct nvme_queue *adminq = raw_nvmeq(dev, 0); |
Ramachandra Rao Gajula | fa08a39 | 2013-05-11 15:19:31 -0700 | [diff] [blame] | 2025 | struct pci_dev *pdev = dev->pci_dev; |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2026 | int result, i, vecs, nr_io_queues, size; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2027 | |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2028 | nr_io_queues = num_possible_cpus(); |
Matthew Wilcox | b348b7d | 2011-02-15 16:16:02 -0500 | [diff] [blame] | 2029 | result = set_queue_count(dev, nr_io_queues); |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 2030 | if (result < 0) |
| 2031 | return result; |
Matthew Wilcox | b348b7d | 2011-02-15 16:16:02 -0500 | [diff] [blame] | 2032 | if (result < nr_io_queues) |
| 2033 | nr_io_queues = result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2034 | |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 2035 | size = db_bar_size(dev, nr_io_queues); |
| 2036 | if (size > 8192) { |
Matthew Wilcox | f1938f6 | 2011-10-20 17:00:41 -0400 | [diff] [blame] | 2037 | iounmap(dev->bar); |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 2038 | do { |
| 2039 | dev->bar = ioremap(pci_resource_start(pdev, 0), size); |
| 2040 | if (dev->bar) |
| 2041 | break; |
| 2042 | if (!--nr_io_queues) |
| 2043 | return -ENOMEM; |
| 2044 | size = db_bar_size(dev, nr_io_queues); |
| 2045 | } while (1); |
Matthew Wilcox | f1938f6 | 2011-10-20 17:00:41 -0400 | [diff] [blame] | 2046 | dev->dbs = ((void __iomem *)dev->bar) + 4096; |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 2047 | adminq->q_db = dev->dbs; |
Matthew Wilcox | f1938f6 | 2011-10-20 17:00:41 -0400 | [diff] [blame] | 2048 | } |
| 2049 | |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 2050 | /* Deregister the admin queue's interrupt */ |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 2051 | free_irq(dev->entry[0].vector, adminq); |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 2052 | |
Matthew Wilcox | 063a809 | 2013-06-20 10:53:48 -0400 | [diff] [blame] | 2053 | vecs = nr_io_queues; |
| 2054 | for (i = 0; i < vecs; i++) |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 2055 | dev->entry[i].entry = i; |
| 2056 | for (;;) { |
Matthew Wilcox | 063a809 | 2013-06-20 10:53:48 -0400 | [diff] [blame] | 2057 | result = pci_enable_msix(pdev, dev->entry, vecs); |
| 2058 | if (result <= 0) |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 2059 | break; |
Matthew Wilcox | 063a809 | 2013-06-20 10:53:48 -0400 | [diff] [blame] | 2060 | vecs = result; |
| 2061 | } |
| 2062 | |
| 2063 | if (result < 0) { |
| 2064 | vecs = nr_io_queues; |
| 2065 | if (vecs > 32) |
| 2066 | vecs = 32; |
| 2067 | for (;;) { |
| 2068 | result = pci_enable_msi_block(pdev, vecs); |
| 2069 | if (result == 0) { |
| 2070 | for (i = 0; i < vecs; i++) |
| 2071 | dev->entry[i].vector = i + pdev->irq; |
| 2072 | break; |
| 2073 | } else if (result < 0) { |
| 2074 | vecs = 1; |
| 2075 | break; |
| 2076 | } |
| 2077 | vecs = result; |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 2078 | } |
| 2079 | } |
| 2080 | |
Matthew Wilcox | 063a809 | 2013-06-20 10:53:48 -0400 | [diff] [blame] | 2081 | /* |
| 2082 | * Should investigate if there's a performance win from allocating |
| 2083 | * more queues than interrupt vectors; it might allow the submission |
| 2084 | * path to scale better, even if the receive path is limited by the |
| 2085 | * number of interrupts. |
| 2086 | */ |
| 2087 | nr_io_queues = vecs; |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2088 | dev->max_qid = nr_io_queues; |
Ramachandra Rao Gajula | fa08a39 | 2013-05-11 15:19:31 -0700 | [diff] [blame] | 2089 | |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 2090 | result = queue_request_irq(dev, adminq, adminq->irqname); |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 2091 | if (result) { |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 2092 | adminq->q_suspended = 1; |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 2093 | goto free_queues; |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 2094 | } |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 2095 | |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 2096 | /* Free previously allocated queues that are no longer usable */ |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2097 | nvme_free_queues(dev, nr_io_queues + 1); |
| 2098 | nvme_assign_io_queues(dev); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 2099 | |
Keith Busch | 33b1e95 | 2014-03-24 10:46:26 -0600 | [diff] [blame] | 2100 | dev->nb.notifier_call = &nvme_cpu_notify; |
| 2101 | result = register_hotcpu_notifier(&dev->nb); |
| 2102 | if (result) |
| 2103 | goto free_queues; |
| 2104 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2105 | return 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2106 | |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 2107 | free_queues: |
Keith Busch | a1a5ef9 | 2013-12-16 13:50:00 -0500 | [diff] [blame] | 2108 | nvme_free_queues(dev, 1); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 2109 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2110 | } |
| 2111 | |
Matthew Wilcox | 422ef0c | 2013-04-16 11:22:36 -0400 | [diff] [blame] | 2112 | /* |
| 2113 | * Return: error value if an error occurred setting up the queues or calling |
| 2114 | * Identify Device. 0 if these succeeded, even if adding some of the |
| 2115 | * namespaces failed. At the moment, these failures are silent. TBD which |
| 2116 | * failures should be reported. |
| 2117 | */ |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 2118 | static int nvme_dev_add(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2119 | { |
Matthew Wilcox | 68608c26 | 2013-06-21 14:36:34 -0400 | [diff] [blame] | 2120 | struct pci_dev *pdev = dev->pci_dev; |
Matthew Wilcox | c3bfe71 | 2013-07-08 17:26:25 -0400 | [diff] [blame] | 2121 | int res; |
| 2122 | unsigned nn, i; |
Keith Busch | cbb6218 | 2013-05-01 13:07:49 -0600 | [diff] [blame] | 2123 | struct nvme_ns *ns; |
Matthew Wilcox | 5181423 | 2011-02-01 16:18:08 -0500 | [diff] [blame] | 2124 | struct nvme_id_ctrl *ctrl; |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 2125 | struct nvme_id_ns *id_ns; |
| 2126 | void *mem; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2127 | dma_addr_t dma_addr; |
Keith Busch | 159b67d | 2013-04-09 17:13:20 -0600 | [diff] [blame] | 2128 | int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2129 | |
Matthew Wilcox | 68608c26 | 2013-06-21 14:36:34 -0400 | [diff] [blame] | 2130 | mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL); |
Keith Busch | a9ef434 | 2013-05-01 13:07:48 -0600 | [diff] [blame] | 2131 | if (!mem) |
| 2132 | return -ENOMEM; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2133 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 2134 | res = nvme_identify(dev, 0, 1, dma_addr); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2135 | if (res) { |
| 2136 | res = -EIO; |
Keith Busch | cbb6218 | 2013-05-01 13:07:49 -0600 | [diff] [blame] | 2137 | goto out; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2138 | } |
| 2139 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 2140 | ctrl = mem; |
Matthew Wilcox | 5181423 | 2011-02-01 16:18:08 -0500 | [diff] [blame] | 2141 | nn = le32_to_cpup(&ctrl->nn); |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame] | 2142 | dev->oncs = le16_to_cpup(&ctrl->oncs); |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 2143 | dev->abort_limit = ctrl->acl + 1; |
Matthew Wilcox | 5181423 | 2011-02-01 16:18:08 -0500 | [diff] [blame] | 2144 | memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn)); |
| 2145 | memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn)); |
| 2146 | memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr)); |
Keith Busch | 159b67d | 2013-04-09 17:13:20 -0600 | [diff] [blame] | 2147 | if (ctrl->mdts) |
Keith Busch | 8fc23e0 | 2012-07-26 11:29:57 -0600 | [diff] [blame] | 2148 | dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9); |
Matthew Wilcox | 68608c26 | 2013-06-21 14:36:34 -0400 | [diff] [blame] | 2149 | if ((pdev->vendor == PCI_VENDOR_ID_INTEL) && |
| 2150 | (pdev->device == 0x0953) && ctrl->vs[3]) |
Keith Busch | 159b67d | 2013-04-09 17:13:20 -0600 | [diff] [blame] | 2151 | dev->stripe_size = 1 << (ctrl->vs[3] + shift); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2152 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 2153 | id_ns = mem; |
Matthew Wilcox | 2b2c189 | 2011-10-07 13:10:13 -0400 | [diff] [blame] | 2154 | for (i = 1; i <= nn; i++) { |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 2155 | res = nvme_identify(dev, i, 0, dma_addr); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2156 | if (res) |
| 2157 | continue; |
| 2158 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 2159 | if (id_ns->ncap == 0) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2160 | continue; |
| 2161 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 2162 | res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i, |
Keith Busch | 08df1e0 | 2012-09-21 10:52:13 -0600 | [diff] [blame] | 2163 | dma_addr + 4096, NULL); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2164 | if (res) |
Keith Busch | 1220903 | 2013-01-31 14:40:38 -0700 | [diff] [blame] | 2165 | memset(mem + 4096, 0, 4096); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2166 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 2167 | ns = nvme_alloc_ns(dev, i, mem, mem + 4096); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2168 | if (ns) |
| 2169 | list_add_tail(&ns->list, &dev->namespaces); |
| 2170 | } |
| 2171 | list_for_each_entry(ns, &dev->namespaces, list) |
| 2172 | add_disk(ns->disk); |
Matthew Wilcox | 422ef0c | 2013-04-16 11:22:36 -0400 | [diff] [blame] | 2173 | res = 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2174 | |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 2175 | out: |
Matthew Wilcox | 684f5c2 | 2011-09-19 17:14:53 -0400 | [diff] [blame] | 2176 | dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2177 | return res; |
| 2178 | } |
| 2179 | |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2180 | static int nvme_dev_map(struct nvme_dev *dev) |
| 2181 | { |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2182 | u64 cap; |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2183 | int bars, result = -ENOMEM; |
| 2184 | struct pci_dev *pdev = dev->pci_dev; |
| 2185 | |
| 2186 | if (pci_enable_device_mem(pdev)) |
| 2187 | return result; |
| 2188 | |
| 2189 | dev->entry[0].vector = pdev->irq; |
| 2190 | pci_set_master(pdev); |
| 2191 | bars = pci_select_bars(pdev, IORESOURCE_MEM); |
| 2192 | if (pci_request_selected_regions(pdev, bars, "nvme")) |
| 2193 | goto disable_pci; |
| 2194 | |
Russell King | 052d0ef | 2013-06-26 23:49:11 +0100 | [diff] [blame] | 2195 | if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) && |
| 2196 | dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) |
| 2197 | goto disable; |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2198 | |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2199 | dev->bar = ioremap(pci_resource_start(pdev, 0), 8192); |
| 2200 | if (!dev->bar) |
| 2201 | goto disable; |
Keith Busch | 0e53d18 | 2013-12-10 13:10:39 -0700 | [diff] [blame] | 2202 | if (readl(&dev->bar->csts) == -1) { |
| 2203 | result = -ENODEV; |
| 2204 | goto unmap; |
| 2205 | } |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2206 | cap = readq(&dev->bar->cap); |
| 2207 | dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH); |
| 2208 | dev->db_stride = 1 << NVME_CAP_STRIDE(cap); |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2209 | dev->dbs = ((void __iomem *)dev->bar) + 4096; |
| 2210 | |
| 2211 | return 0; |
| 2212 | |
Keith Busch | 0e53d18 | 2013-12-10 13:10:39 -0700 | [diff] [blame] | 2213 | unmap: |
| 2214 | iounmap(dev->bar); |
| 2215 | dev->bar = NULL; |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2216 | disable: |
| 2217 | pci_release_regions(pdev); |
| 2218 | disable_pci: |
| 2219 | pci_disable_device(pdev); |
| 2220 | return result; |
| 2221 | } |
| 2222 | |
| 2223 | static void nvme_dev_unmap(struct nvme_dev *dev) |
| 2224 | { |
| 2225 | if (dev->pci_dev->msi_enabled) |
| 2226 | pci_disable_msi(dev->pci_dev); |
| 2227 | else if (dev->pci_dev->msix_enabled) |
| 2228 | pci_disable_msix(dev->pci_dev); |
| 2229 | |
| 2230 | if (dev->bar) { |
| 2231 | iounmap(dev->bar); |
| 2232 | dev->bar = NULL; |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2233 | pci_release_regions(dev->pci_dev); |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2234 | } |
| 2235 | |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2236 | if (pci_is_enabled(dev->pci_dev)) |
| 2237 | pci_disable_device(dev->pci_dev); |
| 2238 | } |
| 2239 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2240 | struct nvme_delq_ctx { |
| 2241 | struct task_struct *waiter; |
| 2242 | struct kthread_worker *worker; |
| 2243 | atomic_t refcount; |
| 2244 | }; |
| 2245 | |
| 2246 | static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev) |
| 2247 | { |
| 2248 | dq->waiter = current; |
| 2249 | mb(); |
| 2250 | |
| 2251 | for (;;) { |
| 2252 | set_current_state(TASK_KILLABLE); |
| 2253 | if (!atomic_read(&dq->refcount)) |
| 2254 | break; |
| 2255 | if (!schedule_timeout(ADMIN_TIMEOUT) || |
| 2256 | fatal_signal_pending(current)) { |
| 2257 | set_current_state(TASK_RUNNING); |
| 2258 | |
| 2259 | nvme_disable_ctrl(dev, readq(&dev->bar->cap)); |
| 2260 | nvme_disable_queue(dev, 0); |
| 2261 | |
| 2262 | send_sig(SIGKILL, dq->worker->task, 1); |
| 2263 | flush_kthread_worker(dq->worker); |
| 2264 | return; |
| 2265 | } |
| 2266 | } |
| 2267 | set_current_state(TASK_RUNNING); |
| 2268 | } |
| 2269 | |
| 2270 | static void nvme_put_dq(struct nvme_delq_ctx *dq) |
| 2271 | { |
| 2272 | atomic_dec(&dq->refcount); |
| 2273 | if (dq->waiter) |
| 2274 | wake_up_process(dq->waiter); |
| 2275 | } |
| 2276 | |
| 2277 | static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq) |
| 2278 | { |
| 2279 | atomic_inc(&dq->refcount); |
| 2280 | return dq; |
| 2281 | } |
| 2282 | |
| 2283 | static void nvme_del_queue_end(struct nvme_queue *nvmeq) |
| 2284 | { |
| 2285 | struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx; |
| 2286 | |
| 2287 | nvme_clear_queue(nvmeq); |
| 2288 | nvme_put_dq(dq); |
| 2289 | } |
| 2290 | |
| 2291 | static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode, |
| 2292 | kthread_work_func_t fn) |
| 2293 | { |
| 2294 | struct nvme_command c; |
| 2295 | |
| 2296 | memset(&c, 0, sizeof(c)); |
| 2297 | c.delete_queue.opcode = opcode; |
| 2298 | c.delete_queue.qid = cpu_to_le16(nvmeq->qid); |
| 2299 | |
| 2300 | init_kthread_work(&nvmeq->cmdinfo.work, fn); |
| 2301 | return nvme_submit_admin_cmd_async(nvmeq->dev, &c, &nvmeq->cmdinfo); |
| 2302 | } |
| 2303 | |
| 2304 | static void nvme_del_cq_work_handler(struct kthread_work *work) |
| 2305 | { |
| 2306 | struct nvme_queue *nvmeq = container_of(work, struct nvme_queue, |
| 2307 | cmdinfo.work); |
| 2308 | nvme_del_queue_end(nvmeq); |
| 2309 | } |
| 2310 | |
| 2311 | static int nvme_delete_cq(struct nvme_queue *nvmeq) |
| 2312 | { |
| 2313 | return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq, |
| 2314 | nvme_del_cq_work_handler); |
| 2315 | } |
| 2316 | |
| 2317 | static void nvme_del_sq_work_handler(struct kthread_work *work) |
| 2318 | { |
| 2319 | struct nvme_queue *nvmeq = container_of(work, struct nvme_queue, |
| 2320 | cmdinfo.work); |
| 2321 | int status = nvmeq->cmdinfo.status; |
| 2322 | |
| 2323 | if (!status) |
| 2324 | status = nvme_delete_cq(nvmeq); |
| 2325 | if (status) |
| 2326 | nvme_del_queue_end(nvmeq); |
| 2327 | } |
| 2328 | |
| 2329 | static int nvme_delete_sq(struct nvme_queue *nvmeq) |
| 2330 | { |
| 2331 | return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq, |
| 2332 | nvme_del_sq_work_handler); |
| 2333 | } |
| 2334 | |
| 2335 | static void nvme_del_queue_start(struct kthread_work *work) |
| 2336 | { |
| 2337 | struct nvme_queue *nvmeq = container_of(work, struct nvme_queue, |
| 2338 | cmdinfo.work); |
| 2339 | allow_signal(SIGKILL); |
| 2340 | if (nvme_delete_sq(nvmeq)) |
| 2341 | nvme_del_queue_end(nvmeq); |
| 2342 | } |
| 2343 | |
| 2344 | static void nvme_disable_io_queues(struct nvme_dev *dev) |
| 2345 | { |
| 2346 | int i; |
| 2347 | DEFINE_KTHREAD_WORKER_ONSTACK(worker); |
| 2348 | struct nvme_delq_ctx dq; |
| 2349 | struct task_struct *kworker_task = kthread_run(kthread_worker_fn, |
| 2350 | &worker, "nvme%d", dev->instance); |
| 2351 | |
| 2352 | if (IS_ERR(kworker_task)) { |
| 2353 | dev_err(&dev->pci_dev->dev, |
| 2354 | "Failed to create queue del task\n"); |
| 2355 | for (i = dev->queue_count - 1; i > 0; i--) |
| 2356 | nvme_disable_queue(dev, i); |
| 2357 | return; |
| 2358 | } |
| 2359 | |
| 2360 | dq.waiter = NULL; |
| 2361 | atomic_set(&dq.refcount, 0); |
| 2362 | dq.worker = &worker; |
| 2363 | for (i = dev->queue_count - 1; i > 0; i--) { |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 2364 | struct nvme_queue *nvmeq = raw_nvmeq(dev, i); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2365 | |
| 2366 | if (nvme_suspend_queue(nvmeq)) |
| 2367 | continue; |
| 2368 | nvmeq->cmdinfo.ctx = nvme_get_dq(&dq); |
| 2369 | nvmeq->cmdinfo.worker = dq.worker; |
| 2370 | init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start); |
| 2371 | queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work); |
| 2372 | } |
| 2373 | nvme_wait_dq(&dq, dev); |
| 2374 | kthread_stop(kworker_task); |
| 2375 | } |
| 2376 | |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2377 | static void nvme_dev_shutdown(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2378 | { |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 2379 | int i; |
| 2380 | |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 2381 | dev->initialized = 0; |
Keith Busch | 33b1e95 | 2014-03-24 10:46:26 -0600 | [diff] [blame] | 2382 | unregister_hotcpu_notifier(&dev->nb); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2383 | |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 2384 | spin_lock(&dev_list_lock); |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2385 | list_del_init(&dev->node); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 2386 | spin_unlock(&dev_list_lock); |
| 2387 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2388 | if (!dev->bar || (dev->bar && readl(&dev->bar->csts) == -1)) { |
| 2389 | for (i = dev->queue_count - 1; i >= 0; i--) { |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 2390 | struct nvme_queue *nvmeq = raw_nvmeq(dev, i); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2391 | nvme_suspend_queue(nvmeq); |
| 2392 | nvme_clear_queue(nvmeq); |
| 2393 | } |
| 2394 | } else { |
| 2395 | nvme_disable_io_queues(dev); |
Keith Busch | 1894d8f | 2013-07-15 15:02:22 -0600 | [diff] [blame] | 2396 | nvme_shutdown_ctrl(dev); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2397 | nvme_disable_queue(dev, 0); |
| 2398 | } |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2399 | nvme_dev_unmap(dev); |
| 2400 | } |
| 2401 | |
| 2402 | static void nvme_dev_remove(struct nvme_dev *dev) |
| 2403 | { |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 2404 | struct nvme_ns *ns; |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2405 | |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 2406 | list_for_each_entry(ns, &dev->namespaces, list) { |
| 2407 | if (ns->disk->flags & GENHD_FL_UP) |
| 2408 | del_gendisk(ns->disk); |
| 2409 | if (!blk_queue_dying(ns->queue)) |
| 2410 | blk_cleanup_queue(ns->queue); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2411 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2412 | } |
| 2413 | |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 2414 | static int nvme_setup_prp_pools(struct nvme_dev *dev) |
| 2415 | { |
| 2416 | struct device *dmadev = &dev->pci_dev->dev; |
| 2417 | dev->prp_page_pool = dma_pool_create("prp list page", dmadev, |
| 2418 | PAGE_SIZE, PAGE_SIZE, 0); |
| 2419 | if (!dev->prp_page_pool) |
| 2420 | return -ENOMEM; |
| 2421 | |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 2422 | /* Optimisation for I/Os between 4k and 128k */ |
| 2423 | dev->prp_small_pool = dma_pool_create("prp list 256", dmadev, |
| 2424 | 256, 256, 0); |
| 2425 | if (!dev->prp_small_pool) { |
| 2426 | dma_pool_destroy(dev->prp_page_pool); |
| 2427 | return -ENOMEM; |
| 2428 | } |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 2429 | return 0; |
| 2430 | } |
| 2431 | |
| 2432 | static void nvme_release_prp_pools(struct nvme_dev *dev) |
| 2433 | { |
| 2434 | dma_pool_destroy(dev->prp_page_pool); |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 2435 | dma_pool_destroy(dev->prp_small_pool); |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 2436 | } |
| 2437 | |
Quoc-Son Anh | cd58ad7 | 2012-02-21 16:50:53 -0700 | [diff] [blame] | 2438 | static DEFINE_IDA(nvme_instance_ida); |
| 2439 | |
| 2440 | static int nvme_set_instance(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2441 | { |
Quoc-Son Anh | cd58ad7 | 2012-02-21 16:50:53 -0700 | [diff] [blame] | 2442 | int instance, error; |
| 2443 | |
| 2444 | do { |
| 2445 | if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL)) |
| 2446 | return -ENODEV; |
| 2447 | |
| 2448 | spin_lock(&dev_list_lock); |
| 2449 | error = ida_get_new(&nvme_instance_ida, &instance); |
| 2450 | spin_unlock(&dev_list_lock); |
| 2451 | } while (error == -EAGAIN); |
| 2452 | |
| 2453 | if (error) |
| 2454 | return -ENODEV; |
| 2455 | |
| 2456 | dev->instance = instance; |
| 2457 | return 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | static void nvme_release_instance(struct nvme_dev *dev) |
| 2461 | { |
Quoc-Son Anh | cd58ad7 | 2012-02-21 16:50:53 -0700 | [diff] [blame] | 2462 | spin_lock(&dev_list_lock); |
| 2463 | ida_remove(&nvme_instance_ida, dev->instance); |
| 2464 | spin_unlock(&dev_list_lock); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2465 | } |
| 2466 | |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 2467 | static void nvme_free_namespaces(struct nvme_dev *dev) |
| 2468 | { |
| 2469 | struct nvme_ns *ns, *next; |
| 2470 | |
| 2471 | list_for_each_entry_safe(ns, next, &dev->namespaces, list) { |
| 2472 | list_del(&ns->list); |
| 2473 | put_disk(ns->disk); |
| 2474 | kfree(ns); |
| 2475 | } |
| 2476 | } |
| 2477 | |
Keith Busch | 5e82e95 | 2013-02-19 10:17:58 -0700 | [diff] [blame] | 2478 | static void nvme_free_dev(struct kref *kref) |
| 2479 | { |
| 2480 | struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref); |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 2481 | |
| 2482 | nvme_free_namespaces(dev); |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2483 | free_percpu(dev->io_queue); |
Keith Busch | 5e82e95 | 2013-02-19 10:17:58 -0700 | [diff] [blame] | 2484 | kfree(dev->queues); |
| 2485 | kfree(dev->entry); |
| 2486 | kfree(dev); |
| 2487 | } |
| 2488 | |
| 2489 | static int nvme_dev_open(struct inode *inode, struct file *f) |
| 2490 | { |
| 2491 | struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev, |
| 2492 | miscdev); |
| 2493 | kref_get(&dev->kref); |
| 2494 | f->private_data = dev; |
| 2495 | return 0; |
| 2496 | } |
| 2497 | |
| 2498 | static int nvme_dev_release(struct inode *inode, struct file *f) |
| 2499 | { |
| 2500 | struct nvme_dev *dev = f->private_data; |
| 2501 | kref_put(&dev->kref, nvme_free_dev); |
| 2502 | return 0; |
| 2503 | } |
| 2504 | |
| 2505 | static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg) |
| 2506 | { |
| 2507 | struct nvme_dev *dev = f->private_data; |
| 2508 | switch (cmd) { |
| 2509 | case NVME_IOCTL_ADMIN_CMD: |
| 2510 | return nvme_user_admin_cmd(dev, (void __user *)arg); |
| 2511 | default: |
| 2512 | return -ENOTTY; |
| 2513 | } |
| 2514 | } |
| 2515 | |
| 2516 | static const struct file_operations nvme_dev_fops = { |
| 2517 | .owner = THIS_MODULE, |
| 2518 | .open = nvme_dev_open, |
| 2519 | .release = nvme_dev_release, |
| 2520 | .unlocked_ioctl = nvme_dev_ioctl, |
| 2521 | .compat_ioctl = nvme_dev_ioctl, |
| 2522 | }; |
| 2523 | |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2524 | static int nvme_dev_start(struct nvme_dev *dev) |
| 2525 | { |
| 2526 | int result; |
| 2527 | |
| 2528 | result = nvme_dev_map(dev); |
| 2529 | if (result) |
| 2530 | return result; |
| 2531 | |
| 2532 | result = nvme_configure_admin_queue(dev); |
| 2533 | if (result) |
| 2534 | goto unmap; |
| 2535 | |
| 2536 | spin_lock(&dev_list_lock); |
| 2537 | list_add(&dev->node, &dev_list); |
| 2538 | spin_unlock(&dev_list_lock); |
| 2539 | |
| 2540 | result = nvme_setup_io_queues(dev); |
Keith Busch | d82e8bf | 2013-09-05 14:45:07 -0600 | [diff] [blame] | 2541 | if (result && result != -EBUSY) |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2542 | goto disable; |
| 2543 | |
Keith Busch | d82e8bf | 2013-09-05 14:45:07 -0600 | [diff] [blame] | 2544 | return result; |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2545 | |
| 2546 | disable: |
Keith Busch | a1a5ef9 | 2013-12-16 13:50:00 -0500 | [diff] [blame] | 2547 | nvme_disable_queue(dev, 0); |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2548 | spin_lock(&dev_list_lock); |
| 2549 | list_del_init(&dev->node); |
| 2550 | spin_unlock(&dev_list_lock); |
| 2551 | unmap: |
| 2552 | nvme_dev_unmap(dev); |
| 2553 | return result; |
| 2554 | } |
| 2555 | |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2556 | static int nvme_remove_dead_ctrl(void *arg) |
| 2557 | { |
| 2558 | struct nvme_dev *dev = (struct nvme_dev *)arg; |
| 2559 | struct pci_dev *pdev = dev->pci_dev; |
| 2560 | |
| 2561 | if (pci_get_drvdata(pdev)) |
| 2562 | pci_stop_and_remove_bus_device(pdev); |
| 2563 | kref_put(&dev->kref, nvme_free_dev); |
| 2564 | return 0; |
| 2565 | } |
| 2566 | |
| 2567 | static void nvme_remove_disks(struct work_struct *ws) |
| 2568 | { |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2569 | struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work); |
| 2570 | |
| 2571 | nvme_dev_remove(dev); |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 2572 | nvme_free_queues(dev, 1); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2573 | } |
| 2574 | |
| 2575 | static int nvme_dev_resume(struct nvme_dev *dev) |
| 2576 | { |
| 2577 | int ret; |
| 2578 | |
| 2579 | ret = nvme_dev_start(dev); |
| 2580 | if (ret && ret != -EBUSY) |
| 2581 | return ret; |
| 2582 | if (ret == -EBUSY) { |
| 2583 | spin_lock(&dev_list_lock); |
Matthew Wilcox | bdfd70f | 2014-01-29 09:33:52 -0500 | [diff] [blame] | 2584 | PREPARE_WORK(&dev->reset_work, nvme_remove_disks); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2585 | queue_work(nvme_workq, &dev->reset_work); |
| 2586 | spin_unlock(&dev_list_lock); |
| 2587 | } |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 2588 | dev->initialized = 1; |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2589 | return 0; |
| 2590 | } |
| 2591 | |
| 2592 | static void nvme_dev_reset(struct nvme_dev *dev) |
| 2593 | { |
| 2594 | nvme_dev_shutdown(dev); |
| 2595 | if (nvme_dev_resume(dev)) { |
| 2596 | dev_err(&dev->pci_dev->dev, "Device failed to resume\n"); |
| 2597 | kref_get(&dev->kref); |
| 2598 | if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d", |
| 2599 | dev->instance))) { |
| 2600 | dev_err(&dev->pci_dev->dev, |
| 2601 | "Failed to start controller remove task\n"); |
| 2602 | kref_put(&dev->kref, nvme_free_dev); |
| 2603 | } |
| 2604 | } |
| 2605 | } |
| 2606 | |
| 2607 | static void nvme_reset_failed_dev(struct work_struct *ws) |
| 2608 | { |
| 2609 | struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work); |
| 2610 | nvme_dev_reset(dev); |
| 2611 | } |
| 2612 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 2613 | 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] | 2614 | { |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2615 | int result = -ENOMEM; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2616 | struct nvme_dev *dev; |
| 2617 | |
| 2618 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 2619 | if (!dev) |
| 2620 | return -ENOMEM; |
| 2621 | dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry), |
| 2622 | GFP_KERNEL); |
| 2623 | if (!dev->entry) |
| 2624 | goto free; |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 2625 | dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *), |
| 2626 | GFP_KERNEL); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2627 | if (!dev->queues) |
| 2628 | goto free; |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2629 | dev->io_queue = alloc_percpu(unsigned short); |
| 2630 | if (!dev->io_queue) |
| 2631 | goto free; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2632 | |
| 2633 | INIT_LIST_HEAD(&dev->namespaces); |
Matthew Wilcox | bdfd70f | 2014-01-29 09:33:52 -0500 | [diff] [blame] | 2634 | INIT_WORK(&dev->reset_work, nvme_reset_failed_dev); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2635 | dev->pci_dev = pdev; |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2636 | pci_set_drvdata(pdev, dev); |
Quoc-Son Anh | cd58ad7 | 2012-02-21 16:50:53 -0700 | [diff] [blame] | 2637 | result = nvme_set_instance(dev); |
| 2638 | if (result) |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2639 | goto free; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2640 | |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 2641 | result = nvme_setup_prp_pools(dev); |
| 2642 | if (result) |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2643 | goto release; |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 2644 | |
Keith Busch | fb35e91 | 2014-03-03 11:09:47 -0700 | [diff] [blame] | 2645 | kref_init(&dev->kref); |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2646 | result = nvme_dev_start(dev); |
Keith Busch | d82e8bf | 2013-09-05 14:45:07 -0600 | [diff] [blame] | 2647 | if (result) { |
| 2648 | if (result == -EBUSY) |
| 2649 | goto create_cdev; |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2650 | goto release_pools; |
Keith Busch | d82e8bf | 2013-09-05 14:45:07 -0600 | [diff] [blame] | 2651 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2652 | |
Matthew Wilcox | 740216f | 2011-02-15 16:28:20 -0500 | [diff] [blame] | 2653 | result = nvme_dev_add(dev); |
Keith Busch | d82e8bf | 2013-09-05 14:45:07 -0600 | [diff] [blame] | 2654 | if (result) |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2655 | goto shutdown; |
Matthew Wilcox | 740216f | 2011-02-15 16:28:20 -0500 | [diff] [blame] | 2656 | |
Keith Busch | d82e8bf | 2013-09-05 14:45:07 -0600 | [diff] [blame] | 2657 | create_cdev: |
Keith Busch | 5e82e95 | 2013-02-19 10:17:58 -0700 | [diff] [blame] | 2658 | scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance); |
| 2659 | dev->miscdev.minor = MISC_DYNAMIC_MINOR; |
| 2660 | dev->miscdev.parent = &pdev->dev; |
| 2661 | dev->miscdev.name = dev->name; |
| 2662 | dev->miscdev.fops = &nvme_dev_fops; |
| 2663 | result = misc_register(&dev->miscdev); |
| 2664 | if (result) |
| 2665 | goto remove; |
| 2666 | |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 2667 | dev->initialized = 1; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2668 | return 0; |
| 2669 | |
Keith Busch | 5e82e95 | 2013-02-19 10:17:58 -0700 | [diff] [blame] | 2670 | remove: |
| 2671 | nvme_dev_remove(dev); |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 2672 | nvme_free_namespaces(dev); |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2673 | shutdown: |
| 2674 | nvme_dev_shutdown(dev); |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2675 | release_pools: |
Keith Busch | a1a5ef9 | 2013-12-16 13:50:00 -0500 | [diff] [blame] | 2676 | nvme_free_queues(dev, 0); |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 2677 | nvme_release_prp_pools(dev); |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2678 | release: |
| 2679 | nvme_release_instance(dev); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2680 | free: |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2681 | free_percpu(dev->io_queue); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2682 | kfree(dev->queues); |
| 2683 | kfree(dev->entry); |
| 2684 | kfree(dev); |
| 2685 | return result; |
| 2686 | } |
| 2687 | |
Keith Busch | 09ece14 | 2014-01-27 11:29:40 -0500 | [diff] [blame] | 2688 | static void nvme_shutdown(struct pci_dev *pdev) |
| 2689 | { |
| 2690 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
| 2691 | nvme_dev_shutdown(dev); |
| 2692 | } |
| 2693 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 2694 | static void nvme_remove(struct pci_dev *pdev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2695 | { |
| 2696 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2697 | |
| 2698 | spin_lock(&dev_list_lock); |
| 2699 | list_del_init(&dev->node); |
| 2700 | spin_unlock(&dev_list_lock); |
| 2701 | |
| 2702 | pci_set_drvdata(pdev, NULL); |
| 2703 | flush_work(&dev->reset_work); |
Keith Busch | 5e82e95 | 2013-02-19 10:17:58 -0700 | [diff] [blame] | 2704 | misc_deregister(&dev->miscdev); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2705 | nvme_dev_remove(dev); |
| 2706 | nvme_dev_shutdown(dev); |
Keith Busch | a1a5ef9 | 2013-12-16 13:50:00 -0500 | [diff] [blame] | 2707 | nvme_free_queues(dev, 0); |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 2708 | rcu_barrier(); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2709 | nvme_release_instance(dev); |
| 2710 | nvme_release_prp_pools(dev); |
Keith Busch | 5e82e95 | 2013-02-19 10:17:58 -0700 | [diff] [blame] | 2711 | kref_put(&dev->kref, nvme_free_dev); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2712 | } |
| 2713 | |
| 2714 | /* These functions are yet to be implemented */ |
| 2715 | #define nvme_error_detected NULL |
| 2716 | #define nvme_dump_registers NULL |
| 2717 | #define nvme_link_reset NULL |
| 2718 | #define nvme_slot_reset NULL |
| 2719 | #define nvme_error_resume NULL |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 2720 | |
Jingoo Han | 671a601 | 2014-02-13 11:19:14 +0900 | [diff] [blame] | 2721 | #ifdef CONFIG_PM_SLEEP |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 2722 | static int nvme_suspend(struct device *dev) |
| 2723 | { |
| 2724 | struct pci_dev *pdev = to_pci_dev(dev); |
| 2725 | struct nvme_dev *ndev = pci_get_drvdata(pdev); |
| 2726 | |
| 2727 | nvme_dev_shutdown(ndev); |
| 2728 | return 0; |
| 2729 | } |
| 2730 | |
| 2731 | static int nvme_resume(struct device *dev) |
| 2732 | { |
| 2733 | struct pci_dev *pdev = to_pci_dev(dev); |
| 2734 | struct nvme_dev *ndev = pci_get_drvdata(pdev); |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 2735 | |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2736 | if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) { |
Matthew Wilcox | bdfd70f | 2014-01-29 09:33:52 -0500 | [diff] [blame] | 2737 | PREPARE_WORK(&ndev->reset_work, nvme_reset_failed_dev); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2738 | queue_work(nvme_workq, &ndev->reset_work); |
| 2739 | } |
| 2740 | return 0; |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 2741 | } |
Jingoo Han | 671a601 | 2014-02-13 11:19:14 +0900 | [diff] [blame] | 2742 | #endif |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 2743 | |
| 2744 | static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2745 | |
Stephen Hemminger | 1d35203 | 2012-09-07 09:33:17 -0700 | [diff] [blame] | 2746 | static const struct pci_error_handlers nvme_err_handler = { |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2747 | .error_detected = nvme_error_detected, |
| 2748 | .mmio_enabled = nvme_dump_registers, |
| 2749 | .link_reset = nvme_link_reset, |
| 2750 | .slot_reset = nvme_slot_reset, |
| 2751 | .resume = nvme_error_resume, |
| 2752 | }; |
| 2753 | |
| 2754 | /* Move to pci_ids.h later */ |
| 2755 | #define PCI_CLASS_STORAGE_EXPRESS 0x010802 |
| 2756 | |
Matthew Wilcox | 6eb0d69 | 2014-03-24 10:11:22 -0400 | [diff] [blame] | 2757 | static const struct pci_device_id nvme_id_table[] = { |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2758 | { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) }, |
| 2759 | { 0, } |
| 2760 | }; |
| 2761 | MODULE_DEVICE_TABLE(pci, nvme_id_table); |
| 2762 | |
| 2763 | static struct pci_driver nvme_driver = { |
| 2764 | .name = "nvme", |
| 2765 | .id_table = nvme_id_table, |
| 2766 | .probe = nvme_probe, |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 2767 | .remove = nvme_remove, |
Keith Busch | 09ece14 | 2014-01-27 11:29:40 -0500 | [diff] [blame] | 2768 | .shutdown = nvme_shutdown, |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 2769 | .driver = { |
| 2770 | .pm = &nvme_dev_pm_ops, |
| 2771 | }, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2772 | .err_handler = &nvme_err_handler, |
| 2773 | }; |
| 2774 | |
| 2775 | static int __init nvme_init(void) |
| 2776 | { |
Matthew Wilcox | 0ac1314 | 2012-07-31 13:31:15 -0400 | [diff] [blame] | 2777 | int result; |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 2778 | |
| 2779 | nvme_thread = kthread_run(nvme_kthread, NULL, "nvme"); |
| 2780 | if (IS_ERR(nvme_thread)) |
| 2781 | return PTR_ERR(nvme_thread); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2782 | |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2783 | result = -ENOMEM; |
| 2784 | nvme_workq = create_singlethread_workqueue("nvme"); |
| 2785 | if (!nvme_workq) |
| 2786 | goto kill_kthread; |
| 2787 | |
Keith Busch | 5c42ea1 | 2012-07-25 16:05:18 -0600 | [diff] [blame] | 2788 | result = register_blkdev(nvme_major, "nvme"); |
| 2789 | if (result < 0) |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2790 | goto kill_workq; |
Keith Busch | 5c42ea1 | 2012-07-25 16:05:18 -0600 | [diff] [blame] | 2791 | else if (result > 0) |
Matthew Wilcox | 0ac1314 | 2012-07-31 13:31:15 -0400 | [diff] [blame] | 2792 | nvme_major = result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2793 | |
| 2794 | result = pci_register_driver(&nvme_driver); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 2795 | if (result) |
| 2796 | goto unregister_blkdev; |
| 2797 | return 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2798 | |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 2799 | unregister_blkdev: |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2800 | unregister_blkdev(nvme_major, "nvme"); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2801 | kill_workq: |
| 2802 | destroy_workqueue(nvme_workq); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 2803 | kill_kthread: |
| 2804 | kthread_stop(nvme_thread); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2805 | return result; |
| 2806 | } |
| 2807 | |
| 2808 | static void __exit nvme_exit(void) |
| 2809 | { |
| 2810 | pci_unregister_driver(&nvme_driver); |
| 2811 | unregister_blkdev(nvme_major, "nvme"); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2812 | destroy_workqueue(nvme_workq); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 2813 | kthread_stop(nvme_thread); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2814 | } |
| 2815 | |
| 2816 | MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>"); |
| 2817 | MODULE_LICENSE("GPL"); |
Matthew Wilcox | 6eb0d69 | 2014-03-24 10:11:22 -0400 | [diff] [blame] | 2818 | MODULE_VERSION("0.9"); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2819 | module_init(nvme_init); |
| 2820 | module_exit(nvme_exit); |