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