blob: 7b615a063e925dcaae7f9eeff9e48c6c648ff19b [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
3 * Copyright (c) 2011, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#include <linux/nvme.h>
20#include <linux/bio.h>
Matthew Wilcox8de05532011-05-12 13:50:28 -040021#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050022#include <linux/blkdev.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040023#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050024#include <linux/errno.h>
25#include <linux/fs.h>
26#include <linux/genhd.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040027#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050028#include <linux/init.h>
29#include <linux/interrupt.h>
30#include <linux/io.h>
31#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050032#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050033#include <linux/kernel.h>
34#include <linux/mm.h>
35#include <linux/module.h>
36#include <linux/moduleparam.h>
37#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050038#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040039#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050040#include <linux/sched.h>
41#include <linux/slab.h>
42#include <linux/types.h>
Vishal Verma5d0f6132013-03-04 18:40:58 -070043#include <scsi/sg.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090044#include <asm-generic/io-64-nonatomic-lo-hi.h>
45
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050046#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))
Matthew Wilcoxe85248e2011-02-06 18:30:16 -050049#define ADMIN_TIMEOUT (60 * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050050
51static int nvme_major;
52module_param(nvme_major, int, 0);
53
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050054static int use_threaded_interrupts;
55module_param(use_threaded_interrupts, int, 0);
56
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050057static DEFINE_SPINLOCK(dev_list_lock);
58static LIST_HEAD(dev_list);
59static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070060static struct workqueue_struct *nvme_workq;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050061
Keith Buschd4b4ff82013-12-10 13:10:37 -070062static void nvme_reset_failed_dev(struct work_struct *ws);
63
Keith Busch4d115422013-12-10 13:10:40 -070064struct async_cmd_info {
65 struct kthread_work work;
66 struct kthread_worker *worker;
67 u32 result;
68 int status;
69 void *ctx;
70};
71
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050072/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050073 * An NVM Express queue. Each device has at least two (one for admin
74 * commands and one for I/O commands).
75 */
76struct nvme_queue {
77 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -050078 struct nvme_dev *dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050079 spinlock_t q_lock;
80 struct nvme_command *sq_cmds;
81 volatile struct nvme_completion *cqes;
82 dma_addr_t sq_dma_addr;
83 dma_addr_t cq_dma_addr;
84 wait_queue_head_t sq_full;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050085 wait_queue_t sq_cong_wait;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050086 struct bio_list sq_cong;
87 u32 __iomem *q_db;
88 u16 q_depth;
89 u16 cq_vector;
90 u16 sq_head;
91 u16 sq_tail;
92 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -070093 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -040094 u8 cq_phase;
95 u8 cqe_seen;
Keith Busch22404272013-07-15 15:02:20 -060096 u8 q_suspended;
Keith Busch4d115422013-12-10 13:10:40 -070097 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050098 unsigned long cmdid_data[];
99};
100
101/*
102 * Check we didin't inadvertently grow the command struct
103 */
104static inline void _nvme_check_size(void)
105{
106 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
107 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
108 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
109 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
110 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400111 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700112 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500113 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
114 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
115 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
116 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600117 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500118}
119
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500120typedef void (*nvme_completion_fn)(struct nvme_dev *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400121 struct nvme_completion *);
122
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500123struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400124 nvme_completion_fn fn;
125 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500126 unsigned long timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700127 int aborted;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500128};
129
130static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
131{
132 return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
133}
134
Keith Busch22404272013-07-15 15:02:20 -0600135static unsigned nvme_queue_extra(int depth)
136{
137 return DIV_ROUND_UP(depth, 8) + (depth * sizeof(struct nvme_cmd_info));
138}
139
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500140/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400141 * alloc_cmdid() - Allocate a Command ID
142 * @nvmeq: The queue that will be used for this command
143 * @ctx: A pointer that will be passed to the handler
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400144 * @handler: The function to call on completion
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500145 *
146 * Allocate a Command ID for a queue. The data passed in will
147 * be passed to the completion handler. This is implemented by using
148 * the bottom two bits of the ctx pointer to store the handler ID.
149 * Passing in a pointer that's not 4-byte aligned will cause a BUG.
150 * We can change this if it becomes a problem.
Matthew Wilcox184d2942011-05-11 21:36:38 -0400151 *
152 * May be called with local interrupts disabled and the q_lock held,
153 * or with interrupts enabled and no locks held.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500154 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400155static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx,
156 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500157{
Matthew Wilcoxe6d15f72011-02-24 08:49:41 -0500158 int depth = nvmeq->q_depth - 1;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500159 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500160 int cmdid;
161
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500162 do {
163 cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
164 if (cmdid >= depth)
165 return -EBUSY;
166 } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
167
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400168 info[cmdid].fn = handler;
169 info[cmdid].ctx = ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500170 info[cmdid].timeout = jiffies + timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700171 info[cmdid].aborted = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500172 return cmdid;
173}
174
175static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400176 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500177{
178 int cmdid;
179 wait_event_killable(nvmeq->sq_full,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500180 (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500181 return (cmdid < 0) ? -EINTR : cmdid;
182}
183
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400184/* Special values must be less than 0x1000 */
185#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500186#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
187#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
188#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500189#define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE)
Keith Buschc30341d2013-12-10 13:10:38 -0700190#define CMD_CTX_ABORT (0x31C + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500191
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500192static void special_completion(struct nvme_dev *dev, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400193 struct nvme_completion *cqe)
194{
195 if (ctx == CMD_CTX_CANCELLED)
196 return;
197 if (ctx == CMD_CTX_FLUSH)
198 return;
Keith Buschc30341d2013-12-10 13:10:38 -0700199 if (ctx == CMD_CTX_ABORT) {
200 ++dev->abort_limit;
201 return;
202 }
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400203 if (ctx == CMD_CTX_COMPLETED) {
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500204 dev_warn(&dev->pci_dev->dev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400205 "completed id %d twice on queue %d\n",
206 cqe->command_id, le16_to_cpup(&cqe->sq_id));
207 return;
208 }
209 if (ctx == CMD_CTX_INVALID) {
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500210 dev_warn(&dev->pci_dev->dev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400211 "invalid id %d completed on queue %d\n",
212 cqe->command_id, le16_to_cpup(&cqe->sq_id));
213 return;
214 }
215
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500216 dev_warn(&dev->pci_dev->dev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400217}
218
Keith Busch4d115422013-12-10 13:10:40 -0700219static void async_completion(struct nvme_dev *dev, void *ctx,
220 struct nvme_completion *cqe)
221{
222 struct async_cmd_info *cmdinfo = ctx;
223 cmdinfo->result = le32_to_cpup(&cqe->result);
224 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
225 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
226}
227
Matthew Wilcox184d2942011-05-11 21:36:38 -0400228/*
229 * Called with local interrupts disabled and the q_lock held. May not sleep.
230 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400231static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid,
232 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500233{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400234 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500235 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500236
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400237 if (cmdid >= nvmeq->q_depth) {
238 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500239 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400240 }
Keith Busch859361a2012-08-02 14:05:59 -0600241 if (fn)
242 *fn = info[cmdid].fn;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400243 ctx = info[cmdid].ctx;
244 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500245 info[cmdid].ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500246 clear_bit(cmdid, nvmeq->cmdid_data);
247 wake_up(&nvmeq->sq_full);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400248 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500249}
250
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400251static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
252 nvme_completion_fn *fn)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500253{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400254 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500255 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400256 if (fn)
257 *fn = info[cmdid].fn;
258 ctx = info[cmdid].ctx;
259 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500260 info[cmdid].ctx = CMD_CTX_CANCELLED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400261 return ctx;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500262}
263
Vishal Verma5d0f6132013-03-04 18:40:58 -0700264struct nvme_queue *get_nvmeq(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500265{
Matthew Wilcox040a93b2011-12-20 11:04:12 -0500266 return dev->queues[get_cpu() + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500267}
268
Vishal Verma5d0f6132013-03-04 18:40:58 -0700269void put_nvmeq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500270{
Matthew Wilcox1b234842011-01-20 13:01:49 -0500271 put_cpu();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500272}
273
274/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400275 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500276 * @nvmeq: The queue to use
277 * @cmd: The command to send
278 *
279 * Safe to use from interrupt context
280 */
281static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
282{
283 unsigned long flags;
284 u16 tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500285 spin_lock_irqsave(&nvmeq->q_lock, flags);
286 tail = nvmeq->sq_tail;
287 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500288 if (++tail == nvmeq->q_depth)
289 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500290 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500291 nvmeq->sq_tail = tail;
292 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
293
294 return 0;
295}
296
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500297static __le64 **iod_list(struct nvme_iod *iod)
298{
299 return ((void *)iod) + iod->offset;
300}
301
302/*
303 * Will slightly overestimate the number of pages needed. This is OK
304 * as it only leads to a small amount of wasted memory for the lifetime of
305 * the I/O.
306 */
307static int nvme_npages(unsigned size)
308{
309 unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE);
310 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
311}
312
313static struct nvme_iod *
314nvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp)
315{
316 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
317 sizeof(__le64 *) * nvme_npages(nbytes) +
318 sizeof(struct scatterlist) * nseg, gfp);
319
320 if (iod) {
321 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
322 iod->npages = -1;
323 iod->length = nbytes;
Keith Busch2b196032012-11-06 11:59:23 -0700324 iod->nents = 0;
Keith Busch61982212013-05-29 15:59:39 -0600325 iod->start_time = jiffies;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500326 }
327
328 return iod;
329}
330
Vishal Verma5d0f6132013-03-04 18:40:58 -0700331void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500332{
333 const int last_prp = PAGE_SIZE / 8 - 1;
334 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500335 __le64 **list = iod_list(iod);
336 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500337
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500338 if (iod->npages == 0)
339 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
340 for (i = 0; i < iod->npages; i++) {
341 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500342 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500343 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500344 prp_dma = next_prp_dma;
345 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500346 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500347}
348
Keith Busch61982212013-05-29 15:59:39 -0600349static void nvme_start_io_acct(struct bio *bio)
350{
351 struct gendisk *disk = bio->bi_bdev->bd_disk;
352 const int rw = bio_data_dir(bio);
353 int cpu = part_stat_lock();
354 part_round_stats(cpu, &disk->part0);
355 part_stat_inc(cpu, &disk->part0, ios[rw]);
356 part_stat_add(cpu, &disk->part0, sectors[rw], bio_sectors(bio));
357 part_inc_in_flight(&disk->part0, rw);
358 part_stat_unlock();
359}
360
361static void nvme_end_io_acct(struct bio *bio, unsigned long start_time)
362{
363 struct gendisk *disk = bio->bi_bdev->bd_disk;
364 const int rw = bio_data_dir(bio);
365 unsigned long duration = jiffies - start_time;
366 int cpu = part_stat_lock();
367 part_stat_add(cpu, &disk->part0, ticks[rw], duration);
368 part_round_stats(cpu, &disk->part0);
369 part_dec_in_flight(&disk->part0, rw);
370 part_stat_unlock();
371}
372
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500373static void bio_completion(struct nvme_dev *dev, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500374 struct nvme_completion *cqe)
375{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500376 struct nvme_iod *iod = ctx;
377 struct bio *bio = iod->private;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500378 u16 status = le16_to_cpup(&cqe->status) >> 1;
379
Keith Busch9e59d092013-08-08 10:25:38 -0600380 if (iod->nents) {
Keith Busch2b196032012-11-06 11:59:23 -0700381 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500382 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busch9e59d092013-08-08 10:25:38 -0600383 nvme_end_io_acct(bio, iod->start_time);
384 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500385 nvme_free_iod(dev, iod);
Keith Busch427e9702013-04-09 11:59:32 -0600386 if (status)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500387 bio_endio(bio, -EIO);
Keith Busch427e9702013-04-09 11:59:32 -0600388 else
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500389 bio_endio(bio, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500390}
391
Matthew Wilcox184d2942011-05-11 21:36:38 -0400392/* length is in bytes. gfp flags indicates whether we may sleep. */
Vishal Verma5d0f6132013-03-04 18:40:58 -0700393int nvme_setup_prps(struct nvme_dev *dev, struct nvme_common_command *cmd,
394 struct nvme_iod *iod, int total_len, gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500395{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500396 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500397 int length = total_len;
398 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500399 int dma_len = sg_dma_len(sg);
400 u64 dma_addr = sg_dma_address(sg);
401 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500402 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500403 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500404 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500405 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500406
407 cmd->prp1 = cpu_to_le64(dma_addr);
408 length -= (PAGE_SIZE - offset);
409 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500410 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500411
412 dma_len -= (PAGE_SIZE - offset);
413 if (dma_len) {
414 dma_addr += (PAGE_SIZE - offset);
415 } else {
416 sg = sg_next(sg);
417 dma_addr = sg_dma_address(sg);
418 dma_len = sg_dma_len(sg);
419 }
420
421 if (length <= PAGE_SIZE) {
422 cmd->prp2 = cpu_to_le64(dma_addr);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500423 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500424 }
425
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500426 nprps = DIV_ROUND_UP(length, PAGE_SIZE);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500427 if (nprps <= (256 / 8)) {
428 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500429 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500430 } else {
431 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500432 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500433 }
434
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400435 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
436 if (!prp_list) {
437 cmd->prp2 = cpu_to_le64(dma_addr);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500438 iod->npages = -1;
439 return (total_len - length) + PAGE_SIZE;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400440 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500441 list[0] = prp_list;
442 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500443 cmd->prp2 = cpu_to_le64(prp_dma);
444 i = 0;
445 for (;;) {
Matthew Wilcox7523d832011-03-16 16:43:40 -0400446 if (i == PAGE_SIZE / 8) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500447 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400448 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500449 if (!prp_list)
450 return total_len - length;
451 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400452 prp_list[0] = old_prp_list[i - 1];
453 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
454 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500455 }
456 prp_list[i++] = cpu_to_le64(dma_addr);
457 dma_len -= PAGE_SIZE;
458 dma_addr += PAGE_SIZE;
459 length -= PAGE_SIZE;
460 if (length <= 0)
461 break;
462 if (dma_len > 0)
463 continue;
464 BUG_ON(dma_len < 0);
465 sg = sg_next(sg);
466 dma_addr = sg_dma_address(sg);
467 dma_len = sg_dma_len(sg);
468 }
469
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500470 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500471}
472
Keith Busch427e9702013-04-09 11:59:32 -0600473struct nvme_bio_pair {
474 struct bio b1, b2, *parent;
475 struct bio_vec *bv1, *bv2;
476 int err;
477 atomic_t cnt;
478};
479
480static void nvme_bio_pair_endio(struct bio *bio, int err)
481{
482 struct nvme_bio_pair *bp = bio->bi_private;
483
484 if (err)
485 bp->err = err;
486
487 if (atomic_dec_and_test(&bp->cnt)) {
488 bio_endio(bp->parent, bp->err);
Keith Busch1b567492013-07-18 12:13:51 -0600489 kfree(bp->bv1);
490 kfree(bp->bv2);
Keith Busch427e9702013-04-09 11:59:32 -0600491 kfree(bp);
492 }
493}
494
495static struct nvme_bio_pair *nvme_bio_split(struct bio *bio, int idx,
496 int len, int offset)
497{
498 struct nvme_bio_pair *bp;
499
500 BUG_ON(len > bio->bi_size);
501 BUG_ON(idx > bio->bi_vcnt);
502
503 bp = kmalloc(sizeof(*bp), GFP_ATOMIC);
504 if (!bp)
505 return NULL;
506 bp->err = 0;
507
508 bp->b1 = *bio;
509 bp->b2 = *bio;
510
511 bp->b1.bi_size = len;
512 bp->b2.bi_size -= len;
513 bp->b1.bi_vcnt = idx;
514 bp->b2.bi_idx = idx;
515 bp->b2.bi_sector += len >> 9;
516
517 if (offset) {
518 bp->bv1 = kmalloc(bio->bi_max_vecs * sizeof(struct bio_vec),
519 GFP_ATOMIC);
520 if (!bp->bv1)
521 goto split_fail_1;
522
523 bp->bv2 = kmalloc(bio->bi_max_vecs * sizeof(struct bio_vec),
524 GFP_ATOMIC);
525 if (!bp->bv2)
526 goto split_fail_2;
527
528 memcpy(bp->bv1, bio->bi_io_vec,
529 bio->bi_max_vecs * sizeof(struct bio_vec));
530 memcpy(bp->bv2, bio->bi_io_vec,
531 bio->bi_max_vecs * sizeof(struct bio_vec));
532
533 bp->b1.bi_io_vec = bp->bv1;
534 bp->b2.bi_io_vec = bp->bv2;
535 bp->b2.bi_io_vec[idx].bv_offset += offset;
536 bp->b2.bi_io_vec[idx].bv_len -= offset;
537 bp->b1.bi_io_vec[idx].bv_len = offset;
538 bp->b1.bi_vcnt++;
539 } else
540 bp->bv1 = bp->bv2 = NULL;
541
542 bp->b1.bi_private = bp;
543 bp->b2.bi_private = bp;
544
545 bp->b1.bi_end_io = nvme_bio_pair_endio;
546 bp->b2.bi_end_io = nvme_bio_pair_endio;
547
548 bp->parent = bio;
549 atomic_set(&bp->cnt, 2);
550
551 return bp;
552
553 split_fail_2:
554 kfree(bp->bv1);
555 split_fail_1:
556 kfree(bp);
557 return NULL;
558}
559
560static int nvme_split_and_submit(struct bio *bio, struct nvme_queue *nvmeq,
561 int idx, int len, int offset)
562{
563 struct nvme_bio_pair *bp = nvme_bio_split(bio, idx, len, offset);
564 if (!bp)
565 return -ENOMEM;
566
567 if (bio_list_empty(&nvmeq->sq_cong))
568 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
569 bio_list_add(&nvmeq->sq_cong, &bp->b1);
570 bio_list_add(&nvmeq->sq_cong, &bp->b2);
571
572 return 0;
573}
574
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500575/* NVMe scatterlists require no holes in the virtual address */
576#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
577 (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
578
Keith Busch427e9702013-04-09 11:59:32 -0600579static int nvme_map_bio(struct nvme_queue *nvmeq, struct nvme_iod *iod,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500580 struct bio *bio, enum dma_data_direction dma_dir, int psegs)
581{
Matthew Wilcox76830842011-02-10 13:55:39 -0500582 struct bio_vec *bvec, *bvprv = NULL;
583 struct scatterlist *sg = NULL;
Keith Busch159b67d2013-04-09 17:13:20 -0600584 int i, length = 0, nsegs = 0, split_len = bio->bi_size;
585
586 if (nvmeq->dev->stripe_size)
587 split_len = nvmeq->dev->stripe_size -
588 ((bio->bi_sector << 9) & (nvmeq->dev->stripe_size - 1));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500589
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500590 sg_init_table(iod->sg, psegs);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500591 bio_for_each_segment(bvec, bio, i) {
Matthew Wilcox76830842011-02-10 13:55:39 -0500592 if (bvprv && BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) {
593 sg->length += bvec->bv_len;
594 } else {
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500595 if (bvprv && BIOVEC_NOT_VIRT_MERGEABLE(bvprv, bvec))
Keith Busch427e9702013-04-09 11:59:32 -0600596 return nvme_split_and_submit(bio, nvmeq, i,
597 length, 0);
598
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500599 sg = sg ? sg + 1 : iod->sg;
Matthew Wilcox76830842011-02-10 13:55:39 -0500600 sg_set_page(sg, bvec->bv_page, bvec->bv_len,
601 bvec->bv_offset);
602 nsegs++;
603 }
Keith Busch159b67d2013-04-09 17:13:20 -0600604
605 if (split_len - length < bvec->bv_len)
606 return nvme_split_and_submit(bio, nvmeq, i, split_len,
607 split_len - length);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500608 length += bvec->bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500609 bvprv = bvec;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500610 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500611 iod->nents = nsegs;
Matthew Wilcox76830842011-02-10 13:55:39 -0500612 sg_mark_end(sg);
Keith Busch427e9702013-04-09 11:59:32 -0600613 if (dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir) == 0)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500614 return -ENOMEM;
Keith Busch427e9702013-04-09 11:59:32 -0600615
Keith Busch159b67d2013-04-09 17:13:20 -0600616 BUG_ON(length != bio->bi_size);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500617 return length;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500618}
619
Keith Busch0e5e4f02012-11-09 16:33:05 -0700620/*
621 * We reuse the small pool to allocate the 16-byte range here as it is not
622 * worth having a special pool for these or additional cases to handle freeing
623 * the iod.
624 */
625static int nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
626 struct bio *bio, struct nvme_iod *iod, int cmdid)
627{
628 struct nvme_dsm_range *range;
629 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
630
631 range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC,
632 &iod->first_dma);
633 if (!range)
634 return -ENOMEM;
635
636 iod_list(iod)[0] = (__le64 *)range;
637 iod->npages = 0;
638
639 range->cattr = cpu_to_le32(0);
640 range->nlb = cpu_to_le32(bio->bi_size >> ns->lba_shift);
Matthew Wilcox063cc6d2013-03-27 21:28:22 -0400641 range->slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_sector));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700642
643 memset(cmnd, 0, sizeof(*cmnd));
644 cmnd->dsm.opcode = nvme_cmd_dsm;
645 cmnd->dsm.command_id = cmdid;
646 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
647 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
648 cmnd->dsm.nr = 0;
649 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
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
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500658static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
659 int cmdid)
660{
661 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
662
663 memset(cmnd, 0, sizeof(*cmnd));
664 cmnd->common.opcode = nvme_cmd_flush;
665 cmnd->common.command_id = cmdid;
666 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
667
668 if (++nvmeq->sq_tail == nvmeq->q_depth)
669 nvmeq->sq_tail = 0;
670 writel(nvmeq->sq_tail, nvmeq->q_db);
671
672 return 0;
673}
674
Vishal Verma5d0f6132013-03-04 18:40:58 -0700675int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500676{
677 int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH,
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500678 special_completion, NVME_IO_TIMEOUT);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500679 if (unlikely(cmdid < 0))
680 return cmdid;
681
682 return nvme_submit_flush(nvmeq, ns, cmdid);
683}
684
Matthew Wilcox184d2942011-05-11 21:36:38 -0400685/*
686 * Called with local interrupts disabled and the q_lock held. May not sleep.
687 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500688static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
689 struct bio *bio)
690{
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500691 struct nvme_command *cmnd;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500692 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500693 enum dma_data_direction dma_dir;
Wei Yongjun1287dab2013-05-13 22:29:04 +0800694 int cmdid, length, result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500695 u16 control;
696 u32 dsmgmt;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500697 int psegs = bio_phys_segments(ns->queue, bio);
698
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500699 if ((bio->bi_rw & REQ_FLUSH) && psegs) {
700 result = nvme_submit_flush_data(nvmeq, ns);
701 if (result)
702 return result;
703 }
704
Wei Yongjun1287dab2013-05-13 22:29:04 +0800705 result = -ENOMEM;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500706 iod = nvme_alloc_iod(psegs, bio->bi_size, GFP_ATOMIC);
707 if (!iod)
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500708 goto nomem;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500709 iod->private = bio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500710
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500711 result = -EBUSY;
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500712 cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500713 if (unlikely(cmdid < 0))
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500714 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500715
Keith Busch0e5e4f02012-11-09 16:33:05 -0700716 if (bio->bi_rw & REQ_DISCARD) {
717 result = nvme_submit_discard(nvmeq, ns, bio, iod, cmdid);
718 if (result)
719 goto free_cmdid;
720 return result;
721 }
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500722 if ((bio->bi_rw & REQ_FLUSH) && !psegs)
723 return nvme_submit_flush(nvmeq, ns, cmdid);
724
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500725 control = 0;
726 if (bio->bi_rw & REQ_FUA)
727 control |= NVME_RW_FUA;
728 if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
729 control |= NVME_RW_LR;
730
731 dsmgmt = 0;
732 if (bio->bi_rw & REQ_RAHEAD)
733 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
734
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500735 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500736
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500737 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500738 if (bio_data_dir(bio)) {
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500739 cmnd->rw.opcode = nvme_cmd_write;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500740 dma_dir = DMA_TO_DEVICE;
741 } else {
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500742 cmnd->rw.opcode = nvme_cmd_read;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500743 dma_dir = DMA_FROM_DEVICE;
744 }
745
Keith Busch427e9702013-04-09 11:59:32 -0600746 result = nvme_map_bio(nvmeq, iod, bio, dma_dir, psegs);
747 if (result <= 0)
Keith Busch859361a2012-08-02 14:05:59 -0600748 goto free_cmdid;
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500749 length = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500750
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500751 cmnd->rw.command_id = cmdid;
752 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500753 length = nvme_setup_prps(nvmeq->dev, &cmnd->common, iod, length,
754 GFP_ATOMIC);
Matthew Wilcox063cc6d2013-03-27 21:28:22 -0400755 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_sector));
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500756 cmnd->rw.length = cpu_to_le16((length >> ns->lba_shift) - 1);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500757 cmnd->rw.control = cpu_to_le16(control);
758 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500759
Keith Busch61982212013-05-29 15:59:39 -0600760 nvme_start_io_acct(bio);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500761 if (++nvmeq->sq_tail == nvmeq->q_depth)
762 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500763 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500764
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500765 return 0;
766
Keith Busch859361a2012-08-02 14:05:59 -0600767 free_cmdid:
768 free_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500769 free_iod:
770 nvme_free_iod(nvmeq->dev, iod);
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500771 nomem:
772 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500773}
774
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400775static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500776{
Matthew Wilcox82123462011-01-20 13:24:06 -0500777 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500778
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500779 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500780 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500781
782 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400783 void *ctx;
784 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500785 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500786 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500787 break;
788 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
789 if (++head == nvmeq->q_depth) {
790 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500791 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500792 }
793
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400794 ctx = free_cmdid(nvmeq, cqe.command_id, &fn);
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500795 fn(nvmeq->dev, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500796 }
797
798 /* If the controller ignores the cq head doorbell and continuously
799 * writes to the queue, it is theoretically possible to wrap around
800 * the queue twice and mistakenly return IRQ_NONE. Linux only
801 * requires that 0.1% of your interrupts are handled, so this isn't
802 * a big problem.
803 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500804 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400805 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500806
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800807 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500808 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500809 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500810
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400811 nvmeq->cqe_seen = 1;
812 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500813}
814
Matthew Wilcox7d822452013-06-24 12:03:57 -0400815static void nvme_make_request(struct request_queue *q, struct bio *bio)
816{
817 struct nvme_ns *ns = q->queuedata;
818 struct nvme_queue *nvmeq = get_nvmeq(ns->dev);
819 int result = -EBUSY;
820
Keith Buschcd638942013-07-15 15:02:23 -0600821 if (!nvmeq) {
822 put_nvmeq(NULL);
823 bio_endio(bio, -EIO);
824 return;
825 }
826
Matthew Wilcox7d822452013-06-24 12:03:57 -0400827 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -0600828 if (!nvmeq->q_suspended && bio_list_empty(&nvmeq->sq_cong))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400829 result = nvme_submit_bio_queue(nvmeq, ns, bio);
830 if (unlikely(result)) {
831 if (bio_list_empty(&nvmeq->sq_cong))
832 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
833 bio_list_add(&nvmeq->sq_cong, bio);
834 }
835
836 nvme_process_cq(nvmeq);
837 spin_unlock_irq(&nvmeq->q_lock);
838 put_nvmeq(nvmeq);
839}
840
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500841static irqreturn_t nvme_irq(int irq, void *data)
842{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500843 irqreturn_t result;
844 struct nvme_queue *nvmeq = data;
845 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400846 nvme_process_cq(nvmeq);
847 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
848 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500849 spin_unlock(&nvmeq->q_lock);
850 return result;
851}
852
853static irqreturn_t nvme_irq_check(int irq, void *data)
854{
855 struct nvme_queue *nvmeq = data;
856 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
857 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
858 return IRQ_NONE;
859 return IRQ_WAKE_THREAD;
860}
861
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500862static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
863{
864 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400865 cancel_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500866 spin_unlock_irq(&nvmeq->q_lock);
867}
868
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400869struct sync_cmd_info {
870 struct task_struct *task;
871 u32 result;
872 int status;
873};
874
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500875static void sync_completion(struct nvme_dev *dev, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400876 struct nvme_completion *cqe)
877{
878 struct sync_cmd_info *cmdinfo = ctx;
879 cmdinfo->result = le32_to_cpup(&cqe->result);
880 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
881 wake_up_process(cmdinfo->task);
882}
883
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500884/*
885 * Returns 0 on success. If the result is negative, it's a Linux error code;
886 * if the result is positive, it's an NVM Express status code
887 */
Vishal Verma5d0f6132013-03-04 18:40:58 -0700888int nvme_submit_sync_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd,
889 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500890{
891 int cmdid;
892 struct sync_cmd_info cmdinfo;
893
894 cmdinfo.task = current;
895 cmdinfo.status = -EINTR;
896
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400897 cmdid = alloc_cmdid_killable(nvmeq, &cmdinfo, sync_completion,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500898 timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500899 if (cmdid < 0)
900 return cmdid;
901 cmd->common.command_id = cmdid;
902
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500903 set_current_state(TASK_KILLABLE);
904 nvme_submit_cmd(nvmeq, cmd);
Keith Busch78f8d252013-04-19 14:11:06 -0600905 schedule_timeout(timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500906
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500907 if (cmdinfo.status == -EINTR) {
908 nvme_abort_command(nvmeq, cmdid);
909 return -EINTR;
910 }
911
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500912 if (result)
913 *result = cmdinfo.result;
914
915 return cmdinfo.status;
916}
917
Keith Busch4d115422013-12-10 13:10:40 -0700918static int nvme_submit_async_cmd(struct nvme_queue *nvmeq,
919 struct nvme_command *cmd,
920 struct async_cmd_info *cmdinfo, unsigned timeout)
921{
922 int cmdid;
923
924 cmdid = alloc_cmdid_killable(nvmeq, cmdinfo, async_completion, timeout);
925 if (cmdid < 0)
926 return cmdid;
927 cmdinfo->status = -EINTR;
928 cmd->common.command_id = cmdid;
929 nvme_submit_cmd(nvmeq, cmd);
930 return 0;
931}
932
Vishal Verma5d0f6132013-03-04 18:40:58 -0700933int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500934 u32 *result)
935{
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500936 return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500937}
938
Keith Busch4d115422013-12-10 13:10:40 -0700939static int nvme_submit_admin_cmd_async(struct nvme_dev *dev,
940 struct nvme_command *cmd, struct async_cmd_info *cmdinfo)
941{
942 return nvme_submit_async_cmd(dev->queues[0], cmd, cmdinfo,
943 ADMIN_TIMEOUT);
944}
945
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500946static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
947{
948 int status;
949 struct nvme_command c;
950
951 memset(&c, 0, sizeof(c));
952 c.delete_queue.opcode = opcode;
953 c.delete_queue.qid = cpu_to_le16(id);
954
955 status = nvme_submit_admin_cmd(dev, &c, NULL);
956 if (status)
957 return -EIO;
958 return 0;
959}
960
961static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
962 struct nvme_queue *nvmeq)
963{
964 int status;
965 struct nvme_command c;
966 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
967
968 memset(&c, 0, sizeof(c));
969 c.create_cq.opcode = nvme_admin_create_cq;
970 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
971 c.create_cq.cqid = cpu_to_le16(qid);
972 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
973 c.create_cq.cq_flags = cpu_to_le16(flags);
974 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
975
976 status = nvme_submit_admin_cmd(dev, &c, NULL);
977 if (status)
978 return -EIO;
979 return 0;
980}
981
982static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
983 struct nvme_queue *nvmeq)
984{
985 int status;
986 struct nvme_command c;
987 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
988
989 memset(&c, 0, sizeof(c));
990 c.create_sq.opcode = nvme_admin_create_sq;
991 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
992 c.create_sq.sqid = cpu_to_le16(qid);
993 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
994 c.create_sq.sq_flags = cpu_to_le16(flags);
995 c.create_sq.cqid = cpu_to_le16(qid);
996
997 status = nvme_submit_admin_cmd(dev, &c, NULL);
998 if (status)
999 return -EIO;
1000 return 0;
1001}
1002
1003static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1004{
1005 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1006}
1007
1008static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1009{
1010 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1011}
1012
Vishal Verma5d0f6132013-03-04 18:40:58 -07001013int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001014 dma_addr_t dma_addr)
1015{
1016 struct nvme_command c;
1017
1018 memset(&c, 0, sizeof(c));
1019 c.identify.opcode = nvme_admin_identify;
1020 c.identify.nsid = cpu_to_le32(nsid);
1021 c.identify.prp1 = cpu_to_le64(dma_addr);
1022 c.identify.cns = cpu_to_le32(cns);
1023
1024 return nvme_submit_admin_cmd(dev, &c, NULL);
1025}
1026
Vishal Verma5d0f6132013-03-04 18:40:58 -07001027int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -06001028 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001029{
1030 struct nvme_command c;
1031
1032 memset(&c, 0, sizeof(c));
1033 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -06001034 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001035 c.features.prp1 = cpu_to_le64(dma_addr);
1036 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001037
Keith Busch08df1e02012-09-21 10:52:13 -06001038 return nvme_submit_admin_cmd(dev, &c, result);
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001039}
1040
Vishal Verma5d0f6132013-03-04 18:40:58 -07001041int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
1042 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001043{
1044 struct nvme_command c;
1045
1046 memset(&c, 0, sizeof(c));
1047 c.features.opcode = nvme_admin_set_features;
1048 c.features.prp1 = cpu_to_le64(dma_addr);
1049 c.features.fid = cpu_to_le32(fid);
1050 c.features.dword11 = cpu_to_le32(dword11);
1051
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001052 return nvme_submit_admin_cmd(dev, &c, result);
1053}
1054
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001055/**
Keith Buschc30341d2013-12-10 13:10:38 -07001056 * nvme_abort_cmd - Attempt aborting a command
1057 * @cmdid: Command id of a timed out IO
1058 * @queue: The queue with timed out IO
1059 *
1060 * Schedule controller reset if the command was already aborted once before and
1061 * still hasn't been returned to the driver, or if this is the admin queue.
1062 */
1063static void nvme_abort_cmd(int cmdid, struct nvme_queue *nvmeq)
1064{
1065 int a_cmdid;
1066 struct nvme_command cmd;
1067 struct nvme_dev *dev = nvmeq->dev;
1068 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
1069
1070 if (!nvmeq->qid || info[cmdid].aborted) {
1071 if (work_busy(&dev->reset_work))
1072 return;
1073 list_del_init(&dev->node);
1074 dev_warn(&dev->pci_dev->dev,
1075 "I/O %d QID %d timeout, reset controller\n", cmdid,
1076 nvmeq->qid);
1077 INIT_WORK(&dev->reset_work, nvme_reset_failed_dev);
1078 queue_work(nvme_workq, &dev->reset_work);
1079 return;
1080 }
1081
1082 if (!dev->abort_limit)
1083 return;
1084
1085 a_cmdid = alloc_cmdid(dev->queues[0], CMD_CTX_ABORT, special_completion,
1086 ADMIN_TIMEOUT);
1087 if (a_cmdid < 0)
1088 return;
1089
1090 memset(&cmd, 0, sizeof(cmd));
1091 cmd.abort.opcode = nvme_admin_abort_cmd;
1092 cmd.abort.cid = cmdid;
1093 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
1094 cmd.abort.command_id = a_cmdid;
1095
1096 --dev->abort_limit;
1097 info[cmdid].aborted = 1;
1098 info[cmdid].timeout = jiffies + ADMIN_TIMEOUT;
1099
1100 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", cmdid,
1101 nvmeq->qid);
1102 nvme_submit_cmd(dev->queues[0], &cmd);
1103}
1104
1105/**
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001106 * nvme_cancel_ios - Cancel outstanding I/Os
1107 * @queue: The queue to cancel I/Os on
1108 * @timeout: True to only cancel I/Os which have timed out
1109 */
1110static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout)
1111{
1112 int depth = nvmeq->q_depth - 1;
1113 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
1114 unsigned long now = jiffies;
1115 int cmdid;
1116
1117 for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
1118 void *ctx;
1119 nvme_completion_fn fn;
1120 static struct nvme_completion cqe = {
Matthew Wilcoxaf2d9ca2013-04-16 15:18:30 -04001121 .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1),
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001122 };
1123
1124 if (timeout && !time_after(now, info[cmdid].timeout))
1125 continue;
Keith Busch053ab7022013-04-30 11:19:38 -06001126 if (info[cmdid].ctx == CMD_CTX_CANCELLED)
1127 continue;
Keith Buschc30341d2013-12-10 13:10:38 -07001128 if (timeout && nvmeq->dev->initialized) {
1129 nvme_abort_cmd(cmdid, nvmeq);
1130 continue;
1131 }
1132 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n", cmdid,
1133 nvmeq->qid);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001134 ctx = cancel_cmdid(nvmeq, cmdid, &fn);
1135 fn(nvmeq->dev, ctx, &cqe);
1136 }
1137}
1138
Keith Busch22404272013-07-15 15:02:20 -06001139static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001140{
Keith Busch22404272013-07-15 15:02:20 -06001141 spin_lock_irq(&nvmeq->q_lock);
1142 while (bio_list_peek(&nvmeq->sq_cong)) {
1143 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1144 bio_endio(bio, -EIO);
1145 }
1146 spin_unlock_irq(&nvmeq->q_lock);
1147
Matthew Wilcox9e866772012-08-03 13:55:56 -04001148 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1149 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1150 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1151 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1152 kfree(nvmeq);
1153}
1154
Keith Busch22404272013-07-15 15:02:20 -06001155static void nvme_free_queues(struct nvme_dev *dev)
1156{
1157 int i;
1158
1159 for (i = dev->queue_count - 1; i >= 0; i--) {
1160 nvme_free_queue(dev->queues[i]);
1161 dev->queue_count--;
1162 dev->queues[i] = NULL;
1163 }
1164}
1165
Keith Busch4d115422013-12-10 13:10:40 -07001166/**
1167 * nvme_suspend_queue - put queue into suspended state
1168 * @nvmeq - queue to suspend
1169 *
1170 * Returns 1 if already suspended, 0 otherwise.
1171 */
1172static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001173{
Keith Busch4d115422013-12-10 13:10:40 -07001174 int vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001175
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001176 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001177 if (nvmeq->q_suspended) {
1178 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07001179 return 1;
Keith Busch32958742012-08-20 14:57:49 -06001180 }
Keith Busch22404272013-07-15 15:02:20 -06001181 nvmeq->q_suspended = 1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001182 spin_unlock_irq(&nvmeq->q_lock);
1183
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001184 irq_set_affinity_hint(vector, NULL);
1185 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001186
Keith Busch4d115422013-12-10 13:10:40 -07001187 return 0;
1188}
1189
1190static void nvme_clear_queue(struct nvme_queue *nvmeq)
1191{
1192 spin_lock_irq(&nvmeq->q_lock);
1193 nvme_process_cq(nvmeq);
1194 nvme_cancel_ios(nvmeq, false);
1195 spin_unlock_irq(&nvmeq->q_lock);
1196}
1197
1198static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1199{
1200 struct nvme_queue *nvmeq = dev->queues[qid];
1201
1202 if (!nvmeq)
1203 return;
1204 if (nvme_suspend_queue(nvmeq))
1205 return;
1206
Keith Busch0e53d182013-12-10 13:10:39 -07001207 /* Don't tell the adapter to delete the admin queue.
1208 * Don't tell a removed adapter to delete IO queues. */
1209 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001210 adapter_delete_sq(dev, qid);
1211 adapter_delete_cq(dev, qid);
1212 }
Keith Busch4d115422013-12-10 13:10:40 -07001213 nvme_clear_queue(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001214}
1215
1216static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
1217 int depth, int vector)
1218{
1219 struct device *dmadev = &dev->pci_dev->dev;
Keith Busch22404272013-07-15 15:02:20 -06001220 unsigned extra = nvme_queue_extra(depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001221 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
1222 if (!nvmeq)
1223 return NULL;
1224
1225 nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
1226 &nvmeq->cq_dma_addr, GFP_KERNEL);
1227 if (!nvmeq->cqes)
1228 goto free_nvmeq;
1229 memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
1230
1231 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
1232 &nvmeq->sq_dma_addr, GFP_KERNEL);
1233 if (!nvmeq->sq_cmds)
1234 goto free_cqdma;
1235
1236 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001237 nvmeq->dev = dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001238 spin_lock_init(&nvmeq->q_lock);
1239 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001240 nvmeq->cq_phase = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001241 init_waitqueue_head(&nvmeq->sq_full);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001242 init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001243 bio_list_init(&nvmeq->sq_cong);
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001244 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001245 nvmeq->q_depth = depth;
1246 nvmeq->cq_vector = vector;
Keith Buschc30341d2013-12-10 13:10:38 -07001247 nvmeq->qid = qid;
Keith Busch22404272013-07-15 15:02:20 -06001248 nvmeq->q_suspended = 1;
1249 dev->queue_count++;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001250
1251 return nvmeq;
1252
1253 free_cqdma:
Keith Busch68b8eca2013-05-01 13:07:47 -06001254 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001255 nvmeq->cq_dma_addr);
1256 free_nvmeq:
1257 kfree(nvmeq);
1258 return NULL;
1259}
1260
Matthew Wilcox30010822011-01-20 09:10:15 -05001261static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1262 const char *name)
1263{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001264 if (use_threaded_interrupts)
1265 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001266 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001267 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001268 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001269 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001270}
1271
Keith Busch22404272013-07-15 15:02:20 -06001272static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001273{
Keith Busch22404272013-07-15 15:02:20 -06001274 struct nvme_dev *dev = nvmeq->dev;
1275 unsigned extra = nvme_queue_extra(nvmeq->q_depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001276
Keith Busch22404272013-07-15 15:02:20 -06001277 nvmeq->sq_tail = 0;
1278 nvmeq->cq_head = 0;
1279 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001280 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001281 memset(nvmeq->cmdid_data, 0, extra);
1282 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
1283 nvme_cancel_ios(nvmeq, false);
1284 nvmeq->q_suspended = 0;
1285}
1286
1287static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1288{
1289 struct nvme_dev *dev = nvmeq->dev;
1290 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001291
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001292 result = adapter_alloc_cq(dev, qid, nvmeq);
1293 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001294 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001295
1296 result = adapter_alloc_sq(dev, qid, nvmeq);
1297 if (result < 0)
1298 goto release_cq;
1299
Matthew Wilcox30010822011-01-20 09:10:15 -05001300 result = queue_request_irq(dev, nvmeq, "nvme");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001301 if (result < 0)
1302 goto release_sq;
1303
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001304 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001305 nvme_init_queue(nvmeq, qid);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001306 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001307
1308 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001309
1310 release_sq:
1311 adapter_delete_sq(dev, qid);
1312 release_cq:
1313 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001314 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001315}
1316
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001317static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1318{
1319 unsigned long timeout;
1320 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1321
1322 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1323
1324 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1325 msleep(100);
1326 if (fatal_signal_pending(current))
1327 return -EINTR;
1328 if (time_after(jiffies, timeout)) {
1329 dev_err(&dev->pci_dev->dev,
1330 "Device not ready; aborting initialisation\n");
1331 return -ENODEV;
1332 }
1333 }
1334
1335 return 0;
1336}
1337
1338/*
1339 * If the device has been passed off to us in an enabled state, just clear
1340 * the enabled bit. The spec says we should set the 'shutdown notification
1341 * bits', but doing so may cause the device to complete commands to the
1342 * admin queue ... and we don't know what memory that might be pointing at!
1343 */
1344static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1345{
Matthew Wilcox44af1462013-05-04 06:43:17 -04001346 u32 cc = readl(&dev->bar->cc);
1347
1348 if (cc & NVME_CC_ENABLE)
1349 writel(cc & ~NVME_CC_ENABLE, &dev->bar->cc);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001350 return nvme_wait_ready(dev, cap, false);
1351}
1352
1353static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1354{
1355 return nvme_wait_ready(dev, cap, true);
1356}
1357
Keith Busch1894d8f2013-07-15 15:02:22 -06001358static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1359{
1360 unsigned long timeout;
1361 u32 cc;
1362
1363 cc = (readl(&dev->bar->cc) & ~NVME_CC_SHN_MASK) | NVME_CC_SHN_NORMAL;
1364 writel(cc, &dev->bar->cc);
1365
1366 timeout = 2 * HZ + jiffies;
1367 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1368 NVME_CSTS_SHST_CMPLT) {
1369 msleep(100);
1370 if (fatal_signal_pending(current))
1371 return -EINTR;
1372 if (time_after(jiffies, timeout)) {
1373 dev_err(&dev->pci_dev->dev,
1374 "Device shutdown incomplete; abort shutdown\n");
1375 return -ENODEV;
1376 }
1377 }
1378
1379 return 0;
1380}
1381
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001382static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001383{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001384 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001385 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001386 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001387 struct nvme_queue *nvmeq;
1388
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001389 result = nvme_disable_ctrl(dev, cap);
1390 if (result < 0)
1391 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001392
Keith Buschcd638942013-07-15 15:02:23 -06001393 nvmeq = dev->queues[0];
1394 if (!nvmeq) {
1395 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
1396 if (!nvmeq)
1397 return -ENOMEM;
1398 dev->queues[0] = nvmeq;
1399 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001400
1401 aqa = nvmeq->q_depth - 1;
1402 aqa |= aqa << 16;
1403
1404 dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
1405 dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
1406 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001407 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001408
1409 writel(aqa, &dev->bar->aqa);
1410 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1411 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
1412 writel(dev->ctrl_config, &dev->bar->cc);
1413
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001414 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001415 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001416 return result;
Matthew Wilcox9e866772012-08-03 13:55:56 -04001417
Matthew Wilcox30010822011-01-20 09:10:15 -05001418 result = queue_request_irq(dev, nvmeq, "nvme admin");
Keith Busch025c5572013-05-01 13:07:51 -06001419 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001420 return result;
Keith Busch025c5572013-05-01 13:07:51 -06001421
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001422 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001423 nvme_init_queue(nvmeq, 0);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001424 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001425 return result;
1426}
1427
Vishal Verma5d0f6132013-03-04 18:40:58 -07001428struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001429 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001430{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001431 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001432 struct scatterlist *sg;
1433 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001434 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001435
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001436 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001437 return ERR_PTR(-EINVAL);
Dan Carpenter5460fc02013-05-13 17:59:50 +03001438 if (!length || length > INT_MAX - PAGE_SIZE)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001439 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001440
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001441 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001442 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1443 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001444 if (!pages)
1445 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001446
1447 err = get_user_pages_fast(addr, count, 1, pages);
1448 if (err < count) {
1449 count = err;
1450 err = -EFAULT;
1451 goto put_pages;
1452 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001453
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001454 iod = nvme_alloc_iod(count, length, GFP_KERNEL);
1455 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001456 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001457 for (i = 0; i < count; i++) {
1458 sg_set_page(&sg[i], pages[i],
Dan Carpenter5460fc02013-05-13 17:59:50 +03001459 min_t(unsigned, length, PAGE_SIZE - offset),
1460 offset);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001461 length -= (PAGE_SIZE - offset);
1462 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001463 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001464 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001465 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001466
1467 err = -ENOMEM;
1468 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1469 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001470 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001471 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001472
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001473 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001474 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001475
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001476 free_iod:
1477 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001478 put_pages:
1479 for (i = 0; i < count; i++)
1480 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001481 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001482 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001483}
1484
Vishal Verma5d0f6132013-03-04 18:40:58 -07001485void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001486 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001487{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001488 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001489
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001490 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1491 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001492
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001493 for (i = 0; i < iod->nents; i++)
1494 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001495}
1496
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001497static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1498{
1499 struct nvme_dev *dev = ns->dev;
1500 struct nvme_queue *nvmeq;
1501 struct nvme_user_io io;
1502 struct nvme_command c;
Keith Buschf410c682013-04-23 17:23:59 -06001503 unsigned length, meta_len;
1504 int status, i;
1505 struct nvme_iod *iod, *meta_iod = NULL;
1506 dma_addr_t meta_dma_addr;
1507 void *meta, *uninitialized_var(meta_mem);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001508
1509 if (copy_from_user(&io, uio, sizeof(io)))
1510 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001511 length = (io.nblocks + 1) << ns->lba_shift;
Keith Buschf410c682013-04-23 17:23:59 -06001512 meta_len = (io.nblocks + 1) * ns->ms;
1513
1514 if (meta_len && ((io.metadata & 3) || !io.metadata))
1515 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001516
1517 switch (io.opcode) {
1518 case nvme_cmd_write:
1519 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001520 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001521 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001522 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001523 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001524 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001525 }
1526
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001527 if (IS_ERR(iod))
1528 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001529
1530 memset(&c, 0, sizeof(c));
1531 c.rw.opcode = io.opcode;
1532 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001533 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001534 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001535 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001536 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001537 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1538 c.rw.reftag = cpu_to_le32(io.reftag);
1539 c.rw.apptag = cpu_to_le16(io.apptag);
1540 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buschf410c682013-04-23 17:23:59 -06001541
1542 if (meta_len) {
Keith Busch1b567492013-07-18 12:13:51 -06001543 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
1544 meta_len);
Keith Buschf410c682013-04-23 17:23:59 -06001545 if (IS_ERR(meta_iod)) {
1546 status = PTR_ERR(meta_iod);
1547 meta_iod = NULL;
1548 goto unmap;
1549 }
1550
1551 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1552 &meta_dma_addr, GFP_KERNEL);
1553 if (!meta_mem) {
1554 status = -ENOMEM;
1555 goto unmap;
1556 }
1557
1558 if (io.opcode & 1) {
1559 int meta_offset = 0;
1560
1561 for (i = 0; i < meta_iod->nents; i++) {
1562 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1563 meta_iod->sg[i].offset;
1564 memcpy(meta_mem + meta_offset, meta,
1565 meta_iod->sg[i].length);
1566 kunmap_atomic(meta);
1567 meta_offset += meta_iod->sg[i].length;
1568 }
1569 }
1570
1571 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1572 }
1573
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001574 length = nvme_setup_prps(dev, &c.common, iod, length, GFP_KERNEL);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001575
Matthew Wilcox040a93b2011-12-20 11:04:12 -05001576 nvmeq = get_nvmeq(dev);
Matthew Wilcoxfa922822011-03-16 16:29:00 -04001577 /*
1578 * Since nvme_submit_sync_cmd sleeps, we can't keep preemption
Matthew Wilcoxb1ad37e2011-02-04 16:14:30 -05001579 * disabled. We may be preempted at any point, and be rescheduled
1580 * to a different CPU. That will cause cacheline bouncing, but no
1581 * additional races since q_lock already protects against other CPUs.
1582 */
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001583 put_nvmeq(nvmeq);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001584 if (length != (io.nblocks + 1) << ns->lba_shift)
1585 status = -ENOMEM;
Keith Busch22404272013-07-15 15:02:20 -06001586 else if (!nvmeq || nvmeq->q_suspended)
1587 status = -EBUSY;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001588 else
Matthew Wilcoxff976d72011-12-20 13:53:01 -05001589 status = nvme_submit_sync_cmd(nvmeq, &c, NULL, NVME_IO_TIMEOUT);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001590
Keith Buschf410c682013-04-23 17:23:59 -06001591 if (meta_len) {
1592 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1593 int meta_offset = 0;
1594
1595 for (i = 0; i < meta_iod->nents; i++) {
1596 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1597 meta_iod->sg[i].offset;
1598 memcpy(meta, meta_mem + meta_offset,
1599 meta_iod->sg[i].length);
1600 kunmap_atomic(meta);
1601 meta_offset += meta_iod->sg[i].length;
1602 }
1603 }
1604
1605 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1606 meta_dma_addr);
1607 }
1608
1609 unmap:
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001610 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001611 nvme_free_iod(dev, iod);
Keith Buschf410c682013-04-23 17:23:59 -06001612
1613 if (meta_iod) {
1614 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1615 nvme_free_iod(dev, meta_iod);
1616 }
1617
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001618 return status;
1619}
1620
Keith Busch50af8ba2012-07-25 16:07:55 -06001621static int nvme_user_admin_cmd(struct nvme_dev *dev,
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001622 struct nvme_admin_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001623{
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001624 struct nvme_admin_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001625 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001626 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001627 struct nvme_iod *uninitialized_var(iod);
Keith Busch94f370c2013-05-09 14:01:38 -06001628 unsigned timeout;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001629
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001630 if (!capable(CAP_SYS_ADMIN))
1631 return -EACCES;
1632 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001633 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001634
1635 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001636 c.common.opcode = cmd.opcode;
1637 c.common.flags = cmd.flags;
1638 c.common.nsid = cpu_to_le32(cmd.nsid);
1639 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1640 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1641 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1642 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1643 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1644 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1645 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1646 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1647
1648 length = cmd.data_len;
1649 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001650 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1651 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001652 if (IS_ERR(iod))
1653 return PTR_ERR(iod);
1654 length = nvme_setup_prps(dev, &c.common, iod, length,
1655 GFP_KERNEL);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001656 }
1657
Keith Busch94f370c2013-05-09 14:01:38 -06001658 timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
1659 ADMIN_TIMEOUT;
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001660 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001661 status = -ENOMEM;
1662 else
Keith Busch94f370c2013-05-09 14:01:38 -06001663 status = nvme_submit_sync_cmd(dev->queues[0], &c, &cmd.result,
1664 timeout);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001665
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001666 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001667 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001668 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001669 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001670
Chayan Biswascf90bc42013-05-22 22:34:49 +00001671 if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
Keith Buschf4f117f2012-09-21 10:49:05 -06001672 sizeof(cmd.result)))
1673 status = -EFAULT;
1674
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001675 return status;
1676}
1677
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001678static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1679 unsigned long arg)
1680{
1681 struct nvme_ns *ns = bdev->bd_disk->private_data;
1682
1683 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001684 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001685 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001686 return ns->ns_id;
1687 case NVME_IOCTL_ADMIN_CMD:
Keith Busch50af8ba2012-07-25 16:07:55 -06001688 return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001689 case NVME_IOCTL_SUBMIT_IO:
1690 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001691 case SG_GET_VERSION_NUM:
1692 return nvme_sg_get_version_num((void __user *)arg);
1693 case SG_IO:
1694 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001695 default:
1696 return -ENOTTY;
1697 }
1698}
1699
Keith Busch320a3822013-10-23 13:07:34 -06001700#ifdef CONFIG_COMPAT
1701static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1702 unsigned int cmd, unsigned long arg)
1703{
1704 struct nvme_ns *ns = bdev->bd_disk->private_data;
1705
1706 switch (cmd) {
1707 case SG_IO:
1708 return nvme_sg_io32(ns, arg);
1709 }
1710 return nvme_ioctl(bdev, mode, cmd, arg);
1711}
1712#else
1713#define nvme_compat_ioctl NULL
1714#endif
1715
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001716static const struct block_device_operations nvme_fops = {
1717 .owner = THIS_MODULE,
1718 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06001719 .compat_ioctl = nvme_compat_ioctl,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001720};
1721
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001722static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1723{
1724 while (bio_list_peek(&nvmeq->sq_cong)) {
1725 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1726 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Keith Busch427e9702013-04-09 11:59:32 -06001727
Matthew Wilcox3cb967c2011-03-16 16:45:49 -04001728 if (bio_list_empty(&nvmeq->sq_cong))
1729 remove_wait_queue(&nvmeq->sq_full,
1730 &nvmeq->sq_cong_wait);
Keith Busch427e9702013-04-09 11:59:32 -06001731 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
1732 if (bio_list_empty(&nvmeq->sq_cong))
1733 add_wait_queue(&nvmeq->sq_full,
1734 &nvmeq->sq_cong_wait);
1735 bio_list_add_head(&nvmeq->sq_cong, bio);
1736 break;
1737 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001738 }
1739}
1740
1741static int nvme_kthread(void *data)
1742{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001743 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001744
1745 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001746 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001747 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001748 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001749 int i;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001750 if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
1751 dev->initialized) {
1752 if (work_busy(&dev->reset_work))
1753 continue;
1754 list_del_init(&dev->node);
1755 dev_warn(&dev->pci_dev->dev,
1756 "Failed status, reset controller\n");
1757 INIT_WORK(&dev->reset_work,
1758 nvme_reset_failed_dev);
1759 queue_work(nvme_workq, &dev->reset_work);
1760 continue;
1761 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001762 for (i = 0; i < dev->queue_count; i++) {
1763 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001764 if (!nvmeq)
1765 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001766 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001767 if (nvmeq->q_suspended)
1768 goto unlock;
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001769 nvme_process_cq(nvmeq);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001770 nvme_cancel_ios(nvmeq, true);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001771 nvme_resubmit_bios(nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001772 unlock:
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001773 spin_unlock_irq(&nvmeq->q_lock);
1774 }
1775 }
1776 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001777 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001778 }
1779 return 0;
1780}
1781
Keith Busch0e5e4f02012-11-09 16:33:05 -07001782static void nvme_config_discard(struct nvme_ns *ns)
1783{
1784 u32 logical_block_size = queue_logical_block_size(ns->queue);
1785 ns->queue->limits.discard_zeroes_data = 0;
1786 ns->queue->limits.discard_alignment = logical_block_size;
1787 ns->queue->limits.discard_granularity = logical_block_size;
1788 ns->queue->limits.max_discard_sectors = 0xffffffff;
1789 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1790}
1791
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001792static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001793 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1794{
1795 struct nvme_ns *ns;
1796 struct gendisk *disk;
1797 int lbaf;
1798
1799 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1800 return NULL;
1801
1802 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1803 if (!ns)
1804 return NULL;
1805 ns->queue = blk_alloc_queue(GFP_KERNEL);
1806 if (!ns->queue)
1807 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001808 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
1809 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1810 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001811 blk_queue_make_request(ns->queue, nvme_make_request);
1812 ns->dev = dev;
1813 ns->queue->queuedata = ns;
1814
Matthew Wilcox469071a2013-12-09 12:58:46 -05001815 disk = alloc_disk(0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001816 if (!disk)
1817 goto out_free_queue;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001818 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001819 ns->disk = disk;
1820 lbaf = id->flbas & 0xf;
1821 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Buschf410c682013-04-23 17:23:59 -06001822 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busche9ef4632012-07-24 15:01:04 -06001823 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06001824 if (dev->max_hw_sectors)
1825 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001826
1827 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001828 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001829 disk->fops = &nvme_fops;
1830 disk->private_data = ns;
1831 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05001832 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001833 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001834 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001835 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1836
Keith Busch0e5e4f02012-11-09 16:33:05 -07001837 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1838 nvme_config_discard(ns);
1839
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001840 return ns;
1841
1842 out_free_queue:
1843 blk_cleanup_queue(ns->queue);
1844 out_free_ns:
1845 kfree(ns);
1846 return NULL;
1847}
1848
1849static void nvme_ns_free(struct nvme_ns *ns)
1850{
1851 put_disk(ns->disk);
1852 blk_cleanup_queue(ns->queue);
1853 kfree(ns);
1854}
1855
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001856static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001857{
1858 int status;
1859 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001860 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001861
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001862 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001863 &result);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001864 if (status)
Keith Busch7e03b122013-07-29 16:20:56 -06001865 return status < 0 ? -EIO : -EBUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001866 return min(result & 0xffff, result >> 16) + 1;
1867}
1868
Keith Busch9d713c22013-07-15 15:02:24 -06001869static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1870{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001871 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06001872}
1873
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001874static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001875{
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001876 struct pci_dev *pdev = dev->pci_dev;
Keith Busch9d713c22013-07-15 15:02:24 -06001877 int result, cpu, i, vecs, nr_io_queues, size, q_depth;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001878
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001879 nr_io_queues = num_online_cpus();
1880 result = set_queue_count(dev, nr_io_queues);
Matthew Wilcox1b234842011-01-20 13:01:49 -05001881 if (result < 0)
1882 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001883 if (result < nr_io_queues)
1884 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001885
Keith Busch9d713c22013-07-15 15:02:24 -06001886 size = db_bar_size(dev, nr_io_queues);
1887 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001888 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06001889 do {
1890 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1891 if (dev->bar)
1892 break;
1893 if (!--nr_io_queues)
1894 return -ENOMEM;
1895 size = db_bar_size(dev, nr_io_queues);
1896 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001897 dev->dbs = ((void __iomem *)dev->bar) + 4096;
1898 dev->queues[0]->q_db = dev->dbs;
1899 }
1900
Keith Busch9d713c22013-07-15 15:02:24 -06001901 /* Deregister the admin queue's interrupt */
1902 free_irq(dev->entry[0].vector, dev->queues[0]);
1903
Matthew Wilcox063a8092013-06-20 10:53:48 -04001904 vecs = nr_io_queues;
1905 for (i = 0; i < vecs; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001906 dev->entry[i].entry = i;
1907 for (;;) {
Matthew Wilcox063a8092013-06-20 10:53:48 -04001908 result = pci_enable_msix(pdev, dev->entry, vecs);
1909 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001910 break;
Matthew Wilcox063a8092013-06-20 10:53:48 -04001911 vecs = result;
1912 }
1913
1914 if (result < 0) {
1915 vecs = nr_io_queues;
1916 if (vecs > 32)
1917 vecs = 32;
1918 for (;;) {
1919 result = pci_enable_msi_block(pdev, vecs);
1920 if (result == 0) {
1921 for (i = 0; i < vecs; i++)
1922 dev->entry[i].vector = i + pdev->irq;
1923 break;
1924 } else if (result < 0) {
1925 vecs = 1;
1926 break;
1927 }
1928 vecs = result;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001929 }
1930 }
1931
Matthew Wilcox063a8092013-06-20 10:53:48 -04001932 /*
1933 * Should investigate if there's a performance win from allocating
1934 * more queues than interrupt vectors; it might allow the submission
1935 * path to scale better, even if the receive path is limited by the
1936 * number of interrupts.
1937 */
1938 nr_io_queues = vecs;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001939
Matthew Wilcox1b234842011-01-20 13:01:49 -05001940 result = queue_request_irq(dev, dev->queues[0], "nvme admin");
Keith Busch9d713c22013-07-15 15:02:24 -06001941 if (result) {
1942 dev->queues[0]->q_suspended = 1;
Keith Busch22404272013-07-15 15:02:20 -06001943 goto free_queues;
Keith Busch9d713c22013-07-15 15:02:24 -06001944 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001945
Keith Buschcd638942013-07-15 15:02:23 -06001946 /* Free previously allocated queues that are no longer usable */
1947 spin_lock(&dev_list_lock);
1948 for (i = dev->queue_count - 1; i > nr_io_queues; i--) {
1949 struct nvme_queue *nvmeq = dev->queues[i];
1950
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001951 spin_lock_irq(&nvmeq->q_lock);
Keith Buschcd638942013-07-15 15:02:23 -06001952 nvme_cancel_ios(nvmeq, false);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001953 spin_unlock_irq(&nvmeq->q_lock);
Keith Buschcd638942013-07-15 15:02:23 -06001954
1955 nvme_free_queue(nvmeq);
1956 dev->queue_count--;
1957 dev->queues[i] = NULL;
1958 }
1959 spin_unlock(&dev_list_lock);
1960
Matthew Wilcox1b234842011-01-20 13:01:49 -05001961 cpu = cpumask_first(cpu_online_mask);
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001962 for (i = 0; i < nr_io_queues; i++) {
Matthew Wilcox1b234842011-01-20 13:01:49 -05001963 irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu));
1964 cpu = cpumask_next(cpu, cpu_online_mask);
1965 }
1966
Keith Buscha0cadb82012-07-27 13:57:23 -04001967 q_depth = min_t(int, NVME_CAP_MQES(readq(&dev->bar->cap)) + 1,
1968 NVME_Q_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001969 for (i = dev->queue_count - 1; i < nr_io_queues; i++) {
Keith Busch22404272013-07-15 15:02:20 -06001970 dev->queues[i + 1] = nvme_alloc_queue(dev, i + 1, q_depth, i);
1971 if (!dev->queues[i + 1]) {
1972 result = -ENOMEM;
1973 goto free_queues;
1974 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001975 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001976
Matthew Wilcox9ecdc942011-03-16 16:52:19 -04001977 for (; i < num_possible_cpus(); i++) {
1978 int target = i % rounddown_pow_of_two(dev->queue_count - 1);
1979 dev->queues[i + 1] = dev->queues[target + 1];
1980 }
1981
Keith Busch22404272013-07-15 15:02:20 -06001982 for (i = 1; i < dev->queue_count; i++) {
1983 result = nvme_create_queue(dev->queues[i], i);
1984 if (result) {
1985 for (--i; i > 0; i--)
1986 nvme_disable_queue(dev, i);
1987 goto free_queues;
1988 }
1989 }
1990
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001991 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001992
Keith Busch22404272013-07-15 15:02:20 -06001993 free_queues:
1994 nvme_free_queues(dev);
1995 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001996}
1997
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04001998/*
1999 * Return: error value if an error occurred setting up the queues or calling
2000 * Identify Device. 0 if these succeeded, even if adding some of the
2001 * namespaces failed. At the moment, these failures are silent. TBD which
2002 * failures should be reported.
2003 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002004static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002005{
Matthew Wilcox68608c262013-06-21 14:36:34 -04002006 struct pci_dev *pdev = dev->pci_dev;
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002007 int res;
2008 unsigned nn, i;
Keith Buschcbb62182013-05-01 13:07:49 -06002009 struct nvme_ns *ns;
Matthew Wilcox51814232011-02-01 16:18:08 -05002010 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002011 struct nvme_id_ns *id_ns;
2012 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002013 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06002014 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002015
Matthew Wilcox68608c262013-06-21 14:36:34 -04002016 mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06002017 if (!mem)
2018 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002019
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002020 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002021 if (res) {
2022 res = -EIO;
Keith Buschcbb62182013-05-01 13:07:49 -06002023 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002024 }
2025
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002026 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05002027 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002028 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002029 dev->abort_limit = ctrl->acl + 1;
Matthew Wilcox51814232011-02-01 16:18:08 -05002030 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2031 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2032 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002033 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002034 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002035 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
2036 (pdev->device == 0x0953) && ctrl->vs[3])
Keith Busch159b67d2013-04-09 17:13:20 -06002037 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002038
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002039 id_ns = mem;
Matthew Wilcox2b2c1892011-10-07 13:10:13 -04002040 for (i = 1; i <= nn; i++) {
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002041 res = nvme_identify(dev, i, 0, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002042 if (res)
2043 continue;
2044
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002045 if (id_ns->ncap == 0)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002046 continue;
2047
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002048 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
Keith Busch08df1e02012-09-21 10:52:13 -06002049 dma_addr + 4096, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002050 if (res)
Keith Busch12209032013-01-31 14:40:38 -07002051 memset(mem + 4096, 0, 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002052
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002053 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002054 if (ns)
2055 list_add_tail(&ns->list, &dev->namespaces);
2056 }
2057 list_for_each_entry(ns, &dev->namespaces, list)
2058 add_disk(ns->disk);
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002059 res = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002060
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002061 out:
Matthew Wilcox684f5c22011-09-19 17:14:53 -04002062 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002063 return res;
2064}
2065
Keith Busch0877cb02013-07-15 15:02:19 -06002066static int nvme_dev_map(struct nvme_dev *dev)
2067{
2068 int bars, result = -ENOMEM;
2069 struct pci_dev *pdev = dev->pci_dev;
2070
2071 if (pci_enable_device_mem(pdev))
2072 return result;
2073
2074 dev->entry[0].vector = pdev->irq;
2075 pci_set_master(pdev);
2076 bars = pci_select_bars(pdev, IORESOURCE_MEM);
2077 if (pci_request_selected_regions(pdev, bars, "nvme"))
2078 goto disable_pci;
2079
Russell King052d0ef2013-06-26 23:49:11 +01002080 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
2081 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2082 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002083
Keith Busch0877cb02013-07-15 15:02:19 -06002084 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2085 if (!dev->bar)
2086 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07002087 if (readl(&dev->bar->csts) == -1) {
2088 result = -ENODEV;
2089 goto unmap;
2090 }
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002091 dev->db_stride = 1 << NVME_CAP_STRIDE(readq(&dev->bar->cap));
Keith Busch0877cb02013-07-15 15:02:19 -06002092 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2093
2094 return 0;
2095
Keith Busch0e53d182013-12-10 13:10:39 -07002096 unmap:
2097 iounmap(dev->bar);
2098 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002099 disable:
2100 pci_release_regions(pdev);
2101 disable_pci:
2102 pci_disable_device(pdev);
2103 return result;
2104}
2105
2106static void nvme_dev_unmap(struct nvme_dev *dev)
2107{
2108 if (dev->pci_dev->msi_enabled)
2109 pci_disable_msi(dev->pci_dev);
2110 else if (dev->pci_dev->msix_enabled)
2111 pci_disable_msix(dev->pci_dev);
2112
2113 if (dev->bar) {
2114 iounmap(dev->bar);
2115 dev->bar = NULL;
Keith Busch9a6b9452013-12-10 13:10:36 -07002116 pci_release_regions(dev->pci_dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002117 }
2118
Keith Busch0877cb02013-07-15 15:02:19 -06002119 if (pci_is_enabled(dev->pci_dev))
2120 pci_disable_device(dev->pci_dev);
2121}
2122
Keith Busch4d115422013-12-10 13:10:40 -07002123struct nvme_delq_ctx {
2124 struct task_struct *waiter;
2125 struct kthread_worker *worker;
2126 atomic_t refcount;
2127};
2128
2129static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2130{
2131 dq->waiter = current;
2132 mb();
2133
2134 for (;;) {
2135 set_current_state(TASK_KILLABLE);
2136 if (!atomic_read(&dq->refcount))
2137 break;
2138 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2139 fatal_signal_pending(current)) {
2140 set_current_state(TASK_RUNNING);
2141
2142 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
2143 nvme_disable_queue(dev, 0);
2144
2145 send_sig(SIGKILL, dq->worker->task, 1);
2146 flush_kthread_worker(dq->worker);
2147 return;
2148 }
2149 }
2150 set_current_state(TASK_RUNNING);
2151}
2152
2153static void nvme_put_dq(struct nvme_delq_ctx *dq)
2154{
2155 atomic_dec(&dq->refcount);
2156 if (dq->waiter)
2157 wake_up_process(dq->waiter);
2158}
2159
2160static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2161{
2162 atomic_inc(&dq->refcount);
2163 return dq;
2164}
2165
2166static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2167{
2168 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
2169
2170 nvme_clear_queue(nvmeq);
2171 nvme_put_dq(dq);
2172}
2173
2174static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2175 kthread_work_func_t fn)
2176{
2177 struct nvme_command c;
2178
2179 memset(&c, 0, sizeof(c));
2180 c.delete_queue.opcode = opcode;
2181 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2182
2183 init_kthread_work(&nvmeq->cmdinfo.work, fn);
2184 return nvme_submit_admin_cmd_async(nvmeq->dev, &c, &nvmeq->cmdinfo);
2185}
2186
2187static void nvme_del_cq_work_handler(struct kthread_work *work)
2188{
2189 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2190 cmdinfo.work);
2191 nvme_del_queue_end(nvmeq);
2192}
2193
2194static int nvme_delete_cq(struct nvme_queue *nvmeq)
2195{
2196 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2197 nvme_del_cq_work_handler);
2198}
2199
2200static void nvme_del_sq_work_handler(struct kthread_work *work)
2201{
2202 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2203 cmdinfo.work);
2204 int status = nvmeq->cmdinfo.status;
2205
2206 if (!status)
2207 status = nvme_delete_cq(nvmeq);
2208 if (status)
2209 nvme_del_queue_end(nvmeq);
2210}
2211
2212static int nvme_delete_sq(struct nvme_queue *nvmeq)
2213{
2214 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2215 nvme_del_sq_work_handler);
2216}
2217
2218static void nvme_del_queue_start(struct kthread_work *work)
2219{
2220 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2221 cmdinfo.work);
2222 allow_signal(SIGKILL);
2223 if (nvme_delete_sq(nvmeq))
2224 nvme_del_queue_end(nvmeq);
2225}
2226
2227static void nvme_disable_io_queues(struct nvme_dev *dev)
2228{
2229 int i;
2230 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2231 struct nvme_delq_ctx dq;
2232 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2233 &worker, "nvme%d", dev->instance);
2234
2235 if (IS_ERR(kworker_task)) {
2236 dev_err(&dev->pci_dev->dev,
2237 "Failed to create queue del task\n");
2238 for (i = dev->queue_count - 1; i > 0; i--)
2239 nvme_disable_queue(dev, i);
2240 return;
2241 }
2242
2243 dq.waiter = NULL;
2244 atomic_set(&dq.refcount, 0);
2245 dq.worker = &worker;
2246 for (i = dev->queue_count - 1; i > 0; i--) {
2247 struct nvme_queue *nvmeq = dev->queues[i];
2248
2249 if (nvme_suspend_queue(nvmeq))
2250 continue;
2251 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2252 nvmeq->cmdinfo.worker = dq.worker;
2253 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2254 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2255 }
2256 nvme_wait_dq(&dq, dev);
2257 kthread_stop(kworker_task);
2258}
2259
Keith Buschf0b50732013-07-15 15:02:21 -06002260static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002261{
Keith Busch22404272013-07-15 15:02:20 -06002262 int i;
2263
Keith Buschd4b4ff82013-12-10 13:10:37 -07002264 dev->initialized = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002265
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002266 spin_lock(&dev_list_lock);
Keith Buschf0b50732013-07-15 15:02:21 -06002267 list_del_init(&dev->node);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002268 spin_unlock(&dev_list_lock);
2269
Keith Busch4d115422013-12-10 13:10:40 -07002270 if (!dev->bar || (dev->bar && readl(&dev->bar->csts) == -1)) {
2271 for (i = dev->queue_count - 1; i >= 0; i--) {
2272 struct nvme_queue *nvmeq = dev->queues[i];
2273 nvme_suspend_queue(nvmeq);
2274 nvme_clear_queue(nvmeq);
2275 }
2276 } else {
2277 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002278 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002279 nvme_disable_queue(dev, 0);
2280 }
Keith Buschf0b50732013-07-15 15:02:21 -06002281 nvme_dev_unmap(dev);
2282}
2283
2284static void nvme_dev_remove(struct nvme_dev *dev)
2285{
2286 struct nvme_ns *ns, *next;
2287
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002288 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2289 list_del(&ns->list);
2290 del_gendisk(ns->disk);
2291 nvme_ns_free(ns);
2292 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002293}
2294
Matthew Wilcox091b6092011-02-10 09:56:01 -05002295static int nvme_setup_prp_pools(struct nvme_dev *dev)
2296{
2297 struct device *dmadev = &dev->pci_dev->dev;
2298 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2299 PAGE_SIZE, PAGE_SIZE, 0);
2300 if (!dev->prp_page_pool)
2301 return -ENOMEM;
2302
Matthew Wilcox99802a72011-02-10 10:30:34 -05002303 /* Optimisation for I/Os between 4k and 128k */
2304 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2305 256, 256, 0);
2306 if (!dev->prp_small_pool) {
2307 dma_pool_destroy(dev->prp_page_pool);
2308 return -ENOMEM;
2309 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002310 return 0;
2311}
2312
2313static void nvme_release_prp_pools(struct nvme_dev *dev)
2314{
2315 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002316 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002317}
2318
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002319static DEFINE_IDA(nvme_instance_ida);
2320
2321static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002322{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002323 int instance, error;
2324
2325 do {
2326 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2327 return -ENODEV;
2328
2329 spin_lock(&dev_list_lock);
2330 error = ida_get_new(&nvme_instance_ida, &instance);
2331 spin_unlock(&dev_list_lock);
2332 } while (error == -EAGAIN);
2333
2334 if (error)
2335 return -ENODEV;
2336
2337 dev->instance = instance;
2338 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002339}
2340
2341static void nvme_release_instance(struct nvme_dev *dev)
2342{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002343 spin_lock(&dev_list_lock);
2344 ida_remove(&nvme_instance_ida, dev->instance);
2345 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002346}
2347
Keith Busch5e82e952013-02-19 10:17:58 -07002348static void nvme_free_dev(struct kref *kref)
2349{
2350 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch5e82e952013-02-19 10:17:58 -07002351 kfree(dev->queues);
2352 kfree(dev->entry);
2353 kfree(dev);
2354}
2355
2356static int nvme_dev_open(struct inode *inode, struct file *f)
2357{
2358 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
2359 miscdev);
2360 kref_get(&dev->kref);
2361 f->private_data = dev;
2362 return 0;
2363}
2364
2365static int nvme_dev_release(struct inode *inode, struct file *f)
2366{
2367 struct nvme_dev *dev = f->private_data;
2368 kref_put(&dev->kref, nvme_free_dev);
2369 return 0;
2370}
2371
2372static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2373{
2374 struct nvme_dev *dev = f->private_data;
2375 switch (cmd) {
2376 case NVME_IOCTL_ADMIN_CMD:
2377 return nvme_user_admin_cmd(dev, (void __user *)arg);
2378 default:
2379 return -ENOTTY;
2380 }
2381}
2382
2383static const struct file_operations nvme_dev_fops = {
2384 .owner = THIS_MODULE,
2385 .open = nvme_dev_open,
2386 .release = nvme_dev_release,
2387 .unlocked_ioctl = nvme_dev_ioctl,
2388 .compat_ioctl = nvme_dev_ioctl,
2389};
2390
Keith Buschf0b50732013-07-15 15:02:21 -06002391static int nvme_dev_start(struct nvme_dev *dev)
2392{
2393 int result;
2394
2395 result = nvme_dev_map(dev);
2396 if (result)
2397 return result;
2398
2399 result = nvme_configure_admin_queue(dev);
2400 if (result)
2401 goto unmap;
2402
2403 spin_lock(&dev_list_lock);
2404 list_add(&dev->node, &dev_list);
2405 spin_unlock(&dev_list_lock);
2406
2407 result = nvme_setup_io_queues(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002408 if (result && result != -EBUSY)
Keith Buschf0b50732013-07-15 15:02:21 -06002409 goto disable;
2410
Keith Buschd82e8bf2013-09-05 14:45:07 -06002411 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002412
2413 disable:
2414 spin_lock(&dev_list_lock);
2415 list_del_init(&dev->node);
2416 spin_unlock(&dev_list_lock);
2417 unmap:
2418 nvme_dev_unmap(dev);
2419 return result;
2420}
2421
Keith Busch9a6b9452013-12-10 13:10:36 -07002422static int nvme_remove_dead_ctrl(void *arg)
2423{
2424 struct nvme_dev *dev = (struct nvme_dev *)arg;
2425 struct pci_dev *pdev = dev->pci_dev;
2426
2427 if (pci_get_drvdata(pdev))
2428 pci_stop_and_remove_bus_device(pdev);
2429 kref_put(&dev->kref, nvme_free_dev);
2430 return 0;
2431}
2432
2433static void nvme_remove_disks(struct work_struct *ws)
2434{
2435 int i;
2436 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2437
2438 nvme_dev_remove(dev);
2439 spin_lock(&dev_list_lock);
2440 for (i = dev->queue_count - 1; i > 0; i--) {
2441 BUG_ON(!dev->queues[i] || !dev->queues[i]->q_suspended);
2442 nvme_free_queue(dev->queues[i]);
2443 dev->queue_count--;
2444 dev->queues[i] = NULL;
2445 }
2446 spin_unlock(&dev_list_lock);
2447}
2448
2449static int nvme_dev_resume(struct nvme_dev *dev)
2450{
2451 int ret;
2452
2453 ret = nvme_dev_start(dev);
2454 if (ret && ret != -EBUSY)
2455 return ret;
2456 if (ret == -EBUSY) {
2457 spin_lock(&dev_list_lock);
2458 INIT_WORK(&dev->reset_work, nvme_remove_disks);
2459 queue_work(nvme_workq, &dev->reset_work);
2460 spin_unlock(&dev_list_lock);
2461 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002462 dev->initialized = 1;
Keith Busch9a6b9452013-12-10 13:10:36 -07002463 return 0;
2464}
2465
2466static void nvme_dev_reset(struct nvme_dev *dev)
2467{
2468 nvme_dev_shutdown(dev);
2469 if (nvme_dev_resume(dev)) {
2470 dev_err(&dev->pci_dev->dev, "Device failed to resume\n");
2471 kref_get(&dev->kref);
2472 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2473 dev->instance))) {
2474 dev_err(&dev->pci_dev->dev,
2475 "Failed to start controller remove task\n");
2476 kref_put(&dev->kref, nvme_free_dev);
2477 }
2478 }
2479}
2480
2481static void nvme_reset_failed_dev(struct work_struct *ws)
2482{
2483 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2484 nvme_dev_reset(dev);
2485}
2486
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002487static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002488{
Keith Busch0877cb02013-07-15 15:02:19 -06002489 int result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002490 struct nvme_dev *dev;
2491
2492 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2493 if (!dev)
2494 return -ENOMEM;
2495 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
2496 GFP_KERNEL);
2497 if (!dev->entry)
2498 goto free;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002499 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
2500 GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002501 if (!dev->queues)
2502 goto free;
2503
2504 INIT_LIST_HEAD(&dev->namespaces);
2505 dev->pci_dev = pdev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002506 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002507 result = nvme_set_instance(dev);
2508 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002509 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002510
Matthew Wilcox091b6092011-02-10 09:56:01 -05002511 result = nvme_setup_prp_pools(dev);
2512 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002513 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002514
Keith Buschf0b50732013-07-15 15:02:21 -06002515 result = nvme_dev_start(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002516 if (result) {
2517 if (result == -EBUSY)
2518 goto create_cdev;
Keith Busch0877cb02013-07-15 15:02:19 -06002519 goto release_pools;
Keith Buschd82e8bf2013-09-05 14:45:07 -06002520 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002521
Matthew Wilcox740216f2011-02-15 16:28:20 -05002522 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002523 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002524 goto shutdown;
Matthew Wilcox740216f2011-02-15 16:28:20 -05002525
Keith Buschd82e8bf2013-09-05 14:45:07 -06002526 create_cdev:
Keith Busch5e82e952013-02-19 10:17:58 -07002527 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
2528 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2529 dev->miscdev.parent = &pdev->dev;
2530 dev->miscdev.name = dev->name;
2531 dev->miscdev.fops = &nvme_dev_fops;
2532 result = misc_register(&dev->miscdev);
2533 if (result)
2534 goto remove;
2535
Keith Buschd4b4ff82013-12-10 13:10:37 -07002536 dev->initialized = 1;
Keith Busch5e82e952013-02-19 10:17:58 -07002537 kref_init(&dev->kref);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002538 return 0;
2539
Keith Busch5e82e952013-02-19 10:17:58 -07002540 remove:
2541 nvme_dev_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002542 shutdown:
2543 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002544 release_pools:
Keith Buschf0b50732013-07-15 15:02:21 -06002545 nvme_free_queues(dev);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002546 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002547 release:
2548 nvme_release_instance(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002549 free:
2550 kfree(dev->queues);
2551 kfree(dev->entry);
2552 kfree(dev);
2553 return result;
2554}
2555
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002556static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002557{
2558 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002559
2560 spin_lock(&dev_list_lock);
2561 list_del_init(&dev->node);
2562 spin_unlock(&dev_list_lock);
2563
2564 pci_set_drvdata(pdev, NULL);
2565 flush_work(&dev->reset_work);
Keith Busch5e82e952013-02-19 10:17:58 -07002566 misc_deregister(&dev->miscdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002567 nvme_dev_remove(dev);
2568 nvme_dev_shutdown(dev);
2569 nvme_free_queues(dev);
2570 nvme_release_instance(dev);
2571 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002572 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002573}
2574
2575/* These functions are yet to be implemented */
2576#define nvme_error_detected NULL
2577#define nvme_dump_registers NULL
2578#define nvme_link_reset NULL
2579#define nvme_slot_reset NULL
2580#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002581
2582static int nvme_suspend(struct device *dev)
2583{
2584 struct pci_dev *pdev = to_pci_dev(dev);
2585 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2586
2587 nvme_dev_shutdown(ndev);
2588 return 0;
2589}
2590
2591static int nvme_resume(struct device *dev)
2592{
2593 struct pci_dev *pdev = to_pci_dev(dev);
2594 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002595
Keith Busch9a6b9452013-12-10 13:10:36 -07002596 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
2597 INIT_WORK(&ndev->reset_work, nvme_reset_failed_dev);
2598 queue_work(nvme_workq, &ndev->reset_work);
2599 }
2600 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002601}
2602
2603static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002604
Stephen Hemminger1d352032012-09-07 09:33:17 -07002605static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002606 .error_detected = nvme_error_detected,
2607 .mmio_enabled = nvme_dump_registers,
2608 .link_reset = nvme_link_reset,
2609 .slot_reset = nvme_slot_reset,
2610 .resume = nvme_error_resume,
2611};
2612
2613/* Move to pci_ids.h later */
2614#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2615
2616static DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
2617 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2618 { 0, }
2619};
2620MODULE_DEVICE_TABLE(pci, nvme_id_table);
2621
2622static struct pci_driver nvme_driver = {
2623 .name = "nvme",
2624 .id_table = nvme_id_table,
2625 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002626 .remove = nvme_remove,
Keith Buschcd638942013-07-15 15:02:23 -06002627 .driver = {
2628 .pm = &nvme_dev_pm_ops,
2629 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002630 .err_handler = &nvme_err_handler,
2631};
2632
2633static int __init nvme_init(void)
2634{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002635 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002636
2637 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2638 if (IS_ERR(nvme_thread))
2639 return PTR_ERR(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002640
Keith Busch9a6b9452013-12-10 13:10:36 -07002641 result = -ENOMEM;
2642 nvme_workq = create_singlethread_workqueue("nvme");
2643 if (!nvme_workq)
2644 goto kill_kthread;
2645
Keith Busch5c42ea12012-07-25 16:05:18 -06002646 result = register_blkdev(nvme_major, "nvme");
2647 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002648 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06002649 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002650 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002651
2652 result = pci_register_driver(&nvme_driver);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002653 if (result)
2654 goto unregister_blkdev;
2655 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002656
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002657 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002658 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002659 kill_workq:
2660 destroy_workqueue(nvme_workq);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002661 kill_kthread:
2662 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002663 return result;
2664}
2665
2666static void __exit nvme_exit(void)
2667{
2668 pci_unregister_driver(&nvme_driver);
2669 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002670 destroy_workqueue(nvme_workq);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002671 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002672}
2673
2674MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2675MODULE_LICENSE("GPL");
Matthew Wilcox366e8212012-01-10 16:30:15 -05002676MODULE_VERSION("0.8");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002677module_init(nvme_init);
2678module_exit(nvme_exit);