blob: 24d695a2f6c4878e71d10df77afed0adc2796b21 [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>
31#include <linux/mm.h>
32#include <linux/module.h>
33#include <linux/moduleparam.h>
Keith Busch77bf25e2015-11-26 12:21:29 +010034#include <linux/mutex.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050035#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>
Linus Torvalds9cf5c092015-11-06 14:22:15 -080042#include <linux/io-64-nonatomic-lo-hi.h>
Keith Busch1d277a62015-10-15 14:10:52 +020043#include <asm/unaligned.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090044
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +020045#include "nvme.h"
46
Keith Busch9d43cf62014-05-13 11:42:02 -060047#define NVME_Q_DEPTH 1024
Jens Axboed31af0a2015-03-06 12:56:13 -070048#define NVME_AQ_DEPTH 256
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050049#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
50#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Christoph Hellwigadf68f22015-11-28 15:42:28 +010051
52/*
53 * We handle AEN commands ourselves and don't even let the
54 * block layer know about them.
55 */
56#define NVME_NR_AEN_COMMANDS 1
57#define NVME_AQ_BLKMQ_DEPTH (NVME_AQ_DEPTH - NVME_NR_AEN_COMMANDS)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050058
Christoph Hellwig21d34712015-11-26 09:08:36 +010059unsigned char admin_timeout = 60;
Keith Busch9d43cf62014-05-13 11:42:02 -060060module_param(admin_timeout, byte, 0644);
61MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050062
Matthew Wilcoxbd676082014-06-03 23:04:30 -040063unsigned char nvme_io_timeout = 30;
64module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
Keith Buschb3550842014-04-04 11:43:36 -060065MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050066
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +010067unsigned char shutdown_timeout = 5;
Dan McLeran2484f402014-07-01 09:33:32 -060068module_param(shutdown_timeout, byte, 0644);
69MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
70
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050071static int use_threaded_interrupts;
72module_param(use_threaded_interrupts, int, 0);
73
Jon Derrick8ffaadf2015-07-20 10:14:09 -060074static bool use_cmb_sqes = true;
75module_param(use_cmb_sqes, bool, 0644);
76MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
77
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050078static LIST_HEAD(dev_list);
79static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070080static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060081static wait_queue_head_t nvme_kthread_wait;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050082
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010083struct nvme_dev;
84struct nvme_queue;
85
Keith Busch4cc06522015-06-05 10:30:08 -060086static int nvme_reset(struct nvme_dev *dev);
Jens Axboea0fa9642015-11-03 20:37:26 -070087static void nvme_process_cq(struct nvme_queue *nvmeq);
Christoph Hellwig5c8809e2015-11-26 12:35:49 +010088static void nvme_remove_dead_ctrl(struct nvme_dev *dev);
Keith Busche1569a12015-11-26 12:11:07 +010089static void nvme_dev_shutdown(struct nvme_dev *dev);
Keith Buschd4b4ff82013-12-10 13:10:37 -070090
Keith Busch4d115422013-12-10 13:10:40 -070091struct async_cmd_info {
92 struct kthread_work work;
93 struct kthread_worker *worker;
Keith Busch4d115422013-12-10 13:10:40 -070094 int status;
95 void *ctx;
96};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050097
98/*
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010099 * Represents an NVM Express device. Each nvme_dev is a PCI function.
100 */
101struct nvme_dev {
102 struct list_head node;
103 struct nvme_queue **queues;
104 struct blk_mq_tag_set tagset;
105 struct blk_mq_tag_set admin_tagset;
106 u32 __iomem *dbs;
107 struct device *dev;
108 struct dma_pool *prp_page_pool;
109 struct dma_pool *prp_small_pool;
110 unsigned queue_count;
111 unsigned online_queues;
112 unsigned max_qid;
113 int q_depth;
114 u32 db_stride;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100115 struct msix_entry *entry;
116 void __iomem *bar;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100117 struct work_struct reset_work;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100118 struct work_struct scan_work;
Christoph Hellwig5c8809e2015-11-26 12:35:49 +0100119 struct work_struct remove_work;
Keith Busch77bf25e2015-11-26 12:21:29 +0100120 struct mutex shutdown_lock;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100121 bool subsystem;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100122 void __iomem *cmb;
123 dma_addr_t cmb_dma_addr;
124 u64 cmb_size;
125 u32 cmbsz;
Christoph Hellwigfd634f412015-11-26 12:42:26 +0100126 unsigned long flags;
127#define NVME_CTRL_RESETTING 0
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100128
129 struct nvme_ctrl ctrl;
130};
131
132static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
133{
134 return container_of(ctrl, struct nvme_dev, ctrl);
135}
136
137/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500138 * An NVM Express queue. Each device has at least two (one for admin
139 * commands and one for I/O commands).
140 */
141struct nvme_queue {
142 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500143 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500144 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500145 spinlock_t q_lock;
146 struct nvme_command *sq_cmds;
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600147 struct nvme_command __iomem *sq_cmds_io;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500148 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600149 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500150 dma_addr_t sq_dma_addr;
151 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500152 u32 __iomem *q_db;
153 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700154 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500155 u16 sq_head;
156 u16 sq_tail;
157 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700158 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400159 u8 cq_phase;
160 u8 cqe_seen;
Keith Busch4d115422013-12-10 13:10:40 -0700161 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500162};
163
164/*
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200165 * The nvme_iod describes the data in an I/O, including the list of PRP
166 * entries. You can't see it in this data structure because C doesn't let
167 * me express that. Use nvme_alloc_iod to ensure there's enough space
168 * allocated to store the PRP list.
169 */
170struct nvme_iod {
171 unsigned long private; /* For the use of the submitter of the I/O */
172 int npages; /* In the PRP list. 0 means small pool in use */
173 int offset; /* Of PRP list */
174 int nents; /* Used in scatterlist */
175 int length; /* Of data, in bytes */
176 dma_addr_t first_dma;
Christoph Hellwigbf684052015-10-26 17:12:51 +0900177 struct scatterlist meta_sg; /* metadata requires single contiguous buffer */
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200178 struct scatterlist sg[0];
179};
180
181/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500182 * Check we didin't inadvertently grow the command struct
183 */
184static inline void _nvme_check_size(void)
185{
186 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
187 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
188 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
189 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
190 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400191 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700192 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500193 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
194 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
195 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
196 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600197 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500198}
199
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500200struct nvme_cmd_info {
Keith Buschc30341d2013-12-10 13:10:38 -0700201 int aborted;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700202 struct nvme_queue *nvmeq;
Christoph Hellwigaae239e2015-11-26 12:59:50 +0100203 struct nvme_iod *iod;
204 struct nvme_iod __iod;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500205};
206
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700207/*
208 * Max size of iod being embedded in the request payload
209 */
210#define NVME_INT_PAGES 2
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100211#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
Chong Yuanfda631f2015-03-27 09:21:32 +0800212#define NVME_INT_MASK 0x01
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700213
214/*
215 * Will slightly overestimate the number of pages needed. This is OK
216 * as it only leads to a small amount of wasted memory for the lifetime of
217 * the I/O.
218 */
219static int nvme_npages(unsigned size, struct nvme_dev *dev)
220{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100221 unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
222 dev->ctrl.page_size);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700223 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
224}
225
226static unsigned int nvme_cmd_size(struct nvme_dev *dev)
227{
228 unsigned int ret = sizeof(struct nvme_cmd_info);
229
230 ret += sizeof(struct nvme_iod);
231 ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
232 ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
233
234 return ret;
235}
236
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700237static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
238 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500239{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700240 struct nvme_dev *dev = data;
241 struct nvme_queue *nvmeq = dev->queues[0];
242
Keith Busch42483222015-06-01 09:29:54 -0600243 WARN_ON(hctx_idx != 0);
244 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
245 WARN_ON(nvmeq->tags);
246
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700247 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600248 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700249 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500250}
251
Keith Busch4af0e212015-06-08 10:08:13 -0600252static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
253{
254 struct nvme_queue *nvmeq = hctx->driver_data;
255
256 nvmeq->tags = NULL;
257}
258
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700259static int nvme_admin_init_request(void *data, struct request *req,
260 unsigned int hctx_idx, unsigned int rq_idx,
261 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600262{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700263 struct nvme_dev *dev = data;
264 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
265 struct nvme_queue *nvmeq = dev->queues[0];
266
267 BUG_ON(!nvmeq);
268 cmd->nvmeq = nvmeq;
269 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600270}
271
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700272static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
273 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500274{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700275 struct nvme_dev *dev = data;
Keith Busch42483222015-06-01 09:29:54 -0600276 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500277
Keith Busch42483222015-06-01 09:29:54 -0600278 if (!nvmeq->tags)
279 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500280
Keith Busch42483222015-06-01 09:29:54 -0600281 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700282 hctx->driver_data = nvmeq;
283 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500284}
285
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700286static int nvme_init_request(void *data, struct request *req,
287 unsigned int hctx_idx, unsigned int rq_idx,
288 unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500289{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700290 struct nvme_dev *dev = data;
291 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
292 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
293
294 BUG_ON(!nvmeq);
295 cmd->nvmeq = nvmeq;
296 return 0;
297}
298
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700299static void *iod_get_private(struct nvme_iod *iod)
300{
301 return (void *) (iod->private & ~0x1UL);
302}
303
304/*
305 * If bit 0 is set, the iod is embedded in the request payload.
306 */
307static bool iod_should_kfree(struct nvme_iod *iod)
308{
Chong Yuanfda631f2015-03-27 09:21:32 +0800309 return (iod->private & NVME_INT_MASK) == 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700310}
311
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100312static void nvme_complete_async_event(struct nvme_dev *dev,
313 struct nvme_completion *cqe)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700314{
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100315 u16 status = le16_to_cpu(cqe->status) >> 1;
316 u32 result = le32_to_cpu(cqe->result);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700317
318 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100319 ++dev->ctrl.event_limit;
Keith Buscha5768aa2015-06-01 14:28:14 -0600320 if (status != NVME_SC_SUCCESS)
321 return;
322
323 switch (result & 0xff07) {
324 case NVME_AER_NOTICE_NS_CHANGED:
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100325 dev_info(dev->dev, "rescanning\n");
326 queue_work(nvme_workq, &dev->scan_work);
Keith Buscha5768aa2015-06-01 14:28:14 -0600327 default:
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100328 dev_warn(dev->dev, "async event result %08x\n", result);
Keith Buscha5768aa2015-06-01 14:28:14 -0600329 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700330}
331
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500332/**
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100333 * __nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500334 * @nvmeq: The queue to use
335 * @cmd: The command to send
336 *
337 * Safe to use from interrupt context
338 */
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530339static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
340 struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500341{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700342 u16 tail = nvmeq->sq_tail;
343
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600344 if (nvmeq->sq_cmds_io)
345 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
346 else
347 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
348
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500349 if (++tail == nvmeq->q_depth)
350 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500351 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500352 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500353}
354
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500355static __le64 **iod_list(struct nvme_iod *iod)
356{
357 return ((void *)iod) + iod->offset;
358}
359
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700360static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
361 unsigned nseg, unsigned long private)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500362{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700363 iod->private = private;
364 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
365 iod->npages = -1;
366 iod->length = nbytes;
367 iod->nents = 0;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500368}
369
370static struct nvme_iod *
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700371__nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
372 unsigned long priv, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500373{
374 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700375 sizeof(__le64 *) * nvme_npages(bytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500376 sizeof(struct scatterlist) * nseg, gfp);
377
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700378 if (iod)
379 iod_init(iod, bytes, nseg, priv);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500380
381 return iod;
382}
383
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700384static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
385 gfp_t gfp)
386{
387 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
388 sizeof(struct nvme_dsm_range);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700389 struct nvme_iod *iod;
390
391 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
392 size <= NVME_INT_BYTES(dev)) {
393 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
394
Christoph Hellwigaae239e2015-11-26 12:59:50 +0100395 iod = &cmd->__iod;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700396 iod_init(iod, size, rq->nr_phys_segments,
Chong Yuanfda631f2015-03-27 09:21:32 +0800397 (unsigned long) rq | NVME_INT_MASK);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700398 return iod;
399 }
400
401 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
402 (unsigned long) rq, gfp);
403}
404
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200405static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500406{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100407 const int last_prp = dev->ctrl.page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500408 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500409 __le64 **list = iod_list(iod);
410 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500411
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500412 if (iod->npages == 0)
413 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
414 for (i = 0; i < iod->npages; i++) {
415 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500416 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500417 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500418 prp_dma = next_prp_dma;
419 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700420
421 if (iod_should_kfree(iod))
422 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500423}
424
Keith Busch52b68d72015-02-23 09:16:21 -0700425#ifdef CONFIG_BLK_DEV_INTEGRITY
Keith Busche1e5e562015-02-19 13:39:03 -0700426static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
427{
428 if (be32_to_cpu(pi->ref_tag) == v)
429 pi->ref_tag = cpu_to_be32(p);
430}
431
432static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
433{
434 if (be32_to_cpu(pi->ref_tag) == p)
435 pi->ref_tag = cpu_to_be32(v);
436}
437
438/**
439 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
440 *
441 * The virtual start sector is the one that was originally submitted by the
442 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
443 * start sector may be different. Remap protection information to match the
444 * physical LBA on writes, and back to the original seed on reads.
445 *
446 * Type 0 and 3 do not have a ref tag, so no remapping required.
447 */
448static void nvme_dif_remap(struct request *req,
449 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
450{
451 struct nvme_ns *ns = req->rq_disk->private_data;
452 struct bio_integrity_payload *bip;
453 struct t10_pi_tuple *pi;
454 void *p, *pmap;
455 u32 i, nlb, ts, phys, virt;
456
457 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
458 return;
459
460 bip = bio_integrity(req->bio);
461 if (!bip)
462 return;
463
464 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
Keith Busche1e5e562015-02-19 13:39:03 -0700465
466 p = pmap;
467 virt = bip_get_seed(bip);
468 phys = nvme_block_nr(ns, blk_rq_pos(req));
469 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
Dan Williamsac6fc482015-10-21 13:20:18 -0400470 ts = ns->disk->queue->integrity.tuple_size;
Keith Busche1e5e562015-02-19 13:39:03 -0700471
472 for (i = 0; i < nlb; i++, virt++, phys++) {
473 pi = (struct t10_pi_tuple *)p;
474 dif_swap(phys, virt, pi);
475 p += ts;
476 }
477 kunmap_atomic(pmap);
478}
Keith Busch52b68d72015-02-23 09:16:21 -0700479#else /* CONFIG_BLK_DEV_INTEGRITY */
480static void nvme_dif_remap(struct request *req,
481 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
482{
483}
484static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
485{
486}
487static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
488{
489}
Keith Busch52b68d72015-02-23 09:16:21 -0700490#endif
491
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200492static bool nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod,
493 int total_len)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500494{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500495 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500496 int length = total_len;
497 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500498 int dma_len = sg_dma_len(sg);
499 u64 dma_addr = sg_dma_address(sg);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100500 u32 page_size = dev->ctrl.page_size;
Murali Iyerf137e0f2015-03-26 11:07:51 -0500501 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500502 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500503 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500504 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500505 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500506
Keith Busch1d090622014-06-23 11:34:01 -0600507 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500508 if (length <= 0)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200509 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500510
Keith Busch1d090622014-06-23 11:34:01 -0600511 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500512 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600513 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500514 } else {
515 sg = sg_next(sg);
516 dma_addr = sg_dma_address(sg);
517 dma_len = sg_dma_len(sg);
518 }
519
Keith Busch1d090622014-06-23 11:34:01 -0600520 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600521 iod->first_dma = dma_addr;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200522 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500523 }
524
Keith Busch1d090622014-06-23 11:34:01 -0600525 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500526 if (nprps <= (256 / 8)) {
527 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500528 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500529 } else {
530 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500531 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500532 }
533
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200534 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400535 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600536 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500537 iod->npages = -1;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200538 return false;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400539 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500540 list[0] = prp_list;
541 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500542 i = 0;
543 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600544 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500545 __le64 *old_prp_list = prp_list;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200546 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500547 if (!prp_list)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200548 return false;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500549 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400550 prp_list[0] = old_prp_list[i - 1];
551 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
552 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500553 }
554 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600555 dma_len -= page_size;
556 dma_addr += page_size;
557 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500558 if (length <= 0)
559 break;
560 if (dma_len > 0)
561 continue;
562 BUG_ON(dma_len < 0);
563 sg = sg_next(sg);
564 dma_addr = sg_dma_address(sg);
565 dma_len = sg_dma_len(sg);
566 }
567
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200568 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500569}
570
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200571static int nvme_map_data(struct nvme_dev *dev, struct nvme_iod *iod,
572 struct nvme_command *cmnd)
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200573{
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200574 struct request *req = iod_get_private(iod);
575 struct request_queue *q = req->q;
576 enum dma_data_direction dma_dir = rq_data_dir(req) ?
577 DMA_TO_DEVICE : DMA_FROM_DEVICE;
578 int ret = BLK_MQ_RQ_QUEUE_ERROR;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200579
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200580 sg_init_table(iod->sg, req->nr_phys_segments);
581 iod->nents = blk_rq_map_sg(q, req, iod->sg);
582 if (!iod->nents)
583 goto out;
584
585 ret = BLK_MQ_RQ_QUEUE_BUSY;
586 if (!dma_map_sg(dev->dev, iod->sg, iod->nents, dma_dir))
587 goto out;
588
589 if (!nvme_setup_prps(dev, iod, blk_rq_bytes(req)))
590 goto out_unmap;
591
592 ret = BLK_MQ_RQ_QUEUE_ERROR;
593 if (blk_integrity_rq(req)) {
594 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
595 goto out_unmap;
596
Christoph Hellwigbf684052015-10-26 17:12:51 +0900597 sg_init_table(&iod->meta_sg, 1);
598 if (blk_rq_map_integrity_sg(q, req->bio, &iod->meta_sg) != 1)
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200599 goto out_unmap;
600
601 if (rq_data_dir(req))
602 nvme_dif_remap(req, nvme_dif_prep);
603
Christoph Hellwigbf684052015-10-26 17:12:51 +0900604 if (!dma_map_sg(dev->dev, &iod->meta_sg, 1, dma_dir))
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200605 goto out_unmap;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200606 }
607
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200608 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
609 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
610 if (blk_integrity_rq(req))
Christoph Hellwigbf684052015-10-26 17:12:51 +0900611 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(&iod->meta_sg));
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200612 return BLK_MQ_RQ_QUEUE_OK;
613
614out_unmap:
615 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
616out:
617 return ret;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200618}
619
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100620static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod)
621{
622 struct request *req = iod_get_private(iod);
623 enum dma_data_direction dma_dir = rq_data_dir(req) ?
624 DMA_TO_DEVICE : DMA_FROM_DEVICE;
625
626 if (iod->nents) {
627 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
628 if (blk_integrity_rq(req)) {
629 if (!rq_data_dir(req))
630 nvme_dif_remap(req, nvme_dif_complete);
Christoph Hellwigbf684052015-10-26 17:12:51 +0900631 dma_unmap_sg(dev->dev, &iod->meta_sg, 1, dma_dir);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100632 }
633 }
634
635 nvme_free_iod(dev, iod);
636}
637
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700638/*
639 * We reuse the small pool to allocate the 16-byte range here as it is not
640 * worth having a special pool for these or additional cases to handle freeing
641 * the iod.
642 */
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200643static int nvme_setup_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
644 struct nvme_iod *iod, struct nvme_command *cmnd)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700645{
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200646 struct request *req = iod_get_private(iod);
647 struct nvme_dsm_range *range;
648
649 range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC,
650 &iod->first_dma);
651 if (!range)
652 return BLK_MQ_RQ_QUEUE_BUSY;
653 iod_list(iod)[0] = (__le64 *)range;
654 iod->npages = 0;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700655
Keith Busch0e5e4f02012-11-09 16:33:05 -0700656 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700657 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
658 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700659
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200660 memset(cmnd, 0, sizeof(*cmnd));
661 cmnd->dsm.opcode = nvme_cmd_dsm;
662 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
663 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
664 cmnd->dsm.nr = 0;
665 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
666 return BLK_MQ_RQ_QUEUE_OK;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700667}
668
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200669/*
670 * NOTE: ns is NULL when called on the admin queue.
671 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700672static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
673 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600674{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700675 struct nvme_ns *ns = hctx->queue->queuedata;
676 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200677 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700678 struct request *req = bd->rq;
679 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
Keith Buschedd10d32014-04-03 16:45:23 -0600680 struct nvme_iod *iod;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200681 struct nvme_command cmnd;
682 int ret = BLK_MQ_RQ_QUEUE_OK;
Keith Buschedd10d32014-04-03 16:45:23 -0600683
Keith Busche1e5e562015-02-19 13:39:03 -0700684 /*
685 * If formated with metadata, require the block layer provide a buffer
686 * unless this namespace is formated such that the metadata can be
687 * stripped/generated by the controller with PRACT=1.
688 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200689 if (ns && ns->ms && !blk_integrity_rq(req)) {
Keith Busch71feb362015-06-19 11:07:30 -0600690 if (!(ns->pi_type && ns->ms == 8) &&
691 req->cmd_type != REQ_TYPE_DRV_PRIV) {
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100692 blk_mq_end_request(req, -EFAULT);
Keith Busche1e5e562015-02-19 13:39:03 -0700693 return BLK_MQ_RQ_QUEUE_OK;
694 }
695 }
696
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200697 iod = nvme_alloc_iod(req, dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600698 if (!iod)
Jens Axboefe543032014-12-11 13:58:39 -0700699 return BLK_MQ_RQ_QUEUE_BUSY;
Keith Buschedd10d32014-04-03 16:45:23 -0600700
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700701 if (req->cmd_flags & REQ_DISCARD) {
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200702 ret = nvme_setup_discard(nvmeq, ns, iod, &cmnd);
703 } else {
704 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
705 memcpy(&cmnd, req->cmd, sizeof(cmnd));
706 else if (req->cmd_flags & REQ_FLUSH)
707 nvme_setup_flush(ns, &cmnd);
708 else
709 nvme_setup_rw(ns, req, &cmnd);
Keith Buschedd10d32014-04-03 16:45:23 -0600710
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200711 if (req->nr_phys_segments)
712 ret = nvme_map_data(dev, iod, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700713 }
714
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200715 if (ret)
716 goto out;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700717
Christoph Hellwigaae239e2015-11-26 12:59:50 +0100718 cmd->iod = iod;
719 cmd->aborted = 0;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200720 cmnd.common.command_id = req->tag;
Christoph Hellwigaae239e2015-11-26 12:59:50 +0100721 blk_mq_start_request(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200722
723 spin_lock_irq(&nvmeq->q_lock);
724 __nvme_submit_cmd(nvmeq, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700725 nvme_process_cq(nvmeq);
726 spin_unlock_irq(&nvmeq->q_lock);
727 return BLK_MQ_RQ_QUEUE_OK;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200728out:
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200729 nvme_free_iod(dev, iod);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200730 return ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500731}
732
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100733static void nvme_complete_rq(struct request *req)
734{
735 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
736 struct nvme_dev *dev = cmd->nvmeq->dev;
737 int error = 0;
738
739 nvme_unmap_data(dev, cmd->iod);
740
741 if (unlikely(req->errors)) {
742 if (nvme_req_needs_retry(req, req->errors)) {
743 nvme_requeue_req(req);
744 return;
745 }
746
747 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
748 error = req->errors;
749 else
750 error = nvme_error_status(req->errors);
751 }
752
753 if (unlikely(cmd->aborted)) {
754 dev_warn(dev->dev,
755 "completing aborted command with status: %04x\n",
756 req->errors);
757 }
758
759 blk_mq_end_request(req, error);
760}
761
Jens Axboea0fa9642015-11-03 20:37:26 -0700762static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500763{
Matthew Wilcox82123462011-01-20 13:24:06 -0500764 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500765
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500766 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500767 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500768
769 for (;;) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500770 struct nvme_completion cqe = nvmeq->cqes[head];
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100771 u16 status = le16_to_cpu(cqe.status);
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100772 struct request *req;
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100773
774 if ((status & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500775 break;
776 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
777 if (++head == nvmeq->q_depth) {
778 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500779 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500780 }
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100781
Jens Axboea0fa9642015-11-03 20:37:26 -0700782 if (tag && *tag == cqe.command_id)
783 *tag = -1;
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100784
Christoph Hellwigaae239e2015-11-26 12:59:50 +0100785 if (unlikely(cqe.command_id >= nvmeq->q_depth)) {
786 dev_warn(nvmeq->q_dmadev,
787 "invalid id %d completed on queue %d\n",
788 cqe.command_id, le16_to_cpu(cqe.sq_id));
789 continue;
790 }
791
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100792 /*
793 * AEN requests are special as they don't time out and can
794 * survive any kind of queue freeze and often don't respond to
795 * aborts. We don't even bother to allocate a struct request
796 * for them but rather special case them here.
797 */
798 if (unlikely(nvmeq->qid == 0 &&
799 cqe.command_id >= NVME_AQ_BLKMQ_DEPTH)) {
800 nvme_complete_async_event(nvmeq->dev, &cqe);
801 continue;
802 }
803
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100804 req = blk_mq_tag_to_rq(*nvmeq->tags, cqe.command_id);
805 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
806 u32 result = le32_to_cpu(cqe.result);
807 req->special = (void *)(uintptr_t)result;
808 }
809 blk_mq_complete_request(req, status >> 1);
810
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500811 }
812
813 /* If the controller ignores the cq head doorbell and continuously
814 * writes to the queue, it is theoretically possible to wrap around
815 * the queue twice and mistakenly return IRQ_NONE. Linux only
816 * requires that 0.1% of your interrupts are handled, so this isn't
817 * a big problem.
818 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500819 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Jens Axboea0fa9642015-11-03 20:37:26 -0700820 return;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500821
Keith Busch604e8c82015-11-20 08:38:13 -0700822 if (likely(nvmeq->cq_vector >= 0))
823 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500824 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500825 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500826
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400827 nvmeq->cqe_seen = 1;
Jens Axboea0fa9642015-11-03 20:37:26 -0700828}
829
830static void nvme_process_cq(struct nvme_queue *nvmeq)
831{
832 __nvme_process_cq(nvmeq, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500833}
834
835static irqreturn_t nvme_irq(int irq, void *data)
836{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500837 irqreturn_t result;
838 struct nvme_queue *nvmeq = data;
839 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400840 nvme_process_cq(nvmeq);
841 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
842 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500843 spin_unlock(&nvmeq->q_lock);
844 return result;
845}
846
847static irqreturn_t nvme_irq_check(int irq, void *data)
848{
849 struct nvme_queue *nvmeq = data;
850 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
851 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
852 return IRQ_NONE;
853 return IRQ_WAKE_THREAD;
854}
855
Jens Axboea0fa9642015-11-03 20:37:26 -0700856static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
857{
858 struct nvme_queue *nvmeq = hctx->driver_data;
859
860 if ((le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) ==
861 nvmeq->cq_phase) {
862 spin_lock_irq(&nvmeq->q_lock);
863 __nvme_process_cq(nvmeq, &tag);
864 spin_unlock_irq(&nvmeq->q_lock);
865
866 if (tag == -1)
867 return 1;
868 }
869
870 return 0;
871}
872
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100873static void nvme_submit_async_event(struct nvme_dev *dev)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700874{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700875 struct nvme_command c;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700876
877 memset(&c, 0, sizeof(c));
878 c.common.opcode = nvme_admin_async_event;
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100879 c.common.command_id = NVME_AQ_BLKMQ_DEPTH + --dev->ctrl.event_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700880
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100881 __nvme_submit_cmd(dev->queues[0], &c);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700882}
883
Christoph Hellwigd8f32162015-11-16 10:28:47 +0100884static void async_cmd_info_endio(struct request *req, int error)
Keith Busch4d115422013-12-10 13:10:40 -0700885{
Christoph Hellwigd8f32162015-11-16 10:28:47 +0100886 struct async_cmd_info *cmdinfo = req->end_io_data;
Keith Busch4d115422013-12-10 13:10:40 -0700887
Christoph Hellwigd8f32162015-11-16 10:28:47 +0100888 cmdinfo->status = req->errors;
889 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
890 blk_mq_free_request(req);
Keith Busch4d115422013-12-10 13:10:40 -0700891}
892
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500893static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
894{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500895 struct nvme_command c;
896
897 memset(&c, 0, sizeof(c));
898 c.delete_queue.opcode = opcode;
899 c.delete_queue.qid = cpu_to_le16(id);
900
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100901 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500902}
903
904static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
905 struct nvme_queue *nvmeq)
906{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500907 struct nvme_command c;
908 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
909
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200910 /*
911 * Note: we (ab)use the fact the the prp fields survive if no data
912 * is attached to the request.
913 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500914 memset(&c, 0, sizeof(c));
915 c.create_cq.opcode = nvme_admin_create_cq;
916 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
917 c.create_cq.cqid = cpu_to_le16(qid);
918 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
919 c.create_cq.cq_flags = cpu_to_le16(flags);
920 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
921
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100922 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500923}
924
925static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
926 struct nvme_queue *nvmeq)
927{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500928 struct nvme_command c;
929 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
930
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200931 /*
932 * Note: we (ab)use the fact the the prp fields survive if no data
933 * is attached to the request.
934 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500935 memset(&c, 0, sizeof(c));
936 c.create_sq.opcode = nvme_admin_create_sq;
937 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
938 c.create_sq.sqid = cpu_to_le16(qid);
939 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
940 c.create_sq.sq_flags = cpu_to_le16(flags);
941 c.create_sq.cqid = cpu_to_le16(qid);
942
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100943 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500944}
945
946static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
947{
948 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
949}
950
951static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
952{
953 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
954}
955
Christoph Hellwige7a2a872015-11-16 10:39:48 +0100956static void abort_endio(struct request *req, int error)
957{
958 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
959 struct nvme_queue *nvmeq = cmd->nvmeq;
960 u32 result = (u32)(uintptr_t)req->special;
961 u16 status = req->errors;
962
963 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
964 atomic_inc(&nvmeq->dev->ctrl.abort_limit);
965
966 blk_mq_free_request(req);
967}
968
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +0200969static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
Keith Buschc30341d2013-12-10 13:10:38 -0700970{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700971 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
972 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -0700973 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700974 struct request *abort_req;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700975 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -0700976
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +0200977 /*
Christoph Hellwigfd634f412015-11-26 12:42:26 +0100978 * Shutdown immediately if controller times out while starting. The
979 * reset work will see the pci device disabled when it gets the forced
980 * cancellation error. All outstanding requests are completed on
981 * shutdown, so we return BLK_EH_HANDLED.
982 */
983 if (test_bit(NVME_CTRL_RESETTING, &dev->flags)) {
984 dev_warn(dev->dev,
985 "I/O %d QID %d timeout, disable controller\n",
986 req->tag, nvmeq->qid);
987 nvme_dev_shutdown(dev);
988 req->errors = NVME_SC_CANCELLED;
989 return BLK_EH_HANDLED;
990 }
991
992 /*
993 * Shutdown the controller immediately and schedule a reset if the
994 * command was already aborted once before and still hasn't been
995 * returned to the driver, or if this is the admin queue.
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +0200996 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700997 if (!nvmeq->qid || cmd_rq->aborted) {
Keith Busche1569a12015-11-26 12:11:07 +0100998 dev_warn(dev->dev,
999 "I/O %d QID %d timeout, reset controller\n",
1000 req->tag, nvmeq->qid);
1001 nvme_dev_shutdown(dev);
1002 queue_work(nvme_workq, &dev->reset_work);
1003
1004 /*
1005 * Mark the request as handled, since the inline shutdown
1006 * forces all outstanding requests to complete.
1007 */
1008 req->errors = NVME_SC_CANCELLED;
1009 return BLK_EH_HANDLED;
Keith Buschc30341d2013-12-10 13:10:38 -07001010 }
1011
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001012 cmd_rq->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -07001013
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001014 if (atomic_dec_return(&dev->ctrl.abort_limit) < 0) {
1015 atomic_inc(&dev->ctrl.abort_limit);
1016 return BLK_EH_RESET_TIMER;
1017 }
1018
1019 memset(&cmd, 0, sizeof(cmd));
1020 cmd.abort.opcode = nvme_admin_abort_cmd;
1021 cmd.abort.cid = req->tag;
1022 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
1023
1024 dev_warn(nvmeq->q_dmadev, "I/O %d QID %d timeout, aborting\n",
1025 req->tag, nvmeq->qid);
1026
1027 abort_req = nvme_alloc_request(dev->ctrl.admin_q, &cmd,
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001028 BLK_MQ_REQ_NOWAIT);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001029 if (IS_ERR(abort_req)) {
1030 atomic_inc(&dev->ctrl.abort_limit);
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001031 return BLK_EH_RESET_TIMER;
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001032 }
Keith Buschc30341d2013-12-10 13:10:38 -07001033
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001034 abort_req->timeout = ADMIN_TIMEOUT;
1035 abort_req->end_io_data = NULL;
1036 blk_execute_rq_nowait(abort_req->q, NULL, abort_req, 0, abort_endio);
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001037
1038 /*
1039 * The aborted req will be completed on receiving the abort req.
1040 * We enable the timer again. If hit twice, it'll cause a device reset,
1041 * as the device then is in a faulty state.
1042 */
1043 return BLK_EH_RESET_TIMER;
Keith Buschc30341d2013-12-10 13:10:38 -07001044}
1045
Keith Busch42483222015-06-01 09:29:54 -06001046static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001047{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001048 struct nvme_queue *nvmeq = data;
Christoph Hellwigaae239e2015-11-26 12:59:50 +01001049 int status;
Keith Buschcef6a942015-01-07 18:55:51 -07001050
1051 if (!blk_mq_request_started(req))
1052 return;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001053
Christoph Hellwigaae239e2015-11-26 12:59:50 +01001054 dev_warn(nvmeq->q_dmadev,
1055 "Cancelling I/O %d QID %d\n", req->tag, nvmeq->qid);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001056
Christoph Hellwigaae239e2015-11-26 12:59:50 +01001057 status = NVME_SC_CANCELLED;
Keith Buschcef6a942015-01-07 18:55:51 -07001058 if (blk_queue_dying(req->q))
Christoph Hellwigaae239e2015-11-26 12:59:50 +01001059 status |= NVME_SC_DNR;
1060 blk_mq_complete_request(req, status);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001061}
1062
Keith Buschf435c282014-07-07 09:14:42 -06001063static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001064{
1065 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1066 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001067 if (nvmeq->sq_cmds)
1068 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
Matthew Wilcox9e866772012-08-03 13:55:56 -04001069 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1070 kfree(nvmeq);
1071}
1072
Keith Buscha1a5ef92013-12-16 13:50:00 -05001073static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001074{
1075 int i;
1076
Keith Buscha1a5ef92013-12-16 13:50:00 -05001077 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001078 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001079 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001080 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001081 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001082 }
Keith Busch22404272013-07-15 15:02:20 -06001083}
1084
Keith Busch4d115422013-12-10 13:10:40 -07001085/**
1086 * nvme_suspend_queue - put queue into suspended state
1087 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001088 */
1089static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001090{
Keith Busch2b25d982014-12-22 12:59:04 -07001091 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001092
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001093 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001094 if (nvmeq->cq_vector == -1) {
1095 spin_unlock_irq(&nvmeq->q_lock);
1096 return 1;
1097 }
1098 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Keith Busch42f61422014-03-24 10:46:25 -06001099 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001100 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001101 spin_unlock_irq(&nvmeq->q_lock);
1102
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001103 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
1104 blk_mq_freeze_queue_start(nvmeq->dev->ctrl.admin_q);
Keith Busch6df3dbc2015-03-26 13:49:33 -06001105
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001106 irq_set_affinity_hint(vector, NULL);
1107 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001108
Keith Busch4d115422013-12-10 13:10:40 -07001109 return 0;
1110}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001111
Keith Busch4d115422013-12-10 13:10:40 -07001112static void nvme_clear_queue(struct nvme_queue *nvmeq)
1113{
Keith Busch22404272013-07-15 15:02:20 -06001114 spin_lock_irq(&nvmeq->q_lock);
Keith Busch42483222015-06-01 09:29:54 -06001115 if (nvmeq->tags && *nvmeq->tags)
1116 blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001117 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001118}
1119
Keith Busch4d115422013-12-10 13:10:40 -07001120static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1121{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001122 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001123
1124 if (!nvmeq)
1125 return;
1126 if (nvme_suspend_queue(nvmeq))
1127 return;
1128
Keith Busch0e53d182013-12-10 13:10:39 -07001129 /* Don't tell the adapter to delete the admin queue.
1130 * Don't tell a removed adapter to delete IO queues. */
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001131 if (qid && readl(dev->bar + NVME_REG_CSTS) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001132 adapter_delete_sq(dev, qid);
1133 adapter_delete_cq(dev, qid);
1134 }
Keith Busch07836e62015-02-19 10:34:48 -07001135
1136 spin_lock_irq(&nvmeq->q_lock);
1137 nvme_process_cq(nvmeq);
1138 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001139}
1140
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001141static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1142 int entry_size)
1143{
1144 int q_depth = dev->q_depth;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001145 unsigned q_size_aligned = roundup(q_depth * entry_size,
1146 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001147
1148 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001149 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001150 mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
Jon Derrickc45f5c92015-07-21 15:08:13 -06001151 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001152
1153 /*
1154 * Ensure the reduced q_depth is above some threshold where it
1155 * would be better to map queues in system memory with the
1156 * original depth
1157 */
1158 if (q_depth < 64)
1159 return -ENOMEM;
1160 }
1161
1162 return q_depth;
1163}
1164
1165static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1166 int qid, int depth)
1167{
1168 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001169 unsigned offset = (qid - 1) * roundup(SQ_SIZE(depth),
1170 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001171 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1172 nvmeq->sq_cmds_io = dev->cmb + offset;
1173 } else {
1174 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1175 &nvmeq->sq_dma_addr, GFP_KERNEL);
1176 if (!nvmeq->sq_cmds)
1177 return -ENOMEM;
1178 }
1179
1180 return 0;
1181}
1182
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001183static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Keith Busch2b25d982014-12-22 12:59:04 -07001184 int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001185{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001186 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001187 if (!nvmeq)
1188 return NULL;
1189
Christoph Hellwige75ec752015-05-22 11:12:39 +02001190 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001191 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001192 if (!nvmeq->cqes)
1193 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001194
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001195 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001196 goto free_cqdma;
1197
Christoph Hellwige75ec752015-05-22 11:12:39 +02001198 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001199 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001200 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001201 dev->ctrl.instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001202 spin_lock_init(&nvmeq->q_lock);
1203 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001204 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001205 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001206 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001207 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001208 nvmeq->cq_vector = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001209 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001210
Jon Derrick36a7e992015-05-27 12:26:23 -06001211 /* make sure queue descriptor is set before queue count, for kthread */
1212 mb();
1213 dev->queue_count++;
1214
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001215 return nvmeq;
1216
1217 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001218 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001219 nvmeq->cq_dma_addr);
1220 free_nvmeq:
1221 kfree(nvmeq);
1222 return NULL;
1223}
1224
Matthew Wilcox30010822011-01-20 09:10:15 -05001225static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1226 const char *name)
1227{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001228 if (use_threaded_interrupts)
1229 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001230 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001231 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001232 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001233 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001234}
1235
Keith Busch22404272013-07-15 15:02:20 -06001236static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001237{
Keith Busch22404272013-07-15 15:02:20 -06001238 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001239
Keith Busch7be50e92014-09-10 15:48:47 -06001240 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001241 nvmeq->sq_tail = 0;
1242 nvmeq->cq_head = 0;
1243 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001244 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001245 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001246 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001247 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001248}
1249
1250static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1251{
1252 struct nvme_dev *dev = nvmeq->dev;
1253 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001254
Keith Busch2b25d982014-12-22 12:59:04 -07001255 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001256 result = adapter_alloc_cq(dev, qid, nvmeq);
1257 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001258 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001259
1260 result = adapter_alloc_sq(dev, qid, nvmeq);
1261 if (result < 0)
1262 goto release_cq;
1263
Matthew Wilcox3193f072014-01-27 15:57:22 -05001264 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001265 if (result < 0)
1266 goto release_sq;
1267
Keith Busch22404272013-07-15 15:02:20 -06001268 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001269 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001270
1271 release_sq:
1272 adapter_delete_sq(dev, qid);
1273 release_cq:
1274 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001275 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001276}
1277
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001278static struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001279 .queue_rq = nvme_queue_rq,
Christoph Hellwigeee417b2015-11-26 13:03:13 +01001280 .complete = nvme_complete_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001281 .map_queue = blk_mq_map_queue,
1282 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001283 .exit_hctx = nvme_admin_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001284 .init_request = nvme_admin_init_request,
1285 .timeout = nvme_timeout,
1286};
1287
1288static struct blk_mq_ops nvme_mq_ops = {
1289 .queue_rq = nvme_queue_rq,
Christoph Hellwigeee417b2015-11-26 13:03:13 +01001290 .complete = nvme_complete_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001291 .map_queue = blk_mq_map_queue,
1292 .init_hctx = nvme_init_hctx,
1293 .init_request = nvme_init_request,
1294 .timeout = nvme_timeout,
Jens Axboea0fa9642015-11-03 20:37:26 -07001295 .poll = nvme_poll,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001296};
1297
Keith Buschea191d22015-01-07 18:55:49 -07001298static void nvme_dev_remove_admin(struct nvme_dev *dev)
1299{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001300 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
1301 blk_cleanup_queue(dev->ctrl.admin_q);
Keith Buschea191d22015-01-07 18:55:49 -07001302 blk_mq_free_tag_set(&dev->admin_tagset);
1303 }
1304}
1305
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001306static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1307{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001308 if (!dev->ctrl.admin_q) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001309 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1310 dev->admin_tagset.nr_hw_queues = 1;
Christoph Hellwigadf68f22015-11-28 15:42:28 +01001311 dev->admin_tagset.queue_depth = NVME_AQ_BLKMQ_DEPTH;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001312 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001313 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001314 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001315 dev->admin_tagset.driver_data = dev;
1316
1317 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1318 return -ENOMEM;
1319
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001320 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1321 if (IS_ERR(dev->ctrl.admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001322 blk_mq_free_tag_set(&dev->admin_tagset);
1323 return -ENOMEM;
1324 }
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001325 if (!blk_get_queue(dev->ctrl.admin_q)) {
Keith Buschea191d22015-01-07 18:55:49 -07001326 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001327 dev->ctrl.admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001328 return -ENODEV;
1329 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001330 } else
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001331 blk_mq_unfreeze_queue(dev->ctrl.admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001332
1333 return 0;
1334}
1335
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001336static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001337{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001338 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001339 u32 aqa;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001340 u64 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001341 struct nvme_queue *nvmeq;
1342
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001343 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1) ?
Keith Buschdfbac8c2015-08-10 15:20:40 -06001344 NVME_CAP_NSSRC(cap) : 0;
1345
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001346 if (dev->subsystem &&
1347 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1348 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001349
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001350 result = nvme_disable_ctrl(&dev->ctrl, cap);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001351 if (result < 0)
1352 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001353
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001354 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001355 if (!nvmeq) {
Keith Busch2b25d982014-12-22 12:59:04 -07001356 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001357 if (!nvmeq)
1358 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001359 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001360
1361 aqa = nvmeq->q_depth - 1;
1362 aqa |= aqa << 16;
1363
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001364 writel(aqa, dev->bar + NVME_REG_AQA);
1365 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1366 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001367
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001368 result = nvme_enable_ctrl(&dev->ctrl, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001369 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001370 goto free_nvmeq;
1371
Keith Busch2b25d982014-12-22 12:59:04 -07001372 nvmeq->cq_vector = 0;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001373 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001374 if (result) {
1375 nvmeq->cq_vector = -1;
Keith Busch0fb59cb2015-01-07 18:55:50 -07001376 goto free_nvmeq;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001377 }
Keith Busch025c5572013-05-01 13:07:51 -06001378
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001379 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001380
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001381 free_nvmeq:
1382 nvme_free_queues(dev, 0);
1383 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001384}
1385
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001386static int nvme_kthread(void *data)
1387{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001388 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001389
1390 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001391 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001392 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001393 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001394 int i;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001395 u32 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001396
Christoph Hellwig846cc052015-11-26 12:10:29 +01001397 /*
1398 * Skip controllers currently under reset.
1399 */
1400 if (work_pending(&dev->reset_work) || work_busy(&dev->reset_work))
1401 continue;
1402
Keith Buschdfbac8c2015-08-10 15:20:40 -06001403 if ((dev->subsystem && (csts & NVME_CSTS_NSSRO)) ||
1404 csts & NVME_CSTS_CFS) {
Christoph Hellwig846cc052015-11-26 12:10:29 +01001405 if (queue_work(nvme_workq, &dev->reset_work)) {
Christoph Hellwig906678922015-10-02 18:49:23 +02001406 dev_warn(dev->dev,
1407 "Failed status: %x, reset controller\n",
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001408 readl(dev->bar + NVME_REG_CSTS));
Christoph Hellwig906678922015-10-02 18:49:23 +02001409 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07001410 continue;
1411 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001412 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001413 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001414 if (!nvmeq)
1415 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001416 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001417 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06001418
Christoph Hellwigadf68f22015-11-28 15:42:28 +01001419 while (i == 0 && dev->ctrl.event_limit > 0)
1420 nvme_submit_async_event(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001421 spin_unlock_irq(&nvmeq->q_lock);
1422 }
1423 }
1424 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001425 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001426 }
1427 return 0;
1428}
1429
Christoph Hellwig749941f2015-11-26 11:46:39 +01001430static int nvme_create_io_queues(struct nvme_dev *dev)
Keith Busch42f61422014-03-24 10:46:25 -06001431{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001432 unsigned i;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001433 int ret = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001434
Christoph Hellwig749941f2015-11-26 11:46:39 +01001435 for (i = dev->queue_count; i <= dev->max_qid; i++) {
1436 if (!nvme_alloc_queue(dev, i, dev->q_depth)) {
1437 ret = -ENOMEM;
Keith Busch42f61422014-03-24 10:46:25 -06001438 break;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001439 }
1440 }
Keith Busch42f61422014-03-24 10:46:25 -06001441
Christoph Hellwig749941f2015-11-26 11:46:39 +01001442 for (i = dev->online_queues; i <= dev->queue_count - 1; i++) {
1443 ret = nvme_create_queue(dev->queues[i], i);
1444 if (ret) {
Christoph Hellwig2659e572015-10-02 18:51:31 +02001445 nvme_free_queues(dev, i);
Keith Busch42f61422014-03-24 10:46:25 -06001446 break;
Christoph Hellwig2659e572015-10-02 18:51:31 +02001447 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01001448 }
1449
1450 /*
1451 * Ignore failing Create SQ/CQ commands, we can continue with less
1452 * than the desired aount of queues, and even a controller without
1453 * I/O queues an still be used to issue admin commands. This might
1454 * be useful to upgrade a buggy firmware for example.
1455 */
1456 return ret >= 0 ? 0 : ret;
Keith Busch42f61422014-03-24 10:46:25 -06001457}
1458
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001459static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
1460{
1461 u64 szu, size, offset;
1462 u32 cmbloc;
1463 resource_size_t bar_size;
1464 struct pci_dev *pdev = to_pci_dev(dev->dev);
1465 void __iomem *cmb;
1466 dma_addr_t dma_addr;
1467
1468 if (!use_cmb_sqes)
1469 return NULL;
1470
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001471 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001472 if (!(NVME_CMB_SZ(dev->cmbsz)))
1473 return NULL;
1474
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001475 cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001476
1477 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
1478 size = szu * NVME_CMB_SZ(dev->cmbsz);
1479 offset = szu * NVME_CMB_OFST(cmbloc);
1480 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(cmbloc));
1481
1482 if (offset > bar_size)
1483 return NULL;
1484
1485 /*
1486 * Controllers may support a CMB size larger than their BAR,
1487 * for example, due to being behind a bridge. Reduce the CMB to
1488 * the reported size of the BAR
1489 */
1490 if (size > bar_size - offset)
1491 size = bar_size - offset;
1492
1493 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(cmbloc)) + offset;
1494 cmb = ioremap_wc(dma_addr, size);
1495 if (!cmb)
1496 return NULL;
1497
1498 dev->cmb_dma_addr = dma_addr;
1499 dev->cmb_size = size;
1500 return cmb;
1501}
1502
1503static inline void nvme_release_cmb(struct nvme_dev *dev)
1504{
1505 if (dev->cmb) {
1506 iounmap(dev->cmb);
1507 dev->cmb = NULL;
1508 }
1509}
1510
Keith Busch9d713c22013-07-15 15:02:24 -06001511static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1512{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001513 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06001514}
1515
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001516static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001517{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001518 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02001519 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch42f61422014-03-24 10:46:25 -06001520 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001521
Keith Busch42f61422014-03-24 10:46:25 -06001522 nr_io_queues = num_possible_cpus();
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001523 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
1524 if (result < 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001525 return result;
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001526
1527 /*
1528 * Degraded controllers might return an error when setting the queue
1529 * count. We still want to be able to bring them online and offer
1530 * access to the admin queue, as that might be only way to fix them up.
1531 */
1532 if (result > 0) {
1533 dev_err(dev->dev, "Could not set queue count (%d)\n", result);
1534 nr_io_queues = 0;
1535 result = 0;
1536 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001537
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001538 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
1539 result = nvme_cmb_qdepth(dev, nr_io_queues,
1540 sizeof(struct nvme_command));
1541 if (result > 0)
1542 dev->q_depth = result;
1543 else
1544 nvme_release_cmb(dev);
1545 }
1546
Keith Busch9d713c22013-07-15 15:02:24 -06001547 size = db_bar_size(dev, nr_io_queues);
1548 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001549 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06001550 do {
1551 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1552 if (dev->bar)
1553 break;
1554 if (!--nr_io_queues)
1555 return -ENOMEM;
1556 size = db_bar_size(dev, nr_io_queues);
1557 } while (1);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001558 dev->dbs = dev->bar + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07001559 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001560 }
1561
Keith Busch9d713c22013-07-15 15:02:24 -06001562 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05001563 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06001564
Jens Axboee32efbf2014-11-14 09:49:26 -07001565 /*
1566 * If we enable msix early due to not intx, disable it again before
1567 * setting up the full range we need.
1568 */
1569 if (!pdev->irq)
1570 pci_disable_msix(pdev);
1571
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01001572 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001573 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01001574 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
1575 if (vecs < 0) {
1576 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
1577 if (vecs < 0) {
1578 vecs = 1;
1579 } else {
1580 for (i = 0; i < vecs; i++)
1581 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001582 }
1583 }
1584
Matthew Wilcox063a8092013-06-20 10:53:48 -04001585 /*
1586 * Should investigate if there's a performance win from allocating
1587 * more queues than interrupt vectors; it might allow the submission
1588 * path to scale better, even if the receive path is limited by the
1589 * number of interrupts.
1590 */
1591 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06001592 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001593
Matthew Wilcox3193f072014-01-27 15:57:22 -05001594 result = queue_request_irq(dev, adminq, adminq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001595 if (result) {
1596 adminq->cq_vector = -1;
Keith Busch22404272013-07-15 15:02:20 -06001597 goto free_queues;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001598 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001599
Keith Buschcd638942013-07-15 15:02:23 -06001600 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06001601 nvme_free_queues(dev, nr_io_queues + 1);
Christoph Hellwig749941f2015-11-26 11:46:39 +01001602 return nvme_create_io_queues(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001603
Keith Busch22404272013-07-15 15:02:20 -06001604 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05001605 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06001606 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001607}
1608
Keith Buschbda4e0f2015-09-03 08:18:17 -06001609static void nvme_set_irq_hints(struct nvme_dev *dev)
1610{
1611 struct nvme_queue *nvmeq;
1612 int i;
1613
1614 for (i = 0; i < dev->online_queues; i++) {
1615 nvmeq = dev->queues[i];
1616
1617 if (!nvmeq->tags || !(*nvmeq->tags))
1618 continue;
1619
1620 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
1621 blk_mq_tags_cpumask(*nvmeq->tags));
1622 }
1623}
1624
Keith Buscha5768aa2015-06-01 14:28:14 -06001625static void nvme_dev_scan(struct work_struct *work)
1626{
1627 struct nvme_dev *dev = container_of(work, struct nvme_dev, scan_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06001628
1629 if (!dev->tagset.tags)
1630 return;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001631 nvme_scan_namespaces(&dev->ctrl);
Keith Buschbda4e0f2015-09-03 08:18:17 -06001632 nvme_set_irq_hints(dev);
Keith Buscha5768aa2015-06-01 14:28:14 -06001633}
1634
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04001635/*
1636 * Return: error value if an error occurred setting up the queues or calling
1637 * Identify Device. 0 if these succeeded, even if adding some of the
1638 * namespaces failed. At the moment, these failures are silent. TBD which
1639 * failures should be reported.
1640 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001641static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001642{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001643 if (!dev->ctrl.tagset) {
Keith Buschffe77042015-06-08 10:08:15 -06001644 dev->tagset.ops = &nvme_mq_ops;
1645 dev->tagset.nr_hw_queues = dev->online_queues - 1;
1646 dev->tagset.timeout = NVME_IO_TIMEOUT;
1647 dev->tagset.numa_node = dev_to_node(dev->dev);
1648 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001649 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Keith Buschffe77042015-06-08 10:08:15 -06001650 dev->tagset.cmd_size = nvme_cmd_size(dev);
1651 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
1652 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001653
Keith Buschffe77042015-06-08 10:08:15 -06001654 if (blk_mq_alloc_tag_set(&dev->tagset))
1655 return 0;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001656 dev->ctrl.tagset = &dev->tagset;
Keith Buschffe77042015-06-08 10:08:15 -06001657 }
Keith Busch92f7a162015-10-23 11:42:02 -06001658 queue_work(nvme_workq, &dev->scan_work);
Keith Busche1e5e562015-02-19 13:39:03 -07001659 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001660}
1661
Keith Busch0877cb02013-07-15 15:02:19 -06001662static int nvme_dev_map(struct nvme_dev *dev)
1663{
Keith Busch42f61422014-03-24 10:46:25 -06001664 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06001665 int bars, result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001666 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001667
1668 if (pci_enable_device_mem(pdev))
1669 return result;
1670
1671 dev->entry[0].vector = pdev->irq;
1672 pci_set_master(pdev);
1673 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07001674 if (!bars)
1675 goto disable_pci;
1676
Keith Busch0877cb02013-07-15 15:02:19 -06001677 if (pci_request_selected_regions(pdev, bars, "nvme"))
1678 goto disable_pci;
1679
Christoph Hellwige75ec752015-05-22 11:12:39 +02001680 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
1681 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01001682 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06001683
Keith Busch0877cb02013-07-15 15:02:19 -06001684 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1685 if (!dev->bar)
1686 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07001687
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001688 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
Keith Busch0e53d182013-12-10 13:10:39 -07001689 result = -ENODEV;
1690 goto unmap;
1691 }
Jens Axboee32efbf2014-11-14 09:49:26 -07001692
1693 /*
1694 * Some devices don't advertse INTx interrupts, pre-enable a single
1695 * MSIX vec for setup. We'll adjust this later.
1696 */
1697 if (!pdev->irq) {
1698 result = pci_enable_msix(pdev, dev->entry, 1);
1699 if (result < 0)
1700 goto unmap;
1701 }
1702
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001703 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
1704
Keith Busch42f61422014-03-24 10:46:25 -06001705 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
1706 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001707 dev->dbs = dev->bar + 4096;
1708 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2))
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001709 dev->cmb = nvme_map_cmb(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001710
1711 return 0;
1712
Keith Busch0e53d182013-12-10 13:10:39 -07001713 unmap:
1714 iounmap(dev->bar);
1715 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06001716 disable:
1717 pci_release_regions(pdev);
1718 disable_pci:
1719 pci_disable_device(pdev);
1720 return result;
1721}
1722
1723static void nvme_dev_unmap(struct nvme_dev *dev)
1724{
Christoph Hellwige75ec752015-05-22 11:12:39 +02001725 struct pci_dev *pdev = to_pci_dev(dev->dev);
1726
1727 if (pdev->msi_enabled)
1728 pci_disable_msi(pdev);
1729 else if (pdev->msix_enabled)
1730 pci_disable_msix(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001731
1732 if (dev->bar) {
1733 iounmap(dev->bar);
1734 dev->bar = NULL;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001735 pci_release_regions(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001736 }
1737
Christoph Hellwige75ec752015-05-22 11:12:39 +02001738 if (pci_is_enabled(pdev))
1739 pci_disable_device(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001740}
1741
Keith Busch4d115422013-12-10 13:10:40 -07001742struct nvme_delq_ctx {
1743 struct task_struct *waiter;
1744 struct kthread_worker *worker;
1745 atomic_t refcount;
1746};
1747
1748static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
1749{
1750 dq->waiter = current;
1751 mb();
1752
1753 for (;;) {
1754 set_current_state(TASK_KILLABLE);
1755 if (!atomic_read(&dq->refcount))
1756 break;
1757 if (!schedule_timeout(ADMIN_TIMEOUT) ||
1758 fatal_signal_pending(current)) {
Keith Busch0fb59cb2015-01-07 18:55:50 -07001759 /*
1760 * Disable the controller first since we can't trust it
1761 * at this point, but leave the admin queue enabled
1762 * until all queue deletion requests are flushed.
1763 * FIXME: This may take a while if there are more h/w
1764 * queues than admin tags.
1765 */
Keith Busch4d115422013-12-10 13:10:40 -07001766 set_current_state(TASK_RUNNING);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001767 nvme_disable_ctrl(&dev->ctrl,
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001768 lo_hi_readq(dev->bar + NVME_REG_CAP));
Keith Busch0fb59cb2015-01-07 18:55:50 -07001769 nvme_clear_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07001770 flush_kthread_worker(dq->worker);
Keith Busch0fb59cb2015-01-07 18:55:50 -07001771 nvme_disable_queue(dev, 0);
Keith Busch4d115422013-12-10 13:10:40 -07001772 return;
1773 }
1774 }
1775 set_current_state(TASK_RUNNING);
1776}
1777
1778static void nvme_put_dq(struct nvme_delq_ctx *dq)
1779{
1780 atomic_dec(&dq->refcount);
1781 if (dq->waiter)
1782 wake_up_process(dq->waiter);
1783}
1784
1785static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
1786{
1787 atomic_inc(&dq->refcount);
1788 return dq;
1789}
1790
1791static void nvme_del_queue_end(struct nvme_queue *nvmeq)
1792{
1793 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
Keith Busch4d115422013-12-10 13:10:40 -07001794 nvme_put_dq(dq);
Keith Busch604e8c82015-11-20 08:38:13 -07001795
1796 spin_lock_irq(&nvmeq->q_lock);
1797 nvme_process_cq(nvmeq);
1798 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07001799}
1800
1801static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
1802 kthread_work_func_t fn)
1803{
Christoph Hellwigd8f32162015-11-16 10:28:47 +01001804 struct request *req;
Keith Busch4d115422013-12-10 13:10:40 -07001805 struct nvme_command c;
1806
1807 memset(&c, 0, sizeof(c));
1808 c.delete_queue.opcode = opcode;
1809 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
1810
1811 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Christoph Hellwigd8f32162015-11-16 10:28:47 +01001812
1813 req = nvme_alloc_request(nvmeq->dev->ctrl.admin_q, &c, 0);
1814 if (IS_ERR(req))
1815 return PTR_ERR(req);
1816
1817 req->timeout = ADMIN_TIMEOUT;
1818 req->end_io_data = &nvmeq->cmdinfo;
1819 blk_execute_rq_nowait(req->q, NULL, req, 0, async_cmd_info_endio);
1820 return 0;
Keith Busch4d115422013-12-10 13:10:40 -07001821}
1822
1823static void nvme_del_cq_work_handler(struct kthread_work *work)
1824{
1825 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1826 cmdinfo.work);
1827 nvme_del_queue_end(nvmeq);
1828}
1829
1830static int nvme_delete_cq(struct nvme_queue *nvmeq)
1831{
1832 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
1833 nvme_del_cq_work_handler);
1834}
1835
1836static void nvme_del_sq_work_handler(struct kthread_work *work)
1837{
1838 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1839 cmdinfo.work);
1840 int status = nvmeq->cmdinfo.status;
1841
1842 if (!status)
1843 status = nvme_delete_cq(nvmeq);
1844 if (status)
1845 nvme_del_queue_end(nvmeq);
1846}
1847
1848static int nvme_delete_sq(struct nvme_queue *nvmeq)
1849{
1850 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
1851 nvme_del_sq_work_handler);
1852}
1853
1854static void nvme_del_queue_start(struct kthread_work *work)
1855{
1856 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1857 cmdinfo.work);
Keith Busch4d115422013-12-10 13:10:40 -07001858 if (nvme_delete_sq(nvmeq))
1859 nvme_del_queue_end(nvmeq);
1860}
1861
1862static void nvme_disable_io_queues(struct nvme_dev *dev)
1863{
1864 int i;
1865 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
1866 struct nvme_delq_ctx dq;
1867 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001868 &worker, "nvme%d", dev->ctrl.instance);
Keith Busch4d115422013-12-10 13:10:40 -07001869
1870 if (IS_ERR(kworker_task)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001871 dev_err(dev->dev,
Keith Busch4d115422013-12-10 13:10:40 -07001872 "Failed to create queue del task\n");
1873 for (i = dev->queue_count - 1; i > 0; i--)
1874 nvme_disable_queue(dev, i);
1875 return;
1876 }
1877
1878 dq.waiter = NULL;
1879 atomic_set(&dq.refcount, 0);
1880 dq.worker = &worker;
1881 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001882 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07001883
1884 if (nvme_suspend_queue(nvmeq))
1885 continue;
1886 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
1887 nvmeq->cmdinfo.worker = dq.worker;
1888 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
1889 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
1890 }
1891 nvme_wait_dq(&dq, dev);
1892 kthread_stop(kworker_task);
1893}
1894
Christoph Hellwig73850142015-10-22 14:03:33 +02001895static int nvme_dev_list_add(struct nvme_dev *dev)
1896{
1897 bool start_thread = false;
1898
1899 spin_lock(&dev_list_lock);
1900 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
1901 start_thread = true;
1902 nvme_thread = NULL;
1903 }
1904 list_add(&dev->node, &dev_list);
1905 spin_unlock(&dev_list_lock);
1906
1907 if (start_thread) {
1908 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
1909 wake_up_all(&nvme_kthread_wait);
1910 } else
1911 wait_event_killable(nvme_kthread_wait, nvme_thread);
1912
1913 if (IS_ERR_OR_NULL(nvme_thread))
1914 return nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
1915
1916 return 0;
1917}
1918
Dan McLeranb9afca32014-04-07 17:10:11 -06001919/*
1920* Remove the node from the device list and check
1921* for whether or not we need to stop the nvme_thread.
1922*/
1923static void nvme_dev_list_remove(struct nvme_dev *dev)
1924{
1925 struct task_struct *tmp = NULL;
1926
1927 spin_lock(&dev_list_lock);
1928 list_del_init(&dev->node);
1929 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
1930 tmp = nvme_thread;
1931 nvme_thread = NULL;
1932 }
1933 spin_unlock(&dev_list_lock);
1934
1935 if (tmp)
1936 kthread_stop(tmp);
1937}
1938
Keith Buschc9d3bf82015-01-07 18:55:52 -07001939static void nvme_freeze_queues(struct nvme_dev *dev)
1940{
1941 struct nvme_ns *ns;
1942
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001943 list_for_each_entry(ns, &dev->ctrl.namespaces, list) {
Keith Buschc9d3bf82015-01-07 18:55:52 -07001944 blk_mq_freeze_queue_start(ns->queue);
1945
Christoph Hellwigcddcd722015-05-07 09:38:14 +02001946 spin_lock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07001947 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
Christoph Hellwigcddcd722015-05-07 09:38:14 +02001948 spin_unlock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07001949
1950 blk_mq_cancel_requeue_work(ns->queue);
1951 blk_mq_stop_hw_queues(ns->queue);
1952 }
1953}
1954
1955static void nvme_unfreeze_queues(struct nvme_dev *dev)
1956{
1957 struct nvme_ns *ns;
1958
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001959 list_for_each_entry(ns, &dev->ctrl.namespaces, list) {
Keith Buschc9d3bf82015-01-07 18:55:52 -07001960 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
1961 blk_mq_unfreeze_queue(ns->queue);
1962 blk_mq_start_stopped_hw_queues(ns->queue, true);
1963 blk_mq_kick_requeue_list(ns->queue);
1964 }
1965}
1966
Keith Buschf0b50732013-07-15 15:02:21 -06001967static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001968{
Keith Busch22404272013-07-15 15:02:20 -06001969 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06001970 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06001971
Dan McLeranb9afca32014-04-07 17:10:11 -06001972 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001973
Keith Busch77bf25e2015-11-26 12:21:29 +01001974 mutex_lock(&dev->shutdown_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07001975 if (dev->bar) {
1976 nvme_freeze_queues(dev);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001977 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschc9d3bf82015-01-07 18:55:52 -07001978 }
Keith Busch7c1b2452014-06-25 11:18:12 -06001979 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07001980 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001981 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07001982 nvme_suspend_queue(nvmeq);
Keith Busch4d115422013-12-10 13:10:40 -07001983 }
1984 } else {
1985 nvme_disable_io_queues(dev);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001986 nvme_shutdown_ctrl(&dev->ctrl);
Keith Busch4d115422013-12-10 13:10:40 -07001987 nvme_disable_queue(dev, 0);
1988 }
Keith Buschf0b50732013-07-15 15:02:21 -06001989 nvme_dev_unmap(dev);
Keith Busch07836e62015-02-19 10:34:48 -07001990
1991 for (i = dev->queue_count - 1; i >= 0; i--)
1992 nvme_clear_queue(dev->queues[i]);
Keith Busch77bf25e2015-11-26 12:21:29 +01001993 mutex_unlock(&dev->shutdown_lock);
Keith Buschf0b50732013-07-15 15:02:21 -06001994}
1995
Matthew Wilcox091b6092011-02-10 09:56:01 -05001996static int nvme_setup_prp_pools(struct nvme_dev *dev)
1997{
Christoph Hellwige75ec752015-05-22 11:12:39 +02001998 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05001999 PAGE_SIZE, PAGE_SIZE, 0);
2000 if (!dev->prp_page_pool)
2001 return -ENOMEM;
2002
Matthew Wilcox99802a72011-02-10 10:30:34 -05002003 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02002004 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05002005 256, 256, 0);
2006 if (!dev->prp_small_pool) {
2007 dma_pool_destroy(dev->prp_page_pool);
2008 return -ENOMEM;
2009 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002010 return 0;
2011}
2012
2013static void nvme_release_prp_pools(struct nvme_dev *dev)
2014{
2015 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002016 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002017}
2018
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002019static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
Keith Busch5e82e952013-02-19 10:17:58 -07002020{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002021 struct nvme_dev *dev = to_nvme_dev(ctrl);
Keith Busch9ac27092014-01-31 16:53:39 -07002022
Christoph Hellwige75ec752015-05-22 11:12:39 +02002023 put_device(dev->dev);
Keith Busch4af0e212015-06-08 10:08:13 -06002024 if (dev->tagset.tags)
2025 blk_mq_free_tag_set(&dev->tagset);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002026 if (dev->ctrl.admin_q)
2027 blk_put_queue(dev->ctrl.admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002028 kfree(dev->queues);
2029 kfree(dev->entry);
2030 kfree(dev);
2031}
2032
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002033static void nvme_reset_work(struct work_struct *work)
Keith Buschf0b50732013-07-15 15:02:21 -06002034{
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002035 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002036 int result;
Keith Buschf0b50732013-07-15 15:02:21 -06002037
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002038 if (WARN_ON(test_bit(NVME_CTRL_RESETTING, &dev->flags)))
2039 goto out;
2040
2041 /*
2042 * If we're called to reset a live controller first shut it down before
2043 * moving on.
2044 */
2045 if (dev->bar)
2046 nvme_dev_shutdown(dev);
2047
2048 set_bit(NVME_CTRL_RESETTING, &dev->flags);
2049
Keith Buschf0b50732013-07-15 15:02:21 -06002050 result = nvme_dev_map(dev);
2051 if (result)
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002052 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002053
2054 result = nvme_configure_admin_queue(dev);
2055 if (result)
2056 goto unmap;
2057
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002058 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002059 result = nvme_alloc_admin_tags(dev);
2060 if (result)
2061 goto disable;
Dan McLeranb9afca32014-04-07 17:10:11 -06002062
Christoph Hellwigce4541f2015-10-16 07:58:46 +02002063 result = nvme_init_identify(&dev->ctrl);
2064 if (result)
2065 goto free_tags;
2066
Keith Buschf0b50732013-07-15 15:02:21 -06002067 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002068 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07002069 goto free_tags;
Keith Buschf0b50732013-07-15 15:02:21 -06002070
Christoph Hellwigadf68f22015-11-28 15:42:28 +01002071 dev->ctrl.event_limit = NVME_NR_AEN_COMMANDS;
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002072
Christoph Hellwig73850142015-10-22 14:03:33 +02002073 result = nvme_dev_list_add(dev);
2074 if (result)
2075 goto remove;
2076
Christoph Hellwig2659e572015-10-02 18:51:31 +02002077 /*
2078 * Keep the controller around but remove all namespaces if we don't have
2079 * any working I/O queue.
2080 */
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002081 if (dev->online_queues < 2) {
2082 dev_warn(dev->dev, "IO queues not created\n");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002083 nvme_remove_namespaces(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002084 } else {
2085 nvme_unfreeze_queues(dev);
2086 nvme_dev_add(dev);
2087 }
2088
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002089 clear_bit(NVME_CTRL_RESETTING, &dev->flags);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002090 return;
Keith Buschf0b50732013-07-15 15:02:21 -06002091
Christoph Hellwig73850142015-10-22 14:03:33 +02002092 remove:
2093 nvme_dev_list_remove(dev);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002094 free_tags:
2095 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002096 blk_put_queue(dev->ctrl.admin_q);
2097 dev->ctrl.admin_q = NULL;
Keith Busch4af0e212015-06-08 10:08:13 -06002098 dev->queues[0]->tags = NULL;
Keith Buschf0b50732013-07-15 15:02:21 -06002099 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002100 nvme_disable_queue(dev, 0);
Keith Buschf0b50732013-07-15 15:02:21 -06002101 unmap:
2102 nvme_dev_unmap(dev);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002103 out:
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002104 nvme_remove_dead_ctrl(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002105}
2106
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002107static void nvme_remove_dead_ctrl_work(struct work_struct *work)
Keith Busch9a6b9452013-12-10 13:10:36 -07002108{
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002109 struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002110 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002111
2112 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06002113 pci_stop_and_remove_bus_device_locked(pdev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002114 nvme_put_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002115}
2116
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002117static void nvme_remove_dead_ctrl(struct nvme_dev *dev)
Keith Buschde3eff22015-06-18 13:36:39 -06002118{
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002119 dev_warn(dev->dev, "Removing after probe failure\n");
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002120 kref_get(&dev->ctrl.kref);
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002121 if (!schedule_work(&dev->remove_work))
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002122 nvme_put_ctrl(&dev->ctrl);
Keith Buschde3eff22015-06-18 13:36:39 -06002123}
2124
Keith Busch4cc06522015-06-05 10:30:08 -06002125static int nvme_reset(struct nvme_dev *dev)
2126{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002127 if (!dev->ctrl.admin_q || blk_queue_dying(dev->ctrl.admin_q))
Keith Busch4cc06522015-06-05 10:30:08 -06002128 return -ENODEV;
2129
Christoph Hellwig846cc052015-11-26 12:10:29 +01002130 if (!queue_work(nvme_workq, &dev->reset_work))
2131 return -EBUSY;
Keith Busch4cc06522015-06-05 10:30:08 -06002132
Christoph Hellwig846cc052015-11-26 12:10:29 +01002133 flush_work(&dev->reset_work);
Christoph Hellwig846cc052015-11-26 12:10:29 +01002134 return 0;
Keith Busch4cc06522015-06-05 10:30:08 -06002135}
2136
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002137static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
2138{
2139 *val = readl(to_nvme_dev(ctrl)->bar + off);
2140 return 0;
2141}
2142
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002143static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
2144{
2145 writel(val, to_nvme_dev(ctrl)->bar + off);
2146 return 0;
2147}
2148
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002149static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
2150{
2151 *val = readq(to_nvme_dev(ctrl)->bar + off);
2152 return 0;
2153}
2154
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002155static bool nvme_pci_io_incapable(struct nvme_ctrl *ctrl)
2156{
2157 struct nvme_dev *dev = to_nvme_dev(ctrl);
2158
2159 return !dev->bar || dev->online_queues < 2;
2160}
2161
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002162static int nvme_pci_reset_ctrl(struct nvme_ctrl *ctrl)
2163{
2164 return nvme_reset(to_nvme_dev(ctrl));
2165}
2166
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002167static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
2168 .reg_read32 = nvme_pci_reg_read32,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002169 .reg_write32 = nvme_pci_reg_write32,
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002170 .reg_read64 = nvme_pci_reg_read64,
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002171 .io_incapable = nvme_pci_io_incapable,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002172 .reset_ctrl = nvme_pci_reset_ctrl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002173 .free_ctrl = nvme_pci_free_ctrl,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002174};
2175
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002176static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002177{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002178 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002179 struct nvme_dev *dev;
2180
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002181 node = dev_to_node(&pdev->dev);
2182 if (node == NUMA_NO_NODE)
2183 set_dev_node(&pdev->dev, 0);
2184
2185 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002186 if (!dev)
2187 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002188 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
2189 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002190 if (!dev->entry)
2191 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002192 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2193 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002194 if (!dev->queues)
2195 goto free;
2196
Christoph Hellwige75ec752015-05-22 11:12:39 +02002197 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002198 pci_set_drvdata(pdev, dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002199
Keith Busche6e96d72015-03-23 09:32:37 -06002200 INIT_LIST_HEAD(&dev->node);
Keith Buscha5768aa2015-06-01 14:28:14 -06002201 INIT_WORK(&dev->scan_work, nvme_dev_scan);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002202 INIT_WORK(&dev->reset_work, nvme_reset_work);
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002203 INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
Keith Busch77bf25e2015-11-26 12:21:29 +01002204 mutex_init(&dev->shutdown_lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002205
2206 result = nvme_setup_prp_pools(dev);
2207 if (result)
2208 goto put_pci;
2209
2210 result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
2211 id->driver_data);
2212 if (result)
2213 goto release_pools;
2214
Keith Busch92f7a162015-10-23 11:42:02 -06002215 queue_work(nvme_workq, &dev->reset_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002216 return 0;
2217
Keith Busch0877cb02013-07-15 15:02:19 -06002218 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05002219 nvme_release_prp_pools(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002220 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02002221 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002222 free:
2223 kfree(dev->queues);
2224 kfree(dev->entry);
2225 kfree(dev);
2226 return result;
2227}
2228
Keith Buschf0d54a52014-05-02 10:40:43 -06002229static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2230{
Keith Buscha6739472014-06-23 16:03:21 -06002231 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002232
Keith Buscha6739472014-06-23 16:03:21 -06002233 if (prepare)
2234 nvme_dev_shutdown(dev);
2235 else
Keith Busch92f7a162015-10-23 11:42:02 -06002236 queue_work(nvme_workq, &dev->reset_work);
Keith Buschf0d54a52014-05-02 10:40:43 -06002237}
2238
Keith Busch09ece142014-01-27 11:29:40 -05002239static void nvme_shutdown(struct pci_dev *pdev)
2240{
2241 struct nvme_dev *dev = pci_get_drvdata(pdev);
2242 nvme_dev_shutdown(dev);
2243}
2244
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002245static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002246{
2247 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002248
2249 spin_lock(&dev_list_lock);
2250 list_del_init(&dev->node);
2251 spin_unlock(&dev_list_lock);
2252
2253 pci_set_drvdata(pdev, NULL);
2254 flush_work(&dev->reset_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06002255 flush_work(&dev->scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002256 nvme_remove_namespaces(&dev->ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002257 nvme_uninit_ctrl(&dev->ctrl);
Keith Busch3399a3f2015-06-18 13:36:40 -06002258 nvme_dev_shutdown(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002259 nvme_dev_remove_admin(dev);
2260 nvme_free_queues(dev, 0);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002261 nvme_release_cmb(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002262 nvme_release_prp_pools(dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002263 nvme_put_ctrl(&dev->ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002264}
2265
2266/* These functions are yet to be implemented */
2267#define nvme_error_detected NULL
2268#define nvme_dump_registers NULL
2269#define nvme_link_reset NULL
2270#define nvme_slot_reset NULL
2271#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002272
Jingoo Han671a6012014-02-13 11:19:14 +09002273#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002274static int nvme_suspend(struct device *dev)
2275{
2276 struct pci_dev *pdev = to_pci_dev(dev);
2277 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2278
2279 nvme_dev_shutdown(ndev);
2280 return 0;
2281}
2282
2283static int nvme_resume(struct device *dev)
2284{
2285 struct pci_dev *pdev = to_pci_dev(dev);
2286 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002287
Keith Busch92f7a162015-10-23 11:42:02 -06002288 queue_work(nvme_workq, &ndev->reset_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07002289 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002290}
Jingoo Han671a6012014-02-13 11:19:14 +09002291#endif
Keith Buschcd638942013-07-15 15:02:23 -06002292
2293static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002294
Stephen Hemminger1d352032012-09-07 09:33:17 -07002295static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002296 .error_detected = nvme_error_detected,
2297 .mmio_enabled = nvme_dump_registers,
2298 .link_reset = nvme_link_reset,
2299 .slot_reset = nvme_slot_reset,
2300 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06002301 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002302};
2303
2304/* Move to pci_ids.h later */
2305#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2306
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002307static const struct pci_device_id nvme_id_table[] = {
Christoph Hellwig106198e2015-11-26 10:07:41 +01002308 { PCI_VDEVICE(INTEL, 0x0953),
2309 .driver_data = NVME_QUIRK_STRIPE_SIZE, },
Keith Busch540c8012015-10-22 15:45:06 -06002310 { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
2311 .driver_data = NVME_QUIRK_IDENTIFY_CNS, },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002312 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
Stephan Güntherc74dc782015-11-04 00:49:45 +01002313 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002314 { 0, }
2315};
2316MODULE_DEVICE_TABLE(pci, nvme_id_table);
2317
2318static struct pci_driver nvme_driver = {
2319 .name = "nvme",
2320 .id_table = nvme_id_table,
2321 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002322 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002323 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002324 .driver = {
2325 .pm = &nvme_dev_pm_ops,
2326 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002327 .err_handler = &nvme_err_handler,
2328};
2329
2330static int __init nvme_init(void)
2331{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002332 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002333
Dan McLeranb9afca32014-04-07 17:10:11 -06002334 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002335
Keith Busch92f7a162015-10-23 11:42:02 -06002336 nvme_workq = alloc_workqueue("nvme", WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
Keith Busch9a6b9452013-12-10 13:10:36 -07002337 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06002338 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07002339
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002340 result = nvme_core_init();
Keith Busch5c42ea12012-07-25 16:05:18 -06002341 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002342 goto kill_workq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002343
Keith Buschf3db22f2014-06-11 11:51:35 -06002344 result = pci_register_driver(&nvme_driver);
2345 if (result)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002346 goto core_exit;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002347 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002348
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002349 core_exit:
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002350 nvme_core_exit();
Keith Busch9a6b9452013-12-10 13:10:36 -07002351 kill_workq:
2352 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002353 return result;
2354}
2355
2356static void __exit nvme_exit(void)
2357{
2358 pci_unregister_driver(&nvme_driver);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002359 nvme_core_exit();
Keith Busch9a6b9452013-12-10 13:10:36 -07002360 destroy_workqueue(nvme_workq);
Dan McLeranb9afca32014-04-07 17:10:11 -06002361 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002362 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002363}
2364
2365MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2366MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07002367MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002368module_init(nvme_init);
2369module_exit(nvme_exit);