blob: bfea7ec22b985b46ed11a3c3c81d7463b61b80c4 [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003 * Copyright (c) 2011-2014, Intel Corporation.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05004 *
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.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050013 */
14
Matthew Wilcox8de05532011-05-12 13:50:28 -040015#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050016#include <linux/blkdev.h>
Matias Bjørlinga4aea562014-11-04 08:20:14 -070017#include <linux/blk-mq.h>
Keith Busch42f61422014-03-24 10:46:25 -060018#include <linux/cpu.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040019#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050020#include <linux/errno.h>
21#include <linux/fs.h>
22#include <linux/genhd.h>
Keith Busch4cc09e22014-04-02 15:45:37 -060023#include <linux/hdreg.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040024#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050025#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/io.h>
28#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050029#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050030#include <linux/kernel.h>
Keith Buscha5768aa2015-06-01 14:28:14 -060031#include <linux/list_sort.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050032#include <linux/mm.h>
33#include <linux/module.h>
34#include <linux/moduleparam.h>
35#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050036#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040037#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050038#include <linux/sched.h>
39#include <linux/slab.h>
Keith Busche1e5e562015-02-19 13:39:03 -070040#include <linux/t10-pi.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050041#include <linux/types.h>
Keith Busch1d277a62015-10-15 14:10:52 +020042#include <linux/pr.h>
Vishal Verma5d0f6132013-03-04 18:40:58 -070043#include <scsi/sg.h>
Linus Torvalds9cf5c092015-11-06 14:22:15 -080044#include <linux/io-64-nonatomic-lo-hi.h>
Keith Busch1d277a62015-10-15 14:10:52 +020045#include <asm/unaligned.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090046
Christoph Hellwig9d99a8d2015-10-02 15:25:49 +020047#include <uapi/linux/nvme_ioctl.h>
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +020048#include "nvme.h"
49
Keith Buschb3fffde2015-02-03 11:21:42 -070050#define NVME_MINORS (1U << MINORBITS)
Keith Busch9d43cf62014-05-13 11:42:02 -060051#define NVME_Q_DEPTH 1024
Jens Axboed31af0a2015-03-06 12:56:13 -070052#define NVME_AQ_DEPTH 256
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050053#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
54#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Dan McLeran2484f402014-07-01 09:33:32 -060055#define SHUTDOWN_TIMEOUT (shutdown_timeout * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050056
Christoph Hellwig21d34712015-11-26 09:08:36 +010057unsigned char admin_timeout = 60;
Keith Busch9d43cf62014-05-13 11:42:02 -060058module_param(admin_timeout, byte, 0644);
59MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050060
Matthew Wilcoxbd676082014-06-03 23:04:30 -040061unsigned char nvme_io_timeout = 30;
62module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
Keith Buschb3550842014-04-04 11:43:36 -060063MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050064
Dan McLeran2484f402014-07-01 09:33:32 -060065static unsigned char shutdown_timeout = 5;
66module_param(shutdown_timeout, byte, 0644);
67MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
68
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050069static int nvme_major;
70module_param(nvme_major, int, 0);
71
Keith Buschb3fffde2015-02-03 11:21:42 -070072static int nvme_char_major;
73module_param(nvme_char_major, int, 0);
74
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050075static int use_threaded_interrupts;
76module_param(use_threaded_interrupts, int, 0);
77
Jon Derrick8ffaadf2015-07-20 10:14:09 -060078static bool use_cmb_sqes = true;
79module_param(use_cmb_sqes, bool, 0644);
80MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
81
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050082static DEFINE_SPINLOCK(dev_list_lock);
83static LIST_HEAD(dev_list);
84static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070085static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060086static wait_queue_head_t nvme_kthread_wait;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050087
Keith Buschb3fffde2015-02-03 11:21:42 -070088static struct class *nvme_class;
89
Christoph Hellwig906678922015-10-02 18:49:23 +020090static int __nvme_reset(struct nvme_dev *dev);
Keith Busch4cc06522015-06-05 10:30:08 -060091static int nvme_reset(struct nvme_dev *dev);
Jens Axboea0fa9642015-11-03 20:37:26 -070092static void nvme_process_cq(struct nvme_queue *nvmeq);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +020093static void nvme_dead_ctrl(struct nvme_dev *dev);
Keith Buschd4b4ff82013-12-10 13:10:37 -070094
Keith Busch4d115422013-12-10 13:10:40 -070095struct async_cmd_info {
96 struct kthread_work work;
97 struct kthread_worker *worker;
Matias Bjørlinga4aea562014-11-04 08:20:14 -070098 struct request *req;
Keith Busch4d115422013-12-10 13:10:40 -070099 u32 result;
100 int status;
101 void *ctx;
102};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500103
104/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500105 * An NVM Express queue. Each device has at least two (one for admin
106 * commands and one for I/O commands).
107 */
108struct nvme_queue {
109 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500110 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500111 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500112 spinlock_t q_lock;
113 struct nvme_command *sq_cmds;
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600114 struct nvme_command __iomem *sq_cmds_io;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500115 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600116 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500117 dma_addr_t sq_dma_addr;
118 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500119 u32 __iomem *q_db;
120 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700121 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500122 u16 sq_head;
123 u16 sq_tail;
124 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700125 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400126 u8 cq_phase;
127 u8 cqe_seen;
Keith Busch4d115422013-12-10 13:10:40 -0700128 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500129};
130
131/*
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200132 * The nvme_iod describes the data in an I/O, including the list of PRP
133 * entries. You can't see it in this data structure because C doesn't let
134 * me express that. Use nvme_alloc_iod to ensure there's enough space
135 * allocated to store the PRP list.
136 */
137struct nvme_iod {
138 unsigned long private; /* For the use of the submitter of the I/O */
139 int npages; /* In the PRP list. 0 means small pool in use */
140 int offset; /* Of PRP list */
141 int nents; /* Used in scatterlist */
142 int length; /* Of data, in bytes */
143 dma_addr_t first_dma;
144 struct scatterlist meta_sg[1]; /* metadata requires single contiguous buffer */
145 struct scatterlist sg[0];
146};
147
148/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500149 * Check we didin't inadvertently grow the command struct
150 */
151static inline void _nvme_check_size(void)
152{
153 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
154 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
155 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
156 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
157 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400158 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700159 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500160 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
161 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
162 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
163 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600164 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500165}
166
Keith Buschedd10d32014-04-03 16:45:23 -0600167typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400168 struct nvme_completion *);
169
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500170struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400171 nvme_completion_fn fn;
172 void *ctx;
Keith Buschc30341d2013-12-10 13:10:38 -0700173 int aborted;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700174 struct nvme_queue *nvmeq;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700175 struct nvme_iod iod[0];
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500176};
177
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700178/*
179 * Max size of iod being embedded in the request payload
180 */
181#define NVME_INT_PAGES 2
182#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->page_size)
Chong Yuanfda631f2015-03-27 09:21:32 +0800183#define NVME_INT_MASK 0x01
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700184
185/*
186 * Will slightly overestimate the number of pages needed. This is OK
187 * as it only leads to a small amount of wasted memory for the lifetime of
188 * the I/O.
189 */
190static int nvme_npages(unsigned size, struct nvme_dev *dev)
191{
192 unsigned nprps = DIV_ROUND_UP(size + dev->page_size, dev->page_size);
193 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
194}
195
196static unsigned int nvme_cmd_size(struct nvme_dev *dev)
197{
198 unsigned int ret = sizeof(struct nvme_cmd_info);
199
200 ret += sizeof(struct nvme_iod);
201 ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
202 ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
203
204 return ret;
205}
206
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700207static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
208 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500209{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700210 struct nvme_dev *dev = data;
211 struct nvme_queue *nvmeq = dev->queues[0];
212
Keith Busch42483222015-06-01 09:29:54 -0600213 WARN_ON(hctx_idx != 0);
214 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
215 WARN_ON(nvmeq->tags);
216
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700217 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600218 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700219 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500220}
221
Keith Busch4af0e212015-06-08 10:08:13 -0600222static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
223{
224 struct nvme_queue *nvmeq = hctx->driver_data;
225
226 nvmeq->tags = NULL;
227}
228
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700229static int nvme_admin_init_request(void *data, struct request *req,
230 unsigned int hctx_idx, unsigned int rq_idx,
231 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600232{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700233 struct nvme_dev *dev = data;
234 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
235 struct nvme_queue *nvmeq = dev->queues[0];
236
237 BUG_ON(!nvmeq);
238 cmd->nvmeq = nvmeq;
239 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600240}
241
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700242static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
243 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500244{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700245 struct nvme_dev *dev = data;
Keith Busch42483222015-06-01 09:29:54 -0600246 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500247
Keith Busch42483222015-06-01 09:29:54 -0600248 if (!nvmeq->tags)
249 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500250
Keith Busch42483222015-06-01 09:29:54 -0600251 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700252 hctx->driver_data = nvmeq;
253 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500254}
255
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700256static int nvme_init_request(void *data, struct request *req,
257 unsigned int hctx_idx, unsigned int rq_idx,
258 unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500259{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700260 struct nvme_dev *dev = data;
261 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
262 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
263
264 BUG_ON(!nvmeq);
265 cmd->nvmeq = nvmeq;
266 return 0;
267}
268
269static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
270 nvme_completion_fn handler)
271{
272 cmd->fn = handler;
273 cmd->ctx = ctx;
274 cmd->aborted = 0;
Keith Buschc917dfe2015-01-07 18:55:48 -0700275 blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500276}
277
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700278static void *iod_get_private(struct nvme_iod *iod)
279{
280 return (void *) (iod->private & ~0x1UL);
281}
282
283/*
284 * If bit 0 is set, the iod is embedded in the request payload.
285 */
286static bool iod_should_kfree(struct nvme_iod *iod)
287{
Chong Yuanfda631f2015-03-27 09:21:32 +0800288 return (iod->private & NVME_INT_MASK) == 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700289}
290
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400291/* Special values must be less than 0x1000 */
292#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500293#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
294#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
295#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500296
Keith Buschedd10d32014-04-03 16:45:23 -0600297static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400298 struct nvme_completion *cqe)
299{
300 if (ctx == CMD_CTX_CANCELLED)
301 return;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400302 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600303 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400304 "completed id %d twice on queue %d\n",
305 cqe->command_id, le16_to_cpup(&cqe->sq_id));
306 return;
307 }
308 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600309 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400310 "invalid id %d completed on queue %d\n",
311 cqe->command_id, le16_to_cpup(&cqe->sq_id));
312 return;
313 }
Keith Buschedd10d32014-04-03 16:45:23 -0600314 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400315}
316
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700317static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
318{
319 void *ctx;
320
321 if (fn)
322 *fn = cmd->fn;
323 ctx = cmd->ctx;
324 cmd->fn = special_completion;
325 cmd->ctx = CMD_CTX_CANCELLED;
326 return ctx;
327}
328
329static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
330 struct nvme_completion *cqe)
331{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700332 u32 result = le32_to_cpup(&cqe->result);
333 u16 status = le16_to_cpup(&cqe->status) >> 1;
334
335 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
336 ++nvmeq->dev->event_limit;
Keith Buscha5768aa2015-06-01 14:28:14 -0600337 if (status != NVME_SC_SUCCESS)
338 return;
339
340 switch (result & 0xff07) {
341 case NVME_AER_NOTICE_NS_CHANGED:
342 dev_info(nvmeq->q_dmadev, "rescanning\n");
343 schedule_work(&nvmeq->dev->scan_work);
344 default:
345 dev_warn(nvmeq->q_dmadev, "async event result %08x\n", result);
346 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700347}
348
349static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
350 struct nvme_completion *cqe)
351{
352 struct request *req = ctx;
353
354 u16 status = le16_to_cpup(&cqe->status) >> 1;
355 u32 result = le32_to_cpup(&cqe->result);
356
Keith Busch42483222015-06-01 09:29:54 -0600357 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700358
359 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
360 ++nvmeq->dev->abort_limit;
361}
362
Keith Buschedd10d32014-04-03 16:45:23 -0600363static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700364 struct nvme_completion *cqe)
365{
366 struct async_cmd_info *cmdinfo = ctx;
367 cmdinfo->result = le32_to_cpup(&cqe->result);
368 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
369 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
Keith Busch42483222015-06-01 09:29:54 -0600370 blk_mq_free_request(cmdinfo->req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700371}
372
373static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
374 unsigned int tag)
375{
Keith Busch42483222015-06-01 09:29:54 -0600376 struct request *req = blk_mq_tag_to_rq(*nvmeq->tags, tag);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700377
378 return blk_mq_rq_to_pdu(req);
Keith Busch4d115422013-12-10 13:10:40 -0700379}
380
Matthew Wilcox184d2942011-05-11 21:36:38 -0400381/*
382 * Called with local interrupts disabled and the q_lock held. May not sleep.
383 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700384static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400385 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500386{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700387 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400388 void *ctx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700389 if (tag >= nvmeq->q_depth) {
390 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500391 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400392 }
Keith Busch859361a2012-08-02 14:05:59 -0600393 if (fn)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700394 *fn = cmd->fn;
395 ctx = cmd->ctx;
396 cmd->fn = special_completion;
397 cmd->ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400398 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500399}
400
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500401/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400402 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500403 * @nvmeq: The queue to use
404 * @cmd: The command to send
405 *
406 * Safe to use from interrupt context
407 */
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530408static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
409 struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500410{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700411 u16 tail = nvmeq->sq_tail;
412
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600413 if (nvmeq->sq_cmds_io)
414 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
415 else
416 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
417
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500418 if (++tail == nvmeq->q_depth)
419 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500420 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500421 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500422}
423
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530424static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700425{
426 unsigned long flags;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700427 spin_lock_irqsave(&nvmeq->q_lock, flags);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530428 __nvme_submit_cmd(nvmeq, cmd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700429 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700430}
431
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500432static __le64 **iod_list(struct nvme_iod *iod)
433{
434 return ((void *)iod) + iod->offset;
435}
436
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700437static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
438 unsigned nseg, unsigned long private)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500439{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700440 iod->private = private;
441 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
442 iod->npages = -1;
443 iod->length = nbytes;
444 iod->nents = 0;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500445}
446
447static struct nvme_iod *
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700448__nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
449 unsigned long priv, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500450{
451 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700452 sizeof(__le64 *) * nvme_npages(bytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500453 sizeof(struct scatterlist) * nseg, gfp);
454
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700455 if (iod)
456 iod_init(iod, bytes, nseg, priv);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500457
458 return iod;
459}
460
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700461static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
462 gfp_t gfp)
463{
464 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
465 sizeof(struct nvme_dsm_range);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700466 struct nvme_iod *iod;
467
468 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
469 size <= NVME_INT_BYTES(dev)) {
470 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
471
472 iod = cmd->iod;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700473 iod_init(iod, size, rq->nr_phys_segments,
Chong Yuanfda631f2015-03-27 09:21:32 +0800474 (unsigned long) rq | NVME_INT_MASK);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700475 return iod;
476 }
477
478 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
479 (unsigned long) rq, gfp);
480}
481
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200482static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500483{
Keith Busch1d090622014-06-23 11:34:01 -0600484 const int last_prp = dev->page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500485 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500486 __le64 **list = iod_list(iod);
487 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500488
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500489 if (iod->npages == 0)
490 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
491 for (i = 0; i < iod->npages; i++) {
492 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500493 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500494 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500495 prp_dma = next_prp_dma;
496 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700497
498 if (iod_should_kfree(iod))
499 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500500}
501
Keith Buschb4ff9c82014-08-29 09:06:12 -0600502static int nvme_error_status(u16 status)
503{
504 switch (status & 0x7ff) {
505 case NVME_SC_SUCCESS:
506 return 0;
507 case NVME_SC_CAP_EXCEEDED:
508 return -ENOSPC;
509 default:
510 return -EIO;
511 }
512}
513
Keith Busch52b68d72015-02-23 09:16:21 -0700514#ifdef CONFIG_BLK_DEV_INTEGRITY
Keith Busche1e5e562015-02-19 13:39:03 -0700515static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
516{
517 if (be32_to_cpu(pi->ref_tag) == v)
518 pi->ref_tag = cpu_to_be32(p);
519}
520
521static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
522{
523 if (be32_to_cpu(pi->ref_tag) == p)
524 pi->ref_tag = cpu_to_be32(v);
525}
526
527/**
528 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
529 *
530 * The virtual start sector is the one that was originally submitted by the
531 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
532 * start sector may be different. Remap protection information to match the
533 * physical LBA on writes, and back to the original seed on reads.
534 *
535 * Type 0 and 3 do not have a ref tag, so no remapping required.
536 */
537static void nvme_dif_remap(struct request *req,
538 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
539{
540 struct nvme_ns *ns = req->rq_disk->private_data;
541 struct bio_integrity_payload *bip;
542 struct t10_pi_tuple *pi;
543 void *p, *pmap;
544 u32 i, nlb, ts, phys, virt;
545
546 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
547 return;
548
549 bip = bio_integrity(req->bio);
550 if (!bip)
551 return;
552
553 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
Keith Busche1e5e562015-02-19 13:39:03 -0700554
555 p = pmap;
556 virt = bip_get_seed(bip);
557 phys = nvme_block_nr(ns, blk_rq_pos(req));
558 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
Dan Williamsac6fc482015-10-21 13:20:18 -0400559 ts = ns->disk->queue->integrity.tuple_size;
Keith Busche1e5e562015-02-19 13:39:03 -0700560
561 for (i = 0; i < nlb; i++, virt++, phys++) {
562 pi = (struct t10_pi_tuple *)p;
563 dif_swap(phys, virt, pi);
564 p += ts;
565 }
566 kunmap_atomic(pmap);
567}
568
Keith Busch52b68d72015-02-23 09:16:21 -0700569static void nvme_init_integrity(struct nvme_ns *ns)
570{
571 struct blk_integrity integrity;
572
573 switch (ns->pi_type) {
574 case NVME_NS_DPS_PI_TYPE3:
Martin K. Petersen0f8087e2015-10-21 13:19:33 -0400575 integrity.profile = &t10_pi_type3_crc;
Keith Busch52b68d72015-02-23 09:16:21 -0700576 break;
577 case NVME_NS_DPS_PI_TYPE1:
578 case NVME_NS_DPS_PI_TYPE2:
Martin K. Petersen0f8087e2015-10-21 13:19:33 -0400579 integrity.profile = &t10_pi_type1_crc;
Keith Busch52b68d72015-02-23 09:16:21 -0700580 break;
581 default:
Dan Williams4125a092015-10-21 13:20:29 -0400582 integrity.profile = NULL;
Keith Busch52b68d72015-02-23 09:16:21 -0700583 break;
584 }
585 integrity.tuple_size = ns->ms;
586 blk_integrity_register(ns->disk, &integrity);
587 blk_queue_max_integrity_segments(ns->queue, 1);
588}
589#else /* CONFIG_BLK_DEV_INTEGRITY */
590static void nvme_dif_remap(struct request *req,
591 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
592{
593}
594static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
595{
596}
597static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
598{
599}
600static void nvme_init_integrity(struct nvme_ns *ns)
601{
602}
603#endif
604
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700605static void req_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500606 struct nvme_completion *cqe)
607{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500608 struct nvme_iod *iod = ctx;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700609 struct request *req = iod_get_private(iod);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700610 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500611 u16 status = le16_to_cpup(&cqe->status) >> 1;
Keith Busch0dfc70c2015-10-15 13:38:48 -0600612 bool requeue = false;
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200613 int error = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500614
Keith Buschedd10d32014-04-03 16:45:23 -0600615 if (unlikely(status)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700616 if (!(status & NVME_SC_DNR || blk_noretry_request(req))
617 && (jiffies - req->start_time) < req->timeout) {
Keith Buschc9d3bf82015-01-07 18:55:52 -0700618 unsigned long flags;
619
Keith Busch0dfc70c2015-10-15 13:38:48 -0600620 requeue = true;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700621 blk_mq_requeue_request(req);
Keith Buschc9d3bf82015-01-07 18:55:52 -0700622 spin_lock_irqsave(req->q->queue_lock, flags);
623 if (!blk_queue_stopped(req->q))
624 blk_mq_kick_requeue_list(req->q);
625 spin_unlock_irqrestore(req->q->queue_lock, flags);
Keith Busch0dfc70c2015-10-15 13:38:48 -0600626 goto release_iod;
Keith Buschedd10d32014-04-03 16:45:23 -0600627 }
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200628
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200629 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
Keith Busch17188bb2015-06-08 10:08:14 -0600630 if (cmd_rq->ctx == CMD_CTX_CANCELLED)
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200631 error = -EINTR;
632 else
633 error = status;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200634 } else {
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200635 error = nvme_error_status(status);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200636 }
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200637 }
638
Keith Buscha0a931d2015-05-22 12:28:31 -0600639 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
640 u32 result = le32_to_cpup(&cqe->result);
641 req->special = (void *)(uintptr_t)result;
642 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700643
644 if (cmd_rq->aborted)
Christoph Hellwige75ec752015-05-22 11:12:39 +0200645 dev_warn(nvmeq->dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700646 "completing aborted command with status:%04x\n",
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200647 error);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700648
Keith Busch0dfc70c2015-10-15 13:38:48 -0600649release_iod:
Keith Busche1e5e562015-02-19 13:39:03 -0700650 if (iod->nents) {
Christoph Hellwige75ec752015-05-22 11:12:39 +0200651 dma_unmap_sg(nvmeq->dev->dev, iod->sg, iod->nents,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700652 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busche1e5e562015-02-19 13:39:03 -0700653 if (blk_integrity_rq(req)) {
654 if (!rq_data_dir(req))
655 nvme_dif_remap(req, nvme_dif_complete);
Christoph Hellwige75ec752015-05-22 11:12:39 +0200656 dma_unmap_sg(nvmeq->dev->dev, iod->meta_sg, 1,
Keith Busche1e5e562015-02-19 13:39:03 -0700657 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
658 }
659 }
Keith Buschedd10d32014-04-03 16:45:23 -0600660 nvme_free_iod(nvmeq->dev, iod);
Keith Busch3291fa52014-04-28 12:30:52 -0600661
Keith Busch0dfc70c2015-10-15 13:38:48 -0600662 if (likely(!requeue))
663 blk_mq_complete_request(req, error);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500664}
665
Matthew Wilcox184d2942011-05-11 21:36:38 -0400666/* length is in bytes. gfp flags indicates whether we may sleep. */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200667static int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod,
668 int total_len, gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500669{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500670 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500671 int length = total_len;
672 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500673 int dma_len = sg_dma_len(sg);
674 u64 dma_addr = sg_dma_address(sg);
Murali Iyerf137e0f2015-03-26 11:07:51 -0500675 u32 page_size = dev->page_size;
676 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500677 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500678 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500679 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500680 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500681
Keith Busch1d090622014-06-23 11:34:01 -0600682 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500683 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500684 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500685
Keith Busch1d090622014-06-23 11:34:01 -0600686 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500687 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600688 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500689 } else {
690 sg = sg_next(sg);
691 dma_addr = sg_dma_address(sg);
692 dma_len = sg_dma_len(sg);
693 }
694
Keith Busch1d090622014-06-23 11:34:01 -0600695 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600696 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500697 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500698 }
699
Keith Busch1d090622014-06-23 11:34:01 -0600700 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500701 if (nprps <= (256 / 8)) {
702 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500703 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500704 } else {
705 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500706 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500707 }
708
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400709 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
710 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600711 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500712 iod->npages = -1;
Keith Busch1d090622014-06-23 11:34:01 -0600713 return (total_len - length) + page_size;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400714 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500715 list[0] = prp_list;
716 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500717 i = 0;
718 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600719 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500720 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400721 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500722 if (!prp_list)
723 return total_len - length;
724 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400725 prp_list[0] = old_prp_list[i - 1];
726 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
727 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500728 }
729 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600730 dma_len -= page_size;
731 dma_addr += page_size;
732 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500733 if (length <= 0)
734 break;
735 if (dma_len > 0)
736 continue;
737 BUG_ON(dma_len < 0);
738 sg = sg_next(sg);
739 dma_addr = sg_dma_address(sg);
740 dma_len = sg_dma_len(sg);
741 }
742
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500743 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500744}
745
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200746static void nvme_submit_priv(struct nvme_queue *nvmeq, struct request *req,
747 struct nvme_iod *iod)
748{
Jon Derrick498c4392015-07-20 10:14:08 -0600749 struct nvme_command cmnd;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200750
Jon Derrick498c4392015-07-20 10:14:08 -0600751 memcpy(&cmnd, req->cmd, sizeof(cmnd));
752 cmnd.rw.command_id = req->tag;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200753 if (req->nr_phys_segments) {
Jon Derrick498c4392015-07-20 10:14:08 -0600754 cmnd.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
755 cmnd.rw.prp2 = cpu_to_le64(iod->first_dma);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200756 }
757
Jon Derrick498c4392015-07-20 10:14:08 -0600758 __nvme_submit_cmd(nvmeq, &cmnd);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200759}
760
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700761/*
762 * We reuse the small pool to allocate the 16-byte range here as it is not
763 * worth having a special pool for these or additional cases to handle freeing
764 * the iod.
765 */
766static void nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
767 struct request *req, struct nvme_iod *iod)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700768{
Keith Buschedd10d32014-04-03 16:45:23 -0600769 struct nvme_dsm_range *range =
770 (struct nvme_dsm_range *)iod_list(iod)[0];
Jon Derrick498c4392015-07-20 10:14:08 -0600771 struct nvme_command cmnd;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700772
Keith Busch0e5e4f02012-11-09 16:33:05 -0700773 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700774 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
775 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700776
Jon Derrick498c4392015-07-20 10:14:08 -0600777 memset(&cmnd, 0, sizeof(cmnd));
778 cmnd.dsm.opcode = nvme_cmd_dsm;
779 cmnd.dsm.command_id = req->tag;
780 cmnd.dsm.nsid = cpu_to_le32(ns->ns_id);
781 cmnd.dsm.prp1 = cpu_to_le64(iod->first_dma);
782 cmnd.dsm.nr = 0;
783 cmnd.dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
Keith Busch0e5e4f02012-11-09 16:33:05 -0700784
Jon Derrick498c4392015-07-20 10:14:08 -0600785 __nvme_submit_cmd(nvmeq, &cmnd);
Keith Busch0e5e4f02012-11-09 16:33:05 -0700786}
787
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700788static void nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500789 int cmdid)
790{
Jon Derrick498c4392015-07-20 10:14:08 -0600791 struct nvme_command cmnd;
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500792
Jon Derrick498c4392015-07-20 10:14:08 -0600793 memset(&cmnd, 0, sizeof(cmnd));
794 cmnd.common.opcode = nvme_cmd_flush;
795 cmnd.common.command_id = cmdid;
796 cmnd.common.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500797
Jon Derrick498c4392015-07-20 10:14:08 -0600798 __nvme_submit_cmd(nvmeq, &cmnd);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500799}
800
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700801static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod,
802 struct nvme_ns *ns)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500803{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700804 struct request *req = iod_get_private(iod);
Jon Derrick498c4392015-07-20 10:14:08 -0600805 struct nvme_command cmnd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700806 u16 control = 0;
807 u32 dsmgmt = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500808
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700809 if (req->cmd_flags & REQ_FUA)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500810 control |= NVME_RW_FUA;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700811 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500812 control |= NVME_RW_LR;
813
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700814 if (req->cmd_flags & REQ_RAHEAD)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500815 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
816
Jon Derrick498c4392015-07-20 10:14:08 -0600817 memset(&cmnd, 0, sizeof(cmnd));
818 cmnd.rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
819 cmnd.rw.command_id = req->tag;
820 cmnd.rw.nsid = cpu_to_le32(ns->ns_id);
821 cmnd.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
822 cmnd.rw.prp2 = cpu_to_le64(iod->first_dma);
823 cmnd.rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
824 cmnd.rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500825
Alok Pandeye19b1272015-08-26 08:56:14 -0600826 if (ns->ms) {
Keith Busche1e5e562015-02-19 13:39:03 -0700827 switch (ns->pi_type) {
828 case NVME_NS_DPS_PI_TYPE3:
829 control |= NVME_RW_PRINFO_PRCHK_GUARD;
830 break;
831 case NVME_NS_DPS_PI_TYPE1:
832 case NVME_NS_DPS_PI_TYPE2:
833 control |= NVME_RW_PRINFO_PRCHK_GUARD |
834 NVME_RW_PRINFO_PRCHK_REF;
Jon Derrick498c4392015-07-20 10:14:08 -0600835 cmnd.rw.reftag = cpu_to_le32(
Keith Busche1e5e562015-02-19 13:39:03 -0700836 nvme_block_nr(ns, blk_rq_pos(req)));
837 break;
838 }
Alok Pandeye19b1272015-08-26 08:56:14 -0600839 if (blk_integrity_rq(req))
840 cmnd.rw.metadata =
841 cpu_to_le64(sg_dma_address(iod->meta_sg));
842 else
843 control |= NVME_RW_PRINFO_PRACT;
844 }
Keith Busche1e5e562015-02-19 13:39:03 -0700845
Jon Derrick498c4392015-07-20 10:14:08 -0600846 cmnd.rw.control = cpu_to_le16(control);
847 cmnd.rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500848
Jon Derrick498c4392015-07-20 10:14:08 -0600849 __nvme_submit_cmd(nvmeq, &cmnd);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500850
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500851 return 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600852}
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500853
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200854/*
855 * NOTE: ns is NULL when called on the admin queue.
856 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700857static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
858 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600859{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700860 struct nvme_ns *ns = hctx->queue->queuedata;
861 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200862 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700863 struct request *req = bd->rq;
864 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
Keith Buschedd10d32014-04-03 16:45:23 -0600865 struct nvme_iod *iod;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700866 enum dma_data_direction dma_dir;
Keith Buschedd10d32014-04-03 16:45:23 -0600867
Keith Busche1e5e562015-02-19 13:39:03 -0700868 /*
869 * If formated with metadata, require the block layer provide a buffer
870 * unless this namespace is formated such that the metadata can be
871 * stripped/generated by the controller with PRACT=1.
872 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200873 if (ns && ns->ms && !blk_integrity_rq(req)) {
Keith Busch71feb362015-06-19 11:07:30 -0600874 if (!(ns->pi_type && ns->ms == 8) &&
875 req->cmd_type != REQ_TYPE_DRV_PRIV) {
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200876 blk_mq_complete_request(req, -EFAULT);
Keith Busche1e5e562015-02-19 13:39:03 -0700877 return BLK_MQ_RQ_QUEUE_OK;
878 }
879 }
880
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200881 iod = nvme_alloc_iod(req, dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600882 if (!iod)
Jens Axboefe543032014-12-11 13:58:39 -0700883 return BLK_MQ_RQ_QUEUE_BUSY;
Keith Buschedd10d32014-04-03 16:45:23 -0600884
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700885 if (req->cmd_flags & REQ_DISCARD) {
Keith Buschedd10d32014-04-03 16:45:23 -0600886 void *range;
887 /*
888 * We reuse the small pool to allocate the 16-byte range here
889 * as it is not worth having a special pool for these or
890 * additional cases to handle freeing the iod.
891 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200892 range = dma_pool_alloc(dev->prp_small_pool, GFP_ATOMIC,
Keith Buschedd10d32014-04-03 16:45:23 -0600893 &iod->first_dma);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700894 if (!range)
Jens Axboefe543032014-12-11 13:58:39 -0700895 goto retry_cmd;
Keith Buschedd10d32014-04-03 16:45:23 -0600896 iod_list(iod)[0] = (__le64 *)range;
897 iod->npages = 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700898 } else if (req->nr_phys_segments) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700899 dma_dir = rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
Keith Buschedd10d32014-04-03 16:45:23 -0600900
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700901 sg_init_table(iod->sg, req->nr_phys_segments);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700902 iod->nents = blk_rq_map_sg(req->q, req, iod->sg);
Jens Axboefe543032014-12-11 13:58:39 -0700903 if (!iod->nents)
904 goto error_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700905
906 if (!dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir))
Jens Axboefe543032014-12-11 13:58:39 -0700907 goto retry_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700908
Jens Axboefe543032014-12-11 13:58:39 -0700909 if (blk_rq_bytes(req) !=
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200910 nvme_setup_prps(dev, iod, blk_rq_bytes(req), GFP_ATOMIC)) {
911 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
Jens Axboefe543032014-12-11 13:58:39 -0700912 goto retry_cmd;
913 }
Keith Busche1e5e562015-02-19 13:39:03 -0700914 if (blk_integrity_rq(req)) {
Christoph Hellwigbf508e92015-10-16 07:58:31 +0200915 if (blk_rq_count_integrity_sg(req->q, req->bio) != 1) {
916 dma_unmap_sg(dev->dev, iod->sg, iod->nents,
917 dma_dir);
Keith Busche1e5e562015-02-19 13:39:03 -0700918 goto error_cmd;
Christoph Hellwigbf508e92015-10-16 07:58:31 +0200919 }
Keith Busche1e5e562015-02-19 13:39:03 -0700920
921 sg_init_table(iod->meta_sg, 1);
922 if (blk_rq_map_integrity_sg(
Christoph Hellwigbf508e92015-10-16 07:58:31 +0200923 req->q, req->bio, iod->meta_sg) != 1) {
924 dma_unmap_sg(dev->dev, iod->sg, iod->nents,
925 dma_dir);
Keith Busche1e5e562015-02-19 13:39:03 -0700926 goto error_cmd;
Christoph Hellwigbf508e92015-10-16 07:58:31 +0200927 }
Keith Busche1e5e562015-02-19 13:39:03 -0700928
929 if (rq_data_dir(req))
930 nvme_dif_remap(req, nvme_dif_prep);
931
Christoph Hellwigbf508e92015-10-16 07:58:31 +0200932 if (!dma_map_sg(nvmeq->q_dmadev, iod->meta_sg, 1, dma_dir)) {
933 dma_unmap_sg(dev->dev, iod->sg, iod->nents,
934 dma_dir);
Keith Busche1e5e562015-02-19 13:39:03 -0700935 goto error_cmd;
Christoph Hellwigbf508e92015-10-16 07:58:31 +0200936 }
Keith Busche1e5e562015-02-19 13:39:03 -0700937 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700938 }
939
Keith Busch9af87852014-12-03 17:07:13 -0700940 nvme_set_info(cmd, iod, req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700941 spin_lock_irq(&nvmeq->q_lock);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200942 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
943 nvme_submit_priv(nvmeq, req, iod);
944 else if (req->cmd_flags & REQ_DISCARD)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700945 nvme_submit_discard(nvmeq, ns, req, iod);
946 else if (req->cmd_flags & REQ_FLUSH)
947 nvme_submit_flush(nvmeq, ns, req->tag);
948 else
949 nvme_submit_iod(nvmeq, iod, ns);
950
951 nvme_process_cq(nvmeq);
952 spin_unlock_irq(&nvmeq->q_lock);
953 return BLK_MQ_RQ_QUEUE_OK;
954
Jens Axboefe543032014-12-11 13:58:39 -0700955 error_cmd:
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200956 nvme_free_iod(dev, iod);
Jens Axboefe543032014-12-11 13:58:39 -0700957 return BLK_MQ_RQ_QUEUE_ERROR;
958 retry_cmd:
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200959 nvme_free_iod(dev, iod);
Jens Axboefe543032014-12-11 13:58:39 -0700960 return BLK_MQ_RQ_QUEUE_BUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500961}
962
Jens Axboea0fa9642015-11-03 20:37:26 -0700963static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500964{
Matthew Wilcox82123462011-01-20 13:24:06 -0500965 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500966
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500967 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500968 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500969
970 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400971 void *ctx;
972 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500973 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500974 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500975 break;
976 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
977 if (++head == nvmeq->q_depth) {
978 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500979 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500980 }
Jens Axboea0fa9642015-11-03 20:37:26 -0700981 if (tag && *tag == cqe.command_id)
982 *tag = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700983 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600984 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500985 }
986
987 /* If the controller ignores the cq head doorbell and continuously
988 * writes to the queue, it is theoretically possible to wrap around
989 * the queue twice and mistakenly return IRQ_NONE. Linux only
990 * requires that 0.1% of your interrupts are handled, so this isn't
991 * a big problem.
992 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500993 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Jens Axboea0fa9642015-11-03 20:37:26 -0700994 return;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500995
Keith Busch604e8c82015-11-20 08:38:13 -0700996 if (likely(nvmeq->cq_vector >= 0))
997 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500998 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500999 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001000
Matthew Wilcoxe9539f42013-06-24 11:47:34 -04001001 nvmeq->cqe_seen = 1;
Jens Axboea0fa9642015-11-03 20:37:26 -07001002}
1003
1004static void nvme_process_cq(struct nvme_queue *nvmeq)
1005{
1006 __nvme_process_cq(nvmeq, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001007}
1008
1009static irqreturn_t nvme_irq(int irq, void *data)
1010{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001011 irqreturn_t result;
1012 struct nvme_queue *nvmeq = data;
1013 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -04001014 nvme_process_cq(nvmeq);
1015 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
1016 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001017 spin_unlock(&nvmeq->q_lock);
1018 return result;
1019}
1020
1021static irqreturn_t nvme_irq_check(int irq, void *data)
1022{
1023 struct nvme_queue *nvmeq = data;
1024 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
1025 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
1026 return IRQ_NONE;
1027 return IRQ_WAKE_THREAD;
1028}
1029
Jens Axboea0fa9642015-11-03 20:37:26 -07001030static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1031{
1032 struct nvme_queue *nvmeq = hctx->driver_data;
1033
1034 if ((le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) ==
1035 nvmeq->cq_phase) {
1036 spin_lock_irq(&nvmeq->q_lock);
1037 __nvme_process_cq(nvmeq, &tag);
1038 spin_unlock_irq(&nvmeq->q_lock);
1039
1040 if (tag == -1)
1041 return 1;
1042 }
1043
1044 return 0;
1045}
1046
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001047static int nvme_submit_async_admin_req(struct nvme_dev *dev)
1048{
1049 struct nvme_queue *nvmeq = dev->queues[0];
1050 struct nvme_command c;
1051 struct nvme_cmd_info *cmd_info;
1052 struct request *req;
1053
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001054 req = blk_mq_alloc_request(dev->admin_q, WRITE,
1055 BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001056 if (IS_ERR(req))
1057 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001058
Keith Buschc917dfe2015-01-07 18:55:48 -07001059 req->cmd_flags |= REQ_NO_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001060 cmd_info = blk_mq_rq_to_pdu(req);
Keith Busch1efccc92015-03-31 10:37:17 -06001061 nvme_set_info(cmd_info, NULL, async_req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001062
1063 memset(&c, 0, sizeof(c));
1064 c.common.opcode = nvme_admin_async_event;
1065 c.common.command_id = req->tag;
1066
Keith Busch42483222015-06-01 09:29:54 -06001067 blk_mq_free_request(req);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301068 __nvme_submit_cmd(nvmeq, &c);
1069 return 0;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001070}
1071
1072static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
Keith Busch4d115422013-12-10 13:10:40 -07001073 struct nvme_command *cmd,
1074 struct async_cmd_info *cmdinfo, unsigned timeout)
1075{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001076 struct nvme_queue *nvmeq = dev->queues[0];
1077 struct request *req;
1078 struct nvme_cmd_info *cmd_rq;
Keith Busch4d115422013-12-10 13:10:40 -07001079
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001080 req = blk_mq_alloc_request(dev->admin_q, WRITE, 0);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001081 if (IS_ERR(req))
1082 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001083
1084 req->timeout = timeout;
1085 cmd_rq = blk_mq_rq_to_pdu(req);
1086 cmdinfo->req = req;
1087 nvme_set_info(cmd_rq, cmdinfo, async_completion);
Keith Busch4d115422013-12-10 13:10:40 -07001088 cmdinfo->status = -EINTR;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001089
1090 cmd->common.command_id = req->tag;
1091
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301092 nvme_submit_cmd(nvmeq, cmd);
1093 return 0;
Keith Busch4d115422013-12-10 13:10:40 -07001094}
1095
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001096static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1097{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001098 struct nvme_command c;
1099
1100 memset(&c, 0, sizeof(c));
1101 c.delete_queue.opcode = opcode;
1102 c.delete_queue.qid = cpu_to_le16(id);
1103
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001104 return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001105}
1106
1107static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1108 struct nvme_queue *nvmeq)
1109{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001110 struct nvme_command c;
1111 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1112
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001113 /*
1114 * Note: we (ab)use the fact the the prp fields survive if no data
1115 * is attached to the request.
1116 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001117 memset(&c, 0, sizeof(c));
1118 c.create_cq.opcode = nvme_admin_create_cq;
1119 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1120 c.create_cq.cqid = cpu_to_le16(qid);
1121 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1122 c.create_cq.cq_flags = cpu_to_le16(flags);
1123 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1124
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001125 return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001126}
1127
1128static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1129 struct nvme_queue *nvmeq)
1130{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001131 struct nvme_command c;
1132 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1133
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001134 /*
1135 * Note: we (ab)use the fact the the prp fields survive if no data
1136 * is attached to the request.
1137 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001138 memset(&c, 0, sizeof(c));
1139 c.create_sq.opcode = nvme_admin_create_sq;
1140 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1141 c.create_sq.sqid = cpu_to_le16(qid);
1142 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1143 c.create_sq.sq_flags = cpu_to_le16(flags);
1144 c.create_sq.cqid = cpu_to_le16(qid);
1145
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001146 return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001147}
1148
1149static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1150{
1151 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1152}
1153
1154static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1155{
1156 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1157}
1158
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001159/**
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001160 * nvme_abort_req - Attempt aborting a request
Keith Buschc30341d2013-12-10 13:10:38 -07001161 *
1162 * Schedule controller reset if the command was already aborted once before and
1163 * still hasn't been returned to the driver, or if this is the admin queue.
1164 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001165static void nvme_abort_req(struct request *req)
Keith Buschc30341d2013-12-10 13:10:38 -07001166{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001167 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1168 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001169 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001170 struct request *abort_req;
1171 struct nvme_cmd_info *abort_cmd;
1172 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -07001173
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001174 if (!nvmeq->qid || cmd_rq->aborted) {
Christoph Hellwig906678922015-10-02 18:49:23 +02001175 spin_lock(&dev_list_lock);
1176 if (!__nvme_reset(dev)) {
1177 dev_warn(dev->dev,
1178 "I/O %d QID %d timeout, reset controller\n",
1179 req->tag, nvmeq->qid);
1180 }
1181 spin_unlock(&dev_list_lock);
Keith Buschc30341d2013-12-10 13:10:38 -07001182 return;
1183 }
1184
1185 if (!dev->abort_limit)
1186 return;
1187
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001188 abort_req = blk_mq_alloc_request(dev->admin_q, WRITE,
1189 BLK_MQ_REQ_NOWAIT);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001190 if (IS_ERR(abort_req))
Keith Buschc30341d2013-12-10 13:10:38 -07001191 return;
1192
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001193 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1194 nvme_set_info(abort_cmd, abort_req, abort_completion);
1195
Keith Buschc30341d2013-12-10 13:10:38 -07001196 memset(&cmd, 0, sizeof(cmd));
1197 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001198 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001199 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001200 cmd.abort.command_id = abort_req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001201
1202 --dev->abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001203 cmd_rq->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -07001204
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001205 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", req->tag,
Keith Buschc30341d2013-12-10 13:10:38 -07001206 nvmeq->qid);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301207 nvme_submit_cmd(dev->queues[0], &cmd);
Keith Buschc30341d2013-12-10 13:10:38 -07001208}
1209
Keith Busch42483222015-06-01 09:29:54 -06001210static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001211{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001212 struct nvme_queue *nvmeq = data;
1213 void *ctx;
1214 nvme_completion_fn fn;
1215 struct nvme_cmd_info *cmd;
Keith Buschcef6a942015-01-07 18:55:51 -07001216 struct nvme_completion cqe;
1217
1218 if (!blk_mq_request_started(req))
1219 return;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001220
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001221 cmd = blk_mq_rq_to_pdu(req);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001222
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001223 if (cmd->ctx == CMD_CTX_CANCELLED)
1224 return;
1225
Keith Buschcef6a942015-01-07 18:55:51 -07001226 if (blk_queue_dying(req->q))
1227 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1228 else
1229 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1230
1231
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001232 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1233 req->tag, nvmeq->qid);
1234 ctx = cancel_cmd_info(cmd, &fn);
1235 fn(nvmeq, ctx, &cqe);
1236}
1237
1238static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
1239{
1240 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
1241 struct nvme_queue *nvmeq = cmd->nvmeq;
1242
Keith Busch07836e62015-02-19 10:34:48 -07001243 dev_warn(nvmeq->q_dmadev, "Timeout I/O %d QID %d\n", req->tag,
1244 nvmeq->qid);
1245 spin_lock_irq(&nvmeq->q_lock);
1246 nvme_abort_req(req);
1247 spin_unlock_irq(&nvmeq->q_lock);
1248
Keith Busch7a509a62015-01-07 18:55:53 -07001249 /*
1250 * The aborted req will be completed on receiving the abort req.
1251 * We enable the timer again. If hit twice, it'll cause a device reset,
1252 * as the device then is in a faulty state.
1253 */
Keith Busch07836e62015-02-19 10:34:48 -07001254 return BLK_EH_RESET_TIMER;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001255}
1256
Keith Buschf435c282014-07-07 09:14:42 -06001257static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001258{
1259 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1260 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001261 if (nvmeq->sq_cmds)
1262 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
Matthew Wilcox9e866772012-08-03 13:55:56 -04001263 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1264 kfree(nvmeq);
1265}
1266
Keith Buscha1a5ef92013-12-16 13:50:00 -05001267static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001268{
1269 int i;
1270
Keith Buscha1a5ef92013-12-16 13:50:00 -05001271 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001272 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001273 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001274 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001275 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001276 }
Keith Busch22404272013-07-15 15:02:20 -06001277}
1278
Keith Busch4d115422013-12-10 13:10:40 -07001279/**
1280 * nvme_suspend_queue - put queue into suspended state
1281 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001282 */
1283static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001284{
Keith Busch2b25d982014-12-22 12:59:04 -07001285 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001286
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001287 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001288 if (nvmeq->cq_vector == -1) {
1289 spin_unlock_irq(&nvmeq->q_lock);
1290 return 1;
1291 }
1292 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Keith Busch42f61422014-03-24 10:46:25 -06001293 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001294 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001295 spin_unlock_irq(&nvmeq->q_lock);
1296
Keith Busch6df3dbc2015-03-26 13:49:33 -06001297 if (!nvmeq->qid && nvmeq->dev->admin_q)
1298 blk_mq_freeze_queue_start(nvmeq->dev->admin_q);
1299
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001300 irq_set_affinity_hint(vector, NULL);
1301 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001302
Keith Busch4d115422013-12-10 13:10:40 -07001303 return 0;
1304}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001305
Keith Busch4d115422013-12-10 13:10:40 -07001306static void nvme_clear_queue(struct nvme_queue *nvmeq)
1307{
Keith Busch22404272013-07-15 15:02:20 -06001308 spin_lock_irq(&nvmeq->q_lock);
Keith Busch42483222015-06-01 09:29:54 -06001309 if (nvmeq->tags && *nvmeq->tags)
1310 blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001311 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001312}
1313
Keith Busch4d115422013-12-10 13:10:40 -07001314static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1315{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001316 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001317
1318 if (!nvmeq)
1319 return;
1320 if (nvme_suspend_queue(nvmeq))
1321 return;
1322
Keith Busch0e53d182013-12-10 13:10:39 -07001323 /* Don't tell the adapter to delete the admin queue.
1324 * Don't tell a removed adapter to delete IO queues. */
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001325 if (qid && readl(dev->bar + NVME_REG_CSTS) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001326 adapter_delete_sq(dev, qid);
1327 adapter_delete_cq(dev, qid);
1328 }
Keith Busch07836e62015-02-19 10:34:48 -07001329
1330 spin_lock_irq(&nvmeq->q_lock);
1331 nvme_process_cq(nvmeq);
1332 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001333}
1334
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001335static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1336 int entry_size)
1337{
1338 int q_depth = dev->q_depth;
1339 unsigned q_size_aligned = roundup(q_depth * entry_size, dev->page_size);
1340
1341 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001342 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
1343 mem_per_q = round_down(mem_per_q, dev->page_size);
1344 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001345
1346 /*
1347 * Ensure the reduced q_depth is above some threshold where it
1348 * would be better to map queues in system memory with the
1349 * original depth
1350 */
1351 if (q_depth < 64)
1352 return -ENOMEM;
1353 }
1354
1355 return q_depth;
1356}
1357
1358static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1359 int qid, int depth)
1360{
1361 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
1362 unsigned offset = (qid - 1) *
1363 roundup(SQ_SIZE(depth), dev->page_size);
1364 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1365 nvmeq->sq_cmds_io = dev->cmb + offset;
1366 } else {
1367 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1368 &nvmeq->sq_dma_addr, GFP_KERNEL);
1369 if (!nvmeq->sq_cmds)
1370 return -ENOMEM;
1371 }
1372
1373 return 0;
1374}
1375
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001376static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Keith Busch2b25d982014-12-22 12:59:04 -07001377 int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001378{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001379 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001380 if (!nvmeq)
1381 return NULL;
1382
Christoph Hellwige75ec752015-05-22 11:12:39 +02001383 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001384 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001385 if (!nvmeq->cqes)
1386 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001387
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001388 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001389 goto free_cqdma;
1390
Christoph Hellwige75ec752015-05-22 11:12:39 +02001391 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001392 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001393 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1394 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001395 spin_lock_init(&nvmeq->q_lock);
1396 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001397 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001398 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001399 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001400 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001401 nvmeq->cq_vector = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001402 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001403
Jon Derrick36a7e992015-05-27 12:26:23 -06001404 /* make sure queue descriptor is set before queue count, for kthread */
1405 mb();
1406 dev->queue_count++;
1407
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001408 return nvmeq;
1409
1410 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001411 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001412 nvmeq->cq_dma_addr);
1413 free_nvmeq:
1414 kfree(nvmeq);
1415 return NULL;
1416}
1417
Matthew Wilcox30010822011-01-20 09:10:15 -05001418static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1419 const char *name)
1420{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001421 if (use_threaded_interrupts)
1422 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001423 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001424 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001425 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001426 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001427}
1428
Keith Busch22404272013-07-15 15:02:20 -06001429static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001430{
Keith Busch22404272013-07-15 15:02:20 -06001431 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001432
Keith Busch7be50e92014-09-10 15:48:47 -06001433 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001434 nvmeq->sq_tail = 0;
1435 nvmeq->cq_head = 0;
1436 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001437 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001438 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001439 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001440 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001441}
1442
1443static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1444{
1445 struct nvme_dev *dev = nvmeq->dev;
1446 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001447
Keith Busch2b25d982014-12-22 12:59:04 -07001448 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001449 result = adapter_alloc_cq(dev, qid, nvmeq);
1450 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001451 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001452
1453 result = adapter_alloc_sq(dev, qid, nvmeq);
1454 if (result < 0)
1455 goto release_cq;
1456
Matthew Wilcox3193f072014-01-27 15:57:22 -05001457 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001458 if (result < 0)
1459 goto release_sq;
1460
Keith Busch22404272013-07-15 15:02:20 -06001461 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001462 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001463
1464 release_sq:
1465 adapter_delete_sq(dev, qid);
1466 release_cq:
1467 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001468 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001469}
1470
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001471static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1472{
1473 unsigned long timeout;
1474 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1475
1476 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1477
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001478 while ((readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_RDY) != bit) {
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001479 msleep(100);
1480 if (fatal_signal_pending(current))
1481 return -EINTR;
1482 if (time_after(jiffies, timeout)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001483 dev_err(dev->dev,
Matthew Wilcox27e81662014-04-11 11:58:45 -04001484 "Device not ready; aborting %s\n", enabled ?
1485 "initialisation" : "reset");
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001486 return -ENODEV;
1487 }
1488 }
1489
1490 return 0;
1491}
1492
1493/*
1494 * If the device has been passed off to us in an enabled state, just clear
1495 * the enabled bit. The spec says we should set the 'shutdown notification
1496 * bits', but doing so may cause the device to complete commands to the
1497 * admin queue ... and we don't know what memory that might be pointing at!
1498 */
1499static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1500{
Dan McLeran01079522014-06-23 08:24:36 -06001501 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1502 dev->ctrl_config &= ~NVME_CC_ENABLE;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001503 writel(dev->ctrl_config, dev->bar + NVME_REG_CC);
Matthew Wilcox44af1462013-05-04 06:43:17 -04001504
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001505 return nvme_wait_ready(dev, cap, false);
1506}
1507
1508static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1509{
Dan McLeran01079522014-06-23 08:24:36 -06001510 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1511 dev->ctrl_config |= NVME_CC_ENABLE;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001512 writel(dev->ctrl_config, dev->bar + NVME_REG_CC);
Dan McLeran01079522014-06-23 08:24:36 -06001513
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001514 return nvme_wait_ready(dev, cap, true);
1515}
1516
Keith Busch1894d8f2013-07-15 15:02:22 -06001517static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1518{
1519 unsigned long timeout;
Keith Busch1894d8f2013-07-15 15:02:22 -06001520
Dan McLeran01079522014-06-23 08:24:36 -06001521 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1522 dev->ctrl_config |= NVME_CC_SHN_NORMAL;
1523
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001524 writel(dev->ctrl_config, dev->bar + NVME_REG_CC);
Keith Busch1894d8f2013-07-15 15:02:22 -06001525
Dan McLeran2484f402014-07-01 09:33:32 -06001526 timeout = SHUTDOWN_TIMEOUT + jiffies;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001527 while ((readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_SHST_MASK) !=
Keith Busch1894d8f2013-07-15 15:02:22 -06001528 NVME_CSTS_SHST_CMPLT) {
1529 msleep(100);
1530 if (fatal_signal_pending(current))
1531 return -EINTR;
1532 if (time_after(jiffies, timeout)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001533 dev_err(dev->dev,
Keith Busch1894d8f2013-07-15 15:02:22 -06001534 "Device shutdown incomplete; abort shutdown\n");
1535 return -ENODEV;
1536 }
1537 }
1538
1539 return 0;
1540}
1541
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001542static struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001543 .queue_rq = nvme_queue_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001544 .map_queue = blk_mq_map_queue,
1545 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001546 .exit_hctx = nvme_admin_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001547 .init_request = nvme_admin_init_request,
1548 .timeout = nvme_timeout,
1549};
1550
1551static struct blk_mq_ops nvme_mq_ops = {
1552 .queue_rq = nvme_queue_rq,
1553 .map_queue = blk_mq_map_queue,
1554 .init_hctx = nvme_init_hctx,
1555 .init_request = nvme_init_request,
1556 .timeout = nvme_timeout,
Jens Axboea0fa9642015-11-03 20:37:26 -07001557 .poll = nvme_poll,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001558};
1559
Keith Buschea191d22015-01-07 18:55:49 -07001560static void nvme_dev_remove_admin(struct nvme_dev *dev)
1561{
1562 if (dev->admin_q && !blk_queue_dying(dev->admin_q)) {
1563 blk_cleanup_queue(dev->admin_q);
1564 blk_mq_free_tag_set(&dev->admin_tagset);
1565 }
1566}
1567
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001568static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1569{
1570 if (!dev->admin_q) {
1571 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1572 dev->admin_tagset.nr_hw_queues = 1;
1573 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1;
Keith Busch1efccc92015-03-31 10:37:17 -06001574 dev->admin_tagset.reserved_tags = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001575 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001576 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001577 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001578 dev->admin_tagset.driver_data = dev;
1579
1580 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1581 return -ENOMEM;
1582
1583 dev->admin_q = blk_mq_init_queue(&dev->admin_tagset);
Ming Lei35b489d2015-01-02 14:25:27 +00001584 if (IS_ERR(dev->admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001585 blk_mq_free_tag_set(&dev->admin_tagset);
1586 return -ENOMEM;
1587 }
Keith Buschea191d22015-01-07 18:55:49 -07001588 if (!blk_get_queue(dev->admin_q)) {
1589 nvme_dev_remove_admin(dev);
Keith Busch4af0e212015-06-08 10:08:13 -06001590 dev->admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001591 return -ENODEV;
1592 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001593 } else
1594 blk_mq_unfreeze_queue(dev->admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001595
1596 return 0;
1597}
1598
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001599static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001600{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001601 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001602 u32 aqa;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001603 u64 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001604 struct nvme_queue *nvmeq;
Nishanth Aravamudanc5c9f252015-11-24 09:55:05 -07001605 /*
1606 * default to a 4K page size, with the intention to update this
1607 * path in the future to accomodate architectures with differing
1608 * kernel and IO page sizes.
1609 */
1610 unsigned page_shift = 12;
Keith Busch1d090622014-06-23 11:34:01 -06001611 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12;
Keith Busch1d090622014-06-23 11:34:01 -06001612
1613 if (page_shift < dev_page_min) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001614 dev_err(dev->dev,
Keith Busch1d090622014-06-23 11:34:01 -06001615 "Minimum device page size (%u) too large for "
1616 "host (%u)\n", 1 << dev_page_min,
1617 1 << page_shift);
1618 return -ENODEV;
1619 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001620
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001621 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1) ?
Keith Buschdfbac8c2015-08-10 15:20:40 -06001622 NVME_CAP_NSSRC(cap) : 0;
1623
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001624 if (dev->subsystem &&
1625 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1626 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001627
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001628 result = nvme_disable_ctrl(dev, cap);
1629 if (result < 0)
1630 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001631
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001632 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001633 if (!nvmeq) {
Keith Busch2b25d982014-12-22 12:59:04 -07001634 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001635 if (!nvmeq)
1636 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001637 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001638
1639 aqa = nvmeq->q_depth - 1;
1640 aqa |= aqa << 16;
1641
Keith Busch1d090622014-06-23 11:34:01 -06001642 dev->page_size = 1 << page_shift;
1643
Dan McLeran01079522014-06-23 08:24:36 -06001644 dev->ctrl_config = NVME_CC_CSS_NVM;
Keith Busch1d090622014-06-23 11:34:01 -06001645 dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001646 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001647 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001648
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001649 writel(aqa, dev->bar + NVME_REG_AQA);
1650 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1651 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001652
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001653 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001654 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001655 goto free_nvmeq;
1656
Keith Busch2b25d982014-12-22 12:59:04 -07001657 nvmeq->cq_vector = 0;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001658 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001659 if (result) {
1660 nvmeq->cq_vector = -1;
Keith Busch0fb59cb2015-01-07 18:55:50 -07001661 goto free_nvmeq;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001662 }
Keith Busch025c5572013-05-01 13:07:51 -06001663
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001664 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001665
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001666 free_nvmeq:
1667 nvme_free_queues(dev, 0);
1668 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001669}
1670
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001671static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1672{
1673 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001674 struct nvme_user_io io;
1675 struct nvme_command c;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001676 unsigned length, meta_len;
Keith Buscha67a9512015-04-07 16:57:19 -06001677 int status, write;
Keith Buscha67a9512015-04-07 16:57:19 -06001678 dma_addr_t meta_dma = 0;
1679 void *meta = NULL;
Arnd Bergmannfec558b2015-05-19 17:05:40 +02001680 void __user *metadata;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001681
1682 if (copy_from_user(&io, uio, sizeof(io)))
1683 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001684
1685 switch (io.opcode) {
1686 case nvme_cmd_write:
1687 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001688 case nvme_cmd_compare:
Matthew Wilcox64132142011-08-09 12:56:37 -04001689 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001690 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001691 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001692 }
1693
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001694 length = (io.nblocks + 1) << ns->lba_shift;
1695 meta_len = (io.nblocks + 1) * ns->ms;
Arnd Bergmann835da3f2015-10-06 22:29:48 +02001696 metadata = (void __user *)(uintptr_t)io.metadata;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001697 write = io.opcode & 1;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001698
Keith Busch71feb362015-06-19 11:07:30 -06001699 if (ns->ext) {
1700 length += meta_len;
1701 meta_len = 0;
Keith Buscha67a9512015-04-07 16:57:19 -06001702 }
1703 if (meta_len) {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001704 if (((io.metadata & 3) || !io.metadata) && !ns->ext)
1705 return -EINVAL;
1706
Christoph Hellwige75ec752015-05-22 11:12:39 +02001707 meta = dma_alloc_coherent(dev->dev, meta_len,
Keith Buscha67a9512015-04-07 16:57:19 -06001708 &meta_dma, GFP_KERNEL);
Arnd Bergmannfec558b2015-05-19 17:05:40 +02001709
Keith Buscha67a9512015-04-07 16:57:19 -06001710 if (!meta) {
1711 status = -ENOMEM;
1712 goto unmap;
1713 }
1714 if (write) {
Arnd Bergmannfec558b2015-05-19 17:05:40 +02001715 if (copy_from_user(meta, metadata, meta_len)) {
Keith Buscha67a9512015-04-07 16:57:19 -06001716 status = -EFAULT;
1717 goto unmap;
1718 }
1719 }
1720 }
1721
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001722 memset(&c, 0, sizeof(c));
1723 c.rw.opcode = io.opcode;
1724 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001725 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001726 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001727 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001728 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001729 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1730 c.rw.reftag = cpu_to_le32(io.reftag);
1731 c.rw.apptag = cpu_to_le16(io.apptag);
1732 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buscha67a9512015-04-07 16:57:19 -06001733 c.rw.metadata = cpu_to_le64(meta_dma);
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001734
1735 status = __nvme_submit_sync_cmd(ns->queue, &c, NULL,
Arnd Bergmann835da3f2015-10-06 22:29:48 +02001736 (void __user *)(uintptr_t)io.addr, length, NULL, 0);
Keith Buschf410c682013-04-23 17:23:59 -06001737 unmap:
Keith Buscha67a9512015-04-07 16:57:19 -06001738 if (meta) {
1739 if (status == NVME_SC_SUCCESS && !write) {
Arnd Bergmannfec558b2015-05-19 17:05:40 +02001740 if (copy_to_user(metadata, meta, meta_len))
Keith Buscha67a9512015-04-07 16:57:19 -06001741 status = -EFAULT;
1742 }
Christoph Hellwige75ec752015-05-22 11:12:39 +02001743 dma_free_coherent(dev->dev, meta_len, meta, meta_dma);
Keith Buschf410c682013-04-23 17:23:59 -06001744 }
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001745 return status;
1746}
1747
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001748static int nvme_user_cmd(struct nvme_dev *dev, struct nvme_ns *ns,
1749 struct nvme_passthru_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001750{
Keith Busch7963e522014-09-12 16:07:20 -06001751 struct nvme_passthru_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001752 struct nvme_command c;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001753 unsigned timeout = 0;
1754 int status;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001755
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001756 if (!capable(CAP_SYS_ADMIN))
1757 return -EACCES;
1758 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001759 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001760
1761 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001762 c.common.opcode = cmd.opcode;
1763 c.common.flags = cmd.flags;
1764 c.common.nsid = cpu_to_le32(cmd.nsid);
1765 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1766 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1767 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1768 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1769 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1770 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1771 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1772 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1773
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001774 if (cmd.timeout_ms)
1775 timeout = msecs_to_jiffies(cmd.timeout_ms);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001776
Christoph Hellwigf705f832015-05-22 11:12:38 +02001777 status = __nvme_submit_sync_cmd(ns ? ns->queue : dev->admin_q, &c,
Arnd Bergmann835da3f2015-10-06 22:29:48 +02001778 NULL, (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001779 &cmd.result, timeout);
1780 if (status >= 0) {
1781 if (put_user(cmd.result, &ucmd->result))
1782 return -EFAULT;
Keith Buschedd10d32014-04-03 16:45:23 -06001783 }
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001784
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001785 return status;
1786}
1787
Jon Derrick81f03fe2015-08-10 15:20:41 -06001788static int nvme_subsys_reset(struct nvme_dev *dev)
1789{
1790 if (!dev->subsystem)
1791 return -ENOTTY;
1792
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001793 writel(0x4E564D65, dev->bar + NVME_REG_NSSR); /* "NVMe" */
Jon Derrick81f03fe2015-08-10 15:20:41 -06001794 return 0;
1795}
1796
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001797static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1798 unsigned long arg)
1799{
1800 struct nvme_ns *ns = bdev->bd_disk->private_data;
1801
1802 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001803 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001804 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001805 return ns->ns_id;
1806 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001807 return nvme_user_cmd(ns->dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06001808 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001809 return nvme_user_cmd(ns->dev, ns, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001810 case NVME_IOCTL_SUBMIT_IO:
1811 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001812 case SG_GET_VERSION_NUM:
1813 return nvme_sg_get_version_num((void __user *)arg);
1814 case SG_IO:
1815 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001816 default:
1817 return -ENOTTY;
1818 }
1819}
1820
Keith Busch320a3822013-10-23 13:07:34 -06001821#ifdef CONFIG_COMPAT
1822static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1823 unsigned int cmd, unsigned long arg)
1824{
Keith Busch320a3822013-10-23 13:07:34 -06001825 switch (cmd) {
1826 case SG_IO:
Keith Busche1797292014-08-27 13:55:38 -06001827 return -ENOIOCTLCMD;
Keith Busch320a3822013-10-23 13:07:34 -06001828 }
1829 return nvme_ioctl(bdev, mode, cmd, arg);
1830}
1831#else
1832#define nvme_compat_ioctl NULL
1833#endif
1834
Keith Busch5105aa52015-10-02 10:37:28 -06001835static void nvme_free_dev(struct kref *kref);
Keith Busch188c3562015-10-01 17:14:10 -06001836static void nvme_free_ns(struct kref *kref)
1837{
1838 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
1839
Matias Bjørlingca064082015-10-29 17:57:29 +09001840 if (ns->type == NVME_NS_LIGHTNVM)
1841 nvme_nvm_unregister(ns->queue, ns->disk->disk_name);
1842
Keith Busch188c3562015-10-01 17:14:10 -06001843 spin_lock(&dev_list_lock);
1844 ns->disk->private_data = NULL;
1845 spin_unlock(&dev_list_lock);
1846
Keith Busch5105aa52015-10-02 10:37:28 -06001847 kref_put(&ns->dev->kref, nvme_free_dev);
Keith Busch188c3562015-10-01 17:14:10 -06001848 put_disk(ns->disk);
1849 kfree(ns);
1850}
1851
Keith Busch9ac27092014-01-31 16:53:39 -07001852static int nvme_open(struct block_device *bdev, fmode_t mode)
1853{
Keith Busch9e603522014-10-03 11:15:47 -06001854 int ret = 0;
1855 struct nvme_ns *ns;
Keith Busch9ac27092014-01-31 16:53:39 -07001856
Keith Busch9e603522014-10-03 11:15:47 -06001857 spin_lock(&dev_list_lock);
1858 ns = bdev->bd_disk->private_data;
1859 if (!ns)
1860 ret = -ENXIO;
Keith Busch188c3562015-10-01 17:14:10 -06001861 else if (!kref_get_unless_zero(&ns->kref))
Keith Busch9e603522014-10-03 11:15:47 -06001862 ret = -ENXIO;
1863 spin_unlock(&dev_list_lock);
1864
1865 return ret;
Keith Busch9ac27092014-01-31 16:53:39 -07001866}
1867
Keith Busch9ac27092014-01-31 16:53:39 -07001868static void nvme_release(struct gendisk *disk, fmode_t mode)
1869{
1870 struct nvme_ns *ns = disk->private_data;
Keith Busch188c3562015-10-01 17:14:10 -06001871 kref_put(&ns->kref, nvme_free_ns);
Keith Busch9ac27092014-01-31 16:53:39 -07001872}
1873
Keith Busch4cc09e22014-04-02 15:45:37 -06001874static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
1875{
1876 /* some standard values */
1877 geo->heads = 1 << 6;
1878 geo->sectors = 1 << 5;
1879 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1880 return 0;
1881}
1882
Keith Busche1e5e562015-02-19 13:39:03 -07001883static void nvme_config_discard(struct nvme_ns *ns)
1884{
1885 u32 logical_block_size = queue_logical_block_size(ns->queue);
1886 ns->queue->limits.discard_zeroes_data = 0;
1887 ns->queue->limits.discard_alignment = logical_block_size;
1888 ns->queue->limits.discard_granularity = logical_block_size;
Jens Axboe2bb4cd52015-07-14 08:15:12 -06001889 blk_queue_max_discard_sectors(ns->queue, 0xffffffff);
Keith Busche1e5e562015-02-19 13:39:03 -07001890 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1891}
1892
Keith Busch1b9dbf72014-09-10 17:21:14 -06001893static int nvme_revalidate_disk(struct gendisk *disk)
1894{
1895 struct nvme_ns *ns = disk->private_data;
1896 struct nvme_dev *dev = ns->dev;
1897 struct nvme_id_ns *id;
Keith Buscha67a9512015-04-07 16:57:19 -06001898 u8 lbaf, pi_type;
1899 u16 old_ms;
Keith Busche1e5e562015-02-19 13:39:03 -07001900 unsigned short bs;
Keith Busch1b9dbf72014-09-10 17:21:14 -06001901
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001902 if (nvme_identify_ns(dev, ns->ns_id, &id)) {
Keith Buscha5768aa2015-06-01 14:28:14 -06001903 dev_warn(dev->dev, "%s: Identify failure nvme%dn%d\n", __func__,
1904 dev->instance, ns->ns_id);
1905 return -ENODEV;
Keith Busch1b9dbf72014-09-10 17:21:14 -06001906 }
Keith Buscha5768aa2015-06-01 14:28:14 -06001907 if (id->ncap == 0) {
1908 kfree(id);
1909 return -ENODEV;
Keith Busche1e5e562015-02-19 13:39:03 -07001910 }
Keith Busch1b9dbf72014-09-10 17:21:14 -06001911
Matias Bjørlingca064082015-10-29 17:57:29 +09001912 if (nvme_nvm_ns_supported(ns, id) && ns->type != NVME_NS_LIGHTNVM) {
1913 if (nvme_nvm_register(ns->queue, disk->disk_name)) {
1914 dev_warn(dev->dev,
1915 "%s: LightNVM init failure\n", __func__);
1916 kfree(id);
1917 return -ENODEV;
1918 }
1919 ns->type = NVME_NS_LIGHTNVM;
1920 }
1921
Keith Busche1e5e562015-02-19 13:39:03 -07001922 old_ms = ns->ms;
1923 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
Keith Busch1b9dbf72014-09-10 17:21:14 -06001924 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Busche1e5e562015-02-19 13:39:03 -07001925 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Buscha67a9512015-04-07 16:57:19 -06001926 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
Keith Busch1b9dbf72014-09-10 17:21:14 -06001927
Keith Busche1e5e562015-02-19 13:39:03 -07001928 /*
1929 * If identify namespace failed, use default 512 byte block size so
1930 * block layer can use before failing read/write for 0 capacity.
1931 */
1932 if (ns->lba_shift == 0)
1933 ns->lba_shift = 9;
1934 bs = 1 << ns->lba_shift;
1935
1936 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
1937 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
1938 id->dps & NVME_NS_DPS_PI_MASK : 0;
1939
Dan Williams4cfc7662015-10-21 13:20:07 -04001940 blk_mq_freeze_queue(disk->queue);
Keith Busch52b68d72015-02-23 09:16:21 -07001941 if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
1942 ns->ms != old_ms ||
Keith Busche1e5e562015-02-19 13:39:03 -07001943 bs != queue_logical_block_size(disk->queue) ||
Keith Buscha67a9512015-04-07 16:57:19 -06001944 (ns->ms && ns->ext)))
Keith Busche1e5e562015-02-19 13:39:03 -07001945 blk_integrity_unregister(disk);
1946
1947 ns->pi_type = pi_type;
1948 blk_queue_logical_block_size(ns->queue, bs);
1949
Martin K. Petersen25520d52015-10-21 13:19:49 -04001950 if (ns->ms && !ns->ext)
Keith Busche1e5e562015-02-19 13:39:03 -07001951 nvme_init_integrity(ns);
1952
Matias Bjørlingca064082015-10-29 17:57:29 +09001953 if ((ns->ms && !(ns->ms == 8 && ns->pi_type) &&
1954 !blk_get_integrity(disk)) ||
1955 ns->type == NVME_NS_LIGHTNVM)
Keith Busche1e5e562015-02-19 13:39:03 -07001956 set_capacity(disk, 0);
1957 else
1958 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1959
1960 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1961 nvme_config_discard(ns);
Dan Williams4cfc7662015-10-21 13:20:07 -04001962 blk_mq_unfreeze_queue(disk->queue);
Keith Busche1e5e562015-02-19 13:39:03 -07001963
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001964 kfree(id);
Keith Busch1b9dbf72014-09-10 17:21:14 -06001965 return 0;
1966}
1967
Keith Busch1d277a62015-10-15 14:10:52 +02001968static char nvme_pr_type(enum pr_type type)
1969{
1970 switch (type) {
1971 case PR_WRITE_EXCLUSIVE:
1972 return 1;
1973 case PR_EXCLUSIVE_ACCESS:
1974 return 2;
1975 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1976 return 3;
1977 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1978 return 4;
1979 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1980 return 5;
1981 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1982 return 6;
1983 default:
1984 return 0;
1985 }
1986};
1987
1988static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1989 u64 key, u64 sa_key, u8 op)
1990{
1991 struct nvme_ns *ns = bdev->bd_disk->private_data;
1992 struct nvme_command c;
1993 u8 data[16] = { 0, };
1994
1995 put_unaligned_le64(key, &data[0]);
1996 put_unaligned_le64(sa_key, &data[8]);
1997
1998 memset(&c, 0, sizeof(c));
1999 c.common.opcode = op;
Christoph Hellwiga6dd1022015-10-22 12:01:05 +02002000 c.common.nsid = cpu_to_le32(ns->ns_id);
2001 c.common.cdw10[0] = cpu_to_le32(cdw10);
Keith Busch1d277a62015-10-15 14:10:52 +02002002
2003 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
2004}
2005
2006static int nvme_pr_register(struct block_device *bdev, u64 old,
2007 u64 new, unsigned flags)
2008{
2009 u32 cdw10;
2010
2011 if (flags & ~PR_FL_IGNORE_KEY)
2012 return -EOPNOTSUPP;
2013
2014 cdw10 = old ? 2 : 0;
2015 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
2016 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
2017 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
2018}
2019
2020static int nvme_pr_reserve(struct block_device *bdev, u64 key,
2021 enum pr_type type, unsigned flags)
2022{
2023 u32 cdw10;
2024
2025 if (flags & ~PR_FL_IGNORE_KEY)
2026 return -EOPNOTSUPP;
2027
2028 cdw10 = nvme_pr_type(type) << 8;
2029 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
2030 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
2031}
2032
2033static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
2034 enum pr_type type, bool abort)
2035{
2036 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
2037 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
2038}
2039
2040static int nvme_pr_clear(struct block_device *bdev, u64 key)
2041{
Dan Carpenter73fcf4e2015-11-03 22:50:49 +03002042 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Keith Busch1d277a62015-10-15 14:10:52 +02002043 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
2044}
2045
2046static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
2047{
2048 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
2049 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
2050}
2051
2052static const struct pr_ops nvme_pr_ops = {
2053 .pr_register = nvme_pr_register,
2054 .pr_reserve = nvme_pr_reserve,
2055 .pr_release = nvme_pr_release,
2056 .pr_preempt = nvme_pr_preempt,
2057 .pr_clear = nvme_pr_clear,
2058};
2059
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002060static const struct block_device_operations nvme_fops = {
2061 .owner = THIS_MODULE,
2062 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06002063 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07002064 .open = nvme_open,
2065 .release = nvme_release,
Keith Busch4cc09e22014-04-02 15:45:37 -06002066 .getgeo = nvme_getgeo,
Keith Busch1b9dbf72014-09-10 17:21:14 -06002067 .revalidate_disk= nvme_revalidate_disk,
Keith Busch1d277a62015-10-15 14:10:52 +02002068 .pr_ops = &nvme_pr_ops,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002069};
2070
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002071static int nvme_kthread(void *data)
2072{
Keith Buschd4b4ff82013-12-10 13:10:37 -07002073 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002074
2075 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04002076 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002077 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07002078 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002079 int i;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002080 u32 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06002081
2082 if ((dev->subsystem && (csts & NVME_CSTS_NSSRO)) ||
2083 csts & NVME_CSTS_CFS) {
Christoph Hellwig906678922015-10-02 18:49:23 +02002084 if (!__nvme_reset(dev)) {
2085 dev_warn(dev->dev,
2086 "Failed status: %x, reset controller\n",
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002087 readl(dev->bar + NVME_REG_CSTS));
Christoph Hellwig906678922015-10-02 18:49:23 +02002088 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002089 continue;
2090 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002091 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002092 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05002093 if (!nvmeq)
2094 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002095 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04002096 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06002097
2098 while ((i == 0) && (dev->event_limit > 0)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002099 if (nvme_submit_async_admin_req(dev))
Keith Busch6fccf932014-06-18 13:58:57 -06002100 break;
2101 dev->event_limit--;
2102 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002103 spin_unlock_irq(&nvmeq->q_lock);
2104 }
2105 }
2106 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08002107 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002108 }
2109 return 0;
2110}
2111
Keith Busche1e5e562015-02-19 13:39:03 -07002112static void nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002113{
2114 struct nvme_ns *ns;
2115 struct gendisk *disk;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002116 int node = dev_to_node(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002117
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002118 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002119 if (!ns)
Keith Busche1e5e562015-02-19 13:39:03 -07002120 return;
2121
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002122 ns->queue = blk_mq_init_queue(&dev->tagset);
Dan Carpenter9f173b32014-11-05 23:39:09 +03002123 if (IS_ERR(ns->queue))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002124 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07002125 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
2126 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002127 ns->dev = dev;
2128 ns->queue->queuedata = ns;
2129
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002130 disk = alloc_disk_node(0, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002131 if (!disk)
2132 goto out_free_queue;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002133
Keith Busch188c3562015-10-01 17:14:10 -06002134 kref_init(&ns->kref);
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002135 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002136 ns->disk = disk;
Keith Busche1e5e562015-02-19 13:39:03 -07002137 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
2138 list_add_tail(&ns->list, &dev->namespaces);
2139
Keith Busche9ef4632012-07-24 15:01:04 -06002140 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busche8244102015-08-12 16:17:54 -06002141 if (dev->max_hw_sectors) {
Keith Busch8fc23e02012-07-26 11:29:57 -06002142 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Keith Busche8244102015-08-12 16:17:54 -06002143 blk_queue_max_segments(ns->queue,
Keith Busch6824c5e2015-11-18 16:33:08 -07002144 (dev->max_hw_sectors / (dev->page_size >> 9)) + 1);
Keith Busche8244102015-08-12 16:17:54 -06002145 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002146 if (dev->stripe_size)
2147 blk_queue_chunk_sectors(ns->queue, dev->stripe_size >> 9);
Keith Buscha7d2ce22014-04-29 11:41:28 -06002148 if (dev->vwc & NVME_CTRL_VWC_PRESENT)
2149 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
Keith Busch03100aa2015-08-19 14:24:05 -07002150 blk_queue_virt_boundary(ns->queue, dev->page_size - 1);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002151
2152 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002153 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002154 disk->fops = &nvme_fops;
2155 disk->private_data = ns;
2156 disk->queue = ns->queue;
Keith Buschb3fffde2015-02-03 11:21:42 -07002157 disk->driverfs_dev = dev->device;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002158 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002159 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002160
Keith Busche1e5e562015-02-19 13:39:03 -07002161 /*
2162 * Initialize capacity to 0 until we establish the namespace format and
2163 * setup integrity extentions if necessary. The revalidate_disk after
2164 * add_disk allows the driver to register with integrity if the format
2165 * requires it.
2166 */
2167 set_capacity(disk, 0);
Keith Buscha5768aa2015-06-01 14:28:14 -06002168 if (nvme_revalidate_disk(ns->disk))
2169 goto out_free_disk;
2170
Keith Busch5105aa52015-10-02 10:37:28 -06002171 kref_get(&dev->kref);
Matias Bjørlingca064082015-10-29 17:57:29 +09002172 if (ns->type != NVME_NS_LIGHTNVM) {
2173 add_disk(ns->disk);
2174 if (ns->ms) {
2175 struct block_device *bd = bdget_disk(ns->disk, 0);
2176 if (!bd)
2177 return;
2178 if (blkdev_get(bd, FMODE_READ, NULL)) {
2179 bdput(bd);
2180 return;
2181 }
2182 blkdev_reread_part(bd);
2183 blkdev_put(bd, FMODE_READ);
Keith Busch7bee6072015-07-14 11:57:48 -06002184 }
Keith Busch7bee6072015-07-14 11:57:48 -06002185 }
Keith Busche1e5e562015-02-19 13:39:03 -07002186 return;
Keith Buscha5768aa2015-06-01 14:28:14 -06002187 out_free_disk:
2188 kfree(disk);
2189 list_del(&ns->list);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002190 out_free_queue:
2191 blk_cleanup_queue(ns->queue);
2192 out_free_ns:
2193 kfree(ns);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002194}
2195
Christoph Hellwig2659e572015-10-02 18:51:31 +02002196/*
2197 * Create I/O queues. Failing to create an I/O queue is not an issue,
2198 * we can continue with less than the desired amount of queues, and
2199 * even a controller without I/O queues an still be used to issue
2200 * admin commands. This might be useful to upgrade a buggy firmware
2201 * for example.
2202 */
Keith Busch42f61422014-03-24 10:46:25 -06002203static void nvme_create_io_queues(struct nvme_dev *dev)
2204{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002205 unsigned i;
Keith Busch42f61422014-03-24 10:46:25 -06002206
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002207 for (i = dev->queue_count; i <= dev->max_qid; i++)
Keith Busch2b25d982014-12-22 12:59:04 -07002208 if (!nvme_alloc_queue(dev, i, dev->q_depth))
Keith Busch42f61422014-03-24 10:46:25 -06002209 break;
2210
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002211 for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
Christoph Hellwig2659e572015-10-02 18:51:31 +02002212 if (nvme_create_queue(dev->queues[i], i)) {
2213 nvme_free_queues(dev, i);
Keith Busch42f61422014-03-24 10:46:25 -06002214 break;
Christoph Hellwig2659e572015-10-02 18:51:31 +02002215 }
Keith Busch42f61422014-03-24 10:46:25 -06002216}
2217
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002218static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002219{
2220 int status;
2221 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002222 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002223
Matthew Wilcoxdf348132012-01-11 07:29:56 -07002224 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002225 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04002226 if (status < 0)
2227 return status;
2228 if (status > 0) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002229 dev_err(dev->dev, "Could not set queue count (%d)\n", status);
Keith Buschbadc34d2014-06-23 14:25:35 -06002230 return 0;
Matthew Wilcox27e81662014-04-11 11:58:45 -04002231 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002232 return min(result & 0xffff, result >> 16) + 1;
2233}
2234
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002235static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
2236{
2237 u64 szu, size, offset;
2238 u32 cmbloc;
2239 resource_size_t bar_size;
2240 struct pci_dev *pdev = to_pci_dev(dev->dev);
2241 void __iomem *cmb;
2242 dma_addr_t dma_addr;
2243
2244 if (!use_cmb_sqes)
2245 return NULL;
2246
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002247 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002248 if (!(NVME_CMB_SZ(dev->cmbsz)))
2249 return NULL;
2250
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002251 cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002252
2253 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
2254 size = szu * NVME_CMB_SZ(dev->cmbsz);
2255 offset = szu * NVME_CMB_OFST(cmbloc);
2256 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(cmbloc));
2257
2258 if (offset > bar_size)
2259 return NULL;
2260
2261 /*
2262 * Controllers may support a CMB size larger than their BAR,
2263 * for example, due to being behind a bridge. Reduce the CMB to
2264 * the reported size of the BAR
2265 */
2266 if (size > bar_size - offset)
2267 size = bar_size - offset;
2268
2269 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(cmbloc)) + offset;
2270 cmb = ioremap_wc(dma_addr, size);
2271 if (!cmb)
2272 return NULL;
2273
2274 dev->cmb_dma_addr = dma_addr;
2275 dev->cmb_size = size;
2276 return cmb;
2277}
2278
2279static inline void nvme_release_cmb(struct nvme_dev *dev)
2280{
2281 if (dev->cmb) {
2282 iounmap(dev->cmb);
2283 dev->cmb = NULL;
2284 }
2285}
2286
Keith Busch9d713c22013-07-15 15:02:24 -06002287static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
2288{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002289 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06002290}
2291
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002292static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002293{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002294 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02002295 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch42f61422014-03-24 10:46:25 -06002296 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002297
Keith Busch42f61422014-03-24 10:46:25 -06002298 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002299 result = set_queue_count(dev, nr_io_queues);
Keith Buschbadc34d2014-06-23 14:25:35 -06002300 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002301 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002302 if (result < nr_io_queues)
2303 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002304
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002305 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
2306 result = nvme_cmb_qdepth(dev, nr_io_queues,
2307 sizeof(struct nvme_command));
2308 if (result > 0)
2309 dev->q_depth = result;
2310 else
2311 nvme_release_cmb(dev);
2312 }
2313
Keith Busch9d713c22013-07-15 15:02:24 -06002314 size = db_bar_size(dev, nr_io_queues);
2315 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002316 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06002317 do {
2318 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
2319 if (dev->bar)
2320 break;
2321 if (!--nr_io_queues)
2322 return -ENOMEM;
2323 size = db_bar_size(dev, nr_io_queues);
2324 } while (1);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002325 dev->dbs = dev->bar + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07002326 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002327 }
2328
Keith Busch9d713c22013-07-15 15:02:24 -06002329 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002330 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002331
Jens Axboee32efbf2014-11-14 09:49:26 -07002332 /*
2333 * If we enable msix early due to not intx, disable it again before
2334 * setting up the full range we need.
2335 */
2336 if (!pdev->irq)
2337 pci_disable_msix(pdev);
2338
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002339 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002340 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002341 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
2342 if (vecs < 0) {
2343 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
2344 if (vecs < 0) {
2345 vecs = 1;
2346 } else {
2347 for (i = 0; i < vecs; i++)
2348 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002349 }
2350 }
2351
Matthew Wilcox063a8092013-06-20 10:53:48 -04002352 /*
2353 * Should investigate if there's a performance win from allocating
2354 * more queues than interrupt vectors; it might allow the submission
2355 * path to scale better, even if the receive path is limited by the
2356 * number of interrupts.
2357 */
2358 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002359 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002360
Matthew Wilcox3193f072014-01-27 15:57:22 -05002361 result = queue_request_irq(dev, adminq, adminq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06002362 if (result) {
2363 adminq->cq_vector = -1;
Keith Busch22404272013-07-15 15:02:20 -06002364 goto free_queues;
Jon Derrick758dd7f2015-06-30 11:22:52 -06002365 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05002366
Keith Buschcd638942013-07-15 15:02:23 -06002367 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002368 nvme_free_queues(dev, nr_io_queues + 1);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002369 nvme_create_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06002370
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002371 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002372
Keith Busch22404272013-07-15 15:02:20 -06002373 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002374 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002375 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002376}
2377
Keith Buscha5768aa2015-06-01 14:28:14 -06002378static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2379{
2380 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2381 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2382
2383 return nsa->ns_id - nsb->ns_id;
2384}
2385
2386static struct nvme_ns *nvme_find_ns(struct nvme_dev *dev, unsigned nsid)
2387{
2388 struct nvme_ns *ns;
2389
2390 list_for_each_entry(ns, &dev->namespaces, list) {
2391 if (ns->ns_id == nsid)
2392 return ns;
2393 if (ns->ns_id > nsid)
2394 break;
2395 }
2396 return NULL;
2397}
2398
2399static inline bool nvme_io_incapable(struct nvme_dev *dev)
2400{
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002401 return (!dev->bar ||
2402 readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_CFS ||
2403 dev->online_queues < 2);
Keith Buscha5768aa2015-06-01 14:28:14 -06002404}
2405
2406static void nvme_ns_remove(struct nvme_ns *ns)
2407{
2408 bool kill = nvme_io_incapable(ns->dev) && !blk_queue_dying(ns->queue);
2409
2410 if (kill)
2411 blk_set_queue_dying(ns->queue);
Dan Williams9609b992015-10-21 13:19:55 -04002412 if (ns->disk->flags & GENHD_FL_UP)
Keith Buscha5768aa2015-06-01 14:28:14 -06002413 del_gendisk(ns->disk);
Keith Buscha5768aa2015-06-01 14:28:14 -06002414 if (kill || !blk_queue_dying(ns->queue)) {
2415 blk_mq_abort_requeue_list(ns->queue);
2416 blk_cleanup_queue(ns->queue);
Keith Busch5105aa52015-10-02 10:37:28 -06002417 }
2418 list_del_init(&ns->list);
2419 kref_put(&ns->kref, nvme_free_ns);
Keith Buscha5768aa2015-06-01 14:28:14 -06002420}
2421
2422static void nvme_scan_namespaces(struct nvme_dev *dev, unsigned nn)
2423{
2424 struct nvme_ns *ns, *next;
2425 unsigned i;
2426
2427 for (i = 1; i <= nn; i++) {
2428 ns = nvme_find_ns(dev, i);
2429 if (ns) {
Keith Busch5105aa52015-10-02 10:37:28 -06002430 if (revalidate_disk(ns->disk))
Keith Buscha5768aa2015-06-01 14:28:14 -06002431 nvme_ns_remove(ns);
Keith Buscha5768aa2015-06-01 14:28:14 -06002432 } else
2433 nvme_alloc_ns(dev, i);
2434 }
2435 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
Keith Busch5105aa52015-10-02 10:37:28 -06002436 if (ns->ns_id > nn)
Keith Buscha5768aa2015-06-01 14:28:14 -06002437 nvme_ns_remove(ns);
Keith Buscha5768aa2015-06-01 14:28:14 -06002438 }
2439 list_sort(NULL, &dev->namespaces, ns_cmp);
2440}
2441
Keith Buschbda4e0f2015-09-03 08:18:17 -06002442static void nvme_set_irq_hints(struct nvme_dev *dev)
2443{
2444 struct nvme_queue *nvmeq;
2445 int i;
2446
2447 for (i = 0; i < dev->online_queues; i++) {
2448 nvmeq = dev->queues[i];
2449
2450 if (!nvmeq->tags || !(*nvmeq->tags))
2451 continue;
2452
2453 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
2454 blk_mq_tags_cpumask(*nvmeq->tags));
2455 }
2456}
2457
Keith Buscha5768aa2015-06-01 14:28:14 -06002458static void nvme_dev_scan(struct work_struct *work)
2459{
2460 struct nvme_dev *dev = container_of(work, struct nvme_dev, scan_work);
2461 struct nvme_id_ctrl *ctrl;
2462
2463 if (!dev->tagset.tags)
2464 return;
2465 if (nvme_identify_ctrl(dev, &ctrl))
2466 return;
2467 nvme_scan_namespaces(dev, le32_to_cpup(&ctrl->nn));
2468 kfree(ctrl);
Keith Buschbda4e0f2015-09-03 08:18:17 -06002469 nvme_set_irq_hints(dev);
Keith Buscha5768aa2015-06-01 14:28:14 -06002470}
2471
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002472/*
2473 * Return: error value if an error occurred setting up the queues or calling
2474 * Identify Device. 0 if these succeeded, even if adding some of the
2475 * namespaces failed. At the moment, these failures are silent. TBD which
2476 * failures should be reported.
2477 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002478static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002479{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002480 struct pci_dev *pdev = to_pci_dev(dev->dev);
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002481 int res;
Matthew Wilcox51814232011-02-01 16:18:08 -05002482 struct nvme_id_ctrl *ctrl;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002483 int shift = NVME_CAP_MPSMIN(lo_hi_readq(dev->bar + NVME_REG_CAP)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002484
Christoph Hellwigd29ec822015-05-22 11:12:46 +02002485 res = nvme_identify_ctrl(dev, &ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002486 if (res) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002487 dev_err(dev->dev, "Identify Controller failed (%d)\n", res);
Keith Busche1e5e562015-02-19 13:39:03 -07002488 return -EIO;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002489 }
2490
Keith Busch0e5e4f02012-11-09 16:33:05 -07002491 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002492 dev->abort_limit = ctrl->acl + 1;
Keith Buscha7d2ce22014-04-29 11:41:28 -06002493 dev->vwc = ctrl->vwc;
Matthew Wilcox51814232011-02-01 16:18:08 -05002494 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2495 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2496 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002497 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002498 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Sathyavathi Mb12363d2015-11-05 12:52:28 -07002499 else
2500 dev->max_hw_sectors = UINT_MAX;
Matthew Wilcox68608c262013-06-21 14:36:34 -04002501 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002502 (pdev->device == 0x0953) && ctrl->vs[3]) {
2503 unsigned int max_hw_sectors;
2504
Keith Busch159b67d2013-04-09 17:13:20 -06002505 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002506 max_hw_sectors = dev->stripe_size >> (shift - 9);
2507 if (dev->max_hw_sectors) {
2508 dev->max_hw_sectors = min(max_hw_sectors,
2509 dev->max_hw_sectors);
2510 } else
2511 dev->max_hw_sectors = max_hw_sectors;
2512 }
Christoph Hellwigd29ec822015-05-22 11:12:46 +02002513 kfree(ctrl);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002514
Keith Buschffe77042015-06-08 10:08:15 -06002515 if (!dev->tagset.tags) {
2516 dev->tagset.ops = &nvme_mq_ops;
2517 dev->tagset.nr_hw_queues = dev->online_queues - 1;
2518 dev->tagset.timeout = NVME_IO_TIMEOUT;
2519 dev->tagset.numa_node = dev_to_node(dev->dev);
2520 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002521 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Keith Buschffe77042015-06-08 10:08:15 -06002522 dev->tagset.cmd_size = nvme_cmd_size(dev);
2523 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
2524 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002525
Keith Buschffe77042015-06-08 10:08:15 -06002526 if (blk_mq_alloc_tag_set(&dev->tagset))
2527 return 0;
2528 }
Keith Buscha5768aa2015-06-01 14:28:14 -06002529 schedule_work(&dev->scan_work);
Keith Busche1e5e562015-02-19 13:39:03 -07002530 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002531}
2532
Keith Busch0877cb02013-07-15 15:02:19 -06002533static int nvme_dev_map(struct nvme_dev *dev)
2534{
Keith Busch42f61422014-03-24 10:46:25 -06002535 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002536 int bars, result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002537 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002538
2539 if (pci_enable_device_mem(pdev))
2540 return result;
2541
2542 dev->entry[0].vector = pdev->irq;
2543 pci_set_master(pdev);
2544 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07002545 if (!bars)
2546 goto disable_pci;
2547
Keith Busch0877cb02013-07-15 15:02:19 -06002548 if (pci_request_selected_regions(pdev, bars, "nvme"))
2549 goto disable_pci;
2550
Christoph Hellwige75ec752015-05-22 11:12:39 +02002551 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
2552 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01002553 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002554
Keith Busch0877cb02013-07-15 15:02:19 -06002555 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2556 if (!dev->bar)
2557 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07002558
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002559 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
Keith Busch0e53d182013-12-10 13:10:39 -07002560 result = -ENODEV;
2561 goto unmap;
2562 }
Jens Axboee32efbf2014-11-14 09:49:26 -07002563
2564 /*
2565 * Some devices don't advertse INTx interrupts, pre-enable a single
2566 * MSIX vec for setup. We'll adjust this later.
2567 */
2568 if (!pdev->irq) {
2569 result = pci_enable_msix(pdev, dev->entry, 1);
2570 if (result < 0)
2571 goto unmap;
2572 }
2573
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002574 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
2575
Keith Busch42f61422014-03-24 10:46:25 -06002576 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2577 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002578 dev->dbs = dev->bar + 4096;
2579 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2))
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002580 dev->cmb = nvme_map_cmb(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002581
2582 return 0;
2583
Keith Busch0e53d182013-12-10 13:10:39 -07002584 unmap:
2585 iounmap(dev->bar);
2586 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002587 disable:
2588 pci_release_regions(pdev);
2589 disable_pci:
2590 pci_disable_device(pdev);
2591 return result;
2592}
2593
2594static void nvme_dev_unmap(struct nvme_dev *dev)
2595{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002596 struct pci_dev *pdev = to_pci_dev(dev->dev);
2597
2598 if (pdev->msi_enabled)
2599 pci_disable_msi(pdev);
2600 else if (pdev->msix_enabled)
2601 pci_disable_msix(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002602
2603 if (dev->bar) {
2604 iounmap(dev->bar);
2605 dev->bar = NULL;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002606 pci_release_regions(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002607 }
2608
Christoph Hellwige75ec752015-05-22 11:12:39 +02002609 if (pci_is_enabled(pdev))
2610 pci_disable_device(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002611}
2612
Keith Busch4d115422013-12-10 13:10:40 -07002613struct nvme_delq_ctx {
2614 struct task_struct *waiter;
2615 struct kthread_worker *worker;
2616 atomic_t refcount;
2617};
2618
2619static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2620{
2621 dq->waiter = current;
2622 mb();
2623
2624 for (;;) {
2625 set_current_state(TASK_KILLABLE);
2626 if (!atomic_read(&dq->refcount))
2627 break;
2628 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2629 fatal_signal_pending(current)) {
Keith Busch0fb59cb2015-01-07 18:55:50 -07002630 /*
2631 * Disable the controller first since we can't trust it
2632 * at this point, but leave the admin queue enabled
2633 * until all queue deletion requests are flushed.
2634 * FIXME: This may take a while if there are more h/w
2635 * queues than admin tags.
2636 */
Keith Busch4d115422013-12-10 13:10:40 -07002637 set_current_state(TASK_RUNNING);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002638 nvme_disable_ctrl(dev,
2639 lo_hi_readq(dev->bar + NVME_REG_CAP));
Keith Busch0fb59cb2015-01-07 18:55:50 -07002640 nvme_clear_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07002641 flush_kthread_worker(dq->worker);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002642 nvme_disable_queue(dev, 0);
Keith Busch4d115422013-12-10 13:10:40 -07002643 return;
2644 }
2645 }
2646 set_current_state(TASK_RUNNING);
2647}
2648
2649static void nvme_put_dq(struct nvme_delq_ctx *dq)
2650{
2651 atomic_dec(&dq->refcount);
2652 if (dq->waiter)
2653 wake_up_process(dq->waiter);
2654}
2655
2656static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2657{
2658 atomic_inc(&dq->refcount);
2659 return dq;
2660}
2661
2662static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2663{
2664 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
Keith Busch4d115422013-12-10 13:10:40 -07002665 nvme_put_dq(dq);
Keith Busch604e8c82015-11-20 08:38:13 -07002666
2667 spin_lock_irq(&nvmeq->q_lock);
2668 nvme_process_cq(nvmeq);
2669 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07002670}
2671
2672static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2673 kthread_work_func_t fn)
2674{
2675 struct nvme_command c;
2676
2677 memset(&c, 0, sizeof(c));
2678 c.delete_queue.opcode = opcode;
2679 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2680
2681 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002682 return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
2683 ADMIN_TIMEOUT);
Keith Busch4d115422013-12-10 13:10:40 -07002684}
2685
2686static void nvme_del_cq_work_handler(struct kthread_work *work)
2687{
2688 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2689 cmdinfo.work);
2690 nvme_del_queue_end(nvmeq);
2691}
2692
2693static int nvme_delete_cq(struct nvme_queue *nvmeq)
2694{
2695 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2696 nvme_del_cq_work_handler);
2697}
2698
2699static void nvme_del_sq_work_handler(struct kthread_work *work)
2700{
2701 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2702 cmdinfo.work);
2703 int status = nvmeq->cmdinfo.status;
2704
2705 if (!status)
2706 status = nvme_delete_cq(nvmeq);
2707 if (status)
2708 nvme_del_queue_end(nvmeq);
2709}
2710
2711static int nvme_delete_sq(struct nvme_queue *nvmeq)
2712{
2713 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2714 nvme_del_sq_work_handler);
2715}
2716
2717static void nvme_del_queue_start(struct kthread_work *work)
2718{
2719 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2720 cmdinfo.work);
Keith Busch4d115422013-12-10 13:10:40 -07002721 if (nvme_delete_sq(nvmeq))
2722 nvme_del_queue_end(nvmeq);
2723}
2724
2725static void nvme_disable_io_queues(struct nvme_dev *dev)
2726{
2727 int i;
2728 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2729 struct nvme_delq_ctx dq;
2730 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2731 &worker, "nvme%d", dev->instance);
2732
2733 if (IS_ERR(kworker_task)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002734 dev_err(dev->dev,
Keith Busch4d115422013-12-10 13:10:40 -07002735 "Failed to create queue del task\n");
2736 for (i = dev->queue_count - 1; i > 0; i--)
2737 nvme_disable_queue(dev, i);
2738 return;
2739 }
2740
2741 dq.waiter = NULL;
2742 atomic_set(&dq.refcount, 0);
2743 dq.worker = &worker;
2744 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002745 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002746
2747 if (nvme_suspend_queue(nvmeq))
2748 continue;
2749 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2750 nvmeq->cmdinfo.worker = dq.worker;
2751 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2752 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2753 }
2754 nvme_wait_dq(&dq, dev);
2755 kthread_stop(kworker_task);
2756}
2757
Dan McLeranb9afca32014-04-07 17:10:11 -06002758/*
2759* Remove the node from the device list and check
2760* for whether or not we need to stop the nvme_thread.
2761*/
2762static void nvme_dev_list_remove(struct nvme_dev *dev)
2763{
2764 struct task_struct *tmp = NULL;
2765
2766 spin_lock(&dev_list_lock);
2767 list_del_init(&dev->node);
2768 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2769 tmp = nvme_thread;
2770 nvme_thread = NULL;
2771 }
2772 spin_unlock(&dev_list_lock);
2773
2774 if (tmp)
2775 kthread_stop(tmp);
2776}
2777
Keith Buschc9d3bf82015-01-07 18:55:52 -07002778static void nvme_freeze_queues(struct nvme_dev *dev)
2779{
2780 struct nvme_ns *ns;
2781
2782 list_for_each_entry(ns, &dev->namespaces, list) {
2783 blk_mq_freeze_queue_start(ns->queue);
2784
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002785 spin_lock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002786 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002787 spin_unlock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002788
2789 blk_mq_cancel_requeue_work(ns->queue);
2790 blk_mq_stop_hw_queues(ns->queue);
2791 }
2792}
2793
2794static void nvme_unfreeze_queues(struct nvme_dev *dev)
2795{
2796 struct nvme_ns *ns;
2797
2798 list_for_each_entry(ns, &dev->namespaces, list) {
2799 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2800 blk_mq_unfreeze_queue(ns->queue);
2801 blk_mq_start_stopped_hw_queues(ns->queue, true);
2802 blk_mq_kick_requeue_list(ns->queue);
2803 }
2804}
2805
Keith Buschf0b50732013-07-15 15:02:21 -06002806static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002807{
Keith Busch22404272013-07-15 15:02:20 -06002808 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06002809 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06002810
Dan McLeranb9afca32014-04-07 17:10:11 -06002811 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002812
Keith Buschc9d3bf82015-01-07 18:55:52 -07002813 if (dev->bar) {
2814 nvme_freeze_queues(dev);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002815 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002816 }
Keith Busch7c1b2452014-06-25 11:18:12 -06002817 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07002818 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002819 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002820 nvme_suspend_queue(nvmeq);
Keith Busch4d115422013-12-10 13:10:40 -07002821 }
2822 } else {
2823 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002824 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002825 nvme_disable_queue(dev, 0);
2826 }
Keith Buschf0b50732013-07-15 15:02:21 -06002827 nvme_dev_unmap(dev);
Keith Busch07836e62015-02-19 10:34:48 -07002828
2829 for (i = dev->queue_count - 1; i >= 0; i--)
2830 nvme_clear_queue(dev->queues[i]);
Keith Buschf0b50732013-07-15 15:02:21 -06002831}
2832
2833static void nvme_dev_remove(struct nvme_dev *dev)
2834{
Keith Busch5105aa52015-10-02 10:37:28 -06002835 struct nvme_ns *ns, *next;
Keith Buschf0b50732013-07-15 15:02:21 -06002836
Keith Busch5105aa52015-10-02 10:37:28 -06002837 list_for_each_entry_safe(ns, next, &dev->namespaces, list)
Keith Buscha5768aa2015-06-01 14:28:14 -06002838 nvme_ns_remove(ns);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002839}
2840
Matthew Wilcox091b6092011-02-10 09:56:01 -05002841static int nvme_setup_prp_pools(struct nvme_dev *dev)
2842{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002843 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05002844 PAGE_SIZE, PAGE_SIZE, 0);
2845 if (!dev->prp_page_pool)
2846 return -ENOMEM;
2847
Matthew Wilcox99802a72011-02-10 10:30:34 -05002848 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02002849 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05002850 256, 256, 0);
2851 if (!dev->prp_small_pool) {
2852 dma_pool_destroy(dev->prp_page_pool);
2853 return -ENOMEM;
2854 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002855 return 0;
2856}
2857
2858static void nvme_release_prp_pools(struct nvme_dev *dev)
2859{
2860 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002861 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002862}
2863
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002864static DEFINE_IDA(nvme_instance_ida);
2865
2866static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002867{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002868 int instance, error;
2869
2870 do {
2871 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2872 return -ENODEV;
2873
2874 spin_lock(&dev_list_lock);
2875 error = ida_get_new(&nvme_instance_ida, &instance);
2876 spin_unlock(&dev_list_lock);
2877 } while (error == -EAGAIN);
2878
2879 if (error)
2880 return -ENODEV;
2881
2882 dev->instance = instance;
2883 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002884}
2885
2886static void nvme_release_instance(struct nvme_dev *dev)
2887{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002888 spin_lock(&dev_list_lock);
2889 ida_remove(&nvme_instance_ida, dev->instance);
2890 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002891}
2892
Keith Busch5e82e952013-02-19 10:17:58 -07002893static void nvme_free_dev(struct kref *kref)
2894{
2895 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002896
Christoph Hellwige75ec752015-05-22 11:12:39 +02002897 put_device(dev->dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07002898 put_device(dev->device);
Indraneel M285dffc2014-12-11 08:24:18 -07002899 nvme_release_instance(dev);
Keith Busch4af0e212015-06-08 10:08:13 -06002900 if (dev->tagset.tags)
2901 blk_mq_free_tag_set(&dev->tagset);
2902 if (dev->admin_q)
2903 blk_put_queue(dev->admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002904 kfree(dev->queues);
2905 kfree(dev->entry);
2906 kfree(dev);
2907}
2908
2909static int nvme_dev_open(struct inode *inode, struct file *f)
2910{
Keith Buschb3fffde2015-02-03 11:21:42 -07002911 struct nvme_dev *dev;
2912 int instance = iminor(inode);
2913 int ret = -ENODEV;
2914
2915 spin_lock(&dev_list_lock);
2916 list_for_each_entry(dev, &dev_list, node) {
2917 if (dev->instance == instance) {
Keith Busch2e1d8442015-02-12 15:33:00 -07002918 if (!dev->admin_q) {
2919 ret = -EWOULDBLOCK;
2920 break;
2921 }
Keith Buschb3fffde2015-02-03 11:21:42 -07002922 if (!kref_get_unless_zero(&dev->kref))
2923 break;
2924 f->private_data = dev;
2925 ret = 0;
2926 break;
2927 }
2928 }
2929 spin_unlock(&dev_list_lock);
2930
2931 return ret;
Keith Busch5e82e952013-02-19 10:17:58 -07002932}
2933
2934static int nvme_dev_release(struct inode *inode, struct file *f)
2935{
2936 struct nvme_dev *dev = f->private_data;
2937 kref_put(&dev->kref, nvme_free_dev);
2938 return 0;
2939}
2940
2941static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2942{
2943 struct nvme_dev *dev = f->private_data;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002944 struct nvme_ns *ns;
2945
Keith Busch5e82e952013-02-19 10:17:58 -07002946 switch (cmd) {
2947 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002948 return nvme_user_cmd(dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06002949 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002950 if (list_empty(&dev->namespaces))
2951 return -ENOTTY;
2952 ns = list_first_entry(&dev->namespaces, struct nvme_ns, list);
2953 return nvme_user_cmd(dev, ns, (void __user *)arg);
Keith Busch4cc06522015-06-05 10:30:08 -06002954 case NVME_IOCTL_RESET:
2955 dev_warn(dev->dev, "resetting controller\n");
2956 return nvme_reset(dev);
Jon Derrick81f03fe2015-08-10 15:20:41 -06002957 case NVME_IOCTL_SUBSYS_RESET:
2958 return nvme_subsys_reset(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002959 default:
2960 return -ENOTTY;
2961 }
2962}
2963
2964static const struct file_operations nvme_dev_fops = {
2965 .owner = THIS_MODULE,
2966 .open = nvme_dev_open,
2967 .release = nvme_dev_release,
2968 .unlocked_ioctl = nvme_dev_ioctl,
2969 .compat_ioctl = nvme_dev_ioctl,
2970};
2971
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002972static void nvme_probe_work(struct work_struct *work)
Keith Buschf0b50732013-07-15 15:02:21 -06002973{
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002974 struct nvme_dev *dev = container_of(work, struct nvme_dev, probe_work);
Dan McLeranb9afca32014-04-07 17:10:11 -06002975 bool start_thread = false;
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002976 int result;
Keith Buschf0b50732013-07-15 15:02:21 -06002977
2978 result = nvme_dev_map(dev);
2979 if (result)
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002980 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002981
2982 result = nvme_configure_admin_queue(dev);
2983 if (result)
2984 goto unmap;
2985
2986 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002987 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2988 start_thread = true;
2989 nvme_thread = NULL;
2990 }
Keith Buschf0b50732013-07-15 15:02:21 -06002991 list_add(&dev->node, &dev_list);
2992 spin_unlock(&dev_list_lock);
2993
Dan McLeranb9afca32014-04-07 17:10:11 -06002994 if (start_thread) {
2995 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
Keith Busch387caa52014-09-22 13:46:19 -06002996 wake_up_all(&nvme_kthread_wait);
Dan McLeranb9afca32014-04-07 17:10:11 -06002997 } else
2998 wait_event_killable(nvme_kthread_wait, nvme_thread);
2999
3000 if (IS_ERR_OR_NULL(nvme_thread)) {
3001 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
3002 goto disable;
3003 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003004
3005 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07003006 result = nvme_alloc_admin_tags(dev);
3007 if (result)
3008 goto disable;
Dan McLeranb9afca32014-04-07 17:10:11 -06003009
Keith Buschf0b50732013-07-15 15:02:21 -06003010 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06003011 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07003012 goto free_tags;
Keith Buschf0b50732013-07-15 15:02:21 -06003013
Keith Busch1efccc92015-03-31 10:37:17 -06003014 dev->event_limit = 1;
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02003015
Christoph Hellwig2659e572015-10-02 18:51:31 +02003016 /*
3017 * Keep the controller around but remove all namespaces if we don't have
3018 * any working I/O queue.
3019 */
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02003020 if (dev->online_queues < 2) {
3021 dev_warn(dev->dev, "IO queues not created\n");
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02003022 nvme_dev_remove(dev);
3023 } else {
3024 nvme_unfreeze_queues(dev);
3025 nvme_dev_add(dev);
3026 }
3027
3028 return;
Keith Buschf0b50732013-07-15 15:02:21 -06003029
Keith Busch0fb59cb2015-01-07 18:55:50 -07003030 free_tags:
3031 nvme_dev_remove_admin(dev);
Keith Busch4af0e212015-06-08 10:08:13 -06003032 blk_put_queue(dev->admin_q);
3033 dev->admin_q = NULL;
3034 dev->queues[0]->tags = NULL;
Keith Buschf0b50732013-07-15 15:02:21 -06003035 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05003036 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06003037 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06003038 unmap:
3039 nvme_dev_unmap(dev);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02003040 out:
3041 if (!work_busy(&dev->reset_work))
3042 nvme_dead_ctrl(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06003043}
3044
Keith Busch9a6b9452013-12-10 13:10:36 -07003045static int nvme_remove_dead_ctrl(void *arg)
3046{
3047 struct nvme_dev *dev = (struct nvme_dev *)arg;
Christoph Hellwige75ec752015-05-22 11:12:39 +02003048 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003049
3050 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06003051 pci_stop_and_remove_bus_device_locked(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003052 kref_put(&dev->kref, nvme_free_dev);
3053 return 0;
3054}
3055
Keith Buschde3eff22015-06-18 13:36:39 -06003056static void nvme_dead_ctrl(struct nvme_dev *dev)
3057{
3058 dev_warn(dev->dev, "Device failed to resume\n");
3059 kref_get(&dev->kref);
3060 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
3061 dev->instance))) {
3062 dev_err(dev->dev,
3063 "Failed to start controller remove task\n");
3064 kref_put(&dev->kref, nvme_free_dev);
3065 }
3066}
3067
Christoph Hellwig77b50d92015-10-02 17:41:18 +02003068static void nvme_reset_work(struct work_struct *ws)
Keith Busch9a6b9452013-12-10 13:10:36 -07003069{
Christoph Hellwig77b50d92015-10-02 17:41:18 +02003070 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
Keith Buschffe77042015-06-08 10:08:15 -06003071 bool in_probe = work_busy(&dev->probe_work);
3072
Keith Busch9a6b9452013-12-10 13:10:36 -07003073 nvme_dev_shutdown(dev);
Keith Buschffe77042015-06-08 10:08:15 -06003074
3075 /* Synchronize with device probe so that work will see failure status
3076 * and exit gracefully without trying to schedule another reset */
3077 flush_work(&dev->probe_work);
3078
3079 /* Fail this device if reset occured during probe to avoid
3080 * infinite initialization loops. */
3081 if (in_probe) {
Keith Buschde3eff22015-06-18 13:36:39 -06003082 nvme_dead_ctrl(dev);
Keith Buschffe77042015-06-08 10:08:15 -06003083 return;
Keith Busch9a6b9452013-12-10 13:10:36 -07003084 }
Keith Buschffe77042015-06-08 10:08:15 -06003085 /* Schedule device resume asynchronously so the reset work is available
3086 * to cleanup errors that may occur during reinitialization */
3087 schedule_work(&dev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07003088}
3089
Christoph Hellwig906678922015-10-02 18:49:23 +02003090static int __nvme_reset(struct nvme_dev *dev)
Keith Busch9a6b9452013-12-10 13:10:36 -07003091{
Christoph Hellwig906678922015-10-02 18:49:23 +02003092 if (work_pending(&dev->reset_work))
3093 return -EBUSY;
3094 list_del_init(&dev->node);
3095 queue_work(nvme_workq, &dev->reset_work);
3096 return 0;
Tejun Heo9ca97372014-03-07 10:24:49 -05003097}
3098
Keith Busch4cc06522015-06-05 10:30:08 -06003099static int nvme_reset(struct nvme_dev *dev)
3100{
Christoph Hellwig906678922015-10-02 18:49:23 +02003101 int ret;
Keith Busch4cc06522015-06-05 10:30:08 -06003102
3103 if (!dev->admin_q || blk_queue_dying(dev->admin_q))
3104 return -ENODEV;
3105
3106 spin_lock(&dev_list_lock);
Christoph Hellwig906678922015-10-02 18:49:23 +02003107 ret = __nvme_reset(dev);
Keith Busch4cc06522015-06-05 10:30:08 -06003108 spin_unlock(&dev_list_lock);
3109
3110 if (!ret) {
3111 flush_work(&dev->reset_work);
Keith Buschffe77042015-06-08 10:08:15 -06003112 flush_work(&dev->probe_work);
Keith Busch4cc06522015-06-05 10:30:08 -06003113 return 0;
3114 }
3115
3116 return ret;
3117}
3118
3119static ssize_t nvme_sysfs_reset(struct device *dev,
3120 struct device_attribute *attr, const char *buf,
3121 size_t count)
3122{
3123 struct nvme_dev *ndev = dev_get_drvdata(dev);
3124 int ret;
3125
3126 ret = nvme_reset(ndev);
3127 if (ret < 0)
3128 return ret;
3129
3130 return count;
3131}
3132static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
3133
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003134static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003135{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003136 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003137 struct nvme_dev *dev;
3138
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003139 node = dev_to_node(&pdev->dev);
3140 if (node == NUMA_NO_NODE)
3141 set_dev_node(&pdev->dev, 0);
3142
3143 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003144 if (!dev)
3145 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003146 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
3147 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003148 if (!dev->entry)
3149 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003150 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
3151 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003152 if (!dev->queues)
3153 goto free;
3154
3155 INIT_LIST_HEAD(&dev->namespaces);
Christoph Hellwig77b50d92015-10-02 17:41:18 +02003156 INIT_WORK(&dev->reset_work, nvme_reset_work);
Christoph Hellwige75ec752015-05-22 11:12:39 +02003157 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003158 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07003159 result = nvme_set_instance(dev);
3160 if (result)
Keith Buscha96d4f52014-08-19 19:15:59 -06003161 goto put_pci;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003162
Matthew Wilcox091b6092011-02-10 09:56:01 -05003163 result = nvme_setup_prp_pools(dev);
3164 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06003165 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05003166
Keith Buschfb35e912014-03-03 11:09:47 -07003167 kref_init(&dev->kref);
Keith Buschb3fffde2015-02-03 11:21:42 -07003168 dev->device = device_create(nvme_class, &pdev->dev,
3169 MKDEV(nvme_char_major, dev->instance),
3170 dev, "nvme%d", dev->instance);
3171 if (IS_ERR(dev->device)) {
3172 result = PTR_ERR(dev->device);
Keith Busch2e1d8442015-02-12 15:33:00 -07003173 goto release_pools;
Keith Buschb3fffde2015-02-03 11:21:42 -07003174 }
3175 get_device(dev->device);
Keith Busch4cc06522015-06-05 10:30:08 -06003176 dev_set_drvdata(dev->device, dev);
3177
3178 result = device_create_file(dev->device, &dev_attr_reset_controller);
3179 if (result)
3180 goto put_dev;
Keith Buschb3fffde2015-02-03 11:21:42 -07003181
Keith Busche6e96d72015-03-23 09:32:37 -06003182 INIT_LIST_HEAD(&dev->node);
Keith Buscha5768aa2015-06-01 14:28:14 -06003183 INIT_WORK(&dev->scan_work, nvme_dev_scan);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02003184 INIT_WORK(&dev->probe_work, nvme_probe_work);
Keith Busch2e1d8442015-02-12 15:33:00 -07003185 schedule_work(&dev->probe_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003186 return 0;
3187
Keith Busch4cc06522015-06-05 10:30:08 -06003188 put_dev:
3189 device_destroy(nvme_class, MKDEV(nvme_char_major, dev->instance));
3190 put_device(dev->device);
Keith Busch0877cb02013-07-15 15:02:19 -06003191 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05003192 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06003193 release:
3194 nvme_release_instance(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06003195 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02003196 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003197 free:
3198 kfree(dev->queues);
3199 kfree(dev->entry);
3200 kfree(dev);
3201 return result;
3202}
3203
Keith Buschf0d54a52014-05-02 10:40:43 -06003204static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
3205{
Keith Buscha6739472014-06-23 16:03:21 -06003206 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06003207
Keith Buscha6739472014-06-23 16:03:21 -06003208 if (prepare)
3209 nvme_dev_shutdown(dev);
3210 else
Keith Busch0a7385a2015-10-02 10:37:29 -06003211 schedule_work(&dev->probe_work);
Keith Buschf0d54a52014-05-02 10:40:43 -06003212}
3213
Keith Busch09ece142014-01-27 11:29:40 -05003214static void nvme_shutdown(struct pci_dev *pdev)
3215{
3216 struct nvme_dev *dev = pci_get_drvdata(pdev);
3217 nvme_dev_shutdown(dev);
3218}
3219
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003220static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003221{
3222 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003223
3224 spin_lock(&dev_list_lock);
3225 list_del_init(&dev->node);
3226 spin_unlock(&dev_list_lock);
3227
3228 pci_set_drvdata(pdev, NULL);
Keith Busch2e1d8442015-02-12 15:33:00 -07003229 flush_work(&dev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07003230 flush_work(&dev->reset_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06003231 flush_work(&dev->scan_work);
Keith Busch4cc06522015-06-05 10:30:08 -06003232 device_remove_file(dev->device, &dev_attr_reset_controller);
Keith Buschc9d3bf82015-01-07 18:55:52 -07003233 nvme_dev_remove(dev);
Keith Busch3399a3f2015-06-18 13:36:40 -06003234 nvme_dev_shutdown(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003235 nvme_dev_remove_admin(dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07003236 device_destroy(nvme_class, MKDEV(nvme_char_major, dev->instance));
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003237 nvme_free_queues(dev, 0);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06003238 nvme_release_cmb(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003239 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07003240 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003241}
3242
3243/* These functions are yet to be implemented */
3244#define nvme_error_detected NULL
3245#define nvme_dump_registers NULL
3246#define nvme_link_reset NULL
3247#define nvme_slot_reset NULL
3248#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06003249
Jingoo Han671a6012014-02-13 11:19:14 +09003250#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06003251static int nvme_suspend(struct device *dev)
3252{
3253 struct pci_dev *pdev = to_pci_dev(dev);
3254 struct nvme_dev *ndev = pci_get_drvdata(pdev);
3255
3256 nvme_dev_shutdown(ndev);
3257 return 0;
3258}
3259
3260static int nvme_resume(struct device *dev)
3261{
3262 struct pci_dev *pdev = to_pci_dev(dev);
3263 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06003264
Keith Busch0a7385a2015-10-02 10:37:29 -06003265 schedule_work(&ndev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07003266 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06003267}
Jingoo Han671a6012014-02-13 11:19:14 +09003268#endif
Keith Buschcd638942013-07-15 15:02:23 -06003269
3270static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003271
Stephen Hemminger1d352032012-09-07 09:33:17 -07003272static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003273 .error_detected = nvme_error_detected,
3274 .mmio_enabled = nvme_dump_registers,
3275 .link_reset = nvme_link_reset,
3276 .slot_reset = nvme_slot_reset,
3277 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06003278 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003279};
3280
3281/* Move to pci_ids.h later */
3282#define PCI_CLASS_STORAGE_EXPRESS 0x010802
3283
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003284static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003285 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
Stephan Güntherc74dc782015-11-04 00:49:45 +01003286 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003287 { 0, }
3288};
3289MODULE_DEVICE_TABLE(pci, nvme_id_table);
3290
3291static struct pci_driver nvme_driver = {
3292 .name = "nvme",
3293 .id_table = nvme_id_table,
3294 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003295 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05003296 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06003297 .driver = {
3298 .pm = &nvme_dev_pm_ops,
3299 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003300 .err_handler = &nvme_err_handler,
3301};
3302
3303static int __init nvme_init(void)
3304{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003305 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003306
Dan McLeranb9afca32014-04-07 17:10:11 -06003307 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003308
Keith Busch9a6b9452013-12-10 13:10:36 -07003309 nvme_workq = create_singlethread_workqueue("nvme");
3310 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06003311 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07003312
Keith Busch5c42ea12012-07-25 16:05:18 -06003313 result = register_blkdev(nvme_major, "nvme");
3314 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07003315 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06003316 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003317 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003318
Keith Buschb3fffde2015-02-03 11:21:42 -07003319 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
3320 &nvme_dev_fops);
3321 if (result < 0)
3322 goto unregister_blkdev;
3323 else if (result > 0)
3324 nvme_char_major = result;
3325
3326 nvme_class = class_create(THIS_MODULE, "nvme");
Alexey Khoroshilovc7270402015-03-07 01:43:41 +03003327 if (IS_ERR(nvme_class)) {
3328 result = PTR_ERR(nvme_class);
Keith Buschb3fffde2015-02-03 11:21:42 -07003329 goto unregister_chrdev;
Alexey Khoroshilovc7270402015-03-07 01:43:41 +03003330 }
Keith Buschb3fffde2015-02-03 11:21:42 -07003331
Keith Buschf3db22f2014-06-11 11:51:35 -06003332 result = pci_register_driver(&nvme_driver);
3333 if (result)
Keith Buschb3fffde2015-02-03 11:21:42 -07003334 goto destroy_class;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003335 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003336
Keith Buschb3fffde2015-02-03 11:21:42 -07003337 destroy_class:
3338 class_destroy(nvme_class);
3339 unregister_chrdev:
3340 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003341 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003342 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003343 kill_workq:
3344 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003345 return result;
3346}
3347
3348static void __exit nvme_exit(void)
3349{
3350 pci_unregister_driver(&nvme_driver);
3351 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003352 destroy_workqueue(nvme_workq);
Keith Buschb3fffde2015-02-03 11:21:42 -07003353 class_destroy(nvme_class);
3354 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Dan McLeranb9afca32014-04-07 17:10:11 -06003355 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04003356 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003357}
3358
3359MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
3360MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07003361MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003362module_init(nvme_init);
3363module_exit(nvme_exit);