nvme-core.c revision 091b609258b8e01cc45b01a41ca5e496f674d989
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>
21b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/blkdev.h>
22b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/errno.h>
23b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/fs.h>
24b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/genhd.h>
25b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/init.h>
26b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/interrupt.h>
27b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/io.h>
28b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/kdev_t.h>
29b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/kernel.h>
30b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/mm.h>
31b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/module.h>
32b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/moduleparam.h>
33b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/pci.h>
34be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox#include <linux/poison.h>
35b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/sched.h>
36b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/slab.h>
37b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/types.h>
38b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/version.h>
39b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
40b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define NVME_Q_DEPTH 1024
41b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define SQ_SIZE(depth)		(depth * sizeof(struct nvme_command))
42b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define CQ_SIZE(depth)		(depth * sizeof(struct nvme_completion))
43b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define NVME_MINORS 64
44e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox#define IO_TIMEOUT	(5 * HZ)
45e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox#define ADMIN_TIMEOUT	(60 * HZ)
46b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
47b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_major;
48b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_param(nvme_major, int, 0);
49b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
5058ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxstatic int use_threaded_interrupts;
5158ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxmodule_param(use_threaded_interrupts, int, 0);
5258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox
53b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/*
54b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Represents an NVM Express device.  Each nvme_dev is a PCI function.
55b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
56b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct nvme_dev {
57b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_queue **queues;
58b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 __iomem *dbs;
59b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct pci_dev *pci_dev;
60091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	struct dma_pool *prp_page_pool;
61b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int instance;
62b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int queue_count;
63b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 ctrl_config;
64b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct msix_entry *entry;
65b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_bar __iomem *bar;
66b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct list_head namespaces;
6751814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	char serial[20];
6851814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	char model[40];
6951814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	char firmware_rev[8];
70b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
71b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
72b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/*
73b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * An NVM Express namespace is equivalent to a SCSI LUN
74b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
75b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct nvme_ns {
76b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct list_head list;
77b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
78b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_dev *dev;
79b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct request_queue *queue;
80b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct gendisk *disk;
81b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
82b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int ns_id;
83b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int lba_shift;
84b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
85b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
86b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/*
87b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * An NVM Express queue.  Each device has at least two (one for admin
88b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * commands and one for I/O commands).
89b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
90b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct nvme_queue {
91b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct device *q_dmadev;
92091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	struct nvme_dev *dev;
93b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	spinlock_t q_lock;
94b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_command *sq_cmds;
95b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	volatile struct nvme_completion *cqes;
96b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_addr_t sq_dma_addr;
97b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_addr_t cq_dma_addr;
98b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	wait_queue_head_t sq_full;
99b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct bio_list sq_cong;
100b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 __iomem *q_db;
101b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 q_depth;
102b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 cq_vector;
103b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 sq_head;
104b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 sq_tail;
105b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 cq_head;
106821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox	u16 cq_phase;
107b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	unsigned long cmdid_data[];
108b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
109b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1109294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcoxstatic void nvme_resubmit_bio(struct nvme_queue *nvmeq, struct bio *bio);
1119294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox
112b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/*
113b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Check we didin't inadvertently grow the command struct
114b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
115b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic inline void _nvme_check_size(void)
116b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
117b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
118b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
119b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
120b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
121b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
122b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
123b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
124b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
125b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
126b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
127b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
128e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstruct nvme_cmd_info {
129e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	unsigned long ctx;
130e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	unsigned long timeout;
131e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox};
132e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox
133e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstatic struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
134e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox{
135e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
136e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox}
137e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox
138b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/**
139b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * alloc_cmdid - Allocate a Command ID
140b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @param nvmeq The queue that will be used for this command
141b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @param ctx A pointer that will be passed to the handler
142b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @param handler The ID of the handler to call
143b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox *
144b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Allocate a Command ID for a queue.  The data passed in will
145b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * be passed to the completion handler.  This is implemented by using
146b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * the bottom two bits of the ctx pointer to store the handler ID.
147b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Passing in a pointer that's not 4-byte aligned will cause a BUG.
148b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * We can change this if it becomes a problem.
149b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
150e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstatic int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx, int handler,
151e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox							unsigned timeout)
152b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
153b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int depth = nvmeq->q_depth;
154e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
155b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int cmdid;
156b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
157b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	BUG_ON((unsigned long)ctx & 3);
158b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
159b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	do {
160b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
161b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (cmdid >= depth)
162b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			return -EBUSY;
163b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	} while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
164b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
165e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	info[cmdid].ctx = (unsigned long)ctx | handler;
166e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	info[cmdid].timeout = jiffies + timeout;
167b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return cmdid;
168b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
169b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
170b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
171e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox						int handler, unsigned timeout)
172b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
173b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int cmdid;
174b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	wait_event_killable(nvmeq->sq_full,
175e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox		(cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
176b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return (cmdid < 0) ? -EINTR : cmdid;
177b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
178b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
179b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* If you need more than four handlers, you'll need to change how
180be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox * alloc_cmdid and nvme_process_cq work.  Consider using a special
181be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox * CMD_CTX value instead, if that works for your situation.
182b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
183b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxenum {
184b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	sync_completion_id = 0,
185b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	bio_completion_id,
186b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
187b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
188be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox#define CMD_CTX_BASE		(POISON_POINTER_DELTA + sync_completion_id)
189d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_CANCELLED	(0x30C + CMD_CTX_BASE)
190d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_COMPLETED	(0x310 + CMD_CTX_BASE)
191d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_INVALID		(0x314 + CMD_CTX_BASE)
192be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox
193b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic unsigned long free_cmdid(struct nvme_queue *nvmeq, int cmdid)
194b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
195b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	unsigned long data;
196e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
197b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
198e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	if (cmdid >= nvmeq->q_depth)
19948e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox		return CMD_CTX_INVALID;
200e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	data = info[cmdid].ctx;
201e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	info[cmdid].ctx = CMD_CTX_COMPLETED;
202b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	clear_bit(cmdid, nvmeq->cmdid_data);
203b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	wake_up(&nvmeq->sq_full);
204b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return data;
205b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
206b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
207be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcoxstatic void cancel_cmdid_data(struct nvme_queue *nvmeq, int cmdid)
2083c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox{
209e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
210e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	info[cmdid].ctx = CMD_CTX_CANCELLED;
2113c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox}
2123c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox
213b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct nvme_queue *get_nvmeq(struct nvme_ns *ns)
214b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
2151b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	int qid, cpu = get_cpu();
2161b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	if (cpu < ns->dev->queue_count)
2171b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		qid = cpu + 1;
2181b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	else
2191b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		qid = (cpu % rounddown_pow_of_two(ns->dev->queue_count)) + 1;
2201b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	return ns->dev->queues[qid];
221b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
222b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
223b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void put_nvmeq(struct nvme_queue *nvmeq)
224b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
2251b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	put_cpu();
226b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
227b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
228b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/**
229b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * nvme_submit_cmd: Copy a command into a queue and ring the doorbell
230b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @nvmeq: The queue to use
231b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @cmd: The command to send
232b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox *
233b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Safe to use from interrupt context
234b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
235b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
236b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
237b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	unsigned long flags;
238b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 tail;
239b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	/* XXX: Need to check tail isn't going to overrun head */
240b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	spin_lock_irqsave(&nvmeq->q_lock, flags);
241b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	tail = nvmeq->sq_tail;
242b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
243b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	writel(tail, nvmeq->q_db);
244b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (++tail == nvmeq->q_depth)
245b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		tail = 0;
246b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->sq_tail = tail;
247b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	spin_unlock_irqrestore(&nvmeq->q_lock, flags);
248b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
249b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
250b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
251b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
252091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcoxstatic __le64 *alloc_prp_list(struct nvme_dev *dev, dma_addr_t *addr)
253e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews{
254091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	return dma_pool_alloc(dev->prp_page_pool, GFP_ATOMIC, addr);
255e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews}
256e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews
257e025344c56e08b155f43ea09647969286c78377cShane Michael Matthewsstruct nvme_prps {
258e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	int npages;
259e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	dma_addr_t first_dma;
260e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	__le64 *list[0];
261e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews};
262e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews
263e025344c56e08b155f43ea09647969286c78377cShane Michael Matthewsstatic void nvme_free_prps(struct nvme_queue *nvmeq, struct nvme_prps *prps)
264e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews{
265e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	const int last_prp = PAGE_SIZE / 8 - 1;
266091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	struct nvme_dev *dev = nvmeq->dev;
267e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	int i;
268e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	dma_addr_t prp_dma;
269e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews
270e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	if (!prps)
271e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		return;
272e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews
273e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	prp_dma = prps->first_dma;
274e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	for (i = 0; i < prps->npages; i++) {
275e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		__le64 *prp_list = prps->list[i];
276e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
277091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox		dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
278e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		prp_dma = next_prp_dma;
279e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	}
280e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	kfree(prps);
281e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews}
282e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews
283d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstruct nvme_bio {
284b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct bio *bio;
285b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int nents;
286e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	struct nvme_prps *prps;
287b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct scatterlist sg[0];
288b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
289b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
290b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* XXX: use a mempool */
291d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstatic struct nvme_bio *alloc_nbio(unsigned nseg, gfp_t gfp)
292b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
293d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	return kzalloc(sizeof(struct nvme_bio) +
294b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			sizeof(struct scatterlist) * nseg, gfp);
295b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
296b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
297d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstatic void free_nbio(struct nvme_queue *nvmeq, struct nvme_bio *nbio)
298b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
299d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	nvme_free_prps(nvmeq, nbio->prps);
300d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	kfree(nbio);
301b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
302b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
303b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void bio_completion(struct nvme_queue *nvmeq, void *ctx,
304b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox						struct nvme_completion *cqe)
305b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
306d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	struct nvme_bio *nbio = ctx;
307d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	struct bio *bio = nbio->bio;
308b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 status = le16_to_cpup(&cqe->status) >> 1;
309b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
310d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	dma_unmap_sg(nvmeq->q_dmadev, nbio->sg, nbio->nents,
311b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
312d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	free_nbio(nvmeq, nbio);
313b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	bio_endio(bio, status ? -EIO : 0);
3149294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox	bio = bio_list_pop(&nvmeq->sq_cong);
3159294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox	if (bio)
3169294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox		nvme_resubmit_bio(nvmeq, bio);
317b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
318b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
319ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox/* length is in bytes */
320e025344c56e08b155f43ea09647969286c78377cShane Michael Matthewsstatic struct nvme_prps *nvme_setup_prps(struct nvme_queue *nvmeq,
321e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews					struct nvme_common_command *cmd,
322ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox					struct scatterlist *sg, int length)
323ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox{
324091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	struct nvme_dev *dev = nvmeq->dev;
325ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	int dma_len = sg_dma_len(sg);
326ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	u64 dma_addr = sg_dma_address(sg);
327ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	int offset = offset_in_page(dma_addr);
328e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	__le64 *prp_list;
329e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	dma_addr_t prp_dma;
330e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	int nprps, npages, i, prp_page;
331e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	struct nvme_prps *prps = NULL;
332ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox
333ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmd->prp1 = cpu_to_le64(dma_addr);
334ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	length -= (PAGE_SIZE - offset);
335ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	if (length <= 0)
336e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		return prps;
337ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox
338ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	dma_len -= (PAGE_SIZE - offset);
339ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	if (dma_len) {
340ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		dma_addr += (PAGE_SIZE - offset);
341ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	} else {
342ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		sg = sg_next(sg);
343ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		dma_addr = sg_dma_address(sg);
344ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		dma_len = sg_dma_len(sg);
345ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	}
346ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox
347ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	if (length <= PAGE_SIZE) {
348ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		cmd->prp2 = cpu_to_le64(dma_addr);
349e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		return prps;
350e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	}
351e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews
352e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	nprps = DIV_ROUND_UP(length, PAGE_SIZE);
353e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	npages = DIV_ROUND_UP(8 * nprps, PAGE_SIZE);
354e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	prps = kmalloc(sizeof(*prps) + sizeof(__le64 *) * npages, GFP_ATOMIC);
355e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	prps->npages = npages;
356e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	prp_page = 0;
357091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	prp_list = alloc_prp_list(dev, &prp_dma);
358e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	prps->list[prp_page++] = prp_list;
359e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	prps->first_dma = prp_dma;
360e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	cmd->prp2 = cpu_to_le64(prp_dma);
361e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	i = 0;
362e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	for (;;) {
363e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		if (i == PAGE_SIZE / 8 - 1) {
364e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews			__le64 *old_prp_list = prp_list;
365091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox			prp_list = alloc_prp_list(dev, &prp_dma);
366e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews			prps->list[prp_page++] = prp_list;
367e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews			old_prp_list[i] = cpu_to_le64(prp_dma);
368e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews			i = 0;
369e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		}
370e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		prp_list[i++] = cpu_to_le64(dma_addr);
371e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		dma_len -= PAGE_SIZE;
372e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		dma_addr += PAGE_SIZE;
373e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		length -= PAGE_SIZE;
374e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		if (length <= 0)
375e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews			break;
376e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		if (dma_len > 0)
377e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews			continue;
378e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		BUG_ON(dma_len < 0);
379e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		sg = sg_next(sg);
380e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		dma_addr = sg_dma_address(sg);
381e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews		dma_len = sg_dma_len(sg);
382ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	}
383ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox
384e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	return prps;
385ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox}
386ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox
387d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstatic int nvme_map_bio(struct device *dev, struct nvme_bio *nbio,
388b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		struct bio *bio, enum dma_data_direction dma_dir, int psegs)
389b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
390b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct bio_vec *bvec;
391d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	struct scatterlist *sg = nbio->sg;
392b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int i, nsegs;
393b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
394b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	sg_init_table(sg, psegs);
395b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	bio_for_each_segment(bvec, bio, i) {
396b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		sg_set_page(sg, bvec->bv_page, bvec->bv_len, bvec->bv_offset);
39751882d00f07da9601cc962a3596e48aafb4f4163Matthew Wilcox		sg++;
398b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		/* XXX: handle non-mergable here */
399b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		nsegs++;
400b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
401d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	nbio->nents = nsegs;
402b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
403d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	return dma_map_sg(dev, nbio->sg, nbio->nents, dma_dir);
404b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
405b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
406b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
407b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox								struct bio *bio)
408b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
409ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	struct nvme_command *cmnd;
410d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	struct nvme_bio *nbio;
411b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	enum dma_data_direction dma_dir;
412b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int cmdid;
413b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u16 control;
414b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 dsmgmt;
415b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	unsigned long flags;
416b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int psegs = bio_phys_segments(ns->queue, bio);
417b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
418d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	nbio = alloc_nbio(psegs, GFP_NOIO);
419d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	if (!nbio)
420b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto congestion;
421d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	nbio->bio = bio;
422b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
423d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	cmdid = alloc_cmdid(nvmeq, nbio, bio_completion_id, IO_TIMEOUT);
424b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (unlikely(cmdid < 0))
425d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox		goto free_nbio;
426b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
427b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	control = 0;
428b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (bio->bi_rw & REQ_FUA)
429b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		control |= NVME_RW_FUA;
430b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
431b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		control |= NVME_RW_LR;
432b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
433b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dsmgmt = 0;
434b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (bio->bi_rw & REQ_RAHEAD)
435b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
436b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
437b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	spin_lock_irqsave(&nvmeq->q_lock, flags);
438ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
439b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
440b8deb62cf271fa9381edc8cf52bcae2f0225c55aMatthew Wilcox	memset(cmnd, 0, sizeof(*cmnd));
441b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (bio_data_dir(bio)) {
442ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		cmnd->rw.opcode = nvme_cmd_write;
443b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		dma_dir = DMA_TO_DEVICE;
444b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	} else {
445ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox		cmnd->rw.opcode = nvme_cmd_read;
446b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		dma_dir = DMA_FROM_DEVICE;
447b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
448b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
449d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	nvme_map_bio(nvmeq->q_dmadev, nbio, bio, dma_dir, psegs);
450b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
451ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd->rw.flags = 1;
452ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd->rw.command_id = cmdid;
453ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
454d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	nbio->prps = nvme_setup_prps(nvmeq, &cmnd->common, nbio->sg,
455e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews								bio->bi_size);
456ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd->rw.slba = cpu_to_le64(bio->bi_sector >> (ns->lba_shift - 9));
457ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd->rw.length = cpu_to_le16((bio->bi_size >> ns->lba_shift) - 1);
458ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd->rw.control = cpu_to_le16(control);
459ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
460b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
461b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	writel(nvmeq->sq_tail, nvmeq->q_db);
462b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (++nvmeq->sq_tail == nvmeq->q_depth)
463b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		nvmeq->sq_tail = 0;
464b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
465b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	spin_unlock_irqrestore(&nvmeq->q_lock, flags);
466b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
467b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
468b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
469d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox free_nbio:
470d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox	free_nbio(nvmeq, nbio);
471b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox congestion:
472b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return -EBUSY;
473b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
474b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
4759294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcoxstatic void nvme_resubmit_bio(struct nvme_queue *nvmeq, struct bio *bio)
4769294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox{
4779294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox	struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
4789294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox	if (nvme_submit_bio_queue(nvmeq, ns, bio))
4799294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox		bio_list_add_head(&nvmeq->sq_cong, bio);
4809294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox	else if (bio_list_empty(&nvmeq->sq_cong))
4819294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox		blk_clear_queue_congested(ns->queue, rw_is_sync(bio->bi_rw));
4829294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox	/* XXX: Need to duplicate the logic from __freed_request here */
4839294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox}
4849294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox
485b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/*
486b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * NB: return value of non-zero would mean that we were a stacking driver.
487b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * make_request must always succeed.
488b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
489b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_make_request(struct request_queue *q, struct bio *bio)
490b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
491b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_ns *ns = q->queuedata;
492b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_queue *nvmeq = get_nvmeq(ns);
493b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
494b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
495b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		blk_set_queue_congested(q, rw_is_sync(bio->bi_rw));
4969294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox		spin_lock_irq(&nvmeq->q_lock);
497b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		bio_list_add(&nvmeq->sq_cong, bio);
4989294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox		spin_unlock_irq(&nvmeq->q_lock);
499b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
500b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	put_nvmeq(nvmeq);
501b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
502b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
503b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
504b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
505b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct sync_cmd_info {
506b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct task_struct *task;
507b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 result;
508b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int status;
509b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
510b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
511b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void sync_completion(struct nvme_queue *nvmeq, void *ctx,
512b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox						struct nvme_completion *cqe)
513b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
514b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct sync_cmd_info *cmdinfo = ctx;
515be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox	if ((unsigned long)cmdinfo == CMD_CTX_CANCELLED)
516be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox		return;
517b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox	if (unlikely((unsigned long)cmdinfo == CMD_CTX_COMPLETED)) {
518b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox		dev_warn(nvmeq->q_dmadev,
519b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox				"completed id %d twice on queue %d\n",
520b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox				cqe->command_id, le16_to_cpup(&cqe->sq_id));
521b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox		return;
522b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox	}
52348e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox	if (unlikely((unsigned long)cmdinfo == CMD_CTX_INVALID)) {
52448e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox		dev_warn(nvmeq->q_dmadev,
52548e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox				"invalid id %d completed on queue %d\n",
52648e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox				cqe->command_id, le16_to_cpup(&cqe->sq_id));
52748e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox		return;
52848e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox	}
529b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cmdinfo->result = le32_to_cpup(&cqe->result);
530b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
531b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	wake_up_process(cmdinfo->task);
532b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
533b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
534b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxtypedef void (*completion_fn)(struct nvme_queue *, void *,
535b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox						struct nvme_completion *);
536b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
537b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic irqreturn_t nvme_process_cq(struct nvme_queue *nvmeq)
538b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
539821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox	u16 head, phase;
540b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
541b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	static const completion_fn completions[4] = {
542b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		[sync_completion_id] = sync_completion,
543b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		[bio_completion_id]  = bio_completion,
544b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	};
545b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
546b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	head = nvmeq->cq_head;
547821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox	phase = nvmeq->cq_phase;
548b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
549b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	for (;;) {
550b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		unsigned long data;
551b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		void *ptr;
552b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		unsigned char handler;
553b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		struct nvme_completion cqe = nvmeq->cqes[head];
554821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox		if ((le16_to_cpu(cqe.status) & 1) != phase)
555b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			break;
556b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
557b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (++head == nvmeq->q_depth) {
558b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			head = 0;
559821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox			phase = !phase;
560b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		}
561b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
562b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		data = free_cmdid(nvmeq, cqe.command_id);
563b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		handler = data & 3;
564b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		ptr = (void *)(data & ~3UL);
565b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		completions[handler](nvmeq, ptr, &cqe);
566b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
567b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
568b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	/* If the controller ignores the cq head doorbell and continuously
569b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	 * writes to the queue, it is theoretically possible to wrap around
570b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	 * the queue twice and mistakenly return IRQ_NONE.  Linux only
571b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	 * requires that 0.1% of your interrupts are handled, so this isn't
572b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	 * a big problem.
573b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	 */
574821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox	if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
575b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return IRQ_NONE;
576b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
577b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	writel(head, nvmeq->q_db + 1);
578b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->cq_head = head;
579821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox	nvmeq->cq_phase = phase;
580b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
581b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return IRQ_HANDLED;
582b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
583b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
584b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic irqreturn_t nvme_irq(int irq, void *data)
585b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
58658ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	irqreturn_t result;
58758ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	struct nvme_queue *nvmeq = data;
58858ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	spin_lock(&nvmeq->q_lock);
58958ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	result = nvme_process_cq(nvmeq);
59058ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	spin_unlock(&nvmeq->q_lock);
59158ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	return result;
59258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox}
59358ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox
59458ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxstatic irqreturn_t nvme_irq_check(int irq, void *data)
59558ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox{
59658ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	struct nvme_queue *nvmeq = data;
59758ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
59858ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
59958ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox		return IRQ_NONE;
60058ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	return IRQ_WAKE_THREAD;
60158ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox}
60258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox
6033c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcoxstatic void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
6043c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox{
6053c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox	spin_lock_irq(&nvmeq->q_lock);
606be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox	cancel_cmdid_data(nvmeq, cmdid);
6073c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox	spin_unlock_irq(&nvmeq->q_lock);
6083c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox}
6093c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox
610b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/*
611b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Returns 0 on success.  If the result is negative, it's a Linux error code;
612b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * if the result is positive, it's an NVM Express status code
613b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */
6143c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcoxstatic int nvme_submit_sync_cmd(struct nvme_queue *nvmeq,
615e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox			struct nvme_command *cmd, u32 *result, unsigned timeout)
616b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
617b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int cmdid;
618b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct sync_cmd_info cmdinfo;
619b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
620b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cmdinfo.task = current;
621b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cmdinfo.status = -EINTR;
622b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
623e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	cmdid = alloc_cmdid_killable(nvmeq, &cmdinfo, sync_completion_id,
624e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox								timeout);
625b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (cmdid < 0)
626b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return cmdid;
627b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cmd->common.command_id = cmdid;
628b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
6293c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox	set_current_state(TASK_KILLABLE);
6303c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox	nvme_submit_cmd(nvmeq, cmd);
631b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	schedule();
632b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
6333c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox	if (cmdinfo.status == -EINTR) {
6343c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox		nvme_abort_command(nvmeq, cmdid);
6353c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox		return -EINTR;
6363c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox	}
6373c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox
638b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (result)
639b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		*result = cmdinfo.result;
640b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
641b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return cmdinfo.status;
642b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
643b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
644b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
645b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox								u32 *result)
646b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
647e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT);
648b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
649b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
650b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
651b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
652b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int status;
653b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_command c;
654b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
655b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset(&c, 0, sizeof(c));
656b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.delete_queue.opcode = opcode;
657b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.delete_queue.qid = cpu_to_le16(id);
658b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
659b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	status = nvme_submit_admin_cmd(dev, &c, NULL);
660b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (status)
661b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -EIO;
662b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
663b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
664b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
665b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
666b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox						struct nvme_queue *nvmeq)
667b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
668b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int status;
669b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_command c;
670b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
671b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
672b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset(&c, 0, sizeof(c));
673b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_cq.opcode = nvme_admin_create_cq;
674b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
675b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_cq.cqid = cpu_to_le16(qid);
676b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
677b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_cq.cq_flags = cpu_to_le16(flags);
678b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
679b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
680b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	status = nvme_submit_admin_cmd(dev, &c, NULL);
681b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (status)
682b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -EIO;
683b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
684b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
685b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
686b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
687b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox						struct nvme_queue *nvmeq)
688b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
689b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int status;
690b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_command c;
691b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
692b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
693b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset(&c, 0, sizeof(c));
694b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_sq.opcode = nvme_admin_create_sq;
695b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
696b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_sq.sqid = cpu_to_le16(qid);
697b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
698b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_sq.sq_flags = cpu_to_le16(flags);
699b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.create_sq.cqid = cpu_to_le16(qid);
700b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
701b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	status = nvme_submit_admin_cmd(dev, &c, NULL);
702b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (status)
703b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -EIO;
704b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
705b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
706b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
707b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
708b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
709b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
710b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
711b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
712b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
713b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
714b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
715b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
716b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
717b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_free_queue(struct nvme_dev *dev, int qid)
718b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
719b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_queue *nvmeq = dev->queues[qid];
720b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
721b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	free_irq(dev->entry[nvmeq->cq_vector].vector, nvmeq);
722b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
723b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	/* Don't tell the adapter to delete the admin queue */
724b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (qid) {
725b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		adapter_delete_sq(dev, qid);
726b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		adapter_delete_cq(dev, qid);
727b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
728b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
729b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
730b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox				(void *)nvmeq->cqes, nvmeq->cq_dma_addr);
731b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
732b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox					nvmeq->sq_cmds, nvmeq->sq_dma_addr);
733b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(nvmeq);
734b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
735b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
736b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
737b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox							int depth, int vector)
738b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
739b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct device *dmadev = &dev->pci_dev->dev;
740e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	unsigned extra = (depth / 8) + (depth * sizeof(struct nvme_cmd_info));
741b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
742b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!nvmeq)
743b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return NULL;
744b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
745b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
746b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox					&nvmeq->cq_dma_addr, GFP_KERNEL);
747b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!nvmeq->cqes)
748b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto free_nvmeq;
749b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
750b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
751b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
752b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox					&nvmeq->sq_dma_addr, GFP_KERNEL);
753b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!nvmeq->sq_cmds)
754b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto free_cqdma;
755b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
756b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->q_dmadev = dmadev;
757091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	nvmeq->dev = dev;
758b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	spin_lock_init(&nvmeq->q_lock);
759b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->cq_head = 0;
760821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox	nvmeq->cq_phase = 1;
761b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	init_waitqueue_head(&nvmeq->sq_full);
762b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	bio_list_init(&nvmeq->sq_cong);
763b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->q_db = &dev->dbs[qid * 2];
764b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->q_depth = depth;
765b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq->cq_vector = vector;
766b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
767b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return nvmeq;
768b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
769b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_cqdma:
770b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(dmadev, CQ_SIZE(nvmeq->q_depth), (void *)nvmeq->cqes,
771b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox							nvmeq->cq_dma_addr);
772b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_nvmeq:
773b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(nvmeq);
774b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return NULL;
775b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
776b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
7773001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcoxstatic int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
7783001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox							const char *name)
7793001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox{
78058ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox	if (use_threaded_interrupts)
78158ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox		return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
782ec6ce618d65b5ce1bef83a5509255107a0feac44Matthew Wilcox					nvme_irq_check, nvme_irq,
78358ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox					IRQF_DISABLED | IRQF_SHARED,
78458ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox					name, nvmeq);
7853001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox	return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
7863001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox				IRQF_DISABLED | IRQF_SHARED, name, nvmeq);
7873001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox}
7883001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox
789b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic __devinit struct nvme_queue *nvme_create_queue(struct nvme_dev *dev,
790b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox					int qid, int cq_size, int vector)
791b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
792b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int result;
793b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_queue *nvmeq = nvme_alloc_queue(dev, qid, cq_size, vector);
794b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
7953f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox	if (!nvmeq)
7963f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox		return NULL;
7973f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox
798b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	result = adapter_alloc_cq(dev, qid, nvmeq);
799b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (result < 0)
800b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto free_nvmeq;
801b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
802b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	result = adapter_alloc_sq(dev, qid, nvmeq);
803b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (result < 0)
804b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto release_cq;
805b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
8063001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox	result = queue_request_irq(dev, nvmeq, "nvme");
807b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (result < 0)
808b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto release_sq;
809b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
810b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return nvmeq;
811b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
812b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox release_sq:
813b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	adapter_delete_sq(dev, qid);
814b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox release_cq:
815b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	adapter_delete_cq(dev, qid);
816b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_nvmeq:
817b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
818b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox				(void *)nvmeq->cqes, nvmeq->cq_dma_addr);
819b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
820b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox					nvmeq->sq_cmds, nvmeq->sq_dma_addr);
821b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(nvmeq);
822b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return NULL;
823b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
824b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
825b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_configure_admin_queue(struct nvme_dev *dev)
826b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
827b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int result;
828b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 aqa;
829b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_queue *nvmeq;
830b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
831b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->dbs = ((void __iomem *)dev->bar) + 4096;
832b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
833b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
8343f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox	if (!nvmeq)
8353f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox		return -ENOMEM;
836b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
837b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	aqa = nvmeq->q_depth - 1;
838b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	aqa |= aqa << 16;
839b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
840b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
841b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
842b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
843b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
8445911f20039ce59d7e7834f0c42151cf759b6f786Shane Michael Matthews	writel(0, &dev->bar->cc);
845b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	writel(aqa, &dev->bar->aqa);
846b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
847b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
848b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	writel(dev->ctrl_config, &dev->bar->cc);
849b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
850b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	while (!(readl(&dev->bar->csts) & NVME_CSTS_RDY)) {
851b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		msleep(100);
852b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (fatal_signal_pending(current))
853b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			return -EINTR;
854b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
855b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
8563001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox	result = queue_request_irq(dev, nvmeq, "nvme admin");
857b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->queues[0] = nvmeq;
858b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return result;
859b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
860b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
8617fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcoxstatic int nvme_map_user_pages(struct nvme_dev *dev, int write,
8627fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox				unsigned long addr, unsigned length,
8637fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox				struct scatterlist **sgp)
864b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
86536c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	int i, err, count, nents, offset;
8667fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	struct scatterlist *sg;
8677fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	struct page **pages;
86836c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox
86936c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	if (addr & 3)
87036c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox		return -EINVAL;
8717fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	if (!length)
8727fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox		return -EINVAL;
8737fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox
87436c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	offset = offset_in_page(addr);
8757fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
8767fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
87736c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox
87836c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	err = get_user_pages_fast(addr, count, 1, pages);
87936c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	if (err < count) {
88036c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox		count = err;
88136c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox		err = -EFAULT;
88236c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox		goto put_pages;
88336c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	}
8847fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox
8857fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	sg = kcalloc(count, sizeof(*sg), GFP_KERNEL);
88636c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	sg_init_table(sg, count);
887ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox	sg_set_page(&sg[0], pages[0], PAGE_SIZE - offset, offset);
8887fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	length -= (PAGE_SIZE - offset);
8897fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	for (i = 1; i < count; i++) {
8907fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox		sg_set_page(&sg[i], pages[i], min_t(int, length, PAGE_SIZE), 0);
8917fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox		length -= PAGE_SIZE;
8927fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	}
8937fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox
8947fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	err = -ENOMEM;
8957fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
8967fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox				write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
89736c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	if (!nents)
89836c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox		goto put_pages;
899b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
9007fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	kfree(pages);
9017fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	*sgp = sg;
9027fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	return nents;
903b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
9047fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox put_pages:
9057fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	for (i = 0; i < count; i++)
9067fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox		put_page(pages[i]);
9077fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	kfree(pages);
9087fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	return err;
9097fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox}
910b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
9117fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcoxstatic void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
9127fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox				unsigned long addr, int length,
9137fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox				struct scatterlist *sg, int nents)
9147fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox{
9157fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	int i, count;
916b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
9177fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	count = DIV_ROUND_UP(offset_in_page(addr) + length, PAGE_SIZE);
91836c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	dma_unmap_sg(&dev->pci_dev->dev, sg, nents, DMA_FROM_DEVICE);
9197fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox
92036c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox	for (i = 0; i < count; i++)
9217fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox		put_page(sg_page(&sg[i]));
9227fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox}
923b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
9247fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcoxstatic int nvme_submit_user_admin_command(struct nvme_dev *dev,
9257fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox					unsigned long addr, unsigned length,
9267fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox					struct nvme_command *cmd)
9277fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox{
9287fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	int err, nents;
9297fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	struct scatterlist *sg;
930e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	struct nvme_prps *prps;
9317fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox
9327fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	nents = nvme_map_user_pages(dev, 0, addr, length, &sg);
9337fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	if (nents < 0)
9347fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox		return nents;
935e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	prps = nvme_setup_prps(dev->queues[0], &cmd->common, sg, length);
9367fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	err = nvme_submit_admin_cmd(dev, cmd, NULL);
9377fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	nvme_unmap_user_pages(dev, 0, addr, length, sg, nents);
938e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	nvme_free_prps(dev->queues[0], prps);
9397fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox	return err ? -EIO : 0;
940b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
941b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
942bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcoxstatic int nvme_identify(struct nvme_ns *ns, unsigned long addr, int cns)
943b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
944b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_command c;
945b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
946bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox	memset(&c, 0, sizeof(c));
947bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox	c.identify.opcode = nvme_admin_identify;
948bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox	c.identify.nsid = cns ? 0 : cpu_to_le32(ns->ns_id);
949bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox	c.identify.cns = cpu_to_le32(cns);
950bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox
951bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox	return nvme_submit_user_admin_command(ns->dev, addr, 4096, &c);
952bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox}
953bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox
954bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcoxstatic int nvme_get_range_type(struct nvme_ns *ns, unsigned long addr)
955bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox{
956bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox	struct nvme_command c;
957b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
958b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset(&c, 0, sizeof(c));
959b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.features.opcode = nvme_admin_get_features;
960b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.features.nsid = cpu_to_le32(ns->ns_id);
961b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.features.fid = cpu_to_le32(NVME_FEAT_LBA_RANGE);
962b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
963bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox	return nvme_submit_user_admin_command(ns->dev, addr, 4096, &c);
964b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
965b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
966a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcoxstatic int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
967a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox{
968a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	struct nvme_dev *dev = ns->dev;
969a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	struct nvme_queue *nvmeq;
970a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	struct nvme_user_io io;
971a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	struct nvme_command c;
972a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	unsigned length;
973a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	u32 result;
974a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	int nents, status;
975a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	struct scatterlist *sg;
976e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	struct nvme_prps *prps;
977a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox
978a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	if (copy_from_user(&io, uio, sizeof(io)))
979a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox		return -EFAULT;
980a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	length = io.nblocks << io.block_shift;
981a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	nents = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length, &sg);
982a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	if (nents < 0)
983a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox		return nents;
984a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox
985a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	memset(&c, 0, sizeof(c));
986a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.opcode = io.opcode;
987a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.flags = io.flags;
988a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.nsid = cpu_to_le32(io.nsid);
989a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.slba = cpu_to_le64(io.slba);
990a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.length = cpu_to_le16(io.nblocks - 1);
991a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.control = cpu_to_le16(io.control);
992a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.dsmgmt = cpu_to_le16(io.dsmgmt);
993a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.reftag = cpu_to_le32(io.reftag);	/* XXX: endian? */
994a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.apptag = cpu_to_le16(io.apptag);
995a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	c.rw.appmask = cpu_to_le16(io.appmask);
996e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	nvmeq = get_nvmeq(ns);
997a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	/* XXX: metadata */
998e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	prps = nvme_setup_prps(nvmeq, &c.common, sg, length);
999a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox
1000b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox	/* Since nvme_submit_sync_cmd sleeps, we can't keep preemption
1001b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox	 * disabled.  We may be preempted at any point, and be rescheduled
1002b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox	 * to a different CPU.  That will cause cacheline bouncing, but no
1003b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox	 * additional races since q_lock already protects against other CPUs.
1004b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox	 */
1005a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	put_nvmeq(nvmeq);
1006e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox	status = nvme_submit_sync_cmd(nvmeq, &c, &result, IO_TIMEOUT);
1007a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox
1008a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	nvme_unmap_user_pages(dev, io.opcode & 1, io.addr, length, sg, nents);
1009e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	nvme_free_prps(nvmeq, prps);
1010a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	put_user(result, &uio->result);
1011a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	return status;
1012a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox}
1013a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox
10146ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcoxstatic int nvme_download_firmware(struct nvme_ns *ns,
10156ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox						struct nvme_dlfw __user *udlfw)
10166ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox{
10176ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	struct nvme_dev *dev = ns->dev;
10186ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	struct nvme_dlfw dlfw;
10196ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	struct nvme_command c;
10206ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	int nents, status;
10216ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	struct scatterlist *sg;
1022e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	struct nvme_prps *prps;
10236ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox
10246ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	if (copy_from_user(&dlfw, udlfw, sizeof(dlfw)))
10256ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox		return -EFAULT;
10266ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	if (dlfw.length >= (1 << 30))
10276ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox		return -EINVAL;
10286ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox
10296ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	nents = nvme_map_user_pages(dev, 1, dlfw.addr, dlfw.length * 4, &sg);
10306ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	if (nents < 0)
10316ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox		return nents;
10326ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox
10336ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	memset(&c, 0, sizeof(c));
10346ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	c.dlfw.opcode = nvme_admin_download_fw;
10356ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	c.dlfw.numd = cpu_to_le32(dlfw.length);
10366ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	c.dlfw.offset = cpu_to_le32(dlfw.offset);
1037e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	prps = nvme_setup_prps(dev->queues[0], &c.common, sg, dlfw.length * 4);
10386ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox
10396ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	status = nvme_submit_admin_cmd(dev, &c, NULL);
10406ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	nvme_unmap_user_pages(dev, 0, dlfw.addr, dlfw.length * 4, sg, nents);
1041e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews	nvme_free_prps(dev->queues[0], prps);
10426ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	return status;
10436ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox}
10446ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox
10456ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcoxstatic int nvme_activate_firmware(struct nvme_ns *ns, unsigned long arg)
10466ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox{
10476ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	struct nvme_dev *dev = ns->dev;
10486ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	struct nvme_command c;
10496ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox
10506ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	memset(&c, 0, sizeof(c));
10516ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	c.common.opcode = nvme_admin_activate_fw;
10526ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	c.common.rsvd10[0] = cpu_to_le32(arg);
10536ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox
10546ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	return nvme_submit_admin_cmd(dev, &c, NULL);
10556ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox}
10566ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox
1057b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1058b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox							unsigned long arg)
1059b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1060b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_ns *ns = bdev->bd_disk->private_data;
1061b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1062b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	switch (cmd) {
1063b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	case NVME_IOCTL_IDENTIFY_NS:
106436c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox		return nvme_identify(ns, arg, 0);
1065b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	case NVME_IOCTL_IDENTIFY_CTRL:
106636c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox		return nvme_identify(ns, arg, 1);
1067b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	case NVME_IOCTL_GET_RANGE_TYPE:
1068bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox		return nvme_get_range_type(ns, arg);
1069a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox	case NVME_IOCTL_SUBMIT_IO:
1070a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox		return nvme_submit_io(ns, (void __user *)arg);
10716ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	case NVME_IOCTL_DOWNLOAD_FW:
10726ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox		return nvme_download_firmware(ns, (void __user *)arg);
10736ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox	case NVME_IOCTL_ACTIVATE_FW:
10746ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox		return nvme_activate_firmware(ns, arg);
1075b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	default:
1076b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -ENOTTY;
1077b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1078b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1079b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1080b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic const struct block_device_operations nvme_fops = {
1081b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.owner		= THIS_MODULE,
1082b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.ioctl		= nvme_ioctl,
1083b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
1084b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1085b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, int index,
1086b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1087b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1088b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_ns *ns;
1089b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct gendisk *disk;
1090b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int lbaf;
1091b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1092b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1093b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return NULL;
1094b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1095b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1096b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!ns)
1097b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return NULL;
1098b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns->queue = blk_alloc_queue(GFP_KERNEL);
1099b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!ns->queue)
1100b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto out_free_ns;
1101b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns->queue->queue_flags = QUEUE_FLAG_DEFAULT | QUEUE_FLAG_NOMERGES |
1102b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox				QUEUE_FLAG_NONROT | QUEUE_FLAG_DISCARD;
1103b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	blk_queue_make_request(ns->queue, nvme_make_request);
1104b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns->dev = dev;
1105b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns->queue->queuedata = ns;
1106b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1107b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk = alloc_disk(NVME_MINORS);
1108b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!disk)
1109b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto out_free_queue;
1110b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns->ns_id = index;
1111b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns->disk = disk;
1112b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	lbaf = id->flbas & 0xf;
1113b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	ns->lba_shift = id->lbaf[lbaf].ds;
1114b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1115b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk->major = nvme_major;
1116b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk->minors = NVME_MINORS;
1117b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk->first_minor = NVME_MINORS * index;
1118b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk->fops = &nvme_fops;
1119b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk->private_data = ns;
1120b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	disk->queue = ns->queue;
1121388f037f4e7f0a24bac6b1a24f144f5d939f58cfMatthew Wilcox	disk->driverfs_dev = &dev->pci_dev->dev;
1122b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	sprintf(disk->disk_name, "nvme%dn%d", dev->instance, index);
1123b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1124b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1125b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return ns;
1126b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1127b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free_queue:
1128b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	blk_cleanup_queue(ns->queue);
1129b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free_ns:
1130b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(ns);
1131b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return NULL;
1132b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1133b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1134b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_ns_free(struct nvme_ns *ns)
1135b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1136b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	put_disk(ns->disk);
1137b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	blk_cleanup_queue(ns->queue);
1138b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(ns);
1139b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1140b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1141b3b06812e199f248561ce7824a4a8a9cd573c05aMatthew Wilcoxstatic int set_queue_count(struct nvme_dev *dev, int count)
1142b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1143b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int status;
1144b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	u32 result;
1145b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_command c;
1146b3b06812e199f248561ce7824a4a8a9cd573c05aMatthew Wilcox	u32 q_count = (count - 1) | ((count - 1) << 16);
1147b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1148b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset(&c, 0, sizeof(c));
1149b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.features.opcode = nvme_admin_get_features;
1150b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.features.fid = cpu_to_le32(NVME_FEAT_NUM_QUEUES);
1151b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	c.features.dword11 = cpu_to_le32(q_count);
1152b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1153b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	status = nvme_submit_admin_cmd(dev, &c, &result);
1154b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (status)
1155b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -EIO;
1156b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return min(result & 0xffff, result >> 16) + 1;
1157b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1158b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1159b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_setup_io_queues(struct nvme_dev *dev)
1160b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
11611b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	int result, cpu, i, nr_queues;
1162b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
11631b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	nr_queues = num_online_cpus();
11641b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	result = set_queue_count(dev, nr_queues);
11651b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	if (result < 0)
11661b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		return result;
11671b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	if (result < nr_queues)
11681b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		nr_queues = result;
1169b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
11701b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	/* Deregister the admin queue's interrupt */
11711b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	free_irq(dev->entry[0].vector, dev->queues[0]);
11721b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox
11731b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	for (i = 0; i < nr_queues; i++)
11741b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		dev->entry[i].entry = i;
11751b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	for (;;) {
11761b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		result = pci_enable_msix(dev->pci_dev, dev->entry, nr_queues);
11771b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		if (result == 0) {
11781b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox			break;
11791b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		} else if (result > 0) {
11801b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox			nr_queues = result;
11811b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox			continue;
11821b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		} else {
11831b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox			nr_queues = 1;
11841b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox			break;
11851b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		}
11861b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	}
11871b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox
11881b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	result = queue_request_irq(dev, dev->queues[0], "nvme admin");
11891b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	/* XXX: handle failure here */
11901b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox
11911b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	cpu = cpumask_first(cpu_online_mask);
11921b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	for (i = 0; i < nr_queues; i++) {
11931b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu));
11941b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		cpu = cpumask_next(cpu, cpu_online_mask);
11951b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	}
11961b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox
11971b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	for (i = 0; i < nr_queues; i++) {
11981b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		dev->queues[i + 1] = nvme_create_queue(dev, i + 1,
11991b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox							NVME_Q_DEPTH, i);
12001b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		if (!dev->queues[i + 1])
12011b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox			return -ENOMEM;
12021b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox		dev->queue_count++;
12031b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	}
1204b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1205b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
1206b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1207b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1208b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_free_queues(struct nvme_dev *dev)
1209b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1210b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int i;
1211b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1212b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	for (i = dev->queue_count - 1; i >= 0; i--)
1213b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		nvme_free_queue(dev, i);
1214b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1215b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1216b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_dev_add(struct nvme_dev *dev)
1217b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1218b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int res, nn, i;
1219b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_ns *ns, *next;
122051814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	struct nvme_id_ctrl *ctrl;
1221b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	void *id;
1222b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_addr_t dma_addr;
1223b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_command cid, crt;
1224b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1225b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	res = nvme_setup_io_queues(dev);
1226b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (res)
1227b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return res;
1228b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1229b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	/* XXX: Switch to a SG list once prp2 works */
1230b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	id = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr,
1231b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox								GFP_KERNEL);
1232b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1233b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset(&cid, 0, sizeof(cid));
1234b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cid.identify.opcode = nvme_admin_identify;
1235b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cid.identify.nsid = 0;
1236b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cid.identify.prp1 = cpu_to_le64(dma_addr);
1237b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cid.identify.cns = cpu_to_le32(1);
1238b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1239b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	res = nvme_submit_admin_cmd(dev, &cid, NULL);
1240b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (res) {
1241b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		res = -EIO;
1242b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto out_free;
1243b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1244b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
124551814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	ctrl = id;
124651814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	nn = le32_to_cpup(&ctrl->nn);
124751814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
124851814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
124951814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox	memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
1250b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1251b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	cid.identify.cns = 0;
1252b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	memset(&crt, 0, sizeof(crt));
1253b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	crt.features.opcode = nvme_admin_get_features;
1254b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	crt.features.prp1 = cpu_to_le64(dma_addr + 4096);
1255b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	crt.features.fid = cpu_to_le32(NVME_FEAT_LBA_RANGE);
1256b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1257b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	for (i = 0; i < nn; i++) {
1258b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		cid.identify.nsid = cpu_to_le32(i);
1259b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		res = nvme_submit_admin_cmd(dev, &cid, NULL);
1260b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (res)
1261b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			continue;
1262b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1263b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (((struct nvme_id_ns *)id)->ncap == 0)
1264b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			continue;
1265b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1266b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		crt.features.nsid = cpu_to_le32(i);
1267b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		res = nvme_submit_admin_cmd(dev, &crt, NULL);
1268b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (res)
1269b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			continue;
1270b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1271b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		ns = nvme_alloc_ns(dev, i, id, id + 4096);
1272b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		if (ns)
1273b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox			list_add_tail(&ns->list, &dev->namespaces);
1274b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1275b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	list_for_each_entry(ns, &dev->namespaces, list)
1276b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		add_disk(ns->disk);
1277b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1278b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr);
1279b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
1280b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1281b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free:
1282b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
1283b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		list_del(&ns->list);
1284b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		nvme_ns_free(ns);
1285b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1286b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1287b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr);
1288b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return res;
1289b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1290b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1291b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_dev_remove(struct nvme_dev *dev)
1292b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1293b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_ns *ns, *next;
1294b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1295b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	/* TODO: wait all I/O finished or cancel them */
1296b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1297b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
1298b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		list_del(&ns->list);
1299b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		del_gendisk(ns->disk);
1300b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		nvme_ns_free(ns);
1301b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1302b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1303b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_free_queues(dev);
1304b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1305b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
1306b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1307b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1308091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcoxstatic int nvme_setup_prp_pools(struct nvme_dev *dev)
1309091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox{
1310091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	struct device *dmadev = &dev->pci_dev->dev;
1311091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
1312091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox						PAGE_SIZE, PAGE_SIZE, 0);
1313091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	if (!dev->prp_page_pool)
1314091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox		return -ENOMEM;
1315091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox
1316091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	return 0;
1317091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox}
1318091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox
1319091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcoxstatic void nvme_release_prp_pools(struct nvme_dev *dev)
1320091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox{
1321091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	dma_pool_destroy(dev->prp_page_pool);
1322091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox}
1323091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox
1324b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* XXX: Use an ida or something to let remove / add work correctly */
1325b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_set_instance(struct nvme_dev *dev)
1326b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1327b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	static int instance;
1328b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->instance = instance++;
1329b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1330b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1331b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_release_instance(struct nvme_dev *dev)
1332b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1333b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1334b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1335b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_probe(struct pci_dev *pdev,
1336b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox						const struct pci_device_id *id)
1337b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1338574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox	int bars, result = -ENOMEM;
1339b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_dev *dev;
1340b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1341b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1342b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!dev)
1343b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -ENOMEM;
1344b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
1345b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox								GFP_KERNEL);
1346b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!dev->entry)
1347b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto free;
13481b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox	dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
13491b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox								GFP_KERNEL);
1350b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!dev->queues)
1351b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto free;
1352b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
13530ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews	if (pci_enable_device_mem(pdev))
13540ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews		goto free;
1355f64d3365a3e5cb46e69db7e2c82a7cb9a5bed1b8Matthew Wilcox	pci_set_master(pdev);
1356574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox	bars = pci_select_bars(pdev, IORESOURCE_MEM);
1357574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox	if (pci_request_selected_regions(pdev, bars, "nvme"))
1358574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox		goto disable;
13590ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews
1360b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	INIT_LIST_HEAD(&dev->namespaces);
1361b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->pci_dev = pdev;
1362b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	pci_set_drvdata(pdev, dev);
13632930353f9f2b9e4629e935acd970cb73c1171229Matthew Wilcox	dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
13642930353f9f2b9e4629e935acd970cb73c1171229Matthew Wilcox	dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
1365b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_set_instance(dev);
136653c9577e9ca68a633c6e9df2b54eaecacfa77f62Matthew Wilcox	dev->entry[0].vector = pdev->irq;
1367b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1368091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	result = nvme_setup_prp_pools(dev);
1369091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	if (result)
1370091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox		goto disable_msix;
1371091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox
1372b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1373b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!dev->bar) {
1374b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		result = -ENOMEM;
1375574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox		goto disable_msix;
1376b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	}
1377b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1378b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	result = nvme_configure_admin_queue(dev);
1379b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (result)
1380b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto unmap;
1381b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	dev->queue_count++;
1382b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1383b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	result = nvme_dev_add(dev);
1384b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (result)
1385b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		goto delete;
1386b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return 0;
1387b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1388b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox delete:
1389b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_free_queues(dev);
1390b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unmap:
1391b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	iounmap(dev->bar);
1392574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox disable_msix:
1393b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	pci_disable_msix(pdev);
1394b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_release_instance(dev);
1395091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	nvme_release_prp_pools(dev);
1396574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox disable:
13970ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews	pci_disable_device(pdev);
1398574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox	pci_release_regions(pdev);
1399b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free:
1400b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(dev->queues);
1401b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(dev->entry);
1402b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(dev);
1403b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return result;
1404b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1405b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1406b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void __devexit nvme_remove(struct pci_dev *pdev)
1407b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1408b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	struct nvme_dev *dev = pci_get_drvdata(pdev);
1409b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_dev_remove(dev);
1410b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	pci_disable_msix(pdev);
1411b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	iounmap(dev->bar);
1412b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_release_instance(dev);
1413091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox	nvme_release_prp_pools(dev);
14140ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews	pci_disable_device(pdev);
1415574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox	pci_release_regions(pdev);
1416b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(dev->queues);
1417b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(dev->entry);
1418b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	kfree(dev);
1419b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1420b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1421b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* These functions are yet to be implemented */
1422b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_error_detected NULL
1423b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_dump_registers NULL
1424b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_link_reset NULL
1425b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_slot_reset NULL
1426b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_error_resume NULL
1427b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_suspend NULL
1428b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_resume NULL
1429b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1430b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct pci_error_handlers nvme_err_handler = {
1431b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.error_detected	= nvme_error_detected,
1432b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.mmio_enabled	= nvme_dump_registers,
1433b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.link_reset	= nvme_link_reset,
1434b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.slot_reset	= nvme_slot_reset,
1435b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.resume		= nvme_error_resume,
1436b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
1437b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1438b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* Move to pci_ids.h later */
1439b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define PCI_CLASS_STORAGE_EXPRESS	0x010802
1440b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1441b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
1442b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	{ PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
1443b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	{ 0, }
1444b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
1445b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_DEVICE_TABLE(pci, nvme_id_table);
1446b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1447b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct pci_driver nvme_driver = {
1448b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.name		= "nvme",
1449b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.id_table	= nvme_id_table,
1450b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.probe		= nvme_probe,
1451b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.remove		= __devexit_p(nvme_remove),
1452b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.suspend	= nvme_suspend,
1453b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.resume		= nvme_resume,
1454b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	.err_handler	= &nvme_err_handler,
1455b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox};
1456b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1457b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __init nvme_init(void)
1458b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1459b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	int result;
1460b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1461b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	nvme_major = register_blkdev(nvme_major, "nvme");
1462b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (nvme_major <= 0)
1463b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return -EBUSY;
1464b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1465b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	result = pci_register_driver(&nvme_driver);
1466b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	if (!result)
1467b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox		return 0;
1468b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1469b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	unregister_blkdev(nvme_major, "nvme");
1470b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	return result;
1471b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1472b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1473b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void __exit nvme_exit(void)
1474b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{
1475b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	pci_unregister_driver(&nvme_driver);
1476b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox	unregister_blkdev(nvme_major, "nvme");
1477b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}
1478b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox
1479b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
1480b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_LICENSE("GPL");
1481db5d0c198d673b6a932b449d4db95a2ad50c755eMatthew WilcoxMODULE_VERSION("0.2");
1482b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_init(nvme_init);
1483b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_exit(nvme_exit);
1484