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