blob: 513908ff46c4b34129ba6f17f6eaddc1c23ccd06 [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
15#include <linux/nvme.h>
Matthew Wilcox8de05532011-05-12 13:50:28 -040016#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050017#include <linux/blkdev.h>
Matias Bjørlinga4aea562014-11-04 08:20:14 -070018#include <linux/blk-mq.h>
Keith Busch42f61422014-03-24 10:46:25 -060019#include <linux/cpu.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040020#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050021#include <linux/errno.h>
22#include <linux/fs.h>
23#include <linux/genhd.h>
Keith Busch4cc09e22014-04-02 15:45:37 -060024#include <linux/hdreg.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040025#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050026#include <linux/init.h>
27#include <linux/interrupt.h>
28#include <linux/io.h>
29#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050030#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050031#include <linux/kernel.h>
32#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>
Vishal Verma5d0f6132013-03-04 18:40:58 -070042#include <scsi/sg.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090043#include <asm-generic/io-64-nonatomic-lo-hi.h>
44
Keith Buschb3fffde2015-02-03 11:21:42 -070045#define NVME_MINORS (1U << MINORBITS)
Keith Busch9d43cf62014-05-13 11:42:02 -060046#define NVME_Q_DEPTH 1024
Jens Axboed31af0a2015-03-06 12:56:13 -070047#define NVME_AQ_DEPTH 256
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050048#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
49#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Keith Busch9d43cf62014-05-13 11:42:02 -060050#define ADMIN_TIMEOUT (admin_timeout * HZ)
Dan McLeran2484f402014-07-01 09:33:32 -060051#define SHUTDOWN_TIMEOUT (shutdown_timeout * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050052
Keith Busch9d43cf62014-05-13 11:42:02 -060053static unsigned char admin_timeout = 60;
54module_param(admin_timeout, byte, 0644);
55MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050056
Matthew Wilcoxbd676082014-06-03 23:04:30 -040057unsigned char nvme_io_timeout = 30;
58module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
Keith Buschb3550842014-04-04 11:43:36 -060059MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050060
Dan McLeran2484f402014-07-01 09:33:32 -060061static unsigned char shutdown_timeout = 5;
62module_param(shutdown_timeout, byte, 0644);
63MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
64
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050065static int nvme_major;
66module_param(nvme_major, int, 0);
67
Keith Buschb3fffde2015-02-03 11:21:42 -070068static int nvme_char_major;
69module_param(nvme_char_major, int, 0);
70
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050071static int use_threaded_interrupts;
72module_param(use_threaded_interrupts, int, 0);
73
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050074static DEFINE_SPINLOCK(dev_list_lock);
75static LIST_HEAD(dev_list);
76static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070077static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060078static wait_queue_head_t nvme_kthread_wait;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050079
Keith Buschb3fffde2015-02-03 11:21:42 -070080static struct class *nvme_class;
81
Keith Buschd4b4ff82013-12-10 13:10:37 -070082static void nvme_reset_failed_dev(struct work_struct *ws);
Matias Bjørlinga4aea562014-11-04 08:20:14 -070083static int nvme_process_cq(struct nvme_queue *nvmeq);
Keith Buschd4b4ff82013-12-10 13:10:37 -070084
Keith Busch4d115422013-12-10 13:10:40 -070085struct async_cmd_info {
86 struct kthread_work work;
87 struct kthread_worker *worker;
Matias Bjørlinga4aea562014-11-04 08:20:14 -070088 struct request *req;
Keith Busch4d115422013-12-10 13:10:40 -070089 u32 result;
90 int status;
91 void *ctx;
92};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050093
94/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050095 * An NVM Express queue. Each device has at least two (one for admin
96 * commands and one for I/O commands).
97 */
98struct nvme_queue {
99 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500100 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500101 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500102 spinlock_t q_lock;
103 struct nvme_command *sq_cmds;
104 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600105 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500106 dma_addr_t sq_dma_addr;
107 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500108 u32 __iomem *q_db;
109 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700110 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500111 u16 sq_head;
112 u16 sq_tail;
113 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700114 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400115 u8 cq_phase;
116 u8 cqe_seen;
Keith Busch4d115422013-12-10 13:10:40 -0700117 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500118};
119
120/*
121 * Check we didin't inadvertently grow the command struct
122 */
123static inline void _nvme_check_size(void)
124{
125 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
126 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
127 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
128 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
129 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400130 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700131 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500132 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
133 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
134 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
135 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600136 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500137}
138
Keith Buschedd10d32014-04-03 16:45:23 -0600139typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400140 struct nvme_completion *);
141
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500142struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400143 nvme_completion_fn fn;
144 void *ctx;
Keith Buschc30341d2013-12-10 13:10:38 -0700145 int aborted;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700146 struct nvme_queue *nvmeq;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700147 struct nvme_iod iod[0];
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500148};
149
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700150/*
151 * Max size of iod being embedded in the request payload
152 */
153#define NVME_INT_PAGES 2
154#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->page_size)
Chong Yuanfda631f2015-03-27 09:21:32 +0800155#define NVME_INT_MASK 0x01
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700156
157/*
158 * Will slightly overestimate the number of pages needed. This is OK
159 * as it only leads to a small amount of wasted memory for the lifetime of
160 * the I/O.
161 */
162static int nvme_npages(unsigned size, struct nvme_dev *dev)
163{
164 unsigned nprps = DIV_ROUND_UP(size + dev->page_size, dev->page_size);
165 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
166}
167
168static unsigned int nvme_cmd_size(struct nvme_dev *dev)
169{
170 unsigned int ret = sizeof(struct nvme_cmd_info);
171
172 ret += sizeof(struct nvme_iod);
173 ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
174 ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
175
176 return ret;
177}
178
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700179static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
180 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500181{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700182 struct nvme_dev *dev = data;
183 struct nvme_queue *nvmeq = dev->queues[0];
184
Keith Busch42483222015-06-01 09:29:54 -0600185 WARN_ON(hctx_idx != 0);
186 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
187 WARN_ON(nvmeq->tags);
188
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700189 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600190 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700191 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500192}
193
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700194static int nvme_admin_init_request(void *data, struct request *req,
195 unsigned int hctx_idx, unsigned int rq_idx,
196 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600197{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700198 struct nvme_dev *dev = data;
199 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
200 struct nvme_queue *nvmeq = dev->queues[0];
201
202 BUG_ON(!nvmeq);
203 cmd->nvmeq = nvmeq;
204 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600205}
206
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700207static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
208 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500209{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700210 struct nvme_dev *dev = data;
Keith Busch42483222015-06-01 09:29:54 -0600211 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500212
Keith Busch42483222015-06-01 09:29:54 -0600213 if (!nvmeq->tags)
214 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500215
Keith Busch42483222015-06-01 09:29:54 -0600216 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700217 hctx->driver_data = nvmeq;
218 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500219}
220
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700221static int nvme_init_request(void *data, struct request *req,
222 unsigned int hctx_idx, unsigned int rq_idx,
223 unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500224{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700225 struct nvme_dev *dev = data;
226 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
227 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
228
229 BUG_ON(!nvmeq);
230 cmd->nvmeq = nvmeq;
231 return 0;
232}
233
234static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
235 nvme_completion_fn handler)
236{
237 cmd->fn = handler;
238 cmd->ctx = ctx;
239 cmd->aborted = 0;
Keith Buschc917dfe2015-01-07 18:55:48 -0700240 blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500241}
242
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700243static void *iod_get_private(struct nvme_iod *iod)
244{
245 return (void *) (iod->private & ~0x1UL);
246}
247
248/*
249 * If bit 0 is set, the iod is embedded in the request payload.
250 */
251static bool iod_should_kfree(struct nvme_iod *iod)
252{
Chong Yuanfda631f2015-03-27 09:21:32 +0800253 return (iod->private & NVME_INT_MASK) == 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700254}
255
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400256/* Special values must be less than 0x1000 */
257#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500258#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
259#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
260#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500261
Keith Buschedd10d32014-04-03 16:45:23 -0600262static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400263 struct nvme_completion *cqe)
264{
265 if (ctx == CMD_CTX_CANCELLED)
266 return;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400267 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600268 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400269 "completed id %d twice on queue %d\n",
270 cqe->command_id, le16_to_cpup(&cqe->sq_id));
271 return;
272 }
273 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600274 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400275 "invalid id %d completed on queue %d\n",
276 cqe->command_id, le16_to_cpup(&cqe->sq_id));
277 return;
278 }
Keith Buschedd10d32014-04-03 16:45:23 -0600279 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400280}
281
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700282static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
283{
284 void *ctx;
285
286 if (fn)
287 *fn = cmd->fn;
288 ctx = cmd->ctx;
289 cmd->fn = special_completion;
290 cmd->ctx = CMD_CTX_CANCELLED;
291 return ctx;
292}
293
294static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
295 struct nvme_completion *cqe)
296{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700297 u32 result = le32_to_cpup(&cqe->result);
298 u16 status = le16_to_cpup(&cqe->status) >> 1;
299
300 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
301 ++nvmeq->dev->event_limit;
302 if (status == NVME_SC_SUCCESS)
303 dev_warn(nvmeq->q_dmadev,
304 "async event result %08x\n", result);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700305}
306
307static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
308 struct nvme_completion *cqe)
309{
310 struct request *req = ctx;
311
312 u16 status = le16_to_cpup(&cqe->status) >> 1;
313 u32 result = le32_to_cpup(&cqe->result);
314
Keith Busch42483222015-06-01 09:29:54 -0600315 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700316
317 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
318 ++nvmeq->dev->abort_limit;
319}
320
Keith Buschedd10d32014-04-03 16:45:23 -0600321static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700322 struct nvme_completion *cqe)
323{
324 struct async_cmd_info *cmdinfo = ctx;
325 cmdinfo->result = le32_to_cpup(&cqe->result);
326 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
327 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
Keith Busch42483222015-06-01 09:29:54 -0600328 blk_mq_free_request(cmdinfo->req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700329}
330
331static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
332 unsigned int tag)
333{
Keith Busch42483222015-06-01 09:29:54 -0600334 struct request *req = blk_mq_tag_to_rq(*nvmeq->tags, tag);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700335
336 return blk_mq_rq_to_pdu(req);
Keith Busch4d115422013-12-10 13:10:40 -0700337}
338
Matthew Wilcox184d2942011-05-11 21:36:38 -0400339/*
340 * Called with local interrupts disabled and the q_lock held. May not sleep.
341 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700342static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400343 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500344{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700345 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400346 void *ctx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700347 if (tag >= nvmeq->q_depth) {
348 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500349 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400350 }
Keith Busch859361a2012-08-02 14:05:59 -0600351 if (fn)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700352 *fn = cmd->fn;
353 ctx = cmd->ctx;
354 cmd->fn = special_completion;
355 cmd->ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400356 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500357}
358
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500359/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400360 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500361 * @nvmeq: The queue to use
362 * @cmd: The command to send
363 *
364 * Safe to use from interrupt context
365 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700366static int __nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500367{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700368 u16 tail = nvmeq->sq_tail;
369
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500370 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500371 if (++tail == nvmeq->q_depth)
372 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500373 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500374 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500375
376 return 0;
377}
378
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700379static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
380{
381 unsigned long flags;
382 int ret;
383 spin_lock_irqsave(&nvmeq->q_lock, flags);
384 ret = __nvme_submit_cmd(nvmeq, cmd);
385 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
386 return ret;
387}
388
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500389static __le64 **iod_list(struct nvme_iod *iod)
390{
391 return ((void *)iod) + iod->offset;
392}
393
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700394static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
395 unsigned nseg, unsigned long private)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500396{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700397 iod->private = private;
398 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
399 iod->npages = -1;
400 iod->length = nbytes;
401 iod->nents = 0;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500402}
403
404static struct nvme_iod *
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700405__nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
406 unsigned long priv, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500407{
408 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700409 sizeof(__le64 *) * nvme_npages(bytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500410 sizeof(struct scatterlist) * nseg, gfp);
411
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700412 if (iod)
413 iod_init(iod, bytes, nseg, priv);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500414
415 return iod;
416}
417
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700418static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
419 gfp_t gfp)
420{
421 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
422 sizeof(struct nvme_dsm_range);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700423 struct nvme_iod *iod;
424
425 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
426 size <= NVME_INT_BYTES(dev)) {
427 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
428
429 iod = cmd->iod;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700430 iod_init(iod, size, rq->nr_phys_segments,
Chong Yuanfda631f2015-03-27 09:21:32 +0800431 (unsigned long) rq | NVME_INT_MASK);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700432 return iod;
433 }
434
435 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
436 (unsigned long) rq, gfp);
437}
438
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200439static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500440{
Keith Busch1d090622014-06-23 11:34:01 -0600441 const int last_prp = dev->page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500442 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500443 __le64 **list = iod_list(iod);
444 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500445
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500446 if (iod->npages == 0)
447 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
448 for (i = 0; i < iod->npages; i++) {
449 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500450 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500451 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500452 prp_dma = next_prp_dma;
453 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700454
455 if (iod_should_kfree(iod))
456 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500457}
458
Keith Buschb4ff9c82014-08-29 09:06:12 -0600459static int nvme_error_status(u16 status)
460{
461 switch (status & 0x7ff) {
462 case NVME_SC_SUCCESS:
463 return 0;
464 case NVME_SC_CAP_EXCEEDED:
465 return -ENOSPC;
466 default:
467 return -EIO;
468 }
469}
470
Keith Busch52b68d72015-02-23 09:16:21 -0700471#ifdef CONFIG_BLK_DEV_INTEGRITY
Keith Busche1e5e562015-02-19 13:39:03 -0700472static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
473{
474 if (be32_to_cpu(pi->ref_tag) == v)
475 pi->ref_tag = cpu_to_be32(p);
476}
477
478static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
479{
480 if (be32_to_cpu(pi->ref_tag) == p)
481 pi->ref_tag = cpu_to_be32(v);
482}
483
484/**
485 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
486 *
487 * The virtual start sector is the one that was originally submitted by the
488 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
489 * start sector may be different. Remap protection information to match the
490 * physical LBA on writes, and back to the original seed on reads.
491 *
492 * Type 0 and 3 do not have a ref tag, so no remapping required.
493 */
494static void nvme_dif_remap(struct request *req,
495 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
496{
497 struct nvme_ns *ns = req->rq_disk->private_data;
498 struct bio_integrity_payload *bip;
499 struct t10_pi_tuple *pi;
500 void *p, *pmap;
501 u32 i, nlb, ts, phys, virt;
502
503 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
504 return;
505
506 bip = bio_integrity(req->bio);
507 if (!bip)
508 return;
509
510 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
Keith Busche1e5e562015-02-19 13:39:03 -0700511
512 p = pmap;
513 virt = bip_get_seed(bip);
514 phys = nvme_block_nr(ns, blk_rq_pos(req));
515 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
516 ts = ns->disk->integrity->tuple_size;
517
518 for (i = 0; i < nlb; i++, virt++, phys++) {
519 pi = (struct t10_pi_tuple *)p;
520 dif_swap(phys, virt, pi);
521 p += ts;
522 }
523 kunmap_atomic(pmap);
524}
525
Keith Busch52b68d72015-02-23 09:16:21 -0700526static int nvme_noop_verify(struct blk_integrity_iter *iter)
527{
528 return 0;
529}
530
531static int nvme_noop_generate(struct blk_integrity_iter *iter)
532{
533 return 0;
534}
535
536struct blk_integrity nvme_meta_noop = {
537 .name = "NVME_META_NOOP",
538 .generate_fn = nvme_noop_generate,
539 .verify_fn = nvme_noop_verify,
540};
541
542static void nvme_init_integrity(struct nvme_ns *ns)
543{
544 struct blk_integrity integrity;
545
546 switch (ns->pi_type) {
547 case NVME_NS_DPS_PI_TYPE3:
548 integrity = t10_pi_type3_crc;
549 break;
550 case NVME_NS_DPS_PI_TYPE1:
551 case NVME_NS_DPS_PI_TYPE2:
552 integrity = t10_pi_type1_crc;
553 break;
554 default:
555 integrity = nvme_meta_noop;
556 break;
557 }
558 integrity.tuple_size = ns->ms;
559 blk_integrity_register(ns->disk, &integrity);
560 blk_queue_max_integrity_segments(ns->queue, 1);
561}
562#else /* CONFIG_BLK_DEV_INTEGRITY */
563static void nvme_dif_remap(struct request *req,
564 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
565{
566}
567static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
568{
569}
570static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
571{
572}
573static void nvme_init_integrity(struct nvme_ns *ns)
574{
575}
576#endif
577
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700578static void req_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500579 struct nvme_completion *cqe)
580{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500581 struct nvme_iod *iod = ctx;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700582 struct request *req = iod_get_private(iod);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700583 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
584
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500585 u16 status = le16_to_cpup(&cqe->status) >> 1;
586
Keith Buschedd10d32014-04-03 16:45:23 -0600587 if (unlikely(status)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700588 if (!(status & NVME_SC_DNR || blk_noretry_request(req))
589 && (jiffies - req->start_time) < req->timeout) {
Keith Buschc9d3bf82015-01-07 18:55:52 -0700590 unsigned long flags;
591
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700592 blk_mq_requeue_request(req);
Keith Buschc9d3bf82015-01-07 18:55:52 -0700593 spin_lock_irqsave(req->q->queue_lock, flags);
594 if (!blk_queue_stopped(req->q))
595 blk_mq_kick_requeue_list(req->q);
596 spin_unlock_irqrestore(req->q->queue_lock, flags);
Keith Buschedd10d32014-04-03 16:45:23 -0600597 return;
598 }
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200599 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200600 req->errors = status;
601 } else {
602 req->errors = nvme_error_status(status);
603 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700604 } else
605 req->errors = 0;
Keith Buscha0a931d2015-05-22 12:28:31 -0600606 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
607 u32 result = le32_to_cpup(&cqe->result);
608 req->special = (void *)(uintptr_t)result;
609 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700610
611 if (cmd_rq->aborted)
Christoph Hellwige75ec752015-05-22 11:12:39 +0200612 dev_warn(nvmeq->dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700613 "completing aborted command with status:%04x\n",
614 status);
615
Keith Busche1e5e562015-02-19 13:39:03 -0700616 if (iod->nents) {
Christoph Hellwige75ec752015-05-22 11:12:39 +0200617 dma_unmap_sg(nvmeq->dev->dev, iod->sg, iod->nents,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700618 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busche1e5e562015-02-19 13:39:03 -0700619 if (blk_integrity_rq(req)) {
620 if (!rq_data_dir(req))
621 nvme_dif_remap(req, nvme_dif_complete);
Christoph Hellwige75ec752015-05-22 11:12:39 +0200622 dma_unmap_sg(nvmeq->dev->dev, iod->meta_sg, 1,
Keith Busche1e5e562015-02-19 13:39:03 -0700623 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
624 }
625 }
Keith Buschedd10d32014-04-03 16:45:23 -0600626 nvme_free_iod(nvmeq->dev, iod);
Keith Busch3291fa52014-04-28 12:30:52 -0600627
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700628 blk_mq_complete_request(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500629}
630
Matthew Wilcox184d2942011-05-11 21:36:38 -0400631/* length is in bytes. gfp flags indicates whether we may sleep. */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200632static int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod,
633 int total_len, gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500634{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500635 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500636 int length = total_len;
637 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500638 int dma_len = sg_dma_len(sg);
639 u64 dma_addr = sg_dma_address(sg);
Murali Iyerf137e0f2015-03-26 11:07:51 -0500640 u32 page_size = dev->page_size;
641 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500642 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500643 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500644 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500645 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500646
Keith Busch1d090622014-06-23 11:34:01 -0600647 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500648 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500649 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500650
Keith Busch1d090622014-06-23 11:34:01 -0600651 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500652 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600653 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500654 } else {
655 sg = sg_next(sg);
656 dma_addr = sg_dma_address(sg);
657 dma_len = sg_dma_len(sg);
658 }
659
Keith Busch1d090622014-06-23 11:34:01 -0600660 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600661 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500662 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500663 }
664
Keith Busch1d090622014-06-23 11:34:01 -0600665 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500666 if (nprps <= (256 / 8)) {
667 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500668 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500669 } else {
670 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500671 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500672 }
673
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400674 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
675 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600676 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500677 iod->npages = -1;
Keith Busch1d090622014-06-23 11:34:01 -0600678 return (total_len - length) + page_size;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400679 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500680 list[0] = prp_list;
681 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500682 i = 0;
683 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600684 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500685 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400686 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500687 if (!prp_list)
688 return total_len - length;
689 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400690 prp_list[0] = old_prp_list[i - 1];
691 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
692 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500693 }
694 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600695 dma_len -= page_size;
696 dma_addr += page_size;
697 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500698 if (length <= 0)
699 break;
700 if (dma_len > 0)
701 continue;
702 BUG_ON(dma_len < 0);
703 sg = sg_next(sg);
704 dma_addr = sg_dma_address(sg);
705 dma_len = sg_dma_len(sg);
706 }
707
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500708 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500709}
710
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200711static void nvme_submit_priv(struct nvme_queue *nvmeq, struct request *req,
712 struct nvme_iod *iod)
713{
714 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
715
716 memcpy(cmnd, req->cmd, sizeof(struct nvme_command));
717 cmnd->rw.command_id = req->tag;
718 if (req->nr_phys_segments) {
719 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
720 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
721 }
722
723 if (++nvmeq->sq_tail == nvmeq->q_depth)
724 nvmeq->sq_tail = 0;
725 writel(nvmeq->sq_tail, nvmeq->q_db);
726}
727
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700728/*
729 * We reuse the small pool to allocate the 16-byte range here as it is not
730 * worth having a special pool for these or additional cases to handle freeing
731 * the iod.
732 */
733static void nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
734 struct request *req, struct nvme_iod *iod)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700735{
Keith Buschedd10d32014-04-03 16:45:23 -0600736 struct nvme_dsm_range *range =
737 (struct nvme_dsm_range *)iod_list(iod)[0];
Keith Busch0e5e4f02012-11-09 16:33:05 -0700738 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
739
Keith Busch0e5e4f02012-11-09 16:33:05 -0700740 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700741 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
742 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700743
744 memset(cmnd, 0, sizeof(*cmnd));
745 cmnd->dsm.opcode = nvme_cmd_dsm;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700746 cmnd->dsm.command_id = req->tag;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700747 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
748 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
749 cmnd->dsm.nr = 0;
750 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
751
752 if (++nvmeq->sq_tail == nvmeq->q_depth)
753 nvmeq->sq_tail = 0;
754 writel(nvmeq->sq_tail, nvmeq->q_db);
Keith Busch0e5e4f02012-11-09 16:33:05 -0700755}
756
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700757static void nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500758 int cmdid)
759{
760 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
761
762 memset(cmnd, 0, sizeof(*cmnd));
763 cmnd->common.opcode = nvme_cmd_flush;
764 cmnd->common.command_id = cmdid;
765 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
766
767 if (++nvmeq->sq_tail == nvmeq->q_depth)
768 nvmeq->sq_tail = 0;
769 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500770}
771
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700772static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod,
773 struct nvme_ns *ns)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500774{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700775 struct request *req = iod_get_private(iod);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500776 struct nvme_command *cmnd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700777 u16 control = 0;
778 u32 dsmgmt = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500779
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700780 if (req->cmd_flags & REQ_FUA)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500781 control |= NVME_RW_FUA;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700782 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500783 control |= NVME_RW_LR;
784
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700785 if (req->cmd_flags & REQ_RAHEAD)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500786 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
787
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500788 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500789 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500790
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700791 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
792 cmnd->rw.command_id = req->tag;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500793 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Keith Buschedd10d32014-04-03 16:45:23 -0600794 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
795 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700796 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
797 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
Keith Busche1e5e562015-02-19 13:39:03 -0700798
799 if (blk_integrity_rq(req)) {
800 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(iod->meta_sg));
801 switch (ns->pi_type) {
802 case NVME_NS_DPS_PI_TYPE3:
803 control |= NVME_RW_PRINFO_PRCHK_GUARD;
804 break;
805 case NVME_NS_DPS_PI_TYPE1:
806 case NVME_NS_DPS_PI_TYPE2:
807 control |= NVME_RW_PRINFO_PRCHK_GUARD |
808 NVME_RW_PRINFO_PRCHK_REF;
809 cmnd->rw.reftag = cpu_to_le32(
810 nvme_block_nr(ns, blk_rq_pos(req)));
811 break;
812 }
813 } else if (ns->ms)
814 control |= NVME_RW_PRINFO_PRACT;
815
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500816 cmnd->rw.control = cpu_to_le16(control);
817 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500818
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500819 if (++nvmeq->sq_tail == nvmeq->q_depth)
820 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500821 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500822
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500823 return 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600824}
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500825
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200826/*
827 * NOTE: ns is NULL when called on the admin queue.
828 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700829static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
830 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600831{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700832 struct nvme_ns *ns = hctx->queue->queuedata;
833 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200834 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700835 struct request *req = bd->rq;
836 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
Keith Buschedd10d32014-04-03 16:45:23 -0600837 struct nvme_iod *iod;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700838 enum dma_data_direction dma_dir;
Keith Buschedd10d32014-04-03 16:45:23 -0600839
Keith Busche1e5e562015-02-19 13:39:03 -0700840 /*
841 * If formated with metadata, require the block layer provide a buffer
842 * unless this namespace is formated such that the metadata can be
843 * stripped/generated by the controller with PRACT=1.
844 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200845 if (ns && ns->ms && !blk_integrity_rq(req)) {
Keith Busche1e5e562015-02-19 13:39:03 -0700846 if (!(ns->pi_type && ns->ms == 8)) {
847 req->errors = -EFAULT;
848 blk_mq_complete_request(req);
849 return BLK_MQ_RQ_QUEUE_OK;
850 }
851 }
852
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200853 iod = nvme_alloc_iod(req, dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600854 if (!iod)
Jens Axboefe543032014-12-11 13:58:39 -0700855 return BLK_MQ_RQ_QUEUE_BUSY;
Keith Buschedd10d32014-04-03 16:45:23 -0600856
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700857 if (req->cmd_flags & REQ_DISCARD) {
Keith Buschedd10d32014-04-03 16:45:23 -0600858 void *range;
859 /*
860 * We reuse the small pool to allocate the 16-byte range here
861 * as it is not worth having a special pool for these or
862 * additional cases to handle freeing the iod.
863 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200864 range = dma_pool_alloc(dev->prp_small_pool, GFP_ATOMIC,
Keith Buschedd10d32014-04-03 16:45:23 -0600865 &iod->first_dma);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700866 if (!range)
Jens Axboefe543032014-12-11 13:58:39 -0700867 goto retry_cmd;
Keith Buschedd10d32014-04-03 16:45:23 -0600868 iod_list(iod)[0] = (__le64 *)range;
869 iod->npages = 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700870 } else if (req->nr_phys_segments) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700871 dma_dir = rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
Keith Buschedd10d32014-04-03 16:45:23 -0600872
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700873 sg_init_table(iod->sg, req->nr_phys_segments);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700874 iod->nents = blk_rq_map_sg(req->q, req, iod->sg);
Jens Axboefe543032014-12-11 13:58:39 -0700875 if (!iod->nents)
876 goto error_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700877
878 if (!dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir))
Jens Axboefe543032014-12-11 13:58:39 -0700879 goto retry_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700880
Jens Axboefe543032014-12-11 13:58:39 -0700881 if (blk_rq_bytes(req) !=
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200882 nvme_setup_prps(dev, iod, blk_rq_bytes(req), GFP_ATOMIC)) {
883 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
Jens Axboefe543032014-12-11 13:58:39 -0700884 goto retry_cmd;
885 }
Keith Busche1e5e562015-02-19 13:39:03 -0700886 if (blk_integrity_rq(req)) {
887 if (blk_rq_count_integrity_sg(req->q, req->bio) != 1)
888 goto error_cmd;
889
890 sg_init_table(iod->meta_sg, 1);
891 if (blk_rq_map_integrity_sg(
892 req->q, req->bio, iod->meta_sg) != 1)
893 goto error_cmd;
894
895 if (rq_data_dir(req))
896 nvme_dif_remap(req, nvme_dif_prep);
897
898 if (!dma_map_sg(nvmeq->q_dmadev, iod->meta_sg, 1, dma_dir))
899 goto error_cmd;
900 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700901 }
902
Keith Busch9af87852014-12-03 17:07:13 -0700903 nvme_set_info(cmd, iod, req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700904 spin_lock_irq(&nvmeq->q_lock);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200905 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
906 nvme_submit_priv(nvmeq, req, iod);
907 else if (req->cmd_flags & REQ_DISCARD)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700908 nvme_submit_discard(nvmeq, ns, req, iod);
909 else if (req->cmd_flags & REQ_FLUSH)
910 nvme_submit_flush(nvmeq, ns, req->tag);
911 else
912 nvme_submit_iod(nvmeq, iod, ns);
913
914 nvme_process_cq(nvmeq);
915 spin_unlock_irq(&nvmeq->q_lock);
916 return BLK_MQ_RQ_QUEUE_OK;
917
Jens Axboefe543032014-12-11 13:58:39 -0700918 error_cmd:
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200919 nvme_free_iod(dev, iod);
Jens Axboefe543032014-12-11 13:58:39 -0700920 return BLK_MQ_RQ_QUEUE_ERROR;
921 retry_cmd:
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200922 nvme_free_iod(dev, iod);
Jens Axboefe543032014-12-11 13:58:39 -0700923 return BLK_MQ_RQ_QUEUE_BUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500924}
925
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400926static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500927{
Matthew Wilcox82123462011-01-20 13:24:06 -0500928 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500929
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500930 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500931 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500932
933 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400934 void *ctx;
935 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500936 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500937 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500938 break;
939 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
940 if (++head == nvmeq->q_depth) {
941 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500942 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500943 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700944 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600945 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500946 }
947
948 /* If the controller ignores the cq head doorbell and continuously
949 * writes to the queue, it is theoretically possible to wrap around
950 * the queue twice and mistakenly return IRQ_NONE. Linux only
951 * requires that 0.1% of your interrupts are handled, so this isn't
952 * a big problem.
953 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500954 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400955 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500956
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800957 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500958 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500959 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500960
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400961 nvmeq->cqe_seen = 1;
962 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500963}
964
965static irqreturn_t nvme_irq(int irq, void *data)
966{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500967 irqreturn_t result;
968 struct nvme_queue *nvmeq = data;
969 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400970 nvme_process_cq(nvmeq);
971 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
972 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500973 spin_unlock(&nvmeq->q_lock);
974 return result;
975}
976
977static irqreturn_t nvme_irq_check(int irq, void *data)
978{
979 struct nvme_queue *nvmeq = data;
980 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
981 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
982 return IRQ_NONE;
983 return IRQ_WAKE_THREAD;
984}
985
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500986/*
987 * Returns 0 on success. If the result is negative, it's a Linux error code;
988 * if the result is positive, it's an NVM Express status code
989 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200990int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
991 void *buffer, void __user *ubuffer, unsigned bufflen,
992 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500993{
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200994 bool write = cmd->common.opcode & 1;
995 struct bio *bio = NULL;
Christoph Hellwigf705f832015-05-22 11:12:38 +0200996 struct request *req;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200997 int ret;
Christoph Hellwigf705f832015-05-22 11:12:38 +0200998
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200999 req = blk_mq_alloc_request(q, write, GFP_KERNEL, false);
Christoph Hellwigf705f832015-05-22 11:12:38 +02001000 if (IS_ERR(req))
1001 return PTR_ERR(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001002
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001003 req->cmd_type = REQ_TYPE_DRV_PRIV;
Keith Busch75619bf2015-05-28 09:48:55 -06001004 req->cmd_flags = REQ_FAILFAST_DRIVER;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001005 req->__data_len = 0;
1006 req->__sector = (sector_t) -1;
1007 req->bio = req->biotail = NULL;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001008
Keith Buschf4ff4142015-05-28 09:48:54 -06001009 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001010
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001011 req->cmd = (unsigned char *)cmd;
1012 req->cmd_len = sizeof(struct nvme_command);
Keith Buscha0a931d2015-05-22 12:28:31 -06001013 req->special = (void *)0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001014
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001015 if (buffer && bufflen) {
1016 ret = blk_rq_map_kern(q, req, buffer, bufflen, __GFP_WAIT);
1017 if (ret)
1018 goto out;
1019 } else if (ubuffer && bufflen) {
1020 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen, __GFP_WAIT);
1021 if (ret)
1022 goto out;
1023 bio = req->bio;
1024 }
Matthew Wilcox3c0cf132011-02-04 16:03:56 -05001025
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001026 blk_execute_rq(req->q, NULL, req, 0);
1027 if (bio)
1028 blk_rq_unmap_user(bio);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001029 if (result)
Keith Buscha0a931d2015-05-22 12:28:31 -06001030 *result = (u32)(uintptr_t)req->special;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001031 ret = req->errors;
1032 out:
Christoph Hellwigf705f832015-05-22 11:12:38 +02001033 blk_mq_free_request(req);
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001034 return ret;
Christoph Hellwigf705f832015-05-22 11:12:38 +02001035}
1036
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001037int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
1038 void *buffer, unsigned bufflen)
Christoph Hellwigf705f832015-05-22 11:12:38 +02001039{
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001040 return __nvme_submit_sync_cmd(q, cmd, buffer, NULL, bufflen, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001041}
1042
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001043static int nvme_submit_async_admin_req(struct nvme_dev *dev)
1044{
1045 struct nvme_queue *nvmeq = dev->queues[0];
1046 struct nvme_command c;
1047 struct nvme_cmd_info *cmd_info;
1048 struct request *req;
1049
Keith Busch1efccc92015-03-31 10:37:17 -06001050 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC, true);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001051 if (IS_ERR(req))
1052 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001053
Keith Buschc917dfe2015-01-07 18:55:48 -07001054 req->cmd_flags |= REQ_NO_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001055 cmd_info = blk_mq_rq_to_pdu(req);
Keith Busch1efccc92015-03-31 10:37:17 -06001056 nvme_set_info(cmd_info, NULL, async_req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001057
1058 memset(&c, 0, sizeof(c));
1059 c.common.opcode = nvme_admin_async_event;
1060 c.common.command_id = req->tag;
1061
Keith Busch42483222015-06-01 09:29:54 -06001062 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001063 return __nvme_submit_cmd(nvmeq, &c);
1064}
1065
1066static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
Keith Busch4d115422013-12-10 13:10:40 -07001067 struct nvme_command *cmd,
1068 struct async_cmd_info *cmdinfo, unsigned timeout)
1069{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001070 struct nvme_queue *nvmeq = dev->queues[0];
1071 struct request *req;
1072 struct nvme_cmd_info *cmd_rq;
Keith Busch4d115422013-12-10 13:10:40 -07001073
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001074 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_KERNEL, false);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001075 if (IS_ERR(req))
1076 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001077
1078 req->timeout = timeout;
1079 cmd_rq = blk_mq_rq_to_pdu(req);
1080 cmdinfo->req = req;
1081 nvme_set_info(cmd_rq, cmdinfo, async_completion);
Keith Busch4d115422013-12-10 13:10:40 -07001082 cmdinfo->status = -EINTR;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001083
1084 cmd->common.command_id = req->tag;
1085
Keith Busch4f5099a2014-03-03 16:39:13 -07001086 return nvme_submit_cmd(nvmeq, cmd);
Keith Busch4d115422013-12-10 13:10:40 -07001087}
1088
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001089static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1090{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001091 struct nvme_command c;
1092
1093 memset(&c, 0, sizeof(c));
1094 c.delete_queue.opcode = opcode;
1095 c.delete_queue.qid = cpu_to_le16(id);
1096
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001097 return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001098}
1099
1100static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1101 struct nvme_queue *nvmeq)
1102{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001103 struct nvme_command c;
1104 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1105
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001106 /*
1107 * Note: we (ab)use the fact the the prp fields survive if no data
1108 * is attached to the request.
1109 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001110 memset(&c, 0, sizeof(c));
1111 c.create_cq.opcode = nvme_admin_create_cq;
1112 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1113 c.create_cq.cqid = cpu_to_le16(qid);
1114 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1115 c.create_cq.cq_flags = cpu_to_le16(flags);
1116 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1117
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001118 return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001119}
1120
1121static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1122 struct nvme_queue *nvmeq)
1123{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001124 struct nvme_command c;
1125 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1126
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001127 /*
1128 * Note: we (ab)use the fact the the prp fields survive if no data
1129 * is attached to the request.
1130 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001131 memset(&c, 0, sizeof(c));
1132 c.create_sq.opcode = nvme_admin_create_sq;
1133 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1134 c.create_sq.sqid = cpu_to_le16(qid);
1135 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1136 c.create_sq.sq_flags = cpu_to_le16(flags);
1137 c.create_sq.cqid = cpu_to_le16(qid);
1138
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001139 return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001140}
1141
1142static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1143{
1144 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1145}
1146
1147static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1148{
1149 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1150}
1151
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001152int nvme_identify_ctrl(struct nvme_dev *dev, struct nvme_id_ctrl **id)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001153{
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001154 struct nvme_command c = {
1155 .identify.opcode = nvme_admin_identify,
1156 .identify.cns = cpu_to_le32(1),
1157 };
1158 int error;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001159
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001160 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
1161 if (!*id)
1162 return -ENOMEM;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001163
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001164 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
1165 sizeof(struct nvme_id_ctrl));
1166 if (error)
1167 kfree(*id);
1168 return error;
1169}
1170
1171int nvme_identify_ns(struct nvme_dev *dev, unsigned nsid,
1172 struct nvme_id_ns **id)
1173{
1174 struct nvme_command c = {
1175 .identify.opcode = nvme_admin_identify,
1176 .identify.nsid = cpu_to_le32(nsid),
1177 };
1178 int error;
1179
1180 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
1181 if (!*id)
1182 return -ENOMEM;
1183
1184 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
1185 sizeof(struct nvme_id_ns));
1186 if (error)
1187 kfree(*id);
1188 return error;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001189}
1190
Vishal Verma5d0f6132013-03-04 18:40:58 -07001191int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -06001192 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001193{
1194 struct nvme_command c;
1195
1196 memset(&c, 0, sizeof(c));
1197 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -06001198 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001199 c.features.prp1 = cpu_to_le64(dma_addr);
1200 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001201
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001202 return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, NULL, 0,
1203 result, 0);
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001204}
1205
Vishal Verma5d0f6132013-03-04 18:40:58 -07001206int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
1207 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001208{
1209 struct nvme_command c;
1210
1211 memset(&c, 0, sizeof(c));
1212 c.features.opcode = nvme_admin_set_features;
1213 c.features.prp1 = cpu_to_le64(dma_addr);
1214 c.features.fid = cpu_to_le32(fid);
1215 c.features.dword11 = cpu_to_le32(dword11);
1216
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001217 return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, NULL, 0,
1218 result, 0);
1219}
1220
1221int nvme_get_log_page(struct nvme_dev *dev, struct nvme_smart_log **log)
1222{
1223 struct nvme_command c = {
1224 .common.opcode = nvme_admin_get_log_page,
1225 .common.nsid = cpu_to_le32(0xFFFFFFFF),
1226 .common.cdw10[0] = cpu_to_le32(
1227 (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
1228 NVME_LOG_SMART),
1229 };
1230 int error;
1231
1232 *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
1233 if (!*log)
1234 return -ENOMEM;
1235
1236 error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
1237 sizeof(struct nvme_smart_log));
1238 if (error)
1239 kfree(*log);
1240 return error;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001241}
1242
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001243/**
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001244 * nvme_abort_req - Attempt aborting a request
Keith Buschc30341d2013-12-10 13:10:38 -07001245 *
1246 * Schedule controller reset if the command was already aborted once before and
1247 * still hasn't been returned to the driver, or if this is the admin queue.
1248 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001249static void nvme_abort_req(struct request *req)
Keith Buschc30341d2013-12-10 13:10:38 -07001250{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001251 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1252 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001253 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001254 struct request *abort_req;
1255 struct nvme_cmd_info *abort_cmd;
1256 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -07001257
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001258 if (!nvmeq->qid || cmd_rq->aborted) {
Keith Busch7a509a62015-01-07 18:55:53 -07001259 unsigned long flags;
1260
1261 spin_lock_irqsave(&dev_list_lock, flags);
Keith Buschc30341d2013-12-10 13:10:38 -07001262 if (work_busy(&dev->reset_work))
Keith Busch7a509a62015-01-07 18:55:53 -07001263 goto out;
Keith Buschc30341d2013-12-10 13:10:38 -07001264 list_del_init(&dev->node);
Christoph Hellwige75ec752015-05-22 11:12:39 +02001265 dev_warn(dev->dev, "I/O %d QID %d timeout, reset controller\n",
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001266 req->tag, nvmeq->qid);
Tejun Heo9ca97372014-03-07 10:24:49 -05001267 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschc30341d2013-12-10 13:10:38 -07001268 queue_work(nvme_workq, &dev->reset_work);
Keith Busch7a509a62015-01-07 18:55:53 -07001269 out:
1270 spin_unlock_irqrestore(&dev_list_lock, flags);
Keith Buschc30341d2013-12-10 13:10:38 -07001271 return;
1272 }
1273
1274 if (!dev->abort_limit)
1275 return;
1276
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001277 abort_req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC,
1278 false);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001279 if (IS_ERR(abort_req))
Keith Buschc30341d2013-12-10 13:10:38 -07001280 return;
1281
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001282 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1283 nvme_set_info(abort_cmd, abort_req, abort_completion);
1284
Keith Buschc30341d2013-12-10 13:10:38 -07001285 memset(&cmd, 0, sizeof(cmd));
1286 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001287 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001288 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001289 cmd.abort.command_id = abort_req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001290
1291 --dev->abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001292 cmd_rq->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -07001293
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001294 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", req->tag,
Keith Buschc30341d2013-12-10 13:10:38 -07001295 nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001296 if (nvme_submit_cmd(dev->queues[0], &cmd) < 0) {
1297 dev_warn(nvmeq->q_dmadev,
1298 "Could not abort I/O %d QID %d",
1299 req->tag, nvmeq->qid);
Sam Bradshawc87fd542014-12-10 13:00:31 -07001300 blk_mq_free_request(abort_req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001301 }
Keith Buschc30341d2013-12-10 13:10:38 -07001302}
1303
Keith Busch42483222015-06-01 09:29:54 -06001304static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001305{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001306 struct nvme_queue *nvmeq = data;
1307 void *ctx;
1308 nvme_completion_fn fn;
1309 struct nvme_cmd_info *cmd;
Keith Buschcef6a942015-01-07 18:55:51 -07001310 struct nvme_completion cqe;
1311
1312 if (!blk_mq_request_started(req))
1313 return;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001314
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001315 cmd = blk_mq_rq_to_pdu(req);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001316
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001317 if (cmd->ctx == CMD_CTX_CANCELLED)
1318 return;
1319
Keith Buschcef6a942015-01-07 18:55:51 -07001320 if (blk_queue_dying(req->q))
1321 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1322 else
1323 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1324
1325
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001326 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1327 req->tag, nvmeq->qid);
1328 ctx = cancel_cmd_info(cmd, &fn);
1329 fn(nvmeq, ctx, &cqe);
1330}
1331
1332static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
1333{
1334 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
1335 struct nvme_queue *nvmeq = cmd->nvmeq;
1336
Keith Busch07836e62015-02-19 10:34:48 -07001337 dev_warn(nvmeq->q_dmadev, "Timeout I/O %d QID %d\n", req->tag,
1338 nvmeq->qid);
1339 spin_lock_irq(&nvmeq->q_lock);
1340 nvme_abort_req(req);
1341 spin_unlock_irq(&nvmeq->q_lock);
1342
Keith Busch7a509a62015-01-07 18:55:53 -07001343 /*
1344 * The aborted req will be completed on receiving the abort req.
1345 * We enable the timer again. If hit twice, it'll cause a device reset,
1346 * as the device then is in a faulty state.
1347 */
Keith Busch07836e62015-02-19 10:34:48 -07001348 return BLK_EH_RESET_TIMER;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001349}
1350
Keith Buschf435c282014-07-07 09:14:42 -06001351static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001352{
1353 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1354 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1355 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1356 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1357 kfree(nvmeq);
1358}
1359
Keith Buscha1a5ef92013-12-16 13:50:00 -05001360static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001361{
1362 int i;
1363
Keith Buscha1a5ef92013-12-16 13:50:00 -05001364 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001365 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001366 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001367 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001368 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001369 }
Keith Busch22404272013-07-15 15:02:20 -06001370}
1371
Keith Busch4d115422013-12-10 13:10:40 -07001372/**
1373 * nvme_suspend_queue - put queue into suspended state
1374 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001375 */
1376static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001377{
Keith Busch2b25d982014-12-22 12:59:04 -07001378 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001379
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001380 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001381 if (nvmeq->cq_vector == -1) {
1382 spin_unlock_irq(&nvmeq->q_lock);
1383 return 1;
1384 }
1385 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Keith Busch42f61422014-03-24 10:46:25 -06001386 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001387 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001388 spin_unlock_irq(&nvmeq->q_lock);
1389
Keith Busch6df3dbc2015-03-26 13:49:33 -06001390 if (!nvmeq->qid && nvmeq->dev->admin_q)
1391 blk_mq_freeze_queue_start(nvmeq->dev->admin_q);
1392
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001393 irq_set_affinity_hint(vector, NULL);
1394 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001395
Keith Busch4d115422013-12-10 13:10:40 -07001396 return 0;
1397}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001398
Keith Busch4d115422013-12-10 13:10:40 -07001399static void nvme_clear_queue(struct nvme_queue *nvmeq)
1400{
Keith Busch22404272013-07-15 15:02:20 -06001401 spin_lock_irq(&nvmeq->q_lock);
Keith Busch42483222015-06-01 09:29:54 -06001402 if (nvmeq->tags && *nvmeq->tags)
1403 blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001404 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001405}
1406
Keith Busch4d115422013-12-10 13:10:40 -07001407static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1408{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001409 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001410
1411 if (!nvmeq)
1412 return;
1413 if (nvme_suspend_queue(nvmeq))
1414 return;
1415
Keith Busch0e53d182013-12-10 13:10:39 -07001416 /* Don't tell the adapter to delete the admin queue.
1417 * Don't tell a removed adapter to delete IO queues. */
1418 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001419 adapter_delete_sq(dev, qid);
1420 adapter_delete_cq(dev, qid);
1421 }
Keith Busch07836e62015-02-19 10:34:48 -07001422
1423 spin_lock_irq(&nvmeq->q_lock);
1424 nvme_process_cq(nvmeq);
1425 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001426}
1427
1428static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Keith Busch2b25d982014-12-22 12:59:04 -07001429 int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001430{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001431 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001432 if (!nvmeq)
1433 return NULL;
1434
Christoph Hellwige75ec752015-05-22 11:12:39 +02001435 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001436 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001437 if (!nvmeq->cqes)
1438 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001439
Christoph Hellwige75ec752015-05-22 11:12:39 +02001440 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001441 &nvmeq->sq_dma_addr, GFP_KERNEL);
1442 if (!nvmeq->sq_cmds)
1443 goto free_cqdma;
1444
Christoph Hellwige75ec752015-05-22 11:12:39 +02001445 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001446 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001447 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1448 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001449 spin_lock_init(&nvmeq->q_lock);
1450 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001451 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001452 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001453 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001454 nvmeq->qid = qid;
Keith Busch22404272013-07-15 15:02:20 -06001455 dev->queue_count++;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001456 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001457
1458 return nvmeq;
1459
1460 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001461 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001462 nvmeq->cq_dma_addr);
1463 free_nvmeq:
1464 kfree(nvmeq);
1465 return NULL;
1466}
1467
Matthew Wilcox30010822011-01-20 09:10:15 -05001468static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1469 const char *name)
1470{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001471 if (use_threaded_interrupts)
1472 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001473 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001474 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001475 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001476 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001477}
1478
Keith Busch22404272013-07-15 15:02:20 -06001479static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001480{
Keith Busch22404272013-07-15 15:02:20 -06001481 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001482
Keith Busch7be50e92014-09-10 15:48:47 -06001483 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001484 nvmeq->sq_tail = 0;
1485 nvmeq->cq_head = 0;
1486 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001487 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001488 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001489 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001490 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001491}
1492
1493static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1494{
1495 struct nvme_dev *dev = nvmeq->dev;
1496 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001497
Keith Busch2b25d982014-12-22 12:59:04 -07001498 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001499 result = adapter_alloc_cq(dev, qid, nvmeq);
1500 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001501 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001502
1503 result = adapter_alloc_sq(dev, qid, nvmeq);
1504 if (result < 0)
1505 goto release_cq;
1506
Matthew Wilcox3193f072014-01-27 15:57:22 -05001507 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001508 if (result < 0)
1509 goto release_sq;
1510
Keith Busch22404272013-07-15 15:02:20 -06001511 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001512 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001513
1514 release_sq:
1515 adapter_delete_sq(dev, qid);
1516 release_cq:
1517 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001518 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001519}
1520
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001521static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1522{
1523 unsigned long timeout;
1524 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1525
1526 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1527
1528 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1529 msleep(100);
1530 if (fatal_signal_pending(current))
1531 return -EINTR;
1532 if (time_after(jiffies, timeout)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001533 dev_err(dev->dev,
Matthew Wilcox27e81662014-04-11 11:58:45 -04001534 "Device not ready; aborting %s\n", enabled ?
1535 "initialisation" : "reset");
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001536 return -ENODEV;
1537 }
1538 }
1539
1540 return 0;
1541}
1542
1543/*
1544 * If the device has been passed off to us in an enabled state, just clear
1545 * the enabled bit. The spec says we should set the 'shutdown notification
1546 * bits', but doing so may cause the device to complete commands to the
1547 * admin queue ... and we don't know what memory that might be pointing at!
1548 */
1549static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1550{
Dan McLeran01079522014-06-23 08:24:36 -06001551 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1552 dev->ctrl_config &= ~NVME_CC_ENABLE;
1553 writel(dev->ctrl_config, &dev->bar->cc);
Matthew Wilcox44af1462013-05-04 06:43:17 -04001554
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001555 return nvme_wait_ready(dev, cap, false);
1556}
1557
1558static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1559{
Dan McLeran01079522014-06-23 08:24:36 -06001560 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1561 dev->ctrl_config |= NVME_CC_ENABLE;
1562 writel(dev->ctrl_config, &dev->bar->cc);
1563
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001564 return nvme_wait_ready(dev, cap, true);
1565}
1566
Keith Busch1894d8f2013-07-15 15:02:22 -06001567static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1568{
1569 unsigned long timeout;
Keith Busch1894d8f2013-07-15 15:02:22 -06001570
Dan McLeran01079522014-06-23 08:24:36 -06001571 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1572 dev->ctrl_config |= NVME_CC_SHN_NORMAL;
1573
1574 writel(dev->ctrl_config, &dev->bar->cc);
Keith Busch1894d8f2013-07-15 15:02:22 -06001575
Dan McLeran2484f402014-07-01 09:33:32 -06001576 timeout = SHUTDOWN_TIMEOUT + jiffies;
Keith Busch1894d8f2013-07-15 15:02:22 -06001577 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1578 NVME_CSTS_SHST_CMPLT) {
1579 msleep(100);
1580 if (fatal_signal_pending(current))
1581 return -EINTR;
1582 if (time_after(jiffies, timeout)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001583 dev_err(dev->dev,
Keith Busch1894d8f2013-07-15 15:02:22 -06001584 "Device shutdown incomplete; abort shutdown\n");
1585 return -ENODEV;
1586 }
1587 }
1588
1589 return 0;
1590}
1591
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001592static struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001593 .queue_rq = nvme_queue_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001594 .map_queue = blk_mq_map_queue,
1595 .init_hctx = nvme_admin_init_hctx,
1596 .init_request = nvme_admin_init_request,
1597 .timeout = nvme_timeout,
1598};
1599
1600static struct blk_mq_ops nvme_mq_ops = {
1601 .queue_rq = nvme_queue_rq,
1602 .map_queue = blk_mq_map_queue,
1603 .init_hctx = nvme_init_hctx,
1604 .init_request = nvme_init_request,
1605 .timeout = nvme_timeout,
1606};
1607
Keith Buschea191d22015-01-07 18:55:49 -07001608static void nvme_dev_remove_admin(struct nvme_dev *dev)
1609{
1610 if (dev->admin_q && !blk_queue_dying(dev->admin_q)) {
1611 blk_cleanup_queue(dev->admin_q);
1612 blk_mq_free_tag_set(&dev->admin_tagset);
1613 }
1614}
1615
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001616static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1617{
1618 if (!dev->admin_q) {
1619 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1620 dev->admin_tagset.nr_hw_queues = 1;
1621 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1;
Keith Busch1efccc92015-03-31 10:37:17 -06001622 dev->admin_tagset.reserved_tags = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001623 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001624 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001625 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001626 dev->admin_tagset.driver_data = dev;
1627
1628 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1629 return -ENOMEM;
1630
1631 dev->admin_q = blk_mq_init_queue(&dev->admin_tagset);
Ming Lei35b489d2015-01-02 14:25:27 +00001632 if (IS_ERR(dev->admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001633 blk_mq_free_tag_set(&dev->admin_tagset);
1634 return -ENOMEM;
1635 }
Keith Buschea191d22015-01-07 18:55:49 -07001636 if (!blk_get_queue(dev->admin_q)) {
1637 nvme_dev_remove_admin(dev);
1638 return -ENODEV;
1639 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001640 } else
1641 blk_mq_unfreeze_queue(dev->admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001642
1643 return 0;
1644}
1645
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001646static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001647{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001648 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001649 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001650 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001651 struct nvme_queue *nvmeq;
Keith Busch1d090622014-06-23 11:34:01 -06001652 unsigned page_shift = PAGE_SHIFT;
1653 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12;
1654 unsigned dev_page_max = NVME_CAP_MPSMAX(cap) + 12;
1655
1656 if (page_shift < dev_page_min) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001657 dev_err(dev->dev,
Keith Busch1d090622014-06-23 11:34:01 -06001658 "Minimum device page size (%u) too large for "
1659 "host (%u)\n", 1 << dev_page_min,
1660 1 << page_shift);
1661 return -ENODEV;
1662 }
1663 if (page_shift > dev_page_max) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001664 dev_info(dev->dev,
Keith Busch1d090622014-06-23 11:34:01 -06001665 "Device maximum page size (%u) smaller than "
1666 "host (%u); enabling work-around\n",
1667 1 << dev_page_max, 1 << page_shift);
1668 page_shift = dev_page_max;
1669 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001670
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001671 result = nvme_disable_ctrl(dev, cap);
1672 if (result < 0)
1673 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001674
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001675 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001676 if (!nvmeq) {
Keith Busch2b25d982014-12-22 12:59:04 -07001677 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001678 if (!nvmeq)
1679 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001680 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001681
1682 aqa = nvmeq->q_depth - 1;
1683 aqa |= aqa << 16;
1684
Keith Busch1d090622014-06-23 11:34:01 -06001685 dev->page_size = 1 << page_shift;
1686
Dan McLeran01079522014-06-23 08:24:36 -06001687 dev->ctrl_config = NVME_CC_CSS_NVM;
Keith Busch1d090622014-06-23 11:34:01 -06001688 dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001689 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001690 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001691
1692 writel(aqa, &dev->bar->aqa);
1693 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1694 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001695
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001696 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001697 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001698 goto free_nvmeq;
1699
Keith Busch2b25d982014-12-22 12:59:04 -07001700 nvmeq->cq_vector = 0;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001701 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Keith Busch025c5572013-05-01 13:07:51 -06001702 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07001703 goto free_nvmeq;
Keith Busch025c5572013-05-01 13:07:51 -06001704
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001705 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001706
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001707 free_nvmeq:
1708 nvme_free_queues(dev, 0);
1709 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001710}
1711
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001712static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1713{
1714 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001715 struct nvme_user_io io;
1716 struct nvme_command c;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001717 unsigned length, meta_len;
Keith Buscha67a9512015-04-07 16:57:19 -06001718 int status, write;
Keith Buscha67a9512015-04-07 16:57:19 -06001719 dma_addr_t meta_dma = 0;
1720 void *meta = NULL;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001721
1722 if (copy_from_user(&io, uio, sizeof(io)))
1723 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001724
1725 switch (io.opcode) {
1726 case nvme_cmd_write:
1727 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001728 case nvme_cmd_compare:
Matthew Wilcox64132142011-08-09 12:56:37 -04001729 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001730 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001731 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001732 }
1733
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001734 length = (io.nblocks + 1) << ns->lba_shift;
1735 meta_len = (io.nblocks + 1) * ns->ms;
1736 write = io.opcode & 1;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001737
Keith Buscha67a9512015-04-07 16:57:19 -06001738 if (meta_len) {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001739 if (((io.metadata & 3) || !io.metadata) && !ns->ext)
1740 return -EINVAL;
1741
1742 if (ns->ext) {
1743 length += meta_len;
1744 meta_len = 0;
1745 }
1746
Christoph Hellwige75ec752015-05-22 11:12:39 +02001747 meta = dma_alloc_coherent(dev->dev, meta_len,
Keith Buscha67a9512015-04-07 16:57:19 -06001748 &meta_dma, GFP_KERNEL);
1749 if (!meta) {
1750 status = -ENOMEM;
1751 goto unmap;
1752 }
1753 if (write) {
1754 if (copy_from_user(meta, (void __user *)io.metadata,
1755 meta_len)) {
1756 status = -EFAULT;
1757 goto unmap;
1758 }
1759 }
1760 }
1761
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001762 memset(&c, 0, sizeof(c));
1763 c.rw.opcode = io.opcode;
1764 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001765 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001766 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001767 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001768 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001769 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1770 c.rw.reftag = cpu_to_le32(io.reftag);
1771 c.rw.apptag = cpu_to_le16(io.apptag);
1772 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buscha67a9512015-04-07 16:57:19 -06001773 c.rw.metadata = cpu_to_le64(meta_dma);
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001774
1775 status = __nvme_submit_sync_cmd(ns->queue, &c, NULL,
1776 (void __user *)io.addr, length, NULL, 0);
Keith Buschf410c682013-04-23 17:23:59 -06001777 unmap:
Keith Buscha67a9512015-04-07 16:57:19 -06001778 if (meta) {
1779 if (status == NVME_SC_SUCCESS && !write) {
1780 if (copy_to_user((void __user *)io.metadata, meta,
1781 meta_len))
1782 status = -EFAULT;
1783 }
Christoph Hellwige75ec752015-05-22 11:12:39 +02001784 dma_free_coherent(dev->dev, meta_len, meta, meta_dma);
Keith Buschf410c682013-04-23 17:23:59 -06001785 }
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001786 return status;
1787}
1788
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001789static int nvme_user_cmd(struct nvme_dev *dev, struct nvme_ns *ns,
1790 struct nvme_passthru_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001791{
Keith Busch7963e522014-09-12 16:07:20 -06001792 struct nvme_passthru_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001793 struct nvme_command c;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001794 unsigned timeout = 0;
1795 int status;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001796
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001797 if (!capable(CAP_SYS_ADMIN))
1798 return -EACCES;
1799 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001800 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001801
1802 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001803 c.common.opcode = cmd.opcode;
1804 c.common.flags = cmd.flags;
1805 c.common.nsid = cpu_to_le32(cmd.nsid);
1806 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1807 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1808 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1809 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1810 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1811 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1812 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1813 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1814
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001815 if (cmd.timeout_ms)
1816 timeout = msecs_to_jiffies(cmd.timeout_ms);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001817
Christoph Hellwigf705f832015-05-22 11:12:38 +02001818 status = __nvme_submit_sync_cmd(ns ? ns->queue : dev->admin_q, &c,
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001819 NULL, (void __user *)cmd.addr, cmd.data_len,
1820 &cmd.result, timeout);
1821 if (status >= 0) {
1822 if (put_user(cmd.result, &ucmd->result))
1823 return -EFAULT;
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001824 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001825
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001826 return status;
1827}
1828
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001829static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1830 unsigned long arg)
1831{
1832 struct nvme_ns *ns = bdev->bd_disk->private_data;
1833
1834 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001835 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001836 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001837 return ns->ns_id;
1838 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001839 return nvme_user_cmd(ns->dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06001840 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001841 return nvme_user_cmd(ns->dev, ns, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001842 case NVME_IOCTL_SUBMIT_IO:
1843 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001844 case SG_GET_VERSION_NUM:
1845 return nvme_sg_get_version_num((void __user *)arg);
1846 case SG_IO:
1847 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001848 default:
1849 return -ENOTTY;
1850 }
1851}
1852
Keith Busch320a3822013-10-23 13:07:34 -06001853#ifdef CONFIG_COMPAT
1854static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1855 unsigned int cmd, unsigned long arg)
1856{
Keith Busch320a3822013-10-23 13:07:34 -06001857 switch (cmd) {
1858 case SG_IO:
Keith Busche1797292014-08-27 13:55:38 -06001859 return -ENOIOCTLCMD;
Keith Busch320a3822013-10-23 13:07:34 -06001860 }
1861 return nvme_ioctl(bdev, mode, cmd, arg);
1862}
1863#else
1864#define nvme_compat_ioctl NULL
1865#endif
1866
Keith Busch9ac27092014-01-31 16:53:39 -07001867static int nvme_open(struct block_device *bdev, fmode_t mode)
1868{
Keith Busch9e603522014-10-03 11:15:47 -06001869 int ret = 0;
1870 struct nvme_ns *ns;
Keith Busch9ac27092014-01-31 16:53:39 -07001871
Keith Busch9e603522014-10-03 11:15:47 -06001872 spin_lock(&dev_list_lock);
1873 ns = bdev->bd_disk->private_data;
1874 if (!ns)
1875 ret = -ENXIO;
1876 else if (!kref_get_unless_zero(&ns->dev->kref))
1877 ret = -ENXIO;
1878 spin_unlock(&dev_list_lock);
1879
1880 return ret;
Keith Busch9ac27092014-01-31 16:53:39 -07001881}
1882
1883static void nvme_free_dev(struct kref *kref);
1884
1885static void nvme_release(struct gendisk *disk, fmode_t mode)
1886{
1887 struct nvme_ns *ns = disk->private_data;
1888 struct nvme_dev *dev = ns->dev;
1889
1890 kref_put(&dev->kref, nvme_free_dev);
1891}
1892
Keith Busch4cc09e22014-04-02 15:45:37 -06001893static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
1894{
1895 /* some standard values */
1896 geo->heads = 1 << 6;
1897 geo->sectors = 1 << 5;
1898 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1899 return 0;
1900}
1901
Keith Busche1e5e562015-02-19 13:39:03 -07001902static void nvme_config_discard(struct nvme_ns *ns)
1903{
1904 u32 logical_block_size = queue_logical_block_size(ns->queue);
1905 ns->queue->limits.discard_zeroes_data = 0;
1906 ns->queue->limits.discard_alignment = logical_block_size;
1907 ns->queue->limits.discard_granularity = logical_block_size;
1908 ns->queue->limits.max_discard_sectors = 0xffffffff;
1909 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1910}
1911
Keith Busch1b9dbf72014-09-10 17:21:14 -06001912static int nvme_revalidate_disk(struct gendisk *disk)
1913{
1914 struct nvme_ns *ns = disk->private_data;
1915 struct nvme_dev *dev = ns->dev;
1916 struct nvme_id_ns *id;
Keith Buscha67a9512015-04-07 16:57:19 -06001917 u8 lbaf, pi_type;
1918 u16 old_ms;
Keith Busche1e5e562015-02-19 13:39:03 -07001919 unsigned short bs;
Keith Busch1b9dbf72014-09-10 17:21:14 -06001920
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001921 if (nvme_identify_ns(dev, ns->ns_id, &id)) {
1922 dev_warn(dev->dev, "%s: Identify failure\n", __func__);
Keith Busch1b9dbf72014-09-10 17:21:14 -06001923 return 0;
1924 }
1925
Keith Busche1e5e562015-02-19 13:39:03 -07001926 old_ms = ns->ms;
1927 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
Keith Busch1b9dbf72014-09-10 17:21:14 -06001928 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Busche1e5e562015-02-19 13:39:03 -07001929 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Buscha67a9512015-04-07 16:57:19 -06001930 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
Keith Busch1b9dbf72014-09-10 17:21:14 -06001931
Keith Busche1e5e562015-02-19 13:39:03 -07001932 /*
1933 * If identify namespace failed, use default 512 byte block size so
1934 * block layer can use before failing read/write for 0 capacity.
1935 */
1936 if (ns->lba_shift == 0)
1937 ns->lba_shift = 9;
1938 bs = 1 << ns->lba_shift;
1939
1940 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
1941 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
1942 id->dps & NVME_NS_DPS_PI_MASK : 0;
1943
Keith Busch52b68d72015-02-23 09:16:21 -07001944 if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
1945 ns->ms != old_ms ||
Keith Busche1e5e562015-02-19 13:39:03 -07001946 bs != queue_logical_block_size(disk->queue) ||
Keith Buscha67a9512015-04-07 16:57:19 -06001947 (ns->ms && ns->ext)))
Keith Busche1e5e562015-02-19 13:39:03 -07001948 blk_integrity_unregister(disk);
1949
1950 ns->pi_type = pi_type;
1951 blk_queue_logical_block_size(ns->queue, bs);
1952
Keith Busch52b68d72015-02-23 09:16:21 -07001953 if (ns->ms && !blk_get_integrity(disk) && (disk->flags & GENHD_FL_UP) &&
Keith Buscha67a9512015-04-07 16:57:19 -06001954 !ns->ext)
Keith Busche1e5e562015-02-19 13:39:03 -07001955 nvme_init_integrity(ns);
1956
Keith Busch52b68d72015-02-23 09:16:21 -07001957 if (id->ncap == 0 || (ns->ms && !blk_get_integrity(disk)))
Keith Busche1e5e562015-02-19 13:39:03 -07001958 set_capacity(disk, 0);
1959 else
1960 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1961
1962 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1963 nvme_config_discard(ns);
1964
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001965 kfree(id);
Keith Busch1b9dbf72014-09-10 17:21:14 -06001966 return 0;
1967}
1968
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001969static const struct block_device_operations nvme_fops = {
1970 .owner = THIS_MODULE,
1971 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06001972 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07001973 .open = nvme_open,
1974 .release = nvme_release,
Keith Busch4cc09e22014-04-02 15:45:37 -06001975 .getgeo = nvme_getgeo,
Keith Busch1b9dbf72014-09-10 17:21:14 -06001976 .revalidate_disk= nvme_revalidate_disk,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001977};
1978
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001979static int nvme_kthread(void *data)
1980{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001981 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001982
1983 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001984 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001985 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001986 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001987 int i;
Keith Busch07836e62015-02-19 10:34:48 -07001988 if (readl(&dev->bar->csts) & NVME_CSTS_CFS) {
Keith Buschd4b4ff82013-12-10 13:10:37 -07001989 if (work_busy(&dev->reset_work))
1990 continue;
1991 list_del_init(&dev->node);
Christoph Hellwige75ec752015-05-22 11:12:39 +02001992 dev_warn(dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001993 "Failed status: %x, reset controller\n",
1994 readl(&dev->bar->csts));
Tejun Heo9ca97372014-03-07 10:24:49 -05001995 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001996 queue_work(nvme_workq, &dev->reset_work);
1997 continue;
1998 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001999 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002000 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05002001 if (!nvmeq)
2002 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002003 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04002004 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06002005
2006 while ((i == 0) && (dev->event_limit > 0)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002007 if (nvme_submit_async_admin_req(dev))
Keith Busch6fccf932014-06-18 13:58:57 -06002008 break;
2009 dev->event_limit--;
2010 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002011 spin_unlock_irq(&nvmeq->q_lock);
2012 }
2013 }
2014 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08002015 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002016 }
2017 return 0;
2018}
2019
Keith Busche1e5e562015-02-19 13:39:03 -07002020static void nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002021{
2022 struct nvme_ns *ns;
2023 struct gendisk *disk;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002024 int node = dev_to_node(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002025
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002026 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002027 if (!ns)
Keith Busche1e5e562015-02-19 13:39:03 -07002028 return;
2029
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002030 ns->queue = blk_mq_init_queue(&dev->tagset);
Dan Carpenter9f173b32014-11-05 23:39:09 +03002031 if (IS_ERR(ns->queue))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002032 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07002033 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
2034 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002035 queue_flag_set_unlocked(QUEUE_FLAG_SG_GAPS, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002036 ns->dev = dev;
2037 ns->queue->queuedata = ns;
2038
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002039 disk = alloc_disk_node(0, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002040 if (!disk)
2041 goto out_free_queue;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002042
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002043 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002044 ns->disk = disk;
Keith Busche1e5e562015-02-19 13:39:03 -07002045 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
2046 list_add_tail(&ns->list, &dev->namespaces);
2047
Keith Busche9ef4632012-07-24 15:01:04 -06002048 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06002049 if (dev->max_hw_sectors)
2050 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002051 if (dev->stripe_size)
2052 blk_queue_chunk_sectors(ns->queue, dev->stripe_size >> 9);
Keith Buscha7d2ce22014-04-29 11:41:28 -06002053 if (dev->vwc & NVME_CTRL_VWC_PRESENT)
2054 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002055
2056 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002057 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002058 disk->fops = &nvme_fops;
2059 disk->private_data = ns;
2060 disk->queue = ns->queue;
Keith Buschb3fffde2015-02-03 11:21:42 -07002061 disk->driverfs_dev = dev->device;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002062 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002063 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002064
Keith Busche1e5e562015-02-19 13:39:03 -07002065 /*
2066 * Initialize capacity to 0 until we establish the namespace format and
2067 * setup integrity extentions if necessary. The revalidate_disk after
2068 * add_disk allows the driver to register with integrity if the format
2069 * requires it.
2070 */
2071 set_capacity(disk, 0);
2072 nvme_revalidate_disk(ns->disk);
2073 add_disk(ns->disk);
2074 if (ns->ms)
2075 revalidate_disk(ns->disk);
2076 return;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002077 out_free_queue:
2078 blk_cleanup_queue(ns->queue);
2079 out_free_ns:
2080 kfree(ns);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002081}
2082
Keith Busch42f61422014-03-24 10:46:25 -06002083static void nvme_create_io_queues(struct nvme_dev *dev)
2084{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002085 unsigned i;
Keith Busch42f61422014-03-24 10:46:25 -06002086
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002087 for (i = dev->queue_count; i <= dev->max_qid; i++)
Keith Busch2b25d982014-12-22 12:59:04 -07002088 if (!nvme_alloc_queue(dev, i, dev->q_depth))
Keith Busch42f61422014-03-24 10:46:25 -06002089 break;
2090
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002091 for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
2092 if (nvme_create_queue(dev->queues[i], i))
Keith Busch42f61422014-03-24 10:46:25 -06002093 break;
2094}
2095
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002096static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002097{
2098 int status;
2099 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002100 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002101
Matthew Wilcoxdf348132012-01-11 07:29:56 -07002102 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002103 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04002104 if (status < 0)
2105 return status;
2106 if (status > 0) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002107 dev_err(dev->dev, "Could not set queue count (%d)\n", status);
Keith Buschbadc34d2014-06-23 14:25:35 -06002108 return 0;
Matthew Wilcox27e81662014-04-11 11:58:45 -04002109 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002110 return min(result & 0xffff, result >> 16) + 1;
2111}
2112
Keith Busch9d713c22013-07-15 15:02:24 -06002113static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
2114{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002115 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06002116}
2117
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002118static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002119{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002120 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02002121 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch42f61422014-03-24 10:46:25 -06002122 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002123
Keith Busch42f61422014-03-24 10:46:25 -06002124 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002125 result = set_queue_count(dev, nr_io_queues);
Keith Buschbadc34d2014-06-23 14:25:35 -06002126 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002127 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002128 if (result < nr_io_queues)
2129 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002130
Keith Busch9d713c22013-07-15 15:02:24 -06002131 size = db_bar_size(dev, nr_io_queues);
2132 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002133 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06002134 do {
2135 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
2136 if (dev->bar)
2137 break;
2138 if (!--nr_io_queues)
2139 return -ENOMEM;
2140 size = db_bar_size(dev, nr_io_queues);
2141 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002142 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07002143 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002144 }
2145
Keith Busch9d713c22013-07-15 15:02:24 -06002146 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002147 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002148
Jens Axboee32efbf2014-11-14 09:49:26 -07002149 /*
2150 * If we enable msix early due to not intx, disable it again before
2151 * setting up the full range we need.
2152 */
2153 if (!pdev->irq)
2154 pci_disable_msix(pdev);
2155
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002156 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002157 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002158 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
2159 if (vecs < 0) {
2160 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
2161 if (vecs < 0) {
2162 vecs = 1;
2163 } else {
2164 for (i = 0; i < vecs; i++)
2165 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002166 }
2167 }
2168
Matthew Wilcox063a8092013-06-20 10:53:48 -04002169 /*
2170 * Should investigate if there's a performance win from allocating
2171 * more queues than interrupt vectors; it might allow the submission
2172 * path to scale better, even if the receive path is limited by the
2173 * number of interrupts.
2174 */
2175 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002176 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002177
Matthew Wilcox3193f072014-01-27 15:57:22 -05002178 result = queue_request_irq(dev, adminq, adminq->irqname);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002179 if (result)
Keith Busch22404272013-07-15 15:02:20 -06002180 goto free_queues;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002181
Keith Buschcd638942013-07-15 15:02:23 -06002182 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002183 nvme_free_queues(dev, nr_io_queues + 1);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002184 nvme_create_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06002185
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002186 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002187
Keith Busch22404272013-07-15 15:02:20 -06002188 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002189 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002190 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002191}
2192
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002193/*
2194 * Return: error value if an error occurred setting up the queues or calling
2195 * Identify Device. 0 if these succeeded, even if adding some of the
2196 * namespaces failed. At the moment, these failures are silent. TBD which
2197 * failures should be reported.
2198 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002199static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002200{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002201 struct pci_dev *pdev = to_pci_dev(dev->dev);
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002202 int res;
2203 unsigned nn, i;
Matthew Wilcox51814232011-02-01 16:18:08 -05002204 struct nvme_id_ctrl *ctrl;
Keith Busch159b67d2013-04-09 17:13:20 -06002205 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002206
Christoph Hellwigd29ec822015-05-22 11:12:46 +02002207 res = nvme_identify_ctrl(dev, &ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002208 if (res) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002209 dev_err(dev->dev, "Identify Controller failed (%d)\n", res);
Keith Busche1e5e562015-02-19 13:39:03 -07002210 return -EIO;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002211 }
2212
Matthew Wilcox51814232011-02-01 16:18:08 -05002213 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002214 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002215 dev->abort_limit = ctrl->acl + 1;
Keith Buscha7d2ce22014-04-29 11:41:28 -06002216 dev->vwc = ctrl->vwc;
Matthew Wilcox51814232011-02-01 16:18:08 -05002217 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2218 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2219 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002220 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002221 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002222 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002223 (pdev->device == 0x0953) && ctrl->vs[3]) {
2224 unsigned int max_hw_sectors;
2225
Keith Busch159b67d2013-04-09 17:13:20 -06002226 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002227 max_hw_sectors = dev->stripe_size >> (shift - 9);
2228 if (dev->max_hw_sectors) {
2229 dev->max_hw_sectors = min(max_hw_sectors,
2230 dev->max_hw_sectors);
2231 } else
2232 dev->max_hw_sectors = max_hw_sectors;
2233 }
Christoph Hellwigd29ec822015-05-22 11:12:46 +02002234 kfree(ctrl);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002235
2236 dev->tagset.ops = &nvme_mq_ops;
2237 dev->tagset.nr_hw_queues = dev->online_queues - 1;
2238 dev->tagset.timeout = NVME_IO_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002239 dev->tagset.numa_node = dev_to_node(dev->dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002240 dev->tagset.queue_depth =
2241 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Jens Axboeac3dd5b2015-01-22 12:07:58 -07002242 dev->tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002243 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
2244 dev->tagset.driver_data = dev;
2245
2246 if (blk_mq_alloc_tag_set(&dev->tagset))
Keith Busche1e5e562015-02-19 13:39:03 -07002247 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002248
Keith Busche1e5e562015-02-19 13:39:03 -07002249 for (i = 1; i <= nn; i++)
2250 nvme_alloc_ns(dev, i);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002251
Keith Busche1e5e562015-02-19 13:39:03 -07002252 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002253}
2254
Keith Busch0877cb02013-07-15 15:02:19 -06002255static int nvme_dev_map(struct nvme_dev *dev)
2256{
Keith Busch42f61422014-03-24 10:46:25 -06002257 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002258 int bars, result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002259 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002260
2261 if (pci_enable_device_mem(pdev))
2262 return result;
2263
2264 dev->entry[0].vector = pdev->irq;
2265 pci_set_master(pdev);
2266 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07002267 if (!bars)
2268 goto disable_pci;
2269
Keith Busch0877cb02013-07-15 15:02:19 -06002270 if (pci_request_selected_regions(pdev, bars, "nvme"))
2271 goto disable_pci;
2272
Christoph Hellwige75ec752015-05-22 11:12:39 +02002273 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
2274 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01002275 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002276
Keith Busch0877cb02013-07-15 15:02:19 -06002277 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2278 if (!dev->bar)
2279 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07002280
Keith Busch0e53d182013-12-10 13:10:39 -07002281 if (readl(&dev->bar->csts) == -1) {
2282 result = -ENODEV;
2283 goto unmap;
2284 }
Jens Axboee32efbf2014-11-14 09:49:26 -07002285
2286 /*
2287 * Some devices don't advertse INTx interrupts, pre-enable a single
2288 * MSIX vec for setup. We'll adjust this later.
2289 */
2290 if (!pdev->irq) {
2291 result = pci_enable_msix(pdev, dev->entry, 1);
2292 if (result < 0)
2293 goto unmap;
2294 }
2295
Keith Busch42f61422014-03-24 10:46:25 -06002296 cap = readq(&dev->bar->cap);
2297 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2298 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Keith Busch0877cb02013-07-15 15:02:19 -06002299 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2300
2301 return 0;
2302
Keith Busch0e53d182013-12-10 13:10:39 -07002303 unmap:
2304 iounmap(dev->bar);
2305 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002306 disable:
2307 pci_release_regions(pdev);
2308 disable_pci:
2309 pci_disable_device(pdev);
2310 return result;
2311}
2312
2313static void nvme_dev_unmap(struct nvme_dev *dev)
2314{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002315 struct pci_dev *pdev = to_pci_dev(dev->dev);
2316
2317 if (pdev->msi_enabled)
2318 pci_disable_msi(pdev);
2319 else if (pdev->msix_enabled)
2320 pci_disable_msix(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002321
2322 if (dev->bar) {
2323 iounmap(dev->bar);
2324 dev->bar = NULL;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002325 pci_release_regions(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002326 }
2327
Christoph Hellwige75ec752015-05-22 11:12:39 +02002328 if (pci_is_enabled(pdev))
2329 pci_disable_device(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002330}
2331
Keith Busch4d115422013-12-10 13:10:40 -07002332struct nvme_delq_ctx {
2333 struct task_struct *waiter;
2334 struct kthread_worker *worker;
2335 atomic_t refcount;
2336};
2337
2338static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2339{
2340 dq->waiter = current;
2341 mb();
2342
2343 for (;;) {
2344 set_current_state(TASK_KILLABLE);
2345 if (!atomic_read(&dq->refcount))
2346 break;
2347 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2348 fatal_signal_pending(current)) {
Keith Busch0fb59cb2015-01-07 18:55:50 -07002349 /*
2350 * Disable the controller first since we can't trust it
2351 * at this point, but leave the admin queue enabled
2352 * until all queue deletion requests are flushed.
2353 * FIXME: This may take a while if there are more h/w
2354 * queues than admin tags.
2355 */
Keith Busch4d115422013-12-10 13:10:40 -07002356 set_current_state(TASK_RUNNING);
Keith Busch4d115422013-12-10 13:10:40 -07002357 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
Keith Busch0fb59cb2015-01-07 18:55:50 -07002358 nvme_clear_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07002359 flush_kthread_worker(dq->worker);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002360 nvme_disable_queue(dev, 0);
Keith Busch4d115422013-12-10 13:10:40 -07002361 return;
2362 }
2363 }
2364 set_current_state(TASK_RUNNING);
2365}
2366
2367static void nvme_put_dq(struct nvme_delq_ctx *dq)
2368{
2369 atomic_dec(&dq->refcount);
2370 if (dq->waiter)
2371 wake_up_process(dq->waiter);
2372}
2373
2374static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2375{
2376 atomic_inc(&dq->refcount);
2377 return dq;
2378}
2379
2380static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2381{
2382 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
Keith Busch4d115422013-12-10 13:10:40 -07002383 nvme_put_dq(dq);
2384}
2385
2386static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2387 kthread_work_func_t fn)
2388{
2389 struct nvme_command c;
2390
2391 memset(&c, 0, sizeof(c));
2392 c.delete_queue.opcode = opcode;
2393 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2394
2395 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002396 return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
2397 ADMIN_TIMEOUT);
Keith Busch4d115422013-12-10 13:10:40 -07002398}
2399
2400static void nvme_del_cq_work_handler(struct kthread_work *work)
2401{
2402 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2403 cmdinfo.work);
2404 nvme_del_queue_end(nvmeq);
2405}
2406
2407static int nvme_delete_cq(struct nvme_queue *nvmeq)
2408{
2409 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2410 nvme_del_cq_work_handler);
2411}
2412
2413static void nvme_del_sq_work_handler(struct kthread_work *work)
2414{
2415 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2416 cmdinfo.work);
2417 int status = nvmeq->cmdinfo.status;
2418
2419 if (!status)
2420 status = nvme_delete_cq(nvmeq);
2421 if (status)
2422 nvme_del_queue_end(nvmeq);
2423}
2424
2425static int nvme_delete_sq(struct nvme_queue *nvmeq)
2426{
2427 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2428 nvme_del_sq_work_handler);
2429}
2430
2431static void nvme_del_queue_start(struct kthread_work *work)
2432{
2433 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2434 cmdinfo.work);
Keith Busch4d115422013-12-10 13:10:40 -07002435 if (nvme_delete_sq(nvmeq))
2436 nvme_del_queue_end(nvmeq);
2437}
2438
2439static void nvme_disable_io_queues(struct nvme_dev *dev)
2440{
2441 int i;
2442 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2443 struct nvme_delq_ctx dq;
2444 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2445 &worker, "nvme%d", dev->instance);
2446
2447 if (IS_ERR(kworker_task)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002448 dev_err(dev->dev,
Keith Busch4d115422013-12-10 13:10:40 -07002449 "Failed to create queue del task\n");
2450 for (i = dev->queue_count - 1; i > 0; i--)
2451 nvme_disable_queue(dev, i);
2452 return;
2453 }
2454
2455 dq.waiter = NULL;
2456 atomic_set(&dq.refcount, 0);
2457 dq.worker = &worker;
2458 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002459 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002460
2461 if (nvme_suspend_queue(nvmeq))
2462 continue;
2463 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2464 nvmeq->cmdinfo.worker = dq.worker;
2465 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2466 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2467 }
2468 nvme_wait_dq(&dq, dev);
2469 kthread_stop(kworker_task);
2470}
2471
Dan McLeranb9afca32014-04-07 17:10:11 -06002472/*
2473* Remove the node from the device list and check
2474* for whether or not we need to stop the nvme_thread.
2475*/
2476static void nvme_dev_list_remove(struct nvme_dev *dev)
2477{
2478 struct task_struct *tmp = NULL;
2479
2480 spin_lock(&dev_list_lock);
2481 list_del_init(&dev->node);
2482 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2483 tmp = nvme_thread;
2484 nvme_thread = NULL;
2485 }
2486 spin_unlock(&dev_list_lock);
2487
2488 if (tmp)
2489 kthread_stop(tmp);
2490}
2491
Keith Buschc9d3bf82015-01-07 18:55:52 -07002492static void nvme_freeze_queues(struct nvme_dev *dev)
2493{
2494 struct nvme_ns *ns;
2495
2496 list_for_each_entry(ns, &dev->namespaces, list) {
2497 blk_mq_freeze_queue_start(ns->queue);
2498
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002499 spin_lock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002500 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002501 spin_unlock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002502
2503 blk_mq_cancel_requeue_work(ns->queue);
2504 blk_mq_stop_hw_queues(ns->queue);
2505 }
2506}
2507
2508static void nvme_unfreeze_queues(struct nvme_dev *dev)
2509{
2510 struct nvme_ns *ns;
2511
2512 list_for_each_entry(ns, &dev->namespaces, list) {
2513 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2514 blk_mq_unfreeze_queue(ns->queue);
2515 blk_mq_start_stopped_hw_queues(ns->queue, true);
2516 blk_mq_kick_requeue_list(ns->queue);
2517 }
2518}
2519
Keith Buschf0b50732013-07-15 15:02:21 -06002520static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002521{
Keith Busch22404272013-07-15 15:02:20 -06002522 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06002523 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06002524
Dan McLeranb9afca32014-04-07 17:10:11 -06002525 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002526
Keith Buschc9d3bf82015-01-07 18:55:52 -07002527 if (dev->bar) {
2528 nvme_freeze_queues(dev);
Keith Busch7c1b2452014-06-25 11:18:12 -06002529 csts = readl(&dev->bar->csts);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002530 }
Keith Busch7c1b2452014-06-25 11:18:12 -06002531 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07002532 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002533 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002534 nvme_suspend_queue(nvmeq);
Keith Busch4d115422013-12-10 13:10:40 -07002535 }
2536 } else {
2537 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002538 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002539 nvme_disable_queue(dev, 0);
2540 }
Keith Buschf0b50732013-07-15 15:02:21 -06002541 nvme_dev_unmap(dev);
Keith Busch07836e62015-02-19 10:34:48 -07002542
2543 for (i = dev->queue_count - 1; i >= 0; i--)
2544 nvme_clear_queue(dev->queues[i]);
Keith Buschf0b50732013-07-15 15:02:21 -06002545}
2546
2547static void nvme_dev_remove(struct nvme_dev *dev)
2548{
Keith Busch9ac27092014-01-31 16:53:39 -07002549 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002550
Keith Busch9ac27092014-01-31 16:53:39 -07002551 list_for_each_entry(ns, &dev->namespaces, list) {
Keith Busche1e5e562015-02-19 13:39:03 -07002552 if (ns->disk->flags & GENHD_FL_UP) {
Keith Busch52b68d72015-02-23 09:16:21 -07002553 if (blk_get_integrity(ns->disk))
Keith Busche1e5e562015-02-19 13:39:03 -07002554 blk_integrity_unregister(ns->disk);
Keith Busch9ac27092014-01-31 16:53:39 -07002555 del_gendisk(ns->disk);
Keith Busche1e5e562015-02-19 13:39:03 -07002556 }
Keith Buschcef6a942015-01-07 18:55:51 -07002557 if (!blk_queue_dying(ns->queue)) {
2558 blk_mq_abort_requeue_list(ns->queue);
Keith Busch9ac27092014-01-31 16:53:39 -07002559 blk_cleanup_queue(ns->queue);
Keith Buschcef6a942015-01-07 18:55:51 -07002560 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002561 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002562}
2563
Matthew Wilcox091b6092011-02-10 09:56:01 -05002564static int nvme_setup_prp_pools(struct nvme_dev *dev)
2565{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002566 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05002567 PAGE_SIZE, PAGE_SIZE, 0);
2568 if (!dev->prp_page_pool)
2569 return -ENOMEM;
2570
Matthew Wilcox99802a72011-02-10 10:30:34 -05002571 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02002572 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05002573 256, 256, 0);
2574 if (!dev->prp_small_pool) {
2575 dma_pool_destroy(dev->prp_page_pool);
2576 return -ENOMEM;
2577 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002578 return 0;
2579}
2580
2581static void nvme_release_prp_pools(struct nvme_dev *dev)
2582{
2583 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002584 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002585}
2586
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002587static DEFINE_IDA(nvme_instance_ida);
2588
2589static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002590{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002591 int instance, error;
2592
2593 do {
2594 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2595 return -ENODEV;
2596
2597 spin_lock(&dev_list_lock);
2598 error = ida_get_new(&nvme_instance_ida, &instance);
2599 spin_unlock(&dev_list_lock);
2600 } while (error == -EAGAIN);
2601
2602 if (error)
2603 return -ENODEV;
2604
2605 dev->instance = instance;
2606 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002607}
2608
2609static void nvme_release_instance(struct nvme_dev *dev)
2610{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002611 spin_lock(&dev_list_lock);
2612 ida_remove(&nvme_instance_ida, dev->instance);
2613 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002614}
2615
Keith Busch9ac27092014-01-31 16:53:39 -07002616static void nvme_free_namespaces(struct nvme_dev *dev)
2617{
2618 struct nvme_ns *ns, *next;
2619
2620 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2621 list_del(&ns->list);
Keith Busch9e603522014-10-03 11:15:47 -06002622
2623 spin_lock(&dev_list_lock);
2624 ns->disk->private_data = NULL;
2625 spin_unlock(&dev_list_lock);
2626
Keith Busch9ac27092014-01-31 16:53:39 -07002627 put_disk(ns->disk);
2628 kfree(ns);
2629 }
2630}
2631
Keith Busch5e82e952013-02-19 10:17:58 -07002632static void nvme_free_dev(struct kref *kref)
2633{
2634 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002635
Christoph Hellwige75ec752015-05-22 11:12:39 +02002636 put_device(dev->dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07002637 put_device(dev->device);
Keith Busch9ac27092014-01-31 16:53:39 -07002638 nvme_free_namespaces(dev);
Indraneel M285dffc2014-12-11 08:24:18 -07002639 nvme_release_instance(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002640 blk_mq_free_tag_set(&dev->tagset);
Keith Buschea191d22015-01-07 18:55:49 -07002641 blk_put_queue(dev->admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002642 kfree(dev->queues);
2643 kfree(dev->entry);
2644 kfree(dev);
2645}
2646
2647static int nvme_dev_open(struct inode *inode, struct file *f)
2648{
Keith Buschb3fffde2015-02-03 11:21:42 -07002649 struct nvme_dev *dev;
2650 int instance = iminor(inode);
2651 int ret = -ENODEV;
2652
2653 spin_lock(&dev_list_lock);
2654 list_for_each_entry(dev, &dev_list, node) {
2655 if (dev->instance == instance) {
Keith Busch2e1d8442015-02-12 15:33:00 -07002656 if (!dev->admin_q) {
2657 ret = -EWOULDBLOCK;
2658 break;
2659 }
Keith Buschb3fffde2015-02-03 11:21:42 -07002660 if (!kref_get_unless_zero(&dev->kref))
2661 break;
2662 f->private_data = dev;
2663 ret = 0;
2664 break;
2665 }
2666 }
2667 spin_unlock(&dev_list_lock);
2668
2669 return ret;
Keith Busch5e82e952013-02-19 10:17:58 -07002670}
2671
2672static int nvme_dev_release(struct inode *inode, struct file *f)
2673{
2674 struct nvme_dev *dev = f->private_data;
2675 kref_put(&dev->kref, nvme_free_dev);
2676 return 0;
2677}
2678
2679static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2680{
2681 struct nvme_dev *dev = f->private_data;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002682 struct nvme_ns *ns;
2683
Keith Busch5e82e952013-02-19 10:17:58 -07002684 switch (cmd) {
2685 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002686 return nvme_user_cmd(dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06002687 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002688 if (list_empty(&dev->namespaces))
2689 return -ENOTTY;
2690 ns = list_first_entry(&dev->namespaces, struct nvme_ns, list);
2691 return nvme_user_cmd(dev, ns, (void __user *)arg);
Keith Busch5e82e952013-02-19 10:17:58 -07002692 default:
2693 return -ENOTTY;
2694 }
2695}
2696
2697static const struct file_operations nvme_dev_fops = {
2698 .owner = THIS_MODULE,
2699 .open = nvme_dev_open,
2700 .release = nvme_dev_release,
2701 .unlocked_ioctl = nvme_dev_ioctl,
2702 .compat_ioctl = nvme_dev_ioctl,
2703};
2704
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002705static void nvme_set_irq_hints(struct nvme_dev *dev)
2706{
2707 struct nvme_queue *nvmeq;
2708 int i;
2709
2710 for (i = 0; i < dev->online_queues; i++) {
2711 nvmeq = dev->queues[i];
2712
Keith Busch42483222015-06-01 09:29:54 -06002713 if (!nvmeq->tags || !(*nvmeq->tags))
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002714 continue;
2715
2716 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
Keith Busch42483222015-06-01 09:29:54 -06002717 blk_mq_tags_cpumask(*nvmeq->tags));
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002718 }
2719}
2720
Keith Buschf0b50732013-07-15 15:02:21 -06002721static int nvme_dev_start(struct nvme_dev *dev)
2722{
2723 int result;
Dan McLeranb9afca32014-04-07 17:10:11 -06002724 bool start_thread = false;
Keith Buschf0b50732013-07-15 15:02:21 -06002725
2726 result = nvme_dev_map(dev);
2727 if (result)
2728 return result;
2729
2730 result = nvme_configure_admin_queue(dev);
2731 if (result)
2732 goto unmap;
2733
2734 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002735 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2736 start_thread = true;
2737 nvme_thread = NULL;
2738 }
Keith Buschf0b50732013-07-15 15:02:21 -06002739 list_add(&dev->node, &dev_list);
2740 spin_unlock(&dev_list_lock);
2741
Dan McLeranb9afca32014-04-07 17:10:11 -06002742 if (start_thread) {
2743 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
Keith Busch387caa52014-09-22 13:46:19 -06002744 wake_up_all(&nvme_kthread_wait);
Dan McLeranb9afca32014-04-07 17:10:11 -06002745 } else
2746 wait_event_killable(nvme_kthread_wait, nvme_thread);
2747
2748 if (IS_ERR_OR_NULL(nvme_thread)) {
2749 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2750 goto disable;
2751 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002752
2753 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002754 result = nvme_alloc_admin_tags(dev);
2755 if (result)
2756 goto disable;
Dan McLeranb9afca32014-04-07 17:10:11 -06002757
Keith Buschf0b50732013-07-15 15:02:21 -06002758 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002759 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07002760 goto free_tags;
Keith Buschf0b50732013-07-15 15:02:21 -06002761
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002762 nvme_set_irq_hints(dev);
2763
Keith Busch1efccc92015-03-31 10:37:17 -06002764 dev->event_limit = 1;
Keith Buschd82e8bf2013-09-05 14:45:07 -06002765 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002766
Keith Busch0fb59cb2015-01-07 18:55:50 -07002767 free_tags:
2768 nvme_dev_remove_admin(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002769 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002770 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06002771 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002772 unmap:
2773 nvme_dev_unmap(dev);
2774 return result;
2775}
2776
Keith Busch9a6b9452013-12-10 13:10:36 -07002777static int nvme_remove_dead_ctrl(void *arg)
2778{
2779 struct nvme_dev *dev = (struct nvme_dev *)arg;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002780 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002781
2782 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06002783 pci_stop_and_remove_bus_device_locked(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002784 kref_put(&dev->kref, nvme_free_dev);
2785 return 0;
2786}
2787
2788static void nvme_remove_disks(struct work_struct *ws)
2789{
Keith Busch9a6b9452013-12-10 13:10:36 -07002790 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2791
Keith Busch5a92e702014-02-21 14:13:44 -07002792 nvme_free_queues(dev, 1);
Keith Busch302c6722014-07-18 11:40:20 -06002793 nvme_dev_remove(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002794}
2795
2796static int nvme_dev_resume(struct nvme_dev *dev)
2797{
2798 int ret;
2799
2800 ret = nvme_dev_start(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002801 if (ret)
Keith Busch9a6b9452013-12-10 13:10:36 -07002802 return ret;
Keith Buschbadc34d2014-06-23 14:25:35 -06002803 if (dev->online_queues < 2) {
Keith Busch9a6b9452013-12-10 13:10:36 -07002804 spin_lock(&dev_list_lock);
Tejun Heo9ca97372014-03-07 10:24:49 -05002805 dev->reset_workfn = nvme_remove_disks;
Keith Busch9a6b9452013-12-10 13:10:36 -07002806 queue_work(nvme_workq, &dev->reset_work);
2807 spin_unlock(&dev_list_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002808 } else {
2809 nvme_unfreeze_queues(dev);
2810 nvme_set_irq_hints(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002811 }
2812 return 0;
2813}
2814
2815static void nvme_dev_reset(struct nvme_dev *dev)
2816{
2817 nvme_dev_shutdown(dev);
2818 if (nvme_dev_resume(dev)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002819 dev_warn(dev->dev, "Device failed to resume\n");
Keith Busch9a6b9452013-12-10 13:10:36 -07002820 kref_get(&dev->kref);
2821 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2822 dev->instance))) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002823 dev_err(dev->dev,
Keith Busch9a6b9452013-12-10 13:10:36 -07002824 "Failed to start controller remove task\n");
2825 kref_put(&dev->kref, nvme_free_dev);
2826 }
2827 }
2828}
2829
2830static void nvme_reset_failed_dev(struct work_struct *ws)
2831{
2832 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2833 nvme_dev_reset(dev);
2834}
2835
Tejun Heo9ca97372014-03-07 10:24:49 -05002836static void nvme_reset_workfn(struct work_struct *work)
2837{
2838 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
2839 dev->reset_workfn(work);
2840}
2841
Keith Busch2e1d8442015-02-12 15:33:00 -07002842static void nvme_async_probe(struct work_struct *work);
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002843static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002844{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002845 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002846 struct nvme_dev *dev;
2847
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002848 node = dev_to_node(&pdev->dev);
2849 if (node == NUMA_NO_NODE)
2850 set_dev_node(&pdev->dev, 0);
2851
2852 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002853 if (!dev)
2854 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002855 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
2856 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002857 if (!dev->entry)
2858 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002859 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2860 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002861 if (!dev->queues)
2862 goto free;
2863
2864 INIT_LIST_HEAD(&dev->namespaces);
Tejun Heo9ca97372014-03-07 10:24:49 -05002865 dev->reset_workfn = nvme_reset_failed_dev;
2866 INIT_WORK(&dev->reset_work, nvme_reset_workfn);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002867 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002868 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002869 result = nvme_set_instance(dev);
2870 if (result)
Keith Buscha96d4f52014-08-19 19:15:59 -06002871 goto put_pci;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002872
Matthew Wilcox091b6092011-02-10 09:56:01 -05002873 result = nvme_setup_prp_pools(dev);
2874 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002875 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002876
Keith Buschfb35e912014-03-03 11:09:47 -07002877 kref_init(&dev->kref);
Keith Buschb3fffde2015-02-03 11:21:42 -07002878 dev->device = device_create(nvme_class, &pdev->dev,
2879 MKDEV(nvme_char_major, dev->instance),
2880 dev, "nvme%d", dev->instance);
2881 if (IS_ERR(dev->device)) {
2882 result = PTR_ERR(dev->device);
Keith Busch2e1d8442015-02-12 15:33:00 -07002883 goto release_pools;
Keith Buschb3fffde2015-02-03 11:21:42 -07002884 }
2885 get_device(dev->device);
2886
Keith Busche6e96d72015-03-23 09:32:37 -06002887 INIT_LIST_HEAD(&dev->node);
Keith Busch2e1d8442015-02-12 15:33:00 -07002888 INIT_WORK(&dev->probe_work, nvme_async_probe);
2889 schedule_work(&dev->probe_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002890 return 0;
2891
Keith Busch0877cb02013-07-15 15:02:19 -06002892 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05002893 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002894 release:
2895 nvme_release_instance(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002896 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02002897 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002898 free:
2899 kfree(dev->queues);
2900 kfree(dev->entry);
2901 kfree(dev);
2902 return result;
2903}
2904
Keith Busch2e1d8442015-02-12 15:33:00 -07002905static void nvme_async_probe(struct work_struct *work)
2906{
2907 struct nvme_dev *dev = container_of(work, struct nvme_dev, probe_work);
2908 int result;
2909
2910 result = nvme_dev_start(dev);
2911 if (result)
2912 goto reset;
2913
2914 if (dev->online_queues > 1)
2915 result = nvme_dev_add(dev);
2916 if (result)
2917 goto reset;
2918
2919 nvme_set_irq_hints(dev);
Keith Busch2e1d8442015-02-12 15:33:00 -07002920 return;
2921 reset:
Keith Busch07836e62015-02-19 10:34:48 -07002922 if (!work_busy(&dev->reset_work)) {
2923 dev->reset_workfn = nvme_reset_failed_dev;
2924 queue_work(nvme_workq, &dev->reset_work);
2925 }
Keith Busch2e1d8442015-02-12 15:33:00 -07002926}
2927
Keith Buschf0d54a52014-05-02 10:40:43 -06002928static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2929{
Keith Buscha6739472014-06-23 16:03:21 -06002930 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002931
Keith Buscha6739472014-06-23 16:03:21 -06002932 if (prepare)
2933 nvme_dev_shutdown(dev);
2934 else
2935 nvme_dev_resume(dev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002936}
2937
Keith Busch09ece142014-01-27 11:29:40 -05002938static void nvme_shutdown(struct pci_dev *pdev)
2939{
2940 struct nvme_dev *dev = pci_get_drvdata(pdev);
2941 nvme_dev_shutdown(dev);
2942}
2943
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002944static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002945{
2946 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002947
2948 spin_lock(&dev_list_lock);
2949 list_del_init(&dev->node);
2950 spin_unlock(&dev_list_lock);
2951
2952 pci_set_drvdata(pdev, NULL);
Keith Busch2e1d8442015-02-12 15:33:00 -07002953 flush_work(&dev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07002954 flush_work(&dev->reset_work);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002955 nvme_dev_shutdown(dev);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002956 nvme_dev_remove(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002957 nvme_dev_remove_admin(dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07002958 device_destroy(nvme_class, MKDEV(nvme_char_major, dev->instance));
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002959 nvme_free_queues(dev, 0);
Keith Busch9a6b9452013-12-10 13:10:36 -07002960 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002961 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002962}
2963
2964/* These functions are yet to be implemented */
2965#define nvme_error_detected NULL
2966#define nvme_dump_registers NULL
2967#define nvme_link_reset NULL
2968#define nvme_slot_reset NULL
2969#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002970
Jingoo Han671a6012014-02-13 11:19:14 +09002971#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002972static int nvme_suspend(struct device *dev)
2973{
2974 struct pci_dev *pdev = to_pci_dev(dev);
2975 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2976
2977 nvme_dev_shutdown(ndev);
2978 return 0;
2979}
2980
2981static int nvme_resume(struct device *dev)
2982{
2983 struct pci_dev *pdev = to_pci_dev(dev);
2984 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002985
Keith Busch9a6b9452013-12-10 13:10:36 -07002986 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Tejun Heo9ca97372014-03-07 10:24:49 -05002987 ndev->reset_workfn = nvme_reset_failed_dev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002988 queue_work(nvme_workq, &ndev->reset_work);
2989 }
2990 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002991}
Jingoo Han671a6012014-02-13 11:19:14 +09002992#endif
Keith Buschcd638942013-07-15 15:02:23 -06002993
2994static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002995
Stephen Hemminger1d352032012-09-07 09:33:17 -07002996static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002997 .error_detected = nvme_error_detected,
2998 .mmio_enabled = nvme_dump_registers,
2999 .link_reset = nvme_link_reset,
3000 .slot_reset = nvme_slot_reset,
3001 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06003002 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003003};
3004
3005/* Move to pci_ids.h later */
3006#define PCI_CLASS_STORAGE_EXPRESS 0x010802
3007
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003008static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003009 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
3010 { 0, }
3011};
3012MODULE_DEVICE_TABLE(pci, nvme_id_table);
3013
3014static struct pci_driver nvme_driver = {
3015 .name = "nvme",
3016 .id_table = nvme_id_table,
3017 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003018 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05003019 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06003020 .driver = {
3021 .pm = &nvme_dev_pm_ops,
3022 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003023 .err_handler = &nvme_err_handler,
3024};
3025
3026static int __init nvme_init(void)
3027{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003028 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003029
Dan McLeranb9afca32014-04-07 17:10:11 -06003030 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003031
Keith Busch9a6b9452013-12-10 13:10:36 -07003032 nvme_workq = create_singlethread_workqueue("nvme");
3033 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06003034 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07003035
Keith Busch5c42ea12012-07-25 16:05:18 -06003036 result = register_blkdev(nvme_major, "nvme");
3037 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07003038 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06003039 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003040 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003041
Keith Buschb3fffde2015-02-03 11:21:42 -07003042 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
3043 &nvme_dev_fops);
3044 if (result < 0)
3045 goto unregister_blkdev;
3046 else if (result > 0)
3047 nvme_char_major = result;
3048
3049 nvme_class = class_create(THIS_MODULE, "nvme");
Alexey Khoroshilovc7270402015-03-07 01:43:41 +03003050 if (IS_ERR(nvme_class)) {
3051 result = PTR_ERR(nvme_class);
Keith Buschb3fffde2015-02-03 11:21:42 -07003052 goto unregister_chrdev;
Alexey Khoroshilovc7270402015-03-07 01:43:41 +03003053 }
Keith Buschb3fffde2015-02-03 11:21:42 -07003054
Keith Buschf3db22f2014-06-11 11:51:35 -06003055 result = pci_register_driver(&nvme_driver);
3056 if (result)
Keith Buschb3fffde2015-02-03 11:21:42 -07003057 goto destroy_class;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003058 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003059
Keith Buschb3fffde2015-02-03 11:21:42 -07003060 destroy_class:
3061 class_destroy(nvme_class);
3062 unregister_chrdev:
3063 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003064 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003065 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003066 kill_workq:
3067 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003068 return result;
3069}
3070
3071static void __exit nvme_exit(void)
3072{
3073 pci_unregister_driver(&nvme_driver);
3074 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003075 destroy_workqueue(nvme_workq);
Keith Buschb3fffde2015-02-03 11:21:42 -07003076 class_destroy(nvme_class);
3077 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Dan McLeranb9afca32014-04-07 17:10:11 -06003078 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04003079 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003080}
3081
3082MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
3083MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07003084MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003085module_init(nvme_init);
3086module_exit(nvme_exit);