]> git.ipfire.org Git - people/arne_f/kernel.git/blame - drivers/block/nvme-core.c
NVMe: Schedule timeout for sync commands
[people/arne_f/kernel.git] / drivers / block / nvme-core.c
CommitLineData
b60503ba
MW
1/*
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>
8de05535 21#include <linux/bitops.h>
b60503ba 22#include <linux/blkdev.h>
fd63e9ce 23#include <linux/delay.h>
b60503ba
MW
24#include <linux/errno.h>
25#include <linux/fs.h>
26#include <linux/genhd.h>
5aff9382 27#include <linux/idr.h>
b60503ba
MW
28#include <linux/init.h>
29#include <linux/interrupt.h>
30#include <linux/io.h>
31#include <linux/kdev_t.h>
1fa6aead 32#include <linux/kthread.h>
b60503ba
MW
33#include <linux/kernel.h>
34#include <linux/mm.h>
35#include <linux/module.h>
36#include <linux/moduleparam.h>
37#include <linux/pci.h>
be7b6275 38#include <linux/poison.h>
b60503ba
MW
39#include <linux/sched.h>
40#include <linux/slab.h>
41#include <linux/types.h>
5d0f6131 42#include <scsi/sg.h>
797a796a
HM
43#include <asm-generic/io-64-nonatomic-lo-hi.h>
44
b60503ba
MW
45#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
e85248e5 49#define ADMIN_TIMEOUT (60 * HZ)
b60503ba
MW
50
51static int nvme_major;
52module_param(nvme_major, int, 0);
53
58ffacb5
MW
54static int use_threaded_interrupts;
55module_param(use_threaded_interrupts, int, 0);
56
1fa6aead
MW
57static DEFINE_SPINLOCK(dev_list_lock);
58static LIST_HEAD(dev_list);
59static struct task_struct *nvme_thread;
60
b60503ba
MW
61/*
62 * An NVM Express queue. Each device has at least two (one for admin
63 * commands and one for I/O commands).
64 */
65struct nvme_queue {
66 struct device *q_dmadev;
091b6092 67 struct nvme_dev *dev;
b60503ba
MW
68 spinlock_t q_lock;
69 struct nvme_command *sq_cmds;
70 volatile struct nvme_completion *cqes;
71 dma_addr_t sq_dma_addr;
72 dma_addr_t cq_dma_addr;
73 wait_queue_head_t sq_full;
1fa6aead 74 wait_queue_t sq_cong_wait;
b60503ba
MW
75 struct bio_list sq_cong;
76 u32 __iomem *q_db;
77 u16 q_depth;
78 u16 cq_vector;
79 u16 sq_head;
80 u16 sq_tail;
81 u16 cq_head;
82123460 82 u16 cq_phase;
b60503ba
MW
83 unsigned long cmdid_data[];
84};
85
86/*
87 * Check we didin't inadvertently grow the command struct
88 */
89static inline void _nvme_check_size(void)
90{
91 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
92 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
93 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
94 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
95 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
f8ebf840 96 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
b60503ba
MW
97 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
98 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
99 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
100 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
6ecec745 101 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
b60503ba
MW
102}
103
5c1281a3 104typedef void (*nvme_completion_fn)(struct nvme_dev *, void *,
c2f5b650
MW
105 struct nvme_completion *);
106
e85248e5 107struct nvme_cmd_info {
c2f5b650
MW
108 nvme_completion_fn fn;
109 void *ctx;
e85248e5
MW
110 unsigned long timeout;
111};
112
113static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
114{
115 return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
116}
117
b60503ba 118/**
714a7a22
MW
119 * alloc_cmdid() - Allocate a Command ID
120 * @nvmeq: The queue that will be used for this command
121 * @ctx: A pointer that will be passed to the handler
c2f5b650 122 * @handler: The function to call on completion
b60503ba
MW
123 *
124 * Allocate a Command ID for a queue. The data passed in will
125 * be passed to the completion handler. This is implemented by using
126 * the bottom two bits of the ctx pointer to store the handler ID.
127 * Passing in a pointer that's not 4-byte aligned will cause a BUG.
128 * We can change this if it becomes a problem.
184d2944
MW
129 *
130 * May be called with local interrupts disabled and the q_lock held,
131 * or with interrupts enabled and no locks held.
b60503ba 132 */
c2f5b650
MW
133static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx,
134 nvme_completion_fn handler, unsigned timeout)
b60503ba 135{
e6d15f79 136 int depth = nvmeq->q_depth - 1;
e85248e5 137 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
b60503ba
MW
138 int cmdid;
139
b60503ba
MW
140 do {
141 cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
142 if (cmdid >= depth)
143 return -EBUSY;
144 } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
145
c2f5b650
MW
146 info[cmdid].fn = handler;
147 info[cmdid].ctx = ctx;
e85248e5 148 info[cmdid].timeout = jiffies + timeout;
b60503ba
MW
149 return cmdid;
150}
151
152static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
c2f5b650 153 nvme_completion_fn handler, unsigned timeout)
b60503ba
MW
154{
155 int cmdid;
156 wait_event_killable(nvmeq->sq_full,
e85248e5 157 (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
b60503ba
MW
158 return (cmdid < 0) ? -EINTR : cmdid;
159}
160
c2f5b650
MW
161/* Special values must be less than 0x1000 */
162#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
d2d87034
MW
163#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
164#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
165#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
00df5cb4 166#define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE)
be7b6275 167
5c1281a3 168static void special_completion(struct nvme_dev *dev, void *ctx,
c2f5b650
MW
169 struct nvme_completion *cqe)
170{
171 if (ctx == CMD_CTX_CANCELLED)
172 return;
173 if (ctx == CMD_CTX_FLUSH)
174 return;
175 if (ctx == CMD_CTX_COMPLETED) {
5c1281a3 176 dev_warn(&dev->pci_dev->dev,
c2f5b650
MW
177 "completed id %d twice on queue %d\n",
178 cqe->command_id, le16_to_cpup(&cqe->sq_id));
179 return;
180 }
181 if (ctx == CMD_CTX_INVALID) {
5c1281a3 182 dev_warn(&dev->pci_dev->dev,
c2f5b650
MW
183 "invalid id %d completed on queue %d\n",
184 cqe->command_id, le16_to_cpup(&cqe->sq_id));
185 return;
186 }
187
5c1281a3 188 dev_warn(&dev->pci_dev->dev, "Unknown special completion %p\n", ctx);
c2f5b650
MW
189}
190
184d2944
MW
191/*
192 * Called with local interrupts disabled and the q_lock held. May not sleep.
193 */
c2f5b650
MW
194static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid,
195 nvme_completion_fn *fn)
b60503ba 196{
c2f5b650 197 void *ctx;
e85248e5 198 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
b60503ba 199
c2f5b650
MW
200 if (cmdid >= nvmeq->q_depth) {
201 *fn = special_completion;
48e3d398 202 return CMD_CTX_INVALID;
c2f5b650 203 }
859361a2
KB
204 if (fn)
205 *fn = info[cmdid].fn;
c2f5b650
MW
206 ctx = info[cmdid].ctx;
207 info[cmdid].fn = special_completion;
e85248e5 208 info[cmdid].ctx = CMD_CTX_COMPLETED;
b60503ba
MW
209 clear_bit(cmdid, nvmeq->cmdid_data);
210 wake_up(&nvmeq->sq_full);
c2f5b650 211 return ctx;
b60503ba
MW
212}
213
c2f5b650
MW
214static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
215 nvme_completion_fn *fn)
3c0cf138 216{
c2f5b650 217 void *ctx;
e85248e5 218 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
c2f5b650
MW
219 if (fn)
220 *fn = info[cmdid].fn;
221 ctx = info[cmdid].ctx;
222 info[cmdid].fn = special_completion;
e85248e5 223 info[cmdid].ctx = CMD_CTX_CANCELLED;
c2f5b650 224 return ctx;
3c0cf138
MW
225}
226
5d0f6131 227struct nvme_queue *get_nvmeq(struct nvme_dev *dev)
b60503ba 228{
040a93b5 229 return dev->queues[get_cpu() + 1];
b60503ba
MW
230}
231
5d0f6131 232void put_nvmeq(struct nvme_queue *nvmeq)
b60503ba 233{
1b23484b 234 put_cpu();
b60503ba
MW
235}
236
237/**
714a7a22 238 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
b60503ba
MW
239 * @nvmeq: The queue to use
240 * @cmd: The command to send
241 *
242 * Safe to use from interrupt context
243 */
244static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
245{
246 unsigned long flags;
247 u16 tail;
b60503ba
MW
248 spin_lock_irqsave(&nvmeq->q_lock, flags);
249 tail = nvmeq->sq_tail;
250 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
b60503ba
MW
251 if (++tail == nvmeq->q_depth)
252 tail = 0;
7547881d 253 writel(tail, nvmeq->q_db);
b60503ba
MW
254 nvmeq->sq_tail = tail;
255 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
256
257 return 0;
258}
259
eca18b23 260static __le64 **iod_list(struct nvme_iod *iod)
e025344c 261{
eca18b23 262 return ((void *)iod) + iod->offset;
e025344c
SMM
263}
264
eca18b23
MW
265/*
266 * Will slightly overestimate the number of pages needed. This is OK
267 * as it only leads to a small amount of wasted memory for the lifetime of
268 * the I/O.
269 */
270static int nvme_npages(unsigned size)
271{
272 unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE);
273 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
274}
b60503ba 275
eca18b23
MW
276static struct nvme_iod *
277nvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp)
b60503ba 278{
eca18b23
MW
279 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
280 sizeof(__le64 *) * nvme_npages(nbytes) +
281 sizeof(struct scatterlist) * nseg, gfp);
282
283 if (iod) {
284 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
285 iod->npages = -1;
286 iod->length = nbytes;
2b196034 287 iod->nents = 0;
eca18b23
MW
288 }
289
290 return iod;
b60503ba
MW
291}
292
5d0f6131 293void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
b60503ba 294{
eca18b23
MW
295 const int last_prp = PAGE_SIZE / 8 - 1;
296 int i;
297 __le64 **list = iod_list(iod);
298 dma_addr_t prp_dma = iod->first_dma;
299
300 if (iod->npages == 0)
301 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
302 for (i = 0; i < iod->npages; i++) {
303 __le64 *prp_list = list[i];
304 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
305 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
306 prp_dma = next_prp_dma;
307 }
308 kfree(iod);
b60503ba
MW
309}
310
5c1281a3 311static void bio_completion(struct nvme_dev *dev, void *ctx,
b60503ba
MW
312 struct nvme_completion *cqe)
313{
eca18b23
MW
314 struct nvme_iod *iod = ctx;
315 struct bio *bio = iod->private;
b60503ba
MW
316 u16 status = le16_to_cpup(&cqe->status) >> 1;
317
2b196034
KB
318 if (iod->nents)
319 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
b60503ba 320 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
eca18b23 321 nvme_free_iod(dev, iod);
427e9708 322 if (status)
1ad2f893 323 bio_endio(bio, -EIO);
427e9708 324 else
1ad2f893 325 bio_endio(bio, 0);
b60503ba
MW
326}
327
184d2944 328/* length is in bytes. gfp flags indicates whether we may sleep. */
5d0f6131
VV
329int nvme_setup_prps(struct nvme_dev *dev, struct nvme_common_command *cmd,
330 struct nvme_iod *iod, int total_len, gfp_t gfp)
ff22b54f 331{
99802a7a 332 struct dma_pool *pool;
eca18b23
MW
333 int length = total_len;
334 struct scatterlist *sg = iod->sg;
ff22b54f
MW
335 int dma_len = sg_dma_len(sg);
336 u64 dma_addr = sg_dma_address(sg);
337 int offset = offset_in_page(dma_addr);
e025344c 338 __le64 *prp_list;
eca18b23 339 __le64 **list = iod_list(iod);
e025344c 340 dma_addr_t prp_dma;
eca18b23 341 int nprps, i;
ff22b54f
MW
342
343 cmd->prp1 = cpu_to_le64(dma_addr);
344 length -= (PAGE_SIZE - offset);
345 if (length <= 0)
eca18b23 346 return total_len;
ff22b54f
MW
347
348 dma_len -= (PAGE_SIZE - offset);
349 if (dma_len) {
350 dma_addr += (PAGE_SIZE - offset);
351 } else {
352 sg = sg_next(sg);
353 dma_addr = sg_dma_address(sg);
354 dma_len = sg_dma_len(sg);
355 }
356
357 if (length <= PAGE_SIZE) {
358 cmd->prp2 = cpu_to_le64(dma_addr);
eca18b23 359 return total_len;
e025344c
SMM
360 }
361
362 nprps = DIV_ROUND_UP(length, PAGE_SIZE);
99802a7a
MW
363 if (nprps <= (256 / 8)) {
364 pool = dev->prp_small_pool;
eca18b23 365 iod->npages = 0;
99802a7a
MW
366 } else {
367 pool = dev->prp_page_pool;
eca18b23 368 iod->npages = 1;
99802a7a
MW
369 }
370
b77954cb
MW
371 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
372 if (!prp_list) {
373 cmd->prp2 = cpu_to_le64(dma_addr);
eca18b23
MW
374 iod->npages = -1;
375 return (total_len - length) + PAGE_SIZE;
b77954cb 376 }
eca18b23
MW
377 list[0] = prp_list;
378 iod->first_dma = prp_dma;
e025344c
SMM
379 cmd->prp2 = cpu_to_le64(prp_dma);
380 i = 0;
381 for (;;) {
7523d834 382 if (i == PAGE_SIZE / 8) {
e025344c 383 __le64 *old_prp_list = prp_list;
b77954cb 384 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
eca18b23
MW
385 if (!prp_list)
386 return total_len - length;
387 list[iod->npages++] = prp_list;
7523d834
MW
388 prp_list[0] = old_prp_list[i - 1];
389 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
390 i = 1;
e025344c
SMM
391 }
392 prp_list[i++] = cpu_to_le64(dma_addr);
393 dma_len -= PAGE_SIZE;
394 dma_addr += PAGE_SIZE;
395 length -= PAGE_SIZE;
396 if (length <= 0)
397 break;
398 if (dma_len > 0)
399 continue;
400 BUG_ON(dma_len < 0);
401 sg = sg_next(sg);
402 dma_addr = sg_dma_address(sg);
403 dma_len = sg_dma_len(sg);
ff22b54f
MW
404 }
405
eca18b23 406 return total_len;
ff22b54f
MW
407}
408
427e9708
KB
409struct nvme_bio_pair {
410 struct bio b1, b2, *parent;
411 struct bio_vec *bv1, *bv2;
412 int err;
413 atomic_t cnt;
414};
415
416static void nvme_bio_pair_endio(struct bio *bio, int err)
417{
418 struct nvme_bio_pair *bp = bio->bi_private;
419
420 if (err)
421 bp->err = err;
422
423 if (atomic_dec_and_test(&bp->cnt)) {
424 bio_endio(bp->parent, bp->err);
425 if (bp->bv1)
426 kfree(bp->bv1);
427 if (bp->bv2)
428 kfree(bp->bv2);
429 kfree(bp);
430 }
431}
432
433static struct nvme_bio_pair *nvme_bio_split(struct bio *bio, int idx,
434 int len, int offset)
435{
436 struct nvme_bio_pair *bp;
437
438 BUG_ON(len > bio->bi_size);
439 BUG_ON(idx > bio->bi_vcnt);
440
441 bp = kmalloc(sizeof(*bp), GFP_ATOMIC);
442 if (!bp)
443 return NULL;
444 bp->err = 0;
445
446 bp->b1 = *bio;
447 bp->b2 = *bio;
448
449 bp->b1.bi_size = len;
450 bp->b2.bi_size -= len;
451 bp->b1.bi_vcnt = idx;
452 bp->b2.bi_idx = idx;
453 bp->b2.bi_sector += len >> 9;
454
455 if (offset) {
456 bp->bv1 = kmalloc(bio->bi_max_vecs * sizeof(struct bio_vec),
457 GFP_ATOMIC);
458 if (!bp->bv1)
459 goto split_fail_1;
460
461 bp->bv2 = kmalloc(bio->bi_max_vecs * sizeof(struct bio_vec),
462 GFP_ATOMIC);
463 if (!bp->bv2)
464 goto split_fail_2;
465
466 memcpy(bp->bv1, bio->bi_io_vec,
467 bio->bi_max_vecs * sizeof(struct bio_vec));
468 memcpy(bp->bv2, bio->bi_io_vec,
469 bio->bi_max_vecs * sizeof(struct bio_vec));
470
471 bp->b1.bi_io_vec = bp->bv1;
472 bp->b2.bi_io_vec = bp->bv2;
473 bp->b2.bi_io_vec[idx].bv_offset += offset;
474 bp->b2.bi_io_vec[idx].bv_len -= offset;
475 bp->b1.bi_io_vec[idx].bv_len = offset;
476 bp->b1.bi_vcnt++;
477 } else
478 bp->bv1 = bp->bv2 = NULL;
479
480 bp->b1.bi_private = bp;
481 bp->b2.bi_private = bp;
482
483 bp->b1.bi_end_io = nvme_bio_pair_endio;
484 bp->b2.bi_end_io = nvme_bio_pair_endio;
485
486 bp->parent = bio;
487 atomic_set(&bp->cnt, 2);
488
489 return bp;
490
491 split_fail_2:
492 kfree(bp->bv1);
493 split_fail_1:
494 kfree(bp);
495 return NULL;
496}
497
498static int nvme_split_and_submit(struct bio *bio, struct nvme_queue *nvmeq,
499 int idx, int len, int offset)
500{
501 struct nvme_bio_pair *bp = nvme_bio_split(bio, idx, len, offset);
502 if (!bp)
503 return -ENOMEM;
504
505 if (bio_list_empty(&nvmeq->sq_cong))
506 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
507 bio_list_add(&nvmeq->sq_cong, &bp->b1);
508 bio_list_add(&nvmeq->sq_cong, &bp->b2);
509
510 return 0;
511}
512
1ad2f893
MW
513/* NVMe scatterlists require no holes in the virtual address */
514#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
515 (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
516
427e9708 517static int nvme_map_bio(struct nvme_queue *nvmeq, struct nvme_iod *iod,
b60503ba
MW
518 struct bio *bio, enum dma_data_direction dma_dir, int psegs)
519{
76830840
MW
520 struct bio_vec *bvec, *bvprv = NULL;
521 struct scatterlist *sg = NULL;
159b67d7
KB
522 int i, length = 0, nsegs = 0, split_len = bio->bi_size;
523
524 if (nvmeq->dev->stripe_size)
525 split_len = nvmeq->dev->stripe_size -
526 ((bio->bi_sector << 9) & (nvmeq->dev->stripe_size - 1));
b60503ba 527
eca18b23 528 sg_init_table(iod->sg, psegs);
b60503ba 529 bio_for_each_segment(bvec, bio, i) {
76830840
MW
530 if (bvprv && BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) {
531 sg->length += bvec->bv_len;
532 } else {
1ad2f893 533 if (bvprv && BIOVEC_NOT_VIRT_MERGEABLE(bvprv, bvec))
427e9708
KB
534 return nvme_split_and_submit(bio, nvmeq, i,
535 length, 0);
536
eca18b23 537 sg = sg ? sg + 1 : iod->sg;
76830840
MW
538 sg_set_page(sg, bvec->bv_page, bvec->bv_len,
539 bvec->bv_offset);
540 nsegs++;
541 }
159b67d7
KB
542
543 if (split_len - length < bvec->bv_len)
544 return nvme_split_and_submit(bio, nvmeq, i, split_len,
545 split_len - length);
1ad2f893 546 length += bvec->bv_len;
76830840 547 bvprv = bvec;
b60503ba 548 }
eca18b23 549 iod->nents = nsegs;
76830840 550 sg_mark_end(sg);
427e9708 551 if (dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir) == 0)
1ad2f893 552 return -ENOMEM;
427e9708 553
159b67d7 554 BUG_ON(length != bio->bi_size);
1ad2f893 555 return length;
b60503ba
MW
556}
557
0e5e4f0e
KB
558/*
559 * We reuse the small pool to allocate the 16-byte range here as it is not
560 * worth having a special pool for these or additional cases to handle freeing
561 * the iod.
562 */
563static int nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
564 struct bio *bio, struct nvme_iod *iod, int cmdid)
565{
566 struct nvme_dsm_range *range;
567 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
568
569 range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC,
570 &iod->first_dma);
571 if (!range)
572 return -ENOMEM;
573
574 iod_list(iod)[0] = (__le64 *)range;
575 iod->npages = 0;
576
577 range->cattr = cpu_to_le32(0);
578 range->nlb = cpu_to_le32(bio->bi_size >> ns->lba_shift);
063cc6d5 579 range->slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_sector));
0e5e4f0e
KB
580
581 memset(cmnd, 0, sizeof(*cmnd));
582 cmnd->dsm.opcode = nvme_cmd_dsm;
583 cmnd->dsm.command_id = cmdid;
584 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
585 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
586 cmnd->dsm.nr = 0;
587 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
588
589 if (++nvmeq->sq_tail == nvmeq->q_depth)
590 nvmeq->sq_tail = 0;
591 writel(nvmeq->sq_tail, nvmeq->q_db);
592
593 return 0;
594}
595
00df5cb4
MW
596static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
597 int cmdid)
598{
599 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
600
601 memset(cmnd, 0, sizeof(*cmnd));
602 cmnd->common.opcode = nvme_cmd_flush;
603 cmnd->common.command_id = cmdid;
604 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
605
606 if (++nvmeq->sq_tail == nvmeq->q_depth)
607 nvmeq->sq_tail = 0;
608 writel(nvmeq->sq_tail, nvmeq->q_db);
609
610 return 0;
611}
612
5d0f6131 613int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns)
00df5cb4
MW
614{
615 int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH,
ff976d72 616 special_completion, NVME_IO_TIMEOUT);
00df5cb4
MW
617 if (unlikely(cmdid < 0))
618 return cmdid;
619
620 return nvme_submit_flush(nvmeq, ns, cmdid);
621}
622
184d2944
MW
623/*
624 * Called with local interrupts disabled and the q_lock held. May not sleep.
625 */
b60503ba
MW
626static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
627 struct bio *bio)
628{
ff22b54f 629 struct nvme_command *cmnd;
eca18b23 630 struct nvme_iod *iod;
b60503ba 631 enum dma_data_direction dma_dir;
1ad2f893 632 int cmdid, length, result = -ENOMEM;
b60503ba
MW
633 u16 control;
634 u32 dsmgmt;
b60503ba
MW
635 int psegs = bio_phys_segments(ns->queue, bio);
636
00df5cb4
MW
637 if ((bio->bi_rw & REQ_FLUSH) && psegs) {
638 result = nvme_submit_flush_data(nvmeq, ns);
639 if (result)
640 return result;
641 }
642
eca18b23
MW
643 iod = nvme_alloc_iod(psegs, bio->bi_size, GFP_ATOMIC);
644 if (!iod)
eeee3226 645 goto nomem;
eca18b23 646 iod->private = bio;
b60503ba 647
eeee3226 648 result = -EBUSY;
ff976d72 649 cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT);
b60503ba 650 if (unlikely(cmdid < 0))
eca18b23 651 goto free_iod;
b60503ba 652
0e5e4f0e
KB
653 if (bio->bi_rw & REQ_DISCARD) {
654 result = nvme_submit_discard(nvmeq, ns, bio, iod, cmdid);
655 if (result)
656 goto free_cmdid;
657 return result;
658 }
00df5cb4
MW
659 if ((bio->bi_rw & REQ_FLUSH) && !psegs)
660 return nvme_submit_flush(nvmeq, ns, cmdid);
661
b60503ba
MW
662 control = 0;
663 if (bio->bi_rw & REQ_FUA)
664 control |= NVME_RW_FUA;
665 if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
666 control |= NVME_RW_LR;
667
668 dsmgmt = 0;
669 if (bio->bi_rw & REQ_RAHEAD)
670 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
671
ff22b54f 672 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
b60503ba 673
b8deb62c 674 memset(cmnd, 0, sizeof(*cmnd));
b60503ba 675 if (bio_data_dir(bio)) {
ff22b54f 676 cmnd->rw.opcode = nvme_cmd_write;
b60503ba
MW
677 dma_dir = DMA_TO_DEVICE;
678 } else {
ff22b54f 679 cmnd->rw.opcode = nvme_cmd_read;
b60503ba
MW
680 dma_dir = DMA_FROM_DEVICE;
681 }
682
427e9708
KB
683 result = nvme_map_bio(nvmeq, iod, bio, dma_dir, psegs);
684 if (result <= 0)
859361a2 685 goto free_cmdid;
1ad2f893 686 length = result;
b60503ba 687
ff22b54f
MW
688 cmnd->rw.command_id = cmdid;
689 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
eca18b23
MW
690 length = nvme_setup_prps(nvmeq->dev, &cmnd->common, iod, length,
691 GFP_ATOMIC);
063cc6d5 692 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_sector));
1ad2f893 693 cmnd->rw.length = cpu_to_le16((length >> ns->lba_shift) - 1);
ff22b54f
MW
694 cmnd->rw.control = cpu_to_le16(control);
695 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
b60503ba 696
b60503ba
MW
697 if (++nvmeq->sq_tail == nvmeq->q_depth)
698 nvmeq->sq_tail = 0;
7547881d 699 writel(nvmeq->sq_tail, nvmeq->q_db);
b60503ba 700
1974b1ae
MW
701 return 0;
702
859361a2
KB
703 free_cmdid:
704 free_cmdid(nvmeq, cmdid, NULL);
eca18b23
MW
705 free_iod:
706 nvme_free_iod(nvmeq->dev, iod);
eeee3226
MW
707 nomem:
708 return result;
b60503ba
MW
709}
710
93c3d65b 711static void nvme_make_request(struct request_queue *q, struct bio *bio)
b60503ba
MW
712{
713 struct nvme_ns *ns = q->queuedata;
040a93b5 714 struct nvme_queue *nvmeq = get_nvmeq(ns->dev);
eeee3226
MW
715 int result = -EBUSY;
716
717 spin_lock_irq(&nvmeq->q_lock);
718 if (bio_list_empty(&nvmeq->sq_cong))
719 result = nvme_submit_bio_queue(nvmeq, ns, bio);
720 if (unlikely(result)) {
721 if (bio_list_empty(&nvmeq->sq_cong))
722 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
b60503ba
MW
723 bio_list_add(&nvmeq->sq_cong, bio);
724 }
eeee3226
MW
725
726 spin_unlock_irq(&nvmeq->q_lock);
b60503ba 727 put_nvmeq(nvmeq);
b60503ba
MW
728}
729
b60503ba
MW
730static irqreturn_t nvme_process_cq(struct nvme_queue *nvmeq)
731{
82123460 732 u16 head, phase;
b60503ba 733
b60503ba 734 head = nvmeq->cq_head;
82123460 735 phase = nvmeq->cq_phase;
b60503ba
MW
736
737 for (;;) {
c2f5b650
MW
738 void *ctx;
739 nvme_completion_fn fn;
b60503ba 740 struct nvme_completion cqe = nvmeq->cqes[head];
82123460 741 if ((le16_to_cpu(cqe.status) & 1) != phase)
b60503ba
MW
742 break;
743 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
744 if (++head == nvmeq->q_depth) {
745 head = 0;
82123460 746 phase = !phase;
b60503ba
MW
747 }
748
c2f5b650 749 ctx = free_cmdid(nvmeq, cqe.command_id, &fn);
5c1281a3 750 fn(nvmeq->dev, ctx, &cqe);
b60503ba
MW
751 }
752
753 /* If the controller ignores the cq head doorbell and continuously
754 * writes to the queue, it is theoretically possible to wrap around
755 * the queue twice and mistakenly return IRQ_NONE. Linux only
756 * requires that 0.1% of your interrupts are handled, so this isn't
757 * a big problem.
758 */
82123460 759 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
b60503ba
MW
760 return IRQ_NONE;
761
f1938f6e 762 writel(head, nvmeq->q_db + (1 << nvmeq->dev->db_stride));
b60503ba 763 nvmeq->cq_head = head;
82123460 764 nvmeq->cq_phase = phase;
b60503ba
MW
765
766 return IRQ_HANDLED;
767}
768
769static irqreturn_t nvme_irq(int irq, void *data)
58ffacb5
MW
770{
771 irqreturn_t result;
772 struct nvme_queue *nvmeq = data;
773 spin_lock(&nvmeq->q_lock);
774 result = nvme_process_cq(nvmeq);
775 spin_unlock(&nvmeq->q_lock);
776 return result;
777}
778
779static irqreturn_t nvme_irq_check(int irq, void *data)
780{
781 struct nvme_queue *nvmeq = data;
782 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
783 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
784 return IRQ_NONE;
785 return IRQ_WAKE_THREAD;
786}
787
3c0cf138
MW
788static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
789{
790 spin_lock_irq(&nvmeq->q_lock);
c2f5b650 791 cancel_cmdid(nvmeq, cmdid, NULL);
3c0cf138
MW
792 spin_unlock_irq(&nvmeq->q_lock);
793}
794
c2f5b650
MW
795struct sync_cmd_info {
796 struct task_struct *task;
797 u32 result;
798 int status;
799};
800
5c1281a3 801static void sync_completion(struct nvme_dev *dev, void *ctx,
c2f5b650
MW
802 struct nvme_completion *cqe)
803{
804 struct sync_cmd_info *cmdinfo = ctx;
805 cmdinfo->result = le32_to_cpup(&cqe->result);
806 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
807 wake_up_process(cmdinfo->task);
808}
809
b60503ba
MW
810/*
811 * Returns 0 on success. If the result is negative, it's a Linux error code;
812 * if the result is positive, it's an NVM Express status code
813 */
5d0f6131
VV
814int nvme_submit_sync_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd,
815 u32 *result, unsigned timeout)
b60503ba
MW
816{
817 int cmdid;
818 struct sync_cmd_info cmdinfo;
819
820 cmdinfo.task = current;
821 cmdinfo.status = -EINTR;
822
c2f5b650 823 cmdid = alloc_cmdid_killable(nvmeq, &cmdinfo, sync_completion,
e85248e5 824 timeout);
b60503ba
MW
825 if (cmdid < 0)
826 return cmdid;
827 cmd->common.command_id = cmdid;
828
3c0cf138
MW
829 set_current_state(TASK_KILLABLE);
830 nvme_submit_cmd(nvmeq, cmd);
78f8d257 831 schedule_timeout(timeout);
b60503ba 832
3c0cf138
MW
833 if (cmdinfo.status == -EINTR) {
834 nvme_abort_command(nvmeq, cmdid);
835 return -EINTR;
836 }
837
b60503ba
MW
838 if (result)
839 *result = cmdinfo.result;
840
841 return cmdinfo.status;
842}
843
5d0f6131 844int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
b60503ba
MW
845 u32 *result)
846{
e85248e5 847 return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT);
b60503ba
MW
848}
849
850static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
851{
852 int status;
853 struct nvme_command c;
854
855 memset(&c, 0, sizeof(c));
856 c.delete_queue.opcode = opcode;
857 c.delete_queue.qid = cpu_to_le16(id);
858
859 status = nvme_submit_admin_cmd(dev, &c, NULL);
860 if (status)
861 return -EIO;
862 return 0;
863}
864
865static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
866 struct nvme_queue *nvmeq)
867{
868 int status;
869 struct nvme_command c;
870 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
871
872 memset(&c, 0, sizeof(c));
873 c.create_cq.opcode = nvme_admin_create_cq;
874 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
875 c.create_cq.cqid = cpu_to_le16(qid);
876 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
877 c.create_cq.cq_flags = cpu_to_le16(flags);
878 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
879
880 status = nvme_submit_admin_cmd(dev, &c, NULL);
881 if (status)
882 return -EIO;
883 return 0;
884}
885
886static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
887 struct nvme_queue *nvmeq)
888{
889 int status;
890 struct nvme_command c;
891 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
892
893 memset(&c, 0, sizeof(c));
894 c.create_sq.opcode = nvme_admin_create_sq;
895 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
896 c.create_sq.sqid = cpu_to_le16(qid);
897 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
898 c.create_sq.sq_flags = cpu_to_le16(flags);
899 c.create_sq.cqid = cpu_to_le16(qid);
900
901 status = nvme_submit_admin_cmd(dev, &c, NULL);
902 if (status)
903 return -EIO;
904 return 0;
905}
906
907static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
908{
909 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
910}
911
912static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
913{
914 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
915}
916
5d0f6131 917int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
bc5fc7e4
MW
918 dma_addr_t dma_addr)
919{
920 struct nvme_command c;
921
922 memset(&c, 0, sizeof(c));
923 c.identify.opcode = nvme_admin_identify;
924 c.identify.nsid = cpu_to_le32(nsid);
925 c.identify.prp1 = cpu_to_le64(dma_addr);
926 c.identify.cns = cpu_to_le32(cns);
927
928 return nvme_submit_admin_cmd(dev, &c, NULL);
929}
930
5d0f6131 931int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
08df1e05 932 dma_addr_t dma_addr, u32 *result)
bc5fc7e4
MW
933{
934 struct nvme_command c;
935
936 memset(&c, 0, sizeof(c));
937 c.features.opcode = nvme_admin_get_features;
a42cecce 938 c.features.nsid = cpu_to_le32(nsid);
bc5fc7e4
MW
939 c.features.prp1 = cpu_to_le64(dma_addr);
940 c.features.fid = cpu_to_le32(fid);
bc5fc7e4 941
08df1e05 942 return nvme_submit_admin_cmd(dev, &c, result);
df348139
MW
943}
944
5d0f6131
VV
945int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
946 dma_addr_t dma_addr, u32 *result)
df348139
MW
947{
948 struct nvme_command c;
949
950 memset(&c, 0, sizeof(c));
951 c.features.opcode = nvme_admin_set_features;
952 c.features.prp1 = cpu_to_le64(dma_addr);
953 c.features.fid = cpu_to_le32(fid);
954 c.features.dword11 = cpu_to_le32(dword11);
955
bc5fc7e4
MW
956 return nvme_submit_admin_cmd(dev, &c, result);
957}
958
a09115b2
MW
959/**
960 * nvme_cancel_ios - Cancel outstanding I/Os
961 * @queue: The queue to cancel I/Os on
962 * @timeout: True to only cancel I/Os which have timed out
963 */
964static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout)
965{
966 int depth = nvmeq->q_depth - 1;
967 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
968 unsigned long now = jiffies;
969 int cmdid;
970
971 for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
972 void *ctx;
973 nvme_completion_fn fn;
974 static struct nvme_completion cqe = {
af2d9ca7 975 .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1),
a09115b2
MW
976 };
977
978 if (timeout && !time_after(now, info[cmdid].timeout))
979 continue;
980 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d\n", cmdid);
981 ctx = cancel_cmdid(nvmeq, cmdid, &fn);
982 fn(nvmeq->dev, ctx, &cqe);
983 }
984}
985
9e866774
MW
986static void nvme_free_queue_mem(struct nvme_queue *nvmeq)
987{
988 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
989 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
990 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
991 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
992 kfree(nvmeq);
993}
994
b60503ba
MW
995static void nvme_free_queue(struct nvme_dev *dev, int qid)
996{
997 struct nvme_queue *nvmeq = dev->queues[qid];
aba2080f 998 int vector = dev->entry[nvmeq->cq_vector].vector;
b60503ba 999
a09115b2
MW
1000 spin_lock_irq(&nvmeq->q_lock);
1001 nvme_cancel_ios(nvmeq, false);
3295874b
KB
1002 while (bio_list_peek(&nvmeq->sq_cong)) {
1003 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1004 bio_endio(bio, -EIO);
1005 }
a09115b2
MW
1006 spin_unlock_irq(&nvmeq->q_lock);
1007
aba2080f
MW
1008 irq_set_affinity_hint(vector, NULL);
1009 free_irq(vector, nvmeq);
b60503ba
MW
1010
1011 /* Don't tell the adapter to delete the admin queue */
1012 if (qid) {
1013 adapter_delete_sq(dev, qid);
1014 adapter_delete_cq(dev, qid);
1015 }
1016
9e866774 1017 nvme_free_queue_mem(nvmeq);
b60503ba
MW
1018}
1019
1020static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
1021 int depth, int vector)
1022{
1023 struct device *dmadev = &dev->pci_dev->dev;
a0cadb85
KB
1024 unsigned extra = DIV_ROUND_UP(depth, 8) + (depth *
1025 sizeof(struct nvme_cmd_info));
b60503ba
MW
1026 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
1027 if (!nvmeq)
1028 return NULL;
1029
1030 nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
1031 &nvmeq->cq_dma_addr, GFP_KERNEL);
1032 if (!nvmeq->cqes)
1033 goto free_nvmeq;
1034 memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
1035
1036 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
1037 &nvmeq->sq_dma_addr, GFP_KERNEL);
1038 if (!nvmeq->sq_cmds)
1039 goto free_cqdma;
1040
1041 nvmeq->q_dmadev = dmadev;
091b6092 1042 nvmeq->dev = dev;
b60503ba
MW
1043 spin_lock_init(&nvmeq->q_lock);
1044 nvmeq->cq_head = 0;
82123460 1045 nvmeq->cq_phase = 1;
b60503ba 1046 init_waitqueue_head(&nvmeq->sq_full);
1fa6aead 1047 init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
b60503ba 1048 bio_list_init(&nvmeq->sq_cong);
f1938f6e 1049 nvmeq->q_db = &dev->dbs[qid << (dev->db_stride + 1)];
b60503ba
MW
1050 nvmeq->q_depth = depth;
1051 nvmeq->cq_vector = vector;
1052
1053 return nvmeq;
1054
1055 free_cqdma:
68b8eca5 1056 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
b60503ba
MW
1057 nvmeq->cq_dma_addr);
1058 free_nvmeq:
1059 kfree(nvmeq);
1060 return NULL;
1061}
1062
3001082c
MW
1063static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1064 const char *name)
1065{
58ffacb5
MW
1066 if (use_threaded_interrupts)
1067 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
ec6ce618 1068 nvme_irq_check, nvme_irq,
58ffacb5
MW
1069 IRQF_DISABLED | IRQF_SHARED,
1070 name, nvmeq);
3001082c
MW
1071 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
1072 IRQF_DISABLED | IRQF_SHARED, name, nvmeq);
1073}
1074
8d85fce7
GKH
1075static struct nvme_queue *nvme_create_queue(struct nvme_dev *dev, int qid,
1076 int cq_size, int vector)
b60503ba
MW
1077{
1078 int result;
1079 struct nvme_queue *nvmeq = nvme_alloc_queue(dev, qid, cq_size, vector);
1080
3f85d50b 1081 if (!nvmeq)
6f0f5449 1082 return ERR_PTR(-ENOMEM);
3f85d50b 1083
b60503ba
MW
1084 result = adapter_alloc_cq(dev, qid, nvmeq);
1085 if (result < 0)
1086 goto free_nvmeq;
1087
1088 result = adapter_alloc_sq(dev, qid, nvmeq);
1089 if (result < 0)
1090 goto release_cq;
1091
3001082c 1092 result = queue_request_irq(dev, nvmeq, "nvme");
b60503ba
MW
1093 if (result < 0)
1094 goto release_sq;
1095
1096 return nvmeq;
1097
1098 release_sq:
1099 adapter_delete_sq(dev, qid);
1100 release_cq:
1101 adapter_delete_cq(dev, qid);
1102 free_nvmeq:
1103 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1104 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1105 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1106 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1107 kfree(nvmeq);
6f0f5449 1108 return ERR_PTR(result);
b60503ba
MW
1109}
1110
8d85fce7 1111static int nvme_configure_admin_queue(struct nvme_dev *dev)
b60503ba 1112{
9e866774 1113 int result = 0;
b60503ba 1114 u32 aqa;
22605f96
MW
1115 u64 cap;
1116 unsigned long timeout;
b60503ba
MW
1117 struct nvme_queue *nvmeq;
1118
1119 dev->dbs = ((void __iomem *)dev->bar) + 4096;
1120
1121 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
3f85d50b
MW
1122 if (!nvmeq)
1123 return -ENOMEM;
b60503ba
MW
1124
1125 aqa = nvmeq->q_depth - 1;
1126 aqa |= aqa << 16;
1127
1128 dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
1129 dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
1130 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
7f53f9d2 1131 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
b60503ba 1132
5911f200 1133 writel(0, &dev->bar->cc);
b60503ba
MW
1134 writel(aqa, &dev->bar->aqa);
1135 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1136 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
1137 writel(dev->ctrl_config, &dev->bar->cc);
1138
22605f96
MW
1139 cap = readq(&dev->bar->cap);
1140 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
f1938f6e 1141 dev->db_stride = NVME_CAP_STRIDE(cap);
22605f96 1142
9e866774 1143 while (!result && !(readl(&dev->bar->csts) & NVME_CSTS_RDY)) {
b60503ba
MW
1144 msleep(100);
1145 if (fatal_signal_pending(current))
9e866774 1146 result = -EINTR;
22605f96
MW
1147 if (time_after(jiffies, timeout)) {
1148 dev_err(&dev->pci_dev->dev,
1149 "Device not ready; aborting initialisation\n");
9e866774 1150 result = -ENODEV;
22605f96 1151 }
b60503ba
MW
1152 }
1153
025c557a
KB
1154 if (result)
1155 goto free_q;
9e866774 1156
3001082c 1157 result = queue_request_irq(dev, nvmeq, "nvme admin");
025c557a
KB
1158 if (result)
1159 goto free_q;
1160
b60503ba
MW
1161 dev->queues[0] = nvmeq;
1162 return result;
025c557a
KB
1163
1164 free_q:
1165 nvme_free_queue_mem(nvmeq);
1166 return result;
b60503ba
MW
1167}
1168
5d0f6131 1169struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
eca18b23 1170 unsigned long addr, unsigned length)
b60503ba 1171{
36c14ed9 1172 int i, err, count, nents, offset;
7fc3cdab
MW
1173 struct scatterlist *sg;
1174 struct page **pages;
eca18b23 1175 struct nvme_iod *iod;
36c14ed9
MW
1176
1177 if (addr & 3)
eca18b23 1178 return ERR_PTR(-EINVAL);
7fc3cdab 1179 if (!length)
eca18b23 1180 return ERR_PTR(-EINVAL);
7fc3cdab 1181
36c14ed9 1182 offset = offset_in_page(addr);
7fc3cdab
MW
1183 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1184 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
22fff826
DC
1185 if (!pages)
1186 return ERR_PTR(-ENOMEM);
36c14ed9
MW
1187
1188 err = get_user_pages_fast(addr, count, 1, pages);
1189 if (err < count) {
1190 count = err;
1191 err = -EFAULT;
1192 goto put_pages;
1193 }
7fc3cdab 1194
eca18b23
MW
1195 iod = nvme_alloc_iod(count, length, GFP_KERNEL);
1196 sg = iod->sg;
36c14ed9 1197 sg_init_table(sg, count);
d0ba1e49
MW
1198 for (i = 0; i < count; i++) {
1199 sg_set_page(&sg[i], pages[i],
1200 min_t(int, length, PAGE_SIZE - offset), offset);
1201 length -= (PAGE_SIZE - offset);
1202 offset = 0;
7fc3cdab 1203 }
fe304c43 1204 sg_mark_end(&sg[i - 1]);
1c2ad9fa 1205 iod->nents = count;
7fc3cdab
MW
1206
1207 err = -ENOMEM;
1208 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1209 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
36c14ed9 1210 if (!nents)
eca18b23 1211 goto free_iod;
b60503ba 1212
7fc3cdab 1213 kfree(pages);
eca18b23 1214 return iod;
b60503ba 1215
eca18b23
MW
1216 free_iod:
1217 kfree(iod);
7fc3cdab
MW
1218 put_pages:
1219 for (i = 0; i < count; i++)
1220 put_page(pages[i]);
1221 kfree(pages);
eca18b23 1222 return ERR_PTR(err);
7fc3cdab 1223}
b60503ba 1224
5d0f6131 1225void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
1c2ad9fa 1226 struct nvme_iod *iod)
7fc3cdab 1227{
1c2ad9fa 1228 int i;
b60503ba 1229
1c2ad9fa
MW
1230 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1231 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
7fc3cdab 1232
1c2ad9fa
MW
1233 for (i = 0; i < iod->nents; i++)
1234 put_page(sg_page(&iod->sg[i]));
7fc3cdab 1235}
b60503ba 1236
a53295b6
MW
1237static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1238{
1239 struct nvme_dev *dev = ns->dev;
1240 struct nvme_queue *nvmeq;
1241 struct nvme_user_io io;
1242 struct nvme_command c;
f410c680
KB
1243 unsigned length, meta_len;
1244 int status, i;
1245 struct nvme_iod *iod, *meta_iod = NULL;
1246 dma_addr_t meta_dma_addr;
1247 void *meta, *uninitialized_var(meta_mem);
a53295b6
MW
1248
1249 if (copy_from_user(&io, uio, sizeof(io)))
1250 return -EFAULT;
6c7d4945 1251 length = (io.nblocks + 1) << ns->lba_shift;
f410c680
KB
1252 meta_len = (io.nblocks + 1) * ns->ms;
1253
1254 if (meta_len && ((io.metadata & 3) || !io.metadata))
1255 return -EINVAL;
6c7d4945
MW
1256
1257 switch (io.opcode) {
1258 case nvme_cmd_write:
1259 case nvme_cmd_read:
6bbf1acd 1260 case nvme_cmd_compare:
eca18b23 1261 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
6413214c 1262 break;
6c7d4945 1263 default:
6bbf1acd 1264 return -EINVAL;
6c7d4945
MW
1265 }
1266
eca18b23
MW
1267 if (IS_ERR(iod))
1268 return PTR_ERR(iod);
a53295b6
MW
1269
1270 memset(&c, 0, sizeof(c));
1271 c.rw.opcode = io.opcode;
1272 c.rw.flags = io.flags;
6c7d4945 1273 c.rw.nsid = cpu_to_le32(ns->ns_id);
a53295b6 1274 c.rw.slba = cpu_to_le64(io.slba);
6c7d4945 1275 c.rw.length = cpu_to_le16(io.nblocks);
a53295b6 1276 c.rw.control = cpu_to_le16(io.control);
1c9b5265
MW
1277 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1278 c.rw.reftag = cpu_to_le32(io.reftag);
1279 c.rw.apptag = cpu_to_le16(io.apptag);
1280 c.rw.appmask = cpu_to_le16(io.appmask);
f410c680
KB
1281
1282 if (meta_len) {
1283 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata, meta_len);
1284 if (IS_ERR(meta_iod)) {
1285 status = PTR_ERR(meta_iod);
1286 meta_iod = NULL;
1287 goto unmap;
1288 }
1289
1290 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1291 &meta_dma_addr, GFP_KERNEL);
1292 if (!meta_mem) {
1293 status = -ENOMEM;
1294 goto unmap;
1295 }
1296
1297 if (io.opcode & 1) {
1298 int meta_offset = 0;
1299
1300 for (i = 0; i < meta_iod->nents; i++) {
1301 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1302 meta_iod->sg[i].offset;
1303 memcpy(meta_mem + meta_offset, meta,
1304 meta_iod->sg[i].length);
1305 kunmap_atomic(meta);
1306 meta_offset += meta_iod->sg[i].length;
1307 }
1308 }
1309
1310 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1311 }
1312
eca18b23 1313 length = nvme_setup_prps(dev, &c.common, iod, length, GFP_KERNEL);
a53295b6 1314
040a93b5 1315 nvmeq = get_nvmeq(dev);
fa922821
MW
1316 /*
1317 * Since nvme_submit_sync_cmd sleeps, we can't keep preemption
b1ad37ef
MW
1318 * disabled. We may be preempted at any point, and be rescheduled
1319 * to a different CPU. That will cause cacheline bouncing, but no
1320 * additional races since q_lock already protects against other CPUs.
1321 */
a53295b6 1322 put_nvmeq(nvmeq);
b77954cb
MW
1323 if (length != (io.nblocks + 1) << ns->lba_shift)
1324 status = -ENOMEM;
1325 else
ff976d72 1326 status = nvme_submit_sync_cmd(nvmeq, &c, NULL, NVME_IO_TIMEOUT);
a53295b6 1327
f410c680
KB
1328 if (meta_len) {
1329 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1330 int meta_offset = 0;
1331
1332 for (i = 0; i < meta_iod->nents; i++) {
1333 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1334 meta_iod->sg[i].offset;
1335 memcpy(meta, meta_mem + meta_offset,
1336 meta_iod->sg[i].length);
1337 kunmap_atomic(meta);
1338 meta_offset += meta_iod->sg[i].length;
1339 }
1340 }
1341
1342 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1343 meta_dma_addr);
1344 }
1345
1346 unmap:
1c2ad9fa 1347 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
eca18b23 1348 nvme_free_iod(dev, iod);
f410c680
KB
1349
1350 if (meta_iod) {
1351 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1352 nvme_free_iod(dev, meta_iod);
1353 }
1354
a53295b6
MW
1355 return status;
1356}
1357
50af8bae 1358static int nvme_user_admin_cmd(struct nvme_dev *dev,
6bbf1acd 1359 struct nvme_admin_cmd __user *ucmd)
6ee44cdc 1360{
6bbf1acd 1361 struct nvme_admin_cmd cmd;
6ee44cdc 1362 struct nvme_command c;
eca18b23 1363 int status, length;
c7d36ab8 1364 struct nvme_iod *uninitialized_var(iod);
6ee44cdc 1365
6bbf1acd
MW
1366 if (!capable(CAP_SYS_ADMIN))
1367 return -EACCES;
1368 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
6ee44cdc 1369 return -EFAULT;
6ee44cdc
MW
1370
1371 memset(&c, 0, sizeof(c));
6bbf1acd
MW
1372 c.common.opcode = cmd.opcode;
1373 c.common.flags = cmd.flags;
1374 c.common.nsid = cpu_to_le32(cmd.nsid);
1375 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1376 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1377 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1378 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1379 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1380 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1381 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1382 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1383
1384 length = cmd.data_len;
1385 if (cmd.data_len) {
49742188
MW
1386 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1387 length);
eca18b23
MW
1388 if (IS_ERR(iod))
1389 return PTR_ERR(iod);
1390 length = nvme_setup_prps(dev, &c.common, iod, length,
1391 GFP_KERNEL);
6bbf1acd
MW
1392 }
1393
1394 if (length != cmd.data_len)
b77954cb
MW
1395 status = -ENOMEM;
1396 else
f4f117f6 1397 status = nvme_submit_admin_cmd(dev, &c, &cmd.result);
eca18b23 1398
6bbf1acd 1399 if (cmd.data_len) {
1c2ad9fa 1400 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
eca18b23 1401 nvme_free_iod(dev, iod);
6bbf1acd 1402 }
f4f117f6
KB
1403
1404 if (!status && copy_to_user(&ucmd->result, &cmd.result,
1405 sizeof(cmd.result)))
1406 status = -EFAULT;
1407
6ee44cdc
MW
1408 return status;
1409}
1410
b60503ba
MW
1411static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1412 unsigned long arg)
1413{
1414 struct nvme_ns *ns = bdev->bd_disk->private_data;
1415
1416 switch (cmd) {
6bbf1acd
MW
1417 case NVME_IOCTL_ID:
1418 return ns->ns_id;
1419 case NVME_IOCTL_ADMIN_CMD:
50af8bae 1420 return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
a53295b6
MW
1421 case NVME_IOCTL_SUBMIT_IO:
1422 return nvme_submit_io(ns, (void __user *)arg);
5d0f6131
VV
1423 case SG_GET_VERSION_NUM:
1424 return nvme_sg_get_version_num((void __user *)arg);
1425 case SG_IO:
1426 return nvme_sg_io(ns, (void __user *)arg);
b60503ba
MW
1427 default:
1428 return -ENOTTY;
1429 }
1430}
1431
1432static const struct block_device_operations nvme_fops = {
1433 .owner = THIS_MODULE,
1434 .ioctl = nvme_ioctl,
49481682 1435 .compat_ioctl = nvme_ioctl,
b60503ba
MW
1436};
1437
1fa6aead
MW
1438static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1439{
1440 while (bio_list_peek(&nvmeq->sq_cong)) {
1441 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1442 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
427e9708
KB
1443
1444 if (bio_list_empty(&nvmeq->sq_cong))
1445 remove_wait_queue(&nvmeq->sq_full,
1446 &nvmeq->sq_cong_wait);
1fa6aead 1447 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
427e9708
KB
1448 if (bio_list_empty(&nvmeq->sq_cong))
1449 add_wait_queue(&nvmeq->sq_full,
1450 &nvmeq->sq_cong_wait);
1fa6aead
MW
1451 bio_list_add_head(&nvmeq->sq_cong, bio);
1452 break;
1453 }
1454 }
1455}
1456
1457static int nvme_kthread(void *data)
1458{
1459 struct nvme_dev *dev;
1460
1461 while (!kthread_should_stop()) {
564a232c 1462 set_current_state(TASK_INTERRUPTIBLE);
1fa6aead
MW
1463 spin_lock(&dev_list_lock);
1464 list_for_each_entry(dev, &dev_list, node) {
1465 int i;
1466 for (i = 0; i < dev->queue_count; i++) {
1467 struct nvme_queue *nvmeq = dev->queues[i];
740216fc
MW
1468 if (!nvmeq)
1469 continue;
1fa6aead
MW
1470 spin_lock_irq(&nvmeq->q_lock);
1471 if (nvme_process_cq(nvmeq))
1472 printk("process_cq did something\n");
a09115b2 1473 nvme_cancel_ios(nvmeq, true);
1fa6aead
MW
1474 nvme_resubmit_bios(nvmeq);
1475 spin_unlock_irq(&nvmeq->q_lock);
1476 }
1477 }
1478 spin_unlock(&dev_list_lock);
acb7aa0d 1479 schedule_timeout(round_jiffies_relative(HZ));
1fa6aead
MW
1480 }
1481 return 0;
1482}
1483
5aff9382
MW
1484static DEFINE_IDA(nvme_index_ida);
1485
1486static int nvme_get_ns_idx(void)
1487{
1488 int index, error;
1489
1490 do {
1491 if (!ida_pre_get(&nvme_index_ida, GFP_KERNEL))
1492 return -1;
1493
1494 spin_lock(&dev_list_lock);
1495 error = ida_get_new(&nvme_index_ida, &index);
1496 spin_unlock(&dev_list_lock);
1497 } while (error == -EAGAIN);
1498
1499 if (error)
1500 index = -1;
1501 return index;
1502}
1503
1504static void nvme_put_ns_idx(int index)
1505{
1506 spin_lock(&dev_list_lock);
1507 ida_remove(&nvme_index_ida, index);
1508 spin_unlock(&dev_list_lock);
1509}
1510
0e5e4f0e
KB
1511static void nvme_config_discard(struct nvme_ns *ns)
1512{
1513 u32 logical_block_size = queue_logical_block_size(ns->queue);
1514 ns->queue->limits.discard_zeroes_data = 0;
1515 ns->queue->limits.discard_alignment = logical_block_size;
1516 ns->queue->limits.discard_granularity = logical_block_size;
1517 ns->queue->limits.max_discard_sectors = 0xffffffff;
1518 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1519}
1520
5aff9382 1521static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, int nsid,
b60503ba
MW
1522 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1523{
1524 struct nvme_ns *ns;
1525 struct gendisk *disk;
1526 int lbaf;
1527
1528 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1529 return NULL;
1530
1531 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1532 if (!ns)
1533 return NULL;
1534 ns->queue = blk_alloc_queue(GFP_KERNEL);
1535 if (!ns->queue)
1536 goto out_free_ns;
4eeb9215
MW
1537 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
1538 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1539 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
b60503ba
MW
1540 blk_queue_make_request(ns->queue, nvme_make_request);
1541 ns->dev = dev;
1542 ns->queue->queuedata = ns;
1543
1544 disk = alloc_disk(NVME_MINORS);
1545 if (!disk)
1546 goto out_free_queue;
5aff9382 1547 ns->ns_id = nsid;
b60503ba
MW
1548 ns->disk = disk;
1549 lbaf = id->flbas & 0xf;
1550 ns->lba_shift = id->lbaf[lbaf].ds;
f410c680 1551 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
e9ef4636 1552 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
8fc23e03
KB
1553 if (dev->max_hw_sectors)
1554 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
b60503ba
MW
1555
1556 disk->major = nvme_major;
1557 disk->minors = NVME_MINORS;
5aff9382 1558 disk->first_minor = NVME_MINORS * nvme_get_ns_idx();
b60503ba
MW
1559 disk->fops = &nvme_fops;
1560 disk->private_data = ns;
1561 disk->queue = ns->queue;
388f037f 1562 disk->driverfs_dev = &dev->pci_dev->dev;
5aff9382 1563 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
b60503ba
MW
1564 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1565
0e5e4f0e
KB
1566 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1567 nvme_config_discard(ns);
1568
b60503ba
MW
1569 return ns;
1570
1571 out_free_queue:
1572 blk_cleanup_queue(ns->queue);
1573 out_free_ns:
1574 kfree(ns);
1575 return NULL;
1576}
1577
1578static void nvme_ns_free(struct nvme_ns *ns)
1579{
5aff9382 1580 int index = ns->disk->first_minor / NVME_MINORS;
b60503ba 1581 put_disk(ns->disk);
5aff9382 1582 nvme_put_ns_idx(index);
b60503ba
MW
1583 blk_cleanup_queue(ns->queue);
1584 kfree(ns);
1585}
1586
b3b06812 1587static int set_queue_count(struct nvme_dev *dev, int count)
b60503ba
MW
1588{
1589 int status;
1590 u32 result;
b3b06812 1591 u32 q_count = (count - 1) | ((count - 1) << 16);
b60503ba 1592
df348139 1593 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
bc5fc7e4 1594 &result);
b60503ba
MW
1595 if (status)
1596 return -EIO;
1597 return min(result & 0xffff, result >> 16) + 1;
1598}
1599
8d85fce7 1600static int nvme_setup_io_queues(struct nvme_dev *dev)
b60503ba 1601{
a0cadb85 1602 int result, cpu, i, nr_io_queues, db_bar_size, q_depth;
b60503ba 1603
b348b7d5
MW
1604 nr_io_queues = num_online_cpus();
1605 result = set_queue_count(dev, nr_io_queues);
1b23484b
MW
1606 if (result < 0)
1607 return result;
b348b7d5
MW
1608 if (result < nr_io_queues)
1609 nr_io_queues = result;
b60503ba 1610
1b23484b
MW
1611 /* Deregister the admin queue's interrupt */
1612 free_irq(dev->entry[0].vector, dev->queues[0]);
1613
f1938f6e
MW
1614 db_bar_size = 4096 + ((nr_io_queues + 1) << (dev->db_stride + 3));
1615 if (db_bar_size > 8192) {
1616 iounmap(dev->bar);
1617 dev->bar = ioremap(pci_resource_start(dev->pci_dev, 0),
1618 db_bar_size);
1619 dev->dbs = ((void __iomem *)dev->bar) + 4096;
1620 dev->queues[0]->q_db = dev->dbs;
1621 }
1622
b348b7d5 1623 for (i = 0; i < nr_io_queues; i++)
1b23484b
MW
1624 dev->entry[i].entry = i;
1625 for (;;) {
b348b7d5
MW
1626 result = pci_enable_msix(dev->pci_dev, dev->entry,
1627 nr_io_queues);
1b23484b
MW
1628 if (result == 0) {
1629 break;
1630 } else if (result > 0) {
b348b7d5 1631 nr_io_queues = result;
1b23484b
MW
1632 continue;
1633 } else {
b348b7d5 1634 nr_io_queues = 1;
1b23484b
MW
1635 break;
1636 }
1637 }
1638
1639 result = queue_request_irq(dev, dev->queues[0], "nvme admin");
1640 /* XXX: handle failure here */
1641
1642 cpu = cpumask_first(cpu_online_mask);
b348b7d5 1643 for (i = 0; i < nr_io_queues; i++) {
1b23484b
MW
1644 irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu));
1645 cpu = cpumask_next(cpu, cpu_online_mask);
1646 }
1647
a0cadb85
KB
1648 q_depth = min_t(int, NVME_CAP_MQES(readq(&dev->bar->cap)) + 1,
1649 NVME_Q_DEPTH);
b348b7d5 1650 for (i = 0; i < nr_io_queues; i++) {
a0cadb85 1651 dev->queues[i + 1] = nvme_create_queue(dev, i + 1, q_depth, i);
6f0f5449
MW
1652 if (IS_ERR(dev->queues[i + 1]))
1653 return PTR_ERR(dev->queues[i + 1]);
1b23484b
MW
1654 dev->queue_count++;
1655 }
b60503ba 1656
9ecdc946
MW
1657 for (; i < num_possible_cpus(); i++) {
1658 int target = i % rounddown_pow_of_two(dev->queue_count - 1);
1659 dev->queues[i + 1] = dev->queues[target + 1];
1660 }
1661
b60503ba
MW
1662 return 0;
1663}
1664
1665static void nvme_free_queues(struct nvme_dev *dev)
1666{
1667 int i;
1668
1669 for (i = dev->queue_count - 1; i >= 0; i--)
1670 nvme_free_queue(dev, i);
1671}
1672
422ef0c7
MW
1673/*
1674 * Return: error value if an error occurred setting up the queues or calling
1675 * Identify Device. 0 if these succeeded, even if adding some of the
1676 * namespaces failed. At the moment, these failures are silent. TBD which
1677 * failures should be reported.
1678 */
8d85fce7 1679static int nvme_dev_add(struct nvme_dev *dev)
b60503ba
MW
1680{
1681 int res, nn, i;
cbb6218f 1682 struct nvme_ns *ns;
51814232 1683 struct nvme_id_ctrl *ctrl;
bc5fc7e4
MW
1684 struct nvme_id_ns *id_ns;
1685 void *mem;
b60503ba 1686 dma_addr_t dma_addr;
159b67d7 1687 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
b60503ba
MW
1688
1689 res = nvme_setup_io_queues(dev);
1690 if (res)
1691 return res;
1692
bc5fc7e4 1693 mem = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr,
b60503ba 1694 GFP_KERNEL);
a9ef4343
KB
1695 if (!mem)
1696 return -ENOMEM;
b60503ba 1697
bc5fc7e4 1698 res = nvme_identify(dev, 0, 1, dma_addr);
b60503ba
MW
1699 if (res) {
1700 res = -EIO;
cbb6218f 1701 goto out;
b60503ba
MW
1702 }
1703
bc5fc7e4 1704 ctrl = mem;
51814232 1705 nn = le32_to_cpup(&ctrl->nn);
0e5e4f0e 1706 dev->oncs = le16_to_cpup(&ctrl->oncs);
51814232
MW
1707 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
1708 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
1709 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
159b67d7 1710 if (ctrl->mdts)
8fc23e03 1711 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
159b67d7
KB
1712 if ((dev->pci_dev->vendor == PCI_VENDOR_ID_INTEL) &&
1713 (dev->pci_dev->device == 0x0953) && ctrl->vs[3])
1714 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
b60503ba 1715
bc5fc7e4 1716 id_ns = mem;
2b2c1896 1717 for (i = 1; i <= nn; i++) {
bc5fc7e4 1718 res = nvme_identify(dev, i, 0, dma_addr);
b60503ba
MW
1719 if (res)
1720 continue;
1721
bc5fc7e4 1722 if (id_ns->ncap == 0)
b60503ba
MW
1723 continue;
1724
bc5fc7e4 1725 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
08df1e05 1726 dma_addr + 4096, NULL);
b60503ba 1727 if (res)
12209036 1728 memset(mem + 4096, 0, 4096);
b60503ba 1729
bc5fc7e4 1730 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
b60503ba
MW
1731 if (ns)
1732 list_add_tail(&ns->list, &dev->namespaces);
1733 }
1734 list_for_each_entry(ns, &dev->namespaces, list)
1735 add_disk(ns->disk);
422ef0c7 1736 res = 0;
b60503ba 1737
bc5fc7e4 1738 out:
684f5c20 1739 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
b60503ba
MW
1740 return res;
1741}
1742
1743static int nvme_dev_remove(struct nvme_dev *dev)
1744{
1745 struct nvme_ns *ns, *next;
1746
1fa6aead
MW
1747 spin_lock(&dev_list_lock);
1748 list_del(&dev->node);
1749 spin_unlock(&dev_list_lock);
1750
b60503ba
MW
1751 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
1752 list_del(&ns->list);
1753 del_gendisk(ns->disk);
1754 nvme_ns_free(ns);
1755 }
1756
1757 nvme_free_queues(dev);
1758
1759 return 0;
1760}
1761
091b6092
MW
1762static int nvme_setup_prp_pools(struct nvme_dev *dev)
1763{
1764 struct device *dmadev = &dev->pci_dev->dev;
1765 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
1766 PAGE_SIZE, PAGE_SIZE, 0);
1767 if (!dev->prp_page_pool)
1768 return -ENOMEM;
1769
99802a7a
MW
1770 /* Optimisation for I/Os between 4k and 128k */
1771 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
1772 256, 256, 0);
1773 if (!dev->prp_small_pool) {
1774 dma_pool_destroy(dev->prp_page_pool);
1775 return -ENOMEM;
1776 }
091b6092
MW
1777 return 0;
1778}
1779
1780static void nvme_release_prp_pools(struct nvme_dev *dev)
1781{
1782 dma_pool_destroy(dev->prp_page_pool);
99802a7a 1783 dma_pool_destroy(dev->prp_small_pool);
091b6092
MW
1784}
1785
cd58ad7d
QSA
1786static DEFINE_IDA(nvme_instance_ida);
1787
1788static int nvme_set_instance(struct nvme_dev *dev)
b60503ba 1789{
cd58ad7d
QSA
1790 int instance, error;
1791
1792 do {
1793 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
1794 return -ENODEV;
1795
1796 spin_lock(&dev_list_lock);
1797 error = ida_get_new(&nvme_instance_ida, &instance);
1798 spin_unlock(&dev_list_lock);
1799 } while (error == -EAGAIN);
1800
1801 if (error)
1802 return -ENODEV;
1803
1804 dev->instance = instance;
1805 return 0;
b60503ba
MW
1806}
1807
1808static void nvme_release_instance(struct nvme_dev *dev)
1809{
cd58ad7d
QSA
1810 spin_lock(&dev_list_lock);
1811 ida_remove(&nvme_instance_ida, dev->instance);
1812 spin_unlock(&dev_list_lock);
b60503ba
MW
1813}
1814
5e82e952
KB
1815static void nvme_free_dev(struct kref *kref)
1816{
1817 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
1818 nvme_dev_remove(dev);
1819 pci_disable_msix(dev->pci_dev);
1820 iounmap(dev->bar);
1821 nvme_release_instance(dev);
1822 nvme_release_prp_pools(dev);
1823 pci_disable_device(dev->pci_dev);
1824 pci_release_regions(dev->pci_dev);
1825 kfree(dev->queues);
1826 kfree(dev->entry);
1827 kfree(dev);
1828}
1829
1830static int nvme_dev_open(struct inode *inode, struct file *f)
1831{
1832 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
1833 miscdev);
1834 kref_get(&dev->kref);
1835 f->private_data = dev;
1836 return 0;
1837}
1838
1839static int nvme_dev_release(struct inode *inode, struct file *f)
1840{
1841 struct nvme_dev *dev = f->private_data;
1842 kref_put(&dev->kref, nvme_free_dev);
1843 return 0;
1844}
1845
1846static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
1847{
1848 struct nvme_dev *dev = f->private_data;
1849 switch (cmd) {
1850 case NVME_IOCTL_ADMIN_CMD:
1851 return nvme_user_admin_cmd(dev, (void __user *)arg);
1852 default:
1853 return -ENOTTY;
1854 }
1855}
1856
1857static const struct file_operations nvme_dev_fops = {
1858 .owner = THIS_MODULE,
1859 .open = nvme_dev_open,
1860 .release = nvme_dev_release,
1861 .unlocked_ioctl = nvme_dev_ioctl,
1862 .compat_ioctl = nvme_dev_ioctl,
1863};
1864
8d85fce7 1865static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
b60503ba 1866{
574e8b95 1867 int bars, result = -ENOMEM;
b60503ba
MW
1868 struct nvme_dev *dev;
1869
1870 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1871 if (!dev)
1872 return -ENOMEM;
1873 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
1874 GFP_KERNEL);
1875 if (!dev->entry)
1876 goto free;
1b23484b
MW
1877 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
1878 GFP_KERNEL);
b60503ba
MW
1879 if (!dev->queues)
1880 goto free;
1881
0ee5a7d7
SMM
1882 if (pci_enable_device_mem(pdev))
1883 goto free;
f64d3365 1884 pci_set_master(pdev);
574e8b95
MW
1885 bars = pci_select_bars(pdev, IORESOURCE_MEM);
1886 if (pci_request_selected_regions(pdev, bars, "nvme"))
1887 goto disable;
0ee5a7d7 1888
b60503ba
MW
1889 INIT_LIST_HEAD(&dev->namespaces);
1890 dev->pci_dev = pdev;
1891 pci_set_drvdata(pdev, dev);
2930353f
MW
1892 dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
1893 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
cd58ad7d
QSA
1894 result = nvme_set_instance(dev);
1895 if (result)
1896 goto disable;
1897
53c9577e 1898 dev->entry[0].vector = pdev->irq;
b60503ba 1899
091b6092
MW
1900 result = nvme_setup_prp_pools(dev);
1901 if (result)
1902 goto disable_msix;
1903
b60503ba
MW
1904 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1905 if (!dev->bar) {
1906 result = -ENOMEM;
574e8b95 1907 goto disable_msix;
b60503ba
MW
1908 }
1909
1910 result = nvme_configure_admin_queue(dev);
1911 if (result)
1912 goto unmap;
1913 dev->queue_count++;
1914
1fa6aead
MW
1915 spin_lock(&dev_list_lock);
1916 list_add(&dev->node, &dev_list);
1917 spin_unlock(&dev_list_lock);
1918
740216fc
MW
1919 result = nvme_dev_add(dev);
1920 if (result)
1921 goto delete;
1922
5e82e952
KB
1923 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
1924 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
1925 dev->miscdev.parent = &pdev->dev;
1926 dev->miscdev.name = dev->name;
1927 dev->miscdev.fops = &nvme_dev_fops;
1928 result = misc_register(&dev->miscdev);
1929 if (result)
1930 goto remove;
1931
1932 kref_init(&dev->kref);
b60503ba
MW
1933 return 0;
1934
5e82e952
KB
1935 remove:
1936 nvme_dev_remove(dev);
b60503ba 1937 delete:
740216fc
MW
1938 spin_lock(&dev_list_lock);
1939 list_del(&dev->node);
1940 spin_unlock(&dev_list_lock);
1941
b60503ba
MW
1942 nvme_free_queues(dev);
1943 unmap:
1944 iounmap(dev->bar);
574e8b95 1945 disable_msix:
b60503ba
MW
1946 pci_disable_msix(pdev);
1947 nvme_release_instance(dev);
091b6092 1948 nvme_release_prp_pools(dev);
574e8b95 1949 disable:
0ee5a7d7 1950 pci_disable_device(pdev);
574e8b95 1951 pci_release_regions(pdev);
b60503ba
MW
1952 free:
1953 kfree(dev->queues);
1954 kfree(dev->entry);
1955 kfree(dev);
1956 return result;
1957}
1958
8d85fce7 1959static void nvme_remove(struct pci_dev *pdev)
b60503ba
MW
1960{
1961 struct nvme_dev *dev = pci_get_drvdata(pdev);
5e82e952
KB
1962 misc_deregister(&dev->miscdev);
1963 kref_put(&dev->kref, nvme_free_dev);
b60503ba
MW
1964}
1965
1966/* These functions are yet to be implemented */
1967#define nvme_error_detected NULL
1968#define nvme_dump_registers NULL
1969#define nvme_link_reset NULL
1970#define nvme_slot_reset NULL
1971#define nvme_error_resume NULL
1972#define nvme_suspend NULL
1973#define nvme_resume NULL
1974
1d352035 1975static const struct pci_error_handlers nvme_err_handler = {
b60503ba
MW
1976 .error_detected = nvme_error_detected,
1977 .mmio_enabled = nvme_dump_registers,
1978 .link_reset = nvme_link_reset,
1979 .slot_reset = nvme_slot_reset,
1980 .resume = nvme_error_resume,
1981};
1982
1983/* Move to pci_ids.h later */
1984#define PCI_CLASS_STORAGE_EXPRESS 0x010802
1985
1986static DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
1987 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
1988 { 0, }
1989};
1990MODULE_DEVICE_TABLE(pci, nvme_id_table);
1991
1992static struct pci_driver nvme_driver = {
1993 .name = "nvme",
1994 .id_table = nvme_id_table,
1995 .probe = nvme_probe,
8d85fce7 1996 .remove = nvme_remove,
b60503ba
MW
1997 .suspend = nvme_suspend,
1998 .resume = nvme_resume,
1999 .err_handler = &nvme_err_handler,
2000};
2001
2002static int __init nvme_init(void)
2003{
0ac13140 2004 int result;
1fa6aead
MW
2005
2006 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2007 if (IS_ERR(nvme_thread))
2008 return PTR_ERR(nvme_thread);
b60503ba 2009
5c42ea16
KB
2010 result = register_blkdev(nvme_major, "nvme");
2011 if (result < 0)
1fa6aead 2012 goto kill_kthread;
5c42ea16 2013 else if (result > 0)
0ac13140 2014 nvme_major = result;
b60503ba
MW
2015
2016 result = pci_register_driver(&nvme_driver);
1fa6aead
MW
2017 if (result)
2018 goto unregister_blkdev;
2019 return 0;
b60503ba 2020
1fa6aead 2021 unregister_blkdev:
b60503ba 2022 unregister_blkdev(nvme_major, "nvme");
1fa6aead
MW
2023 kill_kthread:
2024 kthread_stop(nvme_thread);
b60503ba
MW
2025 return result;
2026}
2027
2028static void __exit nvme_exit(void)
2029{
2030 pci_unregister_driver(&nvme_driver);
2031 unregister_blkdev(nvme_major, "nvme");
1fa6aead 2032 kthread_stop(nvme_thread);
b60503ba
MW
2033}
2034
2035MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2036MODULE_LICENSE("GPL");
366e8217 2037MODULE_VERSION("0.8");
b60503ba
MW
2038module_init(nvme_init);
2039module_exit(nvme_exit);