blob: 48d7bd55207a452f43f37ba118d8695e05bf260b [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003 * Copyright (c) 2011-2014, Intel Corporation.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#include <linux/nvme.h>
20#include <linux/bio.h>
Matthew Wilcox8de05532011-05-12 13:50:28 -040021#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050022#include <linux/blkdev.h>
Keith Busch42f61422014-03-24 10:46:25 -060023#include <linux/cpu.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040024#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050025#include <linux/errno.h>
26#include <linux/fs.h>
27#include <linux/genhd.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040028#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050029#include <linux/init.h>
30#include <linux/interrupt.h>
31#include <linux/io.h>
32#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050033#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050034#include <linux/kernel.h>
35#include <linux/mm.h>
36#include <linux/module.h>
37#include <linux/moduleparam.h>
38#include <linux/pci.h>
Keith Busch42f61422014-03-24 10:46:25 -060039#include <linux/percpu.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050040#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040041#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050042#include <linux/sched.h>
43#include <linux/slab.h>
44#include <linux/types.h>
Vishal Verma5d0f6132013-03-04 18:40:58 -070045#include <scsi/sg.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090046#include <asm-generic/io-64-nonatomic-lo-hi.h>
47
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050048#define NVME_Q_DEPTH 1024
49#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
50#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Matthew Wilcoxe85248e2011-02-06 18:30:16 -050051#define ADMIN_TIMEOUT (60 * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050052
53static int nvme_major;
54module_param(nvme_major, int, 0);
55
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050056static int use_threaded_interrupts;
57module_param(use_threaded_interrupts, int, 0);
58
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050059static DEFINE_SPINLOCK(dev_list_lock);
60static LIST_HEAD(dev_list);
61static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070062static struct workqueue_struct *nvme_workq;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050063
Keith Buschd4b4ff82013-12-10 13:10:37 -070064static void nvme_reset_failed_dev(struct work_struct *ws);
65
Keith Busch4d115422013-12-10 13:10:40 -070066struct async_cmd_info {
67 struct kthread_work work;
68 struct kthread_worker *worker;
69 u32 result;
70 int status;
71 void *ctx;
72};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050073
74/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050075 * An NVM Express queue. Each device has at least two (one for admin
76 * commands and one for I/O commands).
77 */
78struct nvme_queue {
Keith Busch5a92e702014-02-21 14:13:44 -070079 struct rcu_head r_head;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050080 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -050081 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -050082 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050083 spinlock_t q_lock;
84 struct nvme_command *sq_cmds;
85 volatile struct nvme_completion *cqes;
86 dma_addr_t sq_dma_addr;
87 dma_addr_t cq_dma_addr;
88 wait_queue_head_t sq_full;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050089 wait_queue_t sq_cong_wait;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050090 struct bio_list sq_cong;
91 u32 __iomem *q_db;
92 u16 q_depth;
93 u16 cq_vector;
94 u16 sq_head;
95 u16 sq_tail;
96 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -070097 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -040098 u8 cq_phase;
99 u8 cqe_seen;
Keith Busch22404272013-07-15 15:02:20 -0600100 u8 q_suspended;
Keith Busch42f61422014-03-24 10:46:25 -0600101 cpumask_var_t cpu_mask;
Keith Busch4d115422013-12-10 13:10:40 -0700102 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500103 unsigned long cmdid_data[];
104};
105
106/*
107 * Check we didin't inadvertently grow the command struct
108 */
109static inline void _nvme_check_size(void)
110{
111 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
112 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
113 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
114 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
115 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400116 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700117 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500118 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
119 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
120 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
121 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600122 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500123}
124
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500125typedef void (*nvme_completion_fn)(struct nvme_dev *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400126 struct nvme_completion *);
127
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500128struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400129 nvme_completion_fn fn;
130 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500131 unsigned long timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700132 int aborted;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500133};
134
135static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
136{
137 return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
138}
139
Keith Busch22404272013-07-15 15:02:20 -0600140static unsigned nvme_queue_extra(int depth)
141{
142 return DIV_ROUND_UP(depth, 8) + (depth * sizeof(struct nvme_cmd_info));
143}
144
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500145/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400146 * alloc_cmdid() - Allocate a Command ID
147 * @nvmeq: The queue that will be used for this command
148 * @ctx: A pointer that will be passed to the handler
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400149 * @handler: The function to call on completion
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500150 *
151 * Allocate a Command ID for a queue. The data passed in will
152 * be passed to the completion handler. This is implemented by using
153 * the bottom two bits of the ctx pointer to store the handler ID.
154 * Passing in a pointer that's not 4-byte aligned will cause a BUG.
155 * We can change this if it becomes a problem.
Matthew Wilcox184d2942011-05-11 21:36:38 -0400156 *
157 * May be called with local interrupts disabled and the q_lock held,
158 * or with interrupts enabled and no locks held.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500159 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400160static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx,
161 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500162{
Matthew Wilcoxe6d15f72011-02-24 08:49:41 -0500163 int depth = nvmeq->q_depth - 1;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500164 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500165 int cmdid;
166
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500167 do {
168 cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
169 if (cmdid >= depth)
170 return -EBUSY;
171 } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
172
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400173 info[cmdid].fn = handler;
174 info[cmdid].ctx = ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500175 info[cmdid].timeout = jiffies + timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700176 info[cmdid].aborted = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500177 return cmdid;
178}
179
180static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400181 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500182{
183 int cmdid;
184 wait_event_killable(nvmeq->sq_full,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500185 (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500186 return (cmdid < 0) ? -EINTR : cmdid;
187}
188
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400189/* Special values must be less than 0x1000 */
190#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500191#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
192#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
193#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500194#define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE)
Keith Buschc30341d2013-12-10 13:10:38 -0700195#define CMD_CTX_ABORT (0x31C + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500196
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500197static void special_completion(struct nvme_dev *dev, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400198 struct nvme_completion *cqe)
199{
200 if (ctx == CMD_CTX_CANCELLED)
201 return;
202 if (ctx == CMD_CTX_FLUSH)
203 return;
Keith Buschc30341d2013-12-10 13:10:38 -0700204 if (ctx == CMD_CTX_ABORT) {
205 ++dev->abort_limit;
206 return;
207 }
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400208 if (ctx == CMD_CTX_COMPLETED) {
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500209 dev_warn(&dev->pci_dev->dev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400210 "completed id %d twice on queue %d\n",
211 cqe->command_id, le16_to_cpup(&cqe->sq_id));
212 return;
213 }
214 if (ctx == CMD_CTX_INVALID) {
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500215 dev_warn(&dev->pci_dev->dev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400216 "invalid id %d completed on queue %d\n",
217 cqe->command_id, le16_to_cpup(&cqe->sq_id));
218 return;
219 }
220
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500221 dev_warn(&dev->pci_dev->dev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400222}
223
Keith Busch4d115422013-12-10 13:10:40 -0700224static void async_completion(struct nvme_dev *dev, void *ctx,
225 struct nvme_completion *cqe)
226{
227 struct async_cmd_info *cmdinfo = ctx;
228 cmdinfo->result = le32_to_cpup(&cqe->result);
229 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
230 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
231}
232
Matthew Wilcox184d2942011-05-11 21:36:38 -0400233/*
234 * Called with local interrupts disabled and the q_lock held. May not sleep.
235 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400236static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid,
237 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500238{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400239 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500240 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500241
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400242 if (cmdid >= nvmeq->q_depth) {
243 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500244 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400245 }
Keith Busch859361a2012-08-02 14:05:59 -0600246 if (fn)
247 *fn = info[cmdid].fn;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400248 ctx = info[cmdid].ctx;
249 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500250 info[cmdid].ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500251 clear_bit(cmdid, nvmeq->cmdid_data);
252 wake_up(&nvmeq->sq_full);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400253 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500254}
255
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400256static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
257 nvme_completion_fn *fn)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500258{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400259 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500260 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400261 if (fn)
262 *fn = info[cmdid].fn;
263 ctx = info[cmdid].ctx;
264 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500265 info[cmdid].ctx = CMD_CTX_CANCELLED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400266 return ctx;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500267}
268
Keith Busch5a92e702014-02-21 14:13:44 -0700269static struct nvme_queue *raw_nvmeq(struct nvme_dev *dev, int qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500270{
Keith Busch5a92e702014-02-21 14:13:44 -0700271 return rcu_dereference_raw(dev->queues[qid]);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500272}
273
Keith Busch4f5099a2014-03-03 16:39:13 -0700274static struct nvme_queue *get_nvmeq(struct nvme_dev *dev) __acquires(RCU)
Keith Busch5a92e702014-02-21 14:13:44 -0700275{
Keith Busch42f61422014-03-24 10:46:25 -0600276 unsigned queue_id = get_cpu_var(*dev->io_queue);
Keith Busch5a92e702014-02-21 14:13:44 -0700277 rcu_read_lock();
Keith Busch42f61422014-03-24 10:46:25 -0600278 return rcu_dereference(dev->queues[queue_id]);
Keith Busch5a92e702014-02-21 14:13:44 -0700279}
280
Keith Busch4f5099a2014-03-03 16:39:13 -0700281static void put_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500282{
Keith Busch5a92e702014-02-21 14:13:44 -0700283 rcu_read_unlock();
Keith Busch42f61422014-03-24 10:46:25 -0600284 put_cpu_var(nvmeq->dev->io_queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500285}
286
Keith Busch4f5099a2014-03-03 16:39:13 -0700287static struct nvme_queue *lock_nvmeq(struct nvme_dev *dev, int q_idx)
288 __acquires(RCU)
289{
290 rcu_read_lock();
291 return rcu_dereference(dev->queues[q_idx]);
292}
293
294static void unlock_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
295{
296 rcu_read_unlock();
297}
298
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500299/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400300 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500301 * @nvmeq: The queue to use
302 * @cmd: The command to send
303 *
304 * Safe to use from interrupt context
305 */
306static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
307{
308 unsigned long flags;
309 u16 tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500310 spin_lock_irqsave(&nvmeq->q_lock, flags);
Keith Busch4f5099a2014-03-03 16:39:13 -0700311 if (nvmeq->q_suspended) {
312 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
313 return -EBUSY;
314 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500315 tail = nvmeq->sq_tail;
316 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500317 if (++tail == nvmeq->q_depth)
318 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500319 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500320 nvmeq->sq_tail = tail;
321 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
322
323 return 0;
324}
325
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500326static __le64 **iod_list(struct nvme_iod *iod)
327{
328 return ((void *)iod) + iod->offset;
329}
330
331/*
332 * Will slightly overestimate the number of pages needed. This is OK
333 * as it only leads to a small amount of wasted memory for the lifetime of
334 * the I/O.
335 */
336static int nvme_npages(unsigned size)
337{
338 unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE);
339 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
340}
341
342static struct nvme_iod *
343nvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp)
344{
345 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
346 sizeof(__le64 *) * nvme_npages(nbytes) +
347 sizeof(struct scatterlist) * nseg, gfp);
348
349 if (iod) {
350 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
351 iod->npages = -1;
352 iod->length = nbytes;
Keith Busch2b196032012-11-06 11:59:23 -0700353 iod->nents = 0;
Keith Busch61982212013-05-29 15:59:39 -0600354 iod->start_time = jiffies;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500355 }
356
357 return iod;
358}
359
Vishal Verma5d0f6132013-03-04 18:40:58 -0700360void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500361{
362 const int last_prp = PAGE_SIZE / 8 - 1;
363 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500364 __le64 **list = iod_list(iod);
365 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500366
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500367 if (iod->npages == 0)
368 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
369 for (i = 0; i < iod->npages; i++) {
370 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500371 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500372 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500373 prp_dma = next_prp_dma;
374 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500375 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500376}
377
Keith Busch61982212013-05-29 15:59:39 -0600378static void nvme_start_io_acct(struct bio *bio)
379{
380 struct gendisk *disk = bio->bi_bdev->bd_disk;
381 const int rw = bio_data_dir(bio);
382 int cpu = part_stat_lock();
383 part_round_stats(cpu, &disk->part0);
384 part_stat_inc(cpu, &disk->part0, ios[rw]);
385 part_stat_add(cpu, &disk->part0, sectors[rw], bio_sectors(bio));
386 part_inc_in_flight(&disk->part0, rw);
387 part_stat_unlock();
388}
389
390static void nvme_end_io_acct(struct bio *bio, unsigned long start_time)
391{
392 struct gendisk *disk = bio->bi_bdev->bd_disk;
393 const int rw = bio_data_dir(bio);
394 unsigned long duration = jiffies - start_time;
395 int cpu = part_stat_lock();
396 part_stat_add(cpu, &disk->part0, ticks[rw], duration);
397 part_round_stats(cpu, &disk->part0);
398 part_dec_in_flight(&disk->part0, rw);
399 part_stat_unlock();
400}
401
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500402static void bio_completion(struct nvme_dev *dev, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500403 struct nvme_completion *cqe)
404{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500405 struct nvme_iod *iod = ctx;
406 struct bio *bio = iod->private;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500407 u16 status = le16_to_cpup(&cqe->status) >> 1;
408
Keith Busch9e59d092013-08-08 10:25:38 -0600409 if (iod->nents) {
Keith Busch2b196032012-11-06 11:59:23 -0700410 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500411 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busch9e59d092013-08-08 10:25:38 -0600412 nvme_end_io_acct(bio, iod->start_time);
413 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500414 nvme_free_iod(dev, iod);
Keith Busch427e9702013-04-09 11:59:32 -0600415 if (status)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500416 bio_endio(bio, -EIO);
Keith Busch427e9702013-04-09 11:59:32 -0600417 else
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500418 bio_endio(bio, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500419}
420
Matthew Wilcox184d2942011-05-11 21:36:38 -0400421/* length is in bytes. gfp flags indicates whether we may sleep. */
Vishal Verma5d0f6132013-03-04 18:40:58 -0700422int nvme_setup_prps(struct nvme_dev *dev, struct nvme_common_command *cmd,
423 struct nvme_iod *iod, int total_len, gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500424{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500425 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500426 int length = total_len;
427 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500428 int dma_len = sg_dma_len(sg);
429 u64 dma_addr = sg_dma_address(sg);
430 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500431 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500432 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500433 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500434 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500435
436 cmd->prp1 = cpu_to_le64(dma_addr);
437 length -= (PAGE_SIZE - offset);
438 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500439 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500440
441 dma_len -= (PAGE_SIZE - offset);
442 if (dma_len) {
443 dma_addr += (PAGE_SIZE - offset);
444 } else {
445 sg = sg_next(sg);
446 dma_addr = sg_dma_address(sg);
447 dma_len = sg_dma_len(sg);
448 }
449
450 if (length <= PAGE_SIZE) {
451 cmd->prp2 = cpu_to_le64(dma_addr);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500452 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500453 }
454
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500455 nprps = DIV_ROUND_UP(length, PAGE_SIZE);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500456 if (nprps <= (256 / 8)) {
457 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500458 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500459 } else {
460 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500461 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500462 }
463
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400464 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
465 if (!prp_list) {
466 cmd->prp2 = cpu_to_le64(dma_addr);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500467 iod->npages = -1;
468 return (total_len - length) + PAGE_SIZE;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400469 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500470 list[0] = prp_list;
471 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500472 cmd->prp2 = cpu_to_le64(prp_dma);
473 i = 0;
474 for (;;) {
Matthew Wilcox7523d832011-03-16 16:43:40 -0400475 if (i == PAGE_SIZE / 8) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500476 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400477 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500478 if (!prp_list)
479 return total_len - length;
480 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400481 prp_list[0] = old_prp_list[i - 1];
482 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
483 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500484 }
485 prp_list[i++] = cpu_to_le64(dma_addr);
486 dma_len -= PAGE_SIZE;
487 dma_addr += PAGE_SIZE;
488 length -= PAGE_SIZE;
489 if (length <= 0)
490 break;
491 if (dma_len > 0)
492 continue;
493 BUG_ON(dma_len < 0);
494 sg = sg_next(sg);
495 dma_addr = sg_dma_address(sg);
496 dma_len = sg_dma_len(sg);
497 }
498
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500499 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500500}
501
Keith Busch427e9702013-04-09 11:59:32 -0600502static int nvme_split_and_submit(struct bio *bio, struct nvme_queue *nvmeq,
Kent Overstreet20d01892013-11-23 18:21:01 -0800503 int len)
Keith Busch427e9702013-04-09 11:59:32 -0600504{
Kent Overstreet20d01892013-11-23 18:21:01 -0800505 struct bio *split = bio_split(bio, len >> 9, GFP_ATOMIC, NULL);
506 if (!split)
Keith Busch427e9702013-04-09 11:59:32 -0600507 return -ENOMEM;
508
Kent Overstreet20d01892013-11-23 18:21:01 -0800509 bio_chain(split, bio);
510
Keith Busch427e9702013-04-09 11:59:32 -0600511 if (bio_list_empty(&nvmeq->sq_cong))
512 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
Kent Overstreet20d01892013-11-23 18:21:01 -0800513 bio_list_add(&nvmeq->sq_cong, split);
514 bio_list_add(&nvmeq->sq_cong, bio);
Keith Busch427e9702013-04-09 11:59:32 -0600515
516 return 0;
517}
518
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500519/* NVMe scatterlists require no holes in the virtual address */
520#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
521 (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
522
Keith Busch427e9702013-04-09 11:59:32 -0600523static int nvme_map_bio(struct nvme_queue *nvmeq, struct nvme_iod *iod,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500524 struct bio *bio, enum dma_data_direction dma_dir, int psegs)
525{
Kent Overstreet79886132013-11-23 17:19:00 -0800526 struct bio_vec bvec, bvprv;
527 struct bvec_iter iter;
Matthew Wilcox76830842011-02-10 13:55:39 -0500528 struct scatterlist *sg = NULL;
Kent Overstreet79886132013-11-23 17:19:00 -0800529 int length = 0, nsegs = 0, split_len = bio->bi_iter.bi_size;
530 int first = 1;
Keith Busch159b67d2013-04-09 17:13:20 -0600531
532 if (nvmeq->dev->stripe_size)
533 split_len = nvmeq->dev->stripe_size -
Kent Overstreet4f024f32013-10-11 15:44:27 -0700534 ((bio->bi_iter.bi_sector << 9) &
535 (nvmeq->dev->stripe_size - 1));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500536
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500537 sg_init_table(iod->sg, psegs);
Kent Overstreet79886132013-11-23 17:19:00 -0800538 bio_for_each_segment(bvec, bio, iter) {
539 if (!first && BIOVEC_PHYS_MERGEABLE(&bvprv, &bvec)) {
540 sg->length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500541 } else {
Kent Overstreet79886132013-11-23 17:19:00 -0800542 if (!first && BIOVEC_NOT_VIRT_MERGEABLE(&bvprv, &bvec))
543 return nvme_split_and_submit(bio, nvmeq,
Kent Overstreet20d01892013-11-23 18:21:01 -0800544 length);
Keith Busch427e9702013-04-09 11:59:32 -0600545
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500546 sg = sg ? sg + 1 : iod->sg;
Kent Overstreet79886132013-11-23 17:19:00 -0800547 sg_set_page(sg, bvec.bv_page,
548 bvec.bv_len, bvec.bv_offset);
Matthew Wilcox76830842011-02-10 13:55:39 -0500549 nsegs++;
550 }
Keith Busch159b67d2013-04-09 17:13:20 -0600551
Kent Overstreet79886132013-11-23 17:19:00 -0800552 if (split_len - length < bvec.bv_len)
Kent Overstreet20d01892013-11-23 18:21:01 -0800553 return nvme_split_and_submit(bio, nvmeq, split_len);
Kent Overstreet79886132013-11-23 17:19:00 -0800554 length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500555 bvprv = bvec;
Kent Overstreet79886132013-11-23 17:19:00 -0800556 first = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500557 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500558 iod->nents = nsegs;
Matthew Wilcox76830842011-02-10 13:55:39 -0500559 sg_mark_end(sg);
Keith Busch427e9702013-04-09 11:59:32 -0600560 if (dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir) == 0)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500561 return -ENOMEM;
Keith Busch427e9702013-04-09 11:59:32 -0600562
Kent Overstreet4f024f32013-10-11 15:44:27 -0700563 BUG_ON(length != bio->bi_iter.bi_size);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500564 return length;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500565}
566
Keith Busch0e5e4f02012-11-09 16:33:05 -0700567/*
568 * We reuse the small pool to allocate the 16-byte range here as it is not
569 * worth having a special pool for these or additional cases to handle freeing
570 * the iod.
571 */
572static int nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
573 struct bio *bio, struct nvme_iod *iod, int cmdid)
574{
575 struct nvme_dsm_range *range;
576 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
577
578 range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC,
579 &iod->first_dma);
580 if (!range)
581 return -ENOMEM;
582
583 iod_list(iod)[0] = (__le64 *)range;
584 iod->npages = 0;
585
586 range->cattr = cpu_to_le32(0);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700587 range->nlb = cpu_to_le32(bio->bi_iter.bi_size >> ns->lba_shift);
588 range->slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700589
590 memset(cmnd, 0, sizeof(*cmnd));
591 cmnd->dsm.opcode = nvme_cmd_dsm;
592 cmnd->dsm.command_id = cmdid;
593 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
594 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
595 cmnd->dsm.nr = 0;
596 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
597
598 if (++nvmeq->sq_tail == nvmeq->q_depth)
599 nvmeq->sq_tail = 0;
600 writel(nvmeq->sq_tail, nvmeq->q_db);
601
602 return 0;
603}
604
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500605static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
606 int cmdid)
607{
608 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
609
610 memset(cmnd, 0, sizeof(*cmnd));
611 cmnd->common.opcode = nvme_cmd_flush;
612 cmnd->common.command_id = cmdid;
613 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
614
615 if (++nvmeq->sq_tail == nvmeq->q_depth)
616 nvmeq->sq_tail = 0;
617 writel(nvmeq->sq_tail, nvmeq->q_db);
618
619 return 0;
620}
621
Vishal Verma5d0f6132013-03-04 18:40:58 -0700622int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500623{
624 int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH,
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500625 special_completion, NVME_IO_TIMEOUT);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500626 if (unlikely(cmdid < 0))
627 return cmdid;
628
629 return nvme_submit_flush(nvmeq, ns, cmdid);
630}
631
Matthew Wilcox184d2942011-05-11 21:36:38 -0400632/*
633 * Called with local interrupts disabled and the q_lock held. May not sleep.
634 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500635static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
636 struct bio *bio)
637{
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500638 struct nvme_command *cmnd;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500639 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500640 enum dma_data_direction dma_dir;
Wei Yongjun1287dab2013-05-13 22:29:04 +0800641 int cmdid, length, result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500642 u16 control;
643 u32 dsmgmt;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500644 int psegs = bio_phys_segments(ns->queue, bio);
645
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500646 if ((bio->bi_rw & REQ_FLUSH) && psegs) {
647 result = nvme_submit_flush_data(nvmeq, ns);
648 if (result)
649 return result;
650 }
651
Wei Yongjun1287dab2013-05-13 22:29:04 +0800652 result = -ENOMEM;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700653 iod = nvme_alloc_iod(psegs, bio->bi_iter.bi_size, GFP_ATOMIC);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500654 if (!iod)
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500655 goto nomem;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500656 iod->private = bio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500657
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500658 result = -EBUSY;
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500659 cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500660 if (unlikely(cmdid < 0))
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500661 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500662
Keith Busch0e5e4f02012-11-09 16:33:05 -0700663 if (bio->bi_rw & REQ_DISCARD) {
664 result = nvme_submit_discard(nvmeq, ns, bio, iod, cmdid);
665 if (result)
666 goto free_cmdid;
667 return result;
668 }
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500669 if ((bio->bi_rw & REQ_FLUSH) && !psegs)
670 return nvme_submit_flush(nvmeq, ns, cmdid);
671
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500672 control = 0;
673 if (bio->bi_rw & REQ_FUA)
674 control |= NVME_RW_FUA;
675 if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
676 control |= NVME_RW_LR;
677
678 dsmgmt = 0;
679 if (bio->bi_rw & REQ_RAHEAD)
680 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
681
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500682 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500683
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500684 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500685 if (bio_data_dir(bio)) {
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500686 cmnd->rw.opcode = nvme_cmd_write;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500687 dma_dir = DMA_TO_DEVICE;
688 } else {
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500689 cmnd->rw.opcode = nvme_cmd_read;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500690 dma_dir = DMA_FROM_DEVICE;
691 }
692
Keith Busch427e9702013-04-09 11:59:32 -0600693 result = nvme_map_bio(nvmeq, iod, bio, dma_dir, psegs);
694 if (result <= 0)
Keith Busch859361a2012-08-02 14:05:59 -0600695 goto free_cmdid;
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500696 length = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500697
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500698 cmnd->rw.command_id = cmdid;
699 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500700 length = nvme_setup_prps(nvmeq->dev, &cmnd->common, iod, length,
701 GFP_ATOMIC);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700702 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500703 cmnd->rw.length = cpu_to_le16((length >> ns->lba_shift) - 1);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500704 cmnd->rw.control = cpu_to_le16(control);
705 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500706
Keith Busch61982212013-05-29 15:59:39 -0600707 nvme_start_io_acct(bio);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500708 if (++nvmeq->sq_tail == nvmeq->q_depth)
709 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500710 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500711
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500712 return 0;
713
Keith Busch859361a2012-08-02 14:05:59 -0600714 free_cmdid:
715 free_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500716 free_iod:
717 nvme_free_iod(nvmeq->dev, iod);
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500718 nomem:
719 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500720}
721
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400722static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500723{
Matthew Wilcox82123462011-01-20 13:24:06 -0500724 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500725
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500726 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500727 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500728
729 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400730 void *ctx;
731 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500732 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500733 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500734 break;
735 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
736 if (++head == nvmeq->q_depth) {
737 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500738 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500739 }
740
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400741 ctx = free_cmdid(nvmeq, cqe.command_id, &fn);
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500742 fn(nvmeq->dev, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500743 }
744
745 /* If the controller ignores the cq head doorbell and continuously
746 * writes to the queue, it is theoretically possible to wrap around
747 * the queue twice and mistakenly return IRQ_NONE. Linux only
748 * requires that 0.1% of your interrupts are handled, so this isn't
749 * a big problem.
750 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500751 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400752 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500753
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800754 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500755 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500756 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500757
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400758 nvmeq->cqe_seen = 1;
759 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500760}
761
Matthew Wilcox7d822452013-06-24 12:03:57 -0400762static void nvme_make_request(struct request_queue *q, struct bio *bio)
763{
764 struct nvme_ns *ns = q->queuedata;
765 struct nvme_queue *nvmeq = get_nvmeq(ns->dev);
766 int result = -EBUSY;
767
Keith Buschcd638942013-07-15 15:02:23 -0600768 if (!nvmeq) {
769 put_nvmeq(NULL);
770 bio_endio(bio, -EIO);
771 return;
772 }
773
Matthew Wilcox7d822452013-06-24 12:03:57 -0400774 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -0600775 if (!nvmeq->q_suspended && bio_list_empty(&nvmeq->sq_cong))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400776 result = nvme_submit_bio_queue(nvmeq, ns, bio);
777 if (unlikely(result)) {
778 if (bio_list_empty(&nvmeq->sq_cong))
779 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
780 bio_list_add(&nvmeq->sq_cong, bio);
781 }
782
783 nvme_process_cq(nvmeq);
784 spin_unlock_irq(&nvmeq->q_lock);
785 put_nvmeq(nvmeq);
786}
787
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500788static irqreturn_t nvme_irq(int irq, void *data)
789{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500790 irqreturn_t result;
791 struct nvme_queue *nvmeq = data;
792 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400793 nvme_process_cq(nvmeq);
794 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
795 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500796 spin_unlock(&nvmeq->q_lock);
797 return result;
798}
799
800static irqreturn_t nvme_irq_check(int irq, void *data)
801{
802 struct nvme_queue *nvmeq = data;
803 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
804 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
805 return IRQ_NONE;
806 return IRQ_WAKE_THREAD;
807}
808
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500809static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
810{
811 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400812 cancel_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500813 spin_unlock_irq(&nvmeq->q_lock);
814}
815
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400816struct sync_cmd_info {
817 struct task_struct *task;
818 u32 result;
819 int status;
820};
821
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500822static void sync_completion(struct nvme_dev *dev, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400823 struct nvme_completion *cqe)
824{
825 struct sync_cmd_info *cmdinfo = ctx;
826 cmdinfo->result = le32_to_cpup(&cqe->result);
827 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
828 wake_up_process(cmdinfo->task);
829}
830
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500831/*
832 * Returns 0 on success. If the result is negative, it's a Linux error code;
833 * if the result is positive, it's an NVM Express status code
834 */
Keith Busch4f5099a2014-03-03 16:39:13 -0700835static int nvme_submit_sync_cmd(struct nvme_dev *dev, int q_idx,
836 struct nvme_command *cmd,
Vishal Verma5d0f6132013-03-04 18:40:58 -0700837 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500838{
Keith Busch4f5099a2014-03-03 16:39:13 -0700839 int cmdid, ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500840 struct sync_cmd_info cmdinfo;
Keith Busch4f5099a2014-03-03 16:39:13 -0700841 struct nvme_queue *nvmeq;
842
843 nvmeq = lock_nvmeq(dev, q_idx);
844 if (!nvmeq) {
845 unlock_nvmeq(nvmeq);
846 return -ENODEV;
847 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500848
849 cmdinfo.task = current;
850 cmdinfo.status = -EINTR;
851
Keith Busch4f5099a2014-03-03 16:39:13 -0700852 cmdid = alloc_cmdid(nvmeq, &cmdinfo, sync_completion, timeout);
853 if (cmdid < 0) {
854 unlock_nvmeq(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500855 return cmdid;
Keith Busch4f5099a2014-03-03 16:39:13 -0700856 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500857 cmd->common.command_id = cmdid;
858
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500859 set_current_state(TASK_KILLABLE);
Keith Busch4f5099a2014-03-03 16:39:13 -0700860 ret = nvme_submit_cmd(nvmeq, cmd);
861 if (ret) {
862 free_cmdid(nvmeq, cmdid, NULL);
863 unlock_nvmeq(nvmeq);
864 set_current_state(TASK_RUNNING);
865 return ret;
866 }
867 unlock_nvmeq(nvmeq);
Keith Busch78f8d252013-04-19 14:11:06 -0600868 schedule_timeout(timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500869
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500870 if (cmdinfo.status == -EINTR) {
Keith Busch4f5099a2014-03-03 16:39:13 -0700871 nvmeq = lock_nvmeq(dev, q_idx);
872 if (nvmeq)
873 nvme_abort_command(nvmeq, cmdid);
874 unlock_nvmeq(nvmeq);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500875 return -EINTR;
876 }
877
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500878 if (result)
879 *result = cmdinfo.result;
880
881 return cmdinfo.status;
882}
883
Keith Busch4d115422013-12-10 13:10:40 -0700884static int nvme_submit_async_cmd(struct nvme_queue *nvmeq,
885 struct nvme_command *cmd,
886 struct async_cmd_info *cmdinfo, unsigned timeout)
887{
888 int cmdid;
889
890 cmdid = alloc_cmdid_killable(nvmeq, cmdinfo, async_completion, timeout);
891 if (cmdid < 0)
892 return cmdid;
893 cmdinfo->status = -EINTR;
894 cmd->common.command_id = cmdid;
Keith Busch4f5099a2014-03-03 16:39:13 -0700895 return nvme_submit_cmd(nvmeq, cmd);
Keith Busch4d115422013-12-10 13:10:40 -0700896}
897
Vishal Verma5d0f6132013-03-04 18:40:58 -0700898int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500899 u32 *result)
900{
Keith Busch4f5099a2014-03-03 16:39:13 -0700901 return nvme_submit_sync_cmd(dev, 0, cmd, result, ADMIN_TIMEOUT);
902}
903
904int nvme_submit_io_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
905 u32 *result)
906{
907 return nvme_submit_sync_cmd(dev, smp_processor_id() + 1, cmd, result,
908 NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500909}
910
Keith Busch4d115422013-12-10 13:10:40 -0700911static int nvme_submit_admin_cmd_async(struct nvme_dev *dev,
912 struct nvme_command *cmd, struct async_cmd_info *cmdinfo)
913{
Keith Busch5a92e702014-02-21 14:13:44 -0700914 return nvme_submit_async_cmd(raw_nvmeq(dev, 0), cmd, cmdinfo,
Keith Busch4d115422013-12-10 13:10:40 -0700915 ADMIN_TIMEOUT);
916}
917
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500918static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
919{
920 int status;
921 struct nvme_command c;
922
923 memset(&c, 0, sizeof(c));
924 c.delete_queue.opcode = opcode;
925 c.delete_queue.qid = cpu_to_le16(id);
926
927 status = nvme_submit_admin_cmd(dev, &c, NULL);
928 if (status)
929 return -EIO;
930 return 0;
931}
932
933static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
934 struct nvme_queue *nvmeq)
935{
936 int status;
937 struct nvme_command c;
938 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
939
940 memset(&c, 0, sizeof(c));
941 c.create_cq.opcode = nvme_admin_create_cq;
942 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
943 c.create_cq.cqid = cpu_to_le16(qid);
944 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
945 c.create_cq.cq_flags = cpu_to_le16(flags);
946 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
947
948 status = nvme_submit_admin_cmd(dev, &c, NULL);
949 if (status)
950 return -EIO;
951 return 0;
952}
953
954static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
955 struct nvme_queue *nvmeq)
956{
957 int status;
958 struct nvme_command c;
959 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
960
961 memset(&c, 0, sizeof(c));
962 c.create_sq.opcode = nvme_admin_create_sq;
963 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
964 c.create_sq.sqid = cpu_to_le16(qid);
965 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
966 c.create_sq.sq_flags = cpu_to_le16(flags);
967 c.create_sq.cqid = cpu_to_le16(qid);
968
969 status = nvme_submit_admin_cmd(dev, &c, NULL);
970 if (status)
971 return -EIO;
972 return 0;
973}
974
975static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
976{
977 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
978}
979
980static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
981{
982 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
983}
984
Vishal Verma5d0f6132013-03-04 18:40:58 -0700985int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400986 dma_addr_t dma_addr)
987{
988 struct nvme_command c;
989
990 memset(&c, 0, sizeof(c));
991 c.identify.opcode = nvme_admin_identify;
992 c.identify.nsid = cpu_to_le32(nsid);
993 c.identify.prp1 = cpu_to_le64(dma_addr);
994 c.identify.cns = cpu_to_le32(cns);
995
996 return nvme_submit_admin_cmd(dev, &c, NULL);
997}
998
Vishal Verma5d0f6132013-03-04 18:40:58 -0700999int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -06001000 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001001{
1002 struct nvme_command c;
1003
1004 memset(&c, 0, sizeof(c));
1005 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -06001006 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001007 c.features.prp1 = cpu_to_le64(dma_addr);
1008 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001009
Keith Busch08df1e02012-09-21 10:52:13 -06001010 return nvme_submit_admin_cmd(dev, &c, result);
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001011}
1012
Vishal Verma5d0f6132013-03-04 18:40:58 -07001013int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
1014 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001015{
1016 struct nvme_command c;
1017
1018 memset(&c, 0, sizeof(c));
1019 c.features.opcode = nvme_admin_set_features;
1020 c.features.prp1 = cpu_to_le64(dma_addr);
1021 c.features.fid = cpu_to_le32(fid);
1022 c.features.dword11 = cpu_to_le32(dword11);
1023
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001024 return nvme_submit_admin_cmd(dev, &c, result);
1025}
1026
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001027/**
Keith Buschc30341d2013-12-10 13:10:38 -07001028 * nvme_abort_cmd - Attempt aborting a command
1029 * @cmdid: Command id of a timed out IO
1030 * @queue: The queue with timed out IO
1031 *
1032 * Schedule controller reset if the command was already aborted once before and
1033 * still hasn't been returned to the driver, or if this is the admin queue.
1034 */
1035static void nvme_abort_cmd(int cmdid, struct nvme_queue *nvmeq)
1036{
1037 int a_cmdid;
1038 struct nvme_command cmd;
1039 struct nvme_dev *dev = nvmeq->dev;
1040 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Keith Busch5a92e702014-02-21 14:13:44 -07001041 struct nvme_queue *adminq;
Keith Buschc30341d2013-12-10 13:10:38 -07001042
1043 if (!nvmeq->qid || info[cmdid].aborted) {
1044 if (work_busy(&dev->reset_work))
1045 return;
1046 list_del_init(&dev->node);
1047 dev_warn(&dev->pci_dev->dev,
1048 "I/O %d QID %d timeout, reset controller\n", cmdid,
1049 nvmeq->qid);
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05001050 PREPARE_WORK(&dev->reset_work, nvme_reset_failed_dev);
Keith Buschc30341d2013-12-10 13:10:38 -07001051 queue_work(nvme_workq, &dev->reset_work);
1052 return;
1053 }
1054
1055 if (!dev->abort_limit)
1056 return;
1057
Keith Busch5a92e702014-02-21 14:13:44 -07001058 adminq = rcu_dereference(dev->queues[0]);
1059 a_cmdid = alloc_cmdid(adminq, CMD_CTX_ABORT, special_completion,
Keith Buschc30341d2013-12-10 13:10:38 -07001060 ADMIN_TIMEOUT);
1061 if (a_cmdid < 0)
1062 return;
1063
1064 memset(&cmd, 0, sizeof(cmd));
1065 cmd.abort.opcode = nvme_admin_abort_cmd;
1066 cmd.abort.cid = cmdid;
1067 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
1068 cmd.abort.command_id = a_cmdid;
1069
1070 --dev->abort_limit;
1071 info[cmdid].aborted = 1;
1072 info[cmdid].timeout = jiffies + ADMIN_TIMEOUT;
1073
1074 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", cmdid,
1075 nvmeq->qid);
Keith Busch5a92e702014-02-21 14:13:44 -07001076 nvme_submit_cmd(adminq, &cmd);
Keith Buschc30341d2013-12-10 13:10:38 -07001077}
1078
1079/**
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001080 * nvme_cancel_ios - Cancel outstanding I/Os
1081 * @queue: The queue to cancel I/Os on
1082 * @timeout: True to only cancel I/Os which have timed out
1083 */
1084static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout)
1085{
1086 int depth = nvmeq->q_depth - 1;
1087 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
1088 unsigned long now = jiffies;
1089 int cmdid;
1090
1091 for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
1092 void *ctx;
1093 nvme_completion_fn fn;
1094 static struct nvme_completion cqe = {
Matthew Wilcoxaf2d9ca2013-04-16 15:18:30 -04001095 .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1),
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001096 };
1097
1098 if (timeout && !time_after(now, info[cmdid].timeout))
1099 continue;
Keith Busch053ab7022013-04-30 11:19:38 -06001100 if (info[cmdid].ctx == CMD_CTX_CANCELLED)
1101 continue;
Keith Buschc30341d2013-12-10 13:10:38 -07001102 if (timeout && nvmeq->dev->initialized) {
1103 nvme_abort_cmd(cmdid, nvmeq);
1104 continue;
1105 }
1106 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n", cmdid,
1107 nvmeq->qid);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001108 ctx = cancel_cmdid(nvmeq, cmdid, &fn);
1109 fn(nvmeq->dev, ctx, &cqe);
1110 }
1111}
1112
Keith Busch5a92e702014-02-21 14:13:44 -07001113static void nvme_free_queue(struct rcu_head *r)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001114{
Keith Busch5a92e702014-02-21 14:13:44 -07001115 struct nvme_queue *nvmeq = container_of(r, struct nvme_queue, r_head);
1116
Keith Busch22404272013-07-15 15:02:20 -06001117 spin_lock_irq(&nvmeq->q_lock);
1118 while (bio_list_peek(&nvmeq->sq_cong)) {
1119 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1120 bio_endio(bio, -EIO);
1121 }
1122 spin_unlock_irq(&nvmeq->q_lock);
1123
Matthew Wilcox9e866772012-08-03 13:55:56 -04001124 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1125 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1126 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1127 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
Keith Busch42f61422014-03-24 10:46:25 -06001128 if (nvmeq->qid)
1129 free_cpumask_var(nvmeq->cpu_mask);
Matthew Wilcox9e866772012-08-03 13:55:56 -04001130 kfree(nvmeq);
1131}
1132
Keith Buscha1a5ef92013-12-16 13:50:00 -05001133static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001134{
1135 int i;
1136
Keith Buscha1a5ef92013-12-16 13:50:00 -05001137 for (i = dev->queue_count - 1; i >= lowest; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07001138 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
1139 rcu_assign_pointer(dev->queues[i], NULL);
1140 call_rcu(&nvmeq->r_head, nvme_free_queue);
Keith Busch22404272013-07-15 15:02:20 -06001141 dev->queue_count--;
Keith Busch22404272013-07-15 15:02:20 -06001142 }
1143}
1144
Keith Busch4d115422013-12-10 13:10:40 -07001145/**
1146 * nvme_suspend_queue - put queue into suspended state
1147 * @nvmeq - queue to suspend
1148 *
1149 * Returns 1 if already suspended, 0 otherwise.
1150 */
1151static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001152{
Keith Busch4d115422013-12-10 13:10:40 -07001153 int vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001154
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001155 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001156 if (nvmeq->q_suspended) {
1157 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07001158 return 1;
Keith Busch32958742012-08-20 14:57:49 -06001159 }
Keith Busch22404272013-07-15 15:02:20 -06001160 nvmeq->q_suspended = 1;
Keith Busch42f61422014-03-24 10:46:25 -06001161 nvmeq->dev->online_queues--;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001162 spin_unlock_irq(&nvmeq->q_lock);
1163
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001164 irq_set_affinity_hint(vector, NULL);
1165 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001166
Keith Busch4d115422013-12-10 13:10:40 -07001167 return 0;
1168}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001169
Keith Busch4d115422013-12-10 13:10:40 -07001170static void nvme_clear_queue(struct nvme_queue *nvmeq)
1171{
Keith Busch22404272013-07-15 15:02:20 -06001172 spin_lock_irq(&nvmeq->q_lock);
1173 nvme_process_cq(nvmeq);
1174 nvme_cancel_ios(nvmeq, false);
1175 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001176}
1177
Keith Busch4d115422013-12-10 13:10:40 -07001178static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1179{
Keith Busch5a92e702014-02-21 14:13:44 -07001180 struct nvme_queue *nvmeq = raw_nvmeq(dev, qid);
Keith Busch4d115422013-12-10 13:10:40 -07001181
1182 if (!nvmeq)
1183 return;
1184 if (nvme_suspend_queue(nvmeq))
1185 return;
1186
Keith Busch0e53d182013-12-10 13:10:39 -07001187 /* Don't tell the adapter to delete the admin queue.
1188 * Don't tell a removed adapter to delete IO queues. */
1189 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001190 adapter_delete_sq(dev, qid);
1191 adapter_delete_cq(dev, qid);
1192 }
Keith Busch4d115422013-12-10 13:10:40 -07001193 nvme_clear_queue(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001194}
1195
1196static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
1197 int depth, int vector)
1198{
1199 struct device *dmadev = &dev->pci_dev->dev;
Keith Busch22404272013-07-15 15:02:20 -06001200 unsigned extra = nvme_queue_extra(depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001201 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
1202 if (!nvmeq)
1203 return NULL;
1204
1205 nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
1206 &nvmeq->cq_dma_addr, GFP_KERNEL);
1207 if (!nvmeq->cqes)
1208 goto free_nvmeq;
1209 memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
1210
1211 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
1212 &nvmeq->sq_dma_addr, GFP_KERNEL);
1213 if (!nvmeq->sq_cmds)
1214 goto free_cqdma;
1215
Keith Busch42f61422014-03-24 10:46:25 -06001216 if (qid && !zalloc_cpumask_var(&nvmeq->cpu_mask, GFP_KERNEL))
1217 goto free_sqdma;
1218
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001219 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001220 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001221 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1222 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001223 spin_lock_init(&nvmeq->q_lock);
1224 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001225 nvmeq->cq_phase = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001226 init_waitqueue_head(&nvmeq->sq_full);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001227 init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001228 bio_list_init(&nvmeq->sq_cong);
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001229 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001230 nvmeq->q_depth = depth;
1231 nvmeq->cq_vector = vector;
Keith Buschc30341d2013-12-10 13:10:38 -07001232 nvmeq->qid = qid;
Keith Busch22404272013-07-15 15:02:20 -06001233 nvmeq->q_suspended = 1;
1234 dev->queue_count++;
Keith Busch5a92e702014-02-21 14:13:44 -07001235 rcu_assign_pointer(dev->queues[qid], nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001236
1237 return nvmeq;
1238
Keith Busch42f61422014-03-24 10:46:25 -06001239 free_sqdma:
1240 dma_free_coherent(dmadev, SQ_SIZE(depth), (void *)nvmeq->sq_cmds,
1241 nvmeq->sq_dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001242 free_cqdma:
Keith Busch68b8eca2013-05-01 13:07:47 -06001243 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001244 nvmeq->cq_dma_addr);
1245 free_nvmeq:
1246 kfree(nvmeq);
1247 return NULL;
1248}
1249
Matthew Wilcox30010822011-01-20 09:10:15 -05001250static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1251 const char *name)
1252{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001253 if (use_threaded_interrupts)
1254 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001255 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001256 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001257 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001258 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001259}
1260
Keith Busch22404272013-07-15 15:02:20 -06001261static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001262{
Keith Busch22404272013-07-15 15:02:20 -06001263 struct nvme_dev *dev = nvmeq->dev;
1264 unsigned extra = nvme_queue_extra(nvmeq->q_depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001265
Keith Busch22404272013-07-15 15:02:20 -06001266 nvmeq->sq_tail = 0;
1267 nvmeq->cq_head = 0;
1268 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001269 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001270 memset(nvmeq->cmdid_data, 0, extra);
1271 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
1272 nvme_cancel_ios(nvmeq, false);
1273 nvmeq->q_suspended = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001274 dev->online_queues++;
Keith Busch22404272013-07-15 15:02:20 -06001275}
1276
1277static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1278{
1279 struct nvme_dev *dev = nvmeq->dev;
1280 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001281
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001282 result = adapter_alloc_cq(dev, qid, nvmeq);
1283 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001284 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001285
1286 result = adapter_alloc_sq(dev, qid, nvmeq);
1287 if (result < 0)
1288 goto release_cq;
1289
Matthew Wilcox3193f072014-01-27 15:57:22 -05001290 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001291 if (result < 0)
1292 goto release_sq;
1293
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001294 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001295 nvme_init_queue(nvmeq, qid);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001296 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001297
1298 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001299
1300 release_sq:
1301 adapter_delete_sq(dev, qid);
1302 release_cq:
1303 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001304 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001305}
1306
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001307static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1308{
1309 unsigned long timeout;
1310 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1311
1312 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1313
1314 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1315 msleep(100);
1316 if (fatal_signal_pending(current))
1317 return -EINTR;
1318 if (time_after(jiffies, timeout)) {
1319 dev_err(&dev->pci_dev->dev,
1320 "Device not ready; aborting initialisation\n");
1321 return -ENODEV;
1322 }
1323 }
1324
1325 return 0;
1326}
1327
1328/*
1329 * If the device has been passed off to us in an enabled state, just clear
1330 * the enabled bit. The spec says we should set the 'shutdown notification
1331 * bits', but doing so may cause the device to complete commands to the
1332 * admin queue ... and we don't know what memory that might be pointing at!
1333 */
1334static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1335{
Matthew Wilcox44af1462013-05-04 06:43:17 -04001336 u32 cc = readl(&dev->bar->cc);
1337
1338 if (cc & NVME_CC_ENABLE)
1339 writel(cc & ~NVME_CC_ENABLE, &dev->bar->cc);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001340 return nvme_wait_ready(dev, cap, false);
1341}
1342
1343static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1344{
1345 return nvme_wait_ready(dev, cap, true);
1346}
1347
Keith Busch1894d8f2013-07-15 15:02:22 -06001348static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1349{
1350 unsigned long timeout;
1351 u32 cc;
1352
1353 cc = (readl(&dev->bar->cc) & ~NVME_CC_SHN_MASK) | NVME_CC_SHN_NORMAL;
1354 writel(cc, &dev->bar->cc);
1355
1356 timeout = 2 * HZ + jiffies;
1357 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1358 NVME_CSTS_SHST_CMPLT) {
1359 msleep(100);
1360 if (fatal_signal_pending(current))
1361 return -EINTR;
1362 if (time_after(jiffies, timeout)) {
1363 dev_err(&dev->pci_dev->dev,
1364 "Device shutdown incomplete; abort shutdown\n");
1365 return -ENODEV;
1366 }
1367 }
1368
1369 return 0;
1370}
1371
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001372static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001373{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001374 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001375 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001376 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001377 struct nvme_queue *nvmeq;
1378
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001379 result = nvme_disable_ctrl(dev, cap);
1380 if (result < 0)
1381 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001382
Keith Busch5a92e702014-02-21 14:13:44 -07001383 nvmeq = raw_nvmeq(dev, 0);
Keith Buschcd638942013-07-15 15:02:23 -06001384 if (!nvmeq) {
1385 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
1386 if (!nvmeq)
1387 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001388 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001389
1390 aqa = nvmeq->q_depth - 1;
1391 aqa |= aqa << 16;
1392
1393 dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
1394 dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
1395 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001396 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001397
1398 writel(aqa, &dev->bar->aqa);
1399 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1400 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
1401 writel(dev->ctrl_config, &dev->bar->cc);
1402
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001403 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001404 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001405 return result;
Matthew Wilcox9e866772012-08-03 13:55:56 -04001406
Matthew Wilcox3193f072014-01-27 15:57:22 -05001407 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Keith Busch025c5572013-05-01 13:07:51 -06001408 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001409 return result;
Keith Busch025c5572013-05-01 13:07:51 -06001410
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001411 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001412 nvme_init_queue(nvmeq, 0);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001413 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001414 return result;
1415}
1416
Vishal Verma5d0f6132013-03-04 18:40:58 -07001417struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001418 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001419{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001420 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001421 struct scatterlist *sg;
1422 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001423 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001424
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001425 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001426 return ERR_PTR(-EINVAL);
Dan Carpenter5460fc02013-05-13 17:59:50 +03001427 if (!length || length > INT_MAX - PAGE_SIZE)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001428 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001429
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001430 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001431 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1432 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001433 if (!pages)
1434 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001435
1436 err = get_user_pages_fast(addr, count, 1, pages);
1437 if (err < count) {
1438 count = err;
1439 err = -EFAULT;
1440 goto put_pages;
1441 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001442
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001443 iod = nvme_alloc_iod(count, length, GFP_KERNEL);
1444 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001445 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001446 for (i = 0; i < count; i++) {
1447 sg_set_page(&sg[i], pages[i],
Dan Carpenter5460fc02013-05-13 17:59:50 +03001448 min_t(unsigned, length, PAGE_SIZE - offset),
1449 offset);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001450 length -= (PAGE_SIZE - offset);
1451 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001452 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001453 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001454 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001455
1456 err = -ENOMEM;
1457 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1458 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001459 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001460 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001461
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001462 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001463 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001464
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001465 free_iod:
1466 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001467 put_pages:
1468 for (i = 0; i < count; i++)
1469 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001470 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001471 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001472}
1473
Vishal Verma5d0f6132013-03-04 18:40:58 -07001474void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001475 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001476{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001477 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001478
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001479 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1480 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001481
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001482 for (i = 0; i < iod->nents; i++)
1483 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001484}
1485
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001486static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1487{
1488 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001489 struct nvme_user_io io;
1490 struct nvme_command c;
Keith Buschf410c682013-04-23 17:23:59 -06001491 unsigned length, meta_len;
1492 int status, i;
1493 struct nvme_iod *iod, *meta_iod = NULL;
1494 dma_addr_t meta_dma_addr;
1495 void *meta, *uninitialized_var(meta_mem);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001496
1497 if (copy_from_user(&io, uio, sizeof(io)))
1498 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001499 length = (io.nblocks + 1) << ns->lba_shift;
Keith Buschf410c682013-04-23 17:23:59 -06001500 meta_len = (io.nblocks + 1) * ns->ms;
1501
1502 if (meta_len && ((io.metadata & 3) || !io.metadata))
1503 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001504
1505 switch (io.opcode) {
1506 case nvme_cmd_write:
1507 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001508 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001509 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001510 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001511 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001512 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001513 }
1514
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001515 if (IS_ERR(iod))
1516 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001517
1518 memset(&c, 0, sizeof(c));
1519 c.rw.opcode = io.opcode;
1520 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001521 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001522 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001523 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001524 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001525 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1526 c.rw.reftag = cpu_to_le32(io.reftag);
1527 c.rw.apptag = cpu_to_le16(io.apptag);
1528 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buschf410c682013-04-23 17:23:59 -06001529
1530 if (meta_len) {
Keith Busch1b567492013-07-18 12:13:51 -06001531 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
1532 meta_len);
Keith Buschf410c682013-04-23 17:23:59 -06001533 if (IS_ERR(meta_iod)) {
1534 status = PTR_ERR(meta_iod);
1535 meta_iod = NULL;
1536 goto unmap;
1537 }
1538
1539 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1540 &meta_dma_addr, GFP_KERNEL);
1541 if (!meta_mem) {
1542 status = -ENOMEM;
1543 goto unmap;
1544 }
1545
1546 if (io.opcode & 1) {
1547 int meta_offset = 0;
1548
1549 for (i = 0; i < meta_iod->nents; i++) {
1550 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1551 meta_iod->sg[i].offset;
1552 memcpy(meta_mem + meta_offset, meta,
1553 meta_iod->sg[i].length);
1554 kunmap_atomic(meta);
1555 meta_offset += meta_iod->sg[i].length;
1556 }
1557 }
1558
1559 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1560 }
1561
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001562 length = nvme_setup_prps(dev, &c.common, iod, length, GFP_KERNEL);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001563
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001564 if (length != (io.nblocks + 1) << ns->lba_shift)
1565 status = -ENOMEM;
1566 else
Keith Busch4f5099a2014-03-03 16:39:13 -07001567 status = nvme_submit_io_cmd(dev, &c, NULL);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001568
Keith Buschf410c682013-04-23 17:23:59 -06001569 if (meta_len) {
1570 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1571 int meta_offset = 0;
1572
1573 for (i = 0; i < meta_iod->nents; i++) {
1574 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1575 meta_iod->sg[i].offset;
1576 memcpy(meta, meta_mem + meta_offset,
1577 meta_iod->sg[i].length);
1578 kunmap_atomic(meta);
1579 meta_offset += meta_iod->sg[i].length;
1580 }
1581 }
1582
1583 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1584 meta_dma_addr);
1585 }
1586
1587 unmap:
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001588 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001589 nvme_free_iod(dev, iod);
Keith Buschf410c682013-04-23 17:23:59 -06001590
1591 if (meta_iod) {
1592 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1593 nvme_free_iod(dev, meta_iod);
1594 }
1595
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001596 return status;
1597}
1598
Keith Busch50af8ba2012-07-25 16:07:55 -06001599static int nvme_user_admin_cmd(struct nvme_dev *dev,
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001600 struct nvme_admin_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001601{
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001602 struct nvme_admin_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001603 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001604 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001605 struct nvme_iod *uninitialized_var(iod);
Keith Busch94f370c2013-05-09 14:01:38 -06001606 unsigned timeout;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001607
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001608 if (!capable(CAP_SYS_ADMIN))
1609 return -EACCES;
1610 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001611 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001612
1613 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001614 c.common.opcode = cmd.opcode;
1615 c.common.flags = cmd.flags;
1616 c.common.nsid = cpu_to_le32(cmd.nsid);
1617 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1618 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1619 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1620 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1621 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1622 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1623 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1624 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1625
1626 length = cmd.data_len;
1627 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001628 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1629 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001630 if (IS_ERR(iod))
1631 return PTR_ERR(iod);
1632 length = nvme_setup_prps(dev, &c.common, iod, length,
1633 GFP_KERNEL);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001634 }
1635
Keith Busch94f370c2013-05-09 14:01:38 -06001636 timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
1637 ADMIN_TIMEOUT;
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001638 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001639 status = -ENOMEM;
1640 else
Keith Busch4f5099a2014-03-03 16:39:13 -07001641 status = nvme_submit_sync_cmd(dev, 0, &c, &cmd.result, timeout);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001642
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001643 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001644 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001645 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001646 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001647
Chayan Biswascf90bc42013-05-22 22:34:49 +00001648 if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
Keith Buschf4f117f2012-09-21 10:49:05 -06001649 sizeof(cmd.result)))
1650 status = -EFAULT;
1651
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001652 return status;
1653}
1654
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001655static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1656 unsigned long arg)
1657{
1658 struct nvme_ns *ns = bdev->bd_disk->private_data;
1659
1660 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001661 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001662 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001663 return ns->ns_id;
1664 case NVME_IOCTL_ADMIN_CMD:
Keith Busch50af8ba2012-07-25 16:07:55 -06001665 return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001666 case NVME_IOCTL_SUBMIT_IO:
1667 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001668 case SG_GET_VERSION_NUM:
1669 return nvme_sg_get_version_num((void __user *)arg);
1670 case SG_IO:
1671 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001672 default:
1673 return -ENOTTY;
1674 }
1675}
1676
Keith Busch320a3822013-10-23 13:07:34 -06001677#ifdef CONFIG_COMPAT
1678static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1679 unsigned int cmd, unsigned long arg)
1680{
1681 struct nvme_ns *ns = bdev->bd_disk->private_data;
1682
1683 switch (cmd) {
1684 case SG_IO:
1685 return nvme_sg_io32(ns, arg);
1686 }
1687 return nvme_ioctl(bdev, mode, cmd, arg);
1688}
1689#else
1690#define nvme_compat_ioctl NULL
1691#endif
1692
Keith Busch9ac27092014-01-31 16:53:39 -07001693static int nvme_open(struct block_device *bdev, fmode_t mode)
1694{
1695 struct nvme_ns *ns = bdev->bd_disk->private_data;
1696 struct nvme_dev *dev = ns->dev;
1697
1698 kref_get(&dev->kref);
1699 return 0;
1700}
1701
1702static void nvme_free_dev(struct kref *kref);
1703
1704static void nvme_release(struct gendisk *disk, fmode_t mode)
1705{
1706 struct nvme_ns *ns = disk->private_data;
1707 struct nvme_dev *dev = ns->dev;
1708
1709 kref_put(&dev->kref, nvme_free_dev);
1710}
1711
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001712static const struct block_device_operations nvme_fops = {
1713 .owner = THIS_MODULE,
1714 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06001715 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07001716 .open = nvme_open,
1717 .release = nvme_release,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001718};
1719
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001720static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1721{
1722 while (bio_list_peek(&nvmeq->sq_cong)) {
1723 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1724 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Keith Busch427e9702013-04-09 11:59:32 -06001725
Matthew Wilcox3cb967c2011-03-16 16:45:49 -04001726 if (bio_list_empty(&nvmeq->sq_cong))
1727 remove_wait_queue(&nvmeq->sq_full,
1728 &nvmeq->sq_cong_wait);
Keith Busch427e9702013-04-09 11:59:32 -06001729 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
1730 if (bio_list_empty(&nvmeq->sq_cong))
1731 add_wait_queue(&nvmeq->sq_full,
1732 &nvmeq->sq_cong_wait);
1733 bio_list_add_head(&nvmeq->sq_cong, bio);
1734 break;
1735 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001736 }
1737}
1738
1739static int nvme_kthread(void *data)
1740{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001741 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001742
1743 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001744 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001745 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001746 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001747 int i;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001748 if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
1749 dev->initialized) {
1750 if (work_busy(&dev->reset_work))
1751 continue;
1752 list_del_init(&dev->node);
1753 dev_warn(&dev->pci_dev->dev,
1754 "Failed status, reset controller\n");
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05001755 PREPARE_WORK(&dev->reset_work,
Keith Buschd4b4ff82013-12-10 13:10:37 -07001756 nvme_reset_failed_dev);
1757 queue_work(nvme_workq, &dev->reset_work);
1758 continue;
1759 }
Keith Busch5a92e702014-02-21 14:13:44 -07001760 rcu_read_lock();
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001761 for (i = 0; i < dev->queue_count; i++) {
Keith Busch5a92e702014-02-21 14:13:44 -07001762 struct nvme_queue *nvmeq =
1763 rcu_dereference(dev->queues[i]);
Matthew Wilcox740216f2011-02-15 16:28:20 -05001764 if (!nvmeq)
1765 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001766 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001767 if (nvmeq->q_suspended)
1768 goto unlock;
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001769 nvme_process_cq(nvmeq);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001770 nvme_cancel_ios(nvmeq, true);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001771 nvme_resubmit_bios(nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001772 unlock:
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001773 spin_unlock_irq(&nvmeq->q_lock);
1774 }
Keith Busch5a92e702014-02-21 14:13:44 -07001775 rcu_read_unlock();
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001776 }
1777 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001778 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001779 }
1780 return 0;
1781}
1782
Keith Busch0e5e4f02012-11-09 16:33:05 -07001783static void nvme_config_discard(struct nvme_ns *ns)
1784{
1785 u32 logical_block_size = queue_logical_block_size(ns->queue);
1786 ns->queue->limits.discard_zeroes_data = 0;
1787 ns->queue->limits.discard_alignment = logical_block_size;
1788 ns->queue->limits.discard_granularity = logical_block_size;
1789 ns->queue->limits.max_discard_sectors = 0xffffffff;
1790 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1791}
1792
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001793static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001794 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1795{
1796 struct nvme_ns *ns;
1797 struct gendisk *disk;
1798 int lbaf;
1799
1800 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1801 return NULL;
1802
1803 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1804 if (!ns)
1805 return NULL;
1806 ns->queue = blk_alloc_queue(GFP_KERNEL);
1807 if (!ns->queue)
1808 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001809 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
1810 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1811 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001812 blk_queue_make_request(ns->queue, nvme_make_request);
1813 ns->dev = dev;
1814 ns->queue->queuedata = ns;
1815
Matthew Wilcox469071a2013-12-09 12:58:46 -05001816 disk = alloc_disk(0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001817 if (!disk)
1818 goto out_free_queue;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001819 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001820 ns->disk = disk;
1821 lbaf = id->flbas & 0xf;
1822 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Buschf410c682013-04-23 17:23:59 -06001823 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busche9ef4632012-07-24 15:01:04 -06001824 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06001825 if (dev->max_hw_sectors)
1826 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001827
1828 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001829 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001830 disk->fops = &nvme_fops;
1831 disk->private_data = ns;
1832 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05001833 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001834 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001835 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001836 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1837
Keith Busch0e5e4f02012-11-09 16:33:05 -07001838 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1839 nvme_config_discard(ns);
1840
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001841 return ns;
1842
1843 out_free_queue:
1844 blk_cleanup_queue(ns->queue);
1845 out_free_ns:
1846 kfree(ns);
1847 return NULL;
1848}
1849
Keith Busch42f61422014-03-24 10:46:25 -06001850static int nvme_find_closest_node(int node)
1851{
1852 int n, val, min_val = INT_MAX, best_node = node;
1853
1854 for_each_online_node(n) {
1855 if (n == node)
1856 continue;
1857 val = node_distance(node, n);
1858 if (val < min_val) {
1859 min_val = val;
1860 best_node = n;
1861 }
1862 }
1863 return best_node;
1864}
1865
1866static void nvme_set_queue_cpus(cpumask_t *qmask, struct nvme_queue *nvmeq,
1867 int count)
1868{
1869 int cpu;
1870 for_each_cpu(cpu, qmask) {
1871 if (cpumask_weight(nvmeq->cpu_mask) >= count)
1872 break;
1873 if (!cpumask_test_and_set_cpu(cpu, nvmeq->cpu_mask))
1874 *per_cpu_ptr(nvmeq->dev->io_queue, cpu) = nvmeq->qid;
1875 }
1876}
1877
1878static void nvme_add_cpus(cpumask_t *mask, const cpumask_t *unassigned_cpus,
1879 const cpumask_t *new_mask, struct nvme_queue *nvmeq, int cpus_per_queue)
1880{
1881 int next_cpu;
1882 for_each_cpu(next_cpu, new_mask) {
1883 cpumask_or(mask, mask, get_cpu_mask(next_cpu));
1884 cpumask_or(mask, mask, topology_thread_cpumask(next_cpu));
1885 cpumask_and(mask, mask, unassigned_cpus);
1886 nvme_set_queue_cpus(mask, nvmeq, cpus_per_queue);
1887 }
1888}
1889
1890static void nvme_create_io_queues(struct nvme_dev *dev)
1891{
1892 unsigned i, max;
1893
1894 max = min(dev->max_qid, num_online_cpus());
1895 for (i = dev->queue_count; i <= max; i++)
1896 if (!nvme_alloc_queue(dev, i, dev->q_depth, i - 1))
1897 break;
1898
1899 max = min(dev->queue_count - 1, num_online_cpus());
1900 for (i = dev->online_queues; i <= max; i++)
1901 if (nvme_create_queue(raw_nvmeq(dev, i), i))
1902 break;
1903}
1904
1905/*
1906 * If there are fewer queues than online cpus, this will try to optimally
1907 * assign a queue to multiple cpus by grouping cpus that are "close" together:
1908 * thread siblings, core, socket, closest node, then whatever else is
1909 * available.
1910 */
1911static void nvme_assign_io_queues(struct nvme_dev *dev)
1912{
1913 unsigned cpu, cpus_per_queue, queues, remainder, i;
1914 cpumask_var_t unassigned_cpus;
1915
1916 nvme_create_io_queues(dev);
1917
1918 queues = min(dev->online_queues - 1, num_online_cpus());
1919 if (!queues)
1920 return;
1921
1922 cpus_per_queue = num_online_cpus() / queues;
1923 remainder = queues - (num_online_cpus() - queues * cpus_per_queue);
1924
1925 if (!alloc_cpumask_var(&unassigned_cpus, GFP_KERNEL))
1926 return;
1927
1928 cpumask_copy(unassigned_cpus, cpu_online_mask);
1929 cpu = cpumask_first(unassigned_cpus);
1930 for (i = 1; i <= queues; i++) {
1931 struct nvme_queue *nvmeq = lock_nvmeq(dev, i);
1932 cpumask_t mask;
1933
1934 cpumask_clear(nvmeq->cpu_mask);
1935 if (!cpumask_weight(unassigned_cpus)) {
1936 unlock_nvmeq(nvmeq);
1937 break;
1938 }
1939
1940 mask = *get_cpu_mask(cpu);
1941 nvme_set_queue_cpus(&mask, nvmeq, cpus_per_queue);
1942 if (cpus_weight(mask) < cpus_per_queue)
1943 nvme_add_cpus(&mask, unassigned_cpus,
1944 topology_thread_cpumask(cpu),
1945 nvmeq, cpus_per_queue);
1946 if (cpus_weight(mask) < cpus_per_queue)
1947 nvme_add_cpus(&mask, unassigned_cpus,
1948 topology_core_cpumask(cpu),
1949 nvmeq, cpus_per_queue);
1950 if (cpus_weight(mask) < cpus_per_queue)
1951 nvme_add_cpus(&mask, unassigned_cpus,
1952 cpumask_of_node(cpu_to_node(cpu)),
1953 nvmeq, cpus_per_queue);
1954 if (cpus_weight(mask) < cpus_per_queue)
1955 nvme_add_cpus(&mask, unassigned_cpus,
1956 cpumask_of_node(
1957 nvme_find_closest_node(
1958 cpu_to_node(cpu))),
1959 nvmeq, cpus_per_queue);
1960 if (cpus_weight(mask) < cpus_per_queue)
1961 nvme_add_cpus(&mask, unassigned_cpus,
1962 unassigned_cpus,
1963 nvmeq, cpus_per_queue);
1964
1965 WARN(cpumask_weight(nvmeq->cpu_mask) != cpus_per_queue,
1966 "nvme%d qid:%d mis-matched queue-to-cpu assignment\n",
1967 dev->instance, i);
1968
1969 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
1970 nvmeq->cpu_mask);
1971 cpumask_andnot(unassigned_cpus, unassigned_cpus,
1972 nvmeq->cpu_mask);
1973 cpu = cpumask_next(cpu, unassigned_cpus);
1974 if (remainder && !--remainder)
1975 cpus_per_queue++;
1976 unlock_nvmeq(nvmeq);
1977 }
1978 WARN(cpumask_weight(unassigned_cpus), "nvme%d unassigned online cpus\n",
1979 dev->instance);
1980 i = 0;
1981 cpumask_andnot(unassigned_cpus, cpu_possible_mask, cpu_online_mask);
1982 for_each_cpu(cpu, unassigned_cpus)
1983 *per_cpu_ptr(dev->io_queue, cpu) = (i++ % queues) + 1;
1984 free_cpumask_var(unassigned_cpus);
1985}
1986
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001987static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001988{
1989 int status;
1990 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001991 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001992
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001993 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001994 &result);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001995 if (status)
Keith Busch7e03b122013-07-29 16:20:56 -06001996 return status < 0 ? -EIO : -EBUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001997 return min(result & 0xffff, result >> 16) + 1;
1998}
1999
Keith Busch9d713c22013-07-15 15:02:24 -06002000static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
2001{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002002 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06002003}
2004
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002005static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002006{
Keith Busch5a92e702014-02-21 14:13:44 -07002007 struct nvme_queue *adminq = raw_nvmeq(dev, 0);
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002008 struct pci_dev *pdev = dev->pci_dev;
Keith Busch42f61422014-03-24 10:46:25 -06002009 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002010
Keith Busch42f61422014-03-24 10:46:25 -06002011 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002012 result = set_queue_count(dev, nr_io_queues);
Matthew Wilcox1b234842011-01-20 13:01:49 -05002013 if (result < 0)
2014 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002015 if (result < nr_io_queues)
2016 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002017
Keith Busch9d713c22013-07-15 15:02:24 -06002018 size = db_bar_size(dev, nr_io_queues);
2019 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002020 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06002021 do {
2022 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
2023 if (dev->bar)
2024 break;
2025 if (!--nr_io_queues)
2026 return -ENOMEM;
2027 size = db_bar_size(dev, nr_io_queues);
2028 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002029 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07002030 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002031 }
2032
Keith Busch9d713c22013-07-15 15:02:24 -06002033 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002034 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002035
Matthew Wilcox063a8092013-06-20 10:53:48 -04002036 vecs = nr_io_queues;
2037 for (i = 0; i < vecs; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002038 dev->entry[i].entry = i;
2039 for (;;) {
Matthew Wilcox063a8092013-06-20 10:53:48 -04002040 result = pci_enable_msix(pdev, dev->entry, vecs);
2041 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002042 break;
Matthew Wilcox063a8092013-06-20 10:53:48 -04002043 vecs = result;
2044 }
2045
2046 if (result < 0) {
2047 vecs = nr_io_queues;
2048 if (vecs > 32)
2049 vecs = 32;
2050 for (;;) {
2051 result = pci_enable_msi_block(pdev, vecs);
2052 if (result == 0) {
2053 for (i = 0; i < vecs; i++)
2054 dev->entry[i].vector = i + pdev->irq;
2055 break;
2056 } else if (result < 0) {
2057 vecs = 1;
2058 break;
2059 }
2060 vecs = result;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002061 }
2062 }
2063
Matthew Wilcox063a8092013-06-20 10:53:48 -04002064 /*
2065 * Should investigate if there's a performance win from allocating
2066 * more queues than interrupt vectors; it might allow the submission
2067 * path to scale better, even if the receive path is limited by the
2068 * number of interrupts.
2069 */
2070 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002071 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002072
Matthew Wilcox3193f072014-01-27 15:57:22 -05002073 result = queue_request_irq(dev, adminq, adminq->irqname);
Keith Busch9d713c22013-07-15 15:02:24 -06002074 if (result) {
Matthew Wilcox3193f072014-01-27 15:57:22 -05002075 adminq->q_suspended = 1;
Keith Busch22404272013-07-15 15:02:20 -06002076 goto free_queues;
Keith Busch9d713c22013-07-15 15:02:24 -06002077 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05002078
Keith Buschcd638942013-07-15 15:02:23 -06002079 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002080 nvme_free_queues(dev, nr_io_queues + 1);
2081 nvme_assign_io_queues(dev);
Keith Busch22404272013-07-15 15:02:20 -06002082
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002083 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002084
Keith Busch22404272013-07-15 15:02:20 -06002085 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002086 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002087 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002088}
2089
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002090/*
2091 * Return: error value if an error occurred setting up the queues or calling
2092 * Identify Device. 0 if these succeeded, even if adding some of the
2093 * namespaces failed. At the moment, these failures are silent. TBD which
2094 * failures should be reported.
2095 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002096static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002097{
Matthew Wilcox68608c262013-06-21 14:36:34 -04002098 struct pci_dev *pdev = dev->pci_dev;
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002099 int res;
2100 unsigned nn, i;
Keith Buschcbb62182013-05-01 13:07:49 -06002101 struct nvme_ns *ns;
Matthew Wilcox51814232011-02-01 16:18:08 -05002102 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002103 struct nvme_id_ns *id_ns;
2104 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002105 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06002106 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002107
Matthew Wilcox68608c262013-06-21 14:36:34 -04002108 mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06002109 if (!mem)
2110 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002111
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002112 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002113 if (res) {
2114 res = -EIO;
Keith Buschcbb62182013-05-01 13:07:49 -06002115 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002116 }
2117
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002118 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05002119 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002120 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002121 dev->abort_limit = ctrl->acl + 1;
Matthew Wilcox51814232011-02-01 16:18:08 -05002122 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2123 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2124 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002125 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002126 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002127 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
2128 (pdev->device == 0x0953) && ctrl->vs[3])
Keith Busch159b67d2013-04-09 17:13:20 -06002129 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002130
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002131 id_ns = mem;
Matthew Wilcox2b2c1892011-10-07 13:10:13 -04002132 for (i = 1; i <= nn; i++) {
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002133 res = nvme_identify(dev, i, 0, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002134 if (res)
2135 continue;
2136
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002137 if (id_ns->ncap == 0)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002138 continue;
2139
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002140 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
Keith Busch08df1e02012-09-21 10:52:13 -06002141 dma_addr + 4096, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002142 if (res)
Keith Busch12209032013-01-31 14:40:38 -07002143 memset(mem + 4096, 0, 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002144
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002145 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002146 if (ns)
2147 list_add_tail(&ns->list, &dev->namespaces);
2148 }
2149 list_for_each_entry(ns, &dev->namespaces, list)
2150 add_disk(ns->disk);
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002151 res = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002152
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002153 out:
Matthew Wilcox684f5c22011-09-19 17:14:53 -04002154 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002155 return res;
2156}
2157
Keith Busch0877cb02013-07-15 15:02:19 -06002158static int nvme_dev_map(struct nvme_dev *dev)
2159{
Keith Busch42f61422014-03-24 10:46:25 -06002160 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002161 int bars, result = -ENOMEM;
2162 struct pci_dev *pdev = dev->pci_dev;
2163
2164 if (pci_enable_device_mem(pdev))
2165 return result;
2166
2167 dev->entry[0].vector = pdev->irq;
2168 pci_set_master(pdev);
2169 bars = pci_select_bars(pdev, IORESOURCE_MEM);
2170 if (pci_request_selected_regions(pdev, bars, "nvme"))
2171 goto disable_pci;
2172
Russell King052d0ef2013-06-26 23:49:11 +01002173 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
2174 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2175 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002176
Keith Busch0877cb02013-07-15 15:02:19 -06002177 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2178 if (!dev->bar)
2179 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07002180 if (readl(&dev->bar->csts) == -1) {
2181 result = -ENODEV;
2182 goto unmap;
2183 }
Keith Busch42f61422014-03-24 10:46:25 -06002184 cap = readq(&dev->bar->cap);
2185 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2186 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Keith Busch0877cb02013-07-15 15:02:19 -06002187 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2188
2189 return 0;
2190
Keith Busch0e53d182013-12-10 13:10:39 -07002191 unmap:
2192 iounmap(dev->bar);
2193 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002194 disable:
2195 pci_release_regions(pdev);
2196 disable_pci:
2197 pci_disable_device(pdev);
2198 return result;
2199}
2200
2201static void nvme_dev_unmap(struct nvme_dev *dev)
2202{
2203 if (dev->pci_dev->msi_enabled)
2204 pci_disable_msi(dev->pci_dev);
2205 else if (dev->pci_dev->msix_enabled)
2206 pci_disable_msix(dev->pci_dev);
2207
2208 if (dev->bar) {
2209 iounmap(dev->bar);
2210 dev->bar = NULL;
Keith Busch9a6b9452013-12-10 13:10:36 -07002211 pci_release_regions(dev->pci_dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002212 }
2213
Keith Busch0877cb02013-07-15 15:02:19 -06002214 if (pci_is_enabled(dev->pci_dev))
2215 pci_disable_device(dev->pci_dev);
2216}
2217
Keith Busch4d115422013-12-10 13:10:40 -07002218struct nvme_delq_ctx {
2219 struct task_struct *waiter;
2220 struct kthread_worker *worker;
2221 atomic_t refcount;
2222};
2223
2224static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2225{
2226 dq->waiter = current;
2227 mb();
2228
2229 for (;;) {
2230 set_current_state(TASK_KILLABLE);
2231 if (!atomic_read(&dq->refcount))
2232 break;
2233 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2234 fatal_signal_pending(current)) {
2235 set_current_state(TASK_RUNNING);
2236
2237 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
2238 nvme_disable_queue(dev, 0);
2239
2240 send_sig(SIGKILL, dq->worker->task, 1);
2241 flush_kthread_worker(dq->worker);
2242 return;
2243 }
2244 }
2245 set_current_state(TASK_RUNNING);
2246}
2247
2248static void nvme_put_dq(struct nvme_delq_ctx *dq)
2249{
2250 atomic_dec(&dq->refcount);
2251 if (dq->waiter)
2252 wake_up_process(dq->waiter);
2253}
2254
2255static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2256{
2257 atomic_inc(&dq->refcount);
2258 return dq;
2259}
2260
2261static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2262{
2263 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
2264
2265 nvme_clear_queue(nvmeq);
2266 nvme_put_dq(dq);
2267}
2268
2269static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2270 kthread_work_func_t fn)
2271{
2272 struct nvme_command c;
2273
2274 memset(&c, 0, sizeof(c));
2275 c.delete_queue.opcode = opcode;
2276 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2277
2278 init_kthread_work(&nvmeq->cmdinfo.work, fn);
2279 return nvme_submit_admin_cmd_async(nvmeq->dev, &c, &nvmeq->cmdinfo);
2280}
2281
2282static void nvme_del_cq_work_handler(struct kthread_work *work)
2283{
2284 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2285 cmdinfo.work);
2286 nvme_del_queue_end(nvmeq);
2287}
2288
2289static int nvme_delete_cq(struct nvme_queue *nvmeq)
2290{
2291 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2292 nvme_del_cq_work_handler);
2293}
2294
2295static void nvme_del_sq_work_handler(struct kthread_work *work)
2296{
2297 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2298 cmdinfo.work);
2299 int status = nvmeq->cmdinfo.status;
2300
2301 if (!status)
2302 status = nvme_delete_cq(nvmeq);
2303 if (status)
2304 nvme_del_queue_end(nvmeq);
2305}
2306
2307static int nvme_delete_sq(struct nvme_queue *nvmeq)
2308{
2309 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2310 nvme_del_sq_work_handler);
2311}
2312
2313static void nvme_del_queue_start(struct kthread_work *work)
2314{
2315 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2316 cmdinfo.work);
2317 allow_signal(SIGKILL);
2318 if (nvme_delete_sq(nvmeq))
2319 nvme_del_queue_end(nvmeq);
2320}
2321
2322static void nvme_disable_io_queues(struct nvme_dev *dev)
2323{
2324 int i;
2325 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2326 struct nvme_delq_ctx dq;
2327 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2328 &worker, "nvme%d", dev->instance);
2329
2330 if (IS_ERR(kworker_task)) {
2331 dev_err(&dev->pci_dev->dev,
2332 "Failed to create queue del task\n");
2333 for (i = dev->queue_count - 1; i > 0; i--)
2334 nvme_disable_queue(dev, i);
2335 return;
2336 }
2337
2338 dq.waiter = NULL;
2339 atomic_set(&dq.refcount, 0);
2340 dq.worker = &worker;
2341 for (i = dev->queue_count - 1; i > 0; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07002342 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
Keith Busch4d115422013-12-10 13:10:40 -07002343
2344 if (nvme_suspend_queue(nvmeq))
2345 continue;
2346 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2347 nvmeq->cmdinfo.worker = dq.worker;
2348 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2349 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2350 }
2351 nvme_wait_dq(&dq, dev);
2352 kthread_stop(kworker_task);
2353}
2354
Keith Buschf0b50732013-07-15 15:02:21 -06002355static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002356{
Keith Busch22404272013-07-15 15:02:20 -06002357 int i;
2358
Keith Buschd4b4ff82013-12-10 13:10:37 -07002359 dev->initialized = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002360
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002361 spin_lock(&dev_list_lock);
Keith Buschf0b50732013-07-15 15:02:21 -06002362 list_del_init(&dev->node);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002363 spin_unlock(&dev_list_lock);
2364
Keith Busch4d115422013-12-10 13:10:40 -07002365 if (!dev->bar || (dev->bar && readl(&dev->bar->csts) == -1)) {
2366 for (i = dev->queue_count - 1; i >= 0; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07002367 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
Keith Busch4d115422013-12-10 13:10:40 -07002368 nvme_suspend_queue(nvmeq);
2369 nvme_clear_queue(nvmeq);
2370 }
2371 } else {
2372 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002373 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002374 nvme_disable_queue(dev, 0);
2375 }
Keith Buschf0b50732013-07-15 15:02:21 -06002376 nvme_dev_unmap(dev);
2377}
2378
2379static void nvme_dev_remove(struct nvme_dev *dev)
2380{
Keith Busch9ac27092014-01-31 16:53:39 -07002381 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002382
Keith Busch9ac27092014-01-31 16:53:39 -07002383 list_for_each_entry(ns, &dev->namespaces, list) {
2384 if (ns->disk->flags & GENHD_FL_UP)
2385 del_gendisk(ns->disk);
2386 if (!blk_queue_dying(ns->queue))
2387 blk_cleanup_queue(ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002388 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002389}
2390
Matthew Wilcox091b6092011-02-10 09:56:01 -05002391static int nvme_setup_prp_pools(struct nvme_dev *dev)
2392{
2393 struct device *dmadev = &dev->pci_dev->dev;
2394 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2395 PAGE_SIZE, PAGE_SIZE, 0);
2396 if (!dev->prp_page_pool)
2397 return -ENOMEM;
2398
Matthew Wilcox99802a72011-02-10 10:30:34 -05002399 /* Optimisation for I/Os between 4k and 128k */
2400 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2401 256, 256, 0);
2402 if (!dev->prp_small_pool) {
2403 dma_pool_destroy(dev->prp_page_pool);
2404 return -ENOMEM;
2405 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002406 return 0;
2407}
2408
2409static void nvme_release_prp_pools(struct nvme_dev *dev)
2410{
2411 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002412 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002413}
2414
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002415static DEFINE_IDA(nvme_instance_ida);
2416
2417static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002418{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002419 int instance, error;
2420
2421 do {
2422 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2423 return -ENODEV;
2424
2425 spin_lock(&dev_list_lock);
2426 error = ida_get_new(&nvme_instance_ida, &instance);
2427 spin_unlock(&dev_list_lock);
2428 } while (error == -EAGAIN);
2429
2430 if (error)
2431 return -ENODEV;
2432
2433 dev->instance = instance;
2434 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002435}
2436
2437static void nvme_release_instance(struct nvme_dev *dev)
2438{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002439 spin_lock(&dev_list_lock);
2440 ida_remove(&nvme_instance_ida, dev->instance);
2441 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002442}
2443
Keith Busch9ac27092014-01-31 16:53:39 -07002444static void nvme_free_namespaces(struct nvme_dev *dev)
2445{
2446 struct nvme_ns *ns, *next;
2447
2448 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2449 list_del(&ns->list);
2450 put_disk(ns->disk);
2451 kfree(ns);
2452 }
2453}
2454
Keith Busch5e82e952013-02-19 10:17:58 -07002455static void nvme_free_dev(struct kref *kref)
2456{
2457 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002458
2459 nvme_free_namespaces(dev);
Keith Busch42f61422014-03-24 10:46:25 -06002460 free_percpu(dev->io_queue);
Keith Busch5e82e952013-02-19 10:17:58 -07002461 kfree(dev->queues);
2462 kfree(dev->entry);
2463 kfree(dev);
2464}
2465
2466static int nvme_dev_open(struct inode *inode, struct file *f)
2467{
2468 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
2469 miscdev);
2470 kref_get(&dev->kref);
2471 f->private_data = dev;
2472 return 0;
2473}
2474
2475static int nvme_dev_release(struct inode *inode, struct file *f)
2476{
2477 struct nvme_dev *dev = f->private_data;
2478 kref_put(&dev->kref, nvme_free_dev);
2479 return 0;
2480}
2481
2482static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2483{
2484 struct nvme_dev *dev = f->private_data;
2485 switch (cmd) {
2486 case NVME_IOCTL_ADMIN_CMD:
2487 return nvme_user_admin_cmd(dev, (void __user *)arg);
2488 default:
2489 return -ENOTTY;
2490 }
2491}
2492
2493static const struct file_operations nvme_dev_fops = {
2494 .owner = THIS_MODULE,
2495 .open = nvme_dev_open,
2496 .release = nvme_dev_release,
2497 .unlocked_ioctl = nvme_dev_ioctl,
2498 .compat_ioctl = nvme_dev_ioctl,
2499};
2500
Keith Buschf0b50732013-07-15 15:02:21 -06002501static int nvme_dev_start(struct nvme_dev *dev)
2502{
2503 int result;
2504
2505 result = nvme_dev_map(dev);
2506 if (result)
2507 return result;
2508
2509 result = nvme_configure_admin_queue(dev);
2510 if (result)
2511 goto unmap;
2512
2513 spin_lock(&dev_list_lock);
2514 list_add(&dev->node, &dev_list);
2515 spin_unlock(&dev_list_lock);
2516
2517 result = nvme_setup_io_queues(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002518 if (result && result != -EBUSY)
Keith Buschf0b50732013-07-15 15:02:21 -06002519 goto disable;
2520
Keith Buschd82e8bf2013-09-05 14:45:07 -06002521 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002522
2523 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002524 nvme_disable_queue(dev, 0);
Keith Buschf0b50732013-07-15 15:02:21 -06002525 spin_lock(&dev_list_lock);
2526 list_del_init(&dev->node);
2527 spin_unlock(&dev_list_lock);
2528 unmap:
2529 nvme_dev_unmap(dev);
2530 return result;
2531}
2532
Keith Busch9a6b9452013-12-10 13:10:36 -07002533static int nvme_remove_dead_ctrl(void *arg)
2534{
2535 struct nvme_dev *dev = (struct nvme_dev *)arg;
2536 struct pci_dev *pdev = dev->pci_dev;
2537
2538 if (pci_get_drvdata(pdev))
2539 pci_stop_and_remove_bus_device(pdev);
2540 kref_put(&dev->kref, nvme_free_dev);
2541 return 0;
2542}
2543
2544static void nvme_remove_disks(struct work_struct *ws)
2545{
Keith Busch9a6b9452013-12-10 13:10:36 -07002546 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2547
2548 nvme_dev_remove(dev);
Keith Busch5a92e702014-02-21 14:13:44 -07002549 nvme_free_queues(dev, 1);
Keith Busch9a6b9452013-12-10 13:10:36 -07002550}
2551
2552static int nvme_dev_resume(struct nvme_dev *dev)
2553{
2554 int ret;
2555
2556 ret = nvme_dev_start(dev);
2557 if (ret && ret != -EBUSY)
2558 return ret;
2559 if (ret == -EBUSY) {
2560 spin_lock(&dev_list_lock);
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05002561 PREPARE_WORK(&dev->reset_work, nvme_remove_disks);
Keith Busch9a6b9452013-12-10 13:10:36 -07002562 queue_work(nvme_workq, &dev->reset_work);
2563 spin_unlock(&dev_list_lock);
2564 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002565 dev->initialized = 1;
Keith Busch9a6b9452013-12-10 13:10:36 -07002566 return 0;
2567}
2568
2569static void nvme_dev_reset(struct nvme_dev *dev)
2570{
2571 nvme_dev_shutdown(dev);
2572 if (nvme_dev_resume(dev)) {
2573 dev_err(&dev->pci_dev->dev, "Device failed to resume\n");
2574 kref_get(&dev->kref);
2575 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2576 dev->instance))) {
2577 dev_err(&dev->pci_dev->dev,
2578 "Failed to start controller remove task\n");
2579 kref_put(&dev->kref, nvme_free_dev);
2580 }
2581 }
2582}
2583
2584static void nvme_reset_failed_dev(struct work_struct *ws)
2585{
2586 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2587 nvme_dev_reset(dev);
2588}
2589
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002590static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002591{
Keith Busch0877cb02013-07-15 15:02:19 -06002592 int result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002593 struct nvme_dev *dev;
2594
2595 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2596 if (!dev)
2597 return -ENOMEM;
2598 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
2599 GFP_KERNEL);
2600 if (!dev->entry)
2601 goto free;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002602 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
2603 GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002604 if (!dev->queues)
2605 goto free;
Keith Busch42f61422014-03-24 10:46:25 -06002606 dev->io_queue = alloc_percpu(unsigned short);
2607 if (!dev->io_queue)
2608 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002609
2610 INIT_LIST_HEAD(&dev->namespaces);
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05002611 INIT_WORK(&dev->reset_work, nvme_reset_failed_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002612 dev->pci_dev = pdev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002613 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002614 result = nvme_set_instance(dev);
2615 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002616 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002617
Matthew Wilcox091b6092011-02-10 09:56:01 -05002618 result = nvme_setup_prp_pools(dev);
2619 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002620 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002621
Keith Buschfb35e912014-03-03 11:09:47 -07002622 kref_init(&dev->kref);
Keith Buschf0b50732013-07-15 15:02:21 -06002623 result = nvme_dev_start(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002624 if (result) {
2625 if (result == -EBUSY)
2626 goto create_cdev;
Keith Busch0877cb02013-07-15 15:02:19 -06002627 goto release_pools;
Keith Buschd82e8bf2013-09-05 14:45:07 -06002628 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002629
Matthew Wilcox740216f2011-02-15 16:28:20 -05002630 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002631 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002632 goto shutdown;
Matthew Wilcox740216f2011-02-15 16:28:20 -05002633
Keith Buschd82e8bf2013-09-05 14:45:07 -06002634 create_cdev:
Keith Busch5e82e952013-02-19 10:17:58 -07002635 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
2636 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2637 dev->miscdev.parent = &pdev->dev;
2638 dev->miscdev.name = dev->name;
2639 dev->miscdev.fops = &nvme_dev_fops;
2640 result = misc_register(&dev->miscdev);
2641 if (result)
2642 goto remove;
2643
Keith Buschd4b4ff82013-12-10 13:10:37 -07002644 dev->initialized = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002645 return 0;
2646
Keith Busch5e82e952013-02-19 10:17:58 -07002647 remove:
2648 nvme_dev_remove(dev);
Keith Busch9ac27092014-01-31 16:53:39 -07002649 nvme_free_namespaces(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002650 shutdown:
2651 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002652 release_pools:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002653 nvme_free_queues(dev, 0);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002654 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002655 release:
2656 nvme_release_instance(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002657 free:
Keith Busch42f61422014-03-24 10:46:25 -06002658 free_percpu(dev->io_queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002659 kfree(dev->queues);
2660 kfree(dev->entry);
2661 kfree(dev);
2662 return result;
2663}
2664
Keith Busch09ece142014-01-27 11:29:40 -05002665static void nvme_shutdown(struct pci_dev *pdev)
2666{
2667 struct nvme_dev *dev = pci_get_drvdata(pdev);
2668 nvme_dev_shutdown(dev);
2669}
2670
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002671static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002672{
2673 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002674
2675 spin_lock(&dev_list_lock);
2676 list_del_init(&dev->node);
2677 spin_unlock(&dev_list_lock);
2678
2679 pci_set_drvdata(pdev, NULL);
2680 flush_work(&dev->reset_work);
Keith Busch5e82e952013-02-19 10:17:58 -07002681 misc_deregister(&dev->miscdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002682 nvme_dev_remove(dev);
2683 nvme_dev_shutdown(dev);
Keith Buscha1a5ef92013-12-16 13:50:00 -05002684 nvme_free_queues(dev, 0);
Keith Busch5a92e702014-02-21 14:13:44 -07002685 rcu_barrier();
Keith Busch9a6b9452013-12-10 13:10:36 -07002686 nvme_release_instance(dev);
2687 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002688 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002689}
2690
2691/* These functions are yet to be implemented */
2692#define nvme_error_detected NULL
2693#define nvme_dump_registers NULL
2694#define nvme_link_reset NULL
2695#define nvme_slot_reset NULL
2696#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002697
Jingoo Han671a6012014-02-13 11:19:14 +09002698#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002699static int nvme_suspend(struct device *dev)
2700{
2701 struct pci_dev *pdev = to_pci_dev(dev);
2702 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2703
2704 nvme_dev_shutdown(ndev);
2705 return 0;
2706}
2707
2708static int nvme_resume(struct device *dev)
2709{
2710 struct pci_dev *pdev = to_pci_dev(dev);
2711 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002712
Keith Busch9a6b9452013-12-10 13:10:36 -07002713 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05002714 PREPARE_WORK(&ndev->reset_work, nvme_reset_failed_dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002715 queue_work(nvme_workq, &ndev->reset_work);
2716 }
2717 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002718}
Jingoo Han671a6012014-02-13 11:19:14 +09002719#endif
Keith Buschcd638942013-07-15 15:02:23 -06002720
2721static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002722
Stephen Hemminger1d352032012-09-07 09:33:17 -07002723static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002724 .error_detected = nvme_error_detected,
2725 .mmio_enabled = nvme_dump_registers,
2726 .link_reset = nvme_link_reset,
2727 .slot_reset = nvme_slot_reset,
2728 .resume = nvme_error_resume,
2729};
2730
2731/* Move to pci_ids.h later */
2732#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2733
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002734static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002735 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2736 { 0, }
2737};
2738MODULE_DEVICE_TABLE(pci, nvme_id_table);
2739
2740static struct pci_driver nvme_driver = {
2741 .name = "nvme",
2742 .id_table = nvme_id_table,
2743 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002744 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002745 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002746 .driver = {
2747 .pm = &nvme_dev_pm_ops,
2748 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002749 .err_handler = &nvme_err_handler,
2750};
2751
2752static int __init nvme_init(void)
2753{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002754 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002755
2756 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2757 if (IS_ERR(nvme_thread))
2758 return PTR_ERR(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002759
Keith Busch9a6b9452013-12-10 13:10:36 -07002760 result = -ENOMEM;
2761 nvme_workq = create_singlethread_workqueue("nvme");
2762 if (!nvme_workq)
2763 goto kill_kthread;
2764
Keith Busch5c42ea12012-07-25 16:05:18 -06002765 result = register_blkdev(nvme_major, "nvme");
2766 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002767 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06002768 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002769 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002770
2771 result = pci_register_driver(&nvme_driver);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002772 if (result)
2773 goto unregister_blkdev;
2774 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002775
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002776 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002777 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002778 kill_workq:
2779 destroy_workqueue(nvme_workq);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002780 kill_kthread:
2781 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002782 return result;
2783}
2784
2785static void __exit nvme_exit(void)
2786{
2787 pci_unregister_driver(&nvme_driver);
2788 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002789 destroy_workqueue(nvme_workq);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002790 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002791}
2792
2793MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2794MODULE_LICENSE("GPL");
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002795MODULE_VERSION("0.9");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002796module_init(nvme_init);
2797module_exit(nvme_exit);