blob: 26e266072079e5c23edc47af0369d7b931df2f26 [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>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040023#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050024#include <linux/errno.h>
25#include <linux/fs.h>
26#include <linux/genhd.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040027#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050028#include <linux/init.h>
29#include <linux/interrupt.h>
30#include <linux/io.h>
31#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050032#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050033#include <linux/kernel.h>
34#include <linux/mm.h>
35#include <linux/module.h>
36#include <linux/moduleparam.h>
37#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050038#include <linux/poison.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050039#include <linux/sched.h>
40#include <linux/slab.h>
41#include <linux/types.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050042
Hitoshi Mitake797a7962012-02-07 11:45:33 +090043#include <asm-generic/io-64-nonatomic-lo-hi.h>
44
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050045#define NVME_Q_DEPTH 1024
46#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
47#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
48#define NVME_MINORS 64
Matthew Wilcoxff976d72011-12-20 13:53:01 -050049#define NVME_IO_TIMEOUT (5 * HZ)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -050050#define ADMIN_TIMEOUT (60 * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050051
52static int nvme_major;
53module_param(nvme_major, int, 0);
54
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050055static int use_threaded_interrupts;
56module_param(use_threaded_interrupts, int, 0);
57
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050058static DEFINE_SPINLOCK(dev_list_lock);
59static LIST_HEAD(dev_list);
60static struct task_struct *nvme_thread;
61
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050062/*
63 * Represents an NVM Express device. Each nvme_dev is a PCI function.
64 */
65struct nvme_dev {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050066 struct list_head node;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050067 struct nvme_queue **queues;
68 u32 __iomem *dbs;
69 struct pci_dev *pci_dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -050070 struct dma_pool *prp_page_pool;
Matthew Wilcox99802a72011-02-10 10:30:34 -050071 struct dma_pool *prp_small_pool;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050072 int instance;
73 int queue_count;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -040074 int db_stride;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050075 u32 ctrl_config;
76 struct msix_entry *entry;
77 struct nvme_bar __iomem *bar;
78 struct list_head namespaces;
Matthew Wilcox51814232011-02-01 16:18:08 -050079 char serial[20];
80 char model[40];
81 char firmware_rev[8];
Keith Busch8fc23e02012-07-26 11:29:57 -060082 u32 max_hw_sectors;
Keith Busch0e5e4f02012-11-09 16:33:05 -070083 u16 oncs;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050084};
85
86/*
87 * An NVM Express namespace is equivalent to a SCSI LUN
88 */
89struct nvme_ns {
90 struct list_head list;
91
92 struct nvme_dev *dev;
93 struct request_queue *queue;
94 struct gendisk *disk;
95
96 int ns_id;
97 int lba_shift;
98};
99
100/*
101 * An NVM Express queue. Each device has at least two (one for admin
102 * commands and one for I/O commands).
103 */
104struct nvme_queue {
105 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500106 struct nvme_dev *dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500107 spinlock_t q_lock;
108 struct nvme_command *sq_cmds;
109 volatile struct nvme_completion *cqes;
110 dma_addr_t sq_dma_addr;
111 dma_addr_t cq_dma_addr;
112 wait_queue_head_t sq_full;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -0500113 wait_queue_t sq_cong_wait;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500114 struct bio_list sq_cong;
115 u32 __iomem *q_db;
116 u16 q_depth;
117 u16 cq_vector;
118 u16 sq_head;
119 u16 sq_tail;
120 u16 cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500121 u16 cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500122 unsigned long cmdid_data[];
123};
124
125/*
126 * Check we didin't inadvertently grow the command struct
127 */
128static inline void _nvme_check_size(void)
129{
130 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
131 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
132 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
133 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
134 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
135 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
136 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
137 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
138 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600139 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500140}
141
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500142typedef void (*nvme_completion_fn)(struct nvme_dev *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400143 struct nvme_completion *);
144
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500145struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400146 nvme_completion_fn fn;
147 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500148 unsigned long timeout;
149};
150
151static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
152{
153 return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
154}
155
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500156/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400157 * alloc_cmdid() - Allocate a Command ID
158 * @nvmeq: The queue that will be used for this command
159 * @ctx: A pointer that will be passed to the handler
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400160 * @handler: The function to call on completion
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500161 *
162 * Allocate a Command ID for a queue. The data passed in will
163 * be passed to the completion handler. This is implemented by using
164 * the bottom two bits of the ctx pointer to store the handler ID.
165 * Passing in a pointer that's not 4-byte aligned will cause a BUG.
166 * We can change this if it becomes a problem.
Matthew Wilcox184d2942011-05-11 21:36:38 -0400167 *
168 * May be called with local interrupts disabled and the q_lock held,
169 * or with interrupts enabled and no locks held.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500170 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400171static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx,
172 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500173{
Matthew Wilcoxe6d15f72011-02-24 08:49:41 -0500174 int depth = nvmeq->q_depth - 1;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500175 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500176 int cmdid;
177
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500178 do {
179 cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
180 if (cmdid >= depth)
181 return -EBUSY;
182 } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
183
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400184 info[cmdid].fn = handler;
185 info[cmdid].ctx = ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500186 info[cmdid].timeout = jiffies + timeout;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500187 return cmdid;
188}
189
190static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400191 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500192{
193 int cmdid;
194 wait_event_killable(nvmeq->sq_full,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500195 (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500196 return (cmdid < 0) ? -EINTR : cmdid;
197}
198
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400199/* Special values must be less than 0x1000 */
200#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500201#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
202#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
203#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500204#define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500205
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500206static void special_completion(struct nvme_dev *dev, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400207 struct nvme_completion *cqe)
208{
209 if (ctx == CMD_CTX_CANCELLED)
210 return;
211 if (ctx == CMD_CTX_FLUSH)
212 return;
213 if (ctx == CMD_CTX_COMPLETED) {
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500214 dev_warn(&dev->pci_dev->dev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400215 "completed id %d twice on queue %d\n",
216 cqe->command_id, le16_to_cpup(&cqe->sq_id));
217 return;
218 }
219 if (ctx == CMD_CTX_INVALID) {
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500220 dev_warn(&dev->pci_dev->dev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400221 "invalid id %d completed on queue %d\n",
222 cqe->command_id, le16_to_cpup(&cqe->sq_id));
223 return;
224 }
225
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500226 dev_warn(&dev->pci_dev->dev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400227}
228
Matthew Wilcox184d2942011-05-11 21:36:38 -0400229/*
230 * Called with local interrupts disabled and the q_lock held. May not sleep.
231 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400232static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid,
233 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500234{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400235 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500236 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500237
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400238 if (cmdid >= nvmeq->q_depth) {
239 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500240 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400241 }
Keith Busch859361a2012-08-02 14:05:59 -0600242 if (fn)
243 *fn = info[cmdid].fn;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400244 ctx = info[cmdid].ctx;
245 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500246 info[cmdid].ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500247 clear_bit(cmdid, nvmeq->cmdid_data);
248 wake_up(&nvmeq->sq_full);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400249 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500250}
251
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400252static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
253 nvme_completion_fn *fn)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500254{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400255 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500256 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400257 if (fn)
258 *fn = info[cmdid].fn;
259 ctx = info[cmdid].ctx;
260 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500261 info[cmdid].ctx = CMD_CTX_CANCELLED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400262 return ctx;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500263}
264
Matthew Wilcox040a93b2011-12-20 11:04:12 -0500265static struct nvme_queue *get_nvmeq(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500266{
Matthew Wilcox040a93b2011-12-20 11:04:12 -0500267 return dev->queues[get_cpu() + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500268}
269
270static void put_nvmeq(struct nvme_queue *nvmeq)
271{
Matthew Wilcox1b234842011-01-20 13:01:49 -0500272 put_cpu();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500273}
274
275/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400276 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500277 * @nvmeq: The queue to use
278 * @cmd: The command to send
279 *
280 * Safe to use from interrupt context
281 */
282static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
283{
284 unsigned long flags;
285 u16 tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500286 spin_lock_irqsave(&nvmeq->q_lock, flags);
287 tail = nvmeq->sq_tail;
288 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500289 if (++tail == nvmeq->q_depth)
290 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500291 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500292 nvmeq->sq_tail = tail;
293 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
294
295 return 0;
296}
297
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500298/*
299 * The nvme_iod describes the data in an I/O, including the list of PRP
300 * entries. You can't see it in this data structure because C doesn't let
301 * me express that. Use nvme_alloc_iod to ensure there's enough space
302 * allocated to store the PRP list.
303 */
304struct nvme_iod {
305 void *private; /* For the use of the submitter of the I/O */
306 int npages; /* In the PRP list. 0 means small pool in use */
307 int offset; /* Of PRP list */
308 int nents; /* Used in scatterlist */
309 int length; /* Of data, in bytes */
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500310 dma_addr_t first_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500311 struct scatterlist sg[0];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500312};
313
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500314static __le64 **iod_list(struct nvme_iod *iod)
315{
316 return ((void *)iod) + iod->offset;
317}
318
319/*
320 * Will slightly overestimate the number of pages needed. This is OK
321 * as it only leads to a small amount of wasted memory for the lifetime of
322 * the I/O.
323 */
324static int nvme_npages(unsigned size)
325{
326 unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE);
327 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
328}
329
330static struct nvme_iod *
331nvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp)
332{
333 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
334 sizeof(__le64 *) * nvme_npages(nbytes) +
335 sizeof(struct scatterlist) * nseg, gfp);
336
337 if (iod) {
338 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
339 iod->npages = -1;
340 iod->length = nbytes;
Keith Busch2b196032012-11-06 11:59:23 -0700341 iod->nents = 0;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500342 }
343
344 return iod;
345}
346
347static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500348{
349 const int last_prp = PAGE_SIZE / 8 - 1;
350 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500351 __le64 **list = iod_list(iod);
352 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500353
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500354 if (iod->npages == 0)
355 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
356 for (i = 0; i < iod->npages; i++) {
357 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500358 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500359 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500360 prp_dma = next_prp_dma;
361 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500362 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500363}
364
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500365static void requeue_bio(struct nvme_dev *dev, struct bio *bio)
366{
367 struct nvme_queue *nvmeq = get_nvmeq(dev);
368 if (bio_list_empty(&nvmeq->sq_cong))
369 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
370 bio_list_add(&nvmeq->sq_cong, bio);
371 put_nvmeq(nvmeq);
372 wake_up_process(nvme_thread);
373}
374
375static void bio_completion(struct nvme_dev *dev, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500376 struct nvme_completion *cqe)
377{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500378 struct nvme_iod *iod = ctx;
379 struct bio *bio = iod->private;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500380 u16 status = le16_to_cpup(&cqe->status) >> 1;
381
Keith Busch2b196032012-11-06 11:59:23 -0700382 if (iod->nents)
383 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500384 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500385 nvme_free_iod(dev, iod);
Matthew Wilcox09a58f52011-04-28 23:09:09 -0700386 if (status) {
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500387 bio_endio(bio, -EIO);
Matthew Wilcox09a58f52011-04-28 23:09:09 -0700388 } else if (bio->bi_vcnt > bio->bi_idx) {
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500389 requeue_bio(dev, bio);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500390 } else {
391 bio_endio(bio, 0);
392 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500393}
394
Matthew Wilcox184d2942011-05-11 21:36:38 -0400395/* length is in bytes. gfp flags indicates whether we may sleep. */
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500396static int nvme_setup_prps(struct nvme_dev *dev,
397 struct nvme_common_command *cmd, struct nvme_iod *iod,
398 int total_len, gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500399{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500400 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500401 int length = total_len;
402 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500403 int dma_len = sg_dma_len(sg);
404 u64 dma_addr = sg_dma_address(sg);
405 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500406 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500407 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500408 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500409 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500410
411 cmd->prp1 = cpu_to_le64(dma_addr);
412 length -= (PAGE_SIZE - offset);
413 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500414 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500415
416 dma_len -= (PAGE_SIZE - offset);
417 if (dma_len) {
418 dma_addr += (PAGE_SIZE - offset);
419 } else {
420 sg = sg_next(sg);
421 dma_addr = sg_dma_address(sg);
422 dma_len = sg_dma_len(sg);
423 }
424
425 if (length <= PAGE_SIZE) {
426 cmd->prp2 = cpu_to_le64(dma_addr);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500427 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500428 }
429
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500430 nprps = DIV_ROUND_UP(length, PAGE_SIZE);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500431 if (nprps <= (256 / 8)) {
432 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500433 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500434 } else {
435 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500436 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500437 }
438
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400439 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
440 if (!prp_list) {
441 cmd->prp2 = cpu_to_le64(dma_addr);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500442 iod->npages = -1;
443 return (total_len - length) + PAGE_SIZE;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400444 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500445 list[0] = prp_list;
446 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500447 cmd->prp2 = cpu_to_le64(prp_dma);
448 i = 0;
449 for (;;) {
Matthew Wilcox7523d832011-03-16 16:43:40 -0400450 if (i == PAGE_SIZE / 8) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500451 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400452 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500453 if (!prp_list)
454 return total_len - length;
455 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400456 prp_list[0] = old_prp_list[i - 1];
457 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
458 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500459 }
460 prp_list[i++] = cpu_to_le64(dma_addr);
461 dma_len -= PAGE_SIZE;
462 dma_addr += PAGE_SIZE;
463 length -= PAGE_SIZE;
464 if (length <= 0)
465 break;
466 if (dma_len > 0)
467 continue;
468 BUG_ON(dma_len < 0);
469 sg = sg_next(sg);
470 dma_addr = sg_dma_address(sg);
471 dma_len = sg_dma_len(sg);
472 }
473
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500474 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500475}
476
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500477/* NVMe scatterlists require no holes in the virtual address */
478#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
479 (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
480
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500481static int nvme_map_bio(struct device *dev, struct nvme_iod *iod,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500482 struct bio *bio, enum dma_data_direction dma_dir, int psegs)
483{
Matthew Wilcox76830842011-02-10 13:55:39 -0500484 struct bio_vec *bvec, *bvprv = NULL;
485 struct scatterlist *sg = NULL;
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500486 int i, old_idx, length = 0, nsegs = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500487
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500488 sg_init_table(iod->sg, psegs);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500489 old_idx = bio->bi_idx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500490 bio_for_each_segment(bvec, bio, i) {
Matthew Wilcox76830842011-02-10 13:55:39 -0500491 if (bvprv && BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) {
492 sg->length += bvec->bv_len;
493 } else {
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500494 if (bvprv && BIOVEC_NOT_VIRT_MERGEABLE(bvprv, bvec))
495 break;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500496 sg = sg ? sg + 1 : iod->sg;
Matthew Wilcox76830842011-02-10 13:55:39 -0500497 sg_set_page(sg, bvec->bv_page, bvec->bv_len,
498 bvec->bv_offset);
499 nsegs++;
500 }
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500501 length += bvec->bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500502 bvprv = bvec;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500503 }
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500504 bio->bi_idx = i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500505 iod->nents = nsegs;
Matthew Wilcox76830842011-02-10 13:55:39 -0500506 sg_mark_end(sg);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500507 if (dma_map_sg(dev, iod->sg, iod->nents, dma_dir) == 0) {
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500508 bio->bi_idx = old_idx;
509 return -ENOMEM;
510 }
511 return length;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500512}
513
Keith Busch0e5e4f02012-11-09 16:33:05 -0700514/*
515 * We reuse the small pool to allocate the 16-byte range here as it is not
516 * worth having a special pool for these or additional cases to handle freeing
517 * the iod.
518 */
519static int nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
520 struct bio *bio, struct nvme_iod *iod, int cmdid)
521{
522 struct nvme_dsm_range *range;
523 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
524
525 range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC,
526 &iod->first_dma);
527 if (!range)
528 return -ENOMEM;
529
530 iod_list(iod)[0] = (__le64 *)range;
531 iod->npages = 0;
532
533 range->cattr = cpu_to_le32(0);
534 range->nlb = cpu_to_le32(bio->bi_size >> ns->lba_shift);
535 range->slba = cpu_to_le64(bio->bi_sector >> (ns->lba_shift - 9));
536
537 memset(cmnd, 0, sizeof(*cmnd));
538 cmnd->dsm.opcode = nvme_cmd_dsm;
539 cmnd->dsm.command_id = cmdid;
540 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
541 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
542 cmnd->dsm.nr = 0;
543 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
544
545 if (++nvmeq->sq_tail == nvmeq->q_depth)
546 nvmeq->sq_tail = 0;
547 writel(nvmeq->sq_tail, nvmeq->q_db);
548
549 return 0;
550}
551
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500552static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
553 int cmdid)
554{
555 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
556
557 memset(cmnd, 0, sizeof(*cmnd));
558 cmnd->common.opcode = nvme_cmd_flush;
559 cmnd->common.command_id = cmdid;
560 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
561
562 if (++nvmeq->sq_tail == nvmeq->q_depth)
563 nvmeq->sq_tail = 0;
564 writel(nvmeq->sq_tail, nvmeq->q_db);
565
566 return 0;
567}
568
569static int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns)
570{
571 int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH,
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500572 special_completion, NVME_IO_TIMEOUT);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500573 if (unlikely(cmdid < 0))
574 return cmdid;
575
576 return nvme_submit_flush(nvmeq, ns, cmdid);
577}
578
Matthew Wilcox184d2942011-05-11 21:36:38 -0400579/*
580 * Called with local interrupts disabled and the q_lock held. May not sleep.
581 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500582static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
583 struct bio *bio)
584{
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500585 struct nvme_command *cmnd;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500586 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500587 enum dma_data_direction dma_dir;
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500588 int cmdid, length, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500589 u16 control;
590 u32 dsmgmt;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500591 int psegs = bio_phys_segments(ns->queue, bio);
592
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500593 if ((bio->bi_rw & REQ_FLUSH) && psegs) {
594 result = nvme_submit_flush_data(nvmeq, ns);
595 if (result)
596 return result;
597 }
598
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500599 iod = nvme_alloc_iod(psegs, bio->bi_size, GFP_ATOMIC);
600 if (!iod)
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500601 goto nomem;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500602 iod->private = bio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500603
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500604 result = -EBUSY;
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500605 cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500606 if (unlikely(cmdid < 0))
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500607 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500608
Keith Busch0e5e4f02012-11-09 16:33:05 -0700609 if (bio->bi_rw & REQ_DISCARD) {
610 result = nvme_submit_discard(nvmeq, ns, bio, iod, cmdid);
611 if (result)
612 goto free_cmdid;
613 return result;
614 }
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500615 if ((bio->bi_rw & REQ_FLUSH) && !psegs)
616 return nvme_submit_flush(nvmeq, ns, cmdid);
617
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500618 control = 0;
619 if (bio->bi_rw & REQ_FUA)
620 control |= NVME_RW_FUA;
621 if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
622 control |= NVME_RW_LR;
623
624 dsmgmt = 0;
625 if (bio->bi_rw & REQ_RAHEAD)
626 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
627
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500628 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500629
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500630 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500631 if (bio_data_dir(bio)) {
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500632 cmnd->rw.opcode = nvme_cmd_write;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500633 dma_dir = DMA_TO_DEVICE;
634 } else {
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500635 cmnd->rw.opcode = nvme_cmd_read;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500636 dma_dir = DMA_FROM_DEVICE;
637 }
638
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500639 result = nvme_map_bio(nvmeq->q_dmadev, iod, bio, dma_dir, psegs);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500640 if (result < 0)
Keith Busch859361a2012-08-02 14:05:59 -0600641 goto free_cmdid;
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500642 length = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500643
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500644 cmnd->rw.command_id = cmdid;
645 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500646 length = nvme_setup_prps(nvmeq->dev, &cmnd->common, iod, length,
647 GFP_ATOMIC);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500648 cmnd->rw.slba = cpu_to_le64(bio->bi_sector >> (ns->lba_shift - 9));
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500649 cmnd->rw.length = cpu_to_le16((length >> ns->lba_shift) - 1);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500650 cmnd->rw.control = cpu_to_le16(control);
651 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500652
Matthew Wilcoxd8ee9d62011-02-24 08:46:00 -0500653 bio->bi_sector += length >> 9;
654
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500655 if (++nvmeq->sq_tail == nvmeq->q_depth)
656 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500657 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500658
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500659 return 0;
660
Keith Busch859361a2012-08-02 14:05:59 -0600661 free_cmdid:
662 free_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500663 free_iod:
664 nvme_free_iod(nvmeq->dev, iod);
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500665 nomem:
666 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500667}
668
Linus Torvalds93c3d652012-01-18 15:41:27 -0800669static void nvme_make_request(struct request_queue *q, struct bio *bio)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500670{
671 struct nvme_ns *ns = q->queuedata;
Matthew Wilcox040a93b2011-12-20 11:04:12 -0500672 struct nvme_queue *nvmeq = get_nvmeq(ns->dev);
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500673 int result = -EBUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500674
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500675 spin_lock_irq(&nvmeq->q_lock);
676 if (bio_list_empty(&nvmeq->sq_cong))
677 result = nvme_submit_bio_queue(nvmeq, ns, bio);
678 if (unlikely(result)) {
679 if (bio_list_empty(&nvmeq->sq_cong))
680 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500681 bio_list_add(&nvmeq->sq_cong, bio);
682 }
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500683
684 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500685 put_nvmeq(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500686}
687
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500688static irqreturn_t nvme_process_cq(struct nvme_queue *nvmeq)
689{
Matthew Wilcox82123462011-01-20 13:24:06 -0500690 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500691
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500692 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500693 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500694
695 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400696 void *ctx;
697 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500698 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500699 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500700 break;
701 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
702 if (++head == nvmeq->q_depth) {
703 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500704 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500705 }
706
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400707 ctx = free_cmdid(nvmeq, cqe.command_id, &fn);
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500708 fn(nvmeq->dev, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500709 }
710
711 /* If the controller ignores the cq head doorbell and continuously
712 * writes to the queue, it is theoretically possible to wrap around
713 * the queue twice and mistakenly return IRQ_NONE. Linux only
714 * requires that 0.1% of your interrupts are handled, so this isn't
715 * a big problem.
716 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500717 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500718 return IRQ_NONE;
719
Matthew Wilcoxf1938f62011-10-20 17:00:41 -0400720 writel(head, nvmeq->q_db + (1 << nvmeq->dev->db_stride));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500721 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500722 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500723
724 return IRQ_HANDLED;
725}
726
727static irqreturn_t nvme_irq(int irq, void *data)
728{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500729 irqreturn_t result;
730 struct nvme_queue *nvmeq = data;
731 spin_lock(&nvmeq->q_lock);
732 result = nvme_process_cq(nvmeq);
733 spin_unlock(&nvmeq->q_lock);
734 return result;
735}
736
737static irqreturn_t nvme_irq_check(int irq, void *data)
738{
739 struct nvme_queue *nvmeq = data;
740 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
741 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
742 return IRQ_NONE;
743 return IRQ_WAKE_THREAD;
744}
745
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500746static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
747{
748 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400749 cancel_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500750 spin_unlock_irq(&nvmeq->q_lock);
751}
752
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400753struct sync_cmd_info {
754 struct task_struct *task;
755 u32 result;
756 int status;
757};
758
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500759static void sync_completion(struct nvme_dev *dev, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400760 struct nvme_completion *cqe)
761{
762 struct sync_cmd_info *cmdinfo = ctx;
763 cmdinfo->result = le32_to_cpup(&cqe->result);
764 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
765 wake_up_process(cmdinfo->task);
766}
767
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500768/*
769 * Returns 0 on success. If the result is negative, it's a Linux error code;
770 * if the result is positive, it's an NVM Express status code
771 */
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500772static int nvme_submit_sync_cmd(struct nvme_queue *nvmeq,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500773 struct nvme_command *cmd, u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500774{
775 int cmdid;
776 struct sync_cmd_info cmdinfo;
777
778 cmdinfo.task = current;
779 cmdinfo.status = -EINTR;
780
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400781 cmdid = alloc_cmdid_killable(nvmeq, &cmdinfo, sync_completion,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500782 timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500783 if (cmdid < 0)
784 return cmdid;
785 cmd->common.command_id = cmdid;
786
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500787 set_current_state(TASK_KILLABLE);
788 nvme_submit_cmd(nvmeq, cmd);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500789 schedule();
790
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500791 if (cmdinfo.status == -EINTR) {
792 nvme_abort_command(nvmeq, cmdid);
793 return -EINTR;
794 }
795
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500796 if (result)
797 *result = cmdinfo.result;
798
799 return cmdinfo.status;
800}
801
802static int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
803 u32 *result)
804{
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500805 return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500806}
807
808static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
809{
810 int status;
811 struct nvme_command c;
812
813 memset(&c, 0, sizeof(c));
814 c.delete_queue.opcode = opcode;
815 c.delete_queue.qid = cpu_to_le16(id);
816
817 status = nvme_submit_admin_cmd(dev, &c, NULL);
818 if (status)
819 return -EIO;
820 return 0;
821}
822
823static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
824 struct nvme_queue *nvmeq)
825{
826 int status;
827 struct nvme_command c;
828 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
829
830 memset(&c, 0, sizeof(c));
831 c.create_cq.opcode = nvme_admin_create_cq;
832 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
833 c.create_cq.cqid = cpu_to_le16(qid);
834 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
835 c.create_cq.cq_flags = cpu_to_le16(flags);
836 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
837
838 status = nvme_submit_admin_cmd(dev, &c, NULL);
839 if (status)
840 return -EIO;
841 return 0;
842}
843
844static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
845 struct nvme_queue *nvmeq)
846{
847 int status;
848 struct nvme_command c;
849 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
850
851 memset(&c, 0, sizeof(c));
852 c.create_sq.opcode = nvme_admin_create_sq;
853 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
854 c.create_sq.sqid = cpu_to_le16(qid);
855 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
856 c.create_sq.sq_flags = cpu_to_le16(flags);
857 c.create_sq.cqid = cpu_to_le16(qid);
858
859 status = nvme_submit_admin_cmd(dev, &c, NULL);
860 if (status)
861 return -EIO;
862 return 0;
863}
864
865static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
866{
867 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
868}
869
870static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
871{
872 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
873}
874
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400875static int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
876 dma_addr_t dma_addr)
877{
878 struct nvme_command c;
879
880 memset(&c, 0, sizeof(c));
881 c.identify.opcode = nvme_admin_identify;
882 c.identify.nsid = cpu_to_le32(nsid);
883 c.identify.prp1 = cpu_to_le64(dma_addr);
884 c.identify.cns = cpu_to_le32(cns);
885
886 return nvme_submit_admin_cmd(dev, &c, NULL);
887}
888
Keith Busch08df1e02012-09-21 10:52:13 -0600889static int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
890 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400891{
892 struct nvme_command c;
893
894 memset(&c, 0, sizeof(c));
895 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -0600896 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400897 c.features.prp1 = cpu_to_le64(dma_addr);
898 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400899
Keith Busch08df1e02012-09-21 10:52:13 -0600900 return nvme_submit_admin_cmd(dev, &c, result);
Matthew Wilcoxdf348132012-01-11 07:29:56 -0700901}
902
903static int nvme_set_features(struct nvme_dev *dev, unsigned fid,
904 unsigned dword11, dma_addr_t dma_addr, u32 *result)
905{
906 struct nvme_command c;
907
908 memset(&c, 0, sizeof(c));
909 c.features.opcode = nvme_admin_set_features;
910 c.features.prp1 = cpu_to_le64(dma_addr);
911 c.features.fid = cpu_to_le32(fid);
912 c.features.dword11 = cpu_to_le32(dword11);
913
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400914 return nvme_submit_admin_cmd(dev, &c, result);
915}
916
Matthew Wilcoxa09115b2012-08-07 15:56:23 -0400917/**
918 * nvme_cancel_ios - Cancel outstanding I/Os
919 * @queue: The queue to cancel I/Os on
920 * @timeout: True to only cancel I/Os which have timed out
921 */
922static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout)
923{
924 int depth = nvmeq->q_depth - 1;
925 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
926 unsigned long now = jiffies;
927 int cmdid;
928
929 for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
930 void *ctx;
931 nvme_completion_fn fn;
932 static struct nvme_completion cqe = {
933 .status = cpu_to_le16(NVME_SC_ABORT_REQ) << 1,
934 };
935
936 if (timeout && !time_after(now, info[cmdid].timeout))
937 continue;
938 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d\n", cmdid);
939 ctx = cancel_cmdid(nvmeq, cmdid, &fn);
940 fn(nvmeq->dev, ctx, &cqe);
941 }
942}
943
Matthew Wilcox9e866772012-08-03 13:55:56 -0400944static void nvme_free_queue_mem(struct nvme_queue *nvmeq)
945{
946 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
947 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
948 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
949 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
950 kfree(nvmeq);
951}
952
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500953static void nvme_free_queue(struct nvme_dev *dev, int qid)
954{
955 struct nvme_queue *nvmeq = dev->queues[qid];
Matthew Wilcoxaba20802011-03-27 08:52:06 -0400956 int vector = dev->entry[nvmeq->cq_vector].vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500957
Matthew Wilcoxa09115b2012-08-07 15:56:23 -0400958 spin_lock_irq(&nvmeq->q_lock);
959 nvme_cancel_ios(nvmeq, false);
Keith Busch32958742012-08-20 14:57:49 -0600960 while (bio_list_peek(&nvmeq->sq_cong)) {
961 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
962 bio_endio(bio, -EIO);
963 }
Matthew Wilcoxa09115b2012-08-07 15:56:23 -0400964 spin_unlock_irq(&nvmeq->q_lock);
965
Matthew Wilcoxaba20802011-03-27 08:52:06 -0400966 irq_set_affinity_hint(vector, NULL);
967 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500968
969 /* Don't tell the adapter to delete the admin queue */
970 if (qid) {
971 adapter_delete_sq(dev, qid);
972 adapter_delete_cq(dev, qid);
973 }
974
Matthew Wilcox9e866772012-08-03 13:55:56 -0400975 nvme_free_queue_mem(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500976}
977
978static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
979 int depth, int vector)
980{
981 struct device *dmadev = &dev->pci_dev->dev;
Keith Buscha0cadb82012-07-27 13:57:23 -0400982 unsigned extra = DIV_ROUND_UP(depth, 8) + (depth *
983 sizeof(struct nvme_cmd_info));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500984 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
985 if (!nvmeq)
986 return NULL;
987
988 nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
989 &nvmeq->cq_dma_addr, GFP_KERNEL);
990 if (!nvmeq->cqes)
991 goto free_nvmeq;
992 memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
993
994 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
995 &nvmeq->sq_dma_addr, GFP_KERNEL);
996 if (!nvmeq->sq_cmds)
997 goto free_cqdma;
998
999 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001000 nvmeq->dev = dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001001 spin_lock_init(&nvmeq->q_lock);
1002 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001003 nvmeq->cq_phase = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001004 init_waitqueue_head(&nvmeq->sq_full);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001005 init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001006 bio_list_init(&nvmeq->sq_cong);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001007 nvmeq->q_db = &dev->dbs[qid << (dev->db_stride + 1)];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001008 nvmeq->q_depth = depth;
1009 nvmeq->cq_vector = vector;
1010
1011 return nvmeq;
1012
1013 free_cqdma:
1014 dma_free_coherent(dmadev, CQ_SIZE(nvmeq->q_depth), (void *)nvmeq->cqes,
1015 nvmeq->cq_dma_addr);
1016 free_nvmeq:
1017 kfree(nvmeq);
1018 return NULL;
1019}
1020
Matthew Wilcox30010822011-01-20 09:10:15 -05001021static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1022 const char *name)
1023{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001024 if (use_threaded_interrupts)
1025 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Matthew Wilcoxec6ce612011-02-06 09:01:00 -05001026 nvme_irq_check, nvme_irq,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001027 IRQF_DISABLED | IRQF_SHARED,
1028 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001029 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
1030 IRQF_DISABLED | IRQF_SHARED, name, nvmeq);
1031}
1032
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001033static struct nvme_queue *nvme_create_queue(struct nvme_dev *dev, int qid,
1034 int cq_size, int vector)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001035{
1036 int result;
1037 struct nvme_queue *nvmeq = nvme_alloc_queue(dev, qid, cq_size, vector);
1038
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001039 if (!nvmeq)
Matthew Wilcox6f0f5442011-05-11 13:30:59 -07001040 return ERR_PTR(-ENOMEM);
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001041
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001042 result = adapter_alloc_cq(dev, qid, nvmeq);
1043 if (result < 0)
1044 goto free_nvmeq;
1045
1046 result = adapter_alloc_sq(dev, qid, nvmeq);
1047 if (result < 0)
1048 goto release_cq;
1049
Matthew Wilcox30010822011-01-20 09:10:15 -05001050 result = queue_request_irq(dev, nvmeq, "nvme");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001051 if (result < 0)
1052 goto release_sq;
1053
1054 return nvmeq;
1055
1056 release_sq:
1057 adapter_delete_sq(dev, qid);
1058 release_cq:
1059 adapter_delete_cq(dev, qid);
1060 free_nvmeq:
1061 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1062 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1063 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1064 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1065 kfree(nvmeq);
Matthew Wilcox6f0f5442011-05-11 13:30:59 -07001066 return ERR_PTR(result);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001067}
1068
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001069static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001070{
Matthew Wilcox9e866772012-08-03 13:55:56 -04001071 int result = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001072 u32 aqa;
Matthew Wilcox22605f92011-04-19 15:04:20 -04001073 u64 cap;
1074 unsigned long timeout;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001075 struct nvme_queue *nvmeq;
1076
1077 dev->dbs = ((void __iomem *)dev->bar) + 4096;
1078
1079 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001080 if (!nvmeq)
1081 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001082
1083 aqa = nvmeq->q_depth - 1;
1084 aqa |= aqa << 16;
1085
1086 dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
1087 dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
1088 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001089 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001090
Shane Michael Matthews5911f202011-02-01 11:31:55 -05001091 writel(0, &dev->bar->cc);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001092 writel(aqa, &dev->bar->aqa);
1093 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1094 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
1095 writel(dev->ctrl_config, &dev->bar->cc);
1096
Matthew Wilcox22605f92011-04-19 15:04:20 -04001097 cap = readq(&dev->bar->cap);
1098 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001099 dev->db_stride = NVME_CAP_STRIDE(cap);
Matthew Wilcox22605f92011-04-19 15:04:20 -04001100
Matthew Wilcox9e866772012-08-03 13:55:56 -04001101 while (!result && !(readl(&dev->bar->csts) & NVME_CSTS_RDY)) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001102 msleep(100);
1103 if (fatal_signal_pending(current))
Matthew Wilcox9e866772012-08-03 13:55:56 -04001104 result = -EINTR;
Matthew Wilcox22605f92011-04-19 15:04:20 -04001105 if (time_after(jiffies, timeout)) {
1106 dev_err(&dev->pci_dev->dev,
1107 "Device not ready; aborting initialisation\n");
Matthew Wilcox9e866772012-08-03 13:55:56 -04001108 result = -ENODEV;
Matthew Wilcox22605f92011-04-19 15:04:20 -04001109 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001110 }
1111
Matthew Wilcox9e866772012-08-03 13:55:56 -04001112 if (result) {
1113 nvme_free_queue_mem(nvmeq);
1114 return result;
1115 }
1116
Matthew Wilcox30010822011-01-20 09:10:15 -05001117 result = queue_request_irq(dev, nvmeq, "nvme admin");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001118 dev->queues[0] = nvmeq;
1119 return result;
1120}
1121
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001122static struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
1123 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001124{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001125 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001126 struct scatterlist *sg;
1127 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001128 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001129
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001130 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001131 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001132 if (!length)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001133 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001134
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001135 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001136 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1137 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001138 if (!pages)
1139 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001140
1141 err = get_user_pages_fast(addr, count, 1, pages);
1142 if (err < count) {
1143 count = err;
1144 err = -EFAULT;
1145 goto put_pages;
1146 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001147
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001148 iod = nvme_alloc_iod(count, length, GFP_KERNEL);
1149 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001150 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001151 for (i = 0; i < count; i++) {
1152 sg_set_page(&sg[i], pages[i],
1153 min_t(int, length, PAGE_SIZE - offset), offset);
1154 length -= (PAGE_SIZE - offset);
1155 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001156 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001157 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001158 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001159
1160 err = -ENOMEM;
1161 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1162 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001163 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001164 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001165
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001166 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001167 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001168
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001169 free_iod:
1170 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001171 put_pages:
1172 for (i = 0; i < count; i++)
1173 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001174 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001175 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001176}
1177
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001178static void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001179 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001180{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001181 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001182
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001183 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1184 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001185
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001186 for (i = 0; i < iod->nents; i++)
1187 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001188}
1189
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001190static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1191{
1192 struct nvme_dev *dev = ns->dev;
1193 struct nvme_queue *nvmeq;
1194 struct nvme_user_io io;
1195 struct nvme_command c;
1196 unsigned length;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001197 int status;
1198 struct nvme_iod *iod;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001199
1200 if (copy_from_user(&io, uio, sizeof(io)))
1201 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001202 length = (io.nblocks + 1) << ns->lba_shift;
1203
1204 switch (io.opcode) {
1205 case nvme_cmd_write:
1206 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001207 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001208 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001209 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001210 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001211 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001212 }
1213
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001214 if (IS_ERR(iod))
1215 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001216
1217 memset(&c, 0, sizeof(c));
1218 c.rw.opcode = io.opcode;
1219 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001220 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001221 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001222 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001223 c.rw.control = cpu_to_le16(io.control);
1224 c.rw.dsmgmt = cpu_to_le16(io.dsmgmt);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001225 c.rw.reftag = io.reftag;
1226 c.rw.apptag = io.apptag;
1227 c.rw.appmask = io.appmask;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001228 /* XXX: metadata */
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001229 length = nvme_setup_prps(dev, &c.common, iod, length, GFP_KERNEL);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001230
Matthew Wilcox040a93b2011-12-20 11:04:12 -05001231 nvmeq = get_nvmeq(dev);
Matthew Wilcoxfa922822011-03-16 16:29:00 -04001232 /*
1233 * Since nvme_submit_sync_cmd sleeps, we can't keep preemption
Matthew Wilcoxb1ad37e2011-02-04 16:14:30 -05001234 * disabled. We may be preempted at any point, and be rescheduled
1235 * to a different CPU. That will cause cacheline bouncing, but no
1236 * additional races since q_lock already protects against other CPUs.
1237 */
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001238 put_nvmeq(nvmeq);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001239 if (length != (io.nblocks + 1) << ns->lba_shift)
1240 status = -ENOMEM;
1241 else
Matthew Wilcoxff976d72011-12-20 13:53:01 -05001242 status = nvme_submit_sync_cmd(nvmeq, &c, NULL, NVME_IO_TIMEOUT);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001243
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001244 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001245 nvme_free_iod(dev, iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001246 return status;
1247}
1248
Keith Busch50af8ba2012-07-25 16:07:55 -06001249static int nvme_user_admin_cmd(struct nvme_dev *dev,
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001250 struct nvme_admin_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001251{
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001252 struct nvme_admin_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001253 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001254 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001255 struct nvme_iod *uninitialized_var(iod);
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001256
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001257 if (!capable(CAP_SYS_ADMIN))
1258 return -EACCES;
1259 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001260 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001261
1262 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001263 c.common.opcode = cmd.opcode;
1264 c.common.flags = cmd.flags;
1265 c.common.nsid = cpu_to_le32(cmd.nsid);
1266 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1267 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1268 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1269 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1270 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1271 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1272 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1273 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1274
1275 length = cmd.data_len;
1276 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001277 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1278 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001279 if (IS_ERR(iod))
1280 return PTR_ERR(iod);
1281 length = nvme_setup_prps(dev, &c.common, iod, length,
1282 GFP_KERNEL);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001283 }
1284
1285 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001286 status = -ENOMEM;
1287 else
Keith Buschf4f117f2012-09-21 10:49:05 -06001288 status = nvme_submit_admin_cmd(dev, &c, &cmd.result);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001289
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001290 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001291 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001292 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001293 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001294
1295 if (!status && copy_to_user(&ucmd->result, &cmd.result,
1296 sizeof(cmd.result)))
1297 status = -EFAULT;
1298
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001299 return status;
1300}
1301
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001302static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1303 unsigned long arg)
1304{
1305 struct nvme_ns *ns = bdev->bd_disk->private_data;
1306
1307 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001308 case NVME_IOCTL_ID:
1309 return ns->ns_id;
1310 case NVME_IOCTL_ADMIN_CMD:
Keith Busch50af8ba2012-07-25 16:07:55 -06001311 return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001312 case NVME_IOCTL_SUBMIT_IO:
1313 return nvme_submit_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001314 default:
1315 return -ENOTTY;
1316 }
1317}
1318
1319static const struct block_device_operations nvme_fops = {
1320 .owner = THIS_MODULE,
1321 .ioctl = nvme_ioctl,
Matthew Wilcox49481682011-03-19 14:55:38 -04001322 .compat_ioctl = nvme_ioctl,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001323};
1324
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001325static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1326{
1327 while (bio_list_peek(&nvmeq->sq_cong)) {
1328 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1329 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
1330 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
1331 bio_list_add_head(&nvmeq->sq_cong, bio);
1332 break;
1333 }
Matthew Wilcox3cb967c2011-03-16 16:45:49 -04001334 if (bio_list_empty(&nvmeq->sq_cong))
1335 remove_wait_queue(&nvmeq->sq_full,
1336 &nvmeq->sq_cong_wait);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001337 }
1338}
1339
1340static int nvme_kthread(void *data)
1341{
1342 struct nvme_dev *dev;
1343
1344 while (!kthread_should_stop()) {
1345 __set_current_state(TASK_RUNNING);
1346 spin_lock(&dev_list_lock);
1347 list_for_each_entry(dev, &dev_list, node) {
1348 int i;
1349 for (i = 0; i < dev->queue_count; i++) {
1350 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001351 if (!nvmeq)
1352 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001353 spin_lock_irq(&nvmeq->q_lock);
1354 if (nvme_process_cq(nvmeq))
1355 printk("process_cq did something\n");
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001356 nvme_cancel_ios(nvmeq, true);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001357 nvme_resubmit_bios(nvmeq);
1358 spin_unlock_irq(&nvmeq->q_lock);
1359 }
1360 }
1361 spin_unlock(&dev_list_lock);
1362 set_current_state(TASK_INTERRUPTIBLE);
1363 schedule_timeout(HZ);
1364 }
1365 return 0;
1366}
1367
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001368static DEFINE_IDA(nvme_index_ida);
1369
1370static int nvme_get_ns_idx(void)
1371{
1372 int index, error;
1373
1374 do {
1375 if (!ida_pre_get(&nvme_index_ida, GFP_KERNEL))
1376 return -1;
1377
1378 spin_lock(&dev_list_lock);
1379 error = ida_get_new(&nvme_index_ida, &index);
1380 spin_unlock(&dev_list_lock);
1381 } while (error == -EAGAIN);
1382
1383 if (error)
1384 index = -1;
1385 return index;
1386}
1387
1388static void nvme_put_ns_idx(int index)
1389{
1390 spin_lock(&dev_list_lock);
1391 ida_remove(&nvme_index_ida, index);
1392 spin_unlock(&dev_list_lock);
1393}
1394
Keith Busch0e5e4f02012-11-09 16:33:05 -07001395static void nvme_config_discard(struct nvme_ns *ns)
1396{
1397 u32 logical_block_size = queue_logical_block_size(ns->queue);
1398 ns->queue->limits.discard_zeroes_data = 0;
1399 ns->queue->limits.discard_alignment = logical_block_size;
1400 ns->queue->limits.discard_granularity = logical_block_size;
1401 ns->queue->limits.max_discard_sectors = 0xffffffff;
1402 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1403}
1404
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001405static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, int nsid,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001406 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1407{
1408 struct nvme_ns *ns;
1409 struct gendisk *disk;
1410 int lbaf;
1411
1412 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1413 return NULL;
1414
1415 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1416 if (!ns)
1417 return NULL;
1418 ns->queue = blk_alloc_queue(GFP_KERNEL);
1419 if (!ns->queue)
1420 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001421 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
1422 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1423 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001424 blk_queue_make_request(ns->queue, nvme_make_request);
1425 ns->dev = dev;
1426 ns->queue->queuedata = ns;
1427
1428 disk = alloc_disk(NVME_MINORS);
1429 if (!disk)
1430 goto out_free_queue;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001431 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001432 ns->disk = disk;
1433 lbaf = id->flbas & 0xf;
1434 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Busche9ef4632012-07-24 15:01:04 -06001435 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06001436 if (dev->max_hw_sectors)
1437 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001438
1439 disk->major = nvme_major;
1440 disk->minors = NVME_MINORS;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001441 disk->first_minor = NVME_MINORS * nvme_get_ns_idx();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001442 disk->fops = &nvme_fops;
1443 disk->private_data = ns;
1444 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05001445 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001446 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001447 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1448
Keith Busch0e5e4f02012-11-09 16:33:05 -07001449 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1450 nvme_config_discard(ns);
1451
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001452 return ns;
1453
1454 out_free_queue:
1455 blk_cleanup_queue(ns->queue);
1456 out_free_ns:
1457 kfree(ns);
1458 return NULL;
1459}
1460
1461static void nvme_ns_free(struct nvme_ns *ns)
1462{
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001463 int index = ns->disk->first_minor / NVME_MINORS;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001464 put_disk(ns->disk);
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001465 nvme_put_ns_idx(index);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001466 blk_cleanup_queue(ns->queue);
1467 kfree(ns);
1468}
1469
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001470static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001471{
1472 int status;
1473 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001474 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001475
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001476 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001477 &result);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001478 if (status)
1479 return -EIO;
1480 return min(result & 0xffff, result >> 16) + 1;
1481}
1482
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001483static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001484{
Keith Buscha0cadb82012-07-27 13:57:23 -04001485 int result, cpu, i, nr_io_queues, db_bar_size, q_depth;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001486
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001487 nr_io_queues = num_online_cpus();
1488 result = set_queue_count(dev, nr_io_queues);
Matthew Wilcox1b234842011-01-20 13:01:49 -05001489 if (result < 0)
1490 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001491 if (result < nr_io_queues)
1492 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001493
Matthew Wilcox1b234842011-01-20 13:01:49 -05001494 /* Deregister the admin queue's interrupt */
1495 free_irq(dev->entry[0].vector, dev->queues[0]);
1496
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001497 db_bar_size = 4096 + ((nr_io_queues + 1) << (dev->db_stride + 3));
1498 if (db_bar_size > 8192) {
1499 iounmap(dev->bar);
1500 dev->bar = ioremap(pci_resource_start(dev->pci_dev, 0),
1501 db_bar_size);
1502 dev->dbs = ((void __iomem *)dev->bar) + 4096;
1503 dev->queues[0]->q_db = dev->dbs;
1504 }
1505
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001506 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001507 dev->entry[i].entry = i;
1508 for (;;) {
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001509 result = pci_enable_msix(dev->pci_dev, dev->entry,
1510 nr_io_queues);
Matthew Wilcox1b234842011-01-20 13:01:49 -05001511 if (result == 0) {
1512 break;
1513 } else if (result > 0) {
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001514 nr_io_queues = result;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001515 continue;
1516 } else {
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001517 nr_io_queues = 1;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001518 break;
1519 }
1520 }
1521
1522 result = queue_request_irq(dev, dev->queues[0], "nvme admin");
1523 /* XXX: handle failure here */
1524
1525 cpu = cpumask_first(cpu_online_mask);
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001526 for (i = 0; i < nr_io_queues; i++) {
Matthew Wilcox1b234842011-01-20 13:01:49 -05001527 irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu));
1528 cpu = cpumask_next(cpu, cpu_online_mask);
1529 }
1530
Keith Buscha0cadb82012-07-27 13:57:23 -04001531 q_depth = min_t(int, NVME_CAP_MQES(readq(&dev->bar->cap)) + 1,
1532 NVME_Q_DEPTH);
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001533 for (i = 0; i < nr_io_queues; i++) {
Keith Buscha0cadb82012-07-27 13:57:23 -04001534 dev->queues[i + 1] = nvme_create_queue(dev, i + 1, q_depth, i);
Matthew Wilcox6f0f5442011-05-11 13:30:59 -07001535 if (IS_ERR(dev->queues[i + 1]))
1536 return PTR_ERR(dev->queues[i + 1]);
Matthew Wilcox1b234842011-01-20 13:01:49 -05001537 dev->queue_count++;
1538 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001539
Matthew Wilcox9ecdc942011-03-16 16:52:19 -04001540 for (; i < num_possible_cpus(); i++) {
1541 int target = i % rounddown_pow_of_two(dev->queue_count - 1);
1542 dev->queues[i + 1] = dev->queues[target + 1];
1543 }
1544
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001545 return 0;
1546}
1547
1548static void nvme_free_queues(struct nvme_dev *dev)
1549{
1550 int i;
1551
1552 for (i = dev->queue_count - 1; i >= 0; i--)
1553 nvme_free_queue(dev, i);
1554}
1555
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001556static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001557{
1558 int res, nn, i;
1559 struct nvme_ns *ns, *next;
Matthew Wilcox51814232011-02-01 16:18:08 -05001560 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001561 struct nvme_id_ns *id_ns;
1562 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001563 dma_addr_t dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001564
1565 res = nvme_setup_io_queues(dev);
1566 if (res)
1567 return res;
1568
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001569 mem = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001570 GFP_KERNEL);
1571
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001572 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001573 if (res) {
1574 res = -EIO;
1575 goto out_free;
1576 }
1577
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001578 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05001579 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07001580 dev->oncs = le16_to_cpup(&ctrl->oncs);
Matthew Wilcox51814232011-02-01 16:18:08 -05001581 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
1582 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
1583 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch8fc23e02012-07-26 11:29:57 -06001584 if (ctrl->mdts) {
1585 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
1586 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
1587 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001588
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001589 id_ns = mem;
Matthew Wilcox2b2c1892011-10-07 13:10:13 -04001590 for (i = 1; i <= nn; i++) {
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001591 res = nvme_identify(dev, i, 0, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001592 if (res)
1593 continue;
1594
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001595 if (id_ns->ncap == 0)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001596 continue;
1597
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001598 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
Keith Busch08df1e02012-09-21 10:52:13 -06001599 dma_addr + 4096, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001600 if (res)
Keith Busch12209032013-01-31 14:40:38 -07001601 memset(mem + 4096, 0, 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001602
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001603 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001604 if (ns)
1605 list_add_tail(&ns->list, &dev->namespaces);
1606 }
1607 list_for_each_entry(ns, &dev->namespaces, list)
1608 add_disk(ns->disk);
1609
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001610 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001611
1612 out_free:
1613 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
1614 list_del(&ns->list);
1615 nvme_ns_free(ns);
1616 }
1617
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001618 out:
Matthew Wilcox684f5c22011-09-19 17:14:53 -04001619 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001620 return res;
1621}
1622
1623static int nvme_dev_remove(struct nvme_dev *dev)
1624{
1625 struct nvme_ns *ns, *next;
1626
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001627 spin_lock(&dev_list_lock);
1628 list_del(&dev->node);
1629 spin_unlock(&dev_list_lock);
1630
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001631 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
1632 list_del(&ns->list);
1633 del_gendisk(ns->disk);
1634 nvme_ns_free(ns);
1635 }
1636
1637 nvme_free_queues(dev);
1638
1639 return 0;
1640}
1641
Matthew Wilcox091b6092011-02-10 09:56:01 -05001642static int nvme_setup_prp_pools(struct nvme_dev *dev)
1643{
1644 struct device *dmadev = &dev->pci_dev->dev;
1645 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
1646 PAGE_SIZE, PAGE_SIZE, 0);
1647 if (!dev->prp_page_pool)
1648 return -ENOMEM;
1649
Matthew Wilcox99802a72011-02-10 10:30:34 -05001650 /* Optimisation for I/Os between 4k and 128k */
1651 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
1652 256, 256, 0);
1653 if (!dev->prp_small_pool) {
1654 dma_pool_destroy(dev->prp_page_pool);
1655 return -ENOMEM;
1656 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05001657 return 0;
1658}
1659
1660static void nvme_release_prp_pools(struct nvme_dev *dev)
1661{
1662 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05001663 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05001664}
1665
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07001666static DEFINE_IDA(nvme_instance_ida);
1667
1668static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001669{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07001670 int instance, error;
1671
1672 do {
1673 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
1674 return -ENODEV;
1675
1676 spin_lock(&dev_list_lock);
1677 error = ida_get_new(&nvme_instance_ida, &instance);
1678 spin_unlock(&dev_list_lock);
1679 } while (error == -EAGAIN);
1680
1681 if (error)
1682 return -ENODEV;
1683
1684 dev->instance = instance;
1685 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001686}
1687
1688static void nvme_release_instance(struct nvme_dev *dev)
1689{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07001690 spin_lock(&dev_list_lock);
1691 ida_remove(&nvme_instance_ida, dev->instance);
1692 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001693}
1694
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001695static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001696{
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001697 int bars, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001698 struct nvme_dev *dev;
1699
1700 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1701 if (!dev)
1702 return -ENOMEM;
1703 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
1704 GFP_KERNEL);
1705 if (!dev->entry)
1706 goto free;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001707 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
1708 GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001709 if (!dev->queues)
1710 goto free;
1711
Shane Michael Matthews0ee5a7d2011-02-01 08:49:30 -05001712 if (pci_enable_device_mem(pdev))
1713 goto free;
Matthew Wilcoxf64d3362011-02-01 09:01:59 -05001714 pci_set_master(pdev);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001715 bars = pci_select_bars(pdev, IORESOURCE_MEM);
1716 if (pci_request_selected_regions(pdev, bars, "nvme"))
1717 goto disable;
Shane Michael Matthews0ee5a7d2011-02-01 08:49:30 -05001718
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001719 INIT_LIST_HEAD(&dev->namespaces);
1720 dev->pci_dev = pdev;
1721 pci_set_drvdata(pdev, dev);
Matthew Wilcox29303532011-02-01 16:23:39 -05001722 dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
1723 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07001724 result = nvme_set_instance(dev);
1725 if (result)
1726 goto disable;
1727
Matthew Wilcox53c95772011-01-20 13:42:34 -05001728 dev->entry[0].vector = pdev->irq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001729
Matthew Wilcox091b6092011-02-10 09:56:01 -05001730 result = nvme_setup_prp_pools(dev);
1731 if (result)
1732 goto disable_msix;
1733
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001734 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1735 if (!dev->bar) {
1736 result = -ENOMEM;
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001737 goto disable_msix;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001738 }
1739
1740 result = nvme_configure_admin_queue(dev);
1741 if (result)
1742 goto unmap;
1743 dev->queue_count++;
1744
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001745 spin_lock(&dev_list_lock);
1746 list_add(&dev->node, &dev_list);
1747 spin_unlock(&dev_list_lock);
1748
Matthew Wilcox740216f2011-02-15 16:28:20 -05001749 result = nvme_dev_add(dev);
1750 if (result)
1751 goto delete;
1752
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001753 return 0;
1754
1755 delete:
Matthew Wilcox740216f2011-02-15 16:28:20 -05001756 spin_lock(&dev_list_lock);
1757 list_del(&dev->node);
1758 spin_unlock(&dev_list_lock);
1759
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001760 nvme_free_queues(dev);
1761 unmap:
1762 iounmap(dev->bar);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001763 disable_msix:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001764 pci_disable_msix(pdev);
1765 nvme_release_instance(dev);
Matthew Wilcox091b6092011-02-10 09:56:01 -05001766 nvme_release_prp_pools(dev);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001767 disable:
Shane Michael Matthews0ee5a7d2011-02-01 08:49:30 -05001768 pci_disable_device(pdev);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001769 pci_release_regions(pdev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001770 free:
1771 kfree(dev->queues);
1772 kfree(dev->entry);
1773 kfree(dev);
1774 return result;
1775}
1776
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001777static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001778{
1779 struct nvme_dev *dev = pci_get_drvdata(pdev);
1780 nvme_dev_remove(dev);
1781 pci_disable_msix(pdev);
1782 iounmap(dev->bar);
1783 nvme_release_instance(dev);
Matthew Wilcox091b6092011-02-10 09:56:01 -05001784 nvme_release_prp_pools(dev);
Shane Michael Matthews0ee5a7d2011-02-01 08:49:30 -05001785 pci_disable_device(pdev);
Matthew Wilcox574e8b92011-02-01 16:24:35 -05001786 pci_release_regions(pdev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001787 kfree(dev->queues);
1788 kfree(dev->entry);
1789 kfree(dev);
1790}
1791
1792/* These functions are yet to be implemented */
1793#define nvme_error_detected NULL
1794#define nvme_dump_registers NULL
1795#define nvme_link_reset NULL
1796#define nvme_slot_reset NULL
1797#define nvme_error_resume NULL
1798#define nvme_suspend NULL
1799#define nvme_resume NULL
1800
Stephen Hemminger1d352032012-09-07 09:33:17 -07001801static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001802 .error_detected = nvme_error_detected,
1803 .mmio_enabled = nvme_dump_registers,
1804 .link_reset = nvme_link_reset,
1805 .slot_reset = nvme_slot_reset,
1806 .resume = nvme_error_resume,
1807};
1808
1809/* Move to pci_ids.h later */
1810#define PCI_CLASS_STORAGE_EXPRESS 0x010802
1811
1812static DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
1813 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
1814 { 0, }
1815};
1816MODULE_DEVICE_TABLE(pci, nvme_id_table);
1817
1818static struct pci_driver nvme_driver = {
1819 .name = "nvme",
1820 .id_table = nvme_id_table,
1821 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001822 .remove = nvme_remove,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001823 .suspend = nvme_suspend,
1824 .resume = nvme_resume,
1825 .err_handler = &nvme_err_handler,
1826};
1827
1828static int __init nvme_init(void)
1829{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04001830 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001831
1832 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
1833 if (IS_ERR(nvme_thread))
1834 return PTR_ERR(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001835
Keith Busch5c42ea12012-07-25 16:05:18 -06001836 result = register_blkdev(nvme_major, "nvme");
1837 if (result < 0)
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001838 goto kill_kthread;
Keith Busch5c42ea12012-07-25 16:05:18 -06001839 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04001840 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001841
1842 result = pci_register_driver(&nvme_driver);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001843 if (result)
1844 goto unregister_blkdev;
1845 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001846
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001847 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001848 unregister_blkdev(nvme_major, "nvme");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001849 kill_kthread:
1850 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001851 return result;
1852}
1853
1854static void __exit nvme_exit(void)
1855{
1856 pci_unregister_driver(&nvme_driver);
1857 unregister_blkdev(nvme_major, "nvme");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001858 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001859}
1860
1861MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
1862MODULE_LICENSE("GPL");
Matthew Wilcox366e8212012-01-10 16:30:15 -05001863MODULE_VERSION("0.8");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001864module_init(nvme_init);
1865module_exit(nvme_exit);