blob: f94f1731478f86e9d00894c56f964e1f22fc1ffa [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
3 * Copyright (c) 2011, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#include <linux/nvme.h>
20#include <linux/bio.h>
21#include <linux/blkdev.h>
22#include <linux/errno.h>
23#include <linux/fs.h>
24#include <linux/genhd.h>
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/io.h>
28#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050029#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050030#include <linux/kernel.h>
31#include <linux/mm.h>
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050035#include <linux/poison.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050036#include <linux/sched.h>
37#include <linux/slab.h>
38#include <linux/types.h>
39#include <linux/version.h>
40
41#define NVME_Q_DEPTH 1024
42#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
43#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
44#define NVME_MINORS 64
Matthew Wilcoxe85248e2011-02-06 18:30:16 -050045#define IO_TIMEOUT (5 * HZ)
46#define ADMIN_TIMEOUT (60 * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050047
48static int nvme_major;
49module_param(nvme_major, int, 0);
50
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050051static int use_threaded_interrupts;
52module_param(use_threaded_interrupts, int, 0);
53
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050054static DEFINE_SPINLOCK(dev_list_lock);
55static LIST_HEAD(dev_list);
56static struct task_struct *nvme_thread;
57
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050058/*
59 * Represents an NVM Express device. Each nvme_dev is a PCI function.
60 */
61struct nvme_dev {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050062 struct list_head node;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050063 struct nvme_queue **queues;
64 u32 __iomem *dbs;
65 struct pci_dev *pci_dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -050066 struct dma_pool *prp_page_pool;
Matthew Wilcox99802a72011-02-10 10:30:34 -050067 struct dma_pool *prp_small_pool;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050068 int instance;
69 int queue_count;
70 u32 ctrl_config;
71 struct msix_entry *entry;
72 struct nvme_bar __iomem *bar;
73 struct list_head namespaces;
Matthew Wilcox51814232011-02-01 16:18:08 -050074 char serial[20];
75 char model[40];
76 char firmware_rev[8];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050077};
78
79/*
80 * An NVM Express namespace is equivalent to a SCSI LUN
81 */
82struct nvme_ns {
83 struct list_head list;
84
85 struct nvme_dev *dev;
86 struct request_queue *queue;
87 struct gendisk *disk;
88
89 int ns_id;
90 int lba_shift;
91};
92
93/*
94 * An NVM Express queue. Each device has at least two (one for admin
95 * commands and one for I/O commands).
96 */
97struct nvme_queue {
98 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -050099 struct nvme_dev *dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500100 spinlock_t q_lock;
101 struct nvme_command *sq_cmds;
102 volatile struct nvme_completion *cqes;
103 dma_addr_t sq_dma_addr;
104 dma_addr_t cq_dma_addr;
105 wait_queue_head_t sq_full;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -0500106 wait_queue_t sq_cong_wait;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500107 struct bio_list sq_cong;
108 u32 __iomem *q_db;
109 u16 q_depth;
110 u16 cq_vector;
111 u16 sq_head;
112 u16 sq_tail;
113 u16 cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500114 u16 cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500115 unsigned long cmdid_data[];
116};
117
118/*
119 * Check we didin't inadvertently grow the command struct
120 */
121static inline void _nvme_check_size(void)
122{
123 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
124 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
125 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
126 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
127 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
128 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
129 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
130 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
131 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
132}
133
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500134struct nvme_cmd_info {
135 unsigned long ctx;
136 unsigned long timeout;
137};
138
139static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
140{
141 return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
142}
143
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500144/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400145 * alloc_cmdid() - Allocate a Command ID
146 * @nvmeq: The queue that will be used for this command
147 * @ctx: A pointer that will be passed to the handler
148 * @handler: The ID of the handler to call
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500149 *
150 * Allocate a Command ID for a queue. The data passed in will
151 * be passed to the completion handler. This is implemented by using
152 * the bottom two bits of the ctx pointer to store the handler ID.
153 * Passing in a pointer that's not 4-byte aligned will cause a BUG.
154 * We can change this if it becomes a problem.
155 */
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500156static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx, int handler,
157 unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500158{
Matthew Wilcoxe6d15f72011-02-24 08:49:41 -0500159 int depth = nvmeq->q_depth - 1;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500160 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500161 int cmdid;
162
163 BUG_ON((unsigned long)ctx & 3);
164
165 do {
166 cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
167 if (cmdid >= depth)
168 return -EBUSY;
169 } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
170
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500171 info[cmdid].ctx = (unsigned long)ctx | handler;
172 info[cmdid].timeout = jiffies + timeout;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500173 return cmdid;
174}
175
176static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500177 int handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500178{
179 int cmdid;
180 wait_event_killable(nvmeq->sq_full,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500181 (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500182 return (cmdid < 0) ? -EINTR : cmdid;
183}
184
Matthew Wilcoxfa922822011-03-16 16:29:00 -0400185/*
186 * If you need more than four handlers, you'll need to change how
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500187 * alloc_cmdid and nvme_process_cq work. Consider using a special
188 * CMD_CTX value instead, if that works for your situation.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500189 */
190enum {
191 sync_completion_id = 0,
192 bio_completion_id,
193};
194
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500195/* Special values must be a multiple of 4, and less than 0x1000 */
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500196#define CMD_CTX_BASE (POISON_POINTER_DELTA + sync_completion_id)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500197#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
198#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
199#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500200#define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500201
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500202static unsigned long free_cmdid(struct nvme_queue *nvmeq, int cmdid)
203{
204 unsigned long data;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500205 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500206
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500207 if (cmdid >= nvmeq->q_depth)
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500208 return CMD_CTX_INVALID;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500209 data = info[cmdid].ctx;
210 info[cmdid].ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500211 clear_bit(cmdid, nvmeq->cmdid_data);
212 wake_up(&nvmeq->sq_full);
213 return data;
214}
215
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500216static void cancel_cmdid_data(struct nvme_queue *nvmeq, int cmdid)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500217{
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500218 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
219 info[cmdid].ctx = CMD_CTX_CANCELLED;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500220}
221
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500222static struct nvme_queue *get_nvmeq(struct nvme_ns *ns)
223{
Matthew Wilcox9ecdc942011-03-16 16:52:19 -0400224 return ns->dev->queues[get_cpu() + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500225}
226
227static void put_nvmeq(struct nvme_queue *nvmeq)
228{
Matthew Wilcox1b234842011-01-20 13:01:49 -0500229 put_cpu();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500230}
231
232/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400233 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500234 * @nvmeq: The queue to use
235 * @cmd: The command to send
236 *
237 * Safe to use from interrupt context
238 */
239static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
240{
241 unsigned long flags;
242 u16 tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500243 spin_lock_irqsave(&nvmeq->q_lock, flags);
244 tail = nvmeq->sq_tail;
245 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500246 if (++tail == nvmeq->q_depth)
247 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500248 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500249 nvmeq->sq_tail = tail;
250 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
251
252 return 0;
253}
254
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500255struct nvme_prps {
256 int npages;
257 dma_addr_t first_dma;
258 __le64 *list[0];
259};
260
Matthew Wilcoxd5677602011-02-10 10:47:55 -0500261static void nvme_free_prps(struct nvme_dev *dev, struct nvme_prps *prps)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500262{
263 const int last_prp = PAGE_SIZE / 8 - 1;
264 int i;
265 dma_addr_t prp_dma;
266
267 if (!prps)
268 return;
269
270 prp_dma = prps->first_dma;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500271
272 if (prps->npages == 0)
273 dma_pool_free(dev->prp_small_pool, prps->list[0], prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500274 for (i = 0; i < prps->npages; i++) {
275 __le64 *prp_list = prps->list[i];
276 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500277 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500278 prp_dma = next_prp_dma;
279 }
280 kfree(prps);
281}
282
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500283struct nvme_bio {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500284 struct bio *bio;
285 int nents;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500286 struct nvme_prps *prps;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500287 struct scatterlist sg[0];
288};
289
290/* XXX: use a mempool */
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500291static struct nvme_bio *alloc_nbio(unsigned nseg, gfp_t gfp)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500292{
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500293 return kzalloc(sizeof(struct nvme_bio) +
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500294 sizeof(struct scatterlist) * nseg, gfp);
295}
296
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500297static void free_nbio(struct nvme_queue *nvmeq, struct nvme_bio *nbio)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500298{
Matthew Wilcoxd5677602011-02-10 10:47:55 -0500299 nvme_free_prps(nvmeq->dev, nbio->prps);
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500300 kfree(nbio);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500301}
302
303static void bio_completion(struct nvme_queue *nvmeq, void *ctx,
304 struct nvme_completion *cqe)
305{
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500306 struct nvme_bio *nbio = ctx;
307 struct bio *bio = nbio->bio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500308 u16 status = le16_to_cpup(&cqe->status) >> 1;
309
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500310 dma_unmap_sg(nvmeq->q_dmadev, nbio->sg, nbio->nents,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500311 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500312 free_nbio(nvmeq, nbio);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500313 if (status)
314 bio_endio(bio, -EIO);
315 if (bio->bi_vcnt > bio->bi_idx) {
316 bio_list_add(&nvmeq->sq_cong, bio);
317 wake_up_process(nvme_thread);
318 } else {
319 bio_endio(bio, 0);
320 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500321}
322
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500323/* length is in bytes */
Matthew Wilcoxd5677602011-02-10 10:47:55 -0500324static struct nvme_prps *nvme_setup_prps(struct nvme_dev *dev,
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500325 struct nvme_common_command *cmd,
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500326 struct scatterlist *sg, int length)
327{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500328 struct dma_pool *pool;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500329 int dma_len = sg_dma_len(sg);
330 u64 dma_addr = sg_dma_address(sg);
331 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500332 __le64 *prp_list;
333 dma_addr_t prp_dma;
334 int nprps, npages, i, prp_page;
335 struct nvme_prps *prps = NULL;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500336
337 cmd->prp1 = cpu_to_le64(dma_addr);
338 length -= (PAGE_SIZE - offset);
339 if (length <= 0)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500340 return prps;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500341
342 dma_len -= (PAGE_SIZE - offset);
343 if (dma_len) {
344 dma_addr += (PAGE_SIZE - offset);
345 } else {
346 sg = sg_next(sg);
347 dma_addr = sg_dma_address(sg);
348 dma_len = sg_dma_len(sg);
349 }
350
351 if (length <= PAGE_SIZE) {
352 cmd->prp2 = cpu_to_le64(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500353 return prps;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500354 }
355
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500356 nprps = DIV_ROUND_UP(length, PAGE_SIZE);
357 npages = DIV_ROUND_UP(8 * nprps, PAGE_SIZE);
358 prps = kmalloc(sizeof(*prps) + sizeof(__le64 *) * npages, GFP_ATOMIC);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500359 prp_page = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500360 if (nprps <= (256 / 8)) {
361 pool = dev->prp_small_pool;
362 prps->npages = 0;
363 } else {
364 pool = dev->prp_page_pool;
365 prps->npages = npages;
366 }
367
368 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500369 prps->list[prp_page++] = prp_list;
370 prps->first_dma = prp_dma;
371 cmd->prp2 = cpu_to_le64(prp_dma);
372 i = 0;
373 for (;;) {
Matthew Wilcox7523d832011-03-16 16:43:40 -0400374 if (i == PAGE_SIZE / 8) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500375 __le64 *old_prp_list = prp_list;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500376 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500377 prps->list[prp_page++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400378 prp_list[0] = old_prp_list[i - 1];
379 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
380 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500381 }
382 prp_list[i++] = cpu_to_le64(dma_addr);
383 dma_len -= PAGE_SIZE;
384 dma_addr += PAGE_SIZE;
385 length -= PAGE_SIZE;
386 if (length <= 0)
387 break;
388 if (dma_len > 0)
389 continue;
390 BUG_ON(dma_len < 0);
391 sg = sg_next(sg);
392 dma_addr = sg_dma_address(sg);
393 dma_len = sg_dma_len(sg);
394 }
395
396 return prps;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500397}
398
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500399/* NVMe scatterlists require no holes in the virtual address */
400#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
401 (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
402
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500403static int nvme_map_bio(struct device *dev, struct nvme_bio *nbio,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500404 struct bio *bio, enum dma_data_direction dma_dir, int psegs)
405{
Matthew Wilcox76830842011-02-10 13:55:39 -0500406 struct bio_vec *bvec, *bvprv = NULL;
407 struct scatterlist *sg = NULL;
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500408 int i, old_idx, length = 0, nsegs = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500409
Matthew Wilcox76830842011-02-10 13:55:39 -0500410 sg_init_table(nbio->sg, psegs);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500411 old_idx = bio->bi_idx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500412 bio_for_each_segment(bvec, bio, i) {
Matthew Wilcox76830842011-02-10 13:55:39 -0500413 if (bvprv && BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) {
414 sg->length += bvec->bv_len;
415 } else {
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500416 if (bvprv && BIOVEC_NOT_VIRT_MERGEABLE(bvprv, bvec))
417 break;
Matthew Wilcox76830842011-02-10 13:55:39 -0500418 sg = sg ? sg + 1 : nbio->sg;
419 sg_set_page(sg, bvec->bv_page, bvec->bv_len,
420 bvec->bv_offset);
421 nsegs++;
422 }
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500423 length += bvec->bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500424 bvprv = bvec;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500425 }
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500426 bio->bi_idx = i;
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500427 nbio->nents = nsegs;
Matthew Wilcox76830842011-02-10 13:55:39 -0500428 sg_mark_end(sg);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500429 if (dma_map_sg(dev, nbio->sg, nbio->nents, dma_dir) == 0) {
430 bio->bi_idx = old_idx;
431 return -ENOMEM;
432 }
433 return length;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500434}
435
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500436static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
437 int cmdid)
438{
439 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
440
441 memset(cmnd, 0, sizeof(*cmnd));
442 cmnd->common.opcode = nvme_cmd_flush;
443 cmnd->common.command_id = cmdid;
444 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
445
446 if (++nvmeq->sq_tail == nvmeq->q_depth)
447 nvmeq->sq_tail = 0;
448 writel(nvmeq->sq_tail, nvmeq->q_db);
449
450 return 0;
451}
452
453static int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns)
454{
455 int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH,
456 sync_completion_id, IO_TIMEOUT);
457 if (unlikely(cmdid < 0))
458 return cmdid;
459
460 return nvme_submit_flush(nvmeq, ns, cmdid);
461}
462
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500463static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
464 struct bio *bio)
465{
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500466 struct nvme_command *cmnd;
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500467 struct nvme_bio *nbio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500468 enum dma_data_direction dma_dir;
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500469 int cmdid, length, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500470 u16 control;
471 u32 dsmgmt;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500472 int psegs = bio_phys_segments(ns->queue, bio);
473
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500474 if ((bio->bi_rw & REQ_FLUSH) && psegs) {
475 result = nvme_submit_flush_data(nvmeq, ns);
476 if (result)
477 return result;
478 }
479
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500480 nbio = alloc_nbio(psegs, GFP_ATOMIC);
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500481 if (!nbio)
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500482 goto nomem;
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500483 nbio->bio = bio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500484
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500485 result = -EBUSY;
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500486 cmdid = alloc_cmdid(nvmeq, nbio, bio_completion_id, IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500487 if (unlikely(cmdid < 0))
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500488 goto free_nbio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500489
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500490 if ((bio->bi_rw & REQ_FLUSH) && !psegs)
491 return nvme_submit_flush(nvmeq, ns, cmdid);
492
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500493 control = 0;
494 if (bio->bi_rw & REQ_FUA)
495 control |= NVME_RW_FUA;
496 if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
497 control |= NVME_RW_LR;
498
499 dsmgmt = 0;
500 if (bio->bi_rw & REQ_RAHEAD)
501 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
502
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500503 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500504
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500505 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500506 if (bio_data_dir(bio)) {
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500507 cmnd->rw.opcode = nvme_cmd_write;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500508 dma_dir = DMA_TO_DEVICE;
509 } else {
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500510 cmnd->rw.opcode = nvme_cmd_read;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500511 dma_dir = DMA_FROM_DEVICE;
512 }
513
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500514 result = nvme_map_bio(nvmeq->q_dmadev, nbio, bio, dma_dir, psegs);
515 if (result < 0)
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500516 goto free_nbio;
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500517 length = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500518
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500519 cmnd->rw.command_id = cmdid;
520 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxd5677602011-02-10 10:47:55 -0500521 nbio->prps = nvme_setup_prps(nvmeq->dev, &cmnd->common, nbio->sg,
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500522 length);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500523 cmnd->rw.slba = cpu_to_le64(bio->bi_sector >> (ns->lba_shift - 9));
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500524 cmnd->rw.length = cpu_to_le16((length >> ns->lba_shift) - 1);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500525 cmnd->rw.control = cpu_to_le16(control);
526 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500527
Matthew Wilcoxd8ee9d62011-02-24 08:46:00 -0500528 bio->bi_sector += length >> 9;
529
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500530 if (++nvmeq->sq_tail == nvmeq->q_depth)
531 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500532 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500533
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500534 return 0;
535
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500536 free_nbio:
537 free_nbio(nvmeq, nbio);
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500538 nomem:
539 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500540}
541
542/*
543 * NB: return value of non-zero would mean that we were a stacking driver.
544 * make_request must always succeed.
545 */
546static int nvme_make_request(struct request_queue *q, struct bio *bio)
547{
548 struct nvme_ns *ns = q->queuedata;
549 struct nvme_queue *nvmeq = get_nvmeq(ns);
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500550 int result = -EBUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500551
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500552 spin_lock_irq(&nvmeq->q_lock);
553 if (bio_list_empty(&nvmeq->sq_cong))
554 result = nvme_submit_bio_queue(nvmeq, ns, bio);
555 if (unlikely(result)) {
556 if (bio_list_empty(&nvmeq->sq_cong))
557 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500558 bio_list_add(&nvmeq->sq_cong, bio);
559 }
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500560
561 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500562 put_nvmeq(nvmeq);
563
564 return 0;
565}
566
567struct sync_cmd_info {
568 struct task_struct *task;
569 u32 result;
570 int status;
571};
572
573static void sync_completion(struct nvme_queue *nvmeq, void *ctx,
574 struct nvme_completion *cqe)
575{
576 struct sync_cmd_info *cmdinfo = ctx;
Matthew Wilcoxc4270552011-02-22 14:15:34 -0500577 if (unlikely((unsigned long)cmdinfo == CMD_CTX_CANCELLED))
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500578 return;
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500579 if ((unsigned long)cmdinfo == CMD_CTX_FLUSH)
580 return;
Matthew Wilcoxb36235d2011-02-06 08:49:55 -0500581 if (unlikely((unsigned long)cmdinfo == CMD_CTX_COMPLETED)) {
582 dev_warn(nvmeq->q_dmadev,
583 "completed id %d twice on queue %d\n",
584 cqe->command_id, le16_to_cpup(&cqe->sq_id));
585 return;
586 }
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500587 if (unlikely((unsigned long)cmdinfo == CMD_CTX_INVALID)) {
588 dev_warn(nvmeq->q_dmadev,
589 "invalid id %d completed on queue %d\n",
590 cqe->command_id, le16_to_cpup(&cqe->sq_id));
591 return;
592 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500593 cmdinfo->result = le32_to_cpup(&cqe->result);
594 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
595 wake_up_process(cmdinfo->task);
596}
597
598typedef void (*completion_fn)(struct nvme_queue *, void *,
599 struct nvme_completion *);
600
601static irqreturn_t nvme_process_cq(struct nvme_queue *nvmeq)
602{
Matthew Wilcox82123462011-01-20 13:24:06 -0500603 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500604
605 static const completion_fn completions[4] = {
606 [sync_completion_id] = sync_completion,
607 [bio_completion_id] = bio_completion,
608 };
609
610 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500611 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500612
613 for (;;) {
614 unsigned long data;
615 void *ptr;
616 unsigned char handler;
617 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500618 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500619 break;
620 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
621 if (++head == nvmeq->q_depth) {
622 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500623 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500624 }
625
626 data = free_cmdid(nvmeq, cqe.command_id);
627 handler = data & 3;
628 ptr = (void *)(data & ~3UL);
629 completions[handler](nvmeq, ptr, &cqe);
630 }
631
632 /* If the controller ignores the cq head doorbell and continuously
633 * writes to the queue, it is theoretically possible to wrap around
634 * the queue twice and mistakenly return IRQ_NONE. Linux only
635 * requires that 0.1% of your interrupts are handled, so this isn't
636 * a big problem.
637 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500638 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500639 return IRQ_NONE;
640
641 writel(head, nvmeq->q_db + 1);
642 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500643 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500644
645 return IRQ_HANDLED;
646}
647
648static irqreturn_t nvme_irq(int irq, void *data)
649{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500650 irqreturn_t result;
651 struct nvme_queue *nvmeq = data;
652 spin_lock(&nvmeq->q_lock);
653 result = nvme_process_cq(nvmeq);
654 spin_unlock(&nvmeq->q_lock);
655 return result;
656}
657
658static irqreturn_t nvme_irq_check(int irq, void *data)
659{
660 struct nvme_queue *nvmeq = data;
661 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
662 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
663 return IRQ_NONE;
664 return IRQ_WAKE_THREAD;
665}
666
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500667static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
668{
669 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500670 cancel_cmdid_data(nvmeq, cmdid);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500671 spin_unlock_irq(&nvmeq->q_lock);
672}
673
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500674/*
675 * Returns 0 on success. If the result is negative, it's a Linux error code;
676 * if the result is positive, it's an NVM Express status code
677 */
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500678static int nvme_submit_sync_cmd(struct nvme_queue *nvmeq,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500679 struct nvme_command *cmd, u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500680{
681 int cmdid;
682 struct sync_cmd_info cmdinfo;
683
684 cmdinfo.task = current;
685 cmdinfo.status = -EINTR;
686
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500687 cmdid = alloc_cmdid_killable(nvmeq, &cmdinfo, sync_completion_id,
688 timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500689 if (cmdid < 0)
690 return cmdid;
691 cmd->common.command_id = cmdid;
692
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500693 set_current_state(TASK_KILLABLE);
694 nvme_submit_cmd(nvmeq, cmd);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500695 schedule();
696
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500697 if (cmdinfo.status == -EINTR) {
698 nvme_abort_command(nvmeq, cmdid);
699 return -EINTR;
700 }
701
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500702 if (result)
703 *result = cmdinfo.result;
704
705 return cmdinfo.status;
706}
707
708static int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
709 u32 *result)
710{
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500711 return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500712}
713
714static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
715{
716 int status;
717 struct nvme_command c;
718
719 memset(&c, 0, sizeof(c));
720 c.delete_queue.opcode = opcode;
721 c.delete_queue.qid = cpu_to_le16(id);
722
723 status = nvme_submit_admin_cmd(dev, &c, NULL);
724 if (status)
725 return -EIO;
726 return 0;
727}
728
729static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
730 struct nvme_queue *nvmeq)
731{
732 int status;
733 struct nvme_command c;
734 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
735
736 memset(&c, 0, sizeof(c));
737 c.create_cq.opcode = nvme_admin_create_cq;
738 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
739 c.create_cq.cqid = cpu_to_le16(qid);
740 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
741 c.create_cq.cq_flags = cpu_to_le16(flags);
742 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
743
744 status = nvme_submit_admin_cmd(dev, &c, NULL);
745 if (status)
746 return -EIO;
747 return 0;
748}
749
750static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
751 struct nvme_queue *nvmeq)
752{
753 int status;
754 struct nvme_command c;
755 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
756
757 memset(&c, 0, sizeof(c));
758 c.create_sq.opcode = nvme_admin_create_sq;
759 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
760 c.create_sq.sqid = cpu_to_le16(qid);
761 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
762 c.create_sq.sq_flags = cpu_to_le16(flags);
763 c.create_sq.cqid = cpu_to_le16(qid);
764
765 status = nvme_submit_admin_cmd(dev, &c, NULL);
766 if (status)
767 return -EIO;
768 return 0;
769}
770
771static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
772{
773 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
774}
775
776static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
777{
778 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
779}
780
781static void nvme_free_queue(struct nvme_dev *dev, int qid)
782{
783 struct nvme_queue *nvmeq = dev->queues[qid];
784
785 free_irq(dev->entry[nvmeq->cq_vector].vector, nvmeq);
786
787 /* Don't tell the adapter to delete the admin queue */
788 if (qid) {
789 adapter_delete_sq(dev, qid);
790 adapter_delete_cq(dev, qid);
791 }
792
793 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
794 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
795 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
796 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
797 kfree(nvmeq);
798}
799
800static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
801 int depth, int vector)
802{
803 struct device *dmadev = &dev->pci_dev->dev;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500804 unsigned extra = (depth / 8) + (depth * sizeof(struct nvme_cmd_info));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500805 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
806 if (!nvmeq)
807 return NULL;
808
809 nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
810 &nvmeq->cq_dma_addr, GFP_KERNEL);
811 if (!nvmeq->cqes)
812 goto free_nvmeq;
813 memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
814
815 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
816 &nvmeq->sq_dma_addr, GFP_KERNEL);
817 if (!nvmeq->sq_cmds)
818 goto free_cqdma;
819
820 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500821 nvmeq->dev = dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500822 spin_lock_init(&nvmeq->q_lock);
823 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500824 nvmeq->cq_phase = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500825 init_waitqueue_head(&nvmeq->sq_full);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -0500826 init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500827 bio_list_init(&nvmeq->sq_cong);
828 nvmeq->q_db = &dev->dbs[qid * 2];
829 nvmeq->q_depth = depth;
830 nvmeq->cq_vector = vector;
831
832 return nvmeq;
833
834 free_cqdma:
835 dma_free_coherent(dmadev, CQ_SIZE(nvmeq->q_depth), (void *)nvmeq->cqes,
836 nvmeq->cq_dma_addr);
837 free_nvmeq:
838 kfree(nvmeq);
839 return NULL;
840}
841
Matthew Wilcox30010822011-01-20 09:10:15 -0500842static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
843 const char *name)
844{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500845 if (use_threaded_interrupts)
846 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Matthew Wilcoxec6ce612011-02-06 09:01:00 -0500847 nvme_irq_check, nvme_irq,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500848 IRQF_DISABLED | IRQF_SHARED,
849 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -0500850 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
851 IRQF_DISABLED | IRQF_SHARED, name, nvmeq);
852}
853
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500854static __devinit struct nvme_queue *nvme_create_queue(struct nvme_dev *dev,
855 int qid, int cq_size, int vector)
856{
857 int result;
858 struct nvme_queue *nvmeq = nvme_alloc_queue(dev, qid, cq_size, vector);
859
Matthew Wilcox3f85d502011-02-01 08:39:04 -0500860 if (!nvmeq)
861 return NULL;
862
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500863 result = adapter_alloc_cq(dev, qid, nvmeq);
864 if (result < 0)
865 goto free_nvmeq;
866
867 result = adapter_alloc_sq(dev, qid, nvmeq);
868 if (result < 0)
869 goto release_cq;
870
Matthew Wilcox30010822011-01-20 09:10:15 -0500871 result = queue_request_irq(dev, nvmeq, "nvme");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500872 if (result < 0)
873 goto release_sq;
874
875 return nvmeq;
876
877 release_sq:
878 adapter_delete_sq(dev, qid);
879 release_cq:
880 adapter_delete_cq(dev, qid);
881 free_nvmeq:
882 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
883 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
884 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
885 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
886 kfree(nvmeq);
887 return NULL;
888}
889
890static int __devinit nvme_configure_admin_queue(struct nvme_dev *dev)
891{
892 int result;
893 u32 aqa;
894 struct nvme_queue *nvmeq;
895
896 dev->dbs = ((void __iomem *)dev->bar) + 4096;
897
898 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
Matthew Wilcox3f85d502011-02-01 08:39:04 -0500899 if (!nvmeq)
900 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500901
902 aqa = nvmeq->q_depth - 1;
903 aqa |= aqa << 16;
904
905 dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
906 dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
907 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
908
Shane Michael Matthews5911f202011-02-01 11:31:55 -0500909 writel(0, &dev->bar->cc);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500910 writel(aqa, &dev->bar->aqa);
911 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
912 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
913 writel(dev->ctrl_config, &dev->bar->cc);
914
915 while (!(readl(&dev->bar->csts) & NVME_CSTS_RDY)) {
916 msleep(100);
917 if (fatal_signal_pending(current))
918 return -EINTR;
919 }
920
Matthew Wilcox30010822011-01-20 09:10:15 -0500921 result = queue_request_irq(dev, nvmeq, "nvme admin");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500922 dev->queues[0] = nvmeq;
923 return result;
924}
925
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500926static int nvme_map_user_pages(struct nvme_dev *dev, int write,
927 unsigned long addr, unsigned length,
928 struct scatterlist **sgp)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500929{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500930 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500931 struct scatterlist *sg;
932 struct page **pages;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500933
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500934 if (addr & 3)
935 return -EINVAL;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500936 if (!length)
937 return -EINVAL;
938
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500939 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500940 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
941 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500942
943 err = get_user_pages_fast(addr, count, 1, pages);
944 if (err < count) {
945 count = err;
946 err = -EFAULT;
947 goto put_pages;
948 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500949
950 sg = kcalloc(count, sizeof(*sg), GFP_KERNEL);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500951 sg_init_table(sg, count);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500952 sg_set_page(&sg[0], pages[0], PAGE_SIZE - offset, offset);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500953 length -= (PAGE_SIZE - offset);
954 for (i = 1; i < count; i++) {
955 sg_set_page(&sg[i], pages[i], min_t(int, length, PAGE_SIZE), 0);
956 length -= PAGE_SIZE;
957 }
958
959 err = -ENOMEM;
960 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
961 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500962 if (!nents)
963 goto put_pages;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500964
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500965 kfree(pages);
966 *sgp = sg;
967 return nents;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500968
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500969 put_pages:
970 for (i = 0; i < count; i++)
971 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500972 kfree(pages);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500973 return err;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500974}
975
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500976static void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
977 unsigned long addr, int length,
978 struct scatterlist *sg, int nents)
979{
980 int i, count;
981
982 count = DIV_ROUND_UP(offset_in_page(addr) + length, PAGE_SIZE);
983 dma_unmap_sg(&dev->pci_dev->dev, sg, nents, DMA_FROM_DEVICE);
984
985 for (i = 0; i < count; i++)
986 put_page(sg_page(&sg[i]));
987}
988
989static int nvme_submit_user_admin_command(struct nvme_dev *dev,
990 unsigned long addr, unsigned length,
991 struct nvme_command *cmd)
992{
993 int err, nents;
994 struct scatterlist *sg;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500995 struct nvme_prps *prps;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500996
997 nents = nvme_map_user_pages(dev, 0, addr, length, &sg);
998 if (nents < 0)
999 return nents;
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001000 prps = nvme_setup_prps(dev, &cmd->common, sg, length);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001001 err = nvme_submit_admin_cmd(dev, cmd, NULL);
1002 nvme_unmap_user_pages(dev, 0, addr, length, sg, nents);
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001003 nvme_free_prps(dev, prps);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001004 return err ? -EIO : 0;
1005}
1006
Matthew Wilcoxbd38c552011-01-26 14:34:32 -05001007static int nvme_identify(struct nvme_ns *ns, unsigned long addr, int cns)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001008{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001009 struct nvme_command c;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001010
Matthew Wilcoxbd38c552011-01-26 14:34:32 -05001011 memset(&c, 0, sizeof(c));
1012 c.identify.opcode = nvme_admin_identify;
1013 c.identify.nsid = cns ? 0 : cpu_to_le32(ns->ns_id);
1014 c.identify.cns = cpu_to_le32(cns);
1015
1016 return nvme_submit_user_admin_command(ns->dev, addr, 4096, &c);
1017}
1018
1019static int nvme_get_range_type(struct nvme_ns *ns, unsigned long addr)
1020{
1021 struct nvme_command c;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001022
1023 memset(&c, 0, sizeof(c));
1024 c.features.opcode = nvme_admin_get_features;
1025 c.features.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001026 c.features.fid = cpu_to_le32(NVME_FEAT_LBA_RANGE);
1027
Matthew Wilcoxbd38c552011-01-26 14:34:32 -05001028 return nvme_submit_user_admin_command(ns->dev, addr, 4096, &c);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001029}
1030
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001031static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1032{
1033 struct nvme_dev *dev = ns->dev;
1034 struct nvme_queue *nvmeq;
1035 struct nvme_user_io io;
1036 struct nvme_command c;
1037 unsigned length;
1038 u32 result;
1039 int nents, status;
1040 struct scatterlist *sg;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001041 struct nvme_prps *prps;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001042
1043 if (copy_from_user(&io, uio, sizeof(io)))
1044 return -EFAULT;
1045 length = io.nblocks << io.block_shift;
1046 nents = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length, &sg);
1047 if (nents < 0)
1048 return nents;
1049
1050 memset(&c, 0, sizeof(c));
1051 c.rw.opcode = io.opcode;
1052 c.rw.flags = io.flags;
1053 c.rw.nsid = cpu_to_le32(io.nsid);
1054 c.rw.slba = cpu_to_le64(io.slba);
1055 c.rw.length = cpu_to_le16(io.nblocks - 1);
1056 c.rw.control = cpu_to_le16(io.control);
1057 c.rw.dsmgmt = cpu_to_le16(io.dsmgmt);
1058 c.rw.reftag = cpu_to_le32(io.reftag); /* XXX: endian? */
1059 c.rw.apptag = cpu_to_le16(io.apptag);
1060 c.rw.appmask = cpu_to_le16(io.appmask);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001061 /* XXX: metadata */
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001062 prps = nvme_setup_prps(dev, &c.common, sg, length);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001063
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001064 nvmeq = get_nvmeq(ns);
Matthew Wilcoxfa922822011-03-16 16:29:00 -04001065 /*
1066 * Since nvme_submit_sync_cmd sleeps, we can't keep preemption
Matthew Wilcoxb1ad37e2011-02-04 16:14:30 -05001067 * disabled. We may be preempted at any point, and be rescheduled
1068 * to a different CPU. That will cause cacheline bouncing, but no
1069 * additional races since q_lock already protects against other CPUs.
1070 */
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001071 put_nvmeq(nvmeq);
Matthew Wilcoxe85248e2011-02-06 18:30:16 -05001072 status = nvme_submit_sync_cmd(nvmeq, &c, &result, IO_TIMEOUT);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001073
1074 nvme_unmap_user_pages(dev, io.opcode & 1, io.addr, length, sg, nents);
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001075 nvme_free_prps(dev, prps);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001076 put_user(result, &uio->result);
1077 return status;
1078}
1079
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001080static int nvme_download_firmware(struct nvme_ns *ns,
1081 struct nvme_dlfw __user *udlfw)
1082{
1083 struct nvme_dev *dev = ns->dev;
1084 struct nvme_dlfw dlfw;
1085 struct nvme_command c;
1086 int nents, status;
1087 struct scatterlist *sg;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001088 struct nvme_prps *prps;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001089
1090 if (copy_from_user(&dlfw, udlfw, sizeof(dlfw)))
1091 return -EFAULT;
1092 if (dlfw.length >= (1 << 30))
1093 return -EINVAL;
1094
1095 nents = nvme_map_user_pages(dev, 1, dlfw.addr, dlfw.length * 4, &sg);
1096 if (nents < 0)
1097 return nents;
1098
1099 memset(&c, 0, sizeof(c));
1100 c.dlfw.opcode = nvme_admin_download_fw;
1101 c.dlfw.numd = cpu_to_le32(dlfw.length);
1102 c.dlfw.offset = cpu_to_le32(dlfw.offset);
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001103 prps = nvme_setup_prps(dev, &c.common, sg, dlfw.length * 4);
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001104
1105 status = nvme_submit_admin_cmd(dev, &c, NULL);
1106 nvme_unmap_user_pages(dev, 0, dlfw.addr, dlfw.length * 4, sg, nents);
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001107 nvme_free_prps(dev, prps);
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001108 return status;
1109}
1110
1111static int nvme_activate_firmware(struct nvme_ns *ns, unsigned long arg)
1112{
1113 struct nvme_dev *dev = ns->dev;
1114 struct nvme_command c;
1115
1116 memset(&c, 0, sizeof(c));
1117 c.common.opcode = nvme_admin_activate_fw;
1118 c.common.rsvd10[0] = cpu_to_le32(arg);
1119
1120 return nvme_submit_admin_cmd(dev, &c, NULL);
1121}
1122
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001123static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1124 unsigned long arg)
1125{
1126 struct nvme_ns *ns = bdev->bd_disk->private_data;
1127
1128 switch (cmd) {
1129 case NVME_IOCTL_IDENTIFY_NS:
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001130 return nvme_identify(ns, arg, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001131 case NVME_IOCTL_IDENTIFY_CTRL:
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001132 return nvme_identify(ns, arg, 1);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001133 case NVME_IOCTL_GET_RANGE_TYPE:
Matthew Wilcoxbd38c552011-01-26 14:34:32 -05001134 return nvme_get_range_type(ns, arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001135 case NVME_IOCTL_SUBMIT_IO:
1136 return nvme_submit_io(ns, (void __user *)arg);
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001137 case NVME_IOCTL_DOWNLOAD_FW:
1138 return nvme_download_firmware(ns, (void __user *)arg);
1139 case NVME_IOCTL_ACTIVATE_FW:
1140 return nvme_activate_firmware(ns, arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001141 default:
1142 return -ENOTTY;
1143 }
1144}
1145
1146static const struct block_device_operations nvme_fops = {
1147 .owner = THIS_MODULE,
1148 .ioctl = nvme_ioctl,
1149};
1150
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001151static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1152{
1153 while (bio_list_peek(&nvmeq->sq_cong)) {
1154 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1155 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
1156 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
1157 bio_list_add_head(&nvmeq->sq_cong, bio);
1158 break;
1159 }
Matthew Wilcox3cb967c2011-03-16 16:45:49 -04001160 if (bio_list_empty(&nvmeq->sq_cong))
1161 remove_wait_queue(&nvmeq->sq_full,
1162 &nvmeq->sq_cong_wait);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001163 }
1164}
1165
1166static int nvme_kthread(void *data)
1167{
1168 struct nvme_dev *dev;
1169
1170 while (!kthread_should_stop()) {
1171 __set_current_state(TASK_RUNNING);
1172 spin_lock(&dev_list_lock);
1173 list_for_each_entry(dev, &dev_list, node) {
1174 int i;
1175 for (i = 0; i < dev->queue_count; i++) {
1176 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001177 if (!nvmeq)
1178 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001179 spin_lock_irq(&nvmeq->q_lock);
1180 if (nvme_process_cq(nvmeq))
1181 printk("process_cq did something\n");
1182 nvme_resubmit_bios(nvmeq);
1183 spin_unlock_irq(&nvmeq->q_lock);
1184 }
1185 }
1186 spin_unlock(&dev_list_lock);
1187 set_current_state(TASK_INTERRUPTIBLE);
1188 schedule_timeout(HZ);
1189 }
1190 return 0;
1191}
1192
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001193static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, int index,
1194 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1195{
1196 struct nvme_ns *ns;
1197 struct gendisk *disk;
1198 int lbaf;
1199
1200 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1201 return NULL;
1202
1203 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1204 if (!ns)
1205 return NULL;
1206 ns->queue = blk_alloc_queue(GFP_KERNEL);
1207 if (!ns->queue)
1208 goto out_free_ns;
1209 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT | QUEUE_FLAG_NOMERGES |
1210 QUEUE_FLAG_NONROT | QUEUE_FLAG_DISCARD;
1211 blk_queue_make_request(ns->queue, nvme_make_request);
1212 ns->dev = dev;
1213 ns->queue->queuedata = ns;
1214
1215 disk = alloc_disk(NVME_MINORS);
1216 if (!disk)
1217 goto out_free_queue;
1218 ns->ns_id = index;
1219 ns->disk = disk;
1220 lbaf = id->flbas & 0xf;
1221 ns->lba_shift = id->lbaf[lbaf].ds;
1222
1223 disk->major = nvme_major;
1224 disk->minors = NVME_MINORS;
1225 disk->first_minor = NVME_MINORS * index;
1226 disk->fops = &nvme_fops;
1227 disk->private_data = ns;
1228 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05001229 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001230 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, index);
1231 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1232
1233 return ns;
1234
1235 out_free_queue:
1236 blk_cleanup_queue(ns->queue);
1237 out_free_ns:
1238 kfree(ns);
1239 return NULL;
1240}
1241
1242static void nvme_ns_free(struct nvme_ns *ns)
1243{
1244 put_disk(ns->disk);
1245 blk_cleanup_queue(ns->queue);
1246 kfree(ns);
1247}
1248
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001249static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001250{
1251 int status;
1252 u32 result;
1253 struct nvme_command c;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001254 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001255
1256 memset(&c, 0, sizeof(c));
1257 c.features.opcode = nvme_admin_get_features;
1258 c.features.fid = cpu_to_le32(NVME_FEAT_NUM_QUEUES);
1259 c.features.dword11 = cpu_to_le32(q_count);
1260
1261 status = nvme_submit_admin_cmd(dev, &c, &result);
1262 if (status)
1263 return -EIO;
1264 return min(result & 0xffff, result >> 16) + 1;
1265}
1266
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001267static int __devinit nvme_setup_io_queues(struct nvme_dev *dev)
1268{
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001269 int result, cpu, i, nr_io_queues;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001270
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001271 nr_io_queues = num_online_cpus();
1272 result = set_queue_count(dev, nr_io_queues);
Matthew Wilcox1b234842011-01-20 13:01:49 -05001273 if (result < 0)
1274 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001275 if (result < nr_io_queues)
1276 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001277
Matthew Wilcox1b234842011-01-20 13:01:49 -05001278 /* Deregister the admin queue's interrupt */
1279 free_irq(dev->entry[0].vector, dev->queues[0]);
1280
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001281 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001282 dev->entry[i].entry = i;
1283 for (;;) {
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001284 result = pci_enable_msix(dev->pci_dev, dev->entry,
1285 nr_io_queues);
Matthew Wilcox1b234842011-01-20 13:01:49 -05001286 if (result == 0) {
1287 break;
1288 } else if (result > 0) {
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001289 nr_io_queues = result;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001290 continue;
1291 } else {
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001292 nr_io_queues = 1;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001293 break;
1294 }
1295 }
1296
1297 result = queue_request_irq(dev, dev->queues[0], "nvme admin");
1298 /* XXX: handle failure here */
1299
1300 cpu = cpumask_first(cpu_online_mask);
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001301 for (i = 0; i < nr_io_queues; i++) {
Matthew Wilcox1b234842011-01-20 13:01:49 -05001302 irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu));
1303 cpu = cpumask_next(cpu, cpu_online_mask);
1304 }
1305
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001306 for (i = 0; i < nr_io_queues; i++) {
Matthew Wilcox1b234842011-01-20 13:01:49 -05001307 dev->queues[i + 1] = nvme_create_queue(dev, i + 1,
1308 NVME_Q_DEPTH, i);
1309 if (!dev->queues[i + 1])
1310 return -ENOMEM;
1311 dev->queue_count++;
1312 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001313
Matthew Wilcox9ecdc942011-03-16 16:52:19 -04001314 for (; i < num_possible_cpus(); i++) {
1315 int target = i % rounddown_pow_of_two(dev->queue_count - 1);
1316 dev->queues[i + 1] = dev->queues[target + 1];
1317 }
1318
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001319 return 0;
1320}
1321
1322static void nvme_free_queues(struct nvme_dev *dev)
1323{
1324 int i;
1325
1326 for (i = dev->queue_count - 1; i >= 0; i--)
1327 nvme_free_queue(dev, i);
1328}
1329
1330static int __devinit nvme_dev_add(struct nvme_dev *dev)
1331{
1332 int res, nn, i;
1333 struct nvme_ns *ns, *next;
Matthew Wilcox51814232011-02-01 16:18:08 -05001334 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001335 void *id;
1336 dma_addr_t dma_addr;
1337 struct nvme_command cid, crt;
1338
1339 res = nvme_setup_io_queues(dev);
1340 if (res)
1341 return res;
1342
1343 /* XXX: Switch to a SG list once prp2 works */
1344 id = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr,
1345 GFP_KERNEL);
1346
1347 memset(&cid, 0, sizeof(cid));
1348 cid.identify.opcode = nvme_admin_identify;
1349 cid.identify.nsid = 0;
1350 cid.identify.prp1 = cpu_to_le64(dma_addr);
1351 cid.identify.cns = cpu_to_le32(1);
1352
1353 res = nvme_submit_admin_cmd(dev, &cid, NULL);
1354 if (res) {
1355 res = -EIO;
1356 goto out_free;
1357 }
1358
Matthew Wilcox51814232011-02-01 16:18:08 -05001359 ctrl = id;
1360 nn = le32_to_cpup(&ctrl->nn);
1361 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
1362 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
1363 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001364
1365 cid.identify.cns = 0;
1366 memset(&crt, 0, sizeof(crt));
1367 crt.features.opcode = nvme_admin_get_features;
1368 crt.features.prp1 = cpu_to_le64(dma_addr + 4096);
1369 crt.features.fid = cpu_to_le32(NVME_FEAT_LBA_RANGE);
1370
Matthew Wilcoxac88c362011-03-16 16:29:58 -04001371 for (i = 0; i <= nn; i++) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001372 cid.identify.nsid = cpu_to_le32(i);
1373 res = nvme_submit_admin_cmd(dev, &cid, NULL);
1374 if (res)
1375 continue;
1376
1377 if (((struct nvme_id_ns *)id)->ncap == 0)
1378 continue;
1379
1380 crt.features.nsid = cpu_to_le32(i);
1381 res = nvme_submit_admin_cmd(dev, &crt, NULL);
1382 if (res)
1383 continue;
1384
1385 ns = nvme_alloc_ns(dev, i, id, id + 4096);
1386 if (ns)
1387 list_add_tail(&ns->list, &dev->namespaces);
1388 }
1389 list_for_each_entry(ns, &dev->namespaces, list)
1390 add_disk(ns->disk);
1391
1392 dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr);
1393 return 0;
1394
1395 out_free:
1396 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
1397 list_del(&ns->list);
1398 nvme_ns_free(ns);
1399 }
1400
1401 dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr);
1402 return res;
1403}
1404
1405static int nvme_dev_remove(struct nvme_dev *dev)
1406{
1407 struct nvme_ns *ns, *next;
1408
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001409 spin_lock(&dev_list_lock);
1410 list_del(&dev->node);
1411 spin_unlock(&dev_list_lock);
1412
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001413 /* TODO: wait all I/O finished or cancel them */
1414
1415 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
1416 list_del(&ns->list);
1417 del_gendisk(ns->disk);
1418 nvme_ns_free(ns);
1419 }
1420
1421 nvme_free_queues(dev);
1422
1423 return 0;
1424}
1425
Matthew Wilcox091b6092011-02-10 09:56:01 -05001426static int nvme_setup_prp_pools(struct nvme_dev *dev)
1427{
1428 struct device *dmadev = &dev->pci_dev->dev;
1429 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
1430 PAGE_SIZE, PAGE_SIZE, 0);
1431 if (!dev->prp_page_pool)
1432 return -ENOMEM;
1433
Matthew Wilcox99802a72011-02-10 10:30:34 -05001434 /* Optimisation for I/Os between 4k and 128k */
1435 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
1436 256, 256, 0);
1437 if (!dev->prp_small_pool) {
1438 dma_pool_destroy(dev->prp_page_pool);
1439 return -ENOMEM;
1440 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05001441 return 0;
1442}
1443
1444static void nvme_release_prp_pools(struct nvme_dev *dev)
1445{
1446 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05001447 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05001448}
1449
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001450/* XXX: Use an ida or something to let remove / add work correctly */
1451static void nvme_set_instance(struct nvme_dev *dev)
1452{
1453 static int instance;
1454 dev->instance = instance++;
1455}
1456
1457static void nvme_release_instance(struct nvme_dev *dev)
1458{
1459}
1460
1461static int __devinit nvme_probe(struct pci_dev *pdev,
1462 const struct pci_device_id *id)
1463{
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001464 int bars, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001465 struct nvme_dev *dev;
1466
1467 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1468 if (!dev)
1469 return -ENOMEM;
1470 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
1471 GFP_KERNEL);
1472 if (!dev->entry)
1473 goto free;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001474 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
1475 GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001476 if (!dev->queues)
1477 goto free;
1478
Shane Michael Matthews0ee5a7d2011-02-01 08:49:30 -05001479 if (pci_enable_device_mem(pdev))
1480 goto free;
Matthew Wilcoxf64d3362011-02-01 09:01:59 -05001481 pci_set_master(pdev);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001482 bars = pci_select_bars(pdev, IORESOURCE_MEM);
1483 if (pci_request_selected_regions(pdev, bars, "nvme"))
1484 goto disable;
Shane Michael Matthews0ee5a7d2011-02-01 08:49:30 -05001485
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001486 INIT_LIST_HEAD(&dev->namespaces);
1487 dev->pci_dev = pdev;
1488 pci_set_drvdata(pdev, dev);
Matthew Wilcox29303532011-02-01 16:23:39 -05001489 dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
1490 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001491 nvme_set_instance(dev);
Matthew Wilcox53c95772011-01-20 13:42:34 -05001492 dev->entry[0].vector = pdev->irq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001493
Matthew Wilcox091b6092011-02-10 09:56:01 -05001494 result = nvme_setup_prp_pools(dev);
1495 if (result)
1496 goto disable_msix;
1497
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001498 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1499 if (!dev->bar) {
1500 result = -ENOMEM;
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001501 goto disable_msix;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001502 }
1503
1504 result = nvme_configure_admin_queue(dev);
1505 if (result)
1506 goto unmap;
1507 dev->queue_count++;
1508
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001509 spin_lock(&dev_list_lock);
1510 list_add(&dev->node, &dev_list);
1511 spin_unlock(&dev_list_lock);
1512
Matthew Wilcox740216f2011-02-15 16:28:20 -05001513 result = nvme_dev_add(dev);
1514 if (result)
1515 goto delete;
1516
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001517 return 0;
1518
1519 delete:
Matthew Wilcox740216f2011-02-15 16:28:20 -05001520 spin_lock(&dev_list_lock);
1521 list_del(&dev->node);
1522 spin_unlock(&dev_list_lock);
1523
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001524 nvme_free_queues(dev);
1525 unmap:
1526 iounmap(dev->bar);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001527 disable_msix:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001528 pci_disable_msix(pdev);
1529 nvme_release_instance(dev);
Matthew Wilcox091b6092011-02-10 09:56:01 -05001530 nvme_release_prp_pools(dev);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001531 disable:
Shane Michael Matthews0ee5a7d2011-02-01 08:49:30 -05001532 pci_disable_device(pdev);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001533 pci_release_regions(pdev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001534 free:
1535 kfree(dev->queues);
1536 kfree(dev->entry);
1537 kfree(dev);
1538 return result;
1539}
1540
1541static void __devexit nvme_remove(struct pci_dev *pdev)
1542{
1543 struct nvme_dev *dev = pci_get_drvdata(pdev);
1544 nvme_dev_remove(dev);
1545 pci_disable_msix(pdev);
1546 iounmap(dev->bar);
1547 nvme_release_instance(dev);
Matthew Wilcox091b6092011-02-10 09:56:01 -05001548 nvme_release_prp_pools(dev);
Shane Michael Matthews0ee5a7d2011-02-01 08:49:30 -05001549 pci_disable_device(pdev);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001550 pci_release_regions(pdev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001551 kfree(dev->queues);
1552 kfree(dev->entry);
1553 kfree(dev);
1554}
1555
1556/* These functions are yet to be implemented */
1557#define nvme_error_detected NULL
1558#define nvme_dump_registers NULL
1559#define nvme_link_reset NULL
1560#define nvme_slot_reset NULL
1561#define nvme_error_resume NULL
1562#define nvme_suspend NULL
1563#define nvme_resume NULL
1564
1565static struct pci_error_handlers nvme_err_handler = {
1566 .error_detected = nvme_error_detected,
1567 .mmio_enabled = nvme_dump_registers,
1568 .link_reset = nvme_link_reset,
1569 .slot_reset = nvme_slot_reset,
1570 .resume = nvme_error_resume,
1571};
1572
1573/* Move to pci_ids.h later */
1574#define PCI_CLASS_STORAGE_EXPRESS 0x010802
1575
1576static DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
1577 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
1578 { 0, }
1579};
1580MODULE_DEVICE_TABLE(pci, nvme_id_table);
1581
1582static struct pci_driver nvme_driver = {
1583 .name = "nvme",
1584 .id_table = nvme_id_table,
1585 .probe = nvme_probe,
1586 .remove = __devexit_p(nvme_remove),
1587 .suspend = nvme_suspend,
1588 .resume = nvme_resume,
1589 .err_handler = &nvme_err_handler,
1590};
1591
1592static int __init nvme_init(void)
1593{
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001594 int result = -EBUSY;
1595
1596 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
1597 if (IS_ERR(nvme_thread))
1598 return PTR_ERR(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001599
1600 nvme_major = register_blkdev(nvme_major, "nvme");
1601 if (nvme_major <= 0)
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001602 goto kill_kthread;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001603
1604 result = pci_register_driver(&nvme_driver);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001605 if (result)
1606 goto unregister_blkdev;
1607 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001608
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001609 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001610 unregister_blkdev(nvme_major, "nvme");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001611 kill_kthread:
1612 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001613 return result;
1614}
1615
1616static void __exit nvme_exit(void)
1617{
1618 pci_unregister_driver(&nvme_driver);
1619 unregister_blkdev(nvme_major, "nvme");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001620 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001621}
1622
1623MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
1624MODULE_LICENSE("GPL");
Matthew Wilcoxb57ab0f2011-02-24 16:20:14 -05001625MODULE_VERSION("0.4");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001626module_init(nvme_init);
1627module_exit(nvme_exit);