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