blob: 3a35c5807bb7cdaa38da806ee7ae2a083c92ba32 [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 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700387static int __nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500388{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700389 u16 tail = nvmeq->sq_tail;
390
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600391 if (nvmeq->sq_cmds_io)
392 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
393 else
394 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
395
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500396 if (++tail == nvmeq->q_depth)
397 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500398 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500399 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500400
401 return 0;
402}
403
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700404static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
405{
406 unsigned long flags;
407 int ret;
408 spin_lock_irqsave(&nvmeq->q_lock, flags);
409 ret = __nvme_submit_cmd(nvmeq, cmd);
410 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
411 return ret;
412}
413
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500414static __le64 **iod_list(struct nvme_iod *iod)
415{
416 return ((void *)iod) + iod->offset;
417}
418
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700419static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
420 unsigned nseg, unsigned long private)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500421{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700422 iod->private = private;
423 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
424 iod->npages = -1;
425 iod->length = nbytes;
426 iod->nents = 0;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500427}
428
429static struct nvme_iod *
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700430__nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
431 unsigned long priv, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500432{
433 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700434 sizeof(__le64 *) * nvme_npages(bytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500435 sizeof(struct scatterlist) * nseg, gfp);
436
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700437 if (iod)
438 iod_init(iod, bytes, nseg, priv);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500439
440 return iod;
441}
442
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700443static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
444 gfp_t gfp)
445{
446 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
447 sizeof(struct nvme_dsm_range);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700448 struct nvme_iod *iod;
449
450 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
451 size <= NVME_INT_BYTES(dev)) {
452 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
453
454 iod = cmd->iod;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700455 iod_init(iod, size, rq->nr_phys_segments,
Chong Yuanfda631f2015-03-27 09:21:32 +0800456 (unsigned long) rq | NVME_INT_MASK);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700457 return iod;
458 }
459
460 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
461 (unsigned long) rq, gfp);
462}
463
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200464static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500465{
Keith Busch1d090622014-06-23 11:34:01 -0600466 const int last_prp = dev->page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500467 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500468 __le64 **list = iod_list(iod);
469 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500470
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500471 if (iod->npages == 0)
472 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
473 for (i = 0; i < iod->npages; i++) {
474 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500475 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500476 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500477 prp_dma = next_prp_dma;
478 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700479
480 if (iod_should_kfree(iod))
481 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500482}
483
Keith Buschb4ff9c82014-08-29 09:06:12 -0600484static int nvme_error_status(u16 status)
485{
486 switch (status & 0x7ff) {
487 case NVME_SC_SUCCESS:
488 return 0;
489 case NVME_SC_CAP_EXCEEDED:
490 return -ENOSPC;
491 default:
492 return -EIO;
493 }
494}
495
Keith Busch52b68d72015-02-23 09:16:21 -0700496#ifdef CONFIG_BLK_DEV_INTEGRITY
Keith Busche1e5e562015-02-19 13:39:03 -0700497static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
498{
499 if (be32_to_cpu(pi->ref_tag) == v)
500 pi->ref_tag = cpu_to_be32(p);
501}
502
503static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
504{
505 if (be32_to_cpu(pi->ref_tag) == p)
506 pi->ref_tag = cpu_to_be32(v);
507}
508
509/**
510 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
511 *
512 * The virtual start sector is the one that was originally submitted by the
513 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
514 * start sector may be different. Remap protection information to match the
515 * physical LBA on writes, and back to the original seed on reads.
516 *
517 * Type 0 and 3 do not have a ref tag, so no remapping required.
518 */
519static void nvme_dif_remap(struct request *req,
520 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
521{
522 struct nvme_ns *ns = req->rq_disk->private_data;
523 struct bio_integrity_payload *bip;
524 struct t10_pi_tuple *pi;
525 void *p, *pmap;
526 u32 i, nlb, ts, phys, virt;
527
528 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
529 return;
530
531 bip = bio_integrity(req->bio);
532 if (!bip)
533 return;
534
535 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
Keith Busche1e5e562015-02-19 13:39:03 -0700536
537 p = pmap;
538 virt = bip_get_seed(bip);
539 phys = nvme_block_nr(ns, blk_rq_pos(req));
540 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
541 ts = ns->disk->integrity->tuple_size;
542
543 for (i = 0; i < nlb; i++, virt++, phys++) {
544 pi = (struct t10_pi_tuple *)p;
545 dif_swap(phys, virt, pi);
546 p += ts;
547 }
548 kunmap_atomic(pmap);
549}
550
Keith Busch52b68d72015-02-23 09:16:21 -0700551static int nvme_noop_verify(struct blk_integrity_iter *iter)
552{
553 return 0;
554}
555
556static int nvme_noop_generate(struct blk_integrity_iter *iter)
557{
558 return 0;
559}
560
561struct blk_integrity nvme_meta_noop = {
562 .name = "NVME_META_NOOP",
563 .generate_fn = nvme_noop_generate,
564 .verify_fn = nvme_noop_verify,
565};
566
567static void nvme_init_integrity(struct nvme_ns *ns)
568{
569 struct blk_integrity integrity;
570
571 switch (ns->pi_type) {
572 case NVME_NS_DPS_PI_TYPE3:
573 integrity = t10_pi_type3_crc;
574 break;
575 case NVME_NS_DPS_PI_TYPE1:
576 case NVME_NS_DPS_PI_TYPE2:
577 integrity = t10_pi_type1_crc;
578 break;
579 default:
580 integrity = nvme_meta_noop;
581 break;
582 }
583 integrity.tuple_size = ns->ms;
584 blk_integrity_register(ns->disk, &integrity);
585 blk_queue_max_integrity_segments(ns->queue, 1);
586}
587#else /* CONFIG_BLK_DEV_INTEGRITY */
588static void nvme_dif_remap(struct request *req,
589 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
590{
591}
592static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
593{
594}
595static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
596{
597}
598static void nvme_init_integrity(struct nvme_ns *ns)
599{
600}
601#endif
602
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700603static void req_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500604 struct nvme_completion *cqe)
605{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500606 struct nvme_iod *iod = ctx;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700607 struct request *req = iod_get_private(iod);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700608 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
609
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500610 u16 status = le16_to_cpup(&cqe->status) >> 1;
611
Keith Buschedd10d32014-04-03 16:45:23 -0600612 if (unlikely(status)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700613 if (!(status & NVME_SC_DNR || blk_noretry_request(req))
614 && (jiffies - req->start_time) < req->timeout) {
Keith Buschc9d3bf82015-01-07 18:55:52 -0700615 unsigned long flags;
616
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700617 blk_mq_requeue_request(req);
Keith Buschc9d3bf82015-01-07 18:55:52 -0700618 spin_lock_irqsave(req->q->queue_lock, flags);
619 if (!blk_queue_stopped(req->q))
620 blk_mq_kick_requeue_list(req->q);
621 spin_unlock_irqrestore(req->q->queue_lock, flags);
Keith Buschedd10d32014-04-03 16:45:23 -0600622 return;
623 }
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200624 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
Keith Busch17188bb2015-06-08 10:08:14 -0600625 if (cmd_rq->ctx == CMD_CTX_CANCELLED)
626 req->errors = -EINTR;
627 else
628 req->errors = status;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200629 } else {
630 req->errors = nvme_error_status(status);
631 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700632 } else
633 req->errors = 0;
Keith Buscha0a931d2015-05-22 12:28:31 -0600634 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
635 u32 result = le32_to_cpup(&cqe->result);
636 req->special = (void *)(uintptr_t)result;
637 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700638
639 if (cmd_rq->aborted)
Christoph Hellwige75ec752015-05-22 11:12:39 +0200640 dev_warn(nvmeq->dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700641 "completing aborted command with status:%04x\n",
642 status);
643
Keith Busche1e5e562015-02-19 13:39:03 -0700644 if (iod->nents) {
Christoph Hellwige75ec752015-05-22 11:12:39 +0200645 dma_unmap_sg(nvmeq->dev->dev, iod->sg, iod->nents,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700646 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busche1e5e562015-02-19 13:39:03 -0700647 if (blk_integrity_rq(req)) {
648 if (!rq_data_dir(req))
649 nvme_dif_remap(req, nvme_dif_complete);
Christoph Hellwige75ec752015-05-22 11:12:39 +0200650 dma_unmap_sg(nvmeq->dev->dev, iod->meta_sg, 1,
Keith Busche1e5e562015-02-19 13:39:03 -0700651 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
652 }
653 }
Keith Buschedd10d32014-04-03 16:45:23 -0600654 nvme_free_iod(nvmeq->dev, iod);
Keith Busch3291fa52014-04-28 12:30:52 -0600655
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700656 blk_mq_complete_request(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500657}
658
Matthew Wilcox184d2942011-05-11 21:36:38 -0400659/* length is in bytes. gfp flags indicates whether we may sleep. */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200660static int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod,
661 int total_len, gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500662{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500663 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500664 int length = total_len;
665 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500666 int dma_len = sg_dma_len(sg);
667 u64 dma_addr = sg_dma_address(sg);
Murali Iyerf137e0f2015-03-26 11:07:51 -0500668 u32 page_size = dev->page_size;
669 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500670 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500671 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500672 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500673 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500674
Keith Busch1d090622014-06-23 11:34:01 -0600675 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500676 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500677 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500678
Keith Busch1d090622014-06-23 11:34:01 -0600679 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500680 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600681 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500682 } else {
683 sg = sg_next(sg);
684 dma_addr = sg_dma_address(sg);
685 dma_len = sg_dma_len(sg);
686 }
687
Keith Busch1d090622014-06-23 11:34:01 -0600688 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600689 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500690 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500691 }
692
Keith Busch1d090622014-06-23 11:34:01 -0600693 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500694 if (nprps <= (256 / 8)) {
695 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500696 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500697 } else {
698 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500699 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500700 }
701
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400702 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
703 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600704 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500705 iod->npages = -1;
Keith Busch1d090622014-06-23 11:34:01 -0600706 return (total_len - length) + page_size;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400707 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500708 list[0] = prp_list;
709 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500710 i = 0;
711 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600712 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500713 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400714 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500715 if (!prp_list)
716 return total_len - length;
717 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400718 prp_list[0] = old_prp_list[i - 1];
719 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
720 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500721 }
722 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600723 dma_len -= page_size;
724 dma_addr += page_size;
725 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500726 if (length <= 0)
727 break;
728 if (dma_len > 0)
729 continue;
730 BUG_ON(dma_len < 0);
731 sg = sg_next(sg);
732 dma_addr = sg_dma_address(sg);
733 dma_len = sg_dma_len(sg);
734 }
735
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500736 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500737}
738
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200739static void nvme_submit_priv(struct nvme_queue *nvmeq, struct request *req,
740 struct nvme_iod *iod)
741{
Jon Derrick498c4392015-07-20 10:14:08 -0600742 struct nvme_command cmnd;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200743
Jon Derrick498c4392015-07-20 10:14:08 -0600744 memcpy(&cmnd, req->cmd, sizeof(cmnd));
745 cmnd.rw.command_id = req->tag;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200746 if (req->nr_phys_segments) {
Jon Derrick498c4392015-07-20 10:14:08 -0600747 cmnd.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
748 cmnd.rw.prp2 = cpu_to_le64(iod->first_dma);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200749 }
750
Jon Derrick498c4392015-07-20 10:14:08 -0600751 __nvme_submit_cmd(nvmeq, &cmnd);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200752}
753
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700754/*
755 * We reuse the small pool to allocate the 16-byte range here as it is not
756 * worth having a special pool for these or additional cases to handle freeing
757 * the iod.
758 */
759static void nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
760 struct request *req, struct nvme_iod *iod)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700761{
Keith Buschedd10d32014-04-03 16:45:23 -0600762 struct nvme_dsm_range *range =
763 (struct nvme_dsm_range *)iod_list(iod)[0];
Jon Derrick498c4392015-07-20 10:14:08 -0600764 struct nvme_command cmnd;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700765
Keith Busch0e5e4f02012-11-09 16:33:05 -0700766 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700767 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
768 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700769
Jon Derrick498c4392015-07-20 10:14:08 -0600770 memset(&cmnd, 0, sizeof(cmnd));
771 cmnd.dsm.opcode = nvme_cmd_dsm;
772 cmnd.dsm.command_id = req->tag;
773 cmnd.dsm.nsid = cpu_to_le32(ns->ns_id);
774 cmnd.dsm.prp1 = cpu_to_le64(iod->first_dma);
775 cmnd.dsm.nr = 0;
776 cmnd.dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
Keith Busch0e5e4f02012-11-09 16:33:05 -0700777
Jon Derrick498c4392015-07-20 10:14:08 -0600778 __nvme_submit_cmd(nvmeq, &cmnd);
Keith Busch0e5e4f02012-11-09 16:33:05 -0700779}
780
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700781static void nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500782 int cmdid)
783{
Jon Derrick498c4392015-07-20 10:14:08 -0600784 struct nvme_command cmnd;
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500785
Jon Derrick498c4392015-07-20 10:14:08 -0600786 memset(&cmnd, 0, sizeof(cmnd));
787 cmnd.common.opcode = nvme_cmd_flush;
788 cmnd.common.command_id = cmdid;
789 cmnd.common.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500790
Jon Derrick498c4392015-07-20 10:14:08 -0600791 __nvme_submit_cmd(nvmeq, &cmnd);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500792}
793
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700794static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod,
795 struct nvme_ns *ns)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500796{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700797 struct request *req = iod_get_private(iod);
Jon Derrick498c4392015-07-20 10:14:08 -0600798 struct nvme_command cmnd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700799 u16 control = 0;
800 u32 dsmgmt = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500801
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700802 if (req->cmd_flags & REQ_FUA)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500803 control |= NVME_RW_FUA;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700804 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500805 control |= NVME_RW_LR;
806
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700807 if (req->cmd_flags & REQ_RAHEAD)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500808 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
809
Jon Derrick498c4392015-07-20 10:14:08 -0600810 memset(&cmnd, 0, sizeof(cmnd));
811 cmnd.rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
812 cmnd.rw.command_id = req->tag;
813 cmnd.rw.nsid = cpu_to_le32(ns->ns_id);
814 cmnd.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
815 cmnd.rw.prp2 = cpu_to_le64(iod->first_dma);
816 cmnd.rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
817 cmnd.rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
Keith Busche1e5e562015-02-19 13:39:03 -0700818
819 if (blk_integrity_rq(req)) {
Jon Derrick498c4392015-07-20 10:14:08 -0600820 cmnd.rw.metadata = cpu_to_le64(sg_dma_address(iod->meta_sg));
Keith Busche1e5e562015-02-19 13:39:03 -0700821 switch (ns->pi_type) {
822 case NVME_NS_DPS_PI_TYPE3:
823 control |= NVME_RW_PRINFO_PRCHK_GUARD;
824 break;
825 case NVME_NS_DPS_PI_TYPE1:
826 case NVME_NS_DPS_PI_TYPE2:
827 control |= NVME_RW_PRINFO_PRCHK_GUARD |
828 NVME_RW_PRINFO_PRCHK_REF;
Jon Derrick498c4392015-07-20 10:14:08 -0600829 cmnd.rw.reftag = cpu_to_le32(
Keith Busche1e5e562015-02-19 13:39:03 -0700830 nvme_block_nr(ns, blk_rq_pos(req)));
831 break;
832 }
833 } else if (ns->ms)
834 control |= NVME_RW_PRINFO_PRACT;
835
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);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001082 return __nvme_submit_cmd(nvmeq, &c);
1083}
1084
1085static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
Keith Busch4d115422013-12-10 13:10:40 -07001086 struct nvme_command *cmd,
1087 struct async_cmd_info *cmdinfo, unsigned timeout)
1088{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001089 struct nvme_queue *nvmeq = dev->queues[0];
1090 struct request *req;
1091 struct nvme_cmd_info *cmd_rq;
Keith Busch4d115422013-12-10 13:10:40 -07001092
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001093 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_KERNEL, false);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001094 if (IS_ERR(req))
1095 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001096
1097 req->timeout = timeout;
1098 cmd_rq = blk_mq_rq_to_pdu(req);
1099 cmdinfo->req = req;
1100 nvme_set_info(cmd_rq, cmdinfo, async_completion);
Keith Busch4d115422013-12-10 13:10:40 -07001101 cmdinfo->status = -EINTR;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001102
1103 cmd->common.command_id = req->tag;
1104
Keith Busch4f5099a2014-03-03 16:39:13 -07001105 return nvme_submit_cmd(nvmeq, cmd);
Keith Busch4d115422013-12-10 13:10:40 -07001106}
1107
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001108static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1109{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001110 struct nvme_command c;
1111
1112 memset(&c, 0, sizeof(c));
1113 c.delete_queue.opcode = opcode;
1114 c.delete_queue.qid = cpu_to_le16(id);
1115
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001116 return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001117}
1118
1119static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1120 struct nvme_queue *nvmeq)
1121{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001122 struct nvme_command c;
1123 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1124
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001125 /*
1126 * Note: we (ab)use the fact the the prp fields survive if no data
1127 * is attached to the request.
1128 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001129 memset(&c, 0, sizeof(c));
1130 c.create_cq.opcode = nvme_admin_create_cq;
1131 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1132 c.create_cq.cqid = cpu_to_le16(qid);
1133 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1134 c.create_cq.cq_flags = cpu_to_le16(flags);
1135 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1136
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001137 return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001138}
1139
1140static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1141 struct nvme_queue *nvmeq)
1142{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001143 struct nvme_command c;
1144 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1145
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001146 /*
1147 * Note: we (ab)use the fact the the prp fields survive if no data
1148 * is attached to the request.
1149 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001150 memset(&c, 0, sizeof(c));
1151 c.create_sq.opcode = nvme_admin_create_sq;
1152 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1153 c.create_sq.sqid = cpu_to_le16(qid);
1154 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1155 c.create_sq.sq_flags = cpu_to_le16(flags);
1156 c.create_sq.cqid = cpu_to_le16(qid);
1157
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001158 return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001159}
1160
1161static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1162{
1163 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1164}
1165
1166static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1167{
1168 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1169}
1170
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001171int nvme_identify_ctrl(struct nvme_dev *dev, struct nvme_id_ctrl **id)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001172{
Andrew Mortone44ac582015-06-27 12:20:34 -06001173 struct nvme_command c = { };
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001174 int error;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001175
Andrew Mortone44ac582015-06-27 12:20:34 -06001176 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
1177 c.identify.opcode = nvme_admin_identify;
1178 c.identify.cns = cpu_to_le32(1);
1179
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001180 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
1181 if (!*id)
1182 return -ENOMEM;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001183
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001184 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
1185 sizeof(struct nvme_id_ctrl));
1186 if (error)
1187 kfree(*id);
1188 return error;
1189}
1190
1191int nvme_identify_ns(struct nvme_dev *dev, unsigned nsid,
1192 struct nvme_id_ns **id)
1193{
Andrew Mortone44ac582015-06-27 12:20:34 -06001194 struct nvme_command c = { };
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001195 int error;
1196
Andrew Mortone44ac582015-06-27 12:20:34 -06001197 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
1198 c.identify.opcode = nvme_admin_identify,
1199 c.identify.nsid = cpu_to_le32(nsid),
1200
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001201 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
1202 if (!*id)
1203 return -ENOMEM;
1204
1205 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
1206 sizeof(struct nvme_id_ns));
1207 if (error)
1208 kfree(*id);
1209 return error;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001210}
1211
Vishal Verma5d0f6132013-03-04 18:40:58 -07001212int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -06001213 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001214{
1215 struct nvme_command c;
1216
1217 memset(&c, 0, sizeof(c));
1218 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -06001219 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001220 c.features.prp1 = cpu_to_le64(dma_addr);
1221 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001222
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001223 return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, NULL, 0,
1224 result, 0);
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001225}
1226
Vishal Verma5d0f6132013-03-04 18:40:58 -07001227int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
1228 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001229{
1230 struct nvme_command c;
1231
1232 memset(&c, 0, sizeof(c));
1233 c.features.opcode = nvme_admin_set_features;
1234 c.features.prp1 = cpu_to_le64(dma_addr);
1235 c.features.fid = cpu_to_le32(fid);
1236 c.features.dword11 = cpu_to_le32(dword11);
1237
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001238 return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, NULL, 0,
1239 result, 0);
1240}
1241
1242int nvme_get_log_page(struct nvme_dev *dev, struct nvme_smart_log **log)
1243{
Andrew Mortone44ac582015-06-27 12:20:34 -06001244 struct nvme_command c = { };
1245 int error;
1246
1247 c.common.opcode = nvme_admin_get_log_page,
1248 c.common.nsid = cpu_to_le32(0xFFFFFFFF),
1249 c.common.cdw10[0] = cpu_to_le32(
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001250 (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
1251 NVME_LOG_SMART),
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001252
1253 *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
1254 if (!*log)
1255 return -ENOMEM;
1256
1257 error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
1258 sizeof(struct nvme_smart_log));
1259 if (error)
1260 kfree(*log);
1261 return error;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001262}
1263
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001264/**
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001265 * nvme_abort_req - Attempt aborting a request
Keith Buschc30341d2013-12-10 13:10:38 -07001266 *
1267 * Schedule controller reset if the command was already aborted once before and
1268 * still hasn't been returned to the driver, or if this is the admin queue.
1269 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001270static void nvme_abort_req(struct request *req)
Keith Buschc30341d2013-12-10 13:10:38 -07001271{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001272 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1273 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001274 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001275 struct request *abort_req;
1276 struct nvme_cmd_info *abort_cmd;
1277 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -07001278
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001279 if (!nvmeq->qid || cmd_rq->aborted) {
Keith Busch7a509a62015-01-07 18:55:53 -07001280 unsigned long flags;
1281
1282 spin_lock_irqsave(&dev_list_lock, flags);
Keith Buschc30341d2013-12-10 13:10:38 -07001283 if (work_busy(&dev->reset_work))
Keith Busch7a509a62015-01-07 18:55:53 -07001284 goto out;
Keith Buschc30341d2013-12-10 13:10:38 -07001285 list_del_init(&dev->node);
Christoph Hellwige75ec752015-05-22 11:12:39 +02001286 dev_warn(dev->dev, "I/O %d QID %d timeout, reset controller\n",
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001287 req->tag, nvmeq->qid);
Tejun Heo9ca97372014-03-07 10:24:49 -05001288 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschc30341d2013-12-10 13:10:38 -07001289 queue_work(nvme_workq, &dev->reset_work);
Keith Busch7a509a62015-01-07 18:55:53 -07001290 out:
1291 spin_unlock_irqrestore(&dev_list_lock, flags);
Keith Buschc30341d2013-12-10 13:10:38 -07001292 return;
1293 }
1294
1295 if (!dev->abort_limit)
1296 return;
1297
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001298 abort_req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC,
1299 false);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001300 if (IS_ERR(abort_req))
Keith Buschc30341d2013-12-10 13:10:38 -07001301 return;
1302
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001303 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1304 nvme_set_info(abort_cmd, abort_req, abort_completion);
1305
Keith Buschc30341d2013-12-10 13:10:38 -07001306 memset(&cmd, 0, sizeof(cmd));
1307 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001308 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001309 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001310 cmd.abort.command_id = abort_req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001311
1312 --dev->abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001313 cmd_rq->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -07001314
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001315 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", req->tag,
Keith Buschc30341d2013-12-10 13:10:38 -07001316 nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001317 if (nvme_submit_cmd(dev->queues[0], &cmd) < 0) {
1318 dev_warn(nvmeq->q_dmadev,
1319 "Could not abort I/O %d QID %d",
1320 req->tag, nvmeq->qid);
Sam Bradshawc87fd542014-12-10 13:00:31 -07001321 blk_mq_free_request(abort_req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001322 }
Keith Buschc30341d2013-12-10 13:10:38 -07001323}
1324
Keith Busch42483222015-06-01 09:29:54 -06001325static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001326{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001327 struct nvme_queue *nvmeq = data;
1328 void *ctx;
1329 nvme_completion_fn fn;
1330 struct nvme_cmd_info *cmd;
Keith Buschcef6a942015-01-07 18:55:51 -07001331 struct nvme_completion cqe;
1332
1333 if (!blk_mq_request_started(req))
1334 return;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001335
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001336 cmd = blk_mq_rq_to_pdu(req);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001337
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001338 if (cmd->ctx == CMD_CTX_CANCELLED)
1339 return;
1340
Keith Buschcef6a942015-01-07 18:55:51 -07001341 if (blk_queue_dying(req->q))
1342 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1343 else
1344 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1345
1346
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001347 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1348 req->tag, nvmeq->qid);
1349 ctx = cancel_cmd_info(cmd, &fn);
1350 fn(nvmeq, ctx, &cqe);
1351}
1352
1353static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
1354{
1355 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
1356 struct nvme_queue *nvmeq = cmd->nvmeq;
1357
Keith Busch07836e62015-02-19 10:34:48 -07001358 dev_warn(nvmeq->q_dmadev, "Timeout I/O %d QID %d\n", req->tag,
1359 nvmeq->qid);
1360 spin_lock_irq(&nvmeq->q_lock);
1361 nvme_abort_req(req);
1362 spin_unlock_irq(&nvmeq->q_lock);
1363
Keith Busch7a509a62015-01-07 18:55:53 -07001364 /*
1365 * The aborted req will be completed on receiving the abort req.
1366 * We enable the timer again. If hit twice, it'll cause a device reset,
1367 * as the device then is in a faulty state.
1368 */
Keith Busch07836e62015-02-19 10:34:48 -07001369 return BLK_EH_RESET_TIMER;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001370}
1371
Keith Buschf435c282014-07-07 09:14:42 -06001372static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001373{
1374 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1375 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001376 if (nvmeq->sq_cmds)
1377 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
Matthew Wilcox9e866772012-08-03 13:55:56 -04001378 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1379 kfree(nvmeq);
1380}
1381
Keith Buscha1a5ef92013-12-16 13:50:00 -05001382static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001383{
1384 int i;
1385
Keith Buscha1a5ef92013-12-16 13:50:00 -05001386 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001387 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001388 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001389 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001390 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001391 }
Keith Busch22404272013-07-15 15:02:20 -06001392}
1393
Keith Busch4d115422013-12-10 13:10:40 -07001394/**
1395 * nvme_suspend_queue - put queue into suspended state
1396 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001397 */
1398static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001399{
Keith Busch2b25d982014-12-22 12:59:04 -07001400 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001401
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001402 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001403 if (nvmeq->cq_vector == -1) {
1404 spin_unlock_irq(&nvmeq->q_lock);
1405 return 1;
1406 }
1407 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Keith Busch42f61422014-03-24 10:46:25 -06001408 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001409 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001410 spin_unlock_irq(&nvmeq->q_lock);
1411
Keith Busch6df3dbc2015-03-26 13:49:33 -06001412 if (!nvmeq->qid && nvmeq->dev->admin_q)
1413 blk_mq_freeze_queue_start(nvmeq->dev->admin_q);
1414
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001415 irq_set_affinity_hint(vector, NULL);
1416 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001417
Keith Busch4d115422013-12-10 13:10:40 -07001418 return 0;
1419}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001420
Keith Busch4d115422013-12-10 13:10:40 -07001421static void nvme_clear_queue(struct nvme_queue *nvmeq)
1422{
Keith Busch22404272013-07-15 15:02:20 -06001423 spin_lock_irq(&nvmeq->q_lock);
Keith Busch42483222015-06-01 09:29:54 -06001424 if (nvmeq->tags && *nvmeq->tags)
1425 blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001426 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001427}
1428
Keith Busch4d115422013-12-10 13:10:40 -07001429static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1430{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001431 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001432
1433 if (!nvmeq)
1434 return;
1435 if (nvme_suspend_queue(nvmeq))
1436 return;
1437
Keith Busch0e53d182013-12-10 13:10:39 -07001438 /* Don't tell the adapter to delete the admin queue.
1439 * Don't tell a removed adapter to delete IO queues. */
1440 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001441 adapter_delete_sq(dev, qid);
1442 adapter_delete_cq(dev, qid);
1443 }
Keith Busch07836e62015-02-19 10:34:48 -07001444
1445 spin_lock_irq(&nvmeq->q_lock);
1446 nvme_process_cq(nvmeq);
1447 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001448}
1449
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001450static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1451 int entry_size)
1452{
1453 int q_depth = dev->q_depth;
1454 unsigned q_size_aligned = roundup(q_depth * entry_size, dev->page_size);
1455
1456 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001457 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
1458 mem_per_q = round_down(mem_per_q, dev->page_size);
1459 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001460
1461 /*
1462 * Ensure the reduced q_depth is above some threshold where it
1463 * would be better to map queues in system memory with the
1464 * original depth
1465 */
1466 if (q_depth < 64)
1467 return -ENOMEM;
1468 }
1469
1470 return q_depth;
1471}
1472
1473static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1474 int qid, int depth)
1475{
1476 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
1477 unsigned offset = (qid - 1) *
1478 roundup(SQ_SIZE(depth), dev->page_size);
1479 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1480 nvmeq->sq_cmds_io = dev->cmb + offset;
1481 } else {
1482 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1483 &nvmeq->sq_dma_addr, GFP_KERNEL);
1484 if (!nvmeq->sq_cmds)
1485 return -ENOMEM;
1486 }
1487
1488 return 0;
1489}
1490
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001491static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Keith Busch2b25d982014-12-22 12:59:04 -07001492 int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001493{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001494 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001495 if (!nvmeq)
1496 return NULL;
1497
Christoph Hellwige75ec752015-05-22 11:12:39 +02001498 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001499 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001500 if (!nvmeq->cqes)
1501 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001502
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001503 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001504 goto free_cqdma;
1505
Christoph Hellwige75ec752015-05-22 11:12:39 +02001506 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001507 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001508 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1509 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001510 spin_lock_init(&nvmeq->q_lock);
1511 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001512 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001513 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001514 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001515 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001516 nvmeq->cq_vector = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001517 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001518
Jon Derrick36a7e992015-05-27 12:26:23 -06001519 /* make sure queue descriptor is set before queue count, for kthread */
1520 mb();
1521 dev->queue_count++;
1522
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001523 return nvmeq;
1524
1525 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001526 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001527 nvmeq->cq_dma_addr);
1528 free_nvmeq:
1529 kfree(nvmeq);
1530 return NULL;
1531}
1532
Matthew Wilcox30010822011-01-20 09:10:15 -05001533static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1534 const char *name)
1535{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001536 if (use_threaded_interrupts)
1537 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001538 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001539 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001540 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001541 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001542}
1543
Keith Busch22404272013-07-15 15:02:20 -06001544static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001545{
Keith Busch22404272013-07-15 15:02:20 -06001546 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001547
Keith Busch7be50e92014-09-10 15:48:47 -06001548 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001549 nvmeq->sq_tail = 0;
1550 nvmeq->cq_head = 0;
1551 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001552 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001553 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001554 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001555 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001556}
1557
1558static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1559{
1560 struct nvme_dev *dev = nvmeq->dev;
1561 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001562
Keith Busch2b25d982014-12-22 12:59:04 -07001563 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001564 result = adapter_alloc_cq(dev, qid, nvmeq);
1565 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001566 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001567
1568 result = adapter_alloc_sq(dev, qid, nvmeq);
1569 if (result < 0)
1570 goto release_cq;
1571
Matthew Wilcox3193f072014-01-27 15:57:22 -05001572 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001573 if (result < 0)
1574 goto release_sq;
1575
Keith Busch22404272013-07-15 15:02:20 -06001576 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001577 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001578
1579 release_sq:
1580 adapter_delete_sq(dev, qid);
1581 release_cq:
1582 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001583 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001584}
1585
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001586static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1587{
1588 unsigned long timeout;
1589 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1590
1591 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1592
1593 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1594 msleep(100);
1595 if (fatal_signal_pending(current))
1596 return -EINTR;
1597 if (time_after(jiffies, timeout)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001598 dev_err(dev->dev,
Matthew Wilcox27e81662014-04-11 11:58:45 -04001599 "Device not ready; aborting %s\n", enabled ?
1600 "initialisation" : "reset");
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001601 return -ENODEV;
1602 }
1603 }
1604
1605 return 0;
1606}
1607
1608/*
1609 * If the device has been passed off to us in an enabled state, just clear
1610 * the enabled bit. The spec says we should set the 'shutdown notification
1611 * bits', but doing so may cause the device to complete commands to the
1612 * admin queue ... and we don't know what memory that might be pointing at!
1613 */
1614static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1615{
Dan McLeran01079522014-06-23 08:24:36 -06001616 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1617 dev->ctrl_config &= ~NVME_CC_ENABLE;
1618 writel(dev->ctrl_config, &dev->bar->cc);
Matthew Wilcox44af1462013-05-04 06:43:17 -04001619
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001620 return nvme_wait_ready(dev, cap, false);
1621}
1622
1623static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1624{
Dan McLeran01079522014-06-23 08:24:36 -06001625 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1626 dev->ctrl_config |= NVME_CC_ENABLE;
1627 writel(dev->ctrl_config, &dev->bar->cc);
1628
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001629 return nvme_wait_ready(dev, cap, true);
1630}
1631
Keith Busch1894d8f2013-07-15 15:02:22 -06001632static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1633{
1634 unsigned long timeout;
Keith Busch1894d8f2013-07-15 15:02:22 -06001635
Dan McLeran01079522014-06-23 08:24:36 -06001636 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1637 dev->ctrl_config |= NVME_CC_SHN_NORMAL;
1638
1639 writel(dev->ctrl_config, &dev->bar->cc);
Keith Busch1894d8f2013-07-15 15:02:22 -06001640
Dan McLeran2484f402014-07-01 09:33:32 -06001641 timeout = SHUTDOWN_TIMEOUT + jiffies;
Keith Busch1894d8f2013-07-15 15:02:22 -06001642 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1643 NVME_CSTS_SHST_CMPLT) {
1644 msleep(100);
1645 if (fatal_signal_pending(current))
1646 return -EINTR;
1647 if (time_after(jiffies, timeout)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001648 dev_err(dev->dev,
Keith Busch1894d8f2013-07-15 15:02:22 -06001649 "Device shutdown incomplete; abort shutdown\n");
1650 return -ENODEV;
1651 }
1652 }
1653
1654 return 0;
1655}
1656
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001657static struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001658 .queue_rq = nvme_queue_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001659 .map_queue = blk_mq_map_queue,
1660 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001661 .exit_hctx = nvme_admin_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001662 .init_request = nvme_admin_init_request,
1663 .timeout = nvme_timeout,
1664};
1665
1666static struct blk_mq_ops nvme_mq_ops = {
1667 .queue_rq = nvme_queue_rq,
1668 .map_queue = blk_mq_map_queue,
1669 .init_hctx = nvme_init_hctx,
1670 .init_request = nvme_init_request,
1671 .timeout = nvme_timeout,
1672};
1673
Keith Buschea191d22015-01-07 18:55:49 -07001674static void nvme_dev_remove_admin(struct nvme_dev *dev)
1675{
1676 if (dev->admin_q && !blk_queue_dying(dev->admin_q)) {
1677 blk_cleanup_queue(dev->admin_q);
1678 blk_mq_free_tag_set(&dev->admin_tagset);
1679 }
1680}
1681
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001682static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1683{
1684 if (!dev->admin_q) {
1685 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1686 dev->admin_tagset.nr_hw_queues = 1;
1687 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1;
Keith Busch1efccc92015-03-31 10:37:17 -06001688 dev->admin_tagset.reserved_tags = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001689 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001690 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001691 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001692 dev->admin_tagset.driver_data = dev;
1693
1694 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1695 return -ENOMEM;
1696
1697 dev->admin_q = blk_mq_init_queue(&dev->admin_tagset);
Ming Lei35b489d2015-01-02 14:25:27 +00001698 if (IS_ERR(dev->admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001699 blk_mq_free_tag_set(&dev->admin_tagset);
1700 return -ENOMEM;
1701 }
Keith Buschea191d22015-01-07 18:55:49 -07001702 if (!blk_get_queue(dev->admin_q)) {
1703 nvme_dev_remove_admin(dev);
Keith Busch4af0e212015-06-08 10:08:13 -06001704 dev->admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001705 return -ENODEV;
1706 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001707 } else
1708 blk_mq_unfreeze_queue(dev->admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001709
1710 return 0;
1711}
1712
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001713static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001714{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001715 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001716 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001717 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001718 struct nvme_queue *nvmeq;
Keith Busch1d090622014-06-23 11:34:01 -06001719 unsigned page_shift = PAGE_SHIFT;
1720 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12;
1721 unsigned dev_page_max = NVME_CAP_MPSMAX(cap) + 12;
1722
1723 if (page_shift < dev_page_min) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001724 dev_err(dev->dev,
Keith Busch1d090622014-06-23 11:34:01 -06001725 "Minimum device page size (%u) too large for "
1726 "host (%u)\n", 1 << dev_page_min,
1727 1 << page_shift);
1728 return -ENODEV;
1729 }
1730 if (page_shift > dev_page_max) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001731 dev_info(dev->dev,
Keith Busch1d090622014-06-23 11:34:01 -06001732 "Device maximum page size (%u) smaller than "
1733 "host (%u); enabling work-around\n",
1734 1 << dev_page_max, 1 << page_shift);
1735 page_shift = dev_page_max;
1736 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001737
Keith Buschdfbac8c2015-08-10 15:20:40 -06001738 dev->subsystem = readl(&dev->bar->vs) >= NVME_VS(1, 1) ?
1739 NVME_CAP_NSSRC(cap) : 0;
1740
1741 if (dev->subsystem && (readl(&dev->bar->csts) & NVME_CSTS_NSSRO))
1742 writel(NVME_CSTS_NSSRO, &dev->bar->csts);
1743
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001744 result = nvme_disable_ctrl(dev, cap);
1745 if (result < 0)
1746 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001747
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001748 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001749 if (!nvmeq) {
Keith Busch2b25d982014-12-22 12:59:04 -07001750 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001751 if (!nvmeq)
1752 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001753 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001754
1755 aqa = nvmeq->q_depth - 1;
1756 aqa |= aqa << 16;
1757
Keith Busch1d090622014-06-23 11:34:01 -06001758 dev->page_size = 1 << page_shift;
1759
Dan McLeran01079522014-06-23 08:24:36 -06001760 dev->ctrl_config = NVME_CC_CSS_NVM;
Keith Busch1d090622014-06-23 11:34:01 -06001761 dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001762 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001763 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001764
1765 writel(aqa, &dev->bar->aqa);
1766 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1767 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001768
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001769 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001770 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001771 goto free_nvmeq;
1772
Keith Busch2b25d982014-12-22 12:59:04 -07001773 nvmeq->cq_vector = 0;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001774 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001775 if (result) {
1776 nvmeq->cq_vector = -1;
Keith Busch0fb59cb2015-01-07 18:55:50 -07001777 goto free_nvmeq;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001778 }
Keith Busch025c5572013-05-01 13:07:51 -06001779
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001780 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001781
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001782 free_nvmeq:
1783 nvme_free_queues(dev, 0);
1784 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001785}
1786
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001787static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1788{
1789 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001790 struct nvme_user_io io;
1791 struct nvme_command c;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001792 unsigned length, meta_len;
Keith Buscha67a9512015-04-07 16:57:19 -06001793 int status, write;
Keith Buscha67a9512015-04-07 16:57:19 -06001794 dma_addr_t meta_dma = 0;
1795 void *meta = NULL;
Arnd Bergmannfec558b2015-05-19 17:05:40 +02001796 void __user *metadata;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001797
1798 if (copy_from_user(&io, uio, sizeof(io)))
1799 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001800
1801 switch (io.opcode) {
1802 case nvme_cmd_write:
1803 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001804 case nvme_cmd_compare:
Matthew Wilcox64132142011-08-09 12:56:37 -04001805 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001806 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001807 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001808 }
1809
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001810 length = (io.nblocks + 1) << ns->lba_shift;
1811 meta_len = (io.nblocks + 1) * ns->ms;
Linus Torvalds6a398a32015-06-25 15:12:50 -07001812 metadata = (void __user *)(unsigned long)io.metadata;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001813 write = io.opcode & 1;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001814
Keith Busch71feb362015-06-19 11:07:30 -06001815 if (ns->ext) {
1816 length += meta_len;
1817 meta_len = 0;
Keith Buscha67a9512015-04-07 16:57:19 -06001818 }
1819 if (meta_len) {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001820 if (((io.metadata & 3) || !io.metadata) && !ns->ext)
1821 return -EINVAL;
1822
Christoph Hellwige75ec752015-05-22 11:12:39 +02001823 meta = dma_alloc_coherent(dev->dev, meta_len,
Keith Buscha67a9512015-04-07 16:57:19 -06001824 &meta_dma, GFP_KERNEL);
Arnd Bergmannfec558b2015-05-19 17:05:40 +02001825
Keith Buscha67a9512015-04-07 16:57:19 -06001826 if (!meta) {
1827 status = -ENOMEM;
1828 goto unmap;
1829 }
1830 if (write) {
Arnd Bergmannfec558b2015-05-19 17:05:40 +02001831 if (copy_from_user(meta, metadata, meta_len)) {
Keith Buscha67a9512015-04-07 16:57:19 -06001832 status = -EFAULT;
1833 goto unmap;
1834 }
1835 }
1836 }
1837
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001838 memset(&c, 0, sizeof(c));
1839 c.rw.opcode = io.opcode;
1840 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001841 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001842 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001843 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001844 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001845 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1846 c.rw.reftag = cpu_to_le32(io.reftag);
1847 c.rw.apptag = cpu_to_le16(io.apptag);
1848 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buscha67a9512015-04-07 16:57:19 -06001849 c.rw.metadata = cpu_to_le64(meta_dma);
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001850
1851 status = __nvme_submit_sync_cmd(ns->queue, &c, NULL,
1852 (void __user *)io.addr, length, NULL, 0);
Keith Buschf410c682013-04-23 17:23:59 -06001853 unmap:
Keith Buscha67a9512015-04-07 16:57:19 -06001854 if (meta) {
1855 if (status == NVME_SC_SUCCESS && !write) {
Arnd Bergmannfec558b2015-05-19 17:05:40 +02001856 if (copy_to_user(metadata, meta, meta_len))
Keith Buscha67a9512015-04-07 16:57:19 -06001857 status = -EFAULT;
1858 }
Christoph Hellwige75ec752015-05-22 11:12:39 +02001859 dma_free_coherent(dev->dev, meta_len, meta, meta_dma);
Keith Buschf410c682013-04-23 17:23:59 -06001860 }
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001861 return status;
1862}
1863
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001864static int nvme_user_cmd(struct nvme_dev *dev, struct nvme_ns *ns,
1865 struct nvme_passthru_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001866{
Keith Busch7963e522014-09-12 16:07:20 -06001867 struct nvme_passthru_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001868 struct nvme_command c;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001869 unsigned timeout = 0;
1870 int status;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001871
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001872 if (!capable(CAP_SYS_ADMIN))
1873 return -EACCES;
1874 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001875 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001876
1877 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001878 c.common.opcode = cmd.opcode;
1879 c.common.flags = cmd.flags;
1880 c.common.nsid = cpu_to_le32(cmd.nsid);
1881 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1882 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1883 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1884 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1885 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1886 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1887 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1888 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1889
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001890 if (cmd.timeout_ms)
1891 timeout = msecs_to_jiffies(cmd.timeout_ms);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001892
Christoph Hellwigf705f832015-05-22 11:12:38 +02001893 status = __nvme_submit_sync_cmd(ns ? ns->queue : dev->admin_q, &c,
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001894 NULL, (void __user *)cmd.addr, cmd.data_len,
1895 &cmd.result, timeout);
1896 if (status >= 0) {
1897 if (put_user(cmd.result, &ucmd->result))
1898 return -EFAULT;
Keith Buschedd10d32014-04-03 16:45:23 -06001899 }
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001900
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001901 return status;
1902}
1903
Jon Derrick81f03fe2015-08-10 15:20:41 -06001904static int nvme_subsys_reset(struct nvme_dev *dev)
1905{
1906 if (!dev->subsystem)
1907 return -ENOTTY;
1908
1909 writel(0x4E564D65, &dev->bar->nssr); /* "NVMe" */
1910 return 0;
1911}
1912
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001913static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1914 unsigned long arg)
1915{
1916 struct nvme_ns *ns = bdev->bd_disk->private_data;
1917
1918 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001919 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001920 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001921 return ns->ns_id;
1922 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001923 return nvme_user_cmd(ns->dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06001924 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001925 return nvme_user_cmd(ns->dev, ns, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001926 case NVME_IOCTL_SUBMIT_IO:
1927 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001928 case SG_GET_VERSION_NUM:
1929 return nvme_sg_get_version_num((void __user *)arg);
1930 case SG_IO:
1931 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001932 default:
1933 return -ENOTTY;
1934 }
1935}
1936
Keith Busch320a3822013-10-23 13:07:34 -06001937#ifdef CONFIG_COMPAT
1938static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1939 unsigned int cmd, unsigned long arg)
1940{
Keith Busch320a3822013-10-23 13:07:34 -06001941 switch (cmd) {
1942 case SG_IO:
Keith Busche1797292014-08-27 13:55:38 -06001943 return -ENOIOCTLCMD;
Keith Busch320a3822013-10-23 13:07:34 -06001944 }
1945 return nvme_ioctl(bdev, mode, cmd, arg);
1946}
1947#else
1948#define nvme_compat_ioctl NULL
1949#endif
1950
Keith Busch9ac27092014-01-31 16:53:39 -07001951static int nvme_open(struct block_device *bdev, fmode_t mode)
1952{
Keith Busch9e603522014-10-03 11:15:47 -06001953 int ret = 0;
1954 struct nvme_ns *ns;
Keith Busch9ac27092014-01-31 16:53:39 -07001955
Keith Busch9e603522014-10-03 11:15:47 -06001956 spin_lock(&dev_list_lock);
1957 ns = bdev->bd_disk->private_data;
1958 if (!ns)
1959 ret = -ENXIO;
1960 else if (!kref_get_unless_zero(&ns->dev->kref))
1961 ret = -ENXIO;
1962 spin_unlock(&dev_list_lock);
1963
1964 return ret;
Keith Busch9ac27092014-01-31 16:53:39 -07001965}
1966
1967static void nvme_free_dev(struct kref *kref);
1968
1969static void nvme_release(struct gendisk *disk, fmode_t mode)
1970{
1971 struct nvme_ns *ns = disk->private_data;
1972 struct nvme_dev *dev = ns->dev;
1973
1974 kref_put(&dev->kref, nvme_free_dev);
1975}
1976
Keith Busch4cc09e22014-04-02 15:45:37 -06001977static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
1978{
1979 /* some standard values */
1980 geo->heads = 1 << 6;
1981 geo->sectors = 1 << 5;
1982 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1983 return 0;
1984}
1985
Keith Busche1e5e562015-02-19 13:39:03 -07001986static void nvme_config_discard(struct nvme_ns *ns)
1987{
1988 u32 logical_block_size = queue_logical_block_size(ns->queue);
1989 ns->queue->limits.discard_zeroes_data = 0;
1990 ns->queue->limits.discard_alignment = logical_block_size;
1991 ns->queue->limits.discard_granularity = logical_block_size;
Jens Axboe2bb4cd52015-07-14 08:15:12 -06001992 blk_queue_max_discard_sectors(ns->queue, 0xffffffff);
Keith Busche1e5e562015-02-19 13:39:03 -07001993 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1994}
1995
Keith Busch1b9dbf72014-09-10 17:21:14 -06001996static int nvme_revalidate_disk(struct gendisk *disk)
1997{
1998 struct nvme_ns *ns = disk->private_data;
1999 struct nvme_dev *dev = ns->dev;
2000 struct nvme_id_ns *id;
Keith Buscha67a9512015-04-07 16:57:19 -06002001 u8 lbaf, pi_type;
2002 u16 old_ms;
Keith Busche1e5e562015-02-19 13:39:03 -07002003 unsigned short bs;
Keith Busch1b9dbf72014-09-10 17:21:14 -06002004
Christoph Hellwigd29ec822015-05-22 11:12:46 +02002005 if (nvme_identify_ns(dev, ns->ns_id, &id)) {
Keith Buscha5768aa2015-06-01 14:28:14 -06002006 dev_warn(dev->dev, "%s: Identify failure nvme%dn%d\n", __func__,
2007 dev->instance, ns->ns_id);
2008 return -ENODEV;
Keith Busch1b9dbf72014-09-10 17:21:14 -06002009 }
Keith Buscha5768aa2015-06-01 14:28:14 -06002010 if (id->ncap == 0) {
2011 kfree(id);
2012 return -ENODEV;
Keith Busche1e5e562015-02-19 13:39:03 -07002013 }
Keith Busch1b9dbf72014-09-10 17:21:14 -06002014
Keith Busche1e5e562015-02-19 13:39:03 -07002015 old_ms = ns->ms;
2016 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
Keith Busch1b9dbf72014-09-10 17:21:14 -06002017 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Busche1e5e562015-02-19 13:39:03 -07002018 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Buscha67a9512015-04-07 16:57:19 -06002019 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
Keith Busch1b9dbf72014-09-10 17:21:14 -06002020
Keith Busche1e5e562015-02-19 13:39:03 -07002021 /*
2022 * If identify namespace failed, use default 512 byte block size so
2023 * block layer can use before failing read/write for 0 capacity.
2024 */
2025 if (ns->lba_shift == 0)
2026 ns->lba_shift = 9;
2027 bs = 1 << ns->lba_shift;
2028
2029 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
2030 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
2031 id->dps & NVME_NS_DPS_PI_MASK : 0;
2032
Keith Busch52b68d72015-02-23 09:16:21 -07002033 if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
2034 ns->ms != old_ms ||
Keith Busche1e5e562015-02-19 13:39:03 -07002035 bs != queue_logical_block_size(disk->queue) ||
Keith Buscha67a9512015-04-07 16:57:19 -06002036 (ns->ms && ns->ext)))
Keith Busche1e5e562015-02-19 13:39:03 -07002037 blk_integrity_unregister(disk);
2038
2039 ns->pi_type = pi_type;
2040 blk_queue_logical_block_size(ns->queue, bs);
2041
Keith Busch52b68d72015-02-23 09:16:21 -07002042 if (ns->ms && !blk_get_integrity(disk) && (disk->flags & GENHD_FL_UP) &&
Keith Buscha67a9512015-04-07 16:57:19 -06002043 !ns->ext)
Keith Busche1e5e562015-02-19 13:39:03 -07002044 nvme_init_integrity(ns);
2045
Keith Buscha5768aa2015-06-01 14:28:14 -06002046 if (ns->ms && !blk_get_integrity(disk))
Keith Busche1e5e562015-02-19 13:39:03 -07002047 set_capacity(disk, 0);
2048 else
2049 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
2050
2051 if (dev->oncs & NVME_CTRL_ONCS_DSM)
2052 nvme_config_discard(ns);
2053
Christoph Hellwigd29ec822015-05-22 11:12:46 +02002054 kfree(id);
Keith Busch1b9dbf72014-09-10 17:21:14 -06002055 return 0;
2056}
2057
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002058static const struct block_device_operations nvme_fops = {
2059 .owner = THIS_MODULE,
2060 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06002061 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07002062 .open = nvme_open,
2063 .release = nvme_release,
Keith Busch4cc09e22014-04-02 15:45:37 -06002064 .getgeo = nvme_getgeo,
Keith Busch1b9dbf72014-09-10 17:21:14 -06002065 .revalidate_disk= nvme_revalidate_disk,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002066};
2067
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002068static int nvme_kthread(void *data)
2069{
Keith Buschd4b4ff82013-12-10 13:10:37 -07002070 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002071
2072 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04002073 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002074 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07002075 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002076 int i;
Keith Buschdfbac8c2015-08-10 15:20:40 -06002077 u32 csts = readl(&dev->bar->csts);
2078
2079 if ((dev->subsystem && (csts & NVME_CSTS_NSSRO)) ||
2080 csts & NVME_CSTS_CFS) {
Keith Buschd4b4ff82013-12-10 13:10:37 -07002081 if (work_busy(&dev->reset_work))
2082 continue;
2083 list_del_init(&dev->node);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002084 dev_warn(dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002085 "Failed status: %x, reset controller\n",
2086 readl(&dev->bar->csts));
Tejun Heo9ca97372014-03-07 10:24:49 -05002087 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschd4b4ff82013-12-10 13:10:37 -07002088 queue_work(nvme_workq, &dev->reset_work);
2089 continue;
2090 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002091 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002092 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05002093 if (!nvmeq)
2094 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002095 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04002096 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06002097
2098 while ((i == 0) && (dev->event_limit > 0)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002099 if (nvme_submit_async_admin_req(dev))
Keith Busch6fccf932014-06-18 13:58:57 -06002100 break;
2101 dev->event_limit--;
2102 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002103 spin_unlock_irq(&nvmeq->q_lock);
2104 }
2105 }
2106 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08002107 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002108 }
2109 return 0;
2110}
2111
Keith Busche1e5e562015-02-19 13:39:03 -07002112static void nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002113{
2114 struct nvme_ns *ns;
2115 struct gendisk *disk;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002116 int node = dev_to_node(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002117
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002118 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002119 if (!ns)
Keith Busche1e5e562015-02-19 13:39:03 -07002120 return;
2121
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002122 ns->queue = blk_mq_init_queue(&dev->tagset);
Dan Carpenter9f173b32014-11-05 23:39:09 +03002123 if (IS_ERR(ns->queue))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002124 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07002125 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
2126 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002127 queue_flag_set_unlocked(QUEUE_FLAG_SG_GAPS, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002128 ns->dev = dev;
2129 ns->queue->queuedata = ns;
2130
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002131 disk = alloc_disk_node(0, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002132 if (!disk)
2133 goto out_free_queue;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002134
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002135 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002136 ns->disk = disk;
Keith Busche1e5e562015-02-19 13:39:03 -07002137 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
2138 list_add_tail(&ns->list, &dev->namespaces);
2139
Keith Busche9ef4632012-07-24 15:01:04 -06002140 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busche8244102015-08-12 16:17:54 -06002141 if (dev->max_hw_sectors) {
Keith Busch8fc23e02012-07-26 11:29:57 -06002142 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Keith Busche8244102015-08-12 16:17:54 -06002143 blk_queue_max_segments(ns->queue,
2144 ((dev->max_hw_sectors << 9) / dev->page_size) + 1);
2145 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002146 if (dev->stripe_size)
2147 blk_queue_chunk_sectors(ns->queue, dev->stripe_size >> 9);
Keith Buscha7d2ce22014-04-29 11:41:28 -06002148 if (dev->vwc & NVME_CTRL_VWC_PRESENT)
2149 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002150
2151 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002152 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002153 disk->fops = &nvme_fops;
2154 disk->private_data = ns;
2155 disk->queue = ns->queue;
Keith Buschb3fffde2015-02-03 11:21:42 -07002156 disk->driverfs_dev = dev->device;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002157 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002158 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002159
Keith Busche1e5e562015-02-19 13:39:03 -07002160 /*
2161 * Initialize capacity to 0 until we establish the namespace format and
2162 * setup integrity extentions if necessary. The revalidate_disk after
2163 * add_disk allows the driver to register with integrity if the format
2164 * requires it.
2165 */
2166 set_capacity(disk, 0);
Keith Buscha5768aa2015-06-01 14:28:14 -06002167 if (nvme_revalidate_disk(ns->disk))
2168 goto out_free_disk;
2169
Keith Busche1e5e562015-02-19 13:39:03 -07002170 add_disk(ns->disk);
Keith Busch7bee6072015-07-14 11:57:48 -06002171 if (ns->ms) {
2172 struct block_device *bd = bdget_disk(ns->disk, 0);
2173 if (!bd)
2174 return;
2175 if (blkdev_get(bd, FMODE_READ, NULL)) {
2176 bdput(bd);
2177 return;
2178 }
2179 blkdev_reread_part(bd);
2180 blkdev_put(bd, FMODE_READ);
2181 }
Keith Busche1e5e562015-02-19 13:39:03 -07002182 return;
Keith Buscha5768aa2015-06-01 14:28:14 -06002183 out_free_disk:
2184 kfree(disk);
2185 list_del(&ns->list);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002186 out_free_queue:
2187 blk_cleanup_queue(ns->queue);
2188 out_free_ns:
2189 kfree(ns);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002190}
2191
Keith Busch42f61422014-03-24 10:46:25 -06002192static void nvme_create_io_queues(struct nvme_dev *dev)
2193{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002194 unsigned i;
Keith Busch42f61422014-03-24 10:46:25 -06002195
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002196 for (i = dev->queue_count; i <= dev->max_qid; i++)
Keith Busch2b25d982014-12-22 12:59:04 -07002197 if (!nvme_alloc_queue(dev, i, dev->q_depth))
Keith Busch42f61422014-03-24 10:46:25 -06002198 break;
2199
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002200 for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
2201 if (nvme_create_queue(dev->queues[i], i))
Keith Busch42f61422014-03-24 10:46:25 -06002202 break;
2203}
2204
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002205static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002206{
2207 int status;
2208 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002209 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002210
Matthew Wilcoxdf348132012-01-11 07:29:56 -07002211 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002212 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04002213 if (status < 0)
2214 return status;
2215 if (status > 0) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002216 dev_err(dev->dev, "Could not set queue count (%d)\n", status);
Keith Buschbadc34d2014-06-23 14:25:35 -06002217 return 0;
Matthew Wilcox27e81662014-04-11 11:58:45 -04002218 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002219 return min(result & 0xffff, result >> 16) + 1;
2220}
2221
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002222static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
2223{
2224 u64 szu, size, offset;
2225 u32 cmbloc;
2226 resource_size_t bar_size;
2227 struct pci_dev *pdev = to_pci_dev(dev->dev);
2228 void __iomem *cmb;
2229 dma_addr_t dma_addr;
2230
2231 if (!use_cmb_sqes)
2232 return NULL;
2233
2234 dev->cmbsz = readl(&dev->bar->cmbsz);
2235 if (!(NVME_CMB_SZ(dev->cmbsz)))
2236 return NULL;
2237
2238 cmbloc = readl(&dev->bar->cmbloc);
2239
2240 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
2241 size = szu * NVME_CMB_SZ(dev->cmbsz);
2242 offset = szu * NVME_CMB_OFST(cmbloc);
2243 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(cmbloc));
2244
2245 if (offset > bar_size)
2246 return NULL;
2247
2248 /*
2249 * Controllers may support a CMB size larger than their BAR,
2250 * for example, due to being behind a bridge. Reduce the CMB to
2251 * the reported size of the BAR
2252 */
2253 if (size > bar_size - offset)
2254 size = bar_size - offset;
2255
2256 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(cmbloc)) + offset;
2257 cmb = ioremap_wc(dma_addr, size);
2258 if (!cmb)
2259 return NULL;
2260
2261 dev->cmb_dma_addr = dma_addr;
2262 dev->cmb_size = size;
2263 return cmb;
2264}
2265
2266static inline void nvme_release_cmb(struct nvme_dev *dev)
2267{
2268 if (dev->cmb) {
2269 iounmap(dev->cmb);
2270 dev->cmb = NULL;
2271 }
2272}
2273
Keith Busch9d713c22013-07-15 15:02:24 -06002274static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
2275{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002276 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06002277}
2278
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002279static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002280{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002281 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02002282 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch42f61422014-03-24 10:46:25 -06002283 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002284
Keith Busch42f61422014-03-24 10:46:25 -06002285 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002286 result = set_queue_count(dev, nr_io_queues);
Keith Buschbadc34d2014-06-23 14:25:35 -06002287 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002288 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002289 if (result < nr_io_queues)
2290 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002291
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002292 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
2293 result = nvme_cmb_qdepth(dev, nr_io_queues,
2294 sizeof(struct nvme_command));
2295 if (result > 0)
2296 dev->q_depth = result;
2297 else
2298 nvme_release_cmb(dev);
2299 }
2300
Keith Busch9d713c22013-07-15 15:02:24 -06002301 size = db_bar_size(dev, nr_io_queues);
2302 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002303 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06002304 do {
2305 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
2306 if (dev->bar)
2307 break;
2308 if (!--nr_io_queues)
2309 return -ENOMEM;
2310 size = db_bar_size(dev, nr_io_queues);
2311 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002312 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07002313 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002314 }
2315
Keith Busch9d713c22013-07-15 15:02:24 -06002316 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002317 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002318
Jens Axboee32efbf2014-11-14 09:49:26 -07002319 /*
2320 * If we enable msix early due to not intx, disable it again before
2321 * setting up the full range we need.
2322 */
2323 if (!pdev->irq)
2324 pci_disable_msix(pdev);
2325
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002326 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002327 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002328 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
2329 if (vecs < 0) {
2330 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
2331 if (vecs < 0) {
2332 vecs = 1;
2333 } else {
2334 for (i = 0; i < vecs; i++)
2335 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002336 }
2337 }
2338
Matthew Wilcox063a8092013-06-20 10:53:48 -04002339 /*
2340 * Should investigate if there's a performance win from allocating
2341 * more queues than interrupt vectors; it might allow the submission
2342 * path to scale better, even if the receive path is limited by the
2343 * number of interrupts.
2344 */
2345 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002346 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002347
Matthew Wilcox3193f072014-01-27 15:57:22 -05002348 result = queue_request_irq(dev, adminq, adminq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06002349 if (result) {
2350 adminq->cq_vector = -1;
Keith Busch22404272013-07-15 15:02:20 -06002351 goto free_queues;
Jon Derrick758dd7f2015-06-30 11:22:52 -06002352 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05002353
Keith Buschcd638942013-07-15 15:02:23 -06002354 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002355 nvme_free_queues(dev, nr_io_queues + 1);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002356 nvme_create_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06002357
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002358 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002359
Keith Busch22404272013-07-15 15:02:20 -06002360 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002361 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002362 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002363}
2364
Keith Buscha5768aa2015-06-01 14:28:14 -06002365static void nvme_free_namespace(struct nvme_ns *ns)
2366{
2367 list_del(&ns->list);
2368
2369 spin_lock(&dev_list_lock);
2370 ns->disk->private_data = NULL;
2371 spin_unlock(&dev_list_lock);
2372
2373 put_disk(ns->disk);
2374 kfree(ns);
2375}
2376
2377static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2378{
2379 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2380 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2381
2382 return nsa->ns_id - nsb->ns_id;
2383}
2384
2385static struct nvme_ns *nvme_find_ns(struct nvme_dev *dev, unsigned nsid)
2386{
2387 struct nvme_ns *ns;
2388
2389 list_for_each_entry(ns, &dev->namespaces, list) {
2390 if (ns->ns_id == nsid)
2391 return ns;
2392 if (ns->ns_id > nsid)
2393 break;
2394 }
2395 return NULL;
2396}
2397
2398static inline bool nvme_io_incapable(struct nvme_dev *dev)
2399{
2400 return (!dev->bar || readl(&dev->bar->csts) & NVME_CSTS_CFS ||
2401 dev->online_queues < 2);
2402}
2403
2404static void nvme_ns_remove(struct nvme_ns *ns)
2405{
2406 bool kill = nvme_io_incapable(ns->dev) && !blk_queue_dying(ns->queue);
2407
2408 if (kill)
2409 blk_set_queue_dying(ns->queue);
2410 if (ns->disk->flags & GENHD_FL_UP) {
2411 if (blk_get_integrity(ns->disk))
2412 blk_integrity_unregister(ns->disk);
2413 del_gendisk(ns->disk);
2414 }
2415 if (kill || !blk_queue_dying(ns->queue)) {
2416 blk_mq_abort_requeue_list(ns->queue);
2417 blk_cleanup_queue(ns->queue);
2418 }
2419}
2420
2421static void nvme_scan_namespaces(struct nvme_dev *dev, unsigned nn)
2422{
2423 struct nvme_ns *ns, *next;
2424 unsigned i;
2425
2426 for (i = 1; i <= nn; i++) {
2427 ns = nvme_find_ns(dev, i);
2428 if (ns) {
2429 if (revalidate_disk(ns->disk)) {
2430 nvme_ns_remove(ns);
2431 nvme_free_namespace(ns);
2432 }
2433 } else
2434 nvme_alloc_ns(dev, i);
2435 }
2436 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2437 if (ns->ns_id > nn) {
2438 nvme_ns_remove(ns);
2439 nvme_free_namespace(ns);
2440 }
2441 }
2442 list_sort(NULL, &dev->namespaces, ns_cmp);
2443}
2444
2445static void nvme_dev_scan(struct work_struct *work)
2446{
2447 struct nvme_dev *dev = container_of(work, struct nvme_dev, scan_work);
2448 struct nvme_id_ctrl *ctrl;
2449
2450 if (!dev->tagset.tags)
2451 return;
2452 if (nvme_identify_ctrl(dev, &ctrl))
2453 return;
2454 nvme_scan_namespaces(dev, le32_to_cpup(&ctrl->nn));
2455 kfree(ctrl);
2456}
2457
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002458/*
2459 * Return: error value if an error occurred setting up the queues or calling
2460 * Identify Device. 0 if these succeeded, even if adding some of the
2461 * namespaces failed. At the moment, these failures are silent. TBD which
2462 * failures should be reported.
2463 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002464static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002465{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002466 struct pci_dev *pdev = to_pci_dev(dev->dev);
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002467 int res;
Matthew Wilcox51814232011-02-01 16:18:08 -05002468 struct nvme_id_ctrl *ctrl;
Keith Busch159b67d2013-04-09 17:13:20 -06002469 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002470
Christoph Hellwigd29ec822015-05-22 11:12:46 +02002471 res = nvme_identify_ctrl(dev, &ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002472 if (res) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002473 dev_err(dev->dev, "Identify Controller failed (%d)\n", res);
Keith Busche1e5e562015-02-19 13:39:03 -07002474 return -EIO;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002475 }
2476
Keith Busch0e5e4f02012-11-09 16:33:05 -07002477 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002478 dev->abort_limit = ctrl->acl + 1;
Keith Buscha7d2ce22014-04-29 11:41:28 -06002479 dev->vwc = ctrl->vwc;
Matthew Wilcox51814232011-02-01 16:18:08 -05002480 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2481 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2482 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002483 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002484 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002485 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002486 (pdev->device == 0x0953) && ctrl->vs[3]) {
2487 unsigned int max_hw_sectors;
2488
Keith Busch159b67d2013-04-09 17:13:20 -06002489 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002490 max_hw_sectors = dev->stripe_size >> (shift - 9);
2491 if (dev->max_hw_sectors) {
2492 dev->max_hw_sectors = min(max_hw_sectors,
2493 dev->max_hw_sectors);
2494 } else
2495 dev->max_hw_sectors = max_hw_sectors;
2496 }
Christoph Hellwigd29ec822015-05-22 11:12:46 +02002497 kfree(ctrl);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002498
Keith Buschffe77042015-06-08 10:08:15 -06002499 if (!dev->tagset.tags) {
2500 dev->tagset.ops = &nvme_mq_ops;
2501 dev->tagset.nr_hw_queues = dev->online_queues - 1;
2502 dev->tagset.timeout = NVME_IO_TIMEOUT;
2503 dev->tagset.numa_node = dev_to_node(dev->dev);
2504 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002505 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Keith Buschffe77042015-06-08 10:08:15 -06002506 dev->tagset.cmd_size = nvme_cmd_size(dev);
2507 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
2508 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002509
Keith Buschffe77042015-06-08 10:08:15 -06002510 if (blk_mq_alloc_tag_set(&dev->tagset))
2511 return 0;
2512 }
Keith Buscha5768aa2015-06-01 14:28:14 -06002513 schedule_work(&dev->scan_work);
Keith Busche1e5e562015-02-19 13:39:03 -07002514 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002515}
2516
Keith Busch0877cb02013-07-15 15:02:19 -06002517static int nvme_dev_map(struct nvme_dev *dev)
2518{
Keith Busch42f61422014-03-24 10:46:25 -06002519 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002520 int bars, result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002521 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002522
2523 if (pci_enable_device_mem(pdev))
2524 return result;
2525
2526 dev->entry[0].vector = pdev->irq;
2527 pci_set_master(pdev);
2528 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07002529 if (!bars)
2530 goto disable_pci;
2531
Keith Busch0877cb02013-07-15 15:02:19 -06002532 if (pci_request_selected_regions(pdev, bars, "nvme"))
2533 goto disable_pci;
2534
Christoph Hellwige75ec752015-05-22 11:12:39 +02002535 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
2536 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01002537 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002538
Keith Busch0877cb02013-07-15 15:02:19 -06002539 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2540 if (!dev->bar)
2541 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07002542
Keith Busch0e53d182013-12-10 13:10:39 -07002543 if (readl(&dev->bar->csts) == -1) {
2544 result = -ENODEV;
2545 goto unmap;
2546 }
Jens Axboee32efbf2014-11-14 09:49:26 -07002547
2548 /*
2549 * Some devices don't advertse INTx interrupts, pre-enable a single
2550 * MSIX vec for setup. We'll adjust this later.
2551 */
2552 if (!pdev->irq) {
2553 result = pci_enable_msix(pdev, dev->entry, 1);
2554 if (result < 0)
2555 goto unmap;
2556 }
2557
Keith Busch42f61422014-03-24 10:46:25 -06002558 cap = readq(&dev->bar->cap);
2559 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2560 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Keith Busch0877cb02013-07-15 15:02:19 -06002561 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002562 if (readl(&dev->bar->vs) >= NVME_VS(1, 2))
2563 dev->cmb = nvme_map_cmb(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002564
2565 return 0;
2566
Keith Busch0e53d182013-12-10 13:10:39 -07002567 unmap:
2568 iounmap(dev->bar);
2569 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002570 disable:
2571 pci_release_regions(pdev);
2572 disable_pci:
2573 pci_disable_device(pdev);
2574 return result;
2575}
2576
2577static void nvme_dev_unmap(struct nvme_dev *dev)
2578{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002579 struct pci_dev *pdev = to_pci_dev(dev->dev);
2580
2581 if (pdev->msi_enabled)
2582 pci_disable_msi(pdev);
2583 else if (pdev->msix_enabled)
2584 pci_disable_msix(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002585
2586 if (dev->bar) {
2587 iounmap(dev->bar);
2588 dev->bar = NULL;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002589 pci_release_regions(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002590 }
2591
Christoph Hellwige75ec752015-05-22 11:12:39 +02002592 if (pci_is_enabled(pdev))
2593 pci_disable_device(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002594}
2595
Keith Busch4d115422013-12-10 13:10:40 -07002596struct nvme_delq_ctx {
2597 struct task_struct *waiter;
2598 struct kthread_worker *worker;
2599 atomic_t refcount;
2600};
2601
2602static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2603{
2604 dq->waiter = current;
2605 mb();
2606
2607 for (;;) {
2608 set_current_state(TASK_KILLABLE);
2609 if (!atomic_read(&dq->refcount))
2610 break;
2611 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2612 fatal_signal_pending(current)) {
Keith Busch0fb59cb2015-01-07 18:55:50 -07002613 /*
2614 * Disable the controller first since we can't trust it
2615 * at this point, but leave the admin queue enabled
2616 * until all queue deletion requests are flushed.
2617 * FIXME: This may take a while if there are more h/w
2618 * queues than admin tags.
2619 */
Keith Busch4d115422013-12-10 13:10:40 -07002620 set_current_state(TASK_RUNNING);
Keith Busch4d115422013-12-10 13:10:40 -07002621 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
Keith Busch0fb59cb2015-01-07 18:55:50 -07002622 nvme_clear_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07002623 flush_kthread_worker(dq->worker);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002624 nvme_disable_queue(dev, 0);
Keith Busch4d115422013-12-10 13:10:40 -07002625 return;
2626 }
2627 }
2628 set_current_state(TASK_RUNNING);
2629}
2630
2631static void nvme_put_dq(struct nvme_delq_ctx *dq)
2632{
2633 atomic_dec(&dq->refcount);
2634 if (dq->waiter)
2635 wake_up_process(dq->waiter);
2636}
2637
2638static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2639{
2640 atomic_inc(&dq->refcount);
2641 return dq;
2642}
2643
2644static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2645{
2646 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
Keith Busch4d115422013-12-10 13:10:40 -07002647 nvme_put_dq(dq);
2648}
2649
2650static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2651 kthread_work_func_t fn)
2652{
2653 struct nvme_command c;
2654
2655 memset(&c, 0, sizeof(c));
2656 c.delete_queue.opcode = opcode;
2657 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2658
2659 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002660 return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
2661 ADMIN_TIMEOUT);
Keith Busch4d115422013-12-10 13:10:40 -07002662}
2663
2664static void nvme_del_cq_work_handler(struct kthread_work *work)
2665{
2666 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2667 cmdinfo.work);
2668 nvme_del_queue_end(nvmeq);
2669}
2670
2671static int nvme_delete_cq(struct nvme_queue *nvmeq)
2672{
2673 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2674 nvme_del_cq_work_handler);
2675}
2676
2677static void nvme_del_sq_work_handler(struct kthread_work *work)
2678{
2679 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2680 cmdinfo.work);
2681 int status = nvmeq->cmdinfo.status;
2682
2683 if (!status)
2684 status = nvme_delete_cq(nvmeq);
2685 if (status)
2686 nvme_del_queue_end(nvmeq);
2687}
2688
2689static int nvme_delete_sq(struct nvme_queue *nvmeq)
2690{
2691 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2692 nvme_del_sq_work_handler);
2693}
2694
2695static void nvme_del_queue_start(struct kthread_work *work)
2696{
2697 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2698 cmdinfo.work);
Keith Busch4d115422013-12-10 13:10:40 -07002699 if (nvme_delete_sq(nvmeq))
2700 nvme_del_queue_end(nvmeq);
2701}
2702
2703static void nvme_disable_io_queues(struct nvme_dev *dev)
2704{
2705 int i;
2706 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2707 struct nvme_delq_ctx dq;
2708 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2709 &worker, "nvme%d", dev->instance);
2710
2711 if (IS_ERR(kworker_task)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002712 dev_err(dev->dev,
Keith Busch4d115422013-12-10 13:10:40 -07002713 "Failed to create queue del task\n");
2714 for (i = dev->queue_count - 1; i > 0; i--)
2715 nvme_disable_queue(dev, i);
2716 return;
2717 }
2718
2719 dq.waiter = NULL;
2720 atomic_set(&dq.refcount, 0);
2721 dq.worker = &worker;
2722 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002723 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002724
2725 if (nvme_suspend_queue(nvmeq))
2726 continue;
2727 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2728 nvmeq->cmdinfo.worker = dq.worker;
2729 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2730 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2731 }
2732 nvme_wait_dq(&dq, dev);
2733 kthread_stop(kworker_task);
2734}
2735
Dan McLeranb9afca32014-04-07 17:10:11 -06002736/*
2737* Remove the node from the device list and check
2738* for whether or not we need to stop the nvme_thread.
2739*/
2740static void nvme_dev_list_remove(struct nvme_dev *dev)
2741{
2742 struct task_struct *tmp = NULL;
2743
2744 spin_lock(&dev_list_lock);
2745 list_del_init(&dev->node);
2746 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2747 tmp = nvme_thread;
2748 nvme_thread = NULL;
2749 }
2750 spin_unlock(&dev_list_lock);
2751
2752 if (tmp)
2753 kthread_stop(tmp);
2754}
2755
Keith Buschc9d3bf82015-01-07 18:55:52 -07002756static void nvme_freeze_queues(struct nvme_dev *dev)
2757{
2758 struct nvme_ns *ns;
2759
2760 list_for_each_entry(ns, &dev->namespaces, list) {
2761 blk_mq_freeze_queue_start(ns->queue);
2762
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002763 spin_lock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002764 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002765 spin_unlock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002766
2767 blk_mq_cancel_requeue_work(ns->queue);
2768 blk_mq_stop_hw_queues(ns->queue);
2769 }
2770}
2771
2772static void nvme_unfreeze_queues(struct nvme_dev *dev)
2773{
2774 struct nvme_ns *ns;
2775
2776 list_for_each_entry(ns, &dev->namespaces, list) {
2777 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2778 blk_mq_unfreeze_queue(ns->queue);
2779 blk_mq_start_stopped_hw_queues(ns->queue, true);
2780 blk_mq_kick_requeue_list(ns->queue);
2781 }
2782}
2783
Keith Buschf0b50732013-07-15 15:02:21 -06002784static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002785{
Keith Busch22404272013-07-15 15:02:20 -06002786 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06002787 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06002788
Dan McLeranb9afca32014-04-07 17:10:11 -06002789 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002790
Keith Buschc9d3bf82015-01-07 18:55:52 -07002791 if (dev->bar) {
2792 nvme_freeze_queues(dev);
Keith Busch7c1b2452014-06-25 11:18:12 -06002793 csts = readl(&dev->bar->csts);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002794 }
Keith Busch7c1b2452014-06-25 11:18:12 -06002795 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07002796 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002797 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002798 nvme_suspend_queue(nvmeq);
Keith Busch4d115422013-12-10 13:10:40 -07002799 }
2800 } else {
2801 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002802 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002803 nvme_disable_queue(dev, 0);
2804 }
Keith Buschf0b50732013-07-15 15:02:21 -06002805 nvme_dev_unmap(dev);
Keith Busch07836e62015-02-19 10:34:48 -07002806
2807 for (i = dev->queue_count - 1; i >= 0; i--)
2808 nvme_clear_queue(dev->queues[i]);
Keith Buschf0b50732013-07-15 15:02:21 -06002809}
2810
2811static void nvme_dev_remove(struct nvme_dev *dev)
2812{
Keith Busch9ac27092014-01-31 16:53:39 -07002813 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002814
Keith Buscha5768aa2015-06-01 14:28:14 -06002815 list_for_each_entry(ns, &dev->namespaces, list)
2816 nvme_ns_remove(ns);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002817}
2818
Matthew Wilcox091b6092011-02-10 09:56:01 -05002819static int nvme_setup_prp_pools(struct nvme_dev *dev)
2820{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002821 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05002822 PAGE_SIZE, PAGE_SIZE, 0);
2823 if (!dev->prp_page_pool)
2824 return -ENOMEM;
2825
Matthew Wilcox99802a72011-02-10 10:30:34 -05002826 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02002827 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05002828 256, 256, 0);
2829 if (!dev->prp_small_pool) {
2830 dma_pool_destroy(dev->prp_page_pool);
2831 return -ENOMEM;
2832 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002833 return 0;
2834}
2835
2836static void nvme_release_prp_pools(struct nvme_dev *dev)
2837{
2838 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002839 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002840}
2841
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002842static DEFINE_IDA(nvme_instance_ida);
2843
2844static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002845{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002846 int instance, error;
2847
2848 do {
2849 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2850 return -ENODEV;
2851
2852 spin_lock(&dev_list_lock);
2853 error = ida_get_new(&nvme_instance_ida, &instance);
2854 spin_unlock(&dev_list_lock);
2855 } while (error == -EAGAIN);
2856
2857 if (error)
2858 return -ENODEV;
2859
2860 dev->instance = instance;
2861 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002862}
2863
2864static void nvme_release_instance(struct nvme_dev *dev)
2865{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002866 spin_lock(&dev_list_lock);
2867 ida_remove(&nvme_instance_ida, dev->instance);
2868 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002869}
2870
Keith Busch9ac27092014-01-31 16:53:39 -07002871static void nvme_free_namespaces(struct nvme_dev *dev)
2872{
2873 struct nvme_ns *ns, *next;
2874
Keith Buscha5768aa2015-06-01 14:28:14 -06002875 list_for_each_entry_safe(ns, next, &dev->namespaces, list)
2876 nvme_free_namespace(ns);
Keith Busch9ac27092014-01-31 16:53:39 -07002877}
2878
Keith Busch5e82e952013-02-19 10:17:58 -07002879static void nvme_free_dev(struct kref *kref)
2880{
2881 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002882
Christoph Hellwige75ec752015-05-22 11:12:39 +02002883 put_device(dev->dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07002884 put_device(dev->device);
Keith Busch9ac27092014-01-31 16:53:39 -07002885 nvme_free_namespaces(dev);
Indraneel M285dffc2014-12-11 08:24:18 -07002886 nvme_release_instance(dev);
Keith Busch4af0e212015-06-08 10:08:13 -06002887 if (dev->tagset.tags)
2888 blk_mq_free_tag_set(&dev->tagset);
2889 if (dev->admin_q)
2890 blk_put_queue(dev->admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002891 kfree(dev->queues);
2892 kfree(dev->entry);
2893 kfree(dev);
2894}
2895
2896static int nvme_dev_open(struct inode *inode, struct file *f)
2897{
Keith Buschb3fffde2015-02-03 11:21:42 -07002898 struct nvme_dev *dev;
2899 int instance = iminor(inode);
2900 int ret = -ENODEV;
2901
2902 spin_lock(&dev_list_lock);
2903 list_for_each_entry(dev, &dev_list, node) {
2904 if (dev->instance == instance) {
Keith Busch2e1d8442015-02-12 15:33:00 -07002905 if (!dev->admin_q) {
2906 ret = -EWOULDBLOCK;
2907 break;
2908 }
Keith Buschb3fffde2015-02-03 11:21:42 -07002909 if (!kref_get_unless_zero(&dev->kref))
2910 break;
2911 f->private_data = dev;
2912 ret = 0;
2913 break;
2914 }
2915 }
2916 spin_unlock(&dev_list_lock);
2917
2918 return ret;
Keith Busch5e82e952013-02-19 10:17:58 -07002919}
2920
2921static int nvme_dev_release(struct inode *inode, struct file *f)
2922{
2923 struct nvme_dev *dev = f->private_data;
2924 kref_put(&dev->kref, nvme_free_dev);
2925 return 0;
2926}
2927
2928static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2929{
2930 struct nvme_dev *dev = f->private_data;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002931 struct nvme_ns *ns;
2932
Keith Busch5e82e952013-02-19 10:17:58 -07002933 switch (cmd) {
2934 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002935 return nvme_user_cmd(dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06002936 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002937 if (list_empty(&dev->namespaces))
2938 return -ENOTTY;
2939 ns = list_first_entry(&dev->namespaces, struct nvme_ns, list);
2940 return nvme_user_cmd(dev, ns, (void __user *)arg);
Keith Busch4cc06522015-06-05 10:30:08 -06002941 case NVME_IOCTL_RESET:
2942 dev_warn(dev->dev, "resetting controller\n");
2943 return nvme_reset(dev);
Jon Derrick81f03fe2015-08-10 15:20:41 -06002944 case NVME_IOCTL_SUBSYS_RESET:
2945 return nvme_subsys_reset(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002946 default:
2947 return -ENOTTY;
2948 }
2949}
2950
2951static const struct file_operations nvme_dev_fops = {
2952 .owner = THIS_MODULE,
2953 .open = nvme_dev_open,
2954 .release = nvme_dev_release,
2955 .unlocked_ioctl = nvme_dev_ioctl,
2956 .compat_ioctl = nvme_dev_ioctl,
2957};
2958
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002959static void nvme_set_irq_hints(struct nvme_dev *dev)
2960{
2961 struct nvme_queue *nvmeq;
2962 int i;
2963
2964 for (i = 0; i < dev->online_queues; i++) {
2965 nvmeq = dev->queues[i];
2966
Keith Busch42483222015-06-01 09:29:54 -06002967 if (!nvmeq->tags || !(*nvmeq->tags))
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002968 continue;
2969
2970 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
Keith Busch42483222015-06-01 09:29:54 -06002971 blk_mq_tags_cpumask(*nvmeq->tags));
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002972 }
2973}
2974
Keith Buschf0b50732013-07-15 15:02:21 -06002975static int nvme_dev_start(struct nvme_dev *dev)
2976{
2977 int result;
Dan McLeranb9afca32014-04-07 17:10:11 -06002978 bool start_thread = false;
Keith Buschf0b50732013-07-15 15:02:21 -06002979
2980 result = nvme_dev_map(dev);
2981 if (result)
2982 return result;
2983
2984 result = nvme_configure_admin_queue(dev);
2985 if (result)
2986 goto unmap;
2987
2988 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002989 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2990 start_thread = true;
2991 nvme_thread = NULL;
2992 }
Keith Buschf0b50732013-07-15 15:02:21 -06002993 list_add(&dev->node, &dev_list);
2994 spin_unlock(&dev_list_lock);
2995
Dan McLeranb9afca32014-04-07 17:10:11 -06002996 if (start_thread) {
2997 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
Keith Busch387caa52014-09-22 13:46:19 -06002998 wake_up_all(&nvme_kthread_wait);
Dan McLeranb9afca32014-04-07 17:10:11 -06002999 } else
3000 wait_event_killable(nvme_kthread_wait, nvme_thread);
3001
3002 if (IS_ERR_OR_NULL(nvme_thread)) {
3003 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
3004 goto disable;
3005 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003006
3007 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07003008 result = nvme_alloc_admin_tags(dev);
3009 if (result)
3010 goto disable;
Dan McLeranb9afca32014-04-07 17:10:11 -06003011
Keith Buschf0b50732013-07-15 15:02:21 -06003012 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06003013 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07003014 goto free_tags;
Keith Buschf0b50732013-07-15 15:02:21 -06003015
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003016 nvme_set_irq_hints(dev);
3017
Keith Busch1efccc92015-03-31 10:37:17 -06003018 dev->event_limit = 1;
Keith Buschd82e8bf2013-09-05 14:45:07 -06003019 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06003020
Keith Busch0fb59cb2015-01-07 18:55:50 -07003021 free_tags:
3022 nvme_dev_remove_admin(dev);
Keith Busch4af0e212015-06-08 10:08:13 -06003023 blk_put_queue(dev->admin_q);
3024 dev->admin_q = NULL;
3025 dev->queues[0]->tags = NULL;
Keith Buschf0b50732013-07-15 15:02:21 -06003026 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05003027 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06003028 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06003029 unmap:
3030 nvme_dev_unmap(dev);
3031 return result;
3032}
3033
Keith Busch9a6b9452013-12-10 13:10:36 -07003034static int nvme_remove_dead_ctrl(void *arg)
3035{
3036 struct nvme_dev *dev = (struct nvme_dev *)arg;
Christoph Hellwige75ec752015-05-22 11:12:39 +02003037 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003038
3039 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06003040 pci_stop_and_remove_bus_device_locked(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003041 kref_put(&dev->kref, nvme_free_dev);
3042 return 0;
3043}
3044
3045static void nvme_remove_disks(struct work_struct *ws)
3046{
Keith Busch9a6b9452013-12-10 13:10:36 -07003047 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
3048
Keith Busch5a92e702014-02-21 14:13:44 -07003049 nvme_free_queues(dev, 1);
Keith Busch302c6722014-07-18 11:40:20 -06003050 nvme_dev_remove(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003051}
3052
3053static int nvme_dev_resume(struct nvme_dev *dev)
3054{
3055 int ret;
3056
3057 ret = nvme_dev_start(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06003058 if (ret)
Keith Busch9a6b9452013-12-10 13:10:36 -07003059 return ret;
Keith Buschbadc34d2014-06-23 14:25:35 -06003060 if (dev->online_queues < 2) {
Keith Busch9a6b9452013-12-10 13:10:36 -07003061 spin_lock(&dev_list_lock);
Tejun Heo9ca97372014-03-07 10:24:49 -05003062 dev->reset_workfn = nvme_remove_disks;
Keith Busch9a6b9452013-12-10 13:10:36 -07003063 queue_work(nvme_workq, &dev->reset_work);
3064 spin_unlock(&dev_list_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07003065 } else {
3066 nvme_unfreeze_queues(dev);
Keith Buschffe77042015-06-08 10:08:15 -06003067 nvme_dev_add(dev);
Keith Buschc9d3bf82015-01-07 18:55:52 -07003068 nvme_set_irq_hints(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003069 }
3070 return 0;
3071}
3072
Keith Buschde3eff22015-06-18 13:36:39 -06003073static void nvme_dead_ctrl(struct nvme_dev *dev)
3074{
3075 dev_warn(dev->dev, "Device failed to resume\n");
3076 kref_get(&dev->kref);
3077 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
3078 dev->instance))) {
3079 dev_err(dev->dev,
3080 "Failed to start controller remove task\n");
3081 kref_put(&dev->kref, nvme_free_dev);
3082 }
3083}
3084
Keith Busch9a6b9452013-12-10 13:10:36 -07003085static void nvme_dev_reset(struct nvme_dev *dev)
3086{
Keith Buschffe77042015-06-08 10:08:15 -06003087 bool in_probe = work_busy(&dev->probe_work);
3088
Keith Busch9a6b9452013-12-10 13:10:36 -07003089 nvme_dev_shutdown(dev);
Keith Buschffe77042015-06-08 10:08:15 -06003090
3091 /* Synchronize with device probe so that work will see failure status
3092 * and exit gracefully without trying to schedule another reset */
3093 flush_work(&dev->probe_work);
3094
3095 /* Fail this device if reset occured during probe to avoid
3096 * infinite initialization loops. */
3097 if (in_probe) {
Keith Buschde3eff22015-06-18 13:36:39 -06003098 nvme_dead_ctrl(dev);
Keith Buschffe77042015-06-08 10:08:15 -06003099 return;
Keith Busch9a6b9452013-12-10 13:10:36 -07003100 }
Keith Buschffe77042015-06-08 10:08:15 -06003101 /* Schedule device resume asynchronously so the reset work is available
3102 * to cleanup errors that may occur during reinitialization */
3103 schedule_work(&dev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07003104}
3105
3106static void nvme_reset_failed_dev(struct work_struct *ws)
3107{
3108 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
3109 nvme_dev_reset(dev);
3110}
3111
Tejun Heo9ca97372014-03-07 10:24:49 -05003112static void nvme_reset_workfn(struct work_struct *work)
3113{
3114 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
3115 dev->reset_workfn(work);
3116}
3117
Keith Busch4cc06522015-06-05 10:30:08 -06003118static int nvme_reset(struct nvme_dev *dev)
3119{
3120 int ret = -EBUSY;
3121
3122 if (!dev->admin_q || blk_queue_dying(dev->admin_q))
3123 return -ENODEV;
3124
3125 spin_lock(&dev_list_lock);
3126 if (!work_pending(&dev->reset_work)) {
3127 dev->reset_workfn = nvme_reset_failed_dev;
3128 queue_work(nvme_workq, &dev->reset_work);
3129 ret = 0;
3130 }
3131 spin_unlock(&dev_list_lock);
3132
3133 if (!ret) {
3134 flush_work(&dev->reset_work);
Keith Buschffe77042015-06-08 10:08:15 -06003135 flush_work(&dev->probe_work);
Keith Busch4cc06522015-06-05 10:30:08 -06003136 return 0;
3137 }
3138
3139 return ret;
3140}
3141
3142static ssize_t nvme_sysfs_reset(struct device *dev,
3143 struct device_attribute *attr, const char *buf,
3144 size_t count)
3145{
3146 struct nvme_dev *ndev = dev_get_drvdata(dev);
3147 int ret;
3148
3149 ret = nvme_reset(ndev);
3150 if (ret < 0)
3151 return ret;
3152
3153 return count;
3154}
3155static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
3156
Keith Busch2e1d8442015-02-12 15:33:00 -07003157static void nvme_async_probe(struct work_struct *work);
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003158static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003159{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003160 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003161 struct nvme_dev *dev;
3162
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003163 node = dev_to_node(&pdev->dev);
3164 if (node == NUMA_NO_NODE)
3165 set_dev_node(&pdev->dev, 0);
3166
3167 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003168 if (!dev)
3169 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003170 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
3171 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003172 if (!dev->entry)
3173 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003174 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
3175 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003176 if (!dev->queues)
3177 goto free;
3178
3179 INIT_LIST_HEAD(&dev->namespaces);
Tejun Heo9ca97372014-03-07 10:24:49 -05003180 dev->reset_workfn = nvme_reset_failed_dev;
3181 INIT_WORK(&dev->reset_work, nvme_reset_workfn);
Christoph Hellwige75ec752015-05-22 11:12:39 +02003182 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003183 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07003184 result = nvme_set_instance(dev);
3185 if (result)
Keith Buscha96d4f52014-08-19 19:15:59 -06003186 goto put_pci;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003187
Matthew Wilcox091b6092011-02-10 09:56:01 -05003188 result = nvme_setup_prp_pools(dev);
3189 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06003190 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05003191
Keith Buschfb35e912014-03-03 11:09:47 -07003192 kref_init(&dev->kref);
Keith Buschb3fffde2015-02-03 11:21:42 -07003193 dev->device = device_create(nvme_class, &pdev->dev,
3194 MKDEV(nvme_char_major, dev->instance),
3195 dev, "nvme%d", dev->instance);
3196 if (IS_ERR(dev->device)) {
3197 result = PTR_ERR(dev->device);
Keith Busch2e1d8442015-02-12 15:33:00 -07003198 goto release_pools;
Keith Buschb3fffde2015-02-03 11:21:42 -07003199 }
3200 get_device(dev->device);
Keith Busch4cc06522015-06-05 10:30:08 -06003201 dev_set_drvdata(dev->device, dev);
3202
3203 result = device_create_file(dev->device, &dev_attr_reset_controller);
3204 if (result)
3205 goto put_dev;
Keith Buschb3fffde2015-02-03 11:21:42 -07003206
Keith Busche6e96d72015-03-23 09:32:37 -06003207 INIT_LIST_HEAD(&dev->node);
Keith Buscha5768aa2015-06-01 14:28:14 -06003208 INIT_WORK(&dev->scan_work, nvme_dev_scan);
Keith Busch2e1d8442015-02-12 15:33:00 -07003209 INIT_WORK(&dev->probe_work, nvme_async_probe);
3210 schedule_work(&dev->probe_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003211 return 0;
3212
Keith Busch4cc06522015-06-05 10:30:08 -06003213 put_dev:
3214 device_destroy(nvme_class, MKDEV(nvme_char_major, dev->instance));
3215 put_device(dev->device);
Keith Busch0877cb02013-07-15 15:02:19 -06003216 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05003217 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06003218 release:
3219 nvme_release_instance(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06003220 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02003221 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003222 free:
3223 kfree(dev->queues);
3224 kfree(dev->entry);
3225 kfree(dev);
3226 return result;
3227}
3228
Keith Busch2e1d8442015-02-12 15:33:00 -07003229static void nvme_async_probe(struct work_struct *work)
3230{
3231 struct nvme_dev *dev = container_of(work, struct nvme_dev, probe_work);
Keith Busch2e1d8442015-02-12 15:33:00 -07003232
Keith Buschde3eff22015-06-18 13:36:39 -06003233 if (nvme_dev_resume(dev) && !work_busy(&dev->reset_work))
3234 nvme_dead_ctrl(dev);
Keith Busch2e1d8442015-02-12 15:33:00 -07003235}
3236
Keith Buschf0d54a52014-05-02 10:40:43 -06003237static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
3238{
Keith Buscha6739472014-06-23 16:03:21 -06003239 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06003240
Keith Buscha6739472014-06-23 16:03:21 -06003241 if (prepare)
3242 nvme_dev_shutdown(dev);
3243 else
3244 nvme_dev_resume(dev);
Keith Buschf0d54a52014-05-02 10:40:43 -06003245}
3246
Keith Busch09ece142014-01-27 11:29:40 -05003247static void nvme_shutdown(struct pci_dev *pdev)
3248{
3249 struct nvme_dev *dev = pci_get_drvdata(pdev);
3250 nvme_dev_shutdown(dev);
3251}
3252
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003253static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003254{
3255 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003256
3257 spin_lock(&dev_list_lock);
3258 list_del_init(&dev->node);
3259 spin_unlock(&dev_list_lock);
3260
3261 pci_set_drvdata(pdev, NULL);
Keith Busch2e1d8442015-02-12 15:33:00 -07003262 flush_work(&dev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07003263 flush_work(&dev->reset_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06003264 flush_work(&dev->scan_work);
Keith Busch4cc06522015-06-05 10:30:08 -06003265 device_remove_file(dev->device, &dev_attr_reset_controller);
Keith Buschc9d3bf82015-01-07 18:55:52 -07003266 nvme_dev_remove(dev);
Keith Busch3399a3f2015-06-18 13:36:40 -06003267 nvme_dev_shutdown(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003268 nvme_dev_remove_admin(dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07003269 device_destroy(nvme_class, MKDEV(nvme_char_major, dev->instance));
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003270 nvme_free_queues(dev, 0);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06003271 nvme_release_cmb(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003272 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07003273 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003274}
3275
3276/* These functions are yet to be implemented */
3277#define nvme_error_detected NULL
3278#define nvme_dump_registers NULL
3279#define nvme_link_reset NULL
3280#define nvme_slot_reset NULL
3281#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06003282
Jingoo Han671a6012014-02-13 11:19:14 +09003283#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06003284static int nvme_suspend(struct device *dev)
3285{
3286 struct pci_dev *pdev = to_pci_dev(dev);
3287 struct nvme_dev *ndev = pci_get_drvdata(pdev);
3288
3289 nvme_dev_shutdown(ndev);
3290 return 0;
3291}
3292
3293static int nvme_resume(struct device *dev)
3294{
3295 struct pci_dev *pdev = to_pci_dev(dev);
3296 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06003297
Keith Busch9a6b9452013-12-10 13:10:36 -07003298 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Tejun Heo9ca97372014-03-07 10:24:49 -05003299 ndev->reset_workfn = nvme_reset_failed_dev;
Keith Busch9a6b9452013-12-10 13:10:36 -07003300 queue_work(nvme_workq, &ndev->reset_work);
3301 }
3302 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06003303}
Jingoo Han671a6012014-02-13 11:19:14 +09003304#endif
Keith Buschcd638942013-07-15 15:02:23 -06003305
3306static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003307
Stephen Hemminger1d352032012-09-07 09:33:17 -07003308static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003309 .error_detected = nvme_error_detected,
3310 .mmio_enabled = nvme_dump_registers,
3311 .link_reset = nvme_link_reset,
3312 .slot_reset = nvme_slot_reset,
3313 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06003314 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003315};
3316
3317/* Move to pci_ids.h later */
3318#define PCI_CLASS_STORAGE_EXPRESS 0x010802
3319
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003320static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003321 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
3322 { 0, }
3323};
3324MODULE_DEVICE_TABLE(pci, nvme_id_table);
3325
3326static struct pci_driver nvme_driver = {
3327 .name = "nvme",
3328 .id_table = nvme_id_table,
3329 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003330 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05003331 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06003332 .driver = {
3333 .pm = &nvme_dev_pm_ops,
3334 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003335 .err_handler = &nvme_err_handler,
3336};
3337
3338static int __init nvme_init(void)
3339{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003340 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003341
Dan McLeranb9afca32014-04-07 17:10:11 -06003342 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003343
Keith Busch9a6b9452013-12-10 13:10:36 -07003344 nvme_workq = create_singlethread_workqueue("nvme");
3345 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06003346 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07003347
Keith Busch5c42ea12012-07-25 16:05:18 -06003348 result = register_blkdev(nvme_major, "nvme");
3349 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07003350 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06003351 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003352 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003353
Keith Buschb3fffde2015-02-03 11:21:42 -07003354 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
3355 &nvme_dev_fops);
3356 if (result < 0)
3357 goto unregister_blkdev;
3358 else if (result > 0)
3359 nvme_char_major = result;
3360
3361 nvme_class = class_create(THIS_MODULE, "nvme");
Alexey Khoroshilovc7270402015-03-07 01:43:41 +03003362 if (IS_ERR(nvme_class)) {
3363 result = PTR_ERR(nvme_class);
Keith Buschb3fffde2015-02-03 11:21:42 -07003364 goto unregister_chrdev;
Alexey Khoroshilovc7270402015-03-07 01:43:41 +03003365 }
Keith Buschb3fffde2015-02-03 11:21:42 -07003366
Keith Buschf3db22f2014-06-11 11:51:35 -06003367 result = pci_register_driver(&nvme_driver);
3368 if (result)
Keith Buschb3fffde2015-02-03 11:21:42 -07003369 goto destroy_class;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003370 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003371
Keith Buschb3fffde2015-02-03 11:21:42 -07003372 destroy_class:
3373 class_destroy(nvme_class);
3374 unregister_chrdev:
3375 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003376 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003377 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003378 kill_workq:
3379 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003380 return result;
3381}
3382
3383static void __exit nvme_exit(void)
3384{
3385 pci_unregister_driver(&nvme_driver);
3386 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003387 destroy_workqueue(nvme_workq);
Keith Buschb3fffde2015-02-03 11:21:42 -07003388 class_destroy(nvme_class);
3389 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Dan McLeranb9afca32014-04-07 17:10:11 -06003390 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04003391 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003392}
3393
3394MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
3395MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07003396MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003397module_init(nvme_init);
3398module_exit(nvme_exit);