blob: 1c8a82fbbc376def4e0198b310cd788bd6767932 [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))
49#define NVME_MINORS 64
Matthew Wilcoxe85248e2011-02-06 18:30:16 -050050#define ADMIN_TIMEOUT (60 * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050051
52static int nvme_major;
53module_param(nvme_major, int, 0);
54
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050055static int use_threaded_interrupts;
56module_param(use_threaded_interrupts, int, 0);
57
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050058static DEFINE_SPINLOCK(dev_list_lock);
59static LIST_HEAD(dev_list);
60static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070061static struct workqueue_struct *nvme_workq;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050062
Keith Buschd4b4ff82013-12-10 13:10:37 -070063static void nvme_reset_failed_dev(struct work_struct *ws);
64
Keith Busch4d115422013-12-10 13:10:40 -070065struct async_cmd_info {
66 struct kthread_work work;
67 struct kthread_worker *worker;
68 u32 result;
69 int status;
70 void *ctx;
71};
72
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050073/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050074 * An NVM Express queue. Each device has at least two (one for admin
75 * commands and one for I/O commands).
76 */
77struct nvme_queue {
78 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -050079 struct nvme_dev *dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050080 spinlock_t q_lock;
81 struct nvme_command *sq_cmds;
82 volatile struct nvme_completion *cqes;
83 dma_addr_t sq_dma_addr;
84 dma_addr_t cq_dma_addr;
85 wait_queue_head_t sq_full;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050086 wait_queue_t sq_cong_wait;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050087 struct bio_list sq_cong;
88 u32 __iomem *q_db;
89 u16 q_depth;
90 u16 cq_vector;
91 u16 sq_head;
92 u16 sq_tail;
93 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -070094 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -040095 u8 cq_phase;
96 u8 cqe_seen;
Keith Busch22404272013-07-15 15:02:20 -060097 u8 q_suspended;
Keith Busch4d115422013-12-10 13:10:40 -070098 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050099 unsigned long cmdid_data[];
100};
101
102/*
103 * Check we didin't inadvertently grow the command struct
104 */
105static inline void _nvme_check_size(void)
106{
107 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
108 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
109 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
110 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
111 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400112 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700113 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500114 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
115 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
116 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
117 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600118 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500119}
120
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500121typedef void (*nvme_completion_fn)(struct nvme_dev *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400122 struct nvme_completion *);
123
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500124struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400125 nvme_completion_fn fn;
126 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500127 unsigned long timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700128 int aborted;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500129};
130
131static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
132{
133 return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
134}
135
Keith Busch22404272013-07-15 15:02:20 -0600136static unsigned nvme_queue_extra(int depth)
137{
138 return DIV_ROUND_UP(depth, 8) + (depth * sizeof(struct nvme_cmd_info));
139}
140
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500141/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400142 * alloc_cmdid() - Allocate a Command ID
143 * @nvmeq: The queue that will be used for this command
144 * @ctx: A pointer that will be passed to the handler
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400145 * @handler: The function to call on completion
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500146 *
147 * Allocate a Command ID for a queue. The data passed in will
148 * be passed to the completion handler. This is implemented by using
149 * the bottom two bits of the ctx pointer to store the handler ID.
150 * Passing in a pointer that's not 4-byte aligned will cause a BUG.
151 * We can change this if it becomes a problem.
Matthew Wilcox184d2942011-05-11 21:36:38 -0400152 *
153 * May be called with local interrupts disabled and the q_lock held,
154 * or with interrupts enabled and no locks held.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500155 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400156static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx,
157 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500158{
Matthew Wilcoxe6d15f72011-02-24 08:49:41 -0500159 int depth = nvmeq->q_depth - 1;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500160 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500161 int cmdid;
162
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500163 do {
164 cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
165 if (cmdid >= depth)
166 return -EBUSY;
167 } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
168
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400169 info[cmdid].fn = handler;
170 info[cmdid].ctx = ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500171 info[cmdid].timeout = jiffies + timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700172 info[cmdid].aborted = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500173 return cmdid;
174}
175
176static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400177 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500178{
179 int cmdid;
180 wait_event_killable(nvmeq->sq_full,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500181 (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500182 return (cmdid < 0) ? -EINTR : cmdid;
183}
184
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400185/* Special values must be less than 0x1000 */
186#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500187#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
188#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
189#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500190#define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE)
Keith Buschc30341d2013-12-10 13:10:38 -0700191#define CMD_CTX_ABORT (0x31C + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500192
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500193static void special_completion(struct nvme_dev *dev, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400194 struct nvme_completion *cqe)
195{
196 if (ctx == CMD_CTX_CANCELLED)
197 return;
198 if (ctx == CMD_CTX_FLUSH)
199 return;
Keith Buschc30341d2013-12-10 13:10:38 -0700200 if (ctx == CMD_CTX_ABORT) {
201 ++dev->abort_limit;
202 return;
203 }
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400204 if (ctx == CMD_CTX_COMPLETED) {
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500205 dev_warn(&dev->pci_dev->dev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400206 "completed id %d twice on queue %d\n",
207 cqe->command_id, le16_to_cpup(&cqe->sq_id));
208 return;
209 }
210 if (ctx == CMD_CTX_INVALID) {
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500211 dev_warn(&dev->pci_dev->dev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400212 "invalid id %d completed on queue %d\n",
213 cqe->command_id, le16_to_cpup(&cqe->sq_id));
214 return;
215 }
216
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500217 dev_warn(&dev->pci_dev->dev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400218}
219
Keith Busch4d115422013-12-10 13:10:40 -0700220static void async_completion(struct nvme_dev *dev, void *ctx,
221 struct nvme_completion *cqe)
222{
223 struct async_cmd_info *cmdinfo = ctx;
224 cmdinfo->result = le32_to_cpup(&cqe->result);
225 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
226 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
227}
228
Matthew Wilcox184d2942011-05-11 21:36:38 -0400229/*
230 * Called with local interrupts disabled and the q_lock held. May not sleep.
231 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400232static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid,
233 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500234{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400235 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500236 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500237
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400238 if (cmdid >= nvmeq->q_depth) {
239 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500240 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400241 }
Keith Busch859361a2012-08-02 14:05:59 -0600242 if (fn)
243 *fn = info[cmdid].fn;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400244 ctx = info[cmdid].ctx;
245 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500246 info[cmdid].ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500247 clear_bit(cmdid, nvmeq->cmdid_data);
248 wake_up(&nvmeq->sq_full);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400249 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500250}
251
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400252static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
253 nvme_completion_fn *fn)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500254{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400255 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500256 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400257 if (fn)
258 *fn = info[cmdid].fn;
259 ctx = info[cmdid].ctx;
260 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500261 info[cmdid].ctx = CMD_CTX_CANCELLED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400262 return ctx;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500263}
264
Vishal Verma5d0f6132013-03-04 18:40:58 -0700265struct nvme_queue *get_nvmeq(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500266{
Matthew Wilcox040a93b2011-12-20 11:04:12 -0500267 return dev->queues[get_cpu() + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500268}
269
Vishal Verma5d0f6132013-03-04 18:40:58 -0700270void put_nvmeq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500271{
Matthew Wilcox1b234842011-01-20 13:01:49 -0500272 put_cpu();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500273}
274
275/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400276 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500277 * @nvmeq: The queue to use
278 * @cmd: The command to send
279 *
280 * Safe to use from interrupt context
281 */
282static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
283{
284 unsigned long flags;
285 u16 tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500286 spin_lock_irqsave(&nvmeq->q_lock, flags);
287 tail = nvmeq->sq_tail;
288 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500289 if (++tail == nvmeq->q_depth)
290 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500291 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500292 nvmeq->sq_tail = tail;
293 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
294
295 return 0;
296}
297
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500298static __le64 **iod_list(struct nvme_iod *iod)
299{
300 return ((void *)iod) + iod->offset;
301}
302
303/*
304 * Will slightly overestimate the number of pages needed. This is OK
305 * as it only leads to a small amount of wasted memory for the lifetime of
306 * the I/O.
307 */
308static int nvme_npages(unsigned size)
309{
310 unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE);
311 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
312}
313
314static struct nvme_iod *
315nvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp)
316{
317 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
318 sizeof(__le64 *) * nvme_npages(nbytes) +
319 sizeof(struct scatterlist) * nseg, gfp);
320
321 if (iod) {
322 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
323 iod->npages = -1;
324 iod->length = nbytes;
Keith Busch2b196032012-11-06 11:59:23 -0700325 iod->nents = 0;
Keith Busch61982212013-05-29 15:59:39 -0600326 iod->start_time = jiffies;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500327 }
328
329 return iod;
330}
331
Vishal Verma5d0f6132013-03-04 18:40:58 -0700332void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500333{
334 const int last_prp = PAGE_SIZE / 8 - 1;
335 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500336 __le64 **list = iod_list(iod);
337 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500338
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500339 if (iod->npages == 0)
340 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
341 for (i = 0; i < iod->npages; i++) {
342 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500343 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500344 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500345 prp_dma = next_prp_dma;
346 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500347 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500348}
349
Keith Busch61982212013-05-29 15:59:39 -0600350static void nvme_start_io_acct(struct bio *bio)
351{
352 struct gendisk *disk = bio->bi_bdev->bd_disk;
353 const int rw = bio_data_dir(bio);
354 int cpu = part_stat_lock();
355 part_round_stats(cpu, &disk->part0);
356 part_stat_inc(cpu, &disk->part0, ios[rw]);
357 part_stat_add(cpu, &disk->part0, sectors[rw], bio_sectors(bio));
358 part_inc_in_flight(&disk->part0, rw);
359 part_stat_unlock();
360}
361
362static void nvme_end_io_acct(struct bio *bio, unsigned long start_time)
363{
364 struct gendisk *disk = bio->bi_bdev->bd_disk;
365 const int rw = bio_data_dir(bio);
366 unsigned long duration = jiffies - start_time;
367 int cpu = part_stat_lock();
368 part_stat_add(cpu, &disk->part0, ticks[rw], duration);
369 part_round_stats(cpu, &disk->part0);
370 part_dec_in_flight(&disk->part0, rw);
371 part_stat_unlock();
372}
373
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500374static void bio_completion(struct nvme_dev *dev, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500375 struct nvme_completion *cqe)
376{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500377 struct nvme_iod *iod = ctx;
378 struct bio *bio = iod->private;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500379 u16 status = le16_to_cpup(&cqe->status) >> 1;
380
Keith Busch9e59d092013-08-08 10:25:38 -0600381 if (iod->nents) {
Keith Busch2b196032012-11-06 11:59:23 -0700382 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500383 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busch9e59d092013-08-08 10:25:38 -0600384 nvme_end_io_acct(bio, iod->start_time);
385 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500386 nvme_free_iod(dev, iod);
Keith Busch427e9702013-04-09 11:59:32 -0600387 if (status)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500388 bio_endio(bio, -EIO);
Keith Busch427e9702013-04-09 11:59:32 -0600389 else
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500390 bio_endio(bio, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500391}
392
Matthew Wilcox184d2942011-05-11 21:36:38 -0400393/* length is in bytes. gfp flags indicates whether we may sleep. */
Vishal Verma5d0f6132013-03-04 18:40:58 -0700394int nvme_setup_prps(struct nvme_dev *dev, struct nvme_common_command *cmd,
395 struct nvme_iod *iod, int total_len, gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500396{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500397 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500398 int length = total_len;
399 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500400 int dma_len = sg_dma_len(sg);
401 u64 dma_addr = sg_dma_address(sg);
402 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500403 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500404 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500405 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500406 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500407
408 cmd->prp1 = cpu_to_le64(dma_addr);
409 length -= (PAGE_SIZE - offset);
410 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500411 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500412
413 dma_len -= (PAGE_SIZE - offset);
414 if (dma_len) {
415 dma_addr += (PAGE_SIZE - offset);
416 } else {
417 sg = sg_next(sg);
418 dma_addr = sg_dma_address(sg);
419 dma_len = sg_dma_len(sg);
420 }
421
422 if (length <= PAGE_SIZE) {
423 cmd->prp2 = cpu_to_le64(dma_addr);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500424 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500425 }
426
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500427 nprps = DIV_ROUND_UP(length, PAGE_SIZE);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500428 if (nprps <= (256 / 8)) {
429 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500430 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500431 } else {
432 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500433 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500434 }
435
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400436 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
437 if (!prp_list) {
438 cmd->prp2 = cpu_to_le64(dma_addr);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500439 iod->npages = -1;
440 return (total_len - length) + PAGE_SIZE;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400441 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500442 list[0] = prp_list;
443 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500444 cmd->prp2 = cpu_to_le64(prp_dma);
445 i = 0;
446 for (;;) {
Matthew Wilcox7523d832011-03-16 16:43:40 -0400447 if (i == PAGE_SIZE / 8) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500448 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400449 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500450 if (!prp_list)
451 return total_len - length;
452 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400453 prp_list[0] = old_prp_list[i - 1];
454 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
455 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500456 }
457 prp_list[i++] = cpu_to_le64(dma_addr);
458 dma_len -= PAGE_SIZE;
459 dma_addr += PAGE_SIZE;
460 length -= PAGE_SIZE;
461 if (length <= 0)
462 break;
463 if (dma_len > 0)
464 continue;
465 BUG_ON(dma_len < 0);
466 sg = sg_next(sg);
467 dma_addr = sg_dma_address(sg);
468 dma_len = sg_dma_len(sg);
469 }
470
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500471 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500472}
473
Keith Busch427e9702013-04-09 11:59:32 -0600474struct nvme_bio_pair {
475 struct bio b1, b2, *parent;
476 struct bio_vec *bv1, *bv2;
477 int err;
478 atomic_t cnt;
479};
480
481static void nvme_bio_pair_endio(struct bio *bio, int err)
482{
483 struct nvme_bio_pair *bp = bio->bi_private;
484
485 if (err)
486 bp->err = err;
487
488 if (atomic_dec_and_test(&bp->cnt)) {
489 bio_endio(bp->parent, bp->err);
Keith Busch1b567492013-07-18 12:13:51 -0600490 kfree(bp->bv1);
491 kfree(bp->bv2);
Keith Busch427e9702013-04-09 11:59:32 -0600492 kfree(bp);
493 }
494}
495
496static struct nvme_bio_pair *nvme_bio_split(struct bio *bio, int idx,
497 int len, int offset)
498{
499 struct nvme_bio_pair *bp;
500
501 BUG_ON(len > bio->bi_size);
502 BUG_ON(idx > bio->bi_vcnt);
503
504 bp = kmalloc(sizeof(*bp), GFP_ATOMIC);
505 if (!bp)
506 return NULL;
507 bp->err = 0;
508
509 bp->b1 = *bio;
510 bp->b2 = *bio;
511
512 bp->b1.bi_size = len;
513 bp->b2.bi_size -= len;
514 bp->b1.bi_vcnt = idx;
515 bp->b2.bi_idx = idx;
516 bp->b2.bi_sector += len >> 9;
517
518 if (offset) {
519 bp->bv1 = kmalloc(bio->bi_max_vecs * sizeof(struct bio_vec),
520 GFP_ATOMIC);
521 if (!bp->bv1)
522 goto split_fail_1;
523
524 bp->bv2 = kmalloc(bio->bi_max_vecs * sizeof(struct bio_vec),
525 GFP_ATOMIC);
526 if (!bp->bv2)
527 goto split_fail_2;
528
529 memcpy(bp->bv1, bio->bi_io_vec,
530 bio->bi_max_vecs * sizeof(struct bio_vec));
531 memcpy(bp->bv2, bio->bi_io_vec,
532 bio->bi_max_vecs * sizeof(struct bio_vec));
533
534 bp->b1.bi_io_vec = bp->bv1;
535 bp->b2.bi_io_vec = bp->bv2;
536 bp->b2.bi_io_vec[idx].bv_offset += offset;
537 bp->b2.bi_io_vec[idx].bv_len -= offset;
538 bp->b1.bi_io_vec[idx].bv_len = offset;
539 bp->b1.bi_vcnt++;
540 } else
541 bp->bv1 = bp->bv2 = NULL;
542
543 bp->b1.bi_private = bp;
544 bp->b2.bi_private = bp;
545
546 bp->b1.bi_end_io = nvme_bio_pair_endio;
547 bp->b2.bi_end_io = nvme_bio_pair_endio;
548
549 bp->parent = bio;
550 atomic_set(&bp->cnt, 2);
551
552 return bp;
553
554 split_fail_2:
555 kfree(bp->bv1);
556 split_fail_1:
557 kfree(bp);
558 return NULL;
559}
560
561static int nvme_split_and_submit(struct bio *bio, struct nvme_queue *nvmeq,
562 int idx, int len, int offset)
563{
564 struct nvme_bio_pair *bp = nvme_bio_split(bio, idx, len, offset);
565 if (!bp)
566 return -ENOMEM;
567
568 if (bio_list_empty(&nvmeq->sq_cong))
569 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
570 bio_list_add(&nvmeq->sq_cong, &bp->b1);
571 bio_list_add(&nvmeq->sq_cong, &bp->b2);
572
573 return 0;
574}
575
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500576/* NVMe scatterlists require no holes in the virtual address */
577#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
578 (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
579
Keith Busch427e9702013-04-09 11:59:32 -0600580static int nvme_map_bio(struct nvme_queue *nvmeq, struct nvme_iod *iod,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500581 struct bio *bio, enum dma_data_direction dma_dir, int psegs)
582{
Matthew Wilcox76830842011-02-10 13:55:39 -0500583 struct bio_vec *bvec, *bvprv = NULL;
584 struct scatterlist *sg = NULL;
Keith Busch159b67d2013-04-09 17:13:20 -0600585 int i, length = 0, nsegs = 0, split_len = bio->bi_size;
586
587 if (nvmeq->dev->stripe_size)
588 split_len = nvmeq->dev->stripe_size -
589 ((bio->bi_sector << 9) & (nvmeq->dev->stripe_size - 1));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500590
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500591 sg_init_table(iod->sg, psegs);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500592 bio_for_each_segment(bvec, bio, i) {
Matthew Wilcox76830842011-02-10 13:55:39 -0500593 if (bvprv && BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) {
594 sg->length += bvec->bv_len;
595 } else {
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500596 if (bvprv && BIOVEC_NOT_VIRT_MERGEABLE(bvprv, bvec))
Keith Busch427e9702013-04-09 11:59:32 -0600597 return nvme_split_and_submit(bio, nvmeq, i,
598 length, 0);
599
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500600 sg = sg ? sg + 1 : iod->sg;
Matthew Wilcox76830842011-02-10 13:55:39 -0500601 sg_set_page(sg, bvec->bv_page, bvec->bv_len,
602 bvec->bv_offset);
603 nsegs++;
604 }
Keith Busch159b67d2013-04-09 17:13:20 -0600605
606 if (split_len - length < bvec->bv_len)
607 return nvme_split_and_submit(bio, nvmeq, i, split_len,
608 split_len - length);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500609 length += bvec->bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500610 bvprv = bvec;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500611 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500612 iod->nents = nsegs;
Matthew Wilcox76830842011-02-10 13:55:39 -0500613 sg_mark_end(sg);
Keith Busch427e9702013-04-09 11:59:32 -0600614 if (dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir) == 0)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500615 return -ENOMEM;
Keith Busch427e9702013-04-09 11:59:32 -0600616
Keith Busch159b67d2013-04-09 17:13:20 -0600617 BUG_ON(length != bio->bi_size);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500618 return length;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500619}
620
Keith Busch0e5e4f02012-11-09 16:33:05 -0700621/*
622 * We reuse the small pool to allocate the 16-byte range here as it is not
623 * worth having a special pool for these or additional cases to handle freeing
624 * the iod.
625 */
626static int nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
627 struct bio *bio, struct nvme_iod *iod, int cmdid)
628{
629 struct nvme_dsm_range *range;
630 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
631
632 range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC,
633 &iod->first_dma);
634 if (!range)
635 return -ENOMEM;
636
637 iod_list(iod)[0] = (__le64 *)range;
638 iod->npages = 0;
639
640 range->cattr = cpu_to_le32(0);
641 range->nlb = cpu_to_le32(bio->bi_size >> ns->lba_shift);
Matthew Wilcox063cc6d2013-03-27 21:28:22 -0400642 range->slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_sector));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700643
644 memset(cmnd, 0, sizeof(*cmnd));
645 cmnd->dsm.opcode = nvme_cmd_dsm;
646 cmnd->dsm.command_id = cmdid;
647 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
648 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
649 cmnd->dsm.nr = 0;
650 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
651
652 if (++nvmeq->sq_tail == nvmeq->q_depth)
653 nvmeq->sq_tail = 0;
654 writel(nvmeq->sq_tail, nvmeq->q_db);
655
656 return 0;
657}
658
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500659static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
660 int cmdid)
661{
662 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
663
664 memset(cmnd, 0, sizeof(*cmnd));
665 cmnd->common.opcode = nvme_cmd_flush;
666 cmnd->common.command_id = cmdid;
667 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
668
669 if (++nvmeq->sq_tail == nvmeq->q_depth)
670 nvmeq->sq_tail = 0;
671 writel(nvmeq->sq_tail, nvmeq->q_db);
672
673 return 0;
674}
675
Vishal Verma5d0f6132013-03-04 18:40:58 -0700676int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500677{
678 int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH,
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500679 special_completion, NVME_IO_TIMEOUT);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500680 if (unlikely(cmdid < 0))
681 return cmdid;
682
683 return nvme_submit_flush(nvmeq, ns, cmdid);
684}
685
Matthew Wilcox184d2942011-05-11 21:36:38 -0400686/*
687 * Called with local interrupts disabled and the q_lock held. May not sleep.
688 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500689static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
690 struct bio *bio)
691{
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500692 struct nvme_command *cmnd;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500693 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500694 enum dma_data_direction dma_dir;
Wei Yongjun1287dab2013-05-13 22:29:04 +0800695 int cmdid, length, result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500696 u16 control;
697 u32 dsmgmt;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500698 int psegs = bio_phys_segments(ns->queue, bio);
699
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500700 if ((bio->bi_rw & REQ_FLUSH) && psegs) {
701 result = nvme_submit_flush_data(nvmeq, ns);
702 if (result)
703 return result;
704 }
705
Wei Yongjun1287dab2013-05-13 22:29:04 +0800706 result = -ENOMEM;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500707 iod = nvme_alloc_iod(psegs, bio->bi_size, GFP_ATOMIC);
708 if (!iod)
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500709 goto nomem;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500710 iod->private = bio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500711
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500712 result = -EBUSY;
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500713 cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500714 if (unlikely(cmdid < 0))
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500715 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500716
Keith Busch0e5e4f02012-11-09 16:33:05 -0700717 if (bio->bi_rw & REQ_DISCARD) {
718 result = nvme_submit_discard(nvmeq, ns, bio, iod, cmdid);
719 if (result)
720 goto free_cmdid;
721 return result;
722 }
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500723 if ((bio->bi_rw & REQ_FLUSH) && !psegs)
724 return nvme_submit_flush(nvmeq, ns, cmdid);
725
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500726 control = 0;
727 if (bio->bi_rw & REQ_FUA)
728 control |= NVME_RW_FUA;
729 if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
730 control |= NVME_RW_LR;
731
732 dsmgmt = 0;
733 if (bio->bi_rw & REQ_RAHEAD)
734 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
735
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500736 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500737
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500738 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500739 if (bio_data_dir(bio)) {
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500740 cmnd->rw.opcode = nvme_cmd_write;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500741 dma_dir = DMA_TO_DEVICE;
742 } else {
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500743 cmnd->rw.opcode = nvme_cmd_read;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500744 dma_dir = DMA_FROM_DEVICE;
745 }
746
Keith Busch427e9702013-04-09 11:59:32 -0600747 result = nvme_map_bio(nvmeq, iod, bio, dma_dir, psegs);
748 if (result <= 0)
Keith Busch859361a2012-08-02 14:05:59 -0600749 goto free_cmdid;
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500750 length = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500751
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500752 cmnd->rw.command_id = cmdid;
753 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500754 length = nvme_setup_prps(nvmeq->dev, &cmnd->common, iod, length,
755 GFP_ATOMIC);
Matthew Wilcox063cc6d2013-03-27 21:28:22 -0400756 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_sector));
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500757 cmnd->rw.length = cpu_to_le16((length >> ns->lba_shift) - 1);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500758 cmnd->rw.control = cpu_to_le16(control);
759 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500760
Keith Busch61982212013-05-29 15:59:39 -0600761 nvme_start_io_acct(bio);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500762 if (++nvmeq->sq_tail == nvmeq->q_depth)
763 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500764 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500765
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500766 return 0;
767
Keith Busch859361a2012-08-02 14:05:59 -0600768 free_cmdid:
769 free_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500770 free_iod:
771 nvme_free_iod(nvmeq->dev, iod);
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500772 nomem:
773 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500774}
775
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400776static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500777{
Matthew Wilcox82123462011-01-20 13:24:06 -0500778 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500779
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500780 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500781 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500782
783 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400784 void *ctx;
785 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500786 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500787 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500788 break;
789 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
790 if (++head == nvmeq->q_depth) {
791 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500792 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500793 }
794
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400795 ctx = free_cmdid(nvmeq, cqe.command_id, &fn);
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500796 fn(nvmeq->dev, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500797 }
798
799 /* If the controller ignores the cq head doorbell and continuously
800 * writes to the queue, it is theoretically possible to wrap around
801 * the queue twice and mistakenly return IRQ_NONE. Linux only
802 * requires that 0.1% of your interrupts are handled, so this isn't
803 * a big problem.
804 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500805 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400806 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500807
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800808 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500809 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500810 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500811
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400812 nvmeq->cqe_seen = 1;
813 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500814}
815
Matthew Wilcox7d822452013-06-24 12:03:57 -0400816static void nvme_make_request(struct request_queue *q, struct bio *bio)
817{
818 struct nvme_ns *ns = q->queuedata;
819 struct nvme_queue *nvmeq = get_nvmeq(ns->dev);
820 int result = -EBUSY;
821
Keith Buschcd638942013-07-15 15:02:23 -0600822 if (!nvmeq) {
823 put_nvmeq(NULL);
824 bio_endio(bio, -EIO);
825 return;
826 }
827
Matthew Wilcox7d822452013-06-24 12:03:57 -0400828 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -0600829 if (!nvmeq->q_suspended && bio_list_empty(&nvmeq->sq_cong))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400830 result = nvme_submit_bio_queue(nvmeq, ns, bio);
831 if (unlikely(result)) {
832 if (bio_list_empty(&nvmeq->sq_cong))
833 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
834 bio_list_add(&nvmeq->sq_cong, bio);
835 }
836
837 nvme_process_cq(nvmeq);
838 spin_unlock_irq(&nvmeq->q_lock);
839 put_nvmeq(nvmeq);
840}
841
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500842static irqreturn_t nvme_irq(int irq, void *data)
843{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500844 irqreturn_t result;
845 struct nvme_queue *nvmeq = data;
846 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400847 nvme_process_cq(nvmeq);
848 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
849 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500850 spin_unlock(&nvmeq->q_lock);
851 return result;
852}
853
854static irqreturn_t nvme_irq_check(int irq, void *data)
855{
856 struct nvme_queue *nvmeq = data;
857 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
858 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
859 return IRQ_NONE;
860 return IRQ_WAKE_THREAD;
861}
862
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500863static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
864{
865 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400866 cancel_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500867 spin_unlock_irq(&nvmeq->q_lock);
868}
869
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400870struct sync_cmd_info {
871 struct task_struct *task;
872 u32 result;
873 int status;
874};
875
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500876static void sync_completion(struct nvme_dev *dev, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400877 struct nvme_completion *cqe)
878{
879 struct sync_cmd_info *cmdinfo = ctx;
880 cmdinfo->result = le32_to_cpup(&cqe->result);
881 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
882 wake_up_process(cmdinfo->task);
883}
884
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500885/*
886 * Returns 0 on success. If the result is negative, it's a Linux error code;
887 * if the result is positive, it's an NVM Express status code
888 */
Vishal Verma5d0f6132013-03-04 18:40:58 -0700889int nvme_submit_sync_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd,
890 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500891{
892 int cmdid;
893 struct sync_cmd_info cmdinfo;
894
895 cmdinfo.task = current;
896 cmdinfo.status = -EINTR;
897
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400898 cmdid = alloc_cmdid_killable(nvmeq, &cmdinfo, sync_completion,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500899 timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500900 if (cmdid < 0)
901 return cmdid;
902 cmd->common.command_id = cmdid;
903
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500904 set_current_state(TASK_KILLABLE);
905 nvme_submit_cmd(nvmeq, cmd);
Keith Busch78f8d252013-04-19 14:11:06 -0600906 schedule_timeout(timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500907
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500908 if (cmdinfo.status == -EINTR) {
909 nvme_abort_command(nvmeq, cmdid);
910 return -EINTR;
911 }
912
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500913 if (result)
914 *result = cmdinfo.result;
915
916 return cmdinfo.status;
917}
918
Keith Busch4d115422013-12-10 13:10:40 -0700919static int nvme_submit_async_cmd(struct nvme_queue *nvmeq,
920 struct nvme_command *cmd,
921 struct async_cmd_info *cmdinfo, unsigned timeout)
922{
923 int cmdid;
924
925 cmdid = alloc_cmdid_killable(nvmeq, cmdinfo, async_completion, timeout);
926 if (cmdid < 0)
927 return cmdid;
928 cmdinfo->status = -EINTR;
929 cmd->common.command_id = cmdid;
930 nvme_submit_cmd(nvmeq, cmd);
931 return 0;
932}
933
Vishal Verma5d0f6132013-03-04 18:40:58 -0700934int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500935 u32 *result)
936{
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500937 return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500938}
939
Keith Busch4d115422013-12-10 13:10:40 -0700940static int nvme_submit_admin_cmd_async(struct nvme_dev *dev,
941 struct nvme_command *cmd, struct async_cmd_info *cmdinfo)
942{
943 return nvme_submit_async_cmd(dev->queues[0], cmd, cmdinfo,
944 ADMIN_TIMEOUT);
945}
946
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500947static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
948{
949 int status;
950 struct nvme_command c;
951
952 memset(&c, 0, sizeof(c));
953 c.delete_queue.opcode = opcode;
954 c.delete_queue.qid = cpu_to_le16(id);
955
956 status = nvme_submit_admin_cmd(dev, &c, NULL);
957 if (status)
958 return -EIO;
959 return 0;
960}
961
962static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
963 struct nvme_queue *nvmeq)
964{
965 int status;
966 struct nvme_command c;
967 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
968
969 memset(&c, 0, sizeof(c));
970 c.create_cq.opcode = nvme_admin_create_cq;
971 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
972 c.create_cq.cqid = cpu_to_le16(qid);
973 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
974 c.create_cq.cq_flags = cpu_to_le16(flags);
975 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
976
977 status = nvme_submit_admin_cmd(dev, &c, NULL);
978 if (status)
979 return -EIO;
980 return 0;
981}
982
983static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
984 struct nvme_queue *nvmeq)
985{
986 int status;
987 struct nvme_command c;
988 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
989
990 memset(&c, 0, sizeof(c));
991 c.create_sq.opcode = nvme_admin_create_sq;
992 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
993 c.create_sq.sqid = cpu_to_le16(qid);
994 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
995 c.create_sq.sq_flags = cpu_to_le16(flags);
996 c.create_sq.cqid = cpu_to_le16(qid);
997
998 status = nvme_submit_admin_cmd(dev, &c, NULL);
999 if (status)
1000 return -EIO;
1001 return 0;
1002}
1003
1004static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1005{
1006 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1007}
1008
1009static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1010{
1011 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1012}
1013
Vishal Verma5d0f6132013-03-04 18:40:58 -07001014int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001015 dma_addr_t dma_addr)
1016{
1017 struct nvme_command c;
1018
1019 memset(&c, 0, sizeof(c));
1020 c.identify.opcode = nvme_admin_identify;
1021 c.identify.nsid = cpu_to_le32(nsid);
1022 c.identify.prp1 = cpu_to_le64(dma_addr);
1023 c.identify.cns = cpu_to_le32(cns);
1024
1025 return nvme_submit_admin_cmd(dev, &c, NULL);
1026}
1027
Vishal Verma5d0f6132013-03-04 18:40:58 -07001028int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -06001029 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001030{
1031 struct nvme_command c;
1032
1033 memset(&c, 0, sizeof(c));
1034 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -06001035 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001036 c.features.prp1 = cpu_to_le64(dma_addr);
1037 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001038
Keith Busch08df1e02012-09-21 10:52:13 -06001039 return nvme_submit_admin_cmd(dev, &c, result);
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001040}
1041
Vishal Verma5d0f6132013-03-04 18:40:58 -07001042int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
1043 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001044{
1045 struct nvme_command c;
1046
1047 memset(&c, 0, sizeof(c));
1048 c.features.opcode = nvme_admin_set_features;
1049 c.features.prp1 = cpu_to_le64(dma_addr);
1050 c.features.fid = cpu_to_le32(fid);
1051 c.features.dword11 = cpu_to_le32(dword11);
1052
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001053 return nvme_submit_admin_cmd(dev, &c, result);
1054}
1055
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001056/**
Keith Buschc30341d2013-12-10 13:10:38 -07001057 * nvme_abort_cmd - Attempt aborting a command
1058 * @cmdid: Command id of a timed out IO
1059 * @queue: The queue with timed out IO
1060 *
1061 * Schedule controller reset if the command was already aborted once before and
1062 * still hasn't been returned to the driver, or if this is the admin queue.
1063 */
1064static void nvme_abort_cmd(int cmdid, struct nvme_queue *nvmeq)
1065{
1066 int a_cmdid;
1067 struct nvme_command cmd;
1068 struct nvme_dev *dev = nvmeq->dev;
1069 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
1070
1071 if (!nvmeq->qid || info[cmdid].aborted) {
1072 if (work_busy(&dev->reset_work))
1073 return;
1074 list_del_init(&dev->node);
1075 dev_warn(&dev->pci_dev->dev,
1076 "I/O %d QID %d timeout, reset controller\n", cmdid,
1077 nvmeq->qid);
1078 INIT_WORK(&dev->reset_work, nvme_reset_failed_dev);
1079 queue_work(nvme_workq, &dev->reset_work);
1080 return;
1081 }
1082
1083 if (!dev->abort_limit)
1084 return;
1085
1086 a_cmdid = alloc_cmdid(dev->queues[0], CMD_CTX_ABORT, special_completion,
1087 ADMIN_TIMEOUT);
1088 if (a_cmdid < 0)
1089 return;
1090
1091 memset(&cmd, 0, sizeof(cmd));
1092 cmd.abort.opcode = nvme_admin_abort_cmd;
1093 cmd.abort.cid = cmdid;
1094 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
1095 cmd.abort.command_id = a_cmdid;
1096
1097 --dev->abort_limit;
1098 info[cmdid].aborted = 1;
1099 info[cmdid].timeout = jiffies + ADMIN_TIMEOUT;
1100
1101 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", cmdid,
1102 nvmeq->qid);
1103 nvme_submit_cmd(dev->queues[0], &cmd);
1104}
1105
1106/**
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001107 * nvme_cancel_ios - Cancel outstanding I/Os
1108 * @queue: The queue to cancel I/Os on
1109 * @timeout: True to only cancel I/Os which have timed out
1110 */
1111static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout)
1112{
1113 int depth = nvmeq->q_depth - 1;
1114 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
1115 unsigned long now = jiffies;
1116 int cmdid;
1117
1118 for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
1119 void *ctx;
1120 nvme_completion_fn fn;
1121 static struct nvme_completion cqe = {
Matthew Wilcoxaf2d9ca2013-04-16 15:18:30 -04001122 .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1),
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001123 };
1124
1125 if (timeout && !time_after(now, info[cmdid].timeout))
1126 continue;
Keith Busch053ab7022013-04-30 11:19:38 -06001127 if (info[cmdid].ctx == CMD_CTX_CANCELLED)
1128 continue;
Keith Buschc30341d2013-12-10 13:10:38 -07001129 if (timeout && nvmeq->dev->initialized) {
1130 nvme_abort_cmd(cmdid, nvmeq);
1131 continue;
1132 }
1133 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n", cmdid,
1134 nvmeq->qid);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001135 ctx = cancel_cmdid(nvmeq, cmdid, &fn);
1136 fn(nvmeq->dev, ctx, &cqe);
1137 }
1138}
1139
Keith Busch22404272013-07-15 15:02:20 -06001140static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001141{
Keith Busch22404272013-07-15 15:02:20 -06001142 spin_lock_irq(&nvmeq->q_lock);
1143 while (bio_list_peek(&nvmeq->sq_cong)) {
1144 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1145 bio_endio(bio, -EIO);
1146 }
1147 spin_unlock_irq(&nvmeq->q_lock);
1148
Matthew Wilcox9e866772012-08-03 13:55:56 -04001149 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1150 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1151 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1152 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1153 kfree(nvmeq);
1154}
1155
Keith Busch22404272013-07-15 15:02:20 -06001156static void nvme_free_queues(struct nvme_dev *dev)
1157{
1158 int i;
1159
1160 for (i = dev->queue_count - 1; i >= 0; i--) {
1161 nvme_free_queue(dev->queues[i]);
1162 dev->queue_count--;
1163 dev->queues[i] = NULL;
1164 }
1165}
1166
Keith Busch4d115422013-12-10 13:10:40 -07001167/**
1168 * nvme_suspend_queue - put queue into suspended state
1169 * @nvmeq - queue to suspend
1170 *
1171 * Returns 1 if already suspended, 0 otherwise.
1172 */
1173static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001174{
Keith Busch4d115422013-12-10 13:10:40 -07001175 int vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001176
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001177 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001178 if (nvmeq->q_suspended) {
1179 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07001180 return 1;
Keith Busch32958742012-08-20 14:57:49 -06001181 }
Keith Busch22404272013-07-15 15:02:20 -06001182 nvmeq->q_suspended = 1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001183 spin_unlock_irq(&nvmeq->q_lock);
1184
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001185 irq_set_affinity_hint(vector, NULL);
1186 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001187
Keith Busch4d115422013-12-10 13:10:40 -07001188 return 0;
1189}
1190
1191static void nvme_clear_queue(struct nvme_queue *nvmeq)
1192{
1193 spin_lock_irq(&nvmeq->q_lock);
1194 nvme_process_cq(nvmeq);
1195 nvme_cancel_ios(nvmeq, false);
1196 spin_unlock_irq(&nvmeq->q_lock);
1197}
1198
1199static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1200{
1201 struct nvme_queue *nvmeq = dev->queues[qid];
1202
1203 if (!nvmeq)
1204 return;
1205 if (nvme_suspend_queue(nvmeq))
1206 return;
1207
Keith Busch0e53d182013-12-10 13:10:39 -07001208 /* Don't tell the adapter to delete the admin queue.
1209 * Don't tell a removed adapter to delete IO queues. */
1210 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001211 adapter_delete_sq(dev, qid);
1212 adapter_delete_cq(dev, qid);
1213 }
Keith Busch4d115422013-12-10 13:10:40 -07001214 nvme_clear_queue(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001215}
1216
1217static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
1218 int depth, int vector)
1219{
1220 struct device *dmadev = &dev->pci_dev->dev;
Keith Busch22404272013-07-15 15:02:20 -06001221 unsigned extra = nvme_queue_extra(depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001222 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
1223 if (!nvmeq)
1224 return NULL;
1225
1226 nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
1227 &nvmeq->cq_dma_addr, GFP_KERNEL);
1228 if (!nvmeq->cqes)
1229 goto free_nvmeq;
1230 memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
1231
1232 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
1233 &nvmeq->sq_dma_addr, GFP_KERNEL);
1234 if (!nvmeq->sq_cmds)
1235 goto free_cqdma;
1236
1237 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001238 nvmeq->dev = dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001239 spin_lock_init(&nvmeq->q_lock);
1240 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001241 nvmeq->cq_phase = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001242 init_waitqueue_head(&nvmeq->sq_full);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001243 init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001244 bio_list_init(&nvmeq->sq_cong);
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001245 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001246 nvmeq->q_depth = depth;
1247 nvmeq->cq_vector = vector;
Keith Buschc30341d2013-12-10 13:10:38 -07001248 nvmeq->qid = qid;
Keith Busch22404272013-07-15 15:02:20 -06001249 nvmeq->q_suspended = 1;
1250 dev->queue_count++;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001251
1252 return nvmeq;
1253
1254 free_cqdma:
Keith Busch68b8eca2013-05-01 13:07:47 -06001255 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001256 nvmeq->cq_dma_addr);
1257 free_nvmeq:
1258 kfree(nvmeq);
1259 return NULL;
1260}
1261
Matthew Wilcox30010822011-01-20 09:10:15 -05001262static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1263 const char *name)
1264{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001265 if (use_threaded_interrupts)
1266 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001267 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001268 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001269 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001270 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001271}
1272
Keith Busch22404272013-07-15 15:02:20 -06001273static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001274{
Keith Busch22404272013-07-15 15:02:20 -06001275 struct nvme_dev *dev = nvmeq->dev;
1276 unsigned extra = nvme_queue_extra(nvmeq->q_depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001277
Keith Busch22404272013-07-15 15:02:20 -06001278 nvmeq->sq_tail = 0;
1279 nvmeq->cq_head = 0;
1280 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001281 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001282 memset(nvmeq->cmdid_data, 0, extra);
1283 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
1284 nvme_cancel_ios(nvmeq, false);
1285 nvmeq->q_suspended = 0;
1286}
1287
1288static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1289{
1290 struct nvme_dev *dev = nvmeq->dev;
1291 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001292
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001293 result = adapter_alloc_cq(dev, qid, nvmeq);
1294 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001295 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001296
1297 result = adapter_alloc_sq(dev, qid, nvmeq);
1298 if (result < 0)
1299 goto release_cq;
1300
Matthew Wilcox30010822011-01-20 09:10:15 -05001301 result = queue_request_irq(dev, nvmeq, "nvme");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001302 if (result < 0)
1303 goto release_sq;
1304
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001305 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001306 nvme_init_queue(nvmeq, qid);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001307 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001308
1309 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001310
1311 release_sq:
1312 adapter_delete_sq(dev, qid);
1313 release_cq:
1314 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001315 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001316}
1317
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001318static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1319{
1320 unsigned long timeout;
1321 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1322
1323 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1324
1325 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1326 msleep(100);
1327 if (fatal_signal_pending(current))
1328 return -EINTR;
1329 if (time_after(jiffies, timeout)) {
1330 dev_err(&dev->pci_dev->dev,
1331 "Device not ready; aborting initialisation\n");
1332 return -ENODEV;
1333 }
1334 }
1335
1336 return 0;
1337}
1338
1339/*
1340 * If the device has been passed off to us in an enabled state, just clear
1341 * the enabled bit. The spec says we should set the 'shutdown notification
1342 * bits', but doing so may cause the device to complete commands to the
1343 * admin queue ... and we don't know what memory that might be pointing at!
1344 */
1345static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1346{
Matthew Wilcox44af1462013-05-04 06:43:17 -04001347 u32 cc = readl(&dev->bar->cc);
1348
1349 if (cc & NVME_CC_ENABLE)
1350 writel(cc & ~NVME_CC_ENABLE, &dev->bar->cc);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001351 return nvme_wait_ready(dev, cap, false);
1352}
1353
1354static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1355{
1356 return nvme_wait_ready(dev, cap, true);
1357}
1358
Keith Busch1894d8f2013-07-15 15:02:22 -06001359static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1360{
1361 unsigned long timeout;
1362 u32 cc;
1363
1364 cc = (readl(&dev->bar->cc) & ~NVME_CC_SHN_MASK) | NVME_CC_SHN_NORMAL;
1365 writel(cc, &dev->bar->cc);
1366
1367 timeout = 2 * HZ + jiffies;
1368 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1369 NVME_CSTS_SHST_CMPLT) {
1370 msleep(100);
1371 if (fatal_signal_pending(current))
1372 return -EINTR;
1373 if (time_after(jiffies, timeout)) {
1374 dev_err(&dev->pci_dev->dev,
1375 "Device shutdown incomplete; abort shutdown\n");
1376 return -ENODEV;
1377 }
1378 }
1379
1380 return 0;
1381}
1382
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001383static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001384{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001385 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001386 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001387 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001388 struct nvme_queue *nvmeq;
1389
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001390 result = nvme_disable_ctrl(dev, cap);
1391 if (result < 0)
1392 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001393
Keith Buschcd638942013-07-15 15:02:23 -06001394 nvmeq = dev->queues[0];
1395 if (!nvmeq) {
1396 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
1397 if (!nvmeq)
1398 return -ENOMEM;
1399 dev->queues[0] = nvmeq;
1400 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001401
1402 aqa = nvmeq->q_depth - 1;
1403 aqa |= aqa << 16;
1404
1405 dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
1406 dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
1407 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001408 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001409
1410 writel(aqa, &dev->bar->aqa);
1411 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1412 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
1413 writel(dev->ctrl_config, &dev->bar->cc);
1414
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001415 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001416 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001417 return result;
Matthew Wilcox9e866772012-08-03 13:55:56 -04001418
Matthew Wilcox30010822011-01-20 09:10:15 -05001419 result = queue_request_irq(dev, nvmeq, "nvme admin");
Keith Busch025c5572013-05-01 13:07:51 -06001420 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001421 return result;
Keith Busch025c5572013-05-01 13:07:51 -06001422
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001423 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001424 nvme_init_queue(nvmeq, 0);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001425 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001426 return result;
1427}
1428
Vishal Verma5d0f6132013-03-04 18:40:58 -07001429struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001430 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001431{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001432 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001433 struct scatterlist *sg;
1434 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001435 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001436
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001437 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001438 return ERR_PTR(-EINVAL);
Dan Carpenter5460fc02013-05-13 17:59:50 +03001439 if (!length || length > INT_MAX - PAGE_SIZE)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001440 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001441
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001442 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001443 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1444 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001445 if (!pages)
1446 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001447
1448 err = get_user_pages_fast(addr, count, 1, pages);
1449 if (err < count) {
1450 count = err;
1451 err = -EFAULT;
1452 goto put_pages;
1453 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001454
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001455 iod = nvme_alloc_iod(count, length, GFP_KERNEL);
1456 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001457 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001458 for (i = 0; i < count; i++) {
1459 sg_set_page(&sg[i], pages[i],
Dan Carpenter5460fc02013-05-13 17:59:50 +03001460 min_t(unsigned, length, PAGE_SIZE - offset),
1461 offset);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001462 length -= (PAGE_SIZE - offset);
1463 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001464 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001465 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001466 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001467
1468 err = -ENOMEM;
1469 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1470 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001471 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001472 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001473
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001474 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001475 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001476
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001477 free_iod:
1478 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001479 put_pages:
1480 for (i = 0; i < count; i++)
1481 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001482 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001483 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001484}
1485
Vishal Verma5d0f6132013-03-04 18:40:58 -07001486void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001487 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001488{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001489 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001490
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001491 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1492 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001493
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001494 for (i = 0; i < iod->nents; i++)
1495 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001496}
1497
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001498static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1499{
1500 struct nvme_dev *dev = ns->dev;
1501 struct nvme_queue *nvmeq;
1502 struct nvme_user_io io;
1503 struct nvme_command c;
Keith Buschf410c682013-04-23 17:23:59 -06001504 unsigned length, meta_len;
1505 int status, i;
1506 struct nvme_iod *iod, *meta_iod = NULL;
1507 dma_addr_t meta_dma_addr;
1508 void *meta, *uninitialized_var(meta_mem);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001509
1510 if (copy_from_user(&io, uio, sizeof(io)))
1511 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001512 length = (io.nblocks + 1) << ns->lba_shift;
Keith Buschf410c682013-04-23 17:23:59 -06001513 meta_len = (io.nblocks + 1) * ns->ms;
1514
1515 if (meta_len && ((io.metadata & 3) || !io.metadata))
1516 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001517
1518 switch (io.opcode) {
1519 case nvme_cmd_write:
1520 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001521 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001522 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001523 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001524 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001525 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001526 }
1527
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001528 if (IS_ERR(iod))
1529 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001530
1531 memset(&c, 0, sizeof(c));
1532 c.rw.opcode = io.opcode;
1533 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001534 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001535 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001536 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001537 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001538 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1539 c.rw.reftag = cpu_to_le32(io.reftag);
1540 c.rw.apptag = cpu_to_le16(io.apptag);
1541 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buschf410c682013-04-23 17:23:59 -06001542
1543 if (meta_len) {
Keith Busch1b567492013-07-18 12:13:51 -06001544 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
1545 meta_len);
Keith Buschf410c682013-04-23 17:23:59 -06001546 if (IS_ERR(meta_iod)) {
1547 status = PTR_ERR(meta_iod);
1548 meta_iod = NULL;
1549 goto unmap;
1550 }
1551
1552 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1553 &meta_dma_addr, GFP_KERNEL);
1554 if (!meta_mem) {
1555 status = -ENOMEM;
1556 goto unmap;
1557 }
1558
1559 if (io.opcode & 1) {
1560 int meta_offset = 0;
1561
1562 for (i = 0; i < meta_iod->nents; i++) {
1563 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1564 meta_iod->sg[i].offset;
1565 memcpy(meta_mem + meta_offset, meta,
1566 meta_iod->sg[i].length);
1567 kunmap_atomic(meta);
1568 meta_offset += meta_iod->sg[i].length;
1569 }
1570 }
1571
1572 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1573 }
1574
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001575 length = nvme_setup_prps(dev, &c.common, iod, length, GFP_KERNEL);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001576
Matthew Wilcox040a93b2011-12-20 11:04:12 -05001577 nvmeq = get_nvmeq(dev);
Matthew Wilcoxfa922822011-03-16 16:29:00 -04001578 /*
1579 * Since nvme_submit_sync_cmd sleeps, we can't keep preemption
Matthew Wilcoxb1ad37e2011-02-04 16:14:30 -05001580 * disabled. We may be preempted at any point, and be rescheduled
1581 * to a different CPU. That will cause cacheline bouncing, but no
1582 * additional races since q_lock already protects against other CPUs.
1583 */
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001584 put_nvmeq(nvmeq);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001585 if (length != (io.nblocks + 1) << ns->lba_shift)
1586 status = -ENOMEM;
Keith Busch22404272013-07-15 15:02:20 -06001587 else if (!nvmeq || nvmeq->q_suspended)
1588 status = -EBUSY;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001589 else
Matthew Wilcoxff976d72011-12-20 13:53:01 -05001590 status = nvme_submit_sync_cmd(nvmeq, &c, NULL, NVME_IO_TIMEOUT);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001591
Keith Buschf410c682013-04-23 17:23:59 -06001592 if (meta_len) {
1593 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1594 int meta_offset = 0;
1595
1596 for (i = 0; i < meta_iod->nents; i++) {
1597 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1598 meta_iod->sg[i].offset;
1599 memcpy(meta, meta_mem + meta_offset,
1600 meta_iod->sg[i].length);
1601 kunmap_atomic(meta);
1602 meta_offset += meta_iod->sg[i].length;
1603 }
1604 }
1605
1606 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1607 meta_dma_addr);
1608 }
1609
1610 unmap:
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001611 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001612 nvme_free_iod(dev, iod);
Keith Buschf410c682013-04-23 17:23:59 -06001613
1614 if (meta_iod) {
1615 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1616 nvme_free_iod(dev, meta_iod);
1617 }
1618
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001619 return status;
1620}
1621
Keith Busch50af8ba2012-07-25 16:07:55 -06001622static int nvme_user_admin_cmd(struct nvme_dev *dev,
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001623 struct nvme_admin_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001624{
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001625 struct nvme_admin_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001626 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001627 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001628 struct nvme_iod *uninitialized_var(iod);
Keith Busch94f370c2013-05-09 14:01:38 -06001629 unsigned timeout;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001630
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001631 if (!capable(CAP_SYS_ADMIN))
1632 return -EACCES;
1633 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001634 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001635
1636 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001637 c.common.opcode = cmd.opcode;
1638 c.common.flags = cmd.flags;
1639 c.common.nsid = cpu_to_le32(cmd.nsid);
1640 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1641 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1642 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1643 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1644 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1645 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1646 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1647 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1648
1649 length = cmd.data_len;
1650 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001651 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1652 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001653 if (IS_ERR(iod))
1654 return PTR_ERR(iod);
1655 length = nvme_setup_prps(dev, &c.common, iod, length,
1656 GFP_KERNEL);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001657 }
1658
Keith Busch94f370c2013-05-09 14:01:38 -06001659 timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
1660 ADMIN_TIMEOUT;
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001661 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001662 status = -ENOMEM;
1663 else
Keith Busch94f370c2013-05-09 14:01:38 -06001664 status = nvme_submit_sync_cmd(dev->queues[0], &c, &cmd.result,
1665 timeout);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001666
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001667 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001668 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001669 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001670 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001671
Chayan Biswascf90bc42013-05-22 22:34:49 +00001672 if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
Keith Buschf4f117f2012-09-21 10:49:05 -06001673 sizeof(cmd.result)))
1674 status = -EFAULT;
1675
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001676 return status;
1677}
1678
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001679static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1680 unsigned long arg)
1681{
1682 struct nvme_ns *ns = bdev->bd_disk->private_data;
1683
1684 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001685 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001686 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001687 return ns->ns_id;
1688 case NVME_IOCTL_ADMIN_CMD:
Keith Busch50af8ba2012-07-25 16:07:55 -06001689 return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001690 case NVME_IOCTL_SUBMIT_IO:
1691 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001692 case SG_GET_VERSION_NUM:
1693 return nvme_sg_get_version_num((void __user *)arg);
1694 case SG_IO:
1695 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001696 default:
1697 return -ENOTTY;
1698 }
1699}
1700
Keith Busch320a3822013-10-23 13:07:34 -06001701#ifdef CONFIG_COMPAT
1702static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1703 unsigned int cmd, unsigned long arg)
1704{
1705 struct nvme_ns *ns = bdev->bd_disk->private_data;
1706
1707 switch (cmd) {
1708 case SG_IO:
1709 return nvme_sg_io32(ns, arg);
1710 }
1711 return nvme_ioctl(bdev, mode, cmd, arg);
1712}
1713#else
1714#define nvme_compat_ioctl NULL
1715#endif
1716
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001717static const struct block_device_operations nvme_fops = {
1718 .owner = THIS_MODULE,
1719 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06001720 .compat_ioctl = nvme_compat_ioctl,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001721};
1722
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001723static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1724{
1725 while (bio_list_peek(&nvmeq->sq_cong)) {
1726 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1727 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Keith Busch427e9702013-04-09 11:59:32 -06001728
Matthew Wilcox3cb967c2011-03-16 16:45:49 -04001729 if (bio_list_empty(&nvmeq->sq_cong))
1730 remove_wait_queue(&nvmeq->sq_full,
1731 &nvmeq->sq_cong_wait);
Keith Busch427e9702013-04-09 11:59:32 -06001732 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
1733 if (bio_list_empty(&nvmeq->sq_cong))
1734 add_wait_queue(&nvmeq->sq_full,
1735 &nvmeq->sq_cong_wait);
1736 bio_list_add_head(&nvmeq->sq_cong, bio);
1737 break;
1738 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001739 }
1740}
1741
1742static int nvme_kthread(void *data)
1743{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001744 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001745
1746 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001747 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001748 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001749 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001750 int i;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001751 if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
1752 dev->initialized) {
1753 if (work_busy(&dev->reset_work))
1754 continue;
1755 list_del_init(&dev->node);
1756 dev_warn(&dev->pci_dev->dev,
1757 "Failed status, reset controller\n");
1758 INIT_WORK(&dev->reset_work,
1759 nvme_reset_failed_dev);
1760 queue_work(nvme_workq, &dev->reset_work);
1761 continue;
1762 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001763 for (i = 0; i < dev->queue_count; i++) {
1764 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001765 if (!nvmeq)
1766 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001767 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001768 if (nvmeq->q_suspended)
1769 goto unlock;
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001770 nvme_process_cq(nvmeq);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001771 nvme_cancel_ios(nvmeq, true);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001772 nvme_resubmit_bios(nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001773 unlock:
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001774 spin_unlock_irq(&nvmeq->q_lock);
1775 }
1776 }
1777 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001778 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001779 }
1780 return 0;
1781}
1782
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001783static DEFINE_IDA(nvme_index_ida);
1784
1785static int nvme_get_ns_idx(void)
1786{
1787 int index, error;
1788
1789 do {
1790 if (!ida_pre_get(&nvme_index_ida, GFP_KERNEL))
1791 return -1;
1792
1793 spin_lock(&dev_list_lock);
1794 error = ida_get_new(&nvme_index_ida, &index);
1795 spin_unlock(&dev_list_lock);
1796 } while (error == -EAGAIN);
1797
1798 if (error)
1799 index = -1;
1800 return index;
1801}
1802
1803static void nvme_put_ns_idx(int index)
1804{
1805 spin_lock(&dev_list_lock);
1806 ida_remove(&nvme_index_ida, index);
1807 spin_unlock(&dev_list_lock);
1808}
1809
Keith Busch0e5e4f02012-11-09 16:33:05 -07001810static void nvme_config_discard(struct nvme_ns *ns)
1811{
1812 u32 logical_block_size = queue_logical_block_size(ns->queue);
1813 ns->queue->limits.discard_zeroes_data = 0;
1814 ns->queue->limits.discard_alignment = logical_block_size;
1815 ns->queue->limits.discard_granularity = logical_block_size;
1816 ns->queue->limits.max_discard_sectors = 0xffffffff;
1817 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1818}
1819
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001820static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001821 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1822{
1823 struct nvme_ns *ns;
1824 struct gendisk *disk;
1825 int lbaf;
1826
1827 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1828 return NULL;
1829
1830 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1831 if (!ns)
1832 return NULL;
1833 ns->queue = blk_alloc_queue(GFP_KERNEL);
1834 if (!ns->queue)
1835 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001836 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
1837 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1838 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001839 blk_queue_make_request(ns->queue, nvme_make_request);
1840 ns->dev = dev;
1841 ns->queue->queuedata = ns;
1842
1843 disk = alloc_disk(NVME_MINORS);
1844 if (!disk)
1845 goto out_free_queue;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001846 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001847 ns->disk = disk;
1848 lbaf = id->flbas & 0xf;
1849 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Buschf410c682013-04-23 17:23:59 -06001850 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busche9ef4632012-07-24 15:01:04 -06001851 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06001852 if (dev->max_hw_sectors)
1853 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001854
1855 disk->major = nvme_major;
1856 disk->minors = NVME_MINORS;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001857 disk->first_minor = NVME_MINORS * nvme_get_ns_idx();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001858 disk->fops = &nvme_fops;
1859 disk->private_data = ns;
1860 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05001861 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001862 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001863 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1864
Keith Busch0e5e4f02012-11-09 16:33:05 -07001865 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1866 nvme_config_discard(ns);
1867
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001868 return ns;
1869
1870 out_free_queue:
1871 blk_cleanup_queue(ns->queue);
1872 out_free_ns:
1873 kfree(ns);
1874 return NULL;
1875}
1876
1877static void nvme_ns_free(struct nvme_ns *ns)
1878{
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001879 int index = ns->disk->first_minor / NVME_MINORS;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001880 put_disk(ns->disk);
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001881 nvme_put_ns_idx(index);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001882 blk_cleanup_queue(ns->queue);
1883 kfree(ns);
1884}
1885
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001886static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001887{
1888 int status;
1889 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001890 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001891
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001892 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001893 &result);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001894 if (status)
Keith Busch7e03b122013-07-29 16:20:56 -06001895 return status < 0 ? -EIO : -EBUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001896 return min(result & 0xffff, result >> 16) + 1;
1897}
1898
Keith Busch9d713c22013-07-15 15:02:24 -06001899static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1900{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001901 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06001902}
1903
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001904static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001905{
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001906 struct pci_dev *pdev = dev->pci_dev;
Keith Busch9d713c22013-07-15 15:02:24 -06001907 int result, cpu, i, vecs, nr_io_queues, size, q_depth;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001908
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001909 nr_io_queues = num_online_cpus();
1910 result = set_queue_count(dev, nr_io_queues);
Matthew Wilcox1b234842011-01-20 13:01:49 -05001911 if (result < 0)
1912 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001913 if (result < nr_io_queues)
1914 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001915
Keith Busch9d713c22013-07-15 15:02:24 -06001916 size = db_bar_size(dev, nr_io_queues);
1917 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001918 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06001919 do {
1920 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1921 if (dev->bar)
1922 break;
1923 if (!--nr_io_queues)
1924 return -ENOMEM;
1925 size = db_bar_size(dev, nr_io_queues);
1926 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001927 dev->dbs = ((void __iomem *)dev->bar) + 4096;
1928 dev->queues[0]->q_db = dev->dbs;
1929 }
1930
Keith Busch9d713c22013-07-15 15:02:24 -06001931 /* Deregister the admin queue's interrupt */
1932 free_irq(dev->entry[0].vector, dev->queues[0]);
1933
Matthew Wilcox063a8092013-06-20 10:53:48 -04001934 vecs = nr_io_queues;
1935 for (i = 0; i < vecs; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001936 dev->entry[i].entry = i;
1937 for (;;) {
Matthew Wilcox063a8092013-06-20 10:53:48 -04001938 result = pci_enable_msix(pdev, dev->entry, vecs);
1939 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001940 break;
Matthew Wilcox063a8092013-06-20 10:53:48 -04001941 vecs = result;
1942 }
1943
1944 if (result < 0) {
1945 vecs = nr_io_queues;
1946 if (vecs > 32)
1947 vecs = 32;
1948 for (;;) {
1949 result = pci_enable_msi_block(pdev, vecs);
1950 if (result == 0) {
1951 for (i = 0; i < vecs; i++)
1952 dev->entry[i].vector = i + pdev->irq;
1953 break;
1954 } else if (result < 0) {
1955 vecs = 1;
1956 break;
1957 }
1958 vecs = result;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001959 }
1960 }
1961
Matthew Wilcox063a8092013-06-20 10:53:48 -04001962 /*
1963 * Should investigate if there's a performance win from allocating
1964 * more queues than interrupt vectors; it might allow the submission
1965 * path to scale better, even if the receive path is limited by the
1966 * number of interrupts.
1967 */
1968 nr_io_queues = vecs;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001969
Matthew Wilcox1b234842011-01-20 13:01:49 -05001970 result = queue_request_irq(dev, dev->queues[0], "nvme admin");
Keith Busch9d713c22013-07-15 15:02:24 -06001971 if (result) {
1972 dev->queues[0]->q_suspended = 1;
Keith Busch22404272013-07-15 15:02:20 -06001973 goto free_queues;
Keith Busch9d713c22013-07-15 15:02:24 -06001974 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001975
Keith Buschcd638942013-07-15 15:02:23 -06001976 /* Free previously allocated queues that are no longer usable */
1977 spin_lock(&dev_list_lock);
1978 for (i = dev->queue_count - 1; i > nr_io_queues; i--) {
1979 struct nvme_queue *nvmeq = dev->queues[i];
1980
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001981 spin_lock_irq(&nvmeq->q_lock);
Keith Buschcd638942013-07-15 15:02:23 -06001982 nvme_cancel_ios(nvmeq, false);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001983 spin_unlock_irq(&nvmeq->q_lock);
Keith Buschcd638942013-07-15 15:02:23 -06001984
1985 nvme_free_queue(nvmeq);
1986 dev->queue_count--;
1987 dev->queues[i] = NULL;
1988 }
1989 spin_unlock(&dev_list_lock);
1990
Matthew Wilcox1b234842011-01-20 13:01:49 -05001991 cpu = cpumask_first(cpu_online_mask);
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001992 for (i = 0; i < nr_io_queues; i++) {
Matthew Wilcox1b234842011-01-20 13:01:49 -05001993 irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu));
1994 cpu = cpumask_next(cpu, cpu_online_mask);
1995 }
1996
Keith Buscha0cadb82012-07-27 13:57:23 -04001997 q_depth = min_t(int, NVME_CAP_MQES(readq(&dev->bar->cap)) + 1,
1998 NVME_Q_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001999 for (i = dev->queue_count - 1; i < nr_io_queues; i++) {
Keith Busch22404272013-07-15 15:02:20 -06002000 dev->queues[i + 1] = nvme_alloc_queue(dev, i + 1, q_depth, i);
2001 if (!dev->queues[i + 1]) {
2002 result = -ENOMEM;
2003 goto free_queues;
2004 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05002005 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002006
Matthew Wilcox9ecdc942011-03-16 16:52:19 -04002007 for (; i < num_possible_cpus(); i++) {
2008 int target = i % rounddown_pow_of_two(dev->queue_count - 1);
2009 dev->queues[i + 1] = dev->queues[target + 1];
2010 }
2011
Keith Busch22404272013-07-15 15:02:20 -06002012 for (i = 1; i < dev->queue_count; i++) {
2013 result = nvme_create_queue(dev->queues[i], i);
2014 if (result) {
2015 for (--i; i > 0; i--)
2016 nvme_disable_queue(dev, i);
2017 goto free_queues;
2018 }
2019 }
2020
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002021 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002022
Keith Busch22404272013-07-15 15:02:20 -06002023 free_queues:
2024 nvme_free_queues(dev);
2025 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002026}
2027
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002028/*
2029 * Return: error value if an error occurred setting up the queues or calling
2030 * Identify Device. 0 if these succeeded, even if adding some of the
2031 * namespaces failed. At the moment, these failures are silent. TBD which
2032 * failures should be reported.
2033 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002034static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002035{
Matthew Wilcox68608c262013-06-21 14:36:34 -04002036 struct pci_dev *pdev = dev->pci_dev;
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002037 int res;
2038 unsigned nn, i;
Keith Buschcbb62182013-05-01 13:07:49 -06002039 struct nvme_ns *ns;
Matthew Wilcox51814232011-02-01 16:18:08 -05002040 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002041 struct nvme_id_ns *id_ns;
2042 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002043 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06002044 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002045
Matthew Wilcox68608c262013-06-21 14:36:34 -04002046 mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06002047 if (!mem)
2048 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002049
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002050 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002051 if (res) {
2052 res = -EIO;
Keith Buschcbb62182013-05-01 13:07:49 -06002053 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002054 }
2055
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002056 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05002057 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002058 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002059 dev->abort_limit = ctrl->acl + 1;
Matthew Wilcox51814232011-02-01 16:18:08 -05002060 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2061 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2062 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002063 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002064 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002065 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
2066 (pdev->device == 0x0953) && ctrl->vs[3])
Keith Busch159b67d2013-04-09 17:13:20 -06002067 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002068
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002069 id_ns = mem;
Matthew Wilcox2b2c1892011-10-07 13:10:13 -04002070 for (i = 1; i <= nn; i++) {
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002071 res = nvme_identify(dev, i, 0, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002072 if (res)
2073 continue;
2074
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002075 if (id_ns->ncap == 0)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002076 continue;
2077
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002078 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
Keith Busch08df1e02012-09-21 10:52:13 -06002079 dma_addr + 4096, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002080 if (res)
Keith Busch12209032013-01-31 14:40:38 -07002081 memset(mem + 4096, 0, 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002082
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002083 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002084 if (ns)
2085 list_add_tail(&ns->list, &dev->namespaces);
2086 }
2087 list_for_each_entry(ns, &dev->namespaces, list)
2088 add_disk(ns->disk);
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002089 res = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002090
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002091 out:
Matthew Wilcox684f5c22011-09-19 17:14:53 -04002092 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002093 return res;
2094}
2095
Keith Busch0877cb02013-07-15 15:02:19 -06002096static int nvme_dev_map(struct nvme_dev *dev)
2097{
2098 int bars, result = -ENOMEM;
2099 struct pci_dev *pdev = dev->pci_dev;
2100
2101 if (pci_enable_device_mem(pdev))
2102 return result;
2103
2104 dev->entry[0].vector = pdev->irq;
2105 pci_set_master(pdev);
2106 bars = pci_select_bars(pdev, IORESOURCE_MEM);
2107 if (pci_request_selected_regions(pdev, bars, "nvme"))
2108 goto disable_pci;
2109
Russell King052d0ef2013-06-26 23:49:11 +01002110 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
2111 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2112 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002113
Keith Busch0877cb02013-07-15 15:02:19 -06002114 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2115 if (!dev->bar)
2116 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07002117 if (readl(&dev->bar->csts) == -1) {
2118 result = -ENODEV;
2119 goto unmap;
2120 }
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002121 dev->db_stride = 1 << NVME_CAP_STRIDE(readq(&dev->bar->cap));
Keith Busch0877cb02013-07-15 15:02:19 -06002122 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2123
2124 return 0;
2125
Keith Busch0e53d182013-12-10 13:10:39 -07002126 unmap:
2127 iounmap(dev->bar);
2128 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002129 disable:
2130 pci_release_regions(pdev);
2131 disable_pci:
2132 pci_disable_device(pdev);
2133 return result;
2134}
2135
2136static void nvme_dev_unmap(struct nvme_dev *dev)
2137{
2138 if (dev->pci_dev->msi_enabled)
2139 pci_disable_msi(dev->pci_dev);
2140 else if (dev->pci_dev->msix_enabled)
2141 pci_disable_msix(dev->pci_dev);
2142
2143 if (dev->bar) {
2144 iounmap(dev->bar);
2145 dev->bar = NULL;
Keith Busch9a6b9452013-12-10 13:10:36 -07002146 pci_release_regions(dev->pci_dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002147 }
2148
Keith Busch0877cb02013-07-15 15:02:19 -06002149 if (pci_is_enabled(dev->pci_dev))
2150 pci_disable_device(dev->pci_dev);
2151}
2152
Keith Busch4d115422013-12-10 13:10:40 -07002153struct nvme_delq_ctx {
2154 struct task_struct *waiter;
2155 struct kthread_worker *worker;
2156 atomic_t refcount;
2157};
2158
2159static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2160{
2161 dq->waiter = current;
2162 mb();
2163
2164 for (;;) {
2165 set_current_state(TASK_KILLABLE);
2166 if (!atomic_read(&dq->refcount))
2167 break;
2168 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2169 fatal_signal_pending(current)) {
2170 set_current_state(TASK_RUNNING);
2171
2172 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
2173 nvme_disable_queue(dev, 0);
2174
2175 send_sig(SIGKILL, dq->worker->task, 1);
2176 flush_kthread_worker(dq->worker);
2177 return;
2178 }
2179 }
2180 set_current_state(TASK_RUNNING);
2181}
2182
2183static void nvme_put_dq(struct nvme_delq_ctx *dq)
2184{
2185 atomic_dec(&dq->refcount);
2186 if (dq->waiter)
2187 wake_up_process(dq->waiter);
2188}
2189
2190static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2191{
2192 atomic_inc(&dq->refcount);
2193 return dq;
2194}
2195
2196static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2197{
2198 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
2199
2200 nvme_clear_queue(nvmeq);
2201 nvme_put_dq(dq);
2202}
2203
2204static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2205 kthread_work_func_t fn)
2206{
2207 struct nvme_command c;
2208
2209 memset(&c, 0, sizeof(c));
2210 c.delete_queue.opcode = opcode;
2211 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2212
2213 init_kthread_work(&nvmeq->cmdinfo.work, fn);
2214 return nvme_submit_admin_cmd_async(nvmeq->dev, &c, &nvmeq->cmdinfo);
2215}
2216
2217static void nvme_del_cq_work_handler(struct kthread_work *work)
2218{
2219 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2220 cmdinfo.work);
2221 nvme_del_queue_end(nvmeq);
2222}
2223
2224static int nvme_delete_cq(struct nvme_queue *nvmeq)
2225{
2226 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2227 nvme_del_cq_work_handler);
2228}
2229
2230static void nvme_del_sq_work_handler(struct kthread_work *work)
2231{
2232 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2233 cmdinfo.work);
2234 int status = nvmeq->cmdinfo.status;
2235
2236 if (!status)
2237 status = nvme_delete_cq(nvmeq);
2238 if (status)
2239 nvme_del_queue_end(nvmeq);
2240}
2241
2242static int nvme_delete_sq(struct nvme_queue *nvmeq)
2243{
2244 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2245 nvme_del_sq_work_handler);
2246}
2247
2248static void nvme_del_queue_start(struct kthread_work *work)
2249{
2250 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2251 cmdinfo.work);
2252 allow_signal(SIGKILL);
2253 if (nvme_delete_sq(nvmeq))
2254 nvme_del_queue_end(nvmeq);
2255}
2256
2257static void nvme_disable_io_queues(struct nvme_dev *dev)
2258{
2259 int i;
2260 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2261 struct nvme_delq_ctx dq;
2262 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2263 &worker, "nvme%d", dev->instance);
2264
2265 if (IS_ERR(kworker_task)) {
2266 dev_err(&dev->pci_dev->dev,
2267 "Failed to create queue del task\n");
2268 for (i = dev->queue_count - 1; i > 0; i--)
2269 nvme_disable_queue(dev, i);
2270 return;
2271 }
2272
2273 dq.waiter = NULL;
2274 atomic_set(&dq.refcount, 0);
2275 dq.worker = &worker;
2276 for (i = dev->queue_count - 1; i > 0; i--) {
2277 struct nvme_queue *nvmeq = dev->queues[i];
2278
2279 if (nvme_suspend_queue(nvmeq))
2280 continue;
2281 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2282 nvmeq->cmdinfo.worker = dq.worker;
2283 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2284 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2285 }
2286 nvme_wait_dq(&dq, dev);
2287 kthread_stop(kworker_task);
2288}
2289
Keith Buschf0b50732013-07-15 15:02:21 -06002290static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002291{
Keith Busch22404272013-07-15 15:02:20 -06002292 int i;
2293
Keith Buschd4b4ff82013-12-10 13:10:37 -07002294 dev->initialized = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002295
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002296 spin_lock(&dev_list_lock);
Keith Buschf0b50732013-07-15 15:02:21 -06002297 list_del_init(&dev->node);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002298 spin_unlock(&dev_list_lock);
2299
Keith Busch4d115422013-12-10 13:10:40 -07002300 if (!dev->bar || (dev->bar && readl(&dev->bar->csts) == -1)) {
2301 for (i = dev->queue_count - 1; i >= 0; i--) {
2302 struct nvme_queue *nvmeq = dev->queues[i];
2303 nvme_suspend_queue(nvmeq);
2304 nvme_clear_queue(nvmeq);
2305 }
2306 } else {
2307 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002308 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002309 nvme_disable_queue(dev, 0);
2310 }
Keith Buschf0b50732013-07-15 15:02:21 -06002311 nvme_dev_unmap(dev);
2312}
2313
2314static void nvme_dev_remove(struct nvme_dev *dev)
2315{
2316 struct nvme_ns *ns, *next;
2317
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002318 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2319 list_del(&ns->list);
2320 del_gendisk(ns->disk);
2321 nvme_ns_free(ns);
2322 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002323}
2324
Matthew Wilcox091b6092011-02-10 09:56:01 -05002325static int nvme_setup_prp_pools(struct nvme_dev *dev)
2326{
2327 struct device *dmadev = &dev->pci_dev->dev;
2328 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2329 PAGE_SIZE, PAGE_SIZE, 0);
2330 if (!dev->prp_page_pool)
2331 return -ENOMEM;
2332
Matthew Wilcox99802a72011-02-10 10:30:34 -05002333 /* Optimisation for I/Os between 4k and 128k */
2334 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2335 256, 256, 0);
2336 if (!dev->prp_small_pool) {
2337 dma_pool_destroy(dev->prp_page_pool);
2338 return -ENOMEM;
2339 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002340 return 0;
2341}
2342
2343static void nvme_release_prp_pools(struct nvme_dev *dev)
2344{
2345 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002346 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002347}
2348
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002349static DEFINE_IDA(nvme_instance_ida);
2350
2351static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002352{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002353 int instance, error;
2354
2355 do {
2356 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2357 return -ENODEV;
2358
2359 spin_lock(&dev_list_lock);
2360 error = ida_get_new(&nvme_instance_ida, &instance);
2361 spin_unlock(&dev_list_lock);
2362 } while (error == -EAGAIN);
2363
2364 if (error)
2365 return -ENODEV;
2366
2367 dev->instance = instance;
2368 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002369}
2370
2371static void nvme_release_instance(struct nvme_dev *dev)
2372{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002373 spin_lock(&dev_list_lock);
2374 ida_remove(&nvme_instance_ida, dev->instance);
2375 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002376}
2377
Keith Busch5e82e952013-02-19 10:17:58 -07002378static void nvme_free_dev(struct kref *kref)
2379{
2380 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch5e82e952013-02-19 10:17:58 -07002381 kfree(dev->queues);
2382 kfree(dev->entry);
2383 kfree(dev);
2384}
2385
2386static int nvme_dev_open(struct inode *inode, struct file *f)
2387{
2388 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
2389 miscdev);
2390 kref_get(&dev->kref);
2391 f->private_data = dev;
2392 return 0;
2393}
2394
2395static int nvme_dev_release(struct inode *inode, struct file *f)
2396{
2397 struct nvme_dev *dev = f->private_data;
2398 kref_put(&dev->kref, nvme_free_dev);
2399 return 0;
2400}
2401
2402static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2403{
2404 struct nvme_dev *dev = f->private_data;
2405 switch (cmd) {
2406 case NVME_IOCTL_ADMIN_CMD:
2407 return nvme_user_admin_cmd(dev, (void __user *)arg);
2408 default:
2409 return -ENOTTY;
2410 }
2411}
2412
2413static const struct file_operations nvme_dev_fops = {
2414 .owner = THIS_MODULE,
2415 .open = nvme_dev_open,
2416 .release = nvme_dev_release,
2417 .unlocked_ioctl = nvme_dev_ioctl,
2418 .compat_ioctl = nvme_dev_ioctl,
2419};
2420
Keith Buschf0b50732013-07-15 15:02:21 -06002421static int nvme_dev_start(struct nvme_dev *dev)
2422{
2423 int result;
2424
2425 result = nvme_dev_map(dev);
2426 if (result)
2427 return result;
2428
2429 result = nvme_configure_admin_queue(dev);
2430 if (result)
2431 goto unmap;
2432
2433 spin_lock(&dev_list_lock);
2434 list_add(&dev->node, &dev_list);
2435 spin_unlock(&dev_list_lock);
2436
2437 result = nvme_setup_io_queues(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002438 if (result && result != -EBUSY)
Keith Buschf0b50732013-07-15 15:02:21 -06002439 goto disable;
2440
Keith Buschd82e8bf2013-09-05 14:45:07 -06002441 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002442
2443 disable:
2444 spin_lock(&dev_list_lock);
2445 list_del_init(&dev->node);
2446 spin_unlock(&dev_list_lock);
2447 unmap:
2448 nvme_dev_unmap(dev);
2449 return result;
2450}
2451
Keith Busch9a6b9452013-12-10 13:10:36 -07002452static int nvme_remove_dead_ctrl(void *arg)
2453{
2454 struct nvme_dev *dev = (struct nvme_dev *)arg;
2455 struct pci_dev *pdev = dev->pci_dev;
2456
2457 if (pci_get_drvdata(pdev))
2458 pci_stop_and_remove_bus_device(pdev);
2459 kref_put(&dev->kref, nvme_free_dev);
2460 return 0;
2461}
2462
2463static void nvme_remove_disks(struct work_struct *ws)
2464{
2465 int i;
2466 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2467
2468 nvme_dev_remove(dev);
2469 spin_lock(&dev_list_lock);
2470 for (i = dev->queue_count - 1; i > 0; i--) {
2471 BUG_ON(!dev->queues[i] || !dev->queues[i]->q_suspended);
2472 nvme_free_queue(dev->queues[i]);
2473 dev->queue_count--;
2474 dev->queues[i] = NULL;
2475 }
2476 spin_unlock(&dev_list_lock);
2477}
2478
2479static int nvme_dev_resume(struct nvme_dev *dev)
2480{
2481 int ret;
2482
2483 ret = nvme_dev_start(dev);
2484 if (ret && ret != -EBUSY)
2485 return ret;
2486 if (ret == -EBUSY) {
2487 spin_lock(&dev_list_lock);
2488 INIT_WORK(&dev->reset_work, nvme_remove_disks);
2489 queue_work(nvme_workq, &dev->reset_work);
2490 spin_unlock(&dev_list_lock);
2491 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002492 dev->initialized = 1;
Keith Busch9a6b9452013-12-10 13:10:36 -07002493 return 0;
2494}
2495
2496static void nvme_dev_reset(struct nvme_dev *dev)
2497{
2498 nvme_dev_shutdown(dev);
2499 if (nvme_dev_resume(dev)) {
2500 dev_err(&dev->pci_dev->dev, "Device failed to resume\n");
2501 kref_get(&dev->kref);
2502 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2503 dev->instance))) {
2504 dev_err(&dev->pci_dev->dev,
2505 "Failed to start controller remove task\n");
2506 kref_put(&dev->kref, nvme_free_dev);
2507 }
2508 }
2509}
2510
2511static void nvme_reset_failed_dev(struct work_struct *ws)
2512{
2513 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2514 nvme_dev_reset(dev);
2515}
2516
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002517static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002518{
Keith Busch0877cb02013-07-15 15:02:19 -06002519 int result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002520 struct nvme_dev *dev;
2521
2522 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2523 if (!dev)
2524 return -ENOMEM;
2525 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
2526 GFP_KERNEL);
2527 if (!dev->entry)
2528 goto free;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002529 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
2530 GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002531 if (!dev->queues)
2532 goto free;
2533
2534 INIT_LIST_HEAD(&dev->namespaces);
2535 dev->pci_dev = pdev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002536 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002537 result = nvme_set_instance(dev);
2538 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002539 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002540
Matthew Wilcox091b6092011-02-10 09:56:01 -05002541 result = nvme_setup_prp_pools(dev);
2542 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002543 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002544
Keith Buschf0b50732013-07-15 15:02:21 -06002545 result = nvme_dev_start(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002546 if (result) {
2547 if (result == -EBUSY)
2548 goto create_cdev;
Keith Busch0877cb02013-07-15 15:02:19 -06002549 goto release_pools;
Keith Buschd82e8bf2013-09-05 14:45:07 -06002550 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002551
Matthew Wilcox740216f2011-02-15 16:28:20 -05002552 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002553 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002554 goto shutdown;
Matthew Wilcox740216f2011-02-15 16:28:20 -05002555
Keith Buschd82e8bf2013-09-05 14:45:07 -06002556 create_cdev:
Keith Busch5e82e952013-02-19 10:17:58 -07002557 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
2558 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2559 dev->miscdev.parent = &pdev->dev;
2560 dev->miscdev.name = dev->name;
2561 dev->miscdev.fops = &nvme_dev_fops;
2562 result = misc_register(&dev->miscdev);
2563 if (result)
2564 goto remove;
2565
Keith Buschd4b4ff82013-12-10 13:10:37 -07002566 dev->initialized = 1;
Keith Busch5e82e952013-02-19 10:17:58 -07002567 kref_init(&dev->kref);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002568 return 0;
2569
Keith Busch5e82e952013-02-19 10:17:58 -07002570 remove:
2571 nvme_dev_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002572 shutdown:
2573 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002574 release_pools:
Keith Buschf0b50732013-07-15 15:02:21 -06002575 nvme_free_queues(dev);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002576 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002577 release:
2578 nvme_release_instance(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002579 free:
2580 kfree(dev->queues);
2581 kfree(dev->entry);
2582 kfree(dev);
2583 return result;
2584}
2585
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002586static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002587{
2588 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002589
2590 spin_lock(&dev_list_lock);
2591 list_del_init(&dev->node);
2592 spin_unlock(&dev_list_lock);
2593
2594 pci_set_drvdata(pdev, NULL);
2595 flush_work(&dev->reset_work);
Keith Busch5e82e952013-02-19 10:17:58 -07002596 misc_deregister(&dev->miscdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002597 nvme_dev_remove(dev);
2598 nvme_dev_shutdown(dev);
2599 nvme_free_queues(dev);
2600 nvme_release_instance(dev);
2601 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002602 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002603}
2604
2605/* These functions are yet to be implemented */
2606#define nvme_error_detected NULL
2607#define nvme_dump_registers NULL
2608#define nvme_link_reset NULL
2609#define nvme_slot_reset NULL
2610#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002611
2612static int nvme_suspend(struct device *dev)
2613{
2614 struct pci_dev *pdev = to_pci_dev(dev);
2615 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2616
2617 nvme_dev_shutdown(ndev);
2618 return 0;
2619}
2620
2621static int nvme_resume(struct device *dev)
2622{
2623 struct pci_dev *pdev = to_pci_dev(dev);
2624 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002625
Keith Busch9a6b9452013-12-10 13:10:36 -07002626 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
2627 INIT_WORK(&ndev->reset_work, nvme_reset_failed_dev);
2628 queue_work(nvme_workq, &ndev->reset_work);
2629 }
2630 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002631}
2632
2633static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002634
Stephen Hemminger1d352032012-09-07 09:33:17 -07002635static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002636 .error_detected = nvme_error_detected,
2637 .mmio_enabled = nvme_dump_registers,
2638 .link_reset = nvme_link_reset,
2639 .slot_reset = nvme_slot_reset,
2640 .resume = nvme_error_resume,
2641};
2642
2643/* Move to pci_ids.h later */
2644#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2645
2646static DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
2647 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2648 { 0, }
2649};
2650MODULE_DEVICE_TABLE(pci, nvme_id_table);
2651
2652static struct pci_driver nvme_driver = {
2653 .name = "nvme",
2654 .id_table = nvme_id_table,
2655 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002656 .remove = nvme_remove,
Keith Buschcd638942013-07-15 15:02:23 -06002657 .driver = {
2658 .pm = &nvme_dev_pm_ops,
2659 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002660 .err_handler = &nvme_err_handler,
2661};
2662
2663static int __init nvme_init(void)
2664{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002665 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002666
2667 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2668 if (IS_ERR(nvme_thread))
2669 return PTR_ERR(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002670
Keith Busch9a6b9452013-12-10 13:10:36 -07002671 result = -ENOMEM;
2672 nvme_workq = create_singlethread_workqueue("nvme");
2673 if (!nvme_workq)
2674 goto kill_kthread;
2675
Keith Busch5c42ea12012-07-25 16:05:18 -06002676 result = register_blkdev(nvme_major, "nvme");
2677 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002678 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06002679 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002680 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002681
2682 result = pci_register_driver(&nvme_driver);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002683 if (result)
2684 goto unregister_blkdev;
2685 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002686
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002687 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002688 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002689 kill_workq:
2690 destroy_workqueue(nvme_workq);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002691 kill_kthread:
2692 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002693 return result;
2694}
2695
2696static void __exit nvme_exit(void)
2697{
2698 pci_unregister_driver(&nvme_driver);
2699 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002700 destroy_workqueue(nvme_workq);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002701 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002702}
2703
2704MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2705MODULE_LICENSE("GPL");
Matthew Wilcox366e8212012-01-10 16:30:15 -05002706MODULE_VERSION("0.8");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002707module_init(nvme_init);
2708module_exit(nvme_exit);