blob: 27d74490ff87599e9c153dc2b1243e1d7a3bed7b [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 LIST_HEAD(dev_list);
83static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070084static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060085static wait_queue_head_t nvme_kthread_wait;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050086
Keith Buschb3fffde2015-02-03 11:21:42 -070087static struct class *nvme_class;
88
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010089struct nvme_dev;
90struct nvme_queue;
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +010091struct nvme_iod;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010092
Christoph Hellwig906678922015-10-02 18:49:23 +020093static int __nvme_reset(struct nvme_dev *dev);
Keith Busch4cc06522015-06-05 10:30:08 -060094static int nvme_reset(struct nvme_dev *dev);
Jens Axboea0fa9642015-11-03 20:37:26 -070095static void nvme_process_cq(struct nvme_queue *nvmeq);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +010096static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +020097static void nvme_dead_ctrl(struct nvme_dev *dev);
Keith Buschd4b4ff82013-12-10 13:10:37 -070098
Keith Busch4d115422013-12-10 13:10:40 -070099struct async_cmd_info {
100 struct kthread_work work;
101 struct kthread_worker *worker;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700102 struct request *req;
Keith Busch4d115422013-12-10 13:10:40 -0700103 u32 result;
104 int status;
105 void *ctx;
106};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500107
108/*
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100109 * Represents an NVM Express device. Each nvme_dev is a PCI function.
110 */
111struct nvme_dev {
112 struct list_head node;
113 struct nvme_queue **queues;
114 struct blk_mq_tag_set tagset;
115 struct blk_mq_tag_set admin_tagset;
116 u32 __iomem *dbs;
117 struct device *dev;
118 struct dma_pool *prp_page_pool;
119 struct dma_pool *prp_small_pool;
120 unsigned queue_count;
121 unsigned online_queues;
122 unsigned max_qid;
123 int q_depth;
124 u32 db_stride;
125 u32 ctrl_config;
126 struct msix_entry *entry;
127 void __iomem *bar;
128 struct list_head namespaces;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100129 struct device *device;
130 struct work_struct reset_work;
131 struct work_struct probe_work;
132 struct work_struct scan_work;
133 bool subsystem;
134 u32 max_hw_sectors;
135 u32 stripe_size;
136 u32 page_size;
137 void __iomem *cmb;
138 dma_addr_t cmb_dma_addr;
139 u64 cmb_size;
140 u32 cmbsz;
141
142 struct nvme_ctrl ctrl;
143};
144
145static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
146{
147 return container_of(ctrl, struct nvme_dev, ctrl);
148}
149
150/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500151 * An NVM Express queue. Each device has at least two (one for admin
152 * commands and one for I/O commands).
153 */
154struct nvme_queue {
155 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500156 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500157 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500158 spinlock_t q_lock;
159 struct nvme_command *sq_cmds;
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600160 struct nvme_command __iomem *sq_cmds_io;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500161 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600162 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500163 dma_addr_t sq_dma_addr;
164 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500165 u32 __iomem *q_db;
166 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700167 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500168 u16 sq_head;
169 u16 sq_tail;
170 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700171 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400172 u8 cq_phase;
173 u8 cqe_seen;
Keith Busch4d115422013-12-10 13:10:40 -0700174 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500175};
176
177/*
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200178 * The nvme_iod describes the data in an I/O, including the list of PRP
179 * entries. You can't see it in this data structure because C doesn't let
180 * me express that. Use nvme_alloc_iod to ensure there's enough space
181 * allocated to store the PRP list.
182 */
183struct nvme_iod {
184 unsigned long private; /* For the use of the submitter of the I/O */
185 int npages; /* In the PRP list. 0 means small pool in use */
186 int offset; /* Of PRP list */
187 int nents; /* Used in scatterlist */
188 int length; /* Of data, in bytes */
189 dma_addr_t first_dma;
190 struct scatterlist meta_sg[1]; /* metadata requires single contiguous buffer */
191 struct scatterlist sg[0];
192};
193
194/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500195 * Check we didin't inadvertently grow the command struct
196 */
197static inline void _nvme_check_size(void)
198{
199 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
200 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
201 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
202 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
203 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400204 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700205 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500206 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
207 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
208 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
209 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600210 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500211}
212
Keith Buschedd10d32014-04-03 16:45:23 -0600213typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400214 struct nvme_completion *);
215
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500216struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400217 nvme_completion_fn fn;
218 void *ctx;
Keith Buschc30341d2013-12-10 13:10:38 -0700219 int aborted;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700220 struct nvme_queue *nvmeq;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700221 struct nvme_iod iod[0];
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500222};
223
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700224/*
225 * Max size of iod being embedded in the request payload
226 */
227#define NVME_INT_PAGES 2
228#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->page_size)
Chong Yuanfda631f2015-03-27 09:21:32 +0800229#define NVME_INT_MASK 0x01
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700230
231/*
232 * Will slightly overestimate the number of pages needed. This is OK
233 * as it only leads to a small amount of wasted memory for the lifetime of
234 * the I/O.
235 */
236static int nvme_npages(unsigned size, struct nvme_dev *dev)
237{
238 unsigned nprps = DIV_ROUND_UP(size + dev->page_size, dev->page_size);
239 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
240}
241
242static unsigned int nvme_cmd_size(struct nvme_dev *dev)
243{
244 unsigned int ret = sizeof(struct nvme_cmd_info);
245
246 ret += sizeof(struct nvme_iod);
247 ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
248 ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
249
250 return ret;
251}
252
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700253static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
254 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500255{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700256 struct nvme_dev *dev = data;
257 struct nvme_queue *nvmeq = dev->queues[0];
258
Keith Busch42483222015-06-01 09:29:54 -0600259 WARN_ON(hctx_idx != 0);
260 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
261 WARN_ON(nvmeq->tags);
262
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700263 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600264 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700265 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500266}
267
Keith Busch4af0e212015-06-08 10:08:13 -0600268static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
269{
270 struct nvme_queue *nvmeq = hctx->driver_data;
271
272 nvmeq->tags = NULL;
273}
274
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700275static int nvme_admin_init_request(void *data, struct request *req,
276 unsigned int hctx_idx, unsigned int rq_idx,
277 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600278{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700279 struct nvme_dev *dev = data;
280 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
281 struct nvme_queue *nvmeq = dev->queues[0];
282
283 BUG_ON(!nvmeq);
284 cmd->nvmeq = nvmeq;
285 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600286}
287
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700288static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
289 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500290{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700291 struct nvme_dev *dev = data;
Keith Busch42483222015-06-01 09:29:54 -0600292 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500293
Keith Busch42483222015-06-01 09:29:54 -0600294 if (!nvmeq->tags)
295 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500296
Keith Busch42483222015-06-01 09:29:54 -0600297 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700298 hctx->driver_data = nvmeq;
299 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500300}
301
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700302static int nvme_init_request(void *data, struct request *req,
303 unsigned int hctx_idx, unsigned int rq_idx,
304 unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500305{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700306 struct nvme_dev *dev = data;
307 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
308 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
309
310 BUG_ON(!nvmeq);
311 cmd->nvmeq = nvmeq;
312 return 0;
313}
314
315static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
316 nvme_completion_fn handler)
317{
318 cmd->fn = handler;
319 cmd->ctx = ctx;
320 cmd->aborted = 0;
Keith Buschc917dfe2015-01-07 18:55:48 -0700321 blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500322}
323
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700324static void *iod_get_private(struct nvme_iod *iod)
325{
326 return (void *) (iod->private & ~0x1UL);
327}
328
329/*
330 * If bit 0 is set, the iod is embedded in the request payload.
331 */
332static bool iod_should_kfree(struct nvme_iod *iod)
333{
Chong Yuanfda631f2015-03-27 09:21:32 +0800334 return (iod->private & NVME_INT_MASK) == 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700335}
336
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400337/* Special values must be less than 0x1000 */
338#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500339#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
340#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
341#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500342
Keith Buschedd10d32014-04-03 16:45:23 -0600343static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400344 struct nvme_completion *cqe)
345{
346 if (ctx == CMD_CTX_CANCELLED)
347 return;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400348 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600349 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400350 "completed id %d twice on queue %d\n",
351 cqe->command_id, le16_to_cpup(&cqe->sq_id));
352 return;
353 }
354 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600355 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400356 "invalid id %d completed on queue %d\n",
357 cqe->command_id, le16_to_cpup(&cqe->sq_id));
358 return;
359 }
Keith Buschedd10d32014-04-03 16:45:23 -0600360 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400361}
362
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700363static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
364{
365 void *ctx;
366
367 if (fn)
368 *fn = cmd->fn;
369 ctx = cmd->ctx;
370 cmd->fn = special_completion;
371 cmd->ctx = CMD_CTX_CANCELLED;
372 return ctx;
373}
374
375static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
376 struct nvme_completion *cqe)
377{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700378 u32 result = le32_to_cpup(&cqe->result);
379 u16 status = le16_to_cpup(&cqe->status) >> 1;
380
381 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100382 ++nvmeq->dev->ctrl.event_limit;
Keith Buscha5768aa2015-06-01 14:28:14 -0600383 if (status != NVME_SC_SUCCESS)
384 return;
385
386 switch (result & 0xff07) {
387 case NVME_AER_NOTICE_NS_CHANGED:
388 dev_info(nvmeq->q_dmadev, "rescanning\n");
389 schedule_work(&nvmeq->dev->scan_work);
390 default:
391 dev_warn(nvmeq->q_dmadev, "async event result %08x\n", result);
392 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700393}
394
395static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
396 struct nvme_completion *cqe)
397{
398 struct request *req = ctx;
399
400 u16 status = le16_to_cpup(&cqe->status) >> 1;
401 u32 result = le32_to_cpup(&cqe->result);
402
Keith Busch42483222015-06-01 09:29:54 -0600403 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700404
405 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100406 ++nvmeq->dev->ctrl.abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700407}
408
Keith Buschedd10d32014-04-03 16:45:23 -0600409static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700410 struct nvme_completion *cqe)
411{
412 struct async_cmd_info *cmdinfo = ctx;
413 cmdinfo->result = le32_to_cpup(&cqe->result);
414 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
415 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
Keith Busch42483222015-06-01 09:29:54 -0600416 blk_mq_free_request(cmdinfo->req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700417}
418
419static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
420 unsigned int tag)
421{
Keith Busch42483222015-06-01 09:29:54 -0600422 struct request *req = blk_mq_tag_to_rq(*nvmeq->tags, tag);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700423
424 return blk_mq_rq_to_pdu(req);
Keith Busch4d115422013-12-10 13:10:40 -0700425}
426
Matthew Wilcox184d2942011-05-11 21:36:38 -0400427/*
428 * Called with local interrupts disabled and the q_lock held. May not sleep.
429 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700430static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400431 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500432{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700433 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400434 void *ctx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700435 if (tag >= nvmeq->q_depth) {
436 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500437 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400438 }
Keith Busch859361a2012-08-02 14:05:59 -0600439 if (fn)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700440 *fn = cmd->fn;
441 ctx = cmd->ctx;
442 cmd->fn = special_completion;
443 cmd->ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400444 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500445}
446
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500447/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400448 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500449 * @nvmeq: The queue to use
450 * @cmd: The command to send
451 *
452 * Safe to use from interrupt context
453 */
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530454static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
455 struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500456{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700457 u16 tail = nvmeq->sq_tail;
458
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600459 if (nvmeq->sq_cmds_io)
460 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
461 else
462 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
463
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500464 if (++tail == nvmeq->q_depth)
465 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500466 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500467 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500468}
469
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530470static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700471{
472 unsigned long flags;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700473 spin_lock_irqsave(&nvmeq->q_lock, flags);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530474 __nvme_submit_cmd(nvmeq, cmd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700475 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700476}
477
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500478static __le64 **iod_list(struct nvme_iod *iod)
479{
480 return ((void *)iod) + iod->offset;
481}
482
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700483static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
484 unsigned nseg, unsigned long private)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500485{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700486 iod->private = private;
487 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
488 iod->npages = -1;
489 iod->length = nbytes;
490 iod->nents = 0;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500491}
492
493static struct nvme_iod *
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700494__nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
495 unsigned long priv, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500496{
497 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700498 sizeof(__le64 *) * nvme_npages(bytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500499 sizeof(struct scatterlist) * nseg, gfp);
500
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700501 if (iod)
502 iod_init(iod, bytes, nseg, priv);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500503
504 return iod;
505}
506
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700507static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
508 gfp_t gfp)
509{
510 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
511 sizeof(struct nvme_dsm_range);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700512 struct nvme_iod *iod;
513
514 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
515 size <= NVME_INT_BYTES(dev)) {
516 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
517
518 iod = cmd->iod;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700519 iod_init(iod, size, rq->nr_phys_segments,
Chong Yuanfda631f2015-03-27 09:21:32 +0800520 (unsigned long) rq | NVME_INT_MASK);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700521 return iod;
522 }
523
524 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
525 (unsigned long) rq, gfp);
526}
527
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200528static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500529{
Keith Busch1d090622014-06-23 11:34:01 -0600530 const int last_prp = dev->page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500531 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500532 __le64 **list = iod_list(iod);
533 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500534
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500535 if (iod->npages == 0)
536 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
537 for (i = 0; i < iod->npages; i++) {
538 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500539 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500540 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500541 prp_dma = next_prp_dma;
542 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700543
544 if (iod_should_kfree(iod))
545 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500546}
547
Keith Busch52b68d72015-02-23 09:16:21 -0700548#ifdef CONFIG_BLK_DEV_INTEGRITY
Keith Busche1e5e562015-02-19 13:39:03 -0700549static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
550{
551 if (be32_to_cpu(pi->ref_tag) == v)
552 pi->ref_tag = cpu_to_be32(p);
553}
554
555static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
556{
557 if (be32_to_cpu(pi->ref_tag) == p)
558 pi->ref_tag = cpu_to_be32(v);
559}
560
561/**
562 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
563 *
564 * The virtual start sector is the one that was originally submitted by the
565 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
566 * start sector may be different. Remap protection information to match the
567 * physical LBA on writes, and back to the original seed on reads.
568 *
569 * Type 0 and 3 do not have a ref tag, so no remapping required.
570 */
571static void nvme_dif_remap(struct request *req,
572 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
573{
574 struct nvme_ns *ns = req->rq_disk->private_data;
575 struct bio_integrity_payload *bip;
576 struct t10_pi_tuple *pi;
577 void *p, *pmap;
578 u32 i, nlb, ts, phys, virt;
579
580 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
581 return;
582
583 bip = bio_integrity(req->bio);
584 if (!bip)
585 return;
586
587 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
Keith Busche1e5e562015-02-19 13:39:03 -0700588
589 p = pmap;
590 virt = bip_get_seed(bip);
591 phys = nvme_block_nr(ns, blk_rq_pos(req));
592 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
Dan Williamsac6fc482015-10-21 13:20:18 -0400593 ts = ns->disk->queue->integrity.tuple_size;
Keith Busche1e5e562015-02-19 13:39:03 -0700594
595 for (i = 0; i < nlb; i++, virt++, phys++) {
596 pi = (struct t10_pi_tuple *)p;
597 dif_swap(phys, virt, pi);
598 p += ts;
599 }
600 kunmap_atomic(pmap);
601}
Keith Busch52b68d72015-02-23 09:16:21 -0700602#else /* CONFIG_BLK_DEV_INTEGRITY */
603static void nvme_dif_remap(struct request *req,
604 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
605{
606}
607static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
608{
609}
610static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
611{
612}
Keith Busch52b68d72015-02-23 09:16:21 -0700613#endif
614
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700615static void req_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500616 struct nvme_completion *cqe)
617{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500618 struct nvme_iod *iod = ctx;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700619 struct request *req = iod_get_private(iod);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700620 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500621 u16 status = le16_to_cpup(&cqe->status) >> 1;
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200622 int error = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500623
Keith Buschedd10d32014-04-03 16:45:23 -0600624 if (unlikely(status)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700625 if (!(status & NVME_SC_DNR || blk_noretry_request(req))
626 && (jiffies - req->start_time) < req->timeout) {
Keith Buschc9d3bf82015-01-07 18:55:52 -0700627 unsigned long flags;
628
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100629 nvme_unmap_data(nvmeq->dev, iod);
630
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700631 blk_mq_requeue_request(req);
Keith Buschc9d3bf82015-01-07 18:55:52 -0700632 spin_lock_irqsave(req->q->queue_lock, flags);
633 if (!blk_queue_stopped(req->q))
634 blk_mq_kick_requeue_list(req->q);
635 spin_unlock_irqrestore(req->q->queue_lock, flags);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100636 return;
Keith Buschedd10d32014-04-03 16:45:23 -0600637 }
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200638
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200639 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
Keith Busch17188bb42015-06-08 10:08:14 -0600640 if (cmd_rq->ctx == CMD_CTX_CANCELLED)
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200641 error = -EINTR;
642 else
643 error = status;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200644 } else {
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200645 error = nvme_error_status(status);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200646 }
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200647 }
648
Keith Buscha0a931d2015-05-22 12:28:31 -0600649 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
650 u32 result = le32_to_cpup(&cqe->result);
651 req->special = (void *)(uintptr_t)result;
652 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700653
654 if (cmd_rq->aborted)
Christoph Hellwige75ec752015-05-22 11:12:39 +0200655 dev_warn(nvmeq->dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700656 "completing aborted command with status:%04x\n",
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200657 error);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700658
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100659 nvme_unmap_data(nvmeq->dev, iod);
660 blk_mq_complete_request(req, error);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500661}
662
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200663static bool nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod,
664 int total_len)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500665{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500666 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500667 int length = total_len;
668 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500669 int dma_len = sg_dma_len(sg);
670 u64 dma_addr = sg_dma_address(sg);
Murali Iyerf137e0f2015-03-26 11:07:51 -0500671 u32 page_size = dev->page_size;
672 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500673 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500674 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500675 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500676 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500677
Keith Busch1d090622014-06-23 11:34:01 -0600678 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500679 if (length <= 0)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200680 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500681
Keith Busch1d090622014-06-23 11:34:01 -0600682 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500683 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600684 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500685 } else {
686 sg = sg_next(sg);
687 dma_addr = sg_dma_address(sg);
688 dma_len = sg_dma_len(sg);
689 }
690
Keith Busch1d090622014-06-23 11:34:01 -0600691 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600692 iod->first_dma = dma_addr;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200693 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500694 }
695
Keith Busch1d090622014-06-23 11:34:01 -0600696 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500697 if (nprps <= (256 / 8)) {
698 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500699 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500700 } else {
701 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500702 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500703 }
704
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200705 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400706 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600707 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500708 iod->npages = -1;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200709 return false;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400710 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500711 list[0] = prp_list;
712 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500713 i = 0;
714 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600715 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500716 __le64 *old_prp_list = prp_list;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200717 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500718 if (!prp_list)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200719 return false;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500720 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400721 prp_list[0] = old_prp_list[i - 1];
722 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
723 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500724 }
725 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600726 dma_len -= page_size;
727 dma_addr += page_size;
728 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500729 if (length <= 0)
730 break;
731 if (dma_len > 0)
732 continue;
733 BUG_ON(dma_len < 0);
734 sg = sg_next(sg);
735 dma_addr = sg_dma_address(sg);
736 dma_len = sg_dma_len(sg);
737 }
738
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200739 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500740}
741
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200742static int nvme_map_data(struct nvme_dev *dev, struct nvme_iod *iod,
743 struct nvme_command *cmnd)
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200744{
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200745 struct request *req = iod_get_private(iod);
746 struct request_queue *q = req->q;
747 enum dma_data_direction dma_dir = rq_data_dir(req) ?
748 DMA_TO_DEVICE : DMA_FROM_DEVICE;
749 int ret = BLK_MQ_RQ_QUEUE_ERROR;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200750
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200751 sg_init_table(iod->sg, req->nr_phys_segments);
752 iod->nents = blk_rq_map_sg(q, req, iod->sg);
753 if (!iod->nents)
754 goto out;
755
756 ret = BLK_MQ_RQ_QUEUE_BUSY;
757 if (!dma_map_sg(dev->dev, iod->sg, iod->nents, dma_dir))
758 goto out;
759
760 if (!nvme_setup_prps(dev, iod, blk_rq_bytes(req)))
761 goto out_unmap;
762
763 ret = BLK_MQ_RQ_QUEUE_ERROR;
764 if (blk_integrity_rq(req)) {
765 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
766 goto out_unmap;
767
768 sg_init_table(iod->meta_sg, 1);
769 if (blk_rq_map_integrity_sg(q, req->bio, iod->meta_sg) != 1)
770 goto out_unmap;
771
772 if (rq_data_dir(req))
773 nvme_dif_remap(req, nvme_dif_prep);
774
775 if (!dma_map_sg(dev->dev, iod->meta_sg, 1, dma_dir))
776 goto out_unmap;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200777 }
778
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200779 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
780 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
781 if (blk_integrity_rq(req))
782 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(iod->meta_sg));
783 return BLK_MQ_RQ_QUEUE_OK;
784
785out_unmap:
786 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
787out:
788 return ret;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200789}
790
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100791static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod)
792{
793 struct request *req = iod_get_private(iod);
794 enum dma_data_direction dma_dir = rq_data_dir(req) ?
795 DMA_TO_DEVICE : DMA_FROM_DEVICE;
796
797 if (iod->nents) {
798 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
799 if (blk_integrity_rq(req)) {
800 if (!rq_data_dir(req))
801 nvme_dif_remap(req, nvme_dif_complete);
802 dma_unmap_sg(dev->dev, iod->meta_sg, 1, dma_dir);
803 }
804 }
805
806 nvme_free_iod(dev, iod);
807}
808
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700809/*
810 * We reuse the small pool to allocate the 16-byte range here as it is not
811 * worth having a special pool for these or additional cases to handle freeing
812 * the iod.
813 */
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200814static int nvme_setup_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
815 struct nvme_iod *iod, struct nvme_command *cmnd)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700816{
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200817 struct request *req = iod_get_private(iod);
818 struct nvme_dsm_range *range;
819
820 range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC,
821 &iod->first_dma);
822 if (!range)
823 return BLK_MQ_RQ_QUEUE_BUSY;
824 iod_list(iod)[0] = (__le64 *)range;
825 iod->npages = 0;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700826
Keith Busch0e5e4f02012-11-09 16:33:05 -0700827 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700828 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
829 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700830
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200831 memset(cmnd, 0, sizeof(*cmnd));
832 cmnd->dsm.opcode = nvme_cmd_dsm;
833 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
834 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
835 cmnd->dsm.nr = 0;
836 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
837 return BLK_MQ_RQ_QUEUE_OK;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700838}
839
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200840/*
841 * NOTE: ns is NULL when called on the admin queue.
842 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700843static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
844 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600845{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700846 struct nvme_ns *ns = hctx->queue->queuedata;
847 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200848 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700849 struct request *req = bd->rq;
850 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
Keith Buschedd10d32014-04-03 16:45:23 -0600851 struct nvme_iod *iod;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200852 struct nvme_command cmnd;
853 int ret = BLK_MQ_RQ_QUEUE_OK;
Keith Buschedd10d32014-04-03 16:45:23 -0600854
Keith Busche1e5e562015-02-19 13:39:03 -0700855 /*
856 * If formated with metadata, require the block layer provide a buffer
857 * unless this namespace is formated such that the metadata can be
858 * stripped/generated by the controller with PRACT=1.
859 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200860 if (ns && ns->ms && !blk_integrity_rq(req)) {
Keith Busch71feb362015-06-19 11:07:30 -0600861 if (!(ns->pi_type && ns->ms == 8) &&
862 req->cmd_type != REQ_TYPE_DRV_PRIV) {
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200863 blk_mq_complete_request(req, -EFAULT);
Keith Busche1e5e562015-02-19 13:39:03 -0700864 return BLK_MQ_RQ_QUEUE_OK;
865 }
866 }
867
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200868 iod = nvme_alloc_iod(req, dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600869 if (!iod)
Jens Axboefe543032014-12-11 13:58:39 -0700870 return BLK_MQ_RQ_QUEUE_BUSY;
Keith Buschedd10d32014-04-03 16:45:23 -0600871
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700872 if (req->cmd_flags & REQ_DISCARD) {
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200873 ret = nvme_setup_discard(nvmeq, ns, iod, &cmnd);
874 } else {
875 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
876 memcpy(&cmnd, req->cmd, sizeof(cmnd));
877 else if (req->cmd_flags & REQ_FLUSH)
878 nvme_setup_flush(ns, &cmnd);
879 else
880 nvme_setup_rw(ns, req, &cmnd);
Keith Buschedd10d32014-04-03 16:45:23 -0600881
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200882 if (req->nr_phys_segments)
883 ret = nvme_map_data(dev, iod, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700884 }
885
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200886 if (ret)
887 goto out;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700888
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200889 cmnd.common.command_id = req->tag;
890 nvme_set_info(cmd, iod, req_completion);
891
892 spin_lock_irq(&nvmeq->q_lock);
893 __nvme_submit_cmd(nvmeq, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700894 nvme_process_cq(nvmeq);
895 spin_unlock_irq(&nvmeq->q_lock);
896 return BLK_MQ_RQ_QUEUE_OK;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200897out:
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200898 nvme_free_iod(dev, iod);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200899 return ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500900}
901
Jens Axboea0fa9642015-11-03 20:37:26 -0700902static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500903{
Matthew Wilcox82123462011-01-20 13:24:06 -0500904 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500905
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500906 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500907 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500908
909 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400910 void *ctx;
911 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500912 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500913 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500914 break;
915 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
916 if (++head == nvmeq->q_depth) {
917 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500918 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500919 }
Jens Axboea0fa9642015-11-03 20:37:26 -0700920 if (tag && *tag == cqe.command_id)
921 *tag = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700922 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600923 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500924 }
925
926 /* If the controller ignores the cq head doorbell and continuously
927 * writes to the queue, it is theoretically possible to wrap around
928 * the queue twice and mistakenly return IRQ_NONE. Linux only
929 * requires that 0.1% of your interrupts are handled, so this isn't
930 * a big problem.
931 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500932 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Jens Axboea0fa9642015-11-03 20:37:26 -0700933 return;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500934
Keith Busch604e8c82015-11-20 08:38:13 -0700935 if (likely(nvmeq->cq_vector >= 0))
936 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500937 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500938 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500939
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400940 nvmeq->cqe_seen = 1;
Jens Axboea0fa9642015-11-03 20:37:26 -0700941}
942
943static void nvme_process_cq(struct nvme_queue *nvmeq)
944{
945 __nvme_process_cq(nvmeq, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500946}
947
948static irqreturn_t nvme_irq(int irq, void *data)
949{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500950 irqreturn_t result;
951 struct nvme_queue *nvmeq = data;
952 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400953 nvme_process_cq(nvmeq);
954 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
955 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500956 spin_unlock(&nvmeq->q_lock);
957 return result;
958}
959
960static irqreturn_t nvme_irq_check(int irq, void *data)
961{
962 struct nvme_queue *nvmeq = data;
963 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
964 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
965 return IRQ_NONE;
966 return IRQ_WAKE_THREAD;
967}
968
Jens Axboea0fa9642015-11-03 20:37:26 -0700969static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
970{
971 struct nvme_queue *nvmeq = hctx->driver_data;
972
973 if ((le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) ==
974 nvmeq->cq_phase) {
975 spin_lock_irq(&nvmeq->q_lock);
976 __nvme_process_cq(nvmeq, &tag);
977 spin_unlock_irq(&nvmeq->q_lock);
978
979 if (tag == -1)
980 return 1;
981 }
982
983 return 0;
984}
985
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700986static int nvme_submit_async_admin_req(struct nvme_dev *dev)
987{
988 struct nvme_queue *nvmeq = dev->queues[0];
989 struct nvme_command c;
990 struct nvme_cmd_info *cmd_info;
991 struct request *req;
992
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100993 req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE,
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100994 BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED);
Dan Carpenter9f173b32014-11-05 23:39:09 +0300995 if (IS_ERR(req))
996 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700997
Keith Buschc917dfe2015-01-07 18:55:48 -0700998 req->cmd_flags |= REQ_NO_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700999 cmd_info = blk_mq_rq_to_pdu(req);
Keith Busch1efccc92015-03-31 10:37:17 -06001000 nvme_set_info(cmd_info, NULL, async_req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001001
1002 memset(&c, 0, sizeof(c));
1003 c.common.opcode = nvme_admin_async_event;
1004 c.common.command_id = req->tag;
1005
Keith Busch42483222015-06-01 09:29:54 -06001006 blk_mq_free_request(req);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301007 __nvme_submit_cmd(nvmeq, &c);
1008 return 0;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001009}
1010
1011static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
Keith Busch4d115422013-12-10 13:10:40 -07001012 struct nvme_command *cmd,
1013 struct async_cmd_info *cmdinfo, unsigned timeout)
1014{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001015 struct nvme_queue *nvmeq = dev->queues[0];
1016 struct request *req;
1017 struct nvme_cmd_info *cmd_rq;
Keith Busch4d115422013-12-10 13:10:40 -07001018
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001019 req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE, 0);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001020 if (IS_ERR(req))
1021 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001022
1023 req->timeout = timeout;
1024 cmd_rq = blk_mq_rq_to_pdu(req);
1025 cmdinfo->req = req;
1026 nvme_set_info(cmd_rq, cmdinfo, async_completion);
Keith Busch4d115422013-12-10 13:10:40 -07001027 cmdinfo->status = -EINTR;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001028
1029 cmd->common.command_id = req->tag;
1030
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301031 nvme_submit_cmd(nvmeq, cmd);
1032 return 0;
Keith Busch4d115422013-12-10 13:10:40 -07001033}
1034
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001035static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1036{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001037 struct nvme_command c;
1038
1039 memset(&c, 0, sizeof(c));
1040 c.delete_queue.opcode = opcode;
1041 c.delete_queue.qid = cpu_to_le16(id);
1042
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001043 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001044}
1045
1046static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1047 struct nvme_queue *nvmeq)
1048{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001049 struct nvme_command c;
1050 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1051
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001052 /*
1053 * Note: we (ab)use the fact the the prp fields survive if no data
1054 * is attached to the request.
1055 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001056 memset(&c, 0, sizeof(c));
1057 c.create_cq.opcode = nvme_admin_create_cq;
1058 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1059 c.create_cq.cqid = cpu_to_le16(qid);
1060 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1061 c.create_cq.cq_flags = cpu_to_le16(flags);
1062 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1063
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001064 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001065}
1066
1067static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1068 struct nvme_queue *nvmeq)
1069{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001070 struct nvme_command c;
1071 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1072
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001073 /*
1074 * Note: we (ab)use the fact the the prp fields survive if no data
1075 * is attached to the request.
1076 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001077 memset(&c, 0, sizeof(c));
1078 c.create_sq.opcode = nvme_admin_create_sq;
1079 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1080 c.create_sq.sqid = cpu_to_le16(qid);
1081 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1082 c.create_sq.sq_flags = cpu_to_le16(flags);
1083 c.create_sq.cqid = cpu_to_le16(qid);
1084
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001085 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001086}
1087
1088static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1089{
1090 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1091}
1092
1093static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1094{
1095 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1096}
1097
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001098/**
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001099 * nvme_abort_req - Attempt aborting a request
Keith Buschc30341d2013-12-10 13:10:38 -07001100 *
1101 * Schedule controller reset if the command was already aborted once before and
1102 * still hasn't been returned to the driver, or if this is the admin queue.
1103 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001104static void nvme_abort_req(struct request *req)
Keith Buschc30341d2013-12-10 13:10:38 -07001105{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001106 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1107 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001108 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001109 struct request *abort_req;
1110 struct nvme_cmd_info *abort_cmd;
1111 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -07001112
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001113 if (!nvmeq->qid || cmd_rq->aborted) {
Christoph Hellwig906678922015-10-02 18:49:23 +02001114 spin_lock(&dev_list_lock);
1115 if (!__nvme_reset(dev)) {
1116 dev_warn(dev->dev,
1117 "I/O %d QID %d timeout, reset controller\n",
1118 req->tag, nvmeq->qid);
1119 }
1120 spin_unlock(&dev_list_lock);
Keith Buschc30341d2013-12-10 13:10:38 -07001121 return;
1122 }
1123
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001124 if (!dev->ctrl.abort_limit)
Keith Buschc30341d2013-12-10 13:10:38 -07001125 return;
1126
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001127 abort_req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE,
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001128 BLK_MQ_REQ_NOWAIT);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001129 if (IS_ERR(abort_req))
Keith Buschc30341d2013-12-10 13:10:38 -07001130 return;
1131
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001132 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1133 nvme_set_info(abort_cmd, abort_req, abort_completion);
1134
Keith Buschc30341d2013-12-10 13:10:38 -07001135 memset(&cmd, 0, sizeof(cmd));
1136 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001137 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001138 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001139 cmd.abort.command_id = abort_req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001140
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001141 --dev->ctrl.abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001142 cmd_rq->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -07001143
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001144 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", req->tag,
Keith Buschc30341d2013-12-10 13:10:38 -07001145 nvmeq->qid);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301146 nvme_submit_cmd(dev->queues[0], &cmd);
Keith Buschc30341d2013-12-10 13:10:38 -07001147}
1148
Keith Busch42483222015-06-01 09:29:54 -06001149static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001150{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001151 struct nvme_queue *nvmeq = data;
1152 void *ctx;
1153 nvme_completion_fn fn;
1154 struct nvme_cmd_info *cmd;
Keith Buschcef6a942015-01-07 18:55:51 -07001155 struct nvme_completion cqe;
1156
1157 if (!blk_mq_request_started(req))
1158 return;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001159
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001160 cmd = blk_mq_rq_to_pdu(req);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001161
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001162 if (cmd->ctx == CMD_CTX_CANCELLED)
1163 return;
1164
Keith Buschcef6a942015-01-07 18:55:51 -07001165 if (blk_queue_dying(req->q))
1166 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1167 else
1168 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1169
1170
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001171 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1172 req->tag, nvmeq->qid);
1173 ctx = cancel_cmd_info(cmd, &fn);
1174 fn(nvmeq, ctx, &cqe);
1175}
1176
1177static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
1178{
1179 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
1180 struct nvme_queue *nvmeq = cmd->nvmeq;
1181
Keith Busch07836e62015-02-19 10:34:48 -07001182 dev_warn(nvmeq->q_dmadev, "Timeout I/O %d QID %d\n", req->tag,
1183 nvmeq->qid);
1184 spin_lock_irq(&nvmeq->q_lock);
1185 nvme_abort_req(req);
1186 spin_unlock_irq(&nvmeq->q_lock);
1187
Keith Busch7a509a62015-01-07 18:55:53 -07001188 /*
1189 * The aborted req will be completed on receiving the abort req.
1190 * We enable the timer again. If hit twice, it'll cause a device reset,
1191 * as the device then is in a faulty state.
1192 */
Keith Busch07836e62015-02-19 10:34:48 -07001193 return BLK_EH_RESET_TIMER;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001194}
1195
Keith Buschf435c282014-07-07 09:14:42 -06001196static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001197{
1198 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1199 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001200 if (nvmeq->sq_cmds)
1201 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
Matthew Wilcox9e866772012-08-03 13:55:56 -04001202 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1203 kfree(nvmeq);
1204}
1205
Keith Buscha1a5ef92013-12-16 13:50:00 -05001206static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001207{
1208 int i;
1209
Keith Buscha1a5ef92013-12-16 13:50:00 -05001210 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001211 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001212 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001213 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001214 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001215 }
Keith Busch22404272013-07-15 15:02:20 -06001216}
1217
Keith Busch4d115422013-12-10 13:10:40 -07001218/**
1219 * nvme_suspend_queue - put queue into suspended state
1220 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001221 */
1222static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001223{
Keith Busch2b25d982014-12-22 12:59:04 -07001224 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001225
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001226 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001227 if (nvmeq->cq_vector == -1) {
1228 spin_unlock_irq(&nvmeq->q_lock);
1229 return 1;
1230 }
1231 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Keith Busch42f61422014-03-24 10:46:25 -06001232 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001233 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001234 spin_unlock_irq(&nvmeq->q_lock);
1235
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001236 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
1237 blk_mq_freeze_queue_start(nvmeq->dev->ctrl.admin_q);
Keith Busch6df3dbc2015-03-26 13:49:33 -06001238
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001239 irq_set_affinity_hint(vector, NULL);
1240 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001241
Keith Busch4d115422013-12-10 13:10:40 -07001242 return 0;
1243}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001244
Keith Busch4d115422013-12-10 13:10:40 -07001245static void nvme_clear_queue(struct nvme_queue *nvmeq)
1246{
Keith Busch22404272013-07-15 15:02:20 -06001247 spin_lock_irq(&nvmeq->q_lock);
Keith Busch42483222015-06-01 09:29:54 -06001248 if (nvmeq->tags && *nvmeq->tags)
1249 blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001250 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001251}
1252
Keith Busch4d115422013-12-10 13:10:40 -07001253static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1254{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001255 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001256
1257 if (!nvmeq)
1258 return;
1259 if (nvme_suspend_queue(nvmeq))
1260 return;
1261
Keith Busch0e53d182013-12-10 13:10:39 -07001262 /* Don't tell the adapter to delete the admin queue.
1263 * Don't tell a removed adapter to delete IO queues. */
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001264 if (qid && readl(dev->bar + NVME_REG_CSTS) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001265 adapter_delete_sq(dev, qid);
1266 adapter_delete_cq(dev, qid);
1267 }
Keith Busch07836e62015-02-19 10:34:48 -07001268
1269 spin_lock_irq(&nvmeq->q_lock);
1270 nvme_process_cq(nvmeq);
1271 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001272}
1273
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001274static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1275 int entry_size)
1276{
1277 int q_depth = dev->q_depth;
1278 unsigned q_size_aligned = roundup(q_depth * entry_size, dev->page_size);
1279
1280 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001281 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
1282 mem_per_q = round_down(mem_per_q, dev->page_size);
1283 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001284
1285 /*
1286 * Ensure the reduced q_depth is above some threshold where it
1287 * would be better to map queues in system memory with the
1288 * original depth
1289 */
1290 if (q_depth < 64)
1291 return -ENOMEM;
1292 }
1293
1294 return q_depth;
1295}
1296
1297static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1298 int qid, int depth)
1299{
1300 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
1301 unsigned offset = (qid - 1) *
1302 roundup(SQ_SIZE(depth), dev->page_size);
1303 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1304 nvmeq->sq_cmds_io = dev->cmb + offset;
1305 } else {
1306 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1307 &nvmeq->sq_dma_addr, GFP_KERNEL);
1308 if (!nvmeq->sq_cmds)
1309 return -ENOMEM;
1310 }
1311
1312 return 0;
1313}
1314
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001315static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Keith Busch2b25d982014-12-22 12:59:04 -07001316 int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001317{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001318 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001319 if (!nvmeq)
1320 return NULL;
1321
Christoph Hellwige75ec752015-05-22 11:12:39 +02001322 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001323 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001324 if (!nvmeq->cqes)
1325 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001326
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001327 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001328 goto free_cqdma;
1329
Christoph Hellwige75ec752015-05-22 11:12:39 +02001330 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001331 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001332 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001333 dev->ctrl.instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001334 spin_lock_init(&nvmeq->q_lock);
1335 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001336 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001337 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001338 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001339 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001340 nvmeq->cq_vector = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001341 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001342
Jon Derrick36a7e992015-05-27 12:26:23 -06001343 /* make sure queue descriptor is set before queue count, for kthread */
1344 mb();
1345 dev->queue_count++;
1346
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001347 return nvmeq;
1348
1349 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001350 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001351 nvmeq->cq_dma_addr);
1352 free_nvmeq:
1353 kfree(nvmeq);
1354 return NULL;
1355}
1356
Matthew Wilcox30010822011-01-20 09:10:15 -05001357static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1358 const char *name)
1359{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001360 if (use_threaded_interrupts)
1361 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001362 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001363 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001364 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001365 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001366}
1367
Keith Busch22404272013-07-15 15:02:20 -06001368static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001369{
Keith Busch22404272013-07-15 15:02:20 -06001370 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001371
Keith Busch7be50e92014-09-10 15:48:47 -06001372 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001373 nvmeq->sq_tail = 0;
1374 nvmeq->cq_head = 0;
1375 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001376 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001377 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001378 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001379 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001380}
1381
1382static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1383{
1384 struct nvme_dev *dev = nvmeq->dev;
1385 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001386
Keith Busch2b25d982014-12-22 12:59:04 -07001387 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001388 result = adapter_alloc_cq(dev, qid, nvmeq);
1389 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001390 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001391
1392 result = adapter_alloc_sq(dev, qid, nvmeq);
1393 if (result < 0)
1394 goto release_cq;
1395
Matthew Wilcox3193f072014-01-27 15:57:22 -05001396 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001397 if (result < 0)
1398 goto release_sq;
1399
Keith Busch22404272013-07-15 15:02:20 -06001400 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001401 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001402
1403 release_sq:
1404 adapter_delete_sq(dev, qid);
1405 release_cq:
1406 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001407 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001408}
1409
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001410static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1411{
1412 unsigned long timeout;
1413 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1414
1415 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1416
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001417 while ((readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_RDY) != bit) {
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001418 msleep(100);
1419 if (fatal_signal_pending(current))
1420 return -EINTR;
1421 if (time_after(jiffies, timeout)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001422 dev_err(dev->dev,
Matthew Wilcox27e81662014-04-11 11:58:45 -04001423 "Device not ready; aborting %s\n", enabled ?
1424 "initialisation" : "reset");
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001425 return -ENODEV;
1426 }
1427 }
1428
1429 return 0;
1430}
1431
1432/*
1433 * If the device has been passed off to us in an enabled state, just clear
1434 * the enabled bit. The spec says we should set the 'shutdown notification
1435 * bits', but doing so may cause the device to complete commands to the
1436 * admin queue ... and we don't know what memory that might be pointing at!
1437 */
1438static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1439{
Dan McLeran01079522014-06-23 08:24:36 -06001440 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1441 dev->ctrl_config &= ~NVME_CC_ENABLE;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001442 writel(dev->ctrl_config, dev->bar + NVME_REG_CC);
Matthew Wilcox44af1462013-05-04 06:43:17 -04001443
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001444 return nvme_wait_ready(dev, cap, false);
1445}
1446
1447static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1448{
Dan McLeran01079522014-06-23 08:24:36 -06001449 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1450 dev->ctrl_config |= NVME_CC_ENABLE;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001451 writel(dev->ctrl_config, dev->bar + NVME_REG_CC);
Dan McLeran01079522014-06-23 08:24:36 -06001452
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001453 return nvme_wait_ready(dev, cap, true);
1454}
1455
Keith Busch1894d8f2013-07-15 15:02:22 -06001456static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1457{
1458 unsigned long timeout;
Keith Busch1894d8f2013-07-15 15:02:22 -06001459
Dan McLeran01079522014-06-23 08:24:36 -06001460 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1461 dev->ctrl_config |= NVME_CC_SHN_NORMAL;
1462
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001463 writel(dev->ctrl_config, dev->bar + NVME_REG_CC);
Keith Busch1894d8f2013-07-15 15:02:22 -06001464
Dan McLeran2484f402014-07-01 09:33:32 -06001465 timeout = SHUTDOWN_TIMEOUT + jiffies;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001466 while ((readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_SHST_MASK) !=
Keith Busch1894d8f2013-07-15 15:02:22 -06001467 NVME_CSTS_SHST_CMPLT) {
1468 msleep(100);
1469 if (fatal_signal_pending(current))
1470 return -EINTR;
1471 if (time_after(jiffies, timeout)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001472 dev_err(dev->dev,
Keith Busch1894d8f2013-07-15 15:02:22 -06001473 "Device shutdown incomplete; abort shutdown\n");
1474 return -ENODEV;
1475 }
1476 }
1477
1478 return 0;
1479}
1480
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001481static struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001482 .queue_rq = nvme_queue_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001483 .map_queue = blk_mq_map_queue,
1484 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001485 .exit_hctx = nvme_admin_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001486 .init_request = nvme_admin_init_request,
1487 .timeout = nvme_timeout,
1488};
1489
1490static struct blk_mq_ops nvme_mq_ops = {
1491 .queue_rq = nvme_queue_rq,
1492 .map_queue = blk_mq_map_queue,
1493 .init_hctx = nvme_init_hctx,
1494 .init_request = nvme_init_request,
1495 .timeout = nvme_timeout,
Jens Axboea0fa9642015-11-03 20:37:26 -07001496 .poll = nvme_poll,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001497};
1498
Keith Buschea191d22015-01-07 18:55:49 -07001499static void nvme_dev_remove_admin(struct nvme_dev *dev)
1500{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001501 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
1502 blk_cleanup_queue(dev->ctrl.admin_q);
Keith Buschea191d22015-01-07 18:55:49 -07001503 blk_mq_free_tag_set(&dev->admin_tagset);
1504 }
1505}
1506
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001507static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1508{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001509 if (!dev->ctrl.admin_q) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001510 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1511 dev->admin_tagset.nr_hw_queues = 1;
1512 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1;
Keith Busch1efccc92015-03-31 10:37:17 -06001513 dev->admin_tagset.reserved_tags = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001514 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001515 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001516 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001517 dev->admin_tagset.driver_data = dev;
1518
1519 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1520 return -ENOMEM;
1521
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001522 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1523 if (IS_ERR(dev->ctrl.admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001524 blk_mq_free_tag_set(&dev->admin_tagset);
1525 return -ENOMEM;
1526 }
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001527 if (!blk_get_queue(dev->ctrl.admin_q)) {
Keith Buschea191d22015-01-07 18:55:49 -07001528 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001529 dev->ctrl.admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001530 return -ENODEV;
1531 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001532 } else
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001533 blk_mq_unfreeze_queue(dev->ctrl.admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001534
1535 return 0;
1536}
1537
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001538static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001539{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001540 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001541 u32 aqa;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001542 u64 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001543 struct nvme_queue *nvmeq;
Nishanth Aravamudanc5c9f252015-11-24 09:55:05 -07001544 /*
1545 * default to a 4K page size, with the intention to update this
1546 * path in the future to accomodate architectures with differing
1547 * kernel and IO page sizes.
1548 */
1549 unsigned page_shift = 12;
Keith Busch1d090622014-06-23 11:34:01 -06001550 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12;
Keith Busch1d090622014-06-23 11:34:01 -06001551
1552 if (page_shift < dev_page_min) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001553 dev_err(dev->dev,
Keith Busch1d090622014-06-23 11:34:01 -06001554 "Minimum device page size (%u) too large for "
1555 "host (%u)\n", 1 << dev_page_min,
1556 1 << page_shift);
1557 return -ENODEV;
1558 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001559
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001560 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1) ?
Keith Buschdfbac8c2015-08-10 15:20:40 -06001561 NVME_CAP_NSSRC(cap) : 0;
1562
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001563 if (dev->subsystem &&
1564 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1565 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001566
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001567 result = nvme_disable_ctrl(dev, cap);
1568 if (result < 0)
1569 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001570
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001571 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001572 if (!nvmeq) {
Keith Busch2b25d982014-12-22 12:59:04 -07001573 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001574 if (!nvmeq)
1575 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001576 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001577
1578 aqa = nvmeq->q_depth - 1;
1579 aqa |= aqa << 16;
1580
Keith Busch1d090622014-06-23 11:34:01 -06001581 dev->page_size = 1 << page_shift;
1582
Dan McLeran01079522014-06-23 08:24:36 -06001583 dev->ctrl_config = NVME_CC_CSS_NVM;
Keith Busch1d090622014-06-23 11:34:01 -06001584 dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001585 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001586 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001587
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001588 writel(aqa, dev->bar + NVME_REG_AQA);
1589 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1590 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001591
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001592 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001593 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001594 goto free_nvmeq;
1595
Keith Busch2b25d982014-12-22 12:59:04 -07001596 nvmeq->cq_vector = 0;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001597 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001598 if (result) {
1599 nvmeq->cq_vector = -1;
Keith Busch0fb59cb2015-01-07 18:55:50 -07001600 goto free_nvmeq;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001601 }
Keith Busch025c5572013-05-01 13:07:51 -06001602
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001603 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001604
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001605 free_nvmeq:
1606 nvme_free_queues(dev, 0);
1607 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001608}
1609
Jon Derrick81f03fe2015-08-10 15:20:41 -06001610static int nvme_subsys_reset(struct nvme_dev *dev)
1611{
1612 if (!dev->subsystem)
1613 return -ENOTTY;
1614
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001615 writel(0x4E564D65, dev->bar + NVME_REG_NSSR); /* "NVMe" */
Jon Derrick81f03fe2015-08-10 15:20:41 -06001616 return 0;
1617}
1618
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001619static int nvme_kthread(void *data)
1620{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001621 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001622
1623 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001624 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001625 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001626 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001627 int i;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001628 u32 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001629
1630 if ((dev->subsystem && (csts & NVME_CSTS_NSSRO)) ||
1631 csts & NVME_CSTS_CFS) {
Christoph Hellwig906678922015-10-02 18:49:23 +02001632 if (!__nvme_reset(dev)) {
1633 dev_warn(dev->dev,
1634 "Failed status: %x, reset controller\n",
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001635 readl(dev->bar + NVME_REG_CSTS));
Christoph Hellwig906678922015-10-02 18:49:23 +02001636 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07001637 continue;
1638 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001639 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001640 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001641 if (!nvmeq)
1642 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001643 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001644 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06001645
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001646 while (i == 0 && dev->ctrl.event_limit > 0) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001647 if (nvme_submit_async_admin_req(dev))
Keith Busch6fccf932014-06-18 13:58:57 -06001648 break;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001649 dev->ctrl.event_limit--;
Keith Busch6fccf932014-06-18 13:58:57 -06001650 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001651 spin_unlock_irq(&nvmeq->q_lock);
1652 }
1653 }
1654 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001655 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001656 }
1657 return 0;
1658}
1659
Keith Busche1e5e562015-02-19 13:39:03 -07001660static void nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001661{
1662 struct nvme_ns *ns;
1663 struct gendisk *disk;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001664 int node = dev_to_node(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001665
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001666 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001667 if (!ns)
Keith Busche1e5e562015-02-19 13:39:03 -07001668 return;
1669
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001670 ns->queue = blk_mq_init_queue(&dev->tagset);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001671 if (IS_ERR(ns->queue))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001672 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001673 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1674 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001675 ns->ctrl = &dev->ctrl;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001676 ns->queue->queuedata = ns;
1677
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001678 disk = alloc_disk_node(0, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001679 if (!disk)
1680 goto out_free_queue;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001681
Keith Busch188c3562015-10-01 17:14:10 -06001682 kref_init(&ns->kref);
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001683 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001684 ns->disk = disk;
Keith Busche1e5e562015-02-19 13:39:03 -07001685 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
1686 list_add_tail(&ns->list, &dev->namespaces);
1687
Keith Busche9ef4632012-07-24 15:01:04 -06001688 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busche8244102015-08-12 16:17:54 -06001689 if (dev->max_hw_sectors) {
Keith Busch8fc23e02012-07-26 11:29:57 -06001690 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Keith Busche8244102015-08-12 16:17:54 -06001691 blk_queue_max_segments(ns->queue,
Keith Busch6824c5e2015-11-18 16:33:08 -07001692 (dev->max_hw_sectors / (dev->page_size >> 9)) + 1);
Keith Busche8244102015-08-12 16:17:54 -06001693 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001694 if (dev->stripe_size)
1695 blk_queue_chunk_sectors(ns->queue, dev->stripe_size >> 9);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001696 if (dev->ctrl.vwc & NVME_CTRL_VWC_PRESENT)
Keith Buscha7d2ce22014-04-29 11:41:28 -06001697 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
Keith Busch03100aa2015-08-19 14:24:05 -07001698 blk_queue_virt_boundary(ns->queue, dev->page_size - 1);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001699
1700 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001701 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001702 disk->fops = &nvme_fops;
1703 disk->private_data = ns;
1704 disk->queue = ns->queue;
Keith Buschb3fffde2015-02-03 11:21:42 -07001705 disk->driverfs_dev = dev->device;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001706 disk->flags = GENHD_FL_EXT_DEVT;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001707 sprintf(disk->disk_name, "nvme%dn%d", dev->ctrl.instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001708
Keith Busche1e5e562015-02-19 13:39:03 -07001709 /*
1710 * Initialize capacity to 0 until we establish the namespace format and
1711 * setup integrity extentions if necessary. The revalidate_disk after
1712 * add_disk allows the driver to register with integrity if the format
1713 * requires it.
1714 */
1715 set_capacity(disk, 0);
Keith Buscha5768aa2015-06-01 14:28:14 -06001716 if (nvme_revalidate_disk(ns->disk))
1717 goto out_free_disk;
1718
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001719 kref_get(&dev->ctrl.kref);
Matias Bjørlingca064082015-10-29 17:57:29 +09001720 if (ns->type != NVME_NS_LIGHTNVM) {
1721 add_disk(ns->disk);
1722 if (ns->ms) {
1723 struct block_device *bd = bdget_disk(ns->disk, 0);
1724 if (!bd)
1725 return;
1726 if (blkdev_get(bd, FMODE_READ, NULL)) {
1727 bdput(bd);
1728 return;
1729 }
1730 blkdev_reread_part(bd);
1731 blkdev_put(bd, FMODE_READ);
Keith Busch7bee6072015-07-14 11:57:48 -06001732 }
Keith Busch7bee6072015-07-14 11:57:48 -06001733 }
Keith Busche1e5e562015-02-19 13:39:03 -07001734 return;
Keith Buscha5768aa2015-06-01 14:28:14 -06001735 out_free_disk:
1736 kfree(disk);
1737 list_del(&ns->list);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001738 out_free_queue:
1739 blk_cleanup_queue(ns->queue);
1740 out_free_ns:
1741 kfree(ns);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001742}
1743
Christoph Hellwig2659e572015-10-02 18:51:31 +02001744/*
1745 * Create I/O queues. Failing to create an I/O queue is not an issue,
1746 * we can continue with less than the desired amount of queues, and
1747 * even a controller without I/O queues an still be used to issue
1748 * admin commands. This might be useful to upgrade a buggy firmware
1749 * for example.
1750 */
Keith Busch42f61422014-03-24 10:46:25 -06001751static void nvme_create_io_queues(struct nvme_dev *dev)
1752{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001753 unsigned i;
Keith Busch42f61422014-03-24 10:46:25 -06001754
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001755 for (i = dev->queue_count; i <= dev->max_qid; i++)
Keith Busch2b25d982014-12-22 12:59:04 -07001756 if (!nvme_alloc_queue(dev, i, dev->q_depth))
Keith Busch42f61422014-03-24 10:46:25 -06001757 break;
1758
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001759 for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
Christoph Hellwig2659e572015-10-02 18:51:31 +02001760 if (nvme_create_queue(dev->queues[i], i)) {
1761 nvme_free_queues(dev, i);
Keith Busch42f61422014-03-24 10:46:25 -06001762 break;
Christoph Hellwig2659e572015-10-02 18:51:31 +02001763 }
Keith Busch42f61422014-03-24 10:46:25 -06001764}
1765
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001766static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001767{
1768 int status;
1769 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001770 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001771
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001772 status = nvme_set_features(&dev->ctrl, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001773 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04001774 if (status < 0)
1775 return status;
1776 if (status > 0) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001777 dev_err(dev->dev, "Could not set queue count (%d)\n", status);
Keith Buschbadc34d2014-06-23 14:25:35 -06001778 return 0;
Matthew Wilcox27e81662014-04-11 11:58:45 -04001779 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001780 return min(result & 0xffff, result >> 16) + 1;
1781}
1782
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001783static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
1784{
1785 u64 szu, size, offset;
1786 u32 cmbloc;
1787 resource_size_t bar_size;
1788 struct pci_dev *pdev = to_pci_dev(dev->dev);
1789 void __iomem *cmb;
1790 dma_addr_t dma_addr;
1791
1792 if (!use_cmb_sqes)
1793 return NULL;
1794
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001795 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001796 if (!(NVME_CMB_SZ(dev->cmbsz)))
1797 return NULL;
1798
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001799 cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001800
1801 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
1802 size = szu * NVME_CMB_SZ(dev->cmbsz);
1803 offset = szu * NVME_CMB_OFST(cmbloc);
1804 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(cmbloc));
1805
1806 if (offset > bar_size)
1807 return NULL;
1808
1809 /*
1810 * Controllers may support a CMB size larger than their BAR,
1811 * for example, due to being behind a bridge. Reduce the CMB to
1812 * the reported size of the BAR
1813 */
1814 if (size > bar_size - offset)
1815 size = bar_size - offset;
1816
1817 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(cmbloc)) + offset;
1818 cmb = ioremap_wc(dma_addr, size);
1819 if (!cmb)
1820 return NULL;
1821
1822 dev->cmb_dma_addr = dma_addr;
1823 dev->cmb_size = size;
1824 return cmb;
1825}
1826
1827static inline void nvme_release_cmb(struct nvme_dev *dev)
1828{
1829 if (dev->cmb) {
1830 iounmap(dev->cmb);
1831 dev->cmb = NULL;
1832 }
1833}
1834
Keith Busch9d713c22013-07-15 15:02:24 -06001835static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1836{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001837 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06001838}
1839
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001840static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001841{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001842 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02001843 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch42f61422014-03-24 10:46:25 -06001844 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001845
Keith Busch42f61422014-03-24 10:46:25 -06001846 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001847 result = set_queue_count(dev, nr_io_queues);
Keith Buschbadc34d2014-06-23 14:25:35 -06001848 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001849 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001850 if (result < nr_io_queues)
1851 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001852
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001853 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
1854 result = nvme_cmb_qdepth(dev, nr_io_queues,
1855 sizeof(struct nvme_command));
1856 if (result > 0)
1857 dev->q_depth = result;
1858 else
1859 nvme_release_cmb(dev);
1860 }
1861
Keith Busch9d713c22013-07-15 15:02:24 -06001862 size = db_bar_size(dev, nr_io_queues);
1863 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001864 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06001865 do {
1866 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1867 if (dev->bar)
1868 break;
1869 if (!--nr_io_queues)
1870 return -ENOMEM;
1871 size = db_bar_size(dev, nr_io_queues);
1872 } while (1);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001873 dev->dbs = dev->bar + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07001874 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001875 }
1876
Keith Busch9d713c22013-07-15 15:02:24 -06001877 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05001878 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06001879
Jens Axboee32efbf2014-11-14 09:49:26 -07001880 /*
1881 * If we enable msix early due to not intx, disable it again before
1882 * setting up the full range we need.
1883 */
1884 if (!pdev->irq)
1885 pci_disable_msix(pdev);
1886
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01001887 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001888 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01001889 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
1890 if (vecs < 0) {
1891 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
1892 if (vecs < 0) {
1893 vecs = 1;
1894 } else {
1895 for (i = 0; i < vecs; i++)
1896 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001897 }
1898 }
1899
Matthew Wilcox063a8092013-06-20 10:53:48 -04001900 /*
1901 * Should investigate if there's a performance win from allocating
1902 * more queues than interrupt vectors; it might allow the submission
1903 * path to scale better, even if the receive path is limited by the
1904 * number of interrupts.
1905 */
1906 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06001907 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001908
Matthew Wilcox3193f072014-01-27 15:57:22 -05001909 result = queue_request_irq(dev, adminq, adminq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001910 if (result) {
1911 adminq->cq_vector = -1;
Keith Busch22404272013-07-15 15:02:20 -06001912 goto free_queues;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001913 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001914
Keith Buschcd638942013-07-15 15:02:23 -06001915 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06001916 nvme_free_queues(dev, nr_io_queues + 1);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001917 nvme_create_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06001918
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001919 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001920
Keith Busch22404272013-07-15 15:02:20 -06001921 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05001922 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06001923 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001924}
1925
Keith Buscha5768aa2015-06-01 14:28:14 -06001926static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
1927{
1928 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
1929 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
1930
1931 return nsa->ns_id - nsb->ns_id;
1932}
1933
1934static struct nvme_ns *nvme_find_ns(struct nvme_dev *dev, unsigned nsid)
1935{
1936 struct nvme_ns *ns;
1937
1938 list_for_each_entry(ns, &dev->namespaces, list) {
1939 if (ns->ns_id == nsid)
1940 return ns;
1941 if (ns->ns_id > nsid)
1942 break;
1943 }
1944 return NULL;
1945}
1946
1947static inline bool nvme_io_incapable(struct nvme_dev *dev)
1948{
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001949 return (!dev->bar ||
1950 readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_CFS ||
1951 dev->online_queues < 2);
Keith Buscha5768aa2015-06-01 14:28:14 -06001952}
1953
1954static void nvme_ns_remove(struct nvme_ns *ns)
1955{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001956 bool kill = nvme_io_incapable(to_nvme_dev(ns->ctrl)) &&
1957 !blk_queue_dying(ns->queue);
Keith Buscha5768aa2015-06-01 14:28:14 -06001958
1959 if (kill)
1960 blk_set_queue_dying(ns->queue);
Dan Williams9609b992015-10-21 13:19:55 -04001961 if (ns->disk->flags & GENHD_FL_UP)
Keith Buscha5768aa2015-06-01 14:28:14 -06001962 del_gendisk(ns->disk);
Keith Buscha5768aa2015-06-01 14:28:14 -06001963 if (kill || !blk_queue_dying(ns->queue)) {
1964 blk_mq_abort_requeue_list(ns->queue);
1965 blk_cleanup_queue(ns->queue);
Keith Busch5105aa52015-10-02 10:37:28 -06001966 }
1967 list_del_init(&ns->list);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001968 nvme_put_ns(ns);
Keith Buscha5768aa2015-06-01 14:28:14 -06001969}
1970
1971static void nvme_scan_namespaces(struct nvme_dev *dev, unsigned nn)
1972{
1973 struct nvme_ns *ns, *next;
1974 unsigned i;
1975
1976 for (i = 1; i <= nn; i++) {
1977 ns = nvme_find_ns(dev, i);
1978 if (ns) {
Keith Busch5105aa52015-10-02 10:37:28 -06001979 if (revalidate_disk(ns->disk))
Keith Buscha5768aa2015-06-01 14:28:14 -06001980 nvme_ns_remove(ns);
Keith Buscha5768aa2015-06-01 14:28:14 -06001981 } else
1982 nvme_alloc_ns(dev, i);
1983 }
1984 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
Keith Busch5105aa52015-10-02 10:37:28 -06001985 if (ns->ns_id > nn)
Keith Buscha5768aa2015-06-01 14:28:14 -06001986 nvme_ns_remove(ns);
Keith Buscha5768aa2015-06-01 14:28:14 -06001987 }
1988 list_sort(NULL, &dev->namespaces, ns_cmp);
1989}
1990
Keith Buschbda4e0f2015-09-03 08:18:17 -06001991static void nvme_set_irq_hints(struct nvme_dev *dev)
1992{
1993 struct nvme_queue *nvmeq;
1994 int i;
1995
1996 for (i = 0; i < dev->online_queues; i++) {
1997 nvmeq = dev->queues[i];
1998
1999 if (!nvmeq->tags || !(*nvmeq->tags))
2000 continue;
2001
2002 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
2003 blk_mq_tags_cpumask(*nvmeq->tags));
2004 }
2005}
2006
Keith Buscha5768aa2015-06-01 14:28:14 -06002007static void nvme_dev_scan(struct work_struct *work)
2008{
2009 struct nvme_dev *dev = container_of(work, struct nvme_dev, scan_work);
2010 struct nvme_id_ctrl *ctrl;
2011
2012 if (!dev->tagset.tags)
2013 return;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002014 if (nvme_identify_ctrl(&dev->ctrl, &ctrl))
Keith Buscha5768aa2015-06-01 14:28:14 -06002015 return;
2016 nvme_scan_namespaces(dev, le32_to_cpup(&ctrl->nn));
2017 kfree(ctrl);
Keith Buschbda4e0f2015-09-03 08:18:17 -06002018 nvme_set_irq_hints(dev);
Keith Buscha5768aa2015-06-01 14:28:14 -06002019}
2020
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002021/*
2022 * Return: error value if an error occurred setting up the queues or calling
2023 * Identify Device. 0 if these succeeded, even if adding some of the
2024 * namespaces failed. At the moment, these failures are silent. TBD which
2025 * failures should be reported.
2026 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002027static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002028{
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002029 int res;
Matthew Wilcox51814232011-02-01 16:18:08 -05002030 struct nvme_id_ctrl *ctrl;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002031 int shift = NVME_CAP_MPSMIN(lo_hi_readq(dev->bar + NVME_REG_CAP)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002032
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002033 res = nvme_identify_ctrl(&dev->ctrl, &ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002034 if (res) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002035 dev_err(dev->dev, "Identify Controller failed (%d)\n", res);
Keith Busche1e5e562015-02-19 13:39:03 -07002036 return -EIO;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002037 }
2038
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002039 dev->ctrl.oncs = le16_to_cpup(&ctrl->oncs);
2040 dev->ctrl.abort_limit = ctrl->acl + 1;
2041 dev->ctrl.vwc = ctrl->vwc;
2042 memcpy(dev->ctrl.serial, ctrl->sn, sizeof(ctrl->sn));
2043 memcpy(dev->ctrl.model, ctrl->mn, sizeof(ctrl->mn));
2044 memcpy(dev->ctrl.firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002045 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002046 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Sathyavathi Mb12363d2015-11-05 12:52:28 -07002047 else
2048 dev->max_hw_sectors = UINT_MAX;
Christoph Hellwig106198e2015-11-26 10:07:41 +01002049
2050 if ((dev->ctrl.quirks & NVME_QUIRK_STRIPE_SIZE) && ctrl->vs[3]) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002051 unsigned int max_hw_sectors;
2052
Keith Busch159b67d2013-04-09 17:13:20 -06002053 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002054 max_hw_sectors = dev->stripe_size >> (shift - 9);
2055 if (dev->max_hw_sectors) {
2056 dev->max_hw_sectors = min(max_hw_sectors,
2057 dev->max_hw_sectors);
2058 } else
2059 dev->max_hw_sectors = max_hw_sectors;
2060 }
Christoph Hellwigd29ec822015-05-22 11:12:46 +02002061 kfree(ctrl);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002062
Keith Buschffe77042015-06-08 10:08:15 -06002063 if (!dev->tagset.tags) {
2064 dev->tagset.ops = &nvme_mq_ops;
2065 dev->tagset.nr_hw_queues = dev->online_queues - 1;
2066 dev->tagset.timeout = NVME_IO_TIMEOUT;
2067 dev->tagset.numa_node = dev_to_node(dev->dev);
2068 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002069 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Keith Buschffe77042015-06-08 10:08:15 -06002070 dev->tagset.cmd_size = nvme_cmd_size(dev);
2071 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
2072 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002073
Keith Buschffe77042015-06-08 10:08:15 -06002074 if (blk_mq_alloc_tag_set(&dev->tagset))
2075 return 0;
2076 }
Keith Buscha5768aa2015-06-01 14:28:14 -06002077 schedule_work(&dev->scan_work);
Keith Busche1e5e562015-02-19 13:39:03 -07002078 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002079}
2080
Keith Busch0877cb02013-07-15 15:02:19 -06002081static int nvme_dev_map(struct nvme_dev *dev)
2082{
Keith Busch42f61422014-03-24 10:46:25 -06002083 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002084 int bars, result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002085 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002086
2087 if (pci_enable_device_mem(pdev))
2088 return result;
2089
2090 dev->entry[0].vector = pdev->irq;
2091 pci_set_master(pdev);
2092 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07002093 if (!bars)
2094 goto disable_pci;
2095
Keith Busch0877cb02013-07-15 15:02:19 -06002096 if (pci_request_selected_regions(pdev, bars, "nvme"))
2097 goto disable_pci;
2098
Christoph Hellwige75ec752015-05-22 11:12:39 +02002099 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
2100 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01002101 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002102
Keith Busch0877cb02013-07-15 15:02:19 -06002103 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2104 if (!dev->bar)
2105 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07002106
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002107 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
Keith Busch0e53d182013-12-10 13:10:39 -07002108 result = -ENODEV;
2109 goto unmap;
2110 }
Jens Axboee32efbf2014-11-14 09:49:26 -07002111
2112 /*
2113 * Some devices don't advertse INTx interrupts, pre-enable a single
2114 * MSIX vec for setup. We'll adjust this later.
2115 */
2116 if (!pdev->irq) {
2117 result = pci_enable_msix(pdev, dev->entry, 1);
2118 if (result < 0)
2119 goto unmap;
2120 }
2121
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002122 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
2123
Keith Busch42f61422014-03-24 10:46:25 -06002124 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2125 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002126 dev->dbs = dev->bar + 4096;
2127 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2))
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002128 dev->cmb = nvme_map_cmb(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002129
2130 return 0;
2131
Keith Busch0e53d182013-12-10 13:10:39 -07002132 unmap:
2133 iounmap(dev->bar);
2134 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002135 disable:
2136 pci_release_regions(pdev);
2137 disable_pci:
2138 pci_disable_device(pdev);
2139 return result;
2140}
2141
2142static void nvme_dev_unmap(struct nvme_dev *dev)
2143{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002144 struct pci_dev *pdev = to_pci_dev(dev->dev);
2145
2146 if (pdev->msi_enabled)
2147 pci_disable_msi(pdev);
2148 else if (pdev->msix_enabled)
2149 pci_disable_msix(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002150
2151 if (dev->bar) {
2152 iounmap(dev->bar);
2153 dev->bar = NULL;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002154 pci_release_regions(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002155 }
2156
Christoph Hellwige75ec752015-05-22 11:12:39 +02002157 if (pci_is_enabled(pdev))
2158 pci_disable_device(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002159}
2160
Keith Busch4d115422013-12-10 13:10:40 -07002161struct nvme_delq_ctx {
2162 struct task_struct *waiter;
2163 struct kthread_worker *worker;
2164 atomic_t refcount;
2165};
2166
2167static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2168{
2169 dq->waiter = current;
2170 mb();
2171
2172 for (;;) {
2173 set_current_state(TASK_KILLABLE);
2174 if (!atomic_read(&dq->refcount))
2175 break;
2176 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2177 fatal_signal_pending(current)) {
Keith Busch0fb59cb2015-01-07 18:55:50 -07002178 /*
2179 * Disable the controller first since we can't trust it
2180 * at this point, but leave the admin queue enabled
2181 * until all queue deletion requests are flushed.
2182 * FIXME: This may take a while if there are more h/w
2183 * queues than admin tags.
2184 */
Keith Busch4d115422013-12-10 13:10:40 -07002185 set_current_state(TASK_RUNNING);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002186 nvme_disable_ctrl(dev,
2187 lo_hi_readq(dev->bar + NVME_REG_CAP));
Keith Busch0fb59cb2015-01-07 18:55:50 -07002188 nvme_clear_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07002189 flush_kthread_worker(dq->worker);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002190 nvme_disable_queue(dev, 0);
Keith Busch4d115422013-12-10 13:10:40 -07002191 return;
2192 }
2193 }
2194 set_current_state(TASK_RUNNING);
2195}
2196
2197static void nvme_put_dq(struct nvme_delq_ctx *dq)
2198{
2199 atomic_dec(&dq->refcount);
2200 if (dq->waiter)
2201 wake_up_process(dq->waiter);
2202}
2203
2204static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2205{
2206 atomic_inc(&dq->refcount);
2207 return dq;
2208}
2209
2210static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2211{
2212 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
Keith Busch4d115422013-12-10 13:10:40 -07002213 nvme_put_dq(dq);
Keith Busch604e8c82015-11-20 08:38:13 -07002214
2215 spin_lock_irq(&nvmeq->q_lock);
2216 nvme_process_cq(nvmeq);
2217 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07002218}
2219
2220static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2221 kthread_work_func_t fn)
2222{
2223 struct nvme_command c;
2224
2225 memset(&c, 0, sizeof(c));
2226 c.delete_queue.opcode = opcode;
2227 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2228
2229 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002230 return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
2231 ADMIN_TIMEOUT);
Keith Busch4d115422013-12-10 13:10:40 -07002232}
2233
2234static void nvme_del_cq_work_handler(struct kthread_work *work)
2235{
2236 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2237 cmdinfo.work);
2238 nvme_del_queue_end(nvmeq);
2239}
2240
2241static int nvme_delete_cq(struct nvme_queue *nvmeq)
2242{
2243 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2244 nvme_del_cq_work_handler);
2245}
2246
2247static void nvme_del_sq_work_handler(struct kthread_work *work)
2248{
2249 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2250 cmdinfo.work);
2251 int status = nvmeq->cmdinfo.status;
2252
2253 if (!status)
2254 status = nvme_delete_cq(nvmeq);
2255 if (status)
2256 nvme_del_queue_end(nvmeq);
2257}
2258
2259static int nvme_delete_sq(struct nvme_queue *nvmeq)
2260{
2261 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2262 nvme_del_sq_work_handler);
2263}
2264
2265static void nvme_del_queue_start(struct kthread_work *work)
2266{
2267 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2268 cmdinfo.work);
Keith Busch4d115422013-12-10 13:10:40 -07002269 if (nvme_delete_sq(nvmeq))
2270 nvme_del_queue_end(nvmeq);
2271}
2272
2273static void nvme_disable_io_queues(struct nvme_dev *dev)
2274{
2275 int i;
2276 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2277 struct nvme_delq_ctx dq;
2278 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002279 &worker, "nvme%d", dev->ctrl.instance);
Keith Busch4d115422013-12-10 13:10:40 -07002280
2281 if (IS_ERR(kworker_task)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002282 dev_err(dev->dev,
Keith Busch4d115422013-12-10 13:10:40 -07002283 "Failed to create queue del task\n");
2284 for (i = dev->queue_count - 1; i > 0; i--)
2285 nvme_disable_queue(dev, i);
2286 return;
2287 }
2288
2289 dq.waiter = NULL;
2290 atomic_set(&dq.refcount, 0);
2291 dq.worker = &worker;
2292 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002293 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002294
2295 if (nvme_suspend_queue(nvmeq))
2296 continue;
2297 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2298 nvmeq->cmdinfo.worker = dq.worker;
2299 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2300 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2301 }
2302 nvme_wait_dq(&dq, dev);
2303 kthread_stop(kworker_task);
2304}
2305
Dan McLeranb9afca32014-04-07 17:10:11 -06002306/*
2307* Remove the node from the device list and check
2308* for whether or not we need to stop the nvme_thread.
2309*/
2310static void nvme_dev_list_remove(struct nvme_dev *dev)
2311{
2312 struct task_struct *tmp = NULL;
2313
2314 spin_lock(&dev_list_lock);
2315 list_del_init(&dev->node);
2316 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2317 tmp = nvme_thread;
2318 nvme_thread = NULL;
2319 }
2320 spin_unlock(&dev_list_lock);
2321
2322 if (tmp)
2323 kthread_stop(tmp);
2324}
2325
Keith Buschc9d3bf82015-01-07 18:55:52 -07002326static void nvme_freeze_queues(struct nvme_dev *dev)
2327{
2328 struct nvme_ns *ns;
2329
2330 list_for_each_entry(ns, &dev->namespaces, list) {
2331 blk_mq_freeze_queue_start(ns->queue);
2332
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002333 spin_lock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002334 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002335 spin_unlock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002336
2337 blk_mq_cancel_requeue_work(ns->queue);
2338 blk_mq_stop_hw_queues(ns->queue);
2339 }
2340}
2341
2342static void nvme_unfreeze_queues(struct nvme_dev *dev)
2343{
2344 struct nvme_ns *ns;
2345
2346 list_for_each_entry(ns, &dev->namespaces, list) {
2347 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2348 blk_mq_unfreeze_queue(ns->queue);
2349 blk_mq_start_stopped_hw_queues(ns->queue, true);
2350 blk_mq_kick_requeue_list(ns->queue);
2351 }
2352}
2353
Keith Buschf0b50732013-07-15 15:02:21 -06002354static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002355{
Keith Busch22404272013-07-15 15:02:20 -06002356 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06002357 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06002358
Dan McLeranb9afca32014-04-07 17:10:11 -06002359 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002360
Keith Buschc9d3bf82015-01-07 18:55:52 -07002361 if (dev->bar) {
2362 nvme_freeze_queues(dev);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002363 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002364 }
Keith Busch7c1b2452014-06-25 11:18:12 -06002365 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07002366 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002367 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002368 nvme_suspend_queue(nvmeq);
Keith Busch4d115422013-12-10 13:10:40 -07002369 }
2370 } else {
2371 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002372 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002373 nvme_disable_queue(dev, 0);
2374 }
Keith Buschf0b50732013-07-15 15:02:21 -06002375 nvme_dev_unmap(dev);
Keith Busch07836e62015-02-19 10:34:48 -07002376
2377 for (i = dev->queue_count - 1; i >= 0; i--)
2378 nvme_clear_queue(dev->queues[i]);
Keith Buschf0b50732013-07-15 15:02:21 -06002379}
2380
2381static void nvme_dev_remove(struct nvme_dev *dev)
2382{
Keith Busch5105aa52015-10-02 10:37:28 -06002383 struct nvme_ns *ns, *next;
Keith Buschf0b50732013-07-15 15:02:21 -06002384
Keith Busch5105aa52015-10-02 10:37:28 -06002385 list_for_each_entry_safe(ns, next, &dev->namespaces, list)
Keith Buscha5768aa2015-06-01 14:28:14 -06002386 nvme_ns_remove(ns);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002387}
2388
Matthew Wilcox091b6092011-02-10 09:56:01 -05002389static int nvme_setup_prp_pools(struct nvme_dev *dev)
2390{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002391 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05002392 PAGE_SIZE, PAGE_SIZE, 0);
2393 if (!dev->prp_page_pool)
2394 return -ENOMEM;
2395
Matthew Wilcox99802a72011-02-10 10:30:34 -05002396 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02002397 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05002398 256, 256, 0);
2399 if (!dev->prp_small_pool) {
2400 dma_pool_destroy(dev->prp_page_pool);
2401 return -ENOMEM;
2402 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002403 return 0;
2404}
2405
2406static void nvme_release_prp_pools(struct nvme_dev *dev)
2407{
2408 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002409 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002410}
2411
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002412static DEFINE_IDA(nvme_instance_ida);
2413
2414static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002415{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002416 int instance, error;
2417
2418 do {
2419 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2420 return -ENODEV;
2421
2422 spin_lock(&dev_list_lock);
2423 error = ida_get_new(&nvme_instance_ida, &instance);
2424 spin_unlock(&dev_list_lock);
2425 } while (error == -EAGAIN);
2426
2427 if (error)
2428 return -ENODEV;
2429
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002430 dev->ctrl.instance = instance;
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002431 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002432}
2433
2434static void nvme_release_instance(struct nvme_dev *dev)
2435{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002436 spin_lock(&dev_list_lock);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002437 ida_remove(&nvme_instance_ida, dev->ctrl.instance);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002438 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002439}
2440
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002441static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
Keith Busch5e82e952013-02-19 10:17:58 -07002442{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002443 struct nvme_dev *dev = to_nvme_dev(ctrl);
Keith Busch9ac27092014-01-31 16:53:39 -07002444
Christoph Hellwige75ec752015-05-22 11:12:39 +02002445 put_device(dev->dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07002446 put_device(dev->device);
Indraneel M285dffc2014-12-11 08:24:18 -07002447 nvme_release_instance(dev);
Keith Busch4af0e212015-06-08 10:08:13 -06002448 if (dev->tagset.tags)
2449 blk_mq_free_tag_set(&dev->tagset);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002450 if (dev->ctrl.admin_q)
2451 blk_put_queue(dev->ctrl.admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002452 kfree(dev->queues);
2453 kfree(dev->entry);
2454 kfree(dev);
2455}
2456
2457static int nvme_dev_open(struct inode *inode, struct file *f)
2458{
Keith Buschb3fffde2015-02-03 11:21:42 -07002459 struct nvme_dev *dev;
2460 int instance = iminor(inode);
2461 int ret = -ENODEV;
2462
2463 spin_lock(&dev_list_lock);
2464 list_for_each_entry(dev, &dev_list, node) {
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002465 if (dev->ctrl.instance == instance) {
2466 if (!dev->ctrl.admin_q) {
Keith Busch2e1d8442015-02-12 15:33:00 -07002467 ret = -EWOULDBLOCK;
2468 break;
2469 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002470 if (!kref_get_unless_zero(&dev->ctrl.kref))
Keith Buschb3fffde2015-02-03 11:21:42 -07002471 break;
2472 f->private_data = dev;
2473 ret = 0;
2474 break;
2475 }
2476 }
2477 spin_unlock(&dev_list_lock);
2478
2479 return ret;
Keith Busch5e82e952013-02-19 10:17:58 -07002480}
2481
2482static int nvme_dev_release(struct inode *inode, struct file *f)
2483{
2484 struct nvme_dev *dev = f->private_data;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002485 nvme_put_ctrl(&dev->ctrl);
Keith Busch5e82e952013-02-19 10:17:58 -07002486 return 0;
2487}
2488
2489static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2490{
2491 struct nvme_dev *dev = f->private_data;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002492 struct nvme_ns *ns;
2493
Keith Busch5e82e952013-02-19 10:17:58 -07002494 switch (cmd) {
2495 case NVME_IOCTL_ADMIN_CMD:
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002496 return nvme_user_cmd(&dev->ctrl, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06002497 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002498 if (list_empty(&dev->namespaces))
2499 return -ENOTTY;
2500 ns = list_first_entry(&dev->namespaces, struct nvme_ns, list);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002501 return nvme_user_cmd(&dev->ctrl, ns, (void __user *)arg);
Keith Busch4cc06522015-06-05 10:30:08 -06002502 case NVME_IOCTL_RESET:
2503 dev_warn(dev->dev, "resetting controller\n");
2504 return nvme_reset(dev);
Jon Derrick81f03fe2015-08-10 15:20:41 -06002505 case NVME_IOCTL_SUBSYS_RESET:
2506 return nvme_subsys_reset(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002507 default:
2508 return -ENOTTY;
2509 }
2510}
2511
2512static const struct file_operations nvme_dev_fops = {
2513 .owner = THIS_MODULE,
2514 .open = nvme_dev_open,
2515 .release = nvme_dev_release,
2516 .unlocked_ioctl = nvme_dev_ioctl,
2517 .compat_ioctl = nvme_dev_ioctl,
2518};
2519
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002520static void nvme_probe_work(struct work_struct *work)
Keith Buschf0b50732013-07-15 15:02:21 -06002521{
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002522 struct nvme_dev *dev = container_of(work, struct nvme_dev, probe_work);
Dan McLeranb9afca32014-04-07 17:10:11 -06002523 bool start_thread = false;
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002524 int result;
Keith Buschf0b50732013-07-15 15:02:21 -06002525
2526 result = nvme_dev_map(dev);
2527 if (result)
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002528 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002529
2530 result = nvme_configure_admin_queue(dev);
2531 if (result)
2532 goto unmap;
2533
2534 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002535 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2536 start_thread = true;
2537 nvme_thread = NULL;
2538 }
Keith Buschf0b50732013-07-15 15:02:21 -06002539 list_add(&dev->node, &dev_list);
2540 spin_unlock(&dev_list_lock);
2541
Dan McLeranb9afca32014-04-07 17:10:11 -06002542 if (start_thread) {
2543 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
Keith Busch387caa52014-09-22 13:46:19 -06002544 wake_up_all(&nvme_kthread_wait);
Dan McLeranb9afca32014-04-07 17:10:11 -06002545 } else
2546 wait_event_killable(nvme_kthread_wait, nvme_thread);
2547
2548 if (IS_ERR_OR_NULL(nvme_thread)) {
2549 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2550 goto disable;
2551 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002552
2553 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002554 result = nvme_alloc_admin_tags(dev);
2555 if (result)
2556 goto disable;
Dan McLeranb9afca32014-04-07 17:10:11 -06002557
Keith Buschf0b50732013-07-15 15:02:21 -06002558 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002559 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07002560 goto free_tags;
Keith Buschf0b50732013-07-15 15:02:21 -06002561
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002562 dev->ctrl.event_limit = 1;
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002563
Christoph Hellwig2659e572015-10-02 18:51:31 +02002564 /*
2565 * Keep the controller around but remove all namespaces if we don't have
2566 * any working I/O queue.
2567 */
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002568 if (dev->online_queues < 2) {
2569 dev_warn(dev->dev, "IO queues not created\n");
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002570 nvme_dev_remove(dev);
2571 } else {
2572 nvme_unfreeze_queues(dev);
2573 nvme_dev_add(dev);
2574 }
2575
2576 return;
Keith Buschf0b50732013-07-15 15:02:21 -06002577
Keith Busch0fb59cb2015-01-07 18:55:50 -07002578 free_tags:
2579 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002580 blk_put_queue(dev->ctrl.admin_q);
2581 dev->ctrl.admin_q = NULL;
Keith Busch4af0e212015-06-08 10:08:13 -06002582 dev->queues[0]->tags = NULL;
Keith Buschf0b50732013-07-15 15:02:21 -06002583 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002584 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06002585 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002586 unmap:
2587 nvme_dev_unmap(dev);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002588 out:
2589 if (!work_busy(&dev->reset_work))
2590 nvme_dead_ctrl(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002591}
2592
Keith Busch9a6b9452013-12-10 13:10:36 -07002593static int nvme_remove_dead_ctrl(void *arg)
2594{
2595 struct nvme_dev *dev = (struct nvme_dev *)arg;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002596 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002597
2598 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06002599 pci_stop_and_remove_bus_device_locked(pdev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002600 nvme_put_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002601 return 0;
2602}
2603
Keith Buschde3eff22015-06-18 13:36:39 -06002604static void nvme_dead_ctrl(struct nvme_dev *dev)
2605{
2606 dev_warn(dev->dev, "Device failed to resume\n");
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002607 kref_get(&dev->ctrl.kref);
Keith Buschde3eff22015-06-18 13:36:39 -06002608 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002609 dev->ctrl.instance))) {
Keith Buschde3eff22015-06-18 13:36:39 -06002610 dev_err(dev->dev,
2611 "Failed to start controller remove task\n");
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002612 nvme_put_ctrl(&dev->ctrl);
Keith Buschde3eff22015-06-18 13:36:39 -06002613 }
2614}
2615
Christoph Hellwig77b50d92015-10-02 17:41:18 +02002616static void nvme_reset_work(struct work_struct *ws)
Keith Busch9a6b9452013-12-10 13:10:36 -07002617{
Christoph Hellwig77b50d92015-10-02 17:41:18 +02002618 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
Keith Buschffe77042015-06-08 10:08:15 -06002619 bool in_probe = work_busy(&dev->probe_work);
2620
Keith Busch9a6b9452013-12-10 13:10:36 -07002621 nvme_dev_shutdown(dev);
Keith Buschffe77042015-06-08 10:08:15 -06002622
2623 /* Synchronize with device probe so that work will see failure status
2624 * and exit gracefully without trying to schedule another reset */
2625 flush_work(&dev->probe_work);
2626
2627 /* Fail this device if reset occured during probe to avoid
2628 * infinite initialization loops. */
2629 if (in_probe) {
Keith Buschde3eff22015-06-18 13:36:39 -06002630 nvme_dead_ctrl(dev);
Keith Buschffe77042015-06-08 10:08:15 -06002631 return;
Keith Busch9a6b9452013-12-10 13:10:36 -07002632 }
Keith Buschffe77042015-06-08 10:08:15 -06002633 /* Schedule device resume asynchronously so the reset work is available
2634 * to cleanup errors that may occur during reinitialization */
2635 schedule_work(&dev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07002636}
2637
Christoph Hellwig906678922015-10-02 18:49:23 +02002638static int __nvme_reset(struct nvme_dev *dev)
Keith Busch9a6b9452013-12-10 13:10:36 -07002639{
Christoph Hellwig906678922015-10-02 18:49:23 +02002640 if (work_pending(&dev->reset_work))
2641 return -EBUSY;
2642 list_del_init(&dev->node);
2643 queue_work(nvme_workq, &dev->reset_work);
2644 return 0;
Tejun Heo9ca97372014-03-07 10:24:49 -05002645}
2646
Keith Busch4cc06522015-06-05 10:30:08 -06002647static int nvme_reset(struct nvme_dev *dev)
2648{
Christoph Hellwig906678922015-10-02 18:49:23 +02002649 int ret;
Keith Busch4cc06522015-06-05 10:30:08 -06002650
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002651 if (!dev->ctrl.admin_q || blk_queue_dying(dev->ctrl.admin_q))
Keith Busch4cc06522015-06-05 10:30:08 -06002652 return -ENODEV;
2653
2654 spin_lock(&dev_list_lock);
Christoph Hellwig906678922015-10-02 18:49:23 +02002655 ret = __nvme_reset(dev);
Keith Busch4cc06522015-06-05 10:30:08 -06002656 spin_unlock(&dev_list_lock);
2657
2658 if (!ret) {
2659 flush_work(&dev->reset_work);
Keith Buschffe77042015-06-08 10:08:15 -06002660 flush_work(&dev->probe_work);
Keith Busch4cc06522015-06-05 10:30:08 -06002661 return 0;
2662 }
2663
2664 return ret;
2665}
2666
2667static ssize_t nvme_sysfs_reset(struct device *dev,
2668 struct device_attribute *attr, const char *buf,
2669 size_t count)
2670{
2671 struct nvme_dev *ndev = dev_get_drvdata(dev);
2672 int ret;
2673
2674 ret = nvme_reset(ndev);
2675 if (ret < 0)
2676 return ret;
2677
2678 return count;
2679}
2680static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2681
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002682static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
2683{
2684 *val = readl(to_nvme_dev(ctrl)->bar + off);
2685 return 0;
2686}
2687
2688static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
2689 .reg_read32 = nvme_pci_reg_read32,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002690 .free_ctrl = nvme_pci_free_ctrl,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002691};
2692
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002693static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002694{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002695 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002696 struct nvme_dev *dev;
2697
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002698 node = dev_to_node(&pdev->dev);
2699 if (node == NUMA_NO_NODE)
2700 set_dev_node(&pdev->dev, 0);
2701
2702 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002703 if (!dev)
2704 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002705 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
2706 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002707 if (!dev->entry)
2708 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002709 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2710 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002711 if (!dev->queues)
2712 goto free;
2713
2714 INIT_LIST_HEAD(&dev->namespaces);
Christoph Hellwig77b50d92015-10-02 17:41:18 +02002715 INIT_WORK(&dev->reset_work, nvme_reset_work);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002716 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002717 pci_set_drvdata(pdev, dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002718
2719 dev->ctrl.ops = &nvme_pci_ctrl_ops;
2720 dev->ctrl.dev = dev->dev;
Christoph Hellwig106198e2015-11-26 10:07:41 +01002721 dev->ctrl.quirks = id->driver_data;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002722
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002723 result = nvme_set_instance(dev);
2724 if (result)
Keith Buscha96d4f52014-08-19 19:15:59 -06002725 goto put_pci;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002726
Matthew Wilcox091b6092011-02-10 09:56:01 -05002727 result = nvme_setup_prp_pools(dev);
2728 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002729 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002730
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002731 kref_init(&dev->ctrl.kref);
Keith Buschb3fffde2015-02-03 11:21:42 -07002732 dev->device = device_create(nvme_class, &pdev->dev,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002733 MKDEV(nvme_char_major, dev->ctrl.instance),
2734 dev, "nvme%d", dev->ctrl.instance);
Keith Buschb3fffde2015-02-03 11:21:42 -07002735 if (IS_ERR(dev->device)) {
2736 result = PTR_ERR(dev->device);
Keith Busch2e1d8442015-02-12 15:33:00 -07002737 goto release_pools;
Keith Buschb3fffde2015-02-03 11:21:42 -07002738 }
2739 get_device(dev->device);
Keith Busch4cc06522015-06-05 10:30:08 -06002740 dev_set_drvdata(dev->device, dev);
2741
2742 result = device_create_file(dev->device, &dev_attr_reset_controller);
2743 if (result)
2744 goto put_dev;
Keith Buschb3fffde2015-02-03 11:21:42 -07002745
Keith Busche6e96d72015-03-23 09:32:37 -06002746 INIT_LIST_HEAD(&dev->node);
Keith Buscha5768aa2015-06-01 14:28:14 -06002747 INIT_WORK(&dev->scan_work, nvme_dev_scan);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002748 INIT_WORK(&dev->probe_work, nvme_probe_work);
Keith Busch2e1d8442015-02-12 15:33:00 -07002749 schedule_work(&dev->probe_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002750 return 0;
2751
Keith Busch4cc06522015-06-05 10:30:08 -06002752 put_dev:
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002753 device_destroy(nvme_class, MKDEV(nvme_char_major, dev->ctrl.instance));
Keith Busch4cc06522015-06-05 10:30:08 -06002754 put_device(dev->device);
Keith Busch0877cb02013-07-15 15:02:19 -06002755 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05002756 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002757 release:
2758 nvme_release_instance(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002759 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02002760 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002761 free:
2762 kfree(dev->queues);
2763 kfree(dev->entry);
2764 kfree(dev);
2765 return result;
2766}
2767
Keith Buschf0d54a52014-05-02 10:40:43 -06002768static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2769{
Keith Buscha6739472014-06-23 16:03:21 -06002770 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002771
Keith Buscha6739472014-06-23 16:03:21 -06002772 if (prepare)
2773 nvme_dev_shutdown(dev);
2774 else
Keith Busch0a7385a2015-10-02 10:37:29 -06002775 schedule_work(&dev->probe_work);
Keith Buschf0d54a52014-05-02 10:40:43 -06002776}
2777
Keith Busch09ece142014-01-27 11:29:40 -05002778static void nvme_shutdown(struct pci_dev *pdev)
2779{
2780 struct nvme_dev *dev = pci_get_drvdata(pdev);
2781 nvme_dev_shutdown(dev);
2782}
2783
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002784static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002785{
2786 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002787
2788 spin_lock(&dev_list_lock);
2789 list_del_init(&dev->node);
2790 spin_unlock(&dev_list_lock);
2791
2792 pci_set_drvdata(pdev, NULL);
Keith Busch2e1d8442015-02-12 15:33:00 -07002793 flush_work(&dev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07002794 flush_work(&dev->reset_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06002795 flush_work(&dev->scan_work);
Keith Busch4cc06522015-06-05 10:30:08 -06002796 device_remove_file(dev->device, &dev_attr_reset_controller);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002797 nvme_dev_remove(dev);
Keith Busch3399a3f2015-06-18 13:36:40 -06002798 nvme_dev_shutdown(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002799 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002800 device_destroy(nvme_class, MKDEV(nvme_char_major, dev->ctrl.instance));
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002801 nvme_free_queues(dev, 0);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002802 nvme_release_cmb(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002803 nvme_release_prp_pools(dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002804 nvme_put_ctrl(&dev->ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002805}
2806
2807/* These functions are yet to be implemented */
2808#define nvme_error_detected NULL
2809#define nvme_dump_registers NULL
2810#define nvme_link_reset NULL
2811#define nvme_slot_reset NULL
2812#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002813
Jingoo Han671a6012014-02-13 11:19:14 +09002814#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002815static int nvme_suspend(struct device *dev)
2816{
2817 struct pci_dev *pdev = to_pci_dev(dev);
2818 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2819
2820 nvme_dev_shutdown(ndev);
2821 return 0;
2822}
2823
2824static int nvme_resume(struct device *dev)
2825{
2826 struct pci_dev *pdev = to_pci_dev(dev);
2827 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002828
Keith Busch0a7385a2015-10-02 10:37:29 -06002829 schedule_work(&ndev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07002830 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002831}
Jingoo Han671a6012014-02-13 11:19:14 +09002832#endif
Keith Buschcd638942013-07-15 15:02:23 -06002833
2834static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002835
Stephen Hemminger1d352032012-09-07 09:33:17 -07002836static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002837 .error_detected = nvme_error_detected,
2838 .mmio_enabled = nvme_dump_registers,
2839 .link_reset = nvme_link_reset,
2840 .slot_reset = nvme_slot_reset,
2841 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06002842 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002843};
2844
2845/* Move to pci_ids.h later */
2846#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2847
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002848static const struct pci_device_id nvme_id_table[] = {
Christoph Hellwig106198e2015-11-26 10:07:41 +01002849 { PCI_VDEVICE(INTEL, 0x0953),
2850 .driver_data = NVME_QUIRK_STRIPE_SIZE, },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002851 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
Stephan Güntherc74dc782015-11-04 00:49:45 +01002852 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002853 { 0, }
2854};
2855MODULE_DEVICE_TABLE(pci, nvme_id_table);
2856
2857static struct pci_driver nvme_driver = {
2858 .name = "nvme",
2859 .id_table = nvme_id_table,
2860 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002861 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002862 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002863 .driver = {
2864 .pm = &nvme_dev_pm_ops,
2865 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002866 .err_handler = &nvme_err_handler,
2867};
2868
2869static int __init nvme_init(void)
2870{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002871 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002872
Dan McLeranb9afca32014-04-07 17:10:11 -06002873 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002874
Keith Busch9a6b9452013-12-10 13:10:36 -07002875 nvme_workq = create_singlethread_workqueue("nvme");
2876 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06002877 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07002878
Keith Busch5c42ea12012-07-25 16:05:18 -06002879 result = register_blkdev(nvme_major, "nvme");
2880 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002881 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06002882 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002883 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002884
Keith Buschb3fffde2015-02-03 11:21:42 -07002885 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
2886 &nvme_dev_fops);
2887 if (result < 0)
2888 goto unregister_blkdev;
2889 else if (result > 0)
2890 nvme_char_major = result;
2891
2892 nvme_class = class_create(THIS_MODULE, "nvme");
Alexey Khoroshilovc7270402015-03-07 01:43:41 +03002893 if (IS_ERR(nvme_class)) {
2894 result = PTR_ERR(nvme_class);
Keith Buschb3fffde2015-02-03 11:21:42 -07002895 goto unregister_chrdev;
Alexey Khoroshilovc7270402015-03-07 01:43:41 +03002896 }
Keith Buschb3fffde2015-02-03 11:21:42 -07002897
Keith Buschf3db22f2014-06-11 11:51:35 -06002898 result = pci_register_driver(&nvme_driver);
2899 if (result)
Keith Buschb3fffde2015-02-03 11:21:42 -07002900 goto destroy_class;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002901 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002902
Keith Buschb3fffde2015-02-03 11:21:42 -07002903 destroy_class:
2904 class_destroy(nvme_class);
2905 unregister_chrdev:
2906 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002907 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002908 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002909 kill_workq:
2910 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002911 return result;
2912}
2913
2914static void __exit nvme_exit(void)
2915{
2916 pci_unregister_driver(&nvme_driver);
2917 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002918 destroy_workqueue(nvme_workq);
Keith Buschb3fffde2015-02-03 11:21:42 -07002919 class_destroy(nvme_class);
2920 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Dan McLeranb9afca32014-04-07 17:10:11 -06002921 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002922 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002923}
2924
2925MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2926MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07002927MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002928module_init(nvme_init);
2929module_exit(nvme_exit);