blob: b9f07f81ea5d7c12cb6da588968e325aec084351 [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.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050013 */
14
15#include <linux/nvme.h>
16#include <linux/bio.h>
Matthew Wilcox8de05532011-05-12 13:50:28 -040017#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050018#include <linux/blkdev.h>
Keith Busch42f61422014-03-24 10:46:25 -060019#include <linux/cpu.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040020#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050021#include <linux/errno.h>
22#include <linux/fs.h>
23#include <linux/genhd.h>
Keith Busch4cc09e22014-04-02 15:45:37 -060024#include <linux/hdreg.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040025#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050026#include <linux/init.h>
27#include <linux/interrupt.h>
28#include <linux/io.h>
29#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050030#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050031#include <linux/kernel.h>
32#include <linux/mm.h>
33#include <linux/module.h>
34#include <linux/moduleparam.h>
35#include <linux/pci.h>
Keith Busch42f61422014-03-24 10:46:25 -060036#include <linux/percpu.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050037#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040038#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050039#include <linux/sched.h>
40#include <linux/slab.h>
41#include <linux/types.h>
Vishal Verma5d0f6132013-03-04 18:40:58 -070042#include <scsi/sg.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090043#include <asm-generic/io-64-nonatomic-lo-hi.h>
44
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050045#define NVME_Q_DEPTH 1024
46#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
47#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Matthew Wilcoxe85248e2011-02-06 18:30:16 -050048#define ADMIN_TIMEOUT (60 * HZ)
Keith Buschedd10d32014-04-03 16:45:23 -060049#define IOD_TIMEOUT (4 * NVME_IO_TIMEOUT)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050050
Keith Buschb3550842014-04-04 11:43:36 -060051unsigned char io_timeout = 30;
52module_param(io_timeout, byte, 0644);
53MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050054
55static int nvme_major;
56module_param(nvme_major, int, 0);
57
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050058static int use_threaded_interrupts;
59module_param(use_threaded_interrupts, int, 0);
60
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050061static DEFINE_SPINLOCK(dev_list_lock);
62static LIST_HEAD(dev_list);
63static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070064static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060065static wait_queue_head_t nvme_kthread_wait;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050066
Keith Buschd4b4ff82013-12-10 13:10:37 -070067static void nvme_reset_failed_dev(struct work_struct *ws);
68
Keith Busch4d115422013-12-10 13:10:40 -070069struct async_cmd_info {
70 struct kthread_work work;
71 struct kthread_worker *worker;
72 u32 result;
73 int status;
74 void *ctx;
75};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050076
77/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050078 * An NVM Express queue. Each device has at least two (one for admin
79 * commands and one for I/O commands).
80 */
81struct nvme_queue {
Keith Busch5a92e702014-02-21 14:13:44 -070082 struct rcu_head r_head;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050083 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -050084 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -050085 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050086 spinlock_t q_lock;
87 struct nvme_command *sq_cmds;
88 volatile struct nvme_completion *cqes;
89 dma_addr_t sq_dma_addr;
90 dma_addr_t cq_dma_addr;
91 wait_queue_head_t sq_full;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050092 wait_queue_t sq_cong_wait;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050093 struct bio_list sq_cong;
Keith Buschedd10d32014-04-03 16:45:23 -060094 struct list_head iod_bio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050095 u32 __iomem *q_db;
96 u16 q_depth;
97 u16 cq_vector;
98 u16 sq_head;
99 u16 sq_tail;
100 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700101 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400102 u8 cq_phase;
103 u8 cqe_seen;
Keith Busch22404272013-07-15 15:02:20 -0600104 u8 q_suspended;
Keith Busch42f61422014-03-24 10:46:25 -0600105 cpumask_var_t cpu_mask;
Keith Busch4d115422013-12-10 13:10:40 -0700106 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500107 unsigned long cmdid_data[];
108};
109
110/*
111 * Check we didin't inadvertently grow the command struct
112 */
113static inline void _nvme_check_size(void)
114{
115 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
116 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
117 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
118 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
119 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400120 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700121 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500122 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
123 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
124 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
125 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600126 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500127}
128
Keith Buschedd10d32014-04-03 16:45:23 -0600129typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400130 struct nvme_completion *);
131
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500132struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400133 nvme_completion_fn fn;
134 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500135 unsigned long timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700136 int aborted;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500137};
138
139static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
140{
141 return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
142}
143
Keith Busch22404272013-07-15 15:02:20 -0600144static unsigned nvme_queue_extra(int depth)
145{
146 return DIV_ROUND_UP(depth, 8) + (depth * sizeof(struct nvme_cmd_info));
147}
148
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500149/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400150 * alloc_cmdid() - Allocate a Command ID
151 * @nvmeq: The queue that will be used for this command
152 * @ctx: A pointer that will be passed to the handler
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400153 * @handler: The function to call on completion
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500154 *
155 * Allocate a Command ID for a queue. The data passed in will
156 * be passed to the completion handler. This is implemented by using
157 * the bottom two bits of the ctx pointer to store the handler ID.
158 * Passing in a pointer that's not 4-byte aligned will cause a BUG.
159 * We can change this if it becomes a problem.
Matthew Wilcox184d2942011-05-11 21:36:38 -0400160 *
161 * May be called with local interrupts disabled and the q_lock held,
162 * or with interrupts enabled and no locks held.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500163 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400164static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx,
165 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500166{
Matthew Wilcoxe6d15f72011-02-24 08:49:41 -0500167 int depth = nvmeq->q_depth - 1;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500168 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500169 int cmdid;
170
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500171 do {
172 cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
173 if (cmdid >= depth)
174 return -EBUSY;
175 } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
176
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400177 info[cmdid].fn = handler;
178 info[cmdid].ctx = ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500179 info[cmdid].timeout = jiffies + timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700180 info[cmdid].aborted = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500181 return cmdid;
182}
183
184static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400185 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500186{
187 int cmdid;
188 wait_event_killable(nvmeq->sq_full,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500189 (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500190 return (cmdid < 0) ? -EINTR : cmdid;
191}
192
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400193/* Special values must be less than 0x1000 */
194#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500195#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
196#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
197#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500198#define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE)
Keith Buschc30341d2013-12-10 13:10:38 -0700199#define CMD_CTX_ABORT (0x31C + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500200
Keith Buschedd10d32014-04-03 16:45:23 -0600201static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400202 struct nvme_completion *cqe)
203{
204 if (ctx == CMD_CTX_CANCELLED)
205 return;
206 if (ctx == CMD_CTX_FLUSH)
207 return;
Keith Buschc30341d2013-12-10 13:10:38 -0700208 if (ctx == CMD_CTX_ABORT) {
Keith Buschedd10d32014-04-03 16:45:23 -0600209 ++nvmeq->dev->abort_limit;
Keith Buschc30341d2013-12-10 13:10:38 -0700210 return;
211 }
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400212 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600213 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400214 "completed id %d twice on queue %d\n",
215 cqe->command_id, le16_to_cpup(&cqe->sq_id));
216 return;
217 }
218 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600219 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400220 "invalid id %d completed on queue %d\n",
221 cqe->command_id, le16_to_cpup(&cqe->sq_id));
222 return;
223 }
224
Keith Buschedd10d32014-04-03 16:45:23 -0600225 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400226}
227
Keith Buschedd10d32014-04-03 16:45:23 -0600228static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700229 struct nvme_completion *cqe)
230{
231 struct async_cmd_info *cmdinfo = ctx;
232 cmdinfo->result = le32_to_cpup(&cqe->result);
233 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
234 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
235}
236
Matthew Wilcox184d2942011-05-11 21:36:38 -0400237/*
238 * Called with local interrupts disabled and the q_lock held. May not sleep.
239 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400240static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid,
241 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500242{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400243 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500244 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500245
Keith Busch94bbac42014-04-24 18:53:50 -0600246 if (cmdid >= nvmeq->q_depth || !info[cmdid].fn) {
247 if (fn)
248 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500249 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400250 }
Keith Busch859361a2012-08-02 14:05:59 -0600251 if (fn)
252 *fn = info[cmdid].fn;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400253 ctx = info[cmdid].ctx;
254 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500255 info[cmdid].ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500256 clear_bit(cmdid, nvmeq->cmdid_data);
257 wake_up(&nvmeq->sq_full);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400258 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500259}
260
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400261static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
262 nvme_completion_fn *fn)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500263{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400264 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500265 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400266 if (fn)
267 *fn = info[cmdid].fn;
268 ctx = info[cmdid].ctx;
269 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500270 info[cmdid].ctx = CMD_CTX_CANCELLED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400271 return ctx;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500272}
273
Keith Busch5a92e702014-02-21 14:13:44 -0700274static struct nvme_queue *raw_nvmeq(struct nvme_dev *dev, int qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500275{
Keith Busch5a92e702014-02-21 14:13:44 -0700276 return rcu_dereference_raw(dev->queues[qid]);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500277}
278
Keith Busch4f5099a2014-03-03 16:39:13 -0700279static struct nvme_queue *get_nvmeq(struct nvme_dev *dev) __acquires(RCU)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500280{
Keith Busch42f61422014-03-24 10:46:25 -0600281 unsigned queue_id = get_cpu_var(*dev->io_queue);
Keith Busch5a92e702014-02-21 14:13:44 -0700282 rcu_read_lock();
Keith Busch42f61422014-03-24 10:46:25 -0600283 return rcu_dereference(dev->queues[queue_id]);
Keith Busch5a92e702014-02-21 14:13:44 -0700284}
285
Keith Busch4f5099a2014-03-03 16:39:13 -0700286static void put_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500287{
Keith Busch5a92e702014-02-21 14:13:44 -0700288 rcu_read_unlock();
Keith Busch42f61422014-03-24 10:46:25 -0600289 put_cpu_var(nvmeq->dev->io_queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500290}
291
Keith Busch4f5099a2014-03-03 16:39:13 -0700292static struct nvme_queue *lock_nvmeq(struct nvme_dev *dev, int q_idx)
293 __acquires(RCU)
294{
295 rcu_read_lock();
296 return rcu_dereference(dev->queues[q_idx]);
297}
298
299static void unlock_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
300{
301 rcu_read_unlock();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500302}
303
304/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400305 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500306 * @nvmeq: The queue to use
307 * @cmd: The command to send
308 *
309 * Safe to use from interrupt context
310 */
311static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
312{
313 unsigned long flags;
314 u16 tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500315 spin_lock_irqsave(&nvmeq->q_lock, flags);
Keith Busch4f5099a2014-03-03 16:39:13 -0700316 if (nvmeq->q_suspended) {
317 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
318 return -EBUSY;
319 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500320 tail = nvmeq->sq_tail;
321 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500322 if (++tail == nvmeq->q_depth)
323 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500324 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500325 nvmeq->sq_tail = tail;
326 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
327
328 return 0;
329}
330
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500331static __le64 **iod_list(struct nvme_iod *iod)
332{
333 return ((void *)iod) + iod->offset;
334}
335
336/*
337 * Will slightly overestimate the number of pages needed. This is OK
338 * as it only leads to a small amount of wasted memory for the lifetime of
339 * the I/O.
340 */
341static int nvme_npages(unsigned size)
342{
343 unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE);
344 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
345}
346
347static struct nvme_iod *
348nvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp)
349{
350 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
351 sizeof(__le64 *) * nvme_npages(nbytes) +
352 sizeof(struct scatterlist) * nseg, gfp);
353
354 if (iod) {
355 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
356 iod->npages = -1;
357 iod->length = nbytes;
Keith Busch2b196032012-11-06 11:59:23 -0700358 iod->nents = 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600359 iod->first_dma = 0ULL;
Keith Busch61982212013-05-29 15:59:39 -0600360 iod->start_time = jiffies;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500361 }
362
363 return iod;
364}
365
Vishal Verma5d0f6132013-03-04 18:40:58 -0700366void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500367{
368 const int last_prp = PAGE_SIZE / 8 - 1;
369 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500370 __le64 **list = iod_list(iod);
371 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500372
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500373 if (iod->npages == 0)
374 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
375 for (i = 0; i < iod->npages; i++) {
376 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500377 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500378 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500379 prp_dma = next_prp_dma;
380 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500381 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500382}
383
Keith Busch61982212013-05-29 15:59:39 -0600384static void nvme_start_io_acct(struct bio *bio)
385{
386 struct gendisk *disk = bio->bi_bdev->bd_disk;
387 const int rw = bio_data_dir(bio);
388 int cpu = part_stat_lock();
389 part_round_stats(cpu, &disk->part0);
390 part_stat_inc(cpu, &disk->part0, ios[rw]);
391 part_stat_add(cpu, &disk->part0, sectors[rw], bio_sectors(bio));
392 part_inc_in_flight(&disk->part0, rw);
393 part_stat_unlock();
394}
395
396static void nvme_end_io_acct(struct bio *bio, unsigned long start_time)
397{
398 struct gendisk *disk = bio->bi_bdev->bd_disk;
399 const int rw = bio_data_dir(bio);
400 unsigned long duration = jiffies - start_time;
401 int cpu = part_stat_lock();
402 part_stat_add(cpu, &disk->part0, ticks[rw], duration);
403 part_round_stats(cpu, &disk->part0);
404 part_dec_in_flight(&disk->part0, rw);
405 part_stat_unlock();
406}
407
Keith Buschedd10d32014-04-03 16:45:23 -0600408static void bio_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500409 struct nvme_completion *cqe)
410{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500411 struct nvme_iod *iod = ctx;
412 struct bio *bio = iod->private;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500413 u16 status = le16_to_cpup(&cqe->status) >> 1;
414
Keith Buschedd10d32014-04-03 16:45:23 -0600415 if (unlikely(status)) {
416 if (!(status & NVME_SC_DNR ||
417 bio->bi_rw & REQ_FAILFAST_MASK) &&
418 (jiffies - iod->start_time) < IOD_TIMEOUT) {
419 if (!waitqueue_active(&nvmeq->sq_full))
420 add_wait_queue(&nvmeq->sq_full,
421 &nvmeq->sq_cong_wait);
422 list_add_tail(&iod->node, &nvmeq->iod_bio);
423 wake_up(&nvmeq->sq_full);
424 return;
425 }
426 }
Keith Busch9e59d092013-08-08 10:25:38 -0600427 if (iod->nents) {
Keith Buschedd10d32014-04-03 16:45:23 -0600428 dma_unmap_sg(nvmeq->q_dmadev, iod->sg, iod->nents,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500429 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busch9e59d092013-08-08 10:25:38 -0600430 nvme_end_io_acct(bio, iod->start_time);
431 }
Keith Buschedd10d32014-04-03 16:45:23 -0600432 nvme_free_iod(nvmeq->dev, iod);
Keith Busch427e9702013-04-09 11:59:32 -0600433 if (status)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500434 bio_endio(bio, -EIO);
Keith Busch427e9702013-04-09 11:59:32 -0600435 else
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500436 bio_endio(bio, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500437}
438
Matthew Wilcox184d2942011-05-11 21:36:38 -0400439/* length is in bytes. gfp flags indicates whether we may sleep. */
Keith Buschedd10d32014-04-03 16:45:23 -0600440int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod, int total_len,
441 gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500442{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500443 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500444 int length = total_len;
445 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500446 int dma_len = sg_dma_len(sg);
447 u64 dma_addr = sg_dma_address(sg);
448 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500449 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500450 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500451 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500452 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500453
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500454 length -= (PAGE_SIZE - offset);
455 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500456 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500457
458 dma_len -= (PAGE_SIZE - offset);
459 if (dma_len) {
460 dma_addr += (PAGE_SIZE - offset);
461 } else {
462 sg = sg_next(sg);
463 dma_addr = sg_dma_address(sg);
464 dma_len = sg_dma_len(sg);
465 }
466
467 if (length <= PAGE_SIZE) {
Keith Buschedd10d32014-04-03 16:45:23 -0600468 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500469 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500470 }
471
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500472 nprps = DIV_ROUND_UP(length, PAGE_SIZE);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500473 if (nprps <= (256 / 8)) {
474 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500475 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500476 } else {
477 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500478 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500479 }
480
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400481 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
482 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600483 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500484 iod->npages = -1;
485 return (total_len - length) + PAGE_SIZE;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400486 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500487 list[0] = prp_list;
488 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500489 i = 0;
490 for (;;) {
Matthew Wilcox7523d832011-03-16 16:43:40 -0400491 if (i == PAGE_SIZE / 8) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500492 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400493 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500494 if (!prp_list)
495 return total_len - length;
496 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400497 prp_list[0] = old_prp_list[i - 1];
498 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
499 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500500 }
501 prp_list[i++] = cpu_to_le64(dma_addr);
502 dma_len -= PAGE_SIZE;
503 dma_addr += PAGE_SIZE;
504 length -= PAGE_SIZE;
505 if (length <= 0)
506 break;
507 if (dma_len > 0)
508 continue;
509 BUG_ON(dma_len < 0);
510 sg = sg_next(sg);
511 dma_addr = sg_dma_address(sg);
512 dma_len = sg_dma_len(sg);
513 }
514
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500515 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500516}
517
Keith Busch427e9702013-04-09 11:59:32 -0600518static int nvme_split_and_submit(struct bio *bio, struct nvme_queue *nvmeq,
Kent Overstreet20d01892013-11-23 18:21:01 -0800519 int len)
Keith Busch427e9702013-04-09 11:59:32 -0600520{
Kent Overstreet20d01892013-11-23 18:21:01 -0800521 struct bio *split = bio_split(bio, len >> 9, GFP_ATOMIC, NULL);
522 if (!split)
Keith Busch427e9702013-04-09 11:59:32 -0600523 return -ENOMEM;
524
Kent Overstreet20d01892013-11-23 18:21:01 -0800525 bio_chain(split, bio);
526
Keith Buschedd10d32014-04-03 16:45:23 -0600527 if (!waitqueue_active(&nvmeq->sq_full))
Keith Busch427e9702013-04-09 11:59:32 -0600528 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
Kent Overstreet20d01892013-11-23 18:21:01 -0800529 bio_list_add(&nvmeq->sq_cong, split);
530 bio_list_add(&nvmeq->sq_cong, bio);
Keith Buschedd10d32014-04-03 16:45:23 -0600531 wake_up(&nvmeq->sq_full);
Keith Busch427e9702013-04-09 11:59:32 -0600532
533 return 0;
534}
535
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500536/* NVMe scatterlists require no holes in the virtual address */
537#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
538 (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
539
Keith Busch427e9702013-04-09 11:59:32 -0600540static int nvme_map_bio(struct nvme_queue *nvmeq, struct nvme_iod *iod,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500541 struct bio *bio, enum dma_data_direction dma_dir, int psegs)
542{
Kent Overstreet79886132013-11-23 17:19:00 -0800543 struct bio_vec bvec, bvprv;
544 struct bvec_iter iter;
Matthew Wilcox76830842011-02-10 13:55:39 -0500545 struct scatterlist *sg = NULL;
Kent Overstreet79886132013-11-23 17:19:00 -0800546 int length = 0, nsegs = 0, split_len = bio->bi_iter.bi_size;
547 int first = 1;
Keith Busch159b67d2013-04-09 17:13:20 -0600548
549 if (nvmeq->dev->stripe_size)
550 split_len = nvmeq->dev->stripe_size -
Kent Overstreet4f024f32013-10-11 15:44:27 -0700551 ((bio->bi_iter.bi_sector << 9) &
552 (nvmeq->dev->stripe_size - 1));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500553
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500554 sg_init_table(iod->sg, psegs);
Kent Overstreet79886132013-11-23 17:19:00 -0800555 bio_for_each_segment(bvec, bio, iter) {
556 if (!first && BIOVEC_PHYS_MERGEABLE(&bvprv, &bvec)) {
557 sg->length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500558 } else {
Kent Overstreet79886132013-11-23 17:19:00 -0800559 if (!first && BIOVEC_NOT_VIRT_MERGEABLE(&bvprv, &bvec))
560 return nvme_split_and_submit(bio, nvmeq,
Kent Overstreet20d01892013-11-23 18:21:01 -0800561 length);
Keith Busch427e9702013-04-09 11:59:32 -0600562
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500563 sg = sg ? sg + 1 : iod->sg;
Kent Overstreet79886132013-11-23 17:19:00 -0800564 sg_set_page(sg, bvec.bv_page,
565 bvec.bv_len, bvec.bv_offset);
Matthew Wilcox76830842011-02-10 13:55:39 -0500566 nsegs++;
567 }
Keith Busch159b67d2013-04-09 17:13:20 -0600568
Kent Overstreet79886132013-11-23 17:19:00 -0800569 if (split_len - length < bvec.bv_len)
Kent Overstreet20d01892013-11-23 18:21:01 -0800570 return nvme_split_and_submit(bio, nvmeq, split_len);
Kent Overstreet79886132013-11-23 17:19:00 -0800571 length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500572 bvprv = bvec;
Kent Overstreet79886132013-11-23 17:19:00 -0800573 first = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500574 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500575 iod->nents = nsegs;
Matthew Wilcox76830842011-02-10 13:55:39 -0500576 sg_mark_end(sg);
Keith Busch427e9702013-04-09 11:59:32 -0600577 if (dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir) == 0)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500578 return -ENOMEM;
Keith Busch427e9702013-04-09 11:59:32 -0600579
Kent Overstreet4f024f32013-10-11 15:44:27 -0700580 BUG_ON(length != bio->bi_iter.bi_size);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500581 return length;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500582}
583
Keith Busch0e5e4f02012-11-09 16:33:05 -0700584static int nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
585 struct bio *bio, struct nvme_iod *iod, int cmdid)
586{
Keith Buschedd10d32014-04-03 16:45:23 -0600587 struct nvme_dsm_range *range =
588 (struct nvme_dsm_range *)iod_list(iod)[0];
Keith Busch0e5e4f02012-11-09 16:33:05 -0700589 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
590
Keith Busch0e5e4f02012-11-09 16:33:05 -0700591 range->cattr = cpu_to_le32(0);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700592 range->nlb = cpu_to_le32(bio->bi_iter.bi_size >> ns->lba_shift);
593 range->slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700594
595 memset(cmnd, 0, sizeof(*cmnd));
596 cmnd->dsm.opcode = nvme_cmd_dsm;
597 cmnd->dsm.command_id = cmdid;
598 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
599 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
600 cmnd->dsm.nr = 0;
601 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
602
603 if (++nvmeq->sq_tail == nvmeq->q_depth)
604 nvmeq->sq_tail = 0;
605 writel(nvmeq->sq_tail, nvmeq->q_db);
606
607 return 0;
608}
609
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500610static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
611 int cmdid)
612{
613 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
614
615 memset(cmnd, 0, sizeof(*cmnd));
616 cmnd->common.opcode = nvme_cmd_flush;
617 cmnd->common.command_id = cmdid;
618 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
619
620 if (++nvmeq->sq_tail == nvmeq->q_depth)
621 nvmeq->sq_tail = 0;
622 writel(nvmeq->sq_tail, nvmeq->q_db);
623
624 return 0;
625}
626
Vishal Verma5d0f6132013-03-04 18:40:58 -0700627int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500628{
629 int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH,
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500630 special_completion, NVME_IO_TIMEOUT);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500631 if (unlikely(cmdid < 0))
632 return cmdid;
633
634 return nvme_submit_flush(nvmeq, ns, cmdid);
635}
636
Keith Buschedd10d32014-04-03 16:45:23 -0600637static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500638{
Keith Buschedd10d32014-04-03 16:45:23 -0600639 struct bio *bio = iod->private;
640 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500641 struct nvme_command *cmnd;
Keith Buschedd10d32014-04-03 16:45:23 -0600642 int cmdid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500643 u16 control;
644 u32 dsmgmt;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500645
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500646 cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500647 if (unlikely(cmdid < 0))
Keith Buschedd10d32014-04-03 16:45:23 -0600648 return cmdid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500649
Keith Buschedd10d32014-04-03 16:45:23 -0600650 if (bio->bi_rw & REQ_DISCARD)
651 return nvme_submit_discard(nvmeq, ns, bio, iod, cmdid);
652 if ((bio->bi_rw & REQ_FLUSH) && !iod->nents)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500653 return nvme_submit_flush(nvmeq, ns, cmdid);
654
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500655 control = 0;
656 if (bio->bi_rw & REQ_FUA)
657 control |= NVME_RW_FUA;
658 if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
659 control |= NVME_RW_LR;
660
661 dsmgmt = 0;
662 if (bio->bi_rw & REQ_RAHEAD)
663 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
664
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500665 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500666 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500667
Keith Buschedd10d32014-04-03 16:45:23 -0600668 cmnd->rw.opcode = bio_data_dir(bio) ? nvme_cmd_write : nvme_cmd_read;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500669 cmnd->rw.command_id = cmdid;
670 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Keith Buschedd10d32014-04-03 16:45:23 -0600671 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
672 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700673 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Keith Buschedd10d32014-04-03 16:45:23 -0600674 cmnd->rw.length =
675 cpu_to_le16((bio->bi_iter.bi_size >> ns->lba_shift) - 1);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500676 cmnd->rw.control = cpu_to_le16(control);
677 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500678
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500679 if (++nvmeq->sq_tail == nvmeq->q_depth)
680 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500681 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500682
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500683 return 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600684}
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500685
Keith Buschedd10d32014-04-03 16:45:23 -0600686/*
687 * Called with local interrupts disabled and the q_lock held. May not sleep.
688 */
689static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
690 struct bio *bio)
691{
692 struct nvme_iod *iod;
693 int psegs = bio_phys_segments(ns->queue, bio);
694 int result;
695
696 if ((bio->bi_rw & REQ_FLUSH) && psegs) {
697 result = nvme_submit_flush_data(nvmeq, ns);
698 if (result)
699 return result;
700 }
701
702 iod = nvme_alloc_iod(psegs, bio->bi_iter.bi_size, GFP_ATOMIC);
703 if (!iod)
704 return -ENOMEM;
705
706 iod->private = bio;
707 if (bio->bi_rw & REQ_DISCARD) {
708 void *range;
709 /*
710 * We reuse the small pool to allocate the 16-byte range here
711 * as it is not worth having a special pool for these or
712 * additional cases to handle freeing the iod.
713 */
714 range = dma_pool_alloc(nvmeq->dev->prp_small_pool,
715 GFP_ATOMIC,
716 &iod->first_dma);
717 if (!range) {
718 result = -ENOMEM;
719 goto free_iod;
720 }
721 iod_list(iod)[0] = (__le64 *)range;
722 iod->npages = 0;
723 } else if (psegs) {
724 result = nvme_map_bio(nvmeq, iod, bio,
725 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE,
726 psegs);
727 if (result <= 0)
728 goto free_iod;
729 if (nvme_setup_prps(nvmeq->dev, iod, result, GFP_ATOMIC) !=
730 result) {
731 result = -ENOMEM;
732 goto free_iod;
733 }
734 nvme_start_io_acct(bio);
735 }
736 if (unlikely(nvme_submit_iod(nvmeq, iod))) {
737 if (!waitqueue_active(&nvmeq->sq_full))
738 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
739 list_add_tail(&iod->node, &nvmeq->iod_bio);
740 }
741 return 0;
742
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500743 free_iod:
744 nvme_free_iod(nvmeq->dev, iod);
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500745 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500746}
747
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400748static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500749{
Matthew Wilcox82123462011-01-20 13:24:06 -0500750 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500751
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500752 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500753 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500754
755 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400756 void *ctx;
757 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500758 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500759 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500760 break;
761 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
762 if (++head == nvmeq->q_depth) {
763 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500764 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500765 }
766
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400767 ctx = free_cmdid(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600768 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500769 }
770
771 /* If the controller ignores the cq head doorbell and continuously
772 * writes to the queue, it is theoretically possible to wrap around
773 * the queue twice and mistakenly return IRQ_NONE. Linux only
774 * requires that 0.1% of your interrupts are handled, so this isn't
775 * a big problem.
776 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500777 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400778 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500779
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800780 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500781 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500782 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500783
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400784 nvmeq->cqe_seen = 1;
785 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500786}
787
Matthew Wilcox7d822452013-06-24 12:03:57 -0400788static void nvme_make_request(struct request_queue *q, struct bio *bio)
789{
790 struct nvme_ns *ns = q->queuedata;
791 struct nvme_queue *nvmeq = get_nvmeq(ns->dev);
792 int result = -EBUSY;
793
Keith Buschcd638942013-07-15 15:02:23 -0600794 if (!nvmeq) {
795 put_nvmeq(NULL);
796 bio_endio(bio, -EIO);
797 return;
798 }
799
Matthew Wilcox7d822452013-06-24 12:03:57 -0400800 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -0600801 if (!nvmeq->q_suspended && bio_list_empty(&nvmeq->sq_cong))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400802 result = nvme_submit_bio_queue(nvmeq, ns, bio);
803 if (unlikely(result)) {
Keith Buschedd10d32014-04-03 16:45:23 -0600804 if (!waitqueue_active(&nvmeq->sq_full))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400805 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
806 bio_list_add(&nvmeq->sq_cong, bio);
807 }
808
809 nvme_process_cq(nvmeq);
810 spin_unlock_irq(&nvmeq->q_lock);
811 put_nvmeq(nvmeq);
812}
813
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500814static irqreturn_t nvme_irq(int irq, void *data)
815{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500816 irqreturn_t result;
817 struct nvme_queue *nvmeq = data;
818 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400819 nvme_process_cq(nvmeq);
820 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
821 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500822 spin_unlock(&nvmeq->q_lock);
823 return result;
824}
825
826static irqreturn_t nvme_irq_check(int irq, void *data)
827{
828 struct nvme_queue *nvmeq = data;
829 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
830 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
831 return IRQ_NONE;
832 return IRQ_WAKE_THREAD;
833}
834
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500835static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
836{
837 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400838 cancel_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500839 spin_unlock_irq(&nvmeq->q_lock);
840}
841
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400842struct sync_cmd_info {
843 struct task_struct *task;
844 u32 result;
845 int status;
846};
847
Keith Buschedd10d32014-04-03 16:45:23 -0600848static void sync_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400849 struct nvme_completion *cqe)
850{
851 struct sync_cmd_info *cmdinfo = ctx;
852 cmdinfo->result = le32_to_cpup(&cqe->result);
853 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
854 wake_up_process(cmdinfo->task);
855}
856
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500857/*
858 * Returns 0 on success. If the result is negative, it's a Linux error code;
859 * if the result is positive, it's an NVM Express status code
860 */
Keith Busch4f5099a2014-03-03 16:39:13 -0700861static int nvme_submit_sync_cmd(struct nvme_dev *dev, int q_idx,
862 struct nvme_command *cmd,
Vishal Verma5d0f6132013-03-04 18:40:58 -0700863 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500864{
Keith Busch4f5099a2014-03-03 16:39:13 -0700865 int cmdid, ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500866 struct sync_cmd_info cmdinfo;
Keith Busch4f5099a2014-03-03 16:39:13 -0700867 struct nvme_queue *nvmeq;
868
869 nvmeq = lock_nvmeq(dev, q_idx);
870 if (!nvmeq) {
871 unlock_nvmeq(nvmeq);
872 return -ENODEV;
873 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500874
875 cmdinfo.task = current;
876 cmdinfo.status = -EINTR;
877
Keith Busch4f5099a2014-03-03 16:39:13 -0700878 cmdid = alloc_cmdid(nvmeq, &cmdinfo, sync_completion, timeout);
879 if (cmdid < 0) {
880 unlock_nvmeq(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500881 return cmdid;
Keith Busch4f5099a2014-03-03 16:39:13 -0700882 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500883 cmd->common.command_id = cmdid;
884
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500885 set_current_state(TASK_KILLABLE);
Keith Busch4f5099a2014-03-03 16:39:13 -0700886 ret = nvme_submit_cmd(nvmeq, cmd);
887 if (ret) {
888 free_cmdid(nvmeq, cmdid, NULL);
889 unlock_nvmeq(nvmeq);
890 set_current_state(TASK_RUNNING);
891 return ret;
892 }
893 unlock_nvmeq(nvmeq);
Keith Busch78f8d252013-04-19 14:11:06 -0600894 schedule_timeout(timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500895
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500896 if (cmdinfo.status == -EINTR) {
Keith Busch4f5099a2014-03-03 16:39:13 -0700897 nvmeq = lock_nvmeq(dev, q_idx);
898 if (nvmeq)
899 nvme_abort_command(nvmeq, cmdid);
900 unlock_nvmeq(nvmeq);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500901 return -EINTR;
902 }
903
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500904 if (result)
905 *result = cmdinfo.result;
906
907 return cmdinfo.status;
908}
909
Keith Busch4d115422013-12-10 13:10:40 -0700910static int nvme_submit_async_cmd(struct nvme_queue *nvmeq,
911 struct nvme_command *cmd,
912 struct async_cmd_info *cmdinfo, unsigned timeout)
913{
914 int cmdid;
915
916 cmdid = alloc_cmdid_killable(nvmeq, cmdinfo, async_completion, timeout);
917 if (cmdid < 0)
918 return cmdid;
919 cmdinfo->status = -EINTR;
920 cmd->common.command_id = cmdid;
Keith Busch4f5099a2014-03-03 16:39:13 -0700921 return nvme_submit_cmd(nvmeq, cmd);
Keith Busch4d115422013-12-10 13:10:40 -0700922}
923
Vishal Verma5d0f6132013-03-04 18:40:58 -0700924int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500925 u32 *result)
926{
Keith Busch4f5099a2014-03-03 16:39:13 -0700927 return nvme_submit_sync_cmd(dev, 0, cmd, result, ADMIN_TIMEOUT);
928}
929
930int nvme_submit_io_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
931 u32 *result)
932{
933 return nvme_submit_sync_cmd(dev, smp_processor_id() + 1, cmd, result,
934 NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500935}
936
Keith Busch4d115422013-12-10 13:10:40 -0700937static int nvme_submit_admin_cmd_async(struct nvme_dev *dev,
938 struct nvme_command *cmd, struct async_cmd_info *cmdinfo)
939{
Keith Busch5a92e702014-02-21 14:13:44 -0700940 return nvme_submit_async_cmd(raw_nvmeq(dev, 0), cmd, cmdinfo,
Keith Busch4d115422013-12-10 13:10:40 -0700941 ADMIN_TIMEOUT);
942}
943
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500944static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
945{
946 int status;
947 struct nvme_command c;
948
949 memset(&c, 0, sizeof(c));
950 c.delete_queue.opcode = opcode;
951 c.delete_queue.qid = cpu_to_le16(id);
952
953 status = nvme_submit_admin_cmd(dev, &c, NULL);
954 if (status)
955 return -EIO;
956 return 0;
957}
958
959static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
960 struct nvme_queue *nvmeq)
961{
962 int status;
963 struct nvme_command c;
964 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
965
966 memset(&c, 0, sizeof(c));
967 c.create_cq.opcode = nvme_admin_create_cq;
968 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
969 c.create_cq.cqid = cpu_to_le16(qid);
970 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
971 c.create_cq.cq_flags = cpu_to_le16(flags);
972 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
973
974 status = nvme_submit_admin_cmd(dev, &c, NULL);
975 if (status)
976 return -EIO;
977 return 0;
978}
979
980static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
981 struct nvme_queue *nvmeq)
982{
983 int status;
984 struct nvme_command c;
985 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
986
987 memset(&c, 0, sizeof(c));
988 c.create_sq.opcode = nvme_admin_create_sq;
989 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
990 c.create_sq.sqid = cpu_to_le16(qid);
991 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
992 c.create_sq.sq_flags = cpu_to_le16(flags);
993 c.create_sq.cqid = cpu_to_le16(qid);
994
995 status = nvme_submit_admin_cmd(dev, &c, NULL);
996 if (status)
997 return -EIO;
998 return 0;
999}
1000
1001static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1002{
1003 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1004}
1005
1006static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1007{
1008 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1009}
1010
Vishal Verma5d0f6132013-03-04 18:40:58 -07001011int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001012 dma_addr_t dma_addr)
1013{
1014 struct nvme_command c;
1015
1016 memset(&c, 0, sizeof(c));
1017 c.identify.opcode = nvme_admin_identify;
1018 c.identify.nsid = cpu_to_le32(nsid);
1019 c.identify.prp1 = cpu_to_le64(dma_addr);
1020 c.identify.cns = cpu_to_le32(cns);
1021
1022 return nvme_submit_admin_cmd(dev, &c, NULL);
1023}
1024
Vishal Verma5d0f6132013-03-04 18:40:58 -07001025int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -06001026 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001027{
1028 struct nvme_command c;
1029
1030 memset(&c, 0, sizeof(c));
1031 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -06001032 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001033 c.features.prp1 = cpu_to_le64(dma_addr);
1034 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001035
Keith Busch08df1e02012-09-21 10:52:13 -06001036 return nvme_submit_admin_cmd(dev, &c, result);
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001037}
1038
Vishal Verma5d0f6132013-03-04 18:40:58 -07001039int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
1040 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001041{
1042 struct nvme_command c;
1043
1044 memset(&c, 0, sizeof(c));
1045 c.features.opcode = nvme_admin_set_features;
1046 c.features.prp1 = cpu_to_le64(dma_addr);
1047 c.features.fid = cpu_to_le32(fid);
1048 c.features.dword11 = cpu_to_le32(dword11);
1049
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001050 return nvme_submit_admin_cmd(dev, &c, result);
1051}
1052
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001053/**
Keith Buschc30341d2013-12-10 13:10:38 -07001054 * nvme_abort_cmd - Attempt aborting a command
1055 * @cmdid: Command id of a timed out IO
1056 * @queue: The queue with timed out IO
1057 *
1058 * Schedule controller reset if the command was already aborted once before and
1059 * still hasn't been returned to the driver, or if this is the admin queue.
1060 */
1061static void nvme_abort_cmd(int cmdid, struct nvme_queue *nvmeq)
1062{
1063 int a_cmdid;
1064 struct nvme_command cmd;
1065 struct nvme_dev *dev = nvmeq->dev;
1066 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Keith Busch5a92e702014-02-21 14:13:44 -07001067 struct nvme_queue *adminq;
Keith Buschc30341d2013-12-10 13:10:38 -07001068
1069 if (!nvmeq->qid || info[cmdid].aborted) {
1070 if (work_busy(&dev->reset_work))
1071 return;
1072 list_del_init(&dev->node);
1073 dev_warn(&dev->pci_dev->dev,
1074 "I/O %d QID %d timeout, reset controller\n", cmdid,
1075 nvmeq->qid);
Tejun Heo9ca97372014-03-07 10:24:49 -05001076 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschc30341d2013-12-10 13:10:38 -07001077 queue_work(nvme_workq, &dev->reset_work);
1078 return;
1079 }
1080
1081 if (!dev->abort_limit)
1082 return;
1083
Keith Busch5a92e702014-02-21 14:13:44 -07001084 adminq = rcu_dereference(dev->queues[0]);
1085 a_cmdid = alloc_cmdid(adminq, CMD_CTX_ABORT, special_completion,
Keith Buschc30341d2013-12-10 13:10:38 -07001086 ADMIN_TIMEOUT);
1087 if (a_cmdid < 0)
1088 return;
1089
1090 memset(&cmd, 0, sizeof(cmd));
1091 cmd.abort.opcode = nvme_admin_abort_cmd;
1092 cmd.abort.cid = cmdid;
1093 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
1094 cmd.abort.command_id = a_cmdid;
1095
1096 --dev->abort_limit;
1097 info[cmdid].aborted = 1;
1098 info[cmdid].timeout = jiffies + ADMIN_TIMEOUT;
1099
1100 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", cmdid,
1101 nvmeq->qid);
Keith Busch5a92e702014-02-21 14:13:44 -07001102 nvme_submit_cmd(adminq, &cmd);
Keith Buschc30341d2013-12-10 13:10:38 -07001103}
1104
1105/**
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001106 * nvme_cancel_ios - Cancel outstanding I/Os
1107 * @queue: The queue to cancel I/Os on
1108 * @timeout: True to only cancel I/Os which have timed out
1109 */
1110static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout)
1111{
1112 int depth = nvmeq->q_depth - 1;
1113 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
1114 unsigned long now = jiffies;
1115 int cmdid;
1116
1117 for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
1118 void *ctx;
1119 nvme_completion_fn fn;
1120 static struct nvme_completion cqe = {
Matthew Wilcoxaf2d9ca2013-04-16 15:18:30 -04001121 .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1),
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001122 };
1123
1124 if (timeout && !time_after(now, info[cmdid].timeout))
1125 continue;
Keith Busch053ab7022013-04-30 11:19:38 -06001126 if (info[cmdid].ctx == CMD_CTX_CANCELLED)
1127 continue;
Keith Buschc30341d2013-12-10 13:10:38 -07001128 if (timeout && nvmeq->dev->initialized) {
1129 nvme_abort_cmd(cmdid, nvmeq);
1130 continue;
1131 }
1132 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n", cmdid,
1133 nvmeq->qid);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001134 ctx = cancel_cmdid(nvmeq, cmdid, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -06001135 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001136 }
1137}
1138
Keith Busch5a92e702014-02-21 14:13:44 -07001139static void nvme_free_queue(struct rcu_head *r)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001140{
Keith Busch5a92e702014-02-21 14:13:44 -07001141 struct nvme_queue *nvmeq = container_of(r, struct nvme_queue, r_head);
1142
Keith Busch22404272013-07-15 15:02:20 -06001143 spin_lock_irq(&nvmeq->q_lock);
1144 while (bio_list_peek(&nvmeq->sq_cong)) {
1145 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1146 bio_endio(bio, -EIO);
1147 }
Keith Buschedd10d32014-04-03 16:45:23 -06001148 while (!list_empty(&nvmeq->iod_bio)) {
1149 static struct nvme_completion cqe = {
1150 .status = cpu_to_le16(
1151 (NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1),
1152 };
1153 struct nvme_iod *iod = list_first_entry(&nvmeq->iod_bio,
1154 struct nvme_iod,
1155 node);
1156 list_del(&iod->node);
1157 bio_completion(nvmeq, iod, &cqe);
1158 }
Keith Busch22404272013-07-15 15:02:20 -06001159 spin_unlock_irq(&nvmeq->q_lock);
1160
Matthew Wilcox9e866772012-08-03 13:55:56 -04001161 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1162 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1163 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1164 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
Keith Busch42f61422014-03-24 10:46:25 -06001165 if (nvmeq->qid)
1166 free_cpumask_var(nvmeq->cpu_mask);
Matthew Wilcox9e866772012-08-03 13:55:56 -04001167 kfree(nvmeq);
1168}
1169
Keith Buscha1a5ef92013-12-16 13:50:00 -05001170static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001171{
1172 int i;
1173
Keith Buscha1a5ef92013-12-16 13:50:00 -05001174 for (i = dev->queue_count - 1; i >= lowest; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07001175 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
1176 rcu_assign_pointer(dev->queues[i], NULL);
1177 call_rcu(&nvmeq->r_head, nvme_free_queue);
Keith Busch22404272013-07-15 15:02:20 -06001178 dev->queue_count--;
Keith Busch22404272013-07-15 15:02:20 -06001179 }
1180}
1181
Keith Busch4d115422013-12-10 13:10:40 -07001182/**
1183 * nvme_suspend_queue - put queue into suspended state
1184 * @nvmeq - queue to suspend
1185 *
1186 * Returns 1 if already suspended, 0 otherwise.
1187 */
1188static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001189{
Keith Busch4d115422013-12-10 13:10:40 -07001190 int vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001191
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001192 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001193 if (nvmeq->q_suspended) {
1194 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07001195 return 1;
Keith Busch32958742012-08-20 14:57:49 -06001196 }
Keith Busch22404272013-07-15 15:02:20 -06001197 nvmeq->q_suspended = 1;
Keith Busch42f61422014-03-24 10:46:25 -06001198 nvmeq->dev->online_queues--;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001199 spin_unlock_irq(&nvmeq->q_lock);
1200
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001201 irq_set_affinity_hint(vector, NULL);
1202 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001203
Keith Busch4d115422013-12-10 13:10:40 -07001204 return 0;
1205}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001206
Keith Busch4d115422013-12-10 13:10:40 -07001207static void nvme_clear_queue(struct nvme_queue *nvmeq)
1208{
Keith Busch22404272013-07-15 15:02:20 -06001209 spin_lock_irq(&nvmeq->q_lock);
1210 nvme_process_cq(nvmeq);
1211 nvme_cancel_ios(nvmeq, false);
1212 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001213}
1214
Keith Busch4d115422013-12-10 13:10:40 -07001215static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1216{
Keith Busch5a92e702014-02-21 14:13:44 -07001217 struct nvme_queue *nvmeq = raw_nvmeq(dev, qid);
Keith Busch4d115422013-12-10 13:10:40 -07001218
1219 if (!nvmeq)
1220 return;
1221 if (nvme_suspend_queue(nvmeq))
1222 return;
1223
Keith Busch0e53d182013-12-10 13:10:39 -07001224 /* Don't tell the adapter to delete the admin queue.
1225 * Don't tell a removed adapter to delete IO queues. */
1226 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001227 adapter_delete_sq(dev, qid);
1228 adapter_delete_cq(dev, qid);
1229 }
Keith Busch4d115422013-12-10 13:10:40 -07001230 nvme_clear_queue(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001231}
1232
1233static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
1234 int depth, int vector)
1235{
1236 struct device *dmadev = &dev->pci_dev->dev;
Keith Busch22404272013-07-15 15:02:20 -06001237 unsigned extra = nvme_queue_extra(depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001238 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
1239 if (!nvmeq)
1240 return NULL;
1241
1242 nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
1243 &nvmeq->cq_dma_addr, GFP_KERNEL);
1244 if (!nvmeq->cqes)
1245 goto free_nvmeq;
1246 memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
1247
1248 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
1249 &nvmeq->sq_dma_addr, GFP_KERNEL);
1250 if (!nvmeq->sq_cmds)
1251 goto free_cqdma;
1252
Keith Busch42f61422014-03-24 10:46:25 -06001253 if (qid && !zalloc_cpumask_var(&nvmeq->cpu_mask, GFP_KERNEL))
1254 goto free_sqdma;
1255
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001256 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001257 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001258 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1259 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001260 spin_lock_init(&nvmeq->q_lock);
1261 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001262 nvmeq->cq_phase = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001263 init_waitqueue_head(&nvmeq->sq_full);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001264 init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001265 bio_list_init(&nvmeq->sq_cong);
Keith Buschedd10d32014-04-03 16:45:23 -06001266 INIT_LIST_HEAD(&nvmeq->iod_bio);
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001267 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001268 nvmeq->q_depth = depth;
1269 nvmeq->cq_vector = vector;
Keith Buschc30341d2013-12-10 13:10:38 -07001270 nvmeq->qid = qid;
Keith Busch22404272013-07-15 15:02:20 -06001271 nvmeq->q_suspended = 1;
1272 dev->queue_count++;
Keith Busch5a92e702014-02-21 14:13:44 -07001273 rcu_assign_pointer(dev->queues[qid], nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001274
1275 return nvmeq;
1276
Keith Busch42f61422014-03-24 10:46:25 -06001277 free_sqdma:
1278 dma_free_coherent(dmadev, SQ_SIZE(depth), (void *)nvmeq->sq_cmds,
1279 nvmeq->sq_dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001280 free_cqdma:
Keith Busch68b8eca2013-05-01 13:07:47 -06001281 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001282 nvmeq->cq_dma_addr);
1283 free_nvmeq:
1284 kfree(nvmeq);
1285 return NULL;
1286}
1287
Matthew Wilcox30010822011-01-20 09:10:15 -05001288static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1289 const char *name)
1290{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001291 if (use_threaded_interrupts)
1292 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001293 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001294 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001295 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001296 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001297}
1298
Keith Busch22404272013-07-15 15:02:20 -06001299static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001300{
Keith Busch22404272013-07-15 15:02:20 -06001301 struct nvme_dev *dev = nvmeq->dev;
1302 unsigned extra = nvme_queue_extra(nvmeq->q_depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001303
Keith Busch22404272013-07-15 15:02:20 -06001304 nvmeq->sq_tail = 0;
1305 nvmeq->cq_head = 0;
1306 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001307 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001308 memset(nvmeq->cmdid_data, 0, extra);
1309 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
1310 nvme_cancel_ios(nvmeq, false);
1311 nvmeq->q_suspended = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001312 dev->online_queues++;
Keith Busch22404272013-07-15 15:02:20 -06001313}
1314
1315static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1316{
1317 struct nvme_dev *dev = nvmeq->dev;
1318 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001319
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001320 result = adapter_alloc_cq(dev, qid, nvmeq);
1321 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001322 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001323
1324 result = adapter_alloc_sq(dev, qid, nvmeq);
1325 if (result < 0)
1326 goto release_cq;
1327
Matthew Wilcox3193f072014-01-27 15:57:22 -05001328 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001329 if (result < 0)
1330 goto release_sq;
1331
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001332 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001333 nvme_init_queue(nvmeq, qid);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001334 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001335
1336 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001337
1338 release_sq:
1339 adapter_delete_sq(dev, qid);
1340 release_cq:
1341 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001342 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001343}
1344
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001345static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1346{
1347 unsigned long timeout;
1348 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1349
1350 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1351
1352 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1353 msleep(100);
1354 if (fatal_signal_pending(current))
1355 return -EINTR;
1356 if (time_after(jiffies, timeout)) {
1357 dev_err(&dev->pci_dev->dev,
Matthew Wilcox27e81662014-04-11 11:58:45 -04001358 "Device not ready; aborting %s\n", enabled ?
1359 "initialisation" : "reset");
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001360 return -ENODEV;
1361 }
1362 }
1363
1364 return 0;
1365}
1366
1367/*
1368 * If the device has been passed off to us in an enabled state, just clear
1369 * the enabled bit. The spec says we should set the 'shutdown notification
1370 * bits', but doing so may cause the device to complete commands to the
1371 * admin queue ... and we don't know what memory that might be pointing at!
1372 */
1373static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1374{
Matthew Wilcox44af1462013-05-04 06:43:17 -04001375 u32 cc = readl(&dev->bar->cc);
1376
1377 if (cc & NVME_CC_ENABLE)
1378 writel(cc & ~NVME_CC_ENABLE, &dev->bar->cc);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001379 return nvme_wait_ready(dev, cap, false);
1380}
1381
1382static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1383{
1384 return nvme_wait_ready(dev, cap, true);
1385}
1386
Keith Busch1894d8f2013-07-15 15:02:22 -06001387static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1388{
1389 unsigned long timeout;
1390 u32 cc;
1391
1392 cc = (readl(&dev->bar->cc) & ~NVME_CC_SHN_MASK) | NVME_CC_SHN_NORMAL;
1393 writel(cc, &dev->bar->cc);
1394
1395 timeout = 2 * HZ + jiffies;
1396 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1397 NVME_CSTS_SHST_CMPLT) {
1398 msleep(100);
1399 if (fatal_signal_pending(current))
1400 return -EINTR;
1401 if (time_after(jiffies, timeout)) {
1402 dev_err(&dev->pci_dev->dev,
1403 "Device shutdown incomplete; abort shutdown\n");
1404 return -ENODEV;
1405 }
1406 }
1407
1408 return 0;
1409}
1410
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001411static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001412{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001413 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001414 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001415 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001416 struct nvme_queue *nvmeq;
1417
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001418 result = nvme_disable_ctrl(dev, cap);
1419 if (result < 0)
1420 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001421
Keith Busch5a92e702014-02-21 14:13:44 -07001422 nvmeq = raw_nvmeq(dev, 0);
Keith Buschcd638942013-07-15 15:02:23 -06001423 if (!nvmeq) {
1424 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
1425 if (!nvmeq)
1426 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001427 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001428
1429 aqa = nvmeq->q_depth - 1;
1430 aqa |= aqa << 16;
1431
1432 dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
1433 dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
1434 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001435 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001436
1437 writel(aqa, &dev->bar->aqa);
1438 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1439 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
1440 writel(dev->ctrl_config, &dev->bar->cc);
1441
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001442 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001443 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001444 return result;
Matthew Wilcox9e866772012-08-03 13:55:56 -04001445
Matthew Wilcox3193f072014-01-27 15:57:22 -05001446 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Keith Busch025c5572013-05-01 13:07:51 -06001447 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001448 return result;
Keith Busch025c5572013-05-01 13:07:51 -06001449
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001450 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001451 nvme_init_queue(nvmeq, 0);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001452 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001453 return result;
1454}
1455
Vishal Verma5d0f6132013-03-04 18:40:58 -07001456struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001457 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001458{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001459 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001460 struct scatterlist *sg;
1461 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001462 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001463
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001464 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001465 return ERR_PTR(-EINVAL);
Dan Carpenter5460fc02013-05-13 17:59:50 +03001466 if (!length || length > INT_MAX - PAGE_SIZE)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001467 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001468
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001469 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001470 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1471 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001472 if (!pages)
1473 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001474
1475 err = get_user_pages_fast(addr, count, 1, pages);
1476 if (err < count) {
1477 count = err;
1478 err = -EFAULT;
1479 goto put_pages;
1480 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001481
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001482 iod = nvme_alloc_iod(count, length, GFP_KERNEL);
1483 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001484 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001485 for (i = 0; i < count; i++) {
1486 sg_set_page(&sg[i], pages[i],
Dan Carpenter5460fc02013-05-13 17:59:50 +03001487 min_t(unsigned, length, PAGE_SIZE - offset),
1488 offset);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001489 length -= (PAGE_SIZE - offset);
1490 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001491 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001492 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001493 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001494
1495 err = -ENOMEM;
1496 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1497 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001498 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001499 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001500
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001501 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001502 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001503
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001504 free_iod:
1505 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001506 put_pages:
1507 for (i = 0; i < count; i++)
1508 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001509 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001510 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001511}
1512
Vishal Verma5d0f6132013-03-04 18:40:58 -07001513void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001514 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001515{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001516 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001517
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001518 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1519 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001520
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001521 for (i = 0; i < iod->nents; i++)
1522 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001523}
1524
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001525static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1526{
1527 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001528 struct nvme_user_io io;
1529 struct nvme_command c;
Keith Buschf410c682013-04-23 17:23:59 -06001530 unsigned length, meta_len;
1531 int status, i;
1532 struct nvme_iod *iod, *meta_iod = NULL;
1533 dma_addr_t meta_dma_addr;
1534 void *meta, *uninitialized_var(meta_mem);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001535
1536 if (copy_from_user(&io, uio, sizeof(io)))
1537 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001538 length = (io.nblocks + 1) << ns->lba_shift;
Keith Buschf410c682013-04-23 17:23:59 -06001539 meta_len = (io.nblocks + 1) * ns->ms;
1540
1541 if (meta_len && ((io.metadata & 3) || !io.metadata))
1542 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001543
1544 switch (io.opcode) {
1545 case nvme_cmd_write:
1546 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001547 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001548 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001549 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001550 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001551 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001552 }
1553
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001554 if (IS_ERR(iod))
1555 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001556
1557 memset(&c, 0, sizeof(c));
1558 c.rw.opcode = io.opcode;
1559 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001560 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001561 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001562 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001563 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001564 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1565 c.rw.reftag = cpu_to_le32(io.reftag);
1566 c.rw.apptag = cpu_to_le16(io.apptag);
1567 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buschf410c682013-04-23 17:23:59 -06001568
1569 if (meta_len) {
Keith Busch1b567492013-07-18 12:13:51 -06001570 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
1571 meta_len);
Keith Buschf410c682013-04-23 17:23:59 -06001572 if (IS_ERR(meta_iod)) {
1573 status = PTR_ERR(meta_iod);
1574 meta_iod = NULL;
1575 goto unmap;
1576 }
1577
1578 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1579 &meta_dma_addr, GFP_KERNEL);
1580 if (!meta_mem) {
1581 status = -ENOMEM;
1582 goto unmap;
1583 }
1584
1585 if (io.opcode & 1) {
1586 int meta_offset = 0;
1587
1588 for (i = 0; i < meta_iod->nents; i++) {
1589 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1590 meta_iod->sg[i].offset;
1591 memcpy(meta_mem + meta_offset, meta,
1592 meta_iod->sg[i].length);
1593 kunmap_atomic(meta);
1594 meta_offset += meta_iod->sg[i].length;
1595 }
1596 }
1597
1598 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1599 }
1600
Keith Buschedd10d32014-04-03 16:45:23 -06001601 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1602 c.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1603 c.rw.prp2 = cpu_to_le64(iod->first_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001604
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001605 if (length != (io.nblocks + 1) << ns->lba_shift)
1606 status = -ENOMEM;
1607 else
Keith Busch4f5099a2014-03-03 16:39:13 -07001608 status = nvme_submit_io_cmd(dev, &c, NULL);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001609
Keith Buschf410c682013-04-23 17:23:59 -06001610 if (meta_len) {
1611 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1612 int meta_offset = 0;
1613
1614 for (i = 0; i < meta_iod->nents; i++) {
1615 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1616 meta_iod->sg[i].offset;
1617 memcpy(meta, meta_mem + meta_offset,
1618 meta_iod->sg[i].length);
1619 kunmap_atomic(meta);
1620 meta_offset += meta_iod->sg[i].length;
1621 }
1622 }
1623
1624 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1625 meta_dma_addr);
1626 }
1627
1628 unmap:
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001629 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001630 nvme_free_iod(dev, iod);
Keith Buschf410c682013-04-23 17:23:59 -06001631
1632 if (meta_iod) {
1633 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1634 nvme_free_iod(dev, meta_iod);
1635 }
1636
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001637 return status;
1638}
1639
Keith Busch50af8ba2012-07-25 16:07:55 -06001640static int nvme_user_admin_cmd(struct nvme_dev *dev,
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001641 struct nvme_admin_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001642{
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001643 struct nvme_admin_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001644 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001645 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001646 struct nvme_iod *uninitialized_var(iod);
Keith Busch94f370c2013-05-09 14:01:38 -06001647 unsigned timeout;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001648
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001649 if (!capable(CAP_SYS_ADMIN))
1650 return -EACCES;
1651 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001652 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001653
1654 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001655 c.common.opcode = cmd.opcode;
1656 c.common.flags = cmd.flags;
1657 c.common.nsid = cpu_to_le32(cmd.nsid);
1658 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1659 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1660 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1661 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1662 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1663 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1664 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1665 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1666
1667 length = cmd.data_len;
1668 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001669 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1670 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001671 if (IS_ERR(iod))
1672 return PTR_ERR(iod);
Keith Buschedd10d32014-04-03 16:45:23 -06001673 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1674 c.common.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1675 c.common.prp2 = cpu_to_le64(iod->first_dma);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001676 }
1677
Keith Busch94f370c2013-05-09 14:01:38 -06001678 timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
1679 ADMIN_TIMEOUT;
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001680 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001681 status = -ENOMEM;
1682 else
Keith Busch4f5099a2014-03-03 16:39:13 -07001683 status = nvme_submit_sync_cmd(dev, 0, &c, &cmd.result, timeout);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001684
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001685 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001686 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001687 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001688 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001689
Chayan Biswascf90bc42013-05-22 22:34:49 +00001690 if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
Keith Buschf4f117f2012-09-21 10:49:05 -06001691 sizeof(cmd.result)))
1692 status = -EFAULT;
1693
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001694 return status;
1695}
1696
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001697static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1698 unsigned long arg)
1699{
1700 struct nvme_ns *ns = bdev->bd_disk->private_data;
1701
1702 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001703 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001704 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001705 return ns->ns_id;
1706 case NVME_IOCTL_ADMIN_CMD:
Keith Busch50af8ba2012-07-25 16:07:55 -06001707 return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001708 case NVME_IOCTL_SUBMIT_IO:
1709 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001710 case SG_GET_VERSION_NUM:
1711 return nvme_sg_get_version_num((void __user *)arg);
1712 case SG_IO:
1713 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001714 default:
1715 return -ENOTTY;
1716 }
1717}
1718
Keith Busch320a3822013-10-23 13:07:34 -06001719#ifdef CONFIG_COMPAT
1720static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1721 unsigned int cmd, unsigned long arg)
1722{
1723 struct nvme_ns *ns = bdev->bd_disk->private_data;
1724
1725 switch (cmd) {
1726 case SG_IO:
1727 return nvme_sg_io32(ns, arg);
1728 }
1729 return nvme_ioctl(bdev, mode, cmd, arg);
1730}
1731#else
1732#define nvme_compat_ioctl NULL
1733#endif
1734
Keith Busch9ac27092014-01-31 16:53:39 -07001735static int nvme_open(struct block_device *bdev, fmode_t mode)
1736{
1737 struct nvme_ns *ns = bdev->bd_disk->private_data;
1738 struct nvme_dev *dev = ns->dev;
1739
1740 kref_get(&dev->kref);
1741 return 0;
1742}
1743
1744static void nvme_free_dev(struct kref *kref);
1745
1746static void nvme_release(struct gendisk *disk, fmode_t mode)
1747{
1748 struct nvme_ns *ns = disk->private_data;
1749 struct nvme_dev *dev = ns->dev;
1750
1751 kref_put(&dev->kref, nvme_free_dev);
1752}
1753
Keith Busch4cc09e22014-04-02 15:45:37 -06001754static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
1755{
1756 /* some standard values */
1757 geo->heads = 1 << 6;
1758 geo->sectors = 1 << 5;
1759 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1760 return 0;
1761}
1762
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001763static const struct block_device_operations nvme_fops = {
1764 .owner = THIS_MODULE,
1765 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06001766 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07001767 .open = nvme_open,
1768 .release = nvme_release,
Keith Busch4cc09e22014-04-02 15:45:37 -06001769 .getgeo = nvme_getgeo,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001770};
1771
Keith Buschedd10d32014-04-03 16:45:23 -06001772static void nvme_resubmit_iods(struct nvme_queue *nvmeq)
1773{
1774 struct nvme_iod *iod, *next;
1775
1776 list_for_each_entry_safe(iod, next, &nvmeq->iod_bio, node) {
1777 if (unlikely(nvme_submit_iod(nvmeq, iod)))
1778 break;
1779 list_del(&iod->node);
1780 if (bio_list_empty(&nvmeq->sq_cong) &&
1781 list_empty(&nvmeq->iod_bio))
1782 remove_wait_queue(&nvmeq->sq_full,
1783 &nvmeq->sq_cong_wait);
1784 }
1785}
1786
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001787static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1788{
1789 while (bio_list_peek(&nvmeq->sq_cong)) {
1790 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1791 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Keith Busch427e9702013-04-09 11:59:32 -06001792
Keith Buschedd10d32014-04-03 16:45:23 -06001793 if (bio_list_empty(&nvmeq->sq_cong) &&
1794 list_empty(&nvmeq->iod_bio))
Matthew Wilcox3cb967c2011-03-16 16:45:49 -04001795 remove_wait_queue(&nvmeq->sq_full,
1796 &nvmeq->sq_cong_wait);
Keith Busch427e9702013-04-09 11:59:32 -06001797 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
Keith Buschedd10d32014-04-03 16:45:23 -06001798 if (!waitqueue_active(&nvmeq->sq_full))
Keith Busch427e9702013-04-09 11:59:32 -06001799 add_wait_queue(&nvmeq->sq_full,
1800 &nvmeq->sq_cong_wait);
1801 bio_list_add_head(&nvmeq->sq_cong, bio);
1802 break;
1803 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001804 }
1805}
1806
1807static int nvme_kthread(void *data)
1808{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001809 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001810
1811 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001812 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001813 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001814 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001815 int i;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001816 if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
1817 dev->initialized) {
1818 if (work_busy(&dev->reset_work))
1819 continue;
1820 list_del_init(&dev->node);
1821 dev_warn(&dev->pci_dev->dev,
1822 "Failed status, reset controller\n");
Tejun Heo9ca97372014-03-07 10:24:49 -05001823 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001824 queue_work(nvme_workq, &dev->reset_work);
1825 continue;
1826 }
Keith Busch5a92e702014-02-21 14:13:44 -07001827 rcu_read_lock();
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001828 for (i = 0; i < dev->queue_count; i++) {
Keith Busch5a92e702014-02-21 14:13:44 -07001829 struct nvme_queue *nvmeq =
1830 rcu_dereference(dev->queues[i]);
Matthew Wilcox740216f2011-02-15 16:28:20 -05001831 if (!nvmeq)
1832 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001833 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001834 if (nvmeq->q_suspended)
1835 goto unlock;
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001836 nvme_process_cq(nvmeq);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001837 nvme_cancel_ios(nvmeq, true);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001838 nvme_resubmit_bios(nvmeq);
Keith Buschedd10d32014-04-03 16:45:23 -06001839 nvme_resubmit_iods(nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001840 unlock:
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001841 spin_unlock_irq(&nvmeq->q_lock);
1842 }
Keith Busch5a92e702014-02-21 14:13:44 -07001843 rcu_read_unlock();
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001844 }
1845 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001846 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001847 }
1848 return 0;
1849}
1850
Keith Busch0e5e4f02012-11-09 16:33:05 -07001851static void nvme_config_discard(struct nvme_ns *ns)
1852{
1853 u32 logical_block_size = queue_logical_block_size(ns->queue);
1854 ns->queue->limits.discard_zeroes_data = 0;
1855 ns->queue->limits.discard_alignment = logical_block_size;
1856 ns->queue->limits.discard_granularity = logical_block_size;
1857 ns->queue->limits.max_discard_sectors = 0xffffffff;
1858 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1859}
1860
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001861static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001862 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1863{
1864 struct nvme_ns *ns;
1865 struct gendisk *disk;
1866 int lbaf;
1867
1868 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1869 return NULL;
1870
1871 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1872 if (!ns)
1873 return NULL;
1874 ns->queue = blk_alloc_queue(GFP_KERNEL);
1875 if (!ns->queue)
1876 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001877 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
1878 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1879 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001880 blk_queue_make_request(ns->queue, nvme_make_request);
1881 ns->dev = dev;
1882 ns->queue->queuedata = ns;
1883
Matthew Wilcox469071a2013-12-09 12:58:46 -05001884 disk = alloc_disk(0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001885 if (!disk)
1886 goto out_free_queue;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001887 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001888 ns->disk = disk;
1889 lbaf = id->flbas & 0xf;
1890 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Buschf410c682013-04-23 17:23:59 -06001891 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busche9ef4632012-07-24 15:01:04 -06001892 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06001893 if (dev->max_hw_sectors)
1894 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001895
1896 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001897 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001898 disk->fops = &nvme_fops;
1899 disk->private_data = ns;
1900 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05001901 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001902 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001903 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001904 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1905
Keith Busch0e5e4f02012-11-09 16:33:05 -07001906 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1907 nvme_config_discard(ns);
1908
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001909 return ns;
1910
1911 out_free_queue:
1912 blk_cleanup_queue(ns->queue);
1913 out_free_ns:
1914 kfree(ns);
1915 return NULL;
1916}
1917
Keith Busch42f61422014-03-24 10:46:25 -06001918static int nvme_find_closest_node(int node)
1919{
1920 int n, val, min_val = INT_MAX, best_node = node;
1921
1922 for_each_online_node(n) {
1923 if (n == node)
1924 continue;
1925 val = node_distance(node, n);
1926 if (val < min_val) {
1927 min_val = val;
1928 best_node = n;
1929 }
1930 }
1931 return best_node;
1932}
1933
1934static void nvme_set_queue_cpus(cpumask_t *qmask, struct nvme_queue *nvmeq,
1935 int count)
1936{
1937 int cpu;
1938 for_each_cpu(cpu, qmask) {
1939 if (cpumask_weight(nvmeq->cpu_mask) >= count)
1940 break;
1941 if (!cpumask_test_and_set_cpu(cpu, nvmeq->cpu_mask))
1942 *per_cpu_ptr(nvmeq->dev->io_queue, cpu) = nvmeq->qid;
1943 }
1944}
1945
1946static void nvme_add_cpus(cpumask_t *mask, const cpumask_t *unassigned_cpus,
1947 const cpumask_t *new_mask, struct nvme_queue *nvmeq, int cpus_per_queue)
1948{
1949 int next_cpu;
1950 for_each_cpu(next_cpu, new_mask) {
1951 cpumask_or(mask, mask, get_cpu_mask(next_cpu));
1952 cpumask_or(mask, mask, topology_thread_cpumask(next_cpu));
1953 cpumask_and(mask, mask, unassigned_cpus);
1954 nvme_set_queue_cpus(mask, nvmeq, cpus_per_queue);
1955 }
1956}
1957
1958static void nvme_create_io_queues(struct nvme_dev *dev)
1959{
1960 unsigned i, max;
1961
1962 max = min(dev->max_qid, num_online_cpus());
1963 for (i = dev->queue_count; i <= max; i++)
1964 if (!nvme_alloc_queue(dev, i, dev->q_depth, i - 1))
1965 break;
1966
1967 max = min(dev->queue_count - 1, num_online_cpus());
1968 for (i = dev->online_queues; i <= max; i++)
1969 if (nvme_create_queue(raw_nvmeq(dev, i), i))
1970 break;
1971}
1972
1973/*
1974 * If there are fewer queues than online cpus, this will try to optimally
1975 * assign a queue to multiple cpus by grouping cpus that are "close" together:
1976 * thread siblings, core, socket, closest node, then whatever else is
1977 * available.
1978 */
1979static void nvme_assign_io_queues(struct nvme_dev *dev)
1980{
1981 unsigned cpu, cpus_per_queue, queues, remainder, i;
1982 cpumask_var_t unassigned_cpus;
1983
1984 nvme_create_io_queues(dev);
1985
1986 queues = min(dev->online_queues - 1, num_online_cpus());
1987 if (!queues)
1988 return;
1989
1990 cpus_per_queue = num_online_cpus() / queues;
1991 remainder = queues - (num_online_cpus() - queues * cpus_per_queue);
1992
1993 if (!alloc_cpumask_var(&unassigned_cpus, GFP_KERNEL))
1994 return;
1995
1996 cpumask_copy(unassigned_cpus, cpu_online_mask);
1997 cpu = cpumask_first(unassigned_cpus);
1998 for (i = 1; i <= queues; i++) {
1999 struct nvme_queue *nvmeq = lock_nvmeq(dev, i);
2000 cpumask_t mask;
2001
2002 cpumask_clear(nvmeq->cpu_mask);
2003 if (!cpumask_weight(unassigned_cpus)) {
2004 unlock_nvmeq(nvmeq);
2005 break;
2006 }
2007
2008 mask = *get_cpu_mask(cpu);
2009 nvme_set_queue_cpus(&mask, nvmeq, cpus_per_queue);
2010 if (cpus_weight(mask) < cpus_per_queue)
2011 nvme_add_cpus(&mask, unassigned_cpus,
2012 topology_thread_cpumask(cpu),
2013 nvmeq, cpus_per_queue);
2014 if (cpus_weight(mask) < cpus_per_queue)
2015 nvme_add_cpus(&mask, unassigned_cpus,
2016 topology_core_cpumask(cpu),
2017 nvmeq, cpus_per_queue);
2018 if (cpus_weight(mask) < cpus_per_queue)
2019 nvme_add_cpus(&mask, unassigned_cpus,
2020 cpumask_of_node(cpu_to_node(cpu)),
2021 nvmeq, cpus_per_queue);
2022 if (cpus_weight(mask) < cpus_per_queue)
2023 nvme_add_cpus(&mask, unassigned_cpus,
2024 cpumask_of_node(
2025 nvme_find_closest_node(
2026 cpu_to_node(cpu))),
2027 nvmeq, cpus_per_queue);
2028 if (cpus_weight(mask) < cpus_per_queue)
2029 nvme_add_cpus(&mask, unassigned_cpus,
2030 unassigned_cpus,
2031 nvmeq, cpus_per_queue);
2032
2033 WARN(cpumask_weight(nvmeq->cpu_mask) != cpus_per_queue,
2034 "nvme%d qid:%d mis-matched queue-to-cpu assignment\n",
2035 dev->instance, i);
2036
2037 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
2038 nvmeq->cpu_mask);
2039 cpumask_andnot(unassigned_cpus, unassigned_cpus,
2040 nvmeq->cpu_mask);
2041 cpu = cpumask_next(cpu, unassigned_cpus);
2042 if (remainder && !--remainder)
2043 cpus_per_queue++;
2044 unlock_nvmeq(nvmeq);
2045 }
2046 WARN(cpumask_weight(unassigned_cpus), "nvme%d unassigned online cpus\n",
2047 dev->instance);
2048 i = 0;
2049 cpumask_andnot(unassigned_cpus, cpu_possible_mask, cpu_online_mask);
2050 for_each_cpu(cpu, unassigned_cpus)
2051 *per_cpu_ptr(dev->io_queue, cpu) = (i++ % queues) + 1;
2052 free_cpumask_var(unassigned_cpus);
2053}
2054
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002055static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002056{
2057 int status;
2058 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002059 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002060
Matthew Wilcoxdf348132012-01-11 07:29:56 -07002061 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002062 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04002063 if (status < 0)
2064 return status;
2065 if (status > 0) {
2066 dev_err(&dev->pci_dev->dev, "Could not set queue count (%d)\n",
2067 status);
2068 return -EBUSY;
2069 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002070 return min(result & 0xffff, result >> 16) + 1;
2071}
2072
Keith Busch9d713c22013-07-15 15:02:24 -06002073static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
2074{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002075 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06002076}
2077
Keith Busch33b1e952014-03-24 10:46:26 -06002078static int nvme_cpu_notify(struct notifier_block *self,
2079 unsigned long action, void *hcpu)
2080{
2081 struct nvme_dev *dev = container_of(self, struct nvme_dev, nb);
2082 switch (action) {
2083 case CPU_ONLINE:
2084 case CPU_DEAD:
2085 nvme_assign_io_queues(dev);
2086 break;
2087 }
2088 return NOTIFY_OK;
2089}
2090
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002091static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002092{
Keith Busch5a92e702014-02-21 14:13:44 -07002093 struct nvme_queue *adminq = raw_nvmeq(dev, 0);
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002094 struct pci_dev *pdev = dev->pci_dev;
Keith Busch42f61422014-03-24 10:46:25 -06002095 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002096
Keith Busch42f61422014-03-24 10:46:25 -06002097 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002098 result = set_queue_count(dev, nr_io_queues);
Matthew Wilcox1b234842011-01-20 13:01:49 -05002099 if (result < 0)
2100 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002101 if (result < nr_io_queues)
2102 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002103
Keith Busch9d713c22013-07-15 15:02:24 -06002104 size = db_bar_size(dev, nr_io_queues);
2105 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002106 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06002107 do {
2108 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
2109 if (dev->bar)
2110 break;
2111 if (!--nr_io_queues)
2112 return -ENOMEM;
2113 size = db_bar_size(dev, nr_io_queues);
2114 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002115 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07002116 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002117 }
2118
Keith Busch9d713c22013-07-15 15:02:24 -06002119 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002120 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002121
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002122 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002123 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002124 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
2125 if (vecs < 0) {
2126 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
2127 if (vecs < 0) {
2128 vecs = 1;
2129 } else {
2130 for (i = 0; i < vecs; i++)
2131 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002132 }
2133 }
2134
Matthew Wilcox063a8092013-06-20 10:53:48 -04002135 /*
2136 * Should investigate if there's a performance win from allocating
2137 * more queues than interrupt vectors; it might allow the submission
2138 * path to scale better, even if the receive path is limited by the
2139 * number of interrupts.
2140 */
2141 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002142 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002143
Matthew Wilcox3193f072014-01-27 15:57:22 -05002144 result = queue_request_irq(dev, adminq, adminq->irqname);
Keith Busch9d713c22013-07-15 15:02:24 -06002145 if (result) {
Matthew Wilcox3193f072014-01-27 15:57:22 -05002146 adminq->q_suspended = 1;
Keith Busch22404272013-07-15 15:02:20 -06002147 goto free_queues;
Keith Busch9d713c22013-07-15 15:02:24 -06002148 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05002149
Keith Buschcd638942013-07-15 15:02:23 -06002150 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002151 nvme_free_queues(dev, nr_io_queues + 1);
2152 nvme_assign_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06002153
Keith Busch33b1e952014-03-24 10:46:26 -06002154 dev->nb.notifier_call = &nvme_cpu_notify;
2155 result = register_hotcpu_notifier(&dev->nb);
2156 if (result)
2157 goto free_queues;
Keith Busch22404272013-07-15 15:02:20 -06002158
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002159 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002160
Keith Busch22404272013-07-15 15:02:20 -06002161 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002162 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002163 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002164}
2165
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002166/*
2167 * Return: error value if an error occurred setting up the queues or calling
2168 * Identify Device. 0 if these succeeded, even if adding some of the
2169 * namespaces failed. At the moment, these failures are silent. TBD which
2170 * failures should be reported.
2171 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002172static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002173{
Matthew Wilcox68608c262013-06-21 14:36:34 -04002174 struct pci_dev *pdev = dev->pci_dev;
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002175 int res;
2176 unsigned nn, i;
Keith Buschcbb62182013-05-01 13:07:49 -06002177 struct nvme_ns *ns;
Matthew Wilcox51814232011-02-01 16:18:08 -05002178 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002179 struct nvme_id_ns *id_ns;
2180 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002181 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06002182 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002183
Matthew Wilcox68608c262013-06-21 14:36:34 -04002184 mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06002185 if (!mem)
2186 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002187
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002188 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002189 if (res) {
Matthew Wilcox27e81662014-04-11 11:58:45 -04002190 dev_err(&pdev->dev, "Identify Controller failed (%d)\n", res);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002191 res = -EIO;
Keith Buschcbb62182013-05-01 13:07:49 -06002192 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002193 }
2194
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002195 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05002196 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002197 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002198 dev->abort_limit = ctrl->acl + 1;
Matthew Wilcox51814232011-02-01 16:18:08 -05002199 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2200 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2201 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002202 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002203 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002204 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
2205 (pdev->device == 0x0953) && ctrl->vs[3])
Keith Busch159b67d2013-04-09 17:13:20 -06002206 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002207
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002208 id_ns = mem;
Matthew Wilcox2b2c1892011-10-07 13:10:13 -04002209 for (i = 1; i <= nn; i++) {
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002210 res = nvme_identify(dev, i, 0, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002211 if (res)
2212 continue;
2213
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002214 if (id_ns->ncap == 0)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002215 continue;
2216
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002217 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
Keith Busch08df1e02012-09-21 10:52:13 -06002218 dma_addr + 4096, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002219 if (res)
Keith Busch12209032013-01-31 14:40:38 -07002220 memset(mem + 4096, 0, 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002221
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002222 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002223 if (ns)
2224 list_add_tail(&ns->list, &dev->namespaces);
2225 }
2226 list_for_each_entry(ns, &dev->namespaces, list)
2227 add_disk(ns->disk);
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002228 res = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002229
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002230 out:
Matthew Wilcox684f5c22011-09-19 17:14:53 -04002231 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002232 return res;
2233}
2234
Keith Busch0877cb02013-07-15 15:02:19 -06002235static int nvme_dev_map(struct nvme_dev *dev)
2236{
Keith Busch42f61422014-03-24 10:46:25 -06002237 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002238 int bars, result = -ENOMEM;
2239 struct pci_dev *pdev = dev->pci_dev;
2240
2241 if (pci_enable_device_mem(pdev))
2242 return result;
2243
2244 dev->entry[0].vector = pdev->irq;
2245 pci_set_master(pdev);
2246 bars = pci_select_bars(pdev, IORESOURCE_MEM);
2247 if (pci_request_selected_regions(pdev, bars, "nvme"))
2248 goto disable_pci;
2249
Russell King052d0ef2013-06-26 23:49:11 +01002250 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
2251 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2252 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002253
Keith Busch0877cb02013-07-15 15:02:19 -06002254 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2255 if (!dev->bar)
2256 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07002257 if (readl(&dev->bar->csts) == -1) {
2258 result = -ENODEV;
2259 goto unmap;
2260 }
Keith Busch42f61422014-03-24 10:46:25 -06002261 cap = readq(&dev->bar->cap);
2262 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2263 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Keith Busch0877cb02013-07-15 15:02:19 -06002264 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2265
2266 return 0;
2267
Keith Busch0e53d182013-12-10 13:10:39 -07002268 unmap:
2269 iounmap(dev->bar);
2270 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002271 disable:
2272 pci_release_regions(pdev);
2273 disable_pci:
2274 pci_disable_device(pdev);
2275 return result;
2276}
2277
2278static void nvme_dev_unmap(struct nvme_dev *dev)
2279{
2280 if (dev->pci_dev->msi_enabled)
2281 pci_disable_msi(dev->pci_dev);
2282 else if (dev->pci_dev->msix_enabled)
2283 pci_disable_msix(dev->pci_dev);
2284
2285 if (dev->bar) {
2286 iounmap(dev->bar);
2287 dev->bar = NULL;
Keith Busch9a6b9452013-12-10 13:10:36 -07002288 pci_release_regions(dev->pci_dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002289 }
2290
Keith Busch0877cb02013-07-15 15:02:19 -06002291 if (pci_is_enabled(dev->pci_dev))
2292 pci_disable_device(dev->pci_dev);
2293}
2294
Keith Busch4d115422013-12-10 13:10:40 -07002295struct nvme_delq_ctx {
2296 struct task_struct *waiter;
2297 struct kthread_worker *worker;
2298 atomic_t refcount;
2299};
2300
2301static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2302{
2303 dq->waiter = current;
2304 mb();
2305
2306 for (;;) {
2307 set_current_state(TASK_KILLABLE);
2308 if (!atomic_read(&dq->refcount))
2309 break;
2310 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2311 fatal_signal_pending(current)) {
2312 set_current_state(TASK_RUNNING);
2313
2314 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
2315 nvme_disable_queue(dev, 0);
2316
2317 send_sig(SIGKILL, dq->worker->task, 1);
2318 flush_kthread_worker(dq->worker);
2319 return;
2320 }
2321 }
2322 set_current_state(TASK_RUNNING);
2323}
2324
2325static void nvme_put_dq(struct nvme_delq_ctx *dq)
2326{
2327 atomic_dec(&dq->refcount);
2328 if (dq->waiter)
2329 wake_up_process(dq->waiter);
2330}
2331
2332static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2333{
2334 atomic_inc(&dq->refcount);
2335 return dq;
2336}
2337
2338static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2339{
2340 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
2341
2342 nvme_clear_queue(nvmeq);
2343 nvme_put_dq(dq);
2344}
2345
2346static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2347 kthread_work_func_t fn)
2348{
2349 struct nvme_command c;
2350
2351 memset(&c, 0, sizeof(c));
2352 c.delete_queue.opcode = opcode;
2353 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2354
2355 init_kthread_work(&nvmeq->cmdinfo.work, fn);
2356 return nvme_submit_admin_cmd_async(nvmeq->dev, &c, &nvmeq->cmdinfo);
2357}
2358
2359static void nvme_del_cq_work_handler(struct kthread_work *work)
2360{
2361 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2362 cmdinfo.work);
2363 nvme_del_queue_end(nvmeq);
2364}
2365
2366static int nvme_delete_cq(struct nvme_queue *nvmeq)
2367{
2368 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2369 nvme_del_cq_work_handler);
2370}
2371
2372static void nvme_del_sq_work_handler(struct kthread_work *work)
2373{
2374 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2375 cmdinfo.work);
2376 int status = nvmeq->cmdinfo.status;
2377
2378 if (!status)
2379 status = nvme_delete_cq(nvmeq);
2380 if (status)
2381 nvme_del_queue_end(nvmeq);
2382}
2383
2384static int nvme_delete_sq(struct nvme_queue *nvmeq)
2385{
2386 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2387 nvme_del_sq_work_handler);
2388}
2389
2390static void nvme_del_queue_start(struct kthread_work *work)
2391{
2392 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2393 cmdinfo.work);
2394 allow_signal(SIGKILL);
2395 if (nvme_delete_sq(nvmeq))
2396 nvme_del_queue_end(nvmeq);
2397}
2398
2399static void nvme_disable_io_queues(struct nvme_dev *dev)
2400{
2401 int i;
2402 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2403 struct nvme_delq_ctx dq;
2404 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2405 &worker, "nvme%d", dev->instance);
2406
2407 if (IS_ERR(kworker_task)) {
2408 dev_err(&dev->pci_dev->dev,
2409 "Failed to create queue del task\n");
2410 for (i = dev->queue_count - 1; i > 0; i--)
2411 nvme_disable_queue(dev, i);
2412 return;
2413 }
2414
2415 dq.waiter = NULL;
2416 atomic_set(&dq.refcount, 0);
2417 dq.worker = &worker;
2418 for (i = dev->queue_count - 1; i > 0; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07002419 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
Keith Busch4d115422013-12-10 13:10:40 -07002420
2421 if (nvme_suspend_queue(nvmeq))
2422 continue;
2423 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2424 nvmeq->cmdinfo.worker = dq.worker;
2425 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2426 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2427 }
2428 nvme_wait_dq(&dq, dev);
2429 kthread_stop(kworker_task);
2430}
2431
Dan McLeranb9afca32014-04-07 17:10:11 -06002432/*
2433* Remove the node from the device list and check
2434* for whether or not we need to stop the nvme_thread.
2435*/
2436static void nvme_dev_list_remove(struct nvme_dev *dev)
2437{
2438 struct task_struct *tmp = NULL;
2439
2440 spin_lock(&dev_list_lock);
2441 list_del_init(&dev->node);
2442 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2443 tmp = nvme_thread;
2444 nvme_thread = NULL;
2445 }
2446 spin_unlock(&dev_list_lock);
2447
2448 if (tmp)
2449 kthread_stop(tmp);
2450}
2451
Keith Buschf0b50732013-07-15 15:02:21 -06002452static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002453{
Keith Busch22404272013-07-15 15:02:20 -06002454 int i;
2455
Keith Buschd4b4ff82013-12-10 13:10:37 -07002456 dev->initialized = 0;
Keith Busch33b1e952014-03-24 10:46:26 -06002457 unregister_hotcpu_notifier(&dev->nb);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002458
Dan McLeranb9afca32014-04-07 17:10:11 -06002459 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002460
Keith Busch4d115422013-12-10 13:10:40 -07002461 if (!dev->bar || (dev->bar && readl(&dev->bar->csts) == -1)) {
2462 for (i = dev->queue_count - 1; i >= 0; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07002463 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
Keith Busch4d115422013-12-10 13:10:40 -07002464 nvme_suspend_queue(nvmeq);
2465 nvme_clear_queue(nvmeq);
2466 }
2467 } else {
2468 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002469 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002470 nvme_disable_queue(dev, 0);
2471 }
Keith Buschf0b50732013-07-15 15:02:21 -06002472 nvme_dev_unmap(dev);
2473}
2474
2475static void nvme_dev_remove(struct nvme_dev *dev)
2476{
Keith Busch9ac27092014-01-31 16:53:39 -07002477 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002478
Keith Busch9ac27092014-01-31 16:53:39 -07002479 list_for_each_entry(ns, &dev->namespaces, list) {
2480 if (ns->disk->flags & GENHD_FL_UP)
2481 del_gendisk(ns->disk);
2482 if (!blk_queue_dying(ns->queue))
2483 blk_cleanup_queue(ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002484 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002485}
2486
Matthew Wilcox091b6092011-02-10 09:56:01 -05002487static int nvme_setup_prp_pools(struct nvme_dev *dev)
2488{
2489 struct device *dmadev = &dev->pci_dev->dev;
2490 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2491 PAGE_SIZE, PAGE_SIZE, 0);
2492 if (!dev->prp_page_pool)
2493 return -ENOMEM;
2494
Matthew Wilcox99802a72011-02-10 10:30:34 -05002495 /* Optimisation for I/Os between 4k and 128k */
2496 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2497 256, 256, 0);
2498 if (!dev->prp_small_pool) {
2499 dma_pool_destroy(dev->prp_page_pool);
2500 return -ENOMEM;
2501 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002502 return 0;
2503}
2504
2505static void nvme_release_prp_pools(struct nvme_dev *dev)
2506{
2507 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002508 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002509}
2510
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002511static DEFINE_IDA(nvme_instance_ida);
2512
2513static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002514{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002515 int instance, error;
2516
2517 do {
2518 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2519 return -ENODEV;
2520
2521 spin_lock(&dev_list_lock);
2522 error = ida_get_new(&nvme_instance_ida, &instance);
2523 spin_unlock(&dev_list_lock);
2524 } while (error == -EAGAIN);
2525
2526 if (error)
2527 return -ENODEV;
2528
2529 dev->instance = instance;
2530 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002531}
2532
2533static void nvme_release_instance(struct nvme_dev *dev)
2534{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002535 spin_lock(&dev_list_lock);
2536 ida_remove(&nvme_instance_ida, dev->instance);
2537 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002538}
2539
Keith Busch9ac27092014-01-31 16:53:39 -07002540static void nvme_free_namespaces(struct nvme_dev *dev)
2541{
2542 struct nvme_ns *ns, *next;
2543
2544 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2545 list_del(&ns->list);
2546 put_disk(ns->disk);
2547 kfree(ns);
2548 }
2549}
2550
Keith Busch5e82e952013-02-19 10:17:58 -07002551static void nvme_free_dev(struct kref *kref)
2552{
2553 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002554
2555 nvme_free_namespaces(dev);
Keith Busch42f61422014-03-24 10:46:25 -06002556 free_percpu(dev->io_queue);
Keith Busch5e82e952013-02-19 10:17:58 -07002557 kfree(dev->queues);
2558 kfree(dev->entry);
2559 kfree(dev);
2560}
2561
2562static int nvme_dev_open(struct inode *inode, struct file *f)
2563{
2564 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
2565 miscdev);
2566 kref_get(&dev->kref);
2567 f->private_data = dev;
2568 return 0;
2569}
2570
2571static int nvme_dev_release(struct inode *inode, struct file *f)
2572{
2573 struct nvme_dev *dev = f->private_data;
2574 kref_put(&dev->kref, nvme_free_dev);
2575 return 0;
2576}
2577
2578static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2579{
2580 struct nvme_dev *dev = f->private_data;
2581 switch (cmd) {
2582 case NVME_IOCTL_ADMIN_CMD:
2583 return nvme_user_admin_cmd(dev, (void __user *)arg);
2584 default:
2585 return -ENOTTY;
2586 }
2587}
2588
2589static const struct file_operations nvme_dev_fops = {
2590 .owner = THIS_MODULE,
2591 .open = nvme_dev_open,
2592 .release = nvme_dev_release,
2593 .unlocked_ioctl = nvme_dev_ioctl,
2594 .compat_ioctl = nvme_dev_ioctl,
2595};
2596
Keith Buschf0b50732013-07-15 15:02:21 -06002597static int nvme_dev_start(struct nvme_dev *dev)
2598{
2599 int result;
Dan McLeranb9afca32014-04-07 17:10:11 -06002600 bool start_thread = false;
Keith Buschf0b50732013-07-15 15:02:21 -06002601
2602 result = nvme_dev_map(dev);
2603 if (result)
2604 return result;
2605
2606 result = nvme_configure_admin_queue(dev);
2607 if (result)
2608 goto unmap;
2609
2610 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002611 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2612 start_thread = true;
2613 nvme_thread = NULL;
2614 }
Keith Buschf0b50732013-07-15 15:02:21 -06002615 list_add(&dev->node, &dev_list);
2616 spin_unlock(&dev_list_lock);
2617
Dan McLeranb9afca32014-04-07 17:10:11 -06002618 if (start_thread) {
2619 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2620 wake_up(&nvme_kthread_wait);
2621 } else
2622 wait_event_killable(nvme_kthread_wait, nvme_thread);
2623
2624 if (IS_ERR_OR_NULL(nvme_thread)) {
2625 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2626 goto disable;
2627 }
2628
Keith Buschf0b50732013-07-15 15:02:21 -06002629 result = nvme_setup_io_queues(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002630 if (result && result != -EBUSY)
Keith Buschf0b50732013-07-15 15:02:21 -06002631 goto disable;
2632
Keith Buschd82e8bf2013-09-05 14:45:07 -06002633 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002634
2635 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002636 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06002637 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002638 unmap:
2639 nvme_dev_unmap(dev);
2640 return result;
2641}
2642
Keith Busch9a6b9452013-12-10 13:10:36 -07002643static int nvme_remove_dead_ctrl(void *arg)
2644{
2645 struct nvme_dev *dev = (struct nvme_dev *)arg;
2646 struct pci_dev *pdev = dev->pci_dev;
2647
2648 if (pci_get_drvdata(pdev))
2649 pci_stop_and_remove_bus_device(pdev);
2650 kref_put(&dev->kref, nvme_free_dev);
2651 return 0;
2652}
2653
2654static void nvme_remove_disks(struct work_struct *ws)
2655{
Keith Busch9a6b9452013-12-10 13:10:36 -07002656 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2657
2658 nvme_dev_remove(dev);
Keith Busch5a92e702014-02-21 14:13:44 -07002659 nvme_free_queues(dev, 1);
Keith Busch9a6b9452013-12-10 13:10:36 -07002660}
2661
2662static int nvme_dev_resume(struct nvme_dev *dev)
2663{
2664 int ret;
2665
2666 ret = nvme_dev_start(dev);
2667 if (ret && ret != -EBUSY)
2668 return ret;
2669 if (ret == -EBUSY) {
2670 spin_lock(&dev_list_lock);
Tejun Heo9ca97372014-03-07 10:24:49 -05002671 dev->reset_workfn = nvme_remove_disks;
Keith Busch9a6b9452013-12-10 13:10:36 -07002672 queue_work(nvme_workq, &dev->reset_work);
2673 spin_unlock(&dev_list_lock);
2674 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002675 dev->initialized = 1;
Keith Busch9a6b9452013-12-10 13:10:36 -07002676 return 0;
2677}
2678
2679static void nvme_dev_reset(struct nvme_dev *dev)
2680{
2681 nvme_dev_shutdown(dev);
2682 if (nvme_dev_resume(dev)) {
2683 dev_err(&dev->pci_dev->dev, "Device failed to resume\n");
2684 kref_get(&dev->kref);
2685 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2686 dev->instance))) {
2687 dev_err(&dev->pci_dev->dev,
2688 "Failed to start controller remove task\n");
2689 kref_put(&dev->kref, nvme_free_dev);
2690 }
2691 }
2692}
2693
2694static void nvme_reset_failed_dev(struct work_struct *ws)
2695{
2696 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2697 nvme_dev_reset(dev);
2698}
2699
Tejun Heo9ca97372014-03-07 10:24:49 -05002700static void nvme_reset_workfn(struct work_struct *work)
2701{
2702 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
2703 dev->reset_workfn(work);
2704}
2705
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002706static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002707{
Keith Busch0877cb02013-07-15 15:02:19 -06002708 int result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002709 struct nvme_dev *dev;
2710
2711 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2712 if (!dev)
2713 return -ENOMEM;
2714 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
2715 GFP_KERNEL);
2716 if (!dev->entry)
2717 goto free;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002718 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
2719 GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002720 if (!dev->queues)
2721 goto free;
Keith Busch42f61422014-03-24 10:46:25 -06002722 dev->io_queue = alloc_percpu(unsigned short);
2723 if (!dev->io_queue)
2724 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002725
2726 INIT_LIST_HEAD(&dev->namespaces);
Tejun Heo9ca97372014-03-07 10:24:49 -05002727 dev->reset_workfn = nvme_reset_failed_dev;
2728 INIT_WORK(&dev->reset_work, nvme_reset_workfn);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002729 dev->pci_dev = pdev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002730 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002731 result = nvme_set_instance(dev);
2732 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002733 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002734
Matthew Wilcox091b6092011-02-10 09:56:01 -05002735 result = nvme_setup_prp_pools(dev);
2736 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002737 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002738
Keith Buschfb35e912014-03-03 11:09:47 -07002739 kref_init(&dev->kref);
Keith Buschf0b50732013-07-15 15:02:21 -06002740 result = nvme_dev_start(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002741 if (result) {
2742 if (result == -EBUSY)
2743 goto create_cdev;
Keith Busch0877cb02013-07-15 15:02:19 -06002744 goto release_pools;
Keith Buschd82e8bf2013-09-05 14:45:07 -06002745 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002746
Matthew Wilcox740216f2011-02-15 16:28:20 -05002747 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002748 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002749 goto shutdown;
Matthew Wilcox740216f2011-02-15 16:28:20 -05002750
Keith Buschd82e8bf2013-09-05 14:45:07 -06002751 create_cdev:
Keith Busch5e82e952013-02-19 10:17:58 -07002752 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
2753 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2754 dev->miscdev.parent = &pdev->dev;
2755 dev->miscdev.name = dev->name;
2756 dev->miscdev.fops = &nvme_dev_fops;
2757 result = misc_register(&dev->miscdev);
2758 if (result)
2759 goto remove;
2760
Keith Buschd4b4ff82013-12-10 13:10:37 -07002761 dev->initialized = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002762 return 0;
2763
Keith Busch5e82e952013-02-19 10:17:58 -07002764 remove:
2765 nvme_dev_remove(dev);
Keith Busch9ac27092014-01-31 16:53:39 -07002766 nvme_free_namespaces(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002767 shutdown:
2768 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002769 release_pools:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002770 nvme_free_queues(dev, 0);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002771 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002772 release:
2773 nvme_release_instance(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002774 free:
Keith Busch42f61422014-03-24 10:46:25 -06002775 free_percpu(dev->io_queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002776 kfree(dev->queues);
2777 kfree(dev->entry);
2778 kfree(dev);
2779 return result;
2780}
2781
Keith Busch09ece142014-01-27 11:29:40 -05002782static void nvme_shutdown(struct pci_dev *pdev)
2783{
2784 struct nvme_dev *dev = pci_get_drvdata(pdev);
2785 nvme_dev_shutdown(dev);
2786}
2787
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002788static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002789{
2790 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002791
2792 spin_lock(&dev_list_lock);
2793 list_del_init(&dev->node);
2794 spin_unlock(&dev_list_lock);
2795
2796 pci_set_drvdata(pdev, NULL);
2797 flush_work(&dev->reset_work);
Keith Busch5e82e952013-02-19 10:17:58 -07002798 misc_deregister(&dev->miscdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002799 nvme_dev_remove(dev);
2800 nvme_dev_shutdown(dev);
Keith Buscha1a5ef92013-12-16 13:50:00 -05002801 nvme_free_queues(dev, 0);
Keith Busch5a92e702014-02-21 14:13:44 -07002802 rcu_barrier();
Keith Busch9a6b9452013-12-10 13:10:36 -07002803 nvme_release_instance(dev);
2804 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002805 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002806}
2807
2808/* These functions are yet to be implemented */
2809#define nvme_error_detected NULL
2810#define nvme_dump_registers NULL
2811#define nvme_link_reset NULL
2812#define nvme_slot_reset NULL
2813#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002814
Jingoo Han671a6012014-02-13 11:19:14 +09002815#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002816static int nvme_suspend(struct device *dev)
2817{
2818 struct pci_dev *pdev = to_pci_dev(dev);
2819 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2820
2821 nvme_dev_shutdown(ndev);
2822 return 0;
2823}
2824
2825static int nvme_resume(struct device *dev)
2826{
2827 struct pci_dev *pdev = to_pci_dev(dev);
2828 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002829
Keith Busch9a6b9452013-12-10 13:10:36 -07002830 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Tejun Heo9ca97372014-03-07 10:24:49 -05002831 ndev->reset_workfn = nvme_reset_failed_dev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002832 queue_work(nvme_workq, &ndev->reset_work);
2833 }
2834 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002835}
Jingoo Han671a6012014-02-13 11:19:14 +09002836#endif
Keith Buschcd638942013-07-15 15:02:23 -06002837
2838static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002839
Stephen Hemminger1d352032012-09-07 09:33:17 -07002840static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002841 .error_detected = nvme_error_detected,
2842 .mmio_enabled = nvme_dump_registers,
2843 .link_reset = nvme_link_reset,
2844 .slot_reset = nvme_slot_reset,
2845 .resume = nvme_error_resume,
2846};
2847
2848/* Move to pci_ids.h later */
2849#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2850
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002851static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002852 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2853 { 0, }
2854};
2855MODULE_DEVICE_TABLE(pci, nvme_id_table);
2856
2857static struct pci_driver nvme_driver = {
2858 .name = "nvme",
2859 .id_table = nvme_id_table,
2860 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002861 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002862 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002863 .driver = {
2864 .pm = &nvme_dev_pm_ops,
2865 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002866 .err_handler = &nvme_err_handler,
2867};
2868
2869static int __init nvme_init(void)
2870{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002871 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002872
Dan McLeranb9afca32014-04-07 17:10:11 -06002873 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002874
Keith Busch9a6b9452013-12-10 13:10:36 -07002875 nvme_workq = create_singlethread_workqueue("nvme");
2876 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06002877 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07002878
Keith Busch5c42ea12012-07-25 16:05:18 -06002879 result = register_blkdev(nvme_major, "nvme");
2880 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002881 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06002882 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002883 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002884
2885 result = pci_register_driver(&nvme_driver);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002886 if (result)
2887 goto unregister_blkdev;
2888 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002889
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002890 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002891 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002892 kill_workq:
2893 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002894 return result;
2895}
2896
2897static void __exit nvme_exit(void)
2898{
2899 pci_unregister_driver(&nvme_driver);
2900 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002901 destroy_workqueue(nvme_workq);
Dan McLeranb9afca32014-04-07 17:10:11 -06002902 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002903}
2904
2905MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2906MODULE_LICENSE("GPL");
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002907MODULE_VERSION("0.9");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002908module_init(nvme_init);
2909module_exit(nvme_exit);