blob: b97fc3fe0916a6b6fd3fb2be32be44ce3c137b39 [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>
Keith Buscha5768aa2015-06-01 14:28:14 -060032#include <linux/list_sort.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050033#include <linux/mm.h>
34#include <linux/module.h>
35#include <linux/moduleparam.h>
36#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050037#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040038#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050039#include <linux/sched.h>
40#include <linux/slab.h>
Keith Busche1e5e562015-02-19 13:39:03 -070041#include <linux/t10-pi.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050042#include <linux/types.h>
Vishal Verma5d0f6132013-03-04 18:40:58 -070043#include <scsi/sg.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090044#include <asm-generic/io-64-nonatomic-lo-hi.h>
45
Keith Buschb3fffde2015-02-03 11:21:42 -070046#define NVME_MINORS (1U << MINORBITS)
Keith Busch9d43cf62014-05-13 11:42:02 -060047#define NVME_Q_DEPTH 1024
Jens Axboed31af0a2015-03-06 12:56:13 -070048#define NVME_AQ_DEPTH 256
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050049#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
50#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Keith Busch9d43cf62014-05-13 11:42:02 -060051#define ADMIN_TIMEOUT (admin_timeout * HZ)
Dan McLeran2484f402014-07-01 09:33:32 -060052#define SHUTDOWN_TIMEOUT (shutdown_timeout * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050053
Keith Busch9d43cf62014-05-13 11:42:02 -060054static unsigned char admin_timeout = 60;
55module_param(admin_timeout, byte, 0644);
56MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050057
Matthew Wilcoxbd676082014-06-03 23:04:30 -040058unsigned char nvme_io_timeout = 30;
59module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
Keith Buschb3550842014-04-04 11:43:36 -060060MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050061
Dan McLeran2484f402014-07-01 09:33:32 -060062static unsigned char shutdown_timeout = 5;
63module_param(shutdown_timeout, byte, 0644);
64MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
65
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050066static int nvme_major;
67module_param(nvme_major, int, 0);
68
Keith Buschb3fffde2015-02-03 11:21:42 -070069static int nvme_char_major;
70module_param(nvme_char_major, int, 0);
71
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050072static int use_threaded_interrupts;
73module_param(use_threaded_interrupts, int, 0);
74
Jon Derrick8ffaadf2015-07-20 10:14:09 -060075static bool use_cmb_sqes = true;
76module_param(use_cmb_sqes, bool, 0644);
77MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
78
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050079static DEFINE_SPINLOCK(dev_list_lock);
80static LIST_HEAD(dev_list);
81static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070082static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060083static wait_queue_head_t nvme_kthread_wait;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050084
Keith Buschb3fffde2015-02-03 11:21:42 -070085static struct class *nvme_class;
86
Keith Buschd4b4ff82013-12-10 13:10:37 -070087static void nvme_reset_failed_dev(struct work_struct *ws);
Keith Busch4cc06522015-06-05 10:30:08 -060088static int nvme_reset(struct nvme_dev *dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -070089static int nvme_process_cq(struct nvme_queue *nvmeq);
Keith Buschd4b4ff82013-12-10 13:10:37 -070090
Keith Busch4d115422013-12-10 13:10:40 -070091struct async_cmd_info {
92 struct kthread_work work;
93 struct kthread_worker *worker;
Matias Bjørlinga4aea562014-11-04 08:20:14 -070094 struct request *req;
Keith Busch4d115422013-12-10 13:10:40 -070095 u32 result;
96 int status;
97 void *ctx;
98};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050099
100/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500101 * An NVM Express queue. Each device has at least two (one for admin
102 * commands and one for I/O commands).
103 */
104struct nvme_queue {
105 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500106 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500107 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500108 spinlock_t q_lock;
109 struct nvme_command *sq_cmds;
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600110 struct nvme_command __iomem *sq_cmds_io;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500111 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600112 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500113 dma_addr_t sq_dma_addr;
114 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500115 u32 __iomem *q_db;
116 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700117 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500118 u16 sq_head;
119 u16 sq_tail;
120 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700121 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400122 u8 cq_phase;
123 u8 cqe_seen;
Keith Busch4d115422013-12-10 13:10:40 -0700124 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500125};
126
127/*
128 * Check we didin't inadvertently grow the command struct
129 */
130static inline void _nvme_check_size(void)
131{
132 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
133 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
134 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
135 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
136 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400137 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700138 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500139 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
140 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
141 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
142 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600143 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500144}
145
Keith Buschedd10d32014-04-03 16:45:23 -0600146typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400147 struct nvme_completion *);
148
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500149struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400150 nvme_completion_fn fn;
151 void *ctx;
Keith Buschc30341d2013-12-10 13:10:38 -0700152 int aborted;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700153 struct nvme_queue *nvmeq;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700154 struct nvme_iod iod[0];
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500155};
156
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700157/*
158 * Max size of iod being embedded in the request payload
159 */
160#define NVME_INT_PAGES 2
161#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->page_size)
Chong Yuanfda631f2015-03-27 09:21:32 +0800162#define NVME_INT_MASK 0x01
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700163
164/*
165 * Will slightly overestimate the number of pages needed. This is OK
166 * as it only leads to a small amount of wasted memory for the lifetime of
167 * the I/O.
168 */
169static int nvme_npages(unsigned size, struct nvme_dev *dev)
170{
171 unsigned nprps = DIV_ROUND_UP(size + dev->page_size, dev->page_size);
172 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
173}
174
175static unsigned int nvme_cmd_size(struct nvme_dev *dev)
176{
177 unsigned int ret = sizeof(struct nvme_cmd_info);
178
179 ret += sizeof(struct nvme_iod);
180 ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
181 ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
182
183 return ret;
184}
185
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700186static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
187 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500188{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700189 struct nvme_dev *dev = data;
190 struct nvme_queue *nvmeq = dev->queues[0];
191
Keith Busch42483222015-06-01 09:29:54 -0600192 WARN_ON(hctx_idx != 0);
193 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
194 WARN_ON(nvmeq->tags);
195
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700196 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600197 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700198 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500199}
200
Keith Busch4af0e212015-06-08 10:08:13 -0600201static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
202{
203 struct nvme_queue *nvmeq = hctx->driver_data;
204
205 nvmeq->tags = NULL;
206}
207
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700208static int nvme_admin_init_request(void *data, struct request *req,
209 unsigned int hctx_idx, unsigned int rq_idx,
210 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600211{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700212 struct nvme_dev *dev = data;
213 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
214 struct nvme_queue *nvmeq = dev->queues[0];
215
216 BUG_ON(!nvmeq);
217 cmd->nvmeq = nvmeq;
218 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600219}
220
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700221static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
222 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500223{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700224 struct nvme_dev *dev = data;
Keith Busch42483222015-06-01 09:29:54 -0600225 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500226
Keith Busch42483222015-06-01 09:29:54 -0600227 if (!nvmeq->tags)
228 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500229
Keith Busch42483222015-06-01 09:29:54 -0600230 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700231 hctx->driver_data = nvmeq;
232 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500233}
234
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700235static int nvme_init_request(void *data, struct request *req,
236 unsigned int hctx_idx, unsigned int rq_idx,
237 unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500238{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700239 struct nvme_dev *dev = data;
240 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
241 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
242
243 BUG_ON(!nvmeq);
244 cmd->nvmeq = nvmeq;
245 return 0;
246}
247
248static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
249 nvme_completion_fn handler)
250{
251 cmd->fn = handler;
252 cmd->ctx = ctx;
253 cmd->aborted = 0;
Keith Buschc917dfe2015-01-07 18:55:48 -0700254 blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500255}
256
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700257static void *iod_get_private(struct nvme_iod *iod)
258{
259 return (void *) (iod->private & ~0x1UL);
260}
261
262/*
263 * If bit 0 is set, the iod is embedded in the request payload.
264 */
265static bool iod_should_kfree(struct nvme_iod *iod)
266{
Chong Yuanfda631f2015-03-27 09:21:32 +0800267 return (iod->private & NVME_INT_MASK) == 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700268}
269
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400270/* Special values must be less than 0x1000 */
271#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500272#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
273#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
274#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500275
Keith Buschedd10d32014-04-03 16:45:23 -0600276static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400277 struct nvme_completion *cqe)
278{
279 if (ctx == CMD_CTX_CANCELLED)
280 return;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400281 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600282 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400283 "completed id %d twice on queue %d\n",
284 cqe->command_id, le16_to_cpup(&cqe->sq_id));
285 return;
286 }
287 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600288 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400289 "invalid id %d completed on queue %d\n",
290 cqe->command_id, le16_to_cpup(&cqe->sq_id));
291 return;
292 }
Keith Buschedd10d32014-04-03 16:45:23 -0600293 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400294}
295
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700296static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
297{
298 void *ctx;
299
300 if (fn)
301 *fn = cmd->fn;
302 ctx = cmd->ctx;
303 cmd->fn = special_completion;
304 cmd->ctx = CMD_CTX_CANCELLED;
305 return ctx;
306}
307
308static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
309 struct nvme_completion *cqe)
310{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700311 u32 result = le32_to_cpup(&cqe->result);
312 u16 status = le16_to_cpup(&cqe->status) >> 1;
313
314 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
315 ++nvmeq->dev->event_limit;
Keith Buscha5768aa2015-06-01 14:28:14 -0600316 if (status != NVME_SC_SUCCESS)
317 return;
318
319 switch (result & 0xff07) {
320 case NVME_AER_NOTICE_NS_CHANGED:
321 dev_info(nvmeq->q_dmadev, "rescanning\n");
322 schedule_work(&nvmeq->dev->scan_work);
323 default:
324 dev_warn(nvmeq->q_dmadev, "async event result %08x\n", result);
325 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700326}
327
328static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
329 struct nvme_completion *cqe)
330{
331 struct request *req = ctx;
332
333 u16 status = le16_to_cpup(&cqe->status) >> 1;
334 u32 result = le32_to_cpup(&cqe->result);
335
Keith Busch42483222015-06-01 09:29:54 -0600336 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700337
338 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
339 ++nvmeq->dev->abort_limit;
340}
341
Keith Buschedd10d32014-04-03 16:45:23 -0600342static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700343 struct nvme_completion *cqe)
344{
345 struct async_cmd_info *cmdinfo = ctx;
346 cmdinfo->result = le32_to_cpup(&cqe->result);
347 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
348 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
Keith Busch42483222015-06-01 09:29:54 -0600349 blk_mq_free_request(cmdinfo->req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700350}
351
352static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
353 unsigned int tag)
354{
Keith Busch42483222015-06-01 09:29:54 -0600355 struct request *req = blk_mq_tag_to_rq(*nvmeq->tags, tag);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700356
357 return blk_mq_rq_to_pdu(req);
Keith Busch4d115422013-12-10 13:10:40 -0700358}
359
Matthew Wilcox184d2942011-05-11 21:36:38 -0400360/*
361 * Called with local interrupts disabled and the q_lock held. May not sleep.
362 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700363static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400364 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500365{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700366 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400367 void *ctx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700368 if (tag >= nvmeq->q_depth) {
369 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500370 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400371 }
Keith Busch859361a2012-08-02 14:05:59 -0600372 if (fn)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700373 *fn = cmd->fn;
374 ctx = cmd->ctx;
375 cmd->fn = special_completion;
376 cmd->ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400377 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500378}
379
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500380/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400381 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500382 * @nvmeq: The queue to use
383 * @cmd: The command to send
384 *
385 * Safe to use from interrupt context
386 */
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530387static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
388 struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500389{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700390 u16 tail = nvmeq->sq_tail;
391
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600392 if (nvmeq->sq_cmds_io)
393 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
394 else
395 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
396
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500397 if (++tail == nvmeq->q_depth)
398 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500399 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500400 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500401}
402
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530403static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700404{
405 unsigned long flags;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700406 spin_lock_irqsave(&nvmeq->q_lock, flags);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530407 __nvme_submit_cmd(nvmeq, cmd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700408 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700409}
410
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500411static __le64 **iod_list(struct nvme_iod *iod)
412{
413 return ((void *)iod) + iod->offset;
414}
415
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700416static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
417 unsigned nseg, unsigned long private)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500418{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700419 iod->private = private;
420 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
421 iod->npages = -1;
422 iod->length = nbytes;
423 iod->nents = 0;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500424}
425
426static struct nvme_iod *
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700427__nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
428 unsigned long priv, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500429{
430 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700431 sizeof(__le64 *) * nvme_npages(bytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500432 sizeof(struct scatterlist) * nseg, gfp);
433
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700434 if (iod)
435 iod_init(iod, bytes, nseg, priv);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500436
437 return iod;
438}
439
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700440static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
441 gfp_t gfp)
442{
443 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
444 sizeof(struct nvme_dsm_range);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700445 struct nvme_iod *iod;
446
447 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
448 size <= NVME_INT_BYTES(dev)) {
449 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
450
451 iod = cmd->iod;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700452 iod_init(iod, size, rq->nr_phys_segments,
Chong Yuanfda631f2015-03-27 09:21:32 +0800453 (unsigned long) rq | NVME_INT_MASK);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700454 return iod;
455 }
456
457 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
458 (unsigned long) rq, gfp);
459}
460
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200461static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500462{
Keith Busch1d090622014-06-23 11:34:01 -0600463 const int last_prp = dev->page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500464 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500465 __le64 **list = iod_list(iod);
466 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500467
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500468 if (iod->npages == 0)
469 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
470 for (i = 0; i < iod->npages; i++) {
471 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500472 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500473 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500474 prp_dma = next_prp_dma;
475 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700476
477 if (iod_should_kfree(iod))
478 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500479}
480
Keith Buschb4ff9c82014-08-29 09:06:12 -0600481static int nvme_error_status(u16 status)
482{
483 switch (status & 0x7ff) {
484 case NVME_SC_SUCCESS:
485 return 0;
486 case NVME_SC_CAP_EXCEEDED:
487 return -ENOSPC;
488 default:
489 return -EIO;
490 }
491}
492
Keith Busch52b68d72015-02-23 09:16:21 -0700493#ifdef CONFIG_BLK_DEV_INTEGRITY
Keith Busche1e5e562015-02-19 13:39:03 -0700494static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
495{
496 if (be32_to_cpu(pi->ref_tag) == v)
497 pi->ref_tag = cpu_to_be32(p);
498}
499
500static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
501{
502 if (be32_to_cpu(pi->ref_tag) == p)
503 pi->ref_tag = cpu_to_be32(v);
504}
505
506/**
507 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
508 *
509 * The virtual start sector is the one that was originally submitted by the
510 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
511 * start sector may be different. Remap protection information to match the
512 * physical LBA on writes, and back to the original seed on reads.
513 *
514 * Type 0 and 3 do not have a ref tag, so no remapping required.
515 */
516static void nvme_dif_remap(struct request *req,
517 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
518{
519 struct nvme_ns *ns = req->rq_disk->private_data;
520 struct bio_integrity_payload *bip;
521 struct t10_pi_tuple *pi;
522 void *p, *pmap;
523 u32 i, nlb, ts, phys, virt;
524
525 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
526 return;
527
528 bip = bio_integrity(req->bio);
529 if (!bip)
530 return;
531
532 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
Keith Busche1e5e562015-02-19 13:39:03 -0700533
534 p = pmap;
535 virt = bip_get_seed(bip);
536 phys = nvme_block_nr(ns, blk_rq_pos(req));
537 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
538 ts = ns->disk->integrity->tuple_size;
539
540 for (i = 0; i < nlb; i++, virt++, phys++) {
541 pi = (struct t10_pi_tuple *)p;
542 dif_swap(phys, virt, pi);
543 p += ts;
544 }
545 kunmap_atomic(pmap);
546}
547
Keith Busch52b68d72015-02-23 09:16:21 -0700548static int nvme_noop_verify(struct blk_integrity_iter *iter)
549{
550 return 0;
551}
552
553static int nvme_noop_generate(struct blk_integrity_iter *iter)
554{
555 return 0;
556}
557
558struct blk_integrity nvme_meta_noop = {
559 .name = "NVME_META_NOOP",
560 .generate_fn = nvme_noop_generate,
561 .verify_fn = nvme_noop_verify,
562};
563
564static void nvme_init_integrity(struct nvme_ns *ns)
565{
566 struct blk_integrity integrity;
567
568 switch (ns->pi_type) {
569 case NVME_NS_DPS_PI_TYPE3:
570 integrity = t10_pi_type3_crc;
571 break;
572 case NVME_NS_DPS_PI_TYPE1:
573 case NVME_NS_DPS_PI_TYPE2:
574 integrity = t10_pi_type1_crc;
575 break;
576 default:
577 integrity = nvme_meta_noop;
578 break;
579 }
580 integrity.tuple_size = ns->ms;
581 blk_integrity_register(ns->disk, &integrity);
582 blk_queue_max_integrity_segments(ns->queue, 1);
583}
584#else /* CONFIG_BLK_DEV_INTEGRITY */
585static void nvme_dif_remap(struct request *req,
586 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
587{
588}
589static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
590{
591}
592static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
593{
594}
595static void nvme_init_integrity(struct nvme_ns *ns)
596{
597}
598#endif
599
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700600static void req_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500601 struct nvme_completion *cqe)
602{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500603 struct nvme_iod *iod = ctx;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700604 struct request *req = iod_get_private(iod);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700605 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
606
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500607 u16 status = le16_to_cpup(&cqe->status) >> 1;
608
Keith Buschedd10d32014-04-03 16:45:23 -0600609 if (unlikely(status)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700610 if (!(status & NVME_SC_DNR || blk_noretry_request(req))
611 && (jiffies - req->start_time) < req->timeout) {
Keith Buschc9d3bf82015-01-07 18:55:52 -0700612 unsigned long flags;
613
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700614 blk_mq_requeue_request(req);
Keith Buschc9d3bf82015-01-07 18:55:52 -0700615 spin_lock_irqsave(req->q->queue_lock, flags);
616 if (!blk_queue_stopped(req->q))
617 blk_mq_kick_requeue_list(req->q);
618 spin_unlock_irqrestore(req->q->queue_lock, flags);
Keith Buschedd10d32014-04-03 16:45:23 -0600619 return;
620 }
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200621 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
Keith Busch17188bb2015-06-08 10:08:14 -0600622 if (cmd_rq->ctx == CMD_CTX_CANCELLED)
623 req->errors = -EINTR;
624 else
625 req->errors = status;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200626 } else {
627 req->errors = nvme_error_status(status);
628 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700629 } else
630 req->errors = 0;
Keith Buscha0a931d2015-05-22 12:28:31 -0600631 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
632 u32 result = le32_to_cpup(&cqe->result);
633 req->special = (void *)(uintptr_t)result;
634 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700635
636 if (cmd_rq->aborted)
Christoph Hellwige75ec752015-05-22 11:12:39 +0200637 dev_warn(nvmeq->dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700638 "completing aborted command with status:%04x\n",
639 status);
640
Keith Busche1e5e562015-02-19 13:39:03 -0700641 if (iod->nents) {
Christoph Hellwige75ec752015-05-22 11:12:39 +0200642 dma_unmap_sg(nvmeq->dev->dev, iod->sg, iod->nents,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700643 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busche1e5e562015-02-19 13:39:03 -0700644 if (blk_integrity_rq(req)) {
645 if (!rq_data_dir(req))
646 nvme_dif_remap(req, nvme_dif_complete);
Christoph Hellwige75ec752015-05-22 11:12:39 +0200647 dma_unmap_sg(nvmeq->dev->dev, iod->meta_sg, 1,
Keith Busche1e5e562015-02-19 13:39:03 -0700648 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
649 }
650 }
Keith Buschedd10d32014-04-03 16:45:23 -0600651 nvme_free_iod(nvmeq->dev, iod);
Keith Busch3291fa52014-04-28 12:30:52 -0600652
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700653 blk_mq_complete_request(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500654}
655
Matthew Wilcox184d2942011-05-11 21:36:38 -0400656/* length is in bytes. gfp flags indicates whether we may sleep. */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200657static int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod,
658 int total_len, gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500659{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500660 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500661 int length = total_len;
662 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500663 int dma_len = sg_dma_len(sg);
664 u64 dma_addr = sg_dma_address(sg);
Murali Iyerf137e0f2015-03-26 11:07:51 -0500665 u32 page_size = dev->page_size;
666 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500667 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500668 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500669 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500670 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500671
Keith Busch1d090622014-06-23 11:34:01 -0600672 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500673 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500674 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500675
Keith Busch1d090622014-06-23 11:34:01 -0600676 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500677 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600678 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500679 } else {
680 sg = sg_next(sg);
681 dma_addr = sg_dma_address(sg);
682 dma_len = sg_dma_len(sg);
683 }
684
Keith Busch1d090622014-06-23 11:34:01 -0600685 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600686 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500687 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500688 }
689
Keith Busch1d090622014-06-23 11:34:01 -0600690 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500691 if (nprps <= (256 / 8)) {
692 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500693 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500694 } else {
695 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500696 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500697 }
698
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400699 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
700 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600701 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500702 iod->npages = -1;
Keith Busch1d090622014-06-23 11:34:01 -0600703 return (total_len - length) + page_size;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400704 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500705 list[0] = prp_list;
706 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500707 i = 0;
708 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600709 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500710 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400711 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500712 if (!prp_list)
713 return total_len - length;
714 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400715 prp_list[0] = old_prp_list[i - 1];
716 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
717 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500718 }
719 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600720 dma_len -= page_size;
721 dma_addr += page_size;
722 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500723 if (length <= 0)
724 break;
725 if (dma_len > 0)
726 continue;
727 BUG_ON(dma_len < 0);
728 sg = sg_next(sg);
729 dma_addr = sg_dma_address(sg);
730 dma_len = sg_dma_len(sg);
731 }
732
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500733 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500734}
735
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200736static void nvme_submit_priv(struct nvme_queue *nvmeq, struct request *req,
737 struct nvme_iod *iod)
738{
Jon Derrick498c4392015-07-20 10:14:08 -0600739 struct nvme_command cmnd;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200740
Jon Derrick498c4392015-07-20 10:14:08 -0600741 memcpy(&cmnd, req->cmd, sizeof(cmnd));
742 cmnd.rw.command_id = req->tag;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200743 if (req->nr_phys_segments) {
Jon Derrick498c4392015-07-20 10:14:08 -0600744 cmnd.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
745 cmnd.rw.prp2 = cpu_to_le64(iod->first_dma);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200746 }
747
Jon Derrick498c4392015-07-20 10:14:08 -0600748 __nvme_submit_cmd(nvmeq, &cmnd);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200749}
750
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700751/*
752 * We reuse the small pool to allocate the 16-byte range here as it is not
753 * worth having a special pool for these or additional cases to handle freeing
754 * the iod.
755 */
756static void nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
757 struct request *req, struct nvme_iod *iod)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700758{
Keith Buschedd10d32014-04-03 16:45:23 -0600759 struct nvme_dsm_range *range =
760 (struct nvme_dsm_range *)iod_list(iod)[0];
Jon Derrick498c4392015-07-20 10:14:08 -0600761 struct nvme_command cmnd;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700762
Keith Busch0e5e4f02012-11-09 16:33:05 -0700763 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700764 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
765 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700766
Jon Derrick498c4392015-07-20 10:14:08 -0600767 memset(&cmnd, 0, sizeof(cmnd));
768 cmnd.dsm.opcode = nvme_cmd_dsm;
769 cmnd.dsm.command_id = req->tag;
770 cmnd.dsm.nsid = cpu_to_le32(ns->ns_id);
771 cmnd.dsm.prp1 = cpu_to_le64(iod->first_dma);
772 cmnd.dsm.nr = 0;
773 cmnd.dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
Keith Busch0e5e4f02012-11-09 16:33:05 -0700774
Jon Derrick498c4392015-07-20 10:14:08 -0600775 __nvme_submit_cmd(nvmeq, &cmnd);
Keith Busch0e5e4f02012-11-09 16:33:05 -0700776}
777
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700778static void nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500779 int cmdid)
780{
Jon Derrick498c4392015-07-20 10:14:08 -0600781 struct nvme_command cmnd;
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500782
Jon Derrick498c4392015-07-20 10:14:08 -0600783 memset(&cmnd, 0, sizeof(cmnd));
784 cmnd.common.opcode = nvme_cmd_flush;
785 cmnd.common.command_id = cmdid;
786 cmnd.common.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500787
Jon Derrick498c4392015-07-20 10:14:08 -0600788 __nvme_submit_cmd(nvmeq, &cmnd);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500789}
790
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700791static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod,
792 struct nvme_ns *ns)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500793{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700794 struct request *req = iod_get_private(iod);
Jon Derrick498c4392015-07-20 10:14:08 -0600795 struct nvme_command cmnd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700796 u16 control = 0;
797 u32 dsmgmt = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500798
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700799 if (req->cmd_flags & REQ_FUA)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500800 control |= NVME_RW_FUA;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700801 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500802 control |= NVME_RW_LR;
803
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700804 if (req->cmd_flags & REQ_RAHEAD)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500805 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
806
Jon Derrick498c4392015-07-20 10:14:08 -0600807 memset(&cmnd, 0, sizeof(cmnd));
808 cmnd.rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
809 cmnd.rw.command_id = req->tag;
810 cmnd.rw.nsid = cpu_to_le32(ns->ns_id);
811 cmnd.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
812 cmnd.rw.prp2 = cpu_to_le64(iod->first_dma);
813 cmnd.rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
814 cmnd.rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500815
Alok Pandeye19b1272015-08-26 08:56:14 -0600816 if (ns->ms) {
Keith Busche1e5e562015-02-19 13:39:03 -0700817 switch (ns->pi_type) {
818 case NVME_NS_DPS_PI_TYPE3:
819 control |= NVME_RW_PRINFO_PRCHK_GUARD;
820 break;
821 case NVME_NS_DPS_PI_TYPE1:
822 case NVME_NS_DPS_PI_TYPE2:
823 control |= NVME_RW_PRINFO_PRCHK_GUARD |
824 NVME_RW_PRINFO_PRCHK_REF;
Jon Derrick498c4392015-07-20 10:14:08 -0600825 cmnd.rw.reftag = cpu_to_le32(
Keith Busche1e5e562015-02-19 13:39:03 -0700826 nvme_block_nr(ns, blk_rq_pos(req)));
827 break;
828 }
Alok Pandeye19b1272015-08-26 08:56:14 -0600829 if (blk_integrity_rq(req))
830 cmnd.rw.metadata =
831 cpu_to_le64(sg_dma_address(iod->meta_sg));
832 else
833 control |= NVME_RW_PRINFO_PRACT;
834 }
Keith Busche1e5e562015-02-19 13:39:03 -0700835
Jon Derrick498c4392015-07-20 10:14:08 -0600836 cmnd.rw.control = cpu_to_le16(control);
837 cmnd.rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500838
Jon Derrick498c4392015-07-20 10:14:08 -0600839 __nvme_submit_cmd(nvmeq, &cmnd);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500840
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500841 return 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600842}
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500843
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200844/*
845 * NOTE: ns is NULL when called on the admin queue.
846 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700847static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
848 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600849{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700850 struct nvme_ns *ns = hctx->queue->queuedata;
851 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200852 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700853 struct request *req = bd->rq;
854 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
Keith Buschedd10d32014-04-03 16:45:23 -0600855 struct nvme_iod *iod;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700856 enum dma_data_direction dma_dir;
Keith Buschedd10d32014-04-03 16:45:23 -0600857
Keith Busche1e5e562015-02-19 13:39:03 -0700858 /*
859 * If formated with metadata, require the block layer provide a buffer
860 * unless this namespace is formated such that the metadata can be
861 * stripped/generated by the controller with PRACT=1.
862 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200863 if (ns && ns->ms && !blk_integrity_rq(req)) {
Keith Busch71feb362015-06-19 11:07:30 -0600864 if (!(ns->pi_type && ns->ms == 8) &&
865 req->cmd_type != REQ_TYPE_DRV_PRIV) {
Keith Busche1e5e562015-02-19 13:39:03 -0700866 req->errors = -EFAULT;
867 blk_mq_complete_request(req);
868 return BLK_MQ_RQ_QUEUE_OK;
869 }
870 }
871
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200872 iod = nvme_alloc_iod(req, dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600873 if (!iod)
Jens Axboefe543032014-12-11 13:58:39 -0700874 return BLK_MQ_RQ_QUEUE_BUSY;
Keith Buschedd10d32014-04-03 16:45:23 -0600875
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700876 if (req->cmd_flags & REQ_DISCARD) {
Keith Buschedd10d32014-04-03 16:45:23 -0600877 void *range;
878 /*
879 * We reuse the small pool to allocate the 16-byte range here
880 * as it is not worth having a special pool for these or
881 * additional cases to handle freeing the iod.
882 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200883 range = dma_pool_alloc(dev->prp_small_pool, GFP_ATOMIC,
Keith Buschedd10d32014-04-03 16:45:23 -0600884 &iod->first_dma);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700885 if (!range)
Jens Axboefe543032014-12-11 13:58:39 -0700886 goto retry_cmd;
Keith Buschedd10d32014-04-03 16:45:23 -0600887 iod_list(iod)[0] = (__le64 *)range;
888 iod->npages = 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700889 } else if (req->nr_phys_segments) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700890 dma_dir = rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
Keith Buschedd10d32014-04-03 16:45:23 -0600891
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700892 sg_init_table(iod->sg, req->nr_phys_segments);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700893 iod->nents = blk_rq_map_sg(req->q, req, iod->sg);
Jens Axboefe543032014-12-11 13:58:39 -0700894 if (!iod->nents)
895 goto error_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700896
897 if (!dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir))
Jens Axboefe543032014-12-11 13:58:39 -0700898 goto retry_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700899
Jens Axboefe543032014-12-11 13:58:39 -0700900 if (blk_rq_bytes(req) !=
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200901 nvme_setup_prps(dev, iod, blk_rq_bytes(req), GFP_ATOMIC)) {
902 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
Jens Axboefe543032014-12-11 13:58:39 -0700903 goto retry_cmd;
904 }
Keith Busche1e5e562015-02-19 13:39:03 -0700905 if (blk_integrity_rq(req)) {
906 if (blk_rq_count_integrity_sg(req->q, req->bio) != 1)
907 goto error_cmd;
908
909 sg_init_table(iod->meta_sg, 1);
910 if (blk_rq_map_integrity_sg(
911 req->q, req->bio, iod->meta_sg) != 1)
912 goto error_cmd;
913
914 if (rq_data_dir(req))
915 nvme_dif_remap(req, nvme_dif_prep);
916
917 if (!dma_map_sg(nvmeq->q_dmadev, iod->meta_sg, 1, dma_dir))
918 goto error_cmd;
919 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700920 }
921
Keith Busch9af87852014-12-03 17:07:13 -0700922 nvme_set_info(cmd, iod, req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700923 spin_lock_irq(&nvmeq->q_lock);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200924 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
925 nvme_submit_priv(nvmeq, req, iod);
926 else if (req->cmd_flags & REQ_DISCARD)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700927 nvme_submit_discard(nvmeq, ns, req, iod);
928 else if (req->cmd_flags & REQ_FLUSH)
929 nvme_submit_flush(nvmeq, ns, req->tag);
930 else
931 nvme_submit_iod(nvmeq, iod, ns);
932
933 nvme_process_cq(nvmeq);
934 spin_unlock_irq(&nvmeq->q_lock);
935 return BLK_MQ_RQ_QUEUE_OK;
936
Jens Axboefe543032014-12-11 13:58:39 -0700937 error_cmd:
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200938 nvme_free_iod(dev, iod);
Jens Axboefe543032014-12-11 13:58:39 -0700939 return BLK_MQ_RQ_QUEUE_ERROR;
940 retry_cmd:
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200941 nvme_free_iod(dev, iod);
Jens Axboefe543032014-12-11 13:58:39 -0700942 return BLK_MQ_RQ_QUEUE_BUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500943}
944
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400945static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500946{
Matthew Wilcox82123462011-01-20 13:24:06 -0500947 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500948
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500949 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500950 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500951
952 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400953 void *ctx;
954 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500955 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500956 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500957 break;
958 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
959 if (++head == nvmeq->q_depth) {
960 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500961 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500962 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700963 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600964 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500965 }
966
967 /* If the controller ignores the cq head doorbell and continuously
968 * writes to the queue, it is theoretically possible to wrap around
969 * the queue twice and mistakenly return IRQ_NONE. Linux only
970 * requires that 0.1% of your interrupts are handled, so this isn't
971 * a big problem.
972 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500973 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400974 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500975
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800976 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500977 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500978 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500979
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400980 nvmeq->cqe_seen = 1;
981 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500982}
983
984static irqreturn_t nvme_irq(int irq, void *data)
985{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500986 irqreturn_t result;
987 struct nvme_queue *nvmeq = data;
988 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400989 nvme_process_cq(nvmeq);
990 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
991 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500992 spin_unlock(&nvmeq->q_lock);
993 return result;
994}
995
996static irqreturn_t nvme_irq_check(int irq, void *data)
997{
998 struct nvme_queue *nvmeq = data;
999 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
1000 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
1001 return IRQ_NONE;
1002 return IRQ_WAKE_THREAD;
1003}
1004
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001005/*
1006 * Returns 0 on success. If the result is negative, it's a Linux error code;
1007 * if the result is positive, it's an NVM Express status code
1008 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001009int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
1010 void *buffer, void __user *ubuffer, unsigned bufflen,
1011 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001012{
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001013 bool write = cmd->common.opcode & 1;
1014 struct bio *bio = NULL;
Christoph Hellwigf705f832015-05-22 11:12:38 +02001015 struct request *req;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001016 int ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001017
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001018 req = blk_mq_alloc_request(q, write, GFP_KERNEL, false);
Christoph Hellwigf705f832015-05-22 11:12:38 +02001019 if (IS_ERR(req))
1020 return PTR_ERR(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001021
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001022 req->cmd_type = REQ_TYPE_DRV_PRIV;
Matias Bjørlinge112af02015-06-05 14:54:24 +02001023 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001024 req->__data_len = 0;
1025 req->__sector = (sector_t) -1;
1026 req->bio = req->biotail = NULL;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001027
Keith Buschf4ff4142015-05-28 09:48:54 -06001028 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001029
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001030 req->cmd = (unsigned char *)cmd;
1031 req->cmd_len = sizeof(struct nvme_command);
Keith Buscha0a931d2015-05-22 12:28:31 -06001032 req->special = (void *)0;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -05001033
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001034 if (buffer && bufflen) {
1035 ret = blk_rq_map_kern(q, req, buffer, bufflen, __GFP_WAIT);
1036 if (ret)
1037 goto out;
1038 } else if (ubuffer && bufflen) {
1039 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen, __GFP_WAIT);
1040 if (ret)
1041 goto out;
1042 bio = req->bio;
1043 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001044
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001045 blk_execute_rq(req->q, NULL, req, 0);
1046 if (bio)
1047 blk_rq_unmap_user(bio);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001048 if (result)
Keith Buscha0a931d2015-05-22 12:28:31 -06001049 *result = (u32)(uintptr_t)req->special;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001050 ret = req->errors;
1051 out:
Christoph Hellwigf705f832015-05-22 11:12:38 +02001052 blk_mq_free_request(req);
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001053 return ret;
Christoph Hellwigf705f832015-05-22 11:12:38 +02001054}
1055
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001056int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
1057 void *buffer, unsigned bufflen)
Christoph Hellwigf705f832015-05-22 11:12:38 +02001058{
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001059 return __nvme_submit_sync_cmd(q, cmd, buffer, NULL, bufflen, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001060}
1061
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001062static int nvme_submit_async_admin_req(struct nvme_dev *dev)
1063{
1064 struct nvme_queue *nvmeq = dev->queues[0];
1065 struct nvme_command c;
1066 struct nvme_cmd_info *cmd_info;
1067 struct request *req;
1068
Keith Busch1efccc92015-03-31 10:37:17 -06001069 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC, true);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001070 if (IS_ERR(req))
1071 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001072
Keith Buschc917dfe2015-01-07 18:55:48 -07001073 req->cmd_flags |= REQ_NO_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001074 cmd_info = blk_mq_rq_to_pdu(req);
Keith Busch1efccc92015-03-31 10:37:17 -06001075 nvme_set_info(cmd_info, NULL, async_req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001076
1077 memset(&c, 0, sizeof(c));
1078 c.common.opcode = nvme_admin_async_event;
1079 c.common.command_id = req->tag;
1080
Keith Busch42483222015-06-01 09:29:54 -06001081 blk_mq_free_request(req);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301082 __nvme_submit_cmd(nvmeq, &c);
1083 return 0;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001084}
1085
1086static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
Keith Busch4d115422013-12-10 13:10:40 -07001087 struct nvme_command *cmd,
1088 struct async_cmd_info *cmdinfo, unsigned timeout)
1089{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001090 struct nvme_queue *nvmeq = dev->queues[0];
1091 struct request *req;
1092 struct nvme_cmd_info *cmd_rq;
Keith Busch4d115422013-12-10 13:10:40 -07001093
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001094 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_KERNEL, false);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001095 if (IS_ERR(req))
1096 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001097
1098 req->timeout = timeout;
1099 cmd_rq = blk_mq_rq_to_pdu(req);
1100 cmdinfo->req = req;
1101 nvme_set_info(cmd_rq, cmdinfo, async_completion);
Keith Busch4d115422013-12-10 13:10:40 -07001102 cmdinfo->status = -EINTR;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001103
1104 cmd->common.command_id = req->tag;
1105
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301106 nvme_submit_cmd(nvmeq, cmd);
1107 return 0;
Keith Busch4d115422013-12-10 13:10:40 -07001108}
1109
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001110static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1111{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001112 struct nvme_command c;
1113
1114 memset(&c, 0, sizeof(c));
1115 c.delete_queue.opcode = opcode;
1116 c.delete_queue.qid = cpu_to_le16(id);
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_cq(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_CQ_IRQ_ENABLED;
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_cq.opcode = nvme_admin_create_cq;
1133 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1134 c.create_cq.cqid = cpu_to_le16(qid);
1135 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1136 c.create_cq.cq_flags = cpu_to_le16(flags);
1137 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
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_alloc_sq(struct nvme_dev *dev, u16 qid,
1143 struct nvme_queue *nvmeq)
1144{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001145 struct nvme_command c;
1146 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1147
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001148 /*
1149 * Note: we (ab)use the fact the the prp fields survive if no data
1150 * is attached to the request.
1151 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001152 memset(&c, 0, sizeof(c));
1153 c.create_sq.opcode = nvme_admin_create_sq;
1154 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1155 c.create_sq.sqid = cpu_to_le16(qid);
1156 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1157 c.create_sq.sq_flags = cpu_to_le16(flags);
1158 c.create_sq.cqid = cpu_to_le16(qid);
1159
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001160 return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001161}
1162
1163static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1164{
1165 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1166}
1167
1168static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1169{
1170 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1171}
1172
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001173int nvme_identify_ctrl(struct nvme_dev *dev, struct nvme_id_ctrl **id)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001174{
Andrew Mortone44ac582015-06-27 12:20:34 -06001175 struct nvme_command c = { };
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001176 int error;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001177
Andrew Mortone44ac582015-06-27 12:20:34 -06001178 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
1179 c.identify.opcode = nvme_admin_identify;
1180 c.identify.cns = cpu_to_le32(1);
1181
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001182 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
1183 if (!*id)
1184 return -ENOMEM;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001185
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001186 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
1187 sizeof(struct nvme_id_ctrl));
1188 if (error)
1189 kfree(*id);
1190 return error;
1191}
1192
1193int nvme_identify_ns(struct nvme_dev *dev, unsigned nsid,
1194 struct nvme_id_ns **id)
1195{
Andrew Mortone44ac582015-06-27 12:20:34 -06001196 struct nvme_command c = { };
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001197 int error;
1198
Andrew Mortone44ac582015-06-27 12:20:34 -06001199 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
1200 c.identify.opcode = nvme_admin_identify,
1201 c.identify.nsid = cpu_to_le32(nsid),
1202
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001203 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
1204 if (!*id)
1205 return -ENOMEM;
1206
1207 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
1208 sizeof(struct nvme_id_ns));
1209 if (error)
1210 kfree(*id);
1211 return error;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001212}
1213
Vishal Verma5d0f6132013-03-04 18:40:58 -07001214int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -06001215 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001216{
1217 struct nvme_command c;
1218
1219 memset(&c, 0, sizeof(c));
1220 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -06001221 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001222 c.features.prp1 = cpu_to_le64(dma_addr);
1223 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001224
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001225 return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, NULL, 0,
1226 result, 0);
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001227}
1228
Vishal Verma5d0f6132013-03-04 18:40:58 -07001229int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
1230 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001231{
1232 struct nvme_command c;
1233
1234 memset(&c, 0, sizeof(c));
1235 c.features.opcode = nvme_admin_set_features;
1236 c.features.prp1 = cpu_to_le64(dma_addr);
1237 c.features.fid = cpu_to_le32(fid);
1238 c.features.dword11 = cpu_to_le32(dword11);
1239
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001240 return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, NULL, 0,
1241 result, 0);
1242}
1243
1244int nvme_get_log_page(struct nvme_dev *dev, struct nvme_smart_log **log)
1245{
Andrew Mortone44ac582015-06-27 12:20:34 -06001246 struct nvme_command c = { };
1247 int error;
1248
1249 c.common.opcode = nvme_admin_get_log_page,
1250 c.common.nsid = cpu_to_le32(0xFFFFFFFF),
1251 c.common.cdw10[0] = cpu_to_le32(
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001252 (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
1253 NVME_LOG_SMART),
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001254
1255 *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
1256 if (!*log)
1257 return -ENOMEM;
1258
1259 error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
1260 sizeof(struct nvme_smart_log));
1261 if (error)
1262 kfree(*log);
1263 return error;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001264}
1265
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001266/**
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001267 * nvme_abort_req - Attempt aborting a request
Keith Buschc30341d2013-12-10 13:10:38 -07001268 *
1269 * Schedule controller reset if the command was already aborted once before and
1270 * still hasn't been returned to the driver, or if this is the admin queue.
1271 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001272static void nvme_abort_req(struct request *req)
Keith Buschc30341d2013-12-10 13:10:38 -07001273{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001274 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1275 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001276 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001277 struct request *abort_req;
1278 struct nvme_cmd_info *abort_cmd;
1279 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -07001280
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001281 if (!nvmeq->qid || cmd_rq->aborted) {
Keith Busch7a509a62015-01-07 18:55:53 -07001282 unsigned long flags;
1283
1284 spin_lock_irqsave(&dev_list_lock, flags);
Keith Buschc30341d2013-12-10 13:10:38 -07001285 if (work_busy(&dev->reset_work))
Keith Busch7a509a62015-01-07 18:55:53 -07001286 goto out;
Keith Buschc30341d2013-12-10 13:10:38 -07001287 list_del_init(&dev->node);
Christoph Hellwige75ec752015-05-22 11:12:39 +02001288 dev_warn(dev->dev, "I/O %d QID %d timeout, reset controller\n",
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001289 req->tag, nvmeq->qid);
Tejun Heo9ca97372014-03-07 10:24:49 -05001290 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschc30341d2013-12-10 13:10:38 -07001291 queue_work(nvme_workq, &dev->reset_work);
Keith Busch7a509a62015-01-07 18:55:53 -07001292 out:
1293 spin_unlock_irqrestore(&dev_list_lock, flags);
Keith Buschc30341d2013-12-10 13:10:38 -07001294 return;
1295 }
1296
1297 if (!dev->abort_limit)
1298 return;
1299
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001300 abort_req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC,
1301 false);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001302 if (IS_ERR(abort_req))
Keith Buschc30341d2013-12-10 13:10:38 -07001303 return;
1304
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001305 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1306 nvme_set_info(abort_cmd, abort_req, abort_completion);
1307
Keith Buschc30341d2013-12-10 13:10:38 -07001308 memset(&cmd, 0, sizeof(cmd));
1309 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001310 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001311 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001312 cmd.abort.command_id = abort_req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001313
1314 --dev->abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001315 cmd_rq->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -07001316
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001317 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", req->tag,
Keith Buschc30341d2013-12-10 13:10:38 -07001318 nvmeq->qid);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301319 nvme_submit_cmd(dev->queues[0], &cmd);
Keith Buschc30341d2013-12-10 13:10:38 -07001320}
1321
Keith Busch42483222015-06-01 09:29:54 -06001322static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001323{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001324 struct nvme_queue *nvmeq = data;
1325 void *ctx;
1326 nvme_completion_fn fn;
1327 struct nvme_cmd_info *cmd;
Keith Buschcef6a942015-01-07 18:55:51 -07001328 struct nvme_completion cqe;
1329
1330 if (!blk_mq_request_started(req))
1331 return;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001332
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001333 cmd = blk_mq_rq_to_pdu(req);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001334
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001335 if (cmd->ctx == CMD_CTX_CANCELLED)
1336 return;
1337
Keith Buschcef6a942015-01-07 18:55:51 -07001338 if (blk_queue_dying(req->q))
1339 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1340 else
1341 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1342
1343
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001344 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1345 req->tag, nvmeq->qid);
1346 ctx = cancel_cmd_info(cmd, &fn);
1347 fn(nvmeq, ctx, &cqe);
1348}
1349
1350static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
1351{
1352 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
1353 struct nvme_queue *nvmeq = cmd->nvmeq;
1354
Keith Busch07836e62015-02-19 10:34:48 -07001355 dev_warn(nvmeq->q_dmadev, "Timeout I/O %d QID %d\n", req->tag,
1356 nvmeq->qid);
1357 spin_lock_irq(&nvmeq->q_lock);
1358 nvme_abort_req(req);
1359 spin_unlock_irq(&nvmeq->q_lock);
1360
Keith Busch7a509a62015-01-07 18:55:53 -07001361 /*
1362 * The aborted req will be completed on receiving the abort req.
1363 * We enable the timer again. If hit twice, it'll cause a device reset,
1364 * as the device then is in a faulty state.
1365 */
Keith Busch07836e62015-02-19 10:34:48 -07001366 return BLK_EH_RESET_TIMER;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001367}
1368
Keith Buschf435c282014-07-07 09:14:42 -06001369static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001370{
1371 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1372 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001373 if (nvmeq->sq_cmds)
1374 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
Matthew Wilcox9e866772012-08-03 13:55:56 -04001375 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1376 kfree(nvmeq);
1377}
1378
Keith Buscha1a5ef92013-12-16 13:50:00 -05001379static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001380{
1381 int i;
1382
Keith Buscha1a5ef92013-12-16 13:50:00 -05001383 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001384 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001385 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001386 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001387 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001388 }
Keith Busch22404272013-07-15 15:02:20 -06001389}
1390
Keith Busch4d115422013-12-10 13:10:40 -07001391/**
1392 * nvme_suspend_queue - put queue into suspended state
1393 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001394 */
1395static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001396{
Keith Busch2b25d982014-12-22 12:59:04 -07001397 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001398
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001399 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001400 if (nvmeq->cq_vector == -1) {
1401 spin_unlock_irq(&nvmeq->q_lock);
1402 return 1;
1403 }
1404 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Keith Busch42f61422014-03-24 10:46:25 -06001405 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001406 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001407 spin_unlock_irq(&nvmeq->q_lock);
1408
Keith Busch6df3dbc2015-03-26 13:49:33 -06001409 if (!nvmeq->qid && nvmeq->dev->admin_q)
1410 blk_mq_freeze_queue_start(nvmeq->dev->admin_q);
1411
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001412 irq_set_affinity_hint(vector, NULL);
1413 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001414
Keith Busch4d115422013-12-10 13:10:40 -07001415 return 0;
1416}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001417
Keith Busch4d115422013-12-10 13:10:40 -07001418static void nvme_clear_queue(struct nvme_queue *nvmeq)
1419{
Keith Busch22404272013-07-15 15:02:20 -06001420 spin_lock_irq(&nvmeq->q_lock);
Keith Busch42483222015-06-01 09:29:54 -06001421 if (nvmeq->tags && *nvmeq->tags)
1422 blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001423 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001424}
1425
Keith Busch4d115422013-12-10 13:10:40 -07001426static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1427{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001428 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001429
1430 if (!nvmeq)
1431 return;
1432 if (nvme_suspend_queue(nvmeq))
1433 return;
1434
Keith Busch0e53d182013-12-10 13:10:39 -07001435 /* Don't tell the adapter to delete the admin queue.
1436 * Don't tell a removed adapter to delete IO queues. */
1437 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001438 adapter_delete_sq(dev, qid);
1439 adapter_delete_cq(dev, qid);
1440 }
Keith Busch07836e62015-02-19 10:34:48 -07001441
1442 spin_lock_irq(&nvmeq->q_lock);
1443 nvme_process_cq(nvmeq);
1444 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001445}
1446
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001447static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1448 int entry_size)
1449{
1450 int q_depth = dev->q_depth;
1451 unsigned q_size_aligned = roundup(q_depth * entry_size, dev->page_size);
1452
1453 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001454 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
1455 mem_per_q = round_down(mem_per_q, dev->page_size);
1456 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001457
1458 /*
1459 * Ensure the reduced q_depth is above some threshold where it
1460 * would be better to map queues in system memory with the
1461 * original depth
1462 */
1463 if (q_depth < 64)
1464 return -ENOMEM;
1465 }
1466
1467 return q_depth;
1468}
1469
1470static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1471 int qid, int depth)
1472{
1473 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
1474 unsigned offset = (qid - 1) *
1475 roundup(SQ_SIZE(depth), dev->page_size);
1476 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1477 nvmeq->sq_cmds_io = dev->cmb + offset;
1478 } else {
1479 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1480 &nvmeq->sq_dma_addr, GFP_KERNEL);
1481 if (!nvmeq->sq_cmds)
1482 return -ENOMEM;
1483 }
1484
1485 return 0;
1486}
1487
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001488static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Keith Busch2b25d982014-12-22 12:59:04 -07001489 int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001490{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001491 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001492 if (!nvmeq)
1493 return NULL;
1494
Christoph Hellwige75ec752015-05-22 11:12:39 +02001495 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001496 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001497 if (!nvmeq->cqes)
1498 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001499
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001500 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001501 goto free_cqdma;
1502
Christoph Hellwige75ec752015-05-22 11:12:39 +02001503 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001504 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001505 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1506 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001507 spin_lock_init(&nvmeq->q_lock);
1508 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001509 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001510 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001511 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001512 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001513 nvmeq->cq_vector = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001514 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001515
Jon Derrick36a7e992015-05-27 12:26:23 -06001516 /* make sure queue descriptor is set before queue count, for kthread */
1517 mb();
1518 dev->queue_count++;
1519
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001520 return nvmeq;
1521
1522 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001523 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001524 nvmeq->cq_dma_addr);
1525 free_nvmeq:
1526 kfree(nvmeq);
1527 return NULL;
1528}
1529
Matthew Wilcox30010822011-01-20 09:10:15 -05001530static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1531 const char *name)
1532{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001533 if (use_threaded_interrupts)
1534 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001535 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001536 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001537 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001538 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001539}
1540
Keith Busch22404272013-07-15 15:02:20 -06001541static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001542{
Keith Busch22404272013-07-15 15:02:20 -06001543 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001544
Keith Busch7be50e92014-09-10 15:48:47 -06001545 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001546 nvmeq->sq_tail = 0;
1547 nvmeq->cq_head = 0;
1548 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001549 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001550 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001551 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001552 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001553}
1554
1555static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1556{
1557 struct nvme_dev *dev = nvmeq->dev;
1558 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001559
Keith Busch2b25d982014-12-22 12:59:04 -07001560 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001561 result = adapter_alloc_cq(dev, qid, nvmeq);
1562 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001563 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001564
1565 result = adapter_alloc_sq(dev, qid, nvmeq);
1566 if (result < 0)
1567 goto release_cq;
1568
Matthew Wilcox3193f072014-01-27 15:57:22 -05001569 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001570 if (result < 0)
1571 goto release_sq;
1572
Keith Busch22404272013-07-15 15:02:20 -06001573 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001574 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001575
1576 release_sq:
1577 adapter_delete_sq(dev, qid);
1578 release_cq:
1579 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001580 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001581}
1582
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001583static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1584{
1585 unsigned long timeout;
1586 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1587
1588 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1589
1590 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1591 msleep(100);
1592 if (fatal_signal_pending(current))
1593 return -EINTR;
1594 if (time_after(jiffies, timeout)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001595 dev_err(dev->dev,
Matthew Wilcox27e81662014-04-11 11:58:45 -04001596 "Device not ready; aborting %s\n", enabled ?
1597 "initialisation" : "reset");
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001598 return -ENODEV;
1599 }
1600 }
1601
1602 return 0;
1603}
1604
1605/*
1606 * If the device has been passed off to us in an enabled state, just clear
1607 * the enabled bit. The spec says we should set the 'shutdown notification
1608 * bits', but doing so may cause the device to complete commands to the
1609 * admin queue ... and we don't know what memory that might be pointing at!
1610 */
1611static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1612{
Dan McLeran01079522014-06-23 08:24:36 -06001613 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1614 dev->ctrl_config &= ~NVME_CC_ENABLE;
1615 writel(dev->ctrl_config, &dev->bar->cc);
Matthew Wilcox44af1462013-05-04 06:43:17 -04001616
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001617 return nvme_wait_ready(dev, cap, false);
1618}
1619
1620static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1621{
Dan McLeran01079522014-06-23 08:24:36 -06001622 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1623 dev->ctrl_config |= NVME_CC_ENABLE;
1624 writel(dev->ctrl_config, &dev->bar->cc);
1625
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001626 return nvme_wait_ready(dev, cap, true);
1627}
1628
Keith Busch1894d8f2013-07-15 15:02:22 -06001629static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1630{
1631 unsigned long timeout;
Keith Busch1894d8f2013-07-15 15:02:22 -06001632
Dan McLeran01079522014-06-23 08:24:36 -06001633 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1634 dev->ctrl_config |= NVME_CC_SHN_NORMAL;
1635
1636 writel(dev->ctrl_config, &dev->bar->cc);
Keith Busch1894d8f2013-07-15 15:02:22 -06001637
Dan McLeran2484f402014-07-01 09:33:32 -06001638 timeout = SHUTDOWN_TIMEOUT + jiffies;
Keith Busch1894d8f2013-07-15 15:02:22 -06001639 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1640 NVME_CSTS_SHST_CMPLT) {
1641 msleep(100);
1642 if (fatal_signal_pending(current))
1643 return -EINTR;
1644 if (time_after(jiffies, timeout)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001645 dev_err(dev->dev,
Keith Busch1894d8f2013-07-15 15:02:22 -06001646 "Device shutdown incomplete; abort shutdown\n");
1647 return -ENODEV;
1648 }
1649 }
1650
1651 return 0;
1652}
1653
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001654static struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001655 .queue_rq = nvme_queue_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001656 .map_queue = blk_mq_map_queue,
1657 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001658 .exit_hctx = nvme_admin_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001659 .init_request = nvme_admin_init_request,
1660 .timeout = nvme_timeout,
1661};
1662
1663static struct blk_mq_ops nvme_mq_ops = {
1664 .queue_rq = nvme_queue_rq,
1665 .map_queue = blk_mq_map_queue,
1666 .init_hctx = nvme_init_hctx,
1667 .init_request = nvme_init_request,
1668 .timeout = nvme_timeout,
1669};
1670
Keith Buschea191d22015-01-07 18:55:49 -07001671static void nvme_dev_remove_admin(struct nvme_dev *dev)
1672{
1673 if (dev->admin_q && !blk_queue_dying(dev->admin_q)) {
1674 blk_cleanup_queue(dev->admin_q);
1675 blk_mq_free_tag_set(&dev->admin_tagset);
1676 }
1677}
1678
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001679static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1680{
1681 if (!dev->admin_q) {
1682 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1683 dev->admin_tagset.nr_hw_queues = 1;
1684 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1;
Keith Busch1efccc92015-03-31 10:37:17 -06001685 dev->admin_tagset.reserved_tags = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001686 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001687 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001688 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001689 dev->admin_tagset.driver_data = dev;
1690
1691 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1692 return -ENOMEM;
1693
1694 dev->admin_q = blk_mq_init_queue(&dev->admin_tagset);
Ming Lei35b489d2015-01-02 14:25:27 +00001695 if (IS_ERR(dev->admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001696 blk_mq_free_tag_set(&dev->admin_tagset);
1697 return -ENOMEM;
1698 }
Keith Buschea191d22015-01-07 18:55:49 -07001699 if (!blk_get_queue(dev->admin_q)) {
1700 nvme_dev_remove_admin(dev);
Keith Busch4af0e212015-06-08 10:08:13 -06001701 dev->admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001702 return -ENODEV;
1703 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001704 } else
1705 blk_mq_unfreeze_queue(dev->admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001706
1707 return 0;
1708}
1709
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001710static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001711{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001712 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001713 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001714 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001715 struct nvme_queue *nvmeq;
Keith Busch1d090622014-06-23 11:34:01 -06001716 unsigned page_shift = PAGE_SHIFT;
1717 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12;
1718 unsigned dev_page_max = NVME_CAP_MPSMAX(cap) + 12;
1719
1720 if (page_shift < dev_page_min) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001721 dev_err(dev->dev,
Keith Busch1d090622014-06-23 11:34:01 -06001722 "Minimum device page size (%u) too large for "
1723 "host (%u)\n", 1 << dev_page_min,
1724 1 << page_shift);
1725 return -ENODEV;
1726 }
1727 if (page_shift > dev_page_max) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001728 dev_info(dev->dev,
Keith Busch1d090622014-06-23 11:34:01 -06001729 "Device maximum page size (%u) smaller than "
1730 "host (%u); enabling work-around\n",
1731 1 << dev_page_max, 1 << page_shift);
1732 page_shift = dev_page_max;
1733 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001734
Keith Buschdfbac8c2015-08-10 15:20:40 -06001735 dev->subsystem = readl(&dev->bar->vs) >= NVME_VS(1, 1) ?
1736 NVME_CAP_NSSRC(cap) : 0;
1737
1738 if (dev->subsystem && (readl(&dev->bar->csts) & NVME_CSTS_NSSRO))
1739 writel(NVME_CSTS_NSSRO, &dev->bar->csts);
1740
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001741 result = nvme_disable_ctrl(dev, cap);
1742 if (result < 0)
1743 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001744
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001745 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001746 if (!nvmeq) {
Keith Busch2b25d982014-12-22 12:59:04 -07001747 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001748 if (!nvmeq)
1749 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001750 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001751
1752 aqa = nvmeq->q_depth - 1;
1753 aqa |= aqa << 16;
1754
Keith Busch1d090622014-06-23 11:34:01 -06001755 dev->page_size = 1 << page_shift;
1756
Dan McLeran01079522014-06-23 08:24:36 -06001757 dev->ctrl_config = NVME_CC_CSS_NVM;
Keith Busch1d090622014-06-23 11:34:01 -06001758 dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001759 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001760 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001761
1762 writel(aqa, &dev->bar->aqa);
1763 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1764 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001765
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001766 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001767 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001768 goto free_nvmeq;
1769
Keith Busch2b25d982014-12-22 12:59:04 -07001770 nvmeq->cq_vector = 0;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001771 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001772 if (result) {
1773 nvmeq->cq_vector = -1;
Keith Busch0fb59cb2015-01-07 18:55:50 -07001774 goto free_nvmeq;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001775 }
Keith Busch025c5572013-05-01 13:07:51 -06001776
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001777 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001778
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001779 free_nvmeq:
1780 nvme_free_queues(dev, 0);
1781 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001782}
1783
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001784static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1785{
1786 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001787 struct nvme_user_io io;
1788 struct nvme_command c;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001789 unsigned length, meta_len;
Keith Buscha67a9512015-04-07 16:57:19 -06001790 int status, write;
Keith Buscha67a9512015-04-07 16:57:19 -06001791 dma_addr_t meta_dma = 0;
1792 void *meta = NULL;
Arnd Bergmannfec558b2015-05-19 17:05:40 +02001793 void __user *metadata;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001794
1795 if (copy_from_user(&io, uio, sizeof(io)))
1796 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001797
1798 switch (io.opcode) {
1799 case nvme_cmd_write:
1800 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001801 case nvme_cmd_compare:
Matthew Wilcox64132142011-08-09 12:56:37 -04001802 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001803 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001804 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001805 }
1806
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001807 length = (io.nblocks + 1) << ns->lba_shift;
1808 meta_len = (io.nblocks + 1) * ns->ms;
Linus Torvalds6a398a32015-06-25 15:12:50 -07001809 metadata = (void __user *)(unsigned long)io.metadata;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001810 write = io.opcode & 1;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001811
Keith Busch71feb362015-06-19 11:07:30 -06001812 if (ns->ext) {
1813 length += meta_len;
1814 meta_len = 0;
Keith Buscha67a9512015-04-07 16:57:19 -06001815 }
1816 if (meta_len) {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001817 if (((io.metadata & 3) || !io.metadata) && !ns->ext)
1818 return -EINVAL;
1819
Christoph Hellwige75ec752015-05-22 11:12:39 +02001820 meta = dma_alloc_coherent(dev->dev, meta_len,
Keith Buscha67a9512015-04-07 16:57:19 -06001821 &meta_dma, GFP_KERNEL);
Arnd Bergmannfec558b2015-05-19 17:05:40 +02001822
Keith Buscha67a9512015-04-07 16:57:19 -06001823 if (!meta) {
1824 status = -ENOMEM;
1825 goto unmap;
1826 }
1827 if (write) {
Arnd Bergmannfec558b2015-05-19 17:05:40 +02001828 if (copy_from_user(meta, metadata, meta_len)) {
Keith Buscha67a9512015-04-07 16:57:19 -06001829 status = -EFAULT;
1830 goto unmap;
1831 }
1832 }
1833 }
1834
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001835 memset(&c, 0, sizeof(c));
1836 c.rw.opcode = io.opcode;
1837 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001838 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001839 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001840 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001841 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001842 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1843 c.rw.reftag = cpu_to_le32(io.reftag);
1844 c.rw.apptag = cpu_to_le16(io.apptag);
1845 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buscha67a9512015-04-07 16:57:19 -06001846 c.rw.metadata = cpu_to_le64(meta_dma);
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001847
1848 status = __nvme_submit_sync_cmd(ns->queue, &c, NULL,
1849 (void __user *)io.addr, length, NULL, 0);
Keith Buschf410c682013-04-23 17:23:59 -06001850 unmap:
Keith Buscha67a9512015-04-07 16:57:19 -06001851 if (meta) {
1852 if (status == NVME_SC_SUCCESS && !write) {
Arnd Bergmannfec558b2015-05-19 17:05:40 +02001853 if (copy_to_user(metadata, meta, meta_len))
Keith Buscha67a9512015-04-07 16:57:19 -06001854 status = -EFAULT;
1855 }
Christoph Hellwige75ec752015-05-22 11:12:39 +02001856 dma_free_coherent(dev->dev, meta_len, meta, meta_dma);
Keith Buschf410c682013-04-23 17:23:59 -06001857 }
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001858 return status;
1859}
1860
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001861static int nvme_user_cmd(struct nvme_dev *dev, struct nvme_ns *ns,
1862 struct nvme_passthru_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001863{
Keith Busch7963e522014-09-12 16:07:20 -06001864 struct nvme_passthru_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001865 struct nvme_command c;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001866 unsigned timeout = 0;
1867 int status;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001868
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001869 if (!capable(CAP_SYS_ADMIN))
1870 return -EACCES;
1871 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001872 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001873
1874 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001875 c.common.opcode = cmd.opcode;
1876 c.common.flags = cmd.flags;
1877 c.common.nsid = cpu_to_le32(cmd.nsid);
1878 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1879 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1880 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1881 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1882 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1883 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1884 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1885 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1886
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001887 if (cmd.timeout_ms)
1888 timeout = msecs_to_jiffies(cmd.timeout_ms);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001889
Christoph Hellwigf705f832015-05-22 11:12:38 +02001890 status = __nvme_submit_sync_cmd(ns ? ns->queue : dev->admin_q, &c,
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001891 NULL, (void __user *)cmd.addr, cmd.data_len,
1892 &cmd.result, timeout);
1893 if (status >= 0) {
1894 if (put_user(cmd.result, &ucmd->result))
1895 return -EFAULT;
Keith Buschedd10d32014-04-03 16:45:23 -06001896 }
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001897
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001898 return status;
1899}
1900
Jon Derrick81f03fe2015-08-10 15:20:41 -06001901static int nvme_subsys_reset(struct nvme_dev *dev)
1902{
1903 if (!dev->subsystem)
1904 return -ENOTTY;
1905
1906 writel(0x4E564D65, &dev->bar->nssr); /* "NVMe" */
1907 return 0;
1908}
1909
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001910static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1911 unsigned long arg)
1912{
1913 struct nvme_ns *ns = bdev->bd_disk->private_data;
1914
1915 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001916 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001917 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001918 return ns->ns_id;
1919 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001920 return nvme_user_cmd(ns->dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06001921 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001922 return nvme_user_cmd(ns->dev, ns, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001923 case NVME_IOCTL_SUBMIT_IO:
1924 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001925 case SG_GET_VERSION_NUM:
1926 return nvme_sg_get_version_num((void __user *)arg);
1927 case SG_IO:
1928 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001929 default:
1930 return -ENOTTY;
1931 }
1932}
1933
Keith Busch320a3822013-10-23 13:07:34 -06001934#ifdef CONFIG_COMPAT
1935static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1936 unsigned int cmd, unsigned long arg)
1937{
Keith Busch320a3822013-10-23 13:07:34 -06001938 switch (cmd) {
1939 case SG_IO:
Keith Busche1797292014-08-27 13:55:38 -06001940 return -ENOIOCTLCMD;
Keith Busch320a3822013-10-23 13:07:34 -06001941 }
1942 return nvme_ioctl(bdev, mode, cmd, arg);
1943}
1944#else
1945#define nvme_compat_ioctl NULL
1946#endif
1947
Keith Busch9ac27092014-01-31 16:53:39 -07001948static int nvme_open(struct block_device *bdev, fmode_t mode)
1949{
Keith Busch9e603522014-10-03 11:15:47 -06001950 int ret = 0;
1951 struct nvme_ns *ns;
Keith Busch9ac27092014-01-31 16:53:39 -07001952
Keith Busch9e603522014-10-03 11:15:47 -06001953 spin_lock(&dev_list_lock);
1954 ns = bdev->bd_disk->private_data;
1955 if (!ns)
1956 ret = -ENXIO;
1957 else if (!kref_get_unless_zero(&ns->dev->kref))
1958 ret = -ENXIO;
1959 spin_unlock(&dev_list_lock);
1960
1961 return ret;
Keith Busch9ac27092014-01-31 16:53:39 -07001962}
1963
1964static void nvme_free_dev(struct kref *kref);
1965
1966static void nvme_release(struct gendisk *disk, fmode_t mode)
1967{
1968 struct nvme_ns *ns = disk->private_data;
1969 struct nvme_dev *dev = ns->dev;
1970
1971 kref_put(&dev->kref, nvme_free_dev);
1972}
1973
Keith Busch4cc09e22014-04-02 15:45:37 -06001974static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
1975{
1976 /* some standard values */
1977 geo->heads = 1 << 6;
1978 geo->sectors = 1 << 5;
1979 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1980 return 0;
1981}
1982
Keith Busche1e5e562015-02-19 13:39:03 -07001983static void nvme_config_discard(struct nvme_ns *ns)
1984{
1985 u32 logical_block_size = queue_logical_block_size(ns->queue);
1986 ns->queue->limits.discard_zeroes_data = 0;
1987 ns->queue->limits.discard_alignment = logical_block_size;
1988 ns->queue->limits.discard_granularity = logical_block_size;
Jens Axboe2bb4cd52015-07-14 08:15:12 -06001989 blk_queue_max_discard_sectors(ns->queue, 0xffffffff);
Keith Busche1e5e562015-02-19 13:39:03 -07001990 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1991}
1992
Keith Busch1b9dbf72014-09-10 17:21:14 -06001993static int nvme_revalidate_disk(struct gendisk *disk)
1994{
1995 struct nvme_ns *ns = disk->private_data;
1996 struct nvme_dev *dev = ns->dev;
1997 struct nvme_id_ns *id;
Keith Buscha67a9512015-04-07 16:57:19 -06001998 u8 lbaf, pi_type;
1999 u16 old_ms;
Keith Busche1e5e562015-02-19 13:39:03 -07002000 unsigned short bs;
Keith Busch1b9dbf72014-09-10 17:21:14 -06002001
Christoph Hellwigd29ec822015-05-22 11:12:46 +02002002 if (nvme_identify_ns(dev, ns->ns_id, &id)) {
Keith Buscha5768aa2015-06-01 14:28:14 -06002003 dev_warn(dev->dev, "%s: Identify failure nvme%dn%d\n", __func__,
2004 dev->instance, ns->ns_id);
2005 return -ENODEV;
Keith Busch1b9dbf72014-09-10 17:21:14 -06002006 }
Keith Buscha5768aa2015-06-01 14:28:14 -06002007 if (id->ncap == 0) {
2008 kfree(id);
2009 return -ENODEV;
Keith Busche1e5e562015-02-19 13:39:03 -07002010 }
Keith Busch1b9dbf72014-09-10 17:21:14 -06002011
Keith Busche1e5e562015-02-19 13:39:03 -07002012 old_ms = ns->ms;
2013 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
Keith Busch1b9dbf72014-09-10 17:21:14 -06002014 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Busche1e5e562015-02-19 13:39:03 -07002015 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Buscha67a9512015-04-07 16:57:19 -06002016 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
Keith Busch1b9dbf72014-09-10 17:21:14 -06002017
Keith Busche1e5e562015-02-19 13:39:03 -07002018 /*
2019 * If identify namespace failed, use default 512 byte block size so
2020 * block layer can use before failing read/write for 0 capacity.
2021 */
2022 if (ns->lba_shift == 0)
2023 ns->lba_shift = 9;
2024 bs = 1 << ns->lba_shift;
2025
2026 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
2027 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
2028 id->dps & NVME_NS_DPS_PI_MASK : 0;
2029
Keith Busch52b68d72015-02-23 09:16:21 -07002030 if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
2031 ns->ms != old_ms ||
Keith Busche1e5e562015-02-19 13:39:03 -07002032 bs != queue_logical_block_size(disk->queue) ||
Keith Buscha67a9512015-04-07 16:57:19 -06002033 (ns->ms && ns->ext)))
Keith Busche1e5e562015-02-19 13:39:03 -07002034 blk_integrity_unregister(disk);
2035
2036 ns->pi_type = pi_type;
2037 blk_queue_logical_block_size(ns->queue, bs);
2038
Keith Busch52b68d72015-02-23 09:16:21 -07002039 if (ns->ms && !blk_get_integrity(disk) && (disk->flags & GENHD_FL_UP) &&
Keith Buscha67a9512015-04-07 16:57:19 -06002040 !ns->ext)
Keith Busche1e5e562015-02-19 13:39:03 -07002041 nvme_init_integrity(ns);
2042
Alok Pandeye19b1272015-08-26 08:56:14 -06002043 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
Keith Busche1e5e562015-02-19 13:39:03 -07002044 set_capacity(disk, 0);
2045 else
2046 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
2047
2048 if (dev->oncs & NVME_CTRL_ONCS_DSM)
2049 nvme_config_discard(ns);
2050
Christoph Hellwigd29ec822015-05-22 11:12:46 +02002051 kfree(id);
Keith Busch1b9dbf72014-09-10 17:21:14 -06002052 return 0;
2053}
2054
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002055static const struct block_device_operations nvme_fops = {
2056 .owner = THIS_MODULE,
2057 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06002058 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07002059 .open = nvme_open,
2060 .release = nvme_release,
Keith Busch4cc09e22014-04-02 15:45:37 -06002061 .getgeo = nvme_getgeo,
Keith Busch1b9dbf72014-09-10 17:21:14 -06002062 .revalidate_disk= nvme_revalidate_disk,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002063};
2064
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002065static int nvme_kthread(void *data)
2066{
Keith Buschd4b4ff82013-12-10 13:10:37 -07002067 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002068
2069 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04002070 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002071 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07002072 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002073 int i;
Keith Buschdfbac8c2015-08-10 15:20:40 -06002074 u32 csts = readl(&dev->bar->csts);
2075
2076 if ((dev->subsystem && (csts & NVME_CSTS_NSSRO)) ||
2077 csts & NVME_CSTS_CFS) {
Keith Buschd4b4ff82013-12-10 13:10:37 -07002078 if (work_busy(&dev->reset_work))
2079 continue;
2080 list_del_init(&dev->node);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002081 dev_warn(dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002082 "Failed status: %x, reset controller\n",
2083 readl(&dev->bar->csts));
Tejun Heo9ca97372014-03-07 10:24:49 -05002084 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschd4b4ff82013-12-10 13:10:37 -07002085 queue_work(nvme_workq, &dev->reset_work);
2086 continue;
2087 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002088 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002089 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05002090 if (!nvmeq)
2091 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002092 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04002093 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06002094
2095 while ((i == 0) && (dev->event_limit > 0)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002096 if (nvme_submit_async_admin_req(dev))
Keith Busch6fccf932014-06-18 13:58:57 -06002097 break;
2098 dev->event_limit--;
2099 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002100 spin_unlock_irq(&nvmeq->q_lock);
2101 }
2102 }
2103 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08002104 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002105 }
2106 return 0;
2107}
2108
Keith Busche1e5e562015-02-19 13:39:03 -07002109static void nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002110{
2111 struct nvme_ns *ns;
2112 struct gendisk *disk;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002113 int node = dev_to_node(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002114
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002115 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002116 if (!ns)
Keith Busche1e5e562015-02-19 13:39:03 -07002117 return;
2118
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002119 ns->queue = blk_mq_init_queue(&dev->tagset);
Dan Carpenter9f173b32014-11-05 23:39:09 +03002120 if (IS_ERR(ns->queue))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002121 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07002122 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
2123 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002124 ns->dev = dev;
2125 ns->queue->queuedata = ns;
2126
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002127 disk = alloc_disk_node(0, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002128 if (!disk)
2129 goto out_free_queue;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002130
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002131 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002132 ns->disk = disk;
Keith Busche1e5e562015-02-19 13:39:03 -07002133 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
2134 list_add_tail(&ns->list, &dev->namespaces);
2135
Keith Busche9ef4632012-07-24 15:01:04 -06002136 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busche8244102015-08-12 16:17:54 -06002137 if (dev->max_hw_sectors) {
Keith Busch8fc23e02012-07-26 11:29:57 -06002138 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Keith Busche8244102015-08-12 16:17:54 -06002139 blk_queue_max_segments(ns->queue,
2140 ((dev->max_hw_sectors << 9) / dev->page_size) + 1);
2141 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002142 if (dev->stripe_size)
2143 blk_queue_chunk_sectors(ns->queue, dev->stripe_size >> 9);
Keith Buscha7d2ce22014-04-29 11:41:28 -06002144 if (dev->vwc & NVME_CTRL_VWC_PRESENT)
2145 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
Keith Busch03100aa2015-08-19 14:24:05 -07002146 blk_queue_virt_boundary(ns->queue, dev->page_size - 1);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002147
2148 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002149 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002150 disk->fops = &nvme_fops;
2151 disk->private_data = ns;
2152 disk->queue = ns->queue;
Keith Buschb3fffde2015-02-03 11:21:42 -07002153 disk->driverfs_dev = dev->device;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002154 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002155 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002156
Keith Busche1e5e562015-02-19 13:39:03 -07002157 /*
2158 * Initialize capacity to 0 until we establish the namespace format and
2159 * setup integrity extentions if necessary. The revalidate_disk after
2160 * add_disk allows the driver to register with integrity if the format
2161 * requires it.
2162 */
2163 set_capacity(disk, 0);
Keith Buscha5768aa2015-06-01 14:28:14 -06002164 if (nvme_revalidate_disk(ns->disk))
2165 goto out_free_disk;
2166
Keith Busche1e5e562015-02-19 13:39:03 -07002167 add_disk(ns->disk);
Keith Busch7bee6072015-07-14 11:57:48 -06002168 if (ns->ms) {
2169 struct block_device *bd = bdget_disk(ns->disk, 0);
2170 if (!bd)
2171 return;
2172 if (blkdev_get(bd, FMODE_READ, NULL)) {
2173 bdput(bd);
2174 return;
2175 }
2176 blkdev_reread_part(bd);
2177 blkdev_put(bd, FMODE_READ);
2178 }
Keith Busche1e5e562015-02-19 13:39:03 -07002179 return;
Keith Buscha5768aa2015-06-01 14:28:14 -06002180 out_free_disk:
2181 kfree(disk);
2182 list_del(&ns->list);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002183 out_free_queue:
2184 blk_cleanup_queue(ns->queue);
2185 out_free_ns:
2186 kfree(ns);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002187}
2188
Keith Busch42f61422014-03-24 10:46:25 -06002189static void nvme_create_io_queues(struct nvme_dev *dev)
2190{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002191 unsigned i;
Keith Busch42f61422014-03-24 10:46:25 -06002192
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002193 for (i = dev->queue_count; i <= dev->max_qid; i++)
Keith Busch2b25d982014-12-22 12:59:04 -07002194 if (!nvme_alloc_queue(dev, i, dev->q_depth))
Keith Busch42f61422014-03-24 10:46:25 -06002195 break;
2196
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002197 for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
2198 if (nvme_create_queue(dev->queues[i], i))
Keith Busch42f61422014-03-24 10:46:25 -06002199 break;
2200}
2201
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002202static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002203{
2204 int status;
2205 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002206 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002207
Matthew Wilcoxdf348132012-01-11 07:29:56 -07002208 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002209 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04002210 if (status < 0)
2211 return status;
2212 if (status > 0) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002213 dev_err(dev->dev, "Could not set queue count (%d)\n", status);
Keith Buschbadc34d2014-06-23 14:25:35 -06002214 return 0;
Matthew Wilcox27e81662014-04-11 11:58:45 -04002215 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002216 return min(result & 0xffff, result >> 16) + 1;
2217}
2218
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002219static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
2220{
2221 u64 szu, size, offset;
2222 u32 cmbloc;
2223 resource_size_t bar_size;
2224 struct pci_dev *pdev = to_pci_dev(dev->dev);
2225 void __iomem *cmb;
2226 dma_addr_t dma_addr;
2227
2228 if (!use_cmb_sqes)
2229 return NULL;
2230
2231 dev->cmbsz = readl(&dev->bar->cmbsz);
2232 if (!(NVME_CMB_SZ(dev->cmbsz)))
2233 return NULL;
2234
2235 cmbloc = readl(&dev->bar->cmbloc);
2236
2237 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
2238 size = szu * NVME_CMB_SZ(dev->cmbsz);
2239 offset = szu * NVME_CMB_OFST(cmbloc);
2240 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(cmbloc));
2241
2242 if (offset > bar_size)
2243 return NULL;
2244
2245 /*
2246 * Controllers may support a CMB size larger than their BAR,
2247 * for example, due to being behind a bridge. Reduce the CMB to
2248 * the reported size of the BAR
2249 */
2250 if (size > bar_size - offset)
2251 size = bar_size - offset;
2252
2253 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(cmbloc)) + offset;
2254 cmb = ioremap_wc(dma_addr, size);
2255 if (!cmb)
2256 return NULL;
2257
2258 dev->cmb_dma_addr = dma_addr;
2259 dev->cmb_size = size;
2260 return cmb;
2261}
2262
2263static inline void nvme_release_cmb(struct nvme_dev *dev)
2264{
2265 if (dev->cmb) {
2266 iounmap(dev->cmb);
2267 dev->cmb = NULL;
2268 }
2269}
2270
Keith Busch9d713c22013-07-15 15:02:24 -06002271static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
2272{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002273 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06002274}
2275
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002276static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002277{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002278 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02002279 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch42f61422014-03-24 10:46:25 -06002280 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002281
Keith Busch42f61422014-03-24 10:46:25 -06002282 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002283 result = set_queue_count(dev, nr_io_queues);
Keith Buschbadc34d2014-06-23 14:25:35 -06002284 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002285 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002286 if (result < nr_io_queues)
2287 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002288
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002289 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
2290 result = nvme_cmb_qdepth(dev, nr_io_queues,
2291 sizeof(struct nvme_command));
2292 if (result > 0)
2293 dev->q_depth = result;
2294 else
2295 nvme_release_cmb(dev);
2296 }
2297
Keith Busch9d713c22013-07-15 15:02:24 -06002298 size = db_bar_size(dev, nr_io_queues);
2299 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002300 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06002301 do {
2302 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
2303 if (dev->bar)
2304 break;
2305 if (!--nr_io_queues)
2306 return -ENOMEM;
2307 size = db_bar_size(dev, nr_io_queues);
2308 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002309 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07002310 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002311 }
2312
Keith Busch9d713c22013-07-15 15:02:24 -06002313 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002314 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002315
Jens Axboee32efbf2014-11-14 09:49:26 -07002316 /*
2317 * If we enable msix early due to not intx, disable it again before
2318 * setting up the full range we need.
2319 */
2320 if (!pdev->irq)
2321 pci_disable_msix(pdev);
2322
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002323 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002324 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002325 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
2326 if (vecs < 0) {
2327 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
2328 if (vecs < 0) {
2329 vecs = 1;
2330 } else {
2331 for (i = 0; i < vecs; i++)
2332 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002333 }
2334 }
2335
Matthew Wilcox063a8092013-06-20 10:53:48 -04002336 /*
2337 * Should investigate if there's a performance win from allocating
2338 * more queues than interrupt vectors; it might allow the submission
2339 * path to scale better, even if the receive path is limited by the
2340 * number of interrupts.
2341 */
2342 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002343 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002344
Matthew Wilcox3193f072014-01-27 15:57:22 -05002345 result = queue_request_irq(dev, adminq, adminq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06002346 if (result) {
2347 adminq->cq_vector = -1;
Keith Busch22404272013-07-15 15:02:20 -06002348 goto free_queues;
Jon Derrick758dd7f2015-06-30 11:22:52 -06002349 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05002350
Keith Buschcd638942013-07-15 15:02:23 -06002351 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002352 nvme_free_queues(dev, nr_io_queues + 1);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002353 nvme_create_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06002354
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002355 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002356
Keith Busch22404272013-07-15 15:02:20 -06002357 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002358 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002359 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002360}
2361
Keith Buscha5768aa2015-06-01 14:28:14 -06002362static void nvme_free_namespace(struct nvme_ns *ns)
2363{
2364 list_del(&ns->list);
2365
2366 spin_lock(&dev_list_lock);
2367 ns->disk->private_data = NULL;
2368 spin_unlock(&dev_list_lock);
2369
2370 put_disk(ns->disk);
2371 kfree(ns);
2372}
2373
2374static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2375{
2376 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2377 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2378
2379 return nsa->ns_id - nsb->ns_id;
2380}
2381
2382static struct nvme_ns *nvme_find_ns(struct nvme_dev *dev, unsigned nsid)
2383{
2384 struct nvme_ns *ns;
2385
2386 list_for_each_entry(ns, &dev->namespaces, list) {
2387 if (ns->ns_id == nsid)
2388 return ns;
2389 if (ns->ns_id > nsid)
2390 break;
2391 }
2392 return NULL;
2393}
2394
2395static inline bool nvme_io_incapable(struct nvme_dev *dev)
2396{
2397 return (!dev->bar || readl(&dev->bar->csts) & NVME_CSTS_CFS ||
2398 dev->online_queues < 2);
2399}
2400
2401static void nvme_ns_remove(struct nvme_ns *ns)
2402{
2403 bool kill = nvme_io_incapable(ns->dev) && !blk_queue_dying(ns->queue);
2404
2405 if (kill)
2406 blk_set_queue_dying(ns->queue);
2407 if (ns->disk->flags & GENHD_FL_UP) {
2408 if (blk_get_integrity(ns->disk))
2409 blk_integrity_unregister(ns->disk);
2410 del_gendisk(ns->disk);
2411 }
2412 if (kill || !blk_queue_dying(ns->queue)) {
2413 blk_mq_abort_requeue_list(ns->queue);
2414 blk_cleanup_queue(ns->queue);
2415 }
2416}
2417
2418static void nvme_scan_namespaces(struct nvme_dev *dev, unsigned nn)
2419{
2420 struct nvme_ns *ns, *next;
2421 unsigned i;
2422
2423 for (i = 1; i <= nn; i++) {
2424 ns = nvme_find_ns(dev, i);
2425 if (ns) {
2426 if (revalidate_disk(ns->disk)) {
2427 nvme_ns_remove(ns);
2428 nvme_free_namespace(ns);
2429 }
2430 } else
2431 nvme_alloc_ns(dev, i);
2432 }
2433 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2434 if (ns->ns_id > nn) {
2435 nvme_ns_remove(ns);
2436 nvme_free_namespace(ns);
2437 }
2438 }
2439 list_sort(NULL, &dev->namespaces, ns_cmp);
2440}
2441
2442static void nvme_dev_scan(struct work_struct *work)
2443{
2444 struct nvme_dev *dev = container_of(work, struct nvme_dev, scan_work);
2445 struct nvme_id_ctrl *ctrl;
2446
2447 if (!dev->tagset.tags)
2448 return;
2449 if (nvme_identify_ctrl(dev, &ctrl))
2450 return;
2451 nvme_scan_namespaces(dev, le32_to_cpup(&ctrl->nn));
2452 kfree(ctrl);
2453}
2454
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002455/*
2456 * Return: error value if an error occurred setting up the queues or calling
2457 * Identify Device. 0 if these succeeded, even if adding some of the
2458 * namespaces failed. At the moment, these failures are silent. TBD which
2459 * failures should be reported.
2460 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002461static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002462{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002463 struct pci_dev *pdev = to_pci_dev(dev->dev);
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002464 int res;
Matthew Wilcox51814232011-02-01 16:18:08 -05002465 struct nvme_id_ctrl *ctrl;
Keith Busch159b67d2013-04-09 17:13:20 -06002466 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002467
Christoph Hellwigd29ec822015-05-22 11:12:46 +02002468 res = nvme_identify_ctrl(dev, &ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002469 if (res) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002470 dev_err(dev->dev, "Identify Controller failed (%d)\n", res);
Keith Busche1e5e562015-02-19 13:39:03 -07002471 return -EIO;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002472 }
2473
Keith Busch0e5e4f02012-11-09 16:33:05 -07002474 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002475 dev->abort_limit = ctrl->acl + 1;
Keith Buscha7d2ce22014-04-29 11:41:28 -06002476 dev->vwc = ctrl->vwc;
Matthew Wilcox51814232011-02-01 16:18:08 -05002477 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2478 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2479 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002480 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002481 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002482 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002483 (pdev->device == 0x0953) && ctrl->vs[3]) {
2484 unsigned int max_hw_sectors;
2485
Keith Busch159b67d2013-04-09 17:13:20 -06002486 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002487 max_hw_sectors = dev->stripe_size >> (shift - 9);
2488 if (dev->max_hw_sectors) {
2489 dev->max_hw_sectors = min(max_hw_sectors,
2490 dev->max_hw_sectors);
2491 } else
2492 dev->max_hw_sectors = max_hw_sectors;
2493 }
Christoph Hellwigd29ec822015-05-22 11:12:46 +02002494 kfree(ctrl);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002495
Keith Buschffe77042015-06-08 10:08:15 -06002496 if (!dev->tagset.tags) {
2497 dev->tagset.ops = &nvme_mq_ops;
2498 dev->tagset.nr_hw_queues = dev->online_queues - 1;
2499 dev->tagset.timeout = NVME_IO_TIMEOUT;
2500 dev->tagset.numa_node = dev_to_node(dev->dev);
2501 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002502 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Keith Buschffe77042015-06-08 10:08:15 -06002503 dev->tagset.cmd_size = nvme_cmd_size(dev);
2504 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
2505 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002506
Keith Buschffe77042015-06-08 10:08:15 -06002507 if (blk_mq_alloc_tag_set(&dev->tagset))
2508 return 0;
2509 }
Keith Buscha5768aa2015-06-01 14:28:14 -06002510 schedule_work(&dev->scan_work);
Keith Busche1e5e562015-02-19 13:39:03 -07002511 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002512}
2513
Keith Busch0877cb02013-07-15 15:02:19 -06002514static int nvme_dev_map(struct nvme_dev *dev)
2515{
Keith Busch42f61422014-03-24 10:46:25 -06002516 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002517 int bars, result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002518 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002519
2520 if (pci_enable_device_mem(pdev))
2521 return result;
2522
2523 dev->entry[0].vector = pdev->irq;
2524 pci_set_master(pdev);
2525 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07002526 if (!bars)
2527 goto disable_pci;
2528
Keith Busch0877cb02013-07-15 15:02:19 -06002529 if (pci_request_selected_regions(pdev, bars, "nvme"))
2530 goto disable_pci;
2531
Christoph Hellwige75ec752015-05-22 11:12:39 +02002532 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
2533 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01002534 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002535
Keith Busch0877cb02013-07-15 15:02:19 -06002536 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2537 if (!dev->bar)
2538 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07002539
Keith Busch0e53d182013-12-10 13:10:39 -07002540 if (readl(&dev->bar->csts) == -1) {
2541 result = -ENODEV;
2542 goto unmap;
2543 }
Jens Axboee32efbf2014-11-14 09:49:26 -07002544
2545 /*
2546 * Some devices don't advertse INTx interrupts, pre-enable a single
2547 * MSIX vec for setup. We'll adjust this later.
2548 */
2549 if (!pdev->irq) {
2550 result = pci_enable_msix(pdev, dev->entry, 1);
2551 if (result < 0)
2552 goto unmap;
2553 }
2554
Keith Busch42f61422014-03-24 10:46:25 -06002555 cap = readq(&dev->bar->cap);
2556 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2557 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Keith Busch0877cb02013-07-15 15:02:19 -06002558 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002559 if (readl(&dev->bar->vs) >= NVME_VS(1, 2))
2560 dev->cmb = nvme_map_cmb(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002561
2562 return 0;
2563
Keith Busch0e53d182013-12-10 13:10:39 -07002564 unmap:
2565 iounmap(dev->bar);
2566 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002567 disable:
2568 pci_release_regions(pdev);
2569 disable_pci:
2570 pci_disable_device(pdev);
2571 return result;
2572}
2573
2574static void nvme_dev_unmap(struct nvme_dev *dev)
2575{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002576 struct pci_dev *pdev = to_pci_dev(dev->dev);
2577
2578 if (pdev->msi_enabled)
2579 pci_disable_msi(pdev);
2580 else if (pdev->msix_enabled)
2581 pci_disable_msix(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002582
2583 if (dev->bar) {
2584 iounmap(dev->bar);
2585 dev->bar = NULL;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002586 pci_release_regions(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002587 }
2588
Christoph Hellwige75ec752015-05-22 11:12:39 +02002589 if (pci_is_enabled(pdev))
2590 pci_disable_device(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002591}
2592
Keith Busch4d115422013-12-10 13:10:40 -07002593struct nvme_delq_ctx {
2594 struct task_struct *waiter;
2595 struct kthread_worker *worker;
2596 atomic_t refcount;
2597};
2598
2599static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2600{
2601 dq->waiter = current;
2602 mb();
2603
2604 for (;;) {
2605 set_current_state(TASK_KILLABLE);
2606 if (!atomic_read(&dq->refcount))
2607 break;
2608 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2609 fatal_signal_pending(current)) {
Keith Busch0fb59cb2015-01-07 18:55:50 -07002610 /*
2611 * Disable the controller first since we can't trust it
2612 * at this point, but leave the admin queue enabled
2613 * until all queue deletion requests are flushed.
2614 * FIXME: This may take a while if there are more h/w
2615 * queues than admin tags.
2616 */
Keith Busch4d115422013-12-10 13:10:40 -07002617 set_current_state(TASK_RUNNING);
Keith Busch4d115422013-12-10 13:10:40 -07002618 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
Keith Busch0fb59cb2015-01-07 18:55:50 -07002619 nvme_clear_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07002620 flush_kthread_worker(dq->worker);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002621 nvme_disable_queue(dev, 0);
Keith Busch4d115422013-12-10 13:10:40 -07002622 return;
2623 }
2624 }
2625 set_current_state(TASK_RUNNING);
2626}
2627
2628static void nvme_put_dq(struct nvme_delq_ctx *dq)
2629{
2630 atomic_dec(&dq->refcount);
2631 if (dq->waiter)
2632 wake_up_process(dq->waiter);
2633}
2634
2635static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2636{
2637 atomic_inc(&dq->refcount);
2638 return dq;
2639}
2640
2641static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2642{
2643 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
Keith Busch4d115422013-12-10 13:10:40 -07002644 nvme_put_dq(dq);
2645}
2646
2647static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2648 kthread_work_func_t fn)
2649{
2650 struct nvme_command c;
2651
2652 memset(&c, 0, sizeof(c));
2653 c.delete_queue.opcode = opcode;
2654 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2655
2656 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002657 return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
2658 ADMIN_TIMEOUT);
Keith Busch4d115422013-12-10 13:10:40 -07002659}
2660
2661static void nvme_del_cq_work_handler(struct kthread_work *work)
2662{
2663 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2664 cmdinfo.work);
2665 nvme_del_queue_end(nvmeq);
2666}
2667
2668static int nvme_delete_cq(struct nvme_queue *nvmeq)
2669{
2670 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2671 nvme_del_cq_work_handler);
2672}
2673
2674static void nvme_del_sq_work_handler(struct kthread_work *work)
2675{
2676 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2677 cmdinfo.work);
2678 int status = nvmeq->cmdinfo.status;
2679
2680 if (!status)
2681 status = nvme_delete_cq(nvmeq);
2682 if (status)
2683 nvme_del_queue_end(nvmeq);
2684}
2685
2686static int nvme_delete_sq(struct nvme_queue *nvmeq)
2687{
2688 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2689 nvme_del_sq_work_handler);
2690}
2691
2692static void nvme_del_queue_start(struct kthread_work *work)
2693{
2694 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2695 cmdinfo.work);
Keith Busch4d115422013-12-10 13:10:40 -07002696 if (nvme_delete_sq(nvmeq))
2697 nvme_del_queue_end(nvmeq);
2698}
2699
2700static void nvme_disable_io_queues(struct nvme_dev *dev)
2701{
2702 int i;
2703 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2704 struct nvme_delq_ctx dq;
2705 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2706 &worker, "nvme%d", dev->instance);
2707
2708 if (IS_ERR(kworker_task)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002709 dev_err(dev->dev,
Keith Busch4d115422013-12-10 13:10:40 -07002710 "Failed to create queue del task\n");
2711 for (i = dev->queue_count - 1; i > 0; i--)
2712 nvme_disable_queue(dev, i);
2713 return;
2714 }
2715
2716 dq.waiter = NULL;
2717 atomic_set(&dq.refcount, 0);
2718 dq.worker = &worker;
2719 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002720 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002721
2722 if (nvme_suspend_queue(nvmeq))
2723 continue;
2724 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2725 nvmeq->cmdinfo.worker = dq.worker;
2726 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2727 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2728 }
2729 nvme_wait_dq(&dq, dev);
2730 kthread_stop(kworker_task);
2731}
2732
Dan McLeranb9afca32014-04-07 17:10:11 -06002733/*
2734* Remove the node from the device list and check
2735* for whether or not we need to stop the nvme_thread.
2736*/
2737static void nvme_dev_list_remove(struct nvme_dev *dev)
2738{
2739 struct task_struct *tmp = NULL;
2740
2741 spin_lock(&dev_list_lock);
2742 list_del_init(&dev->node);
2743 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2744 tmp = nvme_thread;
2745 nvme_thread = NULL;
2746 }
2747 spin_unlock(&dev_list_lock);
2748
2749 if (tmp)
2750 kthread_stop(tmp);
2751}
2752
Keith Buschc9d3bf82015-01-07 18:55:52 -07002753static void nvme_freeze_queues(struct nvme_dev *dev)
2754{
2755 struct nvme_ns *ns;
2756
2757 list_for_each_entry(ns, &dev->namespaces, list) {
2758 blk_mq_freeze_queue_start(ns->queue);
2759
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002760 spin_lock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002761 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002762 spin_unlock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002763
2764 blk_mq_cancel_requeue_work(ns->queue);
2765 blk_mq_stop_hw_queues(ns->queue);
2766 }
2767}
2768
2769static void nvme_unfreeze_queues(struct nvme_dev *dev)
2770{
2771 struct nvme_ns *ns;
2772
2773 list_for_each_entry(ns, &dev->namespaces, list) {
2774 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2775 blk_mq_unfreeze_queue(ns->queue);
2776 blk_mq_start_stopped_hw_queues(ns->queue, true);
2777 blk_mq_kick_requeue_list(ns->queue);
2778 }
2779}
2780
Keith Buschf0b50732013-07-15 15:02:21 -06002781static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002782{
Keith Busch22404272013-07-15 15:02:20 -06002783 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06002784 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06002785
Dan McLeranb9afca32014-04-07 17:10:11 -06002786 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002787
Keith Buschc9d3bf82015-01-07 18:55:52 -07002788 if (dev->bar) {
2789 nvme_freeze_queues(dev);
Keith Busch7c1b2452014-06-25 11:18:12 -06002790 csts = readl(&dev->bar->csts);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002791 }
Keith Busch7c1b2452014-06-25 11:18:12 -06002792 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07002793 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002794 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002795 nvme_suspend_queue(nvmeq);
Keith Busch4d115422013-12-10 13:10:40 -07002796 }
2797 } else {
2798 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002799 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002800 nvme_disable_queue(dev, 0);
2801 }
Keith Buschf0b50732013-07-15 15:02:21 -06002802 nvme_dev_unmap(dev);
Keith Busch07836e62015-02-19 10:34:48 -07002803
2804 for (i = dev->queue_count - 1; i >= 0; i--)
2805 nvme_clear_queue(dev->queues[i]);
Keith Buschf0b50732013-07-15 15:02:21 -06002806}
2807
2808static void nvme_dev_remove(struct nvme_dev *dev)
2809{
Keith Busch9ac27092014-01-31 16:53:39 -07002810 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002811
Keith Buscha5768aa2015-06-01 14:28:14 -06002812 list_for_each_entry(ns, &dev->namespaces, list)
2813 nvme_ns_remove(ns);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002814}
2815
Matthew Wilcox091b6092011-02-10 09:56:01 -05002816static int nvme_setup_prp_pools(struct nvme_dev *dev)
2817{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002818 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05002819 PAGE_SIZE, PAGE_SIZE, 0);
2820 if (!dev->prp_page_pool)
2821 return -ENOMEM;
2822
Matthew Wilcox99802a72011-02-10 10:30:34 -05002823 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02002824 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05002825 256, 256, 0);
2826 if (!dev->prp_small_pool) {
2827 dma_pool_destroy(dev->prp_page_pool);
2828 return -ENOMEM;
2829 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002830 return 0;
2831}
2832
2833static void nvme_release_prp_pools(struct nvme_dev *dev)
2834{
2835 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002836 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002837}
2838
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002839static DEFINE_IDA(nvme_instance_ida);
2840
2841static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002842{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002843 int instance, error;
2844
2845 do {
2846 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2847 return -ENODEV;
2848
2849 spin_lock(&dev_list_lock);
2850 error = ida_get_new(&nvme_instance_ida, &instance);
2851 spin_unlock(&dev_list_lock);
2852 } while (error == -EAGAIN);
2853
2854 if (error)
2855 return -ENODEV;
2856
2857 dev->instance = instance;
2858 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002859}
2860
2861static void nvme_release_instance(struct nvme_dev *dev)
2862{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002863 spin_lock(&dev_list_lock);
2864 ida_remove(&nvme_instance_ida, dev->instance);
2865 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002866}
2867
Keith Busch9ac27092014-01-31 16:53:39 -07002868static void nvme_free_namespaces(struct nvme_dev *dev)
2869{
2870 struct nvme_ns *ns, *next;
2871
Keith Buscha5768aa2015-06-01 14:28:14 -06002872 list_for_each_entry_safe(ns, next, &dev->namespaces, list)
2873 nvme_free_namespace(ns);
Keith Busch9ac27092014-01-31 16:53:39 -07002874}
2875
Keith Busch5e82e952013-02-19 10:17:58 -07002876static void nvme_free_dev(struct kref *kref)
2877{
2878 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002879
Christoph Hellwige75ec752015-05-22 11:12:39 +02002880 put_device(dev->dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07002881 put_device(dev->device);
Keith Busch9ac27092014-01-31 16:53:39 -07002882 nvme_free_namespaces(dev);
Indraneel M285dffc2014-12-11 08:24:18 -07002883 nvme_release_instance(dev);
Keith Busch4af0e212015-06-08 10:08:13 -06002884 if (dev->tagset.tags)
2885 blk_mq_free_tag_set(&dev->tagset);
2886 if (dev->admin_q)
2887 blk_put_queue(dev->admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002888 kfree(dev->queues);
2889 kfree(dev->entry);
2890 kfree(dev);
2891}
2892
2893static int nvme_dev_open(struct inode *inode, struct file *f)
2894{
Keith Buschb3fffde2015-02-03 11:21:42 -07002895 struct nvme_dev *dev;
2896 int instance = iminor(inode);
2897 int ret = -ENODEV;
2898
2899 spin_lock(&dev_list_lock);
2900 list_for_each_entry(dev, &dev_list, node) {
2901 if (dev->instance == instance) {
Keith Busch2e1d8442015-02-12 15:33:00 -07002902 if (!dev->admin_q) {
2903 ret = -EWOULDBLOCK;
2904 break;
2905 }
Keith Buschb3fffde2015-02-03 11:21:42 -07002906 if (!kref_get_unless_zero(&dev->kref))
2907 break;
2908 f->private_data = dev;
2909 ret = 0;
2910 break;
2911 }
2912 }
2913 spin_unlock(&dev_list_lock);
2914
2915 return ret;
Keith Busch5e82e952013-02-19 10:17:58 -07002916}
2917
2918static int nvme_dev_release(struct inode *inode, struct file *f)
2919{
2920 struct nvme_dev *dev = f->private_data;
2921 kref_put(&dev->kref, nvme_free_dev);
2922 return 0;
2923}
2924
2925static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2926{
2927 struct nvme_dev *dev = f->private_data;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002928 struct nvme_ns *ns;
2929
Keith Busch5e82e952013-02-19 10:17:58 -07002930 switch (cmd) {
2931 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002932 return nvme_user_cmd(dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06002933 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002934 if (list_empty(&dev->namespaces))
2935 return -ENOTTY;
2936 ns = list_first_entry(&dev->namespaces, struct nvme_ns, list);
2937 return nvme_user_cmd(dev, ns, (void __user *)arg);
Keith Busch4cc06522015-06-05 10:30:08 -06002938 case NVME_IOCTL_RESET:
2939 dev_warn(dev->dev, "resetting controller\n");
2940 return nvme_reset(dev);
Jon Derrick81f03fe2015-08-10 15:20:41 -06002941 case NVME_IOCTL_SUBSYS_RESET:
2942 return nvme_subsys_reset(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002943 default:
2944 return -ENOTTY;
2945 }
2946}
2947
2948static const struct file_operations nvme_dev_fops = {
2949 .owner = THIS_MODULE,
2950 .open = nvme_dev_open,
2951 .release = nvme_dev_release,
2952 .unlocked_ioctl = nvme_dev_ioctl,
2953 .compat_ioctl = nvme_dev_ioctl,
2954};
2955
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002956static void nvme_set_irq_hints(struct nvme_dev *dev)
2957{
2958 struct nvme_queue *nvmeq;
2959 int i;
2960
2961 for (i = 0; i < dev->online_queues; i++) {
2962 nvmeq = dev->queues[i];
2963
Keith Busch42483222015-06-01 09:29:54 -06002964 if (!nvmeq->tags || !(*nvmeq->tags))
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002965 continue;
2966
2967 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
Keith Busch42483222015-06-01 09:29:54 -06002968 blk_mq_tags_cpumask(*nvmeq->tags));
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002969 }
2970}
2971
Keith Buschf0b50732013-07-15 15:02:21 -06002972static int nvme_dev_start(struct nvme_dev *dev)
2973{
2974 int result;
Dan McLeranb9afca32014-04-07 17:10:11 -06002975 bool start_thread = false;
Keith Buschf0b50732013-07-15 15:02:21 -06002976
2977 result = nvme_dev_map(dev);
2978 if (result)
2979 return result;
2980
2981 result = nvme_configure_admin_queue(dev);
2982 if (result)
2983 goto unmap;
2984
2985 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002986 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2987 start_thread = true;
2988 nvme_thread = NULL;
2989 }
Keith Buschf0b50732013-07-15 15:02:21 -06002990 list_add(&dev->node, &dev_list);
2991 spin_unlock(&dev_list_lock);
2992
Dan McLeranb9afca32014-04-07 17:10:11 -06002993 if (start_thread) {
2994 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
Keith Busch387caa52014-09-22 13:46:19 -06002995 wake_up_all(&nvme_kthread_wait);
Dan McLeranb9afca32014-04-07 17:10:11 -06002996 } else
2997 wait_event_killable(nvme_kthread_wait, nvme_thread);
2998
2999 if (IS_ERR_OR_NULL(nvme_thread)) {
3000 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
3001 goto disable;
3002 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003003
3004 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07003005 result = nvme_alloc_admin_tags(dev);
3006 if (result)
3007 goto disable;
Dan McLeranb9afca32014-04-07 17:10:11 -06003008
Keith Buschf0b50732013-07-15 15:02:21 -06003009 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06003010 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07003011 goto free_tags;
Keith Buschf0b50732013-07-15 15:02:21 -06003012
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003013 nvme_set_irq_hints(dev);
3014
Keith Busch1efccc92015-03-31 10:37:17 -06003015 dev->event_limit = 1;
Keith Buschd82e8bf2013-09-05 14:45:07 -06003016 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06003017
Keith Busch0fb59cb2015-01-07 18:55:50 -07003018 free_tags:
3019 nvme_dev_remove_admin(dev);
Keith Busch4af0e212015-06-08 10:08:13 -06003020 blk_put_queue(dev->admin_q);
3021 dev->admin_q = NULL;
3022 dev->queues[0]->tags = NULL;
Keith Buschf0b50732013-07-15 15:02:21 -06003023 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05003024 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06003025 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06003026 unmap:
3027 nvme_dev_unmap(dev);
3028 return result;
3029}
3030
Keith Busch9a6b9452013-12-10 13:10:36 -07003031static int nvme_remove_dead_ctrl(void *arg)
3032{
3033 struct nvme_dev *dev = (struct nvme_dev *)arg;
Christoph Hellwige75ec752015-05-22 11:12:39 +02003034 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003035
3036 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06003037 pci_stop_and_remove_bus_device_locked(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003038 kref_put(&dev->kref, nvme_free_dev);
3039 return 0;
3040}
3041
3042static void nvme_remove_disks(struct work_struct *ws)
3043{
Keith Busch9a6b9452013-12-10 13:10:36 -07003044 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
3045
Keith Busch5a92e702014-02-21 14:13:44 -07003046 nvme_free_queues(dev, 1);
Keith Busch302c6722014-07-18 11:40:20 -06003047 nvme_dev_remove(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003048}
3049
3050static int nvme_dev_resume(struct nvme_dev *dev)
3051{
3052 int ret;
3053
3054 ret = nvme_dev_start(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06003055 if (ret)
Keith Busch9a6b9452013-12-10 13:10:36 -07003056 return ret;
Keith Buschbadc34d2014-06-23 14:25:35 -06003057 if (dev->online_queues < 2) {
Keith Busch9a6b9452013-12-10 13:10:36 -07003058 spin_lock(&dev_list_lock);
Tejun Heo9ca97372014-03-07 10:24:49 -05003059 dev->reset_workfn = nvme_remove_disks;
Keith Busch9a6b9452013-12-10 13:10:36 -07003060 queue_work(nvme_workq, &dev->reset_work);
3061 spin_unlock(&dev_list_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07003062 } else {
3063 nvme_unfreeze_queues(dev);
Keith Buschffe77042015-06-08 10:08:15 -06003064 nvme_dev_add(dev);
Keith Buschc9d3bf82015-01-07 18:55:52 -07003065 nvme_set_irq_hints(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003066 }
3067 return 0;
3068}
3069
Keith Buschde3eff22015-06-18 13:36:39 -06003070static void nvme_dead_ctrl(struct nvme_dev *dev)
3071{
3072 dev_warn(dev->dev, "Device failed to resume\n");
3073 kref_get(&dev->kref);
3074 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
3075 dev->instance))) {
3076 dev_err(dev->dev,
3077 "Failed to start controller remove task\n");
3078 kref_put(&dev->kref, nvme_free_dev);
3079 }
3080}
3081
Keith Busch9a6b9452013-12-10 13:10:36 -07003082static void nvme_dev_reset(struct nvme_dev *dev)
3083{
Keith Buschffe77042015-06-08 10:08:15 -06003084 bool in_probe = work_busy(&dev->probe_work);
3085
Keith Busch9a6b9452013-12-10 13:10:36 -07003086 nvme_dev_shutdown(dev);
Keith Buschffe77042015-06-08 10:08:15 -06003087
3088 /* Synchronize with device probe so that work will see failure status
3089 * and exit gracefully without trying to schedule another reset */
3090 flush_work(&dev->probe_work);
3091
3092 /* Fail this device if reset occured during probe to avoid
3093 * infinite initialization loops. */
3094 if (in_probe) {
Keith Buschde3eff22015-06-18 13:36:39 -06003095 nvme_dead_ctrl(dev);
Keith Buschffe77042015-06-08 10:08:15 -06003096 return;
Keith Busch9a6b9452013-12-10 13:10:36 -07003097 }
Keith Buschffe77042015-06-08 10:08:15 -06003098 /* Schedule device resume asynchronously so the reset work is available
3099 * to cleanup errors that may occur during reinitialization */
3100 schedule_work(&dev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07003101}
3102
3103static void nvme_reset_failed_dev(struct work_struct *ws)
3104{
3105 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
3106 nvme_dev_reset(dev);
3107}
3108
Tejun Heo9ca97372014-03-07 10:24:49 -05003109static void nvme_reset_workfn(struct work_struct *work)
3110{
3111 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
3112 dev->reset_workfn(work);
3113}
3114
Keith Busch4cc06522015-06-05 10:30:08 -06003115static int nvme_reset(struct nvme_dev *dev)
3116{
3117 int ret = -EBUSY;
3118
3119 if (!dev->admin_q || blk_queue_dying(dev->admin_q))
3120 return -ENODEV;
3121
3122 spin_lock(&dev_list_lock);
3123 if (!work_pending(&dev->reset_work)) {
3124 dev->reset_workfn = nvme_reset_failed_dev;
3125 queue_work(nvme_workq, &dev->reset_work);
3126 ret = 0;
3127 }
3128 spin_unlock(&dev_list_lock);
3129
3130 if (!ret) {
3131 flush_work(&dev->reset_work);
Keith Buschffe77042015-06-08 10:08:15 -06003132 flush_work(&dev->probe_work);
Keith Busch4cc06522015-06-05 10:30:08 -06003133 return 0;
3134 }
3135
3136 return ret;
3137}
3138
3139static ssize_t nvme_sysfs_reset(struct device *dev,
3140 struct device_attribute *attr, const char *buf,
3141 size_t count)
3142{
3143 struct nvme_dev *ndev = dev_get_drvdata(dev);
3144 int ret;
3145
3146 ret = nvme_reset(ndev);
3147 if (ret < 0)
3148 return ret;
3149
3150 return count;
3151}
3152static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
3153
Keith Busch2e1d8442015-02-12 15:33:00 -07003154static void nvme_async_probe(struct work_struct *work);
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003155static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003156{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003157 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003158 struct nvme_dev *dev;
3159
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003160 node = dev_to_node(&pdev->dev);
3161 if (node == NUMA_NO_NODE)
3162 set_dev_node(&pdev->dev, 0);
3163
3164 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003165 if (!dev)
3166 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003167 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
3168 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003169 if (!dev->entry)
3170 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003171 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
3172 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003173 if (!dev->queues)
3174 goto free;
3175
3176 INIT_LIST_HEAD(&dev->namespaces);
Tejun Heo9ca97372014-03-07 10:24:49 -05003177 dev->reset_workfn = nvme_reset_failed_dev;
3178 INIT_WORK(&dev->reset_work, nvme_reset_workfn);
Christoph Hellwige75ec752015-05-22 11:12:39 +02003179 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003180 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07003181 result = nvme_set_instance(dev);
3182 if (result)
Keith Buscha96d4f52014-08-19 19:15:59 -06003183 goto put_pci;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003184
Matthew Wilcox091b6092011-02-10 09:56:01 -05003185 result = nvme_setup_prp_pools(dev);
3186 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06003187 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05003188
Keith Buschfb35e912014-03-03 11:09:47 -07003189 kref_init(&dev->kref);
Keith Buschb3fffde2015-02-03 11:21:42 -07003190 dev->device = device_create(nvme_class, &pdev->dev,
3191 MKDEV(nvme_char_major, dev->instance),
3192 dev, "nvme%d", dev->instance);
3193 if (IS_ERR(dev->device)) {
3194 result = PTR_ERR(dev->device);
Keith Busch2e1d8442015-02-12 15:33:00 -07003195 goto release_pools;
Keith Buschb3fffde2015-02-03 11:21:42 -07003196 }
3197 get_device(dev->device);
Keith Busch4cc06522015-06-05 10:30:08 -06003198 dev_set_drvdata(dev->device, dev);
3199
3200 result = device_create_file(dev->device, &dev_attr_reset_controller);
3201 if (result)
3202 goto put_dev;
Keith Buschb3fffde2015-02-03 11:21:42 -07003203
Keith Busche6e96d72015-03-23 09:32:37 -06003204 INIT_LIST_HEAD(&dev->node);
Keith Buscha5768aa2015-06-01 14:28:14 -06003205 INIT_WORK(&dev->scan_work, nvme_dev_scan);
Keith Busch2e1d8442015-02-12 15:33:00 -07003206 INIT_WORK(&dev->probe_work, nvme_async_probe);
3207 schedule_work(&dev->probe_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003208 return 0;
3209
Keith Busch4cc06522015-06-05 10:30:08 -06003210 put_dev:
3211 device_destroy(nvme_class, MKDEV(nvme_char_major, dev->instance));
3212 put_device(dev->device);
Keith Busch0877cb02013-07-15 15:02:19 -06003213 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05003214 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06003215 release:
3216 nvme_release_instance(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06003217 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02003218 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003219 free:
3220 kfree(dev->queues);
3221 kfree(dev->entry);
3222 kfree(dev);
3223 return result;
3224}
3225
Keith Busch2e1d8442015-02-12 15:33:00 -07003226static void nvme_async_probe(struct work_struct *work)
3227{
3228 struct nvme_dev *dev = container_of(work, struct nvme_dev, probe_work);
Keith Busch2e1d8442015-02-12 15:33:00 -07003229
Keith Buschde3eff22015-06-18 13:36:39 -06003230 if (nvme_dev_resume(dev) && !work_busy(&dev->reset_work))
3231 nvme_dead_ctrl(dev);
Keith Busch2e1d8442015-02-12 15:33:00 -07003232}
3233
Keith Buschf0d54a52014-05-02 10:40:43 -06003234static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
3235{
Keith Buscha6739472014-06-23 16:03:21 -06003236 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06003237
Keith Buscha6739472014-06-23 16:03:21 -06003238 if (prepare)
3239 nvme_dev_shutdown(dev);
3240 else
3241 nvme_dev_resume(dev);
Keith Buschf0d54a52014-05-02 10:40:43 -06003242}
3243
Keith Busch09ece142014-01-27 11:29:40 -05003244static void nvme_shutdown(struct pci_dev *pdev)
3245{
3246 struct nvme_dev *dev = pci_get_drvdata(pdev);
3247 nvme_dev_shutdown(dev);
3248}
3249
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003250static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003251{
3252 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003253
3254 spin_lock(&dev_list_lock);
3255 list_del_init(&dev->node);
3256 spin_unlock(&dev_list_lock);
3257
3258 pci_set_drvdata(pdev, NULL);
Keith Busch2e1d8442015-02-12 15:33:00 -07003259 flush_work(&dev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07003260 flush_work(&dev->reset_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06003261 flush_work(&dev->scan_work);
Keith Busch4cc06522015-06-05 10:30:08 -06003262 device_remove_file(dev->device, &dev_attr_reset_controller);
Keith Buschc9d3bf82015-01-07 18:55:52 -07003263 nvme_dev_remove(dev);
Keith Busch3399a3f2015-06-18 13:36:40 -06003264 nvme_dev_shutdown(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003265 nvme_dev_remove_admin(dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07003266 device_destroy(nvme_class, MKDEV(nvme_char_major, dev->instance));
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003267 nvme_free_queues(dev, 0);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06003268 nvme_release_cmb(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003269 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07003270 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003271}
3272
3273/* These functions are yet to be implemented */
3274#define nvme_error_detected NULL
3275#define nvme_dump_registers NULL
3276#define nvme_link_reset NULL
3277#define nvme_slot_reset NULL
3278#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06003279
Jingoo Han671a6012014-02-13 11:19:14 +09003280#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06003281static int nvme_suspend(struct device *dev)
3282{
3283 struct pci_dev *pdev = to_pci_dev(dev);
3284 struct nvme_dev *ndev = pci_get_drvdata(pdev);
3285
3286 nvme_dev_shutdown(ndev);
3287 return 0;
3288}
3289
3290static int nvme_resume(struct device *dev)
3291{
3292 struct pci_dev *pdev = to_pci_dev(dev);
3293 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06003294
Keith Busch9a6b9452013-12-10 13:10:36 -07003295 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Tejun Heo9ca97372014-03-07 10:24:49 -05003296 ndev->reset_workfn = nvme_reset_failed_dev;
Keith Busch9a6b9452013-12-10 13:10:36 -07003297 queue_work(nvme_workq, &ndev->reset_work);
3298 }
3299 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06003300}
Jingoo Han671a6012014-02-13 11:19:14 +09003301#endif
Keith Buschcd638942013-07-15 15:02:23 -06003302
3303static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003304
Stephen Hemminger1d352032012-09-07 09:33:17 -07003305static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003306 .error_detected = nvme_error_detected,
3307 .mmio_enabled = nvme_dump_registers,
3308 .link_reset = nvme_link_reset,
3309 .slot_reset = nvme_slot_reset,
3310 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06003311 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003312};
3313
3314/* Move to pci_ids.h later */
3315#define PCI_CLASS_STORAGE_EXPRESS 0x010802
3316
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003317static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003318 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
3319 { 0, }
3320};
3321MODULE_DEVICE_TABLE(pci, nvme_id_table);
3322
3323static struct pci_driver nvme_driver = {
3324 .name = "nvme",
3325 .id_table = nvme_id_table,
3326 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003327 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05003328 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06003329 .driver = {
3330 .pm = &nvme_dev_pm_ops,
3331 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003332 .err_handler = &nvme_err_handler,
3333};
3334
3335static int __init nvme_init(void)
3336{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003337 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003338
Dan McLeranb9afca32014-04-07 17:10:11 -06003339 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003340
Keith Busch9a6b9452013-12-10 13:10:36 -07003341 nvme_workq = create_singlethread_workqueue("nvme");
3342 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06003343 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07003344
Keith Busch5c42ea12012-07-25 16:05:18 -06003345 result = register_blkdev(nvme_major, "nvme");
3346 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07003347 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06003348 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003349 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003350
Keith Buschb3fffde2015-02-03 11:21:42 -07003351 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
3352 &nvme_dev_fops);
3353 if (result < 0)
3354 goto unregister_blkdev;
3355 else if (result > 0)
3356 nvme_char_major = result;
3357
3358 nvme_class = class_create(THIS_MODULE, "nvme");
Alexey Khoroshilovc7270402015-03-07 01:43:41 +03003359 if (IS_ERR(nvme_class)) {
3360 result = PTR_ERR(nvme_class);
Keith Buschb3fffde2015-02-03 11:21:42 -07003361 goto unregister_chrdev;
Alexey Khoroshilovc7270402015-03-07 01:43:41 +03003362 }
Keith Buschb3fffde2015-02-03 11:21:42 -07003363
Keith Buschf3db22f2014-06-11 11:51:35 -06003364 result = pci_register_driver(&nvme_driver);
3365 if (result)
Keith Buschb3fffde2015-02-03 11:21:42 -07003366 goto destroy_class;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003367 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003368
Keith Buschb3fffde2015-02-03 11:21:42 -07003369 destroy_class:
3370 class_destroy(nvme_class);
3371 unregister_chrdev:
3372 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003373 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003374 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003375 kill_workq:
3376 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003377 return result;
3378}
3379
3380static void __exit nvme_exit(void)
3381{
3382 pci_unregister_driver(&nvme_driver);
3383 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003384 destroy_workqueue(nvme_workq);
Keith Buschb3fffde2015-02-03 11:21:42 -07003385 class_destroy(nvme_class);
3386 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Dan McLeranb9afca32014-04-07 17:10:11 -06003387 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04003388 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003389}
3390
3391MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
3392MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07003393MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003394module_init(nvme_init);
3395module_exit(nvme_exit);