blob: 23728099e36f28d1a7702ab7c4076c8ef5f019c7 [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
3 * Copyright (c) 2011, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#include <linux/nvme.h>
20#include <linux/bio.h>
Matthew Wilcox8de05532011-05-12 13:50:28 -040021#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050022#include <linux/blkdev.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040023#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050024#include <linux/errno.h>
25#include <linux/fs.h>
26#include <linux/genhd.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040027#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050028#include <linux/init.h>
29#include <linux/interrupt.h>
30#include <linux/io.h>
31#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050032#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050033#include <linux/kernel.h>
34#include <linux/mm.h>
35#include <linux/module.h>
36#include <linux/moduleparam.h>
37#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050038#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040039#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050040#include <linux/sched.h>
41#include <linux/slab.h>
42#include <linux/types.h>
Vishal Verma5d0f6132013-03-04 18:40:58 -070043#include <scsi/sg.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090044#include <asm-generic/io-64-nonatomic-lo-hi.h>
45
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050046#define NVME_Q_DEPTH 1024
47#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
48#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Matthew Wilcoxe85248e2011-02-06 18:30:16 -050049#define ADMIN_TIMEOUT (60 * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050050
51static int nvme_major;
52module_param(nvme_major, int, 0);
53
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050054static int use_threaded_interrupts;
55module_param(use_threaded_interrupts, int, 0);
56
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050057static DEFINE_SPINLOCK(dev_list_lock);
58static LIST_HEAD(dev_list);
59static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070060static struct workqueue_struct *nvme_workq;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050061
Keith Buschd4b4ff82013-12-10 13:10:37 -070062static void nvme_reset_failed_dev(struct work_struct *ws);
63
Keith Busch4d115422013-12-10 13:10:40 -070064struct async_cmd_info {
65 struct kthread_work work;
66 struct kthread_worker *worker;
67 u32 result;
68 int status;
69 void *ctx;
70};
71
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050072/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050073 * An NVM Express queue. Each device has at least two (one for admin
74 * commands and one for I/O commands).
75 */
76struct nvme_queue {
77 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -050078 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -050079 char irqname[24]; /* nvme4294967295-65535\0 */
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);
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05001078 PREPARE_WORK(&dev->reset_work, nvme_reset_failed_dev);
Keith Buschc30341d2013-12-10 13:10:38 -07001079 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 Buscha1a5ef92013-12-16 13:50:00 -05001156static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001157{
1158 int i;
1159
Keith Buscha1a5ef92013-12-16 13:50:00 -05001160 for (i = dev->queue_count - 1; i >= lowest; i--) {
Keith Busch22404272013-07-15 15:02:20 -06001161 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 Wilcox3193f072014-01-27 15:57:22 -05001239 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1240 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001241 spin_lock_init(&nvmeq->q_lock);
1242 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001243 nvmeq->cq_phase = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001244 init_waitqueue_head(&nvmeq->sq_full);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001245 init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001246 bio_list_init(&nvmeq->sq_cong);
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001247 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001248 nvmeq->q_depth = depth;
1249 nvmeq->cq_vector = vector;
Keith Buschc30341d2013-12-10 13:10:38 -07001250 nvmeq->qid = qid;
Keith Busch22404272013-07-15 15:02:20 -06001251 nvmeq->q_suspended = 1;
1252 dev->queue_count++;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001253
1254 return nvmeq;
1255
1256 free_cqdma:
Keith Busch68b8eca2013-05-01 13:07:47 -06001257 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001258 nvmeq->cq_dma_addr);
1259 free_nvmeq:
1260 kfree(nvmeq);
1261 return NULL;
1262}
1263
Matthew Wilcox30010822011-01-20 09:10:15 -05001264static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1265 const char *name)
1266{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001267 if (use_threaded_interrupts)
1268 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001269 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001270 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001271 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001272 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001273}
1274
Keith Busch22404272013-07-15 15:02:20 -06001275static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001276{
Keith Busch22404272013-07-15 15:02:20 -06001277 struct nvme_dev *dev = nvmeq->dev;
1278 unsigned extra = nvme_queue_extra(nvmeq->q_depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001279
Keith Busch22404272013-07-15 15:02:20 -06001280 nvmeq->sq_tail = 0;
1281 nvmeq->cq_head = 0;
1282 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001283 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001284 memset(nvmeq->cmdid_data, 0, extra);
1285 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
1286 nvme_cancel_ios(nvmeq, false);
1287 nvmeq->q_suspended = 0;
1288}
1289
1290static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1291{
1292 struct nvme_dev *dev = nvmeq->dev;
1293 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001294
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001295 result = adapter_alloc_cq(dev, qid, nvmeq);
1296 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001297 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001298
1299 result = adapter_alloc_sq(dev, qid, nvmeq);
1300 if (result < 0)
1301 goto release_cq;
1302
Matthew Wilcox3193f072014-01-27 15:57:22 -05001303 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001304 if (result < 0)
1305 goto release_sq;
1306
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001307 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001308 nvme_init_queue(nvmeq, qid);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001309 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001310
1311 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001312
1313 release_sq:
1314 adapter_delete_sq(dev, qid);
1315 release_cq:
1316 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001317 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001318}
1319
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001320static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1321{
1322 unsigned long timeout;
1323 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1324
1325 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1326
1327 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1328 msleep(100);
1329 if (fatal_signal_pending(current))
1330 return -EINTR;
1331 if (time_after(jiffies, timeout)) {
1332 dev_err(&dev->pci_dev->dev,
1333 "Device not ready; aborting initialisation\n");
1334 return -ENODEV;
1335 }
1336 }
1337
1338 return 0;
1339}
1340
1341/*
1342 * If the device has been passed off to us in an enabled state, just clear
1343 * the enabled bit. The spec says we should set the 'shutdown notification
1344 * bits', but doing so may cause the device to complete commands to the
1345 * admin queue ... and we don't know what memory that might be pointing at!
1346 */
1347static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1348{
Matthew Wilcox44af1462013-05-04 06:43:17 -04001349 u32 cc = readl(&dev->bar->cc);
1350
1351 if (cc & NVME_CC_ENABLE)
1352 writel(cc & ~NVME_CC_ENABLE, &dev->bar->cc);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001353 return nvme_wait_ready(dev, cap, false);
1354}
1355
1356static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1357{
1358 return nvme_wait_ready(dev, cap, true);
1359}
1360
Keith Busch1894d8f2013-07-15 15:02:22 -06001361static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1362{
1363 unsigned long timeout;
1364 u32 cc;
1365
1366 cc = (readl(&dev->bar->cc) & ~NVME_CC_SHN_MASK) | NVME_CC_SHN_NORMAL;
1367 writel(cc, &dev->bar->cc);
1368
1369 timeout = 2 * HZ + jiffies;
1370 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1371 NVME_CSTS_SHST_CMPLT) {
1372 msleep(100);
1373 if (fatal_signal_pending(current))
1374 return -EINTR;
1375 if (time_after(jiffies, timeout)) {
1376 dev_err(&dev->pci_dev->dev,
1377 "Device shutdown incomplete; abort shutdown\n");
1378 return -ENODEV;
1379 }
1380 }
1381
1382 return 0;
1383}
1384
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001385static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001386{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001387 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001388 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001389 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001390 struct nvme_queue *nvmeq;
1391
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001392 result = nvme_disable_ctrl(dev, cap);
1393 if (result < 0)
1394 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001395
Keith Buschcd638942013-07-15 15:02:23 -06001396 nvmeq = dev->queues[0];
1397 if (!nvmeq) {
1398 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
1399 if (!nvmeq)
1400 return -ENOMEM;
1401 dev->queues[0] = nvmeq;
1402 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001403
1404 aqa = nvmeq->q_depth - 1;
1405 aqa |= aqa << 16;
1406
1407 dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
1408 dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
1409 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001410 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001411
1412 writel(aqa, &dev->bar->aqa);
1413 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1414 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
1415 writel(dev->ctrl_config, &dev->bar->cc);
1416
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001417 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001418 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001419 return result;
Matthew Wilcox9e866772012-08-03 13:55:56 -04001420
Matthew Wilcox3193f072014-01-27 15:57:22 -05001421 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Keith Busch025c5572013-05-01 13:07:51 -06001422 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001423 return result;
Keith Busch025c5572013-05-01 13:07:51 -06001424
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001425 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001426 nvme_init_queue(nvmeq, 0);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001427 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001428 return result;
1429}
1430
Vishal Verma5d0f6132013-03-04 18:40:58 -07001431struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001432 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001433{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001434 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001435 struct scatterlist *sg;
1436 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001437 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001438
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001439 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001440 return ERR_PTR(-EINVAL);
Dan Carpenter5460fc02013-05-13 17:59:50 +03001441 if (!length || length > INT_MAX - PAGE_SIZE)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001442 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001443
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001444 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001445 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1446 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001447 if (!pages)
1448 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001449
1450 err = get_user_pages_fast(addr, count, 1, pages);
1451 if (err < count) {
1452 count = err;
1453 err = -EFAULT;
1454 goto put_pages;
1455 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001456
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001457 iod = nvme_alloc_iod(count, length, GFP_KERNEL);
1458 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001459 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001460 for (i = 0; i < count; i++) {
1461 sg_set_page(&sg[i], pages[i],
Dan Carpenter5460fc02013-05-13 17:59:50 +03001462 min_t(unsigned, length, PAGE_SIZE - offset),
1463 offset);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001464 length -= (PAGE_SIZE - offset);
1465 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001466 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001467 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001468 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001469
1470 err = -ENOMEM;
1471 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1472 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001473 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001474 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001475
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001476 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001477 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001478
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001479 free_iod:
1480 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001481 put_pages:
1482 for (i = 0; i < count; i++)
1483 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001484 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001485 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001486}
1487
Vishal Verma5d0f6132013-03-04 18:40:58 -07001488void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001489 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001490{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001491 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001492
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001493 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1494 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001495
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001496 for (i = 0; i < iod->nents; i++)
1497 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001498}
1499
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001500static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1501{
1502 struct nvme_dev *dev = ns->dev;
1503 struct nvme_queue *nvmeq;
1504 struct nvme_user_io io;
1505 struct nvme_command c;
Keith Buschf410c682013-04-23 17:23:59 -06001506 unsigned length, meta_len;
1507 int status, i;
1508 struct nvme_iod *iod, *meta_iod = NULL;
1509 dma_addr_t meta_dma_addr;
1510 void *meta, *uninitialized_var(meta_mem);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001511
1512 if (copy_from_user(&io, uio, sizeof(io)))
1513 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001514 length = (io.nblocks + 1) << ns->lba_shift;
Keith Buschf410c682013-04-23 17:23:59 -06001515 meta_len = (io.nblocks + 1) * ns->ms;
1516
1517 if (meta_len && ((io.metadata & 3) || !io.metadata))
1518 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001519
1520 switch (io.opcode) {
1521 case nvme_cmd_write:
1522 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001523 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001524 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001525 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001526 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001527 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001528 }
1529
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001530 if (IS_ERR(iod))
1531 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001532
1533 memset(&c, 0, sizeof(c));
1534 c.rw.opcode = io.opcode;
1535 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001536 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001537 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001538 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001539 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001540 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1541 c.rw.reftag = cpu_to_le32(io.reftag);
1542 c.rw.apptag = cpu_to_le16(io.apptag);
1543 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buschf410c682013-04-23 17:23:59 -06001544
1545 if (meta_len) {
Keith Busch1b567492013-07-18 12:13:51 -06001546 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
1547 meta_len);
Keith Buschf410c682013-04-23 17:23:59 -06001548 if (IS_ERR(meta_iod)) {
1549 status = PTR_ERR(meta_iod);
1550 meta_iod = NULL;
1551 goto unmap;
1552 }
1553
1554 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1555 &meta_dma_addr, GFP_KERNEL);
1556 if (!meta_mem) {
1557 status = -ENOMEM;
1558 goto unmap;
1559 }
1560
1561 if (io.opcode & 1) {
1562 int meta_offset = 0;
1563
1564 for (i = 0; i < meta_iod->nents; i++) {
1565 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1566 meta_iod->sg[i].offset;
1567 memcpy(meta_mem + meta_offset, meta,
1568 meta_iod->sg[i].length);
1569 kunmap_atomic(meta);
1570 meta_offset += meta_iod->sg[i].length;
1571 }
1572 }
1573
1574 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1575 }
1576
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001577 length = nvme_setup_prps(dev, &c.common, iod, length, GFP_KERNEL);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001578
Matthew Wilcox040a93b2011-12-20 11:04:12 -05001579 nvmeq = get_nvmeq(dev);
Matthew Wilcoxfa922822011-03-16 16:29:00 -04001580 /*
1581 * Since nvme_submit_sync_cmd sleeps, we can't keep preemption
Matthew Wilcoxb1ad37e2011-02-04 16:14:30 -05001582 * disabled. We may be preempted at any point, and be rescheduled
1583 * to a different CPU. That will cause cacheline bouncing, but no
1584 * additional races since q_lock already protects against other CPUs.
1585 */
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001586 put_nvmeq(nvmeq);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001587 if (length != (io.nblocks + 1) << ns->lba_shift)
1588 status = -ENOMEM;
Keith Busch22404272013-07-15 15:02:20 -06001589 else if (!nvmeq || nvmeq->q_suspended)
1590 status = -EBUSY;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001591 else
Matthew Wilcoxff976d72011-12-20 13:53:01 -05001592 status = nvme_submit_sync_cmd(nvmeq, &c, NULL, NVME_IO_TIMEOUT);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001593
Keith Buschf410c682013-04-23 17:23:59 -06001594 if (meta_len) {
1595 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1596 int meta_offset = 0;
1597
1598 for (i = 0; i < meta_iod->nents; i++) {
1599 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1600 meta_iod->sg[i].offset;
1601 memcpy(meta, meta_mem + meta_offset,
1602 meta_iod->sg[i].length);
1603 kunmap_atomic(meta);
1604 meta_offset += meta_iod->sg[i].length;
1605 }
1606 }
1607
1608 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1609 meta_dma_addr);
1610 }
1611
1612 unmap:
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001613 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001614 nvme_free_iod(dev, iod);
Keith Buschf410c682013-04-23 17:23:59 -06001615
1616 if (meta_iod) {
1617 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1618 nvme_free_iod(dev, meta_iod);
1619 }
1620
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001621 return status;
1622}
1623
Keith Busch50af8ba2012-07-25 16:07:55 -06001624static int nvme_user_admin_cmd(struct nvme_dev *dev,
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001625 struct nvme_admin_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001626{
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001627 struct nvme_admin_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001628 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001629 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001630 struct nvme_iod *uninitialized_var(iod);
Keith Busch94f370c2013-05-09 14:01:38 -06001631 unsigned timeout;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001632
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001633 if (!capable(CAP_SYS_ADMIN))
1634 return -EACCES;
1635 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001636 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001637
1638 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001639 c.common.opcode = cmd.opcode;
1640 c.common.flags = cmd.flags;
1641 c.common.nsid = cpu_to_le32(cmd.nsid);
1642 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1643 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1644 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1645 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1646 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1647 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1648 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1649 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1650
1651 length = cmd.data_len;
1652 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001653 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1654 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001655 if (IS_ERR(iod))
1656 return PTR_ERR(iod);
1657 length = nvme_setup_prps(dev, &c.common, iod, length,
1658 GFP_KERNEL);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001659 }
1660
Keith Busch94f370c2013-05-09 14:01:38 -06001661 timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
1662 ADMIN_TIMEOUT;
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001663 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001664 status = -ENOMEM;
1665 else
Keith Busch94f370c2013-05-09 14:01:38 -06001666 status = nvme_submit_sync_cmd(dev->queues[0], &c, &cmd.result,
1667 timeout);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001668
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001669 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001670 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001671 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001672 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001673
Chayan Biswascf90bc42013-05-22 22:34:49 +00001674 if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
Keith Buschf4f117f2012-09-21 10:49:05 -06001675 sizeof(cmd.result)))
1676 status = -EFAULT;
1677
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001678 return status;
1679}
1680
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001681static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1682 unsigned long arg)
1683{
1684 struct nvme_ns *ns = bdev->bd_disk->private_data;
1685
1686 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001687 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001688 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001689 return ns->ns_id;
1690 case NVME_IOCTL_ADMIN_CMD:
Keith Busch50af8ba2012-07-25 16:07:55 -06001691 return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001692 case NVME_IOCTL_SUBMIT_IO:
1693 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001694 case SG_GET_VERSION_NUM:
1695 return nvme_sg_get_version_num((void __user *)arg);
1696 case SG_IO:
1697 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001698 default:
1699 return -ENOTTY;
1700 }
1701}
1702
Keith Busch320a3822013-10-23 13:07:34 -06001703#ifdef CONFIG_COMPAT
1704static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1705 unsigned int cmd, unsigned long arg)
1706{
1707 struct nvme_ns *ns = bdev->bd_disk->private_data;
1708
1709 switch (cmd) {
1710 case SG_IO:
1711 return nvme_sg_io32(ns, arg);
1712 }
1713 return nvme_ioctl(bdev, mode, cmd, arg);
1714}
1715#else
1716#define nvme_compat_ioctl NULL
1717#endif
1718
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001719static const struct block_device_operations nvme_fops = {
1720 .owner = THIS_MODULE,
1721 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06001722 .compat_ioctl = nvme_compat_ioctl,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001723};
1724
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001725static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1726{
1727 while (bio_list_peek(&nvmeq->sq_cong)) {
1728 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1729 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Keith Busch427e9702013-04-09 11:59:32 -06001730
Matthew Wilcox3cb967c2011-03-16 16:45:49 -04001731 if (bio_list_empty(&nvmeq->sq_cong))
1732 remove_wait_queue(&nvmeq->sq_full,
1733 &nvmeq->sq_cong_wait);
Keith Busch427e9702013-04-09 11:59:32 -06001734 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
1735 if (bio_list_empty(&nvmeq->sq_cong))
1736 add_wait_queue(&nvmeq->sq_full,
1737 &nvmeq->sq_cong_wait);
1738 bio_list_add_head(&nvmeq->sq_cong, bio);
1739 break;
1740 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001741 }
1742}
1743
1744static int nvme_kthread(void *data)
1745{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001746 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001747
1748 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001749 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001750 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001751 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001752 int i;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001753 if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
1754 dev->initialized) {
1755 if (work_busy(&dev->reset_work))
1756 continue;
1757 list_del_init(&dev->node);
1758 dev_warn(&dev->pci_dev->dev,
1759 "Failed status, reset controller\n");
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05001760 PREPARE_WORK(&dev->reset_work,
Keith Buschd4b4ff82013-12-10 13:10:37 -07001761 nvme_reset_failed_dev);
1762 queue_work(nvme_workq, &dev->reset_work);
1763 continue;
1764 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001765 for (i = 0; i < dev->queue_count; i++) {
1766 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001767 if (!nvmeq)
1768 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001769 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001770 if (nvmeq->q_suspended)
1771 goto unlock;
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001772 nvme_process_cq(nvmeq);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001773 nvme_cancel_ios(nvmeq, true);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001774 nvme_resubmit_bios(nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001775 unlock:
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001776 spin_unlock_irq(&nvmeq->q_lock);
1777 }
1778 }
1779 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001780 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001781 }
1782 return 0;
1783}
1784
Keith Busch0e5e4f02012-11-09 16:33:05 -07001785static void nvme_config_discard(struct nvme_ns *ns)
1786{
1787 u32 logical_block_size = queue_logical_block_size(ns->queue);
1788 ns->queue->limits.discard_zeroes_data = 0;
1789 ns->queue->limits.discard_alignment = logical_block_size;
1790 ns->queue->limits.discard_granularity = logical_block_size;
1791 ns->queue->limits.max_discard_sectors = 0xffffffff;
1792 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1793}
1794
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001795static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001796 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1797{
1798 struct nvme_ns *ns;
1799 struct gendisk *disk;
1800 int lbaf;
1801
1802 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1803 return NULL;
1804
1805 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1806 if (!ns)
1807 return NULL;
1808 ns->queue = blk_alloc_queue(GFP_KERNEL);
1809 if (!ns->queue)
1810 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001811 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
1812 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1813 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001814 blk_queue_make_request(ns->queue, nvme_make_request);
1815 ns->dev = dev;
1816 ns->queue->queuedata = ns;
1817
Matthew Wilcox469071a2013-12-09 12:58:46 -05001818 disk = alloc_disk(0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001819 if (!disk)
1820 goto out_free_queue;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001821 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001822 ns->disk = disk;
1823 lbaf = id->flbas & 0xf;
1824 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Buschf410c682013-04-23 17:23:59 -06001825 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busche9ef4632012-07-24 15:01:04 -06001826 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06001827 if (dev->max_hw_sectors)
1828 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001829
1830 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001831 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001832 disk->fops = &nvme_fops;
1833 disk->private_data = ns;
1834 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05001835 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001836 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001837 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001838 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1839
Keith Busch0e5e4f02012-11-09 16:33:05 -07001840 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1841 nvme_config_discard(ns);
1842
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001843 return ns;
1844
1845 out_free_queue:
1846 blk_cleanup_queue(ns->queue);
1847 out_free_ns:
1848 kfree(ns);
1849 return NULL;
1850}
1851
1852static void nvme_ns_free(struct nvme_ns *ns)
1853{
1854 put_disk(ns->disk);
1855 blk_cleanup_queue(ns->queue);
1856 kfree(ns);
1857}
1858
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001859static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001860{
1861 int status;
1862 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001863 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001864
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001865 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001866 &result);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001867 if (status)
Keith Busch7e03b122013-07-29 16:20:56 -06001868 return status < 0 ? -EIO : -EBUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001869 return min(result & 0xffff, result >> 16) + 1;
1870}
1871
Keith Busch9d713c22013-07-15 15:02:24 -06001872static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1873{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001874 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06001875}
1876
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001877static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001878{
Matthew Wilcox3193f072014-01-27 15:57:22 -05001879 struct nvme_queue *adminq = dev->queues[0];
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001880 struct pci_dev *pdev = dev->pci_dev;
Keith Busch9d713c22013-07-15 15:02:24 -06001881 int result, cpu, i, vecs, nr_io_queues, size, q_depth;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001882
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001883 nr_io_queues = num_online_cpus();
1884 result = set_queue_count(dev, nr_io_queues);
Matthew Wilcox1b234842011-01-20 13:01:49 -05001885 if (result < 0)
1886 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001887 if (result < nr_io_queues)
1888 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001889
Keith Busch9d713c22013-07-15 15:02:24 -06001890 size = db_bar_size(dev, nr_io_queues);
1891 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001892 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06001893 do {
1894 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1895 if (dev->bar)
1896 break;
1897 if (!--nr_io_queues)
1898 return -ENOMEM;
1899 size = db_bar_size(dev, nr_io_queues);
1900 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001901 dev->dbs = ((void __iomem *)dev->bar) + 4096;
1902 dev->queues[0]->q_db = dev->dbs;
1903 }
1904
Keith Busch9d713c22013-07-15 15:02:24 -06001905 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05001906 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06001907
Matthew Wilcox063a8092013-06-20 10:53:48 -04001908 vecs = nr_io_queues;
1909 for (i = 0; i < vecs; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001910 dev->entry[i].entry = i;
1911 for (;;) {
Matthew Wilcox063a8092013-06-20 10:53:48 -04001912 result = pci_enable_msix(pdev, dev->entry, vecs);
1913 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001914 break;
Matthew Wilcox063a8092013-06-20 10:53:48 -04001915 vecs = result;
1916 }
1917
1918 if (result < 0) {
1919 vecs = nr_io_queues;
1920 if (vecs > 32)
1921 vecs = 32;
1922 for (;;) {
1923 result = pci_enable_msi_block(pdev, vecs);
1924 if (result == 0) {
1925 for (i = 0; i < vecs; i++)
1926 dev->entry[i].vector = i + pdev->irq;
1927 break;
1928 } else if (result < 0) {
1929 vecs = 1;
1930 break;
1931 }
1932 vecs = result;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001933 }
1934 }
1935
Matthew Wilcox063a8092013-06-20 10:53:48 -04001936 /*
1937 * Should investigate if there's a performance win from allocating
1938 * more queues than interrupt vectors; it might allow the submission
1939 * path to scale better, even if the receive path is limited by the
1940 * number of interrupts.
1941 */
1942 nr_io_queues = vecs;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001943
Matthew Wilcox3193f072014-01-27 15:57:22 -05001944 result = queue_request_irq(dev, adminq, adminq->irqname);
Keith Busch9d713c22013-07-15 15:02:24 -06001945 if (result) {
Matthew Wilcox3193f072014-01-27 15:57:22 -05001946 adminq->q_suspended = 1;
Keith Busch22404272013-07-15 15:02:20 -06001947 goto free_queues;
Keith Busch9d713c22013-07-15 15:02:24 -06001948 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001949
Keith Buschcd638942013-07-15 15:02:23 -06001950 /* Free previously allocated queues that are no longer usable */
1951 spin_lock(&dev_list_lock);
1952 for (i = dev->queue_count - 1; i > nr_io_queues; i--) {
1953 struct nvme_queue *nvmeq = dev->queues[i];
1954
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001955 spin_lock_irq(&nvmeq->q_lock);
Keith Buschcd638942013-07-15 15:02:23 -06001956 nvme_cancel_ios(nvmeq, false);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001957 spin_unlock_irq(&nvmeq->q_lock);
Keith Buschcd638942013-07-15 15:02:23 -06001958
1959 nvme_free_queue(nvmeq);
1960 dev->queue_count--;
1961 dev->queues[i] = NULL;
1962 }
1963 spin_unlock(&dev_list_lock);
1964
Matthew Wilcox1b234842011-01-20 13:01:49 -05001965 cpu = cpumask_first(cpu_online_mask);
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001966 for (i = 0; i < nr_io_queues; i++) {
Matthew Wilcox1b234842011-01-20 13:01:49 -05001967 irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu));
1968 cpu = cpumask_next(cpu, cpu_online_mask);
1969 }
1970
Keith Buscha0cadb82012-07-27 13:57:23 -04001971 q_depth = min_t(int, NVME_CAP_MQES(readq(&dev->bar->cap)) + 1,
1972 NVME_Q_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001973 for (i = dev->queue_count - 1; i < nr_io_queues; i++) {
Keith Busch22404272013-07-15 15:02:20 -06001974 dev->queues[i + 1] = nvme_alloc_queue(dev, i + 1, q_depth, i);
1975 if (!dev->queues[i + 1]) {
1976 result = -ENOMEM;
1977 goto free_queues;
1978 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001979 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001980
Matthew Wilcox9ecdc942011-03-16 16:52:19 -04001981 for (; i < num_possible_cpus(); i++) {
1982 int target = i % rounddown_pow_of_two(dev->queue_count - 1);
1983 dev->queues[i + 1] = dev->queues[target + 1];
1984 }
1985
Keith Busch22404272013-07-15 15:02:20 -06001986 for (i = 1; i < dev->queue_count; i++) {
1987 result = nvme_create_queue(dev->queues[i], i);
1988 if (result) {
1989 for (--i; i > 0; i--)
1990 nvme_disable_queue(dev, i);
1991 goto free_queues;
1992 }
1993 }
1994
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001995 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001996
Keith Busch22404272013-07-15 15:02:20 -06001997 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05001998 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06001999 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002000}
2001
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002002/*
2003 * Return: error value if an error occurred setting up the queues or calling
2004 * Identify Device. 0 if these succeeded, even if adding some of the
2005 * namespaces failed. At the moment, these failures are silent. TBD which
2006 * failures should be reported.
2007 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002008static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002009{
Matthew Wilcox68608c262013-06-21 14:36:34 -04002010 struct pci_dev *pdev = dev->pci_dev;
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002011 int res;
2012 unsigned nn, i;
Keith Buschcbb62182013-05-01 13:07:49 -06002013 struct nvme_ns *ns;
Matthew Wilcox51814232011-02-01 16:18:08 -05002014 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002015 struct nvme_id_ns *id_ns;
2016 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002017 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06002018 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002019
Matthew Wilcox68608c262013-06-21 14:36:34 -04002020 mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06002021 if (!mem)
2022 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002023
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002024 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002025 if (res) {
2026 res = -EIO;
Keith Buschcbb62182013-05-01 13:07:49 -06002027 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002028 }
2029
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002030 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05002031 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002032 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002033 dev->abort_limit = ctrl->acl + 1;
Matthew Wilcox51814232011-02-01 16:18:08 -05002034 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2035 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2036 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002037 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002038 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002039 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
2040 (pdev->device == 0x0953) && ctrl->vs[3])
Keith Busch159b67d2013-04-09 17:13:20 -06002041 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002042
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002043 id_ns = mem;
Matthew Wilcox2b2c1892011-10-07 13:10:13 -04002044 for (i = 1; i <= nn; i++) {
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002045 res = nvme_identify(dev, i, 0, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002046 if (res)
2047 continue;
2048
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002049 if (id_ns->ncap == 0)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002050 continue;
2051
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002052 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
Keith Busch08df1e02012-09-21 10:52:13 -06002053 dma_addr + 4096, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002054 if (res)
Keith Busch12209032013-01-31 14:40:38 -07002055 memset(mem + 4096, 0, 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002056
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002057 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002058 if (ns)
2059 list_add_tail(&ns->list, &dev->namespaces);
2060 }
2061 list_for_each_entry(ns, &dev->namespaces, list)
2062 add_disk(ns->disk);
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002063 res = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002064
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002065 out:
Matthew Wilcox684f5c22011-09-19 17:14:53 -04002066 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002067 return res;
2068}
2069
Keith Busch0877cb02013-07-15 15:02:19 -06002070static int nvme_dev_map(struct nvme_dev *dev)
2071{
2072 int bars, result = -ENOMEM;
2073 struct pci_dev *pdev = dev->pci_dev;
2074
2075 if (pci_enable_device_mem(pdev))
2076 return result;
2077
2078 dev->entry[0].vector = pdev->irq;
2079 pci_set_master(pdev);
2080 bars = pci_select_bars(pdev, IORESOURCE_MEM);
2081 if (pci_request_selected_regions(pdev, bars, "nvme"))
2082 goto disable_pci;
2083
Russell King052d0ef2013-06-26 23:49:11 +01002084 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
2085 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2086 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002087
Keith Busch0877cb02013-07-15 15:02:19 -06002088 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2089 if (!dev->bar)
2090 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07002091 if (readl(&dev->bar->csts) == -1) {
2092 result = -ENODEV;
2093 goto unmap;
2094 }
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002095 dev->db_stride = 1 << NVME_CAP_STRIDE(readq(&dev->bar->cap));
Keith Busch0877cb02013-07-15 15:02:19 -06002096 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2097
2098 return 0;
2099
Keith Busch0e53d182013-12-10 13:10:39 -07002100 unmap:
2101 iounmap(dev->bar);
2102 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002103 disable:
2104 pci_release_regions(pdev);
2105 disable_pci:
2106 pci_disable_device(pdev);
2107 return result;
2108}
2109
2110static void nvme_dev_unmap(struct nvme_dev *dev)
2111{
2112 if (dev->pci_dev->msi_enabled)
2113 pci_disable_msi(dev->pci_dev);
2114 else if (dev->pci_dev->msix_enabled)
2115 pci_disable_msix(dev->pci_dev);
2116
2117 if (dev->bar) {
2118 iounmap(dev->bar);
2119 dev->bar = NULL;
Keith Busch9a6b9452013-12-10 13:10:36 -07002120 pci_release_regions(dev->pci_dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002121 }
2122
Keith Busch0877cb02013-07-15 15:02:19 -06002123 if (pci_is_enabled(dev->pci_dev))
2124 pci_disable_device(dev->pci_dev);
2125}
2126
Keith Busch4d115422013-12-10 13:10:40 -07002127struct nvme_delq_ctx {
2128 struct task_struct *waiter;
2129 struct kthread_worker *worker;
2130 atomic_t refcount;
2131};
2132
2133static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2134{
2135 dq->waiter = current;
2136 mb();
2137
2138 for (;;) {
2139 set_current_state(TASK_KILLABLE);
2140 if (!atomic_read(&dq->refcount))
2141 break;
2142 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2143 fatal_signal_pending(current)) {
2144 set_current_state(TASK_RUNNING);
2145
2146 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
2147 nvme_disable_queue(dev, 0);
2148
2149 send_sig(SIGKILL, dq->worker->task, 1);
2150 flush_kthread_worker(dq->worker);
2151 return;
2152 }
2153 }
2154 set_current_state(TASK_RUNNING);
2155}
2156
2157static void nvme_put_dq(struct nvme_delq_ctx *dq)
2158{
2159 atomic_dec(&dq->refcount);
2160 if (dq->waiter)
2161 wake_up_process(dq->waiter);
2162}
2163
2164static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2165{
2166 atomic_inc(&dq->refcount);
2167 return dq;
2168}
2169
2170static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2171{
2172 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
2173
2174 nvme_clear_queue(nvmeq);
2175 nvme_put_dq(dq);
2176}
2177
2178static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2179 kthread_work_func_t fn)
2180{
2181 struct nvme_command c;
2182
2183 memset(&c, 0, sizeof(c));
2184 c.delete_queue.opcode = opcode;
2185 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2186
2187 init_kthread_work(&nvmeq->cmdinfo.work, fn);
2188 return nvme_submit_admin_cmd_async(nvmeq->dev, &c, &nvmeq->cmdinfo);
2189}
2190
2191static void nvme_del_cq_work_handler(struct kthread_work *work)
2192{
2193 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2194 cmdinfo.work);
2195 nvme_del_queue_end(nvmeq);
2196}
2197
2198static int nvme_delete_cq(struct nvme_queue *nvmeq)
2199{
2200 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2201 nvme_del_cq_work_handler);
2202}
2203
2204static void nvme_del_sq_work_handler(struct kthread_work *work)
2205{
2206 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2207 cmdinfo.work);
2208 int status = nvmeq->cmdinfo.status;
2209
2210 if (!status)
2211 status = nvme_delete_cq(nvmeq);
2212 if (status)
2213 nvme_del_queue_end(nvmeq);
2214}
2215
2216static int nvme_delete_sq(struct nvme_queue *nvmeq)
2217{
2218 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2219 nvme_del_sq_work_handler);
2220}
2221
2222static void nvme_del_queue_start(struct kthread_work *work)
2223{
2224 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2225 cmdinfo.work);
2226 allow_signal(SIGKILL);
2227 if (nvme_delete_sq(nvmeq))
2228 nvme_del_queue_end(nvmeq);
2229}
2230
2231static void nvme_disable_io_queues(struct nvme_dev *dev)
2232{
2233 int i;
2234 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2235 struct nvme_delq_ctx dq;
2236 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2237 &worker, "nvme%d", dev->instance);
2238
2239 if (IS_ERR(kworker_task)) {
2240 dev_err(&dev->pci_dev->dev,
2241 "Failed to create queue del task\n");
2242 for (i = dev->queue_count - 1; i > 0; i--)
2243 nvme_disable_queue(dev, i);
2244 return;
2245 }
2246
2247 dq.waiter = NULL;
2248 atomic_set(&dq.refcount, 0);
2249 dq.worker = &worker;
2250 for (i = dev->queue_count - 1; i > 0; i--) {
2251 struct nvme_queue *nvmeq = dev->queues[i];
2252
2253 if (nvme_suspend_queue(nvmeq))
2254 continue;
2255 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2256 nvmeq->cmdinfo.worker = dq.worker;
2257 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2258 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2259 }
2260 nvme_wait_dq(&dq, dev);
2261 kthread_stop(kworker_task);
2262}
2263
Keith Buschf0b50732013-07-15 15:02:21 -06002264static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002265{
Keith Busch22404272013-07-15 15:02:20 -06002266 int i;
2267
Keith Buschd4b4ff82013-12-10 13:10:37 -07002268 dev->initialized = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002269
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002270 spin_lock(&dev_list_lock);
Keith Buschf0b50732013-07-15 15:02:21 -06002271 list_del_init(&dev->node);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002272 spin_unlock(&dev_list_lock);
2273
Keith Busch4d115422013-12-10 13:10:40 -07002274 if (!dev->bar || (dev->bar && readl(&dev->bar->csts) == -1)) {
2275 for (i = dev->queue_count - 1; i >= 0; i--) {
2276 struct nvme_queue *nvmeq = dev->queues[i];
2277 nvme_suspend_queue(nvmeq);
2278 nvme_clear_queue(nvmeq);
2279 }
2280 } else {
2281 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002282 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002283 nvme_disable_queue(dev, 0);
2284 }
Keith Buschf0b50732013-07-15 15:02:21 -06002285 nvme_dev_unmap(dev);
2286}
2287
2288static void nvme_dev_remove(struct nvme_dev *dev)
2289{
2290 struct nvme_ns *ns, *next;
2291
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002292 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2293 list_del(&ns->list);
2294 del_gendisk(ns->disk);
2295 nvme_ns_free(ns);
2296 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002297}
2298
Matthew Wilcox091b6092011-02-10 09:56:01 -05002299static int nvme_setup_prp_pools(struct nvme_dev *dev)
2300{
2301 struct device *dmadev = &dev->pci_dev->dev;
2302 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2303 PAGE_SIZE, PAGE_SIZE, 0);
2304 if (!dev->prp_page_pool)
2305 return -ENOMEM;
2306
Matthew Wilcox99802a72011-02-10 10:30:34 -05002307 /* Optimisation for I/Os between 4k and 128k */
2308 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2309 256, 256, 0);
2310 if (!dev->prp_small_pool) {
2311 dma_pool_destroy(dev->prp_page_pool);
2312 return -ENOMEM;
2313 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002314 return 0;
2315}
2316
2317static void nvme_release_prp_pools(struct nvme_dev *dev)
2318{
2319 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002320 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002321}
2322
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002323static DEFINE_IDA(nvme_instance_ida);
2324
2325static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002326{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002327 int instance, error;
2328
2329 do {
2330 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2331 return -ENODEV;
2332
2333 spin_lock(&dev_list_lock);
2334 error = ida_get_new(&nvme_instance_ida, &instance);
2335 spin_unlock(&dev_list_lock);
2336 } while (error == -EAGAIN);
2337
2338 if (error)
2339 return -ENODEV;
2340
2341 dev->instance = instance;
2342 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002343}
2344
2345static void nvme_release_instance(struct nvme_dev *dev)
2346{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002347 spin_lock(&dev_list_lock);
2348 ida_remove(&nvme_instance_ida, dev->instance);
2349 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002350}
2351
Keith Busch5e82e952013-02-19 10:17:58 -07002352static void nvme_free_dev(struct kref *kref)
2353{
2354 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch5e82e952013-02-19 10:17:58 -07002355 kfree(dev->queues);
2356 kfree(dev->entry);
2357 kfree(dev);
2358}
2359
2360static int nvme_dev_open(struct inode *inode, struct file *f)
2361{
2362 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
2363 miscdev);
2364 kref_get(&dev->kref);
2365 f->private_data = dev;
2366 return 0;
2367}
2368
2369static int nvme_dev_release(struct inode *inode, struct file *f)
2370{
2371 struct nvme_dev *dev = f->private_data;
2372 kref_put(&dev->kref, nvme_free_dev);
2373 return 0;
2374}
2375
2376static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2377{
2378 struct nvme_dev *dev = f->private_data;
2379 switch (cmd) {
2380 case NVME_IOCTL_ADMIN_CMD:
2381 return nvme_user_admin_cmd(dev, (void __user *)arg);
2382 default:
2383 return -ENOTTY;
2384 }
2385}
2386
2387static const struct file_operations nvme_dev_fops = {
2388 .owner = THIS_MODULE,
2389 .open = nvme_dev_open,
2390 .release = nvme_dev_release,
2391 .unlocked_ioctl = nvme_dev_ioctl,
2392 .compat_ioctl = nvme_dev_ioctl,
2393};
2394
Keith Buschf0b50732013-07-15 15:02:21 -06002395static int nvme_dev_start(struct nvme_dev *dev)
2396{
2397 int result;
2398
2399 result = nvme_dev_map(dev);
2400 if (result)
2401 return result;
2402
2403 result = nvme_configure_admin_queue(dev);
2404 if (result)
2405 goto unmap;
2406
2407 spin_lock(&dev_list_lock);
2408 list_add(&dev->node, &dev_list);
2409 spin_unlock(&dev_list_lock);
2410
2411 result = nvme_setup_io_queues(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002412 if (result && result != -EBUSY)
Keith Buschf0b50732013-07-15 15:02:21 -06002413 goto disable;
2414
Keith Buschd82e8bf2013-09-05 14:45:07 -06002415 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002416
2417 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002418 nvme_disable_queue(dev, 0);
Keith Buschf0b50732013-07-15 15:02:21 -06002419 spin_lock(&dev_list_lock);
2420 list_del_init(&dev->node);
2421 spin_unlock(&dev_list_lock);
2422 unmap:
2423 nvme_dev_unmap(dev);
2424 return result;
2425}
2426
Keith Busch9a6b9452013-12-10 13:10:36 -07002427static int nvme_remove_dead_ctrl(void *arg)
2428{
2429 struct nvme_dev *dev = (struct nvme_dev *)arg;
2430 struct pci_dev *pdev = dev->pci_dev;
2431
2432 if (pci_get_drvdata(pdev))
2433 pci_stop_and_remove_bus_device(pdev);
2434 kref_put(&dev->kref, nvme_free_dev);
2435 return 0;
2436}
2437
2438static void nvme_remove_disks(struct work_struct *ws)
2439{
2440 int i;
2441 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2442
2443 nvme_dev_remove(dev);
2444 spin_lock(&dev_list_lock);
2445 for (i = dev->queue_count - 1; i > 0; i--) {
2446 BUG_ON(!dev->queues[i] || !dev->queues[i]->q_suspended);
2447 nvme_free_queue(dev->queues[i]);
2448 dev->queue_count--;
2449 dev->queues[i] = NULL;
2450 }
2451 spin_unlock(&dev_list_lock);
2452}
2453
2454static int nvme_dev_resume(struct nvme_dev *dev)
2455{
2456 int ret;
2457
2458 ret = nvme_dev_start(dev);
2459 if (ret && ret != -EBUSY)
2460 return ret;
2461 if (ret == -EBUSY) {
2462 spin_lock(&dev_list_lock);
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05002463 PREPARE_WORK(&dev->reset_work, nvme_remove_disks);
Keith Busch9a6b9452013-12-10 13:10:36 -07002464 queue_work(nvme_workq, &dev->reset_work);
2465 spin_unlock(&dev_list_lock);
2466 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002467 dev->initialized = 1;
Keith Busch9a6b9452013-12-10 13:10:36 -07002468 return 0;
2469}
2470
2471static void nvme_dev_reset(struct nvme_dev *dev)
2472{
2473 nvme_dev_shutdown(dev);
2474 if (nvme_dev_resume(dev)) {
2475 dev_err(&dev->pci_dev->dev, "Device failed to resume\n");
2476 kref_get(&dev->kref);
2477 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2478 dev->instance))) {
2479 dev_err(&dev->pci_dev->dev,
2480 "Failed to start controller remove task\n");
2481 kref_put(&dev->kref, nvme_free_dev);
2482 }
2483 }
2484}
2485
2486static void nvme_reset_failed_dev(struct work_struct *ws)
2487{
2488 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2489 nvme_dev_reset(dev);
2490}
2491
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002492static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002493{
Keith Busch0877cb02013-07-15 15:02:19 -06002494 int result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002495 struct nvme_dev *dev;
2496
2497 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2498 if (!dev)
2499 return -ENOMEM;
2500 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
2501 GFP_KERNEL);
2502 if (!dev->entry)
2503 goto free;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002504 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
2505 GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002506 if (!dev->queues)
2507 goto free;
2508
2509 INIT_LIST_HEAD(&dev->namespaces);
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05002510 INIT_WORK(&dev->reset_work, nvme_reset_failed_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002511 dev->pci_dev = pdev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002512 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002513 result = nvme_set_instance(dev);
2514 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002515 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002516
Matthew Wilcox091b6092011-02-10 09:56:01 -05002517 result = nvme_setup_prp_pools(dev);
2518 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002519 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002520
Keith Buschf0b50732013-07-15 15:02:21 -06002521 result = nvme_dev_start(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002522 if (result) {
2523 if (result == -EBUSY)
2524 goto create_cdev;
Keith Busch0877cb02013-07-15 15:02:19 -06002525 goto release_pools;
Keith Buschd82e8bf2013-09-05 14:45:07 -06002526 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002527
Matthew Wilcox740216f2011-02-15 16:28:20 -05002528 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002529 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002530 goto shutdown;
Matthew Wilcox740216f2011-02-15 16:28:20 -05002531
Keith Buschd82e8bf2013-09-05 14:45:07 -06002532 create_cdev:
Keith Busch5e82e952013-02-19 10:17:58 -07002533 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
2534 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2535 dev->miscdev.parent = &pdev->dev;
2536 dev->miscdev.name = dev->name;
2537 dev->miscdev.fops = &nvme_dev_fops;
2538 result = misc_register(&dev->miscdev);
2539 if (result)
2540 goto remove;
2541
Keith Buschd4b4ff82013-12-10 13:10:37 -07002542 dev->initialized = 1;
Keith Busch5e82e952013-02-19 10:17:58 -07002543 kref_init(&dev->kref);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002544 return 0;
2545
Keith Busch5e82e952013-02-19 10:17:58 -07002546 remove:
2547 nvme_dev_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002548 shutdown:
2549 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002550 release_pools:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002551 nvme_free_queues(dev, 0);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002552 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002553 release:
2554 nvme_release_instance(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002555 free:
2556 kfree(dev->queues);
2557 kfree(dev->entry);
2558 kfree(dev);
2559 return result;
2560}
2561
Keith Busch09ece142014-01-27 11:29:40 -05002562static void nvme_shutdown(struct pci_dev *pdev)
2563{
2564 struct nvme_dev *dev = pci_get_drvdata(pdev);
2565 nvme_dev_shutdown(dev);
2566}
2567
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002568static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002569{
2570 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002571
2572 spin_lock(&dev_list_lock);
2573 list_del_init(&dev->node);
2574 spin_unlock(&dev_list_lock);
2575
2576 pci_set_drvdata(pdev, NULL);
2577 flush_work(&dev->reset_work);
Keith Busch5e82e952013-02-19 10:17:58 -07002578 misc_deregister(&dev->miscdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002579 nvme_dev_remove(dev);
2580 nvme_dev_shutdown(dev);
Keith Buscha1a5ef92013-12-16 13:50:00 -05002581 nvme_free_queues(dev, 0);
Keith Busch9a6b9452013-12-10 13:10:36 -07002582 nvme_release_instance(dev);
2583 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002584 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002585}
2586
2587/* These functions are yet to be implemented */
2588#define nvme_error_detected NULL
2589#define nvme_dump_registers NULL
2590#define nvme_link_reset NULL
2591#define nvme_slot_reset NULL
2592#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002593
2594static int nvme_suspend(struct device *dev)
2595{
2596 struct pci_dev *pdev = to_pci_dev(dev);
2597 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2598
2599 nvme_dev_shutdown(ndev);
2600 return 0;
2601}
2602
2603static int nvme_resume(struct device *dev)
2604{
2605 struct pci_dev *pdev = to_pci_dev(dev);
2606 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002607
Keith Busch9a6b9452013-12-10 13:10:36 -07002608 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05002609 PREPARE_WORK(&ndev->reset_work, nvme_reset_failed_dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002610 queue_work(nvme_workq, &ndev->reset_work);
2611 }
2612 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002613}
2614
2615static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002616
Stephen Hemminger1d352032012-09-07 09:33:17 -07002617static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002618 .error_detected = nvme_error_detected,
2619 .mmio_enabled = nvme_dump_registers,
2620 .link_reset = nvme_link_reset,
2621 .slot_reset = nvme_slot_reset,
2622 .resume = nvme_error_resume,
2623};
2624
2625/* Move to pci_ids.h later */
2626#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2627
2628static DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
2629 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2630 { 0, }
2631};
2632MODULE_DEVICE_TABLE(pci, nvme_id_table);
2633
2634static struct pci_driver nvme_driver = {
2635 .name = "nvme",
2636 .id_table = nvme_id_table,
2637 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002638 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002639 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002640 .driver = {
2641 .pm = &nvme_dev_pm_ops,
2642 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002643 .err_handler = &nvme_err_handler,
2644};
2645
2646static int __init nvme_init(void)
2647{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002648 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002649
2650 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2651 if (IS_ERR(nvme_thread))
2652 return PTR_ERR(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002653
Keith Busch9a6b9452013-12-10 13:10:36 -07002654 result = -ENOMEM;
2655 nvme_workq = create_singlethread_workqueue("nvme");
2656 if (!nvme_workq)
2657 goto kill_kthread;
2658
Keith Busch5c42ea12012-07-25 16:05:18 -06002659 result = register_blkdev(nvme_major, "nvme");
2660 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002661 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06002662 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002663 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002664
2665 result = pci_register_driver(&nvme_driver);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002666 if (result)
2667 goto unregister_blkdev;
2668 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002669
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002670 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002671 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002672 kill_workq:
2673 destroy_workqueue(nvme_workq);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002674 kill_kthread:
2675 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002676 return result;
2677}
2678
2679static void __exit nvme_exit(void)
2680{
2681 pci_unregister_driver(&nvme_driver);
2682 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002683 destroy_workqueue(nvme_workq);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002684 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002685}
2686
2687MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2688MODULE_LICENSE("GPL");
Matthew Wilcox366e8212012-01-10 16:30:15 -05002689MODULE_VERSION("0.8");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002690module_init(nvme_init);
2691module_exit(nvme_exit);