blob: a8549dff469147f132ef9ccfeadea841261e8c44 [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/**
145 * alloc_cmdid - Allocate a Command ID
146 * @param nvmeq The queue that will be used for this command
147 * @param ctx A pointer that will be passed to the handler
148 * @param handler The ID of the handler to call
149 *
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
185/* If you need more than four handlers, you'll need to change how
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500186 * alloc_cmdid and nvme_process_cq work. Consider using a special
187 * CMD_CTX value instead, if that works for your situation.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500188 */
189enum {
190 sync_completion_id = 0,
191 bio_completion_id,
192};
193
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500194/* Special values must be a multiple of 4, and less than 0x1000 */
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500195#define CMD_CTX_BASE (POISON_POINTER_DELTA + sync_completion_id)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500196#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
197#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
198#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500199#define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500200
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500201static unsigned long free_cmdid(struct nvme_queue *nvmeq, int cmdid)
202{
203 unsigned long data;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500204 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500205
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500206 if (cmdid >= nvmeq->q_depth)
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500207 return CMD_CTX_INVALID;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500208 data = info[cmdid].ctx;
209 info[cmdid].ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500210 clear_bit(cmdid, nvmeq->cmdid_data);
211 wake_up(&nvmeq->sq_full);
212 return data;
213}
214
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500215static void cancel_cmdid_data(struct nvme_queue *nvmeq, int cmdid)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500216{
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500217 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
218 info[cmdid].ctx = CMD_CTX_CANCELLED;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500219}
220
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500221static struct nvme_queue *get_nvmeq(struct nvme_ns *ns)
222{
Matthew Wilcox1b234842011-01-20 13:01:49 -0500223 int qid, cpu = get_cpu();
224 if (cpu < ns->dev->queue_count)
225 qid = cpu + 1;
226 else
227 qid = (cpu % rounddown_pow_of_two(ns->dev->queue_count)) + 1;
228 return ns->dev->queues[qid];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500229}
230
231static void put_nvmeq(struct nvme_queue *nvmeq)
232{
Matthew Wilcox1b234842011-01-20 13:01:49 -0500233 put_cpu();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500234}
235
236/**
237 * nvme_submit_cmd: Copy a command into a queue and ring the doorbell
238 * @nvmeq: The queue to use
239 * @cmd: The command to send
240 *
241 * Safe to use from interrupt context
242 */
243static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
244{
245 unsigned long flags;
246 u16 tail;
247 /* XXX: Need to check tail isn't going to overrun head */
248 spin_lock_irqsave(&nvmeq->q_lock, flags);
249 tail = nvmeq->sq_tail;
250 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500251 if (++tail == nvmeq->q_depth)
252 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500253 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500254 nvmeq->sq_tail = tail;
255 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
256
257 return 0;
258}
259
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500260struct nvme_prps {
261 int npages;
262 dma_addr_t first_dma;
263 __le64 *list[0];
264};
265
Matthew Wilcoxd5677602011-02-10 10:47:55 -0500266static void nvme_free_prps(struct nvme_dev *dev, struct nvme_prps *prps)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500267{
268 const int last_prp = PAGE_SIZE / 8 - 1;
269 int i;
270 dma_addr_t prp_dma;
271
272 if (!prps)
273 return;
274
275 prp_dma = prps->first_dma;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500276
277 if (prps->npages == 0)
278 dma_pool_free(dev->prp_small_pool, prps->list[0], prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500279 for (i = 0; i < prps->npages; i++) {
280 __le64 *prp_list = prps->list[i];
281 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500282 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500283 prp_dma = next_prp_dma;
284 }
285 kfree(prps);
286}
287
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500288struct nvme_bio {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500289 struct bio *bio;
290 int nents;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500291 struct nvme_prps *prps;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500292 struct scatterlist sg[0];
293};
294
295/* XXX: use a mempool */
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500296static struct nvme_bio *alloc_nbio(unsigned nseg, gfp_t gfp)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500297{
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500298 return kzalloc(sizeof(struct nvme_bio) +
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500299 sizeof(struct scatterlist) * nseg, gfp);
300}
301
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500302static void free_nbio(struct nvme_queue *nvmeq, struct nvme_bio *nbio)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500303{
Matthew Wilcoxd5677602011-02-10 10:47:55 -0500304 nvme_free_prps(nvmeq->dev, nbio->prps);
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500305 kfree(nbio);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500306}
307
308static void bio_completion(struct nvme_queue *nvmeq, void *ctx,
309 struct nvme_completion *cqe)
310{
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500311 struct nvme_bio *nbio = ctx;
312 struct bio *bio = nbio->bio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500313 u16 status = le16_to_cpup(&cqe->status) >> 1;
314
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500315 dma_unmap_sg(nvmeq->q_dmadev, nbio->sg, nbio->nents,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500316 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500317 free_nbio(nvmeq, nbio);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500318 if (status)
319 bio_endio(bio, -EIO);
320 if (bio->bi_vcnt > bio->bi_idx) {
321 bio_list_add(&nvmeq->sq_cong, bio);
322 wake_up_process(nvme_thread);
323 } else {
324 bio_endio(bio, 0);
325 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500326}
327
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500328/* length is in bytes */
Matthew Wilcoxd5677602011-02-10 10:47:55 -0500329static struct nvme_prps *nvme_setup_prps(struct nvme_dev *dev,
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500330 struct nvme_common_command *cmd,
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500331 struct scatterlist *sg, int length)
332{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500333 struct dma_pool *pool;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500334 int dma_len = sg_dma_len(sg);
335 u64 dma_addr = sg_dma_address(sg);
336 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500337 __le64 *prp_list;
338 dma_addr_t prp_dma;
339 int nprps, npages, i, prp_page;
340 struct nvme_prps *prps = NULL;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500341
342 cmd->prp1 = cpu_to_le64(dma_addr);
343 length -= (PAGE_SIZE - offset);
344 if (length <= 0)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500345 return prps;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500346
347 dma_len -= (PAGE_SIZE - offset);
348 if (dma_len) {
349 dma_addr += (PAGE_SIZE - offset);
350 } else {
351 sg = sg_next(sg);
352 dma_addr = sg_dma_address(sg);
353 dma_len = sg_dma_len(sg);
354 }
355
356 if (length <= PAGE_SIZE) {
357 cmd->prp2 = cpu_to_le64(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500358 return prps;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500359 }
360
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500361 nprps = DIV_ROUND_UP(length, PAGE_SIZE);
362 npages = DIV_ROUND_UP(8 * nprps, PAGE_SIZE);
363 prps = kmalloc(sizeof(*prps) + sizeof(__le64 *) * npages, GFP_ATOMIC);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500364 prp_page = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500365 if (nprps <= (256 / 8)) {
366 pool = dev->prp_small_pool;
367 prps->npages = 0;
368 } else {
369 pool = dev->prp_page_pool;
370 prps->npages = npages;
371 }
372
373 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500374 prps->list[prp_page++] = prp_list;
375 prps->first_dma = prp_dma;
376 cmd->prp2 = cpu_to_le64(prp_dma);
377 i = 0;
378 for (;;) {
379 if (i == PAGE_SIZE / 8 - 1) {
380 __le64 *old_prp_list = prp_list;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500381 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500382 prps->list[prp_page++] = prp_list;
383 old_prp_list[i] = cpu_to_le64(prp_dma);
384 i = 0;
385 }
386 prp_list[i++] = cpu_to_le64(dma_addr);
387 dma_len -= PAGE_SIZE;
388 dma_addr += PAGE_SIZE;
389 length -= PAGE_SIZE;
390 if (length <= 0)
391 break;
392 if (dma_len > 0)
393 continue;
394 BUG_ON(dma_len < 0);
395 sg = sg_next(sg);
396 dma_addr = sg_dma_address(sg);
397 dma_len = sg_dma_len(sg);
398 }
399
400 return prps;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500401}
402
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500403/* NVMe scatterlists require no holes in the virtual address */
404#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
405 (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
406
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500407static int nvme_map_bio(struct device *dev, struct nvme_bio *nbio,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500408 struct bio *bio, enum dma_data_direction dma_dir, int psegs)
409{
Matthew Wilcox76830842011-02-10 13:55:39 -0500410 struct bio_vec *bvec, *bvprv = NULL;
411 struct scatterlist *sg = NULL;
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500412 int i, old_idx, length = 0, nsegs = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500413
Matthew Wilcox76830842011-02-10 13:55:39 -0500414 sg_init_table(nbio->sg, psegs);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500415 old_idx = bio->bi_idx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500416 bio_for_each_segment(bvec, bio, i) {
Matthew Wilcox76830842011-02-10 13:55:39 -0500417 if (bvprv && BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) {
418 sg->length += bvec->bv_len;
419 } else {
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500420 if (bvprv && BIOVEC_NOT_VIRT_MERGEABLE(bvprv, bvec))
421 break;
Matthew Wilcox76830842011-02-10 13:55:39 -0500422 sg = sg ? sg + 1 : nbio->sg;
423 sg_set_page(sg, bvec->bv_page, bvec->bv_len,
424 bvec->bv_offset);
425 nsegs++;
426 }
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500427 length += bvec->bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500428 bvprv = bvec;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500429 }
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500430 bio->bi_idx = i;
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500431 nbio->nents = nsegs;
Matthew Wilcox76830842011-02-10 13:55:39 -0500432 sg_mark_end(sg);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500433 if (dma_map_sg(dev, nbio->sg, nbio->nents, dma_dir) == 0) {
434 bio->bi_idx = old_idx;
435 return -ENOMEM;
436 }
437 return length;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500438}
439
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500440static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
441 int cmdid)
442{
443 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
444
445 memset(cmnd, 0, sizeof(*cmnd));
446 cmnd->common.opcode = nvme_cmd_flush;
447 cmnd->common.command_id = cmdid;
448 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
449
450 if (++nvmeq->sq_tail == nvmeq->q_depth)
451 nvmeq->sq_tail = 0;
452 writel(nvmeq->sq_tail, nvmeq->q_db);
453
454 return 0;
455}
456
457static int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns)
458{
459 int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH,
460 sync_completion_id, IO_TIMEOUT);
461 if (unlikely(cmdid < 0))
462 return cmdid;
463
464 return nvme_submit_flush(nvmeq, ns, cmdid);
465}
466
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500467static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
468 struct bio *bio)
469{
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500470 struct nvme_command *cmnd;
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500471 struct nvme_bio *nbio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500472 enum dma_data_direction dma_dir;
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500473 int cmdid, length, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500474 u16 control;
475 u32 dsmgmt;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500476 int psegs = bio_phys_segments(ns->queue, bio);
477
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500478 if ((bio->bi_rw & REQ_FLUSH) && psegs) {
479 result = nvme_submit_flush_data(nvmeq, ns);
480 if (result)
481 return result;
482 }
483
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500484 nbio = alloc_nbio(psegs, GFP_ATOMIC);
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500485 if (!nbio)
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500486 goto nomem;
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500487 nbio->bio = bio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500488
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500489 result = -EBUSY;
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500490 cmdid = alloc_cmdid(nvmeq, nbio, bio_completion_id, IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500491 if (unlikely(cmdid < 0))
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500492 goto free_nbio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500493
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500494 if ((bio->bi_rw & REQ_FLUSH) && !psegs)
495 return nvme_submit_flush(nvmeq, ns, cmdid);
496
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500497 control = 0;
498 if (bio->bi_rw & REQ_FUA)
499 control |= NVME_RW_FUA;
500 if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
501 control |= NVME_RW_LR;
502
503 dsmgmt = 0;
504 if (bio->bi_rw & REQ_RAHEAD)
505 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
506
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500507 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500508
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500509 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500510 if (bio_data_dir(bio)) {
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500511 cmnd->rw.opcode = nvme_cmd_write;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500512 dma_dir = DMA_TO_DEVICE;
513 } else {
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500514 cmnd->rw.opcode = nvme_cmd_read;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500515 dma_dir = DMA_FROM_DEVICE;
516 }
517
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500518 result = nvme_map_bio(nvmeq->q_dmadev, nbio, bio, dma_dir, psegs);
519 if (result < 0)
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500520 goto free_nbio;
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500521 length = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500522
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500523 cmnd->rw.command_id = cmdid;
524 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxd5677602011-02-10 10:47:55 -0500525 nbio->prps = nvme_setup_prps(nvmeq->dev, &cmnd->common, nbio->sg,
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500526 length);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500527 cmnd->rw.slba = cpu_to_le64(bio->bi_sector >> (ns->lba_shift - 9));
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500528 cmnd->rw.length = cpu_to_le16((length >> ns->lba_shift) - 1);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500529 cmnd->rw.control = cpu_to_le16(control);
530 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500531
Matthew Wilcoxd8ee9d62011-02-24 08:46:00 -0500532 bio->bi_sector += length >> 9;
533
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500534 if (++nvmeq->sq_tail == nvmeq->q_depth)
535 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500536 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500537
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500538 return 0;
539
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500540 free_nbio:
541 free_nbio(nvmeq, nbio);
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500542 nomem:
543 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500544}
545
546/*
547 * NB: return value of non-zero would mean that we were a stacking driver.
548 * make_request must always succeed.
549 */
550static int nvme_make_request(struct request_queue *q, struct bio *bio)
551{
552 struct nvme_ns *ns = q->queuedata;
553 struct nvme_queue *nvmeq = get_nvmeq(ns);
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500554 int result = -EBUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500555
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500556 spin_lock_irq(&nvmeq->q_lock);
557 if (bio_list_empty(&nvmeq->sq_cong))
558 result = nvme_submit_bio_queue(nvmeq, ns, bio);
559 if (unlikely(result)) {
560 if (bio_list_empty(&nvmeq->sq_cong))
561 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500562 bio_list_add(&nvmeq->sq_cong, bio);
563 }
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500564
565 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500566 put_nvmeq(nvmeq);
567
568 return 0;
569}
570
571struct sync_cmd_info {
572 struct task_struct *task;
573 u32 result;
574 int status;
575};
576
577static void sync_completion(struct nvme_queue *nvmeq, void *ctx,
578 struct nvme_completion *cqe)
579{
580 struct sync_cmd_info *cmdinfo = ctx;
Matthew Wilcoxc4270552011-02-22 14:15:34 -0500581 if (unlikely((unsigned long)cmdinfo == CMD_CTX_CANCELLED))
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500582 return;
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500583 if ((unsigned long)cmdinfo == CMD_CTX_FLUSH)
584 return;
Matthew Wilcoxb36235d2011-02-06 08:49:55 -0500585 if (unlikely((unsigned long)cmdinfo == CMD_CTX_COMPLETED)) {
586 dev_warn(nvmeq->q_dmadev,
587 "completed id %d twice on queue %d\n",
588 cqe->command_id, le16_to_cpup(&cqe->sq_id));
589 return;
590 }
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500591 if (unlikely((unsigned long)cmdinfo == CMD_CTX_INVALID)) {
592 dev_warn(nvmeq->q_dmadev,
593 "invalid id %d completed on queue %d\n",
594 cqe->command_id, le16_to_cpup(&cqe->sq_id));
595 return;
596 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500597 cmdinfo->result = le32_to_cpup(&cqe->result);
598 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
599 wake_up_process(cmdinfo->task);
600}
601
602typedef void (*completion_fn)(struct nvme_queue *, void *,
603 struct nvme_completion *);
604
605static irqreturn_t nvme_process_cq(struct nvme_queue *nvmeq)
606{
Matthew Wilcox82123462011-01-20 13:24:06 -0500607 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500608
609 static const completion_fn completions[4] = {
610 [sync_completion_id] = sync_completion,
611 [bio_completion_id] = bio_completion,
612 };
613
614 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500615 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500616
617 for (;;) {
618 unsigned long data;
619 void *ptr;
620 unsigned char handler;
621 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500622 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500623 break;
624 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
625 if (++head == nvmeq->q_depth) {
626 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500627 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500628 }
629
630 data = free_cmdid(nvmeq, cqe.command_id);
631 handler = data & 3;
632 ptr = (void *)(data & ~3UL);
633 completions[handler](nvmeq, ptr, &cqe);
634 }
635
636 /* If the controller ignores the cq head doorbell and continuously
637 * writes to the queue, it is theoretically possible to wrap around
638 * the queue twice and mistakenly return IRQ_NONE. Linux only
639 * requires that 0.1% of your interrupts are handled, so this isn't
640 * a big problem.
641 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500642 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500643 return IRQ_NONE;
644
645 writel(head, nvmeq->q_db + 1);
646 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500647 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500648
649 return IRQ_HANDLED;
650}
651
652static irqreturn_t nvme_irq(int irq, void *data)
653{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500654 irqreturn_t result;
655 struct nvme_queue *nvmeq = data;
656 spin_lock(&nvmeq->q_lock);
657 result = nvme_process_cq(nvmeq);
658 spin_unlock(&nvmeq->q_lock);
659 return result;
660}
661
662static irqreturn_t nvme_irq_check(int irq, void *data)
663{
664 struct nvme_queue *nvmeq = data;
665 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
666 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
667 return IRQ_NONE;
668 return IRQ_WAKE_THREAD;
669}
670
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500671static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
672{
673 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500674 cancel_cmdid_data(nvmeq, cmdid);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500675 spin_unlock_irq(&nvmeq->q_lock);
676}
677
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500678/*
679 * Returns 0 on success. If the result is negative, it's a Linux error code;
680 * if the result is positive, it's an NVM Express status code
681 */
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500682static int nvme_submit_sync_cmd(struct nvme_queue *nvmeq,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500683 struct nvme_command *cmd, u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500684{
685 int cmdid;
686 struct sync_cmd_info cmdinfo;
687
688 cmdinfo.task = current;
689 cmdinfo.status = -EINTR;
690
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500691 cmdid = alloc_cmdid_killable(nvmeq, &cmdinfo, sync_completion_id,
692 timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500693 if (cmdid < 0)
694 return cmdid;
695 cmd->common.command_id = cmdid;
696
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500697 set_current_state(TASK_KILLABLE);
698 nvme_submit_cmd(nvmeq, cmd);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500699 schedule();
700
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500701 if (cmdinfo.status == -EINTR) {
702 nvme_abort_command(nvmeq, cmdid);
703 return -EINTR;
704 }
705
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500706 if (result)
707 *result = cmdinfo.result;
708
709 return cmdinfo.status;
710}
711
712static int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
713 u32 *result)
714{
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500715 return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500716}
717
718static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
719{
720 int status;
721 struct nvme_command c;
722
723 memset(&c, 0, sizeof(c));
724 c.delete_queue.opcode = opcode;
725 c.delete_queue.qid = cpu_to_le16(id);
726
727 status = nvme_submit_admin_cmd(dev, &c, NULL);
728 if (status)
729 return -EIO;
730 return 0;
731}
732
733static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
734 struct nvme_queue *nvmeq)
735{
736 int status;
737 struct nvme_command c;
738 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
739
740 memset(&c, 0, sizeof(c));
741 c.create_cq.opcode = nvme_admin_create_cq;
742 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
743 c.create_cq.cqid = cpu_to_le16(qid);
744 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
745 c.create_cq.cq_flags = cpu_to_le16(flags);
746 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
747
748 status = nvme_submit_admin_cmd(dev, &c, NULL);
749 if (status)
750 return -EIO;
751 return 0;
752}
753
754static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
755 struct nvme_queue *nvmeq)
756{
757 int status;
758 struct nvme_command c;
759 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
760
761 memset(&c, 0, sizeof(c));
762 c.create_sq.opcode = nvme_admin_create_sq;
763 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
764 c.create_sq.sqid = cpu_to_le16(qid);
765 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
766 c.create_sq.sq_flags = cpu_to_le16(flags);
767 c.create_sq.cqid = cpu_to_le16(qid);
768
769 status = nvme_submit_admin_cmd(dev, &c, NULL);
770 if (status)
771 return -EIO;
772 return 0;
773}
774
775static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
776{
777 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
778}
779
780static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
781{
782 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
783}
784
785static void nvme_free_queue(struct nvme_dev *dev, int qid)
786{
787 struct nvme_queue *nvmeq = dev->queues[qid];
788
789 free_irq(dev->entry[nvmeq->cq_vector].vector, nvmeq);
790
791 /* Don't tell the adapter to delete the admin queue */
792 if (qid) {
793 adapter_delete_sq(dev, qid);
794 adapter_delete_cq(dev, qid);
795 }
796
797 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
798 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
799 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
800 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
801 kfree(nvmeq);
802}
803
804static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
805 int depth, int vector)
806{
807 struct device *dmadev = &dev->pci_dev->dev;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500808 unsigned extra = (depth / 8) + (depth * sizeof(struct nvme_cmd_info));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500809 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
810 if (!nvmeq)
811 return NULL;
812
813 nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
814 &nvmeq->cq_dma_addr, GFP_KERNEL);
815 if (!nvmeq->cqes)
816 goto free_nvmeq;
817 memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
818
819 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
820 &nvmeq->sq_dma_addr, GFP_KERNEL);
821 if (!nvmeq->sq_cmds)
822 goto free_cqdma;
823
824 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500825 nvmeq->dev = dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500826 spin_lock_init(&nvmeq->q_lock);
827 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500828 nvmeq->cq_phase = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500829 init_waitqueue_head(&nvmeq->sq_full);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -0500830 init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500831 bio_list_init(&nvmeq->sq_cong);
832 nvmeq->q_db = &dev->dbs[qid * 2];
833 nvmeq->q_depth = depth;
834 nvmeq->cq_vector = vector;
835
836 return nvmeq;
837
838 free_cqdma:
839 dma_free_coherent(dmadev, CQ_SIZE(nvmeq->q_depth), (void *)nvmeq->cqes,
840 nvmeq->cq_dma_addr);
841 free_nvmeq:
842 kfree(nvmeq);
843 return NULL;
844}
845
Matthew Wilcox30010822011-01-20 09:10:15 -0500846static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
847 const char *name)
848{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500849 if (use_threaded_interrupts)
850 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Matthew Wilcoxec6ce612011-02-06 09:01:00 -0500851 nvme_irq_check, nvme_irq,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500852 IRQF_DISABLED | IRQF_SHARED,
853 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -0500854 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
855 IRQF_DISABLED | IRQF_SHARED, name, nvmeq);
856}
857
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500858static __devinit struct nvme_queue *nvme_create_queue(struct nvme_dev *dev,
859 int qid, int cq_size, int vector)
860{
861 int result;
862 struct nvme_queue *nvmeq = nvme_alloc_queue(dev, qid, cq_size, vector);
863
Matthew Wilcox3f85d502011-02-01 08:39:04 -0500864 if (!nvmeq)
865 return NULL;
866
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500867 result = adapter_alloc_cq(dev, qid, nvmeq);
868 if (result < 0)
869 goto free_nvmeq;
870
871 result = adapter_alloc_sq(dev, qid, nvmeq);
872 if (result < 0)
873 goto release_cq;
874
Matthew Wilcox30010822011-01-20 09:10:15 -0500875 result = queue_request_irq(dev, nvmeq, "nvme");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500876 if (result < 0)
877 goto release_sq;
878
879 return nvmeq;
880
881 release_sq:
882 adapter_delete_sq(dev, qid);
883 release_cq:
884 adapter_delete_cq(dev, qid);
885 free_nvmeq:
886 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
887 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
888 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
889 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
890 kfree(nvmeq);
891 return NULL;
892}
893
894static int __devinit nvme_configure_admin_queue(struct nvme_dev *dev)
895{
896 int result;
897 u32 aqa;
898 struct nvme_queue *nvmeq;
899
900 dev->dbs = ((void __iomem *)dev->bar) + 4096;
901
902 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
Matthew Wilcox3f85d502011-02-01 08:39:04 -0500903 if (!nvmeq)
904 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500905
906 aqa = nvmeq->q_depth - 1;
907 aqa |= aqa << 16;
908
909 dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
910 dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
911 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
912
Shane Michael Matthews5911f202011-02-01 11:31:55 -0500913 writel(0, &dev->bar->cc);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500914 writel(aqa, &dev->bar->aqa);
915 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
916 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
917 writel(dev->ctrl_config, &dev->bar->cc);
918
919 while (!(readl(&dev->bar->csts) & NVME_CSTS_RDY)) {
920 msleep(100);
921 if (fatal_signal_pending(current))
922 return -EINTR;
923 }
924
Matthew Wilcox30010822011-01-20 09:10:15 -0500925 result = queue_request_irq(dev, nvmeq, "nvme admin");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500926 dev->queues[0] = nvmeq;
927 return result;
928}
929
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500930static int nvme_map_user_pages(struct nvme_dev *dev, int write,
931 unsigned long addr, unsigned length,
932 struct scatterlist **sgp)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500933{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500934 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500935 struct scatterlist *sg;
936 struct page **pages;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500937
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500938 if (addr & 3)
939 return -EINVAL;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500940 if (!length)
941 return -EINVAL;
942
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500943 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500944 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
945 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500946
947 err = get_user_pages_fast(addr, count, 1, pages);
948 if (err < count) {
949 count = err;
950 err = -EFAULT;
951 goto put_pages;
952 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500953
954 sg = kcalloc(count, sizeof(*sg), GFP_KERNEL);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500955 sg_init_table(sg, count);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500956 sg_set_page(&sg[0], pages[0], PAGE_SIZE - offset, offset);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500957 length -= (PAGE_SIZE - offset);
958 for (i = 1; i < count; i++) {
959 sg_set_page(&sg[i], pages[i], min_t(int, length, PAGE_SIZE), 0);
960 length -= PAGE_SIZE;
961 }
962
963 err = -ENOMEM;
964 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
965 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500966 if (!nents)
967 goto put_pages;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500968
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500969 kfree(pages);
970 *sgp = sg;
971 return nents;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500972
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500973 put_pages:
974 for (i = 0; i < count; i++)
975 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500976 kfree(pages);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500977 return err;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500978}
979
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500980static void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
981 unsigned long addr, int length,
982 struct scatterlist *sg, int nents)
983{
984 int i, count;
985
986 count = DIV_ROUND_UP(offset_in_page(addr) + length, PAGE_SIZE);
987 dma_unmap_sg(&dev->pci_dev->dev, sg, nents, DMA_FROM_DEVICE);
988
989 for (i = 0; i < count; i++)
990 put_page(sg_page(&sg[i]));
991}
992
993static int nvme_submit_user_admin_command(struct nvme_dev *dev,
994 unsigned long addr, unsigned length,
995 struct nvme_command *cmd)
996{
997 int err, nents;
998 struct scatterlist *sg;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500999 struct nvme_prps *prps;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001000
1001 nents = nvme_map_user_pages(dev, 0, addr, length, &sg);
1002 if (nents < 0)
1003 return nents;
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001004 prps = nvme_setup_prps(dev, &cmd->common, sg, length);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001005 err = nvme_submit_admin_cmd(dev, cmd, NULL);
1006 nvme_unmap_user_pages(dev, 0, addr, length, sg, nents);
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001007 nvme_free_prps(dev, prps);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001008 return err ? -EIO : 0;
1009}
1010
Matthew Wilcoxbd38c552011-01-26 14:34:32 -05001011static int nvme_identify(struct nvme_ns *ns, unsigned long addr, int cns)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001012{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001013 struct nvme_command c;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001014
Matthew Wilcoxbd38c552011-01-26 14:34:32 -05001015 memset(&c, 0, sizeof(c));
1016 c.identify.opcode = nvme_admin_identify;
1017 c.identify.nsid = cns ? 0 : cpu_to_le32(ns->ns_id);
1018 c.identify.cns = cpu_to_le32(cns);
1019
1020 return nvme_submit_user_admin_command(ns->dev, addr, 4096, &c);
1021}
1022
1023static int nvme_get_range_type(struct nvme_ns *ns, unsigned long addr)
1024{
1025 struct nvme_command c;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001026
1027 memset(&c, 0, sizeof(c));
1028 c.features.opcode = nvme_admin_get_features;
1029 c.features.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001030 c.features.fid = cpu_to_le32(NVME_FEAT_LBA_RANGE);
1031
Matthew Wilcoxbd38c552011-01-26 14:34:32 -05001032 return nvme_submit_user_admin_command(ns->dev, addr, 4096, &c);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001033}
1034
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001035static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1036{
1037 struct nvme_dev *dev = ns->dev;
1038 struct nvme_queue *nvmeq;
1039 struct nvme_user_io io;
1040 struct nvme_command c;
1041 unsigned length;
1042 u32 result;
1043 int nents, status;
1044 struct scatterlist *sg;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001045 struct nvme_prps *prps;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001046
1047 if (copy_from_user(&io, uio, sizeof(io)))
1048 return -EFAULT;
1049 length = io.nblocks << io.block_shift;
1050 nents = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length, &sg);
1051 if (nents < 0)
1052 return nents;
1053
1054 memset(&c, 0, sizeof(c));
1055 c.rw.opcode = io.opcode;
1056 c.rw.flags = io.flags;
1057 c.rw.nsid = cpu_to_le32(io.nsid);
1058 c.rw.slba = cpu_to_le64(io.slba);
1059 c.rw.length = cpu_to_le16(io.nblocks - 1);
1060 c.rw.control = cpu_to_le16(io.control);
1061 c.rw.dsmgmt = cpu_to_le16(io.dsmgmt);
1062 c.rw.reftag = cpu_to_le32(io.reftag); /* XXX: endian? */
1063 c.rw.apptag = cpu_to_le16(io.apptag);
1064 c.rw.appmask = cpu_to_le16(io.appmask);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001065 /* XXX: metadata */
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001066 prps = nvme_setup_prps(dev, &c.common, sg, length);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001067
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001068 nvmeq = get_nvmeq(ns);
Matthew Wilcoxb1ad37e2011-02-04 16:14:30 -05001069 /* Since nvme_submit_sync_cmd sleeps, we can't keep preemption
1070 * disabled. We may be preempted at any point, and be rescheduled
1071 * to a different CPU. That will cause cacheline bouncing, but no
1072 * additional races since q_lock already protects against other CPUs.
1073 */
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001074 put_nvmeq(nvmeq);
Matthew Wilcoxe85248e2011-02-06 18:30:16 -05001075 status = nvme_submit_sync_cmd(nvmeq, &c, &result, IO_TIMEOUT);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001076
1077 nvme_unmap_user_pages(dev, io.opcode & 1, io.addr, length, sg, nents);
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001078 nvme_free_prps(dev, prps);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001079 put_user(result, &uio->result);
1080 return status;
1081}
1082
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001083static int nvme_download_firmware(struct nvme_ns *ns,
1084 struct nvme_dlfw __user *udlfw)
1085{
1086 struct nvme_dev *dev = ns->dev;
1087 struct nvme_dlfw dlfw;
1088 struct nvme_command c;
1089 int nents, status;
1090 struct scatterlist *sg;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001091 struct nvme_prps *prps;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001092
1093 if (copy_from_user(&dlfw, udlfw, sizeof(dlfw)))
1094 return -EFAULT;
1095 if (dlfw.length >= (1 << 30))
1096 return -EINVAL;
1097
1098 nents = nvme_map_user_pages(dev, 1, dlfw.addr, dlfw.length * 4, &sg);
1099 if (nents < 0)
1100 return nents;
1101
1102 memset(&c, 0, sizeof(c));
1103 c.dlfw.opcode = nvme_admin_download_fw;
1104 c.dlfw.numd = cpu_to_le32(dlfw.length);
1105 c.dlfw.offset = cpu_to_le32(dlfw.offset);
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001106 prps = nvme_setup_prps(dev, &c.common, sg, dlfw.length * 4);
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001107
1108 status = nvme_submit_admin_cmd(dev, &c, NULL);
1109 nvme_unmap_user_pages(dev, 0, dlfw.addr, dlfw.length * 4, sg, nents);
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001110 nvme_free_prps(dev, prps);
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001111 return status;
1112}
1113
1114static int nvme_activate_firmware(struct nvme_ns *ns, unsigned long arg)
1115{
1116 struct nvme_dev *dev = ns->dev;
1117 struct nvme_command c;
1118
1119 memset(&c, 0, sizeof(c));
1120 c.common.opcode = nvme_admin_activate_fw;
1121 c.common.rsvd10[0] = cpu_to_le32(arg);
1122
1123 return nvme_submit_admin_cmd(dev, &c, NULL);
1124}
1125
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001126static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1127 unsigned long arg)
1128{
1129 struct nvme_ns *ns = bdev->bd_disk->private_data;
1130
1131 switch (cmd) {
1132 case NVME_IOCTL_IDENTIFY_NS:
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001133 return nvme_identify(ns, arg, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001134 case NVME_IOCTL_IDENTIFY_CTRL:
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001135 return nvme_identify(ns, arg, 1);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001136 case NVME_IOCTL_GET_RANGE_TYPE:
Matthew Wilcoxbd38c552011-01-26 14:34:32 -05001137 return nvme_get_range_type(ns, arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001138 case NVME_IOCTL_SUBMIT_IO:
1139 return nvme_submit_io(ns, (void __user *)arg);
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001140 case NVME_IOCTL_DOWNLOAD_FW:
1141 return nvme_download_firmware(ns, (void __user *)arg);
1142 case NVME_IOCTL_ACTIVATE_FW:
1143 return nvme_activate_firmware(ns, arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001144 default:
1145 return -ENOTTY;
1146 }
1147}
1148
1149static const struct block_device_operations nvme_fops = {
1150 .owner = THIS_MODULE,
1151 .ioctl = nvme_ioctl,
1152};
1153
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001154static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1155{
1156 while (bio_list_peek(&nvmeq->sq_cong)) {
1157 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1158 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
1159 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
1160 bio_list_add_head(&nvmeq->sq_cong, bio);
1161 break;
1162 }
1163 }
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
1314 return 0;
1315}
1316
1317static void nvme_free_queues(struct nvme_dev *dev)
1318{
1319 int i;
1320
1321 for (i = dev->queue_count - 1; i >= 0; i--)
1322 nvme_free_queue(dev, i);
1323}
1324
1325static int __devinit nvme_dev_add(struct nvme_dev *dev)
1326{
1327 int res, nn, i;
1328 struct nvme_ns *ns, *next;
Matthew Wilcox51814232011-02-01 16:18:08 -05001329 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001330 void *id;
1331 dma_addr_t dma_addr;
1332 struct nvme_command cid, crt;
1333
1334 res = nvme_setup_io_queues(dev);
1335 if (res)
1336 return res;
1337
1338 /* XXX: Switch to a SG list once prp2 works */
1339 id = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr,
1340 GFP_KERNEL);
1341
1342 memset(&cid, 0, sizeof(cid));
1343 cid.identify.opcode = nvme_admin_identify;
1344 cid.identify.nsid = 0;
1345 cid.identify.prp1 = cpu_to_le64(dma_addr);
1346 cid.identify.cns = cpu_to_le32(1);
1347
1348 res = nvme_submit_admin_cmd(dev, &cid, NULL);
1349 if (res) {
1350 res = -EIO;
1351 goto out_free;
1352 }
1353
Matthew Wilcox51814232011-02-01 16:18:08 -05001354 ctrl = id;
1355 nn = le32_to_cpup(&ctrl->nn);
1356 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
1357 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
1358 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001359
1360 cid.identify.cns = 0;
1361 memset(&crt, 0, sizeof(crt));
1362 crt.features.opcode = nvme_admin_get_features;
1363 crt.features.prp1 = cpu_to_le64(dma_addr + 4096);
1364 crt.features.fid = cpu_to_le32(NVME_FEAT_LBA_RANGE);
1365
1366 for (i = 0; i < nn; i++) {
1367 cid.identify.nsid = cpu_to_le32(i);
1368 res = nvme_submit_admin_cmd(dev, &cid, NULL);
1369 if (res)
1370 continue;
1371
1372 if (((struct nvme_id_ns *)id)->ncap == 0)
1373 continue;
1374
1375 crt.features.nsid = cpu_to_le32(i);
1376 res = nvme_submit_admin_cmd(dev, &crt, NULL);
1377 if (res)
1378 continue;
1379
1380 ns = nvme_alloc_ns(dev, i, id, id + 4096);
1381 if (ns)
1382 list_add_tail(&ns->list, &dev->namespaces);
1383 }
1384 list_for_each_entry(ns, &dev->namespaces, list)
1385 add_disk(ns->disk);
1386
1387 dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr);
1388 return 0;
1389
1390 out_free:
1391 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
1392 list_del(&ns->list);
1393 nvme_ns_free(ns);
1394 }
1395
1396 dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr);
1397 return res;
1398}
1399
1400static int nvme_dev_remove(struct nvme_dev *dev)
1401{
1402 struct nvme_ns *ns, *next;
1403
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001404 spin_lock(&dev_list_lock);
1405 list_del(&dev->node);
1406 spin_unlock(&dev_list_lock);
1407
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001408 /* TODO: wait all I/O finished or cancel them */
1409
1410 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
1411 list_del(&ns->list);
1412 del_gendisk(ns->disk);
1413 nvme_ns_free(ns);
1414 }
1415
1416 nvme_free_queues(dev);
1417
1418 return 0;
1419}
1420
Matthew Wilcox091b6092011-02-10 09:56:01 -05001421static int nvme_setup_prp_pools(struct nvme_dev *dev)
1422{
1423 struct device *dmadev = &dev->pci_dev->dev;
1424 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
1425 PAGE_SIZE, PAGE_SIZE, 0);
1426 if (!dev->prp_page_pool)
1427 return -ENOMEM;
1428
Matthew Wilcox99802a72011-02-10 10:30:34 -05001429 /* Optimisation for I/Os between 4k and 128k */
1430 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
1431 256, 256, 0);
1432 if (!dev->prp_small_pool) {
1433 dma_pool_destroy(dev->prp_page_pool);
1434 return -ENOMEM;
1435 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05001436 return 0;
1437}
1438
1439static void nvme_release_prp_pools(struct nvme_dev *dev)
1440{
1441 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05001442 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05001443}
1444
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001445/* XXX: Use an ida or something to let remove / add work correctly */
1446static void nvme_set_instance(struct nvme_dev *dev)
1447{
1448 static int instance;
1449 dev->instance = instance++;
1450}
1451
1452static void nvme_release_instance(struct nvme_dev *dev)
1453{
1454}
1455
1456static int __devinit nvme_probe(struct pci_dev *pdev,
1457 const struct pci_device_id *id)
1458{
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001459 int bars, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001460 struct nvme_dev *dev;
1461
1462 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1463 if (!dev)
1464 return -ENOMEM;
1465 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
1466 GFP_KERNEL);
1467 if (!dev->entry)
1468 goto free;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001469 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
1470 GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001471 if (!dev->queues)
1472 goto free;
1473
Shane Michael Matthews0ee5a7d2011-02-01 08:49:30 -05001474 if (pci_enable_device_mem(pdev))
1475 goto free;
Matthew Wilcoxf64d3362011-02-01 09:01:59 -05001476 pci_set_master(pdev);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001477 bars = pci_select_bars(pdev, IORESOURCE_MEM);
1478 if (pci_request_selected_regions(pdev, bars, "nvme"))
1479 goto disable;
Shane Michael Matthews0ee5a7d2011-02-01 08:49:30 -05001480
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001481 INIT_LIST_HEAD(&dev->namespaces);
1482 dev->pci_dev = pdev;
1483 pci_set_drvdata(pdev, dev);
Matthew Wilcox29303532011-02-01 16:23:39 -05001484 dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
1485 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001486 nvme_set_instance(dev);
Matthew Wilcox53c95772011-01-20 13:42:34 -05001487 dev->entry[0].vector = pdev->irq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001488
Matthew Wilcox091b6092011-02-10 09:56:01 -05001489 result = nvme_setup_prp_pools(dev);
1490 if (result)
1491 goto disable_msix;
1492
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001493 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1494 if (!dev->bar) {
1495 result = -ENOMEM;
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001496 goto disable_msix;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001497 }
1498
1499 result = nvme_configure_admin_queue(dev);
1500 if (result)
1501 goto unmap;
1502 dev->queue_count++;
1503
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001504 spin_lock(&dev_list_lock);
1505 list_add(&dev->node, &dev_list);
1506 spin_unlock(&dev_list_lock);
1507
Matthew Wilcox740216f2011-02-15 16:28:20 -05001508 result = nvme_dev_add(dev);
1509 if (result)
1510 goto delete;
1511
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001512 return 0;
1513
1514 delete:
Matthew Wilcox740216f2011-02-15 16:28:20 -05001515 spin_lock(&dev_list_lock);
1516 list_del(&dev->node);
1517 spin_unlock(&dev_list_lock);
1518
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001519 nvme_free_queues(dev);
1520 unmap:
1521 iounmap(dev->bar);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001522 disable_msix:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001523 pci_disable_msix(pdev);
1524 nvme_release_instance(dev);
Matthew Wilcox091b6092011-02-10 09:56:01 -05001525 nvme_release_prp_pools(dev);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001526 disable:
Shane Michael Matthews0ee5a7d2011-02-01 08:49:30 -05001527 pci_disable_device(pdev);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001528 pci_release_regions(pdev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001529 free:
1530 kfree(dev->queues);
1531 kfree(dev->entry);
1532 kfree(dev);
1533 return result;
1534}
1535
1536static void __devexit nvme_remove(struct pci_dev *pdev)
1537{
1538 struct nvme_dev *dev = pci_get_drvdata(pdev);
1539 nvme_dev_remove(dev);
1540 pci_disable_msix(pdev);
1541 iounmap(dev->bar);
1542 nvme_release_instance(dev);
Matthew Wilcox091b6092011-02-10 09:56:01 -05001543 nvme_release_prp_pools(dev);
Shane Michael Matthews0ee5a7d2011-02-01 08:49:30 -05001544 pci_disable_device(pdev);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001545 pci_release_regions(pdev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001546 kfree(dev->queues);
1547 kfree(dev->entry);
1548 kfree(dev);
1549}
1550
1551/* These functions are yet to be implemented */
1552#define nvme_error_detected NULL
1553#define nvme_dump_registers NULL
1554#define nvme_link_reset NULL
1555#define nvme_slot_reset NULL
1556#define nvme_error_resume NULL
1557#define nvme_suspend NULL
1558#define nvme_resume NULL
1559
1560static struct pci_error_handlers nvme_err_handler = {
1561 .error_detected = nvme_error_detected,
1562 .mmio_enabled = nvme_dump_registers,
1563 .link_reset = nvme_link_reset,
1564 .slot_reset = nvme_slot_reset,
1565 .resume = nvme_error_resume,
1566};
1567
1568/* Move to pci_ids.h later */
1569#define PCI_CLASS_STORAGE_EXPRESS 0x010802
1570
1571static DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
1572 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
1573 { 0, }
1574};
1575MODULE_DEVICE_TABLE(pci, nvme_id_table);
1576
1577static struct pci_driver nvme_driver = {
1578 .name = "nvme",
1579 .id_table = nvme_id_table,
1580 .probe = nvme_probe,
1581 .remove = __devexit_p(nvme_remove),
1582 .suspend = nvme_suspend,
1583 .resume = nvme_resume,
1584 .err_handler = &nvme_err_handler,
1585};
1586
1587static int __init nvme_init(void)
1588{
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001589 int result = -EBUSY;
1590
1591 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
1592 if (IS_ERR(nvme_thread))
1593 return PTR_ERR(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001594
1595 nvme_major = register_blkdev(nvme_major, "nvme");
1596 if (nvme_major <= 0)
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001597 goto kill_kthread;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001598
1599 result = pci_register_driver(&nvme_driver);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001600 if (result)
1601 goto unregister_blkdev;
1602 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001603
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001604 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001605 unregister_blkdev(nvme_major, "nvme");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001606 kill_kthread:
1607 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001608 return result;
1609}
1610
1611static void __exit nvme_exit(void)
1612{
1613 pci_unregister_driver(&nvme_driver);
1614 unregister_blkdev(nvme_major, "nvme");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001615 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001616}
1617
1618MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
1619MODULE_LICENSE("GPL");
Matthew Wilcoxb57ab0f2011-02-24 16:20:14 -05001620MODULE_VERSION("0.4");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001621module_init(nvme_init);
1622module_exit(nvme_exit);