blob: 9c0ab2af0fae4f650d1b1ad8c11b2164b10858d0 [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>
Matthew Wilcox8de05532011-05-12 13:50:28 -040021#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050022#include <linux/blkdev.h>
23#include <linux/errno.h>
24#include <linux/fs.h>
25#include <linux/genhd.h>
26#include <linux/init.h>
27#include <linux/interrupt.h>
28#include <linux/io.h>
29#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050030#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050031#include <linux/kernel.h>
32#include <linux/mm.h>
33#include <linux/module.h>
34#include <linux/moduleparam.h>
35#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050036#include <linux/poison.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050037#include <linux/sched.h>
38#include <linux/slab.h>
39#include <linux/types.h>
40#include <linux/version.h>
41
42#define NVME_Q_DEPTH 1024
43#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
44#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
45#define NVME_MINORS 64
Matthew Wilcoxe85248e2011-02-06 18:30:16 -050046#define IO_TIMEOUT (5 * HZ)
47#define ADMIN_TIMEOUT (60 * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050048
49static int nvme_major;
50module_param(nvme_major, int, 0);
51
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050052static int use_threaded_interrupts;
53module_param(use_threaded_interrupts, int, 0);
54
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050055static DEFINE_SPINLOCK(dev_list_lock);
56static LIST_HEAD(dev_list);
57static struct task_struct *nvme_thread;
58
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050059/*
60 * Represents an NVM Express device. Each nvme_dev is a PCI function.
61 */
62struct nvme_dev {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050063 struct list_head node;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050064 struct nvme_queue **queues;
65 u32 __iomem *dbs;
66 struct pci_dev *pci_dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -050067 struct dma_pool *prp_page_pool;
Matthew Wilcox99802a72011-02-10 10:30:34 -050068 struct dma_pool *prp_small_pool;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050069 int instance;
70 int queue_count;
71 u32 ctrl_config;
72 struct msix_entry *entry;
73 struct nvme_bar __iomem *bar;
74 struct list_head namespaces;
Matthew Wilcox51814232011-02-01 16:18:08 -050075 char serial[20];
76 char model[40];
77 char firmware_rev[8];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050078};
79
80/*
81 * An NVM Express namespace is equivalent to a SCSI LUN
82 */
83struct nvme_ns {
84 struct list_head list;
85
86 struct nvme_dev *dev;
87 struct request_queue *queue;
88 struct gendisk *disk;
89
90 int ns_id;
91 int lba_shift;
92};
93
94/*
95 * An NVM Express queue. Each device has at least two (one for admin
96 * commands and one for I/O commands).
97 */
98struct nvme_queue {
99 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500100 struct nvme_dev *dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500101 spinlock_t q_lock;
102 struct nvme_command *sq_cmds;
103 volatile struct nvme_completion *cqes;
104 dma_addr_t sq_dma_addr;
105 dma_addr_t cq_dma_addr;
106 wait_queue_head_t sq_full;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -0500107 wait_queue_t sq_cong_wait;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500108 struct bio_list sq_cong;
109 u32 __iomem *q_db;
110 u16 q_depth;
111 u16 cq_vector;
112 u16 sq_head;
113 u16 sq_tail;
114 u16 cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500115 u16 cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500116 unsigned long cmdid_data[];
117};
118
119/*
120 * Check we didin't inadvertently grow the command struct
121 */
122static inline void _nvme_check_size(void)
123{
124 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
125 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
126 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
127 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
128 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
129 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
130 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
131 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
132 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
133}
134
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500135struct nvme_cmd_info {
136 unsigned long ctx;
137 unsigned long timeout;
138};
139
140static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
141{
142 return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
143}
144
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500145/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400146 * alloc_cmdid() - Allocate a Command ID
147 * @nvmeq: The queue that will be used for this command
148 * @ctx: A pointer that will be passed to the handler
149 * @handler: The ID of the handler to call
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500150 *
151 * Allocate a Command ID for a queue. The data passed in will
152 * be passed to the completion handler. This is implemented by using
153 * the bottom two bits of the ctx pointer to store the handler ID.
154 * Passing in a pointer that's not 4-byte aligned will cause a BUG.
155 * We can change this if it becomes a problem.
156 */
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500157static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx, int handler,
158 unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500159{
Matthew Wilcoxe6d15f72011-02-24 08:49:41 -0500160 int depth = nvmeq->q_depth - 1;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500161 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500162 int cmdid;
163
164 BUG_ON((unsigned long)ctx & 3);
165
166 do {
167 cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
168 if (cmdid >= depth)
169 return -EBUSY;
170 } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
171
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500172 info[cmdid].ctx = (unsigned long)ctx | handler;
173 info[cmdid].timeout = jiffies + timeout;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500174 return cmdid;
175}
176
177static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500178 int handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500179{
180 int cmdid;
181 wait_event_killable(nvmeq->sq_full,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500182 (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500183 return (cmdid < 0) ? -EINTR : cmdid;
184}
185
Matthew Wilcoxfa922822011-03-16 16:29:00 -0400186/*
187 * If you need more than four handlers, you'll need to change how
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500188 * alloc_cmdid and nvme_process_cq work. Consider using a special
189 * CMD_CTX value instead, if that works for your situation.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500190 */
191enum {
192 sync_completion_id = 0,
193 bio_completion_id,
194};
195
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500196/* Special values must be a multiple of 4, and less than 0x1000 */
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500197#define CMD_CTX_BASE (POISON_POINTER_DELTA + sync_completion_id)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500198#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
199#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
200#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500201#define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500202
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500203static unsigned long free_cmdid(struct nvme_queue *nvmeq, int cmdid)
204{
205 unsigned long data;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500206 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500207
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500208 if (cmdid >= nvmeq->q_depth)
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500209 return CMD_CTX_INVALID;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500210 data = info[cmdid].ctx;
211 info[cmdid].ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500212 clear_bit(cmdid, nvmeq->cmdid_data);
213 wake_up(&nvmeq->sq_full);
214 return data;
215}
216
Matthew Wilcox21075bd2011-04-28 23:17:36 -0700217static unsigned long cancel_cmdid(struct nvme_queue *nvmeq, int cmdid)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500218{
Matthew Wilcox21075bd2011-04-28 23:17:36 -0700219 unsigned long data;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500220 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcox21075bd2011-04-28 23:17:36 -0700221 data = info[cmdid].ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500222 info[cmdid].ctx = CMD_CTX_CANCELLED;
Matthew Wilcox21075bd2011-04-28 23:17:36 -0700223 return data;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500224}
225
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500226static struct nvme_queue *get_nvmeq(struct nvme_ns *ns)
227{
Matthew Wilcox9ecdc942011-03-16 16:52:19 -0400228 return ns->dev->queues[get_cpu() + 1];
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/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400237 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500238 * @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;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500247 spin_lock_irqsave(&nvmeq->q_lock, flags);
248 tail = nvmeq->sq_tail;
249 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500250 if (++tail == nvmeq->q_depth)
251 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500252 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500253 nvmeq->sq_tail = tail;
254 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
255
256 return 0;
257}
258
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500259struct nvme_prps {
260 int npages;
261 dma_addr_t first_dma;
262 __le64 *list[0];
263};
264
Matthew Wilcoxd5677602011-02-10 10:47:55 -0500265static void nvme_free_prps(struct nvme_dev *dev, struct nvme_prps *prps)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500266{
267 const int last_prp = PAGE_SIZE / 8 - 1;
268 int i;
269 dma_addr_t prp_dma;
270
271 if (!prps)
272 return;
273
274 prp_dma = prps->first_dma;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500275
276 if (prps->npages == 0)
277 dma_pool_free(dev->prp_small_pool, prps->list[0], prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500278 for (i = 0; i < prps->npages; i++) {
279 __le64 *prp_list = prps->list[i];
280 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500281 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500282 prp_dma = next_prp_dma;
283 }
284 kfree(prps);
285}
286
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500287struct nvme_bio {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500288 struct bio *bio;
289 int nents;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500290 struct nvme_prps *prps;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500291 struct scatterlist sg[0];
292};
293
294/* XXX: use a mempool */
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500295static struct nvme_bio *alloc_nbio(unsigned nseg, gfp_t gfp)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500296{
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500297 return kzalloc(sizeof(struct nvme_bio) +
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500298 sizeof(struct scatterlist) * nseg, gfp);
299}
300
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500301static void free_nbio(struct nvme_queue *nvmeq, struct nvme_bio *nbio)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500302{
Matthew Wilcoxd5677602011-02-10 10:47:55 -0500303 nvme_free_prps(nvmeq->dev, nbio->prps);
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500304 kfree(nbio);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500305}
306
307static void bio_completion(struct nvme_queue *nvmeq, void *ctx,
308 struct nvme_completion *cqe)
309{
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500310 struct nvme_bio *nbio = ctx;
311 struct bio *bio = nbio->bio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500312 u16 status = le16_to_cpup(&cqe->status) >> 1;
313
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500314 dma_unmap_sg(nvmeq->q_dmadev, nbio->sg, nbio->nents,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500315 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcoxd534df32011-02-10 09:03:06 -0500316 free_nbio(nvmeq, nbio);
Matthew Wilcox09a58f52011-04-28 23:09:09 -0700317 if (status) {
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500318 bio_endio(bio, -EIO);
Matthew Wilcox09a58f52011-04-28 23:09:09 -0700319 } else if (bio->bi_vcnt > bio->bi_idx) {
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500320 bio_list_add(&nvmeq->sq_cong, bio);
321 wake_up_process(nvme_thread);
322 } else {
323 bio_endio(bio, 0);
324 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500325}
326
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500327/* length is in bytes */
Matthew Wilcoxd5677602011-02-10 10:47:55 -0500328static struct nvme_prps *nvme_setup_prps(struct nvme_dev *dev,
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500329 struct nvme_common_command *cmd,
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500330 struct scatterlist *sg, int length)
331{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500332 struct dma_pool *pool;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500333 int dma_len = sg_dma_len(sg);
334 u64 dma_addr = sg_dma_address(sg);
335 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500336 __le64 *prp_list;
337 dma_addr_t prp_dma;
338 int nprps, npages, i, prp_page;
339 struct nvme_prps *prps = NULL;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500340
341 cmd->prp1 = cpu_to_le64(dma_addr);
342 length -= (PAGE_SIZE - offset);
343 if (length <= 0)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500344 return prps;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500345
346 dma_len -= (PAGE_SIZE - offset);
347 if (dma_len) {
348 dma_addr += (PAGE_SIZE - offset);
349 } else {
350 sg = sg_next(sg);
351 dma_addr = sg_dma_address(sg);
352 dma_len = sg_dma_len(sg);
353 }
354
355 if (length <= PAGE_SIZE) {
356 cmd->prp2 = cpu_to_le64(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500357 return prps;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500358 }
359
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500360 nprps = DIV_ROUND_UP(length, PAGE_SIZE);
361 npages = DIV_ROUND_UP(8 * nprps, PAGE_SIZE);
362 prps = kmalloc(sizeof(*prps) + sizeof(__le64 *) * npages, GFP_ATOMIC);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500363 prp_page = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500364 if (nprps <= (256 / 8)) {
365 pool = dev->prp_small_pool;
366 prps->npages = 0;
367 } else {
368 pool = dev->prp_page_pool;
369 prps->npages = npages;
370 }
371
372 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500373 prps->list[prp_page++] = prp_list;
374 prps->first_dma = prp_dma;
375 cmd->prp2 = cpu_to_le64(prp_dma);
376 i = 0;
377 for (;;) {
Matthew Wilcox7523d832011-03-16 16:43:40 -0400378 if (i == PAGE_SIZE / 8) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500379 __le64 *old_prp_list = prp_list;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500380 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500381 prps->list[prp_page++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400382 prp_list[0] = old_prp_list[i - 1];
383 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
384 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500385 }
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
Matthew Wilcox8de05532011-05-12 13:50:28 -0400605static const completion_fn nvme_completions[4] = {
606 [sync_completion_id] = sync_completion,
607 [bio_completion_id] = bio_completion,
608};
609
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500610static irqreturn_t nvme_process_cq(struct nvme_queue *nvmeq)
611{
Matthew Wilcox82123462011-01-20 13:24:06 -0500612 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500613
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500614 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);
Matthew Wilcox8de05532011-05-12 13:50:28 -0400633 nvme_completions[handler](nvmeq, ptr, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500634 }
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 Wilcox21075bd2011-04-28 23:17:36 -0700674 cancel_cmdid(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];
Matthew Wilcoxaba20802011-03-27 08:52:06 -0400788 int vector = dev->entry[nvmeq->cq_vector].vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500789
Matthew Wilcoxaba20802011-03-27 08:52:06 -0400790 irq_set_affinity_hint(vector, NULL);
791 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500792
793 /* Don't tell the adapter to delete the admin queue */
794 if (qid) {
795 adapter_delete_sq(dev, qid);
796 adapter_delete_cq(dev, qid);
797 }
798
799 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
800 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
801 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
802 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
803 kfree(nvmeq);
804}
805
806static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
807 int depth, int vector)
808{
809 struct device *dmadev = &dev->pci_dev->dev;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500810 unsigned extra = (depth / 8) + (depth * sizeof(struct nvme_cmd_info));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500811 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
812 if (!nvmeq)
813 return NULL;
814
815 nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
816 &nvmeq->cq_dma_addr, GFP_KERNEL);
817 if (!nvmeq->cqes)
818 goto free_nvmeq;
819 memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
820
821 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
822 &nvmeq->sq_dma_addr, GFP_KERNEL);
823 if (!nvmeq->sq_cmds)
824 goto free_cqdma;
825
826 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500827 nvmeq->dev = dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500828 spin_lock_init(&nvmeq->q_lock);
829 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500830 nvmeq->cq_phase = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500831 init_waitqueue_head(&nvmeq->sq_full);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -0500832 init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500833 bio_list_init(&nvmeq->sq_cong);
834 nvmeq->q_db = &dev->dbs[qid * 2];
835 nvmeq->q_depth = depth;
836 nvmeq->cq_vector = vector;
837
838 return nvmeq;
839
840 free_cqdma:
841 dma_free_coherent(dmadev, CQ_SIZE(nvmeq->q_depth), (void *)nvmeq->cqes,
842 nvmeq->cq_dma_addr);
843 free_nvmeq:
844 kfree(nvmeq);
845 return NULL;
846}
847
Matthew Wilcox30010822011-01-20 09:10:15 -0500848static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
849 const char *name)
850{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500851 if (use_threaded_interrupts)
852 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Matthew Wilcoxec6ce612011-02-06 09:01:00 -0500853 nvme_irq_check, nvme_irq,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500854 IRQF_DISABLED | IRQF_SHARED,
855 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -0500856 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
857 IRQF_DISABLED | IRQF_SHARED, name, nvmeq);
858}
859
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500860static __devinit struct nvme_queue *nvme_create_queue(struct nvme_dev *dev,
861 int qid, int cq_size, int vector)
862{
863 int result;
864 struct nvme_queue *nvmeq = nvme_alloc_queue(dev, qid, cq_size, vector);
865
Matthew Wilcox3f85d502011-02-01 08:39:04 -0500866 if (!nvmeq)
867 return NULL;
868
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500869 result = adapter_alloc_cq(dev, qid, nvmeq);
870 if (result < 0)
871 goto free_nvmeq;
872
873 result = adapter_alloc_sq(dev, qid, nvmeq);
874 if (result < 0)
875 goto release_cq;
876
Matthew Wilcox30010822011-01-20 09:10:15 -0500877 result = queue_request_irq(dev, nvmeq, "nvme");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500878 if (result < 0)
879 goto release_sq;
880
881 return nvmeq;
882
883 release_sq:
884 adapter_delete_sq(dev, qid);
885 release_cq:
886 adapter_delete_cq(dev, qid);
887 free_nvmeq:
888 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
889 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
890 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
891 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
892 kfree(nvmeq);
893 return NULL;
894}
895
896static int __devinit nvme_configure_admin_queue(struct nvme_dev *dev)
897{
898 int result;
899 u32 aqa;
Matthew Wilcox22605f92011-04-19 15:04:20 -0400900 u64 cap;
901 unsigned long timeout;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500902 struct nvme_queue *nvmeq;
903
904 dev->dbs = ((void __iomem *)dev->bar) + 4096;
905
906 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
Matthew Wilcox3f85d502011-02-01 08:39:04 -0500907 if (!nvmeq)
908 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500909
910 aqa = nvmeq->q_depth - 1;
911 aqa |= aqa << 16;
912
913 dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
914 dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
915 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -0400916 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500917
Shane Michael Matthews5911f202011-02-01 11:31:55 -0500918 writel(0, &dev->bar->cc);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500919 writel(aqa, &dev->bar->aqa);
920 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
921 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
922 writel(dev->ctrl_config, &dev->bar->cc);
923
Matthew Wilcox22605f92011-04-19 15:04:20 -0400924 cap = readq(&dev->bar->cap);
925 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
926
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500927 while (!(readl(&dev->bar->csts) & NVME_CSTS_RDY)) {
928 msleep(100);
929 if (fatal_signal_pending(current))
930 return -EINTR;
Matthew Wilcox22605f92011-04-19 15:04:20 -0400931 if (time_after(jiffies, timeout)) {
932 dev_err(&dev->pci_dev->dev,
933 "Device not ready; aborting initialisation\n");
934 return -ENODEV;
935 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500936 }
937
Matthew Wilcox30010822011-01-20 09:10:15 -0500938 result = queue_request_irq(dev, nvmeq, "nvme admin");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500939 dev->queues[0] = nvmeq;
940 return result;
941}
942
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500943static int nvme_map_user_pages(struct nvme_dev *dev, int write,
944 unsigned long addr, unsigned length,
945 struct scatterlist **sgp)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500946{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500947 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500948 struct scatterlist *sg;
949 struct page **pages;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500950
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500951 if (addr & 3)
952 return -EINVAL;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500953 if (!length)
954 return -EINVAL;
955
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500956 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500957 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
958 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500959
960 err = get_user_pages_fast(addr, count, 1, pages);
961 if (err < count) {
962 count = err;
963 err = -EFAULT;
964 goto put_pages;
965 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500966
967 sg = kcalloc(count, sizeof(*sg), GFP_KERNEL);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500968 sg_init_table(sg, count);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500969 sg_set_page(&sg[0], pages[0], PAGE_SIZE - offset, offset);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500970 length -= (PAGE_SIZE - offset);
971 for (i = 1; i < count; i++) {
972 sg_set_page(&sg[i], pages[i], min_t(int, length, PAGE_SIZE), 0);
973 length -= PAGE_SIZE;
974 }
975
976 err = -ENOMEM;
977 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
978 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500979 if (!nents)
980 goto put_pages;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500981
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500982 kfree(pages);
983 *sgp = sg;
984 return nents;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500985
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500986 put_pages:
987 for (i = 0; i < count; i++)
988 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500989 kfree(pages);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -0500990 return err;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500991}
992
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -0500993static void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
994 unsigned long addr, int length,
995 struct scatterlist *sg, int nents)
996{
997 int i, count;
998
999 count = DIV_ROUND_UP(offset_in_page(addr) + length, PAGE_SIZE);
1000 dma_unmap_sg(&dev->pci_dev->dev, sg, nents, DMA_FROM_DEVICE);
1001
1002 for (i = 0; i < count; i++)
1003 put_page(sg_page(&sg[i]));
1004}
1005
1006static int nvme_submit_user_admin_command(struct nvme_dev *dev,
1007 unsigned long addr, unsigned length,
1008 struct nvme_command *cmd)
1009{
1010 int err, nents;
1011 struct scatterlist *sg;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001012 struct nvme_prps *prps;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001013
1014 nents = nvme_map_user_pages(dev, 0, addr, length, &sg);
1015 if (nents < 0)
1016 return nents;
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001017 prps = nvme_setup_prps(dev, &cmd->common, sg, length);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001018 err = nvme_submit_admin_cmd(dev, cmd, NULL);
1019 nvme_unmap_user_pages(dev, 0, addr, length, sg, nents);
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001020 nvme_free_prps(dev, prps);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001021 return err ? -EIO : 0;
1022}
1023
Matthew Wilcoxbd38c552011-01-26 14:34:32 -05001024static int nvme_identify(struct nvme_ns *ns, unsigned long addr, int cns)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001025{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001026 struct nvme_command c;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001027
Matthew Wilcoxbd38c552011-01-26 14:34:32 -05001028 memset(&c, 0, sizeof(c));
1029 c.identify.opcode = nvme_admin_identify;
1030 c.identify.nsid = cns ? 0 : cpu_to_le32(ns->ns_id);
1031 c.identify.cns = cpu_to_le32(cns);
1032
1033 return nvme_submit_user_admin_command(ns->dev, addr, 4096, &c);
1034}
1035
1036static int nvme_get_range_type(struct nvme_ns *ns, unsigned long addr)
1037{
1038 struct nvme_command c;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001039
1040 memset(&c, 0, sizeof(c));
1041 c.features.opcode = nvme_admin_get_features;
1042 c.features.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001043 c.features.fid = cpu_to_le32(NVME_FEAT_LBA_RANGE);
1044
Matthew Wilcoxbd38c552011-01-26 14:34:32 -05001045 return nvme_submit_user_admin_command(ns->dev, addr, 4096, &c);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001046}
1047
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001048static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1049{
1050 struct nvme_dev *dev = ns->dev;
1051 struct nvme_queue *nvmeq;
1052 struct nvme_user_io io;
1053 struct nvme_command c;
1054 unsigned length;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001055 int nents, status;
1056 struct scatterlist *sg;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001057 struct nvme_prps *prps;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001058
1059 if (copy_from_user(&io, uio, sizeof(io)))
1060 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001061 length = (io.nblocks + 1) << ns->lba_shift;
1062
1063 switch (io.opcode) {
1064 case nvme_cmd_write:
1065 case nvme_cmd_read:
1066 nents = nvme_map_user_pages(dev, io.opcode & 1, io.addr,
1067 length, &sg);
1068 default:
1069 return -EFAULT;
1070 }
1071
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001072 if (nents < 0)
1073 return nents;
1074
1075 memset(&c, 0, sizeof(c));
1076 c.rw.opcode = io.opcode;
1077 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001078 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001079 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001080 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001081 c.rw.control = cpu_to_le16(io.control);
1082 c.rw.dsmgmt = cpu_to_le16(io.dsmgmt);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001083 c.rw.reftag = io.reftag;
1084 c.rw.apptag = io.apptag;
1085 c.rw.appmask = io.appmask;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001086 /* XXX: metadata */
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001087 prps = nvme_setup_prps(dev, &c.common, sg, length);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001088
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001089 nvmeq = get_nvmeq(ns);
Matthew Wilcoxfa922822011-03-16 16:29:00 -04001090 /*
1091 * Since nvme_submit_sync_cmd sleeps, we can't keep preemption
Matthew Wilcoxb1ad37e2011-02-04 16:14:30 -05001092 * disabled. We may be preempted at any point, and be rescheduled
1093 * to a different CPU. That will cause cacheline bouncing, but no
1094 * additional races since q_lock already protects against other CPUs.
1095 */
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001096 put_nvmeq(nvmeq);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001097 status = nvme_submit_sync_cmd(nvmeq, &c, NULL, IO_TIMEOUT);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001098
1099 nvme_unmap_user_pages(dev, io.opcode & 1, io.addr, length, sg, nents);
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001100 nvme_free_prps(dev, prps);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001101 return status;
1102}
1103
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001104static int nvme_download_firmware(struct nvme_ns *ns,
1105 struct nvme_dlfw __user *udlfw)
1106{
1107 struct nvme_dev *dev = ns->dev;
1108 struct nvme_dlfw dlfw;
1109 struct nvme_command c;
1110 int nents, status;
1111 struct scatterlist *sg;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001112 struct nvme_prps *prps;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001113
1114 if (copy_from_user(&dlfw, udlfw, sizeof(dlfw)))
1115 return -EFAULT;
1116 if (dlfw.length >= (1 << 30))
1117 return -EINVAL;
1118
1119 nents = nvme_map_user_pages(dev, 1, dlfw.addr, dlfw.length * 4, &sg);
1120 if (nents < 0)
1121 return nents;
1122
1123 memset(&c, 0, sizeof(c));
1124 c.dlfw.opcode = nvme_admin_download_fw;
1125 c.dlfw.numd = cpu_to_le32(dlfw.length);
1126 c.dlfw.offset = cpu_to_le32(dlfw.offset);
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001127 prps = nvme_setup_prps(dev, &c.common, sg, dlfw.length * 4);
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001128
1129 status = nvme_submit_admin_cmd(dev, &c, NULL);
1130 nvme_unmap_user_pages(dev, 0, dlfw.addr, dlfw.length * 4, sg, nents);
Matthew Wilcoxd5677602011-02-10 10:47:55 -05001131 nvme_free_prps(dev, prps);
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001132 return status;
1133}
1134
1135static int nvme_activate_firmware(struct nvme_ns *ns, unsigned long arg)
1136{
1137 struct nvme_dev *dev = ns->dev;
1138 struct nvme_command c;
1139
1140 memset(&c, 0, sizeof(c));
1141 c.common.opcode = nvme_admin_activate_fw;
1142 c.common.rsvd10[0] = cpu_to_le32(arg);
1143
1144 return nvme_submit_admin_cmd(dev, &c, NULL);
1145}
1146
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001147static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1148 unsigned long arg)
1149{
1150 struct nvme_ns *ns = bdev->bd_disk->private_data;
1151
1152 switch (cmd) {
1153 case NVME_IOCTL_IDENTIFY_NS:
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001154 return nvme_identify(ns, arg, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001155 case NVME_IOCTL_IDENTIFY_CTRL:
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001156 return nvme_identify(ns, arg, 1);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001157 case NVME_IOCTL_GET_RANGE_TYPE:
Matthew Wilcoxbd38c552011-01-26 14:34:32 -05001158 return nvme_get_range_type(ns, arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001159 case NVME_IOCTL_SUBMIT_IO:
1160 return nvme_submit_io(ns, (void __user *)arg);
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001161 case NVME_IOCTL_DOWNLOAD_FW:
1162 return nvme_download_firmware(ns, (void __user *)arg);
1163 case NVME_IOCTL_ACTIVATE_FW:
1164 return nvme_activate_firmware(ns, arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001165 default:
1166 return -ENOTTY;
1167 }
1168}
1169
1170static const struct block_device_operations nvme_fops = {
1171 .owner = THIS_MODULE,
1172 .ioctl = nvme_ioctl,
Matthew Wilcox49481682011-03-19 14:55:38 -04001173 .compat_ioctl = nvme_ioctl,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001174};
1175
Matthew Wilcox8de05532011-05-12 13:50:28 -04001176static void nvme_timeout_ios(struct nvme_queue *nvmeq)
1177{
1178 int depth = nvmeq->q_depth - 1;
1179 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
1180 unsigned long now = jiffies;
1181 int cmdid;
1182
1183 for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
1184 unsigned long data;
1185 void *ptr;
1186 unsigned char handler;
1187 static struct nvme_completion cqe = { .status = cpu_to_le16(NVME_SC_ABORT_REQ) << 1, };
1188
1189 if (!time_after(now, info[cmdid].timeout))
1190 continue;
1191 dev_warn(nvmeq->q_dmadev, "Timing out I/O %d\n", cmdid);
1192 data = cancel_cmdid(nvmeq, cmdid);
1193 handler = data & 3;
1194 ptr = (void *)(data & ~3UL);
1195 nvme_completions[handler](nvmeq, ptr, &cqe);
1196 }
1197}
1198
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001199static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1200{
1201 while (bio_list_peek(&nvmeq->sq_cong)) {
1202 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1203 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
1204 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
1205 bio_list_add_head(&nvmeq->sq_cong, bio);
1206 break;
1207 }
Matthew Wilcox3cb967c2011-03-16 16:45:49 -04001208 if (bio_list_empty(&nvmeq->sq_cong))
1209 remove_wait_queue(&nvmeq->sq_full,
1210 &nvmeq->sq_cong_wait);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001211 }
1212}
1213
1214static int nvme_kthread(void *data)
1215{
1216 struct nvme_dev *dev;
1217
1218 while (!kthread_should_stop()) {
1219 __set_current_state(TASK_RUNNING);
1220 spin_lock(&dev_list_lock);
1221 list_for_each_entry(dev, &dev_list, node) {
1222 int i;
1223 for (i = 0; i < dev->queue_count; i++) {
1224 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001225 if (!nvmeq)
1226 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001227 spin_lock_irq(&nvmeq->q_lock);
1228 if (nvme_process_cq(nvmeq))
1229 printk("process_cq did something\n");
Matthew Wilcox8de05532011-05-12 13:50:28 -04001230 nvme_timeout_ios(nvmeq);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001231 nvme_resubmit_bios(nvmeq);
1232 spin_unlock_irq(&nvmeq->q_lock);
1233 }
1234 }
1235 spin_unlock(&dev_list_lock);
1236 set_current_state(TASK_INTERRUPTIBLE);
1237 schedule_timeout(HZ);
1238 }
1239 return 0;
1240}
1241
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001242static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, int index,
1243 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1244{
1245 struct nvme_ns *ns;
1246 struct gendisk *disk;
1247 int lbaf;
1248
1249 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1250 return NULL;
1251
1252 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1253 if (!ns)
1254 return NULL;
1255 ns->queue = blk_alloc_queue(GFP_KERNEL);
1256 if (!ns->queue)
1257 goto out_free_ns;
1258 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT | QUEUE_FLAG_NOMERGES |
1259 QUEUE_FLAG_NONROT | QUEUE_FLAG_DISCARD;
1260 blk_queue_make_request(ns->queue, nvme_make_request);
1261 ns->dev = dev;
1262 ns->queue->queuedata = ns;
1263
1264 disk = alloc_disk(NVME_MINORS);
1265 if (!disk)
1266 goto out_free_queue;
1267 ns->ns_id = index;
1268 ns->disk = disk;
1269 lbaf = id->flbas & 0xf;
1270 ns->lba_shift = id->lbaf[lbaf].ds;
1271
1272 disk->major = nvme_major;
1273 disk->minors = NVME_MINORS;
1274 disk->first_minor = NVME_MINORS * index;
1275 disk->fops = &nvme_fops;
1276 disk->private_data = ns;
1277 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05001278 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001279 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, index);
1280 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1281
1282 return ns;
1283
1284 out_free_queue:
1285 blk_cleanup_queue(ns->queue);
1286 out_free_ns:
1287 kfree(ns);
1288 return NULL;
1289}
1290
1291static void nvme_ns_free(struct nvme_ns *ns)
1292{
1293 put_disk(ns->disk);
1294 blk_cleanup_queue(ns->queue);
1295 kfree(ns);
1296}
1297
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001298static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001299{
1300 int status;
1301 u32 result;
1302 struct nvme_command c;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001303 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001304
1305 memset(&c, 0, sizeof(c));
1306 c.features.opcode = nvme_admin_get_features;
1307 c.features.fid = cpu_to_le32(NVME_FEAT_NUM_QUEUES);
1308 c.features.dword11 = cpu_to_le32(q_count);
1309
1310 status = nvme_submit_admin_cmd(dev, &c, &result);
1311 if (status)
1312 return -EIO;
1313 return min(result & 0xffff, result >> 16) + 1;
1314}
1315
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001316static int __devinit nvme_setup_io_queues(struct nvme_dev *dev)
1317{
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001318 int result, cpu, i, nr_io_queues;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001319
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001320 nr_io_queues = num_online_cpus();
1321 result = set_queue_count(dev, nr_io_queues);
Matthew Wilcox1b234842011-01-20 13:01:49 -05001322 if (result < 0)
1323 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001324 if (result < nr_io_queues)
1325 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001326
Matthew Wilcox1b234842011-01-20 13:01:49 -05001327 /* Deregister the admin queue's interrupt */
1328 free_irq(dev->entry[0].vector, dev->queues[0]);
1329
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001330 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001331 dev->entry[i].entry = i;
1332 for (;;) {
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001333 result = pci_enable_msix(dev->pci_dev, dev->entry,
1334 nr_io_queues);
Matthew Wilcox1b234842011-01-20 13:01:49 -05001335 if (result == 0) {
1336 break;
1337 } else if (result > 0) {
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001338 nr_io_queues = result;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001339 continue;
1340 } else {
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001341 nr_io_queues = 1;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001342 break;
1343 }
1344 }
1345
1346 result = queue_request_irq(dev, dev->queues[0], "nvme admin");
1347 /* XXX: handle failure here */
1348
1349 cpu = cpumask_first(cpu_online_mask);
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001350 for (i = 0; i < nr_io_queues; i++) {
Matthew Wilcox1b234842011-01-20 13:01:49 -05001351 irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu));
1352 cpu = cpumask_next(cpu, cpu_online_mask);
1353 }
1354
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001355 for (i = 0; i < nr_io_queues; i++) {
Matthew Wilcox1b234842011-01-20 13:01:49 -05001356 dev->queues[i + 1] = nvme_create_queue(dev, i + 1,
1357 NVME_Q_DEPTH, i);
1358 if (!dev->queues[i + 1])
1359 return -ENOMEM;
1360 dev->queue_count++;
1361 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001362
Matthew Wilcox9ecdc942011-03-16 16:52:19 -04001363 for (; i < num_possible_cpus(); i++) {
1364 int target = i % rounddown_pow_of_two(dev->queue_count - 1);
1365 dev->queues[i + 1] = dev->queues[target + 1];
1366 }
1367
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001368 return 0;
1369}
1370
1371static void nvme_free_queues(struct nvme_dev *dev)
1372{
1373 int i;
1374
1375 for (i = dev->queue_count - 1; i >= 0; i--)
1376 nvme_free_queue(dev, i);
1377}
1378
1379static int __devinit nvme_dev_add(struct nvme_dev *dev)
1380{
1381 int res, nn, i;
1382 struct nvme_ns *ns, *next;
Matthew Wilcox51814232011-02-01 16:18:08 -05001383 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001384 void *id;
1385 dma_addr_t dma_addr;
1386 struct nvme_command cid, crt;
1387
1388 res = nvme_setup_io_queues(dev);
1389 if (res)
1390 return res;
1391
1392 /* XXX: Switch to a SG list once prp2 works */
1393 id = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr,
1394 GFP_KERNEL);
1395
1396 memset(&cid, 0, sizeof(cid));
1397 cid.identify.opcode = nvme_admin_identify;
1398 cid.identify.nsid = 0;
1399 cid.identify.prp1 = cpu_to_le64(dma_addr);
1400 cid.identify.cns = cpu_to_le32(1);
1401
1402 res = nvme_submit_admin_cmd(dev, &cid, NULL);
1403 if (res) {
1404 res = -EIO;
1405 goto out_free;
1406 }
1407
Matthew Wilcox51814232011-02-01 16:18:08 -05001408 ctrl = id;
1409 nn = le32_to_cpup(&ctrl->nn);
1410 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
1411 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
1412 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001413
1414 cid.identify.cns = 0;
1415 memset(&crt, 0, sizeof(crt));
1416 crt.features.opcode = nvme_admin_get_features;
1417 crt.features.prp1 = cpu_to_le64(dma_addr + 4096);
1418 crt.features.fid = cpu_to_le32(NVME_FEAT_LBA_RANGE);
1419
Matthew Wilcoxac88c362011-03-16 16:29:58 -04001420 for (i = 0; i <= nn; i++) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001421 cid.identify.nsid = cpu_to_le32(i);
1422 res = nvme_submit_admin_cmd(dev, &cid, NULL);
1423 if (res)
1424 continue;
1425
1426 if (((struct nvme_id_ns *)id)->ncap == 0)
1427 continue;
1428
1429 crt.features.nsid = cpu_to_le32(i);
1430 res = nvme_submit_admin_cmd(dev, &crt, NULL);
1431 if (res)
1432 continue;
1433
1434 ns = nvme_alloc_ns(dev, i, id, id + 4096);
1435 if (ns)
1436 list_add_tail(&ns->list, &dev->namespaces);
1437 }
1438 list_for_each_entry(ns, &dev->namespaces, list)
1439 add_disk(ns->disk);
1440
1441 dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr);
1442 return 0;
1443
1444 out_free:
1445 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
1446 list_del(&ns->list);
1447 nvme_ns_free(ns);
1448 }
1449
1450 dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr);
1451 return res;
1452}
1453
1454static int nvme_dev_remove(struct nvme_dev *dev)
1455{
1456 struct nvme_ns *ns, *next;
1457
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001458 spin_lock(&dev_list_lock);
1459 list_del(&dev->node);
1460 spin_unlock(&dev_list_lock);
1461
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001462 /* TODO: wait all I/O finished or cancel them */
1463
1464 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
1465 list_del(&ns->list);
1466 del_gendisk(ns->disk);
1467 nvme_ns_free(ns);
1468 }
1469
1470 nvme_free_queues(dev);
1471
1472 return 0;
1473}
1474
Matthew Wilcox091b6092011-02-10 09:56:01 -05001475static int nvme_setup_prp_pools(struct nvme_dev *dev)
1476{
1477 struct device *dmadev = &dev->pci_dev->dev;
1478 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
1479 PAGE_SIZE, PAGE_SIZE, 0);
1480 if (!dev->prp_page_pool)
1481 return -ENOMEM;
1482
Matthew Wilcox99802a72011-02-10 10:30:34 -05001483 /* Optimisation for I/Os between 4k and 128k */
1484 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
1485 256, 256, 0);
1486 if (!dev->prp_small_pool) {
1487 dma_pool_destroy(dev->prp_page_pool);
1488 return -ENOMEM;
1489 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05001490 return 0;
1491}
1492
1493static void nvme_release_prp_pools(struct nvme_dev *dev)
1494{
1495 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05001496 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05001497}
1498
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001499/* XXX: Use an ida or something to let remove / add work correctly */
1500static void nvme_set_instance(struct nvme_dev *dev)
1501{
1502 static int instance;
1503 dev->instance = instance++;
1504}
1505
1506static void nvme_release_instance(struct nvme_dev *dev)
1507{
1508}
1509
1510static int __devinit nvme_probe(struct pci_dev *pdev,
1511 const struct pci_device_id *id)
1512{
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001513 int bars, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001514 struct nvme_dev *dev;
1515
1516 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1517 if (!dev)
1518 return -ENOMEM;
1519 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
1520 GFP_KERNEL);
1521 if (!dev->entry)
1522 goto free;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001523 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
1524 GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001525 if (!dev->queues)
1526 goto free;
1527
Shane Michael Matthews0ee5a7d2011-02-01 08:49:30 -05001528 if (pci_enable_device_mem(pdev))
1529 goto free;
Matthew Wilcoxf64d3362011-02-01 09:01:59 -05001530 pci_set_master(pdev);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001531 bars = pci_select_bars(pdev, IORESOURCE_MEM);
1532 if (pci_request_selected_regions(pdev, bars, "nvme"))
1533 goto disable;
Shane Michael Matthews0ee5a7d2011-02-01 08:49:30 -05001534
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001535 INIT_LIST_HEAD(&dev->namespaces);
1536 dev->pci_dev = pdev;
1537 pci_set_drvdata(pdev, dev);
Matthew Wilcox29303532011-02-01 16:23:39 -05001538 dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
1539 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001540 nvme_set_instance(dev);
Matthew Wilcox53c95772011-01-20 13:42:34 -05001541 dev->entry[0].vector = pdev->irq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001542
Matthew Wilcox091b6092011-02-10 09:56:01 -05001543 result = nvme_setup_prp_pools(dev);
1544 if (result)
1545 goto disable_msix;
1546
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001547 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1548 if (!dev->bar) {
1549 result = -ENOMEM;
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001550 goto disable_msix;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001551 }
1552
1553 result = nvme_configure_admin_queue(dev);
1554 if (result)
1555 goto unmap;
1556 dev->queue_count++;
1557
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001558 spin_lock(&dev_list_lock);
1559 list_add(&dev->node, &dev_list);
1560 spin_unlock(&dev_list_lock);
1561
Matthew Wilcox740216f2011-02-15 16:28:20 -05001562 result = nvme_dev_add(dev);
1563 if (result)
1564 goto delete;
1565
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001566 return 0;
1567
1568 delete:
Matthew Wilcox740216f2011-02-15 16:28:20 -05001569 spin_lock(&dev_list_lock);
1570 list_del(&dev->node);
1571 spin_unlock(&dev_list_lock);
1572
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001573 nvme_free_queues(dev);
1574 unmap:
1575 iounmap(dev->bar);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001576 disable_msix:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001577 pci_disable_msix(pdev);
1578 nvme_release_instance(dev);
Matthew Wilcox091b6092011-02-10 09:56:01 -05001579 nvme_release_prp_pools(dev);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001580 disable:
Shane Michael Matthews0ee5a7d2011-02-01 08:49:30 -05001581 pci_disable_device(pdev);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001582 pci_release_regions(pdev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001583 free:
1584 kfree(dev->queues);
1585 kfree(dev->entry);
1586 kfree(dev);
1587 return result;
1588}
1589
1590static void __devexit nvme_remove(struct pci_dev *pdev)
1591{
1592 struct nvme_dev *dev = pci_get_drvdata(pdev);
1593 nvme_dev_remove(dev);
1594 pci_disable_msix(pdev);
1595 iounmap(dev->bar);
1596 nvme_release_instance(dev);
Matthew Wilcox091b6092011-02-10 09:56:01 -05001597 nvme_release_prp_pools(dev);
Shane Michael Matthews0ee5a7d2011-02-01 08:49:30 -05001598 pci_disable_device(pdev);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001599 pci_release_regions(pdev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001600 kfree(dev->queues);
1601 kfree(dev->entry);
1602 kfree(dev);
1603}
1604
1605/* These functions are yet to be implemented */
1606#define nvme_error_detected NULL
1607#define nvme_dump_registers NULL
1608#define nvme_link_reset NULL
1609#define nvme_slot_reset NULL
1610#define nvme_error_resume NULL
1611#define nvme_suspend NULL
1612#define nvme_resume NULL
1613
1614static struct pci_error_handlers nvme_err_handler = {
1615 .error_detected = nvme_error_detected,
1616 .mmio_enabled = nvme_dump_registers,
1617 .link_reset = nvme_link_reset,
1618 .slot_reset = nvme_slot_reset,
1619 .resume = nvme_error_resume,
1620};
1621
1622/* Move to pci_ids.h later */
1623#define PCI_CLASS_STORAGE_EXPRESS 0x010802
1624
1625static DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
1626 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
1627 { 0, }
1628};
1629MODULE_DEVICE_TABLE(pci, nvme_id_table);
1630
1631static struct pci_driver nvme_driver = {
1632 .name = "nvme",
1633 .id_table = nvme_id_table,
1634 .probe = nvme_probe,
1635 .remove = __devexit_p(nvme_remove),
1636 .suspend = nvme_suspend,
1637 .resume = nvme_resume,
1638 .err_handler = &nvme_err_handler,
1639};
1640
1641static int __init nvme_init(void)
1642{
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001643 int result = -EBUSY;
1644
1645 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
1646 if (IS_ERR(nvme_thread))
1647 return PTR_ERR(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001648
1649 nvme_major = register_blkdev(nvme_major, "nvme");
1650 if (nvme_major <= 0)
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001651 goto kill_kthread;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001652
1653 result = pci_register_driver(&nvme_driver);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001654 if (result)
1655 goto unregister_blkdev;
1656 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001657
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001658 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001659 unregister_blkdev(nvme_major, "nvme");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001660 kill_kthread:
1661 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001662 return result;
1663}
1664
1665static void __exit nvme_exit(void)
1666{
1667 pci_unregister_driver(&nvme_driver);
1668 unregister_blkdev(nvme_major, "nvme");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001669 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001670}
1671
1672MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
1673MODULE_LICENSE("GPL");
Matthew Wilcox8ef70062011-03-21 10:28:43 -04001674MODULE_VERSION("0.5");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001675module_init(nvme_init);
1676module_exit(nvme_exit);