blob: 48a712734b5591e47f9d26af333102afa8da2126 [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003 * Copyright (c) 2011-2014, Intel Corporation.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050013 */
14
15#include <linux/nvme.h>
16#include <linux/bio.h>
Matthew Wilcox8de05532011-05-12 13:50:28 -040017#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050018#include <linux/blkdev.h>
Keith Busch42f61422014-03-24 10:46:25 -060019#include <linux/cpu.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040020#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050021#include <linux/errno.h>
22#include <linux/fs.h>
23#include <linux/genhd.h>
Keith Busch4cc09e22014-04-02 15:45:37 -060024#include <linux/hdreg.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040025#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050026#include <linux/init.h>
27#include <linux/interrupt.h>
28#include <linux/io.h>
29#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050030#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050031#include <linux/kernel.h>
32#include <linux/mm.h>
33#include <linux/module.h>
34#include <linux/moduleparam.h>
35#include <linux/pci.h>
Keith Busch42f61422014-03-24 10:46:25 -060036#include <linux/percpu.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050037#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040038#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050039#include <linux/sched.h>
40#include <linux/slab.h>
41#include <linux/types.h>
Vishal Verma5d0f6132013-03-04 18:40:58 -070042#include <scsi/sg.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090043#include <asm-generic/io-64-nonatomic-lo-hi.h>
44
Keith Busch3291fa52014-04-28 12:30:52 -060045#include <trace/events/block.h>
46
Keith Busch9d43cf62014-05-13 11:42:02 -060047#define NVME_Q_DEPTH 1024
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050048#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
49#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Keith Busch9d43cf62014-05-13 11:42:02 -060050#define ADMIN_TIMEOUT (admin_timeout * HZ)
51#define IOD_TIMEOUT (retry_time * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050052
Keith Busch9d43cf62014-05-13 11:42:02 -060053static unsigned char admin_timeout = 60;
54module_param(admin_timeout, byte, 0644);
55MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050056
Matthew Wilcoxbd676082014-06-03 23:04:30 -040057unsigned char nvme_io_timeout = 30;
58module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
Keith Buschb3550842014-04-04 11:43:36 -060059MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050060
Keith Busch61e4ce02014-05-13 11:42:01 -060061static unsigned char retry_time = 30;
62module_param(retry_time, byte, 0644);
63MODULE_PARM_DESC(retry_time, "time in seconds to retry failed I/O");
64
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050065static int nvme_major;
66module_param(nvme_major, int, 0);
67
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050068static int use_threaded_interrupts;
69module_param(use_threaded_interrupts, int, 0);
70
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050071static DEFINE_SPINLOCK(dev_list_lock);
72static LIST_HEAD(dev_list);
73static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070074static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060075static wait_queue_head_t nvme_kthread_wait;
Keith Buschf3db22f2014-06-11 11:51:35 -060076static struct notifier_block nvme_nb;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050077
Keith Buschd4b4ff82013-12-10 13:10:37 -070078static void nvme_reset_failed_dev(struct work_struct *ws);
79
Keith Busch4d115422013-12-10 13:10:40 -070080struct async_cmd_info {
81 struct kthread_work work;
82 struct kthread_worker *worker;
83 u32 result;
84 int status;
85 void *ctx;
86};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050087
88/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050089 * An NVM Express queue. Each device has at least two (one for admin
90 * commands and one for I/O commands).
91 */
92struct nvme_queue {
Keith Busch5a92e702014-02-21 14:13:44 -070093 struct rcu_head r_head;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050094 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -050095 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -050096 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050097 spinlock_t q_lock;
98 struct nvme_command *sq_cmds;
99 volatile struct nvme_completion *cqes;
100 dma_addr_t sq_dma_addr;
101 dma_addr_t cq_dma_addr;
102 wait_queue_head_t sq_full;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -0500103 wait_queue_t sq_cong_wait;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500104 struct bio_list sq_cong;
Keith Buschedd10d32014-04-03 16:45:23 -0600105 struct list_head iod_bio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500106 u32 __iomem *q_db;
107 u16 q_depth;
108 u16 cq_vector;
109 u16 sq_head;
110 u16 sq_tail;
111 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700112 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400113 u8 cq_phase;
114 u8 cqe_seen;
Keith Busch22404272013-07-15 15:02:20 -0600115 u8 q_suspended;
Keith Busch42f61422014-03-24 10:46:25 -0600116 cpumask_var_t cpu_mask;
Keith Busch4d115422013-12-10 13:10:40 -0700117 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500118 unsigned long cmdid_data[];
119};
120
121/*
122 * Check we didin't inadvertently grow the command struct
123 */
124static inline void _nvme_check_size(void)
125{
126 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
127 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
128 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
129 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
130 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400131 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700132 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500133 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
134 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
135 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
136 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600137 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500138}
139
Keith Buschedd10d32014-04-03 16:45:23 -0600140typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400141 struct nvme_completion *);
142
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500143struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400144 nvme_completion_fn fn;
145 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500146 unsigned long timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700147 int aborted;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500148};
149
150static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
151{
152 return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
153}
154
Keith Busch22404272013-07-15 15:02:20 -0600155static unsigned nvme_queue_extra(int depth)
156{
157 return DIV_ROUND_UP(depth, 8) + (depth * sizeof(struct nvme_cmd_info));
158}
159
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500160/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400161 * alloc_cmdid() - Allocate a Command ID
162 * @nvmeq: The queue that will be used for this command
163 * @ctx: A pointer that will be passed to the handler
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400164 * @handler: The function to call on completion
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500165 *
166 * Allocate a Command ID for a queue. The data passed in will
167 * be passed to the completion handler. This is implemented by using
168 * the bottom two bits of the ctx pointer to store the handler ID.
169 * Passing in a pointer that's not 4-byte aligned will cause a BUG.
170 * We can change this if it becomes a problem.
Matthew Wilcox184d2942011-05-11 21:36:38 -0400171 *
172 * May be called with local interrupts disabled and the q_lock held,
173 * or with interrupts enabled and no locks held.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500174 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400175static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx,
176 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500177{
Matthew Wilcoxe6d15f72011-02-24 08:49:41 -0500178 int depth = nvmeq->q_depth - 1;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500179 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500180 int cmdid;
181
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500182 do {
183 cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
184 if (cmdid >= depth)
185 return -EBUSY;
186 } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
187
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400188 info[cmdid].fn = handler;
189 info[cmdid].ctx = ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500190 info[cmdid].timeout = jiffies + timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700191 info[cmdid].aborted = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500192 return cmdid;
193}
194
195static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400196 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500197{
198 int cmdid;
199 wait_event_killable(nvmeq->sq_full,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500200 (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500201 return (cmdid < 0) ? -EINTR : cmdid;
202}
203
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400204/* Special values must be less than 0x1000 */
205#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500206#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
207#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
208#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Keith Busch53562be2014-04-29 11:41:29 -0600209#define CMD_CTX_ABORT (0x318 + CMD_CTX_BASE)
Keith Busch6fccf932014-06-18 13:58:57 -0600210#define CMD_CTX_ASYNC (0x31C + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500211
Keith Buschedd10d32014-04-03 16:45:23 -0600212static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400213 struct nvme_completion *cqe)
214{
215 if (ctx == CMD_CTX_CANCELLED)
216 return;
Keith Buschc30341d2013-12-10 13:10:38 -0700217 if (ctx == CMD_CTX_ABORT) {
Keith Buschedd10d32014-04-03 16:45:23 -0600218 ++nvmeq->dev->abort_limit;
Keith Buschc30341d2013-12-10 13:10:38 -0700219 return;
220 }
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400221 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600222 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400223 "completed id %d twice on queue %d\n",
224 cqe->command_id, le16_to_cpup(&cqe->sq_id));
225 return;
226 }
227 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600228 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400229 "invalid id %d completed on queue %d\n",
230 cqe->command_id, le16_to_cpup(&cqe->sq_id));
231 return;
232 }
Keith Busch6fccf932014-06-18 13:58:57 -0600233 if (ctx == CMD_CTX_ASYNC) {
234 u32 result = le32_to_cpup(&cqe->result);
235 u16 status = le16_to_cpup(&cqe->status) >> 1;
236
237 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
238 ++nvmeq->dev->event_limit;
239 if (status == NVME_SC_SUCCESS)
240 dev_warn(nvmeq->q_dmadev,
241 "async event result %08x\n", result);
242 return;
243 }
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400244
Keith Buschedd10d32014-04-03 16:45:23 -0600245 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400246}
247
Keith Buschedd10d32014-04-03 16:45:23 -0600248static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700249 struct nvme_completion *cqe)
250{
251 struct async_cmd_info *cmdinfo = ctx;
252 cmdinfo->result = le32_to_cpup(&cqe->result);
253 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
254 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
255}
256
Matthew Wilcox184d2942011-05-11 21:36:38 -0400257/*
258 * Called with local interrupts disabled and the q_lock held. May not sleep.
259 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400260static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid,
261 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500262{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400263 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500264 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500265
Keith Busch94bbac42014-04-24 18:53:50 -0600266 if (cmdid >= nvmeq->q_depth || !info[cmdid].fn) {
267 if (fn)
268 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500269 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400270 }
Keith Busch859361a2012-08-02 14:05:59 -0600271 if (fn)
272 *fn = info[cmdid].fn;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400273 ctx = info[cmdid].ctx;
274 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500275 info[cmdid].ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500276 clear_bit(cmdid, nvmeq->cmdid_data);
277 wake_up(&nvmeq->sq_full);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400278 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500279}
280
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400281static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
282 nvme_completion_fn *fn)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500283{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400284 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500285 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400286 if (fn)
287 *fn = info[cmdid].fn;
288 ctx = info[cmdid].ctx;
289 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500290 info[cmdid].ctx = CMD_CTX_CANCELLED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400291 return ctx;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500292}
293
Keith Busch5a92e702014-02-21 14:13:44 -0700294static struct nvme_queue *raw_nvmeq(struct nvme_dev *dev, int qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500295{
Keith Busch5a92e702014-02-21 14:13:44 -0700296 return rcu_dereference_raw(dev->queues[qid]);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500297}
298
Keith Busch4f5099a2014-03-03 16:39:13 -0700299static struct nvme_queue *get_nvmeq(struct nvme_dev *dev) __acquires(RCU)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500300{
Keith Buscha51afb52014-05-13 10:32:46 -0600301 struct nvme_queue *nvmeq;
Keith Busch42f61422014-03-24 10:46:25 -0600302 unsigned queue_id = get_cpu_var(*dev->io_queue);
Keith Buscha51afb52014-05-13 10:32:46 -0600303
Keith Busch5a92e702014-02-21 14:13:44 -0700304 rcu_read_lock();
Keith Buscha51afb52014-05-13 10:32:46 -0600305 nvmeq = rcu_dereference(dev->queues[queue_id]);
306 if (nvmeq)
307 return nvmeq;
308
309 rcu_read_unlock();
310 put_cpu_var(*dev->io_queue);
311 return NULL;
Keith Busch5a92e702014-02-21 14:13:44 -0700312}
313
Keith Busch4f5099a2014-03-03 16:39:13 -0700314static void put_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500315{
Keith Busch5a92e702014-02-21 14:13:44 -0700316 rcu_read_unlock();
Keith Busch42f61422014-03-24 10:46:25 -0600317 put_cpu_var(nvmeq->dev->io_queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500318}
319
Keith Busch4f5099a2014-03-03 16:39:13 -0700320static struct nvme_queue *lock_nvmeq(struct nvme_dev *dev, int q_idx)
321 __acquires(RCU)
322{
Keith Buscha51afb52014-05-13 10:32:46 -0600323 struct nvme_queue *nvmeq;
324
Keith Busch4f5099a2014-03-03 16:39:13 -0700325 rcu_read_lock();
Keith Buscha51afb52014-05-13 10:32:46 -0600326 nvmeq = rcu_dereference(dev->queues[q_idx]);
327 if (nvmeq)
328 return nvmeq;
329
330 rcu_read_unlock();
331 return NULL;
Keith Busch4f5099a2014-03-03 16:39:13 -0700332}
333
334static void unlock_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
335{
336 rcu_read_unlock();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500337}
338
339/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400340 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500341 * @nvmeq: The queue to use
342 * @cmd: The command to send
343 *
344 * Safe to use from interrupt context
345 */
346static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
347{
348 unsigned long flags;
349 u16 tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500350 spin_lock_irqsave(&nvmeq->q_lock, flags);
Keith Busch4f5099a2014-03-03 16:39:13 -0700351 if (nvmeq->q_suspended) {
352 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
353 return -EBUSY;
354 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500355 tail = nvmeq->sq_tail;
356 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500357 if (++tail == nvmeq->q_depth)
358 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500359 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500360 nvmeq->sq_tail = tail;
361 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
362
363 return 0;
364}
365
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500366static __le64 **iod_list(struct nvme_iod *iod)
367{
368 return ((void *)iod) + iod->offset;
369}
370
371/*
372 * Will slightly overestimate the number of pages needed. This is OK
373 * as it only leads to a small amount of wasted memory for the lifetime of
374 * the I/O.
375 */
Keith Busch1d090622014-06-23 11:34:01 -0600376static int nvme_npages(unsigned size, struct nvme_dev *dev)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500377{
Keith Busch1d090622014-06-23 11:34:01 -0600378 unsigned nprps = DIV_ROUND_UP(size + dev->page_size, dev->page_size);
379 return DIV_ROUND_UP(8 * nprps, dev->page_size - 8);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500380}
381
382static struct nvme_iod *
Keith Busch1d090622014-06-23 11:34:01 -0600383nvme_alloc_iod(unsigned nseg, unsigned nbytes, struct nvme_dev *dev, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500384{
385 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Keith Busch1d090622014-06-23 11:34:01 -0600386 sizeof(__le64 *) * nvme_npages(nbytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500387 sizeof(struct scatterlist) * nseg, gfp);
388
389 if (iod) {
390 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
391 iod->npages = -1;
392 iod->length = nbytes;
Keith Busch2b196032012-11-06 11:59:23 -0700393 iod->nents = 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600394 iod->first_dma = 0ULL;
Keith Busch61982212013-05-29 15:59:39 -0600395 iod->start_time = jiffies;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500396 }
397
398 return iod;
399}
400
Vishal Verma5d0f6132013-03-04 18:40:58 -0700401void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500402{
Keith Busch1d090622014-06-23 11:34:01 -0600403 const int last_prp = dev->page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500404 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500405 __le64 **list = iod_list(iod);
406 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500407
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500408 if (iod->npages == 0)
409 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
410 for (i = 0; i < iod->npages; i++) {
411 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500412 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500413 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500414 prp_dma = next_prp_dma;
415 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500416 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500417}
418
Keith Busch61982212013-05-29 15:59:39 -0600419static void nvme_start_io_acct(struct bio *bio)
420{
421 struct gendisk *disk = bio->bi_bdev->bd_disk;
Sam Bradshawb4e75cb2014-05-09 13:27:07 -0700422 if (blk_queue_io_stat(disk->queue)) {
423 const int rw = bio_data_dir(bio);
424 int cpu = part_stat_lock();
425 part_round_stats(cpu, &disk->part0);
426 part_stat_inc(cpu, &disk->part0, ios[rw]);
427 part_stat_add(cpu, &disk->part0, sectors[rw],
428 bio_sectors(bio));
429 part_inc_in_flight(&disk->part0, rw);
430 part_stat_unlock();
431 }
Keith Busch61982212013-05-29 15:59:39 -0600432}
433
434static void nvme_end_io_acct(struct bio *bio, unsigned long start_time)
435{
436 struct gendisk *disk = bio->bi_bdev->bd_disk;
Sam Bradshawb4e75cb2014-05-09 13:27:07 -0700437 if (blk_queue_io_stat(disk->queue)) {
438 const int rw = bio_data_dir(bio);
439 unsigned long duration = jiffies - start_time;
440 int cpu = part_stat_lock();
441 part_stat_add(cpu, &disk->part0, ticks[rw], duration);
442 part_round_stats(cpu, &disk->part0);
443 part_dec_in_flight(&disk->part0, rw);
444 part_stat_unlock();
445 }
Keith Busch61982212013-05-29 15:59:39 -0600446}
447
Keith Buschedd10d32014-04-03 16:45:23 -0600448static void bio_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500449 struct nvme_completion *cqe)
450{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500451 struct nvme_iod *iod = ctx;
452 struct bio *bio = iod->private;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500453 u16 status = le16_to_cpup(&cqe->status) >> 1;
Keith Busch3291fa52014-04-28 12:30:52 -0600454 int error = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500455
Keith Buschedd10d32014-04-03 16:45:23 -0600456 if (unlikely(status)) {
457 if (!(status & NVME_SC_DNR ||
458 bio->bi_rw & REQ_FAILFAST_MASK) &&
459 (jiffies - iod->start_time) < IOD_TIMEOUT) {
460 if (!waitqueue_active(&nvmeq->sq_full))
461 add_wait_queue(&nvmeq->sq_full,
462 &nvmeq->sq_cong_wait);
463 list_add_tail(&iod->node, &nvmeq->iod_bio);
464 wake_up(&nvmeq->sq_full);
465 return;
466 }
Keith Busch3291fa52014-04-28 12:30:52 -0600467 error = -EIO;
Keith Buschedd10d32014-04-03 16:45:23 -0600468 }
Keith Busch9e59d092013-08-08 10:25:38 -0600469 if (iod->nents) {
Keith Buschedd10d32014-04-03 16:45:23 -0600470 dma_unmap_sg(nvmeq->q_dmadev, iod->sg, iod->nents,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500471 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busch9e59d092013-08-08 10:25:38 -0600472 nvme_end_io_acct(bio, iod->start_time);
473 }
Keith Buschedd10d32014-04-03 16:45:23 -0600474 nvme_free_iod(nvmeq->dev, iod);
Keith Busch3291fa52014-04-28 12:30:52 -0600475
476 trace_block_bio_complete(bdev_get_queue(bio->bi_bdev), bio, error);
477 bio_endio(bio, error);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500478}
479
Matthew Wilcox184d2942011-05-11 21:36:38 -0400480/* length is in bytes. gfp flags indicates whether we may sleep. */
Keith Buschedd10d32014-04-03 16:45:23 -0600481int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod, int total_len,
482 gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500483{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500484 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500485 int length = total_len;
486 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500487 int dma_len = sg_dma_len(sg);
488 u64 dma_addr = sg_dma_address(sg);
489 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500490 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500491 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500492 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500493 int nprps, i;
Keith Busch1d090622014-06-23 11:34:01 -0600494 u32 page_size = dev->page_size;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500495
Keith Busch1d090622014-06-23 11:34:01 -0600496 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500497 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500498 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500499
Keith Busch1d090622014-06-23 11:34:01 -0600500 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500501 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600502 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500503 } else {
504 sg = sg_next(sg);
505 dma_addr = sg_dma_address(sg);
506 dma_len = sg_dma_len(sg);
507 }
508
Keith Busch1d090622014-06-23 11:34:01 -0600509 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600510 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500511 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500512 }
513
Keith Busch1d090622014-06-23 11:34:01 -0600514 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500515 if (nprps <= (256 / 8)) {
516 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500517 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500518 } else {
519 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500520 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500521 }
522
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400523 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
524 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600525 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500526 iod->npages = -1;
Keith Busch1d090622014-06-23 11:34:01 -0600527 return (total_len - length) + page_size;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400528 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500529 list[0] = prp_list;
530 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500531 i = 0;
532 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600533 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500534 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400535 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500536 if (!prp_list)
537 return total_len - length;
538 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400539 prp_list[0] = old_prp_list[i - 1];
540 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
541 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500542 }
543 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600544 dma_len -= page_size;
545 dma_addr += page_size;
546 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500547 if (length <= 0)
548 break;
549 if (dma_len > 0)
550 continue;
551 BUG_ON(dma_len < 0);
552 sg = sg_next(sg);
553 dma_addr = sg_dma_address(sg);
554 dma_len = sg_dma_len(sg);
555 }
556
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500557 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500558}
559
Keith Busch427e9702013-04-09 11:59:32 -0600560static int nvme_split_and_submit(struct bio *bio, struct nvme_queue *nvmeq,
Kent Overstreet20d01892013-11-23 18:21:01 -0800561 int len)
Keith Busch427e9702013-04-09 11:59:32 -0600562{
Kent Overstreet20d01892013-11-23 18:21:01 -0800563 struct bio *split = bio_split(bio, len >> 9, GFP_ATOMIC, NULL);
564 if (!split)
Keith Busch427e9702013-04-09 11:59:32 -0600565 return -ENOMEM;
566
Keith Busch3291fa52014-04-28 12:30:52 -0600567 trace_block_split(bdev_get_queue(bio->bi_bdev), bio,
568 split->bi_iter.bi_sector);
Kent Overstreet20d01892013-11-23 18:21:01 -0800569 bio_chain(split, bio);
570
Keith Buschedd10d32014-04-03 16:45:23 -0600571 if (!waitqueue_active(&nvmeq->sq_full))
Keith Busch427e9702013-04-09 11:59:32 -0600572 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
Kent Overstreet20d01892013-11-23 18:21:01 -0800573 bio_list_add(&nvmeq->sq_cong, split);
574 bio_list_add(&nvmeq->sq_cong, bio);
Keith Buschedd10d32014-04-03 16:45:23 -0600575 wake_up(&nvmeq->sq_full);
Keith Busch427e9702013-04-09 11:59:32 -0600576
577 return 0;
578}
579
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500580/* NVMe scatterlists require no holes in the virtual address */
581#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
582 (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
583
Keith Busch427e9702013-04-09 11:59:32 -0600584static int nvme_map_bio(struct nvme_queue *nvmeq, struct nvme_iod *iod,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500585 struct bio *bio, enum dma_data_direction dma_dir, int psegs)
586{
Kent Overstreet79886132013-11-23 17:19:00 -0800587 struct bio_vec bvec, bvprv;
588 struct bvec_iter iter;
Matthew Wilcox76830842011-02-10 13:55:39 -0500589 struct scatterlist *sg = NULL;
Kent Overstreet79886132013-11-23 17:19:00 -0800590 int length = 0, nsegs = 0, split_len = bio->bi_iter.bi_size;
591 int first = 1;
Keith Busch159b67d2013-04-09 17:13:20 -0600592
593 if (nvmeq->dev->stripe_size)
594 split_len = nvmeq->dev->stripe_size -
Kent Overstreet4f024f32013-10-11 15:44:27 -0700595 ((bio->bi_iter.bi_sector << 9) &
596 (nvmeq->dev->stripe_size - 1));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500597
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500598 sg_init_table(iod->sg, psegs);
Kent Overstreet79886132013-11-23 17:19:00 -0800599 bio_for_each_segment(bvec, bio, iter) {
600 if (!first && BIOVEC_PHYS_MERGEABLE(&bvprv, &bvec)) {
601 sg->length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500602 } else {
Kent Overstreet79886132013-11-23 17:19:00 -0800603 if (!first && BIOVEC_NOT_VIRT_MERGEABLE(&bvprv, &bvec))
604 return nvme_split_and_submit(bio, nvmeq,
Kent Overstreet20d01892013-11-23 18:21:01 -0800605 length);
Keith Busch427e9702013-04-09 11:59:32 -0600606
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500607 sg = sg ? sg + 1 : iod->sg;
Kent Overstreet79886132013-11-23 17:19:00 -0800608 sg_set_page(sg, bvec.bv_page,
609 bvec.bv_len, bvec.bv_offset);
Matthew Wilcox76830842011-02-10 13:55:39 -0500610 nsegs++;
611 }
Keith Busch159b67d2013-04-09 17:13:20 -0600612
Kent Overstreet79886132013-11-23 17:19:00 -0800613 if (split_len - length < bvec.bv_len)
Kent Overstreet20d01892013-11-23 18:21:01 -0800614 return nvme_split_and_submit(bio, nvmeq, split_len);
Kent Overstreet79886132013-11-23 17:19:00 -0800615 length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500616 bvprv = bvec;
Kent Overstreet79886132013-11-23 17:19:00 -0800617 first = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500618 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500619 iod->nents = nsegs;
Matthew Wilcox76830842011-02-10 13:55:39 -0500620 sg_mark_end(sg);
Keith Busch427e9702013-04-09 11:59:32 -0600621 if (dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir) == 0)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500622 return -ENOMEM;
Keith Busch427e9702013-04-09 11:59:32 -0600623
Kent Overstreet4f024f32013-10-11 15:44:27 -0700624 BUG_ON(length != bio->bi_iter.bi_size);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500625 return length;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500626}
627
Keith Busch0e5e4f02012-11-09 16:33:05 -0700628static int nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
629 struct bio *bio, struct nvme_iod *iod, int cmdid)
630{
Keith Buschedd10d32014-04-03 16:45:23 -0600631 struct nvme_dsm_range *range =
632 (struct nvme_dsm_range *)iod_list(iod)[0];
Keith Busch0e5e4f02012-11-09 16:33:05 -0700633 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
634
Keith Busch0e5e4f02012-11-09 16:33:05 -0700635 range->cattr = cpu_to_le32(0);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700636 range->nlb = cpu_to_le32(bio->bi_iter.bi_size >> ns->lba_shift);
637 range->slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700638
639 memset(cmnd, 0, sizeof(*cmnd));
640 cmnd->dsm.opcode = nvme_cmd_dsm;
641 cmnd->dsm.command_id = cmdid;
642 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
643 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
644 cmnd->dsm.nr = 0;
645 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
646
647 if (++nvmeq->sq_tail == nvmeq->q_depth)
648 nvmeq->sq_tail = 0;
649 writel(nvmeq->sq_tail, nvmeq->q_db);
650
651 return 0;
652}
653
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500654static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
655 int cmdid)
656{
657 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
658
659 memset(cmnd, 0, sizeof(*cmnd));
660 cmnd->common.opcode = nvme_cmd_flush;
661 cmnd->common.command_id = cmdid;
662 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
663
664 if (++nvmeq->sq_tail == nvmeq->q_depth)
665 nvmeq->sq_tail = 0;
666 writel(nvmeq->sq_tail, nvmeq->q_db);
667
668 return 0;
669}
670
Keith Buschedd10d32014-04-03 16:45:23 -0600671static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500672{
Keith Buschedd10d32014-04-03 16:45:23 -0600673 struct bio *bio = iod->private;
674 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500675 struct nvme_command *cmnd;
Keith Buschedd10d32014-04-03 16:45:23 -0600676 int cmdid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500677 u16 control;
678 u32 dsmgmt;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500679
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500680 cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500681 if (unlikely(cmdid < 0))
Keith Buschedd10d32014-04-03 16:45:23 -0600682 return cmdid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500683
Keith Buschedd10d32014-04-03 16:45:23 -0600684 if (bio->bi_rw & REQ_DISCARD)
685 return nvme_submit_discard(nvmeq, ns, bio, iod, cmdid);
Keith Busch53562be2014-04-29 11:41:29 -0600686 if (bio->bi_rw & REQ_FLUSH)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500687 return nvme_submit_flush(nvmeq, ns, cmdid);
688
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500689 control = 0;
690 if (bio->bi_rw & REQ_FUA)
691 control |= NVME_RW_FUA;
692 if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
693 control |= NVME_RW_LR;
694
695 dsmgmt = 0;
696 if (bio->bi_rw & REQ_RAHEAD)
697 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
698
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500699 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500700 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500701
Keith Buschedd10d32014-04-03 16:45:23 -0600702 cmnd->rw.opcode = bio_data_dir(bio) ? nvme_cmd_write : nvme_cmd_read;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500703 cmnd->rw.command_id = cmdid;
704 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Keith Buschedd10d32014-04-03 16:45:23 -0600705 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
706 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700707 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Keith Buschedd10d32014-04-03 16:45:23 -0600708 cmnd->rw.length =
709 cpu_to_le16((bio->bi_iter.bi_size >> ns->lba_shift) - 1);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500710 cmnd->rw.control = cpu_to_le16(control);
711 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500712
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500713 if (++nvmeq->sq_tail == nvmeq->q_depth)
714 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500715 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500716
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500717 return 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600718}
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500719
Keith Busch53562be2014-04-29 11:41:29 -0600720static int nvme_split_flush_data(struct nvme_queue *nvmeq, struct bio *bio)
721{
722 struct bio *split = bio_clone(bio, GFP_ATOMIC);
723 if (!split)
724 return -ENOMEM;
725
726 split->bi_iter.bi_size = 0;
727 split->bi_phys_segments = 0;
728 bio->bi_rw &= ~REQ_FLUSH;
729 bio_chain(split, bio);
730
731 if (!waitqueue_active(&nvmeq->sq_full))
732 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
733 bio_list_add(&nvmeq->sq_cong, split);
734 bio_list_add(&nvmeq->sq_cong, bio);
735 wake_up_process(nvme_thread);
736
737 return 0;
738}
739
Keith Buschedd10d32014-04-03 16:45:23 -0600740/*
741 * Called with local interrupts disabled and the q_lock held. May not sleep.
742 */
743static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
744 struct bio *bio)
745{
746 struct nvme_iod *iod;
747 int psegs = bio_phys_segments(ns->queue, bio);
748 int result;
749
Keith Busch53562be2014-04-29 11:41:29 -0600750 if ((bio->bi_rw & REQ_FLUSH) && psegs)
751 return nvme_split_flush_data(nvmeq, bio);
Keith Buschedd10d32014-04-03 16:45:23 -0600752
Keith Busch1d090622014-06-23 11:34:01 -0600753 iod = nvme_alloc_iod(psegs, bio->bi_iter.bi_size, ns->dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600754 if (!iod)
755 return -ENOMEM;
756
757 iod->private = bio;
758 if (bio->bi_rw & REQ_DISCARD) {
759 void *range;
760 /*
761 * We reuse the small pool to allocate the 16-byte range here
762 * as it is not worth having a special pool for these or
763 * additional cases to handle freeing the iod.
764 */
765 range = dma_pool_alloc(nvmeq->dev->prp_small_pool,
766 GFP_ATOMIC,
767 &iod->first_dma);
768 if (!range) {
769 result = -ENOMEM;
770 goto free_iod;
771 }
772 iod_list(iod)[0] = (__le64 *)range;
773 iod->npages = 0;
774 } else if (psegs) {
775 result = nvme_map_bio(nvmeq, iod, bio,
776 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE,
777 psegs);
778 if (result <= 0)
779 goto free_iod;
780 if (nvme_setup_prps(nvmeq->dev, iod, result, GFP_ATOMIC) !=
781 result) {
782 result = -ENOMEM;
783 goto free_iod;
784 }
785 nvme_start_io_acct(bio);
786 }
787 if (unlikely(nvme_submit_iod(nvmeq, iod))) {
788 if (!waitqueue_active(&nvmeq->sq_full))
789 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
790 list_add_tail(&iod->node, &nvmeq->iod_bio);
791 }
792 return 0;
793
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500794 free_iod:
795 nvme_free_iod(nvmeq->dev, iod);
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500796 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500797}
798
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400799static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500800{
Matthew Wilcox82123462011-01-20 13:24:06 -0500801 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500802
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500803 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500804 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500805
806 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400807 void *ctx;
808 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500809 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500810 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500811 break;
812 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
813 if (++head == nvmeq->q_depth) {
814 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500815 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500816 }
817
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400818 ctx = free_cmdid(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600819 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500820 }
821
822 /* If the controller ignores the cq head doorbell and continuously
823 * writes to the queue, it is theoretically possible to wrap around
824 * the queue twice and mistakenly return IRQ_NONE. Linux only
825 * requires that 0.1% of your interrupts are handled, so this isn't
826 * a big problem.
827 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500828 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400829 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500830
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800831 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500832 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500833 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500834
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400835 nvmeq->cqe_seen = 1;
836 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500837}
838
Matthew Wilcox7d822452013-06-24 12:03:57 -0400839static void nvme_make_request(struct request_queue *q, struct bio *bio)
840{
841 struct nvme_ns *ns = q->queuedata;
842 struct nvme_queue *nvmeq = get_nvmeq(ns->dev);
843 int result = -EBUSY;
844
Keith Buschcd638942013-07-15 15:02:23 -0600845 if (!nvmeq) {
Keith Buschcd638942013-07-15 15:02:23 -0600846 bio_endio(bio, -EIO);
847 return;
848 }
849
Matthew Wilcox7d822452013-06-24 12:03:57 -0400850 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -0600851 if (!nvmeq->q_suspended && bio_list_empty(&nvmeq->sq_cong))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400852 result = nvme_submit_bio_queue(nvmeq, ns, bio);
853 if (unlikely(result)) {
Keith Buschedd10d32014-04-03 16:45:23 -0600854 if (!waitqueue_active(&nvmeq->sq_full))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400855 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
856 bio_list_add(&nvmeq->sq_cong, bio);
857 }
858
859 nvme_process_cq(nvmeq);
860 spin_unlock_irq(&nvmeq->q_lock);
861 put_nvmeq(nvmeq);
862}
863
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500864static irqreturn_t nvme_irq(int irq, void *data)
865{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500866 irqreturn_t result;
867 struct nvme_queue *nvmeq = data;
868 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400869 nvme_process_cq(nvmeq);
870 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
871 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500872 spin_unlock(&nvmeq->q_lock);
873 return result;
874}
875
876static irqreturn_t nvme_irq_check(int irq, void *data)
877{
878 struct nvme_queue *nvmeq = data;
879 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
880 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
881 return IRQ_NONE;
882 return IRQ_WAKE_THREAD;
883}
884
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500885static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
886{
887 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400888 cancel_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500889 spin_unlock_irq(&nvmeq->q_lock);
890}
891
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400892struct sync_cmd_info {
893 struct task_struct *task;
894 u32 result;
895 int status;
896};
897
Keith Buschedd10d32014-04-03 16:45:23 -0600898static void sync_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400899 struct nvme_completion *cqe)
900{
901 struct sync_cmd_info *cmdinfo = ctx;
902 cmdinfo->result = le32_to_cpup(&cqe->result);
903 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
904 wake_up_process(cmdinfo->task);
905}
906
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500907/*
908 * Returns 0 on success. If the result is negative, it's a Linux error code;
909 * if the result is positive, it's an NVM Express status code
910 */
Keith Busch4f5099a2014-03-03 16:39:13 -0700911static int nvme_submit_sync_cmd(struct nvme_dev *dev, int q_idx,
912 struct nvme_command *cmd,
Vishal Verma5d0f6132013-03-04 18:40:58 -0700913 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500914{
Keith Busch4f5099a2014-03-03 16:39:13 -0700915 int cmdid, ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500916 struct sync_cmd_info cmdinfo;
Keith Busch4f5099a2014-03-03 16:39:13 -0700917 struct nvme_queue *nvmeq;
918
919 nvmeq = lock_nvmeq(dev, q_idx);
Keith Buscha51afb52014-05-13 10:32:46 -0600920 if (!nvmeq)
Keith Busch4f5099a2014-03-03 16:39:13 -0700921 return -ENODEV;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500922
923 cmdinfo.task = current;
924 cmdinfo.status = -EINTR;
925
Keith Busch4f5099a2014-03-03 16:39:13 -0700926 cmdid = alloc_cmdid(nvmeq, &cmdinfo, sync_completion, timeout);
927 if (cmdid < 0) {
928 unlock_nvmeq(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500929 return cmdid;
Keith Busch4f5099a2014-03-03 16:39:13 -0700930 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500931 cmd->common.command_id = cmdid;
932
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500933 set_current_state(TASK_KILLABLE);
Keith Busch4f5099a2014-03-03 16:39:13 -0700934 ret = nvme_submit_cmd(nvmeq, cmd);
935 if (ret) {
936 free_cmdid(nvmeq, cmdid, NULL);
937 unlock_nvmeq(nvmeq);
938 set_current_state(TASK_RUNNING);
939 return ret;
940 }
941 unlock_nvmeq(nvmeq);
Keith Busch78f8d252013-04-19 14:11:06 -0600942 schedule_timeout(timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500943
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500944 if (cmdinfo.status == -EINTR) {
Keith Busch4f5099a2014-03-03 16:39:13 -0700945 nvmeq = lock_nvmeq(dev, q_idx);
Keith Buscha51afb52014-05-13 10:32:46 -0600946 if (nvmeq) {
Keith Busch4f5099a2014-03-03 16:39:13 -0700947 nvme_abort_command(nvmeq, cmdid);
Keith Buscha51afb52014-05-13 10:32:46 -0600948 unlock_nvmeq(nvmeq);
949 }
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500950 return -EINTR;
951 }
952
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500953 if (result)
954 *result = cmdinfo.result;
955
956 return cmdinfo.status;
957}
958
Keith Busch4d115422013-12-10 13:10:40 -0700959static int nvme_submit_async_cmd(struct nvme_queue *nvmeq,
960 struct nvme_command *cmd,
961 struct async_cmd_info *cmdinfo, unsigned timeout)
962{
963 int cmdid;
964
965 cmdid = alloc_cmdid_killable(nvmeq, cmdinfo, async_completion, timeout);
966 if (cmdid < 0)
967 return cmdid;
968 cmdinfo->status = -EINTR;
969 cmd->common.command_id = cmdid;
Keith Busch4f5099a2014-03-03 16:39:13 -0700970 return nvme_submit_cmd(nvmeq, cmd);
Keith Busch4d115422013-12-10 13:10:40 -0700971}
972
Vishal Verma5d0f6132013-03-04 18:40:58 -0700973int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500974 u32 *result)
975{
Keith Busch4f5099a2014-03-03 16:39:13 -0700976 return nvme_submit_sync_cmd(dev, 0, cmd, result, ADMIN_TIMEOUT);
977}
978
979int nvme_submit_io_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
980 u32 *result)
981{
982 return nvme_submit_sync_cmd(dev, smp_processor_id() + 1, cmd, result,
983 NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500984}
985
Keith Busch4d115422013-12-10 13:10:40 -0700986static int nvme_submit_admin_cmd_async(struct nvme_dev *dev,
987 struct nvme_command *cmd, struct async_cmd_info *cmdinfo)
988{
Keith Busch5a92e702014-02-21 14:13:44 -0700989 return nvme_submit_async_cmd(raw_nvmeq(dev, 0), cmd, cmdinfo,
Keith Busch4d115422013-12-10 13:10:40 -0700990 ADMIN_TIMEOUT);
991}
992
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500993static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
994{
995 int status;
996 struct nvme_command c;
997
998 memset(&c, 0, sizeof(c));
999 c.delete_queue.opcode = opcode;
1000 c.delete_queue.qid = cpu_to_le16(id);
1001
1002 status = nvme_submit_admin_cmd(dev, &c, NULL);
1003 if (status)
1004 return -EIO;
1005 return 0;
1006}
1007
1008static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1009 struct nvme_queue *nvmeq)
1010{
1011 int status;
1012 struct nvme_command c;
1013 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1014
1015 memset(&c, 0, sizeof(c));
1016 c.create_cq.opcode = nvme_admin_create_cq;
1017 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1018 c.create_cq.cqid = cpu_to_le16(qid);
1019 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1020 c.create_cq.cq_flags = cpu_to_le16(flags);
1021 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1022
1023 status = nvme_submit_admin_cmd(dev, &c, NULL);
1024 if (status)
1025 return -EIO;
1026 return 0;
1027}
1028
1029static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1030 struct nvme_queue *nvmeq)
1031{
1032 int status;
1033 struct nvme_command c;
1034 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1035
1036 memset(&c, 0, sizeof(c));
1037 c.create_sq.opcode = nvme_admin_create_sq;
1038 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1039 c.create_sq.sqid = cpu_to_le16(qid);
1040 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1041 c.create_sq.sq_flags = cpu_to_le16(flags);
1042 c.create_sq.cqid = cpu_to_le16(qid);
1043
1044 status = nvme_submit_admin_cmd(dev, &c, NULL);
1045 if (status)
1046 return -EIO;
1047 return 0;
1048}
1049
1050static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1051{
1052 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1053}
1054
1055static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1056{
1057 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1058}
1059
Vishal Verma5d0f6132013-03-04 18:40:58 -07001060int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001061 dma_addr_t dma_addr)
1062{
1063 struct nvme_command c;
1064
1065 memset(&c, 0, sizeof(c));
1066 c.identify.opcode = nvme_admin_identify;
1067 c.identify.nsid = cpu_to_le32(nsid);
1068 c.identify.prp1 = cpu_to_le64(dma_addr);
1069 c.identify.cns = cpu_to_le32(cns);
1070
1071 return nvme_submit_admin_cmd(dev, &c, NULL);
1072}
1073
Vishal Verma5d0f6132013-03-04 18:40:58 -07001074int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -06001075 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001076{
1077 struct nvme_command c;
1078
1079 memset(&c, 0, sizeof(c));
1080 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -06001081 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001082 c.features.prp1 = cpu_to_le64(dma_addr);
1083 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001084
Keith Busch08df1e02012-09-21 10:52:13 -06001085 return nvme_submit_admin_cmd(dev, &c, result);
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001086}
1087
Vishal Verma5d0f6132013-03-04 18:40:58 -07001088int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
1089 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001090{
1091 struct nvme_command c;
1092
1093 memset(&c, 0, sizeof(c));
1094 c.features.opcode = nvme_admin_set_features;
1095 c.features.prp1 = cpu_to_le64(dma_addr);
1096 c.features.fid = cpu_to_le32(fid);
1097 c.features.dword11 = cpu_to_le32(dword11);
1098
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001099 return nvme_submit_admin_cmd(dev, &c, result);
1100}
1101
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001102/**
Keith Buschc30341d2013-12-10 13:10:38 -07001103 * nvme_abort_cmd - Attempt aborting a command
1104 * @cmdid: Command id of a timed out IO
1105 * @queue: The queue with timed out IO
1106 *
1107 * Schedule controller reset if the command was already aborted once before and
1108 * still hasn't been returned to the driver, or if this is the admin queue.
1109 */
1110static void nvme_abort_cmd(int cmdid, struct nvme_queue *nvmeq)
1111{
1112 int a_cmdid;
1113 struct nvme_command cmd;
1114 struct nvme_dev *dev = nvmeq->dev;
1115 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Keith Busch5a92e702014-02-21 14:13:44 -07001116 struct nvme_queue *adminq;
Keith Buschc30341d2013-12-10 13:10:38 -07001117
1118 if (!nvmeq->qid || info[cmdid].aborted) {
1119 if (work_busy(&dev->reset_work))
1120 return;
1121 list_del_init(&dev->node);
1122 dev_warn(&dev->pci_dev->dev,
1123 "I/O %d QID %d timeout, reset controller\n", cmdid,
1124 nvmeq->qid);
Tejun Heo9ca97372014-03-07 10:24:49 -05001125 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschc30341d2013-12-10 13:10:38 -07001126 queue_work(nvme_workq, &dev->reset_work);
1127 return;
1128 }
1129
1130 if (!dev->abort_limit)
1131 return;
1132
Keith Busch5a92e702014-02-21 14:13:44 -07001133 adminq = rcu_dereference(dev->queues[0]);
1134 a_cmdid = alloc_cmdid(adminq, CMD_CTX_ABORT, special_completion,
Keith Buschc30341d2013-12-10 13:10:38 -07001135 ADMIN_TIMEOUT);
1136 if (a_cmdid < 0)
1137 return;
1138
1139 memset(&cmd, 0, sizeof(cmd));
1140 cmd.abort.opcode = nvme_admin_abort_cmd;
1141 cmd.abort.cid = cmdid;
1142 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
1143 cmd.abort.command_id = a_cmdid;
1144
1145 --dev->abort_limit;
1146 info[cmdid].aborted = 1;
1147 info[cmdid].timeout = jiffies + ADMIN_TIMEOUT;
1148
1149 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", cmdid,
1150 nvmeq->qid);
Keith Busch5a92e702014-02-21 14:13:44 -07001151 nvme_submit_cmd(adminq, &cmd);
Keith Buschc30341d2013-12-10 13:10:38 -07001152}
1153
1154/**
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001155 * nvme_cancel_ios - Cancel outstanding I/Os
1156 * @queue: The queue to cancel I/Os on
1157 * @timeout: True to only cancel I/Os which have timed out
1158 */
1159static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout)
1160{
1161 int depth = nvmeq->q_depth - 1;
1162 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
1163 unsigned long now = jiffies;
1164 int cmdid;
1165
1166 for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
1167 void *ctx;
1168 nvme_completion_fn fn;
1169 static struct nvme_completion cqe = {
Matthew Wilcoxaf2d9ca2013-04-16 15:18:30 -04001170 .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1),
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001171 };
1172
1173 if (timeout && !time_after(now, info[cmdid].timeout))
1174 continue;
Keith Busch053ab7022013-04-30 11:19:38 -06001175 if (info[cmdid].ctx == CMD_CTX_CANCELLED)
1176 continue;
Keith Busch6fccf932014-06-18 13:58:57 -06001177 if (timeout && info[cmdid].ctx == CMD_CTX_ASYNC)
1178 continue;
Keith Buschc30341d2013-12-10 13:10:38 -07001179 if (timeout && nvmeq->dev->initialized) {
1180 nvme_abort_cmd(cmdid, nvmeq);
1181 continue;
1182 }
1183 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n", cmdid,
1184 nvmeq->qid);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001185 ctx = cancel_cmdid(nvmeq, cmdid, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -06001186 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001187 }
1188}
1189
Keith Busch5a92e702014-02-21 14:13:44 -07001190static void nvme_free_queue(struct rcu_head *r)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001191{
Keith Busch5a92e702014-02-21 14:13:44 -07001192 struct nvme_queue *nvmeq = container_of(r, struct nvme_queue, r_head);
1193
Keith Busch22404272013-07-15 15:02:20 -06001194 spin_lock_irq(&nvmeq->q_lock);
1195 while (bio_list_peek(&nvmeq->sq_cong)) {
1196 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1197 bio_endio(bio, -EIO);
1198 }
Keith Buschedd10d32014-04-03 16:45:23 -06001199 while (!list_empty(&nvmeq->iod_bio)) {
1200 static struct nvme_completion cqe = {
1201 .status = cpu_to_le16(
1202 (NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1),
1203 };
1204 struct nvme_iod *iod = list_first_entry(&nvmeq->iod_bio,
1205 struct nvme_iod,
1206 node);
1207 list_del(&iod->node);
1208 bio_completion(nvmeq, iod, &cqe);
1209 }
Keith Busch22404272013-07-15 15:02:20 -06001210 spin_unlock_irq(&nvmeq->q_lock);
1211
Matthew Wilcox9e866772012-08-03 13:55:56 -04001212 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1213 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1214 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1215 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
Keith Busch42f61422014-03-24 10:46:25 -06001216 if (nvmeq->qid)
1217 free_cpumask_var(nvmeq->cpu_mask);
Matthew Wilcox9e866772012-08-03 13:55:56 -04001218 kfree(nvmeq);
1219}
1220
Keith Buscha1a5ef92013-12-16 13:50:00 -05001221static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001222{
1223 int i;
1224
Keith Buscha1a5ef92013-12-16 13:50:00 -05001225 for (i = dev->queue_count - 1; i >= lowest; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07001226 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
1227 rcu_assign_pointer(dev->queues[i], NULL);
1228 call_rcu(&nvmeq->r_head, nvme_free_queue);
Keith Busch22404272013-07-15 15:02:20 -06001229 dev->queue_count--;
Keith Busch22404272013-07-15 15:02:20 -06001230 }
1231}
1232
Keith Busch4d115422013-12-10 13:10:40 -07001233/**
1234 * nvme_suspend_queue - put queue into suspended state
1235 * @nvmeq - queue to suspend
1236 *
1237 * Returns 1 if already suspended, 0 otherwise.
1238 */
1239static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001240{
Keith Busch4d115422013-12-10 13:10:40 -07001241 int vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001242
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001243 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001244 if (nvmeq->q_suspended) {
1245 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07001246 return 1;
Keith Busch32958742012-08-20 14:57:49 -06001247 }
Keith Busch22404272013-07-15 15:02:20 -06001248 nvmeq->q_suspended = 1;
Keith Busch42f61422014-03-24 10:46:25 -06001249 nvmeq->dev->online_queues--;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001250 spin_unlock_irq(&nvmeq->q_lock);
1251
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001252 irq_set_affinity_hint(vector, NULL);
1253 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001254
Keith Busch4d115422013-12-10 13:10:40 -07001255 return 0;
1256}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001257
Keith Busch4d115422013-12-10 13:10:40 -07001258static void nvme_clear_queue(struct nvme_queue *nvmeq)
1259{
Keith Busch22404272013-07-15 15:02:20 -06001260 spin_lock_irq(&nvmeq->q_lock);
1261 nvme_process_cq(nvmeq);
1262 nvme_cancel_ios(nvmeq, false);
1263 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001264}
1265
Keith Busch4d115422013-12-10 13:10:40 -07001266static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1267{
Keith Busch5a92e702014-02-21 14:13:44 -07001268 struct nvme_queue *nvmeq = raw_nvmeq(dev, qid);
Keith Busch4d115422013-12-10 13:10:40 -07001269
1270 if (!nvmeq)
1271 return;
1272 if (nvme_suspend_queue(nvmeq))
1273 return;
1274
Keith Busch0e53d182013-12-10 13:10:39 -07001275 /* Don't tell the adapter to delete the admin queue.
1276 * Don't tell a removed adapter to delete IO queues. */
1277 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001278 adapter_delete_sq(dev, qid);
1279 adapter_delete_cq(dev, qid);
1280 }
Keith Busch4d115422013-12-10 13:10:40 -07001281 nvme_clear_queue(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001282}
1283
1284static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
1285 int depth, int vector)
1286{
1287 struct device *dmadev = &dev->pci_dev->dev;
Keith Busch22404272013-07-15 15:02:20 -06001288 unsigned extra = nvme_queue_extra(depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001289 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
1290 if (!nvmeq)
1291 return NULL;
1292
Joe Perches4d51abf2014-06-15 13:37:33 -07001293 nvmeq->cqes = dma_zalloc_coherent(dmadev, CQ_SIZE(depth),
1294 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001295 if (!nvmeq->cqes)
1296 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001297
1298 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
1299 &nvmeq->sq_dma_addr, GFP_KERNEL);
1300 if (!nvmeq->sq_cmds)
1301 goto free_cqdma;
1302
Keith Busch42f61422014-03-24 10:46:25 -06001303 if (qid && !zalloc_cpumask_var(&nvmeq->cpu_mask, GFP_KERNEL))
1304 goto free_sqdma;
1305
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001306 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001307 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001308 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1309 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001310 spin_lock_init(&nvmeq->q_lock);
1311 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001312 nvmeq->cq_phase = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001313 init_waitqueue_head(&nvmeq->sq_full);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001314 init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001315 bio_list_init(&nvmeq->sq_cong);
Keith Buschedd10d32014-04-03 16:45:23 -06001316 INIT_LIST_HEAD(&nvmeq->iod_bio);
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001317 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001318 nvmeq->q_depth = depth;
1319 nvmeq->cq_vector = vector;
Keith Buschc30341d2013-12-10 13:10:38 -07001320 nvmeq->qid = qid;
Keith Busch22404272013-07-15 15:02:20 -06001321 nvmeq->q_suspended = 1;
1322 dev->queue_count++;
Keith Busch5a92e702014-02-21 14:13:44 -07001323 rcu_assign_pointer(dev->queues[qid], nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001324
1325 return nvmeq;
1326
Keith Busch42f61422014-03-24 10:46:25 -06001327 free_sqdma:
1328 dma_free_coherent(dmadev, SQ_SIZE(depth), (void *)nvmeq->sq_cmds,
1329 nvmeq->sq_dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001330 free_cqdma:
Keith Busch68b8eca2013-05-01 13:07:47 -06001331 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001332 nvmeq->cq_dma_addr);
1333 free_nvmeq:
1334 kfree(nvmeq);
1335 return NULL;
1336}
1337
Matthew Wilcox30010822011-01-20 09:10:15 -05001338static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1339 const char *name)
1340{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001341 if (use_threaded_interrupts)
1342 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001343 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001344 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001345 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001346 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001347}
1348
Keith Busch22404272013-07-15 15:02:20 -06001349static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001350{
Keith Busch22404272013-07-15 15:02:20 -06001351 struct nvme_dev *dev = nvmeq->dev;
1352 unsigned extra = nvme_queue_extra(nvmeq->q_depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001353
Keith Busch22404272013-07-15 15:02:20 -06001354 nvmeq->sq_tail = 0;
1355 nvmeq->cq_head = 0;
1356 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001357 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001358 memset(nvmeq->cmdid_data, 0, extra);
1359 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
1360 nvme_cancel_ios(nvmeq, false);
1361 nvmeq->q_suspended = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001362 dev->online_queues++;
Keith Busch22404272013-07-15 15:02:20 -06001363}
1364
1365static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1366{
1367 struct nvme_dev *dev = nvmeq->dev;
1368 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001369
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001370 result = adapter_alloc_cq(dev, qid, nvmeq);
1371 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001372 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001373
1374 result = adapter_alloc_sq(dev, qid, nvmeq);
1375 if (result < 0)
1376 goto release_cq;
1377
Matthew Wilcox3193f072014-01-27 15:57:22 -05001378 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001379 if (result < 0)
1380 goto release_sq;
1381
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001382 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001383 nvme_init_queue(nvmeq, qid);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001384 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001385
1386 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001387
1388 release_sq:
1389 adapter_delete_sq(dev, qid);
1390 release_cq:
1391 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001392 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001393}
1394
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001395static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1396{
1397 unsigned long timeout;
1398 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1399
1400 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1401
1402 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1403 msleep(100);
1404 if (fatal_signal_pending(current))
1405 return -EINTR;
1406 if (time_after(jiffies, timeout)) {
1407 dev_err(&dev->pci_dev->dev,
Matthew Wilcox27e81662014-04-11 11:58:45 -04001408 "Device not ready; aborting %s\n", enabled ?
1409 "initialisation" : "reset");
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001410 return -ENODEV;
1411 }
1412 }
1413
1414 return 0;
1415}
1416
1417/*
1418 * If the device has been passed off to us in an enabled state, just clear
1419 * the enabled bit. The spec says we should set the 'shutdown notification
1420 * bits', but doing so may cause the device to complete commands to the
1421 * admin queue ... and we don't know what memory that might be pointing at!
1422 */
1423static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1424{
Dan McLeran01079522014-06-23 08:24:36 -06001425 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1426 dev->ctrl_config &= ~NVME_CC_ENABLE;
1427 writel(dev->ctrl_config, &dev->bar->cc);
Matthew Wilcox44af1462013-05-04 06:43:17 -04001428
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001429 return nvme_wait_ready(dev, cap, false);
1430}
1431
1432static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1433{
Dan McLeran01079522014-06-23 08:24:36 -06001434 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1435 dev->ctrl_config |= NVME_CC_ENABLE;
1436 writel(dev->ctrl_config, &dev->bar->cc);
1437
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001438 return nvme_wait_ready(dev, cap, true);
1439}
1440
Keith Busch1894d8f2013-07-15 15:02:22 -06001441static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1442{
1443 unsigned long timeout;
Keith Busch1894d8f2013-07-15 15:02:22 -06001444
Dan McLeran01079522014-06-23 08:24:36 -06001445 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1446 dev->ctrl_config |= NVME_CC_SHN_NORMAL;
1447
1448 writel(dev->ctrl_config, &dev->bar->cc);
Keith Busch1894d8f2013-07-15 15:02:22 -06001449
1450 timeout = 2 * HZ + jiffies;
1451 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1452 NVME_CSTS_SHST_CMPLT) {
1453 msleep(100);
1454 if (fatal_signal_pending(current))
1455 return -EINTR;
1456 if (time_after(jiffies, timeout)) {
1457 dev_err(&dev->pci_dev->dev,
1458 "Device shutdown incomplete; abort shutdown\n");
1459 return -ENODEV;
1460 }
1461 }
1462
1463 return 0;
1464}
1465
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001466static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001467{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001468 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001469 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001470 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001471 struct nvme_queue *nvmeq;
Keith Busch1d090622014-06-23 11:34:01 -06001472 unsigned page_shift = PAGE_SHIFT;
1473 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12;
1474 unsigned dev_page_max = NVME_CAP_MPSMAX(cap) + 12;
1475
1476 if (page_shift < dev_page_min) {
1477 dev_err(&dev->pci_dev->dev,
1478 "Minimum device page size (%u) too large for "
1479 "host (%u)\n", 1 << dev_page_min,
1480 1 << page_shift);
1481 return -ENODEV;
1482 }
1483 if (page_shift > dev_page_max) {
1484 dev_info(&dev->pci_dev->dev,
1485 "Device maximum page size (%u) smaller than "
1486 "host (%u); enabling work-around\n",
1487 1 << dev_page_max, 1 << page_shift);
1488 page_shift = dev_page_max;
1489 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001490
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001491 result = nvme_disable_ctrl(dev, cap);
1492 if (result < 0)
1493 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001494
Keith Busch5a92e702014-02-21 14:13:44 -07001495 nvmeq = raw_nvmeq(dev, 0);
Keith Buschcd638942013-07-15 15:02:23 -06001496 if (!nvmeq) {
1497 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
1498 if (!nvmeq)
1499 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001500 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001501
1502 aqa = nvmeq->q_depth - 1;
1503 aqa |= aqa << 16;
1504
Keith Busch1d090622014-06-23 11:34:01 -06001505 dev->page_size = 1 << page_shift;
1506
Dan McLeran01079522014-06-23 08:24:36 -06001507 dev->ctrl_config = NVME_CC_CSS_NVM;
Keith Busch1d090622014-06-23 11:34:01 -06001508 dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001509 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001510 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001511
1512 writel(aqa, &dev->bar->aqa);
1513 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1514 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001515
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001516 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001517 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001518 return result;
Matthew Wilcox9e866772012-08-03 13:55:56 -04001519
Matthew Wilcox3193f072014-01-27 15:57:22 -05001520 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Keith Busch025c5572013-05-01 13:07:51 -06001521 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001522 return result;
Keith Busch025c5572013-05-01 13:07:51 -06001523
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001524 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001525 nvme_init_queue(nvmeq, 0);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001526 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001527 return result;
1528}
1529
Vishal Verma5d0f6132013-03-04 18:40:58 -07001530struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001531 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001532{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001533 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001534 struct scatterlist *sg;
1535 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001536 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001537
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001538 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001539 return ERR_PTR(-EINVAL);
Dan Carpenter5460fc02013-05-13 17:59:50 +03001540 if (!length || length > INT_MAX - PAGE_SIZE)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001541 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001542
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001543 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001544 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1545 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001546 if (!pages)
1547 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001548
1549 err = get_user_pages_fast(addr, count, 1, pages);
1550 if (err < count) {
1551 count = err;
1552 err = -EFAULT;
1553 goto put_pages;
1554 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001555
Santosh Y6808c5f2014-05-29 10:01:52 +05301556 err = -ENOMEM;
Keith Busch1d090622014-06-23 11:34:01 -06001557 iod = nvme_alloc_iod(count, length, dev, GFP_KERNEL);
Santosh Y6808c5f2014-05-29 10:01:52 +05301558 if (!iod)
1559 goto put_pages;
1560
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001561 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001562 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001563 for (i = 0; i < count; i++) {
1564 sg_set_page(&sg[i], pages[i],
Dan Carpenter5460fc02013-05-13 17:59:50 +03001565 min_t(unsigned, length, PAGE_SIZE - offset),
1566 offset);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001567 length -= (PAGE_SIZE - offset);
1568 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001569 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001570 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001571 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001572
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001573 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1574 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001575 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001576 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001577
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001578 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001579 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001580
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001581 free_iod:
1582 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001583 put_pages:
1584 for (i = 0; i < count; i++)
1585 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001586 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001587 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001588}
1589
Vishal Verma5d0f6132013-03-04 18:40:58 -07001590void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001591 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001592{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001593 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001594
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001595 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1596 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001597
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001598 for (i = 0; i < iod->nents; i++)
1599 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001600}
1601
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001602static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1603{
1604 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001605 struct nvme_user_io io;
1606 struct nvme_command c;
Keith Buschf410c682013-04-23 17:23:59 -06001607 unsigned length, meta_len;
1608 int status, i;
1609 struct nvme_iod *iod, *meta_iod = NULL;
1610 dma_addr_t meta_dma_addr;
1611 void *meta, *uninitialized_var(meta_mem);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001612
1613 if (copy_from_user(&io, uio, sizeof(io)))
1614 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001615 length = (io.nblocks + 1) << ns->lba_shift;
Keith Buschf410c682013-04-23 17:23:59 -06001616 meta_len = (io.nblocks + 1) * ns->ms;
1617
1618 if (meta_len && ((io.metadata & 3) || !io.metadata))
1619 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001620
1621 switch (io.opcode) {
1622 case nvme_cmd_write:
1623 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001624 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001625 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001626 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001627 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001628 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001629 }
1630
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001631 if (IS_ERR(iod))
1632 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001633
1634 memset(&c, 0, sizeof(c));
1635 c.rw.opcode = io.opcode;
1636 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001637 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001638 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001639 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001640 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001641 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1642 c.rw.reftag = cpu_to_le32(io.reftag);
1643 c.rw.apptag = cpu_to_le16(io.apptag);
1644 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buschf410c682013-04-23 17:23:59 -06001645
1646 if (meta_len) {
Keith Busch1b567492013-07-18 12:13:51 -06001647 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
1648 meta_len);
Keith Buschf410c682013-04-23 17:23:59 -06001649 if (IS_ERR(meta_iod)) {
1650 status = PTR_ERR(meta_iod);
1651 meta_iod = NULL;
1652 goto unmap;
1653 }
1654
1655 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1656 &meta_dma_addr, GFP_KERNEL);
1657 if (!meta_mem) {
1658 status = -ENOMEM;
1659 goto unmap;
1660 }
1661
1662 if (io.opcode & 1) {
1663 int meta_offset = 0;
1664
1665 for (i = 0; i < meta_iod->nents; i++) {
1666 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1667 meta_iod->sg[i].offset;
1668 memcpy(meta_mem + meta_offset, meta,
1669 meta_iod->sg[i].length);
1670 kunmap_atomic(meta);
1671 meta_offset += meta_iod->sg[i].length;
1672 }
1673 }
1674
1675 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1676 }
1677
Keith Buschedd10d32014-04-03 16:45:23 -06001678 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1679 c.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1680 c.rw.prp2 = cpu_to_le64(iod->first_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001681
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001682 if (length != (io.nblocks + 1) << ns->lba_shift)
1683 status = -ENOMEM;
1684 else
Keith Busch4f5099a2014-03-03 16:39:13 -07001685 status = nvme_submit_io_cmd(dev, &c, NULL);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001686
Keith Buschf410c682013-04-23 17:23:59 -06001687 if (meta_len) {
1688 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1689 int meta_offset = 0;
1690
1691 for (i = 0; i < meta_iod->nents; i++) {
1692 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1693 meta_iod->sg[i].offset;
1694 memcpy(meta, meta_mem + meta_offset,
1695 meta_iod->sg[i].length);
1696 kunmap_atomic(meta);
1697 meta_offset += meta_iod->sg[i].length;
1698 }
1699 }
1700
1701 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1702 meta_dma_addr);
1703 }
1704
1705 unmap:
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001706 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001707 nvme_free_iod(dev, iod);
Keith Buschf410c682013-04-23 17:23:59 -06001708
1709 if (meta_iod) {
1710 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1711 nvme_free_iod(dev, meta_iod);
1712 }
1713
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001714 return status;
1715}
1716
Keith Busch50af8ba2012-07-25 16:07:55 -06001717static int nvme_user_admin_cmd(struct nvme_dev *dev,
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001718 struct nvme_admin_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001719{
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001720 struct nvme_admin_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001721 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001722 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001723 struct nvme_iod *uninitialized_var(iod);
Keith Busch94f370c2013-05-09 14:01:38 -06001724 unsigned timeout;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001725
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001726 if (!capable(CAP_SYS_ADMIN))
1727 return -EACCES;
1728 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001729 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001730
1731 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001732 c.common.opcode = cmd.opcode;
1733 c.common.flags = cmd.flags;
1734 c.common.nsid = cpu_to_le32(cmd.nsid);
1735 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1736 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1737 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1738 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1739 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1740 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1741 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1742 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1743
1744 length = cmd.data_len;
1745 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001746 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1747 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001748 if (IS_ERR(iod))
1749 return PTR_ERR(iod);
Keith Buschedd10d32014-04-03 16:45:23 -06001750 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1751 c.common.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1752 c.common.prp2 = cpu_to_le64(iod->first_dma);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001753 }
1754
Keith Busch94f370c2013-05-09 14:01:38 -06001755 timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
1756 ADMIN_TIMEOUT;
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001757 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001758 status = -ENOMEM;
1759 else
Keith Busch4f5099a2014-03-03 16:39:13 -07001760 status = nvme_submit_sync_cmd(dev, 0, &c, &cmd.result, timeout);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001761
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001762 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001763 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001764 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001765 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001766
Chayan Biswascf90bc42013-05-22 22:34:49 +00001767 if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
Keith Buschf4f117f2012-09-21 10:49:05 -06001768 sizeof(cmd.result)))
1769 status = -EFAULT;
1770
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001771 return status;
1772}
1773
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001774static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1775 unsigned long arg)
1776{
1777 struct nvme_ns *ns = bdev->bd_disk->private_data;
1778
1779 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001780 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001781 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001782 return ns->ns_id;
1783 case NVME_IOCTL_ADMIN_CMD:
Keith Busch50af8ba2012-07-25 16:07:55 -06001784 return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001785 case NVME_IOCTL_SUBMIT_IO:
1786 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001787 case SG_GET_VERSION_NUM:
1788 return nvme_sg_get_version_num((void __user *)arg);
1789 case SG_IO:
1790 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001791 default:
1792 return -ENOTTY;
1793 }
1794}
1795
Keith Busch320a3822013-10-23 13:07:34 -06001796#ifdef CONFIG_COMPAT
1797static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1798 unsigned int cmd, unsigned long arg)
1799{
1800 struct nvme_ns *ns = bdev->bd_disk->private_data;
1801
1802 switch (cmd) {
1803 case SG_IO:
1804 return nvme_sg_io32(ns, arg);
1805 }
1806 return nvme_ioctl(bdev, mode, cmd, arg);
1807}
1808#else
1809#define nvme_compat_ioctl NULL
1810#endif
1811
Keith Busch9ac27092014-01-31 16:53:39 -07001812static int nvme_open(struct block_device *bdev, fmode_t mode)
1813{
1814 struct nvme_ns *ns = bdev->bd_disk->private_data;
1815 struct nvme_dev *dev = ns->dev;
1816
1817 kref_get(&dev->kref);
1818 return 0;
1819}
1820
1821static void nvme_free_dev(struct kref *kref);
1822
1823static void nvme_release(struct gendisk *disk, fmode_t mode)
1824{
1825 struct nvme_ns *ns = disk->private_data;
1826 struct nvme_dev *dev = ns->dev;
1827
1828 kref_put(&dev->kref, nvme_free_dev);
1829}
1830
Keith Busch4cc09e22014-04-02 15:45:37 -06001831static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
1832{
1833 /* some standard values */
1834 geo->heads = 1 << 6;
1835 geo->sectors = 1 << 5;
1836 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1837 return 0;
1838}
1839
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001840static const struct block_device_operations nvme_fops = {
1841 .owner = THIS_MODULE,
1842 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06001843 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07001844 .open = nvme_open,
1845 .release = nvme_release,
Keith Busch4cc09e22014-04-02 15:45:37 -06001846 .getgeo = nvme_getgeo,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001847};
1848
Keith Buschedd10d32014-04-03 16:45:23 -06001849static void nvme_resubmit_iods(struct nvme_queue *nvmeq)
1850{
1851 struct nvme_iod *iod, *next;
1852
1853 list_for_each_entry_safe(iod, next, &nvmeq->iod_bio, node) {
1854 if (unlikely(nvme_submit_iod(nvmeq, iod)))
1855 break;
1856 list_del(&iod->node);
1857 if (bio_list_empty(&nvmeq->sq_cong) &&
1858 list_empty(&nvmeq->iod_bio))
1859 remove_wait_queue(&nvmeq->sq_full,
1860 &nvmeq->sq_cong_wait);
1861 }
1862}
1863
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001864static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1865{
1866 while (bio_list_peek(&nvmeq->sq_cong)) {
1867 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1868 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Keith Busch427e9702013-04-09 11:59:32 -06001869
Keith Buschedd10d32014-04-03 16:45:23 -06001870 if (bio_list_empty(&nvmeq->sq_cong) &&
1871 list_empty(&nvmeq->iod_bio))
Matthew Wilcox3cb967c2011-03-16 16:45:49 -04001872 remove_wait_queue(&nvmeq->sq_full,
1873 &nvmeq->sq_cong_wait);
Keith Busch427e9702013-04-09 11:59:32 -06001874 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
Keith Buschedd10d32014-04-03 16:45:23 -06001875 if (!waitqueue_active(&nvmeq->sq_full))
Keith Busch427e9702013-04-09 11:59:32 -06001876 add_wait_queue(&nvmeq->sq_full,
1877 &nvmeq->sq_cong_wait);
1878 bio_list_add_head(&nvmeq->sq_cong, bio);
1879 break;
1880 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001881 }
1882}
1883
Keith Busch6fccf932014-06-18 13:58:57 -06001884static int nvme_submit_async_req(struct nvme_queue *nvmeq)
1885{
1886 struct nvme_command *c;
1887 int cmdid;
1888
1889 cmdid = alloc_cmdid(nvmeq, CMD_CTX_ASYNC, special_completion, 0);
1890 if (cmdid < 0)
1891 return cmdid;
1892
1893 c = &nvmeq->sq_cmds[nvmeq->sq_tail];
1894 memset(c, 0, sizeof(*c));
1895 c->common.opcode = nvme_admin_async_event;
1896 c->common.command_id = cmdid;
1897
1898 if (++nvmeq->sq_tail == nvmeq->q_depth)
1899 nvmeq->sq_tail = 0;
1900 writel(nvmeq->sq_tail, nvmeq->q_db);
1901
1902 return 0;
1903}
1904
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001905static int nvme_kthread(void *data)
1906{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001907 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001908
1909 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001910 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001911 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001912 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001913 int i;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001914 if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
1915 dev->initialized) {
1916 if (work_busy(&dev->reset_work))
1917 continue;
1918 list_del_init(&dev->node);
1919 dev_warn(&dev->pci_dev->dev,
1920 "Failed status, reset controller\n");
Tejun Heo9ca97372014-03-07 10:24:49 -05001921 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001922 queue_work(nvme_workq, &dev->reset_work);
1923 continue;
1924 }
Keith Busch5a92e702014-02-21 14:13:44 -07001925 rcu_read_lock();
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001926 for (i = 0; i < dev->queue_count; i++) {
Keith Busch5a92e702014-02-21 14:13:44 -07001927 struct nvme_queue *nvmeq =
1928 rcu_dereference(dev->queues[i]);
Matthew Wilcox740216f2011-02-15 16:28:20 -05001929 if (!nvmeq)
1930 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001931 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001932 if (nvmeq->q_suspended)
1933 goto unlock;
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001934 nvme_process_cq(nvmeq);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001935 nvme_cancel_ios(nvmeq, true);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001936 nvme_resubmit_bios(nvmeq);
Keith Buschedd10d32014-04-03 16:45:23 -06001937 nvme_resubmit_iods(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06001938
1939 while ((i == 0) && (dev->event_limit > 0)) {
1940 if (nvme_submit_async_req(nvmeq))
1941 break;
1942 dev->event_limit--;
1943 }
Keith Busch22404272013-07-15 15:02:20 -06001944 unlock:
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001945 spin_unlock_irq(&nvmeq->q_lock);
1946 }
Keith Busch5a92e702014-02-21 14:13:44 -07001947 rcu_read_unlock();
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001948 }
1949 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001950 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001951 }
1952 return 0;
1953}
1954
Keith Busch0e5e4f02012-11-09 16:33:05 -07001955static void nvme_config_discard(struct nvme_ns *ns)
1956{
1957 u32 logical_block_size = queue_logical_block_size(ns->queue);
1958 ns->queue->limits.discard_zeroes_data = 0;
1959 ns->queue->limits.discard_alignment = logical_block_size;
1960 ns->queue->limits.discard_granularity = logical_block_size;
1961 ns->queue->limits.max_discard_sectors = 0xffffffff;
1962 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1963}
1964
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001965static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001966 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1967{
1968 struct nvme_ns *ns;
1969 struct gendisk *disk;
1970 int lbaf;
1971
1972 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1973 return NULL;
1974
1975 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1976 if (!ns)
1977 return NULL;
1978 ns->queue = blk_alloc_queue(GFP_KERNEL);
1979 if (!ns->queue)
1980 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001981 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
1982 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1983 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -06001984 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001985 blk_queue_make_request(ns->queue, nvme_make_request);
1986 ns->dev = dev;
1987 ns->queue->queuedata = ns;
1988
Matthew Wilcox469071a2013-12-09 12:58:46 -05001989 disk = alloc_disk(0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001990 if (!disk)
1991 goto out_free_queue;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001992 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001993 ns->disk = disk;
1994 lbaf = id->flbas & 0xf;
1995 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Buschf410c682013-04-23 17:23:59 -06001996 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busche9ef4632012-07-24 15:01:04 -06001997 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06001998 if (dev->max_hw_sectors)
1999 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Keith Buscha7d2ce22014-04-29 11:41:28 -06002000 if (dev->vwc & NVME_CTRL_VWC_PRESENT)
2001 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002002
2003 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002004 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002005 disk->fops = &nvme_fops;
2006 disk->private_data = ns;
2007 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05002008 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002009 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002010 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002011 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
2012
Keith Busch0e5e4f02012-11-09 16:33:05 -07002013 if (dev->oncs & NVME_CTRL_ONCS_DSM)
2014 nvme_config_discard(ns);
2015
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002016 return ns;
2017
2018 out_free_queue:
2019 blk_cleanup_queue(ns->queue);
2020 out_free_ns:
2021 kfree(ns);
2022 return NULL;
2023}
2024
Keith Busch42f61422014-03-24 10:46:25 -06002025static int nvme_find_closest_node(int node)
2026{
2027 int n, val, min_val = INT_MAX, best_node = node;
2028
2029 for_each_online_node(n) {
2030 if (n == node)
2031 continue;
2032 val = node_distance(node, n);
2033 if (val < min_val) {
2034 min_val = val;
2035 best_node = n;
2036 }
2037 }
2038 return best_node;
2039}
2040
2041static void nvme_set_queue_cpus(cpumask_t *qmask, struct nvme_queue *nvmeq,
2042 int count)
2043{
2044 int cpu;
2045 for_each_cpu(cpu, qmask) {
2046 if (cpumask_weight(nvmeq->cpu_mask) >= count)
2047 break;
2048 if (!cpumask_test_and_set_cpu(cpu, nvmeq->cpu_mask))
2049 *per_cpu_ptr(nvmeq->dev->io_queue, cpu) = nvmeq->qid;
2050 }
2051}
2052
2053static void nvme_add_cpus(cpumask_t *mask, const cpumask_t *unassigned_cpus,
2054 const cpumask_t *new_mask, struct nvme_queue *nvmeq, int cpus_per_queue)
2055{
2056 int next_cpu;
2057 for_each_cpu(next_cpu, new_mask) {
2058 cpumask_or(mask, mask, get_cpu_mask(next_cpu));
2059 cpumask_or(mask, mask, topology_thread_cpumask(next_cpu));
2060 cpumask_and(mask, mask, unassigned_cpus);
2061 nvme_set_queue_cpus(mask, nvmeq, cpus_per_queue);
2062 }
2063}
2064
2065static void nvme_create_io_queues(struct nvme_dev *dev)
2066{
2067 unsigned i, max;
2068
2069 max = min(dev->max_qid, num_online_cpus());
2070 for (i = dev->queue_count; i <= max; i++)
2071 if (!nvme_alloc_queue(dev, i, dev->q_depth, i - 1))
2072 break;
2073
2074 max = min(dev->queue_count - 1, num_online_cpus());
2075 for (i = dev->online_queues; i <= max; i++)
2076 if (nvme_create_queue(raw_nvmeq(dev, i), i))
2077 break;
2078}
2079
2080/*
2081 * If there are fewer queues than online cpus, this will try to optimally
2082 * assign a queue to multiple cpus by grouping cpus that are "close" together:
2083 * thread siblings, core, socket, closest node, then whatever else is
2084 * available.
2085 */
2086static void nvme_assign_io_queues(struct nvme_dev *dev)
2087{
2088 unsigned cpu, cpus_per_queue, queues, remainder, i;
2089 cpumask_var_t unassigned_cpus;
2090
2091 nvme_create_io_queues(dev);
2092
2093 queues = min(dev->online_queues - 1, num_online_cpus());
2094 if (!queues)
2095 return;
2096
2097 cpus_per_queue = num_online_cpus() / queues;
2098 remainder = queues - (num_online_cpus() - queues * cpus_per_queue);
2099
2100 if (!alloc_cpumask_var(&unassigned_cpus, GFP_KERNEL))
2101 return;
2102
2103 cpumask_copy(unassigned_cpus, cpu_online_mask);
2104 cpu = cpumask_first(unassigned_cpus);
2105 for (i = 1; i <= queues; i++) {
2106 struct nvme_queue *nvmeq = lock_nvmeq(dev, i);
2107 cpumask_t mask;
2108
2109 cpumask_clear(nvmeq->cpu_mask);
2110 if (!cpumask_weight(unassigned_cpus)) {
2111 unlock_nvmeq(nvmeq);
2112 break;
2113 }
2114
2115 mask = *get_cpu_mask(cpu);
2116 nvme_set_queue_cpus(&mask, nvmeq, cpus_per_queue);
2117 if (cpus_weight(mask) < cpus_per_queue)
2118 nvme_add_cpus(&mask, unassigned_cpus,
2119 topology_thread_cpumask(cpu),
2120 nvmeq, cpus_per_queue);
2121 if (cpus_weight(mask) < cpus_per_queue)
2122 nvme_add_cpus(&mask, unassigned_cpus,
2123 topology_core_cpumask(cpu),
2124 nvmeq, cpus_per_queue);
2125 if (cpus_weight(mask) < cpus_per_queue)
2126 nvme_add_cpus(&mask, unassigned_cpus,
2127 cpumask_of_node(cpu_to_node(cpu)),
2128 nvmeq, cpus_per_queue);
2129 if (cpus_weight(mask) < cpus_per_queue)
2130 nvme_add_cpus(&mask, unassigned_cpus,
2131 cpumask_of_node(
2132 nvme_find_closest_node(
2133 cpu_to_node(cpu))),
2134 nvmeq, cpus_per_queue);
2135 if (cpus_weight(mask) < cpus_per_queue)
2136 nvme_add_cpus(&mask, unassigned_cpus,
2137 unassigned_cpus,
2138 nvmeq, cpus_per_queue);
2139
2140 WARN(cpumask_weight(nvmeq->cpu_mask) != cpus_per_queue,
2141 "nvme%d qid:%d mis-matched queue-to-cpu assignment\n",
2142 dev->instance, i);
2143
2144 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
2145 nvmeq->cpu_mask);
2146 cpumask_andnot(unassigned_cpus, unassigned_cpus,
2147 nvmeq->cpu_mask);
2148 cpu = cpumask_next(cpu, unassigned_cpus);
2149 if (remainder && !--remainder)
2150 cpus_per_queue++;
2151 unlock_nvmeq(nvmeq);
2152 }
2153 WARN(cpumask_weight(unassigned_cpus), "nvme%d unassigned online cpus\n",
2154 dev->instance);
2155 i = 0;
2156 cpumask_andnot(unassigned_cpus, cpu_possible_mask, cpu_online_mask);
2157 for_each_cpu(cpu, unassigned_cpus)
2158 *per_cpu_ptr(dev->io_queue, cpu) = (i++ % queues) + 1;
2159 free_cpumask_var(unassigned_cpus);
2160}
2161
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002162static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002163{
2164 int status;
2165 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002166 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002167
Matthew Wilcoxdf348132012-01-11 07:29:56 -07002168 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002169 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04002170 if (status < 0)
2171 return status;
2172 if (status > 0) {
2173 dev_err(&dev->pci_dev->dev, "Could not set queue count (%d)\n",
2174 status);
Keith Buschbadc34d2014-06-23 14:25:35 -06002175 return 0;
Matthew Wilcox27e81662014-04-11 11:58:45 -04002176 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002177 return min(result & 0xffff, result >> 16) + 1;
2178}
2179
Keith Busch9d713c22013-07-15 15:02:24 -06002180static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
2181{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002182 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06002183}
2184
Keith Buschf3db22f2014-06-11 11:51:35 -06002185static void nvme_cpu_workfn(struct work_struct *work)
2186{
2187 struct nvme_dev *dev = container_of(work, struct nvme_dev, cpu_work);
2188 if (dev->initialized)
2189 nvme_assign_io_queues(dev);
2190}
2191
Keith Busch33b1e952014-03-24 10:46:26 -06002192static int nvme_cpu_notify(struct notifier_block *self,
2193 unsigned long action, void *hcpu)
2194{
Keith Buschf3db22f2014-06-11 11:51:35 -06002195 struct nvme_dev *dev;
2196
Keith Busch33b1e952014-03-24 10:46:26 -06002197 switch (action) {
2198 case CPU_ONLINE:
2199 case CPU_DEAD:
Keith Buschf3db22f2014-06-11 11:51:35 -06002200 spin_lock(&dev_list_lock);
2201 list_for_each_entry(dev, &dev_list, node)
2202 schedule_work(&dev->cpu_work);
2203 spin_unlock(&dev_list_lock);
Keith Busch33b1e952014-03-24 10:46:26 -06002204 break;
2205 }
2206 return NOTIFY_OK;
2207}
2208
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002209static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002210{
Keith Busch5a92e702014-02-21 14:13:44 -07002211 struct nvme_queue *adminq = raw_nvmeq(dev, 0);
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002212 struct pci_dev *pdev = dev->pci_dev;
Keith Busch42f61422014-03-24 10:46:25 -06002213 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002214
Keith Busch42f61422014-03-24 10:46:25 -06002215 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002216 result = set_queue_count(dev, nr_io_queues);
Keith Buschbadc34d2014-06-23 14:25:35 -06002217 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002218 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002219 if (result < nr_io_queues)
2220 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002221
Keith Busch9d713c22013-07-15 15:02:24 -06002222 size = db_bar_size(dev, nr_io_queues);
2223 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002224 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06002225 do {
2226 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
2227 if (dev->bar)
2228 break;
2229 if (!--nr_io_queues)
2230 return -ENOMEM;
2231 size = db_bar_size(dev, nr_io_queues);
2232 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002233 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07002234 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002235 }
2236
Keith Busch9d713c22013-07-15 15:02:24 -06002237 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002238 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002239
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002240 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002241 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002242 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
2243 if (vecs < 0) {
2244 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
2245 if (vecs < 0) {
2246 vecs = 1;
2247 } else {
2248 for (i = 0; i < vecs; i++)
2249 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002250 }
2251 }
2252
Matthew Wilcox063a8092013-06-20 10:53:48 -04002253 /*
2254 * Should investigate if there's a performance win from allocating
2255 * more queues than interrupt vectors; it might allow the submission
2256 * path to scale better, even if the receive path is limited by the
2257 * number of interrupts.
2258 */
2259 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002260 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002261
Matthew Wilcox3193f072014-01-27 15:57:22 -05002262 result = queue_request_irq(dev, adminq, adminq->irqname);
Keith Busch9d713c22013-07-15 15:02:24 -06002263 if (result) {
Matthew Wilcox3193f072014-01-27 15:57:22 -05002264 adminq->q_suspended = 1;
Keith Busch22404272013-07-15 15:02:20 -06002265 goto free_queues;
Keith Busch9d713c22013-07-15 15:02:24 -06002266 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05002267
Keith Buschcd638942013-07-15 15:02:23 -06002268 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002269 nvme_free_queues(dev, nr_io_queues + 1);
2270 nvme_assign_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06002271
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002272 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002273
Keith Busch22404272013-07-15 15:02:20 -06002274 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002275 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002276 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002277}
2278
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002279/*
2280 * Return: error value if an error occurred setting up the queues or calling
2281 * Identify Device. 0 if these succeeded, even if adding some of the
2282 * namespaces failed. At the moment, these failures are silent. TBD which
2283 * failures should be reported.
2284 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002285static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002286{
Matthew Wilcox68608c262013-06-21 14:36:34 -04002287 struct pci_dev *pdev = dev->pci_dev;
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002288 int res;
2289 unsigned nn, i;
Keith Buschcbb62182013-05-01 13:07:49 -06002290 struct nvme_ns *ns;
Matthew Wilcox51814232011-02-01 16:18:08 -05002291 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002292 struct nvme_id_ns *id_ns;
2293 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002294 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06002295 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002296
Matthew Wilcox68608c262013-06-21 14:36:34 -04002297 mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06002298 if (!mem)
2299 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002300
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002301 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002302 if (res) {
Matthew Wilcox27e81662014-04-11 11:58:45 -04002303 dev_err(&pdev->dev, "Identify Controller failed (%d)\n", res);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002304 res = -EIO;
Keith Buschcbb62182013-05-01 13:07:49 -06002305 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002306 }
2307
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002308 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05002309 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002310 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002311 dev->abort_limit = ctrl->acl + 1;
Keith Buscha7d2ce22014-04-29 11:41:28 -06002312 dev->vwc = ctrl->vwc;
Keith Busch6fccf932014-06-18 13:58:57 -06002313 dev->event_limit = min(ctrl->aerl + 1, 8);
Matthew Wilcox51814232011-02-01 16:18:08 -05002314 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2315 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2316 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002317 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002318 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002319 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
2320 (pdev->device == 0x0953) && ctrl->vs[3])
Keith Busch159b67d2013-04-09 17:13:20 -06002321 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002322
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002323 id_ns = mem;
Matthew Wilcox2b2c1892011-10-07 13:10:13 -04002324 for (i = 1; i <= nn; i++) {
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002325 res = nvme_identify(dev, i, 0, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002326 if (res)
2327 continue;
2328
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002329 if (id_ns->ncap == 0)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002330 continue;
2331
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002332 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
Keith Busch08df1e02012-09-21 10:52:13 -06002333 dma_addr + 4096, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002334 if (res)
Keith Busch12209032013-01-31 14:40:38 -07002335 memset(mem + 4096, 0, 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002336
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002337 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002338 if (ns)
2339 list_add_tail(&ns->list, &dev->namespaces);
2340 }
2341 list_for_each_entry(ns, &dev->namespaces, list)
2342 add_disk(ns->disk);
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002343 res = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002344
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002345 out:
Matthew Wilcox684f5c22011-09-19 17:14:53 -04002346 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002347 return res;
2348}
2349
Keith Busch0877cb02013-07-15 15:02:19 -06002350static int nvme_dev_map(struct nvme_dev *dev)
2351{
Keith Busch42f61422014-03-24 10:46:25 -06002352 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002353 int bars, result = -ENOMEM;
2354 struct pci_dev *pdev = dev->pci_dev;
2355
2356 if (pci_enable_device_mem(pdev))
2357 return result;
2358
2359 dev->entry[0].vector = pdev->irq;
2360 pci_set_master(pdev);
2361 bars = pci_select_bars(pdev, IORESOURCE_MEM);
2362 if (pci_request_selected_regions(pdev, bars, "nvme"))
2363 goto disable_pci;
2364
Russell King052d0ef2013-06-26 23:49:11 +01002365 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
2366 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2367 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002368
Keith Busch0877cb02013-07-15 15:02:19 -06002369 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2370 if (!dev->bar)
2371 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07002372 if (readl(&dev->bar->csts) == -1) {
2373 result = -ENODEV;
2374 goto unmap;
2375 }
Keith Busch42f61422014-03-24 10:46:25 -06002376 cap = readq(&dev->bar->cap);
2377 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2378 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Keith Busch0877cb02013-07-15 15:02:19 -06002379 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2380
2381 return 0;
2382
Keith Busch0e53d182013-12-10 13:10:39 -07002383 unmap:
2384 iounmap(dev->bar);
2385 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002386 disable:
2387 pci_release_regions(pdev);
2388 disable_pci:
2389 pci_disable_device(pdev);
2390 return result;
2391}
2392
2393static void nvme_dev_unmap(struct nvme_dev *dev)
2394{
2395 if (dev->pci_dev->msi_enabled)
2396 pci_disable_msi(dev->pci_dev);
2397 else if (dev->pci_dev->msix_enabled)
2398 pci_disable_msix(dev->pci_dev);
2399
2400 if (dev->bar) {
2401 iounmap(dev->bar);
2402 dev->bar = NULL;
Keith Busch9a6b9452013-12-10 13:10:36 -07002403 pci_release_regions(dev->pci_dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002404 }
2405
Keith Busch0877cb02013-07-15 15:02:19 -06002406 if (pci_is_enabled(dev->pci_dev))
2407 pci_disable_device(dev->pci_dev);
2408}
2409
Keith Busch4d115422013-12-10 13:10:40 -07002410struct nvme_delq_ctx {
2411 struct task_struct *waiter;
2412 struct kthread_worker *worker;
2413 atomic_t refcount;
2414};
2415
2416static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2417{
2418 dq->waiter = current;
2419 mb();
2420
2421 for (;;) {
2422 set_current_state(TASK_KILLABLE);
2423 if (!atomic_read(&dq->refcount))
2424 break;
2425 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2426 fatal_signal_pending(current)) {
2427 set_current_state(TASK_RUNNING);
2428
2429 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
2430 nvme_disable_queue(dev, 0);
2431
2432 send_sig(SIGKILL, dq->worker->task, 1);
2433 flush_kthread_worker(dq->worker);
2434 return;
2435 }
2436 }
2437 set_current_state(TASK_RUNNING);
2438}
2439
2440static void nvme_put_dq(struct nvme_delq_ctx *dq)
2441{
2442 atomic_dec(&dq->refcount);
2443 if (dq->waiter)
2444 wake_up_process(dq->waiter);
2445}
2446
2447static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2448{
2449 atomic_inc(&dq->refcount);
2450 return dq;
2451}
2452
2453static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2454{
2455 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
2456
2457 nvme_clear_queue(nvmeq);
2458 nvme_put_dq(dq);
2459}
2460
2461static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2462 kthread_work_func_t fn)
2463{
2464 struct nvme_command c;
2465
2466 memset(&c, 0, sizeof(c));
2467 c.delete_queue.opcode = opcode;
2468 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2469
2470 init_kthread_work(&nvmeq->cmdinfo.work, fn);
2471 return nvme_submit_admin_cmd_async(nvmeq->dev, &c, &nvmeq->cmdinfo);
2472}
2473
2474static void nvme_del_cq_work_handler(struct kthread_work *work)
2475{
2476 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2477 cmdinfo.work);
2478 nvme_del_queue_end(nvmeq);
2479}
2480
2481static int nvme_delete_cq(struct nvme_queue *nvmeq)
2482{
2483 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2484 nvme_del_cq_work_handler);
2485}
2486
2487static void nvme_del_sq_work_handler(struct kthread_work *work)
2488{
2489 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2490 cmdinfo.work);
2491 int status = nvmeq->cmdinfo.status;
2492
2493 if (!status)
2494 status = nvme_delete_cq(nvmeq);
2495 if (status)
2496 nvme_del_queue_end(nvmeq);
2497}
2498
2499static int nvme_delete_sq(struct nvme_queue *nvmeq)
2500{
2501 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2502 nvme_del_sq_work_handler);
2503}
2504
2505static void nvme_del_queue_start(struct kthread_work *work)
2506{
2507 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2508 cmdinfo.work);
2509 allow_signal(SIGKILL);
2510 if (nvme_delete_sq(nvmeq))
2511 nvme_del_queue_end(nvmeq);
2512}
2513
2514static void nvme_disable_io_queues(struct nvme_dev *dev)
2515{
2516 int i;
2517 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2518 struct nvme_delq_ctx dq;
2519 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2520 &worker, "nvme%d", dev->instance);
2521
2522 if (IS_ERR(kworker_task)) {
2523 dev_err(&dev->pci_dev->dev,
2524 "Failed to create queue del task\n");
2525 for (i = dev->queue_count - 1; i > 0; i--)
2526 nvme_disable_queue(dev, i);
2527 return;
2528 }
2529
2530 dq.waiter = NULL;
2531 atomic_set(&dq.refcount, 0);
2532 dq.worker = &worker;
2533 for (i = dev->queue_count - 1; i > 0; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07002534 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
Keith Busch4d115422013-12-10 13:10:40 -07002535
2536 if (nvme_suspend_queue(nvmeq))
2537 continue;
2538 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2539 nvmeq->cmdinfo.worker = dq.worker;
2540 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2541 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2542 }
2543 nvme_wait_dq(&dq, dev);
2544 kthread_stop(kworker_task);
2545}
2546
Dan McLeranb9afca32014-04-07 17:10:11 -06002547/*
2548* Remove the node from the device list and check
2549* for whether or not we need to stop the nvme_thread.
2550*/
2551static void nvme_dev_list_remove(struct nvme_dev *dev)
2552{
2553 struct task_struct *tmp = NULL;
2554
2555 spin_lock(&dev_list_lock);
2556 list_del_init(&dev->node);
2557 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2558 tmp = nvme_thread;
2559 nvme_thread = NULL;
2560 }
2561 spin_unlock(&dev_list_lock);
2562
2563 if (tmp)
2564 kthread_stop(tmp);
2565}
2566
Keith Buschf0b50732013-07-15 15:02:21 -06002567static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002568{
Keith Busch22404272013-07-15 15:02:20 -06002569 int i;
2570
Keith Buschd4b4ff82013-12-10 13:10:37 -07002571 dev->initialized = 0;
Dan McLeranb9afca32014-04-07 17:10:11 -06002572 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002573
Keith Busch4d115422013-12-10 13:10:40 -07002574 if (!dev->bar || (dev->bar && readl(&dev->bar->csts) == -1)) {
2575 for (i = dev->queue_count - 1; i >= 0; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07002576 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
Keith Busch4d115422013-12-10 13:10:40 -07002577 nvme_suspend_queue(nvmeq);
2578 nvme_clear_queue(nvmeq);
2579 }
2580 } else {
2581 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002582 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002583 nvme_disable_queue(dev, 0);
2584 }
Keith Buschf0b50732013-07-15 15:02:21 -06002585 nvme_dev_unmap(dev);
2586}
2587
2588static void nvme_dev_remove(struct nvme_dev *dev)
2589{
Keith Busch9ac27092014-01-31 16:53:39 -07002590 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002591
Keith Busch9ac27092014-01-31 16:53:39 -07002592 list_for_each_entry(ns, &dev->namespaces, list) {
2593 if (ns->disk->flags & GENHD_FL_UP)
2594 del_gendisk(ns->disk);
2595 if (!blk_queue_dying(ns->queue))
2596 blk_cleanup_queue(ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002597 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002598}
2599
Matthew Wilcox091b6092011-02-10 09:56:01 -05002600static int nvme_setup_prp_pools(struct nvme_dev *dev)
2601{
2602 struct device *dmadev = &dev->pci_dev->dev;
2603 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2604 PAGE_SIZE, PAGE_SIZE, 0);
2605 if (!dev->prp_page_pool)
2606 return -ENOMEM;
2607
Matthew Wilcox99802a72011-02-10 10:30:34 -05002608 /* Optimisation for I/Os between 4k and 128k */
2609 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2610 256, 256, 0);
2611 if (!dev->prp_small_pool) {
2612 dma_pool_destroy(dev->prp_page_pool);
2613 return -ENOMEM;
2614 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002615 return 0;
2616}
2617
2618static void nvme_release_prp_pools(struct nvme_dev *dev)
2619{
2620 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002621 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002622}
2623
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002624static DEFINE_IDA(nvme_instance_ida);
2625
2626static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002627{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002628 int instance, error;
2629
2630 do {
2631 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2632 return -ENODEV;
2633
2634 spin_lock(&dev_list_lock);
2635 error = ida_get_new(&nvme_instance_ida, &instance);
2636 spin_unlock(&dev_list_lock);
2637 } while (error == -EAGAIN);
2638
2639 if (error)
2640 return -ENODEV;
2641
2642 dev->instance = instance;
2643 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002644}
2645
2646static void nvme_release_instance(struct nvme_dev *dev)
2647{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002648 spin_lock(&dev_list_lock);
2649 ida_remove(&nvme_instance_ida, dev->instance);
2650 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002651}
2652
Keith Busch9ac27092014-01-31 16:53:39 -07002653static void nvme_free_namespaces(struct nvme_dev *dev)
2654{
2655 struct nvme_ns *ns, *next;
2656
2657 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2658 list_del(&ns->list);
2659 put_disk(ns->disk);
2660 kfree(ns);
2661 }
2662}
2663
Keith Busch5e82e952013-02-19 10:17:58 -07002664static void nvme_free_dev(struct kref *kref)
2665{
2666 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002667
2668 nvme_free_namespaces(dev);
Keith Busch42f61422014-03-24 10:46:25 -06002669 free_percpu(dev->io_queue);
Keith Busch5e82e952013-02-19 10:17:58 -07002670 kfree(dev->queues);
2671 kfree(dev->entry);
2672 kfree(dev);
2673}
2674
2675static int nvme_dev_open(struct inode *inode, struct file *f)
2676{
2677 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
2678 miscdev);
2679 kref_get(&dev->kref);
2680 f->private_data = dev;
2681 return 0;
2682}
2683
2684static int nvme_dev_release(struct inode *inode, struct file *f)
2685{
2686 struct nvme_dev *dev = f->private_data;
2687 kref_put(&dev->kref, nvme_free_dev);
2688 return 0;
2689}
2690
2691static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2692{
2693 struct nvme_dev *dev = f->private_data;
2694 switch (cmd) {
2695 case NVME_IOCTL_ADMIN_CMD:
2696 return nvme_user_admin_cmd(dev, (void __user *)arg);
2697 default:
2698 return -ENOTTY;
2699 }
2700}
2701
2702static const struct file_operations nvme_dev_fops = {
2703 .owner = THIS_MODULE,
2704 .open = nvme_dev_open,
2705 .release = nvme_dev_release,
2706 .unlocked_ioctl = nvme_dev_ioctl,
2707 .compat_ioctl = nvme_dev_ioctl,
2708};
2709
Keith Buschf0b50732013-07-15 15:02:21 -06002710static int nvme_dev_start(struct nvme_dev *dev)
2711{
2712 int result;
Dan McLeranb9afca32014-04-07 17:10:11 -06002713 bool start_thread = false;
Keith Buschf0b50732013-07-15 15:02:21 -06002714
2715 result = nvme_dev_map(dev);
2716 if (result)
2717 return result;
2718
2719 result = nvme_configure_admin_queue(dev);
2720 if (result)
2721 goto unmap;
2722
2723 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002724 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2725 start_thread = true;
2726 nvme_thread = NULL;
2727 }
Keith Buschf0b50732013-07-15 15:02:21 -06002728 list_add(&dev->node, &dev_list);
2729 spin_unlock(&dev_list_lock);
2730
Dan McLeranb9afca32014-04-07 17:10:11 -06002731 if (start_thread) {
2732 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2733 wake_up(&nvme_kthread_wait);
2734 } else
2735 wait_event_killable(nvme_kthread_wait, nvme_thread);
2736
2737 if (IS_ERR_OR_NULL(nvme_thread)) {
2738 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2739 goto disable;
2740 }
2741
Keith Buschf0b50732013-07-15 15:02:21 -06002742 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002743 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002744 goto disable;
2745
Keith Buschd82e8bf2013-09-05 14:45:07 -06002746 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002747
2748 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002749 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06002750 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002751 unmap:
2752 nvme_dev_unmap(dev);
2753 return result;
2754}
2755
Keith Busch9a6b9452013-12-10 13:10:36 -07002756static int nvme_remove_dead_ctrl(void *arg)
2757{
2758 struct nvme_dev *dev = (struct nvme_dev *)arg;
2759 struct pci_dev *pdev = dev->pci_dev;
2760
2761 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06002762 pci_stop_and_remove_bus_device_locked(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002763 kref_put(&dev->kref, nvme_free_dev);
2764 return 0;
2765}
2766
2767static void nvme_remove_disks(struct work_struct *ws)
2768{
Keith Busch9a6b9452013-12-10 13:10:36 -07002769 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2770
2771 nvme_dev_remove(dev);
Keith Busch5a92e702014-02-21 14:13:44 -07002772 nvme_free_queues(dev, 1);
Keith Busch9a6b9452013-12-10 13:10:36 -07002773}
2774
2775static int nvme_dev_resume(struct nvme_dev *dev)
2776{
2777 int ret;
2778
2779 ret = nvme_dev_start(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002780 if (ret)
Keith Busch9a6b9452013-12-10 13:10:36 -07002781 return ret;
Keith Buschbadc34d2014-06-23 14:25:35 -06002782 if (dev->online_queues < 2) {
Keith Busch9a6b9452013-12-10 13:10:36 -07002783 spin_lock(&dev_list_lock);
Tejun Heo9ca97372014-03-07 10:24:49 -05002784 dev->reset_workfn = nvme_remove_disks;
Keith Busch9a6b9452013-12-10 13:10:36 -07002785 queue_work(nvme_workq, &dev->reset_work);
2786 spin_unlock(&dev_list_lock);
2787 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002788 dev->initialized = 1;
Keith Busch9a6b9452013-12-10 13:10:36 -07002789 return 0;
2790}
2791
2792static void nvme_dev_reset(struct nvme_dev *dev)
2793{
2794 nvme_dev_shutdown(dev);
2795 if (nvme_dev_resume(dev)) {
2796 dev_err(&dev->pci_dev->dev, "Device failed to resume\n");
2797 kref_get(&dev->kref);
2798 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2799 dev->instance))) {
2800 dev_err(&dev->pci_dev->dev,
2801 "Failed to start controller remove task\n");
2802 kref_put(&dev->kref, nvme_free_dev);
2803 }
2804 }
2805}
2806
2807static void nvme_reset_failed_dev(struct work_struct *ws)
2808{
2809 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2810 nvme_dev_reset(dev);
2811}
2812
Tejun Heo9ca97372014-03-07 10:24:49 -05002813static void nvme_reset_workfn(struct work_struct *work)
2814{
2815 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
2816 dev->reset_workfn(work);
2817}
2818
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002819static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002820{
Keith Busch0877cb02013-07-15 15:02:19 -06002821 int result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002822 struct nvme_dev *dev;
2823
2824 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2825 if (!dev)
2826 return -ENOMEM;
2827 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
2828 GFP_KERNEL);
2829 if (!dev->entry)
2830 goto free;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002831 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
2832 GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002833 if (!dev->queues)
2834 goto free;
Keith Busch42f61422014-03-24 10:46:25 -06002835 dev->io_queue = alloc_percpu(unsigned short);
2836 if (!dev->io_queue)
2837 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002838
2839 INIT_LIST_HEAD(&dev->namespaces);
Tejun Heo9ca97372014-03-07 10:24:49 -05002840 dev->reset_workfn = nvme_reset_failed_dev;
2841 INIT_WORK(&dev->reset_work, nvme_reset_workfn);
Keith Buschf3db22f2014-06-11 11:51:35 -06002842 INIT_WORK(&dev->cpu_work, nvme_cpu_workfn);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002843 dev->pci_dev = pdev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002844 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002845 result = nvme_set_instance(dev);
2846 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002847 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002848
Matthew Wilcox091b6092011-02-10 09:56:01 -05002849 result = nvme_setup_prp_pools(dev);
2850 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002851 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002852
Keith Buschfb35e912014-03-03 11:09:47 -07002853 kref_init(&dev->kref);
Keith Buschf0b50732013-07-15 15:02:21 -06002854 result = nvme_dev_start(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002855 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002856 goto release_pools;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002857
Keith Buschbadc34d2014-06-23 14:25:35 -06002858 if (dev->online_queues > 1)
2859 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002860 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002861 goto shutdown;
Matthew Wilcox740216f2011-02-15 16:28:20 -05002862
Keith Busch5e82e952013-02-19 10:17:58 -07002863 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
2864 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2865 dev->miscdev.parent = &pdev->dev;
2866 dev->miscdev.name = dev->name;
2867 dev->miscdev.fops = &nvme_dev_fops;
2868 result = misc_register(&dev->miscdev);
2869 if (result)
2870 goto remove;
2871
Keith Buschd4b4ff82013-12-10 13:10:37 -07002872 dev->initialized = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002873 return 0;
2874
Keith Busch5e82e952013-02-19 10:17:58 -07002875 remove:
2876 nvme_dev_remove(dev);
Keith Busch9ac27092014-01-31 16:53:39 -07002877 nvme_free_namespaces(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002878 shutdown:
2879 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002880 release_pools:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002881 nvme_free_queues(dev, 0);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002882 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002883 release:
2884 nvme_release_instance(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002885 free:
Keith Busch42f61422014-03-24 10:46:25 -06002886 free_percpu(dev->io_queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002887 kfree(dev->queues);
2888 kfree(dev->entry);
2889 kfree(dev);
2890 return result;
2891}
2892
Keith Buschf0d54a52014-05-02 10:40:43 -06002893static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2894{
2895 struct nvme_dev *dev = pci_get_drvdata(pdev);
2896
2897 if (prepare)
2898 nvme_dev_shutdown(dev);
2899 else
2900 nvme_dev_resume(dev);
2901}
2902
Keith Busch09ece142014-01-27 11:29:40 -05002903static void nvme_shutdown(struct pci_dev *pdev)
2904{
2905 struct nvme_dev *dev = pci_get_drvdata(pdev);
2906 nvme_dev_shutdown(dev);
2907}
2908
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002909static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002910{
2911 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002912
2913 spin_lock(&dev_list_lock);
2914 list_del_init(&dev->node);
2915 spin_unlock(&dev_list_lock);
2916
2917 pci_set_drvdata(pdev, NULL);
2918 flush_work(&dev->reset_work);
Keith Buschf3db22f2014-06-11 11:51:35 -06002919 flush_work(&dev->cpu_work);
Keith Busch5e82e952013-02-19 10:17:58 -07002920 misc_deregister(&dev->miscdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002921 nvme_dev_remove(dev);
2922 nvme_dev_shutdown(dev);
Keith Buscha1a5ef92013-12-16 13:50:00 -05002923 nvme_free_queues(dev, 0);
Keith Busch5a92e702014-02-21 14:13:44 -07002924 rcu_barrier();
Keith Busch9a6b9452013-12-10 13:10:36 -07002925 nvme_release_instance(dev);
2926 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002927 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002928}
2929
2930/* These functions are yet to be implemented */
2931#define nvme_error_detected NULL
2932#define nvme_dump_registers NULL
2933#define nvme_link_reset NULL
2934#define nvme_slot_reset NULL
2935#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002936
Jingoo Han671a6012014-02-13 11:19:14 +09002937#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002938static int nvme_suspend(struct device *dev)
2939{
2940 struct pci_dev *pdev = to_pci_dev(dev);
2941 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2942
2943 nvme_dev_shutdown(ndev);
2944 return 0;
2945}
2946
2947static int nvme_resume(struct device *dev)
2948{
2949 struct pci_dev *pdev = to_pci_dev(dev);
2950 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002951
Keith Busch9a6b9452013-12-10 13:10:36 -07002952 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Tejun Heo9ca97372014-03-07 10:24:49 -05002953 ndev->reset_workfn = nvme_reset_failed_dev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002954 queue_work(nvme_workq, &ndev->reset_work);
2955 }
2956 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002957}
Jingoo Han671a6012014-02-13 11:19:14 +09002958#endif
Keith Buschcd638942013-07-15 15:02:23 -06002959
2960static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002961
Stephen Hemminger1d352032012-09-07 09:33:17 -07002962static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002963 .error_detected = nvme_error_detected,
2964 .mmio_enabled = nvme_dump_registers,
2965 .link_reset = nvme_link_reset,
2966 .slot_reset = nvme_slot_reset,
2967 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06002968 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002969};
2970
2971/* Move to pci_ids.h later */
2972#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2973
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002974static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002975 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2976 { 0, }
2977};
2978MODULE_DEVICE_TABLE(pci, nvme_id_table);
2979
2980static struct pci_driver nvme_driver = {
2981 .name = "nvme",
2982 .id_table = nvme_id_table,
2983 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002984 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002985 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002986 .driver = {
2987 .pm = &nvme_dev_pm_ops,
2988 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002989 .err_handler = &nvme_err_handler,
2990};
2991
2992static int __init nvme_init(void)
2993{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002994 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002995
Dan McLeranb9afca32014-04-07 17:10:11 -06002996 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002997
Keith Busch9a6b9452013-12-10 13:10:36 -07002998 nvme_workq = create_singlethread_workqueue("nvme");
2999 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06003000 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07003001
Keith Busch5c42ea12012-07-25 16:05:18 -06003002 result = register_blkdev(nvme_major, "nvme");
3003 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07003004 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06003005 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003006 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003007
Keith Buschf3db22f2014-06-11 11:51:35 -06003008 nvme_nb.notifier_call = &nvme_cpu_notify;
3009 result = register_hotcpu_notifier(&nvme_nb);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003010 if (result)
3011 goto unregister_blkdev;
Keith Buschf3db22f2014-06-11 11:51:35 -06003012
3013 result = pci_register_driver(&nvme_driver);
3014 if (result)
3015 goto unregister_hotcpu;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003016 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003017
Keith Buschf3db22f2014-06-11 11:51:35 -06003018 unregister_hotcpu:
3019 unregister_hotcpu_notifier(&nvme_nb);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003020 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003021 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003022 kill_workq:
3023 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003024 return result;
3025}
3026
3027static void __exit nvme_exit(void)
3028{
3029 pci_unregister_driver(&nvme_driver);
Keith Buschf3db22f2014-06-11 11:51:35 -06003030 unregister_hotcpu_notifier(&nvme_nb);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003031 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003032 destroy_workqueue(nvme_workq);
Dan McLeranb9afca32014-04-07 17:10:11 -06003033 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04003034 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003035}
3036
3037MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
3038MODULE_LICENSE("GPL");
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003039MODULE_VERSION("0.9");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003040module_init(nvme_init);
3041module_exit(nvme_exit);