blob: cd39390710a058398b326c92c35959aa5353d830 [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
Keith Busch9ac27092014-01-31 16:53:39 -07001719static int nvme_open(struct block_device *bdev, fmode_t mode)
1720{
1721 struct nvme_ns *ns = bdev->bd_disk->private_data;
1722 struct nvme_dev *dev = ns->dev;
1723
1724 kref_get(&dev->kref);
1725 return 0;
1726}
1727
1728static void nvme_free_dev(struct kref *kref);
1729
1730static void nvme_release(struct gendisk *disk, fmode_t mode)
1731{
1732 struct nvme_ns *ns = disk->private_data;
1733 struct nvme_dev *dev = ns->dev;
1734
1735 kref_put(&dev->kref, nvme_free_dev);
1736}
1737
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001738static const struct block_device_operations nvme_fops = {
1739 .owner = THIS_MODULE,
1740 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06001741 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07001742 .open = nvme_open,
1743 .release = nvme_release,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001744};
1745
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001746static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1747{
1748 while (bio_list_peek(&nvmeq->sq_cong)) {
1749 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1750 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Keith Busch427e9702013-04-09 11:59:32 -06001751
Matthew Wilcox3cb967c2011-03-16 16:45:49 -04001752 if (bio_list_empty(&nvmeq->sq_cong))
1753 remove_wait_queue(&nvmeq->sq_full,
1754 &nvmeq->sq_cong_wait);
Keith Busch427e9702013-04-09 11:59:32 -06001755 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
1756 if (bio_list_empty(&nvmeq->sq_cong))
1757 add_wait_queue(&nvmeq->sq_full,
1758 &nvmeq->sq_cong_wait);
1759 bio_list_add_head(&nvmeq->sq_cong, bio);
1760 break;
1761 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001762 }
1763}
1764
1765static int nvme_kthread(void *data)
1766{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001767 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001768
1769 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001770 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001771 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001772 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001773 int i;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001774 if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
1775 dev->initialized) {
1776 if (work_busy(&dev->reset_work))
1777 continue;
1778 list_del_init(&dev->node);
1779 dev_warn(&dev->pci_dev->dev,
1780 "Failed status, reset controller\n");
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05001781 PREPARE_WORK(&dev->reset_work,
Keith Buschd4b4ff82013-12-10 13:10:37 -07001782 nvme_reset_failed_dev);
1783 queue_work(nvme_workq, &dev->reset_work);
1784 continue;
1785 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001786 for (i = 0; i < dev->queue_count; i++) {
1787 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001788 if (!nvmeq)
1789 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001790 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001791 if (nvmeq->q_suspended)
1792 goto unlock;
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001793 nvme_process_cq(nvmeq);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001794 nvme_cancel_ios(nvmeq, true);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001795 nvme_resubmit_bios(nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001796 unlock:
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001797 spin_unlock_irq(&nvmeq->q_lock);
1798 }
1799 }
1800 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001801 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001802 }
1803 return 0;
1804}
1805
Keith Busch0e5e4f02012-11-09 16:33:05 -07001806static void nvme_config_discard(struct nvme_ns *ns)
1807{
1808 u32 logical_block_size = queue_logical_block_size(ns->queue);
1809 ns->queue->limits.discard_zeroes_data = 0;
1810 ns->queue->limits.discard_alignment = logical_block_size;
1811 ns->queue->limits.discard_granularity = logical_block_size;
1812 ns->queue->limits.max_discard_sectors = 0xffffffff;
1813 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1814}
1815
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001816static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001817 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1818{
1819 struct nvme_ns *ns;
1820 struct gendisk *disk;
1821 int lbaf;
1822
1823 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1824 return NULL;
1825
1826 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1827 if (!ns)
1828 return NULL;
1829 ns->queue = blk_alloc_queue(GFP_KERNEL);
1830 if (!ns->queue)
1831 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001832 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
1833 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1834 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001835 blk_queue_make_request(ns->queue, nvme_make_request);
1836 ns->dev = dev;
1837 ns->queue->queuedata = ns;
1838
Matthew Wilcox469071a2013-12-09 12:58:46 -05001839 disk = alloc_disk(0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001840 if (!disk)
1841 goto out_free_queue;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001842 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001843 ns->disk = disk;
1844 lbaf = id->flbas & 0xf;
1845 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Buschf410c682013-04-23 17:23:59 -06001846 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busche9ef4632012-07-24 15:01:04 -06001847 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06001848 if (dev->max_hw_sectors)
1849 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001850
1851 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001852 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001853 disk->fops = &nvme_fops;
1854 disk->private_data = ns;
1855 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05001856 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001857 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001858 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001859 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1860
Keith Busch0e5e4f02012-11-09 16:33:05 -07001861 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1862 nvme_config_discard(ns);
1863
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001864 return ns;
1865
1866 out_free_queue:
1867 blk_cleanup_queue(ns->queue);
1868 out_free_ns:
1869 kfree(ns);
1870 return NULL;
1871}
1872
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001873static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001874{
1875 int status;
1876 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001877 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001878
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001879 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001880 &result);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001881 if (status)
Keith Busch7e03b122013-07-29 16:20:56 -06001882 return status < 0 ? -EIO : -EBUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001883 return min(result & 0xffff, result >> 16) + 1;
1884}
1885
Keith Busch9d713c22013-07-15 15:02:24 -06001886static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1887{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001888 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06001889}
1890
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001891static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001892{
Matthew Wilcox3193f072014-01-27 15:57:22 -05001893 struct nvme_queue *adminq = dev->queues[0];
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001894 struct pci_dev *pdev = dev->pci_dev;
Keith Busch9d713c22013-07-15 15:02:24 -06001895 int result, cpu, i, vecs, nr_io_queues, size, q_depth;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001896
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001897 nr_io_queues = num_online_cpus();
1898 result = set_queue_count(dev, nr_io_queues);
Matthew Wilcox1b234842011-01-20 13:01:49 -05001899 if (result < 0)
1900 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001901 if (result < nr_io_queues)
1902 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001903
Keith Busch9d713c22013-07-15 15:02:24 -06001904 size = db_bar_size(dev, nr_io_queues);
1905 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001906 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06001907 do {
1908 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1909 if (dev->bar)
1910 break;
1911 if (!--nr_io_queues)
1912 return -ENOMEM;
1913 size = db_bar_size(dev, nr_io_queues);
1914 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001915 dev->dbs = ((void __iomem *)dev->bar) + 4096;
1916 dev->queues[0]->q_db = dev->dbs;
1917 }
1918
Keith Busch9d713c22013-07-15 15:02:24 -06001919 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05001920 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06001921
Matthew Wilcox063a8092013-06-20 10:53:48 -04001922 vecs = nr_io_queues;
1923 for (i = 0; i < vecs; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001924 dev->entry[i].entry = i;
1925 for (;;) {
Matthew Wilcox063a8092013-06-20 10:53:48 -04001926 result = pci_enable_msix(pdev, dev->entry, vecs);
1927 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001928 break;
Matthew Wilcox063a8092013-06-20 10:53:48 -04001929 vecs = result;
1930 }
1931
1932 if (result < 0) {
1933 vecs = nr_io_queues;
1934 if (vecs > 32)
1935 vecs = 32;
1936 for (;;) {
1937 result = pci_enable_msi_block(pdev, vecs);
1938 if (result == 0) {
1939 for (i = 0; i < vecs; i++)
1940 dev->entry[i].vector = i + pdev->irq;
1941 break;
1942 } else if (result < 0) {
1943 vecs = 1;
1944 break;
1945 }
1946 vecs = result;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001947 }
1948 }
1949
Matthew Wilcox063a8092013-06-20 10:53:48 -04001950 /*
1951 * Should investigate if there's a performance win from allocating
1952 * more queues than interrupt vectors; it might allow the submission
1953 * path to scale better, even if the receive path is limited by the
1954 * number of interrupts.
1955 */
1956 nr_io_queues = vecs;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001957
Matthew Wilcox3193f072014-01-27 15:57:22 -05001958 result = queue_request_irq(dev, adminq, adminq->irqname);
Keith Busch9d713c22013-07-15 15:02:24 -06001959 if (result) {
Matthew Wilcox3193f072014-01-27 15:57:22 -05001960 adminq->q_suspended = 1;
Keith Busch22404272013-07-15 15:02:20 -06001961 goto free_queues;
Keith Busch9d713c22013-07-15 15:02:24 -06001962 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001963
Keith Buschcd638942013-07-15 15:02:23 -06001964 /* Free previously allocated queues that are no longer usable */
1965 spin_lock(&dev_list_lock);
1966 for (i = dev->queue_count - 1; i > nr_io_queues; i--) {
1967 struct nvme_queue *nvmeq = dev->queues[i];
1968
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001969 spin_lock_irq(&nvmeq->q_lock);
Keith Buschcd638942013-07-15 15:02:23 -06001970 nvme_cancel_ios(nvmeq, false);
Matthew Wilcox0a8d44c2013-10-15 15:01:10 -04001971 spin_unlock_irq(&nvmeq->q_lock);
Keith Buschcd638942013-07-15 15:02:23 -06001972
1973 nvme_free_queue(nvmeq);
1974 dev->queue_count--;
1975 dev->queues[i] = NULL;
1976 }
1977 spin_unlock(&dev_list_lock);
1978
Matthew Wilcox1b234842011-01-20 13:01:49 -05001979 cpu = cpumask_first(cpu_online_mask);
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001980 for (i = 0; i < nr_io_queues; i++) {
Matthew Wilcox1b234842011-01-20 13:01:49 -05001981 irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu));
1982 cpu = cpumask_next(cpu, cpu_online_mask);
1983 }
1984
Keith Buscha0cadb82012-07-27 13:57:23 -04001985 q_depth = min_t(int, NVME_CAP_MQES(readq(&dev->bar->cap)) + 1,
1986 NVME_Q_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001987 for (i = dev->queue_count - 1; i < nr_io_queues; i++) {
Keith Busch22404272013-07-15 15:02:20 -06001988 dev->queues[i + 1] = nvme_alloc_queue(dev, i + 1, q_depth, i);
1989 if (!dev->queues[i + 1]) {
1990 result = -ENOMEM;
1991 goto free_queues;
1992 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001993 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001994
Matthew Wilcox9ecdc942011-03-16 16:52:19 -04001995 for (; i < num_possible_cpus(); i++) {
1996 int target = i % rounddown_pow_of_two(dev->queue_count - 1);
1997 dev->queues[i + 1] = dev->queues[target + 1];
1998 }
1999
Keith Busch22404272013-07-15 15:02:20 -06002000 for (i = 1; i < dev->queue_count; i++) {
2001 result = nvme_create_queue(dev->queues[i], i);
2002 if (result) {
2003 for (--i; i > 0; i--)
2004 nvme_disable_queue(dev, i);
2005 goto free_queues;
2006 }
2007 }
2008
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002009 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002010
Keith Busch22404272013-07-15 15:02:20 -06002011 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002012 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002013 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002014}
2015
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002016/*
2017 * Return: error value if an error occurred setting up the queues or calling
2018 * Identify Device. 0 if these succeeded, even if adding some of the
2019 * namespaces failed. At the moment, these failures are silent. TBD which
2020 * failures should be reported.
2021 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002022static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002023{
Matthew Wilcox68608c262013-06-21 14:36:34 -04002024 struct pci_dev *pdev = dev->pci_dev;
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002025 int res;
2026 unsigned nn, i;
Keith Buschcbb62182013-05-01 13:07:49 -06002027 struct nvme_ns *ns;
Matthew Wilcox51814232011-02-01 16:18:08 -05002028 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002029 struct nvme_id_ns *id_ns;
2030 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002031 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06002032 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002033
Matthew Wilcox68608c262013-06-21 14:36:34 -04002034 mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06002035 if (!mem)
2036 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002037
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002038 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002039 if (res) {
2040 res = -EIO;
Keith Buschcbb62182013-05-01 13:07:49 -06002041 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002042 }
2043
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002044 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05002045 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002046 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002047 dev->abort_limit = ctrl->acl + 1;
Matthew Wilcox51814232011-02-01 16:18:08 -05002048 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2049 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2050 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002051 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002052 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002053 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
2054 (pdev->device == 0x0953) && ctrl->vs[3])
Keith Busch159b67d2013-04-09 17:13:20 -06002055 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002056
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002057 id_ns = mem;
Matthew Wilcox2b2c1892011-10-07 13:10:13 -04002058 for (i = 1; i <= nn; i++) {
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002059 res = nvme_identify(dev, i, 0, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002060 if (res)
2061 continue;
2062
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002063 if (id_ns->ncap == 0)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002064 continue;
2065
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002066 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
Keith Busch08df1e02012-09-21 10:52:13 -06002067 dma_addr + 4096, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002068 if (res)
Keith Busch12209032013-01-31 14:40:38 -07002069 memset(mem + 4096, 0, 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002070
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002071 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002072 if (ns)
2073 list_add_tail(&ns->list, &dev->namespaces);
2074 }
2075 list_for_each_entry(ns, &dev->namespaces, list)
2076 add_disk(ns->disk);
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002077 res = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002078
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002079 out:
Matthew Wilcox684f5c22011-09-19 17:14:53 -04002080 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002081 return res;
2082}
2083
Keith Busch0877cb02013-07-15 15:02:19 -06002084static int nvme_dev_map(struct nvme_dev *dev)
2085{
2086 int bars, result = -ENOMEM;
2087 struct pci_dev *pdev = dev->pci_dev;
2088
2089 if (pci_enable_device_mem(pdev))
2090 return result;
2091
2092 dev->entry[0].vector = pdev->irq;
2093 pci_set_master(pdev);
2094 bars = pci_select_bars(pdev, IORESOURCE_MEM);
2095 if (pci_request_selected_regions(pdev, bars, "nvme"))
2096 goto disable_pci;
2097
Russell King052d0ef2013-06-26 23:49:11 +01002098 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
2099 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2100 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002101
Keith Busch0877cb02013-07-15 15:02:19 -06002102 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2103 if (!dev->bar)
2104 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07002105 if (readl(&dev->bar->csts) == -1) {
2106 result = -ENODEV;
2107 goto unmap;
2108 }
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002109 dev->db_stride = 1 << NVME_CAP_STRIDE(readq(&dev->bar->cap));
Keith Busch0877cb02013-07-15 15:02:19 -06002110 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2111
2112 return 0;
2113
Keith Busch0e53d182013-12-10 13:10:39 -07002114 unmap:
2115 iounmap(dev->bar);
2116 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002117 disable:
2118 pci_release_regions(pdev);
2119 disable_pci:
2120 pci_disable_device(pdev);
2121 return result;
2122}
2123
2124static void nvme_dev_unmap(struct nvme_dev *dev)
2125{
2126 if (dev->pci_dev->msi_enabled)
2127 pci_disable_msi(dev->pci_dev);
2128 else if (dev->pci_dev->msix_enabled)
2129 pci_disable_msix(dev->pci_dev);
2130
2131 if (dev->bar) {
2132 iounmap(dev->bar);
2133 dev->bar = NULL;
Keith Busch9a6b9452013-12-10 13:10:36 -07002134 pci_release_regions(dev->pci_dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002135 }
2136
Keith Busch0877cb02013-07-15 15:02:19 -06002137 if (pci_is_enabled(dev->pci_dev))
2138 pci_disable_device(dev->pci_dev);
2139}
2140
Keith Busch4d115422013-12-10 13:10:40 -07002141struct nvme_delq_ctx {
2142 struct task_struct *waiter;
2143 struct kthread_worker *worker;
2144 atomic_t refcount;
2145};
2146
2147static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2148{
2149 dq->waiter = current;
2150 mb();
2151
2152 for (;;) {
2153 set_current_state(TASK_KILLABLE);
2154 if (!atomic_read(&dq->refcount))
2155 break;
2156 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2157 fatal_signal_pending(current)) {
2158 set_current_state(TASK_RUNNING);
2159
2160 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
2161 nvme_disable_queue(dev, 0);
2162
2163 send_sig(SIGKILL, dq->worker->task, 1);
2164 flush_kthread_worker(dq->worker);
2165 return;
2166 }
2167 }
2168 set_current_state(TASK_RUNNING);
2169}
2170
2171static void nvme_put_dq(struct nvme_delq_ctx *dq)
2172{
2173 atomic_dec(&dq->refcount);
2174 if (dq->waiter)
2175 wake_up_process(dq->waiter);
2176}
2177
2178static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2179{
2180 atomic_inc(&dq->refcount);
2181 return dq;
2182}
2183
2184static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2185{
2186 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
2187
2188 nvme_clear_queue(nvmeq);
2189 nvme_put_dq(dq);
2190}
2191
2192static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2193 kthread_work_func_t fn)
2194{
2195 struct nvme_command c;
2196
2197 memset(&c, 0, sizeof(c));
2198 c.delete_queue.opcode = opcode;
2199 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2200
2201 init_kthread_work(&nvmeq->cmdinfo.work, fn);
2202 return nvme_submit_admin_cmd_async(nvmeq->dev, &c, &nvmeq->cmdinfo);
2203}
2204
2205static void nvme_del_cq_work_handler(struct kthread_work *work)
2206{
2207 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2208 cmdinfo.work);
2209 nvme_del_queue_end(nvmeq);
2210}
2211
2212static int nvme_delete_cq(struct nvme_queue *nvmeq)
2213{
2214 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2215 nvme_del_cq_work_handler);
2216}
2217
2218static void nvme_del_sq_work_handler(struct kthread_work *work)
2219{
2220 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2221 cmdinfo.work);
2222 int status = nvmeq->cmdinfo.status;
2223
2224 if (!status)
2225 status = nvme_delete_cq(nvmeq);
2226 if (status)
2227 nvme_del_queue_end(nvmeq);
2228}
2229
2230static int nvme_delete_sq(struct nvme_queue *nvmeq)
2231{
2232 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2233 nvme_del_sq_work_handler);
2234}
2235
2236static void nvme_del_queue_start(struct kthread_work *work)
2237{
2238 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2239 cmdinfo.work);
2240 allow_signal(SIGKILL);
2241 if (nvme_delete_sq(nvmeq))
2242 nvme_del_queue_end(nvmeq);
2243}
2244
2245static void nvme_disable_io_queues(struct nvme_dev *dev)
2246{
2247 int i;
2248 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2249 struct nvme_delq_ctx dq;
2250 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2251 &worker, "nvme%d", dev->instance);
2252
2253 if (IS_ERR(kworker_task)) {
2254 dev_err(&dev->pci_dev->dev,
2255 "Failed to create queue del task\n");
2256 for (i = dev->queue_count - 1; i > 0; i--)
2257 nvme_disable_queue(dev, i);
2258 return;
2259 }
2260
2261 dq.waiter = NULL;
2262 atomic_set(&dq.refcount, 0);
2263 dq.worker = &worker;
2264 for (i = dev->queue_count - 1; i > 0; i--) {
2265 struct nvme_queue *nvmeq = dev->queues[i];
2266
2267 if (nvme_suspend_queue(nvmeq))
2268 continue;
2269 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2270 nvmeq->cmdinfo.worker = dq.worker;
2271 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2272 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2273 }
2274 nvme_wait_dq(&dq, dev);
2275 kthread_stop(kworker_task);
2276}
2277
Keith Buschf0b50732013-07-15 15:02:21 -06002278static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002279{
Keith Busch22404272013-07-15 15:02:20 -06002280 int i;
2281
Keith Buschd4b4ff82013-12-10 13:10:37 -07002282 dev->initialized = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002283
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002284 spin_lock(&dev_list_lock);
Keith Buschf0b50732013-07-15 15:02:21 -06002285 list_del_init(&dev->node);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002286 spin_unlock(&dev_list_lock);
2287
Keith Busch4d115422013-12-10 13:10:40 -07002288 if (!dev->bar || (dev->bar && readl(&dev->bar->csts) == -1)) {
2289 for (i = dev->queue_count - 1; i >= 0; i--) {
2290 struct nvme_queue *nvmeq = dev->queues[i];
2291 nvme_suspend_queue(nvmeq);
2292 nvme_clear_queue(nvmeq);
2293 }
2294 } else {
2295 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002296 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002297 nvme_disable_queue(dev, 0);
2298 }
Keith Buschf0b50732013-07-15 15:02:21 -06002299 nvme_dev_unmap(dev);
2300}
2301
2302static void nvme_dev_remove(struct nvme_dev *dev)
2303{
Keith Busch9ac27092014-01-31 16:53:39 -07002304 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002305
Keith Busch9ac27092014-01-31 16:53:39 -07002306 list_for_each_entry(ns, &dev->namespaces, list) {
2307 if (ns->disk->flags & GENHD_FL_UP)
2308 del_gendisk(ns->disk);
2309 if (!blk_queue_dying(ns->queue))
2310 blk_cleanup_queue(ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002311 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002312}
2313
Matthew Wilcox091b6092011-02-10 09:56:01 -05002314static int nvme_setup_prp_pools(struct nvme_dev *dev)
2315{
2316 struct device *dmadev = &dev->pci_dev->dev;
2317 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2318 PAGE_SIZE, PAGE_SIZE, 0);
2319 if (!dev->prp_page_pool)
2320 return -ENOMEM;
2321
Matthew Wilcox99802a72011-02-10 10:30:34 -05002322 /* Optimisation for I/Os between 4k and 128k */
2323 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2324 256, 256, 0);
2325 if (!dev->prp_small_pool) {
2326 dma_pool_destroy(dev->prp_page_pool);
2327 return -ENOMEM;
2328 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002329 return 0;
2330}
2331
2332static void nvme_release_prp_pools(struct nvme_dev *dev)
2333{
2334 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002335 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002336}
2337
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002338static DEFINE_IDA(nvme_instance_ida);
2339
2340static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002341{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002342 int instance, error;
2343
2344 do {
2345 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2346 return -ENODEV;
2347
2348 spin_lock(&dev_list_lock);
2349 error = ida_get_new(&nvme_instance_ida, &instance);
2350 spin_unlock(&dev_list_lock);
2351 } while (error == -EAGAIN);
2352
2353 if (error)
2354 return -ENODEV;
2355
2356 dev->instance = instance;
2357 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002358}
2359
2360static void nvme_release_instance(struct nvme_dev *dev)
2361{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002362 spin_lock(&dev_list_lock);
2363 ida_remove(&nvme_instance_ida, dev->instance);
2364 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002365}
2366
Keith Busch9ac27092014-01-31 16:53:39 -07002367static void nvme_free_namespaces(struct nvme_dev *dev)
2368{
2369 struct nvme_ns *ns, *next;
2370
2371 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2372 list_del(&ns->list);
2373 put_disk(ns->disk);
2374 kfree(ns);
2375 }
2376}
2377
Keith Busch5e82e952013-02-19 10:17:58 -07002378static void nvme_free_dev(struct kref *kref)
2379{
2380 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002381
2382 nvme_free_namespaces(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002383 kfree(dev->queues);
2384 kfree(dev->entry);
2385 kfree(dev);
2386}
2387
2388static int nvme_dev_open(struct inode *inode, struct file *f)
2389{
2390 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
2391 miscdev);
2392 kref_get(&dev->kref);
2393 f->private_data = dev;
2394 return 0;
2395}
2396
2397static int nvme_dev_release(struct inode *inode, struct file *f)
2398{
2399 struct nvme_dev *dev = f->private_data;
2400 kref_put(&dev->kref, nvme_free_dev);
2401 return 0;
2402}
2403
2404static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2405{
2406 struct nvme_dev *dev = f->private_data;
2407 switch (cmd) {
2408 case NVME_IOCTL_ADMIN_CMD:
2409 return nvme_user_admin_cmd(dev, (void __user *)arg);
2410 default:
2411 return -ENOTTY;
2412 }
2413}
2414
2415static const struct file_operations nvme_dev_fops = {
2416 .owner = THIS_MODULE,
2417 .open = nvme_dev_open,
2418 .release = nvme_dev_release,
2419 .unlocked_ioctl = nvme_dev_ioctl,
2420 .compat_ioctl = nvme_dev_ioctl,
2421};
2422
Keith Buschf0b50732013-07-15 15:02:21 -06002423static int nvme_dev_start(struct nvme_dev *dev)
2424{
2425 int result;
2426
2427 result = nvme_dev_map(dev);
2428 if (result)
2429 return result;
2430
2431 result = nvme_configure_admin_queue(dev);
2432 if (result)
2433 goto unmap;
2434
2435 spin_lock(&dev_list_lock);
2436 list_add(&dev->node, &dev_list);
2437 spin_unlock(&dev_list_lock);
2438
2439 result = nvme_setup_io_queues(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002440 if (result && result != -EBUSY)
Keith Buschf0b50732013-07-15 15:02:21 -06002441 goto disable;
2442
Keith Buschd82e8bf2013-09-05 14:45:07 -06002443 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002444
2445 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002446 nvme_disable_queue(dev, 0);
Keith Buschf0b50732013-07-15 15:02:21 -06002447 spin_lock(&dev_list_lock);
2448 list_del_init(&dev->node);
2449 spin_unlock(&dev_list_lock);
2450 unmap:
2451 nvme_dev_unmap(dev);
2452 return result;
2453}
2454
Keith Busch9a6b9452013-12-10 13:10:36 -07002455static int nvme_remove_dead_ctrl(void *arg)
2456{
2457 struct nvme_dev *dev = (struct nvme_dev *)arg;
2458 struct pci_dev *pdev = dev->pci_dev;
2459
2460 if (pci_get_drvdata(pdev))
2461 pci_stop_and_remove_bus_device(pdev);
2462 kref_put(&dev->kref, nvme_free_dev);
2463 return 0;
2464}
2465
2466static void nvme_remove_disks(struct work_struct *ws)
2467{
2468 int i;
2469 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2470
2471 nvme_dev_remove(dev);
2472 spin_lock(&dev_list_lock);
2473 for (i = dev->queue_count - 1; i > 0; i--) {
2474 BUG_ON(!dev->queues[i] || !dev->queues[i]->q_suspended);
2475 nvme_free_queue(dev->queues[i]);
2476 dev->queue_count--;
2477 dev->queues[i] = NULL;
2478 }
2479 spin_unlock(&dev_list_lock);
2480}
2481
2482static int nvme_dev_resume(struct nvme_dev *dev)
2483{
2484 int ret;
2485
2486 ret = nvme_dev_start(dev);
2487 if (ret && ret != -EBUSY)
2488 return ret;
2489 if (ret == -EBUSY) {
2490 spin_lock(&dev_list_lock);
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05002491 PREPARE_WORK(&dev->reset_work, nvme_remove_disks);
Keith Busch9a6b9452013-12-10 13:10:36 -07002492 queue_work(nvme_workq, &dev->reset_work);
2493 spin_unlock(&dev_list_lock);
2494 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002495 dev->initialized = 1;
Keith Busch9a6b9452013-12-10 13:10:36 -07002496 return 0;
2497}
2498
2499static void nvme_dev_reset(struct nvme_dev *dev)
2500{
2501 nvme_dev_shutdown(dev);
2502 if (nvme_dev_resume(dev)) {
2503 dev_err(&dev->pci_dev->dev, "Device failed to resume\n");
2504 kref_get(&dev->kref);
2505 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2506 dev->instance))) {
2507 dev_err(&dev->pci_dev->dev,
2508 "Failed to start controller remove task\n");
2509 kref_put(&dev->kref, nvme_free_dev);
2510 }
2511 }
2512}
2513
2514static void nvme_reset_failed_dev(struct work_struct *ws)
2515{
2516 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2517 nvme_dev_reset(dev);
2518}
2519
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002520static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002521{
Keith Busch0877cb02013-07-15 15:02:19 -06002522 int result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002523 struct nvme_dev *dev;
2524
2525 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2526 if (!dev)
2527 return -ENOMEM;
2528 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
2529 GFP_KERNEL);
2530 if (!dev->entry)
2531 goto free;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002532 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
2533 GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002534 if (!dev->queues)
2535 goto free;
2536
2537 INIT_LIST_HEAD(&dev->namespaces);
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05002538 INIT_WORK(&dev->reset_work, nvme_reset_failed_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002539 dev->pci_dev = pdev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002540 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002541 result = nvme_set_instance(dev);
2542 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002543 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002544
Matthew Wilcox091b6092011-02-10 09:56:01 -05002545 result = nvme_setup_prp_pools(dev);
2546 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002547 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002548
Keith Buschf0b50732013-07-15 15:02:21 -06002549 result = nvme_dev_start(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002550 if (result) {
2551 if (result == -EBUSY)
2552 goto create_cdev;
Keith Busch0877cb02013-07-15 15:02:19 -06002553 goto release_pools;
Keith Buschd82e8bf2013-09-05 14:45:07 -06002554 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002555
Keith Busch9ac27092014-01-31 16:53:39 -07002556 kref_init(&dev->kref);
Matthew Wilcox740216f2011-02-15 16:28:20 -05002557 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002558 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002559 goto shutdown;
Matthew Wilcox740216f2011-02-15 16:28:20 -05002560
Keith Buschd82e8bf2013-09-05 14:45:07 -06002561 create_cdev:
Keith Busch5e82e952013-02-19 10:17:58 -07002562 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
2563 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2564 dev->miscdev.parent = &pdev->dev;
2565 dev->miscdev.name = dev->name;
2566 dev->miscdev.fops = &nvme_dev_fops;
2567 result = misc_register(&dev->miscdev);
2568 if (result)
2569 goto remove;
2570
Keith Buschd4b4ff82013-12-10 13:10:37 -07002571 dev->initialized = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002572 return 0;
2573
Keith Busch5e82e952013-02-19 10:17:58 -07002574 remove:
2575 nvme_dev_remove(dev);
Keith Busch9ac27092014-01-31 16:53:39 -07002576 nvme_free_namespaces(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002577 shutdown:
2578 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002579 release_pools:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002580 nvme_free_queues(dev, 0);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002581 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002582 release:
2583 nvme_release_instance(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002584 free:
2585 kfree(dev->queues);
2586 kfree(dev->entry);
2587 kfree(dev);
2588 return result;
2589}
2590
Keith Busch09ece142014-01-27 11:29:40 -05002591static void nvme_shutdown(struct pci_dev *pdev)
2592{
2593 struct nvme_dev *dev = pci_get_drvdata(pdev);
2594 nvme_dev_shutdown(dev);
2595}
2596
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002597static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002598{
2599 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002600
2601 spin_lock(&dev_list_lock);
2602 list_del_init(&dev->node);
2603 spin_unlock(&dev_list_lock);
2604
2605 pci_set_drvdata(pdev, NULL);
2606 flush_work(&dev->reset_work);
Keith Busch5e82e952013-02-19 10:17:58 -07002607 misc_deregister(&dev->miscdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002608 nvme_dev_remove(dev);
2609 nvme_dev_shutdown(dev);
Keith Buscha1a5ef92013-12-16 13:50:00 -05002610 nvme_free_queues(dev, 0);
Keith Busch9a6b9452013-12-10 13:10:36 -07002611 nvme_release_instance(dev);
2612 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002613 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002614}
2615
2616/* These functions are yet to be implemented */
2617#define nvme_error_detected NULL
2618#define nvme_dump_registers NULL
2619#define nvme_link_reset NULL
2620#define nvme_slot_reset NULL
2621#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002622
2623static int nvme_suspend(struct device *dev)
2624{
2625 struct pci_dev *pdev = to_pci_dev(dev);
2626 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2627
2628 nvme_dev_shutdown(ndev);
2629 return 0;
2630}
2631
2632static int nvme_resume(struct device *dev)
2633{
2634 struct pci_dev *pdev = to_pci_dev(dev);
2635 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002636
Keith Busch9a6b9452013-12-10 13:10:36 -07002637 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Matthew Wilcoxbdfd70f2014-01-29 09:33:52 -05002638 PREPARE_WORK(&ndev->reset_work, nvme_reset_failed_dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002639 queue_work(nvme_workq, &ndev->reset_work);
2640 }
2641 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002642}
2643
2644static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002645
Stephen Hemminger1d352032012-09-07 09:33:17 -07002646static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002647 .error_detected = nvme_error_detected,
2648 .mmio_enabled = nvme_dump_registers,
2649 .link_reset = nvme_link_reset,
2650 .slot_reset = nvme_slot_reset,
2651 .resume = nvme_error_resume,
2652};
2653
2654/* Move to pci_ids.h later */
2655#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2656
2657static DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
2658 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2659 { 0, }
2660};
2661MODULE_DEVICE_TABLE(pci, nvme_id_table);
2662
2663static struct pci_driver nvme_driver = {
2664 .name = "nvme",
2665 .id_table = nvme_id_table,
2666 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002667 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002668 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002669 .driver = {
2670 .pm = &nvme_dev_pm_ops,
2671 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002672 .err_handler = &nvme_err_handler,
2673};
2674
2675static int __init nvme_init(void)
2676{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002677 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002678
2679 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2680 if (IS_ERR(nvme_thread))
2681 return PTR_ERR(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002682
Keith Busch9a6b9452013-12-10 13:10:36 -07002683 result = -ENOMEM;
2684 nvme_workq = create_singlethread_workqueue("nvme");
2685 if (!nvme_workq)
2686 goto kill_kthread;
2687
Keith Busch5c42ea12012-07-25 16:05:18 -06002688 result = register_blkdev(nvme_major, "nvme");
2689 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002690 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06002691 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002692 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002693
2694 result = pci_register_driver(&nvme_driver);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002695 if (result)
2696 goto unregister_blkdev;
2697 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002698
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002699 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002700 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002701 kill_workq:
2702 destroy_workqueue(nvme_workq);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002703 kill_kthread:
2704 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002705 return result;
2706}
2707
2708static void __exit nvme_exit(void)
2709{
2710 pci_unregister_driver(&nvme_driver);
2711 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002712 destroy_workqueue(nvme_workq);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002713 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002714}
2715
2716MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2717MODULE_LICENSE("GPL");
Matthew Wilcox366e8212012-01-10 16:30:15 -05002718MODULE_VERSION("0.8");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002719module_init(nvme_init);
2720module_exit(nvme_exit);