nvme-core.c revision 6bbf1acddeed0bfb345a5578f9fcada16f1e514f
1b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/*
2b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * NVM Express device driver
3b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Copyright (c) 2011, Intel Corporation.
4b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox *
5b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * This program is free software; you can redistribute it and/or modify it
6b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * under the terms and conditions of the GNU General Public License,
7b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * version 2, as published by the Free Software Foundation.
8b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox *
9b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * This program is distributed in the hope it will be useful, but WITHOUT
10b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * more details.
13b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox *
14b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * You should have received a copy of the GNU General Public License along with
15b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * this program; if not, write to the Free Software Foundation, Inc.,
16b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
18b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
19b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/nvme.h>
20b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/bio.h>
218de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox#include <linux/bitops.h>
22b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/blkdev.h>
23fd63e9ceeeae58cfe877c2d49d41c1bf7532303cMatthew Wilcox#include <linux/delay.h>
24b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/errno.h>
25b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/fs.h>
26b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/genhd.h>
275aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox#include <linux/idr.h>
28b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/init.h>
29b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/interrupt.h>
30b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/io.h>
31b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/kdev_t.h>
321fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox#include <linux/kthread.h>
33b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/kernel.h>
34b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/mm.h>
35b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/module.h>
36b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/moduleparam.h>
37b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/pci.h>
38be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox#include <linux/poison.h>
39b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/sched.h>
40b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/slab.h>
41b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/types.h>
42b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/version.h>
43b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
44b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define NVME_Q_DEPTH 1024
45b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define SQ_SIZE(depth)		(depth * sizeof(struct nvme_command))
46b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define CQ_SIZE(depth)		(depth * sizeof(struct nvme_completion))
47b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define NVME_MINORS 64
48e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox#define IO_TIMEOUT	(5 * HZ)
49e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox#define ADMIN_TIMEOUT	(60 * HZ)
50b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
51b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_major;
52b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_param(nvme_major, int, 0);
53b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
5458ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxstatic int use_threaded_interrupts;
5558ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxmodule_param(use_threaded_interrupts, int, 0);
5658ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox
571fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic DEFINE_SPINLOCK(dev_list_lock);
581fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic LIST_HEAD(dev_list);
591fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic struct task_struct *nvme_thread;
601fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox
61b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/*
62b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Represents an NVM Express device.  Each nvme_dev is a PCI function.
63b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
64b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct nvme_dev {
651fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	struct list_head node;
66b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_queue **queues;
67b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 __iomem *dbs;
68b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct pci_dev *pci_dev;
69091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	struct dma_pool *prp_page_pool;
7099802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	struct dma_pool *prp_small_pool;
71b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int instance;
72b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int queue_count;
73b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 ctrl_config;
74b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct msix_entry *entry;
75b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_bar __iomem *bar;
76b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct list_head namespaces;
7751814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	char serial[20];
7851814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	char model[40];
7951814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	char firmware_rev[8];
80b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
81b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
82b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/*
83b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * An NVM Express namespace is equivalent to a SCSI LUN
84b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
85b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct nvme_ns {
86b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct list_head list;
87b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
88b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_dev *dev;
89b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct request_queue *queue;
90b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct gendisk *disk;
91b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
92b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int ns_id;
93b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int lba_shift;
94b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
95b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
96b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/*
97b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * An NVM Express queue.  Each device has at least two (one for admin
98b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * commands and one for I/O commands).
99b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
100b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct nvme_queue {
101b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct device *q_dmadev;
102091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	struct nvme_dev *dev;
103b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	spinlock_t q_lock;
104b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_command *sq_cmds;
105b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	volatile struct nvme_completion *cqes;
106b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_addr_t sq_dma_addr;
107b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_addr_t cq_dma_addr;
108b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	wait_queue_head_t sq_full;
1091fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	wait_queue_t sq_cong_wait;
110b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct bio_list sq_cong;
111b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 __iomem *q_db;
112b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 q_depth;
113b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 cq_vector;
114b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 sq_head;
115b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 sq_tail;
116b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 cq_head;
117821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox	u16 cq_phase;
118b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	unsigned long cmdid_data[];
119b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
120b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
121b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/*
122b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Check we didin't inadvertently grow the command struct
123b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
124b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic inline void _nvme_check_size(void)
125b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
126b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
127b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
128b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
129b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
130b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
131b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
132b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
133b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
134b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
135b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
136b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
137e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstruct nvme_cmd_info {
138e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	unsigned long ctx;
139e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	unsigned long timeout;
140e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox};
141e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox
142e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstatic struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
143e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox{
144e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
145e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox}
146e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox
147b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/**
148714a7a22884b74862540bc84955274d86b2f6040Matthew Wilcox * alloc_cmdid() - Allocate a Command ID
149714a7a22884b74862540bc84955274d86b2f6040Matthew Wilcox * @nvmeq: The queue that will be used for this command
150714a7a22884b74862540bc84955274d86b2f6040Matthew Wilcox * @ctx: A pointer that will be passed to the handler
151714a7a22884b74862540bc84955274d86b2f6040Matthew Wilcox * @handler: The ID of the handler to call
152b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox *
153b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Allocate a Command ID for a queue.  The data passed in will
154b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * be passed to the completion handler.  This is implemented by using
155b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * the bottom two bits of the ctx pointer to store the handler ID.
156b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Passing in a pointer that's not 4-byte aligned will cause a BUG.
157b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * We can change this if it becomes a problem.
158184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox *
159184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox * May be called with local interrupts disabled and the q_lock held,
160184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox * or with interrupts enabled and no locks held.
161b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
162e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstatic int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx, int handler,
163e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox							unsigned timeout)
164b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
165e6d15f79f997a98b3a69abbc462fc9041cc1a7b4Matthew Wilcox	int depth = nvmeq->q_depth - 1;
166e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
167b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int cmdid;
168b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
169b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUG_ON((unsigned long)ctx & 3);
170b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
171b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	do {
172b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
173b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (cmdid >= depth)
174b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			return -EBUSY;
175b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	} while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
176b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
177e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	info[cmdid].ctx = (unsigned long)ctx | handler;
178e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	info[cmdid].timeout = jiffies + timeout;
179b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return cmdid;
180b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
181b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
182b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
183e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox						int handler, unsigned timeout)
184b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
185b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int cmdid;
186b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	wait_event_killable(nvmeq->sq_full,
187e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox		(cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
188b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return (cmdid < 0) ? -EINTR : cmdid;
189b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
190b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
191fa92282149842645931580225647238428374758Matthew Wilcox/*
192fa92282149842645931580225647238428374758Matthew Wilcox * If you need more than four handlers, you'll need to change how
193be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox * alloc_cmdid and nvme_process_cq work.  Consider using a special
194be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox * CMD_CTX value instead, if that works for your situation.
195b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
196b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxenum {
197b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	sync_completion_id = 0,
198b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	bio_completion_id,
199b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
200b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
20100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox/* Special values must be a multiple of 4, and less than 0x1000 */
202be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox#define CMD_CTX_BASE		(POISON_POINTER_DELTA + sync_completion_id)
203d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_CANCELLED	(0x30C + CMD_CTX_BASE)
204d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_COMPLETED	(0x310 + CMD_CTX_BASE)
205d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_INVALID		(0x314 + CMD_CTX_BASE)
20600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox#define CMD_CTX_FLUSH		(0x318 + CMD_CTX_BASE)
207be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox
208184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox/*
209184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox * Called with local interrupts disabled and the q_lock held.  May not sleep.
210184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox */
211b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic unsigned long free_cmdid(struct nvme_queue *nvmeq, int cmdid)
212b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
213b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	unsigned long data;
214e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
215b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
216e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	if (cmdid >= nvmeq->q_depth)
21748e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox		return CMD_CTX_INVALID;
218e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	data = info[cmdid].ctx;
219e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	info[cmdid].ctx = CMD_CTX_COMPLETED;
220b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	clear_bit(cmdid, nvmeq->cmdid_data);
221b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	wake_up(&nvmeq->sq_full);
222b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return data;
223b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
224b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
22521075bdee0a6f56058920d889df4ae561bfed754Matthew Wilcoxstatic unsigned long cancel_cmdid(struct nvme_queue *nvmeq, int cmdid)
2263c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox{
22721075bdee0a6f56058920d889df4ae561bfed754Matthew Wilcox	unsigned long data;
228e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
22921075bdee0a6f56058920d889df4ae561bfed754Matthew Wilcox	data = info[cmdid].ctx;
230e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	info[cmdid].ctx = CMD_CTX_CANCELLED;
23121075bdee0a6f56058920d889df4ae561bfed754Matthew Wilcox	return data;
2323c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox}
2333c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox
234b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct nvme_queue *get_nvmeq(struct nvme_ns *ns)
235b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
2369ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox	return ns->dev->queues[get_cpu() + 1];
237b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
238b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
239b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void put_nvmeq(struct nvme_queue *nvmeq)
240b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
2411b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	put_cpu();
242b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
243b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
244b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/**
245714a7a22884b74862540bc84955274d86b2f6040Matthew Wilcox * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
246b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @nvmeq: The queue to use
247b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @cmd: The command to send
248b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox *
249b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Safe to use from interrupt context
250b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
251b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
252b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
253b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	unsigned long flags;
254b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 tail;
255b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	spin_lock_irqsave(&nvmeq->q_lock, flags);
256b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	tail = nvmeq->sq_tail;
257b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
258b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (++tail == nvmeq->q_depth)
259b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		tail = 0;
2607547881d0951384f9833ec3a80fac8f3f16f3b98Matthew Wilcox	writel(tail, nvmeq->q_db);
261b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->sq_tail = tail;
262b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	spin_unlock_irqrestore(&nvmeq->q_lock, flags);
263b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
264b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
265b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
266b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
267e025344c56e08b155f43ea09647969286c78377cShane Michael Matthewsstruct nvme_prps {
268e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	int npages;
269e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	dma_addr_t first_dma;
270e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	__le64 *list[0];
271e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews};
272e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews
273d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcoxstatic void nvme_free_prps(struct nvme_dev *dev, struct nvme_prps *prps)
274e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews{
275e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	const int last_prp = PAGE_SIZE / 8 - 1;
276e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	int i;
277e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	dma_addr_t prp_dma;
278e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews
279e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	if (!prps)
280e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		return;
281e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews
282e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	prp_dma = prps->first_dma;
28399802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox
28499802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	if (prps->npages == 0)
28599802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox		dma_pool_free(dev->prp_small_pool, prps->list[0], prp_dma);
286e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	for (i = 0; i < prps->npages; i++) {
287e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		__le64 *prp_list = prps->list[i];
288e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
289091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox		dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
290e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		prp_dma = next_prp_dma;
291e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	}
292e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	kfree(prps);
293e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews}
294e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews
295d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstruct nvme_bio {
296b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct bio *bio;
297b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int nents;
298e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	struct nvme_prps *prps;
299b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct scatterlist sg[0];
300b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
301b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
302b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* XXX: use a mempool */
303d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstatic struct nvme_bio *alloc_nbio(unsigned nseg, gfp_t gfp)
304b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
305d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	return kzalloc(sizeof(struct nvme_bio) +
306b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			sizeof(struct scatterlist) * nseg, gfp);
307b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
308b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
309d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstatic void free_nbio(struct nvme_queue *nvmeq, struct nvme_bio *nbio)
310b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
311d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox	nvme_free_prps(nvmeq->dev, nbio->prps);
312d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	kfree(nbio);
313b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
314b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
315b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void bio_completion(struct nvme_queue *nvmeq, void *ctx,
316b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox						struct nvme_completion *cqe)
317b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
318d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	struct nvme_bio *nbio = ctx;
319d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	struct bio *bio = nbio->bio;
320b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 status = le16_to_cpup(&cqe->status) >> 1;
321b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
322d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	dma_unmap_sg(nvmeq->q_dmadev, nbio->sg, nbio->nents,
323b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
324d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	free_nbio(nvmeq, nbio);
32509a58f536436efed02ead722e835cb4ce7674afcMatthew Wilcox	if (status) {
3261ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox		bio_endio(bio, -EIO);
32709a58f536436efed02ead722e835cb4ce7674afcMatthew Wilcox	} else if (bio->bi_vcnt > bio->bi_idx) {
328eac623ba7a91474a688eb5d0fcd0eaa6a56dc41cMatthew Wilcox		if (bio_list_empty(&nvmeq->sq_cong))
329eac623ba7a91474a688eb5d0fcd0eaa6a56dc41cMatthew Wilcox			add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
3301ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox		bio_list_add(&nvmeq->sq_cong, bio);
3311ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox		wake_up_process(nvme_thread);
3321ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	} else {
3331ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox		bio_endio(bio, 0);
3341ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	}
335b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
336b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
337184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox/* length is in bytes.  gfp flags indicates whether we may sleep. */
338d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcoxstatic struct nvme_prps *nvme_setup_prps(struct nvme_dev *dev,
339e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews					struct nvme_common_command *cmd,
340b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox					struct scatterlist *sg, int *len,
341b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox					gfp_t gfp)
342ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox{
34399802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	struct dma_pool *pool;
344b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox	int length = *len;
345ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	int dma_len = sg_dma_len(sg);
346ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	u64 dma_addr = sg_dma_address(sg);
347ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	int offset = offset_in_page(dma_addr);
348e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	__le64 *prp_list;
349e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	dma_addr_t prp_dma;
350e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	int nprps, npages, i, prp_page;
351e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	struct nvme_prps *prps = NULL;
352ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox
353ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmd->prp1 = cpu_to_le64(dma_addr);
354ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	length -= (PAGE_SIZE - offset);
355ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	if (length <= 0)
356e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		return prps;
357ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox
358ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	dma_len -= (PAGE_SIZE - offset);
359ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	if (dma_len) {
360ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		dma_addr += (PAGE_SIZE - offset);
361ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	} else {
362ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		sg = sg_next(sg);
363ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		dma_addr = sg_dma_address(sg);
364ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		dma_len = sg_dma_len(sg);
365ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	}
366ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox
367ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	if (length <= PAGE_SIZE) {
368ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		cmd->prp2 = cpu_to_le64(dma_addr);
369e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		return prps;
370e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	}
371e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews
372e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	nprps = DIV_ROUND_UP(length, PAGE_SIZE);
373e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	npages = DIV_ROUND_UP(8 * nprps, PAGE_SIZE);
374b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox	prps = kmalloc(sizeof(*prps) + sizeof(__le64 *) * npages, gfp);
375b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox	if (!prps) {
376b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox		cmd->prp2 = cpu_to_le64(dma_addr);
377b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox		*len = (*len - length) + PAGE_SIZE;
378b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox		return prps;
379b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox	}
380e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	prp_page = 0;
38199802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	if (nprps <= (256 / 8)) {
38299802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox		pool = dev->prp_small_pool;
38399802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox		prps->npages = 0;
38499802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	} else {
38599802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox		pool = dev->prp_page_pool;
38699802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox		prps->npages = npages;
38799802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	}
38899802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox
389b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox	prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
390b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox	if (!prp_list) {
391b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox		cmd->prp2 = cpu_to_le64(dma_addr);
392b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox		*len = (*len - length) + PAGE_SIZE;
393b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox		kfree(prps);
394b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox		return NULL;
395b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox	}
396e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	prps->list[prp_page++] = prp_list;
397e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	prps->first_dma = prp_dma;
398e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	cmd->prp2 = cpu_to_le64(prp_dma);
399e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	i = 0;
400e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	for (;;) {
4017523d834dd1573610078eb1ac0933f6490232f90Matthew Wilcox		if (i == PAGE_SIZE / 8) {
402e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews			__le64 *old_prp_list = prp_list;
403b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox			prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
404b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox			if (!prp_list) {
405b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox				*len = (*len - length);
406b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox				return prps;
407b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox			}
408e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews			prps->list[prp_page++] = prp_list;
4097523d834dd1573610078eb1ac0933f6490232f90Matthew Wilcox			prp_list[0] = old_prp_list[i - 1];
4107523d834dd1573610078eb1ac0933f6490232f90Matthew Wilcox			old_prp_list[i - 1] = cpu_to_le64(prp_dma);
4117523d834dd1573610078eb1ac0933f6490232f90Matthew Wilcox			i = 1;
412e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		}
413e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		prp_list[i++] = cpu_to_le64(dma_addr);
414e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		dma_len -= PAGE_SIZE;
415e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		dma_addr += PAGE_SIZE;
416e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		length -= PAGE_SIZE;
417e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		if (length <= 0)
418e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews			break;
419e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		if (dma_len > 0)
420e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews			continue;
421e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		BUG_ON(dma_len < 0);
422e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		sg = sg_next(sg);
423e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		dma_addr = sg_dma_address(sg);
424e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		dma_len = sg_dma_len(sg);
425ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	}
426ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox
427e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	return prps;
428ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox}
429ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox
4301ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox/* NVMe scatterlists require no holes in the virtual address */
4311ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2)	((vec2)->bv_offset || \
4321ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox			(((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
4331ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox
434d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstatic int nvme_map_bio(struct device *dev, struct nvme_bio *nbio,
435b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		struct bio *bio, enum dma_data_direction dma_dir, int psegs)
436b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
437768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox	struct bio_vec *bvec, *bvprv = NULL;
438768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox	struct scatterlist *sg = NULL;
4391ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	int i, old_idx, length = 0, nsegs = 0;
440b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
441768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox	sg_init_table(nbio->sg, psegs);
4421ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	old_idx = bio->bi_idx;
443b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	bio_for_each_segment(bvec, bio, i) {
444768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox		if (bvprv && BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) {
445768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox			sg->length += bvec->bv_len;
446768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox		} else {
4471ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox			if (bvprv && BIOVEC_NOT_VIRT_MERGEABLE(bvprv, bvec))
4481ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox				break;
449768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox			sg = sg ? sg + 1 : nbio->sg;
450768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox			sg_set_page(sg, bvec->bv_page, bvec->bv_len,
451768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox							bvec->bv_offset);
452768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox			nsegs++;
453768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox		}
4541ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox		length += bvec->bv_len;
455768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox		bvprv = bvec;
456b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
4571ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	bio->bi_idx = i;
458d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	nbio->nents = nsegs;
459768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox	sg_mark_end(sg);
4601ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	if (dma_map_sg(dev, nbio->sg, nbio->nents, dma_dir) == 0) {
4611ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox		bio->bi_idx = old_idx;
4621ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox		return -ENOMEM;
4631ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	}
4641ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	return length;
465b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
466b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
46700df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcoxstatic int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
46800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox								int cmdid)
46900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox{
47000df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
47100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox
47200df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	memset(cmnd, 0, sizeof(*cmnd));
47300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	cmnd->common.opcode = nvme_cmd_flush;
47400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	cmnd->common.command_id = cmdid;
47500df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	cmnd->common.nsid = cpu_to_le32(ns->ns_id);
47600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox
47700df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	if (++nvmeq->sq_tail == nvmeq->q_depth)
47800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox		nvmeq->sq_tail = 0;
47900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	writel(nvmeq->sq_tail, nvmeq->q_db);
48000df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox
48100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	return 0;
48200df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox}
48300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox
48400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcoxstatic int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns)
48500df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox{
48600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH,
48700df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox						sync_completion_id, IO_TIMEOUT);
48800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	if (unlikely(cmdid < 0))
48900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox		return cmdid;
49000df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox
49100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	return nvme_submit_flush(nvmeq, ns, cmdid);
49200df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox}
49300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox
494184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox/*
495184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox * Called with local interrupts disabled and the q_lock held.  May not sleep.
496184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox */
497b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
498b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox								struct bio *bio)
499b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
500ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	struct nvme_command *cmnd;
501d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	struct nvme_bio *nbio;
502b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	enum dma_data_direction dma_dir;
5031ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	int cmdid, length, result = -ENOMEM;
504b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 control;
505b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 dsmgmt;
506b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int psegs = bio_phys_segments(ns->queue, bio);
507b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
50800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	if ((bio->bi_rw & REQ_FLUSH) && psegs) {
50900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox		result = nvme_submit_flush_data(nvmeq, ns);
51000df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox		if (result)
51100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox			return result;
51200df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	}
51300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox
514eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox	nbio = alloc_nbio(psegs, GFP_ATOMIC);
515d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	if (!nbio)
516eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox		goto nomem;
517d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	nbio->bio = bio;
518b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
519eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox	result = -EBUSY;
520d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	cmdid = alloc_cmdid(nvmeq, nbio, bio_completion_id, IO_TIMEOUT);
521b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (unlikely(cmdid < 0))
522d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox		goto free_nbio;
523b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
52400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	if ((bio->bi_rw & REQ_FLUSH) && !psegs)
52500df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox		return nvme_submit_flush(nvmeq, ns, cmdid);
52600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox
527b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	control = 0;
528b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (bio->bi_rw & REQ_FUA)
529b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		control |= NVME_RW_FUA;
530b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
531b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		control |= NVME_RW_LR;
532b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
533b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dsmgmt = 0;
534b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (bio->bi_rw & REQ_RAHEAD)
535b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
536b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
537ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
538b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
539b8deb62cf271fa9381edc8cf52bcae2f0225c55aMatthew Wilcox	memset(cmnd, 0, sizeof(*cmnd));
540b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (bio_data_dir(bio)) {
541ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		cmnd->rw.opcode = nvme_cmd_write;
542b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		dma_dir = DMA_TO_DEVICE;
543b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	} else {
544ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		cmnd->rw.opcode = nvme_cmd_read;
545b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		dma_dir = DMA_FROM_DEVICE;
546b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
547b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
5481ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	result = nvme_map_bio(nvmeq->q_dmadev, nbio, bio, dma_dir, psegs);
5491ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	if (result < 0)
550eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox		goto free_nbio;
5511ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	length = result;
552b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
553ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd->rw.command_id = cmdid;
554ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
555d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox	nbio->prps = nvme_setup_prps(nvmeq->dev, &cmnd->common, nbio->sg,
556b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox							&length, GFP_ATOMIC);
557ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd->rw.slba = cpu_to_le64(bio->bi_sector >> (ns->lba_shift - 9));
5581ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	cmnd->rw.length = cpu_to_le16((length >> ns->lba_shift) - 1);
559ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd->rw.control = cpu_to_le16(control);
560ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
561b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
562d8ee9d69f275769aaad40ef7c944565ff8d2d24fMatthew Wilcox	bio->bi_sector += length >> 9;
563d8ee9d69f275769aaad40ef7c944565ff8d2d24fMatthew Wilcox
564b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (++nvmeq->sq_tail == nvmeq->q_depth)
565b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		nvmeq->sq_tail = 0;
5667547881d0951384f9833ec3a80fac8f3f16f3b98Matthew Wilcox	writel(nvmeq->sq_tail, nvmeq->q_db);
567b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
5681974b1ae8852324a75fb8cfecbc7b758fd5a2c3cMatthew Wilcox	return 0;
5691974b1ae8852324a75fb8cfecbc7b758fd5a2c3cMatthew Wilcox
570d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox free_nbio:
571d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	free_nbio(nvmeq, nbio);
572eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox nomem:
573eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox	return result;
574b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
575b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
576b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/*
577b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * NB: return value of non-zero would mean that we were a stacking driver.
578b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * make_request must always succeed.
579b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
580b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_make_request(struct request_queue *q, struct bio *bio)
581b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
582b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_ns *ns = q->queuedata;
583b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_queue *nvmeq = get_nvmeq(ns);
584eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox	int result = -EBUSY;
585eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox
586eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox	spin_lock_irq(&nvmeq->q_lock);
587eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox	if (bio_list_empty(&nvmeq->sq_cong))
588eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox		result = nvme_submit_bio_queue(nvmeq, ns, bio);
589eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox	if (unlikely(result)) {
590eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox		if (bio_list_empty(&nvmeq->sq_cong))
591eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox			add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
592b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		bio_list_add(&nvmeq->sq_cong, bio);
593b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
594eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox
595eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox	spin_unlock_irq(&nvmeq->q_lock);
596b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	put_nvmeq(nvmeq);
597b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
598b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
599b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
600b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
601b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct sync_cmd_info {
602b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct task_struct *task;
603b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 result;
604b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int status;
605b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
606b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
607b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void sync_completion(struct nvme_queue *nvmeq, void *ctx,
608b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox						struct nvme_completion *cqe)
609b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
610b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct sync_cmd_info *cmdinfo = ctx;
611c42705592be2a539f3027b6f3907de8e8f9591a8Matthew Wilcox	if (unlikely((unsigned long)cmdinfo == CMD_CTX_CANCELLED))
612be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox		return;
61300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	if ((unsigned long)cmdinfo == CMD_CTX_FLUSH)
61400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox		return;
615b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox	if (unlikely((unsigned long)cmdinfo == CMD_CTX_COMPLETED)) {
616b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox		dev_warn(nvmeq->q_dmadev,
617b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox				"completed id %d twice on queue %d\n",
618b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox				cqe->command_id, le16_to_cpup(&cqe->sq_id));
619b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox		return;
620b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox	}
62148e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox	if (unlikely((unsigned long)cmdinfo == CMD_CTX_INVALID)) {
62248e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox		dev_warn(nvmeq->q_dmadev,
62348e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox				"invalid id %d completed on queue %d\n",
62448e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox				cqe->command_id, le16_to_cpup(&cqe->sq_id));
62548e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox		return;
62648e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox	}
627b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cmdinfo->result = le32_to_cpup(&cqe->result);
628b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
629b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	wake_up_process(cmdinfo->task);
630b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
631b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
632b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxtypedef void (*completion_fn)(struct nvme_queue *, void *,
633b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox						struct nvme_completion *);
634b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
6358de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcoxstatic const completion_fn nvme_completions[4] = {
6368de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox	[sync_completion_id] = sync_completion,
6378de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox	[bio_completion_id]  = bio_completion,
6388de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox};
6398de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox
640b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic irqreturn_t nvme_process_cq(struct nvme_queue *nvmeq)
641b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
642821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox	u16 head, phase;
643b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
644b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	head = nvmeq->cq_head;
645821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox	phase = nvmeq->cq_phase;
646b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
647b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	for (;;) {
648b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		unsigned long data;
649b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		void *ptr;
650b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		unsigned char handler;
651b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		struct nvme_completion cqe = nvmeq->cqes[head];
652821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox		if ((le16_to_cpu(cqe.status) & 1) != phase)
653b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			break;
654b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
655b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (++head == nvmeq->q_depth) {
656b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			head = 0;
657821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox			phase = !phase;
658b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		}
659b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
660b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		data = free_cmdid(nvmeq, cqe.command_id);
661b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		handler = data & 3;
662b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		ptr = (void *)(data & ~3UL);
6638de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox		nvme_completions[handler](nvmeq, ptr, &cqe);
664b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
665b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
666b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	/* If the controller ignores the cq head doorbell and continuously
667b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	 * writes to the queue, it is theoretically possible to wrap around
668b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	 * the queue twice and mistakenly return IRQ_NONE.  Linux only
669b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	 * requires that 0.1% of your interrupts are handled, so this isn't
670b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	 * a big problem.
671b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	 */
672821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox	if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
673b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return IRQ_NONE;
674b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
675b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	writel(head, nvmeq->q_db + 1);
676b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->cq_head = head;
677821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox	nvmeq->cq_phase = phase;
678b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
679b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return IRQ_HANDLED;
680b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
681b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
682b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic irqreturn_t nvme_irq(int irq, void *data)
683b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
68458ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	irqreturn_t result;
68558ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	struct nvme_queue *nvmeq = data;
68658ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	spin_lock(&nvmeq->q_lock);
68758ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	result = nvme_process_cq(nvmeq);
68858ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	spin_unlock(&nvmeq->q_lock);
68958ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	return result;
69058ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox}
69158ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox
69258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxstatic irqreturn_t nvme_irq_check(int irq, void *data)
69358ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox{
69458ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	struct nvme_queue *nvmeq = data;
69558ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
69658ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
69758ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox		return IRQ_NONE;
69858ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	return IRQ_WAKE_THREAD;
69958ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox}
70058ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox
7013c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcoxstatic void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
7023c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox{
7033c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox	spin_lock_irq(&nvmeq->q_lock);
70421075bdee0a6f56058920d889df4ae561bfed754Matthew Wilcox	cancel_cmdid(nvmeq, cmdid);
7053c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox	spin_unlock_irq(&nvmeq->q_lock);
7063c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox}
7073c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox
708b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/*
709b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Returns 0 on success.  If the result is negative, it's a Linux error code;
710b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * if the result is positive, it's an NVM Express status code
711b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
7123c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcoxstatic int nvme_submit_sync_cmd(struct nvme_queue *nvmeq,
713e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox			struct nvme_command *cmd, u32 *result, unsigned timeout)
714b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
715b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int cmdid;
716b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct sync_cmd_info cmdinfo;
717b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
718b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cmdinfo.task = current;
719b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cmdinfo.status = -EINTR;
720b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
721e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	cmdid = alloc_cmdid_killable(nvmeq, &cmdinfo, sync_completion_id,
722e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox								timeout);
723b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (cmdid < 0)
724b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return cmdid;
725b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cmd->common.command_id = cmdid;
726b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
7273c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox	set_current_state(TASK_KILLABLE);
7283c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox	nvme_submit_cmd(nvmeq, cmd);
729b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	schedule();
730b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
7313c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox	if (cmdinfo.status == -EINTR) {
7323c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox		nvme_abort_command(nvmeq, cmdid);
7333c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox		return -EINTR;
7343c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox	}
7353c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox
736b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (result)
737b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		*result = cmdinfo.result;
738b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
739b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return cmdinfo.status;
740b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
741b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
742b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
743b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox								u32 *result)
744b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
745e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT);
746b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
747b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
748b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
749b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
750b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int status;
751b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_command c;
752b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
753b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset(&c, 0, sizeof(c));
754b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.delete_queue.opcode = opcode;
755b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.delete_queue.qid = cpu_to_le16(id);
756b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
757b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	status = nvme_submit_admin_cmd(dev, &c, NULL);
758b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (status)
759b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -EIO;
760b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
761b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
762b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
763b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
764b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox						struct nvme_queue *nvmeq)
765b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
766b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int status;
767b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_command c;
768b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
769b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
770b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset(&c, 0, sizeof(c));
771b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_cq.opcode = nvme_admin_create_cq;
772b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
773b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_cq.cqid = cpu_to_le16(qid);
774b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
775b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_cq.cq_flags = cpu_to_le16(flags);
776b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
777b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
778b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	status = nvme_submit_admin_cmd(dev, &c, NULL);
779b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (status)
780b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -EIO;
781b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
782b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
783b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
784b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
785b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox						struct nvme_queue *nvmeq)
786b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
787b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int status;
788b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_command c;
789b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
790b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
791b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset(&c, 0, sizeof(c));
792b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_sq.opcode = nvme_admin_create_sq;
793b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
794b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_sq.sqid = cpu_to_le16(qid);
795b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
796b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_sq.sq_flags = cpu_to_le16(flags);
797b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_sq.cqid = cpu_to_le16(qid);
798b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
799b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	status = nvme_submit_admin_cmd(dev, &c, NULL);
800b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (status)
801b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -EIO;
802b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
803b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
804b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
805b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
806b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
807b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
808b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
809b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
810b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
811b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
812b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
813b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
814b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
815b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_free_queue(struct nvme_dev *dev, int qid)
816b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
817b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_queue *nvmeq = dev->queues[qid];
818aba2080f3f1639f9202f1a52993669844abcfb80Matthew Wilcox	int vector = dev->entry[nvmeq->cq_vector].vector;
819b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
820aba2080f3f1639f9202f1a52993669844abcfb80Matthew Wilcox	irq_set_affinity_hint(vector, NULL);
821aba2080f3f1639f9202f1a52993669844abcfb80Matthew Wilcox	free_irq(vector, nvmeq);
822b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
823b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	/* Don't tell the adapter to delete the admin queue */
824b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (qid) {
825b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		adapter_delete_sq(dev, qid);
826b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		adapter_delete_cq(dev, qid);
827b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
828b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
829b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
830b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox				(void *)nvmeq->cqes, nvmeq->cq_dma_addr);
831b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
832b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox					nvmeq->sq_cmds, nvmeq->sq_dma_addr);
833b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(nvmeq);
834b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
835b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
836b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
837b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox							int depth, int vector)
838b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
839b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct device *dmadev = &dev->pci_dev->dev;
840e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	unsigned extra = (depth / 8) + (depth * sizeof(struct nvme_cmd_info));
841b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
842b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!nvmeq)
843b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return NULL;
844b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
845b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
846b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox					&nvmeq->cq_dma_addr, GFP_KERNEL);
847b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!nvmeq->cqes)
848b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto free_nvmeq;
849b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
850b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
851b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
852b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox					&nvmeq->sq_dma_addr, GFP_KERNEL);
853b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!nvmeq->sq_cmds)
854b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto free_cqdma;
855b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
856b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->q_dmadev = dmadev;
857091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	nvmeq->dev = dev;
858b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	spin_lock_init(&nvmeq->q_lock);
859b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->cq_head = 0;
860821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox	nvmeq->cq_phase = 1;
861b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	init_waitqueue_head(&nvmeq->sq_full);
8621fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
863b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	bio_list_init(&nvmeq->sq_cong);
864b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->q_db = &dev->dbs[qid * 2];
865b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->q_depth = depth;
866b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->cq_vector = vector;
867b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
868b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return nvmeq;
869b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
870b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_cqdma:
871b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(dmadev, CQ_SIZE(nvmeq->q_depth), (void *)nvmeq->cqes,
872b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox							nvmeq->cq_dma_addr);
873b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_nvmeq:
874b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(nvmeq);
875b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return NULL;
876b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
877b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
8783001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcoxstatic int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
8793001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox							const char *name)
8803001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox{
88158ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	if (use_threaded_interrupts)
88258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox		return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
883ec6ce618d65b5ce1bef83a5509255107a0feac44Matthew Wilcox					nvme_irq_check, nvme_irq,
88458ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox					IRQF_DISABLED | IRQF_SHARED,
88558ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox					name, nvmeq);
8863001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox	return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
8873001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox				IRQF_DISABLED | IRQF_SHARED, name, nvmeq);
8883001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox}
8893001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox
890b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic __devinit struct nvme_queue *nvme_create_queue(struct nvme_dev *dev,
891b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox					int qid, int cq_size, int vector)
892b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
893b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int result;
894b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_queue *nvmeq = nvme_alloc_queue(dev, qid, cq_size, vector);
895b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
8963f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox	if (!nvmeq)
8976f0f54499f2edf7e25410cdd99e6f030f3485fd1Matthew Wilcox		return ERR_PTR(-ENOMEM);
8983f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox
899b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	result = adapter_alloc_cq(dev, qid, nvmeq);
900b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (result < 0)
901b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto free_nvmeq;
902b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
903b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	result = adapter_alloc_sq(dev, qid, nvmeq);
904b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (result < 0)
905b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto release_cq;
906b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
9073001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox	result = queue_request_irq(dev, nvmeq, "nvme");
908b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (result < 0)
909b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto release_sq;
910b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
911b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return nvmeq;
912b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
913b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox release_sq:
914b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	adapter_delete_sq(dev, qid);
915b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox release_cq:
916b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	adapter_delete_cq(dev, qid);
917b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_nvmeq:
918b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
919b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox				(void *)nvmeq->cqes, nvmeq->cq_dma_addr);
920b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
921b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox					nvmeq->sq_cmds, nvmeq->sq_dma_addr);
922b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(nvmeq);
9236f0f54499f2edf7e25410cdd99e6f030f3485fd1Matthew Wilcox	return ERR_PTR(result);
924b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
925b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
926b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_configure_admin_queue(struct nvme_dev *dev)
927b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
928b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int result;
929b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 aqa;
93022605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox	u64 cap;
93122605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox	unsigned long timeout;
932b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_queue *nvmeq;
933b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
934b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->dbs = ((void __iomem *)dev->bar) + 4096;
935b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
936b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
9373f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox	if (!nvmeq)
9383f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox		return -ENOMEM;
939b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
940b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	aqa = nvmeq->q_depth - 1;
941b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	aqa |= aqa << 16;
942b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
943b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
944b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
945b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
9467f53f9d2424533256ae86f7df5661a17de743de8Matthew Wilcox	dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
947b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
9485911f20039ce59d7e7834f0c42151cf759b6f786Shane Michael Matthews	writel(0, &dev->bar->cc);
949b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	writel(aqa, &dev->bar->aqa);
950b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
951b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
952b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	writel(dev->ctrl_config, &dev->bar->cc);
953b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
95422605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox	cap = readq(&dev->bar->cap);
95522605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox	timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
95622605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox
957b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	while (!(readl(&dev->bar->csts) & NVME_CSTS_RDY)) {
958b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		msleep(100);
959b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (fatal_signal_pending(current))
960b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			return -EINTR;
96122605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox		if (time_after(jiffies, timeout)) {
96222605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox			dev_err(&dev->pci_dev->dev,
96322605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox				"Device not ready; aborting initialisation\n");
96422605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox			return -ENODEV;
96522605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox		}
966b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
967b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
9683001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox	result = queue_request_irq(dev, nvmeq, "nvme admin");
969b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->queues[0] = nvmeq;
970b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return result;
971b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
972b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
9737fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcoxstatic int nvme_map_user_pages(struct nvme_dev *dev, int write,
9747fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox				unsigned long addr, unsigned length,
9757fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox				struct scatterlist **sgp)
976b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
97736c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	int i, err, count, nents, offset;
9787fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	struct scatterlist *sg;
9797fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	struct page **pages;
98036c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox
98136c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	if (addr & 3)
98236c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox		return -EINVAL;
9837fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	if (!length)
9847fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox		return -EINVAL;
9857fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox
98636c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	offset = offset_in_page(addr);
9877fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
9887fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
98936c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox
99036c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	err = get_user_pages_fast(addr, count, 1, pages);
99136c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	if (err < count) {
99236c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox		count = err;
99336c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox		err = -EFAULT;
99436c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox		goto put_pages;
99536c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	}
9967fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox
9977fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	sg = kcalloc(count, sizeof(*sg), GFP_KERNEL);
99836c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	sg_init_table(sg, count);
999ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	sg_set_page(&sg[0], pages[0], PAGE_SIZE - offset, offset);
10007fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	length -= (PAGE_SIZE - offset);
10017fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	for (i = 1; i < count; i++) {
10027fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox		sg_set_page(&sg[i], pages[i], min_t(int, length, PAGE_SIZE), 0);
10037fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox		length -= PAGE_SIZE;
10047fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	}
10057fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox
10067fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	err = -ENOMEM;
10077fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
10087fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox				write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
100936c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	if (!nents)
101036c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox		goto put_pages;
1011b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
10127fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	kfree(pages);
10137fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	*sgp = sg;
10147fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	return nents;
1015b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
10167fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox put_pages:
10177fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	for (i = 0; i < count; i++)
10187fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox		put_page(pages[i]);
10197fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	kfree(pages);
10207fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	return err;
10217fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox}
1022b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
10237fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcoxstatic void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
10247fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox				unsigned long addr, int length,
10257fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox				struct scatterlist *sg, int nents)
10267fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox{
10277fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	int i, count;
1028b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
10297fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	count = DIV_ROUND_UP(offset_in_page(addr) + length, PAGE_SIZE);
103036c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	dma_unmap_sg(&dev->pci_dev->dev, sg, nents, DMA_FROM_DEVICE);
10317fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox
103236c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	for (i = 0; i < count; i++)
10337fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox		put_page(sg_page(&sg[i]));
10347fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox}
1035b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1036a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcoxstatic int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1037a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox{
1038a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	struct nvme_dev *dev = ns->dev;
1039a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	struct nvme_queue *nvmeq;
1040a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	struct nvme_user_io io;
1041a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	struct nvme_command c;
1042a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	unsigned length;
1043a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	int nents, status;
1044a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	struct scatterlist *sg;
1045e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	struct nvme_prps *prps;
1046a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox
1047a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	if (copy_from_user(&io, uio, sizeof(io)))
1048a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox		return -EFAULT;
10496c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	length = (io.nblocks + 1) << ns->lba_shift;
10506c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox
10516c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	switch (io.opcode) {
10526c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	case nvme_cmd_write:
10536c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	case nvme_cmd_read:
10546bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	case nvme_cmd_compare:
10556c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox		nents = nvme_map_user_pages(dev, io.opcode & 1, io.addr,
10566c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox								length, &sg);
10576c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	default:
10586bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox		return -EINVAL;
10596c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	}
10606c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox
1061a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	if (nents < 0)
1062a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox		return nents;
1063a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox
1064a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	memset(&c, 0, sizeof(c));
1065a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.opcode = io.opcode;
1066a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.flags = io.flags;
10676c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	c.rw.nsid = cpu_to_le32(ns->ns_id);
1068a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.slba = cpu_to_le64(io.slba);
10696c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	c.rw.length = cpu_to_le16(io.nblocks);
1070a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.control = cpu_to_le16(io.control);
1071a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.dsmgmt = cpu_to_le16(io.dsmgmt);
10726c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	c.rw.reftag = io.reftag;
10736c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	c.rw.apptag = io.apptag;
10746c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	c.rw.appmask = io.appmask;
1075a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	/* XXX: metadata */
1076b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox	prps = nvme_setup_prps(dev, &c.common, sg, &length, GFP_KERNEL);
1077a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox
1078d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox	nvmeq = get_nvmeq(ns);
1079fa92282149842645931580225647238428374758Matthew Wilcox	/*
1080fa92282149842645931580225647238428374758Matthew Wilcox	 * Since nvme_submit_sync_cmd sleeps, we can't keep preemption
1081b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox	 * disabled.  We may be preempted at any point, and be rescheduled
1082b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox	 * to a different CPU.  That will cause cacheline bouncing, but no
1083b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox	 * additional races since q_lock already protects against other CPUs.
1084b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox	 */
1085a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	put_nvmeq(nvmeq);
1086b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox	if (length != (io.nblocks + 1) << ns->lba_shift)
1087b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox		status = -ENOMEM;
1088b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox	else
1089b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox		status = nvme_submit_sync_cmd(nvmeq, &c, NULL, IO_TIMEOUT);
1090a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox
1091a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	nvme_unmap_user_pages(dev, io.opcode & 1, io.addr, length, sg, nents);
1092d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox	nvme_free_prps(dev, prps);
1093a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	return status;
1094a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox}
1095a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox
10966bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcoxstatic int nvme_user_admin_cmd(struct nvme_ns *ns,
10976bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox					struct nvme_admin_cmd __user *ucmd)
10986ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox{
10996ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	struct nvme_dev *dev = ns->dev;
11006bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	struct nvme_admin_cmd cmd;
11016ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	struct nvme_command c;
11026bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	int status, length, nents = 0;
11036ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	struct scatterlist *sg;
11046bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	struct nvme_prps *prps = NULL;
11056ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox
11066bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	if (!capable(CAP_SYS_ADMIN))
11076bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox		return -EACCES;
11086bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
11096ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox		return -EFAULT;
11106ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox
11116ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	memset(&c, 0, sizeof(c));
11126bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.opcode = cmd.opcode;
11136bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.flags = cmd.flags;
11146bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.nsid = cpu_to_le32(cmd.nsid);
11156bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
11166bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
11176bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
11186bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
11196bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
11206bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
11216bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
11226bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
11236bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox
11246bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	length = cmd.data_len;
11256bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	if (cmd.data_len) {
11266bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox		nents = nvme_map_user_pages(dev, 1, cmd.addr, length, &sg);
11276bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox		if (nents < 0)
11286bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox			return nents;
11296bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox		prps = nvme_setup_prps(dev, &c.common, sg, &length, GFP_KERNEL);
11306bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	}
11316bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox
11326bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	if (length != cmd.data_len)
1133b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox		status = -ENOMEM;
1134b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox	else
1135b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox		status = nvme_submit_admin_cmd(dev, &c, NULL);
11366bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	if (cmd.data_len) {
11376bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox		nvme_unmap_user_pages(dev, 0, cmd.addr, cmd.data_len, sg,
11386bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox									nents);
11396bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox		nvme_free_prps(dev, prps);
11406bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	}
11416ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	return status;
11426ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox}
11436ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox
1144b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1145b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox							unsigned long arg)
1146b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1147b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_ns *ns = bdev->bd_disk->private_data;
1148b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1149b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	switch (cmd) {
11506bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	case NVME_IOCTL_ID:
11516bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox		return ns->ns_id;
11526bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	case NVME_IOCTL_ADMIN_CMD:
11536bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox		return nvme_user_admin_cmd(ns, (void __user *)arg);
1154a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	case NVME_IOCTL_SUBMIT_IO:
1155a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox		return nvme_submit_io(ns, (void __user *)arg);
1156b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	default:
1157b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -ENOTTY;
1158b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1159b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1160b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1161b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic const struct block_device_operations nvme_fops = {
1162b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.owner		= THIS_MODULE,
1163b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.ioctl		= nvme_ioctl,
11644948168280b269a514045766ddd872cfac5968e1Matthew Wilcox	.compat_ioctl	= nvme_ioctl,
1165b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
1166b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
11678de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcoxstatic void nvme_timeout_ios(struct nvme_queue *nvmeq)
11688de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox{
11698de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox	int depth = nvmeq->q_depth - 1;
11708de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox	struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
11718de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox	unsigned long now = jiffies;
11728de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox	int cmdid;
11738de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox
11748de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox	for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
11758de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox		unsigned long data;
11768de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox		void *ptr;
11778de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox		unsigned char handler;
11788de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox		static struct nvme_completion cqe = { .status = cpu_to_le16(NVME_SC_ABORT_REQ) << 1, };
11798de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox
11808de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox		if (!time_after(now, info[cmdid].timeout))
11818de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox			continue;
11828de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox		dev_warn(nvmeq->q_dmadev, "Timing out I/O %d\n", cmdid);
11838de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox		data = cancel_cmdid(nvmeq, cmdid);
11848de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox		handler = data & 3;
11858de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox		ptr = (void *)(data & ~3UL);
11868de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox		nvme_completions[handler](nvmeq, ptr, &cqe);
11878de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox	}
11888de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox}
11898de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox
11901fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic void nvme_resubmit_bios(struct nvme_queue *nvmeq)
11911fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox{
11921fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	while (bio_list_peek(&nvmeq->sq_cong)) {
11931fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
11941fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
11951fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
11961fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox			bio_list_add_head(&nvmeq->sq_cong, bio);
11971fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox			break;
11981fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		}
11993cb967c03926edd2c414082f4cc0feb7b372edaeMatthew Wilcox		if (bio_list_empty(&nvmeq->sq_cong))
12003cb967c03926edd2c414082f4cc0feb7b372edaeMatthew Wilcox			remove_wait_queue(&nvmeq->sq_full,
12013cb967c03926edd2c414082f4cc0feb7b372edaeMatthew Wilcox							&nvmeq->sq_cong_wait);
12021fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	}
12031fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox}
12041fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox
12051fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic int nvme_kthread(void *data)
12061fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox{
12071fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	struct nvme_dev *dev;
12081fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox
12091fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	while (!kthread_should_stop()) {
12101fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		__set_current_state(TASK_RUNNING);
12111fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		spin_lock(&dev_list_lock);
12121fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		list_for_each_entry(dev, &dev_list, node) {
12131fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox			int i;
12141fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox			for (i = 0; i < dev->queue_count; i++) {
12151fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox				struct nvme_queue *nvmeq = dev->queues[i];
1216740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox				if (!nvmeq)
1217740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox					continue;
12181fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox				spin_lock_irq(&nvmeq->q_lock);
12191fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox				if (nvme_process_cq(nvmeq))
12201fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox					printk("process_cq did something\n");
12218de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox				nvme_timeout_ios(nvmeq);
12221fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox				nvme_resubmit_bios(nvmeq);
12231fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox				spin_unlock_irq(&nvmeq->q_lock);
12241fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox			}
12251fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		}
12261fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		spin_unlock(&dev_list_lock);
12271fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		set_current_state(TASK_INTERRUPTIBLE);
12281fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		schedule_timeout(HZ);
12291fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	}
12301fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	return 0;
12311fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox}
12321fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox
12335aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcoxstatic DEFINE_IDA(nvme_index_ida);
12345aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox
12355aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcoxstatic int nvme_get_ns_idx(void)
12365aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox{
12375aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	int index, error;
12385aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox
12395aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	do {
12405aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox		if (!ida_pre_get(&nvme_index_ida, GFP_KERNEL))
12415aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox			return -1;
12425aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox
12435aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox		spin_lock(&dev_list_lock);
12445aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox		error = ida_get_new(&nvme_index_ida, &index);
12455aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox		spin_unlock(&dev_list_lock);
12465aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	} while (error == -EAGAIN);
12475aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox
12485aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	if (error)
12495aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox		index = -1;
12505aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	return index;
12515aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox}
12525aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox
12535aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcoxstatic void nvme_put_ns_idx(int index)
12545aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox{
12555aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	spin_lock(&dev_list_lock);
12565aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	ida_remove(&nvme_index_ida, index);
12575aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	spin_unlock(&dev_list_lock);
12585aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox}
12595aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox
12605aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcoxstatic struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, int nsid,
1261b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1262b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1263b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_ns *ns;
1264b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct gendisk *disk;
1265b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int lbaf;
1266b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1267b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1268b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return NULL;
1269b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1270b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1271b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!ns)
1272b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return NULL;
1273b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns->queue = blk_alloc_queue(GFP_KERNEL);
1274b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!ns->queue)
1275b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto out_free_ns;
1276b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns->queue->queue_flags = QUEUE_FLAG_DEFAULT | QUEUE_FLAG_NOMERGES |
1277b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox				QUEUE_FLAG_NONROT | QUEUE_FLAG_DISCARD;
1278b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	blk_queue_make_request(ns->queue, nvme_make_request);
1279b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns->dev = dev;
1280b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns->queue->queuedata = ns;
1281b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1282b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk = alloc_disk(NVME_MINORS);
1283b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!disk)
1284b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto out_free_queue;
12855aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	ns->ns_id = nsid;
1286b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns->disk = disk;
1287b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	lbaf = id->flbas & 0xf;
1288b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns->lba_shift = id->lbaf[lbaf].ds;
1289b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1290b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk->major = nvme_major;
1291b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk->minors = NVME_MINORS;
12925aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	disk->first_minor = NVME_MINORS * nvme_get_ns_idx();
1293b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk->fops = &nvme_fops;
1294b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk->private_data = ns;
1295b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk->queue = ns->queue;
1296388f037f4e7f0a24bac6b1a24f144f5d939f58cfMatthew Wilcox	disk->driverfs_dev = &dev->pci_dev->dev;
12975aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
1298b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1299b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1300b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return ns;
1301b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1302b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free_queue:
1303b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	blk_cleanup_queue(ns->queue);
1304b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free_ns:
1305b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(ns);
1306b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return NULL;
1307b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1308b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1309b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_ns_free(struct nvme_ns *ns)
1310b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
13115aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	int index = ns->disk->first_minor / NVME_MINORS;
1312b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	put_disk(ns->disk);
13135aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	nvme_put_ns_idx(index);
1314b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	blk_cleanup_queue(ns->queue);
1315b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(ns);
1316b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1317b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1318b3b06812e199f248561ce7824a4a8a9cd573c05aMatthew Wilcoxstatic int set_queue_count(struct nvme_dev *dev, int count)
1319b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1320b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int status;
1321b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 result;
1322b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_command c;
1323b3b06812e199f248561ce7824a4a8a9cd573c05aMatthew Wilcox	u32 q_count = (count - 1) | ((count - 1) << 16);
1324b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1325b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset(&c, 0, sizeof(c));
1326b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.features.opcode = nvme_admin_get_features;
1327b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.features.fid = cpu_to_le32(NVME_FEAT_NUM_QUEUES);
1328b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.features.dword11 = cpu_to_le32(q_count);
1329b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1330b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	status = nvme_submit_admin_cmd(dev, &c, &result);
1331b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (status)
1332b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -EIO;
1333b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return min(result & 0xffff, result >> 16) + 1;
1334b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1335b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1336b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_setup_io_queues(struct nvme_dev *dev)
1337b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1338b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox	int result, cpu, i, nr_io_queues;
1339b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1340b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox	nr_io_queues = num_online_cpus();
1341b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox	result = set_queue_count(dev, nr_io_queues);
13421b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	if (result < 0)
13431b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		return result;
1344b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox	if (result < nr_io_queues)
1345b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox		nr_io_queues = result;
1346b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
13471b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	/* Deregister the admin queue's interrupt */
13481b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	free_irq(dev->entry[0].vector, dev->queues[0]);
13491b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox
1350b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox	for (i = 0; i < nr_io_queues; i++)
13511b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		dev->entry[i].entry = i;
13521b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	for (;;) {
1353b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox		result = pci_enable_msix(dev->pci_dev, dev->entry,
1354b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox								nr_io_queues);
13551b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		if (result == 0) {
13561b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox			break;
13571b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		} else if (result > 0) {
1358b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox			nr_io_queues = result;
13591b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox			continue;
13601b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		} else {
1361b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox			nr_io_queues = 1;
13621b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox			break;
13631b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		}
13641b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	}
13651b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox
13661b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	result = queue_request_irq(dev, dev->queues[0], "nvme admin");
13671b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	/* XXX: handle failure here */
13681b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox
13691b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	cpu = cpumask_first(cpu_online_mask);
1370b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox	for (i = 0; i < nr_io_queues; i++) {
13711b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu));
13721b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		cpu = cpumask_next(cpu, cpu_online_mask);
13731b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	}
13741b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox
1375b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox	for (i = 0; i < nr_io_queues; i++) {
13761b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		dev->queues[i + 1] = nvme_create_queue(dev, i + 1,
13771b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox							NVME_Q_DEPTH, i);
13786f0f54499f2edf7e25410cdd99e6f030f3485fd1Matthew Wilcox		if (IS_ERR(dev->queues[i + 1]))
13796f0f54499f2edf7e25410cdd99e6f030f3485fd1Matthew Wilcox			return PTR_ERR(dev->queues[i + 1]);
13801b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		dev->queue_count++;
13811b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	}
1382b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
13839ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox	for (; i < num_possible_cpus(); i++) {
13849ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox		int target = i % rounddown_pow_of_two(dev->queue_count - 1);
13859ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox		dev->queues[i + 1] = dev->queues[target + 1];
13869ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox	}
13879ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox
1388b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
1389b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1390b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1391b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_free_queues(struct nvme_dev *dev)
1392b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1393b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int i;
1394b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1395b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	for (i = dev->queue_count - 1; i >= 0; i--)
1396b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		nvme_free_queue(dev, i);
1397b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1398b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1399b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_dev_add(struct nvme_dev *dev)
1400b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1401b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int res, nn, i;
1402b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_ns *ns, *next;
140351814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	struct nvme_id_ctrl *ctrl;
1404b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	void *id;
1405b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_addr_t dma_addr;
1406b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_command cid, crt;
1407b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1408b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	res = nvme_setup_io_queues(dev);
1409b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (res)
1410b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return res;
1411b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1412b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	/* XXX: Switch to a SG list once prp2 works */
1413b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	id = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr,
1414b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox								GFP_KERNEL);
1415b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1416b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset(&cid, 0, sizeof(cid));
1417b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cid.identify.opcode = nvme_admin_identify;
1418b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cid.identify.nsid = 0;
1419b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cid.identify.prp1 = cpu_to_le64(dma_addr);
1420b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cid.identify.cns = cpu_to_le32(1);
1421b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1422b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	res = nvme_submit_admin_cmd(dev, &cid, NULL);
1423b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (res) {
1424b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		res = -EIO;
1425b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto out_free;
1426b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1427b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
142851814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	ctrl = id;
142951814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	nn = le32_to_cpup(&ctrl->nn);
143051814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
143151814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
143251814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
1433b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1434b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cid.identify.cns = 0;
1435b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset(&crt, 0, sizeof(crt));
1436b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	crt.features.opcode = nvme_admin_get_features;
1437b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	crt.features.prp1 = cpu_to_le64(dma_addr + 4096);
1438b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	crt.features.fid = cpu_to_le32(NVME_FEAT_LBA_RANGE);
1439b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1440ac88c36a385b848cb9efcb877fdfc4153a60bcabMatthew Wilcox	for (i = 0; i <= nn; i++) {
1441b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		cid.identify.nsid = cpu_to_le32(i);
1442b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		res = nvme_submit_admin_cmd(dev, &cid, NULL);
1443b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (res)
1444b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			continue;
1445b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1446b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (((struct nvme_id_ns *)id)->ncap == 0)
1447b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			continue;
1448b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1449b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		crt.features.nsid = cpu_to_le32(i);
1450b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		res = nvme_submit_admin_cmd(dev, &crt, NULL);
1451b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (res)
1452b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			continue;
1453b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1454b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		ns = nvme_alloc_ns(dev, i, id, id + 4096);
1455b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (ns)
1456b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			list_add_tail(&ns->list, &dev->namespaces);
1457b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1458b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	list_for_each_entry(ns, &dev->namespaces, list)
1459b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		add_disk(ns->disk);
1460b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1461b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr);
1462b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
1463b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1464b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free:
1465b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
1466b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		list_del(&ns->list);
1467b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		nvme_ns_free(ns);
1468b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1469b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1470b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr);
1471b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return res;
1472b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1473b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1474b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_dev_remove(struct nvme_dev *dev)
1475b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1476b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_ns *ns, *next;
1477b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
14781fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	spin_lock(&dev_list_lock);
14791fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	list_del(&dev->node);
14801fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	spin_unlock(&dev_list_lock);
14811fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox
1482b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	/* TODO: wait all I/O finished or cancel them */
1483b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1484b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
1485b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		list_del(&ns->list);
1486b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		del_gendisk(ns->disk);
1487b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		nvme_ns_free(ns);
1488b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1489b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1490b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_free_queues(dev);
1491b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1492b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
1493b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1494b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1495091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcoxstatic int nvme_setup_prp_pools(struct nvme_dev *dev)
1496091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox{
1497091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	struct device *dmadev = &dev->pci_dev->dev;
1498091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
1499091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox						PAGE_SIZE, PAGE_SIZE, 0);
1500091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	if (!dev->prp_page_pool)
1501091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox		return -ENOMEM;
1502091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox
150399802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	/* Optimisation for I/Os between 4k and 128k */
150499802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
150599802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox						256, 256, 0);
150699802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	if (!dev->prp_small_pool) {
150799802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox		dma_pool_destroy(dev->prp_page_pool);
150899802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox		return -ENOMEM;
150999802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	}
1510091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	return 0;
1511091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox}
1512091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox
1513091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcoxstatic void nvme_release_prp_pools(struct nvme_dev *dev)
1514091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox{
1515091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	dma_pool_destroy(dev->prp_page_pool);
151699802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	dma_pool_destroy(dev->prp_small_pool);
1517091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox}
1518091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox
1519b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* XXX: Use an ida or something to let remove / add work correctly */
1520b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_set_instance(struct nvme_dev *dev)
1521b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1522b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	static int instance;
1523b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->instance = instance++;
1524b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1525b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1526b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_release_instance(struct nvme_dev *dev)
1527b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1528b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1529b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1530b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_probe(struct pci_dev *pdev,
1531b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox						const struct pci_device_id *id)
1532b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1533574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox	int bars, result = -ENOMEM;
1534b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_dev *dev;
1535b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1536b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1537b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!dev)
1538b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -ENOMEM;
1539b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
1540b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox								GFP_KERNEL);
1541b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!dev->entry)
1542b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto free;
15431b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
15441b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox								GFP_KERNEL);
1545b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!dev->queues)
1546b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto free;
1547b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
15480ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews	if (pci_enable_device_mem(pdev))
15490ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews		goto free;
1550f64d3365a3e5cb46e69db7e2c82a7cb9a5bed1b8Matthew Wilcox	pci_set_master(pdev);
1551574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox	bars = pci_select_bars(pdev, IORESOURCE_MEM);
1552574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox	if (pci_request_selected_regions(pdev, bars, "nvme"))
1553574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox		goto disable;
15540ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews
1555b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	INIT_LIST_HEAD(&dev->namespaces);
1556b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->pci_dev = pdev;
1557b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	pci_set_drvdata(pdev, dev);
15582930353f9f2b9e4629e935acd970cb73c1171229Matthew Wilcox	dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
15592930353f9f2b9e4629e935acd970cb73c1171229Matthew Wilcox	dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
1560b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_set_instance(dev);
156153c9577e9ca68a633c6e9df2b54eaecacfa77f62Matthew Wilcox	dev->entry[0].vector = pdev->irq;
1562b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1563091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	result = nvme_setup_prp_pools(dev);
1564091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	if (result)
1565091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox		goto disable_msix;
1566091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox
1567b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1568b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!dev->bar) {
1569b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		result = -ENOMEM;
1570574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox		goto disable_msix;
1571b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1572b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1573b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	result = nvme_configure_admin_queue(dev);
1574b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (result)
1575b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto unmap;
1576b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->queue_count++;
1577b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
15781fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	spin_lock(&dev_list_lock);
15791fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	list_add(&dev->node, &dev_list);
15801fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	spin_unlock(&dev_list_lock);
15811fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox
1582740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox	result = nvme_dev_add(dev);
1583740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox	if (result)
1584740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox		goto delete;
1585740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox
1586b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
1587b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1588b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox delete:
1589740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox	spin_lock(&dev_list_lock);
1590740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox	list_del(&dev->node);
1591740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox	spin_unlock(&dev_list_lock);
1592740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox
1593b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_free_queues(dev);
1594b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unmap:
1595b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	iounmap(dev->bar);
1596574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox disable_msix:
1597b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	pci_disable_msix(pdev);
1598b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_release_instance(dev);
1599091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	nvme_release_prp_pools(dev);
1600574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox disable:
16010ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews	pci_disable_device(pdev);
1602574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox	pci_release_regions(pdev);
1603b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free:
1604b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(dev->queues);
1605b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(dev->entry);
1606b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(dev);
1607b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return result;
1608b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1609b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1610b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void __devexit nvme_remove(struct pci_dev *pdev)
1611b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1612b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_dev *dev = pci_get_drvdata(pdev);
1613b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_dev_remove(dev);
1614b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	pci_disable_msix(pdev);
1615b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	iounmap(dev->bar);
1616b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_release_instance(dev);
1617091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	nvme_release_prp_pools(dev);
16180ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews	pci_disable_device(pdev);
1619574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox	pci_release_regions(pdev);
1620b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(dev->queues);
1621b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(dev->entry);
1622b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(dev);
1623b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1624b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1625b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* These functions are yet to be implemented */
1626b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_error_detected NULL
1627b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_dump_registers NULL
1628b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_link_reset NULL
1629b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_slot_reset NULL
1630b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_error_resume NULL
1631b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_suspend NULL
1632b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_resume NULL
1633b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1634b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct pci_error_handlers nvme_err_handler = {
1635b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.error_detected	= nvme_error_detected,
1636b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.mmio_enabled	= nvme_dump_registers,
1637b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.link_reset	= nvme_link_reset,
1638b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.slot_reset	= nvme_slot_reset,
1639b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.resume		= nvme_error_resume,
1640b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
1641b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1642b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* Move to pci_ids.h later */
1643b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define PCI_CLASS_STORAGE_EXPRESS	0x010802
1644b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1645b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
1646b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	{ PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
1647b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	{ 0, }
1648b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
1649b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_DEVICE_TABLE(pci, nvme_id_table);
1650b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1651b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct pci_driver nvme_driver = {
1652b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.name		= "nvme",
1653b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.id_table	= nvme_id_table,
1654b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.probe		= nvme_probe,
1655b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.remove		= __devexit_p(nvme_remove),
1656b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.suspend	= nvme_suspend,
1657b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.resume		= nvme_resume,
1658b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.err_handler	= &nvme_err_handler,
1659b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
1660b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1661b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __init nvme_init(void)
1662b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
16631fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	int result = -EBUSY;
16641fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox
16651fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
16661fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	if (IS_ERR(nvme_thread))
16671fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		return PTR_ERR(nvme_thread);
1668b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1669b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_major = register_blkdev(nvme_major, "nvme");
1670b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (nvme_major <= 0)
16711fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		goto kill_kthread;
1672b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1673b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	result = pci_register_driver(&nvme_driver);
16741fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	if (result)
16751fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		goto unregister_blkdev;
16761fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	return 0;
1677b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
16781fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox unregister_blkdev:
1679b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	unregister_blkdev(nvme_major, "nvme");
16801fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox kill_kthread:
16811fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	kthread_stop(nvme_thread);
1682b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return result;
1683b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1684b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1685b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void __exit nvme_exit(void)
1686b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1687b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	pci_unregister_driver(&nvme_driver);
1688b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	unregister_blkdev(nvme_major, "nvme");
16891fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	kthread_stop(nvme_thread);
1690b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1691b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1692b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
1693b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_LICENSE("GPL");
1694be5e09484078e95af20acb13e215cd8aec705893Matthew WilcoxMODULE_VERSION("0.6");
1695b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_init(nvme_init);
1696b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_exit(nvme_exit);
1697