blob: e2bb8afbeae5902081f95c52da9e81e58d1d1693 [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
Keith Busch3291fa52014-04-28 12:30:52 -060045#include <trace/events/block.h>
46
Keith Busch9d43cf62014-05-13 11:42:02 -060047#define NVME_Q_DEPTH 1024
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050048#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
49#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Keith Busch9d43cf62014-05-13 11:42:02 -060050#define ADMIN_TIMEOUT (admin_timeout * HZ)
51#define IOD_TIMEOUT (retry_time * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050052
Keith Busch9d43cf62014-05-13 11:42:02 -060053static unsigned char admin_timeout = 60;
54module_param(admin_timeout, byte, 0644);
55MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050056
Matthew Wilcoxbd676082014-06-03 23:04:30 -040057unsigned char nvme_io_timeout = 30;
58module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
Keith Buschb3550842014-04-04 11:43:36 -060059MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050060
Keith Busch61e4ce02014-05-13 11:42:01 -060061static unsigned char retry_time = 30;
62module_param(retry_time, byte, 0644);
63MODULE_PARM_DESC(retry_time, "time in seconds to retry failed I/O");
64
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050065static int nvme_major;
66module_param(nvme_major, int, 0);
67
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050068static int use_threaded_interrupts;
69module_param(use_threaded_interrupts, int, 0);
70
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050071static DEFINE_SPINLOCK(dev_list_lock);
72static LIST_HEAD(dev_list);
73static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070074static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060075static wait_queue_head_t nvme_kthread_wait;
Keith Buschf3db22f2014-06-11 11:51:35 -060076static struct notifier_block nvme_nb;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050077
Keith Buschd4b4ff82013-12-10 13:10:37 -070078static void nvme_reset_failed_dev(struct work_struct *ws);
79
Keith Busch4d115422013-12-10 13:10:40 -070080struct async_cmd_info {
81 struct kthread_work work;
82 struct kthread_worker *worker;
83 u32 result;
84 int status;
85 void *ctx;
86};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050087
88/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050089 * An NVM Express queue. Each device has at least two (one for admin
90 * commands and one for I/O commands).
91 */
92struct nvme_queue {
Keith Busch5a92e702014-02-21 14:13:44 -070093 struct rcu_head r_head;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050094 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -050095 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -050096 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050097 spinlock_t q_lock;
98 struct nvme_command *sq_cmds;
99 volatile struct nvme_completion *cqes;
100 dma_addr_t sq_dma_addr;
101 dma_addr_t cq_dma_addr;
102 wait_queue_head_t sq_full;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -0500103 wait_queue_t sq_cong_wait;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500104 struct bio_list sq_cong;
Keith Buschedd10d32014-04-03 16:45:23 -0600105 struct list_head iod_bio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500106 u32 __iomem *q_db;
107 u16 q_depth;
108 u16 cq_vector;
109 u16 sq_head;
110 u16 sq_tail;
111 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700112 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400113 u8 cq_phase;
114 u8 cqe_seen;
Keith Busch22404272013-07-15 15:02:20 -0600115 u8 q_suspended;
Keith Busch42f61422014-03-24 10:46:25 -0600116 cpumask_var_t cpu_mask;
Keith Busch4d115422013-12-10 13:10:40 -0700117 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500118 unsigned long cmdid_data[];
119};
120
121/*
122 * Check we didin't inadvertently grow the command struct
123 */
124static inline void _nvme_check_size(void)
125{
126 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
127 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
128 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
129 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
130 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400131 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700132 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500133 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
134 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
135 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
136 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600137 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500138}
139
Keith Buschedd10d32014-04-03 16:45:23 -0600140typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400141 struct nvme_completion *);
142
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500143struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400144 nvme_completion_fn fn;
145 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500146 unsigned long timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700147 int aborted;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500148};
149
150static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
151{
152 return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
153}
154
Keith Busch22404272013-07-15 15:02:20 -0600155static unsigned nvme_queue_extra(int depth)
156{
157 return DIV_ROUND_UP(depth, 8) + (depth * sizeof(struct nvme_cmd_info));
158}
159
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500160/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400161 * alloc_cmdid() - Allocate a Command ID
162 * @nvmeq: The queue that will be used for this command
163 * @ctx: A pointer that will be passed to the handler
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400164 * @handler: The function to call on completion
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500165 *
166 * Allocate a Command ID for a queue. The data passed in will
167 * be passed to the completion handler. This is implemented by using
168 * the bottom two bits of the ctx pointer to store the handler ID.
169 * Passing in a pointer that's not 4-byte aligned will cause a BUG.
170 * We can change this if it becomes a problem.
Matthew Wilcox184d2942011-05-11 21:36:38 -0400171 *
172 * May be called with local interrupts disabled and the q_lock held,
173 * or with interrupts enabled and no locks held.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500174 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400175static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx,
176 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500177{
Matthew Wilcoxe6d15f72011-02-24 08:49:41 -0500178 int depth = nvmeq->q_depth - 1;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500179 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500180 int cmdid;
181
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500182 do {
183 cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
184 if (cmdid >= depth)
185 return -EBUSY;
186 } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
187
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400188 info[cmdid].fn = handler;
189 info[cmdid].ctx = ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500190 info[cmdid].timeout = jiffies + timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700191 info[cmdid].aborted = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500192 return cmdid;
193}
194
195static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400196 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500197{
198 int cmdid;
199 wait_event_killable(nvmeq->sq_full,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500200 (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500201 return (cmdid < 0) ? -EINTR : cmdid;
202}
203
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400204/* Special values must be less than 0x1000 */
205#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500206#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
207#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
208#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Keith Busch53562be2014-04-29 11:41:29 -0600209#define CMD_CTX_ABORT (0x318 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500210
Keith Buschedd10d32014-04-03 16:45:23 -0600211static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400212 struct nvme_completion *cqe)
213{
214 if (ctx == CMD_CTX_CANCELLED)
215 return;
Keith Buschc30341d2013-12-10 13:10:38 -0700216 if (ctx == CMD_CTX_ABORT) {
Keith Buschedd10d32014-04-03 16:45:23 -0600217 ++nvmeq->dev->abort_limit;
Keith Buschc30341d2013-12-10 13:10:38 -0700218 return;
219 }
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400220 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600221 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400222 "completed id %d twice on queue %d\n",
223 cqe->command_id, le16_to_cpup(&cqe->sq_id));
224 return;
225 }
226 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600227 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400228 "invalid id %d completed on queue %d\n",
229 cqe->command_id, le16_to_cpup(&cqe->sq_id));
230 return;
231 }
232
Keith Buschedd10d32014-04-03 16:45:23 -0600233 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400234}
235
Keith Buschedd10d32014-04-03 16:45:23 -0600236static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700237 struct nvme_completion *cqe)
238{
239 struct async_cmd_info *cmdinfo = ctx;
240 cmdinfo->result = le32_to_cpup(&cqe->result);
241 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
242 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
243}
244
Matthew Wilcox184d2942011-05-11 21:36:38 -0400245/*
246 * Called with local interrupts disabled and the q_lock held. May not sleep.
247 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400248static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid,
249 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500250{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400251 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500252 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500253
Keith Busch94bbac42014-04-24 18:53:50 -0600254 if (cmdid >= nvmeq->q_depth || !info[cmdid].fn) {
255 if (fn)
256 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500257 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400258 }
Keith Busch859361a2012-08-02 14:05:59 -0600259 if (fn)
260 *fn = info[cmdid].fn;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400261 ctx = info[cmdid].ctx;
262 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500263 info[cmdid].ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500264 clear_bit(cmdid, nvmeq->cmdid_data);
265 wake_up(&nvmeq->sq_full);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400266 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500267}
268
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400269static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
270 nvme_completion_fn *fn)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500271{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400272 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500273 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400274 if (fn)
275 *fn = info[cmdid].fn;
276 ctx = info[cmdid].ctx;
277 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500278 info[cmdid].ctx = CMD_CTX_CANCELLED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400279 return ctx;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500280}
281
Keith Busch5a92e702014-02-21 14:13:44 -0700282static struct nvme_queue *raw_nvmeq(struct nvme_dev *dev, int qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500283{
Keith Busch5a92e702014-02-21 14:13:44 -0700284 return rcu_dereference_raw(dev->queues[qid]);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500285}
286
Keith Busch4f5099a2014-03-03 16:39:13 -0700287static struct nvme_queue *get_nvmeq(struct nvme_dev *dev) __acquires(RCU)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500288{
Keith Buscha51afb52014-05-13 10:32:46 -0600289 struct nvme_queue *nvmeq;
Keith Busch42f61422014-03-24 10:46:25 -0600290 unsigned queue_id = get_cpu_var(*dev->io_queue);
Keith Buscha51afb52014-05-13 10:32:46 -0600291
Keith Busch5a92e702014-02-21 14:13:44 -0700292 rcu_read_lock();
Keith Buscha51afb52014-05-13 10:32:46 -0600293 nvmeq = rcu_dereference(dev->queues[queue_id]);
294 if (nvmeq)
295 return nvmeq;
296
297 rcu_read_unlock();
298 put_cpu_var(*dev->io_queue);
299 return NULL;
Keith Busch5a92e702014-02-21 14:13:44 -0700300}
301
Keith Busch4f5099a2014-03-03 16:39:13 -0700302static void put_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500303{
Keith Busch5a92e702014-02-21 14:13:44 -0700304 rcu_read_unlock();
Keith Busch42f61422014-03-24 10:46:25 -0600305 put_cpu_var(nvmeq->dev->io_queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500306}
307
Keith Busch4f5099a2014-03-03 16:39:13 -0700308static struct nvme_queue *lock_nvmeq(struct nvme_dev *dev, int q_idx)
309 __acquires(RCU)
310{
Keith Buscha51afb52014-05-13 10:32:46 -0600311 struct nvme_queue *nvmeq;
312
Keith Busch4f5099a2014-03-03 16:39:13 -0700313 rcu_read_lock();
Keith Buscha51afb52014-05-13 10:32:46 -0600314 nvmeq = rcu_dereference(dev->queues[q_idx]);
315 if (nvmeq)
316 return nvmeq;
317
318 rcu_read_unlock();
319 return NULL;
Keith Busch4f5099a2014-03-03 16:39:13 -0700320}
321
322static void unlock_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
323{
324 rcu_read_unlock();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500325}
326
327/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400328 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500329 * @nvmeq: The queue to use
330 * @cmd: The command to send
331 *
332 * Safe to use from interrupt context
333 */
334static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
335{
336 unsigned long flags;
337 u16 tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500338 spin_lock_irqsave(&nvmeq->q_lock, flags);
Keith Busch4f5099a2014-03-03 16:39:13 -0700339 if (nvmeq->q_suspended) {
340 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
341 return -EBUSY;
342 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500343 tail = nvmeq->sq_tail;
344 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500345 if (++tail == nvmeq->q_depth)
346 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500347 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500348 nvmeq->sq_tail = tail;
349 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
350
351 return 0;
352}
353
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500354static __le64 **iod_list(struct nvme_iod *iod)
355{
356 return ((void *)iod) + iod->offset;
357}
358
359/*
360 * Will slightly overestimate the number of pages needed. This is OK
361 * as it only leads to a small amount of wasted memory for the lifetime of
362 * the I/O.
363 */
364static int nvme_npages(unsigned size)
365{
366 unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE);
367 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
368}
369
370static struct nvme_iod *
371nvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp)
372{
373 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
374 sizeof(__le64 *) * nvme_npages(nbytes) +
375 sizeof(struct scatterlist) * nseg, gfp);
376
377 if (iod) {
378 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
379 iod->npages = -1;
380 iod->length = nbytes;
Keith Busch2b196032012-11-06 11:59:23 -0700381 iod->nents = 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600382 iod->first_dma = 0ULL;
Keith Busch61982212013-05-29 15:59:39 -0600383 iod->start_time = jiffies;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500384 }
385
386 return iod;
387}
388
Vishal Verma5d0f6132013-03-04 18:40:58 -0700389void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500390{
391 const int last_prp = PAGE_SIZE / 8 - 1;
392 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500393 __le64 **list = iod_list(iod);
394 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500395
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500396 if (iod->npages == 0)
397 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
398 for (i = 0; i < iod->npages; i++) {
399 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500400 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500401 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500402 prp_dma = next_prp_dma;
403 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500404 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500405}
406
Keith Busch61982212013-05-29 15:59:39 -0600407static void nvme_start_io_acct(struct bio *bio)
408{
409 struct gendisk *disk = bio->bi_bdev->bd_disk;
Sam Bradshawb4e75cb2014-05-09 13:27:07 -0700410 if (blk_queue_io_stat(disk->queue)) {
411 const int rw = bio_data_dir(bio);
412 int cpu = part_stat_lock();
413 part_round_stats(cpu, &disk->part0);
414 part_stat_inc(cpu, &disk->part0, ios[rw]);
415 part_stat_add(cpu, &disk->part0, sectors[rw],
416 bio_sectors(bio));
417 part_inc_in_flight(&disk->part0, rw);
418 part_stat_unlock();
419 }
Keith Busch61982212013-05-29 15:59:39 -0600420}
421
422static void nvme_end_io_acct(struct bio *bio, unsigned long start_time)
423{
424 struct gendisk *disk = bio->bi_bdev->bd_disk;
Sam Bradshawb4e75cb2014-05-09 13:27:07 -0700425 if (blk_queue_io_stat(disk->queue)) {
426 const int rw = bio_data_dir(bio);
427 unsigned long duration = jiffies - start_time;
428 int cpu = part_stat_lock();
429 part_stat_add(cpu, &disk->part0, ticks[rw], duration);
430 part_round_stats(cpu, &disk->part0);
431 part_dec_in_flight(&disk->part0, rw);
432 part_stat_unlock();
433 }
Keith Busch61982212013-05-29 15:59:39 -0600434}
435
Keith Buschedd10d32014-04-03 16:45:23 -0600436static void bio_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500437 struct nvme_completion *cqe)
438{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500439 struct nvme_iod *iod = ctx;
440 struct bio *bio = iod->private;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500441 u16 status = le16_to_cpup(&cqe->status) >> 1;
Keith Busch3291fa52014-04-28 12:30:52 -0600442 int error = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500443
Keith Buschedd10d32014-04-03 16:45:23 -0600444 if (unlikely(status)) {
445 if (!(status & NVME_SC_DNR ||
446 bio->bi_rw & REQ_FAILFAST_MASK) &&
447 (jiffies - iod->start_time) < IOD_TIMEOUT) {
448 if (!waitqueue_active(&nvmeq->sq_full))
449 add_wait_queue(&nvmeq->sq_full,
450 &nvmeq->sq_cong_wait);
451 list_add_tail(&iod->node, &nvmeq->iod_bio);
452 wake_up(&nvmeq->sq_full);
453 return;
454 }
Keith Busch3291fa52014-04-28 12:30:52 -0600455 error = -EIO;
Keith Buschedd10d32014-04-03 16:45:23 -0600456 }
Keith Busch9e59d092013-08-08 10:25:38 -0600457 if (iod->nents) {
Keith Buschedd10d32014-04-03 16:45:23 -0600458 dma_unmap_sg(nvmeq->q_dmadev, iod->sg, iod->nents,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500459 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busch9e59d092013-08-08 10:25:38 -0600460 nvme_end_io_acct(bio, iod->start_time);
461 }
Keith Buschedd10d32014-04-03 16:45:23 -0600462 nvme_free_iod(nvmeq->dev, iod);
Keith Busch3291fa52014-04-28 12:30:52 -0600463
464 trace_block_bio_complete(bdev_get_queue(bio->bi_bdev), bio, error);
465 bio_endio(bio, error);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500466}
467
Matthew Wilcox184d2942011-05-11 21:36:38 -0400468/* length is in bytes. gfp flags indicates whether we may sleep. */
Keith Buschedd10d32014-04-03 16:45:23 -0600469int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod, int total_len,
470 gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500471{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500472 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500473 int length = total_len;
474 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500475 int dma_len = sg_dma_len(sg);
476 u64 dma_addr = sg_dma_address(sg);
477 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500478 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500479 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500480 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500481 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500482
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500483 length -= (PAGE_SIZE - offset);
484 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500485 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500486
487 dma_len -= (PAGE_SIZE - offset);
488 if (dma_len) {
489 dma_addr += (PAGE_SIZE - offset);
490 } else {
491 sg = sg_next(sg);
492 dma_addr = sg_dma_address(sg);
493 dma_len = sg_dma_len(sg);
494 }
495
496 if (length <= PAGE_SIZE) {
Keith Buschedd10d32014-04-03 16:45:23 -0600497 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500498 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500499 }
500
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500501 nprps = DIV_ROUND_UP(length, PAGE_SIZE);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500502 if (nprps <= (256 / 8)) {
503 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500504 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500505 } else {
506 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500507 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500508 }
509
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400510 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
511 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600512 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500513 iod->npages = -1;
514 return (total_len - length) + PAGE_SIZE;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400515 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500516 list[0] = prp_list;
517 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500518 i = 0;
519 for (;;) {
Matthew Wilcox7523d832011-03-16 16:43:40 -0400520 if (i == PAGE_SIZE / 8) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500521 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400522 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500523 if (!prp_list)
524 return total_len - length;
525 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400526 prp_list[0] = old_prp_list[i - 1];
527 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
528 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500529 }
530 prp_list[i++] = cpu_to_le64(dma_addr);
531 dma_len -= PAGE_SIZE;
532 dma_addr += PAGE_SIZE;
533 length -= PAGE_SIZE;
534 if (length <= 0)
535 break;
536 if (dma_len > 0)
537 continue;
538 BUG_ON(dma_len < 0);
539 sg = sg_next(sg);
540 dma_addr = sg_dma_address(sg);
541 dma_len = sg_dma_len(sg);
542 }
543
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500544 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500545}
546
Keith Busch427e9702013-04-09 11:59:32 -0600547static int nvme_split_and_submit(struct bio *bio, struct nvme_queue *nvmeq,
Kent Overstreet20d01892013-11-23 18:21:01 -0800548 int len)
Keith Busch427e9702013-04-09 11:59:32 -0600549{
Kent Overstreet20d01892013-11-23 18:21:01 -0800550 struct bio *split = bio_split(bio, len >> 9, GFP_ATOMIC, NULL);
551 if (!split)
Keith Busch427e9702013-04-09 11:59:32 -0600552 return -ENOMEM;
553
Keith Busch3291fa52014-04-28 12:30:52 -0600554 trace_block_split(bdev_get_queue(bio->bi_bdev), bio,
555 split->bi_iter.bi_sector);
Kent Overstreet20d01892013-11-23 18:21:01 -0800556 bio_chain(split, bio);
557
Keith Buschedd10d32014-04-03 16:45:23 -0600558 if (!waitqueue_active(&nvmeq->sq_full))
Keith Busch427e9702013-04-09 11:59:32 -0600559 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
Kent Overstreet20d01892013-11-23 18:21:01 -0800560 bio_list_add(&nvmeq->sq_cong, split);
561 bio_list_add(&nvmeq->sq_cong, bio);
Keith Buschedd10d32014-04-03 16:45:23 -0600562 wake_up(&nvmeq->sq_full);
Keith Busch427e9702013-04-09 11:59:32 -0600563
564 return 0;
565}
566
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500567/* NVMe scatterlists require no holes in the virtual address */
568#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
569 (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
570
Keith Busch427e9702013-04-09 11:59:32 -0600571static int nvme_map_bio(struct nvme_queue *nvmeq, struct nvme_iod *iod,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500572 struct bio *bio, enum dma_data_direction dma_dir, int psegs)
573{
Kent Overstreet79886132013-11-23 17:19:00 -0800574 struct bio_vec bvec, bvprv;
575 struct bvec_iter iter;
Matthew Wilcox76830842011-02-10 13:55:39 -0500576 struct scatterlist *sg = NULL;
Kent Overstreet79886132013-11-23 17:19:00 -0800577 int length = 0, nsegs = 0, split_len = bio->bi_iter.bi_size;
578 int first = 1;
Keith Busch159b67d2013-04-09 17:13:20 -0600579
580 if (nvmeq->dev->stripe_size)
581 split_len = nvmeq->dev->stripe_size -
Kent Overstreet4f024f32013-10-11 15:44:27 -0700582 ((bio->bi_iter.bi_sector << 9) &
583 (nvmeq->dev->stripe_size - 1));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500584
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500585 sg_init_table(iod->sg, psegs);
Kent Overstreet79886132013-11-23 17:19:00 -0800586 bio_for_each_segment(bvec, bio, iter) {
587 if (!first && BIOVEC_PHYS_MERGEABLE(&bvprv, &bvec)) {
588 sg->length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500589 } else {
Kent Overstreet79886132013-11-23 17:19:00 -0800590 if (!first && BIOVEC_NOT_VIRT_MERGEABLE(&bvprv, &bvec))
591 return nvme_split_and_submit(bio, nvmeq,
Kent Overstreet20d01892013-11-23 18:21:01 -0800592 length);
Keith Busch427e9702013-04-09 11:59:32 -0600593
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500594 sg = sg ? sg + 1 : iod->sg;
Kent Overstreet79886132013-11-23 17:19:00 -0800595 sg_set_page(sg, bvec.bv_page,
596 bvec.bv_len, bvec.bv_offset);
Matthew Wilcox76830842011-02-10 13:55:39 -0500597 nsegs++;
598 }
Keith Busch159b67d2013-04-09 17:13:20 -0600599
Kent Overstreet79886132013-11-23 17:19:00 -0800600 if (split_len - length < bvec.bv_len)
Kent Overstreet20d01892013-11-23 18:21:01 -0800601 return nvme_split_and_submit(bio, nvmeq, split_len);
Kent Overstreet79886132013-11-23 17:19:00 -0800602 length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500603 bvprv = bvec;
Kent Overstreet79886132013-11-23 17:19:00 -0800604 first = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500605 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500606 iod->nents = nsegs;
Matthew Wilcox76830842011-02-10 13:55:39 -0500607 sg_mark_end(sg);
Keith Busch427e9702013-04-09 11:59:32 -0600608 if (dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir) == 0)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500609 return -ENOMEM;
Keith Busch427e9702013-04-09 11:59:32 -0600610
Kent Overstreet4f024f32013-10-11 15:44:27 -0700611 BUG_ON(length != bio->bi_iter.bi_size);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500612 return length;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500613}
614
Keith Busch0e5e4f02012-11-09 16:33:05 -0700615static int nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
616 struct bio *bio, struct nvme_iod *iod, int cmdid)
617{
Keith Buschedd10d32014-04-03 16:45:23 -0600618 struct nvme_dsm_range *range =
619 (struct nvme_dsm_range *)iod_list(iod)[0];
Keith Busch0e5e4f02012-11-09 16:33:05 -0700620 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
621
Keith Busch0e5e4f02012-11-09 16:33:05 -0700622 range->cattr = cpu_to_le32(0);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700623 range->nlb = cpu_to_le32(bio->bi_iter.bi_size >> ns->lba_shift);
624 range->slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700625
626 memset(cmnd, 0, sizeof(*cmnd));
627 cmnd->dsm.opcode = nvme_cmd_dsm;
628 cmnd->dsm.command_id = cmdid;
629 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
630 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
631 cmnd->dsm.nr = 0;
632 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
633
634 if (++nvmeq->sq_tail == nvmeq->q_depth)
635 nvmeq->sq_tail = 0;
636 writel(nvmeq->sq_tail, nvmeq->q_db);
637
638 return 0;
639}
640
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500641static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
642 int cmdid)
643{
644 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
645
646 memset(cmnd, 0, sizeof(*cmnd));
647 cmnd->common.opcode = nvme_cmd_flush;
648 cmnd->common.command_id = cmdid;
649 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
650
651 if (++nvmeq->sq_tail == nvmeq->q_depth)
652 nvmeq->sq_tail = 0;
653 writel(nvmeq->sq_tail, nvmeq->q_db);
654
655 return 0;
656}
657
Keith Buschedd10d32014-04-03 16:45:23 -0600658static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500659{
Keith Buschedd10d32014-04-03 16:45:23 -0600660 struct bio *bio = iod->private;
661 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500662 struct nvme_command *cmnd;
Keith Buschedd10d32014-04-03 16:45:23 -0600663 int cmdid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500664 u16 control;
665 u32 dsmgmt;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500666
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500667 cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500668 if (unlikely(cmdid < 0))
Keith Buschedd10d32014-04-03 16:45:23 -0600669 return cmdid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500670
Keith Buschedd10d32014-04-03 16:45:23 -0600671 if (bio->bi_rw & REQ_DISCARD)
672 return nvme_submit_discard(nvmeq, ns, bio, iod, cmdid);
Keith Busch53562be2014-04-29 11:41:29 -0600673 if (bio->bi_rw & REQ_FLUSH)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500674 return nvme_submit_flush(nvmeq, ns, cmdid);
675
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500676 control = 0;
677 if (bio->bi_rw & REQ_FUA)
678 control |= NVME_RW_FUA;
679 if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
680 control |= NVME_RW_LR;
681
682 dsmgmt = 0;
683 if (bio->bi_rw & REQ_RAHEAD)
684 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
685
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500686 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500687 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500688
Keith Buschedd10d32014-04-03 16:45:23 -0600689 cmnd->rw.opcode = bio_data_dir(bio) ? nvme_cmd_write : nvme_cmd_read;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500690 cmnd->rw.command_id = cmdid;
691 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Keith Buschedd10d32014-04-03 16:45:23 -0600692 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
693 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700694 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Keith Buschedd10d32014-04-03 16:45:23 -0600695 cmnd->rw.length =
696 cpu_to_le16((bio->bi_iter.bi_size >> ns->lba_shift) - 1);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500697 cmnd->rw.control = cpu_to_le16(control);
698 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500699
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500700 if (++nvmeq->sq_tail == nvmeq->q_depth)
701 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500702 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500703
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500704 return 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600705}
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500706
Keith Busch53562be2014-04-29 11:41:29 -0600707static int nvme_split_flush_data(struct nvme_queue *nvmeq, struct bio *bio)
708{
709 struct bio *split = bio_clone(bio, GFP_ATOMIC);
710 if (!split)
711 return -ENOMEM;
712
713 split->bi_iter.bi_size = 0;
714 split->bi_phys_segments = 0;
715 bio->bi_rw &= ~REQ_FLUSH;
716 bio_chain(split, bio);
717
718 if (!waitqueue_active(&nvmeq->sq_full))
719 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
720 bio_list_add(&nvmeq->sq_cong, split);
721 bio_list_add(&nvmeq->sq_cong, bio);
722 wake_up_process(nvme_thread);
723
724 return 0;
725}
726
Keith Buschedd10d32014-04-03 16:45:23 -0600727/*
728 * Called with local interrupts disabled and the q_lock held. May not sleep.
729 */
730static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
731 struct bio *bio)
732{
733 struct nvme_iod *iod;
734 int psegs = bio_phys_segments(ns->queue, bio);
735 int result;
736
Keith Busch53562be2014-04-29 11:41:29 -0600737 if ((bio->bi_rw & REQ_FLUSH) && psegs)
738 return nvme_split_flush_data(nvmeq, bio);
Keith Buschedd10d32014-04-03 16:45:23 -0600739
740 iod = nvme_alloc_iod(psegs, bio->bi_iter.bi_size, GFP_ATOMIC);
741 if (!iod)
742 return -ENOMEM;
743
744 iod->private = bio;
745 if (bio->bi_rw & REQ_DISCARD) {
746 void *range;
747 /*
748 * We reuse the small pool to allocate the 16-byte range here
749 * as it is not worth having a special pool for these or
750 * additional cases to handle freeing the iod.
751 */
752 range = dma_pool_alloc(nvmeq->dev->prp_small_pool,
753 GFP_ATOMIC,
754 &iod->first_dma);
755 if (!range) {
756 result = -ENOMEM;
757 goto free_iod;
758 }
759 iod_list(iod)[0] = (__le64 *)range;
760 iod->npages = 0;
761 } else if (psegs) {
762 result = nvme_map_bio(nvmeq, iod, bio,
763 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE,
764 psegs);
765 if (result <= 0)
766 goto free_iod;
767 if (nvme_setup_prps(nvmeq->dev, iod, result, GFP_ATOMIC) !=
768 result) {
769 result = -ENOMEM;
770 goto free_iod;
771 }
772 nvme_start_io_acct(bio);
773 }
774 if (unlikely(nvme_submit_iod(nvmeq, iod))) {
775 if (!waitqueue_active(&nvmeq->sq_full))
776 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
777 list_add_tail(&iod->node, &nvmeq->iod_bio);
778 }
779 return 0;
780
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500781 free_iod:
782 nvme_free_iod(nvmeq->dev, iod);
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500783 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500784}
785
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400786static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500787{
Matthew Wilcox82123462011-01-20 13:24:06 -0500788 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500789
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500790 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500791 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500792
793 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400794 void *ctx;
795 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500796 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500797 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500798 break;
799 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
800 if (++head == nvmeq->q_depth) {
801 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500802 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500803 }
804
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400805 ctx = free_cmdid(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600806 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500807 }
808
809 /* If the controller ignores the cq head doorbell and continuously
810 * writes to the queue, it is theoretically possible to wrap around
811 * the queue twice and mistakenly return IRQ_NONE. Linux only
812 * requires that 0.1% of your interrupts are handled, so this isn't
813 * a big problem.
814 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500815 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400816 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500817
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800818 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500819 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500820 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500821
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400822 nvmeq->cqe_seen = 1;
823 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500824}
825
Matthew Wilcox7d822452013-06-24 12:03:57 -0400826static void nvme_make_request(struct request_queue *q, struct bio *bio)
827{
828 struct nvme_ns *ns = q->queuedata;
829 struct nvme_queue *nvmeq = get_nvmeq(ns->dev);
830 int result = -EBUSY;
831
Keith Buschcd638942013-07-15 15:02:23 -0600832 if (!nvmeq) {
Keith Buschcd638942013-07-15 15:02:23 -0600833 bio_endio(bio, -EIO);
834 return;
835 }
836
Matthew Wilcox7d822452013-06-24 12:03:57 -0400837 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -0600838 if (!nvmeq->q_suspended && bio_list_empty(&nvmeq->sq_cong))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400839 result = nvme_submit_bio_queue(nvmeq, ns, bio);
840 if (unlikely(result)) {
Keith Buschedd10d32014-04-03 16:45:23 -0600841 if (!waitqueue_active(&nvmeq->sq_full))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400842 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
843 bio_list_add(&nvmeq->sq_cong, bio);
844 }
845
846 nvme_process_cq(nvmeq);
847 spin_unlock_irq(&nvmeq->q_lock);
848 put_nvmeq(nvmeq);
849}
850
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500851static irqreturn_t nvme_irq(int irq, void *data)
852{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500853 irqreturn_t result;
854 struct nvme_queue *nvmeq = data;
855 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400856 nvme_process_cq(nvmeq);
857 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
858 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500859 spin_unlock(&nvmeq->q_lock);
860 return result;
861}
862
863static irqreturn_t nvme_irq_check(int irq, void *data)
864{
865 struct nvme_queue *nvmeq = data;
866 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
867 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
868 return IRQ_NONE;
869 return IRQ_WAKE_THREAD;
870}
871
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500872static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
873{
874 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400875 cancel_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500876 spin_unlock_irq(&nvmeq->q_lock);
877}
878
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400879struct sync_cmd_info {
880 struct task_struct *task;
881 u32 result;
882 int status;
883};
884
Keith Buschedd10d32014-04-03 16:45:23 -0600885static void sync_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400886 struct nvme_completion *cqe)
887{
888 struct sync_cmd_info *cmdinfo = ctx;
889 cmdinfo->result = le32_to_cpup(&cqe->result);
890 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
891 wake_up_process(cmdinfo->task);
892}
893
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500894/*
895 * Returns 0 on success. If the result is negative, it's a Linux error code;
896 * if the result is positive, it's an NVM Express status code
897 */
Keith Busch4f5099a2014-03-03 16:39:13 -0700898static int nvme_submit_sync_cmd(struct nvme_dev *dev, int q_idx,
899 struct nvme_command *cmd,
Vishal Verma5d0f6132013-03-04 18:40:58 -0700900 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500901{
Keith Busch4f5099a2014-03-03 16:39:13 -0700902 int cmdid, ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500903 struct sync_cmd_info cmdinfo;
Keith Busch4f5099a2014-03-03 16:39:13 -0700904 struct nvme_queue *nvmeq;
905
906 nvmeq = lock_nvmeq(dev, q_idx);
Keith Buscha51afb52014-05-13 10:32:46 -0600907 if (!nvmeq)
Keith Busch4f5099a2014-03-03 16:39:13 -0700908 return -ENODEV;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500909
910 cmdinfo.task = current;
911 cmdinfo.status = -EINTR;
912
Keith Busch4f5099a2014-03-03 16:39:13 -0700913 cmdid = alloc_cmdid(nvmeq, &cmdinfo, sync_completion, timeout);
914 if (cmdid < 0) {
915 unlock_nvmeq(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500916 return cmdid;
Keith Busch4f5099a2014-03-03 16:39:13 -0700917 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500918 cmd->common.command_id = cmdid;
919
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500920 set_current_state(TASK_KILLABLE);
Keith Busch4f5099a2014-03-03 16:39:13 -0700921 ret = nvme_submit_cmd(nvmeq, cmd);
922 if (ret) {
923 free_cmdid(nvmeq, cmdid, NULL);
924 unlock_nvmeq(nvmeq);
925 set_current_state(TASK_RUNNING);
926 return ret;
927 }
928 unlock_nvmeq(nvmeq);
Keith Busch78f8d252013-04-19 14:11:06 -0600929 schedule_timeout(timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500930
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500931 if (cmdinfo.status == -EINTR) {
Keith Busch4f5099a2014-03-03 16:39:13 -0700932 nvmeq = lock_nvmeq(dev, q_idx);
Keith Buscha51afb52014-05-13 10:32:46 -0600933 if (nvmeq) {
Keith Busch4f5099a2014-03-03 16:39:13 -0700934 nvme_abort_command(nvmeq, cmdid);
Keith Buscha51afb52014-05-13 10:32:46 -0600935 unlock_nvmeq(nvmeq);
936 }
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500937 return -EINTR;
938 }
939
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500940 if (result)
941 *result = cmdinfo.result;
942
943 return cmdinfo.status;
944}
945
Keith Busch4d115422013-12-10 13:10:40 -0700946static int nvme_submit_async_cmd(struct nvme_queue *nvmeq,
947 struct nvme_command *cmd,
948 struct async_cmd_info *cmdinfo, unsigned timeout)
949{
950 int cmdid;
951
952 cmdid = alloc_cmdid_killable(nvmeq, cmdinfo, async_completion, timeout);
953 if (cmdid < 0)
954 return cmdid;
955 cmdinfo->status = -EINTR;
956 cmd->common.command_id = cmdid;
Keith Busch4f5099a2014-03-03 16:39:13 -0700957 return nvme_submit_cmd(nvmeq, cmd);
Keith Busch4d115422013-12-10 13:10:40 -0700958}
959
Vishal Verma5d0f6132013-03-04 18:40:58 -0700960int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500961 u32 *result)
962{
Keith Busch4f5099a2014-03-03 16:39:13 -0700963 return nvme_submit_sync_cmd(dev, 0, cmd, result, ADMIN_TIMEOUT);
964}
965
966int nvme_submit_io_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
967 u32 *result)
968{
969 return nvme_submit_sync_cmd(dev, smp_processor_id() + 1, cmd, result,
970 NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500971}
972
Keith Busch4d115422013-12-10 13:10:40 -0700973static int nvme_submit_admin_cmd_async(struct nvme_dev *dev,
974 struct nvme_command *cmd, struct async_cmd_info *cmdinfo)
975{
Keith Busch5a92e702014-02-21 14:13:44 -0700976 return nvme_submit_async_cmd(raw_nvmeq(dev, 0), cmd, cmdinfo,
Keith Busch4d115422013-12-10 13:10:40 -0700977 ADMIN_TIMEOUT);
978}
979
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500980static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
981{
982 int status;
983 struct nvme_command c;
984
985 memset(&c, 0, sizeof(c));
986 c.delete_queue.opcode = opcode;
987 c.delete_queue.qid = cpu_to_le16(id);
988
989 status = nvme_submit_admin_cmd(dev, &c, NULL);
990 if (status)
991 return -EIO;
992 return 0;
993}
994
995static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
996 struct nvme_queue *nvmeq)
997{
998 int status;
999 struct nvme_command c;
1000 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1001
1002 memset(&c, 0, sizeof(c));
1003 c.create_cq.opcode = nvme_admin_create_cq;
1004 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1005 c.create_cq.cqid = cpu_to_le16(qid);
1006 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1007 c.create_cq.cq_flags = cpu_to_le16(flags);
1008 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1009
1010 status = nvme_submit_admin_cmd(dev, &c, NULL);
1011 if (status)
1012 return -EIO;
1013 return 0;
1014}
1015
1016static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1017 struct nvme_queue *nvmeq)
1018{
1019 int status;
1020 struct nvme_command c;
1021 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1022
1023 memset(&c, 0, sizeof(c));
1024 c.create_sq.opcode = nvme_admin_create_sq;
1025 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1026 c.create_sq.sqid = cpu_to_le16(qid);
1027 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1028 c.create_sq.sq_flags = cpu_to_le16(flags);
1029 c.create_sq.cqid = cpu_to_le16(qid);
1030
1031 status = nvme_submit_admin_cmd(dev, &c, NULL);
1032 if (status)
1033 return -EIO;
1034 return 0;
1035}
1036
1037static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1038{
1039 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1040}
1041
1042static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1043{
1044 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1045}
1046
Vishal Verma5d0f6132013-03-04 18:40:58 -07001047int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001048 dma_addr_t dma_addr)
1049{
1050 struct nvme_command c;
1051
1052 memset(&c, 0, sizeof(c));
1053 c.identify.opcode = nvme_admin_identify;
1054 c.identify.nsid = cpu_to_le32(nsid);
1055 c.identify.prp1 = cpu_to_le64(dma_addr);
1056 c.identify.cns = cpu_to_le32(cns);
1057
1058 return nvme_submit_admin_cmd(dev, &c, NULL);
1059}
1060
Vishal Verma5d0f6132013-03-04 18:40:58 -07001061int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -06001062 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001063{
1064 struct nvme_command c;
1065
1066 memset(&c, 0, sizeof(c));
1067 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -06001068 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001069 c.features.prp1 = cpu_to_le64(dma_addr);
1070 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001071
Keith Busch08df1e02012-09-21 10:52:13 -06001072 return nvme_submit_admin_cmd(dev, &c, result);
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001073}
1074
Vishal Verma5d0f6132013-03-04 18:40:58 -07001075int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
1076 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001077{
1078 struct nvme_command c;
1079
1080 memset(&c, 0, sizeof(c));
1081 c.features.opcode = nvme_admin_set_features;
1082 c.features.prp1 = cpu_to_le64(dma_addr);
1083 c.features.fid = cpu_to_le32(fid);
1084 c.features.dword11 = cpu_to_le32(dword11);
1085
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001086 return nvme_submit_admin_cmd(dev, &c, result);
1087}
1088
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001089/**
Keith Buschc30341d2013-12-10 13:10:38 -07001090 * nvme_abort_cmd - Attempt aborting a command
1091 * @cmdid: Command id of a timed out IO
1092 * @queue: The queue with timed out IO
1093 *
1094 * Schedule controller reset if the command was already aborted once before and
1095 * still hasn't been returned to the driver, or if this is the admin queue.
1096 */
1097static void nvme_abort_cmd(int cmdid, struct nvme_queue *nvmeq)
1098{
1099 int a_cmdid;
1100 struct nvme_command cmd;
1101 struct nvme_dev *dev = nvmeq->dev;
1102 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Keith Busch5a92e702014-02-21 14:13:44 -07001103 struct nvme_queue *adminq;
Keith Buschc30341d2013-12-10 13:10:38 -07001104
1105 if (!nvmeq->qid || info[cmdid].aborted) {
1106 if (work_busy(&dev->reset_work))
1107 return;
1108 list_del_init(&dev->node);
1109 dev_warn(&dev->pci_dev->dev,
1110 "I/O %d QID %d timeout, reset controller\n", cmdid,
1111 nvmeq->qid);
Tejun Heo9ca97372014-03-07 10:24:49 -05001112 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschc30341d2013-12-10 13:10:38 -07001113 queue_work(nvme_workq, &dev->reset_work);
1114 return;
1115 }
1116
1117 if (!dev->abort_limit)
1118 return;
1119
Keith Busch5a92e702014-02-21 14:13:44 -07001120 adminq = rcu_dereference(dev->queues[0]);
1121 a_cmdid = alloc_cmdid(adminq, CMD_CTX_ABORT, special_completion,
Keith Buschc30341d2013-12-10 13:10:38 -07001122 ADMIN_TIMEOUT);
1123 if (a_cmdid < 0)
1124 return;
1125
1126 memset(&cmd, 0, sizeof(cmd));
1127 cmd.abort.opcode = nvme_admin_abort_cmd;
1128 cmd.abort.cid = cmdid;
1129 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
1130 cmd.abort.command_id = a_cmdid;
1131
1132 --dev->abort_limit;
1133 info[cmdid].aborted = 1;
1134 info[cmdid].timeout = jiffies + ADMIN_TIMEOUT;
1135
1136 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", cmdid,
1137 nvmeq->qid);
Keith Busch5a92e702014-02-21 14:13:44 -07001138 nvme_submit_cmd(adminq, &cmd);
Keith Buschc30341d2013-12-10 13:10:38 -07001139}
1140
1141/**
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001142 * nvme_cancel_ios - Cancel outstanding I/Os
1143 * @queue: The queue to cancel I/Os on
1144 * @timeout: True to only cancel I/Os which have timed out
1145 */
1146static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout)
1147{
1148 int depth = nvmeq->q_depth - 1;
1149 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
1150 unsigned long now = jiffies;
1151 int cmdid;
1152
1153 for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
1154 void *ctx;
1155 nvme_completion_fn fn;
1156 static struct nvme_completion cqe = {
Matthew Wilcoxaf2d9ca2013-04-16 15:18:30 -04001157 .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1),
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001158 };
1159
1160 if (timeout && !time_after(now, info[cmdid].timeout))
1161 continue;
Keith Busch053ab7022013-04-30 11:19:38 -06001162 if (info[cmdid].ctx == CMD_CTX_CANCELLED)
1163 continue;
Keith Buschc30341d2013-12-10 13:10:38 -07001164 if (timeout && nvmeq->dev->initialized) {
1165 nvme_abort_cmd(cmdid, nvmeq);
1166 continue;
1167 }
1168 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n", cmdid,
1169 nvmeq->qid);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001170 ctx = cancel_cmdid(nvmeq, cmdid, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -06001171 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001172 }
1173}
1174
Keith Busch5a92e702014-02-21 14:13:44 -07001175static void nvme_free_queue(struct rcu_head *r)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001176{
Keith Busch5a92e702014-02-21 14:13:44 -07001177 struct nvme_queue *nvmeq = container_of(r, struct nvme_queue, r_head);
1178
Keith Busch22404272013-07-15 15:02:20 -06001179 spin_lock_irq(&nvmeq->q_lock);
1180 while (bio_list_peek(&nvmeq->sq_cong)) {
1181 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1182 bio_endio(bio, -EIO);
1183 }
Keith Buschedd10d32014-04-03 16:45:23 -06001184 while (!list_empty(&nvmeq->iod_bio)) {
1185 static struct nvme_completion cqe = {
1186 .status = cpu_to_le16(
1187 (NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1),
1188 };
1189 struct nvme_iod *iod = list_first_entry(&nvmeq->iod_bio,
1190 struct nvme_iod,
1191 node);
1192 list_del(&iod->node);
1193 bio_completion(nvmeq, iod, &cqe);
1194 }
Keith Busch22404272013-07-15 15:02:20 -06001195 spin_unlock_irq(&nvmeq->q_lock);
1196
Matthew Wilcox9e866772012-08-03 13:55:56 -04001197 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1198 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1199 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1200 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
Keith Busch42f61422014-03-24 10:46:25 -06001201 if (nvmeq->qid)
1202 free_cpumask_var(nvmeq->cpu_mask);
Matthew Wilcox9e866772012-08-03 13:55:56 -04001203 kfree(nvmeq);
1204}
1205
Keith Buscha1a5ef92013-12-16 13:50:00 -05001206static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001207{
1208 int i;
1209
Keith Buscha1a5ef92013-12-16 13:50:00 -05001210 for (i = dev->queue_count - 1; i >= lowest; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07001211 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
1212 rcu_assign_pointer(dev->queues[i], NULL);
1213 call_rcu(&nvmeq->r_head, nvme_free_queue);
Keith Busch22404272013-07-15 15:02:20 -06001214 dev->queue_count--;
Keith Busch22404272013-07-15 15:02:20 -06001215 }
1216}
1217
Keith Busch4d115422013-12-10 13:10:40 -07001218/**
1219 * nvme_suspend_queue - put queue into suspended state
1220 * @nvmeq - queue to suspend
1221 *
1222 * Returns 1 if already suspended, 0 otherwise.
1223 */
1224static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001225{
Keith Busch4d115422013-12-10 13:10:40 -07001226 int vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001227
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001228 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001229 if (nvmeq->q_suspended) {
1230 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07001231 return 1;
Keith Busch32958742012-08-20 14:57:49 -06001232 }
Keith Busch22404272013-07-15 15:02:20 -06001233 nvmeq->q_suspended = 1;
Keith Busch42f61422014-03-24 10:46:25 -06001234 nvmeq->dev->online_queues--;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001235 spin_unlock_irq(&nvmeq->q_lock);
1236
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001237 irq_set_affinity_hint(vector, NULL);
1238 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001239
Keith Busch4d115422013-12-10 13:10:40 -07001240 return 0;
1241}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001242
Keith Busch4d115422013-12-10 13:10:40 -07001243static void nvme_clear_queue(struct nvme_queue *nvmeq)
1244{
Keith Busch22404272013-07-15 15:02:20 -06001245 spin_lock_irq(&nvmeq->q_lock);
1246 nvme_process_cq(nvmeq);
1247 nvme_cancel_ios(nvmeq, false);
1248 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001249}
1250
Keith Busch4d115422013-12-10 13:10:40 -07001251static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1252{
Keith Busch5a92e702014-02-21 14:13:44 -07001253 struct nvme_queue *nvmeq = raw_nvmeq(dev, qid);
Keith Busch4d115422013-12-10 13:10:40 -07001254
1255 if (!nvmeq)
1256 return;
1257 if (nvme_suspend_queue(nvmeq))
1258 return;
1259
Keith Busch0e53d182013-12-10 13:10:39 -07001260 /* Don't tell the adapter to delete the admin queue.
1261 * Don't tell a removed adapter to delete IO queues. */
1262 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001263 adapter_delete_sq(dev, qid);
1264 adapter_delete_cq(dev, qid);
1265 }
Keith Busch4d115422013-12-10 13:10:40 -07001266 nvme_clear_queue(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001267}
1268
1269static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
1270 int depth, int vector)
1271{
1272 struct device *dmadev = &dev->pci_dev->dev;
Keith Busch22404272013-07-15 15:02:20 -06001273 unsigned extra = nvme_queue_extra(depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001274 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
1275 if (!nvmeq)
1276 return NULL;
1277
1278 nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
1279 &nvmeq->cq_dma_addr, GFP_KERNEL);
1280 if (!nvmeq->cqes)
1281 goto free_nvmeq;
1282 memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
1283
1284 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
1285 &nvmeq->sq_dma_addr, GFP_KERNEL);
1286 if (!nvmeq->sq_cmds)
1287 goto free_cqdma;
1288
Keith Busch42f61422014-03-24 10:46:25 -06001289 if (qid && !zalloc_cpumask_var(&nvmeq->cpu_mask, GFP_KERNEL))
1290 goto free_sqdma;
1291
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001292 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001293 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001294 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1295 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001296 spin_lock_init(&nvmeq->q_lock);
1297 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001298 nvmeq->cq_phase = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001299 init_waitqueue_head(&nvmeq->sq_full);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001300 init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001301 bio_list_init(&nvmeq->sq_cong);
Keith Buschedd10d32014-04-03 16:45:23 -06001302 INIT_LIST_HEAD(&nvmeq->iod_bio);
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001303 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001304 nvmeq->q_depth = depth;
1305 nvmeq->cq_vector = vector;
Keith Buschc30341d2013-12-10 13:10:38 -07001306 nvmeq->qid = qid;
Keith Busch22404272013-07-15 15:02:20 -06001307 nvmeq->q_suspended = 1;
1308 dev->queue_count++;
Keith Busch5a92e702014-02-21 14:13:44 -07001309 rcu_assign_pointer(dev->queues[qid], nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001310
1311 return nvmeq;
1312
Keith Busch42f61422014-03-24 10:46:25 -06001313 free_sqdma:
1314 dma_free_coherent(dmadev, SQ_SIZE(depth), (void *)nvmeq->sq_cmds,
1315 nvmeq->sq_dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001316 free_cqdma:
Keith Busch68b8eca2013-05-01 13:07:47 -06001317 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001318 nvmeq->cq_dma_addr);
1319 free_nvmeq:
1320 kfree(nvmeq);
1321 return NULL;
1322}
1323
Matthew Wilcox30010822011-01-20 09:10:15 -05001324static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1325 const char *name)
1326{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001327 if (use_threaded_interrupts)
1328 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001329 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001330 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001331 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001332 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001333}
1334
Keith Busch22404272013-07-15 15:02:20 -06001335static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001336{
Keith Busch22404272013-07-15 15:02:20 -06001337 struct nvme_dev *dev = nvmeq->dev;
1338 unsigned extra = nvme_queue_extra(nvmeq->q_depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001339
Keith Busch22404272013-07-15 15:02:20 -06001340 nvmeq->sq_tail = 0;
1341 nvmeq->cq_head = 0;
1342 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001343 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001344 memset(nvmeq->cmdid_data, 0, extra);
1345 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
1346 nvme_cancel_ios(nvmeq, false);
1347 nvmeq->q_suspended = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001348 dev->online_queues++;
Keith Busch22404272013-07-15 15:02:20 -06001349}
1350
1351static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1352{
1353 struct nvme_dev *dev = nvmeq->dev;
1354 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001355
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001356 result = adapter_alloc_cq(dev, qid, nvmeq);
1357 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001358 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001359
1360 result = adapter_alloc_sq(dev, qid, nvmeq);
1361 if (result < 0)
1362 goto release_cq;
1363
Matthew Wilcox3193f072014-01-27 15:57:22 -05001364 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001365 if (result < 0)
1366 goto release_sq;
1367
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001368 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001369 nvme_init_queue(nvmeq, qid);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001370 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001371
1372 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001373
1374 release_sq:
1375 adapter_delete_sq(dev, qid);
1376 release_cq:
1377 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001378 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001379}
1380
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001381static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1382{
1383 unsigned long timeout;
1384 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1385
1386 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1387
1388 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1389 msleep(100);
1390 if (fatal_signal_pending(current))
1391 return -EINTR;
1392 if (time_after(jiffies, timeout)) {
1393 dev_err(&dev->pci_dev->dev,
Matthew Wilcox27e81662014-04-11 11:58:45 -04001394 "Device not ready; aborting %s\n", enabled ?
1395 "initialisation" : "reset");
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001396 return -ENODEV;
1397 }
1398 }
1399
1400 return 0;
1401}
1402
1403/*
1404 * If the device has been passed off to us in an enabled state, just clear
1405 * the enabled bit. The spec says we should set the 'shutdown notification
1406 * bits', but doing so may cause the device to complete commands to the
1407 * admin queue ... and we don't know what memory that might be pointing at!
1408 */
1409static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1410{
Matthew Wilcox44af1462013-05-04 06:43:17 -04001411 u32 cc = readl(&dev->bar->cc);
1412
1413 if (cc & NVME_CC_ENABLE)
1414 writel(cc & ~NVME_CC_ENABLE, &dev->bar->cc);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001415 return nvme_wait_ready(dev, cap, false);
1416}
1417
1418static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1419{
1420 return nvme_wait_ready(dev, cap, true);
1421}
1422
Keith Busch1894d8f2013-07-15 15:02:22 -06001423static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1424{
1425 unsigned long timeout;
1426 u32 cc;
1427
1428 cc = (readl(&dev->bar->cc) & ~NVME_CC_SHN_MASK) | NVME_CC_SHN_NORMAL;
1429 writel(cc, &dev->bar->cc);
1430
1431 timeout = 2 * HZ + jiffies;
1432 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1433 NVME_CSTS_SHST_CMPLT) {
1434 msleep(100);
1435 if (fatal_signal_pending(current))
1436 return -EINTR;
1437 if (time_after(jiffies, timeout)) {
1438 dev_err(&dev->pci_dev->dev,
1439 "Device shutdown incomplete; abort shutdown\n");
1440 return -ENODEV;
1441 }
1442 }
1443
1444 return 0;
1445}
1446
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001447static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001448{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001449 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001450 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001451 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001452 struct nvme_queue *nvmeq;
1453
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001454 result = nvme_disable_ctrl(dev, cap);
1455 if (result < 0)
1456 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001457
Keith Busch5a92e702014-02-21 14:13:44 -07001458 nvmeq = raw_nvmeq(dev, 0);
Keith Buschcd638942013-07-15 15:02:23 -06001459 if (!nvmeq) {
1460 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
1461 if (!nvmeq)
1462 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001463 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001464
1465 aqa = nvmeq->q_depth - 1;
1466 aqa |= aqa << 16;
1467
1468 dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
1469 dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
1470 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001471 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001472
1473 writel(aqa, &dev->bar->aqa);
1474 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1475 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
1476 writel(dev->ctrl_config, &dev->bar->cc);
1477
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001478 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001479 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001480 return result;
Matthew Wilcox9e866772012-08-03 13:55:56 -04001481
Matthew Wilcox3193f072014-01-27 15:57:22 -05001482 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Keith Busch025c5572013-05-01 13:07:51 -06001483 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001484 return result;
Keith Busch025c5572013-05-01 13:07:51 -06001485
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001486 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001487 nvme_init_queue(nvmeq, 0);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001488 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001489 return result;
1490}
1491
Vishal Verma5d0f6132013-03-04 18:40:58 -07001492struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001493 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001494{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001495 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001496 struct scatterlist *sg;
1497 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001498 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001499
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001500 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001501 return ERR_PTR(-EINVAL);
Dan Carpenter5460fc02013-05-13 17:59:50 +03001502 if (!length || length > INT_MAX - PAGE_SIZE)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001503 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001504
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001505 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001506 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1507 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001508 if (!pages)
1509 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001510
1511 err = get_user_pages_fast(addr, count, 1, pages);
1512 if (err < count) {
1513 count = err;
1514 err = -EFAULT;
1515 goto put_pages;
1516 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001517
Santosh Y6808c5f2014-05-29 10:01:52 +05301518 err = -ENOMEM;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001519 iod = nvme_alloc_iod(count, length, GFP_KERNEL);
Santosh Y6808c5f2014-05-29 10:01:52 +05301520 if (!iod)
1521 goto put_pages;
1522
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001523 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001524 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001525 for (i = 0; i < count; i++) {
1526 sg_set_page(&sg[i], pages[i],
Dan Carpenter5460fc02013-05-13 17:59:50 +03001527 min_t(unsigned, length, PAGE_SIZE - offset),
1528 offset);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001529 length -= (PAGE_SIZE - offset);
1530 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001531 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001532 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001533 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001534
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001535 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1536 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001537 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001538 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001539
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001540 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001541 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001542
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001543 free_iod:
1544 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001545 put_pages:
1546 for (i = 0; i < count; i++)
1547 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001548 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001549 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001550}
1551
Vishal Verma5d0f6132013-03-04 18:40:58 -07001552void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001553 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001554{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001555 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001556
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001557 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1558 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001559
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001560 for (i = 0; i < iod->nents; i++)
1561 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001562}
1563
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001564static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1565{
1566 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001567 struct nvme_user_io io;
1568 struct nvme_command c;
Keith Buschf410c682013-04-23 17:23:59 -06001569 unsigned length, meta_len;
1570 int status, i;
1571 struct nvme_iod *iod, *meta_iod = NULL;
1572 dma_addr_t meta_dma_addr;
1573 void *meta, *uninitialized_var(meta_mem);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001574
1575 if (copy_from_user(&io, uio, sizeof(io)))
1576 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001577 length = (io.nblocks + 1) << ns->lba_shift;
Keith Buschf410c682013-04-23 17:23:59 -06001578 meta_len = (io.nblocks + 1) * ns->ms;
1579
1580 if (meta_len && ((io.metadata & 3) || !io.metadata))
1581 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001582
1583 switch (io.opcode) {
1584 case nvme_cmd_write:
1585 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001586 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001587 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001588 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001589 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001590 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001591 }
1592
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001593 if (IS_ERR(iod))
1594 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001595
1596 memset(&c, 0, sizeof(c));
1597 c.rw.opcode = io.opcode;
1598 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001599 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001600 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001601 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001602 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001603 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1604 c.rw.reftag = cpu_to_le32(io.reftag);
1605 c.rw.apptag = cpu_to_le16(io.apptag);
1606 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buschf410c682013-04-23 17:23:59 -06001607
1608 if (meta_len) {
Keith Busch1b567492013-07-18 12:13:51 -06001609 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
1610 meta_len);
Keith Buschf410c682013-04-23 17:23:59 -06001611 if (IS_ERR(meta_iod)) {
1612 status = PTR_ERR(meta_iod);
1613 meta_iod = NULL;
1614 goto unmap;
1615 }
1616
1617 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1618 &meta_dma_addr, GFP_KERNEL);
1619 if (!meta_mem) {
1620 status = -ENOMEM;
1621 goto unmap;
1622 }
1623
1624 if (io.opcode & 1) {
1625 int meta_offset = 0;
1626
1627 for (i = 0; i < meta_iod->nents; i++) {
1628 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1629 meta_iod->sg[i].offset;
1630 memcpy(meta_mem + meta_offset, meta,
1631 meta_iod->sg[i].length);
1632 kunmap_atomic(meta);
1633 meta_offset += meta_iod->sg[i].length;
1634 }
1635 }
1636
1637 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1638 }
1639
Keith Buschedd10d32014-04-03 16:45:23 -06001640 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1641 c.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1642 c.rw.prp2 = cpu_to_le64(iod->first_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001643
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001644 if (length != (io.nblocks + 1) << ns->lba_shift)
1645 status = -ENOMEM;
1646 else
Keith Busch4f5099a2014-03-03 16:39:13 -07001647 status = nvme_submit_io_cmd(dev, &c, NULL);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001648
Keith Buschf410c682013-04-23 17:23:59 -06001649 if (meta_len) {
1650 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1651 int meta_offset = 0;
1652
1653 for (i = 0; i < meta_iod->nents; i++) {
1654 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1655 meta_iod->sg[i].offset;
1656 memcpy(meta, meta_mem + meta_offset,
1657 meta_iod->sg[i].length);
1658 kunmap_atomic(meta);
1659 meta_offset += meta_iod->sg[i].length;
1660 }
1661 }
1662
1663 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1664 meta_dma_addr);
1665 }
1666
1667 unmap:
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001668 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001669 nvme_free_iod(dev, iod);
Keith Buschf410c682013-04-23 17:23:59 -06001670
1671 if (meta_iod) {
1672 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1673 nvme_free_iod(dev, meta_iod);
1674 }
1675
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001676 return status;
1677}
1678
Keith Busch50af8ba2012-07-25 16:07:55 -06001679static int nvme_user_admin_cmd(struct nvme_dev *dev,
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001680 struct nvme_admin_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001681{
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001682 struct nvme_admin_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001683 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001684 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001685 struct nvme_iod *uninitialized_var(iod);
Keith Busch94f370c2013-05-09 14:01:38 -06001686 unsigned timeout;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001687
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001688 if (!capable(CAP_SYS_ADMIN))
1689 return -EACCES;
1690 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001691 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001692
1693 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001694 c.common.opcode = cmd.opcode;
1695 c.common.flags = cmd.flags;
1696 c.common.nsid = cpu_to_le32(cmd.nsid);
1697 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1698 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1699 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1700 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1701 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1702 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1703 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1704 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1705
1706 length = cmd.data_len;
1707 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001708 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1709 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001710 if (IS_ERR(iod))
1711 return PTR_ERR(iod);
Keith Buschedd10d32014-04-03 16:45:23 -06001712 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1713 c.common.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1714 c.common.prp2 = cpu_to_le64(iod->first_dma);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001715 }
1716
Keith Busch94f370c2013-05-09 14:01:38 -06001717 timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
1718 ADMIN_TIMEOUT;
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001719 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001720 status = -ENOMEM;
1721 else
Keith Busch4f5099a2014-03-03 16:39:13 -07001722 status = nvme_submit_sync_cmd(dev, 0, &c, &cmd.result, timeout);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001723
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001724 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001725 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001726 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001727 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001728
Chayan Biswascf90bc42013-05-22 22:34:49 +00001729 if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
Keith Buschf4f117f2012-09-21 10:49:05 -06001730 sizeof(cmd.result)))
1731 status = -EFAULT;
1732
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001733 return status;
1734}
1735
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001736static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1737 unsigned long arg)
1738{
1739 struct nvme_ns *ns = bdev->bd_disk->private_data;
1740
1741 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001742 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001743 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001744 return ns->ns_id;
1745 case NVME_IOCTL_ADMIN_CMD:
Keith Busch50af8ba2012-07-25 16:07:55 -06001746 return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001747 case NVME_IOCTL_SUBMIT_IO:
1748 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001749 case SG_GET_VERSION_NUM:
1750 return nvme_sg_get_version_num((void __user *)arg);
1751 case SG_IO:
1752 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001753 default:
1754 return -ENOTTY;
1755 }
1756}
1757
Keith Busch320a3822013-10-23 13:07:34 -06001758#ifdef CONFIG_COMPAT
1759static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1760 unsigned int cmd, unsigned long arg)
1761{
1762 struct nvme_ns *ns = bdev->bd_disk->private_data;
1763
1764 switch (cmd) {
1765 case SG_IO:
1766 return nvme_sg_io32(ns, arg);
1767 }
1768 return nvme_ioctl(bdev, mode, cmd, arg);
1769}
1770#else
1771#define nvme_compat_ioctl NULL
1772#endif
1773
Keith Busch9ac27092014-01-31 16:53:39 -07001774static int nvme_open(struct block_device *bdev, fmode_t mode)
1775{
1776 struct nvme_ns *ns = bdev->bd_disk->private_data;
1777 struct nvme_dev *dev = ns->dev;
1778
1779 kref_get(&dev->kref);
1780 return 0;
1781}
1782
1783static void nvme_free_dev(struct kref *kref);
1784
1785static void nvme_release(struct gendisk *disk, fmode_t mode)
1786{
1787 struct nvme_ns *ns = disk->private_data;
1788 struct nvme_dev *dev = ns->dev;
1789
1790 kref_put(&dev->kref, nvme_free_dev);
1791}
1792
Keith Busch4cc09e22014-04-02 15:45:37 -06001793static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
1794{
1795 /* some standard values */
1796 geo->heads = 1 << 6;
1797 geo->sectors = 1 << 5;
1798 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1799 return 0;
1800}
1801
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001802static const struct block_device_operations nvme_fops = {
1803 .owner = THIS_MODULE,
1804 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06001805 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07001806 .open = nvme_open,
1807 .release = nvme_release,
Keith Busch4cc09e22014-04-02 15:45:37 -06001808 .getgeo = nvme_getgeo,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001809};
1810
Keith Buschedd10d32014-04-03 16:45:23 -06001811static void nvme_resubmit_iods(struct nvme_queue *nvmeq)
1812{
1813 struct nvme_iod *iod, *next;
1814
1815 list_for_each_entry_safe(iod, next, &nvmeq->iod_bio, node) {
1816 if (unlikely(nvme_submit_iod(nvmeq, iod)))
1817 break;
1818 list_del(&iod->node);
1819 if (bio_list_empty(&nvmeq->sq_cong) &&
1820 list_empty(&nvmeq->iod_bio))
1821 remove_wait_queue(&nvmeq->sq_full,
1822 &nvmeq->sq_cong_wait);
1823 }
1824}
1825
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001826static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1827{
1828 while (bio_list_peek(&nvmeq->sq_cong)) {
1829 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1830 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Keith Busch427e9702013-04-09 11:59:32 -06001831
Keith Buschedd10d32014-04-03 16:45:23 -06001832 if (bio_list_empty(&nvmeq->sq_cong) &&
1833 list_empty(&nvmeq->iod_bio))
Matthew Wilcox3cb967c2011-03-16 16:45:49 -04001834 remove_wait_queue(&nvmeq->sq_full,
1835 &nvmeq->sq_cong_wait);
Keith Busch427e9702013-04-09 11:59:32 -06001836 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
Keith Buschedd10d32014-04-03 16:45:23 -06001837 if (!waitqueue_active(&nvmeq->sq_full))
Keith Busch427e9702013-04-09 11:59:32 -06001838 add_wait_queue(&nvmeq->sq_full,
1839 &nvmeq->sq_cong_wait);
1840 bio_list_add_head(&nvmeq->sq_cong, bio);
1841 break;
1842 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001843 }
1844}
1845
1846static int nvme_kthread(void *data)
1847{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001848 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001849
1850 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001851 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001852 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001853 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001854 int i;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001855 if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
1856 dev->initialized) {
1857 if (work_busy(&dev->reset_work))
1858 continue;
1859 list_del_init(&dev->node);
1860 dev_warn(&dev->pci_dev->dev,
1861 "Failed status, reset controller\n");
Tejun Heo9ca97372014-03-07 10:24:49 -05001862 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001863 queue_work(nvme_workq, &dev->reset_work);
1864 continue;
1865 }
Keith Busch5a92e702014-02-21 14:13:44 -07001866 rcu_read_lock();
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001867 for (i = 0; i < dev->queue_count; i++) {
Keith Busch5a92e702014-02-21 14:13:44 -07001868 struct nvme_queue *nvmeq =
1869 rcu_dereference(dev->queues[i]);
Matthew Wilcox740216f2011-02-15 16:28:20 -05001870 if (!nvmeq)
1871 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001872 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001873 if (nvmeq->q_suspended)
1874 goto unlock;
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001875 nvme_process_cq(nvmeq);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001876 nvme_cancel_ios(nvmeq, true);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001877 nvme_resubmit_bios(nvmeq);
Keith Buschedd10d32014-04-03 16:45:23 -06001878 nvme_resubmit_iods(nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001879 unlock:
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001880 spin_unlock_irq(&nvmeq->q_lock);
1881 }
Keith Busch5a92e702014-02-21 14:13:44 -07001882 rcu_read_unlock();
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001883 }
1884 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001885 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001886 }
1887 return 0;
1888}
1889
Keith Busch0e5e4f02012-11-09 16:33:05 -07001890static void nvme_config_discard(struct nvme_ns *ns)
1891{
1892 u32 logical_block_size = queue_logical_block_size(ns->queue);
1893 ns->queue->limits.discard_zeroes_data = 0;
1894 ns->queue->limits.discard_alignment = logical_block_size;
1895 ns->queue->limits.discard_granularity = logical_block_size;
1896 ns->queue->limits.max_discard_sectors = 0xffffffff;
1897 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1898}
1899
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001900static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001901 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1902{
1903 struct nvme_ns *ns;
1904 struct gendisk *disk;
1905 int lbaf;
1906
1907 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1908 return NULL;
1909
1910 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1911 if (!ns)
1912 return NULL;
1913 ns->queue = blk_alloc_queue(GFP_KERNEL);
1914 if (!ns->queue)
1915 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001916 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
1917 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1918 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -06001919 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001920 blk_queue_make_request(ns->queue, nvme_make_request);
1921 ns->dev = dev;
1922 ns->queue->queuedata = ns;
1923
Matthew Wilcox469071a2013-12-09 12:58:46 -05001924 disk = alloc_disk(0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001925 if (!disk)
1926 goto out_free_queue;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001927 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001928 ns->disk = disk;
1929 lbaf = id->flbas & 0xf;
1930 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Buschf410c682013-04-23 17:23:59 -06001931 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busche9ef4632012-07-24 15:01:04 -06001932 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06001933 if (dev->max_hw_sectors)
1934 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Keith Buscha7d2ce22014-04-29 11:41:28 -06001935 if (dev->vwc & NVME_CTRL_VWC_PRESENT)
1936 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001937
1938 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001939 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001940 disk->fops = &nvme_fops;
1941 disk->private_data = ns;
1942 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05001943 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001944 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001945 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001946 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1947
Keith Busch0e5e4f02012-11-09 16:33:05 -07001948 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1949 nvme_config_discard(ns);
1950
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001951 return ns;
1952
1953 out_free_queue:
1954 blk_cleanup_queue(ns->queue);
1955 out_free_ns:
1956 kfree(ns);
1957 return NULL;
1958}
1959
Keith Busch42f61422014-03-24 10:46:25 -06001960static int nvme_find_closest_node(int node)
1961{
1962 int n, val, min_val = INT_MAX, best_node = node;
1963
1964 for_each_online_node(n) {
1965 if (n == node)
1966 continue;
1967 val = node_distance(node, n);
1968 if (val < min_val) {
1969 min_val = val;
1970 best_node = n;
1971 }
1972 }
1973 return best_node;
1974}
1975
1976static void nvme_set_queue_cpus(cpumask_t *qmask, struct nvme_queue *nvmeq,
1977 int count)
1978{
1979 int cpu;
1980 for_each_cpu(cpu, qmask) {
1981 if (cpumask_weight(nvmeq->cpu_mask) >= count)
1982 break;
1983 if (!cpumask_test_and_set_cpu(cpu, nvmeq->cpu_mask))
1984 *per_cpu_ptr(nvmeq->dev->io_queue, cpu) = nvmeq->qid;
1985 }
1986}
1987
1988static void nvme_add_cpus(cpumask_t *mask, const cpumask_t *unassigned_cpus,
1989 const cpumask_t *new_mask, struct nvme_queue *nvmeq, int cpus_per_queue)
1990{
1991 int next_cpu;
1992 for_each_cpu(next_cpu, new_mask) {
1993 cpumask_or(mask, mask, get_cpu_mask(next_cpu));
1994 cpumask_or(mask, mask, topology_thread_cpumask(next_cpu));
1995 cpumask_and(mask, mask, unassigned_cpus);
1996 nvme_set_queue_cpus(mask, nvmeq, cpus_per_queue);
1997 }
1998}
1999
2000static void nvme_create_io_queues(struct nvme_dev *dev)
2001{
2002 unsigned i, max;
2003
2004 max = min(dev->max_qid, num_online_cpus());
2005 for (i = dev->queue_count; i <= max; i++)
2006 if (!nvme_alloc_queue(dev, i, dev->q_depth, i - 1))
2007 break;
2008
2009 max = min(dev->queue_count - 1, num_online_cpus());
2010 for (i = dev->online_queues; i <= max; i++)
2011 if (nvme_create_queue(raw_nvmeq(dev, i), i))
2012 break;
2013}
2014
2015/*
2016 * If there are fewer queues than online cpus, this will try to optimally
2017 * assign a queue to multiple cpus by grouping cpus that are "close" together:
2018 * thread siblings, core, socket, closest node, then whatever else is
2019 * available.
2020 */
2021static void nvme_assign_io_queues(struct nvme_dev *dev)
2022{
2023 unsigned cpu, cpus_per_queue, queues, remainder, i;
2024 cpumask_var_t unassigned_cpus;
2025
2026 nvme_create_io_queues(dev);
2027
2028 queues = min(dev->online_queues - 1, num_online_cpus());
2029 if (!queues)
2030 return;
2031
2032 cpus_per_queue = num_online_cpus() / queues;
2033 remainder = queues - (num_online_cpus() - queues * cpus_per_queue);
2034
2035 if (!alloc_cpumask_var(&unassigned_cpus, GFP_KERNEL))
2036 return;
2037
2038 cpumask_copy(unassigned_cpus, cpu_online_mask);
2039 cpu = cpumask_first(unassigned_cpus);
2040 for (i = 1; i <= queues; i++) {
2041 struct nvme_queue *nvmeq = lock_nvmeq(dev, i);
2042 cpumask_t mask;
2043
2044 cpumask_clear(nvmeq->cpu_mask);
2045 if (!cpumask_weight(unassigned_cpus)) {
2046 unlock_nvmeq(nvmeq);
2047 break;
2048 }
2049
2050 mask = *get_cpu_mask(cpu);
2051 nvme_set_queue_cpus(&mask, nvmeq, cpus_per_queue);
2052 if (cpus_weight(mask) < cpus_per_queue)
2053 nvme_add_cpus(&mask, unassigned_cpus,
2054 topology_thread_cpumask(cpu),
2055 nvmeq, cpus_per_queue);
2056 if (cpus_weight(mask) < cpus_per_queue)
2057 nvme_add_cpus(&mask, unassigned_cpus,
2058 topology_core_cpumask(cpu),
2059 nvmeq, cpus_per_queue);
2060 if (cpus_weight(mask) < cpus_per_queue)
2061 nvme_add_cpus(&mask, unassigned_cpus,
2062 cpumask_of_node(cpu_to_node(cpu)),
2063 nvmeq, cpus_per_queue);
2064 if (cpus_weight(mask) < cpus_per_queue)
2065 nvme_add_cpus(&mask, unassigned_cpus,
2066 cpumask_of_node(
2067 nvme_find_closest_node(
2068 cpu_to_node(cpu))),
2069 nvmeq, cpus_per_queue);
2070 if (cpus_weight(mask) < cpus_per_queue)
2071 nvme_add_cpus(&mask, unassigned_cpus,
2072 unassigned_cpus,
2073 nvmeq, cpus_per_queue);
2074
2075 WARN(cpumask_weight(nvmeq->cpu_mask) != cpus_per_queue,
2076 "nvme%d qid:%d mis-matched queue-to-cpu assignment\n",
2077 dev->instance, i);
2078
2079 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
2080 nvmeq->cpu_mask);
2081 cpumask_andnot(unassigned_cpus, unassigned_cpus,
2082 nvmeq->cpu_mask);
2083 cpu = cpumask_next(cpu, unassigned_cpus);
2084 if (remainder && !--remainder)
2085 cpus_per_queue++;
2086 unlock_nvmeq(nvmeq);
2087 }
2088 WARN(cpumask_weight(unassigned_cpus), "nvme%d unassigned online cpus\n",
2089 dev->instance);
2090 i = 0;
2091 cpumask_andnot(unassigned_cpus, cpu_possible_mask, cpu_online_mask);
2092 for_each_cpu(cpu, unassigned_cpus)
2093 *per_cpu_ptr(dev->io_queue, cpu) = (i++ % queues) + 1;
2094 free_cpumask_var(unassigned_cpus);
2095}
2096
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002097static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002098{
2099 int status;
2100 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002101 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002102
Matthew Wilcoxdf348132012-01-11 07:29:56 -07002103 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002104 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04002105 if (status < 0)
2106 return status;
2107 if (status > 0) {
2108 dev_err(&dev->pci_dev->dev, "Could not set queue count (%d)\n",
2109 status);
2110 return -EBUSY;
2111 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002112 return min(result & 0xffff, result >> 16) + 1;
2113}
2114
Keith Busch9d713c22013-07-15 15:02:24 -06002115static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
2116{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002117 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06002118}
2119
Keith Buschf3db22f2014-06-11 11:51:35 -06002120static void nvme_cpu_workfn(struct work_struct *work)
2121{
2122 struct nvme_dev *dev = container_of(work, struct nvme_dev, cpu_work);
2123 if (dev->initialized)
2124 nvme_assign_io_queues(dev);
2125}
2126
Keith Busch33b1e952014-03-24 10:46:26 -06002127static int nvme_cpu_notify(struct notifier_block *self,
2128 unsigned long action, void *hcpu)
2129{
Keith Buschf3db22f2014-06-11 11:51:35 -06002130 struct nvme_dev *dev;
2131
Keith Busch33b1e952014-03-24 10:46:26 -06002132 switch (action) {
2133 case CPU_ONLINE:
2134 case CPU_DEAD:
Keith Buschf3db22f2014-06-11 11:51:35 -06002135 spin_lock(&dev_list_lock);
2136 list_for_each_entry(dev, &dev_list, node)
2137 schedule_work(&dev->cpu_work);
2138 spin_unlock(&dev_list_lock);
Keith Busch33b1e952014-03-24 10:46:26 -06002139 break;
2140 }
2141 return NOTIFY_OK;
2142}
2143
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002144static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002145{
Keith Busch5a92e702014-02-21 14:13:44 -07002146 struct nvme_queue *adminq = raw_nvmeq(dev, 0);
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002147 struct pci_dev *pdev = dev->pci_dev;
Keith Busch42f61422014-03-24 10:46:25 -06002148 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002149
Keith Busch42f61422014-03-24 10:46:25 -06002150 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002151 result = set_queue_count(dev, nr_io_queues);
Matthew Wilcox1b234842011-01-20 13:01:49 -05002152 if (result < 0)
2153 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002154 if (result < nr_io_queues)
2155 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002156
Keith Busch9d713c22013-07-15 15:02:24 -06002157 size = db_bar_size(dev, nr_io_queues);
2158 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002159 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06002160 do {
2161 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
2162 if (dev->bar)
2163 break;
2164 if (!--nr_io_queues)
2165 return -ENOMEM;
2166 size = db_bar_size(dev, nr_io_queues);
2167 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002168 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07002169 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002170 }
2171
Keith Busch9d713c22013-07-15 15:02:24 -06002172 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002173 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002174
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002175 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002176 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002177 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
2178 if (vecs < 0) {
2179 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
2180 if (vecs < 0) {
2181 vecs = 1;
2182 } else {
2183 for (i = 0; i < vecs; i++)
2184 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002185 }
2186 }
2187
Matthew Wilcox063a8092013-06-20 10:53:48 -04002188 /*
2189 * Should investigate if there's a performance win from allocating
2190 * more queues than interrupt vectors; it might allow the submission
2191 * path to scale better, even if the receive path is limited by the
2192 * number of interrupts.
2193 */
2194 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002195 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002196
Matthew Wilcox3193f072014-01-27 15:57:22 -05002197 result = queue_request_irq(dev, adminq, adminq->irqname);
Keith Busch9d713c22013-07-15 15:02:24 -06002198 if (result) {
Matthew Wilcox3193f072014-01-27 15:57:22 -05002199 adminq->q_suspended = 1;
Keith Busch22404272013-07-15 15:02:20 -06002200 goto free_queues;
Keith Busch9d713c22013-07-15 15:02:24 -06002201 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05002202
Keith Buschcd638942013-07-15 15:02:23 -06002203 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002204 nvme_free_queues(dev, nr_io_queues + 1);
2205 nvme_assign_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06002206
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002207 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002208
Keith Busch22404272013-07-15 15:02:20 -06002209 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002210 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002211 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002212}
2213
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002214/*
2215 * Return: error value if an error occurred setting up the queues or calling
2216 * Identify Device. 0 if these succeeded, even if adding some of the
2217 * namespaces failed. At the moment, these failures are silent. TBD which
2218 * failures should be reported.
2219 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002220static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002221{
Matthew Wilcox68608c262013-06-21 14:36:34 -04002222 struct pci_dev *pdev = dev->pci_dev;
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002223 int res;
2224 unsigned nn, i;
Keith Buschcbb62182013-05-01 13:07:49 -06002225 struct nvme_ns *ns;
Matthew Wilcox51814232011-02-01 16:18:08 -05002226 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002227 struct nvme_id_ns *id_ns;
2228 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002229 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06002230 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002231
Matthew Wilcox68608c262013-06-21 14:36:34 -04002232 mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06002233 if (!mem)
2234 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002235
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002236 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002237 if (res) {
Matthew Wilcox27e81662014-04-11 11:58:45 -04002238 dev_err(&pdev->dev, "Identify Controller failed (%d)\n", res);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002239 res = -EIO;
Keith Buschcbb62182013-05-01 13:07:49 -06002240 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002241 }
2242
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002243 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05002244 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002245 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002246 dev->abort_limit = ctrl->acl + 1;
Keith Buscha7d2ce22014-04-29 11:41:28 -06002247 dev->vwc = ctrl->vwc;
Matthew Wilcox51814232011-02-01 16:18:08 -05002248 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2249 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2250 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002251 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002252 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002253 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
2254 (pdev->device == 0x0953) && ctrl->vs[3])
Keith Busch159b67d2013-04-09 17:13:20 -06002255 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002256
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002257 id_ns = mem;
Matthew Wilcox2b2c1892011-10-07 13:10:13 -04002258 for (i = 1; i <= nn; i++) {
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002259 res = nvme_identify(dev, i, 0, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002260 if (res)
2261 continue;
2262
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002263 if (id_ns->ncap == 0)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002264 continue;
2265
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002266 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
Keith Busch08df1e02012-09-21 10:52:13 -06002267 dma_addr + 4096, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002268 if (res)
Keith Busch12209032013-01-31 14:40:38 -07002269 memset(mem + 4096, 0, 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002270
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002271 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002272 if (ns)
2273 list_add_tail(&ns->list, &dev->namespaces);
2274 }
2275 list_for_each_entry(ns, &dev->namespaces, list)
2276 add_disk(ns->disk);
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002277 res = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002278
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002279 out:
Matthew Wilcox684f5c22011-09-19 17:14:53 -04002280 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002281 return res;
2282}
2283
Keith Busch0877cb02013-07-15 15:02:19 -06002284static int nvme_dev_map(struct nvme_dev *dev)
2285{
Keith Busch42f61422014-03-24 10:46:25 -06002286 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002287 int bars, result = -ENOMEM;
2288 struct pci_dev *pdev = dev->pci_dev;
2289
2290 if (pci_enable_device_mem(pdev))
2291 return result;
2292
2293 dev->entry[0].vector = pdev->irq;
2294 pci_set_master(pdev);
2295 bars = pci_select_bars(pdev, IORESOURCE_MEM);
2296 if (pci_request_selected_regions(pdev, bars, "nvme"))
2297 goto disable_pci;
2298
Russell King052d0ef2013-06-26 23:49:11 +01002299 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
2300 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2301 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002302
Keith Busch0877cb02013-07-15 15:02:19 -06002303 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2304 if (!dev->bar)
2305 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07002306 if (readl(&dev->bar->csts) == -1) {
2307 result = -ENODEV;
2308 goto unmap;
2309 }
Keith Busch42f61422014-03-24 10:46:25 -06002310 cap = readq(&dev->bar->cap);
2311 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2312 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Keith Busch0877cb02013-07-15 15:02:19 -06002313 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2314
2315 return 0;
2316
Keith Busch0e53d182013-12-10 13:10:39 -07002317 unmap:
2318 iounmap(dev->bar);
2319 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002320 disable:
2321 pci_release_regions(pdev);
2322 disable_pci:
2323 pci_disable_device(pdev);
2324 return result;
2325}
2326
2327static void nvme_dev_unmap(struct nvme_dev *dev)
2328{
2329 if (dev->pci_dev->msi_enabled)
2330 pci_disable_msi(dev->pci_dev);
2331 else if (dev->pci_dev->msix_enabled)
2332 pci_disable_msix(dev->pci_dev);
2333
2334 if (dev->bar) {
2335 iounmap(dev->bar);
2336 dev->bar = NULL;
Keith Busch9a6b9452013-12-10 13:10:36 -07002337 pci_release_regions(dev->pci_dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002338 }
2339
Keith Busch0877cb02013-07-15 15:02:19 -06002340 if (pci_is_enabled(dev->pci_dev))
2341 pci_disable_device(dev->pci_dev);
2342}
2343
Keith Busch4d115422013-12-10 13:10:40 -07002344struct nvme_delq_ctx {
2345 struct task_struct *waiter;
2346 struct kthread_worker *worker;
2347 atomic_t refcount;
2348};
2349
2350static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2351{
2352 dq->waiter = current;
2353 mb();
2354
2355 for (;;) {
2356 set_current_state(TASK_KILLABLE);
2357 if (!atomic_read(&dq->refcount))
2358 break;
2359 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2360 fatal_signal_pending(current)) {
2361 set_current_state(TASK_RUNNING);
2362
2363 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
2364 nvme_disable_queue(dev, 0);
2365
2366 send_sig(SIGKILL, dq->worker->task, 1);
2367 flush_kthread_worker(dq->worker);
2368 return;
2369 }
2370 }
2371 set_current_state(TASK_RUNNING);
2372}
2373
2374static void nvme_put_dq(struct nvme_delq_ctx *dq)
2375{
2376 atomic_dec(&dq->refcount);
2377 if (dq->waiter)
2378 wake_up_process(dq->waiter);
2379}
2380
2381static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2382{
2383 atomic_inc(&dq->refcount);
2384 return dq;
2385}
2386
2387static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2388{
2389 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
2390
2391 nvme_clear_queue(nvmeq);
2392 nvme_put_dq(dq);
2393}
2394
2395static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2396 kthread_work_func_t fn)
2397{
2398 struct nvme_command c;
2399
2400 memset(&c, 0, sizeof(c));
2401 c.delete_queue.opcode = opcode;
2402 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2403
2404 init_kthread_work(&nvmeq->cmdinfo.work, fn);
2405 return nvme_submit_admin_cmd_async(nvmeq->dev, &c, &nvmeq->cmdinfo);
2406}
2407
2408static void nvme_del_cq_work_handler(struct kthread_work *work)
2409{
2410 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2411 cmdinfo.work);
2412 nvme_del_queue_end(nvmeq);
2413}
2414
2415static int nvme_delete_cq(struct nvme_queue *nvmeq)
2416{
2417 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2418 nvme_del_cq_work_handler);
2419}
2420
2421static void nvme_del_sq_work_handler(struct kthread_work *work)
2422{
2423 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2424 cmdinfo.work);
2425 int status = nvmeq->cmdinfo.status;
2426
2427 if (!status)
2428 status = nvme_delete_cq(nvmeq);
2429 if (status)
2430 nvme_del_queue_end(nvmeq);
2431}
2432
2433static int nvme_delete_sq(struct nvme_queue *nvmeq)
2434{
2435 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2436 nvme_del_sq_work_handler);
2437}
2438
2439static void nvme_del_queue_start(struct kthread_work *work)
2440{
2441 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2442 cmdinfo.work);
2443 allow_signal(SIGKILL);
2444 if (nvme_delete_sq(nvmeq))
2445 nvme_del_queue_end(nvmeq);
2446}
2447
2448static void nvme_disable_io_queues(struct nvme_dev *dev)
2449{
2450 int i;
2451 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2452 struct nvme_delq_ctx dq;
2453 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2454 &worker, "nvme%d", dev->instance);
2455
2456 if (IS_ERR(kworker_task)) {
2457 dev_err(&dev->pci_dev->dev,
2458 "Failed to create queue del task\n");
2459 for (i = dev->queue_count - 1; i > 0; i--)
2460 nvme_disable_queue(dev, i);
2461 return;
2462 }
2463
2464 dq.waiter = NULL;
2465 atomic_set(&dq.refcount, 0);
2466 dq.worker = &worker;
2467 for (i = dev->queue_count - 1; i > 0; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07002468 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
Keith Busch4d115422013-12-10 13:10:40 -07002469
2470 if (nvme_suspend_queue(nvmeq))
2471 continue;
2472 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2473 nvmeq->cmdinfo.worker = dq.worker;
2474 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2475 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2476 }
2477 nvme_wait_dq(&dq, dev);
2478 kthread_stop(kworker_task);
2479}
2480
Dan McLeranb9afca32014-04-07 17:10:11 -06002481/*
2482* Remove the node from the device list and check
2483* for whether or not we need to stop the nvme_thread.
2484*/
2485static void nvme_dev_list_remove(struct nvme_dev *dev)
2486{
2487 struct task_struct *tmp = NULL;
2488
2489 spin_lock(&dev_list_lock);
2490 list_del_init(&dev->node);
2491 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2492 tmp = nvme_thread;
2493 nvme_thread = NULL;
2494 }
2495 spin_unlock(&dev_list_lock);
2496
2497 if (tmp)
2498 kthread_stop(tmp);
2499}
2500
Keith Buschf0b50732013-07-15 15:02:21 -06002501static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002502{
Keith Busch22404272013-07-15 15:02:20 -06002503 int i;
2504
Keith Buschd4b4ff82013-12-10 13:10:37 -07002505 dev->initialized = 0;
Dan McLeranb9afca32014-04-07 17:10:11 -06002506 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002507
Keith Busch4d115422013-12-10 13:10:40 -07002508 if (!dev->bar || (dev->bar && readl(&dev->bar->csts) == -1)) {
2509 for (i = dev->queue_count - 1; i >= 0; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07002510 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
Keith Busch4d115422013-12-10 13:10:40 -07002511 nvme_suspend_queue(nvmeq);
2512 nvme_clear_queue(nvmeq);
2513 }
2514 } else {
2515 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002516 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002517 nvme_disable_queue(dev, 0);
2518 }
Keith Buschf0b50732013-07-15 15:02:21 -06002519 nvme_dev_unmap(dev);
2520}
2521
2522static void nvme_dev_remove(struct nvme_dev *dev)
2523{
Keith Busch9ac27092014-01-31 16:53:39 -07002524 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002525
Keith Busch9ac27092014-01-31 16:53:39 -07002526 list_for_each_entry(ns, &dev->namespaces, list) {
2527 if (ns->disk->flags & GENHD_FL_UP)
2528 del_gendisk(ns->disk);
2529 if (!blk_queue_dying(ns->queue))
2530 blk_cleanup_queue(ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002531 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002532}
2533
Matthew Wilcox091b6092011-02-10 09:56:01 -05002534static int nvme_setup_prp_pools(struct nvme_dev *dev)
2535{
2536 struct device *dmadev = &dev->pci_dev->dev;
2537 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2538 PAGE_SIZE, PAGE_SIZE, 0);
2539 if (!dev->prp_page_pool)
2540 return -ENOMEM;
2541
Matthew Wilcox99802a72011-02-10 10:30:34 -05002542 /* Optimisation for I/Os between 4k and 128k */
2543 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2544 256, 256, 0);
2545 if (!dev->prp_small_pool) {
2546 dma_pool_destroy(dev->prp_page_pool);
2547 return -ENOMEM;
2548 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002549 return 0;
2550}
2551
2552static void nvme_release_prp_pools(struct nvme_dev *dev)
2553{
2554 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002555 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002556}
2557
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002558static DEFINE_IDA(nvme_instance_ida);
2559
2560static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002561{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002562 int instance, error;
2563
2564 do {
2565 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2566 return -ENODEV;
2567
2568 spin_lock(&dev_list_lock);
2569 error = ida_get_new(&nvme_instance_ida, &instance);
2570 spin_unlock(&dev_list_lock);
2571 } while (error == -EAGAIN);
2572
2573 if (error)
2574 return -ENODEV;
2575
2576 dev->instance = instance;
2577 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002578}
2579
2580static void nvme_release_instance(struct nvme_dev *dev)
2581{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002582 spin_lock(&dev_list_lock);
2583 ida_remove(&nvme_instance_ida, dev->instance);
2584 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002585}
2586
Keith Busch9ac27092014-01-31 16:53:39 -07002587static void nvme_free_namespaces(struct nvme_dev *dev)
2588{
2589 struct nvme_ns *ns, *next;
2590
2591 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2592 list_del(&ns->list);
2593 put_disk(ns->disk);
2594 kfree(ns);
2595 }
2596}
2597
Keith Busch5e82e952013-02-19 10:17:58 -07002598static void nvme_free_dev(struct kref *kref)
2599{
2600 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002601
2602 nvme_free_namespaces(dev);
Keith Busch42f61422014-03-24 10:46:25 -06002603 free_percpu(dev->io_queue);
Keith Busch5e82e952013-02-19 10:17:58 -07002604 kfree(dev->queues);
2605 kfree(dev->entry);
2606 kfree(dev);
2607}
2608
2609static int nvme_dev_open(struct inode *inode, struct file *f)
2610{
2611 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
2612 miscdev);
2613 kref_get(&dev->kref);
2614 f->private_data = dev;
2615 return 0;
2616}
2617
2618static int nvme_dev_release(struct inode *inode, struct file *f)
2619{
2620 struct nvme_dev *dev = f->private_data;
2621 kref_put(&dev->kref, nvme_free_dev);
2622 return 0;
2623}
2624
2625static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2626{
2627 struct nvme_dev *dev = f->private_data;
2628 switch (cmd) {
2629 case NVME_IOCTL_ADMIN_CMD:
2630 return nvme_user_admin_cmd(dev, (void __user *)arg);
2631 default:
2632 return -ENOTTY;
2633 }
2634}
2635
2636static const struct file_operations nvme_dev_fops = {
2637 .owner = THIS_MODULE,
2638 .open = nvme_dev_open,
2639 .release = nvme_dev_release,
2640 .unlocked_ioctl = nvme_dev_ioctl,
2641 .compat_ioctl = nvme_dev_ioctl,
2642};
2643
Keith Buschf0b50732013-07-15 15:02:21 -06002644static int nvme_dev_start(struct nvme_dev *dev)
2645{
2646 int result;
Dan McLeranb9afca32014-04-07 17:10:11 -06002647 bool start_thread = false;
Keith Buschf0b50732013-07-15 15:02:21 -06002648
2649 result = nvme_dev_map(dev);
2650 if (result)
2651 return result;
2652
2653 result = nvme_configure_admin_queue(dev);
2654 if (result)
2655 goto unmap;
2656
2657 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002658 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2659 start_thread = true;
2660 nvme_thread = NULL;
2661 }
Keith Buschf0b50732013-07-15 15:02:21 -06002662 list_add(&dev->node, &dev_list);
2663 spin_unlock(&dev_list_lock);
2664
Dan McLeranb9afca32014-04-07 17:10:11 -06002665 if (start_thread) {
2666 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2667 wake_up(&nvme_kthread_wait);
2668 } else
2669 wait_event_killable(nvme_kthread_wait, nvme_thread);
2670
2671 if (IS_ERR_OR_NULL(nvme_thread)) {
2672 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2673 goto disable;
2674 }
2675
Keith Buschf0b50732013-07-15 15:02:21 -06002676 result = nvme_setup_io_queues(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002677 if (result && result != -EBUSY)
Keith Buschf0b50732013-07-15 15:02:21 -06002678 goto disable;
2679
Keith Buschd82e8bf2013-09-05 14:45:07 -06002680 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002681
2682 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002683 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06002684 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002685 unmap:
2686 nvme_dev_unmap(dev);
2687 return result;
2688}
2689
Keith Busch9a6b9452013-12-10 13:10:36 -07002690static int nvme_remove_dead_ctrl(void *arg)
2691{
2692 struct nvme_dev *dev = (struct nvme_dev *)arg;
2693 struct pci_dev *pdev = dev->pci_dev;
2694
2695 if (pci_get_drvdata(pdev))
2696 pci_stop_and_remove_bus_device(pdev);
2697 kref_put(&dev->kref, nvme_free_dev);
2698 return 0;
2699}
2700
2701static void nvme_remove_disks(struct work_struct *ws)
2702{
Keith Busch9a6b9452013-12-10 13:10:36 -07002703 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2704
2705 nvme_dev_remove(dev);
Keith Busch5a92e702014-02-21 14:13:44 -07002706 nvme_free_queues(dev, 1);
Keith Busch9a6b9452013-12-10 13:10:36 -07002707}
2708
2709static int nvme_dev_resume(struct nvme_dev *dev)
2710{
2711 int ret;
2712
2713 ret = nvme_dev_start(dev);
2714 if (ret && ret != -EBUSY)
2715 return ret;
2716 if (ret == -EBUSY) {
2717 spin_lock(&dev_list_lock);
Tejun Heo9ca97372014-03-07 10:24:49 -05002718 dev->reset_workfn = nvme_remove_disks;
Keith Busch9a6b9452013-12-10 13:10:36 -07002719 queue_work(nvme_workq, &dev->reset_work);
2720 spin_unlock(&dev_list_lock);
2721 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002722 dev->initialized = 1;
Keith Busch9a6b9452013-12-10 13:10:36 -07002723 return 0;
2724}
2725
2726static void nvme_dev_reset(struct nvme_dev *dev)
2727{
2728 nvme_dev_shutdown(dev);
2729 if (nvme_dev_resume(dev)) {
2730 dev_err(&dev->pci_dev->dev, "Device failed to resume\n");
2731 kref_get(&dev->kref);
2732 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2733 dev->instance))) {
2734 dev_err(&dev->pci_dev->dev,
2735 "Failed to start controller remove task\n");
2736 kref_put(&dev->kref, nvme_free_dev);
2737 }
2738 }
2739}
2740
2741static void nvme_reset_failed_dev(struct work_struct *ws)
2742{
2743 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2744 nvme_dev_reset(dev);
2745}
2746
Tejun Heo9ca97372014-03-07 10:24:49 -05002747static void nvme_reset_workfn(struct work_struct *work)
2748{
2749 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
2750 dev->reset_workfn(work);
2751}
2752
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002753static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002754{
Keith Busch0877cb02013-07-15 15:02:19 -06002755 int result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002756 struct nvme_dev *dev;
2757
2758 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2759 if (!dev)
2760 return -ENOMEM;
2761 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
2762 GFP_KERNEL);
2763 if (!dev->entry)
2764 goto free;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002765 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
2766 GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002767 if (!dev->queues)
2768 goto free;
Keith Busch42f61422014-03-24 10:46:25 -06002769 dev->io_queue = alloc_percpu(unsigned short);
2770 if (!dev->io_queue)
2771 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002772
2773 INIT_LIST_HEAD(&dev->namespaces);
Tejun Heo9ca97372014-03-07 10:24:49 -05002774 dev->reset_workfn = nvme_reset_failed_dev;
2775 INIT_WORK(&dev->reset_work, nvme_reset_workfn);
Keith Buschf3db22f2014-06-11 11:51:35 -06002776 INIT_WORK(&dev->cpu_work, nvme_cpu_workfn);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002777 dev->pci_dev = pdev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002778 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002779 result = nvme_set_instance(dev);
2780 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002781 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002782
Matthew Wilcox091b6092011-02-10 09:56:01 -05002783 result = nvme_setup_prp_pools(dev);
2784 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002785 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002786
Keith Buschfb35e912014-03-03 11:09:47 -07002787 kref_init(&dev->kref);
Keith Buschf0b50732013-07-15 15:02:21 -06002788 result = nvme_dev_start(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002789 if (result) {
2790 if (result == -EBUSY)
2791 goto create_cdev;
Keith Busch0877cb02013-07-15 15:02:19 -06002792 goto release_pools;
Keith Buschd82e8bf2013-09-05 14:45:07 -06002793 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002794
Matthew Wilcox740216f2011-02-15 16:28:20 -05002795 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002796 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002797 goto shutdown;
Matthew Wilcox740216f2011-02-15 16:28:20 -05002798
Keith Buschd82e8bf2013-09-05 14:45:07 -06002799 create_cdev:
Keith Busch5e82e952013-02-19 10:17:58 -07002800 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
2801 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2802 dev->miscdev.parent = &pdev->dev;
2803 dev->miscdev.name = dev->name;
2804 dev->miscdev.fops = &nvme_dev_fops;
2805 result = misc_register(&dev->miscdev);
2806 if (result)
2807 goto remove;
2808
Keith Buschd4b4ff82013-12-10 13:10:37 -07002809 dev->initialized = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002810 return 0;
2811
Keith Busch5e82e952013-02-19 10:17:58 -07002812 remove:
2813 nvme_dev_remove(dev);
Keith Busch9ac27092014-01-31 16:53:39 -07002814 nvme_free_namespaces(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002815 shutdown:
2816 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002817 release_pools:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002818 nvme_free_queues(dev, 0);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002819 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002820 release:
2821 nvme_release_instance(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002822 free:
Keith Busch42f61422014-03-24 10:46:25 -06002823 free_percpu(dev->io_queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002824 kfree(dev->queues);
2825 kfree(dev->entry);
2826 kfree(dev);
2827 return result;
2828}
2829
Keith Buschf0d54a52014-05-02 10:40:43 -06002830static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2831{
2832 struct nvme_dev *dev = pci_get_drvdata(pdev);
2833
2834 if (prepare)
2835 nvme_dev_shutdown(dev);
2836 else
2837 nvme_dev_resume(dev);
2838}
2839
Keith Busch09ece142014-01-27 11:29:40 -05002840static void nvme_shutdown(struct pci_dev *pdev)
2841{
2842 struct nvme_dev *dev = pci_get_drvdata(pdev);
2843 nvme_dev_shutdown(dev);
2844}
2845
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002846static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002847{
2848 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002849
2850 spin_lock(&dev_list_lock);
2851 list_del_init(&dev->node);
2852 spin_unlock(&dev_list_lock);
2853
2854 pci_set_drvdata(pdev, NULL);
2855 flush_work(&dev->reset_work);
Keith Buschf3db22f2014-06-11 11:51:35 -06002856 flush_work(&dev->cpu_work);
Keith Busch5e82e952013-02-19 10:17:58 -07002857 misc_deregister(&dev->miscdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002858 nvme_dev_remove(dev);
2859 nvme_dev_shutdown(dev);
Keith Buscha1a5ef92013-12-16 13:50:00 -05002860 nvme_free_queues(dev, 0);
Keith Busch5a92e702014-02-21 14:13:44 -07002861 rcu_barrier();
Keith Busch9a6b9452013-12-10 13:10:36 -07002862 nvme_release_instance(dev);
2863 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002864 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002865}
2866
2867/* These functions are yet to be implemented */
2868#define nvme_error_detected NULL
2869#define nvme_dump_registers NULL
2870#define nvme_link_reset NULL
2871#define nvme_slot_reset NULL
2872#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002873
Jingoo Han671a6012014-02-13 11:19:14 +09002874#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002875static int nvme_suspend(struct device *dev)
2876{
2877 struct pci_dev *pdev = to_pci_dev(dev);
2878 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2879
2880 nvme_dev_shutdown(ndev);
2881 return 0;
2882}
2883
2884static int nvme_resume(struct device *dev)
2885{
2886 struct pci_dev *pdev = to_pci_dev(dev);
2887 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002888
Keith Busch9a6b9452013-12-10 13:10:36 -07002889 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Tejun Heo9ca97372014-03-07 10:24:49 -05002890 ndev->reset_workfn = nvme_reset_failed_dev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002891 queue_work(nvme_workq, &ndev->reset_work);
2892 }
2893 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002894}
Jingoo Han671a6012014-02-13 11:19:14 +09002895#endif
Keith Buschcd638942013-07-15 15:02:23 -06002896
2897static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002898
Stephen Hemminger1d352032012-09-07 09:33:17 -07002899static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002900 .error_detected = nvme_error_detected,
2901 .mmio_enabled = nvme_dump_registers,
2902 .link_reset = nvme_link_reset,
2903 .slot_reset = nvme_slot_reset,
2904 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06002905 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002906};
2907
2908/* Move to pci_ids.h later */
2909#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2910
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002911static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002912 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2913 { 0, }
2914};
2915MODULE_DEVICE_TABLE(pci, nvme_id_table);
2916
2917static struct pci_driver nvme_driver = {
2918 .name = "nvme",
2919 .id_table = nvme_id_table,
2920 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002921 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002922 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002923 .driver = {
2924 .pm = &nvme_dev_pm_ops,
2925 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002926 .err_handler = &nvme_err_handler,
2927};
2928
2929static int __init nvme_init(void)
2930{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002931 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002932
Dan McLeranb9afca32014-04-07 17:10:11 -06002933 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002934
Keith Busch9a6b9452013-12-10 13:10:36 -07002935 nvme_workq = create_singlethread_workqueue("nvme");
2936 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06002937 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07002938
Keith Busch5c42ea12012-07-25 16:05:18 -06002939 result = register_blkdev(nvme_major, "nvme");
2940 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002941 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06002942 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002943 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002944
Keith Buschf3db22f2014-06-11 11:51:35 -06002945 nvme_nb.notifier_call = &nvme_cpu_notify;
2946 result = register_hotcpu_notifier(&nvme_nb);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002947 if (result)
2948 goto unregister_blkdev;
Keith Buschf3db22f2014-06-11 11:51:35 -06002949
2950 result = pci_register_driver(&nvme_driver);
2951 if (result)
2952 goto unregister_hotcpu;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002953 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002954
Keith Buschf3db22f2014-06-11 11:51:35 -06002955 unregister_hotcpu:
2956 unregister_hotcpu_notifier(&nvme_nb);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002957 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002958 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002959 kill_workq:
2960 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002961 return result;
2962}
2963
2964static void __exit nvme_exit(void)
2965{
2966 pci_unregister_driver(&nvme_driver);
Keith Buschf3db22f2014-06-11 11:51:35 -06002967 unregister_hotcpu_notifier(&nvme_nb);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002968 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002969 destroy_workqueue(nvme_workq);
Dan McLeranb9afca32014-04-07 17:10:11 -06002970 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002971 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002972}
2973
2974MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2975MODULE_LICENSE("GPL");
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002976MODULE_VERSION("0.9");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002977module_init(nvme_init);
2978module_exit(nvme_exit);