2 * NVM Express device driver
3 * Copyright (c) 2011, Intel Corporation.
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.
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
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.
19 #include <linux/nvme.h>
20 #include <linux/bio.h>
21 #include <linux/blkdev.h>
22 #include <linux/errno.h>
24 #include <linux/genhd.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
28 #include <linux/kdev_t.h>
29 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
33 #include <linux/pci.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/types.h>
37 #include <linux/version.h>
39 #define NVME_Q_DEPTH 1024
40 #define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
41 #define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
42 #define NVME_MINORS 64
44 static int nvme_major;
45 module_param(nvme_major, int, 0);
48 * Represents an NVM Express device. Each nvme_dev is a PCI function.
51 struct list_head node;
52 struct nvme_queue **queues;
54 struct pci_dev *pci_dev;
58 struct msix_entry *entry;
59 struct nvme_bar __iomem *bar;
60 struct list_head namespaces;
64 * An NVM Express namespace is equivalent to a SCSI LUN
67 struct list_head list;
70 struct request_queue *queue;
78 * An NVM Express queue. Each device has at least two (one for admin
79 * commands and one for I/O commands).
82 struct device *q_dmadev;
84 struct nvme_command *sq_cmds;
85 volatile struct nvme_completion *cqes;
86 dma_addr_t sq_dma_addr;
87 dma_addr_t cq_dma_addr;
88 wait_queue_head_t sq_full;
89 struct bio_list sq_cong;
97 unsigned long cmdid_data[];
101 * Check we didin't inadvertently grow the command struct
103 static inline void _nvme_check_size(void)
105 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
106 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
107 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
108 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
109 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
110 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
111 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
112 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
113 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
117 * alloc_cmdid - Allocate a Command ID
118 * @param nvmeq The queue that will be used for this command
119 * @param ctx A pointer that will be passed to the handler
120 * @param handler The ID of the handler to call
122 * Allocate a Command ID for a queue. The data passed in will
123 * be passed to the completion handler. This is implemented by using
124 * the bottom two bits of the ctx pointer to store the handler ID.
125 * Passing in a pointer that's not 4-byte aligned will cause a BUG.
126 * We can change this if it becomes a problem.
128 static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx, int handler)
130 int depth = nvmeq->q_depth;
131 unsigned long data = (unsigned long)ctx | handler;
134 BUG_ON((unsigned long)ctx & 3);
137 cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
140 } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
142 nvmeq->cmdid_data[cmdid + BITS_TO_LONGS(depth)] = data;
146 static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
150 wait_event_killable(nvmeq->sq_full,
151 (cmdid = alloc_cmdid(nvmeq, ctx, handler)) >= 0);
152 return (cmdid < 0) ? -EINTR : cmdid;
155 /* If you need more than four handlers, you'll need to change how
156 * alloc_cmdid and nvme_process_cq work
159 sync_completion_id = 0,
163 static unsigned long free_cmdid(struct nvme_queue *nvmeq, int cmdid)
167 data = nvmeq->cmdid_data[cmdid + BITS_TO_LONGS(nvmeq->q_depth)];
168 clear_bit(cmdid, nvmeq->cmdid_data);
169 wake_up(&nvmeq->sq_full);
173 static struct nvme_queue *get_nvmeq(struct nvme_ns *ns)
175 int qid, cpu = get_cpu();
176 if (cpu < ns->dev->queue_count)
179 qid = (cpu % rounddown_pow_of_two(ns->dev->queue_count)) + 1;
180 return ns->dev->queues[qid];
183 static void put_nvmeq(struct nvme_queue *nvmeq)
189 * nvme_submit_cmd: Copy a command into a queue and ring the doorbell
190 * @nvmeq: The queue to use
191 * @cmd: The command to send
193 * Safe to use from interrupt context
195 static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
199 /* XXX: Need to check tail isn't going to overrun head */
200 spin_lock_irqsave(&nvmeq->q_lock, flags);
201 tail = nvmeq->sq_tail;
202 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
203 writel(tail, nvmeq->q_db);
204 if (++tail == nvmeq->q_depth)
206 nvmeq->sq_tail = tail;
207 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
212 struct nvme_req_info {
215 struct scatterlist sg[0];
218 /* XXX: use a mempool */
219 static struct nvme_req_info *alloc_info(unsigned nseg, gfp_t gfp)
221 return kmalloc(sizeof(struct nvme_req_info) +
222 sizeof(struct scatterlist) * nseg, gfp);
225 static void free_info(struct nvme_req_info *info)
230 static void bio_completion(struct nvme_queue *nvmeq, void *ctx,
231 struct nvme_completion *cqe)
233 struct nvme_req_info *info = ctx;
234 struct bio *bio = info->bio;
235 u16 status = le16_to_cpup(&cqe->status) >> 1;
237 dma_unmap_sg(nvmeq->q_dmadev, info->sg, info->nents,
238 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
240 bio_endio(bio, status ? -EIO : 0);
243 /* length is in bytes */
244 static void nvme_setup_prps(struct nvme_common_command *cmd,
245 struct scatterlist *sg, int length)
247 int dma_len = sg_dma_len(sg);
248 u64 dma_addr = sg_dma_address(sg);
249 int offset = offset_in_page(dma_addr);
251 cmd->prp1 = cpu_to_le64(dma_addr);
252 length -= (PAGE_SIZE - offset);
256 dma_len -= (PAGE_SIZE - offset);
258 dma_addr += (PAGE_SIZE - offset);
261 dma_addr = sg_dma_address(sg);
262 dma_len = sg_dma_len(sg);
265 if (length <= PAGE_SIZE) {
266 cmd->prp2 = cpu_to_le64(dma_addr);
270 /* XXX: support PRP lists */
273 static int nvme_map_bio(struct device *dev, struct nvme_req_info *info,
274 struct bio *bio, enum dma_data_direction dma_dir, int psegs)
276 struct bio_vec *bvec;
277 struct scatterlist *sg = info->sg;
280 sg_init_table(sg, psegs);
281 bio_for_each_segment(bvec, bio, i) {
282 sg_set_page(sg, bvec->bv_page, bvec->bv_len, bvec->bv_offset);
283 /* XXX: handle non-mergable here */
288 return dma_map_sg(dev, info->sg, info->nents, dma_dir);
291 static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
294 struct nvme_command *cmnd;
295 struct nvme_req_info *info;
296 enum dma_data_direction dma_dir;
301 int psegs = bio_phys_segments(ns->queue, bio);
303 info = alloc_info(psegs, GFP_NOIO);
308 cmdid = alloc_cmdid(nvmeq, info, bio_completion_id);
309 if (unlikely(cmdid < 0))
313 if (bio->bi_rw & REQ_FUA)
314 control |= NVME_RW_FUA;
315 if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
316 control |= NVME_RW_LR;
319 if (bio->bi_rw & REQ_RAHEAD)
320 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
322 spin_lock_irqsave(&nvmeq->q_lock, flags);
323 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
325 memset(cmnd, 0, sizeof(*cmnd));
326 if (bio_data_dir(bio)) {
327 cmnd->rw.opcode = nvme_cmd_write;
328 dma_dir = DMA_TO_DEVICE;
330 cmnd->rw.opcode = nvme_cmd_read;
331 dma_dir = DMA_FROM_DEVICE;
334 nvme_map_bio(nvmeq->q_dmadev, info, bio, dma_dir, psegs);
337 cmnd->rw.command_id = cmdid;
338 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
339 nvme_setup_prps(&cmnd->common, info->sg, bio->bi_size);
340 cmnd->rw.slba = cpu_to_le64(bio->bi_sector >> (ns->lba_shift - 9));
341 cmnd->rw.length = cpu_to_le16((bio->bi_size >> ns->lba_shift) - 1);
342 cmnd->rw.control = cpu_to_le16(control);
343 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
345 writel(nvmeq->sq_tail, nvmeq->q_db);
346 if (++nvmeq->sq_tail == nvmeq->q_depth)
349 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
360 * NB: return value of non-zero would mean that we were a stacking driver.
361 * make_request must always succeed.
363 static int nvme_make_request(struct request_queue *q, struct bio *bio)
365 struct nvme_ns *ns = q->queuedata;
366 struct nvme_queue *nvmeq = get_nvmeq(ns);
368 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
369 blk_set_queue_congested(q, rw_is_sync(bio->bi_rw));
370 bio_list_add(&nvmeq->sq_cong, bio);
377 struct sync_cmd_info {
378 struct task_struct *task;
383 static void sync_completion(struct nvme_queue *nvmeq, void *ctx,
384 struct nvme_completion *cqe)
386 struct sync_cmd_info *cmdinfo = ctx;
387 cmdinfo->result = le32_to_cpup(&cqe->result);
388 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
389 wake_up_process(cmdinfo->task);
392 typedef void (*completion_fn)(struct nvme_queue *, void *,
393 struct nvme_completion *);
395 static irqreturn_t nvme_process_cq(struct nvme_queue *nvmeq)
399 static const completion_fn completions[4] = {
400 [sync_completion_id] = sync_completion,
401 [bio_completion_id] = bio_completion,
404 head = nvmeq->cq_head;
405 phase = nvmeq->cq_phase;
410 unsigned char handler;
411 struct nvme_completion cqe = nvmeq->cqes[head];
412 if ((le16_to_cpu(cqe.status) & 1) != phase)
414 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
415 if (++head == nvmeq->q_depth) {
420 data = free_cmdid(nvmeq, cqe.command_id);
422 ptr = (void *)(data & ~3UL);
423 completions[handler](nvmeq, ptr, &cqe);
426 /* If the controller ignores the cq head doorbell and continuously
427 * writes to the queue, it is theoretically possible to wrap around
428 * the queue twice and mistakenly return IRQ_NONE. Linux only
429 * requires that 0.1% of your interrupts are handled, so this isn't
432 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
435 writel(head, nvmeq->q_db + 1);
436 nvmeq->cq_head = head;
437 nvmeq->cq_phase = phase;
442 static irqreturn_t nvme_irq(int irq, void *data)
444 return nvme_process_cq(data);
448 * Returns 0 on success. If the result is negative, it's a Linux error code;
449 * if the result is positive, it's an NVM Express status code
451 static int nvme_submit_sync_cmd(struct nvme_queue *q, struct nvme_command *cmd,
455 struct sync_cmd_info cmdinfo;
457 cmdinfo.task = current;
458 cmdinfo.status = -EINTR;
460 cmdid = alloc_cmdid_killable(q, &cmdinfo, sync_completion_id);
463 cmd->common.command_id = cmdid;
465 set_current_state(TASK_UNINTERRUPTIBLE);
466 nvme_submit_cmd(q, cmd);
470 *result = cmdinfo.result;
472 return cmdinfo.status;
475 static int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
478 return nvme_submit_sync_cmd(dev->queues[0], cmd, result);
481 static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
484 struct nvme_command c;
486 memset(&c, 0, sizeof(c));
487 c.delete_queue.opcode = opcode;
488 c.delete_queue.qid = cpu_to_le16(id);
490 status = nvme_submit_admin_cmd(dev, &c, NULL);
496 static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
497 struct nvme_queue *nvmeq)
500 struct nvme_command c;
501 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
503 memset(&c, 0, sizeof(c));
504 c.create_cq.opcode = nvme_admin_create_cq;
505 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
506 c.create_cq.cqid = cpu_to_le16(qid);
507 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
508 c.create_cq.cq_flags = cpu_to_le16(flags);
509 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
511 status = nvme_submit_admin_cmd(dev, &c, NULL);
517 static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
518 struct nvme_queue *nvmeq)
521 struct nvme_command c;
522 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
524 memset(&c, 0, sizeof(c));
525 c.create_sq.opcode = nvme_admin_create_sq;
526 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
527 c.create_sq.sqid = cpu_to_le16(qid);
528 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
529 c.create_sq.sq_flags = cpu_to_le16(flags);
530 c.create_sq.cqid = cpu_to_le16(qid);
532 status = nvme_submit_admin_cmd(dev, &c, NULL);
538 static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
540 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
543 static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
545 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
548 static void nvme_free_queue(struct nvme_dev *dev, int qid)
550 struct nvme_queue *nvmeq = dev->queues[qid];
552 free_irq(dev->entry[nvmeq->cq_vector].vector, nvmeq);
554 /* Don't tell the adapter to delete the admin queue */
556 adapter_delete_sq(dev, qid);
557 adapter_delete_cq(dev, qid);
560 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
561 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
562 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
563 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
567 static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
568 int depth, int vector)
570 struct device *dmadev = &dev->pci_dev->dev;
571 unsigned extra = (depth + BITS_TO_LONGS(depth)) * sizeof(long);
572 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
576 nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
577 &nvmeq->cq_dma_addr, GFP_KERNEL);
580 memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
582 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
583 &nvmeq->sq_dma_addr, GFP_KERNEL);
587 nvmeq->q_dmadev = dmadev;
588 spin_lock_init(&nvmeq->q_lock);
591 init_waitqueue_head(&nvmeq->sq_full);
592 bio_list_init(&nvmeq->sq_cong);
593 nvmeq->q_db = &dev->dbs[qid * 2];
594 nvmeq->q_depth = depth;
595 nvmeq->cq_vector = vector;
600 dma_free_coherent(dmadev, CQ_SIZE(nvmeq->q_depth), (void *)nvmeq->cqes,
607 static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
610 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
611 IRQF_DISABLED | IRQF_SHARED, name, nvmeq);
614 static __devinit struct nvme_queue *nvme_create_queue(struct nvme_dev *dev,
615 int qid, int cq_size, int vector)
618 struct nvme_queue *nvmeq = nvme_alloc_queue(dev, qid, cq_size, vector);
620 result = adapter_alloc_cq(dev, qid, nvmeq);
624 result = adapter_alloc_sq(dev, qid, nvmeq);
628 result = queue_request_irq(dev, nvmeq, "nvme");
635 adapter_delete_sq(dev, qid);
637 adapter_delete_cq(dev, qid);
639 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
640 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
641 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
642 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
647 static int __devinit nvme_configure_admin_queue(struct nvme_dev *dev)
651 struct nvme_queue *nvmeq;
653 dev->dbs = ((void __iomem *)dev->bar) + 4096;
655 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
657 aqa = nvmeq->q_depth - 1;
660 dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
661 dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
662 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
664 writel(aqa, &dev->bar->aqa);
665 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
666 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
667 writel(dev->ctrl_config, &dev->bar->cc);
669 while (!(readl(&dev->bar->csts) & NVME_CSTS_RDY)) {
671 if (fatal_signal_pending(current))
675 result = queue_request_irq(dev, nvmeq, "nvme admin");
676 dev->queues[0] = nvmeq;
680 static int nvme_identify(struct nvme_ns *ns, unsigned long addr, int cns)
682 struct nvme_dev *dev = ns->dev;
683 int i, err, count, nents, offset;
684 struct nvme_command c;
685 struct scatterlist sg[2];
686 struct page *pages[2];
690 offset = offset_in_page(addr);
691 count = offset ? 2 : 1;
693 err = get_user_pages_fast(addr, count, 1, pages);
699 sg_init_table(sg, count);
700 sg_set_page(&sg[0], pages[0], PAGE_SIZE - offset, offset);
702 sg_set_page(&sg[1], pages[1], offset, 0);
703 nents = dma_map_sg(&dev->pci_dev->dev, sg, count, DMA_FROM_DEVICE);
707 memset(&c, 0, sizeof(c));
708 c.identify.opcode = nvme_admin_identify;
709 c.identify.nsid = cns ? 0 : cpu_to_le32(ns->ns_id);
710 nvme_setup_prps(&c.common, sg, 4096);
711 c.identify.cns = cpu_to_le32(cns);
713 err = nvme_submit_admin_cmd(dev, &c, NULL);
718 dma_unmap_sg(&dev->pci_dev->dev, sg, nents, DMA_FROM_DEVICE);
720 for (i = 0; i < count; i++)
726 static int nvme_get_range_type(struct nvme_ns *ns, void __user *addr)
728 struct nvme_dev *dev = ns->dev;
730 struct nvme_command c;
734 page = dma_alloc_coherent(&dev->pci_dev->dev, 4096, &dma_addr,
737 memset(&c, 0, sizeof(c));
738 c.features.opcode = nvme_admin_get_features;
739 c.features.nsid = cpu_to_le32(ns->ns_id);
740 c.features.prp1 = cpu_to_le64(dma_addr);
741 c.features.fid = cpu_to_le32(NVME_FEAT_LBA_RANGE);
743 status = nvme_submit_admin_cmd(dev, &c, NULL);
745 /* XXX: Assuming first range for now */
748 else if (copy_to_user(addr, page, 64))
751 dma_free_coherent(&dev->pci_dev->dev, 4096, page, dma_addr);
756 static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
759 struct nvme_ns *ns = bdev->bd_disk->private_data;
762 case NVME_IOCTL_IDENTIFY_NS:
763 return nvme_identify(ns, arg, 0);
764 case NVME_IOCTL_IDENTIFY_CTRL:
765 return nvme_identify(ns, arg, 1);
766 case NVME_IOCTL_GET_RANGE_TYPE:
767 return nvme_get_range_type(ns, (void __user *)arg);
773 static const struct block_device_operations nvme_fops = {
774 .owner = THIS_MODULE,
778 static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, int index,
779 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
782 struct gendisk *disk;
785 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
788 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
791 ns->queue = blk_alloc_queue(GFP_KERNEL);
794 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT | QUEUE_FLAG_NOMERGES |
795 QUEUE_FLAG_NONROT | QUEUE_FLAG_DISCARD;
796 blk_queue_make_request(ns->queue, nvme_make_request);
798 ns->queue->queuedata = ns;
800 disk = alloc_disk(NVME_MINORS);
805 lbaf = id->flbas & 0xf;
806 ns->lba_shift = id->lbaf[lbaf].ds;
808 disk->major = nvme_major;
809 disk->minors = NVME_MINORS;
810 disk->first_minor = NVME_MINORS * index;
811 disk->fops = &nvme_fops;
812 disk->private_data = ns;
813 disk->queue = ns->queue;
814 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, index);
815 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
820 blk_cleanup_queue(ns->queue);
826 static void nvme_ns_free(struct nvme_ns *ns)
829 blk_cleanup_queue(ns->queue);
833 static int set_queue_count(struct nvme_dev *dev, int count)
837 struct nvme_command c;
838 u32 q_count = (count - 1) | ((count - 1) << 16);
840 memset(&c, 0, sizeof(c));
841 c.features.opcode = nvme_admin_get_features;
842 c.features.fid = cpu_to_le32(NVME_FEAT_NUM_QUEUES);
843 c.features.dword11 = cpu_to_le32(q_count);
845 status = nvme_submit_admin_cmd(dev, &c, &result);
848 return min(result & 0xffff, result >> 16) + 1;
851 static int __devinit nvme_setup_io_queues(struct nvme_dev *dev)
853 int result, cpu, i, nr_queues;
855 nr_queues = num_online_cpus();
856 result = set_queue_count(dev, nr_queues);
859 if (result < nr_queues)
862 /* Deregister the admin queue's interrupt */
863 free_irq(dev->entry[0].vector, dev->queues[0]);
865 for (i = 0; i < nr_queues; i++)
866 dev->entry[i].entry = i;
868 result = pci_enable_msix(dev->pci_dev, dev->entry, nr_queues);
871 } else if (result > 0) {
880 result = queue_request_irq(dev, dev->queues[0], "nvme admin");
881 /* XXX: handle failure here */
883 cpu = cpumask_first(cpu_online_mask);
884 for (i = 0; i < nr_queues; i++) {
885 irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu));
886 cpu = cpumask_next(cpu, cpu_online_mask);
889 for (i = 0; i < nr_queues; i++) {
890 dev->queues[i + 1] = nvme_create_queue(dev, i + 1,
892 if (!dev->queues[i + 1])
900 static void nvme_free_queues(struct nvme_dev *dev)
904 for (i = dev->queue_count - 1; i >= 0; i--)
905 nvme_free_queue(dev, i);
908 static int __devinit nvme_dev_add(struct nvme_dev *dev)
911 struct nvme_ns *ns, *next;
914 struct nvme_command cid, crt;
916 res = nvme_setup_io_queues(dev);
920 /* XXX: Switch to a SG list once prp2 works */
921 id = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr,
924 memset(&cid, 0, sizeof(cid));
925 cid.identify.opcode = nvme_admin_identify;
926 cid.identify.nsid = 0;
927 cid.identify.prp1 = cpu_to_le64(dma_addr);
928 cid.identify.cns = cpu_to_le32(1);
930 res = nvme_submit_admin_cmd(dev, &cid, NULL);
936 nn = le32_to_cpup(&((struct nvme_id_ctrl *)id)->nn);
938 cid.identify.cns = 0;
939 memset(&crt, 0, sizeof(crt));
940 crt.features.opcode = nvme_admin_get_features;
941 crt.features.prp1 = cpu_to_le64(dma_addr + 4096);
942 crt.features.fid = cpu_to_le32(NVME_FEAT_LBA_RANGE);
944 for (i = 0; i < nn; i++) {
945 cid.identify.nsid = cpu_to_le32(i);
946 res = nvme_submit_admin_cmd(dev, &cid, NULL);
950 if (((struct nvme_id_ns *)id)->ncap == 0)
953 crt.features.nsid = cpu_to_le32(i);
954 res = nvme_submit_admin_cmd(dev, &crt, NULL);
958 ns = nvme_alloc_ns(dev, i, id, id + 4096);
960 list_add_tail(&ns->list, &dev->namespaces);
962 list_for_each_entry(ns, &dev->namespaces, list)
965 dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr);
969 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
974 dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr);
978 static int nvme_dev_remove(struct nvme_dev *dev)
980 struct nvme_ns *ns, *next;
982 /* TODO: wait all I/O finished or cancel them */
984 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
986 del_gendisk(ns->disk);
990 nvme_free_queues(dev);
995 /* XXX: Use an ida or something to let remove / add work correctly */
996 static void nvme_set_instance(struct nvme_dev *dev)
999 dev->instance = instance++;
1002 static void nvme_release_instance(struct nvme_dev *dev)
1006 static int __devinit nvme_probe(struct pci_dev *pdev,
1007 const struct pci_device_id *id)
1009 int result = -ENOMEM;
1010 struct nvme_dev *dev;
1012 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1015 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
1019 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
1024 INIT_LIST_HEAD(&dev->namespaces);
1025 dev->pci_dev = pdev;
1026 pci_set_drvdata(pdev, dev);
1027 dma_set_mask(&dev->pci_dev->dev, DMA_BIT_MASK(64));
1028 nvme_set_instance(dev);
1029 dev->entry[0].vector = pdev->irq;
1031 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1037 result = nvme_configure_admin_queue(dev);
1042 result = nvme_dev_add(dev);
1048 nvme_free_queues(dev);
1052 pci_disable_msix(pdev);
1053 nvme_release_instance(dev);
1061 static void __devexit nvme_remove(struct pci_dev *pdev)
1063 struct nvme_dev *dev = pci_get_drvdata(pdev);
1064 nvme_dev_remove(dev);
1065 pci_disable_msix(pdev);
1067 nvme_release_instance(dev);
1073 /* These functions are yet to be implemented */
1074 #define nvme_error_detected NULL
1075 #define nvme_dump_registers NULL
1076 #define nvme_link_reset NULL
1077 #define nvme_slot_reset NULL
1078 #define nvme_error_resume NULL
1079 #define nvme_suspend NULL
1080 #define nvme_resume NULL
1082 static struct pci_error_handlers nvme_err_handler = {
1083 .error_detected = nvme_error_detected,
1084 .mmio_enabled = nvme_dump_registers,
1085 .link_reset = nvme_link_reset,
1086 .slot_reset = nvme_slot_reset,
1087 .resume = nvme_error_resume,
1090 /* Move to pci_ids.h later */
1091 #define PCI_CLASS_STORAGE_EXPRESS 0x010802
1093 static DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
1094 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
1097 MODULE_DEVICE_TABLE(pci, nvme_id_table);
1099 static struct pci_driver nvme_driver = {
1101 .id_table = nvme_id_table,
1102 .probe = nvme_probe,
1103 .remove = __devexit_p(nvme_remove),
1104 .suspend = nvme_suspend,
1105 .resume = nvme_resume,
1106 .err_handler = &nvme_err_handler,
1109 static int __init nvme_init(void)
1113 nvme_major = register_blkdev(nvme_major, "nvme");
1114 if (nvme_major <= 0)
1117 result = pci_register_driver(&nvme_driver);
1121 unregister_blkdev(nvme_major, "nvme");
1125 static void __exit nvme_exit(void)
1127 pci_unregister_driver(&nvme_driver);
1128 unregister_blkdev(nvme_major, "nvme");
1131 MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
1132 MODULE_LICENSE("GPL");
1133 MODULE_VERSION("0.1");
1134 module_init(nvme_init);
1135 module_exit(nvme_exit);