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