blob: efa9c8f4a7a76b320840250c6f73b3a0bb9705c9 [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003 * Copyright (c) 2011-2014, Intel Corporation.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#include <linux/nvme.h>
20#include <linux/bio.h>
Matthew Wilcox8de05532011-05-12 13:50:28 -040021#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050022#include <linux/blkdev.h>
Keith Busch42f61422014-03-24 10:46:25 -060023#include <linux/cpu.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040024#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050025#include <linux/errno.h>
26#include <linux/fs.h>
27#include <linux/genhd.h>
Keith Busch4cc09e22014-04-02 15:45:37 -060028#include <linux/hdreg.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040029#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050030#include <linux/init.h>
31#include <linux/interrupt.h>
32#include <linux/io.h>
33#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050034#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050035#include <linux/kernel.h>
36#include <linux/mm.h>
37#include <linux/module.h>
38#include <linux/moduleparam.h>
39#include <linux/pci.h>
Keith Busch42f61422014-03-24 10:46:25 -060040#include <linux/percpu.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050041#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040042#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050043#include <linux/sched.h>
44#include <linux/slab.h>
45#include <linux/types.h>
Vishal Verma5d0f6132013-03-04 18:40:58 -070046#include <scsi/sg.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090047#include <asm-generic/io-64-nonatomic-lo-hi.h>
48
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050049#define NVME_Q_DEPTH 1024
50#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
51#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Matthew Wilcoxe85248e2011-02-06 18:30:16 -050052#define ADMIN_TIMEOUT (60 * HZ)
Keith Buschedd10d32014-04-03 16:45:23 -060053#define IOD_TIMEOUT (4 * NVME_IO_TIMEOUT)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050054
Keith Buschb3550842014-04-04 11:43:36 -060055unsigned char io_timeout = 30;
56module_param(io_timeout, byte, 0644);
57MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
58
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050059static int nvme_major;
60module_param(nvme_major, int, 0);
61
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050062static int use_threaded_interrupts;
63module_param(use_threaded_interrupts, int, 0);
64
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050065static DEFINE_SPINLOCK(dev_list_lock);
66static LIST_HEAD(dev_list);
67static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070068static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060069static wait_queue_head_t nvme_kthread_wait;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050070
Keith Buschd4b4ff82013-12-10 13:10:37 -070071static void nvme_reset_failed_dev(struct work_struct *ws);
72
Keith Busch4d115422013-12-10 13:10:40 -070073struct async_cmd_info {
74 struct kthread_work work;
75 struct kthread_worker *worker;
76 u32 result;
77 int status;
78 void *ctx;
79};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050080
81/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050082 * An NVM Express queue. Each device has at least two (one for admin
83 * commands and one for I/O commands).
84 */
85struct nvme_queue {
Keith Busch5a92e702014-02-21 14:13:44 -070086 struct rcu_head r_head;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050087 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -050088 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -050089 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050090 spinlock_t q_lock;
91 struct nvme_command *sq_cmds;
92 volatile struct nvme_completion *cqes;
93 dma_addr_t sq_dma_addr;
94 dma_addr_t cq_dma_addr;
95 wait_queue_head_t sq_full;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050096 wait_queue_t sq_cong_wait;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050097 struct bio_list sq_cong;
Keith Buschedd10d32014-04-03 16:45:23 -060098 struct list_head iod_bio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050099 u32 __iomem *q_db;
100 u16 q_depth;
101 u16 cq_vector;
102 u16 sq_head;
103 u16 sq_tail;
104 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700105 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400106 u8 cq_phase;
107 u8 cqe_seen;
Keith Busch22404272013-07-15 15:02:20 -0600108 u8 q_suspended;
Keith Busch42f61422014-03-24 10:46:25 -0600109 cpumask_var_t cpu_mask;
Keith Busch4d115422013-12-10 13:10:40 -0700110 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500111 unsigned long cmdid_data[];
112};
113
114/*
115 * Check we didin't inadvertently grow the command struct
116 */
117static inline void _nvme_check_size(void)
118{
119 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
120 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
121 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
122 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
123 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400124 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700125 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500126 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
127 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
128 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
129 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600130 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500131}
132
Keith Buschedd10d32014-04-03 16:45:23 -0600133typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400134 struct nvme_completion *);
135
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500136struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400137 nvme_completion_fn fn;
138 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500139 unsigned long timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700140 int aborted;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500141};
142
143static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
144{
145 return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
146}
147
Keith Busch22404272013-07-15 15:02:20 -0600148static unsigned nvme_queue_extra(int depth)
149{
150 return DIV_ROUND_UP(depth, 8) + (depth * sizeof(struct nvme_cmd_info));
151}
152
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500153/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400154 * alloc_cmdid() - Allocate a Command ID
155 * @nvmeq: The queue that will be used for this command
156 * @ctx: A pointer that will be passed to the handler
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400157 * @handler: The function to call on completion
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500158 *
159 * Allocate a Command ID for a queue. The data passed in will
160 * be passed to the completion handler. This is implemented by using
161 * the bottom two bits of the ctx pointer to store the handler ID.
162 * Passing in a pointer that's not 4-byte aligned will cause a BUG.
163 * We can change this if it becomes a problem.
Matthew Wilcox184d2942011-05-11 21:36:38 -0400164 *
165 * May be called with local interrupts disabled and the q_lock held,
166 * or with interrupts enabled and no locks held.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500167 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400168static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx,
169 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500170{
Matthew Wilcoxe6d15f72011-02-24 08:49:41 -0500171 int depth = nvmeq->q_depth - 1;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500172 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500173 int cmdid;
174
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500175 do {
176 cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
177 if (cmdid >= depth)
178 return -EBUSY;
179 } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
180
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400181 info[cmdid].fn = handler;
182 info[cmdid].ctx = ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500183 info[cmdid].timeout = jiffies + timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700184 info[cmdid].aborted = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500185 return cmdid;
186}
187
188static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400189 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500190{
191 int cmdid;
192 wait_event_killable(nvmeq->sq_full,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500193 (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500194 return (cmdid < 0) ? -EINTR : cmdid;
195}
196
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400197/* Special values must be less than 0x1000 */
198#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500199#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
200#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
201#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500202#define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE)
Keith Buschc30341d2013-12-10 13:10:38 -0700203#define CMD_CTX_ABORT (0x31C + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500204
Keith Buschedd10d32014-04-03 16:45:23 -0600205static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400206 struct nvme_completion *cqe)
207{
208 if (ctx == CMD_CTX_CANCELLED)
209 return;
210 if (ctx == CMD_CTX_FLUSH)
211 return;
Keith Buschc30341d2013-12-10 13:10:38 -0700212 if (ctx == CMD_CTX_ABORT) {
Keith Buschedd10d32014-04-03 16:45:23 -0600213 ++nvmeq->dev->abort_limit;
Keith Buschc30341d2013-12-10 13:10:38 -0700214 return;
215 }
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400216 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600217 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400218 "completed id %d twice on queue %d\n",
219 cqe->command_id, le16_to_cpup(&cqe->sq_id));
220 return;
221 }
222 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600223 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400224 "invalid id %d completed on queue %d\n",
225 cqe->command_id, le16_to_cpup(&cqe->sq_id));
226 return;
227 }
228
Keith Buschedd10d32014-04-03 16:45:23 -0600229 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400230}
231
Keith Buschedd10d32014-04-03 16:45:23 -0600232static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700233 struct nvme_completion *cqe)
234{
235 struct async_cmd_info *cmdinfo = ctx;
236 cmdinfo->result = le32_to_cpup(&cqe->result);
237 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
238 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
239}
240
Matthew Wilcox184d2942011-05-11 21:36:38 -0400241/*
242 * Called with local interrupts disabled and the q_lock held. May not sleep.
243 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400244static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid,
245 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500246{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400247 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500248 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500249
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400250 if (cmdid >= nvmeq->q_depth) {
251 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500252 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400253 }
Keith Busch859361a2012-08-02 14:05:59 -0600254 if (fn)
255 *fn = info[cmdid].fn;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400256 ctx = info[cmdid].ctx;
257 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500258 info[cmdid].ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500259 clear_bit(cmdid, nvmeq->cmdid_data);
260 wake_up(&nvmeq->sq_full);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400261 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500262}
263
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400264static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
265 nvme_completion_fn *fn)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500266{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400267 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500268 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400269 if (fn)
270 *fn = info[cmdid].fn;
271 ctx = info[cmdid].ctx;
272 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500273 info[cmdid].ctx = CMD_CTX_CANCELLED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400274 return ctx;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500275}
276
Keith Busch5a92e702014-02-21 14:13:44 -0700277static struct nvme_queue *raw_nvmeq(struct nvme_dev *dev, int qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500278{
Keith Busch5a92e702014-02-21 14:13:44 -0700279 return rcu_dereference_raw(dev->queues[qid]);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500280}
281
Keith Busch4f5099a2014-03-03 16:39:13 -0700282static struct nvme_queue *get_nvmeq(struct nvme_dev *dev) __acquires(RCU)
Keith Busch5a92e702014-02-21 14:13:44 -0700283{
Keith Busch42f61422014-03-24 10:46:25 -0600284 unsigned queue_id = get_cpu_var(*dev->io_queue);
Keith Busch5a92e702014-02-21 14:13:44 -0700285 rcu_read_lock();
Keith Busch42f61422014-03-24 10:46:25 -0600286 return rcu_dereference(dev->queues[queue_id]);
Keith Busch5a92e702014-02-21 14:13:44 -0700287}
288
Keith Busch4f5099a2014-03-03 16:39:13 -0700289static void put_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500290{
Keith Busch5a92e702014-02-21 14:13:44 -0700291 rcu_read_unlock();
Keith Busch42f61422014-03-24 10:46:25 -0600292 put_cpu_var(nvmeq->dev->io_queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500293}
294
Keith Busch4f5099a2014-03-03 16:39:13 -0700295static struct nvme_queue *lock_nvmeq(struct nvme_dev *dev, int q_idx)
296 __acquires(RCU)
297{
298 rcu_read_lock();
299 return rcu_dereference(dev->queues[q_idx]);
300}
301
302static void unlock_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
303{
304 rcu_read_unlock();
305}
306
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500307/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400308 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500309 * @nvmeq: The queue to use
310 * @cmd: The command to send
311 *
312 * Safe to use from interrupt context
313 */
314static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
315{
316 unsigned long flags;
317 u16 tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500318 spin_lock_irqsave(&nvmeq->q_lock, flags);
Keith Busch4f5099a2014-03-03 16:39:13 -0700319 if (nvmeq->q_suspended) {
320 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
321 return -EBUSY;
322 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500323 tail = nvmeq->sq_tail;
324 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500325 if (++tail == nvmeq->q_depth)
326 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500327 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500328 nvmeq->sq_tail = tail;
329 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
330
331 return 0;
332}
333
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500334static __le64 **iod_list(struct nvme_iod *iod)
335{
336 return ((void *)iod) + iod->offset;
337}
338
339/*
340 * Will slightly overestimate the number of pages needed. This is OK
341 * as it only leads to a small amount of wasted memory for the lifetime of
342 * the I/O.
343 */
344static int nvme_npages(unsigned size)
345{
346 unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE);
347 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
348}
349
350static struct nvme_iod *
351nvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp)
352{
353 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
354 sizeof(__le64 *) * nvme_npages(nbytes) +
355 sizeof(struct scatterlist) * nseg, gfp);
356
357 if (iod) {
358 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
359 iod->npages = -1;
360 iod->length = nbytes;
Keith Busch2b196032012-11-06 11:59:23 -0700361 iod->nents = 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600362 iod->first_dma = 0ULL;
Keith Busch61982212013-05-29 15:59:39 -0600363 iod->start_time = jiffies;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500364 }
365
366 return iod;
367}
368
Vishal Verma5d0f6132013-03-04 18:40:58 -0700369void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500370{
371 const int last_prp = PAGE_SIZE / 8 - 1;
372 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500373 __le64 **list = iod_list(iod);
374 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500375
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500376 if (iod->npages == 0)
377 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
378 for (i = 0; i < iod->npages; i++) {
379 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500380 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500381 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500382 prp_dma = next_prp_dma;
383 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500384 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500385}
386
Keith Busch61982212013-05-29 15:59:39 -0600387static void nvme_start_io_acct(struct bio *bio)
388{
389 struct gendisk *disk = bio->bi_bdev->bd_disk;
390 const int rw = bio_data_dir(bio);
391 int cpu = part_stat_lock();
392 part_round_stats(cpu, &disk->part0);
393 part_stat_inc(cpu, &disk->part0, ios[rw]);
394 part_stat_add(cpu, &disk->part0, sectors[rw], bio_sectors(bio));
395 part_inc_in_flight(&disk->part0, rw);
396 part_stat_unlock();
397}
398
399static void nvme_end_io_acct(struct bio *bio, unsigned long start_time)
400{
401 struct gendisk *disk = bio->bi_bdev->bd_disk;
402 const int rw = bio_data_dir(bio);
403 unsigned long duration = jiffies - start_time;
404 int cpu = part_stat_lock();
405 part_stat_add(cpu, &disk->part0, ticks[rw], duration);
406 part_round_stats(cpu, &disk->part0);
407 part_dec_in_flight(&disk->part0, rw);
408 part_stat_unlock();
409}
410
Keith Buschedd10d32014-04-03 16:45:23 -0600411static void bio_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500412 struct nvme_completion *cqe)
413{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500414 struct nvme_iod *iod = ctx;
415 struct bio *bio = iod->private;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500416 u16 status = le16_to_cpup(&cqe->status) >> 1;
417
Keith Buschedd10d32014-04-03 16:45:23 -0600418 if (unlikely(status)) {
419 if (!(status & NVME_SC_DNR ||
420 bio->bi_rw & REQ_FAILFAST_MASK) &&
421 (jiffies - iod->start_time) < IOD_TIMEOUT) {
422 if (!waitqueue_active(&nvmeq->sq_full))
423 add_wait_queue(&nvmeq->sq_full,
424 &nvmeq->sq_cong_wait);
425 list_add_tail(&iod->node, &nvmeq->iod_bio);
426 wake_up(&nvmeq->sq_full);
427 return;
428 }
429 }
Keith Busch9e59d092013-08-08 10:25:38 -0600430 if (iod->nents) {
Keith Buschedd10d32014-04-03 16:45:23 -0600431 dma_unmap_sg(nvmeq->q_dmadev, iod->sg, iod->nents,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500432 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busch9e59d092013-08-08 10:25:38 -0600433 nvme_end_io_acct(bio, iod->start_time);
434 }
Keith Buschedd10d32014-04-03 16:45:23 -0600435 nvme_free_iod(nvmeq->dev, iod);
Keith Busch427e9702013-04-09 11:59:32 -0600436 if (status)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500437 bio_endio(bio, -EIO);
Keith Busch427e9702013-04-09 11:59:32 -0600438 else
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500439 bio_endio(bio, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500440}
441
Matthew Wilcox184d2942011-05-11 21:36:38 -0400442/* length is in bytes. gfp flags indicates whether we may sleep. */
Keith Buschedd10d32014-04-03 16:45:23 -0600443int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod, int total_len,
444 gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500445{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500446 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500447 int length = total_len;
448 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500449 int dma_len = sg_dma_len(sg);
450 u64 dma_addr = sg_dma_address(sg);
451 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500452 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500453 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500454 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500455 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500456
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500457 length -= (PAGE_SIZE - offset);
458 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500459 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500460
461 dma_len -= (PAGE_SIZE - offset);
462 if (dma_len) {
463 dma_addr += (PAGE_SIZE - offset);
464 } else {
465 sg = sg_next(sg);
466 dma_addr = sg_dma_address(sg);
467 dma_len = sg_dma_len(sg);
468 }
469
470 if (length <= PAGE_SIZE) {
Keith Buschedd10d32014-04-03 16:45:23 -0600471 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500472 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500473 }
474
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500475 nprps = DIV_ROUND_UP(length, PAGE_SIZE);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500476 if (nprps <= (256 / 8)) {
477 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500478 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500479 } else {
480 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500481 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500482 }
483
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400484 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
485 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600486 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500487 iod->npages = -1;
488 return (total_len - length) + PAGE_SIZE;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400489 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500490 list[0] = prp_list;
491 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500492 i = 0;
493 for (;;) {
Matthew Wilcox7523d832011-03-16 16:43:40 -0400494 if (i == PAGE_SIZE / 8) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500495 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400496 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500497 if (!prp_list)
498 return total_len - length;
499 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400500 prp_list[0] = old_prp_list[i - 1];
501 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
502 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500503 }
504 prp_list[i++] = cpu_to_le64(dma_addr);
505 dma_len -= PAGE_SIZE;
506 dma_addr += PAGE_SIZE;
507 length -= PAGE_SIZE;
508 if (length <= 0)
509 break;
510 if (dma_len > 0)
511 continue;
512 BUG_ON(dma_len < 0);
513 sg = sg_next(sg);
514 dma_addr = sg_dma_address(sg);
515 dma_len = sg_dma_len(sg);
516 }
517
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500518 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500519}
520
Keith Busch427e9702013-04-09 11:59:32 -0600521static int nvme_split_and_submit(struct bio *bio, struct nvme_queue *nvmeq,
Kent Overstreet20d01892013-11-23 18:21:01 -0800522 int len)
Keith Busch427e9702013-04-09 11:59:32 -0600523{
Kent Overstreet20d01892013-11-23 18:21:01 -0800524 struct bio *split = bio_split(bio, len >> 9, GFP_ATOMIC, NULL);
525 if (!split)
Keith Busch427e9702013-04-09 11:59:32 -0600526 return -ENOMEM;
527
Kent Overstreet20d01892013-11-23 18:21:01 -0800528 bio_chain(split, bio);
529
Keith Buschedd10d32014-04-03 16:45:23 -0600530 if (!waitqueue_active(&nvmeq->sq_full))
Keith Busch427e9702013-04-09 11:59:32 -0600531 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
Kent Overstreet20d01892013-11-23 18:21:01 -0800532 bio_list_add(&nvmeq->sq_cong, split);
533 bio_list_add(&nvmeq->sq_cong, bio);
Keith Buschedd10d32014-04-03 16:45:23 -0600534 wake_up(&nvmeq->sq_full);
Keith Busch427e9702013-04-09 11:59:32 -0600535
536 return 0;
537}
538
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500539/* NVMe scatterlists require no holes in the virtual address */
540#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
541 (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
542
Keith Busch427e9702013-04-09 11:59:32 -0600543static int nvme_map_bio(struct nvme_queue *nvmeq, struct nvme_iod *iod,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500544 struct bio *bio, enum dma_data_direction dma_dir, int psegs)
545{
Kent Overstreet79886132013-11-23 17:19:00 -0800546 struct bio_vec bvec, bvprv;
547 struct bvec_iter iter;
Matthew Wilcox76830842011-02-10 13:55:39 -0500548 struct scatterlist *sg = NULL;
Kent Overstreet79886132013-11-23 17:19:00 -0800549 int length = 0, nsegs = 0, split_len = bio->bi_iter.bi_size;
550 int first = 1;
Keith Busch159b67d2013-04-09 17:13:20 -0600551
552 if (nvmeq->dev->stripe_size)
553 split_len = nvmeq->dev->stripe_size -
Kent Overstreet4f024f32013-10-11 15:44:27 -0700554 ((bio->bi_iter.bi_sector << 9) &
555 (nvmeq->dev->stripe_size - 1));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500556
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500557 sg_init_table(iod->sg, psegs);
Kent Overstreet79886132013-11-23 17:19:00 -0800558 bio_for_each_segment(bvec, bio, iter) {
559 if (!first && BIOVEC_PHYS_MERGEABLE(&bvprv, &bvec)) {
560 sg->length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500561 } else {
Kent Overstreet79886132013-11-23 17:19:00 -0800562 if (!first && BIOVEC_NOT_VIRT_MERGEABLE(&bvprv, &bvec))
563 return nvme_split_and_submit(bio, nvmeq,
Kent Overstreet20d01892013-11-23 18:21:01 -0800564 length);
Keith Busch427e9702013-04-09 11:59:32 -0600565
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500566 sg = sg ? sg + 1 : iod->sg;
Kent Overstreet79886132013-11-23 17:19:00 -0800567 sg_set_page(sg, bvec.bv_page,
568 bvec.bv_len, bvec.bv_offset);
Matthew Wilcox76830842011-02-10 13:55:39 -0500569 nsegs++;
570 }
Keith Busch159b67d2013-04-09 17:13:20 -0600571
Kent Overstreet79886132013-11-23 17:19:00 -0800572 if (split_len - length < bvec.bv_len)
Kent Overstreet20d01892013-11-23 18:21:01 -0800573 return nvme_split_and_submit(bio, nvmeq, split_len);
Kent Overstreet79886132013-11-23 17:19:00 -0800574 length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500575 bvprv = bvec;
Kent Overstreet79886132013-11-23 17:19:00 -0800576 first = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500577 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500578 iod->nents = nsegs;
Matthew Wilcox76830842011-02-10 13:55:39 -0500579 sg_mark_end(sg);
Keith Busch427e9702013-04-09 11:59:32 -0600580 if (dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir) == 0)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500581 return -ENOMEM;
Keith Busch427e9702013-04-09 11:59:32 -0600582
Kent Overstreet4f024f32013-10-11 15:44:27 -0700583 BUG_ON(length != bio->bi_iter.bi_size);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500584 return length;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500585}
586
Keith Busch0e5e4f02012-11-09 16:33:05 -0700587static int nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
588 struct bio *bio, struct nvme_iod *iod, int cmdid)
589{
Keith Buschedd10d32014-04-03 16:45:23 -0600590 struct nvme_dsm_range *range =
591 (struct nvme_dsm_range *)iod_list(iod)[0];
Keith Busch0e5e4f02012-11-09 16:33:05 -0700592 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
593
Keith Busch0e5e4f02012-11-09 16:33:05 -0700594 range->cattr = cpu_to_le32(0);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700595 range->nlb = cpu_to_le32(bio->bi_iter.bi_size >> ns->lba_shift);
596 range->slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700597
598 memset(cmnd, 0, sizeof(*cmnd));
599 cmnd->dsm.opcode = nvme_cmd_dsm;
600 cmnd->dsm.command_id = cmdid;
601 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
602 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
603 cmnd->dsm.nr = 0;
604 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
605
606 if (++nvmeq->sq_tail == nvmeq->q_depth)
607 nvmeq->sq_tail = 0;
608 writel(nvmeq->sq_tail, nvmeq->q_db);
609
610 return 0;
611}
612
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500613static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
614 int cmdid)
615{
616 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
617
618 memset(cmnd, 0, sizeof(*cmnd));
619 cmnd->common.opcode = nvme_cmd_flush;
620 cmnd->common.command_id = cmdid;
621 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
622
623 if (++nvmeq->sq_tail == nvmeq->q_depth)
624 nvmeq->sq_tail = 0;
625 writel(nvmeq->sq_tail, nvmeq->q_db);
626
627 return 0;
628}
629
Vishal Verma5d0f6132013-03-04 18:40:58 -0700630int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500631{
632 int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH,
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500633 special_completion, NVME_IO_TIMEOUT);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500634 if (unlikely(cmdid < 0))
635 return cmdid;
636
637 return nvme_submit_flush(nvmeq, ns, cmdid);
638}
639
Keith Buschedd10d32014-04-03 16:45:23 -0600640static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500641{
Keith Buschedd10d32014-04-03 16:45:23 -0600642 struct bio *bio = iod->private;
643 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500644 struct nvme_command *cmnd;
Keith Buschedd10d32014-04-03 16:45:23 -0600645 int cmdid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500646 u16 control;
647 u32 dsmgmt;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500648
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500649 cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500650 if (unlikely(cmdid < 0))
Keith Buschedd10d32014-04-03 16:45:23 -0600651 return cmdid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500652
Keith Buschedd10d32014-04-03 16:45:23 -0600653 if (bio->bi_rw & REQ_DISCARD)
654 return nvme_submit_discard(nvmeq, ns, bio, iod, cmdid);
655 if ((bio->bi_rw & REQ_FLUSH) && !iod->nents)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500656 return nvme_submit_flush(nvmeq, ns, cmdid);
657
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500658 control = 0;
659 if (bio->bi_rw & REQ_FUA)
660 control |= NVME_RW_FUA;
661 if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
662 control |= NVME_RW_LR;
663
664 dsmgmt = 0;
665 if (bio->bi_rw & REQ_RAHEAD)
666 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
667
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500668 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500669 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500670
Keith Buschedd10d32014-04-03 16:45:23 -0600671 cmnd->rw.opcode = bio_data_dir(bio) ? nvme_cmd_write : nvme_cmd_read;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500672 cmnd->rw.command_id = cmdid;
673 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Keith Buschedd10d32014-04-03 16:45:23 -0600674 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
675 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700676 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Keith Buschedd10d32014-04-03 16:45:23 -0600677 cmnd->rw.length =
678 cpu_to_le16((bio->bi_iter.bi_size >> ns->lba_shift) - 1);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500679 cmnd->rw.control = cpu_to_le16(control);
680 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500681
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500682 if (++nvmeq->sq_tail == nvmeq->q_depth)
683 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500684 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500685
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500686 return 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600687}
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500688
Keith Buschedd10d32014-04-03 16:45:23 -0600689/*
690 * Called with local interrupts disabled and the q_lock held. May not sleep.
691 */
692static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
693 struct bio *bio)
694{
695 struct nvme_iod *iod;
696 int psegs = bio_phys_segments(ns->queue, bio);
697 int result;
698
699 if ((bio->bi_rw & REQ_FLUSH) && psegs) {
700 result = nvme_submit_flush_data(nvmeq, ns);
701 if (result)
702 return result;
703 }
704
705 iod = nvme_alloc_iod(psegs, bio->bi_iter.bi_size, GFP_ATOMIC);
706 if (!iod)
707 return -ENOMEM;
708
709 iod->private = bio;
710 if (bio->bi_rw & REQ_DISCARD) {
711 void *range;
712 /*
713 * We reuse the small pool to allocate the 16-byte range here
714 * as it is not worth having a special pool for these or
715 * additional cases to handle freeing the iod.
716 */
717 range = dma_pool_alloc(nvmeq->dev->prp_small_pool,
718 GFP_ATOMIC,
719 &iod->first_dma);
720 if (!range) {
721 result = -ENOMEM;
722 goto free_iod;
723 }
724 iod_list(iod)[0] = (__le64 *)range;
725 iod->npages = 0;
726 } else if (psegs) {
727 result = nvme_map_bio(nvmeq, iod, bio,
728 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE,
729 psegs);
730 if (result <= 0)
731 goto free_iod;
732 if (nvme_setup_prps(nvmeq->dev, iod, result, GFP_ATOMIC) !=
733 result) {
734 result = -ENOMEM;
735 goto free_iod;
736 }
737 nvme_start_io_acct(bio);
738 }
739 if (unlikely(nvme_submit_iod(nvmeq, iod))) {
740 if (!waitqueue_active(&nvmeq->sq_full))
741 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
742 list_add_tail(&iod->node, &nvmeq->iod_bio);
743 }
744 return 0;
745
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500746 free_iod:
747 nvme_free_iod(nvmeq->dev, iod);
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500748 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500749}
750
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400751static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500752{
Matthew Wilcox82123462011-01-20 13:24:06 -0500753 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500754
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500755 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500756 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500757
758 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400759 void *ctx;
760 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500761 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500762 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500763 break;
764 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
765 if (++head == nvmeq->q_depth) {
766 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500767 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500768 }
769
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400770 ctx = free_cmdid(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600771 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500772 }
773
774 /* If the controller ignores the cq head doorbell and continuously
775 * writes to the queue, it is theoretically possible to wrap around
776 * the queue twice and mistakenly return IRQ_NONE. Linux only
777 * requires that 0.1% of your interrupts are handled, so this isn't
778 * a big problem.
779 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500780 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400781 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500782
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800783 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500784 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500785 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500786
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400787 nvmeq->cqe_seen = 1;
788 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500789}
790
Matthew Wilcox7d822452013-06-24 12:03:57 -0400791static void nvme_make_request(struct request_queue *q, struct bio *bio)
792{
793 struct nvme_ns *ns = q->queuedata;
794 struct nvme_queue *nvmeq = get_nvmeq(ns->dev);
795 int result = -EBUSY;
796
Keith Buschcd638942013-07-15 15:02:23 -0600797 if (!nvmeq) {
798 put_nvmeq(NULL);
799 bio_endio(bio, -EIO);
800 return;
801 }
802
Matthew Wilcox7d822452013-06-24 12:03:57 -0400803 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -0600804 if (!nvmeq->q_suspended && bio_list_empty(&nvmeq->sq_cong))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400805 result = nvme_submit_bio_queue(nvmeq, ns, bio);
806 if (unlikely(result)) {
Keith Buschedd10d32014-04-03 16:45:23 -0600807 if (!waitqueue_active(&nvmeq->sq_full))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400808 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
809 bio_list_add(&nvmeq->sq_cong, bio);
810 }
811
812 nvme_process_cq(nvmeq);
813 spin_unlock_irq(&nvmeq->q_lock);
814 put_nvmeq(nvmeq);
815}
816
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500817static irqreturn_t nvme_irq(int irq, void *data)
818{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500819 irqreturn_t result;
820 struct nvme_queue *nvmeq = data;
821 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400822 nvme_process_cq(nvmeq);
823 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
824 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500825 spin_unlock(&nvmeq->q_lock);
826 return result;
827}
828
829static irqreturn_t nvme_irq_check(int irq, void *data)
830{
831 struct nvme_queue *nvmeq = data;
832 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
833 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
834 return IRQ_NONE;
835 return IRQ_WAKE_THREAD;
836}
837
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500838static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
839{
840 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400841 cancel_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500842 spin_unlock_irq(&nvmeq->q_lock);
843}
844
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400845struct sync_cmd_info {
846 struct task_struct *task;
847 u32 result;
848 int status;
849};
850
Keith Buschedd10d32014-04-03 16:45:23 -0600851static void sync_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400852 struct nvme_completion *cqe)
853{
854 struct sync_cmd_info *cmdinfo = ctx;
855 cmdinfo->result = le32_to_cpup(&cqe->result);
856 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
857 wake_up_process(cmdinfo->task);
858}
859
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500860/*
861 * Returns 0 on success. If the result is negative, it's a Linux error code;
862 * if the result is positive, it's an NVM Express status code
863 */
Keith Busch4f5099a2014-03-03 16:39:13 -0700864static int nvme_submit_sync_cmd(struct nvme_dev *dev, int q_idx,
865 struct nvme_command *cmd,
Vishal Verma5d0f6132013-03-04 18:40:58 -0700866 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500867{
Keith Busch4f5099a2014-03-03 16:39:13 -0700868 int cmdid, ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500869 struct sync_cmd_info cmdinfo;
Keith Busch4f5099a2014-03-03 16:39:13 -0700870 struct nvme_queue *nvmeq;
871
872 nvmeq = lock_nvmeq(dev, q_idx);
873 if (!nvmeq) {
874 unlock_nvmeq(nvmeq);
875 return -ENODEV;
876 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500877
878 cmdinfo.task = current;
879 cmdinfo.status = -EINTR;
880
Keith Busch4f5099a2014-03-03 16:39:13 -0700881 cmdid = alloc_cmdid(nvmeq, &cmdinfo, sync_completion, timeout);
882 if (cmdid < 0) {
883 unlock_nvmeq(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500884 return cmdid;
Keith Busch4f5099a2014-03-03 16:39:13 -0700885 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500886 cmd->common.command_id = cmdid;
887
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500888 set_current_state(TASK_KILLABLE);
Keith Busch4f5099a2014-03-03 16:39:13 -0700889 ret = nvme_submit_cmd(nvmeq, cmd);
890 if (ret) {
891 free_cmdid(nvmeq, cmdid, NULL);
892 unlock_nvmeq(nvmeq);
893 set_current_state(TASK_RUNNING);
894 return ret;
895 }
896 unlock_nvmeq(nvmeq);
Keith Busch78f8d252013-04-19 14:11:06 -0600897 schedule_timeout(timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500898
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500899 if (cmdinfo.status == -EINTR) {
Keith Busch4f5099a2014-03-03 16:39:13 -0700900 nvmeq = lock_nvmeq(dev, q_idx);
901 if (nvmeq)
902 nvme_abort_command(nvmeq, cmdid);
903 unlock_nvmeq(nvmeq);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500904 return -EINTR;
905 }
906
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500907 if (result)
908 *result = cmdinfo.result;
909
910 return cmdinfo.status;
911}
912
Keith Busch4d115422013-12-10 13:10:40 -0700913static int nvme_submit_async_cmd(struct nvme_queue *nvmeq,
914 struct nvme_command *cmd,
915 struct async_cmd_info *cmdinfo, unsigned timeout)
916{
917 int cmdid;
918
919 cmdid = alloc_cmdid_killable(nvmeq, cmdinfo, async_completion, timeout);
920 if (cmdid < 0)
921 return cmdid;
922 cmdinfo->status = -EINTR;
923 cmd->common.command_id = cmdid;
Keith Busch4f5099a2014-03-03 16:39:13 -0700924 return nvme_submit_cmd(nvmeq, cmd);
Keith Busch4d115422013-12-10 13:10:40 -0700925}
926
Vishal Verma5d0f6132013-03-04 18:40:58 -0700927int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500928 u32 *result)
929{
Keith Busch4f5099a2014-03-03 16:39:13 -0700930 return nvme_submit_sync_cmd(dev, 0, cmd, result, ADMIN_TIMEOUT);
931}
932
933int nvme_submit_io_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
934 u32 *result)
935{
936 return nvme_submit_sync_cmd(dev, smp_processor_id() + 1, cmd, result,
937 NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500938}
939
Keith Busch4d115422013-12-10 13:10:40 -0700940static int nvme_submit_admin_cmd_async(struct nvme_dev *dev,
941 struct nvme_command *cmd, struct async_cmd_info *cmdinfo)
942{
Keith Busch5a92e702014-02-21 14:13:44 -0700943 return nvme_submit_async_cmd(raw_nvmeq(dev, 0), cmd, cmdinfo,
Keith Busch4d115422013-12-10 13:10:40 -0700944 ADMIN_TIMEOUT);
945}
946
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500947static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
948{
949 int status;
950 struct nvme_command c;
951
952 memset(&c, 0, sizeof(c));
953 c.delete_queue.opcode = opcode;
954 c.delete_queue.qid = cpu_to_le16(id);
955
956 status = nvme_submit_admin_cmd(dev, &c, NULL);
957 if (status)
958 return -EIO;
959 return 0;
960}
961
962static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
963 struct nvme_queue *nvmeq)
964{
965 int status;
966 struct nvme_command c;
967 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
968
969 memset(&c, 0, sizeof(c));
970 c.create_cq.opcode = nvme_admin_create_cq;
971 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
972 c.create_cq.cqid = cpu_to_le16(qid);
973 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
974 c.create_cq.cq_flags = cpu_to_le16(flags);
975 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
976
977 status = nvme_submit_admin_cmd(dev, &c, NULL);
978 if (status)
979 return -EIO;
980 return 0;
981}
982
983static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
984 struct nvme_queue *nvmeq)
985{
986 int status;
987 struct nvme_command c;
988 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
989
990 memset(&c, 0, sizeof(c));
991 c.create_sq.opcode = nvme_admin_create_sq;
992 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
993 c.create_sq.sqid = cpu_to_le16(qid);
994 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
995 c.create_sq.sq_flags = cpu_to_le16(flags);
996 c.create_sq.cqid = cpu_to_le16(qid);
997
998 status = nvme_submit_admin_cmd(dev, &c, NULL);
999 if (status)
1000 return -EIO;
1001 return 0;
1002}
1003
1004static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1005{
1006 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1007}
1008
1009static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1010{
1011 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1012}
1013
Vishal Verma5d0f6132013-03-04 18:40:58 -07001014int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001015 dma_addr_t dma_addr)
1016{
1017 struct nvme_command c;
1018
1019 memset(&c, 0, sizeof(c));
1020 c.identify.opcode = nvme_admin_identify;
1021 c.identify.nsid = cpu_to_le32(nsid);
1022 c.identify.prp1 = cpu_to_le64(dma_addr);
1023 c.identify.cns = cpu_to_le32(cns);
1024
1025 return nvme_submit_admin_cmd(dev, &c, NULL);
1026}
1027
Vishal Verma5d0f6132013-03-04 18:40:58 -07001028int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -06001029 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001030{
1031 struct nvme_command c;
1032
1033 memset(&c, 0, sizeof(c));
1034 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -06001035 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001036 c.features.prp1 = cpu_to_le64(dma_addr);
1037 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001038
Keith Busch08df1e02012-09-21 10:52:13 -06001039 return nvme_submit_admin_cmd(dev, &c, result);
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001040}
1041
Vishal Verma5d0f6132013-03-04 18:40:58 -07001042int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
1043 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001044{
1045 struct nvme_command c;
1046
1047 memset(&c, 0, sizeof(c));
1048 c.features.opcode = nvme_admin_set_features;
1049 c.features.prp1 = cpu_to_le64(dma_addr);
1050 c.features.fid = cpu_to_le32(fid);
1051 c.features.dword11 = cpu_to_le32(dword11);
1052
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001053 return nvme_submit_admin_cmd(dev, &c, result);
1054}
1055
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001056/**
Keith Buschc30341d2013-12-10 13:10:38 -07001057 * nvme_abort_cmd - Attempt aborting a command
1058 * @cmdid: Command id of a timed out IO
1059 * @queue: The queue with timed out IO
1060 *
1061 * Schedule controller reset if the command was already aborted once before and
1062 * still hasn't been returned to the driver, or if this is the admin queue.
1063 */
1064static void nvme_abort_cmd(int cmdid, struct nvme_queue *nvmeq)
1065{
1066 int a_cmdid;
1067 struct nvme_command cmd;
1068 struct nvme_dev *dev = nvmeq->dev;
1069 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Keith Busch5a92e702014-02-21 14:13:44 -07001070 struct nvme_queue *adminq;
Keith Buschc30341d2013-12-10 13:10:38 -07001071
1072 if (!nvmeq->qid || info[cmdid].aborted) {
1073 if (work_busy(&dev->reset_work))
1074 return;
1075 list_del_init(&dev->node);
1076 dev_warn(&dev->pci_dev->dev,
1077 "I/O %d QID %d timeout, reset controller\n", cmdid,
1078 nvmeq->qid);
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05001079 PREPARE_WORK(&dev->reset_work, nvme_reset_failed_dev);
Keith Buschc30341d2013-12-10 13:10:38 -07001080 queue_work(nvme_workq, &dev->reset_work);
1081 return;
1082 }
1083
1084 if (!dev->abort_limit)
1085 return;
1086
Keith Busch5a92e702014-02-21 14:13:44 -07001087 adminq = rcu_dereference(dev->queues[0]);
1088 a_cmdid = alloc_cmdid(adminq, CMD_CTX_ABORT, special_completion,
Keith Buschc30341d2013-12-10 13:10:38 -07001089 ADMIN_TIMEOUT);
1090 if (a_cmdid < 0)
1091 return;
1092
1093 memset(&cmd, 0, sizeof(cmd));
1094 cmd.abort.opcode = nvme_admin_abort_cmd;
1095 cmd.abort.cid = cmdid;
1096 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
1097 cmd.abort.command_id = a_cmdid;
1098
1099 --dev->abort_limit;
1100 info[cmdid].aborted = 1;
1101 info[cmdid].timeout = jiffies + ADMIN_TIMEOUT;
1102
1103 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", cmdid,
1104 nvmeq->qid);
Keith Busch5a92e702014-02-21 14:13:44 -07001105 nvme_submit_cmd(adminq, &cmd);
Keith Buschc30341d2013-12-10 13:10:38 -07001106}
1107
1108/**
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001109 * nvme_cancel_ios - Cancel outstanding I/Os
1110 * @queue: The queue to cancel I/Os on
1111 * @timeout: True to only cancel I/Os which have timed out
1112 */
1113static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout)
1114{
1115 int depth = nvmeq->q_depth - 1;
1116 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
1117 unsigned long now = jiffies;
1118 int cmdid;
1119
1120 for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
1121 void *ctx;
1122 nvme_completion_fn fn;
1123 static struct nvme_completion cqe = {
Matthew Wilcoxaf2d9ca2013-04-16 15:18:30 -04001124 .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1),
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001125 };
1126
1127 if (timeout && !time_after(now, info[cmdid].timeout))
1128 continue;
Keith Busch053ab7022013-04-30 11:19:38 -06001129 if (info[cmdid].ctx == CMD_CTX_CANCELLED)
1130 continue;
Keith Buschc30341d2013-12-10 13:10:38 -07001131 if (timeout && nvmeq->dev->initialized) {
1132 nvme_abort_cmd(cmdid, nvmeq);
1133 continue;
1134 }
1135 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n", cmdid,
1136 nvmeq->qid);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001137 ctx = cancel_cmdid(nvmeq, cmdid, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -06001138 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001139 }
1140}
1141
Keith Busch5a92e702014-02-21 14:13:44 -07001142static void nvme_free_queue(struct rcu_head *r)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001143{
Keith Busch5a92e702014-02-21 14:13:44 -07001144 struct nvme_queue *nvmeq = container_of(r, struct nvme_queue, r_head);
1145
Keith Busch22404272013-07-15 15:02:20 -06001146 spin_lock_irq(&nvmeq->q_lock);
1147 while (bio_list_peek(&nvmeq->sq_cong)) {
1148 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1149 bio_endio(bio, -EIO);
1150 }
Keith Buschedd10d32014-04-03 16:45:23 -06001151 while (!list_empty(&nvmeq->iod_bio)) {
1152 static struct nvme_completion cqe = {
1153 .status = cpu_to_le16(
1154 (NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1),
1155 };
1156 struct nvme_iod *iod = list_first_entry(&nvmeq->iod_bio,
1157 struct nvme_iod,
1158 node);
1159 list_del(&iod->node);
1160 bio_completion(nvmeq, iod, &cqe);
1161 }
Keith Busch22404272013-07-15 15:02:20 -06001162 spin_unlock_irq(&nvmeq->q_lock);
1163
Matthew Wilcox9e866772012-08-03 13:55:56 -04001164 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1165 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1166 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1167 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
Keith Busch42f61422014-03-24 10:46:25 -06001168 if (nvmeq->qid)
1169 free_cpumask_var(nvmeq->cpu_mask);
Matthew Wilcox9e866772012-08-03 13:55:56 -04001170 kfree(nvmeq);
1171}
1172
Keith Buscha1a5ef92013-12-16 13:50:00 -05001173static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001174{
1175 int i;
1176
Keith Buscha1a5ef92013-12-16 13:50:00 -05001177 for (i = dev->queue_count - 1; i >= lowest; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07001178 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
1179 rcu_assign_pointer(dev->queues[i], NULL);
1180 call_rcu(&nvmeq->r_head, nvme_free_queue);
Keith Busch22404272013-07-15 15:02:20 -06001181 dev->queue_count--;
Keith Busch22404272013-07-15 15:02:20 -06001182 }
1183}
1184
Keith Busch4d115422013-12-10 13:10:40 -07001185/**
1186 * nvme_suspend_queue - put queue into suspended state
1187 * @nvmeq - queue to suspend
1188 *
1189 * Returns 1 if already suspended, 0 otherwise.
1190 */
1191static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001192{
Keith Busch4d115422013-12-10 13:10:40 -07001193 int vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001194
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001195 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001196 if (nvmeq->q_suspended) {
1197 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07001198 return 1;
Keith Busch32958742012-08-20 14:57:49 -06001199 }
Keith Busch22404272013-07-15 15:02:20 -06001200 nvmeq->q_suspended = 1;
Keith Busch42f61422014-03-24 10:46:25 -06001201 nvmeq->dev->online_queues--;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001202 spin_unlock_irq(&nvmeq->q_lock);
1203
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001204 irq_set_affinity_hint(vector, NULL);
1205 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001206
Keith Busch4d115422013-12-10 13:10:40 -07001207 return 0;
1208}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001209
Keith Busch4d115422013-12-10 13:10:40 -07001210static void nvme_clear_queue(struct nvme_queue *nvmeq)
1211{
Keith Busch22404272013-07-15 15:02:20 -06001212 spin_lock_irq(&nvmeq->q_lock);
1213 nvme_process_cq(nvmeq);
1214 nvme_cancel_ios(nvmeq, false);
1215 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001216}
1217
Keith Busch4d115422013-12-10 13:10:40 -07001218static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1219{
Keith Busch5a92e702014-02-21 14:13:44 -07001220 struct nvme_queue *nvmeq = raw_nvmeq(dev, qid);
Keith Busch4d115422013-12-10 13:10:40 -07001221
1222 if (!nvmeq)
1223 return;
1224 if (nvme_suspend_queue(nvmeq))
1225 return;
1226
Keith Busch0e53d182013-12-10 13:10:39 -07001227 /* Don't tell the adapter to delete the admin queue.
1228 * Don't tell a removed adapter to delete IO queues. */
1229 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001230 adapter_delete_sq(dev, qid);
1231 adapter_delete_cq(dev, qid);
1232 }
Keith Busch4d115422013-12-10 13:10:40 -07001233 nvme_clear_queue(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001234}
1235
1236static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
1237 int depth, int vector)
1238{
1239 struct device *dmadev = &dev->pci_dev->dev;
Keith Busch22404272013-07-15 15:02:20 -06001240 unsigned extra = nvme_queue_extra(depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001241 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
1242 if (!nvmeq)
1243 return NULL;
1244
1245 nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
1246 &nvmeq->cq_dma_addr, GFP_KERNEL);
1247 if (!nvmeq->cqes)
1248 goto free_nvmeq;
1249 memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
1250
1251 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
1252 &nvmeq->sq_dma_addr, GFP_KERNEL);
1253 if (!nvmeq->sq_cmds)
1254 goto free_cqdma;
1255
Keith Busch42f61422014-03-24 10:46:25 -06001256 if (qid && !zalloc_cpumask_var(&nvmeq->cpu_mask, GFP_KERNEL))
1257 goto free_sqdma;
1258
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001259 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001260 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001261 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1262 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001263 spin_lock_init(&nvmeq->q_lock);
1264 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001265 nvmeq->cq_phase = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001266 init_waitqueue_head(&nvmeq->sq_full);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001267 init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001268 bio_list_init(&nvmeq->sq_cong);
Keith Buschedd10d32014-04-03 16:45:23 -06001269 INIT_LIST_HEAD(&nvmeq->iod_bio);
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001270 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001271 nvmeq->q_depth = depth;
1272 nvmeq->cq_vector = vector;
Keith Buschc30341d2013-12-10 13:10:38 -07001273 nvmeq->qid = qid;
Keith Busch22404272013-07-15 15:02:20 -06001274 nvmeq->q_suspended = 1;
1275 dev->queue_count++;
Keith Busch5a92e702014-02-21 14:13:44 -07001276 rcu_assign_pointer(dev->queues[qid], nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001277
1278 return nvmeq;
1279
Keith Busch42f61422014-03-24 10:46:25 -06001280 free_sqdma:
1281 dma_free_coherent(dmadev, SQ_SIZE(depth), (void *)nvmeq->sq_cmds,
1282 nvmeq->sq_dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001283 free_cqdma:
Keith Busch68b8eca2013-05-01 13:07:47 -06001284 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001285 nvmeq->cq_dma_addr);
1286 free_nvmeq:
1287 kfree(nvmeq);
1288 return NULL;
1289}
1290
Matthew Wilcox30010822011-01-20 09:10:15 -05001291static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1292 const char *name)
1293{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001294 if (use_threaded_interrupts)
1295 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001296 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001297 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001298 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001299 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001300}
1301
Keith Busch22404272013-07-15 15:02:20 -06001302static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001303{
Keith Busch22404272013-07-15 15:02:20 -06001304 struct nvme_dev *dev = nvmeq->dev;
1305 unsigned extra = nvme_queue_extra(nvmeq->q_depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001306
Keith Busch22404272013-07-15 15:02:20 -06001307 nvmeq->sq_tail = 0;
1308 nvmeq->cq_head = 0;
1309 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001310 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001311 memset(nvmeq->cmdid_data, 0, extra);
1312 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
1313 nvme_cancel_ios(nvmeq, false);
1314 nvmeq->q_suspended = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001315 dev->online_queues++;
Keith Busch22404272013-07-15 15:02:20 -06001316}
1317
1318static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1319{
1320 struct nvme_dev *dev = nvmeq->dev;
1321 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001322
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001323 result = adapter_alloc_cq(dev, qid, nvmeq);
1324 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001325 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001326
1327 result = adapter_alloc_sq(dev, qid, nvmeq);
1328 if (result < 0)
1329 goto release_cq;
1330
Matthew Wilcox3193f072014-01-27 15:57:22 -05001331 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001332 if (result < 0)
1333 goto release_sq;
1334
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001335 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001336 nvme_init_queue(nvmeq, qid);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001337 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001338
1339 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001340
1341 release_sq:
1342 adapter_delete_sq(dev, qid);
1343 release_cq:
1344 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001345 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001346}
1347
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001348static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1349{
1350 unsigned long timeout;
1351 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1352
1353 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1354
1355 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1356 msleep(100);
1357 if (fatal_signal_pending(current))
1358 return -EINTR;
1359 if (time_after(jiffies, timeout)) {
1360 dev_err(&dev->pci_dev->dev,
1361 "Device not ready; aborting initialisation\n");
1362 return -ENODEV;
1363 }
1364 }
1365
1366 return 0;
1367}
1368
1369/*
1370 * If the device has been passed off to us in an enabled state, just clear
1371 * the enabled bit. The spec says we should set the 'shutdown notification
1372 * bits', but doing so may cause the device to complete commands to the
1373 * admin queue ... and we don't know what memory that might be pointing at!
1374 */
1375static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1376{
Matthew Wilcox44af1462013-05-04 06:43:17 -04001377 u32 cc = readl(&dev->bar->cc);
1378
1379 if (cc & NVME_CC_ENABLE)
1380 writel(cc & ~NVME_CC_ENABLE, &dev->bar->cc);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001381 return nvme_wait_ready(dev, cap, false);
1382}
1383
1384static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1385{
1386 return nvme_wait_ready(dev, cap, true);
1387}
1388
Keith Busch1894d8f2013-07-15 15:02:22 -06001389static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1390{
1391 unsigned long timeout;
1392 u32 cc;
1393
1394 cc = (readl(&dev->bar->cc) & ~NVME_CC_SHN_MASK) | NVME_CC_SHN_NORMAL;
1395 writel(cc, &dev->bar->cc);
1396
1397 timeout = 2 * HZ + jiffies;
1398 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1399 NVME_CSTS_SHST_CMPLT) {
1400 msleep(100);
1401 if (fatal_signal_pending(current))
1402 return -EINTR;
1403 if (time_after(jiffies, timeout)) {
1404 dev_err(&dev->pci_dev->dev,
1405 "Device shutdown incomplete; abort shutdown\n");
1406 return -ENODEV;
1407 }
1408 }
1409
1410 return 0;
1411}
1412
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001413static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001414{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001415 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001416 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001417 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001418 struct nvme_queue *nvmeq;
1419
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001420 result = nvme_disable_ctrl(dev, cap);
1421 if (result < 0)
1422 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001423
Keith Busch5a92e702014-02-21 14:13:44 -07001424 nvmeq = raw_nvmeq(dev, 0);
Keith Buschcd638942013-07-15 15:02:23 -06001425 if (!nvmeq) {
1426 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
1427 if (!nvmeq)
1428 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001429 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001430
1431 aqa = nvmeq->q_depth - 1;
1432 aqa |= aqa << 16;
1433
1434 dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
1435 dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
1436 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001437 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001438
1439 writel(aqa, &dev->bar->aqa);
1440 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1441 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
1442 writel(dev->ctrl_config, &dev->bar->cc);
1443
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001444 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001445 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001446 return result;
Matthew Wilcox9e866772012-08-03 13:55:56 -04001447
Matthew Wilcox3193f072014-01-27 15:57:22 -05001448 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Keith Busch025c5572013-05-01 13:07:51 -06001449 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001450 return result;
Keith Busch025c5572013-05-01 13:07:51 -06001451
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001452 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001453 nvme_init_queue(nvmeq, 0);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001454 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001455 return result;
1456}
1457
Vishal Verma5d0f6132013-03-04 18:40:58 -07001458struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001459 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001460{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001461 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001462 struct scatterlist *sg;
1463 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001464 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001465
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001466 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001467 return ERR_PTR(-EINVAL);
Dan Carpenter5460fc02013-05-13 17:59:50 +03001468 if (!length || length > INT_MAX - PAGE_SIZE)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001469 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001470
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001471 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001472 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1473 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001474 if (!pages)
1475 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001476
1477 err = get_user_pages_fast(addr, count, 1, pages);
1478 if (err < count) {
1479 count = err;
1480 err = -EFAULT;
1481 goto put_pages;
1482 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001483
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001484 iod = nvme_alloc_iod(count, length, GFP_KERNEL);
1485 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001486 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001487 for (i = 0; i < count; i++) {
1488 sg_set_page(&sg[i], pages[i],
Dan Carpenter5460fc02013-05-13 17:59:50 +03001489 min_t(unsigned, length, PAGE_SIZE - offset),
1490 offset);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001491 length -= (PAGE_SIZE - offset);
1492 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001493 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001494 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001495 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001496
1497 err = -ENOMEM;
1498 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1499 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001500 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001501 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001502
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001503 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001504 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001505
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001506 free_iod:
1507 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001508 put_pages:
1509 for (i = 0; i < count; i++)
1510 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001511 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001512 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001513}
1514
Vishal Verma5d0f6132013-03-04 18:40:58 -07001515void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001516 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001517{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001518 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001519
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001520 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1521 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001522
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001523 for (i = 0; i < iod->nents; i++)
1524 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001525}
1526
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001527static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1528{
1529 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001530 struct nvme_user_io io;
1531 struct nvme_command c;
Keith Buschf410c682013-04-23 17:23:59 -06001532 unsigned length, meta_len;
1533 int status, i;
1534 struct nvme_iod *iod, *meta_iod = NULL;
1535 dma_addr_t meta_dma_addr;
1536 void *meta, *uninitialized_var(meta_mem);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001537
1538 if (copy_from_user(&io, uio, sizeof(io)))
1539 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001540 length = (io.nblocks + 1) << ns->lba_shift;
Keith Buschf410c682013-04-23 17:23:59 -06001541 meta_len = (io.nblocks + 1) * ns->ms;
1542
1543 if (meta_len && ((io.metadata & 3) || !io.metadata))
1544 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001545
1546 switch (io.opcode) {
1547 case nvme_cmd_write:
1548 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001549 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001550 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001551 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001552 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001553 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001554 }
1555
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001556 if (IS_ERR(iod))
1557 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001558
1559 memset(&c, 0, sizeof(c));
1560 c.rw.opcode = io.opcode;
1561 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001562 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001563 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001564 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001565 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001566 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1567 c.rw.reftag = cpu_to_le32(io.reftag);
1568 c.rw.apptag = cpu_to_le16(io.apptag);
1569 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buschf410c682013-04-23 17:23:59 -06001570
1571 if (meta_len) {
Keith Busch1b567492013-07-18 12:13:51 -06001572 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
1573 meta_len);
Keith Buschf410c682013-04-23 17:23:59 -06001574 if (IS_ERR(meta_iod)) {
1575 status = PTR_ERR(meta_iod);
1576 meta_iod = NULL;
1577 goto unmap;
1578 }
1579
1580 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1581 &meta_dma_addr, GFP_KERNEL);
1582 if (!meta_mem) {
1583 status = -ENOMEM;
1584 goto unmap;
1585 }
1586
1587 if (io.opcode & 1) {
1588 int meta_offset = 0;
1589
1590 for (i = 0; i < meta_iod->nents; i++) {
1591 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1592 meta_iod->sg[i].offset;
1593 memcpy(meta_mem + meta_offset, meta,
1594 meta_iod->sg[i].length);
1595 kunmap_atomic(meta);
1596 meta_offset += meta_iod->sg[i].length;
1597 }
1598 }
1599
1600 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1601 }
1602
Keith Buschedd10d32014-04-03 16:45:23 -06001603 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1604 c.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1605 c.rw.prp2 = cpu_to_le64(iod->first_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001606
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001607 if (length != (io.nblocks + 1) << ns->lba_shift)
1608 status = -ENOMEM;
1609 else
Keith Busch4f5099a2014-03-03 16:39:13 -07001610 status = nvme_submit_io_cmd(dev, &c, NULL);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001611
Keith Buschf410c682013-04-23 17:23:59 -06001612 if (meta_len) {
1613 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1614 int meta_offset = 0;
1615
1616 for (i = 0; i < meta_iod->nents; i++) {
1617 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1618 meta_iod->sg[i].offset;
1619 memcpy(meta, meta_mem + meta_offset,
1620 meta_iod->sg[i].length);
1621 kunmap_atomic(meta);
1622 meta_offset += meta_iod->sg[i].length;
1623 }
1624 }
1625
1626 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1627 meta_dma_addr);
1628 }
1629
1630 unmap:
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001631 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001632 nvme_free_iod(dev, iod);
Keith Buschf410c682013-04-23 17:23:59 -06001633
1634 if (meta_iod) {
1635 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1636 nvme_free_iod(dev, meta_iod);
1637 }
1638
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001639 return status;
1640}
1641
Keith Busch50af8ba2012-07-25 16:07:55 -06001642static int nvme_user_admin_cmd(struct nvme_dev *dev,
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001643 struct nvme_admin_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001644{
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001645 struct nvme_admin_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001646 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001647 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001648 struct nvme_iod *uninitialized_var(iod);
Keith Busch94f370c2013-05-09 14:01:38 -06001649 unsigned timeout;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001650
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001651 if (!capable(CAP_SYS_ADMIN))
1652 return -EACCES;
1653 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001654 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001655
1656 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001657 c.common.opcode = cmd.opcode;
1658 c.common.flags = cmd.flags;
1659 c.common.nsid = cpu_to_le32(cmd.nsid);
1660 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1661 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1662 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1663 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1664 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1665 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1666 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1667 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1668
1669 length = cmd.data_len;
1670 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001671 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1672 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001673 if (IS_ERR(iod))
1674 return PTR_ERR(iod);
Keith Buschedd10d32014-04-03 16:45:23 -06001675 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1676 c.common.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1677 c.common.prp2 = cpu_to_le64(iod->first_dma);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001678 }
1679
Keith Busch94f370c2013-05-09 14:01:38 -06001680 timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
1681 ADMIN_TIMEOUT;
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001682 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001683 status = -ENOMEM;
1684 else
Keith Busch4f5099a2014-03-03 16:39:13 -07001685 status = nvme_submit_sync_cmd(dev, 0, &c, &cmd.result, timeout);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001686
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001687 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001688 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001689 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001690 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001691
Chayan Biswascf90bc42013-05-22 22:34:49 +00001692 if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
Keith Buschf4f117f2012-09-21 10:49:05 -06001693 sizeof(cmd.result)))
1694 status = -EFAULT;
1695
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001696 return status;
1697}
1698
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001699static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1700 unsigned long arg)
1701{
1702 struct nvme_ns *ns = bdev->bd_disk->private_data;
1703
1704 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001705 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001706 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001707 return ns->ns_id;
1708 case NVME_IOCTL_ADMIN_CMD:
Keith Busch50af8ba2012-07-25 16:07:55 -06001709 return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001710 case NVME_IOCTL_SUBMIT_IO:
1711 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001712 case SG_GET_VERSION_NUM:
1713 return nvme_sg_get_version_num((void __user *)arg);
1714 case SG_IO:
1715 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001716 default:
1717 return -ENOTTY;
1718 }
1719}
1720
Keith Busch320a3822013-10-23 13:07:34 -06001721#ifdef CONFIG_COMPAT
1722static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1723 unsigned int cmd, unsigned long arg)
1724{
1725 struct nvme_ns *ns = bdev->bd_disk->private_data;
1726
1727 switch (cmd) {
1728 case SG_IO:
1729 return nvme_sg_io32(ns, arg);
1730 }
1731 return nvme_ioctl(bdev, mode, cmd, arg);
1732}
1733#else
1734#define nvme_compat_ioctl NULL
1735#endif
1736
Keith Busch9ac27092014-01-31 16:53:39 -07001737static int nvme_open(struct block_device *bdev, fmode_t mode)
1738{
1739 struct nvme_ns *ns = bdev->bd_disk->private_data;
1740 struct nvme_dev *dev = ns->dev;
1741
1742 kref_get(&dev->kref);
1743 return 0;
1744}
1745
1746static void nvme_free_dev(struct kref *kref);
1747
1748static void nvme_release(struct gendisk *disk, fmode_t mode)
1749{
1750 struct nvme_ns *ns = disk->private_data;
1751 struct nvme_dev *dev = ns->dev;
1752
1753 kref_put(&dev->kref, nvme_free_dev);
1754}
1755
Keith Busch4cc09e22014-04-02 15:45:37 -06001756static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
1757{
1758 /* some standard values */
1759 geo->heads = 1 << 6;
1760 geo->sectors = 1 << 5;
1761 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1762 return 0;
1763}
1764
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001765static const struct block_device_operations nvme_fops = {
1766 .owner = THIS_MODULE,
1767 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06001768 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07001769 .open = nvme_open,
1770 .release = nvme_release,
Keith Busch4cc09e22014-04-02 15:45:37 -06001771 .getgeo = nvme_getgeo,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001772};
1773
Keith Buschedd10d32014-04-03 16:45:23 -06001774static void nvme_resubmit_iods(struct nvme_queue *nvmeq)
1775{
1776 struct nvme_iod *iod, *next;
1777
1778 list_for_each_entry_safe(iod, next, &nvmeq->iod_bio, node) {
1779 if (unlikely(nvme_submit_iod(nvmeq, iod)))
1780 break;
1781 list_del(&iod->node);
1782 if (bio_list_empty(&nvmeq->sq_cong) &&
1783 list_empty(&nvmeq->iod_bio))
1784 remove_wait_queue(&nvmeq->sq_full,
1785 &nvmeq->sq_cong_wait);
1786 }
1787}
1788
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001789static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1790{
1791 while (bio_list_peek(&nvmeq->sq_cong)) {
1792 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1793 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Keith Busch427e9702013-04-09 11:59:32 -06001794
Keith Buschedd10d32014-04-03 16:45:23 -06001795 if (bio_list_empty(&nvmeq->sq_cong) &&
1796 list_empty(&nvmeq->iod_bio))
Matthew Wilcox3cb967c2011-03-16 16:45:49 -04001797 remove_wait_queue(&nvmeq->sq_full,
1798 &nvmeq->sq_cong_wait);
Keith Busch427e9702013-04-09 11:59:32 -06001799 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
Keith Buschedd10d32014-04-03 16:45:23 -06001800 if (!waitqueue_active(&nvmeq->sq_full))
Keith Busch427e9702013-04-09 11:59:32 -06001801 add_wait_queue(&nvmeq->sq_full,
1802 &nvmeq->sq_cong_wait);
1803 bio_list_add_head(&nvmeq->sq_cong, bio);
1804 break;
1805 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001806 }
1807}
1808
1809static int nvme_kthread(void *data)
1810{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001811 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001812
1813 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001814 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001815 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001816 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001817 int i;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001818 if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
1819 dev->initialized) {
1820 if (work_busy(&dev->reset_work))
1821 continue;
1822 list_del_init(&dev->node);
1823 dev_warn(&dev->pci_dev->dev,
1824 "Failed status, reset controller\n");
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05001825 PREPARE_WORK(&dev->reset_work,
Keith Buschd4b4ff82013-12-10 13:10:37 -07001826 nvme_reset_failed_dev);
1827 queue_work(nvme_workq, &dev->reset_work);
1828 continue;
1829 }
Keith Busch5a92e702014-02-21 14:13:44 -07001830 rcu_read_lock();
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001831 for (i = 0; i < dev->queue_count; i++) {
Keith Busch5a92e702014-02-21 14:13:44 -07001832 struct nvme_queue *nvmeq =
1833 rcu_dereference(dev->queues[i]);
Matthew Wilcox740216f2011-02-15 16:28:20 -05001834 if (!nvmeq)
1835 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001836 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001837 if (nvmeq->q_suspended)
1838 goto unlock;
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001839 nvme_process_cq(nvmeq);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001840 nvme_cancel_ios(nvmeq, true);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001841 nvme_resubmit_bios(nvmeq);
Keith Buschedd10d32014-04-03 16:45:23 -06001842 nvme_resubmit_iods(nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001843 unlock:
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001844 spin_unlock_irq(&nvmeq->q_lock);
1845 }
Keith Busch5a92e702014-02-21 14:13:44 -07001846 rcu_read_unlock();
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001847 }
1848 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001849 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001850 }
1851 return 0;
1852}
1853
Keith Busch0e5e4f02012-11-09 16:33:05 -07001854static void nvme_config_discard(struct nvme_ns *ns)
1855{
1856 u32 logical_block_size = queue_logical_block_size(ns->queue);
1857 ns->queue->limits.discard_zeroes_data = 0;
1858 ns->queue->limits.discard_alignment = logical_block_size;
1859 ns->queue->limits.discard_granularity = logical_block_size;
1860 ns->queue->limits.max_discard_sectors = 0xffffffff;
1861 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1862}
1863
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001864static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001865 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1866{
1867 struct nvme_ns *ns;
1868 struct gendisk *disk;
1869 int lbaf;
1870
1871 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1872 return NULL;
1873
1874 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1875 if (!ns)
1876 return NULL;
1877 ns->queue = blk_alloc_queue(GFP_KERNEL);
1878 if (!ns->queue)
1879 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001880 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
1881 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1882 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001883 blk_queue_make_request(ns->queue, nvme_make_request);
1884 ns->dev = dev;
1885 ns->queue->queuedata = ns;
1886
Matthew Wilcox469071a2013-12-09 12:58:46 -05001887 disk = alloc_disk(0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001888 if (!disk)
1889 goto out_free_queue;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001890 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001891 ns->disk = disk;
1892 lbaf = id->flbas & 0xf;
1893 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Buschf410c682013-04-23 17:23:59 -06001894 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busche9ef4632012-07-24 15:01:04 -06001895 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06001896 if (dev->max_hw_sectors)
1897 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001898
1899 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001900 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001901 disk->fops = &nvme_fops;
1902 disk->private_data = ns;
1903 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05001904 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001905 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001906 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001907 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1908
Keith Busch0e5e4f02012-11-09 16:33:05 -07001909 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1910 nvme_config_discard(ns);
1911
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001912 return ns;
1913
1914 out_free_queue:
1915 blk_cleanup_queue(ns->queue);
1916 out_free_ns:
1917 kfree(ns);
1918 return NULL;
1919}
1920
Keith Busch42f61422014-03-24 10:46:25 -06001921static int nvme_find_closest_node(int node)
1922{
1923 int n, val, min_val = INT_MAX, best_node = node;
1924
1925 for_each_online_node(n) {
1926 if (n == node)
1927 continue;
1928 val = node_distance(node, n);
1929 if (val < min_val) {
1930 min_val = val;
1931 best_node = n;
1932 }
1933 }
1934 return best_node;
1935}
1936
1937static void nvme_set_queue_cpus(cpumask_t *qmask, struct nvme_queue *nvmeq,
1938 int count)
1939{
1940 int cpu;
1941 for_each_cpu(cpu, qmask) {
1942 if (cpumask_weight(nvmeq->cpu_mask) >= count)
1943 break;
1944 if (!cpumask_test_and_set_cpu(cpu, nvmeq->cpu_mask))
1945 *per_cpu_ptr(nvmeq->dev->io_queue, cpu) = nvmeq->qid;
1946 }
1947}
1948
1949static void nvme_add_cpus(cpumask_t *mask, const cpumask_t *unassigned_cpus,
1950 const cpumask_t *new_mask, struct nvme_queue *nvmeq, int cpus_per_queue)
1951{
1952 int next_cpu;
1953 for_each_cpu(next_cpu, new_mask) {
1954 cpumask_or(mask, mask, get_cpu_mask(next_cpu));
1955 cpumask_or(mask, mask, topology_thread_cpumask(next_cpu));
1956 cpumask_and(mask, mask, unassigned_cpus);
1957 nvme_set_queue_cpus(mask, nvmeq, cpus_per_queue);
1958 }
1959}
1960
1961static void nvme_create_io_queues(struct nvme_dev *dev)
1962{
1963 unsigned i, max;
1964
1965 max = min(dev->max_qid, num_online_cpus());
1966 for (i = dev->queue_count; i <= max; i++)
1967 if (!nvme_alloc_queue(dev, i, dev->q_depth, i - 1))
1968 break;
1969
1970 max = min(dev->queue_count - 1, num_online_cpus());
1971 for (i = dev->online_queues; i <= max; i++)
1972 if (nvme_create_queue(raw_nvmeq(dev, i), i))
1973 break;
1974}
1975
1976/*
1977 * If there are fewer queues than online cpus, this will try to optimally
1978 * assign a queue to multiple cpus by grouping cpus that are "close" together:
1979 * thread siblings, core, socket, closest node, then whatever else is
1980 * available.
1981 */
1982static void nvme_assign_io_queues(struct nvme_dev *dev)
1983{
1984 unsigned cpu, cpus_per_queue, queues, remainder, i;
1985 cpumask_var_t unassigned_cpus;
1986
1987 nvme_create_io_queues(dev);
1988
1989 queues = min(dev->online_queues - 1, num_online_cpus());
1990 if (!queues)
1991 return;
1992
1993 cpus_per_queue = num_online_cpus() / queues;
1994 remainder = queues - (num_online_cpus() - queues * cpus_per_queue);
1995
1996 if (!alloc_cpumask_var(&unassigned_cpus, GFP_KERNEL))
1997 return;
1998
1999 cpumask_copy(unassigned_cpus, cpu_online_mask);
2000 cpu = cpumask_first(unassigned_cpus);
2001 for (i = 1; i <= queues; i++) {
2002 struct nvme_queue *nvmeq = lock_nvmeq(dev, i);
2003 cpumask_t mask;
2004
2005 cpumask_clear(nvmeq->cpu_mask);
2006 if (!cpumask_weight(unassigned_cpus)) {
2007 unlock_nvmeq(nvmeq);
2008 break;
2009 }
2010
2011 mask = *get_cpu_mask(cpu);
2012 nvme_set_queue_cpus(&mask, nvmeq, cpus_per_queue);
2013 if (cpus_weight(mask) < cpus_per_queue)
2014 nvme_add_cpus(&mask, unassigned_cpus,
2015 topology_thread_cpumask(cpu),
2016 nvmeq, cpus_per_queue);
2017 if (cpus_weight(mask) < cpus_per_queue)
2018 nvme_add_cpus(&mask, unassigned_cpus,
2019 topology_core_cpumask(cpu),
2020 nvmeq, cpus_per_queue);
2021 if (cpus_weight(mask) < cpus_per_queue)
2022 nvme_add_cpus(&mask, unassigned_cpus,
2023 cpumask_of_node(cpu_to_node(cpu)),
2024 nvmeq, cpus_per_queue);
2025 if (cpus_weight(mask) < cpus_per_queue)
2026 nvme_add_cpus(&mask, unassigned_cpus,
2027 cpumask_of_node(
2028 nvme_find_closest_node(
2029 cpu_to_node(cpu))),
2030 nvmeq, cpus_per_queue);
2031 if (cpus_weight(mask) < cpus_per_queue)
2032 nvme_add_cpus(&mask, unassigned_cpus,
2033 unassigned_cpus,
2034 nvmeq, cpus_per_queue);
2035
2036 WARN(cpumask_weight(nvmeq->cpu_mask) != cpus_per_queue,
2037 "nvme%d qid:%d mis-matched queue-to-cpu assignment\n",
2038 dev->instance, i);
2039
2040 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
2041 nvmeq->cpu_mask);
2042 cpumask_andnot(unassigned_cpus, unassigned_cpus,
2043 nvmeq->cpu_mask);
2044 cpu = cpumask_next(cpu, unassigned_cpus);
2045 if (remainder && !--remainder)
2046 cpus_per_queue++;
2047 unlock_nvmeq(nvmeq);
2048 }
2049 WARN(cpumask_weight(unassigned_cpus), "nvme%d unassigned online cpus\n",
2050 dev->instance);
2051 i = 0;
2052 cpumask_andnot(unassigned_cpus, cpu_possible_mask, cpu_online_mask);
2053 for_each_cpu(cpu, unassigned_cpus)
2054 *per_cpu_ptr(dev->io_queue, cpu) = (i++ % queues) + 1;
2055 free_cpumask_var(unassigned_cpus);
2056}
2057
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002058static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002059{
2060 int status;
2061 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002062 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002063
Matthew Wilcoxdf348132012-01-11 07:29:56 -07002064 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002065 &result);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002066 if (status)
Keith Busch7e03b122013-07-29 16:20:56 -06002067 return status < 0 ? -EIO : -EBUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002068 return min(result & 0xffff, result >> 16) + 1;
2069}
2070
Keith Busch9d713c22013-07-15 15:02:24 -06002071static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
2072{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002073 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06002074}
2075
Keith Busch33b1e952014-03-24 10:46:26 -06002076static int nvme_cpu_notify(struct notifier_block *self,
2077 unsigned long action, void *hcpu)
2078{
2079 struct nvme_dev *dev = container_of(self, struct nvme_dev, nb);
2080 switch (action) {
2081 case CPU_ONLINE:
2082 case CPU_DEAD:
2083 nvme_assign_io_queues(dev);
2084 break;
2085 }
2086 return NOTIFY_OK;
2087}
2088
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002089static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002090{
Keith Busch5a92e702014-02-21 14:13:44 -07002091 struct nvme_queue *adminq = raw_nvmeq(dev, 0);
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002092 struct pci_dev *pdev = dev->pci_dev;
Keith Busch42f61422014-03-24 10:46:25 -06002093 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002094
Keith Busch42f61422014-03-24 10:46:25 -06002095 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002096 result = set_queue_count(dev, nr_io_queues);
Matthew Wilcox1b234842011-01-20 13:01:49 -05002097 if (result < 0)
2098 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002099 if (result < nr_io_queues)
2100 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002101
Keith Busch9d713c22013-07-15 15:02:24 -06002102 size = db_bar_size(dev, nr_io_queues);
2103 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002104 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06002105 do {
2106 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
2107 if (dev->bar)
2108 break;
2109 if (!--nr_io_queues)
2110 return -ENOMEM;
2111 size = db_bar_size(dev, nr_io_queues);
2112 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002113 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07002114 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002115 }
2116
Keith Busch9d713c22013-07-15 15:02:24 -06002117 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002118 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002119
Matthew Wilcox063a8092013-06-20 10:53:48 -04002120 vecs = nr_io_queues;
2121 for (i = 0; i < vecs; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002122 dev->entry[i].entry = i;
2123 for (;;) {
Matthew Wilcox063a8092013-06-20 10:53:48 -04002124 result = pci_enable_msix(pdev, dev->entry, vecs);
2125 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002126 break;
Matthew Wilcox063a8092013-06-20 10:53:48 -04002127 vecs = result;
2128 }
2129
2130 if (result < 0) {
2131 vecs = nr_io_queues;
2132 if (vecs > 32)
2133 vecs = 32;
2134 for (;;) {
2135 result = pci_enable_msi_block(pdev, vecs);
2136 if (result == 0) {
2137 for (i = 0; i < vecs; i++)
2138 dev->entry[i].vector = i + pdev->irq;
2139 break;
2140 } else if (result < 0) {
2141 vecs = 1;
2142 break;
2143 }
2144 vecs = result;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002145 }
2146 }
2147
Matthew Wilcox063a8092013-06-20 10:53:48 -04002148 /*
2149 * Should investigate if there's a performance win from allocating
2150 * more queues than interrupt vectors; it might allow the submission
2151 * path to scale better, even if the receive path is limited by the
2152 * number of interrupts.
2153 */
2154 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002155 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002156
Matthew Wilcox3193f072014-01-27 15:57:22 -05002157 result = queue_request_irq(dev, adminq, adminq->irqname);
Keith Busch9d713c22013-07-15 15:02:24 -06002158 if (result) {
Matthew Wilcox3193f072014-01-27 15:57:22 -05002159 adminq->q_suspended = 1;
Keith Busch22404272013-07-15 15:02:20 -06002160 goto free_queues;
Keith Busch9d713c22013-07-15 15:02:24 -06002161 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05002162
Keith Buschcd638942013-07-15 15:02:23 -06002163 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002164 nvme_free_queues(dev, nr_io_queues + 1);
2165 nvme_assign_io_queues(dev);
Keith Busch22404272013-07-15 15:02:20 -06002166
Keith Busch33b1e952014-03-24 10:46:26 -06002167 dev->nb.notifier_call = &nvme_cpu_notify;
2168 result = register_hotcpu_notifier(&dev->nb);
2169 if (result)
2170 goto free_queues;
2171
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002172 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002173
Keith Busch22404272013-07-15 15:02:20 -06002174 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002175 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002176 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002177}
2178
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002179/*
2180 * Return: error value if an error occurred setting up the queues or calling
2181 * Identify Device. 0 if these succeeded, even if adding some of the
2182 * namespaces failed. At the moment, these failures are silent. TBD which
2183 * failures should be reported.
2184 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002185static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002186{
Matthew Wilcox68608c262013-06-21 14:36:34 -04002187 struct pci_dev *pdev = dev->pci_dev;
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002188 int res;
2189 unsigned nn, i;
Keith Buschcbb62182013-05-01 13:07:49 -06002190 struct nvme_ns *ns;
Matthew Wilcox51814232011-02-01 16:18:08 -05002191 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002192 struct nvme_id_ns *id_ns;
2193 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002194 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06002195 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002196
Matthew Wilcox68608c262013-06-21 14:36:34 -04002197 mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06002198 if (!mem)
2199 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002200
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002201 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002202 if (res) {
2203 res = -EIO;
Keith Buschcbb62182013-05-01 13:07:49 -06002204 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002205 }
2206
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002207 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05002208 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002209 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002210 dev->abort_limit = ctrl->acl + 1;
Matthew Wilcox51814232011-02-01 16:18:08 -05002211 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2212 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2213 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002214 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002215 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002216 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
2217 (pdev->device == 0x0953) && ctrl->vs[3])
Keith Busch159b67d2013-04-09 17:13:20 -06002218 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002219
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002220 id_ns = mem;
Matthew Wilcox2b2c1892011-10-07 13:10:13 -04002221 for (i = 1; i <= nn; i++) {
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002222 res = nvme_identify(dev, i, 0, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002223 if (res)
2224 continue;
2225
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002226 if (id_ns->ncap == 0)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002227 continue;
2228
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002229 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
Keith Busch08df1e02012-09-21 10:52:13 -06002230 dma_addr + 4096, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002231 if (res)
Keith Busch12209032013-01-31 14:40:38 -07002232 memset(mem + 4096, 0, 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002233
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002234 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002235 if (ns)
2236 list_add_tail(&ns->list, &dev->namespaces);
2237 }
2238 list_for_each_entry(ns, &dev->namespaces, list)
2239 add_disk(ns->disk);
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002240 res = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002241
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002242 out:
Matthew Wilcox684f5c22011-09-19 17:14:53 -04002243 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002244 return res;
2245}
2246
Keith Busch0877cb02013-07-15 15:02:19 -06002247static int nvme_dev_map(struct nvme_dev *dev)
2248{
Keith Busch42f61422014-03-24 10:46:25 -06002249 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002250 int bars, result = -ENOMEM;
2251 struct pci_dev *pdev = dev->pci_dev;
2252
2253 if (pci_enable_device_mem(pdev))
2254 return result;
2255
2256 dev->entry[0].vector = pdev->irq;
2257 pci_set_master(pdev);
2258 bars = pci_select_bars(pdev, IORESOURCE_MEM);
2259 if (pci_request_selected_regions(pdev, bars, "nvme"))
2260 goto disable_pci;
2261
Russell King052d0ef2013-06-26 23:49:11 +01002262 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
2263 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2264 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002265
Keith Busch0877cb02013-07-15 15:02:19 -06002266 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2267 if (!dev->bar)
2268 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07002269 if (readl(&dev->bar->csts) == -1) {
2270 result = -ENODEV;
2271 goto unmap;
2272 }
Keith Busch42f61422014-03-24 10:46:25 -06002273 cap = readq(&dev->bar->cap);
2274 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2275 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Keith Busch0877cb02013-07-15 15:02:19 -06002276 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2277
2278 return 0;
2279
Keith Busch0e53d182013-12-10 13:10:39 -07002280 unmap:
2281 iounmap(dev->bar);
2282 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002283 disable:
2284 pci_release_regions(pdev);
2285 disable_pci:
2286 pci_disable_device(pdev);
2287 return result;
2288}
2289
2290static void nvme_dev_unmap(struct nvme_dev *dev)
2291{
2292 if (dev->pci_dev->msi_enabled)
2293 pci_disable_msi(dev->pci_dev);
2294 else if (dev->pci_dev->msix_enabled)
2295 pci_disable_msix(dev->pci_dev);
2296
2297 if (dev->bar) {
2298 iounmap(dev->bar);
2299 dev->bar = NULL;
Keith Busch9a6b9452013-12-10 13:10:36 -07002300 pci_release_regions(dev->pci_dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002301 }
2302
Keith Busch0877cb02013-07-15 15:02:19 -06002303 if (pci_is_enabled(dev->pci_dev))
2304 pci_disable_device(dev->pci_dev);
2305}
2306
Keith Busch4d115422013-12-10 13:10:40 -07002307struct nvme_delq_ctx {
2308 struct task_struct *waiter;
2309 struct kthread_worker *worker;
2310 atomic_t refcount;
2311};
2312
2313static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2314{
2315 dq->waiter = current;
2316 mb();
2317
2318 for (;;) {
2319 set_current_state(TASK_KILLABLE);
2320 if (!atomic_read(&dq->refcount))
2321 break;
2322 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2323 fatal_signal_pending(current)) {
2324 set_current_state(TASK_RUNNING);
2325
2326 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
2327 nvme_disable_queue(dev, 0);
2328
2329 send_sig(SIGKILL, dq->worker->task, 1);
2330 flush_kthread_worker(dq->worker);
2331 return;
2332 }
2333 }
2334 set_current_state(TASK_RUNNING);
2335}
2336
2337static void nvme_put_dq(struct nvme_delq_ctx *dq)
2338{
2339 atomic_dec(&dq->refcount);
2340 if (dq->waiter)
2341 wake_up_process(dq->waiter);
2342}
2343
2344static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2345{
2346 atomic_inc(&dq->refcount);
2347 return dq;
2348}
2349
2350static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2351{
2352 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
2353
2354 nvme_clear_queue(nvmeq);
2355 nvme_put_dq(dq);
2356}
2357
2358static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2359 kthread_work_func_t fn)
2360{
2361 struct nvme_command c;
2362
2363 memset(&c, 0, sizeof(c));
2364 c.delete_queue.opcode = opcode;
2365 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2366
2367 init_kthread_work(&nvmeq->cmdinfo.work, fn);
2368 return nvme_submit_admin_cmd_async(nvmeq->dev, &c, &nvmeq->cmdinfo);
2369}
2370
2371static void nvme_del_cq_work_handler(struct kthread_work *work)
2372{
2373 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2374 cmdinfo.work);
2375 nvme_del_queue_end(nvmeq);
2376}
2377
2378static int nvme_delete_cq(struct nvme_queue *nvmeq)
2379{
2380 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2381 nvme_del_cq_work_handler);
2382}
2383
2384static void nvme_del_sq_work_handler(struct kthread_work *work)
2385{
2386 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2387 cmdinfo.work);
2388 int status = nvmeq->cmdinfo.status;
2389
2390 if (!status)
2391 status = nvme_delete_cq(nvmeq);
2392 if (status)
2393 nvme_del_queue_end(nvmeq);
2394}
2395
2396static int nvme_delete_sq(struct nvme_queue *nvmeq)
2397{
2398 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2399 nvme_del_sq_work_handler);
2400}
2401
2402static void nvme_del_queue_start(struct kthread_work *work)
2403{
2404 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2405 cmdinfo.work);
2406 allow_signal(SIGKILL);
2407 if (nvme_delete_sq(nvmeq))
2408 nvme_del_queue_end(nvmeq);
2409}
2410
2411static void nvme_disable_io_queues(struct nvme_dev *dev)
2412{
2413 int i;
2414 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2415 struct nvme_delq_ctx dq;
2416 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2417 &worker, "nvme%d", dev->instance);
2418
2419 if (IS_ERR(kworker_task)) {
2420 dev_err(&dev->pci_dev->dev,
2421 "Failed to create queue del task\n");
2422 for (i = dev->queue_count - 1; i > 0; i--)
2423 nvme_disable_queue(dev, i);
2424 return;
2425 }
2426
2427 dq.waiter = NULL;
2428 atomic_set(&dq.refcount, 0);
2429 dq.worker = &worker;
2430 for (i = dev->queue_count - 1; i > 0; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07002431 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
Keith Busch4d115422013-12-10 13:10:40 -07002432
2433 if (nvme_suspend_queue(nvmeq))
2434 continue;
2435 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2436 nvmeq->cmdinfo.worker = dq.worker;
2437 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2438 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2439 }
2440 nvme_wait_dq(&dq, dev);
2441 kthread_stop(kworker_task);
2442}
2443
Dan McLeranb9afca32014-04-07 17:10:11 -06002444/*
2445* Remove the node from the device list and check
2446* for whether or not we need to stop the nvme_thread.
2447*/
2448static void nvme_dev_list_remove(struct nvme_dev *dev)
2449{
2450 struct task_struct *tmp = NULL;
2451
2452 spin_lock(&dev_list_lock);
2453 list_del_init(&dev->node);
2454 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2455 tmp = nvme_thread;
2456 nvme_thread = NULL;
2457 }
2458 spin_unlock(&dev_list_lock);
2459
2460 if (tmp)
2461 kthread_stop(tmp);
2462}
2463
Keith Buschf0b50732013-07-15 15:02:21 -06002464static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002465{
Keith Busch22404272013-07-15 15:02:20 -06002466 int i;
2467
Keith Buschd4b4ff82013-12-10 13:10:37 -07002468 dev->initialized = 0;
Keith Busch33b1e952014-03-24 10:46:26 -06002469 unregister_hotcpu_notifier(&dev->nb);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002470
Dan McLeranb9afca32014-04-07 17:10:11 -06002471 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002472
Keith Busch4d115422013-12-10 13:10:40 -07002473 if (!dev->bar || (dev->bar && readl(&dev->bar->csts) == -1)) {
2474 for (i = dev->queue_count - 1; i >= 0; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07002475 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
Keith Busch4d115422013-12-10 13:10:40 -07002476 nvme_suspend_queue(nvmeq);
2477 nvme_clear_queue(nvmeq);
2478 }
2479 } else {
2480 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002481 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002482 nvme_disable_queue(dev, 0);
2483 }
Keith Buschf0b50732013-07-15 15:02:21 -06002484 nvme_dev_unmap(dev);
2485}
2486
2487static void nvme_dev_remove(struct nvme_dev *dev)
2488{
Keith Busch9ac27092014-01-31 16:53:39 -07002489 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002490
Keith Busch9ac27092014-01-31 16:53:39 -07002491 list_for_each_entry(ns, &dev->namespaces, list) {
2492 if (ns->disk->flags & GENHD_FL_UP)
2493 del_gendisk(ns->disk);
2494 if (!blk_queue_dying(ns->queue))
2495 blk_cleanup_queue(ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002496 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002497}
2498
Matthew Wilcox091b6092011-02-10 09:56:01 -05002499static int nvme_setup_prp_pools(struct nvme_dev *dev)
2500{
2501 struct device *dmadev = &dev->pci_dev->dev;
2502 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2503 PAGE_SIZE, PAGE_SIZE, 0);
2504 if (!dev->prp_page_pool)
2505 return -ENOMEM;
2506
Matthew Wilcox99802a72011-02-10 10:30:34 -05002507 /* Optimisation for I/Os between 4k and 128k */
2508 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2509 256, 256, 0);
2510 if (!dev->prp_small_pool) {
2511 dma_pool_destroy(dev->prp_page_pool);
2512 return -ENOMEM;
2513 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002514 return 0;
2515}
2516
2517static void nvme_release_prp_pools(struct nvme_dev *dev)
2518{
2519 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002520 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002521}
2522
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002523static DEFINE_IDA(nvme_instance_ida);
2524
2525static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002526{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002527 int instance, error;
2528
2529 do {
2530 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2531 return -ENODEV;
2532
2533 spin_lock(&dev_list_lock);
2534 error = ida_get_new(&nvme_instance_ida, &instance);
2535 spin_unlock(&dev_list_lock);
2536 } while (error == -EAGAIN);
2537
2538 if (error)
2539 return -ENODEV;
2540
2541 dev->instance = instance;
2542 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002543}
2544
2545static void nvme_release_instance(struct nvme_dev *dev)
2546{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002547 spin_lock(&dev_list_lock);
2548 ida_remove(&nvme_instance_ida, dev->instance);
2549 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002550}
2551
Keith Busch9ac27092014-01-31 16:53:39 -07002552static void nvme_free_namespaces(struct nvme_dev *dev)
2553{
2554 struct nvme_ns *ns, *next;
2555
2556 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2557 list_del(&ns->list);
2558 put_disk(ns->disk);
2559 kfree(ns);
2560 }
2561}
2562
Keith Busch5e82e952013-02-19 10:17:58 -07002563static void nvme_free_dev(struct kref *kref)
2564{
2565 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002566
2567 nvme_free_namespaces(dev);
Keith Busch42f61422014-03-24 10:46:25 -06002568 free_percpu(dev->io_queue);
Keith Busch5e82e952013-02-19 10:17:58 -07002569 kfree(dev->queues);
2570 kfree(dev->entry);
2571 kfree(dev);
2572}
2573
2574static int nvme_dev_open(struct inode *inode, struct file *f)
2575{
2576 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
2577 miscdev);
2578 kref_get(&dev->kref);
2579 f->private_data = dev;
2580 return 0;
2581}
2582
2583static int nvme_dev_release(struct inode *inode, struct file *f)
2584{
2585 struct nvme_dev *dev = f->private_data;
2586 kref_put(&dev->kref, nvme_free_dev);
2587 return 0;
2588}
2589
2590static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2591{
2592 struct nvme_dev *dev = f->private_data;
2593 switch (cmd) {
2594 case NVME_IOCTL_ADMIN_CMD:
2595 return nvme_user_admin_cmd(dev, (void __user *)arg);
2596 default:
2597 return -ENOTTY;
2598 }
2599}
2600
2601static const struct file_operations nvme_dev_fops = {
2602 .owner = THIS_MODULE,
2603 .open = nvme_dev_open,
2604 .release = nvme_dev_release,
2605 .unlocked_ioctl = nvme_dev_ioctl,
2606 .compat_ioctl = nvme_dev_ioctl,
2607};
2608
Keith Buschf0b50732013-07-15 15:02:21 -06002609static int nvme_dev_start(struct nvme_dev *dev)
2610{
2611 int result;
Dan McLeranb9afca32014-04-07 17:10:11 -06002612 bool start_thread = false;
Keith Buschf0b50732013-07-15 15:02:21 -06002613
2614 result = nvme_dev_map(dev);
2615 if (result)
2616 return result;
2617
2618 result = nvme_configure_admin_queue(dev);
2619 if (result)
2620 goto unmap;
2621
2622 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002623 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2624 start_thread = true;
2625 nvme_thread = NULL;
2626 }
Keith Buschf0b50732013-07-15 15:02:21 -06002627 list_add(&dev->node, &dev_list);
2628 spin_unlock(&dev_list_lock);
2629
Dan McLeranb9afca32014-04-07 17:10:11 -06002630 if (start_thread) {
2631 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2632 wake_up(&nvme_kthread_wait);
2633 } else
2634 wait_event_killable(nvme_kthread_wait, nvme_thread);
2635
2636 if (IS_ERR_OR_NULL(nvme_thread)) {
2637 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2638 goto disable;
2639 }
2640
Keith Buschf0b50732013-07-15 15:02:21 -06002641 result = nvme_setup_io_queues(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002642 if (result && result != -EBUSY)
Keith Buschf0b50732013-07-15 15:02:21 -06002643 goto disable;
2644
Keith Buschd82e8bf2013-09-05 14:45:07 -06002645 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002646
2647 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002648 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06002649 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002650 unmap:
2651 nvme_dev_unmap(dev);
2652 return result;
2653}
2654
Keith Busch9a6b9452013-12-10 13:10:36 -07002655static int nvme_remove_dead_ctrl(void *arg)
2656{
2657 struct nvme_dev *dev = (struct nvme_dev *)arg;
2658 struct pci_dev *pdev = dev->pci_dev;
2659
2660 if (pci_get_drvdata(pdev))
2661 pci_stop_and_remove_bus_device(pdev);
2662 kref_put(&dev->kref, nvme_free_dev);
2663 return 0;
2664}
2665
2666static void nvme_remove_disks(struct work_struct *ws)
2667{
Keith Busch9a6b9452013-12-10 13:10:36 -07002668 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2669
2670 nvme_dev_remove(dev);
Keith Busch5a92e702014-02-21 14:13:44 -07002671 nvme_free_queues(dev, 1);
Keith Busch9a6b9452013-12-10 13:10:36 -07002672}
2673
2674static int nvme_dev_resume(struct nvme_dev *dev)
2675{
2676 int ret;
2677
2678 ret = nvme_dev_start(dev);
2679 if (ret && ret != -EBUSY)
2680 return ret;
2681 if (ret == -EBUSY) {
2682 spin_lock(&dev_list_lock);
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05002683 PREPARE_WORK(&dev->reset_work, nvme_remove_disks);
Keith Busch9a6b9452013-12-10 13:10:36 -07002684 queue_work(nvme_workq, &dev->reset_work);
2685 spin_unlock(&dev_list_lock);
2686 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002687 dev->initialized = 1;
Keith Busch9a6b9452013-12-10 13:10:36 -07002688 return 0;
2689}
2690
2691static void nvme_dev_reset(struct nvme_dev *dev)
2692{
2693 nvme_dev_shutdown(dev);
2694 if (nvme_dev_resume(dev)) {
2695 dev_err(&dev->pci_dev->dev, "Device failed to resume\n");
2696 kref_get(&dev->kref);
2697 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2698 dev->instance))) {
2699 dev_err(&dev->pci_dev->dev,
2700 "Failed to start controller remove task\n");
2701 kref_put(&dev->kref, nvme_free_dev);
2702 }
2703 }
2704}
2705
2706static void nvme_reset_failed_dev(struct work_struct *ws)
2707{
2708 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2709 nvme_dev_reset(dev);
2710}
2711
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002712static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002713{
Keith Busch0877cb02013-07-15 15:02:19 -06002714 int result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002715 struct nvme_dev *dev;
2716
2717 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2718 if (!dev)
2719 return -ENOMEM;
2720 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
2721 GFP_KERNEL);
2722 if (!dev->entry)
2723 goto free;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002724 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
2725 GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002726 if (!dev->queues)
2727 goto free;
Keith Busch42f61422014-03-24 10:46:25 -06002728 dev->io_queue = alloc_percpu(unsigned short);
2729 if (!dev->io_queue)
2730 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002731
2732 INIT_LIST_HEAD(&dev->namespaces);
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05002733 INIT_WORK(&dev->reset_work, nvme_reset_failed_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002734 dev->pci_dev = pdev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002735 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002736 result = nvme_set_instance(dev);
2737 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002738 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002739
Matthew Wilcox091b6092011-02-10 09:56:01 -05002740 result = nvme_setup_prp_pools(dev);
2741 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002742 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002743
Keith Buschfb35e912014-03-03 11:09:47 -07002744 kref_init(&dev->kref);
Keith Buschf0b50732013-07-15 15:02:21 -06002745 result = nvme_dev_start(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002746 if (result) {
2747 if (result == -EBUSY)
2748 goto create_cdev;
Keith Busch0877cb02013-07-15 15:02:19 -06002749 goto release_pools;
Keith Buschd82e8bf2013-09-05 14:45:07 -06002750 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002751
Matthew Wilcox740216f2011-02-15 16:28:20 -05002752 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002753 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002754 goto shutdown;
Matthew Wilcox740216f2011-02-15 16:28:20 -05002755
Keith Buschd82e8bf2013-09-05 14:45:07 -06002756 create_cdev:
Keith Busch5e82e952013-02-19 10:17:58 -07002757 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
2758 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2759 dev->miscdev.parent = &pdev->dev;
2760 dev->miscdev.name = dev->name;
2761 dev->miscdev.fops = &nvme_dev_fops;
2762 result = misc_register(&dev->miscdev);
2763 if (result)
2764 goto remove;
2765
Keith Buschd4b4ff82013-12-10 13:10:37 -07002766 dev->initialized = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002767 return 0;
2768
Keith Busch5e82e952013-02-19 10:17:58 -07002769 remove:
2770 nvme_dev_remove(dev);
Keith Busch9ac27092014-01-31 16:53:39 -07002771 nvme_free_namespaces(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002772 shutdown:
2773 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002774 release_pools:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002775 nvme_free_queues(dev, 0);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002776 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002777 release:
2778 nvme_release_instance(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002779 free:
Keith Busch42f61422014-03-24 10:46:25 -06002780 free_percpu(dev->io_queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002781 kfree(dev->queues);
2782 kfree(dev->entry);
2783 kfree(dev);
2784 return result;
2785}
2786
Keith Busch09ece142014-01-27 11:29:40 -05002787static void nvme_shutdown(struct pci_dev *pdev)
2788{
2789 struct nvme_dev *dev = pci_get_drvdata(pdev);
2790 nvme_dev_shutdown(dev);
2791}
2792
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002793static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002794{
2795 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002796
2797 spin_lock(&dev_list_lock);
2798 list_del_init(&dev->node);
2799 spin_unlock(&dev_list_lock);
2800
2801 pci_set_drvdata(pdev, NULL);
2802 flush_work(&dev->reset_work);
Keith Busch5e82e952013-02-19 10:17:58 -07002803 misc_deregister(&dev->miscdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002804 nvme_dev_remove(dev);
2805 nvme_dev_shutdown(dev);
Keith Buscha1a5ef92013-12-16 13:50:00 -05002806 nvme_free_queues(dev, 0);
Keith Busch5a92e702014-02-21 14:13:44 -07002807 rcu_barrier();
Keith Busch9a6b9452013-12-10 13:10:36 -07002808 nvme_release_instance(dev);
2809 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002810 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002811}
2812
2813/* These functions are yet to be implemented */
2814#define nvme_error_detected NULL
2815#define nvme_dump_registers NULL
2816#define nvme_link_reset NULL
2817#define nvme_slot_reset NULL
2818#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002819
Jingoo Han671a6012014-02-13 11:19:14 +09002820#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002821static int nvme_suspend(struct device *dev)
2822{
2823 struct pci_dev *pdev = to_pci_dev(dev);
2824 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2825
2826 nvme_dev_shutdown(ndev);
2827 return 0;
2828}
2829
2830static int nvme_resume(struct device *dev)
2831{
2832 struct pci_dev *pdev = to_pci_dev(dev);
2833 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002834
Keith Busch9a6b9452013-12-10 13:10:36 -07002835 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05002836 PREPARE_WORK(&ndev->reset_work, nvme_reset_failed_dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002837 queue_work(nvme_workq, &ndev->reset_work);
2838 }
2839 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002840}
Jingoo Han671a6012014-02-13 11:19:14 +09002841#endif
Keith Buschcd638942013-07-15 15:02:23 -06002842
2843static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002844
Stephen Hemminger1d352032012-09-07 09:33:17 -07002845static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002846 .error_detected = nvme_error_detected,
2847 .mmio_enabled = nvme_dump_registers,
2848 .link_reset = nvme_link_reset,
2849 .slot_reset = nvme_slot_reset,
2850 .resume = nvme_error_resume,
2851};
2852
2853/* Move to pci_ids.h later */
2854#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2855
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002856static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002857 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2858 { 0, }
2859};
2860MODULE_DEVICE_TABLE(pci, nvme_id_table);
2861
2862static struct pci_driver nvme_driver = {
2863 .name = "nvme",
2864 .id_table = nvme_id_table,
2865 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002866 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002867 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002868 .driver = {
2869 .pm = &nvme_dev_pm_ops,
2870 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002871 .err_handler = &nvme_err_handler,
2872};
2873
2874static int __init nvme_init(void)
2875{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002876 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002877
Dan McLeranb9afca32014-04-07 17:10:11 -06002878 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002879
Keith Busch9a6b9452013-12-10 13:10:36 -07002880 nvme_workq = create_singlethread_workqueue("nvme");
2881 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06002882 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07002883
Keith Busch5c42ea12012-07-25 16:05:18 -06002884 result = register_blkdev(nvme_major, "nvme");
2885 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002886 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06002887 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002888 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002889
2890 result = pci_register_driver(&nvme_driver);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002891 if (result)
2892 goto unregister_blkdev;
2893 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002894
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002895 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002896 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002897 kill_workq:
2898 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002899 return result;
2900}
2901
2902static void __exit nvme_exit(void)
2903{
2904 pci_unregister_driver(&nvme_driver);
2905 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002906 destroy_workqueue(nvme_workq);
Dan McLeranb9afca32014-04-07 17:10:11 -06002907 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002908}
2909
2910MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2911MODULE_LICENSE("GPL");
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002912MODULE_VERSION("0.9");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002913module_init(nvme_init);
2914module_exit(nvme_exit);