nvme-core.c revision 4eeb9215a0d5c9494ca8b20158cc8ee82618840c
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
48ff976d724a74e4522e9ca2de1fb37ac4520f454fMatthew Wilcox#define NVME_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;
73f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox	int db_stride;
74b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 ctrl_config;
75b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct msix_entry *entry;
76b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_bar __iomem *bar;
77b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct list_head namespaces;
7851814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	char serial[20];
7951814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	char model[40];
8051814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	char firmware_rev[8];
81b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
82b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
83b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/*
84b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * An NVM Express namespace is equivalent to a SCSI LUN
85b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
86b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct nvme_ns {
87b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct list_head list;
88b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
89b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_dev *dev;
90b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct request_queue *queue;
91b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct gendisk *disk;
92b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
93b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int ns_id;
94b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int lba_shift;
95b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
96b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
97b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/*
98b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * An NVM Express queue.  Each device has at least two (one for admin
99b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * commands and one for I/O commands).
100b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
101b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct nvme_queue {
102b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct device *q_dmadev;
103091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	struct nvme_dev *dev;
104b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	spinlock_t q_lock;
105b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_command *sq_cmds;
106b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	volatile struct nvme_completion *cqes;
107b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_addr_t sq_dma_addr;
108b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_addr_t cq_dma_addr;
109b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	wait_queue_head_t sq_full;
1101fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	wait_queue_t sq_cong_wait;
111b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct bio_list sq_cong;
112b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 __iomem *q_db;
113b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 q_depth;
114b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 cq_vector;
115b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 sq_head;
116b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 sq_tail;
117b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 cq_head;
118821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox	u16 cq_phase;
119b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	unsigned long cmdid_data[];
120b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
121b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
122b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/*
123b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Check we didin't inadvertently grow the command struct
124b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
125b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic inline void _nvme_check_size(void)
126b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
127b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
128b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
129b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
130b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
131b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
132b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
133b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
134b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
135b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
136b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
137b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1385c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcoxtypedef void (*nvme_completion_fn)(struct nvme_dev *, void *,
139c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox						struct nvme_completion *);
140c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox
141e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstruct nvme_cmd_info {
142c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	nvme_completion_fn fn;
143c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	void *ctx;
144e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	unsigned long timeout;
145e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox};
146e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox
147e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstatic struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
148e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox{
149e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
150e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox}
151e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox
152b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/**
153714a7a22884b74862540bc84955274d86b2f6040Matthew Wilcox * alloc_cmdid() - Allocate a Command ID
154714a7a22884b74862540bc84955274d86b2f6040Matthew Wilcox * @nvmeq: The queue that will be used for this command
155714a7a22884b74862540bc84955274d86b2f6040Matthew Wilcox * @ctx: A pointer that will be passed to the handler
156c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox * @handler: The function to call on completion
157b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox *
158b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Allocate a Command ID for a queue.  The data passed in will
159b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * be passed to the completion handler.  This is implemented by using
160b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * the bottom two bits of the ctx pointer to store the handler ID.
161b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Passing in a pointer that's not 4-byte aligned will cause a BUG.
162b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * We can change this if it becomes a problem.
163184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox *
164184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox * May be called with local interrupts disabled and the q_lock held,
165184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox * or with interrupts enabled and no locks held.
166b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
167c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcoxstatic int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx,
168c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox				nvme_completion_fn handler, unsigned timeout)
169b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
170e6d15f79f997a98b3a69abbc462fc9041cc1a7b4Matthew Wilcox	int depth = nvmeq->q_depth - 1;
171e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
172b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int cmdid;
173b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
174b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	do {
175b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
176b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (cmdid >= depth)
177b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			return -EBUSY;
178b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	} while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
179b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
180c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	info[cmdid].fn = handler;
181c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	info[cmdid].ctx = ctx;
182e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	info[cmdid].timeout = jiffies + timeout;
183b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return cmdid;
184b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
185b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
186b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
187c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox				nvme_completion_fn handler, unsigned timeout)
188b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
189b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int cmdid;
190b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	wait_event_killable(nvmeq->sq_full,
191e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox		(cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
192b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return (cmdid < 0) ? -EINTR : cmdid;
193b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
194b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
195c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox/* Special values must be less than 0x1000 */
196c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox#define CMD_CTX_BASE		((void *)POISON_POINTER_DELTA)
197d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_CANCELLED	(0x30C + CMD_CTX_BASE)
198d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_COMPLETED	(0x310 + CMD_CTX_BASE)
199d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_INVALID		(0x314 + CMD_CTX_BASE)
20000df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox#define CMD_CTX_FLUSH		(0x318 + CMD_CTX_BASE)
201be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox
2025c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcoxstatic void special_completion(struct nvme_dev *dev, void *ctx,
203c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox						struct nvme_completion *cqe)
204c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox{
205c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	if (ctx == CMD_CTX_CANCELLED)
206c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox		return;
207c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	if (ctx == CMD_CTX_FLUSH)
208c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox		return;
209c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	if (ctx == CMD_CTX_COMPLETED) {
2105c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox		dev_warn(&dev->pci_dev->dev,
211c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox				"completed id %d twice on queue %d\n",
212c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox				cqe->command_id, le16_to_cpup(&cqe->sq_id));
213c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox		return;
214c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	}
215c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	if (ctx == CMD_CTX_INVALID) {
2165c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox		dev_warn(&dev->pci_dev->dev,
217c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox				"invalid id %d completed on queue %d\n",
218c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox				cqe->command_id, le16_to_cpup(&cqe->sq_id));
219c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox		return;
220c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	}
221c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox
2225c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox	dev_warn(&dev->pci_dev->dev, "Unknown special completion %p\n", ctx);
223c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox}
224c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox
225184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox/*
226184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox * Called with local interrupts disabled and the q_lock held.  May not sleep.
227184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox */
228c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcoxstatic void *free_cmdid(struct nvme_queue *nvmeq, int cmdid,
229c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox						nvme_completion_fn *fn)
230b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
231c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	void *ctx;
232e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
233b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
234c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	if (cmdid >= nvmeq->q_depth) {
235c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox		*fn = special_completion;
23648e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox		return CMD_CTX_INVALID;
237c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	}
238c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	*fn = info[cmdid].fn;
239c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	ctx = info[cmdid].ctx;
240c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	info[cmdid].fn = special_completion;
241e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	info[cmdid].ctx = CMD_CTX_COMPLETED;
242b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	clear_bit(cmdid, nvmeq->cmdid_data);
243b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	wake_up(&nvmeq->sq_full);
244c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	return ctx;
245b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
246b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
247c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcoxstatic void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
248c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox						nvme_completion_fn *fn)
2493c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox{
250c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	void *ctx;
251e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
252c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	if (fn)
253c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox		*fn = info[cmdid].fn;
254c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	ctx = info[cmdid].ctx;
255c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	info[cmdid].fn = special_completion;
256e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	info[cmdid].ctx = CMD_CTX_CANCELLED;
257c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	return ctx;
2583c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox}
2593c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox
260040a93b52a9eee8177ebaf2ba0ee0f9f518d1bf8Matthew Wilcoxstatic struct nvme_queue *get_nvmeq(struct nvme_dev *dev)
261b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
262040a93b52a9eee8177ebaf2ba0ee0f9f518d1bf8Matthew Wilcox	return dev->queues[get_cpu() + 1];
263b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
264b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
265b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void put_nvmeq(struct nvme_queue *nvmeq)
266b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
2671b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	put_cpu();
268b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
269b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
270b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/**
271714a7a22884b74862540bc84955274d86b2f6040Matthew Wilcox * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
272b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @nvmeq: The queue to use
273b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @cmd: The command to send
274b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox *
275b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Safe to use from interrupt context
276b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
277b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
278b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
279b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	unsigned long flags;
280b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 tail;
281b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	spin_lock_irqsave(&nvmeq->q_lock, flags);
282b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	tail = nvmeq->sq_tail;
283b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
284b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (++tail == nvmeq->q_depth)
285b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		tail = 0;
2867547881d0951384f9833ec3a80fac8f3f16f3b98Matthew Wilcox	writel(tail, nvmeq->q_db);
287b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->sq_tail = tail;
288b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	spin_unlock_irqrestore(&nvmeq->q_lock, flags);
289b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
290b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
291b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
292b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
293eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox/*
294eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox * The nvme_iod describes the data in an I/O, including the list of PRP
295eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox * entries.  You can't see it in this data structure because C doesn't let
296eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox * me express that.  Use nvme_alloc_iod to ensure there's enough space
297eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox * allocated to store the PRP list.
298eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox */
299eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcoxstruct nvme_iod {
300eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	void *private;		/* For the use of the submitter of the I/O */
301eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	int npages;		/* In the PRP list. 0 means small pool in use */
302eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	int offset;		/* Of PRP list */
303eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	int nents;		/* Used in scatterlist */
304eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	int length;		/* Of data, in bytes */
305e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	dma_addr_t first_dma;
306eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	struct scatterlist sg[0];
307e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews};
308e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews
309eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcoxstatic __le64 **iod_list(struct nvme_iod *iod)
310e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews{
311eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	return ((void *)iod) + iod->offset;
312e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews}
313e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews
314eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox/*
315eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox * Will slightly overestimate the number of pages needed.  This is OK
316eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox * as it only leads to a small amount of wasted memory for the lifetime of
317eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox * the I/O.
318eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox */
319eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcoxstatic int nvme_npages(unsigned size)
320eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox{
321eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE);
322eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
323eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox}
324b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
325eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcoxstatic struct nvme_iod *
326eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcoxnvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp)
327b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
328eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
329eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox				sizeof(__le64 *) * nvme_npages(nbytes) +
330eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox				sizeof(struct scatterlist) * nseg, gfp);
331eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox
332eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	if (iod) {
333eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		iod->offset = offsetof(struct nvme_iod, sg[nseg]);
334eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		iod->npages = -1;
335eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		iod->length = nbytes;
336eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	}
337eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox
338eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	return iod;
339b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
340b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
341eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcoxstatic void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
342b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
343eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	const int last_prp = PAGE_SIZE / 8 - 1;
344eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	int i;
345eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	__le64 **list = iod_list(iod);
346eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	dma_addr_t prp_dma = iod->first_dma;
347eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox
348eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	if (iod->npages == 0)
349eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
350eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	for (i = 0; i < iod->npages; i++) {
351eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		__le64 *prp_list = list[i];
352eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
353eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
354eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		prp_dma = next_prp_dma;
355eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	}
356eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	kfree(iod);
357b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
358b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
3595c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcoxstatic void requeue_bio(struct nvme_dev *dev, struct bio *bio)
3605c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox{
3615c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox	struct nvme_queue *nvmeq = get_nvmeq(dev);
3625c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox	if (bio_list_empty(&nvmeq->sq_cong))
3635c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox		add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
3645c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox	bio_list_add(&nvmeq->sq_cong, bio);
3655c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox	put_nvmeq(nvmeq);
3665c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox	wake_up_process(nvme_thread);
3675c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox}
3685c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox
3695c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcoxstatic void bio_completion(struct nvme_dev *dev, void *ctx,
370b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox						struct nvme_completion *cqe)
371b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
372eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	struct nvme_iod *iod = ctx;
373eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	struct bio *bio = iod->private;
374b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 status = le16_to_cpup(&cqe->status) >> 1;
375b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
376eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
377b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
378eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	nvme_free_iod(dev, iod);
37909a58f536436efed02ead722e835cb4ce7674afcMatthew Wilcox	if (status) {
3801ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox		bio_endio(bio, -EIO);
38109a58f536436efed02ead722e835cb4ce7674afcMatthew Wilcox	} else if (bio->bi_vcnt > bio->bi_idx) {
3825c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox		requeue_bio(dev, bio);
3831ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	} else {
3841ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox		bio_endio(bio, 0);
3851ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	}
386b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
387b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
388184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox/* length is in bytes.  gfp flags indicates whether we may sleep. */
389eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcoxstatic int nvme_setup_prps(struct nvme_dev *dev,
390eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox			struct nvme_common_command *cmd, struct nvme_iod *iod,
391eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox			int total_len, gfp_t gfp)
392ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox{
39399802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	struct dma_pool *pool;
394eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	int length = total_len;
395eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	struct scatterlist *sg = iod->sg;
396ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	int dma_len = sg_dma_len(sg);
397ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	u64 dma_addr = sg_dma_address(sg);
398ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	int offset = offset_in_page(dma_addr);
399e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	__le64 *prp_list;
400eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	__le64 **list = iod_list(iod);
401e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	dma_addr_t prp_dma;
402eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	int nprps, i;
403ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox
404ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmd->prp1 = cpu_to_le64(dma_addr);
405ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	length -= (PAGE_SIZE - offset);
406ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	if (length <= 0)
407eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		return total_len;
408ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox
409ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	dma_len -= (PAGE_SIZE - offset);
410ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	if (dma_len) {
411ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		dma_addr += (PAGE_SIZE - offset);
412ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	} else {
413ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		sg = sg_next(sg);
414ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		dma_addr = sg_dma_address(sg);
415ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		dma_len = sg_dma_len(sg);
416ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	}
417ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox
418ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	if (length <= PAGE_SIZE) {
419ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		cmd->prp2 = cpu_to_le64(dma_addr);
420eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		return total_len;
421e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	}
422e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews
423e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	nprps = DIV_ROUND_UP(length, PAGE_SIZE);
42499802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	if (nprps <= (256 / 8)) {
42599802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox		pool = dev->prp_small_pool;
426eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		iod->npages = 0;
42799802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	} else {
42899802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox		pool = dev->prp_page_pool;
429eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		iod->npages = 1;
43099802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	}
43199802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox
432b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox	prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
433b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox	if (!prp_list) {
434b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox		cmd->prp2 = cpu_to_le64(dma_addr);
435eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		iod->npages = -1;
436eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		return (total_len - length) + PAGE_SIZE;
437b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox	}
438eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	list[0] = prp_list;
439eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	iod->first_dma = prp_dma;
440e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	cmd->prp2 = cpu_to_le64(prp_dma);
441e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	i = 0;
442e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	for (;;) {
4437523d834dd1573610078eb1ac0933f6490232f90Matthew Wilcox		if (i == PAGE_SIZE / 8) {
444e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews			__le64 *old_prp_list = prp_list;
445b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox			prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
446eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox			if (!prp_list)
447eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox				return total_len - length;
448eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox			list[iod->npages++] = prp_list;
4497523d834dd1573610078eb1ac0933f6490232f90Matthew Wilcox			prp_list[0] = old_prp_list[i - 1];
4507523d834dd1573610078eb1ac0933f6490232f90Matthew Wilcox			old_prp_list[i - 1] = cpu_to_le64(prp_dma);
4517523d834dd1573610078eb1ac0933f6490232f90Matthew Wilcox			i = 1;
452e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		}
453e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		prp_list[i++] = cpu_to_le64(dma_addr);
454e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		dma_len -= PAGE_SIZE;
455e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		dma_addr += PAGE_SIZE;
456e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		length -= PAGE_SIZE;
457e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		if (length <= 0)
458e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews			break;
459e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		if (dma_len > 0)
460e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews			continue;
461e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		BUG_ON(dma_len < 0);
462e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		sg = sg_next(sg);
463e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		dma_addr = sg_dma_address(sg);
464e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		dma_len = sg_dma_len(sg);
465ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	}
466ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox
467eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	return total_len;
468ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox}
469ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox
4701ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox/* NVMe scatterlists require no holes in the virtual address */
4711ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2)	((vec2)->bv_offset || \
4721ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox			(((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
4731ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox
474eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcoxstatic int nvme_map_bio(struct device *dev, struct nvme_iod *iod,
475b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		struct bio *bio, enum dma_data_direction dma_dir, int psegs)
476b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
477768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox	struct bio_vec *bvec, *bvprv = NULL;
478768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox	struct scatterlist *sg = NULL;
4791ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	int i, old_idx, length = 0, nsegs = 0;
480b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
481eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	sg_init_table(iod->sg, psegs);
4821ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	old_idx = bio->bi_idx;
483b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	bio_for_each_segment(bvec, bio, i) {
484768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox		if (bvprv && BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) {
485768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox			sg->length += bvec->bv_len;
486768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox		} else {
4871ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox			if (bvprv && BIOVEC_NOT_VIRT_MERGEABLE(bvprv, bvec))
4881ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox				break;
489eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox			sg = sg ? sg + 1 : iod->sg;
490768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox			sg_set_page(sg, bvec->bv_page, bvec->bv_len,
491768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox							bvec->bv_offset);
492768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox			nsegs++;
493768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox		}
4941ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox		length += bvec->bv_len;
495768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox		bvprv = bvec;
496b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
4971ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	bio->bi_idx = i;
498eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	iod->nents = nsegs;
499768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox	sg_mark_end(sg);
500eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	if (dma_map_sg(dev, iod->sg, iod->nents, dma_dir) == 0) {
5011ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox		bio->bi_idx = old_idx;
5021ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox		return -ENOMEM;
5031ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	}
5041ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	return length;
505b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
506b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
50700df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcoxstatic int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
50800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox								int cmdid)
50900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox{
51000df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
51100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox
51200df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	memset(cmnd, 0, sizeof(*cmnd));
51300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	cmnd->common.opcode = nvme_cmd_flush;
51400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	cmnd->common.command_id = cmdid;
51500df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	cmnd->common.nsid = cpu_to_le32(ns->ns_id);
51600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox
51700df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	if (++nvmeq->sq_tail == nvmeq->q_depth)
51800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox		nvmeq->sq_tail = 0;
51900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	writel(nvmeq->sq_tail, nvmeq->q_db);
52000df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox
52100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	return 0;
52200df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox}
52300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox
52400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcoxstatic int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns)
52500df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox{
52600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH,
527ff976d724a74e4522e9ca2de1fb37ac4520f454fMatthew Wilcox					special_completion, NVME_IO_TIMEOUT);
52800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	if (unlikely(cmdid < 0))
52900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox		return cmdid;
53000df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox
53100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	return nvme_submit_flush(nvmeq, ns, cmdid);
53200df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox}
53300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox
534184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox/*
535184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox * Called with local interrupts disabled and the q_lock held.  May not sleep.
536184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox */
537b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
538b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox								struct bio *bio)
539b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
540ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	struct nvme_command *cmnd;
541eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	struct nvme_iod *iod;
542b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	enum dma_data_direction dma_dir;
5431ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	int cmdid, length, result = -ENOMEM;
544b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 control;
545b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 dsmgmt;
546b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int psegs = bio_phys_segments(ns->queue, bio);
547b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
54800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	if ((bio->bi_rw & REQ_FLUSH) && psegs) {
54900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox		result = nvme_submit_flush_data(nvmeq, ns);
55000df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox		if (result)
55100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox			return result;
55200df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	}
55300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox
554eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	iod = nvme_alloc_iod(psegs, bio->bi_size, GFP_ATOMIC);
555eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	if (!iod)
556eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox		goto nomem;
557eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	iod->private = bio;
558b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
559eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox	result = -EBUSY;
560ff976d724a74e4522e9ca2de1fb37ac4520f454fMatthew Wilcox	cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT);
561b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (unlikely(cmdid < 0))
562eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		goto free_iod;
563b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
56400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox	if ((bio->bi_rw & REQ_FLUSH) && !psegs)
56500df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox		return nvme_submit_flush(nvmeq, ns, cmdid);
56600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox
567b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	control = 0;
568b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (bio->bi_rw & REQ_FUA)
569b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		control |= NVME_RW_FUA;
570b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
571b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		control |= NVME_RW_LR;
572b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
573b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dsmgmt = 0;
574b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (bio->bi_rw & REQ_RAHEAD)
575b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
576b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
577ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
578b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
579b8deb62cf271fa9381edc8cf52bcae2f0225c55aMatthew Wilcox	memset(cmnd, 0, sizeof(*cmnd));
580b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (bio_data_dir(bio)) {
581ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		cmnd->rw.opcode = nvme_cmd_write;
582b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		dma_dir = DMA_TO_DEVICE;
583b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	} else {
584ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		cmnd->rw.opcode = nvme_cmd_read;
585b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		dma_dir = DMA_FROM_DEVICE;
586b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
587b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
588eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	result = nvme_map_bio(nvmeq->q_dmadev, iod, bio, dma_dir, psegs);
5891ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	if (result < 0)
590eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		goto free_iod;
5911ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	length = result;
592b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
593ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd->rw.command_id = cmdid;
594ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
595eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	length = nvme_setup_prps(nvmeq->dev, &cmnd->common, iod, length,
596eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox								GFP_ATOMIC);
597ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd->rw.slba = cpu_to_le64(bio->bi_sector >> (ns->lba_shift - 9));
5981ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox	cmnd->rw.length = cpu_to_le16((length >> ns->lba_shift) - 1);
599ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd->rw.control = cpu_to_le16(control);
600ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
601b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
602d8ee9d69f275769aaad40ef7c944565ff8d2d24fMatthew Wilcox	bio->bi_sector += length >> 9;
603d8ee9d69f275769aaad40ef7c944565ff8d2d24fMatthew Wilcox
604b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (++nvmeq->sq_tail == nvmeq->q_depth)
605b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		nvmeq->sq_tail = 0;
6067547881d0951384f9833ec3a80fac8f3f16f3b98Matthew Wilcox	writel(nvmeq->sq_tail, nvmeq->q_db);
607b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
6081974b1ae8852324a75fb8cfecbc7b758fd5a2c3cMatthew Wilcox	return 0;
6091974b1ae8852324a75fb8cfecbc7b758fd5a2c3cMatthew Wilcox
610eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox free_iod:
611eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	nvme_free_iod(nvmeq->dev, iod);
612eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox nomem:
613eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox	return result;
614b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
615b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
616b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/*
617b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * NB: return value of non-zero would mean that we were a stacking driver.
618b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * make_request must always succeed.
619b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
620b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_make_request(struct request_queue *q, struct bio *bio)
621b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
622b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_ns *ns = q->queuedata;
623040a93b52a9eee8177ebaf2ba0ee0f9f518d1bf8Matthew Wilcox	struct nvme_queue *nvmeq = get_nvmeq(ns->dev);
624eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox	int result = -EBUSY;
625eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox
626eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox	spin_lock_irq(&nvmeq->q_lock);
627eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox	if (bio_list_empty(&nvmeq->sq_cong))
628eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox		result = nvme_submit_bio_queue(nvmeq, ns, bio);
629eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox	if (unlikely(result)) {
630eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox		if (bio_list_empty(&nvmeq->sq_cong))
631eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox			add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
632b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		bio_list_add(&nvmeq->sq_cong, bio);
633b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
634eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox
635eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox	spin_unlock_irq(&nvmeq->q_lock);
636b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	put_nvmeq(nvmeq);
637b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
638b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
639b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
640b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
641b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic irqreturn_t nvme_process_cq(struct nvme_queue *nvmeq)
642b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
643821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox	u16 head, phase;
644b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
645b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	head = nvmeq->cq_head;
646821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox	phase = nvmeq->cq_phase;
647b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
648b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	for (;;) {
649c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox		void *ctx;
650c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox		nvme_completion_fn fn;
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
660c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox		ctx = free_cmdid(nvmeq, cqe.command_id, &fn);
6615c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox		fn(nvmeq->dev, ctx, &cqe);
662b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
663b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
664b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	/* If the controller ignores the cq head doorbell and continuously
665b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	 * writes to the queue, it is theoretically possible to wrap around
666b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	 * the queue twice and mistakenly return IRQ_NONE.  Linux only
667b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	 * requires that 0.1% of your interrupts are handled, so this isn't
668b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	 * a big problem.
669b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	 */
670821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox	if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
671b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return IRQ_NONE;
672b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
673f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox	writel(head, nvmeq->q_db + (1 << nvmeq->dev->db_stride));
674b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->cq_head = head;
675821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox	nvmeq->cq_phase = phase;
676b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
677b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return IRQ_HANDLED;
678b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
679b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
680b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic irqreturn_t nvme_irq(int irq, void *data)
681b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
68258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	irqreturn_t result;
68358ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	struct nvme_queue *nvmeq = data;
68458ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	spin_lock(&nvmeq->q_lock);
68558ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	result = nvme_process_cq(nvmeq);
68658ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	spin_unlock(&nvmeq->q_lock);
68758ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	return result;
68858ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox}
68958ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox
69058ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxstatic irqreturn_t nvme_irq_check(int irq, void *data)
69158ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox{
69258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	struct nvme_queue *nvmeq = data;
69358ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
69458ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
69558ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox		return IRQ_NONE;
69658ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	return IRQ_WAKE_THREAD;
69758ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox}
69858ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox
6993c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcoxstatic void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
7003c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox{
7013c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox	spin_lock_irq(&nvmeq->q_lock);
702c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	cancel_cmdid(nvmeq, cmdid, NULL);
7033c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox	spin_unlock_irq(&nvmeq->q_lock);
7043c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox}
7053c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox
706c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcoxstruct sync_cmd_info {
707c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	struct task_struct *task;
708c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	u32 result;
709c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	int status;
710c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox};
711c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox
7125c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcoxstatic void sync_completion(struct nvme_dev *dev, void *ctx,
713c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox						struct nvme_completion *cqe)
714c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox{
715c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	struct sync_cmd_info *cmdinfo = ctx;
716c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	cmdinfo->result = le32_to_cpup(&cqe->result);
717c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
718c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	wake_up_process(cmdinfo->task);
719c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox}
720c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox
721b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/*
722b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Returns 0 on success.  If the result is negative, it's a Linux error code;
723b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * if the result is positive, it's an NVM Express status code
724b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
7253c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcoxstatic int nvme_submit_sync_cmd(struct nvme_queue *nvmeq,
726e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox			struct nvme_command *cmd, u32 *result, unsigned timeout)
727b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
728b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int cmdid;
729b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct sync_cmd_info cmdinfo;
730b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
731b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cmdinfo.task = current;
732b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cmdinfo.status = -EINTR;
733b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
734c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox	cmdid = alloc_cmdid_killable(nvmeq, &cmdinfo, sync_completion,
735e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox								timeout);
736b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (cmdid < 0)
737b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return cmdid;
738b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cmd->common.command_id = cmdid;
739b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
7403c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox	set_current_state(TASK_KILLABLE);
7413c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox	nvme_submit_cmd(nvmeq, cmd);
742b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	schedule();
743b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
7443c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox	if (cmdinfo.status == -EINTR) {
7453c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox		nvme_abort_command(nvmeq, cmdid);
7463c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox		return -EINTR;
7473c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox	}
7483c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox
749b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (result)
750b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		*result = cmdinfo.result;
751b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
752b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return cmdinfo.status;
753b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
754b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
755b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
756b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox								u32 *result)
757b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
758e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT);
759b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
760b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
761b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
762b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
763b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int status;
764b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_command c;
765b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
766b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset(&c, 0, sizeof(c));
767b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.delete_queue.opcode = opcode;
768b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.delete_queue.qid = cpu_to_le16(id);
769b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
770b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	status = nvme_submit_admin_cmd(dev, &c, NULL);
771b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (status)
772b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -EIO;
773b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
774b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
775b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
776b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
777b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox						struct nvme_queue *nvmeq)
778b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
779b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int status;
780b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_command c;
781b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
782b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
783b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset(&c, 0, sizeof(c));
784b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_cq.opcode = nvme_admin_create_cq;
785b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
786b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_cq.cqid = cpu_to_le16(qid);
787b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
788b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_cq.cq_flags = cpu_to_le16(flags);
789b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
790b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
791b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	status = nvme_submit_admin_cmd(dev, &c, NULL);
792b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (status)
793b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -EIO;
794b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
795b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
796b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
797b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
798b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox						struct nvme_queue *nvmeq)
799b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
800b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int status;
801b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_command c;
802b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
803b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
804b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset(&c, 0, sizeof(c));
805b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_sq.opcode = nvme_admin_create_sq;
806b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
807b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_sq.sqid = cpu_to_le16(qid);
808b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
809b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_sq.sq_flags = cpu_to_le16(flags);
810b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_sq.cqid = cpu_to_le16(qid);
811b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
812b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	status = nvme_submit_admin_cmd(dev, &c, NULL);
813b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (status)
814b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -EIO;
815b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
816b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
817b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
818b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
819b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
820b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
821b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
822b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
823b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
824b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
825b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
826b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
827b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
828bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcoxstatic int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
829bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox							dma_addr_t dma_addr)
830bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox{
831bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	struct nvme_command c;
832bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox
833bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	memset(&c, 0, sizeof(c));
834bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	c.identify.opcode = nvme_admin_identify;
835bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	c.identify.nsid = cpu_to_le32(nsid);
836bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	c.identify.prp1 = cpu_to_le64(dma_addr);
837bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	c.identify.cns = cpu_to_le32(cns);
838bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox
839bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	return nvme_submit_admin_cmd(dev, &c, NULL);
840bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox}
841bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox
842bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcoxstatic int nvme_get_features(struct nvme_dev *dev, unsigned fid,
843bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox			unsigned dword11, dma_addr_t dma_addr, u32 *result)
844bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox{
845bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	struct nvme_command c;
846bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox
847bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	memset(&c, 0, sizeof(c));
848bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	c.features.opcode = nvme_admin_get_features;
849bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	c.features.prp1 = cpu_to_le64(dma_addr);
850bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	c.features.fid = cpu_to_le32(fid);
851bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	c.features.dword11 = cpu_to_le32(dword11);
852bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox
853bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	return nvme_submit_admin_cmd(dev, &c, result);
854bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox}
855bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox
856b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_free_queue(struct nvme_dev *dev, int qid)
857b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
858b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_queue *nvmeq = dev->queues[qid];
859aba2080f3f1639f9202f1a52993669844abcfb80Matthew Wilcox	int vector = dev->entry[nvmeq->cq_vector].vector;
860b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
861aba2080f3f1639f9202f1a52993669844abcfb80Matthew Wilcox	irq_set_affinity_hint(vector, NULL);
862aba2080f3f1639f9202f1a52993669844abcfb80Matthew Wilcox	free_irq(vector, nvmeq);
863b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
864b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	/* Don't tell the adapter to delete the admin queue */
865b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (qid) {
866b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		adapter_delete_sq(dev, qid);
867b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		adapter_delete_cq(dev, qid);
868b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
869b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
870b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
871b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox				(void *)nvmeq->cqes, nvmeq->cq_dma_addr);
872b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
873b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox					nvmeq->sq_cmds, nvmeq->sq_dma_addr);
874b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(nvmeq);
875b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
876b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
877b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
878b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox							int depth, int vector)
879b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
880b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct device *dmadev = &dev->pci_dev->dev;
881e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	unsigned extra = (depth / 8) + (depth * sizeof(struct nvme_cmd_info));
882b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
883b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!nvmeq)
884b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return NULL;
885b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
886b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
887b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox					&nvmeq->cq_dma_addr, GFP_KERNEL);
888b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!nvmeq->cqes)
889b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto free_nvmeq;
890b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
891b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
892b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
893b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox					&nvmeq->sq_dma_addr, GFP_KERNEL);
894b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!nvmeq->sq_cmds)
895b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto free_cqdma;
896b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
897b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->q_dmadev = dmadev;
898091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	nvmeq->dev = dev;
899b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	spin_lock_init(&nvmeq->q_lock);
900b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->cq_head = 0;
901821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox	nvmeq->cq_phase = 1;
902b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	init_waitqueue_head(&nvmeq->sq_full);
9031fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
904b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	bio_list_init(&nvmeq->sq_cong);
905f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox	nvmeq->q_db = &dev->dbs[qid << (dev->db_stride + 1)];
906b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->q_depth = depth;
907b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->cq_vector = vector;
908b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
909b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return nvmeq;
910b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
911b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_cqdma:
912b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(dmadev, CQ_SIZE(nvmeq->q_depth), (void *)nvmeq->cqes,
913b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox							nvmeq->cq_dma_addr);
914b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_nvmeq:
915b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(nvmeq);
916b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return NULL;
917b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
918b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
9193001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcoxstatic int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
9203001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox							const char *name)
9213001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox{
92258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	if (use_threaded_interrupts)
92358ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox		return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
924ec6ce618d65b5ce1bef83a5509255107a0feac44Matthew Wilcox					nvme_irq_check, nvme_irq,
92558ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox					IRQF_DISABLED | IRQF_SHARED,
92658ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox					name, nvmeq);
9273001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox	return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
9283001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox				IRQF_DISABLED | IRQF_SHARED, name, nvmeq);
9293001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox}
9303001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox
931b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic __devinit struct nvme_queue *nvme_create_queue(struct nvme_dev *dev,
932b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox					int qid, int cq_size, int vector)
933b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
934b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int result;
935b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_queue *nvmeq = nvme_alloc_queue(dev, qid, cq_size, vector);
936b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
9373f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox	if (!nvmeq)
9386f0f54499f2edf7e25410cdd99e6f030f3485fd1Matthew Wilcox		return ERR_PTR(-ENOMEM);
9393f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox
940b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	result = adapter_alloc_cq(dev, qid, nvmeq);
941b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (result < 0)
942b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto free_nvmeq;
943b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
944b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	result = adapter_alloc_sq(dev, qid, nvmeq);
945b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (result < 0)
946b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto release_cq;
947b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
9483001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox	result = queue_request_irq(dev, nvmeq, "nvme");
949b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (result < 0)
950b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto release_sq;
951b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
952b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return nvmeq;
953b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
954b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox release_sq:
955b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	adapter_delete_sq(dev, qid);
956b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox release_cq:
957b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	adapter_delete_cq(dev, qid);
958b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_nvmeq:
959b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
960b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox				(void *)nvmeq->cqes, nvmeq->cq_dma_addr);
961b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
962b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox					nvmeq->sq_cmds, nvmeq->sq_dma_addr);
963b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(nvmeq);
9646f0f54499f2edf7e25410cdd99e6f030f3485fd1Matthew Wilcox	return ERR_PTR(result);
965b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
966b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
967b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_configure_admin_queue(struct nvme_dev *dev)
968b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
969b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int result;
970b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 aqa;
97122605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox	u64 cap;
97222605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox	unsigned long timeout;
973b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_queue *nvmeq;
974b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
975b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->dbs = ((void __iomem *)dev->bar) + 4096;
976b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
977b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
9783f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox	if (!nvmeq)
9793f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox		return -ENOMEM;
980b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
981b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	aqa = nvmeq->q_depth - 1;
982b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	aqa |= aqa << 16;
983b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
984b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
985b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
986b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
9877f53f9d2424533256ae86f7df5661a17de743de8Matthew Wilcox	dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
988b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
9895911f20039ce59d7e7834f0c42151cf759b6f786Shane Michael Matthews	writel(0, &dev->bar->cc);
990b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	writel(aqa, &dev->bar->aqa);
991b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
992b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
993b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	writel(dev->ctrl_config, &dev->bar->cc);
994b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
99522605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox	cap = readq(&dev->bar->cap);
99622605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox	timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
997f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox	dev->db_stride = NVME_CAP_STRIDE(cap);
99822605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox
999b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	while (!(readl(&dev->bar->csts) & NVME_CSTS_RDY)) {
1000b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		msleep(100);
1001b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (fatal_signal_pending(current))
1002b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			return -EINTR;
100322605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox		if (time_after(jiffies, timeout)) {
100422605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox			dev_err(&dev->pci_dev->dev,
100522605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox				"Device not ready; aborting initialisation\n");
100622605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox			return -ENODEV;
100722605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox		}
1008b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1009b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
10103001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox	result = queue_request_irq(dev, nvmeq, "nvme admin");
1011b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->queues[0] = nvmeq;
1012b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return result;
1013b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1014b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1015eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcoxstatic struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
1016eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox				unsigned long addr, unsigned length)
1017b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
101836c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	int i, err, count, nents, offset;
10197fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	struct scatterlist *sg;
10207fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	struct page **pages;
1021eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	struct nvme_iod *iod;
102236c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox
102336c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	if (addr & 3)
1024eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		return ERR_PTR(-EINVAL);
10257fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	if (!length)
1026eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		return ERR_PTR(-EINVAL);
10277fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox
102836c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	offset = offset_in_page(addr);
10297fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
10307fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
103136c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox
103236c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	err = get_user_pages_fast(addr, count, 1, pages);
103336c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	if (err < count) {
103436c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox		count = err;
103536c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox		err = -EFAULT;
103636c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox		goto put_pages;
103736c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	}
10387fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox
1039eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	iod = nvme_alloc_iod(count, length, GFP_KERNEL);
1040eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	sg = iod->sg;
104136c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	sg_init_table(sg, count);
1042d0ba1e497bca83a3d353eb47c9658afc54d83228Matthew Wilcox	for (i = 0; i < count; i++) {
1043d0ba1e497bca83a3d353eb47c9658afc54d83228Matthew Wilcox		sg_set_page(&sg[i], pages[i],
1044d0ba1e497bca83a3d353eb47c9658afc54d83228Matthew Wilcox				min_t(int, length, PAGE_SIZE - offset), offset);
1045d0ba1e497bca83a3d353eb47c9658afc54d83228Matthew Wilcox		length -= (PAGE_SIZE - offset);
1046d0ba1e497bca83a3d353eb47c9658afc54d83228Matthew Wilcox		offset = 0;
10477fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	}
1048fe304c43c6d63e29ed4fc46a874d7a74313788c5Matthew Wilcox	sg_mark_end(&sg[i - 1]);
10491c2ad9faaf662b4a525348775deca3ac8e6c35a0Matthew Wilcox	iod->nents = count;
10507fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox
10517fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	err = -ENOMEM;
10527fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
10537fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox				write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
105436c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	if (!nents)
1055eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		goto free_iod;
1056b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
10577fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	kfree(pages);
1058eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	return iod;
1059b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1060eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox free_iod:
1061eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	kfree(iod);
10627fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox put_pages:
10637fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	for (i = 0; i < count; i++)
10647fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox		put_page(pages[i]);
10657fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	kfree(pages);
1066eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	return ERR_PTR(err);
10677fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox}
1068b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
10697fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcoxstatic void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
10701c2ad9faaf662b4a525348775deca3ac8e6c35a0Matthew Wilcox			struct nvme_iod *iod)
10717fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox{
10721c2ad9faaf662b4a525348775deca3ac8e6c35a0Matthew Wilcox	int i;
1073b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
10741c2ad9faaf662b4a525348775deca3ac8e6c35a0Matthew Wilcox	dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
10751c2ad9faaf662b4a525348775deca3ac8e6c35a0Matthew Wilcox				write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
10767fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox
10771c2ad9faaf662b4a525348775deca3ac8e6c35a0Matthew Wilcox	for (i = 0; i < iod->nents; i++)
10781c2ad9faaf662b4a525348775deca3ac8e6c35a0Matthew Wilcox		put_page(sg_page(&iod->sg[i]));
10797fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox}
1080b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1081a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcoxstatic int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1082a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox{
1083a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	struct nvme_dev *dev = ns->dev;
1084a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	struct nvme_queue *nvmeq;
1085a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	struct nvme_user_io io;
1086a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	struct nvme_command c;
1087a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	unsigned length;
1088eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	int status;
1089eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	struct nvme_iod *iod;
1090a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox
1091a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	if (copy_from_user(&io, uio, sizeof(io)))
1092a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox		return -EFAULT;
10936c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	length = (io.nblocks + 1) << ns->lba_shift;
10946c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox
10956c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	switch (io.opcode) {
10966c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	case nvme_cmd_write:
10976c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	case nvme_cmd_read:
10986bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	case nvme_cmd_compare:
1099eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
11006413214c5d424fd5aae6567848340f962ad2ce0fMatthew Wilcox		break;
11016c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	default:
11026bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox		return -EINVAL;
11036c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	}
11046c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox
1105eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	if (IS_ERR(iod))
1106eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		return PTR_ERR(iod);
1107a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox
1108a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	memset(&c, 0, sizeof(c));
1109a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.opcode = io.opcode;
1110a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.flags = io.flags;
11116c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	c.rw.nsid = cpu_to_le32(ns->ns_id);
1112a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.slba = cpu_to_le64(io.slba);
11136c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	c.rw.length = cpu_to_le16(io.nblocks);
1114a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.control = cpu_to_le16(io.control);
1115a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.dsmgmt = cpu_to_le16(io.dsmgmt);
11166c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	c.rw.reftag = io.reftag;
11176c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	c.rw.apptag = io.apptag;
11186c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox	c.rw.appmask = io.appmask;
1119a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	/* XXX: metadata */
1120eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	length = nvme_setup_prps(dev, &c.common, iod, length, GFP_KERNEL);
1121a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox
1122040a93b52a9eee8177ebaf2ba0ee0f9f518d1bf8Matthew Wilcox	nvmeq = get_nvmeq(dev);
1123fa92282149842645931580225647238428374758Matthew Wilcox	/*
1124fa92282149842645931580225647238428374758Matthew Wilcox	 * Since nvme_submit_sync_cmd sleeps, we can't keep preemption
1125b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox	 * disabled.  We may be preempted at any point, and be rescheduled
1126b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox	 * to a different CPU.  That will cause cacheline bouncing, but no
1127b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox	 * additional races since q_lock already protects against other CPUs.
1128b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox	 */
1129a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	put_nvmeq(nvmeq);
1130b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox	if (length != (io.nblocks + 1) << ns->lba_shift)
1131b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox		status = -ENOMEM;
1132b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox	else
1133ff976d724a74e4522e9ca2de1fb37ac4520f454fMatthew Wilcox		status = nvme_submit_sync_cmd(nvmeq, &c, NULL, NVME_IO_TIMEOUT);
1134a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox
11351c2ad9faaf662b4a525348775deca3ac8e6c35a0Matthew Wilcox	nvme_unmap_user_pages(dev, io.opcode & 1, iod);
1136eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	nvme_free_iod(dev, iod);
1137a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	return status;
1138a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox}
1139a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox
11406bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcoxstatic int nvme_user_admin_cmd(struct nvme_ns *ns,
11416bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox					struct nvme_admin_cmd __user *ucmd)
11426ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox{
11436ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	struct nvme_dev *dev = ns->dev;
11446bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	struct nvme_admin_cmd cmd;
11456ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	struct nvme_command c;
1146eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	int status, length;
1147eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox	struct nvme_iod *iod;
11486ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox
11496bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	if (!capable(CAP_SYS_ADMIN))
11506bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox		return -EACCES;
11516bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
11526ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox		return -EFAULT;
11536ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox
11546ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	memset(&c, 0, sizeof(c));
11556bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.opcode = cmd.opcode;
11566bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.flags = cmd.flags;
11576bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.nsid = cpu_to_le32(cmd.nsid);
11586bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
11596bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
11606bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
11616bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
11626bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
11636bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
11646bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
11656bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
11666bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox
11676bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	length = cmd.data_len;
11686bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	if (cmd.data_len) {
1169497421880acecd0281d3182d534f3d28c927caecMatthew Wilcox		iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1170497421880acecd0281d3182d534f3d28c927caecMatthew Wilcox								length);
1171eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		if (IS_ERR(iod))
1172eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox			return PTR_ERR(iod);
1173eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		length = nvme_setup_prps(dev, &c.common, iod, length,
1174eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox								GFP_KERNEL);
11756bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	}
11766bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox
11776bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	if (length != cmd.data_len)
1178b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox		status = -ENOMEM;
1179b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox	else
1180b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox		status = nvme_submit_admin_cmd(dev, &c, NULL);
1181eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox
11826bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	if (cmd.data_len) {
11831c2ad9faaf662b4a525348775deca3ac8e6c35a0Matthew Wilcox		nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
1184eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox		nvme_free_iod(dev, iod);
11856bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	}
11866ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	return status;
11876ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox}
11886ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox
1189b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1190b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox							unsigned long arg)
1191b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1192b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_ns *ns = bdev->bd_disk->private_data;
1193b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1194b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	switch (cmd) {
11956bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	case NVME_IOCTL_ID:
11966bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox		return ns->ns_id;
11976bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox	case NVME_IOCTL_ADMIN_CMD:
11986bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox		return nvme_user_admin_cmd(ns, (void __user *)arg);
1199a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	case NVME_IOCTL_SUBMIT_IO:
1200a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox		return nvme_submit_io(ns, (void __user *)arg);
1201b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	default:
1202b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -ENOTTY;
1203b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1204b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1205b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1206b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic const struct block_device_operations nvme_fops = {
1207b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.owner		= THIS_MODULE,
1208b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.ioctl		= nvme_ioctl,
12094948168280b269a514045766ddd872cfac5968e1Matthew Wilcox	.compat_ioctl	= nvme_ioctl,
1210b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
1211b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
12128de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcoxstatic void nvme_timeout_ios(struct nvme_queue *nvmeq)
12138de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox{
12148de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox	int depth = nvmeq->q_depth - 1;
12158de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox	struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
12168de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox	unsigned long now = jiffies;
12178de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox	int cmdid;
12188de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox
12198de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox	for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
1220c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox		void *ctx;
1221c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox		nvme_completion_fn fn;
12228de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox		static struct nvme_completion cqe = { .status = cpu_to_le16(NVME_SC_ABORT_REQ) << 1, };
12238de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox
12248de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox		if (!time_after(now, info[cmdid].timeout))
12258de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox			continue;
12268de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox		dev_warn(nvmeq->q_dmadev, "Timing out I/O %d\n", cmdid);
1227c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox		ctx = cancel_cmdid(nvmeq, cmdid, &fn);
12285c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox		fn(nvmeq->dev, ctx, &cqe);
12298de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox	}
12308de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox}
12318de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox
12321fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic void nvme_resubmit_bios(struct nvme_queue *nvmeq)
12331fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox{
12341fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	while (bio_list_peek(&nvmeq->sq_cong)) {
12351fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
12361fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
12371fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
12381fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox			bio_list_add_head(&nvmeq->sq_cong, bio);
12391fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox			break;
12401fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		}
12413cb967c03926edd2c414082f4cc0feb7b372edaeMatthew Wilcox		if (bio_list_empty(&nvmeq->sq_cong))
12423cb967c03926edd2c414082f4cc0feb7b372edaeMatthew Wilcox			remove_wait_queue(&nvmeq->sq_full,
12433cb967c03926edd2c414082f4cc0feb7b372edaeMatthew Wilcox							&nvmeq->sq_cong_wait);
12441fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	}
12451fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox}
12461fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox
12471fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic int nvme_kthread(void *data)
12481fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox{
12491fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	struct nvme_dev *dev;
12501fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox
12511fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	while (!kthread_should_stop()) {
12521fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		__set_current_state(TASK_RUNNING);
12531fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		spin_lock(&dev_list_lock);
12541fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		list_for_each_entry(dev, &dev_list, node) {
12551fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox			int i;
12561fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox			for (i = 0; i < dev->queue_count; i++) {
12571fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox				struct nvme_queue *nvmeq = dev->queues[i];
1258740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox				if (!nvmeq)
1259740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox					continue;
12601fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox				spin_lock_irq(&nvmeq->q_lock);
12611fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox				if (nvme_process_cq(nvmeq))
12621fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox					printk("process_cq did something\n");
12638de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox				nvme_timeout_ios(nvmeq);
12641fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox				nvme_resubmit_bios(nvmeq);
12651fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox				spin_unlock_irq(&nvmeq->q_lock);
12661fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox			}
12671fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		}
12681fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		spin_unlock(&dev_list_lock);
12691fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		set_current_state(TASK_INTERRUPTIBLE);
12701fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		schedule_timeout(HZ);
12711fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	}
12721fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	return 0;
12731fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox}
12741fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox
12755aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcoxstatic DEFINE_IDA(nvme_index_ida);
12765aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox
12775aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcoxstatic int nvme_get_ns_idx(void)
12785aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox{
12795aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	int index, error;
12805aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox
12815aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	do {
12825aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox		if (!ida_pre_get(&nvme_index_ida, GFP_KERNEL))
12835aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox			return -1;
12845aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox
12855aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox		spin_lock(&dev_list_lock);
12865aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox		error = ida_get_new(&nvme_index_ida, &index);
12875aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox		spin_unlock(&dev_list_lock);
12885aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	} while (error == -EAGAIN);
12895aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox
12905aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	if (error)
12915aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox		index = -1;
12925aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	return index;
12935aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox}
12945aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox
12955aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcoxstatic void nvme_put_ns_idx(int index)
12965aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox{
12975aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	spin_lock(&dev_list_lock);
12985aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	ida_remove(&nvme_index_ida, index);
12995aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	spin_unlock(&dev_list_lock);
13005aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox}
13015aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox
13025aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcoxstatic struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, int nsid,
1303b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1304b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1305b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_ns *ns;
1306b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct gendisk *disk;
1307b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int lbaf;
1308b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1309b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1310b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return NULL;
1311b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1312b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1313b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!ns)
1314b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return NULL;
1315b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns->queue = blk_alloc_queue(GFP_KERNEL);
1316b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!ns->queue)
1317b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto out_free_ns;
13184eeb9215a0d5c9494ca8b20158cc8ee82618840cMatthew Wilcox	ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
13194eeb9215a0d5c9494ca8b20158cc8ee82618840cMatthew Wilcox	queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
13204eeb9215a0d5c9494ca8b20158cc8ee82618840cMatthew Wilcox	queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
13214eeb9215a0d5c9494ca8b20158cc8ee82618840cMatthew Wilcox/*	queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue); */
1322b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	blk_queue_make_request(ns->queue, nvme_make_request);
1323b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns->dev = dev;
1324b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns->queue->queuedata = ns;
1325b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1326b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk = alloc_disk(NVME_MINORS);
1327b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!disk)
1328b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto out_free_queue;
13295aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	ns->ns_id = nsid;
1330b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns->disk = disk;
1331b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	lbaf = id->flbas & 0xf;
1332b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns->lba_shift = id->lbaf[lbaf].ds;
1333b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1334b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk->major = nvme_major;
1335b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk->minors = NVME_MINORS;
13365aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	disk->first_minor = NVME_MINORS * nvme_get_ns_idx();
1337b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk->fops = &nvme_fops;
1338b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk->private_data = ns;
1339b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk->queue = ns->queue;
1340388f037f4e7f0a24bac6b1a24f144f5d939f58cfMatthew Wilcox	disk->driverfs_dev = &dev->pci_dev->dev;
13415aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
1342b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1343b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1344b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return ns;
1345b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1346b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free_queue:
1347b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	blk_cleanup_queue(ns->queue);
1348b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free_ns:
1349b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(ns);
1350b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return NULL;
1351b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1352b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1353b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_ns_free(struct nvme_ns *ns)
1354b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
13555aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	int index = ns->disk->first_minor / NVME_MINORS;
1356b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	put_disk(ns->disk);
13575aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox	nvme_put_ns_idx(index);
1358b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	blk_cleanup_queue(ns->queue);
1359b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(ns);
1360b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1361b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1362b3b06812e199f248561ce7824a4a8a9cd573c05aMatthew Wilcoxstatic int set_queue_count(struct nvme_dev *dev, int count)
1363b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1364b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int status;
1365b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 result;
1366b3b06812e199f248561ce7824a4a8a9cd573c05aMatthew Wilcox	u32 q_count = (count - 1) | ((count - 1) << 16);
1367b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1368bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	status = nvme_get_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
1369bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox								&result);
1370b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (status)
1371b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -EIO;
1372b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return min(result & 0xffff, result >> 16) + 1;
1373b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1374b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1375b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_setup_io_queues(struct nvme_dev *dev)
1376b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1377f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox	int result, cpu, i, nr_io_queues, db_bar_size;
1378b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1379b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox	nr_io_queues = num_online_cpus();
1380b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox	result = set_queue_count(dev, nr_io_queues);
13811b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	if (result < 0)
13821b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		return result;
1383b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox	if (result < nr_io_queues)
1384b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox		nr_io_queues = result;
1385b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
13861b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	/* Deregister the admin queue's interrupt */
13871b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	free_irq(dev->entry[0].vector, dev->queues[0]);
13881b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox
1389f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox	db_bar_size = 4096 + ((nr_io_queues + 1) << (dev->db_stride + 3));
1390f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox	if (db_bar_size > 8192) {
1391f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox		iounmap(dev->bar);
1392f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox		dev->bar = ioremap(pci_resource_start(dev->pci_dev, 0),
1393f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox								db_bar_size);
1394f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox		dev->dbs = ((void __iomem *)dev->bar) + 4096;
1395f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox		dev->queues[0]->q_db = dev->dbs;
1396f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox	}
1397f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox
1398b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox	for (i = 0; i < nr_io_queues; i++)
13991b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		dev->entry[i].entry = i;
14001b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	for (;;) {
1401b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox		result = pci_enable_msix(dev->pci_dev, dev->entry,
1402b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox								nr_io_queues);
14031b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		if (result == 0) {
14041b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox			break;
14051b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		} else if (result > 0) {
1406b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox			nr_io_queues = result;
14071b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox			continue;
14081b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		} else {
1409b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox			nr_io_queues = 1;
14101b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox			break;
14111b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		}
14121b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	}
14131b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox
14141b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	result = queue_request_irq(dev, dev->queues[0], "nvme admin");
14151b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	/* XXX: handle failure here */
14161b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox
14171b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	cpu = cpumask_first(cpu_online_mask);
1418b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox	for (i = 0; i < nr_io_queues; i++) {
14191b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu));
14201b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		cpu = cpumask_next(cpu, cpu_online_mask);
14211b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	}
14221b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox
1423b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox	for (i = 0; i < nr_io_queues; i++) {
14241b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		dev->queues[i + 1] = nvme_create_queue(dev, i + 1,
14251b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox							NVME_Q_DEPTH, i);
14266f0f54499f2edf7e25410cdd99e6f030f3485fd1Matthew Wilcox		if (IS_ERR(dev->queues[i + 1]))
14276f0f54499f2edf7e25410cdd99e6f030f3485fd1Matthew Wilcox			return PTR_ERR(dev->queues[i + 1]);
14281b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		dev->queue_count++;
14291b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	}
1430b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
14319ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox	for (; i < num_possible_cpus(); i++) {
14329ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox		int target = i % rounddown_pow_of_two(dev->queue_count - 1);
14339ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox		dev->queues[i + 1] = dev->queues[target + 1];
14349ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox	}
14359ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox
1436b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
1437b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1438b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1439b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_free_queues(struct nvme_dev *dev)
1440b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1441b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int i;
1442b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1443b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	for (i = dev->queue_count - 1; i >= 0; i--)
1444b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		nvme_free_queue(dev, i);
1445b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1446b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1447b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_dev_add(struct nvme_dev *dev)
1448b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1449b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int res, nn, i;
1450b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_ns *ns, *next;
145151814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	struct nvme_id_ctrl *ctrl;
1452bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	struct nvme_id_ns *id_ns;
1453bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	void *mem;
1454b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_addr_t dma_addr;
1455b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1456b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	res = nvme_setup_io_queues(dev);
1457b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (res)
1458b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return res;
1459b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1460bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	mem = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr,
1461b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox								GFP_KERNEL);
1462b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1463bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	res = nvme_identify(dev, 0, 1, dma_addr);
1464b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (res) {
1465b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		res = -EIO;
1466b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto out_free;
1467b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1468b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1469bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	ctrl = mem;
147051814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	nn = le32_to_cpup(&ctrl->nn);
147151814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
147251814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
147351814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
1474b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1475bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	id_ns = mem;
14762b2c1896871838cdf549442e8ad0264be5fa74e3Matthew Wilcox	for (i = 1; i <= nn; i++) {
1477bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox		res = nvme_identify(dev, i, 0, dma_addr);
1478b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (res)
1479b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			continue;
1480b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1481bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox		if (id_ns->ncap == 0)
1482b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			continue;
1483b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1484bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox		res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
1485bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox							dma_addr + 4096, NULL);
1486b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (res)
1487b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			continue;
1488b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1489bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox		ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
1490b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (ns)
1491b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			list_add_tail(&ns->list, &dev->namespaces);
1492b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1493b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	list_for_each_entry(ns, &dev->namespaces, list)
1494b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		add_disk(ns->disk);
1495b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1496bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox	goto out;
1497b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1498b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free:
1499b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
1500b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		list_del(&ns->list);
1501b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		nvme_ns_free(ns);
1502b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1503b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1504bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox out:
1505684f5c2025b067a23722e620d0b3b858d8dc5d01Matthew Wilcox	dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
1506b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return res;
1507b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1508b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1509b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_dev_remove(struct nvme_dev *dev)
1510b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1511b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_ns *ns, *next;
1512b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
15131fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	spin_lock(&dev_list_lock);
15141fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	list_del(&dev->node);
15151fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	spin_unlock(&dev_list_lock);
15161fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox
1517b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	/* TODO: wait all I/O finished or cancel them */
1518b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1519b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
1520b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		list_del(&ns->list);
1521b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		del_gendisk(ns->disk);
1522b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		nvme_ns_free(ns);
1523b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1524b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1525b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_free_queues(dev);
1526b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1527b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
1528b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1529b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1530091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcoxstatic int nvme_setup_prp_pools(struct nvme_dev *dev)
1531091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox{
1532091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	struct device *dmadev = &dev->pci_dev->dev;
1533091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
1534091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox						PAGE_SIZE, PAGE_SIZE, 0);
1535091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	if (!dev->prp_page_pool)
1536091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox		return -ENOMEM;
1537091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox
153899802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	/* Optimisation for I/Os between 4k and 128k */
153999802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
154099802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox						256, 256, 0);
154199802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	if (!dev->prp_small_pool) {
154299802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox		dma_pool_destroy(dev->prp_page_pool);
154399802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox		return -ENOMEM;
154499802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	}
1545091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	return 0;
1546091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox}
1547091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox
1548091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcoxstatic void nvme_release_prp_pools(struct nvme_dev *dev)
1549091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox{
1550091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	dma_pool_destroy(dev->prp_page_pool);
155199802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox	dma_pool_destroy(dev->prp_small_pool);
1552091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox}
1553091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox
1554b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* XXX: Use an ida or something to let remove / add work correctly */
1555b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_set_instance(struct nvme_dev *dev)
1556b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1557b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	static int instance;
1558b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->instance = instance++;
1559b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1560b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1561b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_release_instance(struct nvme_dev *dev)
1562b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1563b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1564b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1565b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_probe(struct pci_dev *pdev,
1566b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox						const struct pci_device_id *id)
1567b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1568574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox	int bars, result = -ENOMEM;
1569b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_dev *dev;
1570b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1571b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1572b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!dev)
1573b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -ENOMEM;
1574b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
1575b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox								GFP_KERNEL);
1576b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!dev->entry)
1577b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto free;
15781b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
15791b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox								GFP_KERNEL);
1580b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!dev->queues)
1581b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto free;
1582b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
15830ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews	if (pci_enable_device_mem(pdev))
15840ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews		goto free;
1585f64d3365a3e5cb46e69db7e2c82a7cb9a5bed1b8Matthew Wilcox	pci_set_master(pdev);
1586574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox	bars = pci_select_bars(pdev, IORESOURCE_MEM);
1587574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox	if (pci_request_selected_regions(pdev, bars, "nvme"))
1588574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox		goto disable;
15890ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews
1590b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	INIT_LIST_HEAD(&dev->namespaces);
1591b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->pci_dev = pdev;
1592b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	pci_set_drvdata(pdev, dev);
15932930353f9f2b9e4629e935acd970cb73c1171229Matthew Wilcox	dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
15942930353f9f2b9e4629e935acd970cb73c1171229Matthew Wilcox	dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
1595b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_set_instance(dev);
159653c9577e9ca68a633c6e9df2b54eaecacfa77f62Matthew Wilcox	dev->entry[0].vector = pdev->irq;
1597b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1598091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	result = nvme_setup_prp_pools(dev);
1599091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	if (result)
1600091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox		goto disable_msix;
1601091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox
1602b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1603b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!dev->bar) {
1604b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		result = -ENOMEM;
1605574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox		goto disable_msix;
1606b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1607b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1608b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	result = nvme_configure_admin_queue(dev);
1609b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (result)
1610b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto unmap;
1611b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->queue_count++;
1612b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
16131fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	spin_lock(&dev_list_lock);
16141fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	list_add(&dev->node, &dev_list);
16151fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	spin_unlock(&dev_list_lock);
16161fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox
1617740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox	result = nvme_dev_add(dev);
1618740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox	if (result)
1619740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox		goto delete;
1620740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox
1621b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
1622b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1623b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox delete:
1624740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox	spin_lock(&dev_list_lock);
1625740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox	list_del(&dev->node);
1626740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox	spin_unlock(&dev_list_lock);
1627740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox
1628b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_free_queues(dev);
1629b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unmap:
1630b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	iounmap(dev->bar);
1631574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox disable_msix:
1632b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	pci_disable_msix(pdev);
1633b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_release_instance(dev);
1634091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	nvme_release_prp_pools(dev);
1635574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox disable:
16360ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews	pci_disable_device(pdev);
1637574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox	pci_release_regions(pdev);
1638b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free:
1639b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(dev->queues);
1640b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(dev->entry);
1641b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(dev);
1642b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return result;
1643b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1644b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1645b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void __devexit nvme_remove(struct pci_dev *pdev)
1646b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1647b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_dev *dev = pci_get_drvdata(pdev);
1648b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_dev_remove(dev);
1649b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	pci_disable_msix(pdev);
1650b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	iounmap(dev->bar);
1651b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_release_instance(dev);
1652091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	nvme_release_prp_pools(dev);
16530ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews	pci_disable_device(pdev);
1654574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox	pci_release_regions(pdev);
1655b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(dev->queues);
1656b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(dev->entry);
1657b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(dev);
1658b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1659b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1660b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* These functions are yet to be implemented */
1661b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_error_detected NULL
1662b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_dump_registers NULL
1663b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_link_reset NULL
1664b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_slot_reset NULL
1665b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_error_resume NULL
1666b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_suspend NULL
1667b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_resume NULL
1668b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1669b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct pci_error_handlers nvme_err_handler = {
1670b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.error_detected	= nvme_error_detected,
1671b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.mmio_enabled	= nvme_dump_registers,
1672b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.link_reset	= nvme_link_reset,
1673b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.slot_reset	= nvme_slot_reset,
1674b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.resume		= nvme_error_resume,
1675b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
1676b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1677b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* Move to pci_ids.h later */
1678b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define PCI_CLASS_STORAGE_EXPRESS	0x010802
1679b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1680b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
1681b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	{ PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
1682b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	{ 0, }
1683b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
1684b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_DEVICE_TABLE(pci, nvme_id_table);
1685b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1686b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct pci_driver nvme_driver = {
1687b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.name		= "nvme",
1688b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.id_table	= nvme_id_table,
1689b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.probe		= nvme_probe,
1690b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.remove		= __devexit_p(nvme_remove),
1691b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.suspend	= nvme_suspend,
1692b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.resume		= nvme_resume,
1693b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.err_handler	= &nvme_err_handler,
1694b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
1695b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1696b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __init nvme_init(void)
1697b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
16981fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	int result = -EBUSY;
16991fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox
17001fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
17011fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	if (IS_ERR(nvme_thread))
17021fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		return PTR_ERR(nvme_thread);
1703b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1704b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_major = register_blkdev(nvme_major, "nvme");
1705b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (nvme_major <= 0)
17061fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		goto kill_kthread;
1707b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1708b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	result = pci_register_driver(&nvme_driver);
17091fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	if (result)
17101fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox		goto unregister_blkdev;
17111fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	return 0;
1712b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
17131fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox unregister_blkdev:
1714b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	unregister_blkdev(nvme_major, "nvme");
17151fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox kill_kthread:
17161fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	kthread_stop(nvme_thread);
1717b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return result;
1718b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1719b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1720b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void __exit nvme_exit(void)
1721b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1722b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	pci_unregister_driver(&nvme_driver);
1723b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	unregister_blkdev(nvme_major, "nvme");
17241fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox	kthread_stop(nvme_thread);
1725b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1726b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1727b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
1728b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_LICENSE("GPL");
1729ce38c149576fd0a3360fec3bef4012212d42e736Matthew WilcoxMODULE_VERSION("0.7");
1730b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_init(nvme_init);
1731b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_exit(nvme_exit);
1732