blob: 074e9829bb0830ba0b3c163090574d7e71ada875 [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003 * Copyright (c) 2011-2014, Intel Corporation.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050013 */
14
15#include <linux/nvme.h>
16#include <linux/bio.h>
Matthew Wilcox8de05532011-05-12 13:50:28 -040017#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050018#include <linux/blkdev.h>
Keith Busch42f61422014-03-24 10:46:25 -060019#include <linux/cpu.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040020#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050021#include <linux/errno.h>
22#include <linux/fs.h>
23#include <linux/genhd.h>
Keith Busch4cc09e22014-04-02 15:45:37 -060024#include <linux/hdreg.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040025#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050026#include <linux/init.h>
27#include <linux/interrupt.h>
28#include <linux/io.h>
29#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050030#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050031#include <linux/kernel.h>
32#include <linux/mm.h>
33#include <linux/module.h>
34#include <linux/moduleparam.h>
35#include <linux/pci.h>
Keith Busch42f61422014-03-24 10:46:25 -060036#include <linux/percpu.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050037#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040038#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050039#include <linux/sched.h>
40#include <linux/slab.h>
41#include <linux/types.h>
Vishal Verma5d0f6132013-03-04 18:40:58 -070042#include <scsi/sg.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090043#include <asm-generic/io-64-nonatomic-lo-hi.h>
44
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050045#define NVME_Q_DEPTH 1024
46#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
47#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Matthew Wilcoxe85248e2011-02-06 18:30:16 -050048#define ADMIN_TIMEOUT (60 * HZ)
Keith Buschedd10d32014-04-03 16:45:23 -060049#define IOD_TIMEOUT (4 * NVME_IO_TIMEOUT)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050050
Keith Buschb3550842014-04-04 11:43:36 -060051unsigned char io_timeout = 30;
52module_param(io_timeout, byte, 0644);
53MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050054
55static int nvme_major;
56module_param(nvme_major, int, 0);
57
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050058static int use_threaded_interrupts;
59module_param(use_threaded_interrupts, int, 0);
60
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050061static DEFINE_SPINLOCK(dev_list_lock);
62static LIST_HEAD(dev_list);
63static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070064static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060065static wait_queue_head_t nvme_kthread_wait;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050066
Keith Buschd4b4ff82013-12-10 13:10:37 -070067static void nvme_reset_failed_dev(struct work_struct *ws);
68
Keith Busch4d115422013-12-10 13:10:40 -070069struct async_cmd_info {
70 struct kthread_work work;
71 struct kthread_worker *worker;
72 u32 result;
73 int status;
74 void *ctx;
75};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050076
77/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050078 * An NVM Express queue. Each device has at least two (one for admin
79 * commands and one for I/O commands).
80 */
81struct nvme_queue {
Keith Busch5a92e702014-02-21 14:13:44 -070082 struct rcu_head r_head;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050083 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -050084 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -050085 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050086 spinlock_t q_lock;
87 struct nvme_command *sq_cmds;
88 volatile struct nvme_completion *cqes;
89 dma_addr_t sq_dma_addr;
90 dma_addr_t cq_dma_addr;
91 wait_queue_head_t sq_full;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050092 wait_queue_t sq_cong_wait;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050093 struct bio_list sq_cong;
Keith Buschedd10d32014-04-03 16:45:23 -060094 struct list_head iod_bio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050095 u32 __iomem *q_db;
96 u16 q_depth;
97 u16 cq_vector;
98 u16 sq_head;
99 u16 sq_tail;
100 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700101 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400102 u8 cq_phase;
103 u8 cqe_seen;
Keith Busch22404272013-07-15 15:02:20 -0600104 u8 q_suspended;
Keith Busch42f61422014-03-24 10:46:25 -0600105 cpumask_var_t cpu_mask;
Keith Busch4d115422013-12-10 13:10:40 -0700106 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500107 unsigned long cmdid_data[];
108};
109
110/*
111 * Check we didin't inadvertently grow the command struct
112 */
113static inline void _nvme_check_size(void)
114{
115 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
116 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
117 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
118 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
119 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400120 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700121 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500122 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
123 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
124 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
125 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600126 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500127}
128
Keith Buschedd10d32014-04-03 16:45:23 -0600129typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400130 struct nvme_completion *);
131
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500132struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400133 nvme_completion_fn fn;
134 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500135 unsigned long timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700136 int aborted;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500137};
138
139static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
140{
141 return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
142}
143
Keith Busch22404272013-07-15 15:02:20 -0600144static unsigned nvme_queue_extra(int depth)
145{
146 return DIV_ROUND_UP(depth, 8) + (depth * sizeof(struct nvme_cmd_info));
147}
148
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500149/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400150 * alloc_cmdid() - Allocate a Command ID
151 * @nvmeq: The queue that will be used for this command
152 * @ctx: A pointer that will be passed to the handler
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400153 * @handler: The function to call on completion
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500154 *
155 * Allocate a Command ID for a queue. The data passed in will
156 * be passed to the completion handler. This is implemented by using
157 * the bottom two bits of the ctx pointer to store the handler ID.
158 * Passing in a pointer that's not 4-byte aligned will cause a BUG.
159 * We can change this if it becomes a problem.
Matthew Wilcox184d2942011-05-11 21:36:38 -0400160 *
161 * May be called with local interrupts disabled and the q_lock held,
162 * or with interrupts enabled and no locks held.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500163 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400164static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx,
165 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500166{
Matthew Wilcoxe6d15f72011-02-24 08:49:41 -0500167 int depth = nvmeq->q_depth - 1;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500168 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500169 int cmdid;
170
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500171 do {
172 cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
173 if (cmdid >= depth)
174 return -EBUSY;
175 } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
176
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400177 info[cmdid].fn = handler;
178 info[cmdid].ctx = ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500179 info[cmdid].timeout = jiffies + timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700180 info[cmdid].aborted = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500181 return cmdid;
182}
183
184static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400185 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500186{
187 int cmdid;
188 wait_event_killable(nvmeq->sq_full,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500189 (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500190 return (cmdid < 0) ? -EINTR : cmdid;
191}
192
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400193/* Special values must be less than 0x1000 */
194#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500195#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
196#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
197#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500198#define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE)
Keith Buschc30341d2013-12-10 13:10:38 -0700199#define CMD_CTX_ABORT (0x31C + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500200
Keith Buschedd10d32014-04-03 16:45:23 -0600201static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400202 struct nvme_completion *cqe)
203{
204 if (ctx == CMD_CTX_CANCELLED)
205 return;
206 if (ctx == CMD_CTX_FLUSH)
207 return;
Keith Buschc30341d2013-12-10 13:10:38 -0700208 if (ctx == CMD_CTX_ABORT) {
Keith Buschedd10d32014-04-03 16:45:23 -0600209 ++nvmeq->dev->abort_limit;
Keith Buschc30341d2013-12-10 13:10:38 -0700210 return;
211 }
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400212 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600213 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400214 "completed id %d twice on queue %d\n",
215 cqe->command_id, le16_to_cpup(&cqe->sq_id));
216 return;
217 }
218 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600219 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400220 "invalid id %d completed on queue %d\n",
221 cqe->command_id, le16_to_cpup(&cqe->sq_id));
222 return;
223 }
224
Keith Buschedd10d32014-04-03 16:45:23 -0600225 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400226}
227
Keith Buschedd10d32014-04-03 16:45:23 -0600228static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700229 struct nvme_completion *cqe)
230{
231 struct async_cmd_info *cmdinfo = ctx;
232 cmdinfo->result = le32_to_cpup(&cqe->result);
233 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
234 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
235}
236
Matthew Wilcox184d2942011-05-11 21:36:38 -0400237/*
238 * Called with local interrupts disabled and the q_lock held. May not sleep.
239 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400240static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid,
241 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500242{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400243 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500244 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500245
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400246 if (cmdid >= nvmeq->q_depth) {
247 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500248 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400249 }
Keith Busch859361a2012-08-02 14:05:59 -0600250 if (fn)
251 *fn = info[cmdid].fn;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400252 ctx = info[cmdid].ctx;
253 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500254 info[cmdid].ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500255 clear_bit(cmdid, nvmeq->cmdid_data);
256 wake_up(&nvmeq->sq_full);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400257 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500258}
259
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400260static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
261 nvme_completion_fn *fn)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500262{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400263 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500264 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400265 if (fn)
266 *fn = info[cmdid].fn;
267 ctx = info[cmdid].ctx;
268 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500269 info[cmdid].ctx = CMD_CTX_CANCELLED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400270 return ctx;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500271}
272
Keith Busch5a92e702014-02-21 14:13:44 -0700273static struct nvme_queue *raw_nvmeq(struct nvme_dev *dev, int qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500274{
Keith Busch5a92e702014-02-21 14:13:44 -0700275 return rcu_dereference_raw(dev->queues[qid]);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500276}
277
Keith Busch4f5099a2014-03-03 16:39:13 -0700278static struct nvme_queue *get_nvmeq(struct nvme_dev *dev) __acquires(RCU)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500279{
Keith Busch42f61422014-03-24 10:46:25 -0600280 unsigned queue_id = get_cpu_var(*dev->io_queue);
Keith Busch5a92e702014-02-21 14:13:44 -0700281 rcu_read_lock();
Keith Busch42f61422014-03-24 10:46:25 -0600282 return rcu_dereference(dev->queues[queue_id]);
Keith Busch5a92e702014-02-21 14:13:44 -0700283}
284
Keith Busch4f5099a2014-03-03 16:39:13 -0700285static void put_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500286{
Keith Busch5a92e702014-02-21 14:13:44 -0700287 rcu_read_unlock();
Keith Busch42f61422014-03-24 10:46:25 -0600288 put_cpu_var(nvmeq->dev->io_queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500289}
290
Keith Busch4f5099a2014-03-03 16:39:13 -0700291static struct nvme_queue *lock_nvmeq(struct nvme_dev *dev, int q_idx)
292 __acquires(RCU)
293{
294 rcu_read_lock();
295 return rcu_dereference(dev->queues[q_idx]);
296}
297
298static void unlock_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
299{
300 rcu_read_unlock();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500301}
302
303/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400304 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500305 * @nvmeq: The queue to use
306 * @cmd: The command to send
307 *
308 * Safe to use from interrupt context
309 */
310static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
311{
312 unsigned long flags;
313 u16 tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500314 spin_lock_irqsave(&nvmeq->q_lock, flags);
Keith Busch4f5099a2014-03-03 16:39:13 -0700315 if (nvmeq->q_suspended) {
316 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
317 return -EBUSY;
318 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500319 tail = nvmeq->sq_tail;
320 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500321 if (++tail == nvmeq->q_depth)
322 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500323 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500324 nvmeq->sq_tail = tail;
325 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
326
327 return 0;
328}
329
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500330static __le64 **iod_list(struct nvme_iod *iod)
331{
332 return ((void *)iod) + iod->offset;
333}
334
335/*
336 * Will slightly overestimate the number of pages needed. This is OK
337 * as it only leads to a small amount of wasted memory for the lifetime of
338 * the I/O.
339 */
340static int nvme_npages(unsigned size)
341{
342 unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE);
343 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
344}
345
346static struct nvme_iod *
347nvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp)
348{
349 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
350 sizeof(__le64 *) * nvme_npages(nbytes) +
351 sizeof(struct scatterlist) * nseg, gfp);
352
353 if (iod) {
354 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
355 iod->npages = -1;
356 iod->length = nbytes;
Keith Busch2b196032012-11-06 11:59:23 -0700357 iod->nents = 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600358 iod->first_dma = 0ULL;
Keith Busch61982212013-05-29 15:59:39 -0600359 iod->start_time = jiffies;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500360 }
361
362 return iod;
363}
364
Vishal Verma5d0f6132013-03-04 18:40:58 -0700365void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500366{
367 const int last_prp = PAGE_SIZE / 8 - 1;
368 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500369 __le64 **list = iod_list(iod);
370 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500371
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500372 if (iod->npages == 0)
373 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
374 for (i = 0; i < iod->npages; i++) {
375 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500376 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500377 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500378 prp_dma = next_prp_dma;
379 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500380 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500381}
382
Keith Busch61982212013-05-29 15:59:39 -0600383static void nvme_start_io_acct(struct bio *bio)
384{
385 struct gendisk *disk = bio->bi_bdev->bd_disk;
386 const int rw = bio_data_dir(bio);
387 int cpu = part_stat_lock();
388 part_round_stats(cpu, &disk->part0);
389 part_stat_inc(cpu, &disk->part0, ios[rw]);
390 part_stat_add(cpu, &disk->part0, sectors[rw], bio_sectors(bio));
391 part_inc_in_flight(&disk->part0, rw);
392 part_stat_unlock();
393}
394
395static void nvme_end_io_acct(struct bio *bio, unsigned long start_time)
396{
397 struct gendisk *disk = bio->bi_bdev->bd_disk;
398 const int rw = bio_data_dir(bio);
399 unsigned long duration = jiffies - start_time;
400 int cpu = part_stat_lock();
401 part_stat_add(cpu, &disk->part0, ticks[rw], duration);
402 part_round_stats(cpu, &disk->part0);
403 part_dec_in_flight(&disk->part0, rw);
404 part_stat_unlock();
405}
406
Keith Buschedd10d32014-04-03 16:45:23 -0600407static void bio_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500408 struct nvme_completion *cqe)
409{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500410 struct nvme_iod *iod = ctx;
411 struct bio *bio = iod->private;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500412 u16 status = le16_to_cpup(&cqe->status) >> 1;
413
Keith Buschedd10d32014-04-03 16:45:23 -0600414 if (unlikely(status)) {
415 if (!(status & NVME_SC_DNR ||
416 bio->bi_rw & REQ_FAILFAST_MASK) &&
417 (jiffies - iod->start_time) < IOD_TIMEOUT) {
418 if (!waitqueue_active(&nvmeq->sq_full))
419 add_wait_queue(&nvmeq->sq_full,
420 &nvmeq->sq_cong_wait);
421 list_add_tail(&iod->node, &nvmeq->iod_bio);
422 wake_up(&nvmeq->sq_full);
423 return;
424 }
425 }
Keith Busch9e59d092013-08-08 10:25:38 -0600426 if (iod->nents) {
Keith Buschedd10d32014-04-03 16:45:23 -0600427 dma_unmap_sg(nvmeq->q_dmadev, iod->sg, iod->nents,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500428 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busch9e59d092013-08-08 10:25:38 -0600429 nvme_end_io_acct(bio, iod->start_time);
430 }
Keith Buschedd10d32014-04-03 16:45:23 -0600431 nvme_free_iod(nvmeq->dev, iod);
Keith Busch427e9702013-04-09 11:59:32 -0600432 if (status)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500433 bio_endio(bio, -EIO);
Keith Busch427e9702013-04-09 11:59:32 -0600434 else
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500435 bio_endio(bio, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500436}
437
Matthew Wilcox184d2942011-05-11 21:36:38 -0400438/* length is in bytes. gfp flags indicates whether we may sleep. */
Keith Buschedd10d32014-04-03 16:45:23 -0600439int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod, int total_len,
440 gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500441{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500442 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500443 int length = total_len;
444 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500445 int dma_len = sg_dma_len(sg);
446 u64 dma_addr = sg_dma_address(sg);
447 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500448 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500449 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500450 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500451 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500452
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500453 length -= (PAGE_SIZE - offset);
454 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500455 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500456
457 dma_len -= (PAGE_SIZE - offset);
458 if (dma_len) {
459 dma_addr += (PAGE_SIZE - offset);
460 } else {
461 sg = sg_next(sg);
462 dma_addr = sg_dma_address(sg);
463 dma_len = sg_dma_len(sg);
464 }
465
466 if (length <= PAGE_SIZE) {
Keith Buschedd10d32014-04-03 16:45:23 -0600467 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500468 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500469 }
470
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500471 nprps = DIV_ROUND_UP(length, PAGE_SIZE);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500472 if (nprps <= (256 / 8)) {
473 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500474 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500475 } else {
476 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500477 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500478 }
479
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400480 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
481 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600482 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500483 iod->npages = -1;
484 return (total_len - length) + PAGE_SIZE;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400485 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500486 list[0] = prp_list;
487 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500488 i = 0;
489 for (;;) {
Matthew Wilcox7523d832011-03-16 16:43:40 -0400490 if (i == PAGE_SIZE / 8) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500491 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400492 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500493 if (!prp_list)
494 return total_len - length;
495 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400496 prp_list[0] = old_prp_list[i - 1];
497 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
498 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500499 }
500 prp_list[i++] = cpu_to_le64(dma_addr);
501 dma_len -= PAGE_SIZE;
502 dma_addr += PAGE_SIZE;
503 length -= PAGE_SIZE;
504 if (length <= 0)
505 break;
506 if (dma_len > 0)
507 continue;
508 BUG_ON(dma_len < 0);
509 sg = sg_next(sg);
510 dma_addr = sg_dma_address(sg);
511 dma_len = sg_dma_len(sg);
512 }
513
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500514 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500515}
516
Keith Busch427e9702013-04-09 11:59:32 -0600517static int nvme_split_and_submit(struct bio *bio, struct nvme_queue *nvmeq,
Kent Overstreet20d01892013-11-23 18:21:01 -0800518 int len)
Keith Busch427e9702013-04-09 11:59:32 -0600519{
Kent Overstreet20d01892013-11-23 18:21:01 -0800520 struct bio *split = bio_split(bio, len >> 9, GFP_ATOMIC, NULL);
521 if (!split)
Keith Busch427e9702013-04-09 11:59:32 -0600522 return -ENOMEM;
523
Kent Overstreet20d01892013-11-23 18:21:01 -0800524 bio_chain(split, bio);
525
Keith Buschedd10d32014-04-03 16:45:23 -0600526 if (!waitqueue_active(&nvmeq->sq_full))
Keith Busch427e9702013-04-09 11:59:32 -0600527 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
Kent Overstreet20d01892013-11-23 18:21:01 -0800528 bio_list_add(&nvmeq->sq_cong, split);
529 bio_list_add(&nvmeq->sq_cong, bio);
Keith Buschedd10d32014-04-03 16:45:23 -0600530 wake_up(&nvmeq->sq_full);
Keith Busch427e9702013-04-09 11:59:32 -0600531
532 return 0;
533}
534
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500535/* NVMe scatterlists require no holes in the virtual address */
536#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
537 (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
538
Keith Busch427e9702013-04-09 11:59:32 -0600539static int nvme_map_bio(struct nvme_queue *nvmeq, struct nvme_iod *iod,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500540 struct bio *bio, enum dma_data_direction dma_dir, int psegs)
541{
Kent Overstreet79886132013-11-23 17:19:00 -0800542 struct bio_vec bvec, bvprv;
543 struct bvec_iter iter;
Matthew Wilcox76830842011-02-10 13:55:39 -0500544 struct scatterlist *sg = NULL;
Kent Overstreet79886132013-11-23 17:19:00 -0800545 int length = 0, nsegs = 0, split_len = bio->bi_iter.bi_size;
546 int first = 1;
Keith Busch159b67d2013-04-09 17:13:20 -0600547
548 if (nvmeq->dev->stripe_size)
549 split_len = nvmeq->dev->stripe_size -
Kent Overstreet4f024f32013-10-11 15:44:27 -0700550 ((bio->bi_iter.bi_sector << 9) &
551 (nvmeq->dev->stripe_size - 1));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500552
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500553 sg_init_table(iod->sg, psegs);
Kent Overstreet79886132013-11-23 17:19:00 -0800554 bio_for_each_segment(bvec, bio, iter) {
555 if (!first && BIOVEC_PHYS_MERGEABLE(&bvprv, &bvec)) {
556 sg->length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500557 } else {
Kent Overstreet79886132013-11-23 17:19:00 -0800558 if (!first && BIOVEC_NOT_VIRT_MERGEABLE(&bvprv, &bvec))
559 return nvme_split_and_submit(bio, nvmeq,
Kent Overstreet20d01892013-11-23 18:21:01 -0800560 length);
Keith Busch427e9702013-04-09 11:59:32 -0600561
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500562 sg = sg ? sg + 1 : iod->sg;
Kent Overstreet79886132013-11-23 17:19:00 -0800563 sg_set_page(sg, bvec.bv_page,
564 bvec.bv_len, bvec.bv_offset);
Matthew Wilcox76830842011-02-10 13:55:39 -0500565 nsegs++;
566 }
Keith Busch159b67d2013-04-09 17:13:20 -0600567
Kent Overstreet79886132013-11-23 17:19:00 -0800568 if (split_len - length < bvec.bv_len)
Kent Overstreet20d01892013-11-23 18:21:01 -0800569 return nvme_split_and_submit(bio, nvmeq, split_len);
Kent Overstreet79886132013-11-23 17:19:00 -0800570 length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500571 bvprv = bvec;
Kent Overstreet79886132013-11-23 17:19:00 -0800572 first = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500573 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500574 iod->nents = nsegs;
Matthew Wilcox76830842011-02-10 13:55:39 -0500575 sg_mark_end(sg);
Keith Busch427e9702013-04-09 11:59:32 -0600576 if (dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir) == 0)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500577 return -ENOMEM;
Keith Busch427e9702013-04-09 11:59:32 -0600578
Kent Overstreet4f024f32013-10-11 15:44:27 -0700579 BUG_ON(length != bio->bi_iter.bi_size);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500580 return length;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500581}
582
Keith Busch0e5e4f02012-11-09 16:33:05 -0700583static int nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
584 struct bio *bio, struct nvme_iod *iod, int cmdid)
585{
Keith Buschedd10d32014-04-03 16:45:23 -0600586 struct nvme_dsm_range *range =
587 (struct nvme_dsm_range *)iod_list(iod)[0];
Keith Busch0e5e4f02012-11-09 16:33:05 -0700588 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
589
Keith Busch0e5e4f02012-11-09 16:33:05 -0700590 range->cattr = cpu_to_le32(0);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700591 range->nlb = cpu_to_le32(bio->bi_iter.bi_size >> ns->lba_shift);
592 range->slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700593
594 memset(cmnd, 0, sizeof(*cmnd));
595 cmnd->dsm.opcode = nvme_cmd_dsm;
596 cmnd->dsm.command_id = cmdid;
597 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
598 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
599 cmnd->dsm.nr = 0;
600 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
601
602 if (++nvmeq->sq_tail == nvmeq->q_depth)
603 nvmeq->sq_tail = 0;
604 writel(nvmeq->sq_tail, nvmeq->q_db);
605
606 return 0;
607}
608
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500609static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
610 int cmdid)
611{
612 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
613
614 memset(cmnd, 0, sizeof(*cmnd));
615 cmnd->common.opcode = nvme_cmd_flush;
616 cmnd->common.command_id = cmdid;
617 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
618
619 if (++nvmeq->sq_tail == nvmeq->q_depth)
620 nvmeq->sq_tail = 0;
621 writel(nvmeq->sq_tail, nvmeq->q_db);
622
623 return 0;
624}
625
Vishal Verma5d0f6132013-03-04 18:40:58 -0700626int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500627{
628 int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH,
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500629 special_completion, NVME_IO_TIMEOUT);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500630 if (unlikely(cmdid < 0))
631 return cmdid;
632
633 return nvme_submit_flush(nvmeq, ns, cmdid);
634}
635
Keith Buschedd10d32014-04-03 16:45:23 -0600636static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500637{
Keith Buschedd10d32014-04-03 16:45:23 -0600638 struct bio *bio = iod->private;
639 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500640 struct nvme_command *cmnd;
Keith Buschedd10d32014-04-03 16:45:23 -0600641 int cmdid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500642 u16 control;
643 u32 dsmgmt;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500644
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500645 cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500646 if (unlikely(cmdid < 0))
Keith Buschedd10d32014-04-03 16:45:23 -0600647 return cmdid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500648
Keith Buschedd10d32014-04-03 16:45:23 -0600649 if (bio->bi_rw & REQ_DISCARD)
650 return nvme_submit_discard(nvmeq, ns, bio, iod, cmdid);
651 if ((bio->bi_rw & REQ_FLUSH) && !iod->nents)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500652 return nvme_submit_flush(nvmeq, ns, cmdid);
653
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500654 control = 0;
655 if (bio->bi_rw & REQ_FUA)
656 control |= NVME_RW_FUA;
657 if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
658 control |= NVME_RW_LR;
659
660 dsmgmt = 0;
661 if (bio->bi_rw & REQ_RAHEAD)
662 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
663
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500664 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500665 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500666
Keith Buschedd10d32014-04-03 16:45:23 -0600667 cmnd->rw.opcode = bio_data_dir(bio) ? nvme_cmd_write : nvme_cmd_read;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500668 cmnd->rw.command_id = cmdid;
669 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Keith Buschedd10d32014-04-03 16:45:23 -0600670 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
671 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700672 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Keith Buschedd10d32014-04-03 16:45:23 -0600673 cmnd->rw.length =
674 cpu_to_le16((bio->bi_iter.bi_size >> ns->lba_shift) - 1);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500675 cmnd->rw.control = cpu_to_le16(control);
676 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500677
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500678 if (++nvmeq->sq_tail == nvmeq->q_depth)
679 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500680 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500681
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500682 return 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600683}
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500684
Keith Buschedd10d32014-04-03 16:45:23 -0600685/*
686 * Called with local interrupts disabled and the q_lock held. May not sleep.
687 */
688static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
689 struct bio *bio)
690{
691 struct nvme_iod *iod;
692 int psegs = bio_phys_segments(ns->queue, bio);
693 int result;
694
695 if ((bio->bi_rw & REQ_FLUSH) && psegs) {
696 result = nvme_submit_flush_data(nvmeq, ns);
697 if (result)
698 return result;
699 }
700
701 iod = nvme_alloc_iod(psegs, bio->bi_iter.bi_size, GFP_ATOMIC);
702 if (!iod)
703 return -ENOMEM;
704
705 iod->private = bio;
706 if (bio->bi_rw & REQ_DISCARD) {
707 void *range;
708 /*
709 * We reuse the small pool to allocate the 16-byte range here
710 * as it is not worth having a special pool for these or
711 * additional cases to handle freeing the iod.
712 */
713 range = dma_pool_alloc(nvmeq->dev->prp_small_pool,
714 GFP_ATOMIC,
715 &iod->first_dma);
716 if (!range) {
717 result = -ENOMEM;
718 goto free_iod;
719 }
720 iod_list(iod)[0] = (__le64 *)range;
721 iod->npages = 0;
722 } else if (psegs) {
723 result = nvme_map_bio(nvmeq, iod, bio,
724 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE,
725 psegs);
726 if (result <= 0)
727 goto free_iod;
728 if (nvme_setup_prps(nvmeq->dev, iod, result, GFP_ATOMIC) !=
729 result) {
730 result = -ENOMEM;
731 goto free_iod;
732 }
733 nvme_start_io_acct(bio);
734 }
735 if (unlikely(nvme_submit_iod(nvmeq, iod))) {
736 if (!waitqueue_active(&nvmeq->sq_full))
737 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
738 list_add_tail(&iod->node, &nvmeq->iod_bio);
739 }
740 return 0;
741
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500742 free_iod:
743 nvme_free_iod(nvmeq->dev, iod);
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500744 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500745}
746
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400747static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500748{
Matthew Wilcox82123462011-01-20 13:24:06 -0500749 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500750
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500751 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500752 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500753
754 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400755 void *ctx;
756 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500757 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500758 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500759 break;
760 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
761 if (++head == nvmeq->q_depth) {
762 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500763 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500764 }
765
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400766 ctx = free_cmdid(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600767 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500768 }
769
770 /* If the controller ignores the cq head doorbell and continuously
771 * writes to the queue, it is theoretically possible to wrap around
772 * the queue twice and mistakenly return IRQ_NONE. Linux only
773 * requires that 0.1% of your interrupts are handled, so this isn't
774 * a big problem.
775 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500776 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400777 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500778
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800779 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500780 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500781 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500782
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400783 nvmeq->cqe_seen = 1;
784 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500785}
786
Matthew Wilcox7d822452013-06-24 12:03:57 -0400787static void nvme_make_request(struct request_queue *q, struct bio *bio)
788{
789 struct nvme_ns *ns = q->queuedata;
790 struct nvme_queue *nvmeq = get_nvmeq(ns->dev);
791 int result = -EBUSY;
792
Keith Buschcd638942013-07-15 15:02:23 -0600793 if (!nvmeq) {
794 put_nvmeq(NULL);
795 bio_endio(bio, -EIO);
796 return;
797 }
798
Matthew Wilcox7d822452013-06-24 12:03:57 -0400799 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -0600800 if (!nvmeq->q_suspended && bio_list_empty(&nvmeq->sq_cong))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400801 result = nvme_submit_bio_queue(nvmeq, ns, bio);
802 if (unlikely(result)) {
Keith Buschedd10d32014-04-03 16:45:23 -0600803 if (!waitqueue_active(&nvmeq->sq_full))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400804 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
805 bio_list_add(&nvmeq->sq_cong, bio);
806 }
807
808 nvme_process_cq(nvmeq);
809 spin_unlock_irq(&nvmeq->q_lock);
810 put_nvmeq(nvmeq);
811}
812
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500813static irqreturn_t nvme_irq(int irq, void *data)
814{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500815 irqreturn_t result;
816 struct nvme_queue *nvmeq = data;
817 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400818 nvme_process_cq(nvmeq);
819 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
820 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500821 spin_unlock(&nvmeq->q_lock);
822 return result;
823}
824
825static irqreturn_t nvme_irq_check(int irq, void *data)
826{
827 struct nvme_queue *nvmeq = data;
828 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
829 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
830 return IRQ_NONE;
831 return IRQ_WAKE_THREAD;
832}
833
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500834static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
835{
836 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400837 cancel_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500838 spin_unlock_irq(&nvmeq->q_lock);
839}
840
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400841struct sync_cmd_info {
842 struct task_struct *task;
843 u32 result;
844 int status;
845};
846
Keith Buschedd10d32014-04-03 16:45:23 -0600847static void sync_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400848 struct nvme_completion *cqe)
849{
850 struct sync_cmd_info *cmdinfo = ctx;
851 cmdinfo->result = le32_to_cpup(&cqe->result);
852 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
853 wake_up_process(cmdinfo->task);
854}
855
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500856/*
857 * Returns 0 on success. If the result is negative, it's a Linux error code;
858 * if the result is positive, it's an NVM Express status code
859 */
Keith Busch4f5099a2014-03-03 16:39:13 -0700860static int nvme_submit_sync_cmd(struct nvme_dev *dev, int q_idx,
861 struct nvme_command *cmd,
Vishal Verma5d0f6132013-03-04 18:40:58 -0700862 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500863{
Keith Busch4f5099a2014-03-03 16:39:13 -0700864 int cmdid, ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500865 struct sync_cmd_info cmdinfo;
Keith Busch4f5099a2014-03-03 16:39:13 -0700866 struct nvme_queue *nvmeq;
867
868 nvmeq = lock_nvmeq(dev, q_idx);
869 if (!nvmeq) {
870 unlock_nvmeq(nvmeq);
871 return -ENODEV;
872 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500873
874 cmdinfo.task = current;
875 cmdinfo.status = -EINTR;
876
Keith Busch4f5099a2014-03-03 16:39:13 -0700877 cmdid = alloc_cmdid(nvmeq, &cmdinfo, sync_completion, timeout);
878 if (cmdid < 0) {
879 unlock_nvmeq(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500880 return cmdid;
Keith Busch4f5099a2014-03-03 16:39:13 -0700881 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500882 cmd->common.command_id = cmdid;
883
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500884 set_current_state(TASK_KILLABLE);
Keith Busch4f5099a2014-03-03 16:39:13 -0700885 ret = nvme_submit_cmd(nvmeq, cmd);
886 if (ret) {
887 free_cmdid(nvmeq, cmdid, NULL);
888 unlock_nvmeq(nvmeq);
889 set_current_state(TASK_RUNNING);
890 return ret;
891 }
892 unlock_nvmeq(nvmeq);
Keith Busch78f8d252013-04-19 14:11:06 -0600893 schedule_timeout(timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500894
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500895 if (cmdinfo.status == -EINTR) {
Keith Busch4f5099a2014-03-03 16:39:13 -0700896 nvmeq = lock_nvmeq(dev, q_idx);
897 if (nvmeq)
898 nvme_abort_command(nvmeq, cmdid);
899 unlock_nvmeq(nvmeq);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500900 return -EINTR;
901 }
902
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500903 if (result)
904 *result = cmdinfo.result;
905
906 return cmdinfo.status;
907}
908
Keith Busch4d115422013-12-10 13:10:40 -0700909static int nvme_submit_async_cmd(struct nvme_queue *nvmeq,
910 struct nvme_command *cmd,
911 struct async_cmd_info *cmdinfo, unsigned timeout)
912{
913 int cmdid;
914
915 cmdid = alloc_cmdid_killable(nvmeq, cmdinfo, async_completion, timeout);
916 if (cmdid < 0)
917 return cmdid;
918 cmdinfo->status = -EINTR;
919 cmd->common.command_id = cmdid;
Keith Busch4f5099a2014-03-03 16:39:13 -0700920 return nvme_submit_cmd(nvmeq, cmd);
Keith Busch4d115422013-12-10 13:10:40 -0700921}
922
Vishal Verma5d0f6132013-03-04 18:40:58 -0700923int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500924 u32 *result)
925{
Keith Busch4f5099a2014-03-03 16:39:13 -0700926 return nvme_submit_sync_cmd(dev, 0, cmd, result, ADMIN_TIMEOUT);
927}
928
929int nvme_submit_io_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
930 u32 *result)
931{
932 return nvme_submit_sync_cmd(dev, smp_processor_id() + 1, cmd, result,
933 NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500934}
935
Keith Busch4d115422013-12-10 13:10:40 -0700936static int nvme_submit_admin_cmd_async(struct nvme_dev *dev,
937 struct nvme_command *cmd, struct async_cmd_info *cmdinfo)
938{
Keith Busch5a92e702014-02-21 14:13:44 -0700939 return nvme_submit_async_cmd(raw_nvmeq(dev, 0), cmd, cmdinfo,
Keith Busch4d115422013-12-10 13:10:40 -0700940 ADMIN_TIMEOUT);
941}
942
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500943static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
944{
945 int status;
946 struct nvme_command c;
947
948 memset(&c, 0, sizeof(c));
949 c.delete_queue.opcode = opcode;
950 c.delete_queue.qid = cpu_to_le16(id);
951
952 status = nvme_submit_admin_cmd(dev, &c, NULL);
953 if (status)
954 return -EIO;
955 return 0;
956}
957
958static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
959 struct nvme_queue *nvmeq)
960{
961 int status;
962 struct nvme_command c;
963 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
964
965 memset(&c, 0, sizeof(c));
966 c.create_cq.opcode = nvme_admin_create_cq;
967 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
968 c.create_cq.cqid = cpu_to_le16(qid);
969 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
970 c.create_cq.cq_flags = cpu_to_le16(flags);
971 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
972
973 status = nvme_submit_admin_cmd(dev, &c, NULL);
974 if (status)
975 return -EIO;
976 return 0;
977}
978
979static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
980 struct nvme_queue *nvmeq)
981{
982 int status;
983 struct nvme_command c;
984 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
985
986 memset(&c, 0, sizeof(c));
987 c.create_sq.opcode = nvme_admin_create_sq;
988 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
989 c.create_sq.sqid = cpu_to_le16(qid);
990 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
991 c.create_sq.sq_flags = cpu_to_le16(flags);
992 c.create_sq.cqid = cpu_to_le16(qid);
993
994 status = nvme_submit_admin_cmd(dev, &c, NULL);
995 if (status)
996 return -EIO;
997 return 0;
998}
999
1000static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1001{
1002 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1003}
1004
1005static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1006{
1007 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1008}
1009
Vishal Verma5d0f6132013-03-04 18:40:58 -07001010int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001011 dma_addr_t dma_addr)
1012{
1013 struct nvme_command c;
1014
1015 memset(&c, 0, sizeof(c));
1016 c.identify.opcode = nvme_admin_identify;
1017 c.identify.nsid = cpu_to_le32(nsid);
1018 c.identify.prp1 = cpu_to_le64(dma_addr);
1019 c.identify.cns = cpu_to_le32(cns);
1020
1021 return nvme_submit_admin_cmd(dev, &c, NULL);
1022}
1023
Vishal Verma5d0f6132013-03-04 18:40:58 -07001024int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -06001025 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001026{
1027 struct nvme_command c;
1028
1029 memset(&c, 0, sizeof(c));
1030 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -06001031 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001032 c.features.prp1 = cpu_to_le64(dma_addr);
1033 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001034
Keith Busch08df1e02012-09-21 10:52:13 -06001035 return nvme_submit_admin_cmd(dev, &c, result);
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001036}
1037
Vishal Verma5d0f6132013-03-04 18:40:58 -07001038int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
1039 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001040{
1041 struct nvme_command c;
1042
1043 memset(&c, 0, sizeof(c));
1044 c.features.opcode = nvme_admin_set_features;
1045 c.features.prp1 = cpu_to_le64(dma_addr);
1046 c.features.fid = cpu_to_le32(fid);
1047 c.features.dword11 = cpu_to_le32(dword11);
1048
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001049 return nvme_submit_admin_cmd(dev, &c, result);
1050}
1051
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001052/**
Keith Buschc30341d2013-12-10 13:10:38 -07001053 * nvme_abort_cmd - Attempt aborting a command
1054 * @cmdid: Command id of a timed out IO
1055 * @queue: The queue with timed out IO
1056 *
1057 * Schedule controller reset if the command was already aborted once before and
1058 * still hasn't been returned to the driver, or if this is the admin queue.
1059 */
1060static void nvme_abort_cmd(int cmdid, struct nvme_queue *nvmeq)
1061{
1062 int a_cmdid;
1063 struct nvme_command cmd;
1064 struct nvme_dev *dev = nvmeq->dev;
1065 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Keith Busch5a92e702014-02-21 14:13:44 -07001066 struct nvme_queue *adminq;
Keith Buschc30341d2013-12-10 13:10:38 -07001067
1068 if (!nvmeq->qid || info[cmdid].aborted) {
1069 if (work_busy(&dev->reset_work))
1070 return;
1071 list_del_init(&dev->node);
1072 dev_warn(&dev->pci_dev->dev,
1073 "I/O %d QID %d timeout, reset controller\n", cmdid,
1074 nvmeq->qid);
Tejun Heo9ca97372014-03-07 10:24:49 -05001075 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschc30341d2013-12-10 13:10:38 -07001076 queue_work(nvme_workq, &dev->reset_work);
1077 return;
1078 }
1079
1080 if (!dev->abort_limit)
1081 return;
1082
Keith Busch5a92e702014-02-21 14:13:44 -07001083 adminq = rcu_dereference(dev->queues[0]);
1084 a_cmdid = alloc_cmdid(adminq, CMD_CTX_ABORT, special_completion,
Keith Buschc30341d2013-12-10 13:10:38 -07001085 ADMIN_TIMEOUT);
1086 if (a_cmdid < 0)
1087 return;
1088
1089 memset(&cmd, 0, sizeof(cmd));
1090 cmd.abort.opcode = nvme_admin_abort_cmd;
1091 cmd.abort.cid = cmdid;
1092 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
1093 cmd.abort.command_id = a_cmdid;
1094
1095 --dev->abort_limit;
1096 info[cmdid].aborted = 1;
1097 info[cmdid].timeout = jiffies + ADMIN_TIMEOUT;
1098
1099 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", cmdid,
1100 nvmeq->qid);
Keith Busch5a92e702014-02-21 14:13:44 -07001101 nvme_submit_cmd(adminq, &cmd);
Keith Buschc30341d2013-12-10 13:10:38 -07001102}
1103
1104/**
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001105 * nvme_cancel_ios - Cancel outstanding I/Os
1106 * @queue: The queue to cancel I/Os on
1107 * @timeout: True to only cancel I/Os which have timed out
1108 */
1109static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout)
1110{
1111 int depth = nvmeq->q_depth - 1;
1112 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
1113 unsigned long now = jiffies;
1114 int cmdid;
1115
1116 for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
1117 void *ctx;
1118 nvme_completion_fn fn;
1119 static struct nvme_completion cqe = {
Matthew Wilcoxaf2d9ca2013-04-16 15:18:30 -04001120 .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1),
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001121 };
1122
1123 if (timeout && !time_after(now, info[cmdid].timeout))
1124 continue;
Keith Busch053ab7022013-04-30 11:19:38 -06001125 if (info[cmdid].ctx == CMD_CTX_CANCELLED)
1126 continue;
Keith Buschc30341d2013-12-10 13:10:38 -07001127 if (timeout && nvmeq->dev->initialized) {
1128 nvme_abort_cmd(cmdid, nvmeq);
1129 continue;
1130 }
1131 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n", cmdid,
1132 nvmeq->qid);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001133 ctx = cancel_cmdid(nvmeq, cmdid, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -06001134 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001135 }
1136}
1137
Keith Busch5a92e702014-02-21 14:13:44 -07001138static void nvme_free_queue(struct rcu_head *r)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001139{
Keith Busch5a92e702014-02-21 14:13:44 -07001140 struct nvme_queue *nvmeq = container_of(r, struct nvme_queue, r_head);
1141
Keith Busch22404272013-07-15 15:02:20 -06001142 spin_lock_irq(&nvmeq->q_lock);
1143 while (bio_list_peek(&nvmeq->sq_cong)) {
1144 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1145 bio_endio(bio, -EIO);
1146 }
Keith Buschedd10d32014-04-03 16:45:23 -06001147 while (!list_empty(&nvmeq->iod_bio)) {
1148 static struct nvme_completion cqe = {
1149 .status = cpu_to_le16(
1150 (NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1),
1151 };
1152 struct nvme_iod *iod = list_first_entry(&nvmeq->iod_bio,
1153 struct nvme_iod,
1154 node);
1155 list_del(&iod->node);
1156 bio_completion(nvmeq, iod, &cqe);
1157 }
Keith Busch22404272013-07-15 15:02:20 -06001158 spin_unlock_irq(&nvmeq->q_lock);
1159
Matthew Wilcox9e866772012-08-03 13:55:56 -04001160 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1161 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1162 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1163 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
Keith Busch42f61422014-03-24 10:46:25 -06001164 if (nvmeq->qid)
1165 free_cpumask_var(nvmeq->cpu_mask);
Matthew Wilcox9e866772012-08-03 13:55:56 -04001166 kfree(nvmeq);
1167}
1168
Keith Buscha1a5ef92013-12-16 13:50:00 -05001169static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001170{
1171 int i;
1172
Keith Buscha1a5ef92013-12-16 13:50:00 -05001173 for (i = dev->queue_count - 1; i >= lowest; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07001174 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
1175 rcu_assign_pointer(dev->queues[i], NULL);
1176 call_rcu(&nvmeq->r_head, nvme_free_queue);
Keith Busch22404272013-07-15 15:02:20 -06001177 dev->queue_count--;
Keith Busch22404272013-07-15 15:02:20 -06001178 }
1179}
1180
Keith Busch4d115422013-12-10 13:10:40 -07001181/**
1182 * nvme_suspend_queue - put queue into suspended state
1183 * @nvmeq - queue to suspend
1184 *
1185 * Returns 1 if already suspended, 0 otherwise.
1186 */
1187static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001188{
Keith Busch4d115422013-12-10 13:10:40 -07001189 int vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001190
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001191 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001192 if (nvmeq->q_suspended) {
1193 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07001194 return 1;
Keith Busch32958742012-08-20 14:57:49 -06001195 }
Keith Busch22404272013-07-15 15:02:20 -06001196 nvmeq->q_suspended = 1;
Keith Busch42f61422014-03-24 10:46:25 -06001197 nvmeq->dev->online_queues--;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001198 spin_unlock_irq(&nvmeq->q_lock);
1199
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001200 irq_set_affinity_hint(vector, NULL);
1201 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001202
Keith Busch4d115422013-12-10 13:10:40 -07001203 return 0;
1204}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001205
Keith Busch4d115422013-12-10 13:10:40 -07001206static void nvme_clear_queue(struct nvme_queue *nvmeq)
1207{
Keith Busch22404272013-07-15 15:02:20 -06001208 spin_lock_irq(&nvmeq->q_lock);
1209 nvme_process_cq(nvmeq);
1210 nvme_cancel_ios(nvmeq, false);
1211 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001212}
1213
Keith Busch4d115422013-12-10 13:10:40 -07001214static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1215{
Keith Busch5a92e702014-02-21 14:13:44 -07001216 struct nvme_queue *nvmeq = raw_nvmeq(dev, qid);
Keith Busch4d115422013-12-10 13:10:40 -07001217
1218 if (!nvmeq)
1219 return;
1220 if (nvme_suspend_queue(nvmeq))
1221 return;
1222
Keith Busch0e53d182013-12-10 13:10:39 -07001223 /* Don't tell the adapter to delete the admin queue.
1224 * Don't tell a removed adapter to delete IO queues. */
1225 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001226 adapter_delete_sq(dev, qid);
1227 adapter_delete_cq(dev, qid);
1228 }
Keith Busch4d115422013-12-10 13:10:40 -07001229 nvme_clear_queue(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001230}
1231
1232static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
1233 int depth, int vector)
1234{
1235 struct device *dmadev = &dev->pci_dev->dev;
Keith Busch22404272013-07-15 15:02:20 -06001236 unsigned extra = nvme_queue_extra(depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001237 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
1238 if (!nvmeq)
1239 return NULL;
1240
1241 nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
1242 &nvmeq->cq_dma_addr, GFP_KERNEL);
1243 if (!nvmeq->cqes)
1244 goto free_nvmeq;
1245 memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
1246
1247 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
1248 &nvmeq->sq_dma_addr, GFP_KERNEL);
1249 if (!nvmeq->sq_cmds)
1250 goto free_cqdma;
1251
Keith Busch42f61422014-03-24 10:46:25 -06001252 if (qid && !zalloc_cpumask_var(&nvmeq->cpu_mask, GFP_KERNEL))
1253 goto free_sqdma;
1254
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001255 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001256 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001257 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1258 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001259 spin_lock_init(&nvmeq->q_lock);
1260 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001261 nvmeq->cq_phase = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001262 init_waitqueue_head(&nvmeq->sq_full);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001263 init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001264 bio_list_init(&nvmeq->sq_cong);
Keith Buschedd10d32014-04-03 16:45:23 -06001265 INIT_LIST_HEAD(&nvmeq->iod_bio);
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001266 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001267 nvmeq->q_depth = depth;
1268 nvmeq->cq_vector = vector;
Keith Buschc30341d2013-12-10 13:10:38 -07001269 nvmeq->qid = qid;
Keith Busch22404272013-07-15 15:02:20 -06001270 nvmeq->q_suspended = 1;
1271 dev->queue_count++;
Keith Busch5a92e702014-02-21 14:13:44 -07001272 rcu_assign_pointer(dev->queues[qid], nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001273
1274 return nvmeq;
1275
Keith Busch42f61422014-03-24 10:46:25 -06001276 free_sqdma:
1277 dma_free_coherent(dmadev, SQ_SIZE(depth), (void *)nvmeq->sq_cmds,
1278 nvmeq->sq_dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001279 free_cqdma:
Keith Busch68b8eca2013-05-01 13:07:47 -06001280 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001281 nvmeq->cq_dma_addr);
1282 free_nvmeq:
1283 kfree(nvmeq);
1284 return NULL;
1285}
1286
Matthew Wilcox30010822011-01-20 09:10:15 -05001287static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1288 const char *name)
1289{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001290 if (use_threaded_interrupts)
1291 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001292 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001293 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001294 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001295 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001296}
1297
Keith Busch22404272013-07-15 15:02:20 -06001298static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001299{
Keith Busch22404272013-07-15 15:02:20 -06001300 struct nvme_dev *dev = nvmeq->dev;
1301 unsigned extra = nvme_queue_extra(nvmeq->q_depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001302
Keith Busch22404272013-07-15 15:02:20 -06001303 nvmeq->sq_tail = 0;
1304 nvmeq->cq_head = 0;
1305 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001306 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001307 memset(nvmeq->cmdid_data, 0, extra);
1308 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
1309 nvme_cancel_ios(nvmeq, false);
1310 nvmeq->q_suspended = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001311 dev->online_queues++;
Keith Busch22404272013-07-15 15:02:20 -06001312}
1313
1314static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1315{
1316 struct nvme_dev *dev = nvmeq->dev;
1317 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001318
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001319 result = adapter_alloc_cq(dev, qid, nvmeq);
1320 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001321 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001322
1323 result = adapter_alloc_sq(dev, qid, nvmeq);
1324 if (result < 0)
1325 goto release_cq;
1326
Matthew Wilcox3193f072014-01-27 15:57:22 -05001327 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001328 if (result < 0)
1329 goto release_sq;
1330
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001331 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001332 nvme_init_queue(nvmeq, qid);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001333 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001334
1335 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001336
1337 release_sq:
1338 adapter_delete_sq(dev, qid);
1339 release_cq:
1340 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001341 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001342}
1343
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001344static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1345{
1346 unsigned long timeout;
1347 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1348
1349 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1350
1351 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1352 msleep(100);
1353 if (fatal_signal_pending(current))
1354 return -EINTR;
1355 if (time_after(jiffies, timeout)) {
1356 dev_err(&dev->pci_dev->dev,
Matthew Wilcox27e81662014-04-11 11:58:45 -04001357 "Device not ready; aborting %s\n", enabled ?
1358 "initialisation" : "reset");
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001359 return -ENODEV;
1360 }
1361 }
1362
1363 return 0;
1364}
1365
1366/*
1367 * If the device has been passed off to us in an enabled state, just clear
1368 * the enabled bit. The spec says we should set the 'shutdown notification
1369 * bits', but doing so may cause the device to complete commands to the
1370 * admin queue ... and we don't know what memory that might be pointing at!
1371 */
1372static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1373{
Matthew Wilcox44af1462013-05-04 06:43:17 -04001374 u32 cc = readl(&dev->bar->cc);
1375
1376 if (cc & NVME_CC_ENABLE)
1377 writel(cc & ~NVME_CC_ENABLE, &dev->bar->cc);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001378 return nvme_wait_ready(dev, cap, false);
1379}
1380
1381static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1382{
1383 return nvme_wait_ready(dev, cap, true);
1384}
1385
Keith Busch1894d8f2013-07-15 15:02:22 -06001386static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1387{
1388 unsigned long timeout;
1389 u32 cc;
1390
1391 cc = (readl(&dev->bar->cc) & ~NVME_CC_SHN_MASK) | NVME_CC_SHN_NORMAL;
1392 writel(cc, &dev->bar->cc);
1393
1394 timeout = 2 * HZ + jiffies;
1395 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1396 NVME_CSTS_SHST_CMPLT) {
1397 msleep(100);
1398 if (fatal_signal_pending(current))
1399 return -EINTR;
1400 if (time_after(jiffies, timeout)) {
1401 dev_err(&dev->pci_dev->dev,
1402 "Device shutdown incomplete; abort shutdown\n");
1403 return -ENODEV;
1404 }
1405 }
1406
1407 return 0;
1408}
1409
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001410static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001411{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001412 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001413 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001414 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001415 struct nvme_queue *nvmeq;
1416
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001417 result = nvme_disable_ctrl(dev, cap);
1418 if (result < 0)
1419 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001420
Keith Busch5a92e702014-02-21 14:13:44 -07001421 nvmeq = raw_nvmeq(dev, 0);
Keith Buschcd638942013-07-15 15:02:23 -06001422 if (!nvmeq) {
1423 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
1424 if (!nvmeq)
1425 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001426 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001427
1428 aqa = nvmeq->q_depth - 1;
1429 aqa |= aqa << 16;
1430
1431 dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
1432 dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
1433 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001434 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001435
1436 writel(aqa, &dev->bar->aqa);
1437 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1438 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
1439 writel(dev->ctrl_config, &dev->bar->cc);
1440
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001441 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001442 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001443 return result;
Matthew Wilcox9e866772012-08-03 13:55:56 -04001444
Matthew Wilcox3193f072014-01-27 15:57:22 -05001445 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Keith Busch025c5572013-05-01 13:07:51 -06001446 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001447 return result;
Keith Busch025c5572013-05-01 13:07:51 -06001448
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001449 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001450 nvme_init_queue(nvmeq, 0);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001451 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001452 return result;
1453}
1454
Vishal Verma5d0f6132013-03-04 18:40:58 -07001455struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001456 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001457{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001458 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001459 struct scatterlist *sg;
1460 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001461 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001462
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001463 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001464 return ERR_PTR(-EINVAL);
Dan Carpenter5460fc02013-05-13 17:59:50 +03001465 if (!length || length > INT_MAX - PAGE_SIZE)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001466 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001467
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001468 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001469 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1470 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001471 if (!pages)
1472 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001473
1474 err = get_user_pages_fast(addr, count, 1, pages);
1475 if (err < count) {
1476 count = err;
1477 err = -EFAULT;
1478 goto put_pages;
1479 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001480
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001481 iod = nvme_alloc_iod(count, length, GFP_KERNEL);
1482 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001483 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001484 for (i = 0; i < count; i++) {
1485 sg_set_page(&sg[i], pages[i],
Dan Carpenter5460fc02013-05-13 17:59:50 +03001486 min_t(unsigned, length, PAGE_SIZE - offset),
1487 offset);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001488 length -= (PAGE_SIZE - offset);
1489 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001490 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001491 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001492 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001493
1494 err = -ENOMEM;
1495 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1496 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001497 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001498 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001499
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001500 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001501 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001502
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001503 free_iod:
1504 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001505 put_pages:
1506 for (i = 0; i < count; i++)
1507 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001508 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001509 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001510}
1511
Vishal Verma5d0f6132013-03-04 18:40:58 -07001512void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001513 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001514{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001515 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001516
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001517 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1518 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001519
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001520 for (i = 0; i < iod->nents; i++)
1521 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001522}
1523
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001524static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1525{
1526 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001527 struct nvme_user_io io;
1528 struct nvme_command c;
Keith Buschf410c682013-04-23 17:23:59 -06001529 unsigned length, meta_len;
1530 int status, i;
1531 struct nvme_iod *iod, *meta_iod = NULL;
1532 dma_addr_t meta_dma_addr;
1533 void *meta, *uninitialized_var(meta_mem);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001534
1535 if (copy_from_user(&io, uio, sizeof(io)))
1536 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001537 length = (io.nblocks + 1) << ns->lba_shift;
Keith Buschf410c682013-04-23 17:23:59 -06001538 meta_len = (io.nblocks + 1) * ns->ms;
1539
1540 if (meta_len && ((io.metadata & 3) || !io.metadata))
1541 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001542
1543 switch (io.opcode) {
1544 case nvme_cmd_write:
1545 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001546 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001547 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001548 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001549 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001550 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001551 }
1552
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001553 if (IS_ERR(iod))
1554 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001555
1556 memset(&c, 0, sizeof(c));
1557 c.rw.opcode = io.opcode;
1558 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001559 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001560 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001561 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001562 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001563 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1564 c.rw.reftag = cpu_to_le32(io.reftag);
1565 c.rw.apptag = cpu_to_le16(io.apptag);
1566 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buschf410c682013-04-23 17:23:59 -06001567
1568 if (meta_len) {
Keith Busch1b567492013-07-18 12:13:51 -06001569 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
1570 meta_len);
Keith Buschf410c682013-04-23 17:23:59 -06001571 if (IS_ERR(meta_iod)) {
1572 status = PTR_ERR(meta_iod);
1573 meta_iod = NULL;
1574 goto unmap;
1575 }
1576
1577 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1578 &meta_dma_addr, GFP_KERNEL);
1579 if (!meta_mem) {
1580 status = -ENOMEM;
1581 goto unmap;
1582 }
1583
1584 if (io.opcode & 1) {
1585 int meta_offset = 0;
1586
1587 for (i = 0; i < meta_iod->nents; i++) {
1588 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1589 meta_iod->sg[i].offset;
1590 memcpy(meta_mem + meta_offset, meta,
1591 meta_iod->sg[i].length);
1592 kunmap_atomic(meta);
1593 meta_offset += meta_iod->sg[i].length;
1594 }
1595 }
1596
1597 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1598 }
1599
Keith Buschedd10d32014-04-03 16:45:23 -06001600 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1601 c.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1602 c.rw.prp2 = cpu_to_le64(iod->first_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001603
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001604 if (length != (io.nblocks + 1) << ns->lba_shift)
1605 status = -ENOMEM;
1606 else
Keith Busch4f5099a2014-03-03 16:39:13 -07001607 status = nvme_submit_io_cmd(dev, &c, NULL);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001608
Keith Buschf410c682013-04-23 17:23:59 -06001609 if (meta_len) {
1610 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1611 int meta_offset = 0;
1612
1613 for (i = 0; i < meta_iod->nents; i++) {
1614 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1615 meta_iod->sg[i].offset;
1616 memcpy(meta, meta_mem + meta_offset,
1617 meta_iod->sg[i].length);
1618 kunmap_atomic(meta);
1619 meta_offset += meta_iod->sg[i].length;
1620 }
1621 }
1622
1623 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1624 meta_dma_addr);
1625 }
1626
1627 unmap:
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001628 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001629 nvme_free_iod(dev, iod);
Keith Buschf410c682013-04-23 17:23:59 -06001630
1631 if (meta_iod) {
1632 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1633 nvme_free_iod(dev, meta_iod);
1634 }
1635
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001636 return status;
1637}
1638
Keith Busch50af8ba2012-07-25 16:07:55 -06001639static int nvme_user_admin_cmd(struct nvme_dev *dev,
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001640 struct nvme_admin_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001641{
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001642 struct nvme_admin_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001643 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001644 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001645 struct nvme_iod *uninitialized_var(iod);
Keith Busch94f370c2013-05-09 14:01:38 -06001646 unsigned timeout;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001647
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001648 if (!capable(CAP_SYS_ADMIN))
1649 return -EACCES;
1650 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001651 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001652
1653 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001654 c.common.opcode = cmd.opcode;
1655 c.common.flags = cmd.flags;
1656 c.common.nsid = cpu_to_le32(cmd.nsid);
1657 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1658 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1659 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1660 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1661 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1662 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1663 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1664 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1665
1666 length = cmd.data_len;
1667 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001668 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1669 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001670 if (IS_ERR(iod))
1671 return PTR_ERR(iod);
Keith Buschedd10d32014-04-03 16:45:23 -06001672 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1673 c.common.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1674 c.common.prp2 = cpu_to_le64(iod->first_dma);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001675 }
1676
Keith Busch94f370c2013-05-09 14:01:38 -06001677 timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
1678 ADMIN_TIMEOUT;
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001679 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001680 status = -ENOMEM;
1681 else
Keith Busch4f5099a2014-03-03 16:39:13 -07001682 status = nvme_submit_sync_cmd(dev, 0, &c, &cmd.result, timeout);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001683
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001684 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001685 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001686 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001687 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001688
Chayan Biswascf90bc42013-05-22 22:34:49 +00001689 if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
Keith Buschf4f117f2012-09-21 10:49:05 -06001690 sizeof(cmd.result)))
1691 status = -EFAULT;
1692
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001693 return status;
1694}
1695
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001696static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1697 unsigned long arg)
1698{
1699 struct nvme_ns *ns = bdev->bd_disk->private_data;
1700
1701 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001702 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001703 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001704 return ns->ns_id;
1705 case NVME_IOCTL_ADMIN_CMD:
Keith Busch50af8ba2012-07-25 16:07:55 -06001706 return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001707 case NVME_IOCTL_SUBMIT_IO:
1708 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001709 case SG_GET_VERSION_NUM:
1710 return nvme_sg_get_version_num((void __user *)arg);
1711 case SG_IO:
1712 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001713 default:
1714 return -ENOTTY;
1715 }
1716}
1717
Keith Busch320a3822013-10-23 13:07:34 -06001718#ifdef CONFIG_COMPAT
1719static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1720 unsigned int cmd, unsigned long arg)
1721{
1722 struct nvme_ns *ns = bdev->bd_disk->private_data;
1723
1724 switch (cmd) {
1725 case SG_IO:
1726 return nvme_sg_io32(ns, arg);
1727 }
1728 return nvme_ioctl(bdev, mode, cmd, arg);
1729}
1730#else
1731#define nvme_compat_ioctl NULL
1732#endif
1733
Keith Busch9ac27092014-01-31 16:53:39 -07001734static int nvme_open(struct block_device *bdev, fmode_t mode)
1735{
1736 struct nvme_ns *ns = bdev->bd_disk->private_data;
1737 struct nvme_dev *dev = ns->dev;
1738
1739 kref_get(&dev->kref);
1740 return 0;
1741}
1742
1743static void nvme_free_dev(struct kref *kref);
1744
1745static void nvme_release(struct gendisk *disk, fmode_t mode)
1746{
1747 struct nvme_ns *ns = disk->private_data;
1748 struct nvme_dev *dev = ns->dev;
1749
1750 kref_put(&dev->kref, nvme_free_dev);
1751}
1752
Keith Busch4cc09e22014-04-02 15:45:37 -06001753static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
1754{
1755 /* some standard values */
1756 geo->heads = 1 << 6;
1757 geo->sectors = 1 << 5;
1758 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1759 return 0;
1760}
1761
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001762static const struct block_device_operations nvme_fops = {
1763 .owner = THIS_MODULE,
1764 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06001765 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07001766 .open = nvme_open,
1767 .release = nvme_release,
Keith Busch4cc09e22014-04-02 15:45:37 -06001768 .getgeo = nvme_getgeo,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001769};
1770
Keith Buschedd10d32014-04-03 16:45:23 -06001771static void nvme_resubmit_iods(struct nvme_queue *nvmeq)
1772{
1773 struct nvme_iod *iod, *next;
1774
1775 list_for_each_entry_safe(iod, next, &nvmeq->iod_bio, node) {
1776 if (unlikely(nvme_submit_iod(nvmeq, iod)))
1777 break;
1778 list_del(&iod->node);
1779 if (bio_list_empty(&nvmeq->sq_cong) &&
1780 list_empty(&nvmeq->iod_bio))
1781 remove_wait_queue(&nvmeq->sq_full,
1782 &nvmeq->sq_cong_wait);
1783 }
1784}
1785
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001786static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1787{
1788 while (bio_list_peek(&nvmeq->sq_cong)) {
1789 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1790 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Keith Busch427e9702013-04-09 11:59:32 -06001791
Keith Buschedd10d32014-04-03 16:45:23 -06001792 if (bio_list_empty(&nvmeq->sq_cong) &&
1793 list_empty(&nvmeq->iod_bio))
Matthew Wilcox3cb967c2011-03-16 16:45:49 -04001794 remove_wait_queue(&nvmeq->sq_full,
1795 &nvmeq->sq_cong_wait);
Keith Busch427e9702013-04-09 11:59:32 -06001796 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
Keith Buschedd10d32014-04-03 16:45:23 -06001797 if (!waitqueue_active(&nvmeq->sq_full))
Keith Busch427e9702013-04-09 11:59:32 -06001798 add_wait_queue(&nvmeq->sq_full,
1799 &nvmeq->sq_cong_wait);
1800 bio_list_add_head(&nvmeq->sq_cong, bio);
1801 break;
1802 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001803 }
1804}
1805
1806static int nvme_kthread(void *data)
1807{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001808 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001809
1810 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001811 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001812 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001813 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001814 int i;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001815 if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
1816 dev->initialized) {
1817 if (work_busy(&dev->reset_work))
1818 continue;
1819 list_del_init(&dev->node);
1820 dev_warn(&dev->pci_dev->dev,
1821 "Failed status, reset controller\n");
Tejun Heo9ca97372014-03-07 10:24:49 -05001822 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001823 queue_work(nvme_workq, &dev->reset_work);
1824 continue;
1825 }
Keith Busch5a92e702014-02-21 14:13:44 -07001826 rcu_read_lock();
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001827 for (i = 0; i < dev->queue_count; i++) {
Keith Busch5a92e702014-02-21 14:13:44 -07001828 struct nvme_queue *nvmeq =
1829 rcu_dereference(dev->queues[i]);
Matthew Wilcox740216f2011-02-15 16:28:20 -05001830 if (!nvmeq)
1831 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001832 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001833 if (nvmeq->q_suspended)
1834 goto unlock;
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001835 nvme_process_cq(nvmeq);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001836 nvme_cancel_ios(nvmeq, true);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001837 nvme_resubmit_bios(nvmeq);
Keith Buschedd10d32014-04-03 16:45:23 -06001838 nvme_resubmit_iods(nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001839 unlock:
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001840 spin_unlock_irq(&nvmeq->q_lock);
1841 }
Keith Busch5a92e702014-02-21 14:13:44 -07001842 rcu_read_unlock();
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001843 }
1844 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001845 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001846 }
1847 return 0;
1848}
1849
Keith Busch0e5e4f02012-11-09 16:33:05 -07001850static void nvme_config_discard(struct nvme_ns *ns)
1851{
1852 u32 logical_block_size = queue_logical_block_size(ns->queue);
1853 ns->queue->limits.discard_zeroes_data = 0;
1854 ns->queue->limits.discard_alignment = logical_block_size;
1855 ns->queue->limits.discard_granularity = logical_block_size;
1856 ns->queue->limits.max_discard_sectors = 0xffffffff;
1857 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1858}
1859
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001860static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001861 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1862{
1863 struct nvme_ns *ns;
1864 struct gendisk *disk;
1865 int lbaf;
1866
1867 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1868 return NULL;
1869
1870 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1871 if (!ns)
1872 return NULL;
1873 ns->queue = blk_alloc_queue(GFP_KERNEL);
1874 if (!ns->queue)
1875 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001876 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
1877 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1878 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001879 blk_queue_make_request(ns->queue, nvme_make_request);
1880 ns->dev = dev;
1881 ns->queue->queuedata = ns;
1882
Matthew Wilcox469071a2013-12-09 12:58:46 -05001883 disk = alloc_disk(0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001884 if (!disk)
1885 goto out_free_queue;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001886 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001887 ns->disk = disk;
1888 lbaf = id->flbas & 0xf;
1889 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Buschf410c682013-04-23 17:23:59 -06001890 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busche9ef4632012-07-24 15:01:04 -06001891 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06001892 if (dev->max_hw_sectors)
1893 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001894
1895 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001896 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001897 disk->fops = &nvme_fops;
1898 disk->private_data = ns;
1899 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05001900 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001901 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001902 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001903 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1904
Keith Busch0e5e4f02012-11-09 16:33:05 -07001905 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1906 nvme_config_discard(ns);
1907
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001908 return ns;
1909
1910 out_free_queue:
1911 blk_cleanup_queue(ns->queue);
1912 out_free_ns:
1913 kfree(ns);
1914 return NULL;
1915}
1916
Keith Busch42f61422014-03-24 10:46:25 -06001917static int nvme_find_closest_node(int node)
1918{
1919 int n, val, min_val = INT_MAX, best_node = node;
1920
1921 for_each_online_node(n) {
1922 if (n == node)
1923 continue;
1924 val = node_distance(node, n);
1925 if (val < min_val) {
1926 min_val = val;
1927 best_node = n;
1928 }
1929 }
1930 return best_node;
1931}
1932
1933static void nvme_set_queue_cpus(cpumask_t *qmask, struct nvme_queue *nvmeq,
1934 int count)
1935{
1936 int cpu;
1937 for_each_cpu(cpu, qmask) {
1938 if (cpumask_weight(nvmeq->cpu_mask) >= count)
1939 break;
1940 if (!cpumask_test_and_set_cpu(cpu, nvmeq->cpu_mask))
1941 *per_cpu_ptr(nvmeq->dev->io_queue, cpu) = nvmeq->qid;
1942 }
1943}
1944
1945static void nvme_add_cpus(cpumask_t *mask, const cpumask_t *unassigned_cpus,
1946 const cpumask_t *new_mask, struct nvme_queue *nvmeq, int cpus_per_queue)
1947{
1948 int next_cpu;
1949 for_each_cpu(next_cpu, new_mask) {
1950 cpumask_or(mask, mask, get_cpu_mask(next_cpu));
1951 cpumask_or(mask, mask, topology_thread_cpumask(next_cpu));
1952 cpumask_and(mask, mask, unassigned_cpus);
1953 nvme_set_queue_cpus(mask, nvmeq, cpus_per_queue);
1954 }
1955}
1956
1957static void nvme_create_io_queues(struct nvme_dev *dev)
1958{
1959 unsigned i, max;
1960
1961 max = min(dev->max_qid, num_online_cpus());
1962 for (i = dev->queue_count; i <= max; i++)
1963 if (!nvme_alloc_queue(dev, i, dev->q_depth, i - 1))
1964 break;
1965
1966 max = min(dev->queue_count - 1, num_online_cpus());
1967 for (i = dev->online_queues; i <= max; i++)
1968 if (nvme_create_queue(raw_nvmeq(dev, i), i))
1969 break;
1970}
1971
1972/*
1973 * If there are fewer queues than online cpus, this will try to optimally
1974 * assign a queue to multiple cpus by grouping cpus that are "close" together:
1975 * thread siblings, core, socket, closest node, then whatever else is
1976 * available.
1977 */
1978static void nvme_assign_io_queues(struct nvme_dev *dev)
1979{
1980 unsigned cpu, cpus_per_queue, queues, remainder, i;
1981 cpumask_var_t unassigned_cpus;
1982
1983 nvme_create_io_queues(dev);
1984
1985 queues = min(dev->online_queues - 1, num_online_cpus());
1986 if (!queues)
1987 return;
1988
1989 cpus_per_queue = num_online_cpus() / queues;
1990 remainder = queues - (num_online_cpus() - queues * cpus_per_queue);
1991
1992 if (!alloc_cpumask_var(&unassigned_cpus, GFP_KERNEL))
1993 return;
1994
1995 cpumask_copy(unassigned_cpus, cpu_online_mask);
1996 cpu = cpumask_first(unassigned_cpus);
1997 for (i = 1; i <= queues; i++) {
1998 struct nvme_queue *nvmeq = lock_nvmeq(dev, i);
1999 cpumask_t mask;
2000
2001 cpumask_clear(nvmeq->cpu_mask);
2002 if (!cpumask_weight(unassigned_cpus)) {
2003 unlock_nvmeq(nvmeq);
2004 break;
2005 }
2006
2007 mask = *get_cpu_mask(cpu);
2008 nvme_set_queue_cpus(&mask, nvmeq, cpus_per_queue);
2009 if (cpus_weight(mask) < cpus_per_queue)
2010 nvme_add_cpus(&mask, unassigned_cpus,
2011 topology_thread_cpumask(cpu),
2012 nvmeq, cpus_per_queue);
2013 if (cpus_weight(mask) < cpus_per_queue)
2014 nvme_add_cpus(&mask, unassigned_cpus,
2015 topology_core_cpumask(cpu),
2016 nvmeq, cpus_per_queue);
2017 if (cpus_weight(mask) < cpus_per_queue)
2018 nvme_add_cpus(&mask, unassigned_cpus,
2019 cpumask_of_node(cpu_to_node(cpu)),
2020 nvmeq, cpus_per_queue);
2021 if (cpus_weight(mask) < cpus_per_queue)
2022 nvme_add_cpus(&mask, unassigned_cpus,
2023 cpumask_of_node(
2024 nvme_find_closest_node(
2025 cpu_to_node(cpu))),
2026 nvmeq, cpus_per_queue);
2027 if (cpus_weight(mask) < cpus_per_queue)
2028 nvme_add_cpus(&mask, unassigned_cpus,
2029 unassigned_cpus,
2030 nvmeq, cpus_per_queue);
2031
2032 WARN(cpumask_weight(nvmeq->cpu_mask) != cpus_per_queue,
2033 "nvme%d qid:%d mis-matched queue-to-cpu assignment\n",
2034 dev->instance, i);
2035
2036 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
2037 nvmeq->cpu_mask);
2038 cpumask_andnot(unassigned_cpus, unassigned_cpus,
2039 nvmeq->cpu_mask);
2040 cpu = cpumask_next(cpu, unassigned_cpus);
2041 if (remainder && !--remainder)
2042 cpus_per_queue++;
2043 unlock_nvmeq(nvmeq);
2044 }
2045 WARN(cpumask_weight(unassigned_cpus), "nvme%d unassigned online cpus\n",
2046 dev->instance);
2047 i = 0;
2048 cpumask_andnot(unassigned_cpus, cpu_possible_mask, cpu_online_mask);
2049 for_each_cpu(cpu, unassigned_cpus)
2050 *per_cpu_ptr(dev->io_queue, cpu) = (i++ % queues) + 1;
2051 free_cpumask_var(unassigned_cpus);
2052}
2053
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002054static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002055{
2056 int status;
2057 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002058 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002059
Matthew Wilcoxdf348132012-01-11 07:29:56 -07002060 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002061 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04002062 if (status < 0)
2063 return status;
2064 if (status > 0) {
2065 dev_err(&dev->pci_dev->dev, "Could not set queue count (%d)\n",
2066 status);
2067 return -EBUSY;
2068 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002069 return min(result & 0xffff, result >> 16) + 1;
2070}
2071
Keith Busch9d713c22013-07-15 15:02:24 -06002072static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
2073{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002074 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06002075}
2076
Keith Busch33b1e952014-03-24 10:46:26 -06002077static int nvme_cpu_notify(struct notifier_block *self,
2078 unsigned long action, void *hcpu)
2079{
2080 struct nvme_dev *dev = container_of(self, struct nvme_dev, nb);
2081 switch (action) {
2082 case CPU_ONLINE:
2083 case CPU_DEAD:
2084 nvme_assign_io_queues(dev);
2085 break;
2086 }
2087 return NOTIFY_OK;
2088}
2089
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002090static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002091{
Keith Busch5a92e702014-02-21 14:13:44 -07002092 struct nvme_queue *adminq = raw_nvmeq(dev, 0);
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002093 struct pci_dev *pdev = dev->pci_dev;
Keith Busch42f61422014-03-24 10:46:25 -06002094 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002095
Keith Busch42f61422014-03-24 10:46:25 -06002096 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002097 result = set_queue_count(dev, nr_io_queues);
Matthew Wilcox1b234842011-01-20 13:01:49 -05002098 if (result < 0)
2099 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002100 if (result < nr_io_queues)
2101 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002102
Keith Busch9d713c22013-07-15 15:02:24 -06002103 size = db_bar_size(dev, nr_io_queues);
2104 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002105 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06002106 do {
2107 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
2108 if (dev->bar)
2109 break;
2110 if (!--nr_io_queues)
2111 return -ENOMEM;
2112 size = db_bar_size(dev, nr_io_queues);
2113 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002114 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07002115 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002116 }
2117
Keith Busch9d713c22013-07-15 15:02:24 -06002118 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002119 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002120
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002121 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002122 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002123 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
2124 if (vecs < 0) {
2125 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
2126 if (vecs < 0) {
2127 vecs = 1;
2128 } else {
2129 for (i = 0; i < vecs; i++)
2130 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002131 }
2132 }
2133
Matthew Wilcox063a8092013-06-20 10:53:48 -04002134 /*
2135 * Should investigate if there's a performance win from allocating
2136 * more queues than interrupt vectors; it might allow the submission
2137 * path to scale better, even if the receive path is limited by the
2138 * number of interrupts.
2139 */
2140 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002141 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002142
Matthew Wilcox3193f072014-01-27 15:57:22 -05002143 result = queue_request_irq(dev, adminq, adminq->irqname);
Keith Busch9d713c22013-07-15 15:02:24 -06002144 if (result) {
Matthew Wilcox3193f072014-01-27 15:57:22 -05002145 adminq->q_suspended = 1;
Keith Busch22404272013-07-15 15:02:20 -06002146 goto free_queues;
Keith Busch9d713c22013-07-15 15:02:24 -06002147 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05002148
Keith Buschcd638942013-07-15 15:02:23 -06002149 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002150 nvme_free_queues(dev, nr_io_queues + 1);
2151 nvme_assign_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06002152
Keith Busch33b1e952014-03-24 10:46:26 -06002153 dev->nb.notifier_call = &nvme_cpu_notify;
2154 result = register_hotcpu_notifier(&dev->nb);
2155 if (result)
2156 goto free_queues;
Keith Busch22404272013-07-15 15:02:20 -06002157
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002158 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002159
Keith Busch22404272013-07-15 15:02:20 -06002160 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002161 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002162 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002163}
2164
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002165/*
2166 * Return: error value if an error occurred setting up the queues or calling
2167 * Identify Device. 0 if these succeeded, even if adding some of the
2168 * namespaces failed. At the moment, these failures are silent. TBD which
2169 * failures should be reported.
2170 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002171static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002172{
Matthew Wilcox68608c262013-06-21 14:36:34 -04002173 struct pci_dev *pdev = dev->pci_dev;
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002174 int res;
2175 unsigned nn, i;
Keith Buschcbb62182013-05-01 13:07:49 -06002176 struct nvme_ns *ns;
Matthew Wilcox51814232011-02-01 16:18:08 -05002177 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002178 struct nvme_id_ns *id_ns;
2179 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002180 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06002181 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002182
Matthew Wilcox68608c262013-06-21 14:36:34 -04002183 mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06002184 if (!mem)
2185 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002186
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002187 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002188 if (res) {
Matthew Wilcox27e81662014-04-11 11:58:45 -04002189 dev_err(&pdev->dev, "Identify Controller failed (%d)\n", res);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002190 res = -EIO;
Keith Buschcbb62182013-05-01 13:07:49 -06002191 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002192 }
2193
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002194 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05002195 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002196 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002197 dev->abort_limit = ctrl->acl + 1;
Matthew Wilcox51814232011-02-01 16:18:08 -05002198 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2199 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2200 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002201 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002202 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002203 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
2204 (pdev->device == 0x0953) && ctrl->vs[3])
Keith Busch159b67d2013-04-09 17:13:20 -06002205 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002206
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002207 id_ns = mem;
Matthew Wilcox2b2c1892011-10-07 13:10:13 -04002208 for (i = 1; i <= nn; i++) {
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002209 res = nvme_identify(dev, i, 0, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002210 if (res)
2211 continue;
2212
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002213 if (id_ns->ncap == 0)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002214 continue;
2215
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002216 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
Keith Busch08df1e02012-09-21 10:52:13 -06002217 dma_addr + 4096, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002218 if (res)
Keith Busch12209032013-01-31 14:40:38 -07002219 memset(mem + 4096, 0, 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002220
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002221 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002222 if (ns)
2223 list_add_tail(&ns->list, &dev->namespaces);
2224 }
2225 list_for_each_entry(ns, &dev->namespaces, list)
2226 add_disk(ns->disk);
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002227 res = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002228
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002229 out:
Matthew Wilcox684f5c22011-09-19 17:14:53 -04002230 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002231 return res;
2232}
2233
Keith Busch0877cb02013-07-15 15:02:19 -06002234static int nvme_dev_map(struct nvme_dev *dev)
2235{
Keith Busch42f61422014-03-24 10:46:25 -06002236 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002237 int bars, result = -ENOMEM;
2238 struct pci_dev *pdev = dev->pci_dev;
2239
2240 if (pci_enable_device_mem(pdev))
2241 return result;
2242
2243 dev->entry[0].vector = pdev->irq;
2244 pci_set_master(pdev);
2245 bars = pci_select_bars(pdev, IORESOURCE_MEM);
2246 if (pci_request_selected_regions(pdev, bars, "nvme"))
2247 goto disable_pci;
2248
Russell King052d0ef2013-06-26 23:49:11 +01002249 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
2250 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2251 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002252
Keith Busch0877cb02013-07-15 15:02:19 -06002253 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2254 if (!dev->bar)
2255 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07002256 if (readl(&dev->bar->csts) == -1) {
2257 result = -ENODEV;
2258 goto unmap;
2259 }
Keith Busch42f61422014-03-24 10:46:25 -06002260 cap = readq(&dev->bar->cap);
2261 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2262 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Keith Busch0877cb02013-07-15 15:02:19 -06002263 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2264
2265 return 0;
2266
Keith Busch0e53d182013-12-10 13:10:39 -07002267 unmap:
2268 iounmap(dev->bar);
2269 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002270 disable:
2271 pci_release_regions(pdev);
2272 disable_pci:
2273 pci_disable_device(pdev);
2274 return result;
2275}
2276
2277static void nvme_dev_unmap(struct nvme_dev *dev)
2278{
2279 if (dev->pci_dev->msi_enabled)
2280 pci_disable_msi(dev->pci_dev);
2281 else if (dev->pci_dev->msix_enabled)
2282 pci_disable_msix(dev->pci_dev);
2283
2284 if (dev->bar) {
2285 iounmap(dev->bar);
2286 dev->bar = NULL;
Keith Busch9a6b9452013-12-10 13:10:36 -07002287 pci_release_regions(dev->pci_dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002288 }
2289
Keith Busch0877cb02013-07-15 15:02:19 -06002290 if (pci_is_enabled(dev->pci_dev))
2291 pci_disable_device(dev->pci_dev);
2292}
2293
Keith Busch4d115422013-12-10 13:10:40 -07002294struct nvme_delq_ctx {
2295 struct task_struct *waiter;
2296 struct kthread_worker *worker;
2297 atomic_t refcount;
2298};
2299
2300static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2301{
2302 dq->waiter = current;
2303 mb();
2304
2305 for (;;) {
2306 set_current_state(TASK_KILLABLE);
2307 if (!atomic_read(&dq->refcount))
2308 break;
2309 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2310 fatal_signal_pending(current)) {
2311 set_current_state(TASK_RUNNING);
2312
2313 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
2314 nvme_disable_queue(dev, 0);
2315
2316 send_sig(SIGKILL, dq->worker->task, 1);
2317 flush_kthread_worker(dq->worker);
2318 return;
2319 }
2320 }
2321 set_current_state(TASK_RUNNING);
2322}
2323
2324static void nvme_put_dq(struct nvme_delq_ctx *dq)
2325{
2326 atomic_dec(&dq->refcount);
2327 if (dq->waiter)
2328 wake_up_process(dq->waiter);
2329}
2330
2331static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2332{
2333 atomic_inc(&dq->refcount);
2334 return dq;
2335}
2336
2337static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2338{
2339 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
2340
2341 nvme_clear_queue(nvmeq);
2342 nvme_put_dq(dq);
2343}
2344
2345static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2346 kthread_work_func_t fn)
2347{
2348 struct nvme_command c;
2349
2350 memset(&c, 0, sizeof(c));
2351 c.delete_queue.opcode = opcode;
2352 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2353
2354 init_kthread_work(&nvmeq->cmdinfo.work, fn);
2355 return nvme_submit_admin_cmd_async(nvmeq->dev, &c, &nvmeq->cmdinfo);
2356}
2357
2358static void nvme_del_cq_work_handler(struct kthread_work *work)
2359{
2360 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2361 cmdinfo.work);
2362 nvme_del_queue_end(nvmeq);
2363}
2364
2365static int nvme_delete_cq(struct nvme_queue *nvmeq)
2366{
2367 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2368 nvme_del_cq_work_handler);
2369}
2370
2371static void nvme_del_sq_work_handler(struct kthread_work *work)
2372{
2373 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2374 cmdinfo.work);
2375 int status = nvmeq->cmdinfo.status;
2376
2377 if (!status)
2378 status = nvme_delete_cq(nvmeq);
2379 if (status)
2380 nvme_del_queue_end(nvmeq);
2381}
2382
2383static int nvme_delete_sq(struct nvme_queue *nvmeq)
2384{
2385 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2386 nvme_del_sq_work_handler);
2387}
2388
2389static void nvme_del_queue_start(struct kthread_work *work)
2390{
2391 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2392 cmdinfo.work);
2393 allow_signal(SIGKILL);
2394 if (nvme_delete_sq(nvmeq))
2395 nvme_del_queue_end(nvmeq);
2396}
2397
2398static void nvme_disable_io_queues(struct nvme_dev *dev)
2399{
2400 int i;
2401 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2402 struct nvme_delq_ctx dq;
2403 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2404 &worker, "nvme%d", dev->instance);
2405
2406 if (IS_ERR(kworker_task)) {
2407 dev_err(&dev->pci_dev->dev,
2408 "Failed to create queue del task\n");
2409 for (i = dev->queue_count - 1; i > 0; i--)
2410 nvme_disable_queue(dev, i);
2411 return;
2412 }
2413
2414 dq.waiter = NULL;
2415 atomic_set(&dq.refcount, 0);
2416 dq.worker = &worker;
2417 for (i = dev->queue_count - 1; i > 0; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07002418 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
Keith Busch4d115422013-12-10 13:10:40 -07002419
2420 if (nvme_suspend_queue(nvmeq))
2421 continue;
2422 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2423 nvmeq->cmdinfo.worker = dq.worker;
2424 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2425 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2426 }
2427 nvme_wait_dq(&dq, dev);
2428 kthread_stop(kworker_task);
2429}
2430
Dan McLeranb9afca32014-04-07 17:10:11 -06002431/*
2432* Remove the node from the device list and check
2433* for whether or not we need to stop the nvme_thread.
2434*/
2435static void nvme_dev_list_remove(struct nvme_dev *dev)
2436{
2437 struct task_struct *tmp = NULL;
2438
2439 spin_lock(&dev_list_lock);
2440 list_del_init(&dev->node);
2441 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2442 tmp = nvme_thread;
2443 nvme_thread = NULL;
2444 }
2445 spin_unlock(&dev_list_lock);
2446
2447 if (tmp)
2448 kthread_stop(tmp);
2449}
2450
Keith Buschf0b50732013-07-15 15:02:21 -06002451static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002452{
Keith Busch22404272013-07-15 15:02:20 -06002453 int i;
2454
Keith Buschd4b4ff82013-12-10 13:10:37 -07002455 dev->initialized = 0;
Keith Busch33b1e952014-03-24 10:46:26 -06002456 unregister_hotcpu_notifier(&dev->nb);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002457
Dan McLeranb9afca32014-04-07 17:10:11 -06002458 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002459
Keith Busch4d115422013-12-10 13:10:40 -07002460 if (!dev->bar || (dev->bar && readl(&dev->bar->csts) == -1)) {
2461 for (i = dev->queue_count - 1; i >= 0; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07002462 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
Keith Busch4d115422013-12-10 13:10:40 -07002463 nvme_suspend_queue(nvmeq);
2464 nvme_clear_queue(nvmeq);
2465 }
2466 } else {
2467 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002468 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002469 nvme_disable_queue(dev, 0);
2470 }
Keith Buschf0b50732013-07-15 15:02:21 -06002471 nvme_dev_unmap(dev);
2472}
2473
2474static void nvme_dev_remove(struct nvme_dev *dev)
2475{
Keith Busch9ac27092014-01-31 16:53:39 -07002476 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002477
Keith Busch9ac27092014-01-31 16:53:39 -07002478 list_for_each_entry(ns, &dev->namespaces, list) {
2479 if (ns->disk->flags & GENHD_FL_UP)
2480 del_gendisk(ns->disk);
2481 if (!blk_queue_dying(ns->queue))
2482 blk_cleanup_queue(ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002483 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002484}
2485
Matthew Wilcox091b6092011-02-10 09:56:01 -05002486static int nvme_setup_prp_pools(struct nvme_dev *dev)
2487{
2488 struct device *dmadev = &dev->pci_dev->dev;
2489 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2490 PAGE_SIZE, PAGE_SIZE, 0);
2491 if (!dev->prp_page_pool)
2492 return -ENOMEM;
2493
Matthew Wilcox99802a72011-02-10 10:30:34 -05002494 /* Optimisation for I/Os between 4k and 128k */
2495 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2496 256, 256, 0);
2497 if (!dev->prp_small_pool) {
2498 dma_pool_destroy(dev->prp_page_pool);
2499 return -ENOMEM;
2500 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002501 return 0;
2502}
2503
2504static void nvme_release_prp_pools(struct nvme_dev *dev)
2505{
2506 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002507 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002508}
2509
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002510static DEFINE_IDA(nvme_instance_ida);
2511
2512static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002513{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002514 int instance, error;
2515
2516 do {
2517 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2518 return -ENODEV;
2519
2520 spin_lock(&dev_list_lock);
2521 error = ida_get_new(&nvme_instance_ida, &instance);
2522 spin_unlock(&dev_list_lock);
2523 } while (error == -EAGAIN);
2524
2525 if (error)
2526 return -ENODEV;
2527
2528 dev->instance = instance;
2529 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002530}
2531
2532static void nvme_release_instance(struct nvme_dev *dev)
2533{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002534 spin_lock(&dev_list_lock);
2535 ida_remove(&nvme_instance_ida, dev->instance);
2536 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002537}
2538
Keith Busch9ac27092014-01-31 16:53:39 -07002539static void nvme_free_namespaces(struct nvme_dev *dev)
2540{
2541 struct nvme_ns *ns, *next;
2542
2543 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2544 list_del(&ns->list);
2545 put_disk(ns->disk);
2546 kfree(ns);
2547 }
2548}
2549
Keith Busch5e82e952013-02-19 10:17:58 -07002550static void nvme_free_dev(struct kref *kref)
2551{
2552 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002553
2554 nvme_free_namespaces(dev);
Keith Busch42f61422014-03-24 10:46:25 -06002555 free_percpu(dev->io_queue);
Keith Busch5e82e952013-02-19 10:17:58 -07002556 kfree(dev->queues);
2557 kfree(dev->entry);
2558 kfree(dev);
2559}
2560
2561static int nvme_dev_open(struct inode *inode, struct file *f)
2562{
2563 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
2564 miscdev);
2565 kref_get(&dev->kref);
2566 f->private_data = dev;
2567 return 0;
2568}
2569
2570static int nvme_dev_release(struct inode *inode, struct file *f)
2571{
2572 struct nvme_dev *dev = f->private_data;
2573 kref_put(&dev->kref, nvme_free_dev);
2574 return 0;
2575}
2576
2577static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2578{
2579 struct nvme_dev *dev = f->private_data;
2580 switch (cmd) {
2581 case NVME_IOCTL_ADMIN_CMD:
2582 return nvme_user_admin_cmd(dev, (void __user *)arg);
2583 default:
2584 return -ENOTTY;
2585 }
2586}
2587
2588static const struct file_operations nvme_dev_fops = {
2589 .owner = THIS_MODULE,
2590 .open = nvme_dev_open,
2591 .release = nvme_dev_release,
2592 .unlocked_ioctl = nvme_dev_ioctl,
2593 .compat_ioctl = nvme_dev_ioctl,
2594};
2595
Keith Buschf0b50732013-07-15 15:02:21 -06002596static int nvme_dev_start(struct nvme_dev *dev)
2597{
2598 int result;
Dan McLeranb9afca32014-04-07 17:10:11 -06002599 bool start_thread = false;
Keith Buschf0b50732013-07-15 15:02:21 -06002600
2601 result = nvme_dev_map(dev);
2602 if (result)
2603 return result;
2604
2605 result = nvme_configure_admin_queue(dev);
2606 if (result)
2607 goto unmap;
2608
2609 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002610 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2611 start_thread = true;
2612 nvme_thread = NULL;
2613 }
Keith Buschf0b50732013-07-15 15:02:21 -06002614 list_add(&dev->node, &dev_list);
2615 spin_unlock(&dev_list_lock);
2616
Dan McLeranb9afca32014-04-07 17:10:11 -06002617 if (start_thread) {
2618 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2619 wake_up(&nvme_kthread_wait);
2620 } else
2621 wait_event_killable(nvme_kthread_wait, nvme_thread);
2622
2623 if (IS_ERR_OR_NULL(nvme_thread)) {
2624 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2625 goto disable;
2626 }
2627
Keith Buschf0b50732013-07-15 15:02:21 -06002628 result = nvme_setup_io_queues(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002629 if (result && result != -EBUSY)
Keith Buschf0b50732013-07-15 15:02:21 -06002630 goto disable;
2631
Keith Buschd82e8bf2013-09-05 14:45:07 -06002632 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002633
2634 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002635 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06002636 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002637 unmap:
2638 nvme_dev_unmap(dev);
2639 return result;
2640}
2641
Keith Busch9a6b9452013-12-10 13:10:36 -07002642static int nvme_remove_dead_ctrl(void *arg)
2643{
2644 struct nvme_dev *dev = (struct nvme_dev *)arg;
2645 struct pci_dev *pdev = dev->pci_dev;
2646
2647 if (pci_get_drvdata(pdev))
2648 pci_stop_and_remove_bus_device(pdev);
2649 kref_put(&dev->kref, nvme_free_dev);
2650 return 0;
2651}
2652
2653static void nvme_remove_disks(struct work_struct *ws)
2654{
Keith Busch9a6b9452013-12-10 13:10:36 -07002655 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2656
2657 nvme_dev_remove(dev);
Keith Busch5a92e702014-02-21 14:13:44 -07002658 nvme_free_queues(dev, 1);
Keith Busch9a6b9452013-12-10 13:10:36 -07002659}
2660
2661static int nvme_dev_resume(struct nvme_dev *dev)
2662{
2663 int ret;
2664
2665 ret = nvme_dev_start(dev);
2666 if (ret && ret != -EBUSY)
2667 return ret;
2668 if (ret == -EBUSY) {
2669 spin_lock(&dev_list_lock);
Tejun Heo9ca97372014-03-07 10:24:49 -05002670 dev->reset_workfn = nvme_remove_disks;
Keith Busch9a6b9452013-12-10 13:10:36 -07002671 queue_work(nvme_workq, &dev->reset_work);
2672 spin_unlock(&dev_list_lock);
2673 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002674 dev->initialized = 1;
Keith Busch9a6b9452013-12-10 13:10:36 -07002675 return 0;
2676}
2677
2678static void nvme_dev_reset(struct nvme_dev *dev)
2679{
2680 nvme_dev_shutdown(dev);
2681 if (nvme_dev_resume(dev)) {
2682 dev_err(&dev->pci_dev->dev, "Device failed to resume\n");
2683 kref_get(&dev->kref);
2684 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2685 dev->instance))) {
2686 dev_err(&dev->pci_dev->dev,
2687 "Failed to start controller remove task\n");
2688 kref_put(&dev->kref, nvme_free_dev);
2689 }
2690 }
2691}
2692
2693static void nvme_reset_failed_dev(struct work_struct *ws)
2694{
2695 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2696 nvme_dev_reset(dev);
2697}
2698
Tejun Heo9ca97372014-03-07 10:24:49 -05002699static void nvme_reset_workfn(struct work_struct *work)
2700{
2701 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
2702 dev->reset_workfn(work);
2703}
2704
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002705static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002706{
Keith Busch0877cb02013-07-15 15:02:19 -06002707 int result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002708 struct nvme_dev *dev;
2709
2710 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2711 if (!dev)
2712 return -ENOMEM;
2713 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
2714 GFP_KERNEL);
2715 if (!dev->entry)
2716 goto free;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002717 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
2718 GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002719 if (!dev->queues)
2720 goto free;
Keith Busch42f61422014-03-24 10:46:25 -06002721 dev->io_queue = alloc_percpu(unsigned short);
2722 if (!dev->io_queue)
2723 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002724
2725 INIT_LIST_HEAD(&dev->namespaces);
Tejun Heo9ca97372014-03-07 10:24:49 -05002726 dev->reset_workfn = nvme_reset_failed_dev;
2727 INIT_WORK(&dev->reset_work, nvme_reset_workfn);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002728 dev->pci_dev = pdev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002729 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002730 result = nvme_set_instance(dev);
2731 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002732 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002733
Matthew Wilcox091b6092011-02-10 09:56:01 -05002734 result = nvme_setup_prp_pools(dev);
2735 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002736 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002737
Keith Buschfb35e912014-03-03 11:09:47 -07002738 kref_init(&dev->kref);
Keith Buschf0b50732013-07-15 15:02:21 -06002739 result = nvme_dev_start(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002740 if (result) {
2741 if (result == -EBUSY)
2742 goto create_cdev;
Keith Busch0877cb02013-07-15 15:02:19 -06002743 goto release_pools;
Keith Buschd82e8bf2013-09-05 14:45:07 -06002744 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002745
Matthew Wilcox740216f2011-02-15 16:28:20 -05002746 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002747 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002748 goto shutdown;
Matthew Wilcox740216f2011-02-15 16:28:20 -05002749
Keith Buschd82e8bf2013-09-05 14:45:07 -06002750 create_cdev:
Keith Busch5e82e952013-02-19 10:17:58 -07002751 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
2752 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2753 dev->miscdev.parent = &pdev->dev;
2754 dev->miscdev.name = dev->name;
2755 dev->miscdev.fops = &nvme_dev_fops;
2756 result = misc_register(&dev->miscdev);
2757 if (result)
2758 goto remove;
2759
Keith Buschd4b4ff82013-12-10 13:10:37 -07002760 dev->initialized = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002761 return 0;
2762
Keith Busch5e82e952013-02-19 10:17:58 -07002763 remove:
2764 nvme_dev_remove(dev);
Keith Busch9ac27092014-01-31 16:53:39 -07002765 nvme_free_namespaces(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002766 shutdown:
2767 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002768 release_pools:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002769 nvme_free_queues(dev, 0);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002770 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002771 release:
2772 nvme_release_instance(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002773 free:
Keith Busch42f61422014-03-24 10:46:25 -06002774 free_percpu(dev->io_queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002775 kfree(dev->queues);
2776 kfree(dev->entry);
2777 kfree(dev);
2778 return result;
2779}
2780
Keith Busch09ece142014-01-27 11:29:40 -05002781static void nvme_shutdown(struct pci_dev *pdev)
2782{
2783 struct nvme_dev *dev = pci_get_drvdata(pdev);
2784 nvme_dev_shutdown(dev);
2785}
2786
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002787static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002788{
2789 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002790
2791 spin_lock(&dev_list_lock);
2792 list_del_init(&dev->node);
2793 spin_unlock(&dev_list_lock);
2794
2795 pci_set_drvdata(pdev, NULL);
2796 flush_work(&dev->reset_work);
Keith Busch5e82e952013-02-19 10:17:58 -07002797 misc_deregister(&dev->miscdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002798 nvme_dev_remove(dev);
2799 nvme_dev_shutdown(dev);
Keith Buscha1a5ef92013-12-16 13:50:00 -05002800 nvme_free_queues(dev, 0);
Keith Busch5a92e702014-02-21 14:13:44 -07002801 rcu_barrier();
Keith Busch9a6b9452013-12-10 13:10:36 -07002802 nvme_release_instance(dev);
2803 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002804 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002805}
2806
2807/* These functions are yet to be implemented */
2808#define nvme_error_detected NULL
2809#define nvme_dump_registers NULL
2810#define nvme_link_reset NULL
2811#define nvme_slot_reset NULL
2812#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002813
Jingoo Han671a6012014-02-13 11:19:14 +09002814#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002815static int nvme_suspend(struct device *dev)
2816{
2817 struct pci_dev *pdev = to_pci_dev(dev);
2818 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2819
2820 nvme_dev_shutdown(ndev);
2821 return 0;
2822}
2823
2824static int nvme_resume(struct device *dev)
2825{
2826 struct pci_dev *pdev = to_pci_dev(dev);
2827 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002828
Keith Busch9a6b9452013-12-10 13:10:36 -07002829 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Tejun Heo9ca97372014-03-07 10:24:49 -05002830 ndev->reset_workfn = nvme_reset_failed_dev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002831 queue_work(nvme_workq, &ndev->reset_work);
2832 }
2833 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002834}
Jingoo Han671a6012014-02-13 11:19:14 +09002835#endif
Keith Buschcd638942013-07-15 15:02:23 -06002836
2837static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002838
Stephen Hemminger1d352032012-09-07 09:33:17 -07002839static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002840 .error_detected = nvme_error_detected,
2841 .mmio_enabled = nvme_dump_registers,
2842 .link_reset = nvme_link_reset,
2843 .slot_reset = nvme_slot_reset,
2844 .resume = nvme_error_resume,
2845};
2846
2847/* Move to pci_ids.h later */
2848#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2849
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002850static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002851 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2852 { 0, }
2853};
2854MODULE_DEVICE_TABLE(pci, nvme_id_table);
2855
2856static struct pci_driver nvme_driver = {
2857 .name = "nvme",
2858 .id_table = nvme_id_table,
2859 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002860 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002861 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002862 .driver = {
2863 .pm = &nvme_dev_pm_ops,
2864 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002865 .err_handler = &nvme_err_handler,
2866};
2867
2868static int __init nvme_init(void)
2869{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002870 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002871
Dan McLeranb9afca32014-04-07 17:10:11 -06002872 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002873
Keith Busch9a6b9452013-12-10 13:10:36 -07002874 nvme_workq = create_singlethread_workqueue("nvme");
2875 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06002876 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07002877
Keith Busch5c42ea12012-07-25 16:05:18 -06002878 result = register_blkdev(nvme_major, "nvme");
2879 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002880 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06002881 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002882 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002883
2884 result = pci_register_driver(&nvme_driver);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002885 if (result)
2886 goto unregister_blkdev;
2887 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002888
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002889 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002890 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002891 kill_workq:
2892 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002893 return result;
2894}
2895
2896static void __exit nvme_exit(void)
2897{
2898 pci_unregister_driver(&nvme_driver);
2899 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002900 destroy_workqueue(nvme_workq);
Dan McLeranb9afca32014-04-07 17:10:11 -06002901 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002902}
2903
2904MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2905MODULE_LICENSE("GPL");
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002906MODULE_VERSION("0.9");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002907module_init(nvme_init);
2908module_exit(nvme_exit);