blob: 04664cadadfa3657d627be9dea48c3a4da7ffcc4 [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
3 * Copyright (c) 2011, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#include <linux/nvme.h>
20#include <linux/bio.h>
Matthew Wilcox8de05532011-05-12 13:50:28 -040021#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050022#include <linux/blkdev.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040023#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050024#include <linux/errno.h>
25#include <linux/fs.h>
26#include <linux/genhd.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040027#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050028#include <linux/init.h>
29#include <linux/interrupt.h>
30#include <linux/io.h>
31#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050032#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050033#include <linux/kernel.h>
34#include <linux/mm.h>
35#include <linux/module.h>
36#include <linux/moduleparam.h>
37#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050038#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040039#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050040#include <linux/sched.h>
41#include <linux/slab.h>
42#include <linux/types.h>
Vishal Verma5d0f6132013-03-04 18:40:58 -070043#include <scsi/sg.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090044#include <asm-generic/io-64-nonatomic-lo-hi.h>
45
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050046#define NVME_Q_DEPTH 1024
47#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
48#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Matthew Wilcoxe85248e2011-02-06 18:30:16 -050049#define ADMIN_TIMEOUT (60 * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050050
51static int nvme_major;
52module_param(nvme_major, int, 0);
53
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050054static int use_threaded_interrupts;
55module_param(use_threaded_interrupts, int, 0);
56
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050057static DEFINE_SPINLOCK(dev_list_lock);
58static LIST_HEAD(dev_list);
59static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070060static struct workqueue_struct *nvme_workq;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050061
Keith Buschd4b4ff82013-12-10 13:10:37 -070062static void nvme_reset_failed_dev(struct work_struct *ws);
63
Keith Busch4d115422013-12-10 13:10:40 -070064struct async_cmd_info {
65 struct kthread_work work;
66 struct kthread_worker *worker;
67 u32 result;
68 int status;
69 void *ctx;
70};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050071
72/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050073 * An NVM Express queue. Each device has at least two (one for admin
74 * commands and one for I/O commands).
75 */
76struct nvme_queue {
Keith Busch5a92e702014-02-21 14:13:44 -070077 struct rcu_head r_head;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050078 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -050079 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -050080 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050081 spinlock_t q_lock;
82 struct nvme_command *sq_cmds;
83 volatile struct nvme_completion *cqes;
84 dma_addr_t sq_dma_addr;
85 dma_addr_t cq_dma_addr;
86 wait_queue_head_t sq_full;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050087 wait_queue_t sq_cong_wait;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050088 struct bio_list sq_cong;
89 u32 __iomem *q_db;
90 u16 q_depth;
91 u16 cq_vector;
92 u16 sq_head;
93 u16 sq_tail;
94 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -070095 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -040096 u8 cq_phase;
97 u8 cqe_seen;
Keith Busch22404272013-07-15 15:02:20 -060098 u8 q_suspended;
Keith Busch4d115422013-12-10 13:10:40 -070099 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500100 unsigned long cmdid_data[];
101};
102
103/*
104 * Check we didin't inadvertently grow the command struct
105 */
106static inline void _nvme_check_size(void)
107{
108 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
109 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
110 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
111 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
112 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400113 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700114 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500115 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
116 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
117 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
118 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600119 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500120}
121
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500122typedef void (*nvme_completion_fn)(struct nvme_dev *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400123 struct nvme_completion *);
124
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500125struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400126 nvme_completion_fn fn;
127 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500128 unsigned long timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700129 int aborted;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500130};
131
132static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
133{
134 return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
135}
136
Keith Busch22404272013-07-15 15:02:20 -0600137static unsigned nvme_queue_extra(int depth)
138{
139 return DIV_ROUND_UP(depth, 8) + (depth * sizeof(struct nvme_cmd_info));
140}
141
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500142/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400143 * alloc_cmdid() - Allocate a Command ID
144 * @nvmeq: The queue that will be used for this command
145 * @ctx: A pointer that will be passed to the handler
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400146 * @handler: The function to call on completion
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500147 *
148 * Allocate a Command ID for a queue. The data passed in will
149 * be passed to the completion handler. This is implemented by using
150 * the bottom two bits of the ctx pointer to store the handler ID.
151 * Passing in a pointer that's not 4-byte aligned will cause a BUG.
152 * We can change this if it becomes a problem.
Matthew Wilcox184d2942011-05-11 21:36:38 -0400153 *
154 * May be called with local interrupts disabled and the q_lock held,
155 * or with interrupts enabled and no locks held.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500156 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400157static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx,
158 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500159{
Matthew Wilcoxe6d15f72011-02-24 08:49:41 -0500160 int depth = nvmeq->q_depth - 1;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500161 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500162 int cmdid;
163
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500164 do {
165 cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
166 if (cmdid >= depth)
167 return -EBUSY;
168 } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
169
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400170 info[cmdid].fn = handler;
171 info[cmdid].ctx = ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500172 info[cmdid].timeout = jiffies + timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700173 info[cmdid].aborted = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500174 return cmdid;
175}
176
177static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400178 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500179{
180 int cmdid;
181 wait_event_killable(nvmeq->sq_full,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500182 (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500183 return (cmdid < 0) ? -EINTR : cmdid;
184}
185
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400186/* Special values must be less than 0x1000 */
187#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500188#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
189#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
190#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500191#define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE)
Keith Buschc30341d2013-12-10 13:10:38 -0700192#define CMD_CTX_ABORT (0x31C + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500193
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500194static void special_completion(struct nvme_dev *dev, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400195 struct nvme_completion *cqe)
196{
197 if (ctx == CMD_CTX_CANCELLED)
198 return;
199 if (ctx == CMD_CTX_FLUSH)
200 return;
Keith Buschc30341d2013-12-10 13:10:38 -0700201 if (ctx == CMD_CTX_ABORT) {
202 ++dev->abort_limit;
203 return;
204 }
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400205 if (ctx == CMD_CTX_COMPLETED) {
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500206 dev_warn(&dev->pci_dev->dev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400207 "completed id %d twice on queue %d\n",
208 cqe->command_id, le16_to_cpup(&cqe->sq_id));
209 return;
210 }
211 if (ctx == CMD_CTX_INVALID) {
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500212 dev_warn(&dev->pci_dev->dev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400213 "invalid id %d completed on queue %d\n",
214 cqe->command_id, le16_to_cpup(&cqe->sq_id));
215 return;
216 }
217
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500218 dev_warn(&dev->pci_dev->dev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400219}
220
Keith Busch4d115422013-12-10 13:10:40 -0700221static void async_completion(struct nvme_dev *dev, void *ctx,
222 struct nvme_completion *cqe)
223{
224 struct async_cmd_info *cmdinfo = ctx;
225 cmdinfo->result = le32_to_cpup(&cqe->result);
226 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
227 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
228}
229
Matthew Wilcox184d2942011-05-11 21:36:38 -0400230/*
231 * Called with local interrupts disabled and the q_lock held. May not sleep.
232 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400233static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid,
234 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500235{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400236 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500237 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500238
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400239 if (cmdid >= nvmeq->q_depth) {
240 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500241 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400242 }
Keith Busch859361a2012-08-02 14:05:59 -0600243 if (fn)
244 *fn = info[cmdid].fn;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400245 ctx = info[cmdid].ctx;
246 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500247 info[cmdid].ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500248 clear_bit(cmdid, nvmeq->cmdid_data);
249 wake_up(&nvmeq->sq_full);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400250 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500251}
252
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400253static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
254 nvme_completion_fn *fn)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500255{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400256 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500257 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400258 if (fn)
259 *fn = info[cmdid].fn;
260 ctx = info[cmdid].ctx;
261 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500262 info[cmdid].ctx = CMD_CTX_CANCELLED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400263 return ctx;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500264}
265
Keith Busch5a92e702014-02-21 14:13:44 -0700266static struct nvme_queue *raw_nvmeq(struct nvme_dev *dev, int qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500267{
Keith Busch5a92e702014-02-21 14:13:44 -0700268 return rcu_dereference_raw(dev->queues[qid]);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500269}
270
Keith Busch4f5099a2014-03-03 16:39:13 -0700271static struct nvme_queue *get_nvmeq(struct nvme_dev *dev) __acquires(RCU)
Keith Busch5a92e702014-02-21 14:13:44 -0700272{
273 rcu_read_lock();
274 return rcu_dereference(dev->queues[get_cpu() + 1]);
275}
276
Keith Busch4f5099a2014-03-03 16:39:13 -0700277static void put_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500278{
Matthew Wilcox1b234842011-01-20 13:01:49 -0500279 put_cpu();
Keith Busch5a92e702014-02-21 14:13:44 -0700280 rcu_read_unlock();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500281}
282
Keith Busch4f5099a2014-03-03 16:39:13 -0700283static struct nvme_queue *lock_nvmeq(struct nvme_dev *dev, int q_idx)
284 __acquires(RCU)
285{
286 rcu_read_lock();
287 return rcu_dereference(dev->queues[q_idx]);
288}
289
290static void unlock_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
291{
292 rcu_read_unlock();
293}
294
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500295/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400296 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500297 * @nvmeq: The queue to use
298 * @cmd: The command to send
299 *
300 * Safe to use from interrupt context
301 */
302static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
303{
304 unsigned long flags;
305 u16 tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500306 spin_lock_irqsave(&nvmeq->q_lock, flags);
Keith Busch4f5099a2014-03-03 16:39:13 -0700307 if (nvmeq->q_suspended) {
308 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
309 return -EBUSY;
310 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500311 tail = nvmeq->sq_tail;
312 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500313 if (++tail == nvmeq->q_depth)
314 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500315 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500316 nvmeq->sq_tail = tail;
317 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
318
319 return 0;
320}
321
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500322static __le64 **iod_list(struct nvme_iod *iod)
323{
324 return ((void *)iod) + iod->offset;
325}
326
327/*
328 * Will slightly overestimate the number of pages needed. This is OK
329 * as it only leads to a small amount of wasted memory for the lifetime of
330 * the I/O.
331 */
332static int nvme_npages(unsigned size)
333{
334 unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE);
335 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
336}
337
338static struct nvme_iod *
339nvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp)
340{
341 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
342 sizeof(__le64 *) * nvme_npages(nbytes) +
343 sizeof(struct scatterlist) * nseg, gfp);
344
345 if (iod) {
346 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
347 iod->npages = -1;
348 iod->length = nbytes;
Keith Busch2b196032012-11-06 11:59:23 -0700349 iod->nents = 0;
Keith Busch61982212013-05-29 15:59:39 -0600350 iod->start_time = jiffies;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500351 }
352
353 return iod;
354}
355
Vishal Verma5d0f6132013-03-04 18:40:58 -0700356void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500357{
358 const int last_prp = PAGE_SIZE / 8 - 1;
359 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500360 __le64 **list = iod_list(iod);
361 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500362
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500363 if (iod->npages == 0)
364 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
365 for (i = 0; i < iod->npages; i++) {
366 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500367 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500368 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500369 prp_dma = next_prp_dma;
370 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500371 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500372}
373
Keith Busch61982212013-05-29 15:59:39 -0600374static void nvme_start_io_acct(struct bio *bio)
375{
376 struct gendisk *disk = bio->bi_bdev->bd_disk;
377 const int rw = bio_data_dir(bio);
378 int cpu = part_stat_lock();
379 part_round_stats(cpu, &disk->part0);
380 part_stat_inc(cpu, &disk->part0, ios[rw]);
381 part_stat_add(cpu, &disk->part0, sectors[rw], bio_sectors(bio));
382 part_inc_in_flight(&disk->part0, rw);
383 part_stat_unlock();
384}
385
386static void nvme_end_io_acct(struct bio *bio, unsigned long start_time)
387{
388 struct gendisk *disk = bio->bi_bdev->bd_disk;
389 const int rw = bio_data_dir(bio);
390 unsigned long duration = jiffies - start_time;
391 int cpu = part_stat_lock();
392 part_stat_add(cpu, &disk->part0, ticks[rw], duration);
393 part_round_stats(cpu, &disk->part0);
394 part_dec_in_flight(&disk->part0, rw);
395 part_stat_unlock();
396}
397
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500398static void bio_completion(struct nvme_dev *dev, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500399 struct nvme_completion *cqe)
400{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500401 struct nvme_iod *iod = ctx;
402 struct bio *bio = iod->private;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500403 u16 status = le16_to_cpup(&cqe->status) >> 1;
404
Keith Busch9e59d092013-08-08 10:25:38 -0600405 if (iod->nents) {
Keith Busch2b196032012-11-06 11:59:23 -0700406 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500407 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busch9e59d092013-08-08 10:25:38 -0600408 nvme_end_io_acct(bio, iod->start_time);
409 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500410 nvme_free_iod(dev, iod);
Keith Busch427e9702013-04-09 11:59:32 -0600411 if (status)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500412 bio_endio(bio, -EIO);
Keith Busch427e9702013-04-09 11:59:32 -0600413 else
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500414 bio_endio(bio, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500415}
416
Matthew Wilcox184d2942011-05-11 21:36:38 -0400417/* length is in bytes. gfp flags indicates whether we may sleep. */
Vishal Verma5d0f6132013-03-04 18:40:58 -0700418int nvme_setup_prps(struct nvme_dev *dev, struct nvme_common_command *cmd,
419 struct nvme_iod *iod, int total_len, gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500420{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500421 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500422 int length = total_len;
423 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500424 int dma_len = sg_dma_len(sg);
425 u64 dma_addr = sg_dma_address(sg);
426 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500427 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500428 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500429 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500430 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500431
432 cmd->prp1 = cpu_to_le64(dma_addr);
433 length -= (PAGE_SIZE - offset);
434 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500435 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500436
437 dma_len -= (PAGE_SIZE - offset);
438 if (dma_len) {
439 dma_addr += (PAGE_SIZE - offset);
440 } else {
441 sg = sg_next(sg);
442 dma_addr = sg_dma_address(sg);
443 dma_len = sg_dma_len(sg);
444 }
445
446 if (length <= PAGE_SIZE) {
447 cmd->prp2 = cpu_to_le64(dma_addr);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500448 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500449 }
450
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500451 nprps = DIV_ROUND_UP(length, PAGE_SIZE);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500452 if (nprps <= (256 / 8)) {
453 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500454 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500455 } else {
456 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500457 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500458 }
459
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400460 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
461 if (!prp_list) {
462 cmd->prp2 = cpu_to_le64(dma_addr);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500463 iod->npages = -1;
464 return (total_len - length) + PAGE_SIZE;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400465 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500466 list[0] = prp_list;
467 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500468 cmd->prp2 = cpu_to_le64(prp_dma);
469 i = 0;
470 for (;;) {
Matthew Wilcox7523d832011-03-16 16:43:40 -0400471 if (i == PAGE_SIZE / 8) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500472 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400473 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500474 if (!prp_list)
475 return total_len - length;
476 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400477 prp_list[0] = old_prp_list[i - 1];
478 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
479 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500480 }
481 prp_list[i++] = cpu_to_le64(dma_addr);
482 dma_len -= PAGE_SIZE;
483 dma_addr += PAGE_SIZE;
484 length -= PAGE_SIZE;
485 if (length <= 0)
486 break;
487 if (dma_len > 0)
488 continue;
489 BUG_ON(dma_len < 0);
490 sg = sg_next(sg);
491 dma_addr = sg_dma_address(sg);
492 dma_len = sg_dma_len(sg);
493 }
494
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500495 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500496}
497
Keith Busch427e9702013-04-09 11:59:32 -0600498static int nvme_split_and_submit(struct bio *bio, struct nvme_queue *nvmeq,
Kent Overstreet20d01892013-11-23 18:21:01 -0800499 int len)
Keith Busch427e9702013-04-09 11:59:32 -0600500{
Kent Overstreet20d01892013-11-23 18:21:01 -0800501 struct bio *split = bio_split(bio, len >> 9, GFP_ATOMIC, NULL);
502 if (!split)
Keith Busch427e9702013-04-09 11:59:32 -0600503 return -ENOMEM;
504
Kent Overstreet20d01892013-11-23 18:21:01 -0800505 bio_chain(split, bio);
506
Keith Busch427e9702013-04-09 11:59:32 -0600507 if (bio_list_empty(&nvmeq->sq_cong))
508 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
Kent Overstreet20d01892013-11-23 18:21:01 -0800509 bio_list_add(&nvmeq->sq_cong, split);
510 bio_list_add(&nvmeq->sq_cong, bio);
Keith Busch427e9702013-04-09 11:59:32 -0600511
512 return 0;
513}
514
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500515/* NVMe scatterlists require no holes in the virtual address */
516#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
517 (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
518
Keith Busch427e9702013-04-09 11:59:32 -0600519static int nvme_map_bio(struct nvme_queue *nvmeq, struct nvme_iod *iod,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500520 struct bio *bio, enum dma_data_direction dma_dir, int psegs)
521{
Kent Overstreet79886132013-11-23 17:19:00 -0800522 struct bio_vec bvec, bvprv;
523 struct bvec_iter iter;
Matthew Wilcox76830842011-02-10 13:55:39 -0500524 struct scatterlist *sg = NULL;
Kent Overstreet79886132013-11-23 17:19:00 -0800525 int length = 0, nsegs = 0, split_len = bio->bi_iter.bi_size;
526 int first = 1;
Keith Busch159b67d2013-04-09 17:13:20 -0600527
528 if (nvmeq->dev->stripe_size)
529 split_len = nvmeq->dev->stripe_size -
Kent Overstreet4f024f32013-10-11 15:44:27 -0700530 ((bio->bi_iter.bi_sector << 9) &
531 (nvmeq->dev->stripe_size - 1));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500532
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500533 sg_init_table(iod->sg, psegs);
Kent Overstreet79886132013-11-23 17:19:00 -0800534 bio_for_each_segment(bvec, bio, iter) {
535 if (!first && BIOVEC_PHYS_MERGEABLE(&bvprv, &bvec)) {
536 sg->length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500537 } else {
Kent Overstreet79886132013-11-23 17:19:00 -0800538 if (!first && BIOVEC_NOT_VIRT_MERGEABLE(&bvprv, &bvec))
539 return nvme_split_and_submit(bio, nvmeq,
Kent Overstreet20d01892013-11-23 18:21:01 -0800540 length);
Keith Busch427e9702013-04-09 11:59:32 -0600541
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500542 sg = sg ? sg + 1 : iod->sg;
Kent Overstreet79886132013-11-23 17:19:00 -0800543 sg_set_page(sg, bvec.bv_page,
544 bvec.bv_len, bvec.bv_offset);
Matthew Wilcox76830842011-02-10 13:55:39 -0500545 nsegs++;
546 }
Keith Busch159b67d2013-04-09 17:13:20 -0600547
Kent Overstreet79886132013-11-23 17:19:00 -0800548 if (split_len - length < bvec.bv_len)
Kent Overstreet20d01892013-11-23 18:21:01 -0800549 return nvme_split_and_submit(bio, nvmeq, split_len);
Kent Overstreet79886132013-11-23 17:19:00 -0800550 length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500551 bvprv = bvec;
Kent Overstreet79886132013-11-23 17:19:00 -0800552 first = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500553 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500554 iod->nents = nsegs;
Matthew Wilcox76830842011-02-10 13:55:39 -0500555 sg_mark_end(sg);
Keith Busch427e9702013-04-09 11:59:32 -0600556 if (dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir) == 0)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500557 return -ENOMEM;
Keith Busch427e9702013-04-09 11:59:32 -0600558
Kent Overstreet4f024f32013-10-11 15:44:27 -0700559 BUG_ON(length != bio->bi_iter.bi_size);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500560 return length;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500561}
562
Keith Busch0e5e4f02012-11-09 16:33:05 -0700563/*
564 * We reuse the small pool to allocate the 16-byte range here as it is not
565 * worth having a special pool for these or additional cases to handle freeing
566 * the iod.
567 */
568static int nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
569 struct bio *bio, struct nvme_iod *iod, int cmdid)
570{
571 struct nvme_dsm_range *range;
572 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
573
574 range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC,
575 &iod->first_dma);
576 if (!range)
577 return -ENOMEM;
578
579 iod_list(iod)[0] = (__le64 *)range;
580 iod->npages = 0;
581
582 range->cattr = cpu_to_le32(0);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700583 range->nlb = cpu_to_le32(bio->bi_iter.bi_size >> ns->lba_shift);
584 range->slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700585
586 memset(cmnd, 0, sizeof(*cmnd));
587 cmnd->dsm.opcode = nvme_cmd_dsm;
588 cmnd->dsm.command_id = cmdid;
589 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
590 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
591 cmnd->dsm.nr = 0;
592 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
593
594 if (++nvmeq->sq_tail == nvmeq->q_depth)
595 nvmeq->sq_tail = 0;
596 writel(nvmeq->sq_tail, nvmeq->q_db);
597
598 return 0;
599}
600
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500601static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
602 int cmdid)
603{
604 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
605
606 memset(cmnd, 0, sizeof(*cmnd));
607 cmnd->common.opcode = nvme_cmd_flush;
608 cmnd->common.command_id = cmdid;
609 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
610
611 if (++nvmeq->sq_tail == nvmeq->q_depth)
612 nvmeq->sq_tail = 0;
613 writel(nvmeq->sq_tail, nvmeq->q_db);
614
615 return 0;
616}
617
Vishal Verma5d0f6132013-03-04 18:40:58 -0700618int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500619{
620 int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH,
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500621 special_completion, NVME_IO_TIMEOUT);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500622 if (unlikely(cmdid < 0))
623 return cmdid;
624
625 return nvme_submit_flush(nvmeq, ns, cmdid);
626}
627
Matthew Wilcox184d2942011-05-11 21:36:38 -0400628/*
629 * Called with local interrupts disabled and the q_lock held. May not sleep.
630 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500631static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
632 struct bio *bio)
633{
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500634 struct nvme_command *cmnd;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500635 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500636 enum dma_data_direction dma_dir;
Wei Yongjun1287dab2013-05-13 22:29:04 +0800637 int cmdid, length, result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500638 u16 control;
639 u32 dsmgmt;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500640 int psegs = bio_phys_segments(ns->queue, bio);
641
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500642 if ((bio->bi_rw & REQ_FLUSH) && psegs) {
643 result = nvme_submit_flush_data(nvmeq, ns);
644 if (result)
645 return result;
646 }
647
Wei Yongjun1287dab2013-05-13 22:29:04 +0800648 result = -ENOMEM;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700649 iod = nvme_alloc_iod(psegs, bio->bi_iter.bi_size, GFP_ATOMIC);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500650 if (!iod)
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500651 goto nomem;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500652 iod->private = bio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500653
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500654 result = -EBUSY;
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500655 cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500656 if (unlikely(cmdid < 0))
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500657 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500658
Keith Busch0e5e4f02012-11-09 16:33:05 -0700659 if (bio->bi_rw & REQ_DISCARD) {
660 result = nvme_submit_discard(nvmeq, ns, bio, iod, cmdid);
661 if (result)
662 goto free_cmdid;
663 return result;
664 }
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500665 if ((bio->bi_rw & REQ_FLUSH) && !psegs)
666 return nvme_submit_flush(nvmeq, ns, cmdid);
667
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500668 control = 0;
669 if (bio->bi_rw & REQ_FUA)
670 control |= NVME_RW_FUA;
671 if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
672 control |= NVME_RW_LR;
673
674 dsmgmt = 0;
675 if (bio->bi_rw & REQ_RAHEAD)
676 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
677
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500678 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500679
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500680 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500681 if (bio_data_dir(bio)) {
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500682 cmnd->rw.opcode = nvme_cmd_write;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500683 dma_dir = DMA_TO_DEVICE;
684 } else {
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500685 cmnd->rw.opcode = nvme_cmd_read;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500686 dma_dir = DMA_FROM_DEVICE;
687 }
688
Keith Busch427e9702013-04-09 11:59:32 -0600689 result = nvme_map_bio(nvmeq, iod, bio, dma_dir, psegs);
690 if (result <= 0)
Keith Busch859361a2012-08-02 14:05:59 -0600691 goto free_cmdid;
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500692 length = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500693
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500694 cmnd->rw.command_id = cmdid;
695 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500696 length = nvme_setup_prps(nvmeq->dev, &cmnd->common, iod, length,
697 GFP_ATOMIC);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700698 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500699 cmnd->rw.length = cpu_to_le16((length >> ns->lba_shift) - 1);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500700 cmnd->rw.control = cpu_to_le16(control);
701 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500702
Keith Busch61982212013-05-29 15:59:39 -0600703 nvme_start_io_acct(bio);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500704 if (++nvmeq->sq_tail == nvmeq->q_depth)
705 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500706 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500707
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500708 return 0;
709
Keith Busch859361a2012-08-02 14:05:59 -0600710 free_cmdid:
711 free_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500712 free_iod:
713 nvme_free_iod(nvmeq->dev, iod);
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500714 nomem:
715 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500716}
717
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400718static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500719{
Matthew Wilcox82123462011-01-20 13:24:06 -0500720 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500721
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500722 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500723 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500724
725 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400726 void *ctx;
727 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500728 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500729 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500730 break;
731 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
732 if (++head == nvmeq->q_depth) {
733 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500734 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500735 }
736
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400737 ctx = free_cmdid(nvmeq, cqe.command_id, &fn);
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500738 fn(nvmeq->dev, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500739 }
740
741 /* If the controller ignores the cq head doorbell and continuously
742 * writes to the queue, it is theoretically possible to wrap around
743 * the queue twice and mistakenly return IRQ_NONE. Linux only
744 * requires that 0.1% of your interrupts are handled, so this isn't
745 * a big problem.
746 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500747 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400748 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500749
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800750 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500751 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500752 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500753
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400754 nvmeq->cqe_seen = 1;
755 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500756}
757
Matthew Wilcox7d822452013-06-24 12:03:57 -0400758static void nvme_make_request(struct request_queue *q, struct bio *bio)
759{
760 struct nvme_ns *ns = q->queuedata;
761 struct nvme_queue *nvmeq = get_nvmeq(ns->dev);
762 int result = -EBUSY;
763
Keith Buschcd638942013-07-15 15:02:23 -0600764 if (!nvmeq) {
765 put_nvmeq(NULL);
766 bio_endio(bio, -EIO);
767 return;
768 }
769
Matthew Wilcox7d822452013-06-24 12:03:57 -0400770 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -0600771 if (!nvmeq->q_suspended && bio_list_empty(&nvmeq->sq_cong))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400772 result = nvme_submit_bio_queue(nvmeq, ns, bio);
773 if (unlikely(result)) {
774 if (bio_list_empty(&nvmeq->sq_cong))
775 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
776 bio_list_add(&nvmeq->sq_cong, bio);
777 }
778
779 nvme_process_cq(nvmeq);
780 spin_unlock_irq(&nvmeq->q_lock);
781 put_nvmeq(nvmeq);
782}
783
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500784static irqreturn_t nvme_irq(int irq, void *data)
785{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500786 irqreturn_t result;
787 struct nvme_queue *nvmeq = data;
788 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400789 nvme_process_cq(nvmeq);
790 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
791 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500792 spin_unlock(&nvmeq->q_lock);
793 return result;
794}
795
796static irqreturn_t nvme_irq_check(int irq, void *data)
797{
798 struct nvme_queue *nvmeq = data;
799 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
800 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
801 return IRQ_NONE;
802 return IRQ_WAKE_THREAD;
803}
804
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500805static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
806{
807 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400808 cancel_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500809 spin_unlock_irq(&nvmeq->q_lock);
810}
811
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400812struct sync_cmd_info {
813 struct task_struct *task;
814 u32 result;
815 int status;
816};
817
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500818static void sync_completion(struct nvme_dev *dev, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400819 struct nvme_completion *cqe)
820{
821 struct sync_cmd_info *cmdinfo = ctx;
822 cmdinfo->result = le32_to_cpup(&cqe->result);
823 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
824 wake_up_process(cmdinfo->task);
825}
826
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500827/*
828 * Returns 0 on success. If the result is negative, it's a Linux error code;
829 * if the result is positive, it's an NVM Express status code
830 */
Keith Busch4f5099a2014-03-03 16:39:13 -0700831static int nvme_submit_sync_cmd(struct nvme_dev *dev, int q_idx,
832 struct nvme_command *cmd,
Vishal Verma5d0f6132013-03-04 18:40:58 -0700833 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500834{
Keith Busch4f5099a2014-03-03 16:39:13 -0700835 int cmdid, ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500836 struct sync_cmd_info cmdinfo;
Keith Busch4f5099a2014-03-03 16:39:13 -0700837 struct nvme_queue *nvmeq;
838
839 nvmeq = lock_nvmeq(dev, q_idx);
840 if (!nvmeq) {
841 unlock_nvmeq(nvmeq);
842 return -ENODEV;
843 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500844
845 cmdinfo.task = current;
846 cmdinfo.status = -EINTR;
847
Keith Busch4f5099a2014-03-03 16:39:13 -0700848 cmdid = alloc_cmdid(nvmeq, &cmdinfo, sync_completion, timeout);
849 if (cmdid < 0) {
850 unlock_nvmeq(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500851 return cmdid;
Keith Busch4f5099a2014-03-03 16:39:13 -0700852 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500853 cmd->common.command_id = cmdid;
854
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500855 set_current_state(TASK_KILLABLE);
Keith Busch4f5099a2014-03-03 16:39:13 -0700856 ret = nvme_submit_cmd(nvmeq, cmd);
857 if (ret) {
858 free_cmdid(nvmeq, cmdid, NULL);
859 unlock_nvmeq(nvmeq);
860 set_current_state(TASK_RUNNING);
861 return ret;
862 }
863 unlock_nvmeq(nvmeq);
Keith Busch78f8d252013-04-19 14:11:06 -0600864 schedule_timeout(timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500865
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500866 if (cmdinfo.status == -EINTR) {
Keith Busch4f5099a2014-03-03 16:39:13 -0700867 nvmeq = lock_nvmeq(dev, q_idx);
868 if (nvmeq)
869 nvme_abort_command(nvmeq, cmdid);
870 unlock_nvmeq(nvmeq);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500871 return -EINTR;
872 }
873
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500874 if (result)
875 *result = cmdinfo.result;
876
877 return cmdinfo.status;
878}
879
Keith Busch4d115422013-12-10 13:10:40 -0700880static int nvme_submit_async_cmd(struct nvme_queue *nvmeq,
881 struct nvme_command *cmd,
882 struct async_cmd_info *cmdinfo, unsigned timeout)
883{
884 int cmdid;
885
886 cmdid = alloc_cmdid_killable(nvmeq, cmdinfo, async_completion, timeout);
887 if (cmdid < 0)
888 return cmdid;
889 cmdinfo->status = -EINTR;
890 cmd->common.command_id = cmdid;
Keith Busch4f5099a2014-03-03 16:39:13 -0700891 return nvme_submit_cmd(nvmeq, cmd);
Keith Busch4d115422013-12-10 13:10:40 -0700892}
893
Vishal Verma5d0f6132013-03-04 18:40:58 -0700894int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500895 u32 *result)
896{
Keith Busch4f5099a2014-03-03 16:39:13 -0700897 return nvme_submit_sync_cmd(dev, 0, cmd, result, ADMIN_TIMEOUT);
898}
899
900int nvme_submit_io_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
901 u32 *result)
902{
903 return nvme_submit_sync_cmd(dev, smp_processor_id() + 1, cmd, result,
904 NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500905}
906
Keith Busch4d115422013-12-10 13:10:40 -0700907static int nvme_submit_admin_cmd_async(struct nvme_dev *dev,
908 struct nvme_command *cmd, struct async_cmd_info *cmdinfo)
909{
Keith Busch5a92e702014-02-21 14:13:44 -0700910 return nvme_submit_async_cmd(raw_nvmeq(dev, 0), cmd, cmdinfo,
Keith Busch4d115422013-12-10 13:10:40 -0700911 ADMIN_TIMEOUT);
912}
913
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500914static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
915{
916 int status;
917 struct nvme_command c;
918
919 memset(&c, 0, sizeof(c));
920 c.delete_queue.opcode = opcode;
921 c.delete_queue.qid = cpu_to_le16(id);
922
923 status = nvme_submit_admin_cmd(dev, &c, NULL);
924 if (status)
925 return -EIO;
926 return 0;
927}
928
929static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
930 struct nvme_queue *nvmeq)
931{
932 int status;
933 struct nvme_command c;
934 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
935
936 memset(&c, 0, sizeof(c));
937 c.create_cq.opcode = nvme_admin_create_cq;
938 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
939 c.create_cq.cqid = cpu_to_le16(qid);
940 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
941 c.create_cq.cq_flags = cpu_to_le16(flags);
942 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
943
944 status = nvme_submit_admin_cmd(dev, &c, NULL);
945 if (status)
946 return -EIO;
947 return 0;
948}
949
950static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
951 struct nvme_queue *nvmeq)
952{
953 int status;
954 struct nvme_command c;
955 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
956
957 memset(&c, 0, sizeof(c));
958 c.create_sq.opcode = nvme_admin_create_sq;
959 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
960 c.create_sq.sqid = cpu_to_le16(qid);
961 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
962 c.create_sq.sq_flags = cpu_to_le16(flags);
963 c.create_sq.cqid = cpu_to_le16(qid);
964
965 status = nvme_submit_admin_cmd(dev, &c, NULL);
966 if (status)
967 return -EIO;
968 return 0;
969}
970
971static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
972{
973 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
974}
975
976static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
977{
978 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
979}
980
Vishal Verma5d0f6132013-03-04 18:40:58 -0700981int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400982 dma_addr_t dma_addr)
983{
984 struct nvme_command c;
985
986 memset(&c, 0, sizeof(c));
987 c.identify.opcode = nvme_admin_identify;
988 c.identify.nsid = cpu_to_le32(nsid);
989 c.identify.prp1 = cpu_to_le64(dma_addr);
990 c.identify.cns = cpu_to_le32(cns);
991
992 return nvme_submit_admin_cmd(dev, &c, NULL);
993}
994
Vishal Verma5d0f6132013-03-04 18:40:58 -0700995int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -0600996 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400997{
998 struct nvme_command c;
999
1000 memset(&c, 0, sizeof(c));
1001 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -06001002 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001003 c.features.prp1 = cpu_to_le64(dma_addr);
1004 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001005
Keith Busch08df1e02012-09-21 10:52:13 -06001006 return nvme_submit_admin_cmd(dev, &c, result);
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001007}
1008
Vishal Verma5d0f6132013-03-04 18:40:58 -07001009int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
1010 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001011{
1012 struct nvme_command c;
1013
1014 memset(&c, 0, sizeof(c));
1015 c.features.opcode = nvme_admin_set_features;
1016 c.features.prp1 = cpu_to_le64(dma_addr);
1017 c.features.fid = cpu_to_le32(fid);
1018 c.features.dword11 = cpu_to_le32(dword11);
1019
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001020 return nvme_submit_admin_cmd(dev, &c, result);
1021}
1022
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001023/**
Keith Buschc30341d2013-12-10 13:10:38 -07001024 * nvme_abort_cmd - Attempt aborting a command
1025 * @cmdid: Command id of a timed out IO
1026 * @queue: The queue with timed out IO
1027 *
1028 * Schedule controller reset if the command was already aborted once before and
1029 * still hasn't been returned to the driver, or if this is the admin queue.
1030 */
1031static void nvme_abort_cmd(int cmdid, struct nvme_queue *nvmeq)
1032{
1033 int a_cmdid;
1034 struct nvme_command cmd;
1035 struct nvme_dev *dev = nvmeq->dev;
1036 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Keith Busch5a92e702014-02-21 14:13:44 -07001037 struct nvme_queue *adminq;
Keith Buschc30341d2013-12-10 13:10:38 -07001038
1039 if (!nvmeq->qid || info[cmdid].aborted) {
1040 if (work_busy(&dev->reset_work))
1041 return;
1042 list_del_init(&dev->node);
1043 dev_warn(&dev->pci_dev->dev,
1044 "I/O %d QID %d timeout, reset controller\n", cmdid,
1045 nvmeq->qid);
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05001046 PREPARE_WORK(&dev->reset_work, nvme_reset_failed_dev);
Keith Buschc30341d2013-12-10 13:10:38 -07001047 queue_work(nvme_workq, &dev->reset_work);
1048 return;
1049 }
1050
1051 if (!dev->abort_limit)
1052 return;
1053
Keith Busch5a92e702014-02-21 14:13:44 -07001054 adminq = rcu_dereference(dev->queues[0]);
1055 a_cmdid = alloc_cmdid(adminq, CMD_CTX_ABORT, special_completion,
Keith Buschc30341d2013-12-10 13:10:38 -07001056 ADMIN_TIMEOUT);
1057 if (a_cmdid < 0)
1058 return;
1059
1060 memset(&cmd, 0, sizeof(cmd));
1061 cmd.abort.opcode = nvme_admin_abort_cmd;
1062 cmd.abort.cid = cmdid;
1063 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
1064 cmd.abort.command_id = a_cmdid;
1065
1066 --dev->abort_limit;
1067 info[cmdid].aborted = 1;
1068 info[cmdid].timeout = jiffies + ADMIN_TIMEOUT;
1069
1070 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", cmdid,
1071 nvmeq->qid);
Keith Busch5a92e702014-02-21 14:13:44 -07001072 nvme_submit_cmd(adminq, &cmd);
Keith Buschc30341d2013-12-10 13:10:38 -07001073}
1074
1075/**
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001076 * nvme_cancel_ios - Cancel outstanding I/Os
1077 * @queue: The queue to cancel I/Os on
1078 * @timeout: True to only cancel I/Os which have timed out
1079 */
1080static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout)
1081{
1082 int depth = nvmeq->q_depth - 1;
1083 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
1084 unsigned long now = jiffies;
1085 int cmdid;
1086
1087 for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
1088 void *ctx;
1089 nvme_completion_fn fn;
1090 static struct nvme_completion cqe = {
Matthew Wilcoxaf2d9ca2013-04-16 15:18:30 -04001091 .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1),
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001092 };
1093
1094 if (timeout && !time_after(now, info[cmdid].timeout))
1095 continue;
Keith Busch053ab7022013-04-30 11:19:38 -06001096 if (info[cmdid].ctx == CMD_CTX_CANCELLED)
1097 continue;
Keith Buschc30341d2013-12-10 13:10:38 -07001098 if (timeout && nvmeq->dev->initialized) {
1099 nvme_abort_cmd(cmdid, nvmeq);
1100 continue;
1101 }
1102 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n", cmdid,
1103 nvmeq->qid);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001104 ctx = cancel_cmdid(nvmeq, cmdid, &fn);
1105 fn(nvmeq->dev, ctx, &cqe);
1106 }
1107}
1108
Keith Busch5a92e702014-02-21 14:13:44 -07001109static void nvme_free_queue(struct rcu_head *r)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001110{
Keith Busch5a92e702014-02-21 14:13:44 -07001111 struct nvme_queue *nvmeq = container_of(r, struct nvme_queue, r_head);
1112
Keith Busch22404272013-07-15 15:02:20 -06001113 spin_lock_irq(&nvmeq->q_lock);
1114 while (bio_list_peek(&nvmeq->sq_cong)) {
1115 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1116 bio_endio(bio, -EIO);
1117 }
1118 spin_unlock_irq(&nvmeq->q_lock);
1119
Matthew Wilcox9e866772012-08-03 13:55:56 -04001120 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1121 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1122 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1123 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1124 kfree(nvmeq);
1125}
1126
Keith Buscha1a5ef92013-12-16 13:50:00 -05001127static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001128{
1129 int i;
1130
Keith Busch5a92e702014-02-21 14:13:44 -07001131 for (i = num_possible_cpus(); i > dev->queue_count - 1; i--)
1132 rcu_assign_pointer(dev->queues[i], NULL);
Keith Buscha1a5ef92013-12-16 13:50:00 -05001133 for (i = dev->queue_count - 1; i >= lowest; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07001134 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
1135 rcu_assign_pointer(dev->queues[i], NULL);
1136 call_rcu(&nvmeq->r_head, nvme_free_queue);
Keith Busch22404272013-07-15 15:02:20 -06001137 dev->queue_count--;
Keith Busch22404272013-07-15 15:02:20 -06001138 }
1139}
1140
Keith Busch4d115422013-12-10 13:10:40 -07001141/**
1142 * nvme_suspend_queue - put queue into suspended state
1143 * @nvmeq - queue to suspend
1144 *
1145 * Returns 1 if already suspended, 0 otherwise.
1146 */
1147static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001148{
Keith Busch4d115422013-12-10 13:10:40 -07001149 int vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001150
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001151 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001152 if (nvmeq->q_suspended) {
1153 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07001154 return 1;
Keith Busch32958742012-08-20 14:57:49 -06001155 }
Keith Busch22404272013-07-15 15:02:20 -06001156 nvmeq->q_suspended = 1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001157 spin_unlock_irq(&nvmeq->q_lock);
1158
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001159 irq_set_affinity_hint(vector, NULL);
1160 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001161
Keith Busch4d115422013-12-10 13:10:40 -07001162 return 0;
1163}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001164
Keith Busch4d115422013-12-10 13:10:40 -07001165static void nvme_clear_queue(struct nvme_queue *nvmeq)
1166{
Keith Busch22404272013-07-15 15:02:20 -06001167 spin_lock_irq(&nvmeq->q_lock);
1168 nvme_process_cq(nvmeq);
1169 nvme_cancel_ios(nvmeq, false);
1170 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001171}
1172
Keith Busch4d115422013-12-10 13:10:40 -07001173static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1174{
Keith Busch5a92e702014-02-21 14:13:44 -07001175 struct nvme_queue *nvmeq = raw_nvmeq(dev, qid);
Keith Busch4d115422013-12-10 13:10:40 -07001176
1177 if (!nvmeq)
1178 return;
1179 if (nvme_suspend_queue(nvmeq))
1180 return;
1181
Keith Busch0e53d182013-12-10 13:10:39 -07001182 /* Don't tell the adapter to delete the admin queue.
1183 * Don't tell a removed adapter to delete IO queues. */
1184 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001185 adapter_delete_sq(dev, qid);
1186 adapter_delete_cq(dev, qid);
1187 }
Keith Busch4d115422013-12-10 13:10:40 -07001188 nvme_clear_queue(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001189}
1190
1191static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
1192 int depth, int vector)
1193{
1194 struct device *dmadev = &dev->pci_dev->dev;
Keith Busch22404272013-07-15 15:02:20 -06001195 unsigned extra = nvme_queue_extra(depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001196 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
1197 if (!nvmeq)
1198 return NULL;
1199
1200 nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
1201 &nvmeq->cq_dma_addr, GFP_KERNEL);
1202 if (!nvmeq->cqes)
1203 goto free_nvmeq;
1204 memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
1205
1206 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
1207 &nvmeq->sq_dma_addr, GFP_KERNEL);
1208 if (!nvmeq->sq_cmds)
1209 goto free_cqdma;
1210
1211 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001212 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001213 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1214 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001215 spin_lock_init(&nvmeq->q_lock);
1216 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001217 nvmeq->cq_phase = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001218 init_waitqueue_head(&nvmeq->sq_full);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001219 init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001220 bio_list_init(&nvmeq->sq_cong);
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001221 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001222 nvmeq->q_depth = depth;
1223 nvmeq->cq_vector = vector;
Keith Buschc30341d2013-12-10 13:10:38 -07001224 nvmeq->qid = qid;
Keith Busch22404272013-07-15 15:02:20 -06001225 nvmeq->q_suspended = 1;
1226 dev->queue_count++;
Keith Busch5a92e702014-02-21 14:13:44 -07001227 rcu_assign_pointer(dev->queues[qid], nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001228
1229 return nvmeq;
1230
1231 free_cqdma:
Keith Busch68b8eca2013-05-01 13:07:47 -06001232 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001233 nvmeq->cq_dma_addr);
1234 free_nvmeq:
1235 kfree(nvmeq);
1236 return NULL;
1237}
1238
Matthew Wilcox30010822011-01-20 09:10:15 -05001239static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1240 const char *name)
1241{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001242 if (use_threaded_interrupts)
1243 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001244 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001245 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001246 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001247 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001248}
1249
Keith Busch22404272013-07-15 15:02:20 -06001250static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001251{
Keith Busch22404272013-07-15 15:02:20 -06001252 struct nvme_dev *dev = nvmeq->dev;
1253 unsigned extra = nvme_queue_extra(nvmeq->q_depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001254
Keith Busch22404272013-07-15 15:02:20 -06001255 nvmeq->sq_tail = 0;
1256 nvmeq->cq_head = 0;
1257 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001258 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001259 memset(nvmeq->cmdid_data, 0, extra);
1260 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
1261 nvme_cancel_ios(nvmeq, false);
1262 nvmeq->q_suspended = 0;
1263}
1264
1265static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1266{
1267 struct nvme_dev *dev = nvmeq->dev;
1268 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001269
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001270 result = adapter_alloc_cq(dev, qid, nvmeq);
1271 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001272 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001273
1274 result = adapter_alloc_sq(dev, qid, nvmeq);
1275 if (result < 0)
1276 goto release_cq;
1277
Matthew Wilcox3193f072014-01-27 15:57:22 -05001278 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001279 if (result < 0)
1280 goto release_sq;
1281
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001282 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001283 nvme_init_queue(nvmeq, qid);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001284 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001285
1286 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001287
1288 release_sq:
1289 adapter_delete_sq(dev, qid);
1290 release_cq:
1291 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001292 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001293}
1294
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001295static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1296{
1297 unsigned long timeout;
1298 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1299
1300 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1301
1302 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1303 msleep(100);
1304 if (fatal_signal_pending(current))
1305 return -EINTR;
1306 if (time_after(jiffies, timeout)) {
1307 dev_err(&dev->pci_dev->dev,
1308 "Device not ready; aborting initialisation\n");
1309 return -ENODEV;
1310 }
1311 }
1312
1313 return 0;
1314}
1315
1316/*
1317 * If the device has been passed off to us in an enabled state, just clear
1318 * the enabled bit. The spec says we should set the 'shutdown notification
1319 * bits', but doing so may cause the device to complete commands to the
1320 * admin queue ... and we don't know what memory that might be pointing at!
1321 */
1322static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1323{
Matthew Wilcox44af1462013-05-04 06:43:17 -04001324 u32 cc = readl(&dev->bar->cc);
1325
1326 if (cc & NVME_CC_ENABLE)
1327 writel(cc & ~NVME_CC_ENABLE, &dev->bar->cc);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001328 return nvme_wait_ready(dev, cap, false);
1329}
1330
1331static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1332{
1333 return nvme_wait_ready(dev, cap, true);
1334}
1335
Keith Busch1894d8f2013-07-15 15:02:22 -06001336static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1337{
1338 unsigned long timeout;
1339 u32 cc;
1340
1341 cc = (readl(&dev->bar->cc) & ~NVME_CC_SHN_MASK) | NVME_CC_SHN_NORMAL;
1342 writel(cc, &dev->bar->cc);
1343
1344 timeout = 2 * HZ + jiffies;
1345 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1346 NVME_CSTS_SHST_CMPLT) {
1347 msleep(100);
1348 if (fatal_signal_pending(current))
1349 return -EINTR;
1350 if (time_after(jiffies, timeout)) {
1351 dev_err(&dev->pci_dev->dev,
1352 "Device shutdown incomplete; abort shutdown\n");
1353 return -ENODEV;
1354 }
1355 }
1356
1357 return 0;
1358}
1359
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001360static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001361{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001362 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001363 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001364 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001365 struct nvme_queue *nvmeq;
1366
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001367 result = nvme_disable_ctrl(dev, cap);
1368 if (result < 0)
1369 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001370
Keith Busch5a92e702014-02-21 14:13:44 -07001371 nvmeq = raw_nvmeq(dev, 0);
Keith Buschcd638942013-07-15 15:02:23 -06001372 if (!nvmeq) {
1373 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
1374 if (!nvmeq)
1375 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001376 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001377
1378 aqa = nvmeq->q_depth - 1;
1379 aqa |= aqa << 16;
1380
1381 dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
1382 dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
1383 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001384 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001385
1386 writel(aqa, &dev->bar->aqa);
1387 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1388 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
1389 writel(dev->ctrl_config, &dev->bar->cc);
1390
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001391 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001392 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001393 return result;
Matthew Wilcox9e866772012-08-03 13:55:56 -04001394
Matthew Wilcox3193f072014-01-27 15:57:22 -05001395 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Keith Busch025c5572013-05-01 13:07:51 -06001396 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001397 return result;
Keith Busch025c5572013-05-01 13:07:51 -06001398
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001399 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001400 nvme_init_queue(nvmeq, 0);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001401 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001402 return result;
1403}
1404
Vishal Verma5d0f6132013-03-04 18:40:58 -07001405struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001406 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001407{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001408 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001409 struct scatterlist *sg;
1410 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001411 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001412
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001413 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001414 return ERR_PTR(-EINVAL);
Dan Carpenter5460fc02013-05-13 17:59:50 +03001415 if (!length || length > INT_MAX - PAGE_SIZE)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001416 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001417
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001418 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001419 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1420 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001421 if (!pages)
1422 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001423
1424 err = get_user_pages_fast(addr, count, 1, pages);
1425 if (err < count) {
1426 count = err;
1427 err = -EFAULT;
1428 goto put_pages;
1429 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001430
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001431 iod = nvme_alloc_iod(count, length, GFP_KERNEL);
1432 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001433 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001434 for (i = 0; i < count; i++) {
1435 sg_set_page(&sg[i], pages[i],
Dan Carpenter5460fc02013-05-13 17:59:50 +03001436 min_t(unsigned, length, PAGE_SIZE - offset),
1437 offset);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001438 length -= (PAGE_SIZE - offset);
1439 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001440 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001441 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001442 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001443
1444 err = -ENOMEM;
1445 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1446 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001447 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001448 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001449
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001450 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001451 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001452
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001453 free_iod:
1454 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001455 put_pages:
1456 for (i = 0; i < count; i++)
1457 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001458 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001459 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001460}
1461
Vishal Verma5d0f6132013-03-04 18:40:58 -07001462void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001463 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001464{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001465 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001466
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001467 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1468 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001469
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001470 for (i = 0; i < iod->nents; i++)
1471 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001472}
1473
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001474static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1475{
1476 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001477 struct nvme_user_io io;
1478 struct nvme_command c;
Keith Buschf410c682013-04-23 17:23:59 -06001479 unsigned length, meta_len;
1480 int status, i;
1481 struct nvme_iod *iod, *meta_iod = NULL;
1482 dma_addr_t meta_dma_addr;
1483 void *meta, *uninitialized_var(meta_mem);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001484
1485 if (copy_from_user(&io, uio, sizeof(io)))
1486 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001487 length = (io.nblocks + 1) << ns->lba_shift;
Keith Buschf410c682013-04-23 17:23:59 -06001488 meta_len = (io.nblocks + 1) * ns->ms;
1489
1490 if (meta_len && ((io.metadata & 3) || !io.metadata))
1491 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001492
1493 switch (io.opcode) {
1494 case nvme_cmd_write:
1495 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001496 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001497 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001498 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001499 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001500 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001501 }
1502
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001503 if (IS_ERR(iod))
1504 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001505
1506 memset(&c, 0, sizeof(c));
1507 c.rw.opcode = io.opcode;
1508 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001509 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001510 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001511 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001512 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001513 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1514 c.rw.reftag = cpu_to_le32(io.reftag);
1515 c.rw.apptag = cpu_to_le16(io.apptag);
1516 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buschf410c682013-04-23 17:23:59 -06001517
1518 if (meta_len) {
Keith Busch1b567492013-07-18 12:13:51 -06001519 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
1520 meta_len);
Keith Buschf410c682013-04-23 17:23:59 -06001521 if (IS_ERR(meta_iod)) {
1522 status = PTR_ERR(meta_iod);
1523 meta_iod = NULL;
1524 goto unmap;
1525 }
1526
1527 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1528 &meta_dma_addr, GFP_KERNEL);
1529 if (!meta_mem) {
1530 status = -ENOMEM;
1531 goto unmap;
1532 }
1533
1534 if (io.opcode & 1) {
1535 int meta_offset = 0;
1536
1537 for (i = 0; i < meta_iod->nents; i++) {
1538 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1539 meta_iod->sg[i].offset;
1540 memcpy(meta_mem + meta_offset, meta,
1541 meta_iod->sg[i].length);
1542 kunmap_atomic(meta);
1543 meta_offset += meta_iod->sg[i].length;
1544 }
1545 }
1546
1547 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1548 }
1549
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001550 length = nvme_setup_prps(dev, &c.common, iod, length, GFP_KERNEL);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001551
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001552 if (length != (io.nblocks + 1) << ns->lba_shift)
1553 status = -ENOMEM;
1554 else
Keith Busch4f5099a2014-03-03 16:39:13 -07001555 status = nvme_submit_io_cmd(dev, &c, NULL);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001556
Keith Buschf410c682013-04-23 17:23:59 -06001557 if (meta_len) {
1558 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1559 int meta_offset = 0;
1560
1561 for (i = 0; i < meta_iod->nents; i++) {
1562 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1563 meta_iod->sg[i].offset;
1564 memcpy(meta, meta_mem + meta_offset,
1565 meta_iod->sg[i].length);
1566 kunmap_atomic(meta);
1567 meta_offset += meta_iod->sg[i].length;
1568 }
1569 }
1570
1571 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1572 meta_dma_addr);
1573 }
1574
1575 unmap:
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001576 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001577 nvme_free_iod(dev, iod);
Keith Buschf410c682013-04-23 17:23:59 -06001578
1579 if (meta_iod) {
1580 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1581 nvme_free_iod(dev, meta_iod);
1582 }
1583
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001584 return status;
1585}
1586
Keith Busch50af8ba2012-07-25 16:07:55 -06001587static int nvme_user_admin_cmd(struct nvme_dev *dev,
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001588 struct nvme_admin_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001589{
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001590 struct nvme_admin_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001591 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001592 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001593 struct nvme_iod *uninitialized_var(iod);
Keith Busch94f370c2013-05-09 14:01:38 -06001594 unsigned timeout;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001595
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001596 if (!capable(CAP_SYS_ADMIN))
1597 return -EACCES;
1598 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001599 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001600
1601 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001602 c.common.opcode = cmd.opcode;
1603 c.common.flags = cmd.flags;
1604 c.common.nsid = cpu_to_le32(cmd.nsid);
1605 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1606 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1607 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1608 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1609 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1610 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1611 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1612 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1613
1614 length = cmd.data_len;
1615 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001616 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1617 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001618 if (IS_ERR(iod))
1619 return PTR_ERR(iod);
1620 length = nvme_setup_prps(dev, &c.common, iod, length,
1621 GFP_KERNEL);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001622 }
1623
Keith Busch94f370c2013-05-09 14:01:38 -06001624 timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
1625 ADMIN_TIMEOUT;
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001626 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001627 status = -ENOMEM;
1628 else
Keith Busch4f5099a2014-03-03 16:39:13 -07001629 status = nvme_submit_sync_cmd(dev, 0, &c, &cmd.result, timeout);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001630
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001631 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001632 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001633 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001634 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001635
Chayan Biswascf90bc42013-05-22 22:34:49 +00001636 if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
Keith Buschf4f117f2012-09-21 10:49:05 -06001637 sizeof(cmd.result)))
1638 status = -EFAULT;
1639
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001640 return status;
1641}
1642
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001643static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1644 unsigned long arg)
1645{
1646 struct nvme_ns *ns = bdev->bd_disk->private_data;
1647
1648 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001649 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001650 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001651 return ns->ns_id;
1652 case NVME_IOCTL_ADMIN_CMD:
Keith Busch50af8ba2012-07-25 16:07:55 -06001653 return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001654 case NVME_IOCTL_SUBMIT_IO:
1655 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001656 case SG_GET_VERSION_NUM:
1657 return nvme_sg_get_version_num((void __user *)arg);
1658 case SG_IO:
1659 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001660 default:
1661 return -ENOTTY;
1662 }
1663}
1664
Keith Busch320a3822013-10-23 13:07:34 -06001665#ifdef CONFIG_COMPAT
1666static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1667 unsigned int cmd, unsigned long arg)
1668{
1669 struct nvme_ns *ns = bdev->bd_disk->private_data;
1670
1671 switch (cmd) {
1672 case SG_IO:
1673 return nvme_sg_io32(ns, arg);
1674 }
1675 return nvme_ioctl(bdev, mode, cmd, arg);
1676}
1677#else
1678#define nvme_compat_ioctl NULL
1679#endif
1680
Keith Busch9ac27092014-01-31 16:53:39 -07001681static int nvme_open(struct block_device *bdev, fmode_t mode)
1682{
1683 struct nvme_ns *ns = bdev->bd_disk->private_data;
1684 struct nvme_dev *dev = ns->dev;
1685
1686 kref_get(&dev->kref);
1687 return 0;
1688}
1689
1690static void nvme_free_dev(struct kref *kref);
1691
1692static void nvme_release(struct gendisk *disk, fmode_t mode)
1693{
1694 struct nvme_ns *ns = disk->private_data;
1695 struct nvme_dev *dev = ns->dev;
1696
1697 kref_put(&dev->kref, nvme_free_dev);
1698}
1699
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001700static const struct block_device_operations nvme_fops = {
1701 .owner = THIS_MODULE,
1702 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06001703 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07001704 .open = nvme_open,
1705 .release = nvme_release,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001706};
1707
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001708static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1709{
1710 while (bio_list_peek(&nvmeq->sq_cong)) {
1711 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1712 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Keith Busch427e9702013-04-09 11:59:32 -06001713
Matthew Wilcox3cb967c2011-03-16 16:45:49 -04001714 if (bio_list_empty(&nvmeq->sq_cong))
1715 remove_wait_queue(&nvmeq->sq_full,
1716 &nvmeq->sq_cong_wait);
Keith Busch427e9702013-04-09 11:59:32 -06001717 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
1718 if (bio_list_empty(&nvmeq->sq_cong))
1719 add_wait_queue(&nvmeq->sq_full,
1720 &nvmeq->sq_cong_wait);
1721 bio_list_add_head(&nvmeq->sq_cong, bio);
1722 break;
1723 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001724 }
1725}
1726
1727static int nvme_kthread(void *data)
1728{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001729 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001730
1731 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001732 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001733 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001734 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001735 int i;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001736 if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
1737 dev->initialized) {
1738 if (work_busy(&dev->reset_work))
1739 continue;
1740 list_del_init(&dev->node);
1741 dev_warn(&dev->pci_dev->dev,
1742 "Failed status, reset controller\n");
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05001743 PREPARE_WORK(&dev->reset_work,
Keith Buschd4b4ff82013-12-10 13:10:37 -07001744 nvme_reset_failed_dev);
1745 queue_work(nvme_workq, &dev->reset_work);
1746 continue;
1747 }
Keith Busch5a92e702014-02-21 14:13:44 -07001748 rcu_read_lock();
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001749 for (i = 0; i < dev->queue_count; i++) {
Keith Busch5a92e702014-02-21 14:13:44 -07001750 struct nvme_queue *nvmeq =
1751 rcu_dereference(dev->queues[i]);
Matthew Wilcox740216f2011-02-15 16:28:20 -05001752 if (!nvmeq)
1753 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001754 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001755 if (nvmeq->q_suspended)
1756 goto unlock;
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001757 nvme_process_cq(nvmeq);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001758 nvme_cancel_ios(nvmeq, true);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001759 nvme_resubmit_bios(nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001760 unlock:
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001761 spin_unlock_irq(&nvmeq->q_lock);
1762 }
Keith Busch5a92e702014-02-21 14:13:44 -07001763 rcu_read_unlock();
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001764 }
1765 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001766 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001767 }
1768 return 0;
1769}
1770
Keith Busch0e5e4f02012-11-09 16:33:05 -07001771static void nvme_config_discard(struct nvme_ns *ns)
1772{
1773 u32 logical_block_size = queue_logical_block_size(ns->queue);
1774 ns->queue->limits.discard_zeroes_data = 0;
1775 ns->queue->limits.discard_alignment = logical_block_size;
1776 ns->queue->limits.discard_granularity = logical_block_size;
1777 ns->queue->limits.max_discard_sectors = 0xffffffff;
1778 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1779}
1780
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001781static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001782 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1783{
1784 struct nvme_ns *ns;
1785 struct gendisk *disk;
1786 int lbaf;
1787
1788 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1789 return NULL;
1790
1791 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1792 if (!ns)
1793 return NULL;
1794 ns->queue = blk_alloc_queue(GFP_KERNEL);
1795 if (!ns->queue)
1796 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001797 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
1798 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1799 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001800 blk_queue_make_request(ns->queue, nvme_make_request);
1801 ns->dev = dev;
1802 ns->queue->queuedata = ns;
1803
Matthew Wilcox469071a2013-12-09 12:58:46 -05001804 disk = alloc_disk(0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001805 if (!disk)
1806 goto out_free_queue;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001807 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001808 ns->disk = disk;
1809 lbaf = id->flbas & 0xf;
1810 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Buschf410c682013-04-23 17:23:59 -06001811 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busche9ef4632012-07-24 15:01:04 -06001812 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06001813 if (dev->max_hw_sectors)
1814 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001815
1816 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001817 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001818 disk->fops = &nvme_fops;
1819 disk->private_data = ns;
1820 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05001821 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001822 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001823 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001824 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1825
Keith Busch0e5e4f02012-11-09 16:33:05 -07001826 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1827 nvme_config_discard(ns);
1828
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001829 return ns;
1830
1831 out_free_queue:
1832 blk_cleanup_queue(ns->queue);
1833 out_free_ns:
1834 kfree(ns);
1835 return NULL;
1836}
1837
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001838static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001839{
1840 int status;
1841 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001842 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001843
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001844 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001845 &result);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001846 if (status)
Keith Busch7e03b122013-07-29 16:20:56 -06001847 return status < 0 ? -EIO : -EBUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001848 return min(result & 0xffff, result >> 16) + 1;
1849}
1850
Keith Busch9d713c22013-07-15 15:02:24 -06001851static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1852{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001853 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06001854}
1855
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001856static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001857{
Keith Busch5a92e702014-02-21 14:13:44 -07001858 struct nvme_queue *adminq = raw_nvmeq(dev, 0);
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001859 struct pci_dev *pdev = dev->pci_dev;
Keith Busch9d713c22013-07-15 15:02:24 -06001860 int result, cpu, i, vecs, nr_io_queues, size, q_depth;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001861
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001862 nr_io_queues = num_online_cpus();
1863 result = set_queue_count(dev, nr_io_queues);
Matthew Wilcox1b234842011-01-20 13:01:49 -05001864 if (result < 0)
1865 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001866 if (result < nr_io_queues)
1867 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001868
Keith Busch9d713c22013-07-15 15:02:24 -06001869 size = db_bar_size(dev, nr_io_queues);
1870 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001871 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06001872 do {
1873 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1874 if (dev->bar)
1875 break;
1876 if (!--nr_io_queues)
1877 return -ENOMEM;
1878 size = db_bar_size(dev, nr_io_queues);
1879 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001880 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07001881 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001882 }
1883
Keith Busch9d713c22013-07-15 15:02:24 -06001884 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05001885 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06001886
Matthew Wilcox063a8092013-06-20 10:53:48 -04001887 vecs = nr_io_queues;
1888 for (i = 0; i < vecs; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001889 dev->entry[i].entry = i;
1890 for (;;) {
Matthew Wilcox063a8092013-06-20 10:53:48 -04001891 result = pci_enable_msix(pdev, dev->entry, vecs);
1892 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001893 break;
Matthew Wilcox063a8092013-06-20 10:53:48 -04001894 vecs = result;
1895 }
1896
1897 if (result < 0) {
1898 vecs = nr_io_queues;
1899 if (vecs > 32)
1900 vecs = 32;
1901 for (;;) {
1902 result = pci_enable_msi_block(pdev, vecs);
1903 if (result == 0) {
1904 for (i = 0; i < vecs; i++)
1905 dev->entry[i].vector = i + pdev->irq;
1906 break;
1907 } else if (result < 0) {
1908 vecs = 1;
1909 break;
1910 }
1911 vecs = result;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001912 }
1913 }
1914
Matthew Wilcox063a8092013-06-20 10:53:48 -04001915 /*
1916 * Should investigate if there's a performance win from allocating
1917 * more queues than interrupt vectors; it might allow the submission
1918 * path to scale better, even if the receive path is limited by the
1919 * number of interrupts.
1920 */
1921 nr_io_queues = vecs;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001922
Matthew Wilcox3193f072014-01-27 15:57:22 -05001923 result = queue_request_irq(dev, adminq, adminq->irqname);
Keith Busch9d713c22013-07-15 15:02:24 -06001924 if (result) {
Matthew Wilcox3193f072014-01-27 15:57:22 -05001925 adminq->q_suspended = 1;
Keith Busch22404272013-07-15 15:02:20 -06001926 goto free_queues;
Keith Busch9d713c22013-07-15 15:02:24 -06001927 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001928
Keith Buschcd638942013-07-15 15:02:23 -06001929 /* Free previously allocated queues that are no longer usable */
Keith Busch5a92e702014-02-21 14:13:44 -07001930 nvme_free_queues(dev, nr_io_queues);
Keith Buschcd638942013-07-15 15:02:23 -06001931
Matthew Wilcox1b234842011-01-20 13:01:49 -05001932 cpu = cpumask_first(cpu_online_mask);
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001933 for (i = 0; i < nr_io_queues; i++) {
Matthew Wilcox1b234842011-01-20 13:01:49 -05001934 irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu));
1935 cpu = cpumask_next(cpu, cpu_online_mask);
1936 }
1937
Keith Buscha0cadb82012-07-27 13:57:23 -04001938 q_depth = min_t(int, NVME_CAP_MQES(readq(&dev->bar->cap)) + 1,
1939 NVME_Q_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001940 for (i = dev->queue_count - 1; i < nr_io_queues; i++) {
Keith Busch5a92e702014-02-21 14:13:44 -07001941 if (!nvme_alloc_queue(dev, i + 1, q_depth, i)) {
Keith Busch22404272013-07-15 15:02:20 -06001942 result = -ENOMEM;
1943 goto free_queues;
1944 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001945 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001946
Matthew Wilcox9ecdc942011-03-16 16:52:19 -04001947 for (; i < num_possible_cpus(); i++) {
1948 int target = i % rounddown_pow_of_two(dev->queue_count - 1);
Keith Busch5a92e702014-02-21 14:13:44 -07001949 rcu_assign_pointer(dev->queues[i + 1], dev->queues[target + 1]);
Matthew Wilcox9ecdc942011-03-16 16:52:19 -04001950 }
1951
Keith Busch22404272013-07-15 15:02:20 -06001952 for (i = 1; i < dev->queue_count; i++) {
Keith Busch5a92e702014-02-21 14:13:44 -07001953 result = nvme_create_queue(raw_nvmeq(dev, i), i);
Keith Busch22404272013-07-15 15:02:20 -06001954 if (result) {
1955 for (--i; i > 0; i--)
1956 nvme_disable_queue(dev, i);
1957 goto free_queues;
1958 }
1959 }
1960
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001961 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001962
Keith Busch22404272013-07-15 15:02:20 -06001963 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05001964 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06001965 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001966}
1967
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04001968/*
1969 * Return: error value if an error occurred setting up the queues or calling
1970 * Identify Device. 0 if these succeeded, even if adding some of the
1971 * namespaces failed. At the moment, these failures are silent. TBD which
1972 * failures should be reported.
1973 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001974static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001975{
Matthew Wilcox68608c262013-06-21 14:36:34 -04001976 struct pci_dev *pdev = dev->pci_dev;
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001977 int res;
1978 unsigned nn, i;
Keith Buschcbb62182013-05-01 13:07:49 -06001979 struct nvme_ns *ns;
Matthew Wilcox51814232011-02-01 16:18:08 -05001980 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001981 struct nvme_id_ns *id_ns;
1982 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001983 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06001984 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001985
Matthew Wilcox68608c262013-06-21 14:36:34 -04001986 mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06001987 if (!mem)
1988 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001989
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001990 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001991 if (res) {
1992 res = -EIO;
Keith Buschcbb62182013-05-01 13:07:49 -06001993 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001994 }
1995
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001996 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05001997 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07001998 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07001999 dev->abort_limit = ctrl->acl + 1;
Matthew Wilcox51814232011-02-01 16:18:08 -05002000 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2001 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2002 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002003 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002004 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002005 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
2006 (pdev->device == 0x0953) && ctrl->vs[3])
Keith Busch159b67d2013-04-09 17:13:20 -06002007 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002008
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002009 id_ns = mem;
Matthew Wilcox2b2c1892011-10-07 13:10:13 -04002010 for (i = 1; i <= nn; i++) {
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002011 res = nvme_identify(dev, i, 0, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002012 if (res)
2013 continue;
2014
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002015 if (id_ns->ncap == 0)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002016 continue;
2017
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002018 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
Keith Busch08df1e02012-09-21 10:52:13 -06002019 dma_addr + 4096, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002020 if (res)
Keith Busch12209032013-01-31 14:40:38 -07002021 memset(mem + 4096, 0, 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002022
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002023 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002024 if (ns)
2025 list_add_tail(&ns->list, &dev->namespaces);
2026 }
2027 list_for_each_entry(ns, &dev->namespaces, list)
2028 add_disk(ns->disk);
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002029 res = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002030
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002031 out:
Matthew Wilcox684f5c22011-09-19 17:14:53 -04002032 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002033 return res;
2034}
2035
Keith Busch0877cb02013-07-15 15:02:19 -06002036static int nvme_dev_map(struct nvme_dev *dev)
2037{
2038 int bars, result = -ENOMEM;
2039 struct pci_dev *pdev = dev->pci_dev;
2040
2041 if (pci_enable_device_mem(pdev))
2042 return result;
2043
2044 dev->entry[0].vector = pdev->irq;
2045 pci_set_master(pdev);
2046 bars = pci_select_bars(pdev, IORESOURCE_MEM);
2047 if (pci_request_selected_regions(pdev, bars, "nvme"))
2048 goto disable_pci;
2049
Russell King052d0ef2013-06-26 23:49:11 +01002050 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
2051 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2052 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002053
Keith Busch0877cb02013-07-15 15:02:19 -06002054 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2055 if (!dev->bar)
2056 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07002057 if (readl(&dev->bar->csts) == -1) {
2058 result = -ENODEV;
2059 goto unmap;
2060 }
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002061 dev->db_stride = 1 << NVME_CAP_STRIDE(readq(&dev->bar->cap));
Keith Busch0877cb02013-07-15 15:02:19 -06002062 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2063
2064 return 0;
2065
Keith Busch0e53d182013-12-10 13:10:39 -07002066 unmap:
2067 iounmap(dev->bar);
2068 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002069 disable:
2070 pci_release_regions(pdev);
2071 disable_pci:
2072 pci_disable_device(pdev);
2073 return result;
2074}
2075
2076static void nvme_dev_unmap(struct nvme_dev *dev)
2077{
2078 if (dev->pci_dev->msi_enabled)
2079 pci_disable_msi(dev->pci_dev);
2080 else if (dev->pci_dev->msix_enabled)
2081 pci_disable_msix(dev->pci_dev);
2082
2083 if (dev->bar) {
2084 iounmap(dev->bar);
2085 dev->bar = NULL;
Keith Busch9a6b9452013-12-10 13:10:36 -07002086 pci_release_regions(dev->pci_dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002087 }
2088
Keith Busch0877cb02013-07-15 15:02:19 -06002089 if (pci_is_enabled(dev->pci_dev))
2090 pci_disable_device(dev->pci_dev);
2091}
2092
Keith Busch4d115422013-12-10 13:10:40 -07002093struct nvme_delq_ctx {
2094 struct task_struct *waiter;
2095 struct kthread_worker *worker;
2096 atomic_t refcount;
2097};
2098
2099static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2100{
2101 dq->waiter = current;
2102 mb();
2103
2104 for (;;) {
2105 set_current_state(TASK_KILLABLE);
2106 if (!atomic_read(&dq->refcount))
2107 break;
2108 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2109 fatal_signal_pending(current)) {
2110 set_current_state(TASK_RUNNING);
2111
2112 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
2113 nvme_disable_queue(dev, 0);
2114
2115 send_sig(SIGKILL, dq->worker->task, 1);
2116 flush_kthread_worker(dq->worker);
2117 return;
2118 }
2119 }
2120 set_current_state(TASK_RUNNING);
2121}
2122
2123static void nvme_put_dq(struct nvme_delq_ctx *dq)
2124{
2125 atomic_dec(&dq->refcount);
2126 if (dq->waiter)
2127 wake_up_process(dq->waiter);
2128}
2129
2130static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2131{
2132 atomic_inc(&dq->refcount);
2133 return dq;
2134}
2135
2136static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2137{
2138 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
2139
2140 nvme_clear_queue(nvmeq);
2141 nvme_put_dq(dq);
2142}
2143
2144static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2145 kthread_work_func_t fn)
2146{
2147 struct nvme_command c;
2148
2149 memset(&c, 0, sizeof(c));
2150 c.delete_queue.opcode = opcode;
2151 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2152
2153 init_kthread_work(&nvmeq->cmdinfo.work, fn);
2154 return nvme_submit_admin_cmd_async(nvmeq->dev, &c, &nvmeq->cmdinfo);
2155}
2156
2157static void nvme_del_cq_work_handler(struct kthread_work *work)
2158{
2159 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2160 cmdinfo.work);
2161 nvme_del_queue_end(nvmeq);
2162}
2163
2164static int nvme_delete_cq(struct nvme_queue *nvmeq)
2165{
2166 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2167 nvme_del_cq_work_handler);
2168}
2169
2170static void nvme_del_sq_work_handler(struct kthread_work *work)
2171{
2172 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2173 cmdinfo.work);
2174 int status = nvmeq->cmdinfo.status;
2175
2176 if (!status)
2177 status = nvme_delete_cq(nvmeq);
2178 if (status)
2179 nvme_del_queue_end(nvmeq);
2180}
2181
2182static int nvme_delete_sq(struct nvme_queue *nvmeq)
2183{
2184 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2185 nvme_del_sq_work_handler);
2186}
2187
2188static void nvme_del_queue_start(struct kthread_work *work)
2189{
2190 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2191 cmdinfo.work);
2192 allow_signal(SIGKILL);
2193 if (nvme_delete_sq(nvmeq))
2194 nvme_del_queue_end(nvmeq);
2195}
2196
2197static void nvme_disable_io_queues(struct nvme_dev *dev)
2198{
2199 int i;
2200 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2201 struct nvme_delq_ctx dq;
2202 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2203 &worker, "nvme%d", dev->instance);
2204
2205 if (IS_ERR(kworker_task)) {
2206 dev_err(&dev->pci_dev->dev,
2207 "Failed to create queue del task\n");
2208 for (i = dev->queue_count - 1; i > 0; i--)
2209 nvme_disable_queue(dev, i);
2210 return;
2211 }
2212
2213 dq.waiter = NULL;
2214 atomic_set(&dq.refcount, 0);
2215 dq.worker = &worker;
2216 for (i = dev->queue_count - 1; i > 0; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07002217 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
Keith Busch4d115422013-12-10 13:10:40 -07002218
2219 if (nvme_suspend_queue(nvmeq))
2220 continue;
2221 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2222 nvmeq->cmdinfo.worker = dq.worker;
2223 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2224 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2225 }
2226 nvme_wait_dq(&dq, dev);
2227 kthread_stop(kworker_task);
2228}
2229
Keith Buschf0b50732013-07-15 15:02:21 -06002230static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002231{
Keith Busch22404272013-07-15 15:02:20 -06002232 int i;
2233
Keith Buschd4b4ff82013-12-10 13:10:37 -07002234 dev->initialized = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002235
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002236 spin_lock(&dev_list_lock);
Keith Buschf0b50732013-07-15 15:02:21 -06002237 list_del_init(&dev->node);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002238 spin_unlock(&dev_list_lock);
2239
Keith Busch4d115422013-12-10 13:10:40 -07002240 if (!dev->bar || (dev->bar && readl(&dev->bar->csts) == -1)) {
2241 for (i = dev->queue_count - 1; i >= 0; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07002242 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
Keith Busch4d115422013-12-10 13:10:40 -07002243 nvme_suspend_queue(nvmeq);
2244 nvme_clear_queue(nvmeq);
2245 }
2246 } else {
2247 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002248 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002249 nvme_disable_queue(dev, 0);
2250 }
Keith Buschf0b50732013-07-15 15:02:21 -06002251 nvme_dev_unmap(dev);
2252}
2253
2254static void nvme_dev_remove(struct nvme_dev *dev)
2255{
Keith Busch9ac27092014-01-31 16:53:39 -07002256 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002257
Keith Busch9ac27092014-01-31 16:53:39 -07002258 list_for_each_entry(ns, &dev->namespaces, list) {
2259 if (ns->disk->flags & GENHD_FL_UP)
2260 del_gendisk(ns->disk);
2261 if (!blk_queue_dying(ns->queue))
2262 blk_cleanup_queue(ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002263 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002264}
2265
Matthew Wilcox091b6092011-02-10 09:56:01 -05002266static int nvme_setup_prp_pools(struct nvme_dev *dev)
2267{
2268 struct device *dmadev = &dev->pci_dev->dev;
2269 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2270 PAGE_SIZE, PAGE_SIZE, 0);
2271 if (!dev->prp_page_pool)
2272 return -ENOMEM;
2273
Matthew Wilcox99802a72011-02-10 10:30:34 -05002274 /* Optimisation for I/Os between 4k and 128k */
2275 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2276 256, 256, 0);
2277 if (!dev->prp_small_pool) {
2278 dma_pool_destroy(dev->prp_page_pool);
2279 return -ENOMEM;
2280 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002281 return 0;
2282}
2283
2284static void nvme_release_prp_pools(struct nvme_dev *dev)
2285{
2286 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002287 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002288}
2289
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002290static DEFINE_IDA(nvme_instance_ida);
2291
2292static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002293{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002294 int instance, error;
2295
2296 do {
2297 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2298 return -ENODEV;
2299
2300 spin_lock(&dev_list_lock);
2301 error = ida_get_new(&nvme_instance_ida, &instance);
2302 spin_unlock(&dev_list_lock);
2303 } while (error == -EAGAIN);
2304
2305 if (error)
2306 return -ENODEV;
2307
2308 dev->instance = instance;
2309 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002310}
2311
2312static void nvme_release_instance(struct nvme_dev *dev)
2313{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002314 spin_lock(&dev_list_lock);
2315 ida_remove(&nvme_instance_ida, dev->instance);
2316 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002317}
2318
Keith Busch9ac27092014-01-31 16:53:39 -07002319static void nvme_free_namespaces(struct nvme_dev *dev)
2320{
2321 struct nvme_ns *ns, *next;
2322
2323 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2324 list_del(&ns->list);
2325 put_disk(ns->disk);
2326 kfree(ns);
2327 }
2328}
2329
Keith Busch5e82e952013-02-19 10:17:58 -07002330static void nvme_free_dev(struct kref *kref)
2331{
2332 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002333
2334 nvme_free_namespaces(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002335 kfree(dev->queues);
2336 kfree(dev->entry);
2337 kfree(dev);
2338}
2339
2340static int nvme_dev_open(struct inode *inode, struct file *f)
2341{
2342 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
2343 miscdev);
2344 kref_get(&dev->kref);
2345 f->private_data = dev;
2346 return 0;
2347}
2348
2349static int nvme_dev_release(struct inode *inode, struct file *f)
2350{
2351 struct nvme_dev *dev = f->private_data;
2352 kref_put(&dev->kref, nvme_free_dev);
2353 return 0;
2354}
2355
2356static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2357{
2358 struct nvme_dev *dev = f->private_data;
2359 switch (cmd) {
2360 case NVME_IOCTL_ADMIN_CMD:
2361 return nvme_user_admin_cmd(dev, (void __user *)arg);
2362 default:
2363 return -ENOTTY;
2364 }
2365}
2366
2367static const struct file_operations nvme_dev_fops = {
2368 .owner = THIS_MODULE,
2369 .open = nvme_dev_open,
2370 .release = nvme_dev_release,
2371 .unlocked_ioctl = nvme_dev_ioctl,
2372 .compat_ioctl = nvme_dev_ioctl,
2373};
2374
Keith Buschf0b50732013-07-15 15:02:21 -06002375static int nvme_dev_start(struct nvme_dev *dev)
2376{
2377 int result;
2378
2379 result = nvme_dev_map(dev);
2380 if (result)
2381 return result;
2382
2383 result = nvme_configure_admin_queue(dev);
2384 if (result)
2385 goto unmap;
2386
2387 spin_lock(&dev_list_lock);
2388 list_add(&dev->node, &dev_list);
2389 spin_unlock(&dev_list_lock);
2390
2391 result = nvme_setup_io_queues(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002392 if (result && result != -EBUSY)
Keith Buschf0b50732013-07-15 15:02:21 -06002393 goto disable;
2394
Keith Buschd82e8bf2013-09-05 14:45:07 -06002395 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002396
2397 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002398 nvme_disable_queue(dev, 0);
Keith Buschf0b50732013-07-15 15:02:21 -06002399 spin_lock(&dev_list_lock);
2400 list_del_init(&dev->node);
2401 spin_unlock(&dev_list_lock);
2402 unmap:
2403 nvme_dev_unmap(dev);
2404 return result;
2405}
2406
Keith Busch9a6b9452013-12-10 13:10:36 -07002407static int nvme_remove_dead_ctrl(void *arg)
2408{
2409 struct nvme_dev *dev = (struct nvme_dev *)arg;
2410 struct pci_dev *pdev = dev->pci_dev;
2411
2412 if (pci_get_drvdata(pdev))
2413 pci_stop_and_remove_bus_device(pdev);
2414 kref_put(&dev->kref, nvme_free_dev);
2415 return 0;
2416}
2417
2418static void nvme_remove_disks(struct work_struct *ws)
2419{
Keith Busch9a6b9452013-12-10 13:10:36 -07002420 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2421
2422 nvme_dev_remove(dev);
Keith Busch5a92e702014-02-21 14:13:44 -07002423 nvme_free_queues(dev, 1);
Keith Busch9a6b9452013-12-10 13:10:36 -07002424}
2425
2426static int nvme_dev_resume(struct nvme_dev *dev)
2427{
2428 int ret;
2429
2430 ret = nvme_dev_start(dev);
2431 if (ret && ret != -EBUSY)
2432 return ret;
2433 if (ret == -EBUSY) {
2434 spin_lock(&dev_list_lock);
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05002435 PREPARE_WORK(&dev->reset_work, nvme_remove_disks);
Keith Busch9a6b9452013-12-10 13:10:36 -07002436 queue_work(nvme_workq, &dev->reset_work);
2437 spin_unlock(&dev_list_lock);
2438 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002439 dev->initialized = 1;
Keith Busch9a6b9452013-12-10 13:10:36 -07002440 return 0;
2441}
2442
2443static void nvme_dev_reset(struct nvme_dev *dev)
2444{
2445 nvme_dev_shutdown(dev);
2446 if (nvme_dev_resume(dev)) {
2447 dev_err(&dev->pci_dev->dev, "Device failed to resume\n");
2448 kref_get(&dev->kref);
2449 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2450 dev->instance))) {
2451 dev_err(&dev->pci_dev->dev,
2452 "Failed to start controller remove task\n");
2453 kref_put(&dev->kref, nvme_free_dev);
2454 }
2455 }
2456}
2457
2458static void nvme_reset_failed_dev(struct work_struct *ws)
2459{
2460 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2461 nvme_dev_reset(dev);
2462}
2463
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002464static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002465{
Keith Busch0877cb02013-07-15 15:02:19 -06002466 int result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002467 struct nvme_dev *dev;
2468
2469 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2470 if (!dev)
2471 return -ENOMEM;
2472 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
2473 GFP_KERNEL);
2474 if (!dev->entry)
2475 goto free;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002476 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
2477 GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002478 if (!dev->queues)
2479 goto free;
2480
2481 INIT_LIST_HEAD(&dev->namespaces);
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05002482 INIT_WORK(&dev->reset_work, nvme_reset_failed_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002483 dev->pci_dev = pdev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002484 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002485 result = nvme_set_instance(dev);
2486 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002487 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002488
Matthew Wilcox091b6092011-02-10 09:56:01 -05002489 result = nvme_setup_prp_pools(dev);
2490 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002491 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002492
Keith Buschfb35e912014-03-03 11:09:47 -07002493 kref_init(&dev->kref);
Keith Buschf0b50732013-07-15 15:02:21 -06002494 result = nvme_dev_start(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002495 if (result) {
2496 if (result == -EBUSY)
2497 goto create_cdev;
Keith Busch0877cb02013-07-15 15:02:19 -06002498 goto release_pools;
Keith Buschd82e8bf2013-09-05 14:45:07 -06002499 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002500
Matthew Wilcox740216f2011-02-15 16:28:20 -05002501 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002502 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002503 goto shutdown;
Matthew Wilcox740216f2011-02-15 16:28:20 -05002504
Keith Buschd82e8bf2013-09-05 14:45:07 -06002505 create_cdev:
Keith Busch5e82e952013-02-19 10:17:58 -07002506 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
2507 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2508 dev->miscdev.parent = &pdev->dev;
2509 dev->miscdev.name = dev->name;
2510 dev->miscdev.fops = &nvme_dev_fops;
2511 result = misc_register(&dev->miscdev);
2512 if (result)
2513 goto remove;
2514
Keith Buschd4b4ff82013-12-10 13:10:37 -07002515 dev->initialized = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002516 return 0;
2517
Keith Busch5e82e952013-02-19 10:17:58 -07002518 remove:
2519 nvme_dev_remove(dev);
Keith Busch9ac27092014-01-31 16:53:39 -07002520 nvme_free_namespaces(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002521 shutdown:
2522 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002523 release_pools:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002524 nvme_free_queues(dev, 0);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002525 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002526 release:
2527 nvme_release_instance(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002528 free:
2529 kfree(dev->queues);
2530 kfree(dev->entry);
2531 kfree(dev);
2532 return result;
2533}
2534
Keith Busch09ece142014-01-27 11:29:40 -05002535static void nvme_shutdown(struct pci_dev *pdev)
2536{
2537 struct nvme_dev *dev = pci_get_drvdata(pdev);
2538 nvme_dev_shutdown(dev);
2539}
2540
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002541static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002542{
2543 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002544
2545 spin_lock(&dev_list_lock);
2546 list_del_init(&dev->node);
2547 spin_unlock(&dev_list_lock);
2548
2549 pci_set_drvdata(pdev, NULL);
2550 flush_work(&dev->reset_work);
Keith Busch5e82e952013-02-19 10:17:58 -07002551 misc_deregister(&dev->miscdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002552 nvme_dev_remove(dev);
2553 nvme_dev_shutdown(dev);
Keith Buscha1a5ef92013-12-16 13:50:00 -05002554 nvme_free_queues(dev, 0);
Keith Busch5a92e702014-02-21 14:13:44 -07002555 rcu_barrier();
Keith Busch9a6b9452013-12-10 13:10:36 -07002556 nvme_release_instance(dev);
2557 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002558 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002559}
2560
2561/* These functions are yet to be implemented */
2562#define nvme_error_detected NULL
2563#define nvme_dump_registers NULL
2564#define nvme_link_reset NULL
2565#define nvme_slot_reset NULL
2566#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002567
Jingoo Han671a6012014-02-13 11:19:14 +09002568#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002569static int nvme_suspend(struct device *dev)
2570{
2571 struct pci_dev *pdev = to_pci_dev(dev);
2572 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2573
2574 nvme_dev_shutdown(ndev);
2575 return 0;
2576}
2577
2578static int nvme_resume(struct device *dev)
2579{
2580 struct pci_dev *pdev = to_pci_dev(dev);
2581 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002582
Keith Busch9a6b9452013-12-10 13:10:36 -07002583 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05002584 PREPARE_WORK(&ndev->reset_work, nvme_reset_failed_dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002585 queue_work(nvme_workq, &ndev->reset_work);
2586 }
2587 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002588}
Jingoo Han671a6012014-02-13 11:19:14 +09002589#endif
Keith Buschcd638942013-07-15 15:02:23 -06002590
2591static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002592
Stephen Hemminger1d352032012-09-07 09:33:17 -07002593static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002594 .error_detected = nvme_error_detected,
2595 .mmio_enabled = nvme_dump_registers,
2596 .link_reset = nvme_link_reset,
2597 .slot_reset = nvme_slot_reset,
2598 .resume = nvme_error_resume,
2599};
2600
2601/* Move to pci_ids.h later */
2602#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2603
2604static DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
2605 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2606 { 0, }
2607};
2608MODULE_DEVICE_TABLE(pci, nvme_id_table);
2609
2610static struct pci_driver nvme_driver = {
2611 .name = "nvme",
2612 .id_table = nvme_id_table,
2613 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002614 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002615 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002616 .driver = {
2617 .pm = &nvme_dev_pm_ops,
2618 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002619 .err_handler = &nvme_err_handler,
2620};
2621
2622static int __init nvme_init(void)
2623{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002624 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002625
2626 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2627 if (IS_ERR(nvme_thread))
2628 return PTR_ERR(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002629
Keith Busch9a6b9452013-12-10 13:10:36 -07002630 result = -ENOMEM;
2631 nvme_workq = create_singlethread_workqueue("nvme");
2632 if (!nvme_workq)
2633 goto kill_kthread;
2634
Keith Busch5c42ea12012-07-25 16:05:18 -06002635 result = register_blkdev(nvme_major, "nvme");
2636 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002637 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06002638 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002639 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002640
2641 result = pci_register_driver(&nvme_driver);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002642 if (result)
2643 goto unregister_blkdev;
2644 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002645
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002646 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002647 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002648 kill_workq:
2649 destroy_workqueue(nvme_workq);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002650 kill_kthread:
2651 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002652 return result;
2653}
2654
2655static void __exit nvme_exit(void)
2656{
2657 pci_unregister_driver(&nvme_driver);
2658 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002659 destroy_workqueue(nvme_workq);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002660 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002661}
2662
2663MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2664MODULE_LICENSE("GPL");
Matthew Wilcox366e8212012-01-10 16:30:15 -05002665MODULE_VERSION("0.8");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002666module_init(nvme_init);
2667module_exit(nvme_exit);