blob: 2af079e571fc72e8df1d534b6675a2f8ec05f983 [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)
52
53static 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;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050076
Keith Buschd4b4ff82013-12-10 13:10:37 -070077static void nvme_reset_failed_dev(struct work_struct *ws);
78
Keith Busch4d115422013-12-10 13:10:40 -070079struct async_cmd_info {
80 struct kthread_work work;
81 struct kthread_worker *worker;
82 u32 result;
83 int status;
84 void *ctx;
85};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050086
87/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050088 * An NVM Express queue. Each device has at least two (one for admin
89 * commands and one for I/O commands).
90 */
91struct nvme_queue {
Keith Busch5a92e702014-02-21 14:13:44 -070092 struct rcu_head r_head;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050093 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -050094 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -050095 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050096 spinlock_t q_lock;
97 struct nvme_command *sq_cmds;
98 volatile struct nvme_completion *cqes;
99 dma_addr_t sq_dma_addr;
100 dma_addr_t cq_dma_addr;
101 wait_queue_head_t sq_full;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -0500102 wait_queue_t sq_cong_wait;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500103 struct bio_list sq_cong;
Keith Buschedd10d32014-04-03 16:45:23 -0600104 struct list_head iod_bio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500105 u32 __iomem *q_db;
106 u16 q_depth;
107 u16 cq_vector;
108 u16 sq_head;
109 u16 sq_tail;
110 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700111 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400112 u8 cq_phase;
113 u8 cqe_seen;
Keith Busch22404272013-07-15 15:02:20 -0600114 u8 q_suspended;
Keith Busch42f61422014-03-24 10:46:25 -0600115 cpumask_var_t cpu_mask;
Keith Busch4d115422013-12-10 13:10:40 -0700116 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500117 unsigned long cmdid_data[];
118};
119
120/*
121 * Check we didin't inadvertently grow the command struct
122 */
123static inline void _nvme_check_size(void)
124{
125 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
126 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
127 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
128 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
129 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400130 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700131 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500132 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
133 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
134 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
135 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600136 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500137}
138
Keith Buschedd10d32014-04-03 16:45:23 -0600139typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400140 struct nvme_completion *);
141
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500142struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400143 nvme_completion_fn fn;
144 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500145 unsigned long timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700146 int aborted;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500147};
148
149static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
150{
151 return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
152}
153
Keith Busch22404272013-07-15 15:02:20 -0600154static unsigned nvme_queue_extra(int depth)
155{
156 return DIV_ROUND_UP(depth, 8) + (depth * sizeof(struct nvme_cmd_info));
157}
158
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500159/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400160 * alloc_cmdid() - Allocate a Command ID
161 * @nvmeq: The queue that will be used for this command
162 * @ctx: A pointer that will be passed to the handler
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400163 * @handler: The function to call on completion
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500164 *
165 * Allocate a Command ID for a queue. The data passed in will
166 * be passed to the completion handler. This is implemented by using
167 * the bottom two bits of the ctx pointer to store the handler ID.
168 * Passing in a pointer that's not 4-byte aligned will cause a BUG.
169 * We can change this if it becomes a problem.
Matthew Wilcox184d2942011-05-11 21:36:38 -0400170 *
171 * May be called with local interrupts disabled and the q_lock held,
172 * or with interrupts enabled and no locks held.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500173 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400174static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx,
175 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500176{
Matthew Wilcoxe6d15f72011-02-24 08:49:41 -0500177 int depth = nvmeq->q_depth - 1;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500178 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500179 int cmdid;
180
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500181 do {
182 cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
183 if (cmdid >= depth)
184 return -EBUSY;
185 } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
186
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400187 info[cmdid].fn = handler;
188 info[cmdid].ctx = ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500189 info[cmdid].timeout = jiffies + timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700190 info[cmdid].aborted = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500191 return cmdid;
192}
193
194static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400195 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500196{
197 int cmdid;
198 wait_event_killable(nvmeq->sq_full,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500199 (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500200 return (cmdid < 0) ? -EINTR : cmdid;
201}
202
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400203/* Special values must be less than 0x1000 */
204#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500205#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
206#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
207#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Keith Busch53562be2014-04-29 11:41:29 -0600208#define CMD_CTX_ABORT (0x318 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500209
Keith Buschedd10d32014-04-03 16:45:23 -0600210static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400211 struct nvme_completion *cqe)
212{
213 if (ctx == CMD_CTX_CANCELLED)
214 return;
Keith Buschc30341d2013-12-10 13:10:38 -0700215 if (ctx == CMD_CTX_ABORT) {
Keith Buschedd10d32014-04-03 16:45:23 -0600216 ++nvmeq->dev->abort_limit;
Keith Buschc30341d2013-12-10 13:10:38 -0700217 return;
218 }
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400219 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600220 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400221 "completed id %d twice on queue %d\n",
222 cqe->command_id, le16_to_cpup(&cqe->sq_id));
223 return;
224 }
225 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600226 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400227 "invalid id %d completed on queue %d\n",
228 cqe->command_id, le16_to_cpup(&cqe->sq_id));
229 return;
230 }
231
Keith Buschedd10d32014-04-03 16:45:23 -0600232 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400233}
234
Keith Buschedd10d32014-04-03 16:45:23 -0600235static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700236 struct nvme_completion *cqe)
237{
238 struct async_cmd_info *cmdinfo = ctx;
239 cmdinfo->result = le32_to_cpup(&cqe->result);
240 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
241 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
242}
243
Matthew Wilcox184d2942011-05-11 21:36:38 -0400244/*
245 * Called with local interrupts disabled and the q_lock held. May not sleep.
246 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400247static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid,
248 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500249{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400250 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500251 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500252
Keith Busch94bbac42014-04-24 18:53:50 -0600253 if (cmdid >= nvmeq->q_depth || !info[cmdid].fn) {
254 if (fn)
255 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500256 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400257 }
Keith Busch859361a2012-08-02 14:05:59 -0600258 if (fn)
259 *fn = info[cmdid].fn;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400260 ctx = info[cmdid].ctx;
261 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500262 info[cmdid].ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500263 clear_bit(cmdid, nvmeq->cmdid_data);
264 wake_up(&nvmeq->sq_full);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400265 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500266}
267
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400268static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
269 nvme_completion_fn *fn)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500270{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400271 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500272 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400273 if (fn)
274 *fn = info[cmdid].fn;
275 ctx = info[cmdid].ctx;
276 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500277 info[cmdid].ctx = CMD_CTX_CANCELLED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400278 return ctx;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500279}
280
Keith Busch5a92e702014-02-21 14:13:44 -0700281static struct nvme_queue *raw_nvmeq(struct nvme_dev *dev, int qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500282{
Keith Busch5a92e702014-02-21 14:13:44 -0700283 return rcu_dereference_raw(dev->queues[qid]);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500284}
285
Keith Busch4f5099a2014-03-03 16:39:13 -0700286static struct nvme_queue *get_nvmeq(struct nvme_dev *dev) __acquires(RCU)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500287{
Keith Buscha51afb52014-05-13 10:32:46 -0600288 struct nvme_queue *nvmeq;
Keith Busch42f61422014-03-24 10:46:25 -0600289 unsigned queue_id = get_cpu_var(*dev->io_queue);
Keith Buscha51afb52014-05-13 10:32:46 -0600290
Keith Busch5a92e702014-02-21 14:13:44 -0700291 rcu_read_lock();
Keith Buscha51afb52014-05-13 10:32:46 -0600292 nvmeq = rcu_dereference(dev->queues[queue_id]);
293 if (nvmeq)
294 return nvmeq;
295
296 rcu_read_unlock();
297 put_cpu_var(*dev->io_queue);
298 return NULL;
Keith Busch5a92e702014-02-21 14:13:44 -0700299}
300
Keith Busch4f5099a2014-03-03 16:39:13 -0700301static void put_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500302{
Keith Busch5a92e702014-02-21 14:13:44 -0700303 rcu_read_unlock();
Keith Busch42f61422014-03-24 10:46:25 -0600304 put_cpu_var(nvmeq->dev->io_queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500305}
306
Keith Busch4f5099a2014-03-03 16:39:13 -0700307static struct nvme_queue *lock_nvmeq(struct nvme_dev *dev, int q_idx)
308 __acquires(RCU)
309{
Keith Buscha51afb52014-05-13 10:32:46 -0600310 struct nvme_queue *nvmeq;
311
Keith Busch4f5099a2014-03-03 16:39:13 -0700312 rcu_read_lock();
Keith Buscha51afb52014-05-13 10:32:46 -0600313 nvmeq = rcu_dereference(dev->queues[q_idx]);
314 if (nvmeq)
315 return nvmeq;
316
317 rcu_read_unlock();
318 return NULL;
Keith Busch4f5099a2014-03-03 16:39:13 -0700319}
320
321static void unlock_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
322{
323 rcu_read_unlock();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500324}
325
326/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400327 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500328 * @nvmeq: The queue to use
329 * @cmd: The command to send
330 *
331 * Safe to use from interrupt context
332 */
333static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
334{
335 unsigned long flags;
336 u16 tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500337 spin_lock_irqsave(&nvmeq->q_lock, flags);
Keith Busch4f5099a2014-03-03 16:39:13 -0700338 if (nvmeq->q_suspended) {
339 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
340 return -EBUSY;
341 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500342 tail = nvmeq->sq_tail;
343 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500344 if (++tail == nvmeq->q_depth)
345 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500346 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500347 nvmeq->sq_tail = tail;
348 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
349
350 return 0;
351}
352
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500353static __le64 **iod_list(struct nvme_iod *iod)
354{
355 return ((void *)iod) + iod->offset;
356}
357
358/*
359 * Will slightly overestimate the number of pages needed. This is OK
360 * as it only leads to a small amount of wasted memory for the lifetime of
361 * the I/O.
362 */
363static int nvme_npages(unsigned size)
364{
365 unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE);
366 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
367}
368
369static struct nvme_iod *
370nvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp)
371{
372 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
373 sizeof(__le64 *) * nvme_npages(nbytes) +
374 sizeof(struct scatterlist) * nseg, gfp);
375
376 if (iod) {
377 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
378 iod->npages = -1;
379 iod->length = nbytes;
Keith Busch2b196032012-11-06 11:59:23 -0700380 iod->nents = 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600381 iod->first_dma = 0ULL;
Keith Busch61982212013-05-29 15:59:39 -0600382 iod->start_time = jiffies;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500383 }
384
385 return iod;
386}
387
Vishal Verma5d0f6132013-03-04 18:40:58 -0700388void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500389{
390 const int last_prp = PAGE_SIZE / 8 - 1;
391 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500392 __le64 **list = iod_list(iod);
393 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500394
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500395 if (iod->npages == 0)
396 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
397 for (i = 0; i < iod->npages; i++) {
398 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500399 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500400 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500401 prp_dma = next_prp_dma;
402 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500403 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500404}
405
Keith Busch61982212013-05-29 15:59:39 -0600406static void nvme_start_io_acct(struct bio *bio)
407{
408 struct gendisk *disk = bio->bi_bdev->bd_disk;
Sam Bradshawb4e75cb2014-05-09 13:27:07 -0700409 if (blk_queue_io_stat(disk->queue)) {
410 const int rw = bio_data_dir(bio);
411 int cpu = part_stat_lock();
412 part_round_stats(cpu, &disk->part0);
413 part_stat_inc(cpu, &disk->part0, ios[rw]);
414 part_stat_add(cpu, &disk->part0, sectors[rw],
415 bio_sectors(bio));
416 part_inc_in_flight(&disk->part0, rw);
417 part_stat_unlock();
418 }
Keith Busch61982212013-05-29 15:59:39 -0600419}
420
421static void nvme_end_io_acct(struct bio *bio, unsigned long start_time)
422{
423 struct gendisk *disk = bio->bi_bdev->bd_disk;
Sam Bradshawb4e75cb2014-05-09 13:27:07 -0700424 if (blk_queue_io_stat(disk->queue)) {
425 const int rw = bio_data_dir(bio);
426 unsigned long duration = jiffies - start_time;
427 int cpu = part_stat_lock();
428 part_stat_add(cpu, &disk->part0, ticks[rw], duration);
429 part_round_stats(cpu, &disk->part0);
430 part_dec_in_flight(&disk->part0, rw);
431 part_stat_unlock();
432 }
Keith Busch61982212013-05-29 15:59:39 -0600433}
434
Keith Buschedd10d32014-04-03 16:45:23 -0600435static void bio_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500436 struct nvme_completion *cqe)
437{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500438 struct nvme_iod *iod = ctx;
439 struct bio *bio = iod->private;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500440 u16 status = le16_to_cpup(&cqe->status) >> 1;
Keith Busch3291fa52014-04-28 12:30:52 -0600441 int error = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500442
Keith Buschedd10d32014-04-03 16:45:23 -0600443 if (unlikely(status)) {
444 if (!(status & NVME_SC_DNR ||
445 bio->bi_rw & REQ_FAILFAST_MASK) &&
446 (jiffies - iod->start_time) < IOD_TIMEOUT) {
447 if (!waitqueue_active(&nvmeq->sq_full))
448 add_wait_queue(&nvmeq->sq_full,
449 &nvmeq->sq_cong_wait);
450 list_add_tail(&iod->node, &nvmeq->iod_bio);
451 wake_up(&nvmeq->sq_full);
452 return;
453 }
Keith Busch3291fa52014-04-28 12:30:52 -0600454 error = -EIO;
Keith Buschedd10d32014-04-03 16:45:23 -0600455 }
Keith Busch9e59d092013-08-08 10:25:38 -0600456 if (iod->nents) {
Keith Buschedd10d32014-04-03 16:45:23 -0600457 dma_unmap_sg(nvmeq->q_dmadev, iod->sg, iod->nents,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500458 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busch9e59d092013-08-08 10:25:38 -0600459 nvme_end_io_acct(bio, iod->start_time);
460 }
Keith Buschedd10d32014-04-03 16:45:23 -0600461 nvme_free_iod(nvmeq->dev, iod);
Keith Busch3291fa52014-04-28 12:30:52 -0600462
463 trace_block_bio_complete(bdev_get_queue(bio->bi_bdev), bio, error);
464 bio_endio(bio, error);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500465}
466
Matthew Wilcox184d2942011-05-11 21:36:38 -0400467/* length is in bytes. gfp flags indicates whether we may sleep. */
Keith Buschedd10d32014-04-03 16:45:23 -0600468int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod, int total_len,
469 gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500470{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500471 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500472 int length = total_len;
473 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500474 int dma_len = sg_dma_len(sg);
475 u64 dma_addr = sg_dma_address(sg);
476 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500477 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500478 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500479 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500480 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500481
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500482 length -= (PAGE_SIZE - offset);
483 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500484 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500485
486 dma_len -= (PAGE_SIZE - offset);
487 if (dma_len) {
488 dma_addr += (PAGE_SIZE - offset);
489 } else {
490 sg = sg_next(sg);
491 dma_addr = sg_dma_address(sg);
492 dma_len = sg_dma_len(sg);
493 }
494
495 if (length <= PAGE_SIZE) {
Keith Buschedd10d32014-04-03 16:45:23 -0600496 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500497 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500498 }
499
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500500 nprps = DIV_ROUND_UP(length, PAGE_SIZE);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500501 if (nprps <= (256 / 8)) {
502 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500503 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500504 } else {
505 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500506 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500507 }
508
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400509 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
510 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600511 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500512 iod->npages = -1;
513 return (total_len - length) + PAGE_SIZE;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400514 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500515 list[0] = prp_list;
516 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500517 i = 0;
518 for (;;) {
Matthew Wilcox7523d832011-03-16 16:43:40 -0400519 if (i == PAGE_SIZE / 8) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500520 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400521 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500522 if (!prp_list)
523 return total_len - length;
524 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400525 prp_list[0] = old_prp_list[i - 1];
526 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
527 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500528 }
529 prp_list[i++] = cpu_to_le64(dma_addr);
530 dma_len -= PAGE_SIZE;
531 dma_addr += PAGE_SIZE;
532 length -= PAGE_SIZE;
533 if (length <= 0)
534 break;
535 if (dma_len > 0)
536 continue;
537 BUG_ON(dma_len < 0);
538 sg = sg_next(sg);
539 dma_addr = sg_dma_address(sg);
540 dma_len = sg_dma_len(sg);
541 }
542
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500543 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500544}
545
Keith Busch427e9702013-04-09 11:59:32 -0600546static int nvme_split_and_submit(struct bio *bio, struct nvme_queue *nvmeq,
Kent Overstreet20d01892013-11-23 18:21:01 -0800547 int len)
Keith Busch427e9702013-04-09 11:59:32 -0600548{
Kent Overstreet20d01892013-11-23 18:21:01 -0800549 struct bio *split = bio_split(bio, len >> 9, GFP_ATOMIC, NULL);
550 if (!split)
Keith Busch427e9702013-04-09 11:59:32 -0600551 return -ENOMEM;
552
Keith Busch3291fa52014-04-28 12:30:52 -0600553 trace_block_split(bdev_get_queue(bio->bi_bdev), bio,
554 split->bi_iter.bi_sector);
Kent Overstreet20d01892013-11-23 18:21:01 -0800555 bio_chain(split, bio);
556
Keith Buschedd10d32014-04-03 16:45:23 -0600557 if (!waitqueue_active(&nvmeq->sq_full))
Keith Busch427e9702013-04-09 11:59:32 -0600558 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
Kent Overstreet20d01892013-11-23 18:21:01 -0800559 bio_list_add(&nvmeq->sq_cong, split);
560 bio_list_add(&nvmeq->sq_cong, bio);
Keith Buschedd10d32014-04-03 16:45:23 -0600561 wake_up(&nvmeq->sq_full);
Keith Busch427e9702013-04-09 11:59:32 -0600562
563 return 0;
564}
565
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500566/* NVMe scatterlists require no holes in the virtual address */
567#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
568 (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
569
Keith Busch427e9702013-04-09 11:59:32 -0600570static int nvme_map_bio(struct nvme_queue *nvmeq, struct nvme_iod *iod,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500571 struct bio *bio, enum dma_data_direction dma_dir, int psegs)
572{
Kent Overstreet79886132013-11-23 17:19:00 -0800573 struct bio_vec bvec, bvprv;
574 struct bvec_iter iter;
Matthew Wilcox76830842011-02-10 13:55:39 -0500575 struct scatterlist *sg = NULL;
Kent Overstreet79886132013-11-23 17:19:00 -0800576 int length = 0, nsegs = 0, split_len = bio->bi_iter.bi_size;
577 int first = 1;
Keith Busch159b67d2013-04-09 17:13:20 -0600578
579 if (nvmeq->dev->stripe_size)
580 split_len = nvmeq->dev->stripe_size -
Kent Overstreet4f024f32013-10-11 15:44:27 -0700581 ((bio->bi_iter.bi_sector << 9) &
582 (nvmeq->dev->stripe_size - 1));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500583
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500584 sg_init_table(iod->sg, psegs);
Kent Overstreet79886132013-11-23 17:19:00 -0800585 bio_for_each_segment(bvec, bio, iter) {
586 if (!first && BIOVEC_PHYS_MERGEABLE(&bvprv, &bvec)) {
587 sg->length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500588 } else {
Kent Overstreet79886132013-11-23 17:19:00 -0800589 if (!first && BIOVEC_NOT_VIRT_MERGEABLE(&bvprv, &bvec))
590 return nvme_split_and_submit(bio, nvmeq,
Kent Overstreet20d01892013-11-23 18:21:01 -0800591 length);
Keith Busch427e9702013-04-09 11:59:32 -0600592
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500593 sg = sg ? sg + 1 : iod->sg;
Kent Overstreet79886132013-11-23 17:19:00 -0800594 sg_set_page(sg, bvec.bv_page,
595 bvec.bv_len, bvec.bv_offset);
Matthew Wilcox76830842011-02-10 13:55:39 -0500596 nsegs++;
597 }
Keith Busch159b67d2013-04-09 17:13:20 -0600598
Kent Overstreet79886132013-11-23 17:19:00 -0800599 if (split_len - length < bvec.bv_len)
Kent Overstreet20d01892013-11-23 18:21:01 -0800600 return nvme_split_and_submit(bio, nvmeq, split_len);
Kent Overstreet79886132013-11-23 17:19:00 -0800601 length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500602 bvprv = bvec;
Kent Overstreet79886132013-11-23 17:19:00 -0800603 first = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500604 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500605 iod->nents = nsegs;
Matthew Wilcox76830842011-02-10 13:55:39 -0500606 sg_mark_end(sg);
Keith Busch427e9702013-04-09 11:59:32 -0600607 if (dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir) == 0)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500608 return -ENOMEM;
Keith Busch427e9702013-04-09 11:59:32 -0600609
Kent Overstreet4f024f32013-10-11 15:44:27 -0700610 BUG_ON(length != bio->bi_iter.bi_size);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500611 return length;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500612}
613
Keith Busch0e5e4f02012-11-09 16:33:05 -0700614static int nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
615 struct bio *bio, struct nvme_iod *iod, int cmdid)
616{
Keith Buschedd10d32014-04-03 16:45:23 -0600617 struct nvme_dsm_range *range =
618 (struct nvme_dsm_range *)iod_list(iod)[0];
Keith Busch0e5e4f02012-11-09 16:33:05 -0700619 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
620
Keith Busch0e5e4f02012-11-09 16:33:05 -0700621 range->cattr = cpu_to_le32(0);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700622 range->nlb = cpu_to_le32(bio->bi_iter.bi_size >> ns->lba_shift);
623 range->slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700624
625 memset(cmnd, 0, sizeof(*cmnd));
626 cmnd->dsm.opcode = nvme_cmd_dsm;
627 cmnd->dsm.command_id = cmdid;
628 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
629 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
630 cmnd->dsm.nr = 0;
631 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
632
633 if (++nvmeq->sq_tail == nvmeq->q_depth)
634 nvmeq->sq_tail = 0;
635 writel(nvmeq->sq_tail, nvmeq->q_db);
636
637 return 0;
638}
639
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500640static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
641 int cmdid)
642{
643 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
644
645 memset(cmnd, 0, sizeof(*cmnd));
646 cmnd->common.opcode = nvme_cmd_flush;
647 cmnd->common.command_id = cmdid;
648 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
649
650 if (++nvmeq->sq_tail == nvmeq->q_depth)
651 nvmeq->sq_tail = 0;
652 writel(nvmeq->sq_tail, nvmeq->q_db);
653
654 return 0;
655}
656
Keith Buschedd10d32014-04-03 16:45:23 -0600657static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500658{
Keith Buschedd10d32014-04-03 16:45:23 -0600659 struct bio *bio = iod->private;
660 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500661 struct nvme_command *cmnd;
Keith Buschedd10d32014-04-03 16:45:23 -0600662 int cmdid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500663 u16 control;
664 u32 dsmgmt;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500665
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500666 cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500667 if (unlikely(cmdid < 0))
Keith Buschedd10d32014-04-03 16:45:23 -0600668 return cmdid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500669
Keith Buschedd10d32014-04-03 16:45:23 -0600670 if (bio->bi_rw & REQ_DISCARD)
671 return nvme_submit_discard(nvmeq, ns, bio, iod, cmdid);
Keith Busch53562be2014-04-29 11:41:29 -0600672 if (bio->bi_rw & REQ_FLUSH)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500673 return nvme_submit_flush(nvmeq, ns, cmdid);
674
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500675 control = 0;
676 if (bio->bi_rw & REQ_FUA)
677 control |= NVME_RW_FUA;
678 if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
679 control |= NVME_RW_LR;
680
681 dsmgmt = 0;
682 if (bio->bi_rw & REQ_RAHEAD)
683 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
684
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500685 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500686 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500687
Keith Buschedd10d32014-04-03 16:45:23 -0600688 cmnd->rw.opcode = bio_data_dir(bio) ? nvme_cmd_write : nvme_cmd_read;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500689 cmnd->rw.command_id = cmdid;
690 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Keith Buschedd10d32014-04-03 16:45:23 -0600691 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
692 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700693 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Keith Buschedd10d32014-04-03 16:45:23 -0600694 cmnd->rw.length =
695 cpu_to_le16((bio->bi_iter.bi_size >> ns->lba_shift) - 1);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500696 cmnd->rw.control = cpu_to_le16(control);
697 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500698
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500699 if (++nvmeq->sq_tail == nvmeq->q_depth)
700 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500701 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500702
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500703 return 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600704}
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500705
Keith Busch53562be2014-04-29 11:41:29 -0600706static int nvme_split_flush_data(struct nvme_queue *nvmeq, struct bio *bio)
707{
708 struct bio *split = bio_clone(bio, GFP_ATOMIC);
709 if (!split)
710 return -ENOMEM;
711
712 split->bi_iter.bi_size = 0;
713 split->bi_phys_segments = 0;
714 bio->bi_rw &= ~REQ_FLUSH;
715 bio_chain(split, bio);
716
717 if (!waitqueue_active(&nvmeq->sq_full))
718 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
719 bio_list_add(&nvmeq->sq_cong, split);
720 bio_list_add(&nvmeq->sq_cong, bio);
721 wake_up_process(nvme_thread);
722
723 return 0;
724}
725
Keith Buschedd10d32014-04-03 16:45:23 -0600726/*
727 * Called with local interrupts disabled and the q_lock held. May not sleep.
728 */
729static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
730 struct bio *bio)
731{
732 struct nvme_iod *iod;
733 int psegs = bio_phys_segments(ns->queue, bio);
734 int result;
735
Keith Busch53562be2014-04-29 11:41:29 -0600736 if ((bio->bi_rw & REQ_FLUSH) && psegs)
737 return nvme_split_flush_data(nvmeq, bio);
Keith Buschedd10d32014-04-03 16:45:23 -0600738
739 iod = nvme_alloc_iod(psegs, bio->bi_iter.bi_size, GFP_ATOMIC);
740 if (!iod)
741 return -ENOMEM;
742
743 iod->private = bio;
744 if (bio->bi_rw & REQ_DISCARD) {
745 void *range;
746 /*
747 * We reuse the small pool to allocate the 16-byte range here
748 * as it is not worth having a special pool for these or
749 * additional cases to handle freeing the iod.
750 */
751 range = dma_pool_alloc(nvmeq->dev->prp_small_pool,
752 GFP_ATOMIC,
753 &iod->first_dma);
754 if (!range) {
755 result = -ENOMEM;
756 goto free_iod;
757 }
758 iod_list(iod)[0] = (__le64 *)range;
759 iod->npages = 0;
760 } else if (psegs) {
761 result = nvme_map_bio(nvmeq, iod, bio,
762 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE,
763 psegs);
764 if (result <= 0)
765 goto free_iod;
766 if (nvme_setup_prps(nvmeq->dev, iod, result, GFP_ATOMIC) !=
767 result) {
768 result = -ENOMEM;
769 goto free_iod;
770 }
771 nvme_start_io_acct(bio);
772 }
773 if (unlikely(nvme_submit_iod(nvmeq, iod))) {
774 if (!waitqueue_active(&nvmeq->sq_full))
775 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
776 list_add_tail(&iod->node, &nvmeq->iod_bio);
777 }
778 return 0;
779
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500780 free_iod:
781 nvme_free_iod(nvmeq->dev, iod);
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500782 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500783}
784
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400785static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500786{
Matthew Wilcox82123462011-01-20 13:24:06 -0500787 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500788
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500789 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500790 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500791
792 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400793 void *ctx;
794 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500795 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500796 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500797 break;
798 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
799 if (++head == nvmeq->q_depth) {
800 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500801 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500802 }
803
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400804 ctx = free_cmdid(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600805 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500806 }
807
808 /* If the controller ignores the cq head doorbell and continuously
809 * writes to the queue, it is theoretically possible to wrap around
810 * the queue twice and mistakenly return IRQ_NONE. Linux only
811 * requires that 0.1% of your interrupts are handled, so this isn't
812 * a big problem.
813 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500814 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400815 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500816
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800817 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500818 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500819 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500820
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400821 nvmeq->cqe_seen = 1;
822 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500823}
824
Matthew Wilcox7d822452013-06-24 12:03:57 -0400825static void nvme_make_request(struct request_queue *q, struct bio *bio)
826{
827 struct nvme_ns *ns = q->queuedata;
828 struct nvme_queue *nvmeq = get_nvmeq(ns->dev);
829 int result = -EBUSY;
830
Keith Buschcd638942013-07-15 15:02:23 -0600831 if (!nvmeq) {
Keith Buschcd638942013-07-15 15:02:23 -0600832 bio_endio(bio, -EIO);
833 return;
834 }
835
Matthew Wilcox7d822452013-06-24 12:03:57 -0400836 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -0600837 if (!nvmeq->q_suspended && bio_list_empty(&nvmeq->sq_cong))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400838 result = nvme_submit_bio_queue(nvmeq, ns, bio);
839 if (unlikely(result)) {
Keith Buschedd10d32014-04-03 16:45:23 -0600840 if (!waitqueue_active(&nvmeq->sq_full))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400841 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
842 bio_list_add(&nvmeq->sq_cong, bio);
843 }
844
845 nvme_process_cq(nvmeq);
846 spin_unlock_irq(&nvmeq->q_lock);
847 put_nvmeq(nvmeq);
848}
849
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500850static irqreturn_t nvme_irq(int irq, void *data)
851{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500852 irqreturn_t result;
853 struct nvme_queue *nvmeq = data;
854 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400855 nvme_process_cq(nvmeq);
856 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
857 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500858 spin_unlock(&nvmeq->q_lock);
859 return result;
860}
861
862static irqreturn_t nvme_irq_check(int irq, void *data)
863{
864 struct nvme_queue *nvmeq = data;
865 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
866 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
867 return IRQ_NONE;
868 return IRQ_WAKE_THREAD;
869}
870
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500871static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
872{
873 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400874 cancel_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500875 spin_unlock_irq(&nvmeq->q_lock);
876}
877
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400878struct sync_cmd_info {
879 struct task_struct *task;
880 u32 result;
881 int status;
882};
883
Keith Buschedd10d32014-04-03 16:45:23 -0600884static void sync_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400885 struct nvme_completion *cqe)
886{
887 struct sync_cmd_info *cmdinfo = ctx;
888 cmdinfo->result = le32_to_cpup(&cqe->result);
889 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
890 wake_up_process(cmdinfo->task);
891}
892
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500893/*
894 * Returns 0 on success. If the result is negative, it's a Linux error code;
895 * if the result is positive, it's an NVM Express status code
896 */
Keith Busch4f5099a2014-03-03 16:39:13 -0700897static int nvme_submit_sync_cmd(struct nvme_dev *dev, int q_idx,
898 struct nvme_command *cmd,
Vishal Verma5d0f6132013-03-04 18:40:58 -0700899 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500900{
Keith Busch4f5099a2014-03-03 16:39:13 -0700901 int cmdid, ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500902 struct sync_cmd_info cmdinfo;
Keith Busch4f5099a2014-03-03 16:39:13 -0700903 struct nvme_queue *nvmeq;
904
905 nvmeq = lock_nvmeq(dev, q_idx);
Keith Buscha51afb52014-05-13 10:32:46 -0600906 if (!nvmeq)
Keith Busch4f5099a2014-03-03 16:39:13 -0700907 return -ENODEV;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500908
909 cmdinfo.task = current;
910 cmdinfo.status = -EINTR;
911
Keith Busch4f5099a2014-03-03 16:39:13 -0700912 cmdid = alloc_cmdid(nvmeq, &cmdinfo, sync_completion, timeout);
913 if (cmdid < 0) {
914 unlock_nvmeq(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500915 return cmdid;
Keith Busch4f5099a2014-03-03 16:39:13 -0700916 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500917 cmd->common.command_id = cmdid;
918
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500919 set_current_state(TASK_KILLABLE);
Keith Busch4f5099a2014-03-03 16:39:13 -0700920 ret = nvme_submit_cmd(nvmeq, cmd);
921 if (ret) {
922 free_cmdid(nvmeq, cmdid, NULL);
923 unlock_nvmeq(nvmeq);
924 set_current_state(TASK_RUNNING);
925 return ret;
926 }
927 unlock_nvmeq(nvmeq);
Keith Busch78f8d252013-04-19 14:11:06 -0600928 schedule_timeout(timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500929
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500930 if (cmdinfo.status == -EINTR) {
Keith Busch4f5099a2014-03-03 16:39:13 -0700931 nvmeq = lock_nvmeq(dev, q_idx);
Keith Buscha51afb52014-05-13 10:32:46 -0600932 if (nvmeq) {
Keith Busch4f5099a2014-03-03 16:39:13 -0700933 nvme_abort_command(nvmeq, cmdid);
Keith Buscha51afb52014-05-13 10:32:46 -0600934 unlock_nvmeq(nvmeq);
935 }
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500936 return -EINTR;
937 }
938
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500939 if (result)
940 *result = cmdinfo.result;
941
942 return cmdinfo.status;
943}
944
Keith Busch4d115422013-12-10 13:10:40 -0700945static int nvme_submit_async_cmd(struct nvme_queue *nvmeq,
946 struct nvme_command *cmd,
947 struct async_cmd_info *cmdinfo, unsigned timeout)
948{
949 int cmdid;
950
951 cmdid = alloc_cmdid_killable(nvmeq, cmdinfo, async_completion, timeout);
952 if (cmdid < 0)
953 return cmdid;
954 cmdinfo->status = -EINTR;
955 cmd->common.command_id = cmdid;
Keith Busch4f5099a2014-03-03 16:39:13 -0700956 return nvme_submit_cmd(nvmeq, cmd);
Keith Busch4d115422013-12-10 13:10:40 -0700957}
958
Vishal Verma5d0f6132013-03-04 18:40:58 -0700959int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500960 u32 *result)
961{
Keith Busch4f5099a2014-03-03 16:39:13 -0700962 return nvme_submit_sync_cmd(dev, 0, cmd, result, ADMIN_TIMEOUT);
963}
964
965int nvme_submit_io_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
966 u32 *result)
967{
968 return nvme_submit_sync_cmd(dev, smp_processor_id() + 1, cmd, result,
969 NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500970}
971
Keith Busch4d115422013-12-10 13:10:40 -0700972static int nvme_submit_admin_cmd_async(struct nvme_dev *dev,
973 struct nvme_command *cmd, struct async_cmd_info *cmdinfo)
974{
Keith Busch5a92e702014-02-21 14:13:44 -0700975 return nvme_submit_async_cmd(raw_nvmeq(dev, 0), cmd, cmdinfo,
Keith Busch4d115422013-12-10 13:10:40 -0700976 ADMIN_TIMEOUT);
977}
978
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500979static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
980{
981 int status;
982 struct nvme_command c;
983
984 memset(&c, 0, sizeof(c));
985 c.delete_queue.opcode = opcode;
986 c.delete_queue.qid = cpu_to_le16(id);
987
988 status = nvme_submit_admin_cmd(dev, &c, NULL);
989 if (status)
990 return -EIO;
991 return 0;
992}
993
994static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
995 struct nvme_queue *nvmeq)
996{
997 int status;
998 struct nvme_command c;
999 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1000
1001 memset(&c, 0, sizeof(c));
1002 c.create_cq.opcode = nvme_admin_create_cq;
1003 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1004 c.create_cq.cqid = cpu_to_le16(qid);
1005 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1006 c.create_cq.cq_flags = cpu_to_le16(flags);
1007 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1008
1009 status = nvme_submit_admin_cmd(dev, &c, NULL);
1010 if (status)
1011 return -EIO;
1012 return 0;
1013}
1014
1015static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1016 struct nvme_queue *nvmeq)
1017{
1018 int status;
1019 struct nvme_command c;
1020 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1021
1022 memset(&c, 0, sizeof(c));
1023 c.create_sq.opcode = nvme_admin_create_sq;
1024 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1025 c.create_sq.sqid = cpu_to_le16(qid);
1026 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1027 c.create_sq.sq_flags = cpu_to_le16(flags);
1028 c.create_sq.cqid = cpu_to_le16(qid);
1029
1030 status = nvme_submit_admin_cmd(dev, &c, NULL);
1031 if (status)
1032 return -EIO;
1033 return 0;
1034}
1035
1036static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1037{
1038 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1039}
1040
1041static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1042{
1043 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1044}
1045
Vishal Verma5d0f6132013-03-04 18:40:58 -07001046int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001047 dma_addr_t dma_addr)
1048{
1049 struct nvme_command c;
1050
1051 memset(&c, 0, sizeof(c));
1052 c.identify.opcode = nvme_admin_identify;
1053 c.identify.nsid = cpu_to_le32(nsid);
1054 c.identify.prp1 = cpu_to_le64(dma_addr);
1055 c.identify.cns = cpu_to_le32(cns);
1056
1057 return nvme_submit_admin_cmd(dev, &c, NULL);
1058}
1059
Vishal Verma5d0f6132013-03-04 18:40:58 -07001060int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -06001061 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001062{
1063 struct nvme_command c;
1064
1065 memset(&c, 0, sizeof(c));
1066 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -06001067 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001068 c.features.prp1 = cpu_to_le64(dma_addr);
1069 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001070
Keith Busch08df1e02012-09-21 10:52:13 -06001071 return nvme_submit_admin_cmd(dev, &c, result);
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001072}
1073
Vishal Verma5d0f6132013-03-04 18:40:58 -07001074int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
1075 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001076{
1077 struct nvme_command c;
1078
1079 memset(&c, 0, sizeof(c));
1080 c.features.opcode = nvme_admin_set_features;
1081 c.features.prp1 = cpu_to_le64(dma_addr);
1082 c.features.fid = cpu_to_le32(fid);
1083 c.features.dword11 = cpu_to_le32(dword11);
1084
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001085 return nvme_submit_admin_cmd(dev, &c, result);
1086}
1087
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001088/**
Keith Buschc30341d2013-12-10 13:10:38 -07001089 * nvme_abort_cmd - Attempt aborting a command
1090 * @cmdid: Command id of a timed out IO
1091 * @queue: The queue with timed out IO
1092 *
1093 * Schedule controller reset if the command was already aborted once before and
1094 * still hasn't been returned to the driver, or if this is the admin queue.
1095 */
1096static void nvme_abort_cmd(int cmdid, struct nvme_queue *nvmeq)
1097{
1098 int a_cmdid;
1099 struct nvme_command cmd;
1100 struct nvme_dev *dev = nvmeq->dev;
1101 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Keith Busch5a92e702014-02-21 14:13:44 -07001102 struct nvme_queue *adminq;
Keith Buschc30341d2013-12-10 13:10:38 -07001103
1104 if (!nvmeq->qid || info[cmdid].aborted) {
1105 if (work_busy(&dev->reset_work))
1106 return;
1107 list_del_init(&dev->node);
1108 dev_warn(&dev->pci_dev->dev,
1109 "I/O %d QID %d timeout, reset controller\n", cmdid,
1110 nvmeq->qid);
Tejun Heo9ca97372014-03-07 10:24:49 -05001111 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschc30341d2013-12-10 13:10:38 -07001112 queue_work(nvme_workq, &dev->reset_work);
1113 return;
1114 }
1115
1116 if (!dev->abort_limit)
1117 return;
1118
Keith Busch5a92e702014-02-21 14:13:44 -07001119 adminq = rcu_dereference(dev->queues[0]);
1120 a_cmdid = alloc_cmdid(adminq, CMD_CTX_ABORT, special_completion,
Keith Buschc30341d2013-12-10 13:10:38 -07001121 ADMIN_TIMEOUT);
1122 if (a_cmdid < 0)
1123 return;
1124
1125 memset(&cmd, 0, sizeof(cmd));
1126 cmd.abort.opcode = nvme_admin_abort_cmd;
1127 cmd.abort.cid = cmdid;
1128 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
1129 cmd.abort.command_id = a_cmdid;
1130
1131 --dev->abort_limit;
1132 info[cmdid].aborted = 1;
1133 info[cmdid].timeout = jiffies + ADMIN_TIMEOUT;
1134
1135 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", cmdid,
1136 nvmeq->qid);
Keith Busch5a92e702014-02-21 14:13:44 -07001137 nvme_submit_cmd(adminq, &cmd);
Keith Buschc30341d2013-12-10 13:10:38 -07001138}
1139
1140/**
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001141 * nvme_cancel_ios - Cancel outstanding I/Os
1142 * @queue: The queue to cancel I/Os on
1143 * @timeout: True to only cancel I/Os which have timed out
1144 */
1145static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout)
1146{
1147 int depth = nvmeq->q_depth - 1;
1148 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
1149 unsigned long now = jiffies;
1150 int cmdid;
1151
1152 for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
1153 void *ctx;
1154 nvme_completion_fn fn;
1155 static struct nvme_completion cqe = {
Matthew Wilcoxaf2d9ca2013-04-16 15:18:30 -04001156 .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1),
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001157 };
1158
1159 if (timeout && !time_after(now, info[cmdid].timeout))
1160 continue;
Keith Busch053ab7022013-04-30 11:19:38 -06001161 if (info[cmdid].ctx == CMD_CTX_CANCELLED)
1162 continue;
Keith Buschc30341d2013-12-10 13:10:38 -07001163 if (timeout && nvmeq->dev->initialized) {
1164 nvme_abort_cmd(cmdid, nvmeq);
1165 continue;
1166 }
1167 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n", cmdid,
1168 nvmeq->qid);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001169 ctx = cancel_cmdid(nvmeq, cmdid, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -06001170 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001171 }
1172}
1173
Keith Busch5a92e702014-02-21 14:13:44 -07001174static void nvme_free_queue(struct rcu_head *r)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001175{
Keith Busch5a92e702014-02-21 14:13:44 -07001176 struct nvme_queue *nvmeq = container_of(r, struct nvme_queue, r_head);
1177
Keith Busch22404272013-07-15 15:02:20 -06001178 spin_lock_irq(&nvmeq->q_lock);
1179 while (bio_list_peek(&nvmeq->sq_cong)) {
1180 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1181 bio_endio(bio, -EIO);
1182 }
Keith Buschedd10d32014-04-03 16:45:23 -06001183 while (!list_empty(&nvmeq->iod_bio)) {
1184 static struct nvme_completion cqe = {
1185 .status = cpu_to_le16(
1186 (NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1),
1187 };
1188 struct nvme_iod *iod = list_first_entry(&nvmeq->iod_bio,
1189 struct nvme_iod,
1190 node);
1191 list_del(&iod->node);
1192 bio_completion(nvmeq, iod, &cqe);
1193 }
Keith Busch22404272013-07-15 15:02:20 -06001194 spin_unlock_irq(&nvmeq->q_lock);
1195
Matthew Wilcox9e866772012-08-03 13:55:56 -04001196 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1197 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1198 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1199 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
Keith Busch42f61422014-03-24 10:46:25 -06001200 if (nvmeq->qid)
1201 free_cpumask_var(nvmeq->cpu_mask);
Matthew Wilcox9e866772012-08-03 13:55:56 -04001202 kfree(nvmeq);
1203}
1204
Keith Buscha1a5ef92013-12-16 13:50:00 -05001205static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001206{
1207 int i;
1208
Keith Buscha1a5ef92013-12-16 13:50:00 -05001209 for (i = dev->queue_count - 1; i >= lowest; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07001210 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
1211 rcu_assign_pointer(dev->queues[i], NULL);
1212 call_rcu(&nvmeq->r_head, nvme_free_queue);
Keith Busch22404272013-07-15 15:02:20 -06001213 dev->queue_count--;
Keith Busch22404272013-07-15 15:02:20 -06001214 }
1215}
1216
Keith Busch4d115422013-12-10 13:10:40 -07001217/**
1218 * nvme_suspend_queue - put queue into suspended state
1219 * @nvmeq - queue to suspend
1220 *
1221 * Returns 1 if already suspended, 0 otherwise.
1222 */
1223static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001224{
Keith Busch4d115422013-12-10 13:10:40 -07001225 int vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001226
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001227 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001228 if (nvmeq->q_suspended) {
1229 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07001230 return 1;
Keith Busch32958742012-08-20 14:57:49 -06001231 }
Keith Busch22404272013-07-15 15:02:20 -06001232 nvmeq->q_suspended = 1;
Keith Busch42f61422014-03-24 10:46:25 -06001233 nvmeq->dev->online_queues--;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001234 spin_unlock_irq(&nvmeq->q_lock);
1235
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001236 irq_set_affinity_hint(vector, NULL);
1237 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001238
Keith Busch4d115422013-12-10 13:10:40 -07001239 return 0;
1240}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001241
Keith Busch4d115422013-12-10 13:10:40 -07001242static void nvme_clear_queue(struct nvme_queue *nvmeq)
1243{
Keith Busch22404272013-07-15 15:02:20 -06001244 spin_lock_irq(&nvmeq->q_lock);
1245 nvme_process_cq(nvmeq);
1246 nvme_cancel_ios(nvmeq, false);
1247 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001248}
1249
Keith Busch4d115422013-12-10 13:10:40 -07001250static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1251{
Keith Busch5a92e702014-02-21 14:13:44 -07001252 struct nvme_queue *nvmeq = raw_nvmeq(dev, qid);
Keith Busch4d115422013-12-10 13:10:40 -07001253
1254 if (!nvmeq)
1255 return;
1256 if (nvme_suspend_queue(nvmeq))
1257 return;
1258
Keith Busch0e53d182013-12-10 13:10:39 -07001259 /* Don't tell the adapter to delete the admin queue.
1260 * Don't tell a removed adapter to delete IO queues. */
1261 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001262 adapter_delete_sq(dev, qid);
1263 adapter_delete_cq(dev, qid);
1264 }
Keith Busch4d115422013-12-10 13:10:40 -07001265 nvme_clear_queue(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001266}
1267
1268static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
1269 int depth, int vector)
1270{
1271 struct device *dmadev = &dev->pci_dev->dev;
Keith Busch22404272013-07-15 15:02:20 -06001272 unsigned extra = nvme_queue_extra(depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001273 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
1274 if (!nvmeq)
1275 return NULL;
1276
1277 nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
1278 &nvmeq->cq_dma_addr, GFP_KERNEL);
1279 if (!nvmeq->cqes)
1280 goto free_nvmeq;
1281 memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
1282
1283 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
1284 &nvmeq->sq_dma_addr, GFP_KERNEL);
1285 if (!nvmeq->sq_cmds)
1286 goto free_cqdma;
1287
Keith Busch42f61422014-03-24 10:46:25 -06001288 if (qid && !zalloc_cpumask_var(&nvmeq->cpu_mask, GFP_KERNEL))
1289 goto free_sqdma;
1290
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001291 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001292 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001293 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1294 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001295 spin_lock_init(&nvmeq->q_lock);
1296 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001297 nvmeq->cq_phase = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001298 init_waitqueue_head(&nvmeq->sq_full);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001299 init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001300 bio_list_init(&nvmeq->sq_cong);
Keith Buschedd10d32014-04-03 16:45:23 -06001301 INIT_LIST_HEAD(&nvmeq->iod_bio);
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001302 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001303 nvmeq->q_depth = depth;
1304 nvmeq->cq_vector = vector;
Keith Buschc30341d2013-12-10 13:10:38 -07001305 nvmeq->qid = qid;
Keith Busch22404272013-07-15 15:02:20 -06001306 nvmeq->q_suspended = 1;
1307 dev->queue_count++;
Keith Busch5a92e702014-02-21 14:13:44 -07001308 rcu_assign_pointer(dev->queues[qid], nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001309
1310 return nvmeq;
1311
Keith Busch42f61422014-03-24 10:46:25 -06001312 free_sqdma:
1313 dma_free_coherent(dmadev, SQ_SIZE(depth), (void *)nvmeq->sq_cmds,
1314 nvmeq->sq_dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001315 free_cqdma:
Keith Busch68b8eca2013-05-01 13:07:47 -06001316 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001317 nvmeq->cq_dma_addr);
1318 free_nvmeq:
1319 kfree(nvmeq);
1320 return NULL;
1321}
1322
Matthew Wilcox30010822011-01-20 09:10:15 -05001323static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1324 const char *name)
1325{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001326 if (use_threaded_interrupts)
1327 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001328 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001329 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001330 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001331 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001332}
1333
Keith Busch22404272013-07-15 15:02:20 -06001334static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001335{
Keith Busch22404272013-07-15 15:02:20 -06001336 struct nvme_dev *dev = nvmeq->dev;
1337 unsigned extra = nvme_queue_extra(nvmeq->q_depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001338
Keith Busch22404272013-07-15 15:02:20 -06001339 nvmeq->sq_tail = 0;
1340 nvmeq->cq_head = 0;
1341 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001342 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001343 memset(nvmeq->cmdid_data, 0, extra);
1344 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
1345 nvme_cancel_ios(nvmeq, false);
1346 nvmeq->q_suspended = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001347 dev->online_queues++;
Keith Busch22404272013-07-15 15:02:20 -06001348}
1349
1350static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1351{
1352 struct nvme_dev *dev = nvmeq->dev;
1353 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001354
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001355 result = adapter_alloc_cq(dev, qid, nvmeq);
1356 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001357 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001358
1359 result = adapter_alloc_sq(dev, qid, nvmeq);
1360 if (result < 0)
1361 goto release_cq;
1362
Matthew Wilcox3193f072014-01-27 15:57:22 -05001363 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001364 if (result < 0)
1365 goto release_sq;
1366
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001367 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001368 nvme_init_queue(nvmeq, qid);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001369 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001370
1371 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001372
1373 release_sq:
1374 adapter_delete_sq(dev, qid);
1375 release_cq:
1376 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001377 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001378}
1379
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001380static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1381{
1382 unsigned long timeout;
1383 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1384
1385 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1386
1387 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1388 msleep(100);
1389 if (fatal_signal_pending(current))
1390 return -EINTR;
1391 if (time_after(jiffies, timeout)) {
1392 dev_err(&dev->pci_dev->dev,
Matthew Wilcox27e81662014-04-11 11:58:45 -04001393 "Device not ready; aborting %s\n", enabled ?
1394 "initialisation" : "reset");
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001395 return -ENODEV;
1396 }
1397 }
1398
1399 return 0;
1400}
1401
1402/*
1403 * If the device has been passed off to us in an enabled state, just clear
1404 * the enabled bit. The spec says we should set the 'shutdown notification
1405 * bits', but doing so may cause the device to complete commands to the
1406 * admin queue ... and we don't know what memory that might be pointing at!
1407 */
1408static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1409{
Matthew Wilcox44af1462013-05-04 06:43:17 -04001410 u32 cc = readl(&dev->bar->cc);
1411
1412 if (cc & NVME_CC_ENABLE)
1413 writel(cc & ~NVME_CC_ENABLE, &dev->bar->cc);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001414 return nvme_wait_ready(dev, cap, false);
1415}
1416
1417static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1418{
1419 return nvme_wait_ready(dev, cap, true);
1420}
1421
Keith Busch1894d8f2013-07-15 15:02:22 -06001422static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1423{
1424 unsigned long timeout;
1425 u32 cc;
1426
1427 cc = (readl(&dev->bar->cc) & ~NVME_CC_SHN_MASK) | NVME_CC_SHN_NORMAL;
1428 writel(cc, &dev->bar->cc);
1429
1430 timeout = 2 * HZ + jiffies;
1431 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1432 NVME_CSTS_SHST_CMPLT) {
1433 msleep(100);
1434 if (fatal_signal_pending(current))
1435 return -EINTR;
1436 if (time_after(jiffies, timeout)) {
1437 dev_err(&dev->pci_dev->dev,
1438 "Device shutdown incomplete; abort shutdown\n");
1439 return -ENODEV;
1440 }
1441 }
1442
1443 return 0;
1444}
1445
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001446static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001447{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001448 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001449 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001450 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001451 struct nvme_queue *nvmeq;
1452
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001453 result = nvme_disable_ctrl(dev, cap);
1454 if (result < 0)
1455 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001456
Keith Busch5a92e702014-02-21 14:13:44 -07001457 nvmeq = raw_nvmeq(dev, 0);
Keith Buschcd638942013-07-15 15:02:23 -06001458 if (!nvmeq) {
1459 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
1460 if (!nvmeq)
1461 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001462 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001463
1464 aqa = nvmeq->q_depth - 1;
1465 aqa |= aqa << 16;
1466
1467 dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
1468 dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
1469 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001470 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001471
1472 writel(aqa, &dev->bar->aqa);
1473 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1474 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
1475 writel(dev->ctrl_config, &dev->bar->cc);
1476
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001477 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001478 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001479 return result;
Matthew Wilcox9e866772012-08-03 13:55:56 -04001480
Matthew Wilcox3193f072014-01-27 15:57:22 -05001481 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Keith Busch025c5572013-05-01 13:07:51 -06001482 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001483 return result;
Keith Busch025c5572013-05-01 13:07:51 -06001484
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001485 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001486 nvme_init_queue(nvmeq, 0);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001487 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001488 return result;
1489}
1490
Vishal Verma5d0f6132013-03-04 18:40:58 -07001491struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001492 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001493{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001494 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001495 struct scatterlist *sg;
1496 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001497 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001498
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001499 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001500 return ERR_PTR(-EINVAL);
Dan Carpenter5460fc02013-05-13 17:59:50 +03001501 if (!length || length > INT_MAX - PAGE_SIZE)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001502 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001503
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001504 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001505 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1506 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001507 if (!pages)
1508 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001509
1510 err = get_user_pages_fast(addr, count, 1, pages);
1511 if (err < count) {
1512 count = err;
1513 err = -EFAULT;
1514 goto put_pages;
1515 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001516
Santosh Y6808c5f2014-05-29 10:01:52 +05301517 err = -ENOMEM;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001518 iod = nvme_alloc_iod(count, length, GFP_KERNEL);
Santosh Y6808c5f2014-05-29 10:01:52 +05301519 if (!iod)
1520 goto put_pages;
1521
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001522 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001523 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001524 for (i = 0; i < count; i++) {
1525 sg_set_page(&sg[i], pages[i],
Dan Carpenter5460fc02013-05-13 17:59:50 +03001526 min_t(unsigned, length, PAGE_SIZE - offset),
1527 offset);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001528 length -= (PAGE_SIZE - offset);
1529 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001530 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001531 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001532 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001533
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001534 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1535 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001536 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001537 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001538
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001539 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001540 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001541
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001542 free_iod:
1543 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001544 put_pages:
1545 for (i = 0; i < count; i++)
1546 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001547 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001548 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001549}
1550
Vishal Verma5d0f6132013-03-04 18:40:58 -07001551void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001552 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001553{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001554 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001555
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001556 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1557 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001558
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001559 for (i = 0; i < iod->nents; i++)
1560 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001561}
1562
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001563static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1564{
1565 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001566 struct nvme_user_io io;
1567 struct nvme_command c;
Keith Buschf410c682013-04-23 17:23:59 -06001568 unsigned length, meta_len;
1569 int status, i;
1570 struct nvme_iod *iod, *meta_iod = NULL;
1571 dma_addr_t meta_dma_addr;
1572 void *meta, *uninitialized_var(meta_mem);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001573
1574 if (copy_from_user(&io, uio, sizeof(io)))
1575 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001576 length = (io.nblocks + 1) << ns->lba_shift;
Keith Buschf410c682013-04-23 17:23:59 -06001577 meta_len = (io.nblocks + 1) * ns->ms;
1578
1579 if (meta_len && ((io.metadata & 3) || !io.metadata))
1580 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001581
1582 switch (io.opcode) {
1583 case nvme_cmd_write:
1584 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001585 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001586 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001587 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001588 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001589 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001590 }
1591
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001592 if (IS_ERR(iod))
1593 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001594
1595 memset(&c, 0, sizeof(c));
1596 c.rw.opcode = io.opcode;
1597 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001598 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001599 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001600 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001601 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001602 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1603 c.rw.reftag = cpu_to_le32(io.reftag);
1604 c.rw.apptag = cpu_to_le16(io.apptag);
1605 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buschf410c682013-04-23 17:23:59 -06001606
1607 if (meta_len) {
Keith Busch1b567492013-07-18 12:13:51 -06001608 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
1609 meta_len);
Keith Buschf410c682013-04-23 17:23:59 -06001610 if (IS_ERR(meta_iod)) {
1611 status = PTR_ERR(meta_iod);
1612 meta_iod = NULL;
1613 goto unmap;
1614 }
1615
1616 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1617 &meta_dma_addr, GFP_KERNEL);
1618 if (!meta_mem) {
1619 status = -ENOMEM;
1620 goto unmap;
1621 }
1622
1623 if (io.opcode & 1) {
1624 int meta_offset = 0;
1625
1626 for (i = 0; i < meta_iod->nents; i++) {
1627 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1628 meta_iod->sg[i].offset;
1629 memcpy(meta_mem + meta_offset, meta,
1630 meta_iod->sg[i].length);
1631 kunmap_atomic(meta);
1632 meta_offset += meta_iod->sg[i].length;
1633 }
1634 }
1635
1636 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1637 }
1638
Keith Buschedd10d32014-04-03 16:45:23 -06001639 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1640 c.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1641 c.rw.prp2 = cpu_to_le64(iod->first_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001642
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001643 if (length != (io.nblocks + 1) << ns->lba_shift)
1644 status = -ENOMEM;
1645 else
Keith Busch4f5099a2014-03-03 16:39:13 -07001646 status = nvme_submit_io_cmd(dev, &c, NULL);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001647
Keith Buschf410c682013-04-23 17:23:59 -06001648 if (meta_len) {
1649 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1650 int meta_offset = 0;
1651
1652 for (i = 0; i < meta_iod->nents; i++) {
1653 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1654 meta_iod->sg[i].offset;
1655 memcpy(meta, meta_mem + meta_offset,
1656 meta_iod->sg[i].length);
1657 kunmap_atomic(meta);
1658 meta_offset += meta_iod->sg[i].length;
1659 }
1660 }
1661
1662 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1663 meta_dma_addr);
1664 }
1665
1666 unmap:
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001667 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001668 nvme_free_iod(dev, iod);
Keith Buschf410c682013-04-23 17:23:59 -06001669
1670 if (meta_iod) {
1671 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1672 nvme_free_iod(dev, meta_iod);
1673 }
1674
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001675 return status;
1676}
1677
Keith Busch50af8ba2012-07-25 16:07:55 -06001678static int nvme_user_admin_cmd(struct nvme_dev *dev,
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001679 struct nvme_admin_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001680{
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001681 struct nvme_admin_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001682 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001683 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001684 struct nvme_iod *uninitialized_var(iod);
Keith Busch94f370c2013-05-09 14:01:38 -06001685 unsigned timeout;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001686
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001687 if (!capable(CAP_SYS_ADMIN))
1688 return -EACCES;
1689 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001690 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001691
1692 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001693 c.common.opcode = cmd.opcode;
1694 c.common.flags = cmd.flags;
1695 c.common.nsid = cpu_to_le32(cmd.nsid);
1696 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1697 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1698 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1699 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1700 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1701 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1702 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1703 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1704
1705 length = cmd.data_len;
1706 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001707 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1708 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001709 if (IS_ERR(iod))
1710 return PTR_ERR(iod);
Keith Buschedd10d32014-04-03 16:45:23 -06001711 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1712 c.common.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1713 c.common.prp2 = cpu_to_le64(iod->first_dma);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001714 }
1715
Keith Busch94f370c2013-05-09 14:01:38 -06001716 timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
1717 ADMIN_TIMEOUT;
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001718 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001719 status = -ENOMEM;
1720 else
Keith Busch4f5099a2014-03-03 16:39:13 -07001721 status = nvme_submit_sync_cmd(dev, 0, &c, &cmd.result, timeout);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001722
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001723 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001724 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001725 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001726 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001727
Chayan Biswascf90bc42013-05-22 22:34:49 +00001728 if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
Keith Buschf4f117f2012-09-21 10:49:05 -06001729 sizeof(cmd.result)))
1730 status = -EFAULT;
1731
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001732 return status;
1733}
1734
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001735static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1736 unsigned long arg)
1737{
1738 struct nvme_ns *ns = bdev->bd_disk->private_data;
1739
1740 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001741 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001742 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001743 return ns->ns_id;
1744 case NVME_IOCTL_ADMIN_CMD:
Keith Busch50af8ba2012-07-25 16:07:55 -06001745 return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001746 case NVME_IOCTL_SUBMIT_IO:
1747 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001748 case SG_GET_VERSION_NUM:
1749 return nvme_sg_get_version_num((void __user *)arg);
1750 case SG_IO:
1751 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001752 default:
1753 return -ENOTTY;
1754 }
1755}
1756
Keith Busch320a3822013-10-23 13:07:34 -06001757#ifdef CONFIG_COMPAT
1758static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1759 unsigned int cmd, unsigned long arg)
1760{
1761 struct nvme_ns *ns = bdev->bd_disk->private_data;
1762
1763 switch (cmd) {
1764 case SG_IO:
1765 return nvme_sg_io32(ns, arg);
1766 }
1767 return nvme_ioctl(bdev, mode, cmd, arg);
1768}
1769#else
1770#define nvme_compat_ioctl NULL
1771#endif
1772
Keith Busch9ac27092014-01-31 16:53:39 -07001773static int nvme_open(struct block_device *bdev, fmode_t mode)
1774{
1775 struct nvme_ns *ns = bdev->bd_disk->private_data;
1776 struct nvme_dev *dev = ns->dev;
1777
1778 kref_get(&dev->kref);
1779 return 0;
1780}
1781
1782static void nvme_free_dev(struct kref *kref);
1783
1784static void nvme_release(struct gendisk *disk, fmode_t mode)
1785{
1786 struct nvme_ns *ns = disk->private_data;
1787 struct nvme_dev *dev = ns->dev;
1788
1789 kref_put(&dev->kref, nvme_free_dev);
1790}
1791
Keith Busch4cc09e22014-04-02 15:45:37 -06001792static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
1793{
1794 /* some standard values */
1795 geo->heads = 1 << 6;
1796 geo->sectors = 1 << 5;
1797 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1798 return 0;
1799}
1800
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001801static const struct block_device_operations nvme_fops = {
1802 .owner = THIS_MODULE,
1803 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06001804 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07001805 .open = nvme_open,
1806 .release = nvme_release,
Keith Busch4cc09e22014-04-02 15:45:37 -06001807 .getgeo = nvme_getgeo,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001808};
1809
Keith Buschedd10d32014-04-03 16:45:23 -06001810static void nvme_resubmit_iods(struct nvme_queue *nvmeq)
1811{
1812 struct nvme_iod *iod, *next;
1813
1814 list_for_each_entry_safe(iod, next, &nvmeq->iod_bio, node) {
1815 if (unlikely(nvme_submit_iod(nvmeq, iod)))
1816 break;
1817 list_del(&iod->node);
1818 if (bio_list_empty(&nvmeq->sq_cong) &&
1819 list_empty(&nvmeq->iod_bio))
1820 remove_wait_queue(&nvmeq->sq_full,
1821 &nvmeq->sq_cong_wait);
1822 }
1823}
1824
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001825static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1826{
1827 while (bio_list_peek(&nvmeq->sq_cong)) {
1828 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1829 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Keith Busch427e9702013-04-09 11:59:32 -06001830
Keith Buschedd10d32014-04-03 16:45:23 -06001831 if (bio_list_empty(&nvmeq->sq_cong) &&
1832 list_empty(&nvmeq->iod_bio))
Matthew Wilcox3cb967c2011-03-16 16:45:49 -04001833 remove_wait_queue(&nvmeq->sq_full,
1834 &nvmeq->sq_cong_wait);
Keith Busch427e9702013-04-09 11:59:32 -06001835 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
Keith Buschedd10d32014-04-03 16:45:23 -06001836 if (!waitqueue_active(&nvmeq->sq_full))
Keith Busch427e9702013-04-09 11:59:32 -06001837 add_wait_queue(&nvmeq->sq_full,
1838 &nvmeq->sq_cong_wait);
1839 bio_list_add_head(&nvmeq->sq_cong, bio);
1840 break;
1841 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001842 }
1843}
1844
1845static int nvme_kthread(void *data)
1846{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001847 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001848
1849 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001850 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001851 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001852 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001853 int i;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001854 if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
1855 dev->initialized) {
1856 if (work_busy(&dev->reset_work))
1857 continue;
1858 list_del_init(&dev->node);
1859 dev_warn(&dev->pci_dev->dev,
1860 "Failed status, reset controller\n");
Tejun Heo9ca97372014-03-07 10:24:49 -05001861 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001862 queue_work(nvme_workq, &dev->reset_work);
1863 continue;
1864 }
Keith Busch5a92e702014-02-21 14:13:44 -07001865 rcu_read_lock();
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001866 for (i = 0; i < dev->queue_count; i++) {
Keith Busch5a92e702014-02-21 14:13:44 -07001867 struct nvme_queue *nvmeq =
1868 rcu_dereference(dev->queues[i]);
Matthew Wilcox740216f2011-02-15 16:28:20 -05001869 if (!nvmeq)
1870 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001871 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001872 if (nvmeq->q_suspended)
1873 goto unlock;
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001874 nvme_process_cq(nvmeq);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001875 nvme_cancel_ios(nvmeq, true);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001876 nvme_resubmit_bios(nvmeq);
Keith Buschedd10d32014-04-03 16:45:23 -06001877 nvme_resubmit_iods(nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001878 unlock:
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001879 spin_unlock_irq(&nvmeq->q_lock);
1880 }
Keith Busch5a92e702014-02-21 14:13:44 -07001881 rcu_read_unlock();
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001882 }
1883 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001884 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001885 }
1886 return 0;
1887}
1888
Keith Busch0e5e4f02012-11-09 16:33:05 -07001889static void nvme_config_discard(struct nvme_ns *ns)
1890{
1891 u32 logical_block_size = queue_logical_block_size(ns->queue);
1892 ns->queue->limits.discard_zeroes_data = 0;
1893 ns->queue->limits.discard_alignment = logical_block_size;
1894 ns->queue->limits.discard_granularity = logical_block_size;
1895 ns->queue->limits.max_discard_sectors = 0xffffffff;
1896 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1897}
1898
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001899static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001900 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1901{
1902 struct nvme_ns *ns;
1903 struct gendisk *disk;
1904 int lbaf;
1905
1906 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1907 return NULL;
1908
1909 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1910 if (!ns)
1911 return NULL;
1912 ns->queue = blk_alloc_queue(GFP_KERNEL);
1913 if (!ns->queue)
1914 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001915 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
1916 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1917 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001918 blk_queue_make_request(ns->queue, nvme_make_request);
1919 ns->dev = dev;
1920 ns->queue->queuedata = ns;
1921
Matthew Wilcox469071a2013-12-09 12:58:46 -05001922 disk = alloc_disk(0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001923 if (!disk)
1924 goto out_free_queue;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001925 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001926 ns->disk = disk;
1927 lbaf = id->flbas & 0xf;
1928 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Buschf410c682013-04-23 17:23:59 -06001929 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busche9ef4632012-07-24 15:01:04 -06001930 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06001931 if (dev->max_hw_sectors)
1932 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Keith Buscha7d2ce22014-04-29 11:41:28 -06001933 if (dev->vwc & NVME_CTRL_VWC_PRESENT)
1934 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001935
1936 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001937 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001938 disk->fops = &nvme_fops;
1939 disk->private_data = ns;
1940 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05001941 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001942 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001943 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001944 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1945
Keith Busch0e5e4f02012-11-09 16:33:05 -07001946 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1947 nvme_config_discard(ns);
1948
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001949 return ns;
1950
1951 out_free_queue:
1952 blk_cleanup_queue(ns->queue);
1953 out_free_ns:
1954 kfree(ns);
1955 return NULL;
1956}
1957
Keith Busch42f61422014-03-24 10:46:25 -06001958static int nvme_find_closest_node(int node)
1959{
1960 int n, val, min_val = INT_MAX, best_node = node;
1961
1962 for_each_online_node(n) {
1963 if (n == node)
1964 continue;
1965 val = node_distance(node, n);
1966 if (val < min_val) {
1967 min_val = val;
1968 best_node = n;
1969 }
1970 }
1971 return best_node;
1972}
1973
1974static void nvme_set_queue_cpus(cpumask_t *qmask, struct nvme_queue *nvmeq,
1975 int count)
1976{
1977 int cpu;
1978 for_each_cpu(cpu, qmask) {
1979 if (cpumask_weight(nvmeq->cpu_mask) >= count)
1980 break;
1981 if (!cpumask_test_and_set_cpu(cpu, nvmeq->cpu_mask))
1982 *per_cpu_ptr(nvmeq->dev->io_queue, cpu) = nvmeq->qid;
1983 }
1984}
1985
1986static void nvme_add_cpus(cpumask_t *mask, const cpumask_t *unassigned_cpus,
1987 const cpumask_t *new_mask, struct nvme_queue *nvmeq, int cpus_per_queue)
1988{
1989 int next_cpu;
1990 for_each_cpu(next_cpu, new_mask) {
1991 cpumask_or(mask, mask, get_cpu_mask(next_cpu));
1992 cpumask_or(mask, mask, topology_thread_cpumask(next_cpu));
1993 cpumask_and(mask, mask, unassigned_cpus);
1994 nvme_set_queue_cpus(mask, nvmeq, cpus_per_queue);
1995 }
1996}
1997
1998static void nvme_create_io_queues(struct nvme_dev *dev)
1999{
2000 unsigned i, max;
2001
2002 max = min(dev->max_qid, num_online_cpus());
2003 for (i = dev->queue_count; i <= max; i++)
2004 if (!nvme_alloc_queue(dev, i, dev->q_depth, i - 1))
2005 break;
2006
2007 max = min(dev->queue_count - 1, num_online_cpus());
2008 for (i = dev->online_queues; i <= max; i++)
2009 if (nvme_create_queue(raw_nvmeq(dev, i), i))
2010 break;
2011}
2012
2013/*
2014 * If there are fewer queues than online cpus, this will try to optimally
2015 * assign a queue to multiple cpus by grouping cpus that are "close" together:
2016 * thread siblings, core, socket, closest node, then whatever else is
2017 * available.
2018 */
2019static void nvme_assign_io_queues(struct nvme_dev *dev)
2020{
2021 unsigned cpu, cpus_per_queue, queues, remainder, i;
2022 cpumask_var_t unassigned_cpus;
2023
2024 nvme_create_io_queues(dev);
2025
2026 queues = min(dev->online_queues - 1, num_online_cpus());
2027 if (!queues)
2028 return;
2029
2030 cpus_per_queue = num_online_cpus() / queues;
2031 remainder = queues - (num_online_cpus() - queues * cpus_per_queue);
2032
2033 if (!alloc_cpumask_var(&unassigned_cpus, GFP_KERNEL))
2034 return;
2035
2036 cpumask_copy(unassigned_cpus, cpu_online_mask);
2037 cpu = cpumask_first(unassigned_cpus);
2038 for (i = 1; i <= queues; i++) {
2039 struct nvme_queue *nvmeq = lock_nvmeq(dev, i);
2040 cpumask_t mask;
2041
2042 cpumask_clear(nvmeq->cpu_mask);
2043 if (!cpumask_weight(unassigned_cpus)) {
2044 unlock_nvmeq(nvmeq);
2045 break;
2046 }
2047
2048 mask = *get_cpu_mask(cpu);
2049 nvme_set_queue_cpus(&mask, nvmeq, cpus_per_queue);
2050 if (cpus_weight(mask) < cpus_per_queue)
2051 nvme_add_cpus(&mask, unassigned_cpus,
2052 topology_thread_cpumask(cpu),
2053 nvmeq, cpus_per_queue);
2054 if (cpus_weight(mask) < cpus_per_queue)
2055 nvme_add_cpus(&mask, unassigned_cpus,
2056 topology_core_cpumask(cpu),
2057 nvmeq, cpus_per_queue);
2058 if (cpus_weight(mask) < cpus_per_queue)
2059 nvme_add_cpus(&mask, unassigned_cpus,
2060 cpumask_of_node(cpu_to_node(cpu)),
2061 nvmeq, cpus_per_queue);
2062 if (cpus_weight(mask) < cpus_per_queue)
2063 nvme_add_cpus(&mask, unassigned_cpus,
2064 cpumask_of_node(
2065 nvme_find_closest_node(
2066 cpu_to_node(cpu))),
2067 nvmeq, cpus_per_queue);
2068 if (cpus_weight(mask) < cpus_per_queue)
2069 nvme_add_cpus(&mask, unassigned_cpus,
2070 unassigned_cpus,
2071 nvmeq, cpus_per_queue);
2072
2073 WARN(cpumask_weight(nvmeq->cpu_mask) != cpus_per_queue,
2074 "nvme%d qid:%d mis-matched queue-to-cpu assignment\n",
2075 dev->instance, i);
2076
2077 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
2078 nvmeq->cpu_mask);
2079 cpumask_andnot(unassigned_cpus, unassigned_cpus,
2080 nvmeq->cpu_mask);
2081 cpu = cpumask_next(cpu, unassigned_cpus);
2082 if (remainder && !--remainder)
2083 cpus_per_queue++;
2084 unlock_nvmeq(nvmeq);
2085 }
2086 WARN(cpumask_weight(unassigned_cpus), "nvme%d unassigned online cpus\n",
2087 dev->instance);
2088 i = 0;
2089 cpumask_andnot(unassigned_cpus, cpu_possible_mask, cpu_online_mask);
2090 for_each_cpu(cpu, unassigned_cpus)
2091 *per_cpu_ptr(dev->io_queue, cpu) = (i++ % queues) + 1;
2092 free_cpumask_var(unassigned_cpus);
2093}
2094
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002095static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002096{
2097 int status;
2098 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002099 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002100
Matthew Wilcoxdf348132012-01-11 07:29:56 -07002101 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002102 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04002103 if (status < 0)
2104 return status;
2105 if (status > 0) {
2106 dev_err(&dev->pci_dev->dev, "Could not set queue count (%d)\n",
2107 status);
2108 return -EBUSY;
2109 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002110 return min(result & 0xffff, result >> 16) + 1;
2111}
2112
Keith Busch9d713c22013-07-15 15:02:24 -06002113static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
2114{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002115 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06002116}
2117
Keith Busch33b1e952014-03-24 10:46:26 -06002118static int nvme_cpu_notify(struct notifier_block *self,
2119 unsigned long action, void *hcpu)
2120{
2121 struct nvme_dev *dev = container_of(self, struct nvme_dev, nb);
2122 switch (action) {
2123 case CPU_ONLINE:
2124 case CPU_DEAD:
2125 nvme_assign_io_queues(dev);
2126 break;
2127 }
2128 return NOTIFY_OK;
2129}
2130
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002131static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002132{
Keith Busch5a92e702014-02-21 14:13:44 -07002133 struct nvme_queue *adminq = raw_nvmeq(dev, 0);
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002134 struct pci_dev *pdev = dev->pci_dev;
Keith Busch42f61422014-03-24 10:46:25 -06002135 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002136
Keith Busch42f61422014-03-24 10:46:25 -06002137 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002138 result = set_queue_count(dev, nr_io_queues);
Matthew Wilcox1b234842011-01-20 13:01:49 -05002139 if (result < 0)
2140 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002141 if (result < nr_io_queues)
2142 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002143
Keith Busch9d713c22013-07-15 15:02:24 -06002144 size = db_bar_size(dev, nr_io_queues);
2145 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002146 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06002147 do {
2148 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
2149 if (dev->bar)
2150 break;
2151 if (!--nr_io_queues)
2152 return -ENOMEM;
2153 size = db_bar_size(dev, nr_io_queues);
2154 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002155 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07002156 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002157 }
2158
Keith Busch9d713c22013-07-15 15:02:24 -06002159 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002160 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002161
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002162 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002163 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002164 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
2165 if (vecs < 0) {
2166 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
2167 if (vecs < 0) {
2168 vecs = 1;
2169 } else {
2170 for (i = 0; i < vecs; i++)
2171 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002172 }
2173 }
2174
Matthew Wilcox063a8092013-06-20 10:53:48 -04002175 /*
2176 * Should investigate if there's a performance win from allocating
2177 * more queues than interrupt vectors; it might allow the submission
2178 * path to scale better, even if the receive path is limited by the
2179 * number of interrupts.
2180 */
2181 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002182 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002183
Matthew Wilcox3193f072014-01-27 15:57:22 -05002184 result = queue_request_irq(dev, adminq, adminq->irqname);
Keith Busch9d713c22013-07-15 15:02:24 -06002185 if (result) {
Matthew Wilcox3193f072014-01-27 15:57:22 -05002186 adminq->q_suspended = 1;
Keith Busch22404272013-07-15 15:02:20 -06002187 goto free_queues;
Keith Busch9d713c22013-07-15 15:02:24 -06002188 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05002189
Keith Buschcd638942013-07-15 15:02:23 -06002190 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002191 nvme_free_queues(dev, nr_io_queues + 1);
2192 nvme_assign_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06002193
Keith Busch33b1e952014-03-24 10:46:26 -06002194 dev->nb.notifier_call = &nvme_cpu_notify;
2195 result = register_hotcpu_notifier(&dev->nb);
2196 if (result)
2197 goto free_queues;
Keith Busch22404272013-07-15 15:02:20 -06002198
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002199 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002200
Keith Busch22404272013-07-15 15:02:20 -06002201 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002202 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002203 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002204}
2205
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002206/*
2207 * Return: error value if an error occurred setting up the queues or calling
2208 * Identify Device. 0 if these succeeded, even if adding some of the
2209 * namespaces failed. At the moment, these failures are silent. TBD which
2210 * failures should be reported.
2211 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002212static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002213{
Matthew Wilcox68608c262013-06-21 14:36:34 -04002214 struct pci_dev *pdev = dev->pci_dev;
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002215 int res;
2216 unsigned nn, i;
Keith Buschcbb62182013-05-01 13:07:49 -06002217 struct nvme_ns *ns;
Matthew Wilcox51814232011-02-01 16:18:08 -05002218 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002219 struct nvme_id_ns *id_ns;
2220 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002221 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06002222 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002223
Matthew Wilcox68608c262013-06-21 14:36:34 -04002224 mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06002225 if (!mem)
2226 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002227
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002228 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002229 if (res) {
Matthew Wilcox27e81662014-04-11 11:58:45 -04002230 dev_err(&pdev->dev, "Identify Controller failed (%d)\n", res);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002231 res = -EIO;
Keith Buschcbb62182013-05-01 13:07:49 -06002232 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002233 }
2234
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002235 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05002236 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002237 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002238 dev->abort_limit = ctrl->acl + 1;
Keith Buscha7d2ce22014-04-29 11:41:28 -06002239 dev->vwc = ctrl->vwc;
Matthew Wilcox51814232011-02-01 16:18:08 -05002240 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2241 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2242 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002243 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002244 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002245 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
2246 (pdev->device == 0x0953) && ctrl->vs[3])
Keith Busch159b67d2013-04-09 17:13:20 -06002247 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002248
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002249 id_ns = mem;
Matthew Wilcox2b2c1892011-10-07 13:10:13 -04002250 for (i = 1; i <= nn; i++) {
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002251 res = nvme_identify(dev, i, 0, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002252 if (res)
2253 continue;
2254
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002255 if (id_ns->ncap == 0)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002256 continue;
2257
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002258 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
Keith Busch08df1e02012-09-21 10:52:13 -06002259 dma_addr + 4096, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002260 if (res)
Keith Busch12209032013-01-31 14:40:38 -07002261 memset(mem + 4096, 0, 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002262
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002263 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002264 if (ns)
2265 list_add_tail(&ns->list, &dev->namespaces);
2266 }
2267 list_for_each_entry(ns, &dev->namespaces, list)
2268 add_disk(ns->disk);
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002269 res = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002270
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002271 out:
Matthew Wilcox684f5c22011-09-19 17:14:53 -04002272 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002273 return res;
2274}
2275
Keith Busch0877cb02013-07-15 15:02:19 -06002276static int nvme_dev_map(struct nvme_dev *dev)
2277{
Keith Busch42f61422014-03-24 10:46:25 -06002278 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002279 int bars, result = -ENOMEM;
2280 struct pci_dev *pdev = dev->pci_dev;
2281
2282 if (pci_enable_device_mem(pdev))
2283 return result;
2284
2285 dev->entry[0].vector = pdev->irq;
2286 pci_set_master(pdev);
2287 bars = pci_select_bars(pdev, IORESOURCE_MEM);
2288 if (pci_request_selected_regions(pdev, bars, "nvme"))
2289 goto disable_pci;
2290
Russell King052d0ef2013-06-26 23:49:11 +01002291 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
2292 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2293 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002294
Keith Busch0877cb02013-07-15 15:02:19 -06002295 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2296 if (!dev->bar)
2297 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07002298 if (readl(&dev->bar->csts) == -1) {
2299 result = -ENODEV;
2300 goto unmap;
2301 }
Keith Busch42f61422014-03-24 10:46:25 -06002302 cap = readq(&dev->bar->cap);
2303 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2304 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Keith Busch0877cb02013-07-15 15:02:19 -06002305 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2306
2307 return 0;
2308
Keith Busch0e53d182013-12-10 13:10:39 -07002309 unmap:
2310 iounmap(dev->bar);
2311 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002312 disable:
2313 pci_release_regions(pdev);
2314 disable_pci:
2315 pci_disable_device(pdev);
2316 return result;
2317}
2318
2319static void nvme_dev_unmap(struct nvme_dev *dev)
2320{
2321 if (dev->pci_dev->msi_enabled)
2322 pci_disable_msi(dev->pci_dev);
2323 else if (dev->pci_dev->msix_enabled)
2324 pci_disable_msix(dev->pci_dev);
2325
2326 if (dev->bar) {
2327 iounmap(dev->bar);
2328 dev->bar = NULL;
Keith Busch9a6b9452013-12-10 13:10:36 -07002329 pci_release_regions(dev->pci_dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002330 }
2331
Keith Busch0877cb02013-07-15 15:02:19 -06002332 if (pci_is_enabled(dev->pci_dev))
2333 pci_disable_device(dev->pci_dev);
2334}
2335
Keith Busch4d115422013-12-10 13:10:40 -07002336struct nvme_delq_ctx {
2337 struct task_struct *waiter;
2338 struct kthread_worker *worker;
2339 atomic_t refcount;
2340};
2341
2342static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2343{
2344 dq->waiter = current;
2345 mb();
2346
2347 for (;;) {
2348 set_current_state(TASK_KILLABLE);
2349 if (!atomic_read(&dq->refcount))
2350 break;
2351 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2352 fatal_signal_pending(current)) {
2353 set_current_state(TASK_RUNNING);
2354
2355 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
2356 nvme_disable_queue(dev, 0);
2357
2358 send_sig(SIGKILL, dq->worker->task, 1);
2359 flush_kthread_worker(dq->worker);
2360 return;
2361 }
2362 }
2363 set_current_state(TASK_RUNNING);
2364}
2365
2366static void nvme_put_dq(struct nvme_delq_ctx *dq)
2367{
2368 atomic_dec(&dq->refcount);
2369 if (dq->waiter)
2370 wake_up_process(dq->waiter);
2371}
2372
2373static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2374{
2375 atomic_inc(&dq->refcount);
2376 return dq;
2377}
2378
2379static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2380{
2381 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
2382
2383 nvme_clear_queue(nvmeq);
2384 nvme_put_dq(dq);
2385}
2386
2387static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2388 kthread_work_func_t fn)
2389{
2390 struct nvme_command c;
2391
2392 memset(&c, 0, sizeof(c));
2393 c.delete_queue.opcode = opcode;
2394 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2395
2396 init_kthread_work(&nvmeq->cmdinfo.work, fn);
2397 return nvme_submit_admin_cmd_async(nvmeq->dev, &c, &nvmeq->cmdinfo);
2398}
2399
2400static void nvme_del_cq_work_handler(struct kthread_work *work)
2401{
2402 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2403 cmdinfo.work);
2404 nvme_del_queue_end(nvmeq);
2405}
2406
2407static int nvme_delete_cq(struct nvme_queue *nvmeq)
2408{
2409 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2410 nvme_del_cq_work_handler);
2411}
2412
2413static void nvme_del_sq_work_handler(struct kthread_work *work)
2414{
2415 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2416 cmdinfo.work);
2417 int status = nvmeq->cmdinfo.status;
2418
2419 if (!status)
2420 status = nvme_delete_cq(nvmeq);
2421 if (status)
2422 nvme_del_queue_end(nvmeq);
2423}
2424
2425static int nvme_delete_sq(struct nvme_queue *nvmeq)
2426{
2427 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2428 nvme_del_sq_work_handler);
2429}
2430
2431static void nvme_del_queue_start(struct kthread_work *work)
2432{
2433 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2434 cmdinfo.work);
2435 allow_signal(SIGKILL);
2436 if (nvme_delete_sq(nvmeq))
2437 nvme_del_queue_end(nvmeq);
2438}
2439
2440static void nvme_disable_io_queues(struct nvme_dev *dev)
2441{
2442 int i;
2443 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2444 struct nvme_delq_ctx dq;
2445 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2446 &worker, "nvme%d", dev->instance);
2447
2448 if (IS_ERR(kworker_task)) {
2449 dev_err(&dev->pci_dev->dev,
2450 "Failed to create queue del task\n");
2451 for (i = dev->queue_count - 1; i > 0; i--)
2452 nvme_disable_queue(dev, i);
2453 return;
2454 }
2455
2456 dq.waiter = NULL;
2457 atomic_set(&dq.refcount, 0);
2458 dq.worker = &worker;
2459 for (i = dev->queue_count - 1; i > 0; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07002460 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
Keith Busch4d115422013-12-10 13:10:40 -07002461
2462 if (nvme_suspend_queue(nvmeq))
2463 continue;
2464 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2465 nvmeq->cmdinfo.worker = dq.worker;
2466 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2467 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2468 }
2469 nvme_wait_dq(&dq, dev);
2470 kthread_stop(kworker_task);
2471}
2472
Dan McLeranb9afca32014-04-07 17:10:11 -06002473/*
2474* Remove the node from the device list and check
2475* for whether or not we need to stop the nvme_thread.
2476*/
2477static void nvme_dev_list_remove(struct nvme_dev *dev)
2478{
2479 struct task_struct *tmp = NULL;
2480
2481 spin_lock(&dev_list_lock);
2482 list_del_init(&dev->node);
2483 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2484 tmp = nvme_thread;
2485 nvme_thread = NULL;
2486 }
2487 spin_unlock(&dev_list_lock);
2488
2489 if (tmp)
2490 kthread_stop(tmp);
2491}
2492
Keith Buschf0b50732013-07-15 15:02:21 -06002493static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002494{
Keith Busch22404272013-07-15 15:02:20 -06002495 int i;
2496
Keith Buschd4b4ff82013-12-10 13:10:37 -07002497 dev->initialized = 0;
Keith Busch33b1e952014-03-24 10:46:26 -06002498 unregister_hotcpu_notifier(&dev->nb);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002499
Dan McLeranb9afca32014-04-07 17:10:11 -06002500 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002501
Keith Busch4d115422013-12-10 13:10:40 -07002502 if (!dev->bar || (dev->bar && readl(&dev->bar->csts) == -1)) {
2503 for (i = dev->queue_count - 1; i >= 0; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07002504 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
Keith Busch4d115422013-12-10 13:10:40 -07002505 nvme_suspend_queue(nvmeq);
2506 nvme_clear_queue(nvmeq);
2507 }
2508 } else {
2509 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002510 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002511 nvme_disable_queue(dev, 0);
2512 }
Keith Buschf0b50732013-07-15 15:02:21 -06002513 nvme_dev_unmap(dev);
2514}
2515
2516static void nvme_dev_remove(struct nvme_dev *dev)
2517{
Keith Busch9ac27092014-01-31 16:53:39 -07002518 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002519
Keith Busch9ac27092014-01-31 16:53:39 -07002520 list_for_each_entry(ns, &dev->namespaces, list) {
2521 if (ns->disk->flags & GENHD_FL_UP)
2522 del_gendisk(ns->disk);
2523 if (!blk_queue_dying(ns->queue))
2524 blk_cleanup_queue(ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002525 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002526}
2527
Matthew Wilcox091b6092011-02-10 09:56:01 -05002528static int nvme_setup_prp_pools(struct nvme_dev *dev)
2529{
2530 struct device *dmadev = &dev->pci_dev->dev;
2531 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2532 PAGE_SIZE, PAGE_SIZE, 0);
2533 if (!dev->prp_page_pool)
2534 return -ENOMEM;
2535
Matthew Wilcox99802a72011-02-10 10:30:34 -05002536 /* Optimisation for I/Os between 4k and 128k */
2537 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2538 256, 256, 0);
2539 if (!dev->prp_small_pool) {
2540 dma_pool_destroy(dev->prp_page_pool);
2541 return -ENOMEM;
2542 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002543 return 0;
2544}
2545
2546static void nvme_release_prp_pools(struct nvme_dev *dev)
2547{
2548 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002549 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002550}
2551
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002552static DEFINE_IDA(nvme_instance_ida);
2553
2554static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002555{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002556 int instance, error;
2557
2558 do {
2559 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2560 return -ENODEV;
2561
2562 spin_lock(&dev_list_lock);
2563 error = ida_get_new(&nvme_instance_ida, &instance);
2564 spin_unlock(&dev_list_lock);
2565 } while (error == -EAGAIN);
2566
2567 if (error)
2568 return -ENODEV;
2569
2570 dev->instance = instance;
2571 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002572}
2573
2574static void nvme_release_instance(struct nvme_dev *dev)
2575{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002576 spin_lock(&dev_list_lock);
2577 ida_remove(&nvme_instance_ida, dev->instance);
2578 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002579}
2580
Keith Busch9ac27092014-01-31 16:53:39 -07002581static void nvme_free_namespaces(struct nvme_dev *dev)
2582{
2583 struct nvme_ns *ns, *next;
2584
2585 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2586 list_del(&ns->list);
2587 put_disk(ns->disk);
2588 kfree(ns);
2589 }
2590}
2591
Keith Busch5e82e952013-02-19 10:17:58 -07002592static void nvme_free_dev(struct kref *kref)
2593{
2594 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002595
2596 nvme_free_namespaces(dev);
Keith Busch42f61422014-03-24 10:46:25 -06002597 free_percpu(dev->io_queue);
Keith Busch5e82e952013-02-19 10:17:58 -07002598 kfree(dev->queues);
2599 kfree(dev->entry);
2600 kfree(dev);
2601}
2602
2603static int nvme_dev_open(struct inode *inode, struct file *f)
2604{
2605 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
2606 miscdev);
2607 kref_get(&dev->kref);
2608 f->private_data = dev;
2609 return 0;
2610}
2611
2612static int nvme_dev_release(struct inode *inode, struct file *f)
2613{
2614 struct nvme_dev *dev = f->private_data;
2615 kref_put(&dev->kref, nvme_free_dev);
2616 return 0;
2617}
2618
2619static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2620{
2621 struct nvme_dev *dev = f->private_data;
2622 switch (cmd) {
2623 case NVME_IOCTL_ADMIN_CMD:
2624 return nvme_user_admin_cmd(dev, (void __user *)arg);
2625 default:
2626 return -ENOTTY;
2627 }
2628}
2629
2630static const struct file_operations nvme_dev_fops = {
2631 .owner = THIS_MODULE,
2632 .open = nvme_dev_open,
2633 .release = nvme_dev_release,
2634 .unlocked_ioctl = nvme_dev_ioctl,
2635 .compat_ioctl = nvme_dev_ioctl,
2636};
2637
Keith Buschf0b50732013-07-15 15:02:21 -06002638static int nvme_dev_start(struct nvme_dev *dev)
2639{
2640 int result;
Dan McLeranb9afca32014-04-07 17:10:11 -06002641 bool start_thread = false;
Keith Buschf0b50732013-07-15 15:02:21 -06002642
2643 result = nvme_dev_map(dev);
2644 if (result)
2645 return result;
2646
2647 result = nvme_configure_admin_queue(dev);
2648 if (result)
2649 goto unmap;
2650
2651 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002652 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2653 start_thread = true;
2654 nvme_thread = NULL;
2655 }
Keith Buschf0b50732013-07-15 15:02:21 -06002656 list_add(&dev->node, &dev_list);
2657 spin_unlock(&dev_list_lock);
2658
Dan McLeranb9afca32014-04-07 17:10:11 -06002659 if (start_thread) {
2660 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2661 wake_up(&nvme_kthread_wait);
2662 } else
2663 wait_event_killable(nvme_kthread_wait, nvme_thread);
2664
2665 if (IS_ERR_OR_NULL(nvme_thread)) {
2666 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2667 goto disable;
2668 }
2669
Keith Buschf0b50732013-07-15 15:02:21 -06002670 result = nvme_setup_io_queues(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002671 if (result && result != -EBUSY)
Keith Buschf0b50732013-07-15 15:02:21 -06002672 goto disable;
2673
Keith Buschd82e8bf2013-09-05 14:45:07 -06002674 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002675
2676 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002677 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06002678 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002679 unmap:
2680 nvme_dev_unmap(dev);
2681 return result;
2682}
2683
Keith Busch9a6b9452013-12-10 13:10:36 -07002684static int nvme_remove_dead_ctrl(void *arg)
2685{
2686 struct nvme_dev *dev = (struct nvme_dev *)arg;
2687 struct pci_dev *pdev = dev->pci_dev;
2688
2689 if (pci_get_drvdata(pdev))
2690 pci_stop_and_remove_bus_device(pdev);
2691 kref_put(&dev->kref, nvme_free_dev);
2692 return 0;
2693}
2694
2695static void nvme_remove_disks(struct work_struct *ws)
2696{
Keith Busch9a6b9452013-12-10 13:10:36 -07002697 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2698
2699 nvme_dev_remove(dev);
Keith Busch5a92e702014-02-21 14:13:44 -07002700 nvme_free_queues(dev, 1);
Keith Busch9a6b9452013-12-10 13:10:36 -07002701}
2702
2703static int nvme_dev_resume(struct nvme_dev *dev)
2704{
2705 int ret;
2706
2707 ret = nvme_dev_start(dev);
2708 if (ret && ret != -EBUSY)
2709 return ret;
2710 if (ret == -EBUSY) {
2711 spin_lock(&dev_list_lock);
Tejun Heo9ca97372014-03-07 10:24:49 -05002712 dev->reset_workfn = nvme_remove_disks;
Keith Busch9a6b9452013-12-10 13:10:36 -07002713 queue_work(nvme_workq, &dev->reset_work);
2714 spin_unlock(&dev_list_lock);
2715 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002716 dev->initialized = 1;
Keith Busch9a6b9452013-12-10 13:10:36 -07002717 return 0;
2718}
2719
2720static void nvme_dev_reset(struct nvme_dev *dev)
2721{
2722 nvme_dev_shutdown(dev);
2723 if (nvme_dev_resume(dev)) {
2724 dev_err(&dev->pci_dev->dev, "Device failed to resume\n");
2725 kref_get(&dev->kref);
2726 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2727 dev->instance))) {
2728 dev_err(&dev->pci_dev->dev,
2729 "Failed to start controller remove task\n");
2730 kref_put(&dev->kref, nvme_free_dev);
2731 }
2732 }
2733}
2734
2735static void nvme_reset_failed_dev(struct work_struct *ws)
2736{
2737 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2738 nvme_dev_reset(dev);
2739}
2740
Tejun Heo9ca97372014-03-07 10:24:49 -05002741static void nvme_reset_workfn(struct work_struct *work)
2742{
2743 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
2744 dev->reset_workfn(work);
2745}
2746
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002747static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002748{
Keith Busch0877cb02013-07-15 15:02:19 -06002749 int result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002750 struct nvme_dev *dev;
2751
2752 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2753 if (!dev)
2754 return -ENOMEM;
2755 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
2756 GFP_KERNEL);
2757 if (!dev->entry)
2758 goto free;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002759 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
2760 GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002761 if (!dev->queues)
2762 goto free;
Keith Busch42f61422014-03-24 10:46:25 -06002763 dev->io_queue = alloc_percpu(unsigned short);
2764 if (!dev->io_queue)
2765 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002766
2767 INIT_LIST_HEAD(&dev->namespaces);
Tejun Heo9ca97372014-03-07 10:24:49 -05002768 dev->reset_workfn = nvme_reset_failed_dev;
2769 INIT_WORK(&dev->reset_work, nvme_reset_workfn);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002770 dev->pci_dev = pdev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002771 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002772 result = nvme_set_instance(dev);
2773 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002774 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002775
Matthew Wilcox091b6092011-02-10 09:56:01 -05002776 result = nvme_setup_prp_pools(dev);
2777 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002778 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002779
Keith Buschfb35e912014-03-03 11:09:47 -07002780 kref_init(&dev->kref);
Keith Buschf0b50732013-07-15 15:02:21 -06002781 result = nvme_dev_start(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002782 if (result) {
2783 if (result == -EBUSY)
2784 goto create_cdev;
Keith Busch0877cb02013-07-15 15:02:19 -06002785 goto release_pools;
Keith Buschd82e8bf2013-09-05 14:45:07 -06002786 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002787
Matthew Wilcox740216f2011-02-15 16:28:20 -05002788 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002789 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002790 goto shutdown;
Matthew Wilcox740216f2011-02-15 16:28:20 -05002791
Keith Buschd82e8bf2013-09-05 14:45:07 -06002792 create_cdev:
Keith Busch5e82e952013-02-19 10:17:58 -07002793 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
2794 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2795 dev->miscdev.parent = &pdev->dev;
2796 dev->miscdev.name = dev->name;
2797 dev->miscdev.fops = &nvme_dev_fops;
2798 result = misc_register(&dev->miscdev);
2799 if (result)
2800 goto remove;
2801
Keith Buschd4b4ff82013-12-10 13:10:37 -07002802 dev->initialized = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002803 return 0;
2804
Keith Busch5e82e952013-02-19 10:17:58 -07002805 remove:
2806 nvme_dev_remove(dev);
Keith Busch9ac27092014-01-31 16:53:39 -07002807 nvme_free_namespaces(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002808 shutdown:
2809 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002810 release_pools:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002811 nvme_free_queues(dev, 0);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002812 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002813 release:
2814 nvme_release_instance(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002815 free:
Keith Busch42f61422014-03-24 10:46:25 -06002816 free_percpu(dev->io_queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002817 kfree(dev->queues);
2818 kfree(dev->entry);
2819 kfree(dev);
2820 return result;
2821}
2822
Keith Busch09ece142014-01-27 11:29:40 -05002823static void nvme_shutdown(struct pci_dev *pdev)
2824{
2825 struct nvme_dev *dev = pci_get_drvdata(pdev);
2826 nvme_dev_shutdown(dev);
2827}
2828
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002829static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002830{
2831 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002832
2833 spin_lock(&dev_list_lock);
2834 list_del_init(&dev->node);
2835 spin_unlock(&dev_list_lock);
2836
2837 pci_set_drvdata(pdev, NULL);
2838 flush_work(&dev->reset_work);
Keith Busch5e82e952013-02-19 10:17:58 -07002839 misc_deregister(&dev->miscdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002840 nvme_dev_remove(dev);
2841 nvme_dev_shutdown(dev);
Keith Buscha1a5ef92013-12-16 13:50:00 -05002842 nvme_free_queues(dev, 0);
Keith Busch5a92e702014-02-21 14:13:44 -07002843 rcu_barrier();
Keith Busch9a6b9452013-12-10 13:10:36 -07002844 nvme_release_instance(dev);
2845 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002846 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002847}
2848
2849/* These functions are yet to be implemented */
2850#define nvme_error_detected NULL
2851#define nvme_dump_registers NULL
2852#define nvme_link_reset NULL
2853#define nvme_slot_reset NULL
2854#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002855
Jingoo Han671a6012014-02-13 11:19:14 +09002856#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002857static int nvme_suspend(struct device *dev)
2858{
2859 struct pci_dev *pdev = to_pci_dev(dev);
2860 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2861
2862 nvme_dev_shutdown(ndev);
2863 return 0;
2864}
2865
2866static int nvme_resume(struct device *dev)
2867{
2868 struct pci_dev *pdev = to_pci_dev(dev);
2869 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002870
Keith Busch9a6b9452013-12-10 13:10:36 -07002871 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Tejun Heo9ca97372014-03-07 10:24:49 -05002872 ndev->reset_workfn = nvme_reset_failed_dev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002873 queue_work(nvme_workq, &ndev->reset_work);
2874 }
2875 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002876}
Jingoo Han671a6012014-02-13 11:19:14 +09002877#endif
Keith Buschcd638942013-07-15 15:02:23 -06002878
2879static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002880
Stephen Hemminger1d352032012-09-07 09:33:17 -07002881static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002882 .error_detected = nvme_error_detected,
2883 .mmio_enabled = nvme_dump_registers,
2884 .link_reset = nvme_link_reset,
2885 .slot_reset = nvme_slot_reset,
2886 .resume = nvme_error_resume,
2887};
2888
2889/* Move to pci_ids.h later */
2890#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2891
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002892static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002893 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2894 { 0, }
2895};
2896MODULE_DEVICE_TABLE(pci, nvme_id_table);
2897
2898static struct pci_driver nvme_driver = {
2899 .name = "nvme",
2900 .id_table = nvme_id_table,
2901 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002902 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002903 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002904 .driver = {
2905 .pm = &nvme_dev_pm_ops,
2906 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002907 .err_handler = &nvme_err_handler,
2908};
2909
2910static int __init nvme_init(void)
2911{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002912 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002913
Dan McLeranb9afca32014-04-07 17:10:11 -06002914 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002915
Keith Busch9a6b9452013-12-10 13:10:36 -07002916 nvme_workq = create_singlethread_workqueue("nvme");
2917 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06002918 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07002919
Keith Busch5c42ea12012-07-25 16:05:18 -06002920 result = register_blkdev(nvme_major, "nvme");
2921 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002922 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06002923 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002924 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002925
2926 result = pci_register_driver(&nvme_driver);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002927 if (result)
2928 goto unregister_blkdev;
2929 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002930
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002931 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002932 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002933 kill_workq:
2934 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002935 return result;
2936}
2937
2938static void __exit nvme_exit(void)
2939{
2940 pci_unregister_driver(&nvme_driver);
2941 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002942 destroy_workqueue(nvme_workq);
Dan McLeranb9afca32014-04-07 17:10:11 -06002943 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002944 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002945}
2946
2947MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2948MODULE_LICENSE("GPL");
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002949MODULE_VERSION("0.9");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002950module_init(nvme_init);
2951module_exit(nvme_exit);