blob: 8a564f4ecf9913643a0ff89195237afba82a43e7 [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003 * Copyright (c) 2011-2014, Intel Corporation.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050013 */
14
Matthew Wilcox8de05532011-05-12 13:50:28 -040015#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050016#include <linux/blkdev.h>
Matias Bjørlinga4aea562014-11-04 08:20:14 -070017#include <linux/blk-mq.h>
Keith Busch42f61422014-03-24 10:46:25 -060018#include <linux/cpu.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040019#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050020#include <linux/errno.h>
21#include <linux/fs.h>
22#include <linux/genhd.h>
Keith Busch4cc09e22014-04-02 15:45:37 -060023#include <linux/hdreg.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040024#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050025#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/io.h>
28#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050029#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050030#include <linux/kernel.h>
Keith Buscha5768aa2015-06-01 14:28:14 -060031#include <linux/list_sort.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050032#include <linux/mm.h>
33#include <linux/module.h>
34#include <linux/moduleparam.h>
35#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050036#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040037#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050038#include <linux/sched.h>
39#include <linux/slab.h>
Keith Busche1e5e562015-02-19 13:39:03 -070040#include <linux/t10-pi.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050041#include <linux/types.h>
Keith Busch1d277a62015-10-15 14:10:52 +020042#include <linux/pr.h>
Vishal Verma5d0f6132013-03-04 18:40:58 -070043#include <scsi/sg.h>
Linus Torvalds9cf5c092015-11-06 14:22:15 -080044#include <linux/io-64-nonatomic-lo-hi.h>
Keith Busch1d277a62015-10-15 14:10:52 +020045#include <asm/unaligned.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090046
Christoph Hellwig9d99a8d2015-10-02 15:25:49 +020047#include <uapi/linux/nvme_ioctl.h>
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +020048#include "nvme.h"
49
Keith Buschb3fffde2015-02-03 11:21:42 -070050#define NVME_MINORS (1U << MINORBITS)
Keith Busch9d43cf62014-05-13 11:42:02 -060051#define NVME_Q_DEPTH 1024
Jens Axboed31af0a2015-03-06 12:56:13 -070052#define NVME_AQ_DEPTH 256
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050053#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
54#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Dan McLeran2484f402014-07-01 09:33:32 -060055#define SHUTDOWN_TIMEOUT (shutdown_timeout * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050056
Christoph Hellwig21d34712015-11-26 09:08:36 +010057unsigned char admin_timeout = 60;
Keith Busch9d43cf62014-05-13 11:42:02 -060058module_param(admin_timeout, byte, 0644);
59MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050060
Matthew Wilcoxbd676082014-06-03 23:04:30 -040061unsigned char nvme_io_timeout = 30;
62module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
Keith Buschb3550842014-04-04 11:43:36 -060063MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050064
Dan McLeran2484f402014-07-01 09:33:32 -060065static unsigned char shutdown_timeout = 5;
66module_param(shutdown_timeout, byte, 0644);
67MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
68
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050069static int nvme_major;
70module_param(nvme_major, int, 0);
71
Keith Buschb3fffde2015-02-03 11:21:42 -070072static int nvme_char_major;
73module_param(nvme_char_major, int, 0);
74
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050075static int use_threaded_interrupts;
76module_param(use_threaded_interrupts, int, 0);
77
Jon Derrick8ffaadf2015-07-20 10:14:09 -060078static bool use_cmb_sqes = true;
79module_param(use_cmb_sqes, bool, 0644);
80MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
81
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050082static DEFINE_SPINLOCK(dev_list_lock);
83static LIST_HEAD(dev_list);
84static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070085static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060086static wait_queue_head_t nvme_kthread_wait;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050087
Keith Buschb3fffde2015-02-03 11:21:42 -070088static struct class *nvme_class;
89
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010090struct nvme_dev;
91struct nvme_queue;
92
Christoph Hellwig906678922015-10-02 18:49:23 +020093static int __nvme_reset(struct nvme_dev *dev);
Keith Busch4cc06522015-06-05 10:30:08 -060094static int nvme_reset(struct nvme_dev *dev);
Jens Axboea0fa9642015-11-03 20:37:26 -070095static void nvme_process_cq(struct nvme_queue *nvmeq);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +020096static void nvme_dead_ctrl(struct nvme_dev *dev);
Keith Buschd4b4ff82013-12-10 13:10:37 -070097
Keith Busch4d115422013-12-10 13:10:40 -070098struct async_cmd_info {
99 struct kthread_work work;
100 struct kthread_worker *worker;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700101 struct request *req;
Keith Busch4d115422013-12-10 13:10:40 -0700102 u32 result;
103 int status;
104 void *ctx;
105};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500106
107/*
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100108 * Represents an NVM Express device. Each nvme_dev is a PCI function.
109 */
110struct nvme_dev {
111 struct list_head node;
112 struct nvme_queue **queues;
113 struct blk_mq_tag_set tagset;
114 struct blk_mq_tag_set admin_tagset;
115 u32 __iomem *dbs;
116 struct device *dev;
117 struct dma_pool *prp_page_pool;
118 struct dma_pool *prp_small_pool;
119 unsigned queue_count;
120 unsigned online_queues;
121 unsigned max_qid;
122 int q_depth;
123 u32 db_stride;
124 u32 ctrl_config;
125 struct msix_entry *entry;
126 void __iomem *bar;
127 struct list_head namespaces;
128 struct kref kref;
129 struct device *device;
130 struct work_struct reset_work;
131 struct work_struct probe_work;
132 struct work_struct scan_work;
133 bool subsystem;
134 u32 max_hw_sectors;
135 u32 stripe_size;
136 u32 page_size;
137 void __iomem *cmb;
138 dma_addr_t cmb_dma_addr;
139 u64 cmb_size;
140 u32 cmbsz;
141
142 struct nvme_ctrl ctrl;
143};
144
145static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
146{
147 return container_of(ctrl, struct nvme_dev, ctrl);
148}
149
150/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500151 * An NVM Express queue. Each device has at least two (one for admin
152 * commands and one for I/O commands).
153 */
154struct nvme_queue {
155 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500156 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500157 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500158 spinlock_t q_lock;
159 struct nvme_command *sq_cmds;
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600160 struct nvme_command __iomem *sq_cmds_io;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500161 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600162 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500163 dma_addr_t sq_dma_addr;
164 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500165 u32 __iomem *q_db;
166 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700167 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500168 u16 sq_head;
169 u16 sq_tail;
170 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700171 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400172 u8 cq_phase;
173 u8 cqe_seen;
Keith Busch4d115422013-12-10 13:10:40 -0700174 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500175};
176
177/*
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200178 * The nvme_iod describes the data in an I/O, including the list of PRP
179 * entries. You can't see it in this data structure because C doesn't let
180 * me express that. Use nvme_alloc_iod to ensure there's enough space
181 * allocated to store the PRP list.
182 */
183struct nvme_iod {
184 unsigned long private; /* For the use of the submitter of the I/O */
185 int npages; /* In the PRP list. 0 means small pool in use */
186 int offset; /* Of PRP list */
187 int nents; /* Used in scatterlist */
188 int length; /* Of data, in bytes */
189 dma_addr_t first_dma;
190 struct scatterlist meta_sg[1]; /* metadata requires single contiguous buffer */
191 struct scatterlist sg[0];
192};
193
194/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500195 * Check we didin't inadvertently grow the command struct
196 */
197static inline void _nvme_check_size(void)
198{
199 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
200 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
201 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
202 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
203 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400204 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700205 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500206 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
207 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
208 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
209 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600210 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500211}
212
Keith Buschedd10d32014-04-03 16:45:23 -0600213typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400214 struct nvme_completion *);
215
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500216struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400217 nvme_completion_fn fn;
218 void *ctx;
Keith Buschc30341d2013-12-10 13:10:38 -0700219 int aborted;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700220 struct nvme_queue *nvmeq;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700221 struct nvme_iod iod[0];
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500222};
223
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700224/*
225 * Max size of iod being embedded in the request payload
226 */
227#define NVME_INT_PAGES 2
228#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->page_size)
Chong Yuanfda631f2015-03-27 09:21:32 +0800229#define NVME_INT_MASK 0x01
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700230
231/*
232 * Will slightly overestimate the number of pages needed. This is OK
233 * as it only leads to a small amount of wasted memory for the lifetime of
234 * the I/O.
235 */
236static int nvme_npages(unsigned size, struct nvme_dev *dev)
237{
238 unsigned nprps = DIV_ROUND_UP(size + dev->page_size, dev->page_size);
239 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
240}
241
242static unsigned int nvme_cmd_size(struct nvme_dev *dev)
243{
244 unsigned int ret = sizeof(struct nvme_cmd_info);
245
246 ret += sizeof(struct nvme_iod);
247 ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
248 ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
249
250 return ret;
251}
252
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700253static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
254 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500255{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700256 struct nvme_dev *dev = data;
257 struct nvme_queue *nvmeq = dev->queues[0];
258
Keith Busch42483222015-06-01 09:29:54 -0600259 WARN_ON(hctx_idx != 0);
260 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
261 WARN_ON(nvmeq->tags);
262
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700263 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600264 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700265 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500266}
267
Keith Busch4af0e212015-06-08 10:08:13 -0600268static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
269{
270 struct nvme_queue *nvmeq = hctx->driver_data;
271
272 nvmeq->tags = NULL;
273}
274
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700275static int nvme_admin_init_request(void *data, struct request *req,
276 unsigned int hctx_idx, unsigned int rq_idx,
277 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600278{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700279 struct nvme_dev *dev = data;
280 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
281 struct nvme_queue *nvmeq = dev->queues[0];
282
283 BUG_ON(!nvmeq);
284 cmd->nvmeq = nvmeq;
285 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600286}
287
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700288static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
289 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500290{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700291 struct nvme_dev *dev = data;
Keith Busch42483222015-06-01 09:29:54 -0600292 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500293
Keith Busch42483222015-06-01 09:29:54 -0600294 if (!nvmeq->tags)
295 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500296
Keith Busch42483222015-06-01 09:29:54 -0600297 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700298 hctx->driver_data = nvmeq;
299 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500300}
301
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700302static int nvme_init_request(void *data, struct request *req,
303 unsigned int hctx_idx, unsigned int rq_idx,
304 unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500305{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700306 struct nvme_dev *dev = data;
307 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
308 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
309
310 BUG_ON(!nvmeq);
311 cmd->nvmeq = nvmeq;
312 return 0;
313}
314
315static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
316 nvme_completion_fn handler)
317{
318 cmd->fn = handler;
319 cmd->ctx = ctx;
320 cmd->aborted = 0;
Keith Buschc917dfe2015-01-07 18:55:48 -0700321 blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500322}
323
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700324static void *iod_get_private(struct nvme_iod *iod)
325{
326 return (void *) (iod->private & ~0x1UL);
327}
328
329/*
330 * If bit 0 is set, the iod is embedded in the request payload.
331 */
332static bool iod_should_kfree(struct nvme_iod *iod)
333{
Chong Yuanfda631f2015-03-27 09:21:32 +0800334 return (iod->private & NVME_INT_MASK) == 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700335}
336
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400337/* Special values must be less than 0x1000 */
338#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500339#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
340#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
341#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500342
Keith Buschedd10d32014-04-03 16:45:23 -0600343static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400344 struct nvme_completion *cqe)
345{
346 if (ctx == CMD_CTX_CANCELLED)
347 return;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400348 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600349 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400350 "completed id %d twice on queue %d\n",
351 cqe->command_id, le16_to_cpup(&cqe->sq_id));
352 return;
353 }
354 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600355 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400356 "invalid id %d completed on queue %d\n",
357 cqe->command_id, le16_to_cpup(&cqe->sq_id));
358 return;
359 }
Keith Buschedd10d32014-04-03 16:45:23 -0600360 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400361}
362
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700363static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
364{
365 void *ctx;
366
367 if (fn)
368 *fn = cmd->fn;
369 ctx = cmd->ctx;
370 cmd->fn = special_completion;
371 cmd->ctx = CMD_CTX_CANCELLED;
372 return ctx;
373}
374
375static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
376 struct nvme_completion *cqe)
377{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700378 u32 result = le32_to_cpup(&cqe->result);
379 u16 status = le16_to_cpup(&cqe->status) >> 1;
380
381 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100382 ++nvmeq->dev->ctrl.event_limit;
Keith Buscha5768aa2015-06-01 14:28:14 -0600383 if (status != NVME_SC_SUCCESS)
384 return;
385
386 switch (result & 0xff07) {
387 case NVME_AER_NOTICE_NS_CHANGED:
388 dev_info(nvmeq->q_dmadev, "rescanning\n");
389 schedule_work(&nvmeq->dev->scan_work);
390 default:
391 dev_warn(nvmeq->q_dmadev, "async event result %08x\n", result);
392 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700393}
394
395static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
396 struct nvme_completion *cqe)
397{
398 struct request *req = ctx;
399
400 u16 status = le16_to_cpup(&cqe->status) >> 1;
401 u32 result = le32_to_cpup(&cqe->result);
402
Keith Busch42483222015-06-01 09:29:54 -0600403 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700404
405 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100406 ++nvmeq->dev->ctrl.abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700407}
408
Keith Buschedd10d32014-04-03 16:45:23 -0600409static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700410 struct nvme_completion *cqe)
411{
412 struct async_cmd_info *cmdinfo = ctx;
413 cmdinfo->result = le32_to_cpup(&cqe->result);
414 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
415 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
Keith Busch42483222015-06-01 09:29:54 -0600416 blk_mq_free_request(cmdinfo->req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700417}
418
419static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
420 unsigned int tag)
421{
Keith Busch42483222015-06-01 09:29:54 -0600422 struct request *req = blk_mq_tag_to_rq(*nvmeq->tags, tag);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700423
424 return blk_mq_rq_to_pdu(req);
Keith Busch4d115422013-12-10 13:10:40 -0700425}
426
Matthew Wilcox184d2942011-05-11 21:36:38 -0400427/*
428 * Called with local interrupts disabled and the q_lock held. May not sleep.
429 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700430static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400431 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500432{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700433 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400434 void *ctx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700435 if (tag >= nvmeq->q_depth) {
436 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500437 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400438 }
Keith Busch859361a2012-08-02 14:05:59 -0600439 if (fn)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700440 *fn = cmd->fn;
441 ctx = cmd->ctx;
442 cmd->fn = special_completion;
443 cmd->ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400444 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500445}
446
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500447/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400448 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500449 * @nvmeq: The queue to use
450 * @cmd: The command to send
451 *
452 * Safe to use from interrupt context
453 */
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530454static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
455 struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500456{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700457 u16 tail = nvmeq->sq_tail;
458
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600459 if (nvmeq->sq_cmds_io)
460 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
461 else
462 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
463
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500464 if (++tail == nvmeq->q_depth)
465 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500466 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500467 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500468}
469
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530470static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700471{
472 unsigned long flags;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700473 spin_lock_irqsave(&nvmeq->q_lock, flags);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530474 __nvme_submit_cmd(nvmeq, cmd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700475 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700476}
477
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500478static __le64 **iod_list(struct nvme_iod *iod)
479{
480 return ((void *)iod) + iod->offset;
481}
482
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700483static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
484 unsigned nseg, unsigned long private)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500485{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700486 iod->private = private;
487 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
488 iod->npages = -1;
489 iod->length = nbytes;
490 iod->nents = 0;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500491}
492
493static struct nvme_iod *
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700494__nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
495 unsigned long priv, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500496{
497 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700498 sizeof(__le64 *) * nvme_npages(bytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500499 sizeof(struct scatterlist) * nseg, gfp);
500
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700501 if (iod)
502 iod_init(iod, bytes, nseg, priv);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500503
504 return iod;
505}
506
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700507static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
508 gfp_t gfp)
509{
510 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
511 sizeof(struct nvme_dsm_range);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700512 struct nvme_iod *iod;
513
514 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
515 size <= NVME_INT_BYTES(dev)) {
516 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
517
518 iod = cmd->iod;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700519 iod_init(iod, size, rq->nr_phys_segments,
Chong Yuanfda631f2015-03-27 09:21:32 +0800520 (unsigned long) rq | NVME_INT_MASK);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700521 return iod;
522 }
523
524 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
525 (unsigned long) rq, gfp);
526}
527
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200528static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500529{
Keith Busch1d090622014-06-23 11:34:01 -0600530 const int last_prp = dev->page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500531 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500532 __le64 **list = iod_list(iod);
533 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500534
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500535 if (iod->npages == 0)
536 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
537 for (i = 0; i < iod->npages; i++) {
538 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500539 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500540 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500541 prp_dma = next_prp_dma;
542 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700543
544 if (iod_should_kfree(iod))
545 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500546}
547
Keith Buschb4ff9c82014-08-29 09:06:12 -0600548static int nvme_error_status(u16 status)
549{
550 switch (status & 0x7ff) {
551 case NVME_SC_SUCCESS:
552 return 0;
553 case NVME_SC_CAP_EXCEEDED:
554 return -ENOSPC;
555 default:
556 return -EIO;
557 }
558}
559
Keith Busch52b68d72015-02-23 09:16:21 -0700560#ifdef CONFIG_BLK_DEV_INTEGRITY
Keith Busche1e5e562015-02-19 13:39:03 -0700561static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
562{
563 if (be32_to_cpu(pi->ref_tag) == v)
564 pi->ref_tag = cpu_to_be32(p);
565}
566
567static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
568{
569 if (be32_to_cpu(pi->ref_tag) == p)
570 pi->ref_tag = cpu_to_be32(v);
571}
572
573/**
574 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
575 *
576 * The virtual start sector is the one that was originally submitted by the
577 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
578 * start sector may be different. Remap protection information to match the
579 * physical LBA on writes, and back to the original seed on reads.
580 *
581 * Type 0 and 3 do not have a ref tag, so no remapping required.
582 */
583static void nvme_dif_remap(struct request *req,
584 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
585{
586 struct nvme_ns *ns = req->rq_disk->private_data;
587 struct bio_integrity_payload *bip;
588 struct t10_pi_tuple *pi;
589 void *p, *pmap;
590 u32 i, nlb, ts, phys, virt;
591
592 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
593 return;
594
595 bip = bio_integrity(req->bio);
596 if (!bip)
597 return;
598
599 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
Keith Busche1e5e562015-02-19 13:39:03 -0700600
601 p = pmap;
602 virt = bip_get_seed(bip);
603 phys = nvme_block_nr(ns, blk_rq_pos(req));
604 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
Dan Williamsac6fc482015-10-21 13:20:18 -0400605 ts = ns->disk->queue->integrity.tuple_size;
Keith Busche1e5e562015-02-19 13:39:03 -0700606
607 for (i = 0; i < nlb; i++, virt++, phys++) {
608 pi = (struct t10_pi_tuple *)p;
609 dif_swap(phys, virt, pi);
610 p += ts;
611 }
612 kunmap_atomic(pmap);
613}
614
Keith Busch52b68d72015-02-23 09:16:21 -0700615static void nvme_init_integrity(struct nvme_ns *ns)
616{
617 struct blk_integrity integrity;
618
619 switch (ns->pi_type) {
620 case NVME_NS_DPS_PI_TYPE3:
Martin K. Petersen0f8087e2015-10-21 13:19:33 -0400621 integrity.profile = &t10_pi_type3_crc;
Keith Busch52b68d72015-02-23 09:16:21 -0700622 break;
623 case NVME_NS_DPS_PI_TYPE1:
624 case NVME_NS_DPS_PI_TYPE2:
Martin K. Petersen0f8087e2015-10-21 13:19:33 -0400625 integrity.profile = &t10_pi_type1_crc;
Keith Busch52b68d72015-02-23 09:16:21 -0700626 break;
627 default:
Dan Williams4125a092015-10-21 13:20:29 -0400628 integrity.profile = NULL;
Keith Busch52b68d72015-02-23 09:16:21 -0700629 break;
630 }
631 integrity.tuple_size = ns->ms;
632 blk_integrity_register(ns->disk, &integrity);
633 blk_queue_max_integrity_segments(ns->queue, 1);
634}
635#else /* CONFIG_BLK_DEV_INTEGRITY */
636static void nvme_dif_remap(struct request *req,
637 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
638{
639}
640static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
641{
642}
643static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
644{
645}
646static void nvme_init_integrity(struct nvme_ns *ns)
647{
648}
649#endif
650
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700651static void req_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500652 struct nvme_completion *cqe)
653{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500654 struct nvme_iod *iod = ctx;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700655 struct request *req = iod_get_private(iod);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700656 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500657 u16 status = le16_to_cpup(&cqe->status) >> 1;
Keith Busch0dfc70c2015-10-15 13:38:48 -0600658 bool requeue = false;
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200659 int error = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500660
Keith Buschedd10d32014-04-03 16:45:23 -0600661 if (unlikely(status)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700662 if (!(status & NVME_SC_DNR || blk_noretry_request(req))
663 && (jiffies - req->start_time) < req->timeout) {
Keith Buschc9d3bf82015-01-07 18:55:52 -0700664 unsigned long flags;
665
Keith Busch0dfc70c2015-10-15 13:38:48 -0600666 requeue = true;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700667 blk_mq_requeue_request(req);
Keith Buschc9d3bf82015-01-07 18:55:52 -0700668 spin_lock_irqsave(req->q->queue_lock, flags);
669 if (!blk_queue_stopped(req->q))
670 blk_mq_kick_requeue_list(req->q);
671 spin_unlock_irqrestore(req->q->queue_lock, flags);
Keith Busch0dfc70c2015-10-15 13:38:48 -0600672 goto release_iod;
Keith Buschedd10d32014-04-03 16:45:23 -0600673 }
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200674
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200675 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
Keith Busch17188bb2015-06-08 10:08:14 -0600676 if (cmd_rq->ctx == CMD_CTX_CANCELLED)
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200677 error = -EINTR;
678 else
679 error = status;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200680 } else {
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200681 error = nvme_error_status(status);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200682 }
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200683 }
684
Keith Buscha0a931d2015-05-22 12:28:31 -0600685 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
686 u32 result = le32_to_cpup(&cqe->result);
687 req->special = (void *)(uintptr_t)result;
688 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700689
690 if (cmd_rq->aborted)
Christoph Hellwige75ec752015-05-22 11:12:39 +0200691 dev_warn(nvmeq->dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700692 "completing aborted command with status:%04x\n",
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200693 error);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700694
Keith Busch0dfc70c2015-10-15 13:38:48 -0600695release_iod:
Keith Busche1e5e562015-02-19 13:39:03 -0700696 if (iod->nents) {
Christoph Hellwige75ec752015-05-22 11:12:39 +0200697 dma_unmap_sg(nvmeq->dev->dev, iod->sg, iod->nents,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700698 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busche1e5e562015-02-19 13:39:03 -0700699 if (blk_integrity_rq(req)) {
700 if (!rq_data_dir(req))
701 nvme_dif_remap(req, nvme_dif_complete);
Christoph Hellwige75ec752015-05-22 11:12:39 +0200702 dma_unmap_sg(nvmeq->dev->dev, iod->meta_sg, 1,
Keith Busche1e5e562015-02-19 13:39:03 -0700703 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
704 }
705 }
Keith Buschedd10d32014-04-03 16:45:23 -0600706 nvme_free_iod(nvmeq->dev, iod);
Keith Busch3291fa52014-04-28 12:30:52 -0600707
Keith Busch0dfc70c2015-10-15 13:38:48 -0600708 if (likely(!requeue))
709 blk_mq_complete_request(req, error);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500710}
711
Matthew Wilcox184d2942011-05-11 21:36:38 -0400712/* length is in bytes. gfp flags indicates whether we may sleep. */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200713static int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod,
714 int total_len, gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500715{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500716 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500717 int length = total_len;
718 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500719 int dma_len = sg_dma_len(sg);
720 u64 dma_addr = sg_dma_address(sg);
Murali Iyerf137e0f2015-03-26 11:07:51 -0500721 u32 page_size = dev->page_size;
722 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500723 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500724 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500725 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500726 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500727
Keith Busch1d090622014-06-23 11:34:01 -0600728 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500729 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500730 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500731
Keith Busch1d090622014-06-23 11:34:01 -0600732 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500733 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600734 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500735 } else {
736 sg = sg_next(sg);
737 dma_addr = sg_dma_address(sg);
738 dma_len = sg_dma_len(sg);
739 }
740
Keith Busch1d090622014-06-23 11:34:01 -0600741 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600742 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500743 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500744 }
745
Keith Busch1d090622014-06-23 11:34:01 -0600746 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500747 if (nprps <= (256 / 8)) {
748 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500749 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500750 } else {
751 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500752 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500753 }
754
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400755 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
756 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600757 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500758 iod->npages = -1;
Keith Busch1d090622014-06-23 11:34:01 -0600759 return (total_len - length) + page_size;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400760 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500761 list[0] = prp_list;
762 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500763 i = 0;
764 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600765 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500766 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400767 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500768 if (!prp_list)
769 return total_len - length;
770 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400771 prp_list[0] = old_prp_list[i - 1];
772 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
773 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500774 }
775 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600776 dma_len -= page_size;
777 dma_addr += page_size;
778 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500779 if (length <= 0)
780 break;
781 if (dma_len > 0)
782 continue;
783 BUG_ON(dma_len < 0);
784 sg = sg_next(sg);
785 dma_addr = sg_dma_address(sg);
786 dma_len = sg_dma_len(sg);
787 }
788
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500789 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500790}
791
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200792static void nvme_submit_priv(struct nvme_queue *nvmeq, struct request *req,
793 struct nvme_iod *iod)
794{
Jon Derrick498c4392015-07-20 10:14:08 -0600795 struct nvme_command cmnd;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200796
Jon Derrick498c4392015-07-20 10:14:08 -0600797 memcpy(&cmnd, req->cmd, sizeof(cmnd));
798 cmnd.rw.command_id = req->tag;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200799 if (req->nr_phys_segments) {
Jon Derrick498c4392015-07-20 10:14:08 -0600800 cmnd.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
801 cmnd.rw.prp2 = cpu_to_le64(iod->first_dma);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200802 }
803
Jon Derrick498c4392015-07-20 10:14:08 -0600804 __nvme_submit_cmd(nvmeq, &cmnd);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200805}
806
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700807/*
808 * We reuse the small pool to allocate the 16-byte range here as it is not
809 * worth having a special pool for these or additional cases to handle freeing
810 * the iod.
811 */
812static void nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
813 struct request *req, struct nvme_iod *iod)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700814{
Keith Buschedd10d32014-04-03 16:45:23 -0600815 struct nvme_dsm_range *range =
816 (struct nvme_dsm_range *)iod_list(iod)[0];
Jon Derrick498c4392015-07-20 10:14:08 -0600817 struct nvme_command cmnd;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700818
Keith Busch0e5e4f02012-11-09 16:33:05 -0700819 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700820 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
821 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700822
Jon Derrick498c4392015-07-20 10:14:08 -0600823 memset(&cmnd, 0, sizeof(cmnd));
824 cmnd.dsm.opcode = nvme_cmd_dsm;
825 cmnd.dsm.command_id = req->tag;
826 cmnd.dsm.nsid = cpu_to_le32(ns->ns_id);
827 cmnd.dsm.prp1 = cpu_to_le64(iod->first_dma);
828 cmnd.dsm.nr = 0;
829 cmnd.dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
Keith Busch0e5e4f02012-11-09 16:33:05 -0700830
Jon Derrick498c4392015-07-20 10:14:08 -0600831 __nvme_submit_cmd(nvmeq, &cmnd);
Keith Busch0e5e4f02012-11-09 16:33:05 -0700832}
833
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700834static void nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500835 int cmdid)
836{
Jon Derrick498c4392015-07-20 10:14:08 -0600837 struct nvme_command cmnd;
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500838
Jon Derrick498c4392015-07-20 10:14:08 -0600839 memset(&cmnd, 0, sizeof(cmnd));
840 cmnd.common.opcode = nvme_cmd_flush;
841 cmnd.common.command_id = cmdid;
842 cmnd.common.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500843
Jon Derrick498c4392015-07-20 10:14:08 -0600844 __nvme_submit_cmd(nvmeq, &cmnd);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500845}
846
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700847static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod,
848 struct nvme_ns *ns)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500849{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700850 struct request *req = iod_get_private(iod);
Jon Derrick498c4392015-07-20 10:14:08 -0600851 struct nvme_command cmnd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700852 u16 control = 0;
853 u32 dsmgmt = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500854
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700855 if (req->cmd_flags & REQ_FUA)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500856 control |= NVME_RW_FUA;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700857 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500858 control |= NVME_RW_LR;
859
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700860 if (req->cmd_flags & REQ_RAHEAD)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500861 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
862
Jon Derrick498c4392015-07-20 10:14:08 -0600863 memset(&cmnd, 0, sizeof(cmnd));
864 cmnd.rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
865 cmnd.rw.command_id = req->tag;
866 cmnd.rw.nsid = cpu_to_le32(ns->ns_id);
867 cmnd.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
868 cmnd.rw.prp2 = cpu_to_le64(iod->first_dma);
869 cmnd.rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
870 cmnd.rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500871
Alok Pandeye19b1272015-08-26 08:56:14 -0600872 if (ns->ms) {
Keith Busche1e5e562015-02-19 13:39:03 -0700873 switch (ns->pi_type) {
874 case NVME_NS_DPS_PI_TYPE3:
875 control |= NVME_RW_PRINFO_PRCHK_GUARD;
876 break;
877 case NVME_NS_DPS_PI_TYPE1:
878 case NVME_NS_DPS_PI_TYPE2:
879 control |= NVME_RW_PRINFO_PRCHK_GUARD |
880 NVME_RW_PRINFO_PRCHK_REF;
Jon Derrick498c4392015-07-20 10:14:08 -0600881 cmnd.rw.reftag = cpu_to_le32(
Keith Busche1e5e562015-02-19 13:39:03 -0700882 nvme_block_nr(ns, blk_rq_pos(req)));
883 break;
884 }
Alok Pandeye19b1272015-08-26 08:56:14 -0600885 if (blk_integrity_rq(req))
886 cmnd.rw.metadata =
887 cpu_to_le64(sg_dma_address(iod->meta_sg));
888 else
889 control |= NVME_RW_PRINFO_PRACT;
890 }
Keith Busche1e5e562015-02-19 13:39:03 -0700891
Jon Derrick498c4392015-07-20 10:14:08 -0600892 cmnd.rw.control = cpu_to_le16(control);
893 cmnd.rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500894
Jon Derrick498c4392015-07-20 10:14:08 -0600895 __nvme_submit_cmd(nvmeq, &cmnd);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500896
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500897 return 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600898}
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500899
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200900/*
901 * NOTE: ns is NULL when called on the admin queue.
902 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700903static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
904 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600905{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700906 struct nvme_ns *ns = hctx->queue->queuedata;
907 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200908 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700909 struct request *req = bd->rq;
910 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
Keith Buschedd10d32014-04-03 16:45:23 -0600911 struct nvme_iod *iod;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700912 enum dma_data_direction dma_dir;
Keith Buschedd10d32014-04-03 16:45:23 -0600913
Keith Busche1e5e562015-02-19 13:39:03 -0700914 /*
915 * If formated with metadata, require the block layer provide a buffer
916 * unless this namespace is formated such that the metadata can be
917 * stripped/generated by the controller with PRACT=1.
918 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200919 if (ns && ns->ms && !blk_integrity_rq(req)) {
Keith Busch71feb362015-06-19 11:07:30 -0600920 if (!(ns->pi_type && ns->ms == 8) &&
921 req->cmd_type != REQ_TYPE_DRV_PRIV) {
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200922 blk_mq_complete_request(req, -EFAULT);
Keith Busche1e5e562015-02-19 13:39:03 -0700923 return BLK_MQ_RQ_QUEUE_OK;
924 }
925 }
926
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200927 iod = nvme_alloc_iod(req, dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600928 if (!iod)
Jens Axboefe543032014-12-11 13:58:39 -0700929 return BLK_MQ_RQ_QUEUE_BUSY;
Keith Buschedd10d32014-04-03 16:45:23 -0600930
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700931 if (req->cmd_flags & REQ_DISCARD) {
Keith Buschedd10d32014-04-03 16:45:23 -0600932 void *range;
933 /*
934 * We reuse the small pool to allocate the 16-byte range here
935 * as it is not worth having a special pool for these or
936 * additional cases to handle freeing the iod.
937 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200938 range = dma_pool_alloc(dev->prp_small_pool, GFP_ATOMIC,
Keith Buschedd10d32014-04-03 16:45:23 -0600939 &iod->first_dma);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700940 if (!range)
Jens Axboefe543032014-12-11 13:58:39 -0700941 goto retry_cmd;
Keith Buschedd10d32014-04-03 16:45:23 -0600942 iod_list(iod)[0] = (__le64 *)range;
943 iod->npages = 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700944 } else if (req->nr_phys_segments) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700945 dma_dir = rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
Keith Buschedd10d32014-04-03 16:45:23 -0600946
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700947 sg_init_table(iod->sg, req->nr_phys_segments);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700948 iod->nents = blk_rq_map_sg(req->q, req, iod->sg);
Jens Axboefe543032014-12-11 13:58:39 -0700949 if (!iod->nents)
950 goto error_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700951
952 if (!dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir))
Jens Axboefe543032014-12-11 13:58:39 -0700953 goto retry_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700954
Jens Axboefe543032014-12-11 13:58:39 -0700955 if (blk_rq_bytes(req) !=
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200956 nvme_setup_prps(dev, iod, blk_rq_bytes(req), GFP_ATOMIC)) {
957 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
Jens Axboefe543032014-12-11 13:58:39 -0700958 goto retry_cmd;
959 }
Keith Busche1e5e562015-02-19 13:39:03 -0700960 if (blk_integrity_rq(req)) {
Christoph Hellwigbf508e92015-10-16 07:58:31 +0200961 if (blk_rq_count_integrity_sg(req->q, req->bio) != 1) {
962 dma_unmap_sg(dev->dev, iod->sg, iod->nents,
963 dma_dir);
Keith Busche1e5e562015-02-19 13:39:03 -0700964 goto error_cmd;
Christoph Hellwigbf508e92015-10-16 07:58:31 +0200965 }
Keith Busche1e5e562015-02-19 13:39:03 -0700966
967 sg_init_table(iod->meta_sg, 1);
968 if (blk_rq_map_integrity_sg(
Christoph Hellwigbf508e92015-10-16 07:58:31 +0200969 req->q, req->bio, iod->meta_sg) != 1) {
970 dma_unmap_sg(dev->dev, iod->sg, iod->nents,
971 dma_dir);
Keith Busche1e5e562015-02-19 13:39:03 -0700972 goto error_cmd;
Christoph Hellwigbf508e92015-10-16 07:58:31 +0200973 }
Keith Busche1e5e562015-02-19 13:39:03 -0700974
975 if (rq_data_dir(req))
976 nvme_dif_remap(req, nvme_dif_prep);
977
Christoph Hellwigbf508e92015-10-16 07:58:31 +0200978 if (!dma_map_sg(nvmeq->q_dmadev, iod->meta_sg, 1, dma_dir)) {
979 dma_unmap_sg(dev->dev, iod->sg, iod->nents,
980 dma_dir);
Keith Busche1e5e562015-02-19 13:39:03 -0700981 goto error_cmd;
Christoph Hellwigbf508e92015-10-16 07:58:31 +0200982 }
Keith Busche1e5e562015-02-19 13:39:03 -0700983 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700984 }
985
Keith Busch9af87852014-12-03 17:07:13 -0700986 nvme_set_info(cmd, iod, req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700987 spin_lock_irq(&nvmeq->q_lock);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200988 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
989 nvme_submit_priv(nvmeq, req, iod);
990 else if (req->cmd_flags & REQ_DISCARD)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700991 nvme_submit_discard(nvmeq, ns, req, iod);
992 else if (req->cmd_flags & REQ_FLUSH)
993 nvme_submit_flush(nvmeq, ns, req->tag);
994 else
995 nvme_submit_iod(nvmeq, iod, ns);
996
997 nvme_process_cq(nvmeq);
998 spin_unlock_irq(&nvmeq->q_lock);
999 return BLK_MQ_RQ_QUEUE_OK;
1000
Jens Axboefe543032014-12-11 13:58:39 -07001001 error_cmd:
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001002 nvme_free_iod(dev, iod);
Jens Axboefe543032014-12-11 13:58:39 -07001003 return BLK_MQ_RQ_QUEUE_ERROR;
1004 retry_cmd:
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001005 nvme_free_iod(dev, iod);
Jens Axboefe543032014-12-11 13:58:39 -07001006 return BLK_MQ_RQ_QUEUE_BUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001007}
1008
Jens Axboea0fa9642015-11-03 20:37:26 -07001009static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001010{
Matthew Wilcox82123462011-01-20 13:24:06 -05001011 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001012
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001013 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -05001014 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001015
1016 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -04001017 void *ctx;
1018 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001019 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -05001020 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001021 break;
1022 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
1023 if (++head == nvmeq->q_depth) {
1024 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001025 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001026 }
Jens Axboea0fa9642015-11-03 20:37:26 -07001027 if (tag && *tag == cqe.command_id)
1028 *tag = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001029 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -06001030 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001031 }
1032
1033 /* If the controller ignores the cq head doorbell and continuously
1034 * writes to the queue, it is theoretically possible to wrap around
1035 * the queue twice and mistakenly return IRQ_NONE. Linux only
1036 * requires that 0.1% of your interrupts are handled, so this isn't
1037 * a big problem.
1038 */
Matthew Wilcox82123462011-01-20 13:24:06 -05001039 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Jens Axboea0fa9642015-11-03 20:37:26 -07001040 return;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001041
Keith Busch604e8c82015-11-20 08:38:13 -07001042 if (likely(nvmeq->cq_vector >= 0))
1043 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001044 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -05001045 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001046
Matthew Wilcoxe9539f42013-06-24 11:47:34 -04001047 nvmeq->cqe_seen = 1;
Jens Axboea0fa9642015-11-03 20:37:26 -07001048}
1049
1050static void nvme_process_cq(struct nvme_queue *nvmeq)
1051{
1052 __nvme_process_cq(nvmeq, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001053}
1054
1055static irqreturn_t nvme_irq(int irq, void *data)
1056{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001057 irqreturn_t result;
1058 struct nvme_queue *nvmeq = data;
1059 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -04001060 nvme_process_cq(nvmeq);
1061 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
1062 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001063 spin_unlock(&nvmeq->q_lock);
1064 return result;
1065}
1066
1067static irqreturn_t nvme_irq_check(int irq, void *data)
1068{
1069 struct nvme_queue *nvmeq = data;
1070 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
1071 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
1072 return IRQ_NONE;
1073 return IRQ_WAKE_THREAD;
1074}
1075
Jens Axboea0fa9642015-11-03 20:37:26 -07001076static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1077{
1078 struct nvme_queue *nvmeq = hctx->driver_data;
1079
1080 if ((le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) ==
1081 nvmeq->cq_phase) {
1082 spin_lock_irq(&nvmeq->q_lock);
1083 __nvme_process_cq(nvmeq, &tag);
1084 spin_unlock_irq(&nvmeq->q_lock);
1085
1086 if (tag == -1)
1087 return 1;
1088 }
1089
1090 return 0;
1091}
1092
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001093static int nvme_submit_async_admin_req(struct nvme_dev *dev)
1094{
1095 struct nvme_queue *nvmeq = dev->queues[0];
1096 struct nvme_command c;
1097 struct nvme_cmd_info *cmd_info;
1098 struct request *req;
1099
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001100 req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE,
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001101 BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001102 if (IS_ERR(req))
1103 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001104
Keith Buschc917dfe2015-01-07 18:55:48 -07001105 req->cmd_flags |= REQ_NO_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001106 cmd_info = blk_mq_rq_to_pdu(req);
Keith Busch1efccc92015-03-31 10:37:17 -06001107 nvme_set_info(cmd_info, NULL, async_req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001108
1109 memset(&c, 0, sizeof(c));
1110 c.common.opcode = nvme_admin_async_event;
1111 c.common.command_id = req->tag;
1112
Keith Busch42483222015-06-01 09:29:54 -06001113 blk_mq_free_request(req);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301114 __nvme_submit_cmd(nvmeq, &c);
1115 return 0;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001116}
1117
1118static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
Keith Busch4d115422013-12-10 13:10:40 -07001119 struct nvme_command *cmd,
1120 struct async_cmd_info *cmdinfo, unsigned timeout)
1121{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001122 struct nvme_queue *nvmeq = dev->queues[0];
1123 struct request *req;
1124 struct nvme_cmd_info *cmd_rq;
Keith Busch4d115422013-12-10 13:10:40 -07001125
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001126 req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE, 0);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001127 if (IS_ERR(req))
1128 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001129
1130 req->timeout = timeout;
1131 cmd_rq = blk_mq_rq_to_pdu(req);
1132 cmdinfo->req = req;
1133 nvme_set_info(cmd_rq, cmdinfo, async_completion);
Keith Busch4d115422013-12-10 13:10:40 -07001134 cmdinfo->status = -EINTR;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001135
1136 cmd->common.command_id = req->tag;
1137
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301138 nvme_submit_cmd(nvmeq, cmd);
1139 return 0;
Keith Busch4d115422013-12-10 13:10:40 -07001140}
1141
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001142static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1143{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001144 struct nvme_command c;
1145
1146 memset(&c, 0, sizeof(c));
1147 c.delete_queue.opcode = opcode;
1148 c.delete_queue.qid = cpu_to_le16(id);
1149
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001150 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001151}
1152
1153static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1154 struct nvme_queue *nvmeq)
1155{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001156 struct nvme_command c;
1157 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1158
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001159 /*
1160 * Note: we (ab)use the fact the the prp fields survive if no data
1161 * is attached to the request.
1162 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001163 memset(&c, 0, sizeof(c));
1164 c.create_cq.opcode = nvme_admin_create_cq;
1165 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1166 c.create_cq.cqid = cpu_to_le16(qid);
1167 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1168 c.create_cq.cq_flags = cpu_to_le16(flags);
1169 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1170
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001171 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001172}
1173
1174static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1175 struct nvme_queue *nvmeq)
1176{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001177 struct nvme_command c;
1178 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1179
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001180 /*
1181 * Note: we (ab)use the fact the the prp fields survive if no data
1182 * is attached to the request.
1183 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001184 memset(&c, 0, sizeof(c));
1185 c.create_sq.opcode = nvme_admin_create_sq;
1186 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1187 c.create_sq.sqid = cpu_to_le16(qid);
1188 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1189 c.create_sq.sq_flags = cpu_to_le16(flags);
1190 c.create_sq.cqid = cpu_to_le16(qid);
1191
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001192 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001193}
1194
1195static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1196{
1197 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1198}
1199
1200static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1201{
1202 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1203}
1204
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001205/**
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001206 * nvme_abort_req - Attempt aborting a request
Keith Buschc30341d2013-12-10 13:10:38 -07001207 *
1208 * Schedule controller reset if the command was already aborted once before and
1209 * still hasn't been returned to the driver, or if this is the admin queue.
1210 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001211static void nvme_abort_req(struct request *req)
Keith Buschc30341d2013-12-10 13:10:38 -07001212{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001213 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1214 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001215 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001216 struct request *abort_req;
1217 struct nvme_cmd_info *abort_cmd;
1218 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -07001219
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001220 if (!nvmeq->qid || cmd_rq->aborted) {
Christoph Hellwig906678922015-10-02 18:49:23 +02001221 spin_lock(&dev_list_lock);
1222 if (!__nvme_reset(dev)) {
1223 dev_warn(dev->dev,
1224 "I/O %d QID %d timeout, reset controller\n",
1225 req->tag, nvmeq->qid);
1226 }
1227 spin_unlock(&dev_list_lock);
Keith Buschc30341d2013-12-10 13:10:38 -07001228 return;
1229 }
1230
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001231 if (!dev->ctrl.abort_limit)
Keith Buschc30341d2013-12-10 13:10:38 -07001232 return;
1233
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001234 abort_req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE,
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001235 BLK_MQ_REQ_NOWAIT);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001236 if (IS_ERR(abort_req))
Keith Buschc30341d2013-12-10 13:10:38 -07001237 return;
1238
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001239 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1240 nvme_set_info(abort_cmd, abort_req, abort_completion);
1241
Keith Buschc30341d2013-12-10 13:10:38 -07001242 memset(&cmd, 0, sizeof(cmd));
1243 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001244 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001245 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001246 cmd.abort.command_id = abort_req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001247
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001248 --dev->ctrl.abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001249 cmd_rq->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -07001250
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001251 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", req->tag,
Keith Buschc30341d2013-12-10 13:10:38 -07001252 nvmeq->qid);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301253 nvme_submit_cmd(dev->queues[0], &cmd);
Keith Buschc30341d2013-12-10 13:10:38 -07001254}
1255
Keith Busch42483222015-06-01 09:29:54 -06001256static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001257{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001258 struct nvme_queue *nvmeq = data;
1259 void *ctx;
1260 nvme_completion_fn fn;
1261 struct nvme_cmd_info *cmd;
Keith Buschcef6a942015-01-07 18:55:51 -07001262 struct nvme_completion cqe;
1263
1264 if (!blk_mq_request_started(req))
1265 return;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001266
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001267 cmd = blk_mq_rq_to_pdu(req);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001268
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001269 if (cmd->ctx == CMD_CTX_CANCELLED)
1270 return;
1271
Keith Buschcef6a942015-01-07 18:55:51 -07001272 if (blk_queue_dying(req->q))
1273 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1274 else
1275 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1276
1277
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001278 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1279 req->tag, nvmeq->qid);
1280 ctx = cancel_cmd_info(cmd, &fn);
1281 fn(nvmeq, ctx, &cqe);
1282}
1283
1284static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
1285{
1286 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
1287 struct nvme_queue *nvmeq = cmd->nvmeq;
1288
Keith Busch07836e62015-02-19 10:34:48 -07001289 dev_warn(nvmeq->q_dmadev, "Timeout I/O %d QID %d\n", req->tag,
1290 nvmeq->qid);
1291 spin_lock_irq(&nvmeq->q_lock);
1292 nvme_abort_req(req);
1293 spin_unlock_irq(&nvmeq->q_lock);
1294
Keith Busch7a509a62015-01-07 18:55:53 -07001295 /*
1296 * The aborted req will be completed on receiving the abort req.
1297 * We enable the timer again. If hit twice, it'll cause a device reset,
1298 * as the device then is in a faulty state.
1299 */
Keith Busch07836e62015-02-19 10:34:48 -07001300 return BLK_EH_RESET_TIMER;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001301}
1302
Keith Buschf435c282014-07-07 09:14:42 -06001303static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001304{
1305 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1306 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001307 if (nvmeq->sq_cmds)
1308 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
Matthew Wilcox9e866772012-08-03 13:55:56 -04001309 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1310 kfree(nvmeq);
1311}
1312
Keith Buscha1a5ef92013-12-16 13:50:00 -05001313static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001314{
1315 int i;
1316
Keith Buscha1a5ef92013-12-16 13:50:00 -05001317 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001318 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001319 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001320 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001321 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001322 }
Keith Busch22404272013-07-15 15:02:20 -06001323}
1324
Keith Busch4d115422013-12-10 13:10:40 -07001325/**
1326 * nvme_suspend_queue - put queue into suspended state
1327 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001328 */
1329static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001330{
Keith Busch2b25d982014-12-22 12:59:04 -07001331 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001332
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001333 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001334 if (nvmeq->cq_vector == -1) {
1335 spin_unlock_irq(&nvmeq->q_lock);
1336 return 1;
1337 }
1338 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Keith Busch42f61422014-03-24 10:46:25 -06001339 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001340 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001341 spin_unlock_irq(&nvmeq->q_lock);
1342
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001343 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
1344 blk_mq_freeze_queue_start(nvmeq->dev->ctrl.admin_q);
Keith Busch6df3dbc2015-03-26 13:49:33 -06001345
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001346 irq_set_affinity_hint(vector, NULL);
1347 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001348
Keith Busch4d115422013-12-10 13:10:40 -07001349 return 0;
1350}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001351
Keith Busch4d115422013-12-10 13:10:40 -07001352static void nvme_clear_queue(struct nvme_queue *nvmeq)
1353{
Keith Busch22404272013-07-15 15:02:20 -06001354 spin_lock_irq(&nvmeq->q_lock);
Keith Busch42483222015-06-01 09:29:54 -06001355 if (nvmeq->tags && *nvmeq->tags)
1356 blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001357 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001358}
1359
Keith Busch4d115422013-12-10 13:10:40 -07001360static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1361{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001362 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001363
1364 if (!nvmeq)
1365 return;
1366 if (nvme_suspend_queue(nvmeq))
1367 return;
1368
Keith Busch0e53d182013-12-10 13:10:39 -07001369 /* Don't tell the adapter to delete the admin queue.
1370 * Don't tell a removed adapter to delete IO queues. */
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001371 if (qid && readl(dev->bar + NVME_REG_CSTS) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001372 adapter_delete_sq(dev, qid);
1373 adapter_delete_cq(dev, qid);
1374 }
Keith Busch07836e62015-02-19 10:34:48 -07001375
1376 spin_lock_irq(&nvmeq->q_lock);
1377 nvme_process_cq(nvmeq);
1378 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001379}
1380
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001381static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1382 int entry_size)
1383{
1384 int q_depth = dev->q_depth;
1385 unsigned q_size_aligned = roundup(q_depth * entry_size, dev->page_size);
1386
1387 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001388 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
1389 mem_per_q = round_down(mem_per_q, dev->page_size);
1390 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001391
1392 /*
1393 * Ensure the reduced q_depth is above some threshold where it
1394 * would be better to map queues in system memory with the
1395 * original depth
1396 */
1397 if (q_depth < 64)
1398 return -ENOMEM;
1399 }
1400
1401 return q_depth;
1402}
1403
1404static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1405 int qid, int depth)
1406{
1407 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
1408 unsigned offset = (qid - 1) *
1409 roundup(SQ_SIZE(depth), dev->page_size);
1410 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1411 nvmeq->sq_cmds_io = dev->cmb + offset;
1412 } else {
1413 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1414 &nvmeq->sq_dma_addr, GFP_KERNEL);
1415 if (!nvmeq->sq_cmds)
1416 return -ENOMEM;
1417 }
1418
1419 return 0;
1420}
1421
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001422static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Keith Busch2b25d982014-12-22 12:59:04 -07001423 int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001424{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001425 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001426 if (!nvmeq)
1427 return NULL;
1428
Christoph Hellwige75ec752015-05-22 11:12:39 +02001429 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001430 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001431 if (!nvmeq->cqes)
1432 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001433
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001434 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001435 goto free_cqdma;
1436
Christoph Hellwige75ec752015-05-22 11:12:39 +02001437 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001438 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001439 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001440 dev->ctrl.instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001441 spin_lock_init(&nvmeq->q_lock);
1442 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001443 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001444 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001445 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001446 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001447 nvmeq->cq_vector = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001448 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001449
Jon Derrick36a7e992015-05-27 12:26:23 -06001450 /* make sure queue descriptor is set before queue count, for kthread */
1451 mb();
1452 dev->queue_count++;
1453
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001454 return nvmeq;
1455
1456 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001457 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001458 nvmeq->cq_dma_addr);
1459 free_nvmeq:
1460 kfree(nvmeq);
1461 return NULL;
1462}
1463
Matthew Wilcox30010822011-01-20 09:10:15 -05001464static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1465 const char *name)
1466{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001467 if (use_threaded_interrupts)
1468 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001469 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001470 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001471 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001472 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001473}
1474
Keith Busch22404272013-07-15 15:02:20 -06001475static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001476{
Keith Busch22404272013-07-15 15:02:20 -06001477 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001478
Keith Busch7be50e92014-09-10 15:48:47 -06001479 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001480 nvmeq->sq_tail = 0;
1481 nvmeq->cq_head = 0;
1482 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001483 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001484 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001485 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001486 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001487}
1488
1489static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1490{
1491 struct nvme_dev *dev = nvmeq->dev;
1492 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001493
Keith Busch2b25d982014-12-22 12:59:04 -07001494 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001495 result = adapter_alloc_cq(dev, qid, nvmeq);
1496 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001497 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001498
1499 result = adapter_alloc_sq(dev, qid, nvmeq);
1500 if (result < 0)
1501 goto release_cq;
1502
Matthew Wilcox3193f072014-01-27 15:57:22 -05001503 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001504 if (result < 0)
1505 goto release_sq;
1506
Keith Busch22404272013-07-15 15:02:20 -06001507 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001508 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001509
1510 release_sq:
1511 adapter_delete_sq(dev, qid);
1512 release_cq:
1513 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001514 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001515}
1516
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001517static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1518{
1519 unsigned long timeout;
1520 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1521
1522 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1523
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001524 while ((readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_RDY) != bit) {
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001525 msleep(100);
1526 if (fatal_signal_pending(current))
1527 return -EINTR;
1528 if (time_after(jiffies, timeout)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001529 dev_err(dev->dev,
Matthew Wilcox27e81662014-04-11 11:58:45 -04001530 "Device not ready; aborting %s\n", enabled ?
1531 "initialisation" : "reset");
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001532 return -ENODEV;
1533 }
1534 }
1535
1536 return 0;
1537}
1538
1539/*
1540 * If the device has been passed off to us in an enabled state, just clear
1541 * the enabled bit. The spec says we should set the 'shutdown notification
1542 * bits', but doing so may cause the device to complete commands to the
1543 * admin queue ... and we don't know what memory that might be pointing at!
1544 */
1545static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1546{
Dan McLeran01079522014-06-23 08:24:36 -06001547 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1548 dev->ctrl_config &= ~NVME_CC_ENABLE;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001549 writel(dev->ctrl_config, dev->bar + NVME_REG_CC);
Matthew Wilcox44af1462013-05-04 06:43:17 -04001550
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001551 return nvme_wait_ready(dev, cap, false);
1552}
1553
1554static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1555{
Dan McLeran01079522014-06-23 08:24:36 -06001556 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1557 dev->ctrl_config |= NVME_CC_ENABLE;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001558 writel(dev->ctrl_config, dev->bar + NVME_REG_CC);
Dan McLeran01079522014-06-23 08:24:36 -06001559
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001560 return nvme_wait_ready(dev, cap, true);
1561}
1562
Keith Busch1894d8f2013-07-15 15:02:22 -06001563static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1564{
1565 unsigned long timeout;
Keith Busch1894d8f2013-07-15 15:02:22 -06001566
Dan McLeran01079522014-06-23 08:24:36 -06001567 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1568 dev->ctrl_config |= NVME_CC_SHN_NORMAL;
1569
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001570 writel(dev->ctrl_config, dev->bar + NVME_REG_CC);
Keith Busch1894d8f2013-07-15 15:02:22 -06001571
Dan McLeran2484f402014-07-01 09:33:32 -06001572 timeout = SHUTDOWN_TIMEOUT + jiffies;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001573 while ((readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_SHST_MASK) !=
Keith Busch1894d8f2013-07-15 15:02:22 -06001574 NVME_CSTS_SHST_CMPLT) {
1575 msleep(100);
1576 if (fatal_signal_pending(current))
1577 return -EINTR;
1578 if (time_after(jiffies, timeout)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001579 dev_err(dev->dev,
Keith Busch1894d8f2013-07-15 15:02:22 -06001580 "Device shutdown incomplete; abort shutdown\n");
1581 return -ENODEV;
1582 }
1583 }
1584
1585 return 0;
1586}
1587
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001588static struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001589 .queue_rq = nvme_queue_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001590 .map_queue = blk_mq_map_queue,
1591 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001592 .exit_hctx = nvme_admin_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001593 .init_request = nvme_admin_init_request,
1594 .timeout = nvme_timeout,
1595};
1596
1597static struct blk_mq_ops nvme_mq_ops = {
1598 .queue_rq = nvme_queue_rq,
1599 .map_queue = blk_mq_map_queue,
1600 .init_hctx = nvme_init_hctx,
1601 .init_request = nvme_init_request,
1602 .timeout = nvme_timeout,
Jens Axboea0fa9642015-11-03 20:37:26 -07001603 .poll = nvme_poll,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001604};
1605
Keith Buschea191d22015-01-07 18:55:49 -07001606static void nvme_dev_remove_admin(struct nvme_dev *dev)
1607{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001608 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
1609 blk_cleanup_queue(dev->ctrl.admin_q);
Keith Buschea191d22015-01-07 18:55:49 -07001610 blk_mq_free_tag_set(&dev->admin_tagset);
1611 }
1612}
1613
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001614static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1615{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001616 if (!dev->ctrl.admin_q) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001617 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1618 dev->admin_tagset.nr_hw_queues = 1;
1619 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1;
Keith Busch1efccc92015-03-31 10:37:17 -06001620 dev->admin_tagset.reserved_tags = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001621 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001622 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001623 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001624 dev->admin_tagset.driver_data = dev;
1625
1626 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1627 return -ENOMEM;
1628
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001629 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1630 if (IS_ERR(dev->ctrl.admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001631 blk_mq_free_tag_set(&dev->admin_tagset);
1632 return -ENOMEM;
1633 }
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001634 if (!blk_get_queue(dev->ctrl.admin_q)) {
Keith Buschea191d22015-01-07 18:55:49 -07001635 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001636 dev->ctrl.admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001637 return -ENODEV;
1638 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001639 } else
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001640 blk_mq_unfreeze_queue(dev->ctrl.admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001641
1642 return 0;
1643}
1644
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001645static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001646{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001647 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001648 u32 aqa;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001649 u64 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001650 struct nvme_queue *nvmeq;
Nishanth Aravamudanc5c9f252015-11-24 09:55:05 -07001651 /*
1652 * default to a 4K page size, with the intention to update this
1653 * path in the future to accomodate architectures with differing
1654 * kernel and IO page sizes.
1655 */
1656 unsigned page_shift = 12;
Keith Busch1d090622014-06-23 11:34:01 -06001657 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12;
Keith Busch1d090622014-06-23 11:34:01 -06001658
1659 if (page_shift < dev_page_min) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001660 dev_err(dev->dev,
Keith Busch1d090622014-06-23 11:34:01 -06001661 "Minimum device page size (%u) too large for "
1662 "host (%u)\n", 1 << dev_page_min,
1663 1 << page_shift);
1664 return -ENODEV;
1665 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001666
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001667 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1) ?
Keith Buschdfbac8c2015-08-10 15:20:40 -06001668 NVME_CAP_NSSRC(cap) : 0;
1669
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001670 if (dev->subsystem &&
1671 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1672 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001673
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001674 result = nvme_disable_ctrl(dev, cap);
1675 if (result < 0)
1676 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001677
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001678 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001679 if (!nvmeq) {
Keith Busch2b25d982014-12-22 12:59:04 -07001680 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001681 if (!nvmeq)
1682 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001683 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001684
1685 aqa = nvmeq->q_depth - 1;
1686 aqa |= aqa << 16;
1687
Keith Busch1d090622014-06-23 11:34:01 -06001688 dev->page_size = 1 << page_shift;
1689
Dan McLeran01079522014-06-23 08:24:36 -06001690 dev->ctrl_config = NVME_CC_CSS_NVM;
Keith Busch1d090622014-06-23 11:34:01 -06001691 dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001692 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001693 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001694
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001695 writel(aqa, dev->bar + NVME_REG_AQA);
1696 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1697 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001698
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001699 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001700 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001701 goto free_nvmeq;
1702
Keith Busch2b25d982014-12-22 12:59:04 -07001703 nvmeq->cq_vector = 0;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001704 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001705 if (result) {
1706 nvmeq->cq_vector = -1;
Keith Busch0fb59cb2015-01-07 18:55:50 -07001707 goto free_nvmeq;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001708 }
Keith Busch025c5572013-05-01 13:07:51 -06001709
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001710 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001711
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001712 free_nvmeq:
1713 nvme_free_queues(dev, 0);
1714 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001715}
1716
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001717static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1718{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001719 struct nvme_dev *dev = to_nvme_dev(ns->ctrl);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001720 struct nvme_user_io io;
1721 struct nvme_command c;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001722 unsigned length, meta_len;
Keith Buscha67a9512015-04-07 16:57:19 -06001723 int status, write;
Keith Buscha67a9512015-04-07 16:57:19 -06001724 dma_addr_t meta_dma = 0;
1725 void *meta = NULL;
Arnd Bergmannfec558b2015-05-19 17:05:40 +02001726 void __user *metadata;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001727
1728 if (copy_from_user(&io, uio, sizeof(io)))
1729 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001730
1731 switch (io.opcode) {
1732 case nvme_cmd_write:
1733 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001734 case nvme_cmd_compare:
Matthew Wilcox64132142011-08-09 12:56:37 -04001735 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001736 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001737 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001738 }
1739
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001740 length = (io.nblocks + 1) << ns->lba_shift;
1741 meta_len = (io.nblocks + 1) * ns->ms;
Arnd Bergmann835da3f2015-10-06 22:29:48 +02001742 metadata = (void __user *)(uintptr_t)io.metadata;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001743 write = io.opcode & 1;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001744
Keith Busch71feb362015-06-19 11:07:30 -06001745 if (ns->ext) {
1746 length += meta_len;
1747 meta_len = 0;
Keith Buscha67a9512015-04-07 16:57:19 -06001748 }
1749 if (meta_len) {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001750 if (((io.metadata & 3) || !io.metadata) && !ns->ext)
1751 return -EINVAL;
1752
Christoph Hellwige75ec752015-05-22 11:12:39 +02001753 meta = dma_alloc_coherent(dev->dev, meta_len,
Keith Buscha67a9512015-04-07 16:57:19 -06001754 &meta_dma, GFP_KERNEL);
Arnd Bergmannfec558b2015-05-19 17:05:40 +02001755
Keith Buscha67a9512015-04-07 16:57:19 -06001756 if (!meta) {
1757 status = -ENOMEM;
1758 goto unmap;
1759 }
1760 if (write) {
Arnd Bergmannfec558b2015-05-19 17:05:40 +02001761 if (copy_from_user(meta, metadata, meta_len)) {
Keith Buscha67a9512015-04-07 16:57:19 -06001762 status = -EFAULT;
1763 goto unmap;
1764 }
1765 }
1766 }
1767
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001768 memset(&c, 0, sizeof(c));
1769 c.rw.opcode = io.opcode;
1770 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001771 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001772 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001773 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001774 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001775 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1776 c.rw.reftag = cpu_to_le32(io.reftag);
1777 c.rw.apptag = cpu_to_le16(io.apptag);
1778 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buscha67a9512015-04-07 16:57:19 -06001779 c.rw.metadata = cpu_to_le64(meta_dma);
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001780
1781 status = __nvme_submit_sync_cmd(ns->queue, &c, NULL,
Arnd Bergmann835da3f2015-10-06 22:29:48 +02001782 (void __user *)(uintptr_t)io.addr, length, NULL, 0);
Keith Buschf410c682013-04-23 17:23:59 -06001783 unmap:
Keith Buscha67a9512015-04-07 16:57:19 -06001784 if (meta) {
1785 if (status == NVME_SC_SUCCESS && !write) {
Arnd Bergmannfec558b2015-05-19 17:05:40 +02001786 if (copy_to_user(metadata, meta, meta_len))
Keith Buscha67a9512015-04-07 16:57:19 -06001787 status = -EFAULT;
1788 }
Christoph Hellwige75ec752015-05-22 11:12:39 +02001789 dma_free_coherent(dev->dev, meta_len, meta, meta_dma);
Keith Buschf410c682013-04-23 17:23:59 -06001790 }
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001791 return status;
1792}
1793
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001794static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001795 struct nvme_passthru_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001796{
Keith Busch7963e522014-09-12 16:07:20 -06001797 struct nvme_passthru_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001798 struct nvme_command c;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001799 unsigned timeout = 0;
1800 int status;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001801
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001802 if (!capable(CAP_SYS_ADMIN))
1803 return -EACCES;
1804 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001805 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001806
1807 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001808 c.common.opcode = cmd.opcode;
1809 c.common.flags = cmd.flags;
1810 c.common.nsid = cpu_to_le32(cmd.nsid);
1811 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1812 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1813 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1814 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1815 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1816 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1817 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1818 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1819
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001820 if (cmd.timeout_ms)
1821 timeout = msecs_to_jiffies(cmd.timeout_ms);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001822
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001823 status = __nvme_submit_sync_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmann835da3f2015-10-06 22:29:48 +02001824 NULL, (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001825 &cmd.result, timeout);
1826 if (status >= 0) {
1827 if (put_user(cmd.result, &ucmd->result))
1828 return -EFAULT;
Keith Buschedd10d32014-04-03 16:45:23 -06001829 }
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001830
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001831 return status;
1832}
1833
Jon Derrick81f03fe2015-08-10 15:20:41 -06001834static int nvme_subsys_reset(struct nvme_dev *dev)
1835{
1836 if (!dev->subsystem)
1837 return -ENOTTY;
1838
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001839 writel(0x4E564D65, dev->bar + NVME_REG_NSSR); /* "NVMe" */
Jon Derrick81f03fe2015-08-10 15:20:41 -06001840 return 0;
1841}
1842
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001843static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1844 unsigned long arg)
1845{
1846 struct nvme_ns *ns = bdev->bd_disk->private_data;
1847
1848 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001849 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001850 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001851 return ns->ns_id;
1852 case NVME_IOCTL_ADMIN_CMD:
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001853 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06001854 case NVME_IOCTL_IO_CMD:
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001855 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001856 case NVME_IOCTL_SUBMIT_IO:
1857 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001858 case SG_GET_VERSION_NUM:
1859 return nvme_sg_get_version_num((void __user *)arg);
1860 case SG_IO:
1861 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001862 default:
1863 return -ENOTTY;
1864 }
1865}
1866
Keith Busch320a3822013-10-23 13:07:34 -06001867#ifdef CONFIG_COMPAT
1868static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1869 unsigned int cmd, unsigned long arg)
1870{
Keith Busch320a3822013-10-23 13:07:34 -06001871 switch (cmd) {
1872 case SG_IO:
Keith Busche1797292014-08-27 13:55:38 -06001873 return -ENOIOCTLCMD;
Keith Busch320a3822013-10-23 13:07:34 -06001874 }
1875 return nvme_ioctl(bdev, mode, cmd, arg);
1876}
1877#else
1878#define nvme_compat_ioctl NULL
1879#endif
1880
Keith Busch5105aa52015-10-02 10:37:28 -06001881static void nvme_free_dev(struct kref *kref);
Keith Busch188c3562015-10-01 17:14:10 -06001882static void nvme_free_ns(struct kref *kref)
1883{
1884 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001885 struct nvme_dev *dev = to_nvme_dev(ns->ctrl);
Keith Busch188c3562015-10-01 17:14:10 -06001886
Matias Bjørlingca064082015-10-29 17:57:29 +09001887 if (ns->type == NVME_NS_LIGHTNVM)
1888 nvme_nvm_unregister(ns->queue, ns->disk->disk_name);
1889
Keith Busch188c3562015-10-01 17:14:10 -06001890 spin_lock(&dev_list_lock);
1891 ns->disk->private_data = NULL;
1892 spin_unlock(&dev_list_lock);
1893
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001894 kref_put(&dev->kref, nvme_free_dev);
Keith Busch188c3562015-10-01 17:14:10 -06001895 put_disk(ns->disk);
1896 kfree(ns);
1897}
1898
Keith Busch9ac27092014-01-31 16:53:39 -07001899static int nvme_open(struct block_device *bdev, fmode_t mode)
1900{
Keith Busch9e603522014-10-03 11:15:47 -06001901 int ret = 0;
1902 struct nvme_ns *ns;
Keith Busch9ac27092014-01-31 16:53:39 -07001903
Keith Busch9e603522014-10-03 11:15:47 -06001904 spin_lock(&dev_list_lock);
1905 ns = bdev->bd_disk->private_data;
1906 if (!ns)
1907 ret = -ENXIO;
Keith Busch188c3562015-10-01 17:14:10 -06001908 else if (!kref_get_unless_zero(&ns->kref))
Keith Busch9e603522014-10-03 11:15:47 -06001909 ret = -ENXIO;
1910 spin_unlock(&dev_list_lock);
1911
1912 return ret;
Keith Busch9ac27092014-01-31 16:53:39 -07001913}
1914
Keith Busch9ac27092014-01-31 16:53:39 -07001915static void nvme_release(struct gendisk *disk, fmode_t mode)
1916{
1917 struct nvme_ns *ns = disk->private_data;
Keith Busch188c3562015-10-01 17:14:10 -06001918 kref_put(&ns->kref, nvme_free_ns);
Keith Busch9ac27092014-01-31 16:53:39 -07001919}
1920
Keith Busch4cc09e22014-04-02 15:45:37 -06001921static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
1922{
1923 /* some standard values */
1924 geo->heads = 1 << 6;
1925 geo->sectors = 1 << 5;
1926 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1927 return 0;
1928}
1929
Keith Busche1e5e562015-02-19 13:39:03 -07001930static void nvme_config_discard(struct nvme_ns *ns)
1931{
1932 u32 logical_block_size = queue_logical_block_size(ns->queue);
1933 ns->queue->limits.discard_zeroes_data = 0;
1934 ns->queue->limits.discard_alignment = logical_block_size;
1935 ns->queue->limits.discard_granularity = logical_block_size;
Jens Axboe2bb4cd52015-07-14 08:15:12 -06001936 blk_queue_max_discard_sectors(ns->queue, 0xffffffff);
Keith Busche1e5e562015-02-19 13:39:03 -07001937 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1938}
1939
Keith Busch1b9dbf72014-09-10 17:21:14 -06001940static int nvme_revalidate_disk(struct gendisk *disk)
1941{
1942 struct nvme_ns *ns = disk->private_data;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001943 struct nvme_dev *dev = to_nvme_dev(ns->ctrl);
Keith Busch1b9dbf72014-09-10 17:21:14 -06001944 struct nvme_id_ns *id;
Keith Buscha67a9512015-04-07 16:57:19 -06001945 u8 lbaf, pi_type;
1946 u16 old_ms;
Keith Busche1e5e562015-02-19 13:39:03 -07001947 unsigned short bs;
Keith Busch1b9dbf72014-09-10 17:21:14 -06001948
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001949 if (nvme_identify_ns(&dev->ctrl, ns->ns_id, &id)) {
Keith Buscha5768aa2015-06-01 14:28:14 -06001950 dev_warn(dev->dev, "%s: Identify failure nvme%dn%d\n", __func__,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001951 dev->ctrl.instance, ns->ns_id);
Keith Buscha5768aa2015-06-01 14:28:14 -06001952 return -ENODEV;
Keith Busch1b9dbf72014-09-10 17:21:14 -06001953 }
Keith Buscha5768aa2015-06-01 14:28:14 -06001954 if (id->ncap == 0) {
1955 kfree(id);
1956 return -ENODEV;
Keith Busche1e5e562015-02-19 13:39:03 -07001957 }
Keith Busch1b9dbf72014-09-10 17:21:14 -06001958
Matias Bjørlingca064082015-10-29 17:57:29 +09001959 if (nvme_nvm_ns_supported(ns, id) && ns->type != NVME_NS_LIGHTNVM) {
1960 if (nvme_nvm_register(ns->queue, disk->disk_name)) {
1961 dev_warn(dev->dev,
1962 "%s: LightNVM init failure\n", __func__);
1963 kfree(id);
1964 return -ENODEV;
1965 }
1966 ns->type = NVME_NS_LIGHTNVM;
1967 }
1968
Keith Busche1e5e562015-02-19 13:39:03 -07001969 old_ms = ns->ms;
1970 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
Keith Busch1b9dbf72014-09-10 17:21:14 -06001971 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Busche1e5e562015-02-19 13:39:03 -07001972 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Buscha67a9512015-04-07 16:57:19 -06001973 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
Keith Busch1b9dbf72014-09-10 17:21:14 -06001974
Keith Busche1e5e562015-02-19 13:39:03 -07001975 /*
1976 * If identify namespace failed, use default 512 byte block size so
1977 * block layer can use before failing read/write for 0 capacity.
1978 */
1979 if (ns->lba_shift == 0)
1980 ns->lba_shift = 9;
1981 bs = 1 << ns->lba_shift;
1982
1983 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
1984 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
1985 id->dps & NVME_NS_DPS_PI_MASK : 0;
1986
Dan Williams4cfc7662015-10-21 13:20:07 -04001987 blk_mq_freeze_queue(disk->queue);
Keith Busch52b68d72015-02-23 09:16:21 -07001988 if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
1989 ns->ms != old_ms ||
Keith Busche1e5e562015-02-19 13:39:03 -07001990 bs != queue_logical_block_size(disk->queue) ||
Keith Buscha67a9512015-04-07 16:57:19 -06001991 (ns->ms && ns->ext)))
Keith Busche1e5e562015-02-19 13:39:03 -07001992 blk_integrity_unregister(disk);
1993
1994 ns->pi_type = pi_type;
1995 blk_queue_logical_block_size(ns->queue, bs);
1996
Martin K. Petersen25520d52015-10-21 13:19:49 -04001997 if (ns->ms && !ns->ext)
Keith Busche1e5e562015-02-19 13:39:03 -07001998 nvme_init_integrity(ns);
1999
Matias Bjørlingca064082015-10-29 17:57:29 +09002000 if ((ns->ms && !(ns->ms == 8 && ns->pi_type) &&
2001 !blk_get_integrity(disk)) ||
2002 ns->type == NVME_NS_LIGHTNVM)
Keith Busche1e5e562015-02-19 13:39:03 -07002003 set_capacity(disk, 0);
2004 else
2005 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
2006
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002007 if (dev->ctrl.oncs & NVME_CTRL_ONCS_DSM)
Keith Busche1e5e562015-02-19 13:39:03 -07002008 nvme_config_discard(ns);
Dan Williams4cfc7662015-10-21 13:20:07 -04002009 blk_mq_unfreeze_queue(disk->queue);
Keith Busche1e5e562015-02-19 13:39:03 -07002010
Christoph Hellwigd29ec822015-05-22 11:12:46 +02002011 kfree(id);
Keith Busch1b9dbf72014-09-10 17:21:14 -06002012 return 0;
2013}
2014
Keith Busch1d277a62015-10-15 14:10:52 +02002015static char nvme_pr_type(enum pr_type type)
2016{
2017 switch (type) {
2018 case PR_WRITE_EXCLUSIVE:
2019 return 1;
2020 case PR_EXCLUSIVE_ACCESS:
2021 return 2;
2022 case PR_WRITE_EXCLUSIVE_REG_ONLY:
2023 return 3;
2024 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
2025 return 4;
2026 case PR_WRITE_EXCLUSIVE_ALL_REGS:
2027 return 5;
2028 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
2029 return 6;
2030 default:
2031 return 0;
2032 }
2033};
2034
2035static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
2036 u64 key, u64 sa_key, u8 op)
2037{
2038 struct nvme_ns *ns = bdev->bd_disk->private_data;
2039 struct nvme_command c;
2040 u8 data[16] = { 0, };
2041
2042 put_unaligned_le64(key, &data[0]);
2043 put_unaligned_le64(sa_key, &data[8]);
2044
2045 memset(&c, 0, sizeof(c));
2046 c.common.opcode = op;
Christoph Hellwiga6dd1022015-10-22 12:01:05 +02002047 c.common.nsid = cpu_to_le32(ns->ns_id);
2048 c.common.cdw10[0] = cpu_to_le32(cdw10);
Keith Busch1d277a62015-10-15 14:10:52 +02002049
2050 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
2051}
2052
2053static int nvme_pr_register(struct block_device *bdev, u64 old,
2054 u64 new, unsigned flags)
2055{
2056 u32 cdw10;
2057
2058 if (flags & ~PR_FL_IGNORE_KEY)
2059 return -EOPNOTSUPP;
2060
2061 cdw10 = old ? 2 : 0;
2062 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
2063 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
2064 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
2065}
2066
2067static int nvme_pr_reserve(struct block_device *bdev, u64 key,
2068 enum pr_type type, unsigned flags)
2069{
2070 u32 cdw10;
2071
2072 if (flags & ~PR_FL_IGNORE_KEY)
2073 return -EOPNOTSUPP;
2074
2075 cdw10 = nvme_pr_type(type) << 8;
2076 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
2077 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
2078}
2079
2080static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
2081 enum pr_type type, bool abort)
2082{
2083 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
2084 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
2085}
2086
2087static int nvme_pr_clear(struct block_device *bdev, u64 key)
2088{
Dan Carpenter73fcf4e2015-11-03 22:50:49 +03002089 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Keith Busch1d277a62015-10-15 14:10:52 +02002090 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
2091}
2092
2093static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
2094{
2095 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
2096 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
2097}
2098
2099static const struct pr_ops nvme_pr_ops = {
2100 .pr_register = nvme_pr_register,
2101 .pr_reserve = nvme_pr_reserve,
2102 .pr_release = nvme_pr_release,
2103 .pr_preempt = nvme_pr_preempt,
2104 .pr_clear = nvme_pr_clear,
2105};
2106
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002107static const struct block_device_operations nvme_fops = {
2108 .owner = THIS_MODULE,
2109 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06002110 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07002111 .open = nvme_open,
2112 .release = nvme_release,
Keith Busch4cc09e22014-04-02 15:45:37 -06002113 .getgeo = nvme_getgeo,
Keith Busch1b9dbf72014-09-10 17:21:14 -06002114 .revalidate_disk= nvme_revalidate_disk,
Keith Busch1d277a62015-10-15 14:10:52 +02002115 .pr_ops = &nvme_pr_ops,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002116};
2117
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002118static int nvme_kthread(void *data)
2119{
Keith Buschd4b4ff82013-12-10 13:10:37 -07002120 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002121
2122 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04002123 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002124 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07002125 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002126 int i;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002127 u32 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06002128
2129 if ((dev->subsystem && (csts & NVME_CSTS_NSSRO)) ||
2130 csts & NVME_CSTS_CFS) {
Christoph Hellwig906678922015-10-02 18:49:23 +02002131 if (!__nvme_reset(dev)) {
2132 dev_warn(dev->dev,
2133 "Failed status: %x, reset controller\n",
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002134 readl(dev->bar + NVME_REG_CSTS));
Christoph Hellwig906678922015-10-02 18:49:23 +02002135 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002136 continue;
2137 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002138 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002139 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05002140 if (!nvmeq)
2141 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002142 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04002143 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06002144
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002145 while (i == 0 && dev->ctrl.event_limit > 0) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002146 if (nvme_submit_async_admin_req(dev))
Keith Busch6fccf932014-06-18 13:58:57 -06002147 break;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002148 dev->ctrl.event_limit--;
Keith Busch6fccf932014-06-18 13:58:57 -06002149 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002150 spin_unlock_irq(&nvmeq->q_lock);
2151 }
2152 }
2153 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08002154 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002155 }
2156 return 0;
2157}
2158
Keith Busche1e5e562015-02-19 13:39:03 -07002159static void nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002160{
2161 struct nvme_ns *ns;
2162 struct gendisk *disk;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002163 int node = dev_to_node(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002164
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002165 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002166 if (!ns)
Keith Busche1e5e562015-02-19 13:39:03 -07002167 return;
2168
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002169 ns->queue = blk_mq_init_queue(&dev->tagset);
Dan Carpenter9f173b32014-11-05 23:39:09 +03002170 if (IS_ERR(ns->queue))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002171 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07002172 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
2173 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002174 ns->ctrl = &dev->ctrl;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002175 ns->queue->queuedata = ns;
2176
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002177 disk = alloc_disk_node(0, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002178 if (!disk)
2179 goto out_free_queue;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002180
Keith Busch188c3562015-10-01 17:14:10 -06002181 kref_init(&ns->kref);
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002182 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002183 ns->disk = disk;
Keith Busche1e5e562015-02-19 13:39:03 -07002184 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
2185 list_add_tail(&ns->list, &dev->namespaces);
2186
Keith Busche9ef4632012-07-24 15:01:04 -06002187 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busche8244102015-08-12 16:17:54 -06002188 if (dev->max_hw_sectors) {
Keith Busch8fc23e02012-07-26 11:29:57 -06002189 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Keith Busche8244102015-08-12 16:17:54 -06002190 blk_queue_max_segments(ns->queue,
Keith Busch6824c5e2015-11-18 16:33:08 -07002191 (dev->max_hw_sectors / (dev->page_size >> 9)) + 1);
Keith Busche8244102015-08-12 16:17:54 -06002192 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002193 if (dev->stripe_size)
2194 blk_queue_chunk_sectors(ns->queue, dev->stripe_size >> 9);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002195 if (dev->ctrl.vwc & NVME_CTRL_VWC_PRESENT)
Keith Buscha7d2ce22014-04-29 11:41:28 -06002196 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
Keith Busch03100aa2015-08-19 14:24:05 -07002197 blk_queue_virt_boundary(ns->queue, dev->page_size - 1);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002198
2199 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002200 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002201 disk->fops = &nvme_fops;
2202 disk->private_data = ns;
2203 disk->queue = ns->queue;
Keith Buschb3fffde2015-02-03 11:21:42 -07002204 disk->driverfs_dev = dev->device;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002205 disk->flags = GENHD_FL_EXT_DEVT;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002206 sprintf(disk->disk_name, "nvme%dn%d", dev->ctrl.instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002207
Keith Busche1e5e562015-02-19 13:39:03 -07002208 /*
2209 * Initialize capacity to 0 until we establish the namespace format and
2210 * setup integrity extentions if necessary. The revalidate_disk after
2211 * add_disk allows the driver to register with integrity if the format
2212 * requires it.
2213 */
2214 set_capacity(disk, 0);
Keith Buscha5768aa2015-06-01 14:28:14 -06002215 if (nvme_revalidate_disk(ns->disk))
2216 goto out_free_disk;
2217
Keith Busch5105aa52015-10-02 10:37:28 -06002218 kref_get(&dev->kref);
Matias Bjørlingca064082015-10-29 17:57:29 +09002219 if (ns->type != NVME_NS_LIGHTNVM) {
2220 add_disk(ns->disk);
2221 if (ns->ms) {
2222 struct block_device *bd = bdget_disk(ns->disk, 0);
2223 if (!bd)
2224 return;
2225 if (blkdev_get(bd, FMODE_READ, NULL)) {
2226 bdput(bd);
2227 return;
2228 }
2229 blkdev_reread_part(bd);
2230 blkdev_put(bd, FMODE_READ);
Keith Busch7bee6072015-07-14 11:57:48 -06002231 }
Keith Busch7bee6072015-07-14 11:57:48 -06002232 }
Keith Busche1e5e562015-02-19 13:39:03 -07002233 return;
Keith Buscha5768aa2015-06-01 14:28:14 -06002234 out_free_disk:
2235 kfree(disk);
2236 list_del(&ns->list);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002237 out_free_queue:
2238 blk_cleanup_queue(ns->queue);
2239 out_free_ns:
2240 kfree(ns);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002241}
2242
Christoph Hellwig2659e572015-10-02 18:51:31 +02002243/*
2244 * Create I/O queues. Failing to create an I/O queue is not an issue,
2245 * we can continue with less than the desired amount of queues, and
2246 * even a controller without I/O queues an still be used to issue
2247 * admin commands. This might be useful to upgrade a buggy firmware
2248 * for example.
2249 */
Keith Busch42f61422014-03-24 10:46:25 -06002250static void nvme_create_io_queues(struct nvme_dev *dev)
2251{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002252 unsigned i;
Keith Busch42f61422014-03-24 10:46:25 -06002253
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002254 for (i = dev->queue_count; i <= dev->max_qid; i++)
Keith Busch2b25d982014-12-22 12:59:04 -07002255 if (!nvme_alloc_queue(dev, i, dev->q_depth))
Keith Busch42f61422014-03-24 10:46:25 -06002256 break;
2257
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002258 for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
Christoph Hellwig2659e572015-10-02 18:51:31 +02002259 if (nvme_create_queue(dev->queues[i], i)) {
2260 nvme_free_queues(dev, i);
Keith Busch42f61422014-03-24 10:46:25 -06002261 break;
Christoph Hellwig2659e572015-10-02 18:51:31 +02002262 }
Keith Busch42f61422014-03-24 10:46:25 -06002263}
2264
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002265static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002266{
2267 int status;
2268 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002269 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002270
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002271 status = nvme_set_features(&dev->ctrl, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002272 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04002273 if (status < 0)
2274 return status;
2275 if (status > 0) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002276 dev_err(dev->dev, "Could not set queue count (%d)\n", status);
Keith Buschbadc34d2014-06-23 14:25:35 -06002277 return 0;
Matthew Wilcox27e81662014-04-11 11:58:45 -04002278 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002279 return min(result & 0xffff, result >> 16) + 1;
2280}
2281
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002282static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
2283{
2284 u64 szu, size, offset;
2285 u32 cmbloc;
2286 resource_size_t bar_size;
2287 struct pci_dev *pdev = to_pci_dev(dev->dev);
2288 void __iomem *cmb;
2289 dma_addr_t dma_addr;
2290
2291 if (!use_cmb_sqes)
2292 return NULL;
2293
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002294 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002295 if (!(NVME_CMB_SZ(dev->cmbsz)))
2296 return NULL;
2297
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002298 cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002299
2300 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
2301 size = szu * NVME_CMB_SZ(dev->cmbsz);
2302 offset = szu * NVME_CMB_OFST(cmbloc);
2303 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(cmbloc));
2304
2305 if (offset > bar_size)
2306 return NULL;
2307
2308 /*
2309 * Controllers may support a CMB size larger than their BAR,
2310 * for example, due to being behind a bridge. Reduce the CMB to
2311 * the reported size of the BAR
2312 */
2313 if (size > bar_size - offset)
2314 size = bar_size - offset;
2315
2316 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(cmbloc)) + offset;
2317 cmb = ioremap_wc(dma_addr, size);
2318 if (!cmb)
2319 return NULL;
2320
2321 dev->cmb_dma_addr = dma_addr;
2322 dev->cmb_size = size;
2323 return cmb;
2324}
2325
2326static inline void nvme_release_cmb(struct nvme_dev *dev)
2327{
2328 if (dev->cmb) {
2329 iounmap(dev->cmb);
2330 dev->cmb = NULL;
2331 }
2332}
2333
Keith Busch9d713c22013-07-15 15:02:24 -06002334static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
2335{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002336 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06002337}
2338
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002339static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002340{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002341 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02002342 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch42f61422014-03-24 10:46:25 -06002343 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002344
Keith Busch42f61422014-03-24 10:46:25 -06002345 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002346 result = set_queue_count(dev, nr_io_queues);
Keith Buschbadc34d2014-06-23 14:25:35 -06002347 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002348 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002349 if (result < nr_io_queues)
2350 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002351
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002352 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
2353 result = nvme_cmb_qdepth(dev, nr_io_queues,
2354 sizeof(struct nvme_command));
2355 if (result > 0)
2356 dev->q_depth = result;
2357 else
2358 nvme_release_cmb(dev);
2359 }
2360
Keith Busch9d713c22013-07-15 15:02:24 -06002361 size = db_bar_size(dev, nr_io_queues);
2362 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002363 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06002364 do {
2365 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
2366 if (dev->bar)
2367 break;
2368 if (!--nr_io_queues)
2369 return -ENOMEM;
2370 size = db_bar_size(dev, nr_io_queues);
2371 } while (1);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002372 dev->dbs = dev->bar + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07002373 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002374 }
2375
Keith Busch9d713c22013-07-15 15:02:24 -06002376 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002377 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002378
Jens Axboee32efbf2014-11-14 09:49:26 -07002379 /*
2380 * If we enable msix early due to not intx, disable it again before
2381 * setting up the full range we need.
2382 */
2383 if (!pdev->irq)
2384 pci_disable_msix(pdev);
2385
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002386 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002387 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002388 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
2389 if (vecs < 0) {
2390 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
2391 if (vecs < 0) {
2392 vecs = 1;
2393 } else {
2394 for (i = 0; i < vecs; i++)
2395 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002396 }
2397 }
2398
Matthew Wilcox063a8092013-06-20 10:53:48 -04002399 /*
2400 * Should investigate if there's a performance win from allocating
2401 * more queues than interrupt vectors; it might allow the submission
2402 * path to scale better, even if the receive path is limited by the
2403 * number of interrupts.
2404 */
2405 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002406 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002407
Matthew Wilcox3193f072014-01-27 15:57:22 -05002408 result = queue_request_irq(dev, adminq, adminq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06002409 if (result) {
2410 adminq->cq_vector = -1;
Keith Busch22404272013-07-15 15:02:20 -06002411 goto free_queues;
Jon Derrick758dd7f2015-06-30 11:22:52 -06002412 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05002413
Keith Buschcd638942013-07-15 15:02:23 -06002414 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002415 nvme_free_queues(dev, nr_io_queues + 1);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002416 nvme_create_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06002417
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002418 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002419
Keith Busch22404272013-07-15 15:02:20 -06002420 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002421 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002422 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002423}
2424
Keith Buscha5768aa2015-06-01 14:28:14 -06002425static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2426{
2427 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2428 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2429
2430 return nsa->ns_id - nsb->ns_id;
2431}
2432
2433static struct nvme_ns *nvme_find_ns(struct nvme_dev *dev, unsigned nsid)
2434{
2435 struct nvme_ns *ns;
2436
2437 list_for_each_entry(ns, &dev->namespaces, list) {
2438 if (ns->ns_id == nsid)
2439 return ns;
2440 if (ns->ns_id > nsid)
2441 break;
2442 }
2443 return NULL;
2444}
2445
2446static inline bool nvme_io_incapable(struct nvme_dev *dev)
2447{
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002448 return (!dev->bar ||
2449 readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_CFS ||
2450 dev->online_queues < 2);
Keith Buscha5768aa2015-06-01 14:28:14 -06002451}
2452
2453static void nvme_ns_remove(struct nvme_ns *ns)
2454{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002455 bool kill = nvme_io_incapable(to_nvme_dev(ns->ctrl)) &&
2456 !blk_queue_dying(ns->queue);
Keith Buscha5768aa2015-06-01 14:28:14 -06002457
2458 if (kill)
2459 blk_set_queue_dying(ns->queue);
Dan Williams9609b992015-10-21 13:19:55 -04002460 if (ns->disk->flags & GENHD_FL_UP)
Keith Buscha5768aa2015-06-01 14:28:14 -06002461 del_gendisk(ns->disk);
Keith Buscha5768aa2015-06-01 14:28:14 -06002462 if (kill || !blk_queue_dying(ns->queue)) {
2463 blk_mq_abort_requeue_list(ns->queue);
2464 blk_cleanup_queue(ns->queue);
Keith Busch5105aa52015-10-02 10:37:28 -06002465 }
2466 list_del_init(&ns->list);
2467 kref_put(&ns->kref, nvme_free_ns);
Keith Buscha5768aa2015-06-01 14:28:14 -06002468}
2469
2470static void nvme_scan_namespaces(struct nvme_dev *dev, unsigned nn)
2471{
2472 struct nvme_ns *ns, *next;
2473 unsigned i;
2474
2475 for (i = 1; i <= nn; i++) {
2476 ns = nvme_find_ns(dev, i);
2477 if (ns) {
Keith Busch5105aa52015-10-02 10:37:28 -06002478 if (revalidate_disk(ns->disk))
Keith Buscha5768aa2015-06-01 14:28:14 -06002479 nvme_ns_remove(ns);
Keith Buscha5768aa2015-06-01 14:28:14 -06002480 } else
2481 nvme_alloc_ns(dev, i);
2482 }
2483 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
Keith Busch5105aa52015-10-02 10:37:28 -06002484 if (ns->ns_id > nn)
Keith Buscha5768aa2015-06-01 14:28:14 -06002485 nvme_ns_remove(ns);
Keith Buscha5768aa2015-06-01 14:28:14 -06002486 }
2487 list_sort(NULL, &dev->namespaces, ns_cmp);
2488}
2489
Keith Buschbda4e0f2015-09-03 08:18:17 -06002490static void nvme_set_irq_hints(struct nvme_dev *dev)
2491{
2492 struct nvme_queue *nvmeq;
2493 int i;
2494
2495 for (i = 0; i < dev->online_queues; i++) {
2496 nvmeq = dev->queues[i];
2497
2498 if (!nvmeq->tags || !(*nvmeq->tags))
2499 continue;
2500
2501 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
2502 blk_mq_tags_cpumask(*nvmeq->tags));
2503 }
2504}
2505
Keith Buscha5768aa2015-06-01 14:28:14 -06002506static void nvme_dev_scan(struct work_struct *work)
2507{
2508 struct nvme_dev *dev = container_of(work, struct nvme_dev, scan_work);
2509 struct nvme_id_ctrl *ctrl;
2510
2511 if (!dev->tagset.tags)
2512 return;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002513 if (nvme_identify_ctrl(&dev->ctrl, &ctrl))
Keith Buscha5768aa2015-06-01 14:28:14 -06002514 return;
2515 nvme_scan_namespaces(dev, le32_to_cpup(&ctrl->nn));
2516 kfree(ctrl);
Keith Buschbda4e0f2015-09-03 08:18:17 -06002517 nvme_set_irq_hints(dev);
Keith Buscha5768aa2015-06-01 14:28:14 -06002518}
2519
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002520/*
2521 * Return: error value if an error occurred setting up the queues or calling
2522 * Identify Device. 0 if these succeeded, even if adding some of the
2523 * namespaces failed. At the moment, these failures are silent. TBD which
2524 * failures should be reported.
2525 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002526static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002527{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002528 struct pci_dev *pdev = to_pci_dev(dev->dev);
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002529 int res;
Matthew Wilcox51814232011-02-01 16:18:08 -05002530 struct nvme_id_ctrl *ctrl;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002531 int shift = NVME_CAP_MPSMIN(lo_hi_readq(dev->bar + NVME_REG_CAP)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002532
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002533 res = nvme_identify_ctrl(&dev->ctrl, &ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002534 if (res) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002535 dev_err(dev->dev, "Identify Controller failed (%d)\n", res);
Keith Busche1e5e562015-02-19 13:39:03 -07002536 return -EIO;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002537 }
2538
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002539 dev->ctrl.oncs = le16_to_cpup(&ctrl->oncs);
2540 dev->ctrl.abort_limit = ctrl->acl + 1;
2541 dev->ctrl.vwc = ctrl->vwc;
2542 memcpy(dev->ctrl.serial, ctrl->sn, sizeof(ctrl->sn));
2543 memcpy(dev->ctrl.model, ctrl->mn, sizeof(ctrl->mn));
2544 memcpy(dev->ctrl.firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002545 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002546 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Sathyavathi Mb12363d2015-11-05 12:52:28 -07002547 else
2548 dev->max_hw_sectors = UINT_MAX;
Matthew Wilcox68608c262013-06-21 14:36:34 -04002549 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002550 (pdev->device == 0x0953) && ctrl->vs[3]) {
2551 unsigned int max_hw_sectors;
2552
Keith Busch159b67d2013-04-09 17:13:20 -06002553 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002554 max_hw_sectors = dev->stripe_size >> (shift - 9);
2555 if (dev->max_hw_sectors) {
2556 dev->max_hw_sectors = min(max_hw_sectors,
2557 dev->max_hw_sectors);
2558 } else
2559 dev->max_hw_sectors = max_hw_sectors;
2560 }
Christoph Hellwigd29ec822015-05-22 11:12:46 +02002561 kfree(ctrl);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002562
Keith Buschffe77042015-06-08 10:08:15 -06002563 if (!dev->tagset.tags) {
2564 dev->tagset.ops = &nvme_mq_ops;
2565 dev->tagset.nr_hw_queues = dev->online_queues - 1;
2566 dev->tagset.timeout = NVME_IO_TIMEOUT;
2567 dev->tagset.numa_node = dev_to_node(dev->dev);
2568 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002569 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Keith Buschffe77042015-06-08 10:08:15 -06002570 dev->tagset.cmd_size = nvme_cmd_size(dev);
2571 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
2572 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002573
Keith Buschffe77042015-06-08 10:08:15 -06002574 if (blk_mq_alloc_tag_set(&dev->tagset))
2575 return 0;
2576 }
Keith Buscha5768aa2015-06-01 14:28:14 -06002577 schedule_work(&dev->scan_work);
Keith Busche1e5e562015-02-19 13:39:03 -07002578 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002579}
2580
Keith Busch0877cb02013-07-15 15:02:19 -06002581static int nvme_dev_map(struct nvme_dev *dev)
2582{
Keith Busch42f61422014-03-24 10:46:25 -06002583 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002584 int bars, result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002585 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002586
2587 if (pci_enable_device_mem(pdev))
2588 return result;
2589
2590 dev->entry[0].vector = pdev->irq;
2591 pci_set_master(pdev);
2592 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07002593 if (!bars)
2594 goto disable_pci;
2595
Keith Busch0877cb02013-07-15 15:02:19 -06002596 if (pci_request_selected_regions(pdev, bars, "nvme"))
2597 goto disable_pci;
2598
Christoph Hellwige75ec752015-05-22 11:12:39 +02002599 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
2600 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01002601 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002602
Keith Busch0877cb02013-07-15 15:02:19 -06002603 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2604 if (!dev->bar)
2605 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07002606
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002607 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
Keith Busch0e53d182013-12-10 13:10:39 -07002608 result = -ENODEV;
2609 goto unmap;
2610 }
Jens Axboee32efbf2014-11-14 09:49:26 -07002611
2612 /*
2613 * Some devices don't advertse INTx interrupts, pre-enable a single
2614 * MSIX vec for setup. We'll adjust this later.
2615 */
2616 if (!pdev->irq) {
2617 result = pci_enable_msix(pdev, dev->entry, 1);
2618 if (result < 0)
2619 goto unmap;
2620 }
2621
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002622 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
2623
Keith Busch42f61422014-03-24 10:46:25 -06002624 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2625 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002626 dev->dbs = dev->bar + 4096;
2627 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2))
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002628 dev->cmb = nvme_map_cmb(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002629
2630 return 0;
2631
Keith Busch0e53d182013-12-10 13:10:39 -07002632 unmap:
2633 iounmap(dev->bar);
2634 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002635 disable:
2636 pci_release_regions(pdev);
2637 disable_pci:
2638 pci_disable_device(pdev);
2639 return result;
2640}
2641
2642static void nvme_dev_unmap(struct nvme_dev *dev)
2643{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002644 struct pci_dev *pdev = to_pci_dev(dev->dev);
2645
2646 if (pdev->msi_enabled)
2647 pci_disable_msi(pdev);
2648 else if (pdev->msix_enabled)
2649 pci_disable_msix(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002650
2651 if (dev->bar) {
2652 iounmap(dev->bar);
2653 dev->bar = NULL;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002654 pci_release_regions(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002655 }
2656
Christoph Hellwige75ec752015-05-22 11:12:39 +02002657 if (pci_is_enabled(pdev))
2658 pci_disable_device(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002659}
2660
Keith Busch4d115422013-12-10 13:10:40 -07002661struct nvme_delq_ctx {
2662 struct task_struct *waiter;
2663 struct kthread_worker *worker;
2664 atomic_t refcount;
2665};
2666
2667static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2668{
2669 dq->waiter = current;
2670 mb();
2671
2672 for (;;) {
2673 set_current_state(TASK_KILLABLE);
2674 if (!atomic_read(&dq->refcount))
2675 break;
2676 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2677 fatal_signal_pending(current)) {
Keith Busch0fb59cb2015-01-07 18:55:50 -07002678 /*
2679 * Disable the controller first since we can't trust it
2680 * at this point, but leave the admin queue enabled
2681 * until all queue deletion requests are flushed.
2682 * FIXME: This may take a while if there are more h/w
2683 * queues than admin tags.
2684 */
Keith Busch4d115422013-12-10 13:10:40 -07002685 set_current_state(TASK_RUNNING);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002686 nvme_disable_ctrl(dev,
2687 lo_hi_readq(dev->bar + NVME_REG_CAP));
Keith Busch0fb59cb2015-01-07 18:55:50 -07002688 nvme_clear_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07002689 flush_kthread_worker(dq->worker);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002690 nvme_disable_queue(dev, 0);
Keith Busch4d115422013-12-10 13:10:40 -07002691 return;
2692 }
2693 }
2694 set_current_state(TASK_RUNNING);
2695}
2696
2697static void nvme_put_dq(struct nvme_delq_ctx *dq)
2698{
2699 atomic_dec(&dq->refcount);
2700 if (dq->waiter)
2701 wake_up_process(dq->waiter);
2702}
2703
2704static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2705{
2706 atomic_inc(&dq->refcount);
2707 return dq;
2708}
2709
2710static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2711{
2712 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
Keith Busch4d115422013-12-10 13:10:40 -07002713 nvme_put_dq(dq);
Keith Busch604e8c82015-11-20 08:38:13 -07002714
2715 spin_lock_irq(&nvmeq->q_lock);
2716 nvme_process_cq(nvmeq);
2717 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07002718}
2719
2720static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2721 kthread_work_func_t fn)
2722{
2723 struct nvme_command c;
2724
2725 memset(&c, 0, sizeof(c));
2726 c.delete_queue.opcode = opcode;
2727 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2728
2729 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002730 return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
2731 ADMIN_TIMEOUT);
Keith Busch4d115422013-12-10 13:10:40 -07002732}
2733
2734static void nvme_del_cq_work_handler(struct kthread_work *work)
2735{
2736 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2737 cmdinfo.work);
2738 nvme_del_queue_end(nvmeq);
2739}
2740
2741static int nvme_delete_cq(struct nvme_queue *nvmeq)
2742{
2743 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2744 nvme_del_cq_work_handler);
2745}
2746
2747static void nvme_del_sq_work_handler(struct kthread_work *work)
2748{
2749 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2750 cmdinfo.work);
2751 int status = nvmeq->cmdinfo.status;
2752
2753 if (!status)
2754 status = nvme_delete_cq(nvmeq);
2755 if (status)
2756 nvme_del_queue_end(nvmeq);
2757}
2758
2759static int nvme_delete_sq(struct nvme_queue *nvmeq)
2760{
2761 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2762 nvme_del_sq_work_handler);
2763}
2764
2765static void nvme_del_queue_start(struct kthread_work *work)
2766{
2767 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2768 cmdinfo.work);
Keith Busch4d115422013-12-10 13:10:40 -07002769 if (nvme_delete_sq(nvmeq))
2770 nvme_del_queue_end(nvmeq);
2771}
2772
2773static void nvme_disable_io_queues(struct nvme_dev *dev)
2774{
2775 int i;
2776 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2777 struct nvme_delq_ctx dq;
2778 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002779 &worker, "nvme%d", dev->ctrl.instance);
Keith Busch4d115422013-12-10 13:10:40 -07002780
2781 if (IS_ERR(kworker_task)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002782 dev_err(dev->dev,
Keith Busch4d115422013-12-10 13:10:40 -07002783 "Failed to create queue del task\n");
2784 for (i = dev->queue_count - 1; i > 0; i--)
2785 nvme_disable_queue(dev, i);
2786 return;
2787 }
2788
2789 dq.waiter = NULL;
2790 atomic_set(&dq.refcount, 0);
2791 dq.worker = &worker;
2792 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002793 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002794
2795 if (nvme_suspend_queue(nvmeq))
2796 continue;
2797 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2798 nvmeq->cmdinfo.worker = dq.worker;
2799 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2800 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2801 }
2802 nvme_wait_dq(&dq, dev);
2803 kthread_stop(kworker_task);
2804}
2805
Dan McLeranb9afca32014-04-07 17:10:11 -06002806/*
2807* Remove the node from the device list and check
2808* for whether or not we need to stop the nvme_thread.
2809*/
2810static void nvme_dev_list_remove(struct nvme_dev *dev)
2811{
2812 struct task_struct *tmp = NULL;
2813
2814 spin_lock(&dev_list_lock);
2815 list_del_init(&dev->node);
2816 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2817 tmp = nvme_thread;
2818 nvme_thread = NULL;
2819 }
2820 spin_unlock(&dev_list_lock);
2821
2822 if (tmp)
2823 kthread_stop(tmp);
2824}
2825
Keith Buschc9d3bf82015-01-07 18:55:52 -07002826static void nvme_freeze_queues(struct nvme_dev *dev)
2827{
2828 struct nvme_ns *ns;
2829
2830 list_for_each_entry(ns, &dev->namespaces, list) {
2831 blk_mq_freeze_queue_start(ns->queue);
2832
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002833 spin_lock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002834 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002835 spin_unlock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002836
2837 blk_mq_cancel_requeue_work(ns->queue);
2838 blk_mq_stop_hw_queues(ns->queue);
2839 }
2840}
2841
2842static void nvme_unfreeze_queues(struct nvme_dev *dev)
2843{
2844 struct nvme_ns *ns;
2845
2846 list_for_each_entry(ns, &dev->namespaces, list) {
2847 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2848 blk_mq_unfreeze_queue(ns->queue);
2849 blk_mq_start_stopped_hw_queues(ns->queue, true);
2850 blk_mq_kick_requeue_list(ns->queue);
2851 }
2852}
2853
Keith Buschf0b50732013-07-15 15:02:21 -06002854static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002855{
Keith Busch22404272013-07-15 15:02:20 -06002856 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06002857 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06002858
Dan McLeranb9afca32014-04-07 17:10:11 -06002859 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002860
Keith Buschc9d3bf82015-01-07 18:55:52 -07002861 if (dev->bar) {
2862 nvme_freeze_queues(dev);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002863 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002864 }
Keith Busch7c1b2452014-06-25 11:18:12 -06002865 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07002866 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002867 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002868 nvme_suspend_queue(nvmeq);
Keith Busch4d115422013-12-10 13:10:40 -07002869 }
2870 } else {
2871 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002872 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002873 nvme_disable_queue(dev, 0);
2874 }
Keith Buschf0b50732013-07-15 15:02:21 -06002875 nvme_dev_unmap(dev);
Keith Busch07836e62015-02-19 10:34:48 -07002876
2877 for (i = dev->queue_count - 1; i >= 0; i--)
2878 nvme_clear_queue(dev->queues[i]);
Keith Buschf0b50732013-07-15 15:02:21 -06002879}
2880
2881static void nvme_dev_remove(struct nvme_dev *dev)
2882{
Keith Busch5105aa52015-10-02 10:37:28 -06002883 struct nvme_ns *ns, *next;
Keith Buschf0b50732013-07-15 15:02:21 -06002884
Keith Busch5105aa52015-10-02 10:37:28 -06002885 list_for_each_entry_safe(ns, next, &dev->namespaces, list)
Keith Buscha5768aa2015-06-01 14:28:14 -06002886 nvme_ns_remove(ns);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002887}
2888
Matthew Wilcox091b6092011-02-10 09:56:01 -05002889static int nvme_setup_prp_pools(struct nvme_dev *dev)
2890{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002891 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05002892 PAGE_SIZE, PAGE_SIZE, 0);
2893 if (!dev->prp_page_pool)
2894 return -ENOMEM;
2895
Matthew Wilcox99802a72011-02-10 10:30:34 -05002896 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02002897 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05002898 256, 256, 0);
2899 if (!dev->prp_small_pool) {
2900 dma_pool_destroy(dev->prp_page_pool);
2901 return -ENOMEM;
2902 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002903 return 0;
2904}
2905
2906static void nvme_release_prp_pools(struct nvme_dev *dev)
2907{
2908 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002909 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002910}
2911
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002912static DEFINE_IDA(nvme_instance_ida);
2913
2914static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002915{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002916 int instance, error;
2917
2918 do {
2919 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2920 return -ENODEV;
2921
2922 spin_lock(&dev_list_lock);
2923 error = ida_get_new(&nvme_instance_ida, &instance);
2924 spin_unlock(&dev_list_lock);
2925 } while (error == -EAGAIN);
2926
2927 if (error)
2928 return -ENODEV;
2929
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002930 dev->ctrl.instance = instance;
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002931 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002932}
2933
2934static void nvme_release_instance(struct nvme_dev *dev)
2935{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002936 spin_lock(&dev_list_lock);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002937 ida_remove(&nvme_instance_ida, dev->ctrl.instance);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002938 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002939}
2940
Keith Busch5e82e952013-02-19 10:17:58 -07002941static void nvme_free_dev(struct kref *kref)
2942{
2943 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002944
Christoph Hellwige75ec752015-05-22 11:12:39 +02002945 put_device(dev->dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07002946 put_device(dev->device);
Indraneel M285dffc2014-12-11 08:24:18 -07002947 nvme_release_instance(dev);
Keith Busch4af0e212015-06-08 10:08:13 -06002948 if (dev->tagset.tags)
2949 blk_mq_free_tag_set(&dev->tagset);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002950 if (dev->ctrl.admin_q)
2951 blk_put_queue(dev->ctrl.admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002952 kfree(dev->queues);
2953 kfree(dev->entry);
2954 kfree(dev);
2955}
2956
2957static int nvme_dev_open(struct inode *inode, struct file *f)
2958{
Keith Buschb3fffde2015-02-03 11:21:42 -07002959 struct nvme_dev *dev;
2960 int instance = iminor(inode);
2961 int ret = -ENODEV;
2962
2963 spin_lock(&dev_list_lock);
2964 list_for_each_entry(dev, &dev_list, node) {
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002965 if (dev->ctrl.instance == instance) {
2966 if (!dev->ctrl.admin_q) {
Keith Busch2e1d8442015-02-12 15:33:00 -07002967 ret = -EWOULDBLOCK;
2968 break;
2969 }
Keith Buschb3fffde2015-02-03 11:21:42 -07002970 if (!kref_get_unless_zero(&dev->kref))
2971 break;
2972 f->private_data = dev;
2973 ret = 0;
2974 break;
2975 }
2976 }
2977 spin_unlock(&dev_list_lock);
2978
2979 return ret;
Keith Busch5e82e952013-02-19 10:17:58 -07002980}
2981
2982static int nvme_dev_release(struct inode *inode, struct file *f)
2983{
2984 struct nvme_dev *dev = f->private_data;
2985 kref_put(&dev->kref, nvme_free_dev);
2986 return 0;
2987}
2988
2989static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2990{
2991 struct nvme_dev *dev = f->private_data;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002992 struct nvme_ns *ns;
2993
Keith Busch5e82e952013-02-19 10:17:58 -07002994 switch (cmd) {
2995 case NVME_IOCTL_ADMIN_CMD:
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002996 return nvme_user_cmd(&dev->ctrl, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06002997 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002998 if (list_empty(&dev->namespaces))
2999 return -ENOTTY;
3000 ns = list_first_entry(&dev->namespaces, struct nvme_ns, list);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01003001 return nvme_user_cmd(&dev->ctrl, ns, (void __user *)arg);
Keith Busch4cc06522015-06-05 10:30:08 -06003002 case NVME_IOCTL_RESET:
3003 dev_warn(dev->dev, "resetting controller\n");
3004 return nvme_reset(dev);
Jon Derrick81f03fe2015-08-10 15:20:41 -06003005 case NVME_IOCTL_SUBSYS_RESET:
3006 return nvme_subsys_reset(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07003007 default:
3008 return -ENOTTY;
3009 }
3010}
3011
3012static const struct file_operations nvme_dev_fops = {
3013 .owner = THIS_MODULE,
3014 .open = nvme_dev_open,
3015 .release = nvme_dev_release,
3016 .unlocked_ioctl = nvme_dev_ioctl,
3017 .compat_ioctl = nvme_dev_ioctl,
3018};
3019
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02003020static void nvme_probe_work(struct work_struct *work)
Keith Buschf0b50732013-07-15 15:02:21 -06003021{
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02003022 struct nvme_dev *dev = container_of(work, struct nvme_dev, probe_work);
Dan McLeranb9afca32014-04-07 17:10:11 -06003023 bool start_thread = false;
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02003024 int result;
Keith Buschf0b50732013-07-15 15:02:21 -06003025
3026 result = nvme_dev_map(dev);
3027 if (result)
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02003028 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06003029
3030 result = nvme_configure_admin_queue(dev);
3031 if (result)
3032 goto unmap;
3033
3034 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06003035 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
3036 start_thread = true;
3037 nvme_thread = NULL;
3038 }
Keith Buschf0b50732013-07-15 15:02:21 -06003039 list_add(&dev->node, &dev_list);
3040 spin_unlock(&dev_list_lock);
3041
Dan McLeranb9afca32014-04-07 17:10:11 -06003042 if (start_thread) {
3043 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
Keith Busch387caa52014-09-22 13:46:19 -06003044 wake_up_all(&nvme_kthread_wait);
Dan McLeranb9afca32014-04-07 17:10:11 -06003045 } else
3046 wait_event_killable(nvme_kthread_wait, nvme_thread);
3047
3048 if (IS_ERR_OR_NULL(nvme_thread)) {
3049 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
3050 goto disable;
3051 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003052
3053 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07003054 result = nvme_alloc_admin_tags(dev);
3055 if (result)
3056 goto disable;
Dan McLeranb9afca32014-04-07 17:10:11 -06003057
Keith Buschf0b50732013-07-15 15:02:21 -06003058 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06003059 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07003060 goto free_tags;
Keith Buschf0b50732013-07-15 15:02:21 -06003061
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01003062 dev->ctrl.event_limit = 1;
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02003063
Christoph Hellwig2659e572015-10-02 18:51:31 +02003064 /*
3065 * Keep the controller around but remove all namespaces if we don't have
3066 * any working I/O queue.
3067 */
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02003068 if (dev->online_queues < 2) {
3069 dev_warn(dev->dev, "IO queues not created\n");
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02003070 nvme_dev_remove(dev);
3071 } else {
3072 nvme_unfreeze_queues(dev);
3073 nvme_dev_add(dev);
3074 }
3075
3076 return;
Keith Buschf0b50732013-07-15 15:02:21 -06003077
Keith Busch0fb59cb2015-01-07 18:55:50 -07003078 free_tags:
3079 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01003080 blk_put_queue(dev->ctrl.admin_q);
3081 dev->ctrl.admin_q = NULL;
Keith Busch4af0e212015-06-08 10:08:13 -06003082 dev->queues[0]->tags = NULL;
Keith Buschf0b50732013-07-15 15:02:21 -06003083 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05003084 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06003085 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06003086 unmap:
3087 nvme_dev_unmap(dev);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02003088 out:
3089 if (!work_busy(&dev->reset_work))
3090 nvme_dead_ctrl(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06003091}
3092
Keith Busch9a6b9452013-12-10 13:10:36 -07003093static int nvme_remove_dead_ctrl(void *arg)
3094{
3095 struct nvme_dev *dev = (struct nvme_dev *)arg;
Christoph Hellwige75ec752015-05-22 11:12:39 +02003096 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003097
3098 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06003099 pci_stop_and_remove_bus_device_locked(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003100 kref_put(&dev->kref, nvme_free_dev);
3101 return 0;
3102}
3103
Keith Buschde3eff22015-06-18 13:36:39 -06003104static void nvme_dead_ctrl(struct nvme_dev *dev)
3105{
3106 dev_warn(dev->dev, "Device failed to resume\n");
3107 kref_get(&dev->kref);
3108 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01003109 dev->ctrl.instance))) {
Keith Buschde3eff22015-06-18 13:36:39 -06003110 dev_err(dev->dev,
3111 "Failed to start controller remove task\n");
3112 kref_put(&dev->kref, nvme_free_dev);
3113 }
3114}
3115
Christoph Hellwig77b50d92015-10-02 17:41:18 +02003116static void nvme_reset_work(struct work_struct *ws)
Keith Busch9a6b9452013-12-10 13:10:36 -07003117{
Christoph Hellwig77b50d92015-10-02 17:41:18 +02003118 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
Keith Buschffe77042015-06-08 10:08:15 -06003119 bool in_probe = work_busy(&dev->probe_work);
3120
Keith Busch9a6b9452013-12-10 13:10:36 -07003121 nvme_dev_shutdown(dev);
Keith Buschffe77042015-06-08 10:08:15 -06003122
3123 /* Synchronize with device probe so that work will see failure status
3124 * and exit gracefully without trying to schedule another reset */
3125 flush_work(&dev->probe_work);
3126
3127 /* Fail this device if reset occured during probe to avoid
3128 * infinite initialization loops. */
3129 if (in_probe) {
Keith Buschde3eff22015-06-18 13:36:39 -06003130 nvme_dead_ctrl(dev);
Keith Buschffe77042015-06-08 10:08:15 -06003131 return;
Keith Busch9a6b9452013-12-10 13:10:36 -07003132 }
Keith Buschffe77042015-06-08 10:08:15 -06003133 /* Schedule device resume asynchronously so the reset work is available
3134 * to cleanup errors that may occur during reinitialization */
3135 schedule_work(&dev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07003136}
3137
Christoph Hellwig906678922015-10-02 18:49:23 +02003138static int __nvme_reset(struct nvme_dev *dev)
Keith Busch9a6b9452013-12-10 13:10:36 -07003139{
Christoph Hellwig906678922015-10-02 18:49:23 +02003140 if (work_pending(&dev->reset_work))
3141 return -EBUSY;
3142 list_del_init(&dev->node);
3143 queue_work(nvme_workq, &dev->reset_work);
3144 return 0;
Tejun Heo9ca97372014-03-07 10:24:49 -05003145}
3146
Keith Busch4cc06522015-06-05 10:30:08 -06003147static int nvme_reset(struct nvme_dev *dev)
3148{
Christoph Hellwig906678922015-10-02 18:49:23 +02003149 int ret;
Keith Busch4cc06522015-06-05 10:30:08 -06003150
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01003151 if (!dev->ctrl.admin_q || blk_queue_dying(dev->ctrl.admin_q))
Keith Busch4cc06522015-06-05 10:30:08 -06003152 return -ENODEV;
3153
3154 spin_lock(&dev_list_lock);
Christoph Hellwig906678922015-10-02 18:49:23 +02003155 ret = __nvme_reset(dev);
Keith Busch4cc06522015-06-05 10:30:08 -06003156 spin_unlock(&dev_list_lock);
3157
3158 if (!ret) {
3159 flush_work(&dev->reset_work);
Keith Buschffe77042015-06-08 10:08:15 -06003160 flush_work(&dev->probe_work);
Keith Busch4cc06522015-06-05 10:30:08 -06003161 return 0;
3162 }
3163
3164 return ret;
3165}
3166
3167static ssize_t nvme_sysfs_reset(struct device *dev,
3168 struct device_attribute *attr, const char *buf,
3169 size_t count)
3170{
3171 struct nvme_dev *ndev = dev_get_drvdata(dev);
3172 int ret;
3173
3174 ret = nvme_reset(ndev);
3175 if (ret < 0)
3176 return ret;
3177
3178 return count;
3179}
3180static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
3181
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01003182static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
3183{
3184 *val = readl(to_nvme_dev(ctrl)->bar + off);
3185 return 0;
3186}
3187
3188static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
3189 .reg_read32 = nvme_pci_reg_read32,
3190};
3191
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003192static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003193{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003194 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003195 struct nvme_dev *dev;
3196
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003197 node = dev_to_node(&pdev->dev);
3198 if (node == NUMA_NO_NODE)
3199 set_dev_node(&pdev->dev, 0);
3200
3201 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003202 if (!dev)
3203 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003204 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
3205 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003206 if (!dev->entry)
3207 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003208 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
3209 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003210 if (!dev->queues)
3211 goto free;
3212
3213 INIT_LIST_HEAD(&dev->namespaces);
Christoph Hellwig77b50d92015-10-02 17:41:18 +02003214 INIT_WORK(&dev->reset_work, nvme_reset_work);
Christoph Hellwige75ec752015-05-22 11:12:39 +02003215 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003216 pci_set_drvdata(pdev, dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01003217
3218 dev->ctrl.ops = &nvme_pci_ctrl_ops;
3219 dev->ctrl.dev = dev->dev;
3220
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07003221 result = nvme_set_instance(dev);
3222 if (result)
Keith Buscha96d4f52014-08-19 19:15:59 -06003223 goto put_pci;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003224
Matthew Wilcox091b6092011-02-10 09:56:01 -05003225 result = nvme_setup_prp_pools(dev);
3226 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06003227 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05003228
Keith Buschfb35e912014-03-03 11:09:47 -07003229 kref_init(&dev->kref);
Keith Buschb3fffde2015-02-03 11:21:42 -07003230 dev->device = device_create(nvme_class, &pdev->dev,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01003231 MKDEV(nvme_char_major, dev->ctrl.instance),
3232 dev, "nvme%d", dev->ctrl.instance);
Keith Buschb3fffde2015-02-03 11:21:42 -07003233 if (IS_ERR(dev->device)) {
3234 result = PTR_ERR(dev->device);
Keith Busch2e1d8442015-02-12 15:33:00 -07003235 goto release_pools;
Keith Buschb3fffde2015-02-03 11:21:42 -07003236 }
3237 get_device(dev->device);
Keith Busch4cc06522015-06-05 10:30:08 -06003238 dev_set_drvdata(dev->device, dev);
3239
3240 result = device_create_file(dev->device, &dev_attr_reset_controller);
3241 if (result)
3242 goto put_dev;
Keith Buschb3fffde2015-02-03 11:21:42 -07003243
Keith Busche6e96d72015-03-23 09:32:37 -06003244 INIT_LIST_HEAD(&dev->node);
Keith Buscha5768aa2015-06-01 14:28:14 -06003245 INIT_WORK(&dev->scan_work, nvme_dev_scan);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02003246 INIT_WORK(&dev->probe_work, nvme_probe_work);
Keith Busch2e1d8442015-02-12 15:33:00 -07003247 schedule_work(&dev->probe_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003248 return 0;
3249
Keith Busch4cc06522015-06-05 10:30:08 -06003250 put_dev:
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01003251 device_destroy(nvme_class, MKDEV(nvme_char_major, dev->ctrl.instance));
Keith Busch4cc06522015-06-05 10:30:08 -06003252 put_device(dev->device);
Keith Busch0877cb02013-07-15 15:02:19 -06003253 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05003254 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06003255 release:
3256 nvme_release_instance(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06003257 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02003258 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003259 free:
3260 kfree(dev->queues);
3261 kfree(dev->entry);
3262 kfree(dev);
3263 return result;
3264}
3265
Keith Buschf0d54a52014-05-02 10:40:43 -06003266static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
3267{
Keith Buscha6739472014-06-23 16:03:21 -06003268 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06003269
Keith Buscha6739472014-06-23 16:03:21 -06003270 if (prepare)
3271 nvme_dev_shutdown(dev);
3272 else
Keith Busch0a7385a2015-10-02 10:37:29 -06003273 schedule_work(&dev->probe_work);
Keith Buschf0d54a52014-05-02 10:40:43 -06003274}
3275
Keith Busch09ece142014-01-27 11:29:40 -05003276static void nvme_shutdown(struct pci_dev *pdev)
3277{
3278 struct nvme_dev *dev = pci_get_drvdata(pdev);
3279 nvme_dev_shutdown(dev);
3280}
3281
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003282static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003283{
3284 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003285
3286 spin_lock(&dev_list_lock);
3287 list_del_init(&dev->node);
3288 spin_unlock(&dev_list_lock);
3289
3290 pci_set_drvdata(pdev, NULL);
Keith Busch2e1d8442015-02-12 15:33:00 -07003291 flush_work(&dev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07003292 flush_work(&dev->reset_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06003293 flush_work(&dev->scan_work);
Keith Busch4cc06522015-06-05 10:30:08 -06003294 device_remove_file(dev->device, &dev_attr_reset_controller);
Keith Buschc9d3bf82015-01-07 18:55:52 -07003295 nvme_dev_remove(dev);
Keith Busch3399a3f2015-06-18 13:36:40 -06003296 nvme_dev_shutdown(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003297 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01003298 device_destroy(nvme_class, MKDEV(nvme_char_major, dev->ctrl.instance));
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003299 nvme_free_queues(dev, 0);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06003300 nvme_release_cmb(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003301 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07003302 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003303}
3304
3305/* These functions are yet to be implemented */
3306#define nvme_error_detected NULL
3307#define nvme_dump_registers NULL
3308#define nvme_link_reset NULL
3309#define nvme_slot_reset NULL
3310#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06003311
Jingoo Han671a6012014-02-13 11:19:14 +09003312#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06003313static int nvme_suspend(struct device *dev)
3314{
3315 struct pci_dev *pdev = to_pci_dev(dev);
3316 struct nvme_dev *ndev = pci_get_drvdata(pdev);
3317
3318 nvme_dev_shutdown(ndev);
3319 return 0;
3320}
3321
3322static int nvme_resume(struct device *dev)
3323{
3324 struct pci_dev *pdev = to_pci_dev(dev);
3325 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06003326
Keith Busch0a7385a2015-10-02 10:37:29 -06003327 schedule_work(&ndev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07003328 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06003329}
Jingoo Han671a6012014-02-13 11:19:14 +09003330#endif
Keith Buschcd638942013-07-15 15:02:23 -06003331
3332static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003333
Stephen Hemminger1d352032012-09-07 09:33:17 -07003334static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003335 .error_detected = nvme_error_detected,
3336 .mmio_enabled = nvme_dump_registers,
3337 .link_reset = nvme_link_reset,
3338 .slot_reset = nvme_slot_reset,
3339 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06003340 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003341};
3342
3343/* Move to pci_ids.h later */
3344#define PCI_CLASS_STORAGE_EXPRESS 0x010802
3345
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003346static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003347 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
Stephan Güntherc74dc782015-11-04 00:49:45 +01003348 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003349 { 0, }
3350};
3351MODULE_DEVICE_TABLE(pci, nvme_id_table);
3352
3353static struct pci_driver nvme_driver = {
3354 .name = "nvme",
3355 .id_table = nvme_id_table,
3356 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003357 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05003358 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06003359 .driver = {
3360 .pm = &nvme_dev_pm_ops,
3361 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003362 .err_handler = &nvme_err_handler,
3363};
3364
3365static int __init nvme_init(void)
3366{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003367 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003368
Dan McLeranb9afca32014-04-07 17:10:11 -06003369 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003370
Keith Busch9a6b9452013-12-10 13:10:36 -07003371 nvme_workq = create_singlethread_workqueue("nvme");
3372 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06003373 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07003374
Keith Busch5c42ea12012-07-25 16:05:18 -06003375 result = register_blkdev(nvme_major, "nvme");
3376 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07003377 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06003378 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003379 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003380
Keith Buschb3fffde2015-02-03 11:21:42 -07003381 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
3382 &nvme_dev_fops);
3383 if (result < 0)
3384 goto unregister_blkdev;
3385 else if (result > 0)
3386 nvme_char_major = result;
3387
3388 nvme_class = class_create(THIS_MODULE, "nvme");
Alexey Khoroshilovc7270402015-03-07 01:43:41 +03003389 if (IS_ERR(nvme_class)) {
3390 result = PTR_ERR(nvme_class);
Keith Buschb3fffde2015-02-03 11:21:42 -07003391 goto unregister_chrdev;
Alexey Khoroshilovc7270402015-03-07 01:43:41 +03003392 }
Keith Buschb3fffde2015-02-03 11:21:42 -07003393
Keith Buschf3db22f2014-06-11 11:51:35 -06003394 result = pci_register_driver(&nvme_driver);
3395 if (result)
Keith Buschb3fffde2015-02-03 11:21:42 -07003396 goto destroy_class;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003397 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003398
Keith Buschb3fffde2015-02-03 11:21:42 -07003399 destroy_class:
3400 class_destroy(nvme_class);
3401 unregister_chrdev:
3402 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003403 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003404 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003405 kill_workq:
3406 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003407 return result;
3408}
3409
3410static void __exit nvme_exit(void)
3411{
3412 pci_unregister_driver(&nvme_driver);
3413 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003414 destroy_workqueue(nvme_workq);
Keith Buschb3fffde2015-02-03 11:21:42 -07003415 class_destroy(nvme_class);
3416 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Dan McLeranb9afca32014-04-07 17:10:11 -06003417 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04003418 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003419}
3420
3421MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
3422MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07003423MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003424module_init(nvme_init);
3425module_exit(nvme_exit);