blob: 3ffa57a932ea47d96c49663eea80d153f46414b1 [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003 * Copyright (c) 2011-2014, Intel Corporation.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050013 */
14
15#include <linux/nvme.h>
Matthew Wilcox8de05532011-05-12 13:50:28 -040016#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050017#include <linux/blkdev.h>
Matias Bjørlinga4aea562014-11-04 08:20:14 -070018#include <linux/blk-mq.h>
Keith Busch42f61422014-03-24 10:46:25 -060019#include <linux/cpu.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040020#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050021#include <linux/errno.h>
22#include <linux/fs.h>
23#include <linux/genhd.h>
Keith Busch4cc09e22014-04-02 15:45:37 -060024#include <linux/hdreg.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040025#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050026#include <linux/init.h>
27#include <linux/interrupt.h>
28#include <linux/io.h>
29#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050030#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050031#include <linux/kernel.h>
32#include <linux/mm.h>
33#include <linux/module.h>
34#include <linux/moduleparam.h>
35#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050036#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040037#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050038#include <linux/sched.h>
39#include <linux/slab.h>
Keith Busche1e5e562015-02-19 13:39:03 -070040#include <linux/t10-pi.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050041#include <linux/types.h>
Vishal Verma5d0f6132013-03-04 18:40:58 -070042#include <scsi/sg.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090043#include <asm-generic/io-64-nonatomic-lo-hi.h>
44
Keith Busch9d43cf62014-05-13 11:42:02 -060045#define NVME_Q_DEPTH 1024
Matias Bjørlinga4aea562014-11-04 08:20:14 -070046#define NVME_AQ_DEPTH 64
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050047#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
48#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Keith Busch9d43cf62014-05-13 11:42:02 -060049#define ADMIN_TIMEOUT (admin_timeout * HZ)
Dan McLeran2484f402014-07-01 09:33:32 -060050#define SHUTDOWN_TIMEOUT (shutdown_timeout * HZ)
Keith Busch9d43cf62014-05-13 11:42:02 -060051#define IOD_TIMEOUT (retry_time * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050052
Keith Busch9d43cf62014-05-13 11:42:02 -060053static unsigned char admin_timeout = 60;
54module_param(admin_timeout, byte, 0644);
55MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050056
Matthew Wilcoxbd676082014-06-03 23:04:30 -040057unsigned char nvme_io_timeout = 30;
58module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
Keith Buschb3550842014-04-04 11:43:36 -060059MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050060
Keith Busch61e4ce02014-05-13 11:42:01 -060061static unsigned char retry_time = 30;
62module_param(retry_time, byte, 0644);
63MODULE_PARM_DESC(retry_time, "time in seconds to retry failed I/O");
64
Dan McLeran2484f402014-07-01 09:33:32 -060065static unsigned char shutdown_timeout = 5;
66module_param(shutdown_timeout, byte, 0644);
67MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
68
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050069static int nvme_major;
70module_param(nvme_major, int, 0);
71
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050072static int use_threaded_interrupts;
73module_param(use_threaded_interrupts, int, 0);
74
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050075static DEFINE_SPINLOCK(dev_list_lock);
76static LIST_HEAD(dev_list);
77static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070078static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060079static wait_queue_head_t nvme_kthread_wait;
Keith Buschf3db22f2014-06-11 11:51:35 -060080static struct notifier_block nvme_nb;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050081
Keith Buschd4b4ff82013-12-10 13:10:37 -070082static void nvme_reset_failed_dev(struct work_struct *ws);
Matias Bjørlinga4aea562014-11-04 08:20:14 -070083static int nvme_process_cq(struct nvme_queue *nvmeq);
Keith Buschd4b4ff82013-12-10 13:10:37 -070084
Keith Busch4d115422013-12-10 13:10:40 -070085struct async_cmd_info {
86 struct kthread_work work;
87 struct kthread_worker *worker;
Matias Bjørlinga4aea562014-11-04 08:20:14 -070088 struct request *req;
Keith Busch4d115422013-12-10 13:10:40 -070089 u32 result;
90 int status;
91 void *ctx;
92};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050093
94/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050095 * An NVM Express queue. Each device has at least two (one for admin
96 * commands and one for I/O commands).
97 */
98struct nvme_queue {
Keith Buschf435c282014-07-07 09:14:42 -060099 struct llist_node node;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500100 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500101 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500102 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500103 spinlock_t q_lock;
104 struct nvme_command *sq_cmds;
105 volatile struct nvme_completion *cqes;
106 dma_addr_t sq_dma_addr;
107 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500108 u32 __iomem *q_db;
109 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700110 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500111 u16 sq_head;
112 u16 sq_tail;
113 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700114 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400115 u8 cq_phase;
116 u8 cqe_seen;
Keith Busch4d115422013-12-10 13:10:40 -0700117 struct async_cmd_info cmdinfo;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700118 struct blk_mq_hw_ctx *hctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500119};
120
121/*
122 * Check we didin't inadvertently grow the command struct
123 */
124static inline void _nvme_check_size(void)
125{
126 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
127 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
128 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
129 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
130 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400131 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700132 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500133 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
134 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
135 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
136 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600137 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500138}
139
Keith Buschedd10d32014-04-03 16:45:23 -0600140typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400141 struct nvme_completion *);
142
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500143struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400144 nvme_completion_fn fn;
145 void *ctx;
Keith Buschc30341d2013-12-10 13:10:38 -0700146 int aborted;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700147 struct nvme_queue *nvmeq;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700148 struct nvme_iod iod[0];
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500149};
150
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700151/*
152 * Max size of iod being embedded in the request payload
153 */
154#define NVME_INT_PAGES 2
155#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->page_size)
156
157/*
158 * Will slightly overestimate the number of pages needed. This is OK
159 * as it only leads to a small amount of wasted memory for the lifetime of
160 * the I/O.
161 */
162static int nvme_npages(unsigned size, struct nvme_dev *dev)
163{
164 unsigned nprps = DIV_ROUND_UP(size + dev->page_size, dev->page_size);
165 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
166}
167
168static unsigned int nvme_cmd_size(struct nvme_dev *dev)
169{
170 unsigned int ret = sizeof(struct nvme_cmd_info);
171
172 ret += sizeof(struct nvme_iod);
173 ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
174 ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
175
176 return ret;
177}
178
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700179static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
180 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500181{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700182 struct nvme_dev *dev = data;
183 struct nvme_queue *nvmeq = dev->queues[0];
184
185 WARN_ON(nvmeq->hctx);
186 nvmeq->hctx = hctx;
187 hctx->driver_data = nvmeq;
188 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500189}
190
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700191static int nvme_admin_init_request(void *data, struct request *req,
192 unsigned int hctx_idx, unsigned int rq_idx,
193 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600194{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700195 struct nvme_dev *dev = data;
196 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
197 struct nvme_queue *nvmeq = dev->queues[0];
198
199 BUG_ON(!nvmeq);
200 cmd->nvmeq = nvmeq;
201 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600202}
203
Jens Axboe2c30540b2014-11-14 09:47:32 -0700204static void nvme_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
205{
206 struct nvme_queue *nvmeq = hctx->driver_data;
207
208 nvmeq->hctx = NULL;
209}
210
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700211static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
212 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500213{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700214 struct nvme_dev *dev = data;
215 struct nvme_queue *nvmeq = dev->queues[
216 (hctx_idx % dev->queue_count) + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500217
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700218 if (!nvmeq->hctx)
219 nvmeq->hctx = hctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500220
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700221 /* nvmeq queues are shared between namespaces. We assume here that
222 * blk-mq map the tags so they match up with the nvme queue tags. */
223 WARN_ON(nvmeq->hctx->tags != hctx->tags);
224
225 hctx->driver_data = nvmeq;
226 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500227}
228
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700229static int nvme_init_request(void *data, struct request *req,
230 unsigned int hctx_idx, unsigned int rq_idx,
231 unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500232{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700233 struct nvme_dev *dev = data;
234 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
235 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
236
237 BUG_ON(!nvmeq);
238 cmd->nvmeq = nvmeq;
239 return 0;
240}
241
242static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
243 nvme_completion_fn handler)
244{
245 cmd->fn = handler;
246 cmd->ctx = ctx;
247 cmd->aborted = 0;
Keith Buschc917dfe2015-01-07 18:55:48 -0700248 blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500249}
250
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700251static void *iod_get_private(struct nvme_iod *iod)
252{
253 return (void *) (iod->private & ~0x1UL);
254}
255
256/*
257 * If bit 0 is set, the iod is embedded in the request payload.
258 */
259static bool iod_should_kfree(struct nvme_iod *iod)
260{
261 return (iod->private & 0x01) == 0;
262}
263
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400264/* Special values must be less than 0x1000 */
265#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500266#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
267#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
268#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500269
Keith Buschedd10d32014-04-03 16:45:23 -0600270static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400271 struct nvme_completion *cqe)
272{
273 if (ctx == CMD_CTX_CANCELLED)
274 return;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400275 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600276 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400277 "completed id %d twice on queue %d\n",
278 cqe->command_id, le16_to_cpup(&cqe->sq_id));
279 return;
280 }
281 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600282 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400283 "invalid id %d completed on queue %d\n",
284 cqe->command_id, le16_to_cpup(&cqe->sq_id));
285 return;
286 }
Keith Buschedd10d32014-04-03 16:45:23 -0600287 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400288}
289
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700290static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
291{
292 void *ctx;
293
294 if (fn)
295 *fn = cmd->fn;
296 ctx = cmd->ctx;
297 cmd->fn = special_completion;
298 cmd->ctx = CMD_CTX_CANCELLED;
299 return ctx;
300}
301
302static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
303 struct nvme_completion *cqe)
304{
305 struct request *req = ctx;
306
307 u32 result = le32_to_cpup(&cqe->result);
308 u16 status = le16_to_cpup(&cqe->status) >> 1;
309
310 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
311 ++nvmeq->dev->event_limit;
312 if (status == NVME_SC_SUCCESS)
313 dev_warn(nvmeq->q_dmadev,
314 "async event result %08x\n", result);
315
Jens Axboe9d135bb2014-11-17 10:43:42 -0700316 blk_mq_free_hctx_request(nvmeq->hctx, req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700317}
318
319static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
320 struct nvme_completion *cqe)
321{
322 struct request *req = ctx;
323
324 u16 status = le16_to_cpup(&cqe->status) >> 1;
325 u32 result = le32_to_cpup(&cqe->result);
326
Jens Axboe9d135bb2014-11-17 10:43:42 -0700327 blk_mq_free_hctx_request(nvmeq->hctx, req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700328
329 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
330 ++nvmeq->dev->abort_limit;
331}
332
Keith Buschedd10d32014-04-03 16:45:23 -0600333static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700334 struct nvme_completion *cqe)
335{
336 struct async_cmd_info *cmdinfo = ctx;
337 cmdinfo->result = le32_to_cpup(&cqe->result);
338 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
339 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
Jens Axboe9d135bb2014-11-17 10:43:42 -0700340 blk_mq_free_hctx_request(nvmeq->hctx, cmdinfo->req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700341}
342
343static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
344 unsigned int tag)
345{
346 struct blk_mq_hw_ctx *hctx = nvmeq->hctx;
347 struct request *req = blk_mq_tag_to_rq(hctx->tags, tag);
348
349 return blk_mq_rq_to_pdu(req);
Keith Busch4d115422013-12-10 13:10:40 -0700350}
351
Matthew Wilcox184d2942011-05-11 21:36:38 -0400352/*
353 * Called with local interrupts disabled and the q_lock held. May not sleep.
354 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700355static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400356 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500357{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700358 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400359 void *ctx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700360 if (tag >= nvmeq->q_depth) {
361 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500362 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400363 }
Keith Busch859361a2012-08-02 14:05:59 -0600364 if (fn)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700365 *fn = cmd->fn;
366 ctx = cmd->ctx;
367 cmd->fn = special_completion;
368 cmd->ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400369 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500370}
371
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500372/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400373 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500374 * @nvmeq: The queue to use
375 * @cmd: The command to send
376 *
377 * Safe to use from interrupt context
378 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700379static int __nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500380{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700381 u16 tail = nvmeq->sq_tail;
382
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500383 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500384 if (++tail == nvmeq->q_depth)
385 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500386 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500387 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500388
389 return 0;
390}
391
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700392static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
393{
394 unsigned long flags;
395 int ret;
396 spin_lock_irqsave(&nvmeq->q_lock, flags);
397 ret = __nvme_submit_cmd(nvmeq, cmd);
398 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
399 return ret;
400}
401
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500402static __le64 **iod_list(struct nvme_iod *iod)
403{
404 return ((void *)iod) + iod->offset;
405}
406
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700407static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
408 unsigned nseg, unsigned long private)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500409{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700410 iod->private = private;
411 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
412 iod->npages = -1;
413 iod->length = nbytes;
414 iod->nents = 0;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500415}
416
417static struct nvme_iod *
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700418__nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
419 unsigned long priv, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500420{
421 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700422 sizeof(__le64 *) * nvme_npages(bytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500423 sizeof(struct scatterlist) * nseg, gfp);
424
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700425 if (iod)
426 iod_init(iod, bytes, nseg, priv);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500427
428 return iod;
429}
430
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700431static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
432 gfp_t gfp)
433{
434 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
435 sizeof(struct nvme_dsm_range);
436 unsigned long mask = 0;
437 struct nvme_iod *iod;
438
439 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
440 size <= NVME_INT_BYTES(dev)) {
441 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
442
443 iod = cmd->iod;
444 mask = 0x01;
445 iod_init(iod, size, rq->nr_phys_segments,
446 (unsigned long) rq | 0x01);
447 return iod;
448 }
449
450 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
451 (unsigned long) rq, gfp);
452}
453
Vishal Verma5d0f6132013-03-04 18:40:58 -0700454void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500455{
Keith Busch1d090622014-06-23 11:34:01 -0600456 const int last_prp = dev->page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500457 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500458 __le64 **list = iod_list(iod);
459 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500460
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500461 if (iod->npages == 0)
462 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
463 for (i = 0; i < iod->npages; i++) {
464 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500465 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500466 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500467 prp_dma = next_prp_dma;
468 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700469
470 if (iod_should_kfree(iod))
471 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500472}
473
Keith Buschb4ff9c82014-08-29 09:06:12 -0600474static int nvme_error_status(u16 status)
475{
476 switch (status & 0x7ff) {
477 case NVME_SC_SUCCESS:
478 return 0;
479 case NVME_SC_CAP_EXCEEDED:
480 return -ENOSPC;
481 default:
482 return -EIO;
483 }
484}
485
Keith Busche1e5e562015-02-19 13:39:03 -0700486static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
487{
488 if (be32_to_cpu(pi->ref_tag) == v)
489 pi->ref_tag = cpu_to_be32(p);
490}
491
492static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
493{
494 if (be32_to_cpu(pi->ref_tag) == p)
495 pi->ref_tag = cpu_to_be32(v);
496}
497
498/**
499 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
500 *
501 * The virtual start sector is the one that was originally submitted by the
502 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
503 * start sector may be different. Remap protection information to match the
504 * physical LBA on writes, and back to the original seed on reads.
505 *
506 * Type 0 and 3 do not have a ref tag, so no remapping required.
507 */
508static void nvme_dif_remap(struct request *req,
509 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
510{
511 struct nvme_ns *ns = req->rq_disk->private_data;
512 struct bio_integrity_payload *bip;
513 struct t10_pi_tuple *pi;
514 void *p, *pmap;
515 u32 i, nlb, ts, phys, virt;
516
517 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
518 return;
519
520 bip = bio_integrity(req->bio);
521 if (!bip)
522 return;
523
524 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
525 if (!pmap)
526 return;
527
528 p = pmap;
529 virt = bip_get_seed(bip);
530 phys = nvme_block_nr(ns, blk_rq_pos(req));
531 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
532 ts = ns->disk->integrity->tuple_size;
533
534 for (i = 0; i < nlb; i++, virt++, phys++) {
535 pi = (struct t10_pi_tuple *)p;
536 dif_swap(phys, virt, pi);
537 p += ts;
538 }
539 kunmap_atomic(pmap);
540}
541
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700542static void req_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500543 struct nvme_completion *cqe)
544{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500545 struct nvme_iod *iod = ctx;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700546 struct request *req = iod_get_private(iod);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700547 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
548
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500549 u16 status = le16_to_cpup(&cqe->status) >> 1;
550
Keith Buschedd10d32014-04-03 16:45:23 -0600551 if (unlikely(status)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700552 if (!(status & NVME_SC_DNR || blk_noretry_request(req))
553 && (jiffies - req->start_time) < req->timeout) {
Keith Buschc9d3bf82015-01-07 18:55:52 -0700554 unsigned long flags;
555
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700556 blk_mq_requeue_request(req);
Keith Buschc9d3bf82015-01-07 18:55:52 -0700557 spin_lock_irqsave(req->q->queue_lock, flags);
558 if (!blk_queue_stopped(req->q))
559 blk_mq_kick_requeue_list(req->q);
560 spin_unlock_irqrestore(req->q->queue_lock, flags);
Keith Buschedd10d32014-04-03 16:45:23 -0600561 return;
562 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700563 req->errors = nvme_error_status(status);
564 } else
565 req->errors = 0;
566
567 if (cmd_rq->aborted)
568 dev_warn(&nvmeq->dev->pci_dev->dev,
569 "completing aborted command with status:%04x\n",
570 status);
571
Keith Busche1e5e562015-02-19 13:39:03 -0700572 if (iod->nents) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700573 dma_unmap_sg(&nvmeq->dev->pci_dev->dev, iod->sg, iod->nents,
574 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busche1e5e562015-02-19 13:39:03 -0700575 if (blk_integrity_rq(req)) {
576 if (!rq_data_dir(req))
577 nvme_dif_remap(req, nvme_dif_complete);
578 dma_unmap_sg(&nvmeq->dev->pci_dev->dev, iod->meta_sg, 1,
579 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
580 }
581 }
Keith Buschedd10d32014-04-03 16:45:23 -0600582 nvme_free_iod(nvmeq->dev, iod);
Keith Busch3291fa52014-04-28 12:30:52 -0600583
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700584 blk_mq_complete_request(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500585}
586
Matthew Wilcox184d2942011-05-11 21:36:38 -0400587/* length is in bytes. gfp flags indicates whether we may sleep. */
Keith Buschedd10d32014-04-03 16:45:23 -0600588int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod, int total_len,
589 gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500590{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500591 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500592 int length = total_len;
593 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500594 int dma_len = sg_dma_len(sg);
595 u64 dma_addr = sg_dma_address(sg);
596 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500597 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500598 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500599 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500600 int nprps, i;
Keith Busch1d090622014-06-23 11:34:01 -0600601 u32 page_size = dev->page_size;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500602
Keith Busch1d090622014-06-23 11:34:01 -0600603 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500604 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500605 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500606
Keith Busch1d090622014-06-23 11:34:01 -0600607 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500608 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600609 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500610 } else {
611 sg = sg_next(sg);
612 dma_addr = sg_dma_address(sg);
613 dma_len = sg_dma_len(sg);
614 }
615
Keith Busch1d090622014-06-23 11:34:01 -0600616 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600617 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500618 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500619 }
620
Keith Busch1d090622014-06-23 11:34:01 -0600621 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500622 if (nprps <= (256 / 8)) {
623 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500624 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500625 } else {
626 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500627 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500628 }
629
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400630 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
631 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600632 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500633 iod->npages = -1;
Keith Busch1d090622014-06-23 11:34:01 -0600634 return (total_len - length) + page_size;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400635 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500636 list[0] = prp_list;
637 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500638 i = 0;
639 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600640 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500641 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400642 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500643 if (!prp_list)
644 return total_len - length;
645 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400646 prp_list[0] = old_prp_list[i - 1];
647 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
648 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500649 }
650 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600651 dma_len -= page_size;
652 dma_addr += page_size;
653 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500654 if (length <= 0)
655 break;
656 if (dma_len > 0)
657 continue;
658 BUG_ON(dma_len < 0);
659 sg = sg_next(sg);
660 dma_addr = sg_dma_address(sg);
661 dma_len = sg_dma_len(sg);
662 }
663
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500664 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500665}
666
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700667/*
668 * We reuse the small pool to allocate the 16-byte range here as it is not
669 * worth having a special pool for these or additional cases to handle freeing
670 * the iod.
671 */
672static void nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
673 struct request *req, struct nvme_iod *iod)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700674{
Keith Buschedd10d32014-04-03 16:45:23 -0600675 struct nvme_dsm_range *range =
676 (struct nvme_dsm_range *)iod_list(iod)[0];
Keith Busch0e5e4f02012-11-09 16:33:05 -0700677 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
678
Keith Busch0e5e4f02012-11-09 16:33:05 -0700679 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700680 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
681 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700682
683 memset(cmnd, 0, sizeof(*cmnd));
684 cmnd->dsm.opcode = nvme_cmd_dsm;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700685 cmnd->dsm.command_id = req->tag;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700686 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
687 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
688 cmnd->dsm.nr = 0;
689 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
690
691 if (++nvmeq->sq_tail == nvmeq->q_depth)
692 nvmeq->sq_tail = 0;
693 writel(nvmeq->sq_tail, nvmeq->q_db);
Keith Busch0e5e4f02012-11-09 16:33:05 -0700694}
695
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700696static void nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500697 int cmdid)
698{
699 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
700
701 memset(cmnd, 0, sizeof(*cmnd));
702 cmnd->common.opcode = nvme_cmd_flush;
703 cmnd->common.command_id = cmdid;
704 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
705
706 if (++nvmeq->sq_tail == nvmeq->q_depth)
707 nvmeq->sq_tail = 0;
708 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500709}
710
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700711static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod,
712 struct nvme_ns *ns)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500713{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700714 struct request *req = iod_get_private(iod);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500715 struct nvme_command *cmnd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700716 u16 control = 0;
717 u32 dsmgmt = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500718
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700719 if (req->cmd_flags & REQ_FUA)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500720 control |= NVME_RW_FUA;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700721 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500722 control |= NVME_RW_LR;
723
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700724 if (req->cmd_flags & REQ_RAHEAD)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500725 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
726
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500727 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500728 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500729
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700730 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
731 cmnd->rw.command_id = req->tag;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500732 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Keith Buschedd10d32014-04-03 16:45:23 -0600733 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
734 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700735 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
736 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
Keith Busche1e5e562015-02-19 13:39:03 -0700737
738 if (blk_integrity_rq(req)) {
739 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(iod->meta_sg));
740 switch (ns->pi_type) {
741 case NVME_NS_DPS_PI_TYPE3:
742 control |= NVME_RW_PRINFO_PRCHK_GUARD;
743 break;
744 case NVME_NS_DPS_PI_TYPE1:
745 case NVME_NS_DPS_PI_TYPE2:
746 control |= NVME_RW_PRINFO_PRCHK_GUARD |
747 NVME_RW_PRINFO_PRCHK_REF;
748 cmnd->rw.reftag = cpu_to_le32(
749 nvme_block_nr(ns, blk_rq_pos(req)));
750 break;
751 }
752 } else if (ns->ms)
753 control |= NVME_RW_PRINFO_PRACT;
754
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500755 cmnd->rw.control = cpu_to_le16(control);
756 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500757
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500758 if (++nvmeq->sq_tail == nvmeq->q_depth)
759 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500760 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500761
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500762 return 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600763}
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500764
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700765static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
766 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600767{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700768 struct nvme_ns *ns = hctx->queue->queuedata;
769 struct nvme_queue *nvmeq = hctx->driver_data;
770 struct request *req = bd->rq;
771 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
Keith Buschedd10d32014-04-03 16:45:23 -0600772 struct nvme_iod *iod;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700773 enum dma_data_direction dma_dir;
Keith Buschedd10d32014-04-03 16:45:23 -0600774
Keith Busche1e5e562015-02-19 13:39:03 -0700775 /*
776 * If formated with metadata, require the block layer provide a buffer
777 * unless this namespace is formated such that the metadata can be
778 * stripped/generated by the controller with PRACT=1.
779 */
780 if (ns->ms && !blk_integrity_rq(req)) {
781 if (!(ns->pi_type && ns->ms == 8)) {
782 req->errors = -EFAULT;
783 blk_mq_complete_request(req);
784 return BLK_MQ_RQ_QUEUE_OK;
785 }
786 }
787
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700788 iod = nvme_alloc_iod(req, ns->dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600789 if (!iod)
Jens Axboefe543032014-12-11 13:58:39 -0700790 return BLK_MQ_RQ_QUEUE_BUSY;
Keith Buschedd10d32014-04-03 16:45:23 -0600791
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700792 if (req->cmd_flags & REQ_DISCARD) {
Keith Buschedd10d32014-04-03 16:45:23 -0600793 void *range;
794 /*
795 * We reuse the small pool to allocate the 16-byte range here
796 * as it is not worth having a special pool for these or
797 * additional cases to handle freeing the iod.
798 */
799 range = dma_pool_alloc(nvmeq->dev->prp_small_pool,
800 GFP_ATOMIC,
801 &iod->first_dma);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700802 if (!range)
Jens Axboefe543032014-12-11 13:58:39 -0700803 goto retry_cmd;
Keith Buschedd10d32014-04-03 16:45:23 -0600804 iod_list(iod)[0] = (__le64 *)range;
805 iod->npages = 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700806 } else if (req->nr_phys_segments) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700807 dma_dir = rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
Keith Buschedd10d32014-04-03 16:45:23 -0600808
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700809 sg_init_table(iod->sg, req->nr_phys_segments);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700810 iod->nents = blk_rq_map_sg(req->q, req, iod->sg);
Jens Axboefe543032014-12-11 13:58:39 -0700811 if (!iod->nents)
812 goto error_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700813
814 if (!dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir))
Jens Axboefe543032014-12-11 13:58:39 -0700815 goto retry_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700816
Jens Axboefe543032014-12-11 13:58:39 -0700817 if (blk_rq_bytes(req) !=
818 nvme_setup_prps(nvmeq->dev, iod, blk_rq_bytes(req), GFP_ATOMIC)) {
819 dma_unmap_sg(&nvmeq->dev->pci_dev->dev, iod->sg,
820 iod->nents, dma_dir);
821 goto retry_cmd;
822 }
Keith Busche1e5e562015-02-19 13:39:03 -0700823 if (blk_integrity_rq(req)) {
824 if (blk_rq_count_integrity_sg(req->q, req->bio) != 1)
825 goto error_cmd;
826
827 sg_init_table(iod->meta_sg, 1);
828 if (blk_rq_map_integrity_sg(
829 req->q, req->bio, iod->meta_sg) != 1)
830 goto error_cmd;
831
832 if (rq_data_dir(req))
833 nvme_dif_remap(req, nvme_dif_prep);
834
835 if (!dma_map_sg(nvmeq->q_dmadev, iod->meta_sg, 1, dma_dir))
836 goto error_cmd;
837 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700838 }
839
Keith Busch9af87852014-12-03 17:07:13 -0700840 nvme_set_info(cmd, iod, req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700841 spin_lock_irq(&nvmeq->q_lock);
842 if (req->cmd_flags & REQ_DISCARD)
843 nvme_submit_discard(nvmeq, ns, req, iod);
844 else if (req->cmd_flags & REQ_FLUSH)
845 nvme_submit_flush(nvmeq, ns, req->tag);
846 else
847 nvme_submit_iod(nvmeq, iod, ns);
848
849 nvme_process_cq(nvmeq);
850 spin_unlock_irq(&nvmeq->q_lock);
851 return BLK_MQ_RQ_QUEUE_OK;
852
Jens Axboefe543032014-12-11 13:58:39 -0700853 error_cmd:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500854 nvme_free_iod(nvmeq->dev, iod);
Jens Axboefe543032014-12-11 13:58:39 -0700855 return BLK_MQ_RQ_QUEUE_ERROR;
856 retry_cmd:
857 nvme_free_iod(nvmeq->dev, iod);
858 return BLK_MQ_RQ_QUEUE_BUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500859}
860
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400861static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500862{
Matthew Wilcox82123462011-01-20 13:24:06 -0500863 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500864
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500865 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500866 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500867
868 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400869 void *ctx;
870 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500871 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500872 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500873 break;
874 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
875 if (++head == nvmeq->q_depth) {
876 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500877 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500878 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700879 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600880 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500881 }
882
883 /* If the controller ignores the cq head doorbell and continuously
884 * writes to the queue, it is theoretically possible to wrap around
885 * the queue twice and mistakenly return IRQ_NONE. Linux only
886 * requires that 0.1% of your interrupts are handled, so this isn't
887 * a big problem.
888 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500889 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400890 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500891
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800892 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500893 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500894 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500895
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400896 nvmeq->cqe_seen = 1;
897 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500898}
899
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700900/* Admin queue isn't initialized as a request queue. If at some point this
901 * happens anyway, make sure to notify the user */
902static int nvme_admin_queue_rq(struct blk_mq_hw_ctx *hctx,
903 const struct blk_mq_queue_data *bd)
Matthew Wilcox7d822452013-06-24 12:03:57 -0400904{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700905 WARN_ON_ONCE(1);
906 return BLK_MQ_RQ_QUEUE_ERROR;
Matthew Wilcox7d822452013-06-24 12:03:57 -0400907}
908
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500909static irqreturn_t nvme_irq(int irq, void *data)
910{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500911 irqreturn_t result;
912 struct nvme_queue *nvmeq = data;
913 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400914 nvme_process_cq(nvmeq);
915 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
916 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500917 spin_unlock(&nvmeq->q_lock);
918 return result;
919}
920
921static irqreturn_t nvme_irq_check(int irq, void *data)
922{
923 struct nvme_queue *nvmeq = data;
924 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
925 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
926 return IRQ_NONE;
927 return IRQ_WAKE_THREAD;
928}
929
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700930static void nvme_abort_cmd_info(struct nvme_queue *nvmeq, struct nvme_cmd_info *
931 cmd_info)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500932{
933 spin_lock_irq(&nvmeq->q_lock);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700934 cancel_cmd_info(cmd_info, NULL);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500935 spin_unlock_irq(&nvmeq->q_lock);
936}
937
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400938struct sync_cmd_info {
939 struct task_struct *task;
940 u32 result;
941 int status;
942};
943
Keith Buschedd10d32014-04-03 16:45:23 -0600944static void sync_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400945 struct nvme_completion *cqe)
946{
947 struct sync_cmd_info *cmdinfo = ctx;
948 cmdinfo->result = le32_to_cpup(&cqe->result);
949 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
950 wake_up_process(cmdinfo->task);
951}
952
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500953/*
954 * Returns 0 on success. If the result is negative, it's a Linux error code;
955 * if the result is positive, it's an NVM Express status code
956 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700957static int nvme_submit_sync_cmd(struct request *req, struct nvme_command *cmd,
Vishal Verma5d0f6132013-03-04 18:40:58 -0700958 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500959{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700960 int ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500961 struct sync_cmd_info cmdinfo;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700962 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
963 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500964
965 cmdinfo.task = current;
966 cmdinfo.status = -EINTR;
967
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700968 cmd->common.command_id = req->tag;
969
970 nvme_set_info(cmd_rq, &cmdinfo, sync_completion);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500971
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500972 set_current_state(TASK_KILLABLE);
Keith Busch4f5099a2014-03-03 16:39:13 -0700973 ret = nvme_submit_cmd(nvmeq, cmd);
974 if (ret) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700975 nvme_finish_cmd(nvmeq, req->tag, NULL);
Keith Busch4f5099a2014-03-03 16:39:13 -0700976 set_current_state(TASK_RUNNING);
Keith Busch4f5099a2014-03-03 16:39:13 -0700977 }
Jens Axboe849c6e72014-12-12 08:53:40 -0700978 ret = schedule_timeout(timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500979
Jens Axboe849c6e72014-12-12 08:53:40 -0700980 /*
981 * Ensure that sync_completion has either run, or that it will
982 * never run.
983 */
984 nvme_abort_cmd_info(nvmeq, blk_mq_rq_to_pdu(req));
985
986 /*
987 * We never got the completion
988 */
989 if (cmdinfo.status == -EINTR)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500990 return -EINTR;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500991
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500992 if (result)
993 *result = cmdinfo.result;
994
995 return cmdinfo.status;
996}
997
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700998static int nvme_submit_async_admin_req(struct nvme_dev *dev)
999{
1000 struct nvme_queue *nvmeq = dev->queues[0];
1001 struct nvme_command c;
1002 struct nvme_cmd_info *cmd_info;
1003 struct request *req;
1004
Jens Axboe6dcc0cf2014-11-18 08:21:18 -07001005 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC, false);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001006 if (IS_ERR(req))
1007 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001008
Keith Buschc917dfe2015-01-07 18:55:48 -07001009 req->cmd_flags |= REQ_NO_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001010 cmd_info = blk_mq_rq_to_pdu(req);
1011 nvme_set_info(cmd_info, req, async_req_completion);
1012
1013 memset(&c, 0, sizeof(c));
1014 c.common.opcode = nvme_admin_async_event;
1015 c.common.command_id = req->tag;
1016
1017 return __nvme_submit_cmd(nvmeq, &c);
1018}
1019
1020static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
Keith Busch4d115422013-12-10 13:10:40 -07001021 struct nvme_command *cmd,
1022 struct async_cmd_info *cmdinfo, unsigned timeout)
1023{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001024 struct nvme_queue *nvmeq = dev->queues[0];
1025 struct request *req;
1026 struct nvme_cmd_info *cmd_rq;
Keith Busch4d115422013-12-10 13:10:40 -07001027
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001028 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_KERNEL, false);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001029 if (IS_ERR(req))
1030 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001031
1032 req->timeout = timeout;
1033 cmd_rq = blk_mq_rq_to_pdu(req);
1034 cmdinfo->req = req;
1035 nvme_set_info(cmd_rq, cmdinfo, async_completion);
Keith Busch4d115422013-12-10 13:10:40 -07001036 cmdinfo->status = -EINTR;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001037
1038 cmd->common.command_id = req->tag;
1039
Keith Busch4f5099a2014-03-03 16:39:13 -07001040 return nvme_submit_cmd(nvmeq, cmd);
Keith Busch4d115422013-12-10 13:10:40 -07001041}
1042
kbuild test robota64e6bb2014-11-05 18:47:07 +08001043static int __nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001044 u32 *result, unsigned timeout)
1045{
1046 int res;
1047 struct request *req;
1048
1049 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_KERNEL, false);
Jens Axboe97fe3832014-12-10 13:02:44 -07001050 if (IS_ERR(req))
1051 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001052 res = nvme_submit_sync_cmd(req, cmd, result, timeout);
Jens Axboe9d135bb2014-11-17 10:43:42 -07001053 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001054 return res;
1055}
1056
Vishal Verma5d0f6132013-03-04 18:40:58 -07001057int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001058 u32 *result)
1059{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001060 return __nvme_submit_admin_cmd(dev, cmd, result, ADMIN_TIMEOUT);
Keith Busch4f5099a2014-03-03 16:39:13 -07001061}
1062
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001063int nvme_submit_io_cmd(struct nvme_dev *dev, struct nvme_ns *ns,
1064 struct nvme_command *cmd, u32 *result)
Keith Busch4f5099a2014-03-03 16:39:13 -07001065{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001066 int res;
1067 struct request *req;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001068
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001069 req = blk_mq_alloc_request(ns->queue, WRITE, (GFP_KERNEL|__GFP_WAIT),
1070 false);
Jens Axboe97fe3832014-12-10 13:02:44 -07001071 if (IS_ERR(req))
1072 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001073 res = nvme_submit_sync_cmd(req, cmd, result, NVME_IO_TIMEOUT);
Jens Axboe9d135bb2014-11-17 10:43:42 -07001074 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001075 return res;
Keith Busch4d115422013-12-10 13:10:40 -07001076}
1077
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001078static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1079{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001080 struct nvme_command c;
1081
1082 memset(&c, 0, sizeof(c));
1083 c.delete_queue.opcode = opcode;
1084 c.delete_queue.qid = cpu_to_le16(id);
1085
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001086 return nvme_submit_admin_cmd(dev, &c, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001087}
1088
1089static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1090 struct nvme_queue *nvmeq)
1091{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001092 struct nvme_command c;
1093 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1094
1095 memset(&c, 0, sizeof(c));
1096 c.create_cq.opcode = nvme_admin_create_cq;
1097 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1098 c.create_cq.cqid = cpu_to_le16(qid);
1099 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1100 c.create_cq.cq_flags = cpu_to_le16(flags);
1101 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1102
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001103 return nvme_submit_admin_cmd(dev, &c, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001104}
1105
1106static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1107 struct nvme_queue *nvmeq)
1108{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001109 struct nvme_command c;
1110 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1111
1112 memset(&c, 0, sizeof(c));
1113 c.create_sq.opcode = nvme_admin_create_sq;
1114 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1115 c.create_sq.sqid = cpu_to_le16(qid);
1116 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1117 c.create_sq.sq_flags = cpu_to_le16(flags);
1118 c.create_sq.cqid = cpu_to_le16(qid);
1119
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001120 return nvme_submit_admin_cmd(dev, &c, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001121}
1122
1123static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1124{
1125 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1126}
1127
1128static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1129{
1130 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1131}
1132
Vishal Verma5d0f6132013-03-04 18:40:58 -07001133int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001134 dma_addr_t dma_addr)
1135{
1136 struct nvme_command c;
1137
1138 memset(&c, 0, sizeof(c));
1139 c.identify.opcode = nvme_admin_identify;
1140 c.identify.nsid = cpu_to_le32(nsid);
1141 c.identify.prp1 = cpu_to_le64(dma_addr);
1142 c.identify.cns = cpu_to_le32(cns);
1143
1144 return nvme_submit_admin_cmd(dev, &c, NULL);
1145}
1146
Vishal Verma5d0f6132013-03-04 18:40:58 -07001147int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -06001148 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001149{
1150 struct nvme_command c;
1151
1152 memset(&c, 0, sizeof(c));
1153 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -06001154 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001155 c.features.prp1 = cpu_to_le64(dma_addr);
1156 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001157
Keith Busch08df1e02012-09-21 10:52:13 -06001158 return nvme_submit_admin_cmd(dev, &c, result);
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001159}
1160
Vishal Verma5d0f6132013-03-04 18:40:58 -07001161int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
1162 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001163{
1164 struct nvme_command c;
1165
1166 memset(&c, 0, sizeof(c));
1167 c.features.opcode = nvme_admin_set_features;
1168 c.features.prp1 = cpu_to_le64(dma_addr);
1169 c.features.fid = cpu_to_le32(fid);
1170 c.features.dword11 = cpu_to_le32(dword11);
1171
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001172 return nvme_submit_admin_cmd(dev, &c, result);
1173}
1174
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001175/**
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001176 * nvme_abort_req - Attempt aborting a request
Keith Buschc30341d2013-12-10 13:10:38 -07001177 *
1178 * Schedule controller reset if the command was already aborted once before and
1179 * still hasn't been returned to the driver, or if this is the admin queue.
1180 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001181static void nvme_abort_req(struct request *req)
Keith Buschc30341d2013-12-10 13:10:38 -07001182{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001183 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1184 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001185 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001186 struct request *abort_req;
1187 struct nvme_cmd_info *abort_cmd;
1188 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -07001189
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001190 if (!nvmeq->qid || cmd_rq->aborted) {
Keith Busch7a509a62015-01-07 18:55:53 -07001191 unsigned long flags;
1192
1193 spin_lock_irqsave(&dev_list_lock, flags);
Keith Buschc30341d2013-12-10 13:10:38 -07001194 if (work_busy(&dev->reset_work))
Keith Busch7a509a62015-01-07 18:55:53 -07001195 goto out;
Keith Buschc30341d2013-12-10 13:10:38 -07001196 list_del_init(&dev->node);
1197 dev_warn(&dev->pci_dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001198 "I/O %d QID %d timeout, reset controller\n",
1199 req->tag, nvmeq->qid);
Tejun Heo9ca97372014-03-07 10:24:49 -05001200 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschc30341d2013-12-10 13:10:38 -07001201 queue_work(nvme_workq, &dev->reset_work);
Keith Busch7a509a62015-01-07 18:55:53 -07001202 out:
1203 spin_unlock_irqrestore(&dev_list_lock, flags);
Keith Buschc30341d2013-12-10 13:10:38 -07001204 return;
1205 }
1206
1207 if (!dev->abort_limit)
1208 return;
1209
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001210 abort_req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC,
1211 false);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001212 if (IS_ERR(abort_req))
Keith Buschc30341d2013-12-10 13:10:38 -07001213 return;
1214
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001215 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1216 nvme_set_info(abort_cmd, abort_req, abort_completion);
1217
Keith Buschc30341d2013-12-10 13:10:38 -07001218 memset(&cmd, 0, sizeof(cmd));
1219 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001220 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001221 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001222 cmd.abort.command_id = abort_req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001223
1224 --dev->abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001225 cmd_rq->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -07001226
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001227 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", req->tag,
Keith Buschc30341d2013-12-10 13:10:38 -07001228 nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001229 if (nvme_submit_cmd(dev->queues[0], &cmd) < 0) {
1230 dev_warn(nvmeq->q_dmadev,
1231 "Could not abort I/O %d QID %d",
1232 req->tag, nvmeq->qid);
Sam Bradshawc87fd542014-12-10 13:00:31 -07001233 blk_mq_free_request(abort_req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001234 }
Keith Buschc30341d2013-12-10 13:10:38 -07001235}
1236
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001237static void nvme_cancel_queue_ios(struct blk_mq_hw_ctx *hctx,
1238 struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001239{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001240 struct nvme_queue *nvmeq = data;
1241 void *ctx;
1242 nvme_completion_fn fn;
1243 struct nvme_cmd_info *cmd;
Keith Buschcef6a942015-01-07 18:55:51 -07001244 struct nvme_completion cqe;
1245
1246 if (!blk_mq_request_started(req))
1247 return;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001248
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001249 cmd = blk_mq_rq_to_pdu(req);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001250
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001251 if (cmd->ctx == CMD_CTX_CANCELLED)
1252 return;
1253
Keith Buschcef6a942015-01-07 18:55:51 -07001254 if (blk_queue_dying(req->q))
1255 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1256 else
1257 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1258
1259
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001260 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1261 req->tag, nvmeq->qid);
1262 ctx = cancel_cmd_info(cmd, &fn);
1263 fn(nvmeq, ctx, &cqe);
1264}
1265
1266static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
1267{
1268 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
1269 struct nvme_queue *nvmeq = cmd->nvmeq;
1270
Keith Busch7a509a62015-01-07 18:55:53 -07001271 /*
1272 * The aborted req will be completed on receiving the abort req.
1273 * We enable the timer again. If hit twice, it'll cause a device reset,
1274 * as the device then is in a faulty state.
1275 */
1276 int ret = BLK_EH_RESET_TIMER;
1277
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001278 dev_warn(nvmeq->q_dmadev, "Timeout I/O %d QID %d\n", req->tag,
1279 nvmeq->qid);
Keith Buschc917dfe2015-01-07 18:55:48 -07001280
Keith Busch7a509a62015-01-07 18:55:53 -07001281 spin_lock_irq(&nvmeq->q_lock);
Keith Buschc917dfe2015-01-07 18:55:48 -07001282 if (!nvmeq->dev->initialized) {
1283 /*
1284 * Force cancelled command frees the request, which requires we
1285 * return BLK_EH_NOT_HANDLED.
1286 */
1287 nvme_cancel_queue_ios(nvmeq->hctx, req, nvmeq, reserved);
Keith Busch7a509a62015-01-07 18:55:53 -07001288 ret = BLK_EH_NOT_HANDLED;
1289 } else
1290 nvme_abort_req(req);
1291 spin_unlock_irq(&nvmeq->q_lock);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001292
Keith Busch7a509a62015-01-07 18:55:53 -07001293 return ret;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001294}
1295
Keith Buschf435c282014-07-07 09:14:42 -06001296static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001297{
1298 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1299 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1300 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1301 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1302 kfree(nvmeq);
1303}
1304
Keith Buscha1a5ef92013-12-16 13:50:00 -05001305static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001306{
1307 int i;
1308
Keith Buscha1a5ef92013-12-16 13:50:00 -05001309 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001310 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001311 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001312 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001313 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001314 }
Keith Busch22404272013-07-15 15:02:20 -06001315}
1316
Keith Busch4d115422013-12-10 13:10:40 -07001317/**
1318 * nvme_suspend_queue - put queue into suspended state
1319 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001320 */
1321static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001322{
Keith Busch2b25d982014-12-22 12:59:04 -07001323 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001324
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001325 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001326 if (nvmeq->cq_vector == -1) {
1327 spin_unlock_irq(&nvmeq->q_lock);
1328 return 1;
1329 }
1330 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Keith Busch42f61422014-03-24 10:46:25 -06001331 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001332 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001333 spin_unlock_irq(&nvmeq->q_lock);
1334
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001335 irq_set_affinity_hint(vector, NULL);
1336 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001337
Keith Busch4d115422013-12-10 13:10:40 -07001338 return 0;
1339}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001340
Keith Busch4d115422013-12-10 13:10:40 -07001341static void nvme_clear_queue(struct nvme_queue *nvmeq)
1342{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001343 struct blk_mq_hw_ctx *hctx = nvmeq->hctx;
1344
Keith Busch22404272013-07-15 15:02:20 -06001345 spin_lock_irq(&nvmeq->q_lock);
1346 nvme_process_cq(nvmeq);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001347 if (hctx && hctx->tags)
1348 blk_mq_tag_busy_iter(hctx, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001349 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001350}
1351
Keith Busch4d115422013-12-10 13:10:40 -07001352static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1353{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001354 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001355
1356 if (!nvmeq)
1357 return;
1358 if (nvme_suspend_queue(nvmeq))
1359 return;
1360
Keith Busch0e53d182013-12-10 13:10:39 -07001361 /* Don't tell the adapter to delete the admin queue.
1362 * Don't tell a removed adapter to delete IO queues. */
1363 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001364 adapter_delete_sq(dev, qid);
1365 adapter_delete_cq(dev, qid);
1366 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001367 if (!qid && dev->admin_q)
1368 blk_mq_freeze_queue_start(dev->admin_q);
Keith Busch4d115422013-12-10 13:10:40 -07001369 nvme_clear_queue(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001370}
1371
1372static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Keith Busch2b25d982014-12-22 12:59:04 -07001373 int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001374{
1375 struct device *dmadev = &dev->pci_dev->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001376 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001377 if (!nvmeq)
1378 return NULL;
1379
Joe Perches4d51abf2014-06-15 13:37:33 -07001380 nvmeq->cqes = dma_zalloc_coherent(dmadev, CQ_SIZE(depth),
1381 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001382 if (!nvmeq->cqes)
1383 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001384
1385 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
1386 &nvmeq->sq_dma_addr, GFP_KERNEL);
1387 if (!nvmeq->sq_cmds)
1388 goto free_cqdma;
1389
1390 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001391 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001392 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1393 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001394 spin_lock_init(&nvmeq->q_lock);
1395 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001396 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001397 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001398 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001399 nvmeq->qid = qid;
Keith Busch22404272013-07-15 15:02:20 -06001400 dev->queue_count++;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001401 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001402
1403 return nvmeq;
1404
1405 free_cqdma:
Keith Busch68b8eca2013-05-01 13:07:47 -06001406 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001407 nvmeq->cq_dma_addr);
1408 free_nvmeq:
1409 kfree(nvmeq);
1410 return NULL;
1411}
1412
Matthew Wilcox30010822011-01-20 09:10:15 -05001413static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1414 const char *name)
1415{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001416 if (use_threaded_interrupts)
1417 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001418 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001419 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001420 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001421 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001422}
1423
Keith Busch22404272013-07-15 15:02:20 -06001424static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001425{
Keith Busch22404272013-07-15 15:02:20 -06001426 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001427
Keith Busch7be50e92014-09-10 15:48:47 -06001428 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001429 nvmeq->sq_tail = 0;
1430 nvmeq->cq_head = 0;
1431 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001432 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001433 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001434 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001435 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001436}
1437
1438static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1439{
1440 struct nvme_dev *dev = nvmeq->dev;
1441 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001442
Keith Busch2b25d982014-12-22 12:59:04 -07001443 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001444 result = adapter_alloc_cq(dev, qid, nvmeq);
1445 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001446 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001447
1448 result = adapter_alloc_sq(dev, qid, nvmeq);
1449 if (result < 0)
1450 goto release_cq;
1451
Matthew Wilcox3193f072014-01-27 15:57:22 -05001452 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001453 if (result < 0)
1454 goto release_sq;
1455
Keith Busch22404272013-07-15 15:02:20 -06001456 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001457 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001458
1459 release_sq:
1460 adapter_delete_sq(dev, qid);
1461 release_cq:
1462 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001463 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001464}
1465
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001466static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1467{
1468 unsigned long timeout;
1469 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1470
1471 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1472
1473 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1474 msleep(100);
1475 if (fatal_signal_pending(current))
1476 return -EINTR;
1477 if (time_after(jiffies, timeout)) {
1478 dev_err(&dev->pci_dev->dev,
Matthew Wilcox27e81662014-04-11 11:58:45 -04001479 "Device not ready; aborting %s\n", enabled ?
1480 "initialisation" : "reset");
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001481 return -ENODEV;
1482 }
1483 }
1484
1485 return 0;
1486}
1487
1488/*
1489 * If the device has been passed off to us in an enabled state, just clear
1490 * the enabled bit. The spec says we should set the 'shutdown notification
1491 * bits', but doing so may cause the device to complete commands to the
1492 * admin queue ... and we don't know what memory that might be pointing at!
1493 */
1494static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1495{
Dan McLeran01079522014-06-23 08:24:36 -06001496 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1497 dev->ctrl_config &= ~NVME_CC_ENABLE;
1498 writel(dev->ctrl_config, &dev->bar->cc);
Matthew Wilcox44af1462013-05-04 06:43:17 -04001499
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001500 return nvme_wait_ready(dev, cap, false);
1501}
1502
1503static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1504{
Dan McLeran01079522014-06-23 08:24:36 -06001505 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1506 dev->ctrl_config |= NVME_CC_ENABLE;
1507 writel(dev->ctrl_config, &dev->bar->cc);
1508
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001509 return nvme_wait_ready(dev, cap, true);
1510}
1511
Keith Busch1894d8f2013-07-15 15:02:22 -06001512static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1513{
1514 unsigned long timeout;
Keith Busch1894d8f2013-07-15 15:02:22 -06001515
Dan McLeran01079522014-06-23 08:24:36 -06001516 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1517 dev->ctrl_config |= NVME_CC_SHN_NORMAL;
1518
1519 writel(dev->ctrl_config, &dev->bar->cc);
Keith Busch1894d8f2013-07-15 15:02:22 -06001520
Dan McLeran2484f402014-07-01 09:33:32 -06001521 timeout = SHUTDOWN_TIMEOUT + jiffies;
Keith Busch1894d8f2013-07-15 15:02:22 -06001522 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1523 NVME_CSTS_SHST_CMPLT) {
1524 msleep(100);
1525 if (fatal_signal_pending(current))
1526 return -EINTR;
1527 if (time_after(jiffies, timeout)) {
1528 dev_err(&dev->pci_dev->dev,
1529 "Device shutdown incomplete; abort shutdown\n");
1530 return -ENODEV;
1531 }
1532 }
1533
1534 return 0;
1535}
1536
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001537static struct blk_mq_ops nvme_mq_admin_ops = {
1538 .queue_rq = nvme_admin_queue_rq,
1539 .map_queue = blk_mq_map_queue,
1540 .init_hctx = nvme_admin_init_hctx,
Jens Axboe2c30540b2014-11-14 09:47:32 -07001541 .exit_hctx = nvme_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001542 .init_request = nvme_admin_init_request,
1543 .timeout = nvme_timeout,
1544};
1545
1546static struct blk_mq_ops nvme_mq_ops = {
1547 .queue_rq = nvme_queue_rq,
1548 .map_queue = blk_mq_map_queue,
1549 .init_hctx = nvme_init_hctx,
Jens Axboe2c30540b2014-11-14 09:47:32 -07001550 .exit_hctx = nvme_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001551 .init_request = nvme_init_request,
1552 .timeout = nvme_timeout,
1553};
1554
Keith Buschea191d22015-01-07 18:55:49 -07001555static void nvme_dev_remove_admin(struct nvme_dev *dev)
1556{
1557 if (dev->admin_q && !blk_queue_dying(dev->admin_q)) {
1558 blk_cleanup_queue(dev->admin_q);
1559 blk_mq_free_tag_set(&dev->admin_tagset);
1560 }
1561}
1562
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001563static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1564{
1565 if (!dev->admin_q) {
1566 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1567 dev->admin_tagset.nr_hw_queues = 1;
1568 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1;
1569 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
1570 dev->admin_tagset.numa_node = dev_to_node(&dev->pci_dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001571 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001572 dev->admin_tagset.driver_data = dev;
1573
1574 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1575 return -ENOMEM;
1576
1577 dev->admin_q = blk_mq_init_queue(&dev->admin_tagset);
Ming Lei35b489d2015-01-02 14:25:27 +00001578 if (IS_ERR(dev->admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001579 blk_mq_free_tag_set(&dev->admin_tagset);
1580 return -ENOMEM;
1581 }
Keith Buschea191d22015-01-07 18:55:49 -07001582 if (!blk_get_queue(dev->admin_q)) {
1583 nvme_dev_remove_admin(dev);
1584 return -ENODEV;
1585 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001586 } else
1587 blk_mq_unfreeze_queue(dev->admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001588
1589 return 0;
1590}
1591
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001592static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001593{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001594 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001595 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001596 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001597 struct nvme_queue *nvmeq;
Keith Busch1d090622014-06-23 11:34:01 -06001598 unsigned page_shift = PAGE_SHIFT;
1599 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12;
1600 unsigned dev_page_max = NVME_CAP_MPSMAX(cap) + 12;
1601
1602 if (page_shift < dev_page_min) {
1603 dev_err(&dev->pci_dev->dev,
1604 "Minimum device page size (%u) too large for "
1605 "host (%u)\n", 1 << dev_page_min,
1606 1 << page_shift);
1607 return -ENODEV;
1608 }
1609 if (page_shift > dev_page_max) {
1610 dev_info(&dev->pci_dev->dev,
1611 "Device maximum page size (%u) smaller than "
1612 "host (%u); enabling work-around\n",
1613 1 << dev_page_max, 1 << page_shift);
1614 page_shift = dev_page_max;
1615 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001616
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001617 result = nvme_disable_ctrl(dev, cap);
1618 if (result < 0)
1619 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001620
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001621 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001622 if (!nvmeq) {
Keith Busch2b25d982014-12-22 12:59:04 -07001623 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001624 if (!nvmeq)
1625 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001626 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001627
1628 aqa = nvmeq->q_depth - 1;
1629 aqa |= aqa << 16;
1630
Keith Busch1d090622014-06-23 11:34:01 -06001631 dev->page_size = 1 << page_shift;
1632
Dan McLeran01079522014-06-23 08:24:36 -06001633 dev->ctrl_config = NVME_CC_CSS_NVM;
Keith Busch1d090622014-06-23 11:34:01 -06001634 dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001635 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001636 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001637
1638 writel(aqa, &dev->bar->aqa);
1639 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1640 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001641
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001642 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001643 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001644 goto free_nvmeq;
1645
Keith Busch2b25d982014-12-22 12:59:04 -07001646 nvmeq->cq_vector = 0;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001647 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Keith Busch025c5572013-05-01 13:07:51 -06001648 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07001649 goto free_nvmeq;
Keith Busch025c5572013-05-01 13:07:51 -06001650
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001651 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001652
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001653 free_nvmeq:
1654 nvme_free_queues(dev, 0);
1655 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001656}
1657
Vishal Verma5d0f6132013-03-04 18:40:58 -07001658struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001659 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001660{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001661 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001662 struct scatterlist *sg;
1663 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001664 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001665
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001666 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001667 return ERR_PTR(-EINVAL);
Dan Carpenter5460fc02013-05-13 17:59:50 +03001668 if (!length || length > INT_MAX - PAGE_SIZE)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001669 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001670
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001671 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001672 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1673 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001674 if (!pages)
1675 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001676
1677 err = get_user_pages_fast(addr, count, 1, pages);
1678 if (err < count) {
1679 count = err;
1680 err = -EFAULT;
1681 goto put_pages;
1682 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001683
Santosh Y6808c5f2014-05-29 10:01:52 +05301684 err = -ENOMEM;
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001685 iod = __nvme_alloc_iod(count, length, dev, 0, GFP_KERNEL);
Santosh Y6808c5f2014-05-29 10:01:52 +05301686 if (!iod)
1687 goto put_pages;
1688
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001689 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001690 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001691 for (i = 0; i < count; i++) {
1692 sg_set_page(&sg[i], pages[i],
Dan Carpenter5460fc02013-05-13 17:59:50 +03001693 min_t(unsigned, length, PAGE_SIZE - offset),
1694 offset);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001695 length -= (PAGE_SIZE - offset);
1696 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001697 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001698 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001699 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001700
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001701 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1702 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001703 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001704 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001705
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001706 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001707 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001708
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001709 free_iod:
1710 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001711 put_pages:
1712 for (i = 0; i < count; i++)
1713 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001714 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001715 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001716}
1717
Vishal Verma5d0f6132013-03-04 18:40:58 -07001718void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001719 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001720{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001721 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001722
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001723 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1724 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001725
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001726 for (i = 0; i < iod->nents; i++)
1727 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001728}
1729
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001730static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1731{
1732 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001733 struct nvme_user_io io;
1734 struct nvme_command c;
Keith Buschf410c682013-04-23 17:23:59 -06001735 unsigned length, meta_len;
1736 int status, i;
1737 struct nvme_iod *iod, *meta_iod = NULL;
1738 dma_addr_t meta_dma_addr;
1739 void *meta, *uninitialized_var(meta_mem);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001740
1741 if (copy_from_user(&io, uio, sizeof(io)))
1742 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001743 length = (io.nblocks + 1) << ns->lba_shift;
Keith Buschf410c682013-04-23 17:23:59 -06001744 meta_len = (io.nblocks + 1) * ns->ms;
1745
1746 if (meta_len && ((io.metadata & 3) || !io.metadata))
1747 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001748
1749 switch (io.opcode) {
1750 case nvme_cmd_write:
1751 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001752 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001753 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001754 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001755 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001756 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001757 }
1758
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001759 if (IS_ERR(iod))
1760 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001761
1762 memset(&c, 0, sizeof(c));
1763 c.rw.opcode = io.opcode;
1764 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001765 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001766 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001767 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001768 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001769 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1770 c.rw.reftag = cpu_to_le32(io.reftag);
1771 c.rw.apptag = cpu_to_le16(io.apptag);
1772 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buschf410c682013-04-23 17:23:59 -06001773
1774 if (meta_len) {
Keith Busch1b567492013-07-18 12:13:51 -06001775 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
1776 meta_len);
Keith Buschf410c682013-04-23 17:23:59 -06001777 if (IS_ERR(meta_iod)) {
1778 status = PTR_ERR(meta_iod);
1779 meta_iod = NULL;
1780 goto unmap;
1781 }
1782
1783 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1784 &meta_dma_addr, GFP_KERNEL);
1785 if (!meta_mem) {
1786 status = -ENOMEM;
1787 goto unmap;
1788 }
1789
1790 if (io.opcode & 1) {
1791 int meta_offset = 0;
1792
1793 for (i = 0; i < meta_iod->nents; i++) {
1794 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1795 meta_iod->sg[i].offset;
1796 memcpy(meta_mem + meta_offset, meta,
1797 meta_iod->sg[i].length);
1798 kunmap_atomic(meta);
1799 meta_offset += meta_iod->sg[i].length;
1800 }
1801 }
1802
1803 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1804 }
1805
Keith Buschedd10d32014-04-03 16:45:23 -06001806 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1807 c.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1808 c.rw.prp2 = cpu_to_le64(iod->first_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001809
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001810 if (length != (io.nblocks + 1) << ns->lba_shift)
1811 status = -ENOMEM;
1812 else
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001813 status = nvme_submit_io_cmd(dev, ns, &c, NULL);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001814
Keith Buschf410c682013-04-23 17:23:59 -06001815 if (meta_len) {
1816 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1817 int meta_offset = 0;
1818
1819 for (i = 0; i < meta_iod->nents; i++) {
1820 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1821 meta_iod->sg[i].offset;
1822 memcpy(meta, meta_mem + meta_offset,
1823 meta_iod->sg[i].length);
1824 kunmap_atomic(meta);
1825 meta_offset += meta_iod->sg[i].length;
1826 }
1827 }
1828
1829 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1830 meta_dma_addr);
1831 }
1832
1833 unmap:
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001834 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001835 nvme_free_iod(dev, iod);
Keith Buschf410c682013-04-23 17:23:59 -06001836
1837 if (meta_iod) {
1838 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1839 nvme_free_iod(dev, meta_iod);
1840 }
1841
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001842 return status;
1843}
1844
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001845static int nvme_user_cmd(struct nvme_dev *dev, struct nvme_ns *ns,
1846 struct nvme_passthru_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001847{
Keith Busch7963e522014-09-12 16:07:20 -06001848 struct nvme_passthru_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001849 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001850 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001851 struct nvme_iod *uninitialized_var(iod);
Keith Busch94f370c2013-05-09 14:01:38 -06001852 unsigned timeout;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001853
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001854 if (!capable(CAP_SYS_ADMIN))
1855 return -EACCES;
1856 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001857 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001858
1859 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001860 c.common.opcode = cmd.opcode;
1861 c.common.flags = cmd.flags;
1862 c.common.nsid = cpu_to_le32(cmd.nsid);
1863 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1864 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1865 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1866 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1867 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1868 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1869 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1870 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1871
1872 length = cmd.data_len;
1873 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001874 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1875 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001876 if (IS_ERR(iod))
1877 return PTR_ERR(iod);
Keith Buschedd10d32014-04-03 16:45:23 -06001878 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1879 c.common.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1880 c.common.prp2 = cpu_to_le64(iod->first_dma);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001881 }
1882
Keith Busch94f370c2013-05-09 14:01:38 -06001883 timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
1884 ADMIN_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001885
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001886 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001887 status = -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001888 else if (ns) {
1889 struct request *req;
1890
1891 req = blk_mq_alloc_request(ns->queue, WRITE,
1892 (GFP_KERNEL|__GFP_WAIT), false);
Jens Axboe97fe3832014-12-10 13:02:44 -07001893 if (IS_ERR(req))
1894 status = PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001895 else {
1896 status = nvme_submit_sync_cmd(req, &c, &cmd.result,
1897 timeout);
Jens Axboe9d135bb2014-11-17 10:43:42 -07001898 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001899 }
1900 } else
1901 status = __nvme_submit_admin_cmd(dev, &c, &cmd.result, timeout);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001902
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001903 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001904 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001905 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001906 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001907
Chayan Biswascf90bc42013-05-22 22:34:49 +00001908 if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
Keith Buschf4f117f2012-09-21 10:49:05 -06001909 sizeof(cmd.result)))
1910 status = -EFAULT;
1911
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001912 return status;
1913}
1914
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001915static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1916 unsigned long arg)
1917{
1918 struct nvme_ns *ns = bdev->bd_disk->private_data;
1919
1920 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001921 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001922 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001923 return ns->ns_id;
1924 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001925 return nvme_user_cmd(ns->dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06001926 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001927 return nvme_user_cmd(ns->dev, ns, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001928 case NVME_IOCTL_SUBMIT_IO:
1929 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001930 case SG_GET_VERSION_NUM:
1931 return nvme_sg_get_version_num((void __user *)arg);
1932 case SG_IO:
1933 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001934 default:
1935 return -ENOTTY;
1936 }
1937}
1938
Keith Busch320a3822013-10-23 13:07:34 -06001939#ifdef CONFIG_COMPAT
1940static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1941 unsigned int cmd, unsigned long arg)
1942{
Keith Busch320a3822013-10-23 13:07:34 -06001943 switch (cmd) {
1944 case SG_IO:
Keith Busche1797292014-08-27 13:55:38 -06001945 return -ENOIOCTLCMD;
Keith Busch320a3822013-10-23 13:07:34 -06001946 }
1947 return nvme_ioctl(bdev, mode, cmd, arg);
1948}
1949#else
1950#define nvme_compat_ioctl NULL
1951#endif
1952
Keith Busch9ac27092014-01-31 16:53:39 -07001953static int nvme_open(struct block_device *bdev, fmode_t mode)
1954{
Keith Busch9e603522014-10-03 11:15:47 -06001955 int ret = 0;
1956 struct nvme_ns *ns;
Keith Busch9ac27092014-01-31 16:53:39 -07001957
Keith Busch9e603522014-10-03 11:15:47 -06001958 spin_lock(&dev_list_lock);
1959 ns = bdev->bd_disk->private_data;
1960 if (!ns)
1961 ret = -ENXIO;
1962 else if (!kref_get_unless_zero(&ns->dev->kref))
1963 ret = -ENXIO;
1964 spin_unlock(&dev_list_lock);
1965
1966 return ret;
Keith Busch9ac27092014-01-31 16:53:39 -07001967}
1968
1969static void nvme_free_dev(struct kref *kref);
1970
1971static void nvme_release(struct gendisk *disk, fmode_t mode)
1972{
1973 struct nvme_ns *ns = disk->private_data;
1974 struct nvme_dev *dev = ns->dev;
1975
1976 kref_put(&dev->kref, nvme_free_dev);
1977}
1978
Keith Busch4cc09e22014-04-02 15:45:37 -06001979static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
1980{
1981 /* some standard values */
1982 geo->heads = 1 << 6;
1983 geo->sectors = 1 << 5;
1984 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1985 return 0;
1986}
1987
Keith Busche1e5e562015-02-19 13:39:03 -07001988static void nvme_config_discard(struct nvme_ns *ns)
1989{
1990 u32 logical_block_size = queue_logical_block_size(ns->queue);
1991 ns->queue->limits.discard_zeroes_data = 0;
1992 ns->queue->limits.discard_alignment = logical_block_size;
1993 ns->queue->limits.discard_granularity = logical_block_size;
1994 ns->queue->limits.max_discard_sectors = 0xffffffff;
1995 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1996}
1997
1998static int nvme_noop_verify(struct blk_integrity_iter *iter)
1999{
2000 return 0;
2001}
2002
2003static int nvme_noop_generate(struct blk_integrity_iter *iter)
2004{
2005 return 0;
2006}
2007
2008struct blk_integrity nvme_meta_noop = {
2009 .name = "NVME_META_NOOP",
2010 .generate_fn = nvme_noop_generate,
2011 .verify_fn = nvme_noop_verify,
2012};
2013
2014static void nvme_init_integrity(struct nvme_ns *ns)
2015{
2016 struct blk_integrity integrity;
2017
2018 switch (ns->pi_type) {
2019 case NVME_NS_DPS_PI_TYPE3:
2020 integrity = t10_pi_type3_crc;
2021 break;
2022 case NVME_NS_DPS_PI_TYPE1:
2023 case NVME_NS_DPS_PI_TYPE2:
2024 integrity = t10_pi_type1_crc;
2025 break;
2026 default:
2027 integrity = nvme_meta_noop;
2028 break;
2029 }
2030 integrity.tuple_size = ns->ms;
2031 blk_integrity_register(ns->disk, &integrity);
2032 blk_queue_max_integrity_segments(ns->queue, 1);
2033}
2034
Keith Busch1b9dbf72014-09-10 17:21:14 -06002035static int nvme_revalidate_disk(struct gendisk *disk)
2036{
2037 struct nvme_ns *ns = disk->private_data;
2038 struct nvme_dev *dev = ns->dev;
2039 struct nvme_id_ns *id;
2040 dma_addr_t dma_addr;
Keith Busche1e5e562015-02-19 13:39:03 -07002041 int lbaf, pi_type, old_ms;
2042 unsigned short bs;
Keith Busch1b9dbf72014-09-10 17:21:14 -06002043
2044 id = dma_alloc_coherent(&dev->pci_dev->dev, 4096, &dma_addr,
2045 GFP_KERNEL);
2046 if (!id) {
2047 dev_warn(&dev->pci_dev->dev, "%s: Memory alocation failure\n",
2048 __func__);
2049 return 0;
2050 }
Keith Busche1e5e562015-02-19 13:39:03 -07002051 if (nvme_identify(dev, ns->ns_id, 0, dma_addr)) {
2052 dev_warn(&dev->pci_dev->dev,
2053 "identify failed ns:%d, setting capacity to 0\n",
2054 ns->ns_id);
2055 memset(id, 0, sizeof(*id));
2056 }
Keith Busch1b9dbf72014-09-10 17:21:14 -06002057
Keith Busche1e5e562015-02-19 13:39:03 -07002058 old_ms = ns->ms;
2059 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
Keith Busch1b9dbf72014-09-10 17:21:14 -06002060 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Busche1e5e562015-02-19 13:39:03 -07002061 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busch1b9dbf72014-09-10 17:21:14 -06002062
Keith Busche1e5e562015-02-19 13:39:03 -07002063 /*
2064 * If identify namespace failed, use default 512 byte block size so
2065 * block layer can use before failing read/write for 0 capacity.
2066 */
2067 if (ns->lba_shift == 0)
2068 ns->lba_shift = 9;
2069 bs = 1 << ns->lba_shift;
2070
2071 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
2072 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
2073 id->dps & NVME_NS_DPS_PI_MASK : 0;
2074
2075 if (disk->integrity && (ns->pi_type != pi_type || ns->ms != old_ms ||
2076 bs != queue_logical_block_size(disk->queue) ||
2077 (ns->ms && id->flbas & NVME_NS_FLBAS_META_EXT)))
2078 blk_integrity_unregister(disk);
2079
2080 ns->pi_type = pi_type;
2081 blk_queue_logical_block_size(ns->queue, bs);
2082
2083 if (ns->ms && !disk->integrity && (disk->flags & GENHD_FL_UP) &&
2084 !(id->flbas & NVME_NS_FLBAS_META_EXT))
2085 nvme_init_integrity(ns);
2086
2087 if (id->ncap == 0 || (ns->ms && !disk->integrity))
2088 set_capacity(disk, 0);
2089 else
2090 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
2091
2092 if (dev->oncs & NVME_CTRL_ONCS_DSM)
2093 nvme_config_discard(ns);
2094
Keith Busch1b9dbf72014-09-10 17:21:14 -06002095 dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr);
2096 return 0;
2097}
2098
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002099static const struct block_device_operations nvme_fops = {
2100 .owner = THIS_MODULE,
2101 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06002102 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07002103 .open = nvme_open,
2104 .release = nvme_release,
Keith Busch4cc09e22014-04-02 15:45:37 -06002105 .getgeo = nvme_getgeo,
Keith Busch1b9dbf72014-09-10 17:21:14 -06002106 .revalidate_disk= nvme_revalidate_disk,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002107};
2108
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002109static int nvme_kthread(void *data)
2110{
Keith Buschd4b4ff82013-12-10 13:10:37 -07002111 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002112
2113 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04002114 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002115 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07002116 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002117 int i;
Keith Buschd4b4ff82013-12-10 13:10:37 -07002118 if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
2119 dev->initialized) {
2120 if (work_busy(&dev->reset_work))
2121 continue;
2122 list_del_init(&dev->node);
2123 dev_warn(&dev->pci_dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002124 "Failed status: %x, reset controller\n",
2125 readl(&dev->bar->csts));
Tejun Heo9ca97372014-03-07 10:24:49 -05002126 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschd4b4ff82013-12-10 13:10:37 -07002127 queue_work(nvme_workq, &dev->reset_work);
2128 continue;
2129 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002130 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002131 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05002132 if (!nvmeq)
2133 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002134 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04002135 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06002136
2137 while ((i == 0) && (dev->event_limit > 0)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002138 if (nvme_submit_async_admin_req(dev))
Keith Busch6fccf932014-06-18 13:58:57 -06002139 break;
2140 dev->event_limit--;
2141 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002142 spin_unlock_irq(&nvmeq->q_lock);
2143 }
2144 }
2145 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08002146 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002147 }
2148 return 0;
2149}
2150
Keith Busche1e5e562015-02-19 13:39:03 -07002151static void nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002152{
2153 struct nvme_ns *ns;
2154 struct gendisk *disk;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002155 int node = dev_to_node(&dev->pci_dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002156
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002157 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002158 if (!ns)
Keith Busche1e5e562015-02-19 13:39:03 -07002159 return;
2160
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002161 ns->queue = blk_mq_init_queue(&dev->tagset);
Dan Carpenter9f173b32014-11-05 23:39:09 +03002162 if (IS_ERR(ns->queue))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002163 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07002164 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
2165 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002166 queue_flag_set_unlocked(QUEUE_FLAG_SG_GAPS, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002167 ns->dev = dev;
2168 ns->queue->queuedata = ns;
2169
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002170 disk = alloc_disk_node(0, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002171 if (!disk)
2172 goto out_free_queue;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002173
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002174 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002175 ns->disk = disk;
Keith Busche1e5e562015-02-19 13:39:03 -07002176 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
2177 list_add_tail(&ns->list, &dev->namespaces);
2178
Keith Busche9ef4632012-07-24 15:01:04 -06002179 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06002180 if (dev->max_hw_sectors)
2181 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002182 if (dev->stripe_size)
2183 blk_queue_chunk_sectors(ns->queue, dev->stripe_size >> 9);
Keith Buscha7d2ce22014-04-29 11:41:28 -06002184 if (dev->vwc & NVME_CTRL_VWC_PRESENT)
2185 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002186
2187 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002188 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002189 disk->fops = &nvme_fops;
2190 disk->private_data = ns;
2191 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05002192 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002193 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002194 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002195
Keith Busche1e5e562015-02-19 13:39:03 -07002196 /*
2197 * Initialize capacity to 0 until we establish the namespace format and
2198 * setup integrity extentions if necessary. The revalidate_disk after
2199 * add_disk allows the driver to register with integrity if the format
2200 * requires it.
2201 */
2202 set_capacity(disk, 0);
2203 nvme_revalidate_disk(ns->disk);
2204 add_disk(ns->disk);
2205 if (ns->ms)
2206 revalidate_disk(ns->disk);
2207 return;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002208 out_free_queue:
2209 blk_cleanup_queue(ns->queue);
2210 out_free_ns:
2211 kfree(ns);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002212}
2213
Keith Busch42f61422014-03-24 10:46:25 -06002214static void nvme_create_io_queues(struct nvme_dev *dev)
2215{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002216 unsigned i;
Keith Busch42f61422014-03-24 10:46:25 -06002217
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002218 for (i = dev->queue_count; i <= dev->max_qid; i++)
Keith Busch2b25d982014-12-22 12:59:04 -07002219 if (!nvme_alloc_queue(dev, i, dev->q_depth))
Keith Busch42f61422014-03-24 10:46:25 -06002220 break;
2221
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002222 for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
2223 if (nvme_create_queue(dev->queues[i], i))
Keith Busch42f61422014-03-24 10:46:25 -06002224 break;
2225}
2226
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002227static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002228{
2229 int status;
2230 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002231 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002232
Matthew Wilcoxdf348132012-01-11 07:29:56 -07002233 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002234 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04002235 if (status < 0)
2236 return status;
2237 if (status > 0) {
2238 dev_err(&dev->pci_dev->dev, "Could not set queue count (%d)\n",
2239 status);
Keith Buschbadc34d2014-06-23 14:25:35 -06002240 return 0;
Matthew Wilcox27e81662014-04-11 11:58:45 -04002241 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002242 return min(result & 0xffff, result >> 16) + 1;
2243}
2244
Keith Busch9d713c22013-07-15 15:02:24 -06002245static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
2246{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002247 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06002248}
2249
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002250static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002251{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002252 struct nvme_queue *adminq = dev->queues[0];
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002253 struct pci_dev *pdev = dev->pci_dev;
Keith Busch42f61422014-03-24 10:46:25 -06002254 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002255
Keith Busch42f61422014-03-24 10:46:25 -06002256 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002257 result = set_queue_count(dev, nr_io_queues);
Keith Buschbadc34d2014-06-23 14:25:35 -06002258 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002259 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002260 if (result < nr_io_queues)
2261 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002262
Keith Busch9d713c22013-07-15 15:02:24 -06002263 size = db_bar_size(dev, nr_io_queues);
2264 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002265 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06002266 do {
2267 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
2268 if (dev->bar)
2269 break;
2270 if (!--nr_io_queues)
2271 return -ENOMEM;
2272 size = db_bar_size(dev, nr_io_queues);
2273 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002274 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07002275 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002276 }
2277
Keith Busch9d713c22013-07-15 15:02:24 -06002278 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002279 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002280
Jens Axboee32efbf2014-11-14 09:49:26 -07002281 /*
2282 * If we enable msix early due to not intx, disable it again before
2283 * setting up the full range we need.
2284 */
2285 if (!pdev->irq)
2286 pci_disable_msix(pdev);
2287
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002288 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002289 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002290 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
2291 if (vecs < 0) {
2292 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
2293 if (vecs < 0) {
2294 vecs = 1;
2295 } else {
2296 for (i = 0; i < vecs; i++)
2297 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002298 }
2299 }
2300
Matthew Wilcox063a8092013-06-20 10:53:48 -04002301 /*
2302 * Should investigate if there's a performance win from allocating
2303 * more queues than interrupt vectors; it might allow the submission
2304 * path to scale better, even if the receive path is limited by the
2305 * number of interrupts.
2306 */
2307 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002308 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002309
Matthew Wilcox3193f072014-01-27 15:57:22 -05002310 result = queue_request_irq(dev, adminq, adminq->irqname);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002311 if (result)
Keith Busch22404272013-07-15 15:02:20 -06002312 goto free_queues;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002313
Keith Buschcd638942013-07-15 15:02:23 -06002314 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002315 nvme_free_queues(dev, nr_io_queues + 1);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002316 nvme_create_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06002317
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002318 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002319
Keith Busch22404272013-07-15 15:02:20 -06002320 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002321 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002322 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002323}
2324
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002325/*
2326 * Return: error value if an error occurred setting up the queues or calling
2327 * Identify Device. 0 if these succeeded, even if adding some of the
2328 * namespaces failed. At the moment, these failures are silent. TBD which
2329 * failures should be reported.
2330 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002331static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002332{
Matthew Wilcox68608c262013-06-21 14:36:34 -04002333 struct pci_dev *pdev = dev->pci_dev;
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002334 int res;
2335 unsigned nn, i;
Matthew Wilcox51814232011-02-01 16:18:08 -05002336 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002337 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002338 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06002339 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002340
Keith Busche1e5e562015-02-19 13:39:03 -07002341 mem = dma_alloc_coherent(&pdev->dev, 4096, &dma_addr, GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06002342 if (!mem)
2343 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002344
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002345 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002346 if (res) {
Matthew Wilcox27e81662014-04-11 11:58:45 -04002347 dev_err(&pdev->dev, "Identify Controller failed (%d)\n", res);
Keith Busche1e5e562015-02-19 13:39:03 -07002348 dma_free_coherent(&dev->pci_dev->dev, 4096, mem, dma_addr);
2349 return -EIO;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002350 }
2351
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002352 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05002353 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002354 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002355 dev->abort_limit = ctrl->acl + 1;
Keith Buscha7d2ce22014-04-29 11:41:28 -06002356 dev->vwc = ctrl->vwc;
Keith Busch6fccf932014-06-18 13:58:57 -06002357 dev->event_limit = min(ctrl->aerl + 1, 8);
Matthew Wilcox51814232011-02-01 16:18:08 -05002358 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2359 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2360 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002361 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002362 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002363 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002364 (pdev->device == 0x0953) && ctrl->vs[3]) {
2365 unsigned int max_hw_sectors;
2366
Keith Busch159b67d2013-04-09 17:13:20 -06002367 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002368 max_hw_sectors = dev->stripe_size >> (shift - 9);
2369 if (dev->max_hw_sectors) {
2370 dev->max_hw_sectors = min(max_hw_sectors,
2371 dev->max_hw_sectors);
2372 } else
2373 dev->max_hw_sectors = max_hw_sectors;
2374 }
Keith Busche1e5e562015-02-19 13:39:03 -07002375 dma_free_coherent(&dev->pci_dev->dev, 4096, mem, dma_addr);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002376
2377 dev->tagset.ops = &nvme_mq_ops;
2378 dev->tagset.nr_hw_queues = dev->online_queues - 1;
2379 dev->tagset.timeout = NVME_IO_TIMEOUT;
2380 dev->tagset.numa_node = dev_to_node(&dev->pci_dev->dev);
2381 dev->tagset.queue_depth =
2382 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Jens Axboeac3dd5b2015-01-22 12:07:58 -07002383 dev->tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002384 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
2385 dev->tagset.driver_data = dev;
2386
2387 if (blk_mq_alloc_tag_set(&dev->tagset))
Keith Busche1e5e562015-02-19 13:39:03 -07002388 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002389
Keith Busche1e5e562015-02-19 13:39:03 -07002390 for (i = 1; i <= nn; i++)
2391 nvme_alloc_ns(dev, i);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002392
Keith Busche1e5e562015-02-19 13:39:03 -07002393 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002394}
2395
Keith Busch0877cb02013-07-15 15:02:19 -06002396static int nvme_dev_map(struct nvme_dev *dev)
2397{
Keith Busch42f61422014-03-24 10:46:25 -06002398 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002399 int bars, result = -ENOMEM;
2400 struct pci_dev *pdev = dev->pci_dev;
2401
2402 if (pci_enable_device_mem(pdev))
2403 return result;
2404
2405 dev->entry[0].vector = pdev->irq;
2406 pci_set_master(pdev);
2407 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07002408 if (!bars)
2409 goto disable_pci;
2410
Keith Busch0877cb02013-07-15 15:02:19 -06002411 if (pci_request_selected_regions(pdev, bars, "nvme"))
2412 goto disable_pci;
2413
Russell King052d0ef2013-06-26 23:49:11 +01002414 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
2415 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2416 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002417
Keith Busch0877cb02013-07-15 15:02:19 -06002418 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2419 if (!dev->bar)
2420 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07002421
Keith Busch0e53d182013-12-10 13:10:39 -07002422 if (readl(&dev->bar->csts) == -1) {
2423 result = -ENODEV;
2424 goto unmap;
2425 }
Jens Axboee32efbf2014-11-14 09:49:26 -07002426
2427 /*
2428 * Some devices don't advertse INTx interrupts, pre-enable a single
2429 * MSIX vec for setup. We'll adjust this later.
2430 */
2431 if (!pdev->irq) {
2432 result = pci_enable_msix(pdev, dev->entry, 1);
2433 if (result < 0)
2434 goto unmap;
2435 }
2436
Keith Busch42f61422014-03-24 10:46:25 -06002437 cap = readq(&dev->bar->cap);
2438 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2439 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Keith Busch0877cb02013-07-15 15:02:19 -06002440 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2441
2442 return 0;
2443
Keith Busch0e53d182013-12-10 13:10:39 -07002444 unmap:
2445 iounmap(dev->bar);
2446 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002447 disable:
2448 pci_release_regions(pdev);
2449 disable_pci:
2450 pci_disable_device(pdev);
2451 return result;
2452}
2453
2454static void nvme_dev_unmap(struct nvme_dev *dev)
2455{
2456 if (dev->pci_dev->msi_enabled)
2457 pci_disable_msi(dev->pci_dev);
2458 else if (dev->pci_dev->msix_enabled)
2459 pci_disable_msix(dev->pci_dev);
2460
2461 if (dev->bar) {
2462 iounmap(dev->bar);
2463 dev->bar = NULL;
Keith Busch9a6b9452013-12-10 13:10:36 -07002464 pci_release_regions(dev->pci_dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002465 }
2466
Keith Busch0877cb02013-07-15 15:02:19 -06002467 if (pci_is_enabled(dev->pci_dev))
2468 pci_disable_device(dev->pci_dev);
2469}
2470
Keith Busch4d115422013-12-10 13:10:40 -07002471struct nvme_delq_ctx {
2472 struct task_struct *waiter;
2473 struct kthread_worker *worker;
2474 atomic_t refcount;
2475};
2476
2477static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2478{
2479 dq->waiter = current;
2480 mb();
2481
2482 for (;;) {
2483 set_current_state(TASK_KILLABLE);
2484 if (!atomic_read(&dq->refcount))
2485 break;
2486 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2487 fatal_signal_pending(current)) {
Keith Busch0fb59cb2015-01-07 18:55:50 -07002488 /*
2489 * Disable the controller first since we can't trust it
2490 * at this point, but leave the admin queue enabled
2491 * until all queue deletion requests are flushed.
2492 * FIXME: This may take a while if there are more h/w
2493 * queues than admin tags.
2494 */
Keith Busch4d115422013-12-10 13:10:40 -07002495 set_current_state(TASK_RUNNING);
Keith Busch4d115422013-12-10 13:10:40 -07002496 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
Keith Busch0fb59cb2015-01-07 18:55:50 -07002497 nvme_clear_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07002498 flush_kthread_worker(dq->worker);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002499 nvme_disable_queue(dev, 0);
Keith Busch4d115422013-12-10 13:10:40 -07002500 return;
2501 }
2502 }
2503 set_current_state(TASK_RUNNING);
2504}
2505
2506static void nvme_put_dq(struct nvme_delq_ctx *dq)
2507{
2508 atomic_dec(&dq->refcount);
2509 if (dq->waiter)
2510 wake_up_process(dq->waiter);
2511}
2512
2513static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2514{
2515 atomic_inc(&dq->refcount);
2516 return dq;
2517}
2518
2519static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2520{
2521 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
2522
2523 nvme_clear_queue(nvmeq);
2524 nvme_put_dq(dq);
2525}
2526
2527static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2528 kthread_work_func_t fn)
2529{
2530 struct nvme_command c;
2531
2532 memset(&c, 0, sizeof(c));
2533 c.delete_queue.opcode = opcode;
2534 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2535
2536 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002537 return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
2538 ADMIN_TIMEOUT);
Keith Busch4d115422013-12-10 13:10:40 -07002539}
2540
2541static void nvme_del_cq_work_handler(struct kthread_work *work)
2542{
2543 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2544 cmdinfo.work);
2545 nvme_del_queue_end(nvmeq);
2546}
2547
2548static int nvme_delete_cq(struct nvme_queue *nvmeq)
2549{
2550 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2551 nvme_del_cq_work_handler);
2552}
2553
2554static void nvme_del_sq_work_handler(struct kthread_work *work)
2555{
2556 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2557 cmdinfo.work);
2558 int status = nvmeq->cmdinfo.status;
2559
2560 if (!status)
2561 status = nvme_delete_cq(nvmeq);
2562 if (status)
2563 nvme_del_queue_end(nvmeq);
2564}
2565
2566static int nvme_delete_sq(struct nvme_queue *nvmeq)
2567{
2568 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2569 nvme_del_sq_work_handler);
2570}
2571
2572static void nvme_del_queue_start(struct kthread_work *work)
2573{
2574 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2575 cmdinfo.work);
Keith Busch4d115422013-12-10 13:10:40 -07002576 if (nvme_delete_sq(nvmeq))
2577 nvme_del_queue_end(nvmeq);
2578}
2579
2580static void nvme_disable_io_queues(struct nvme_dev *dev)
2581{
2582 int i;
2583 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2584 struct nvme_delq_ctx dq;
2585 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2586 &worker, "nvme%d", dev->instance);
2587
2588 if (IS_ERR(kworker_task)) {
2589 dev_err(&dev->pci_dev->dev,
2590 "Failed to create queue del task\n");
2591 for (i = dev->queue_count - 1; i > 0; i--)
2592 nvme_disable_queue(dev, i);
2593 return;
2594 }
2595
2596 dq.waiter = NULL;
2597 atomic_set(&dq.refcount, 0);
2598 dq.worker = &worker;
2599 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002600 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002601
2602 if (nvme_suspend_queue(nvmeq))
2603 continue;
2604 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2605 nvmeq->cmdinfo.worker = dq.worker;
2606 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2607 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2608 }
2609 nvme_wait_dq(&dq, dev);
2610 kthread_stop(kworker_task);
2611}
2612
Dan McLeranb9afca32014-04-07 17:10:11 -06002613/*
2614* Remove the node from the device list and check
2615* for whether or not we need to stop the nvme_thread.
2616*/
2617static void nvme_dev_list_remove(struct nvme_dev *dev)
2618{
2619 struct task_struct *tmp = NULL;
2620
2621 spin_lock(&dev_list_lock);
2622 list_del_init(&dev->node);
2623 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2624 tmp = nvme_thread;
2625 nvme_thread = NULL;
2626 }
2627 spin_unlock(&dev_list_lock);
2628
2629 if (tmp)
2630 kthread_stop(tmp);
2631}
2632
Keith Buschc9d3bf82015-01-07 18:55:52 -07002633static void nvme_freeze_queues(struct nvme_dev *dev)
2634{
2635 struct nvme_ns *ns;
2636
2637 list_for_each_entry(ns, &dev->namespaces, list) {
2638 blk_mq_freeze_queue_start(ns->queue);
2639
2640 spin_lock(ns->queue->queue_lock);
2641 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
2642 spin_unlock(ns->queue->queue_lock);
2643
2644 blk_mq_cancel_requeue_work(ns->queue);
2645 blk_mq_stop_hw_queues(ns->queue);
2646 }
2647}
2648
2649static void nvme_unfreeze_queues(struct nvme_dev *dev)
2650{
2651 struct nvme_ns *ns;
2652
2653 list_for_each_entry(ns, &dev->namespaces, list) {
2654 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2655 blk_mq_unfreeze_queue(ns->queue);
2656 blk_mq_start_stopped_hw_queues(ns->queue, true);
2657 blk_mq_kick_requeue_list(ns->queue);
2658 }
2659}
2660
Keith Buschf0b50732013-07-15 15:02:21 -06002661static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002662{
Keith Busch22404272013-07-15 15:02:20 -06002663 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06002664 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06002665
Keith Buschd4b4ff82013-12-10 13:10:37 -07002666 dev->initialized = 0;
Dan McLeranb9afca32014-04-07 17:10:11 -06002667 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002668
Keith Buschc9d3bf82015-01-07 18:55:52 -07002669 if (dev->bar) {
2670 nvme_freeze_queues(dev);
Keith Busch7c1b2452014-06-25 11:18:12 -06002671 csts = readl(&dev->bar->csts);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002672 }
Keith Busch7c1b2452014-06-25 11:18:12 -06002673 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07002674 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002675 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002676 nvme_suspend_queue(nvmeq);
2677 nvme_clear_queue(nvmeq);
2678 }
2679 } else {
2680 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002681 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002682 nvme_disable_queue(dev, 0);
2683 }
Keith Buschf0b50732013-07-15 15:02:21 -06002684 nvme_dev_unmap(dev);
2685}
2686
2687static void nvme_dev_remove(struct nvme_dev *dev)
2688{
Keith Busch9ac27092014-01-31 16:53:39 -07002689 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002690
Keith Busch9ac27092014-01-31 16:53:39 -07002691 list_for_each_entry(ns, &dev->namespaces, list) {
Keith Busche1e5e562015-02-19 13:39:03 -07002692 if (ns->disk->flags & GENHD_FL_UP) {
2693 if (ns->disk->integrity)
2694 blk_integrity_unregister(ns->disk);
Keith Busch9ac27092014-01-31 16:53:39 -07002695 del_gendisk(ns->disk);
Keith Busche1e5e562015-02-19 13:39:03 -07002696 }
Keith Buschcef6a942015-01-07 18:55:51 -07002697 if (!blk_queue_dying(ns->queue)) {
2698 blk_mq_abort_requeue_list(ns->queue);
Keith Busch9ac27092014-01-31 16:53:39 -07002699 blk_cleanup_queue(ns->queue);
Keith Buschcef6a942015-01-07 18:55:51 -07002700 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002701 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002702}
2703
Matthew Wilcox091b6092011-02-10 09:56:01 -05002704static int nvme_setup_prp_pools(struct nvme_dev *dev)
2705{
2706 struct device *dmadev = &dev->pci_dev->dev;
2707 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2708 PAGE_SIZE, PAGE_SIZE, 0);
2709 if (!dev->prp_page_pool)
2710 return -ENOMEM;
2711
Matthew Wilcox99802a72011-02-10 10:30:34 -05002712 /* Optimisation for I/Os between 4k and 128k */
2713 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2714 256, 256, 0);
2715 if (!dev->prp_small_pool) {
2716 dma_pool_destroy(dev->prp_page_pool);
2717 return -ENOMEM;
2718 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002719 return 0;
2720}
2721
2722static void nvme_release_prp_pools(struct nvme_dev *dev)
2723{
2724 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002725 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002726}
2727
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002728static DEFINE_IDA(nvme_instance_ida);
2729
2730static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002731{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002732 int instance, error;
2733
2734 do {
2735 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2736 return -ENODEV;
2737
2738 spin_lock(&dev_list_lock);
2739 error = ida_get_new(&nvme_instance_ida, &instance);
2740 spin_unlock(&dev_list_lock);
2741 } while (error == -EAGAIN);
2742
2743 if (error)
2744 return -ENODEV;
2745
2746 dev->instance = instance;
2747 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002748}
2749
2750static void nvme_release_instance(struct nvme_dev *dev)
2751{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002752 spin_lock(&dev_list_lock);
2753 ida_remove(&nvme_instance_ida, dev->instance);
2754 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002755}
2756
Keith Busch9ac27092014-01-31 16:53:39 -07002757static void nvme_free_namespaces(struct nvme_dev *dev)
2758{
2759 struct nvme_ns *ns, *next;
2760
2761 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2762 list_del(&ns->list);
Keith Busch9e603522014-10-03 11:15:47 -06002763
2764 spin_lock(&dev_list_lock);
2765 ns->disk->private_data = NULL;
2766 spin_unlock(&dev_list_lock);
2767
Keith Busch9ac27092014-01-31 16:53:39 -07002768 put_disk(ns->disk);
2769 kfree(ns);
2770 }
2771}
2772
Keith Busch5e82e952013-02-19 10:17:58 -07002773static void nvme_free_dev(struct kref *kref)
2774{
2775 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002776
Keith Buscha96d4f52014-08-19 19:15:59 -06002777 pci_dev_put(dev->pci_dev);
Keith Busch9ac27092014-01-31 16:53:39 -07002778 nvme_free_namespaces(dev);
Indraneel M285dffc2014-12-11 08:24:18 -07002779 nvme_release_instance(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002780 blk_mq_free_tag_set(&dev->tagset);
Keith Buschea191d22015-01-07 18:55:49 -07002781 blk_put_queue(dev->admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002782 kfree(dev->queues);
2783 kfree(dev->entry);
2784 kfree(dev);
2785}
2786
2787static int nvme_dev_open(struct inode *inode, struct file *f)
2788{
2789 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
2790 miscdev);
2791 kref_get(&dev->kref);
2792 f->private_data = dev;
2793 return 0;
2794}
2795
2796static int nvme_dev_release(struct inode *inode, struct file *f)
2797{
2798 struct nvme_dev *dev = f->private_data;
2799 kref_put(&dev->kref, nvme_free_dev);
2800 return 0;
2801}
2802
2803static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2804{
2805 struct nvme_dev *dev = f->private_data;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002806 struct nvme_ns *ns;
2807
Keith Busch5e82e952013-02-19 10:17:58 -07002808 switch (cmd) {
2809 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002810 return nvme_user_cmd(dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06002811 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002812 if (list_empty(&dev->namespaces))
2813 return -ENOTTY;
2814 ns = list_first_entry(&dev->namespaces, struct nvme_ns, list);
2815 return nvme_user_cmd(dev, ns, (void __user *)arg);
Keith Busch5e82e952013-02-19 10:17:58 -07002816 default:
2817 return -ENOTTY;
2818 }
2819}
2820
2821static const struct file_operations nvme_dev_fops = {
2822 .owner = THIS_MODULE,
2823 .open = nvme_dev_open,
2824 .release = nvme_dev_release,
2825 .unlocked_ioctl = nvme_dev_ioctl,
2826 .compat_ioctl = nvme_dev_ioctl,
2827};
2828
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002829static void nvme_set_irq_hints(struct nvme_dev *dev)
2830{
2831 struct nvme_queue *nvmeq;
2832 int i;
2833
2834 for (i = 0; i < dev->online_queues; i++) {
2835 nvmeq = dev->queues[i];
2836
2837 if (!nvmeq->hctx)
2838 continue;
2839
2840 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
2841 nvmeq->hctx->cpumask);
2842 }
2843}
2844
Keith Buschf0b50732013-07-15 15:02:21 -06002845static int nvme_dev_start(struct nvme_dev *dev)
2846{
2847 int result;
Dan McLeranb9afca32014-04-07 17:10:11 -06002848 bool start_thread = false;
Keith Buschf0b50732013-07-15 15:02:21 -06002849
2850 result = nvme_dev_map(dev);
2851 if (result)
2852 return result;
2853
2854 result = nvme_configure_admin_queue(dev);
2855 if (result)
2856 goto unmap;
2857
2858 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002859 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2860 start_thread = true;
2861 nvme_thread = NULL;
2862 }
Keith Buschf0b50732013-07-15 15:02:21 -06002863 list_add(&dev->node, &dev_list);
2864 spin_unlock(&dev_list_lock);
2865
Dan McLeranb9afca32014-04-07 17:10:11 -06002866 if (start_thread) {
2867 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
Keith Busch387caa52014-09-22 13:46:19 -06002868 wake_up_all(&nvme_kthread_wait);
Dan McLeranb9afca32014-04-07 17:10:11 -06002869 } else
2870 wait_event_killable(nvme_kthread_wait, nvme_thread);
2871
2872 if (IS_ERR_OR_NULL(nvme_thread)) {
2873 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2874 goto disable;
2875 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002876
2877 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002878 result = nvme_alloc_admin_tags(dev);
2879 if (result)
2880 goto disable;
Dan McLeranb9afca32014-04-07 17:10:11 -06002881
Keith Buschf0b50732013-07-15 15:02:21 -06002882 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002883 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07002884 goto free_tags;
Keith Buschf0b50732013-07-15 15:02:21 -06002885
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002886 nvme_set_irq_hints(dev);
2887
Keith Buschd82e8bf2013-09-05 14:45:07 -06002888 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002889
Keith Busch0fb59cb2015-01-07 18:55:50 -07002890 free_tags:
2891 nvme_dev_remove_admin(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002892 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002893 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06002894 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002895 unmap:
2896 nvme_dev_unmap(dev);
2897 return result;
2898}
2899
Keith Busch9a6b9452013-12-10 13:10:36 -07002900static int nvme_remove_dead_ctrl(void *arg)
2901{
2902 struct nvme_dev *dev = (struct nvme_dev *)arg;
2903 struct pci_dev *pdev = dev->pci_dev;
2904
2905 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06002906 pci_stop_and_remove_bus_device_locked(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002907 kref_put(&dev->kref, nvme_free_dev);
2908 return 0;
2909}
2910
2911static void nvme_remove_disks(struct work_struct *ws)
2912{
Keith Busch9a6b9452013-12-10 13:10:36 -07002913 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2914
Keith Busch5a92e702014-02-21 14:13:44 -07002915 nvme_free_queues(dev, 1);
Keith Busch302c6722014-07-18 11:40:20 -06002916 nvme_dev_remove(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002917}
2918
2919static int nvme_dev_resume(struct nvme_dev *dev)
2920{
2921 int ret;
2922
2923 ret = nvme_dev_start(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002924 if (ret)
Keith Busch9a6b9452013-12-10 13:10:36 -07002925 return ret;
Keith Buschbadc34d2014-06-23 14:25:35 -06002926 if (dev->online_queues < 2) {
Keith Busch9a6b9452013-12-10 13:10:36 -07002927 spin_lock(&dev_list_lock);
Tejun Heo9ca97372014-03-07 10:24:49 -05002928 dev->reset_workfn = nvme_remove_disks;
Keith Busch9a6b9452013-12-10 13:10:36 -07002929 queue_work(nvme_workq, &dev->reset_work);
2930 spin_unlock(&dev_list_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002931 } else {
2932 nvme_unfreeze_queues(dev);
2933 nvme_set_irq_hints(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002934 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002935 dev->initialized = 1;
Keith Busch9a6b9452013-12-10 13:10:36 -07002936 return 0;
2937}
2938
2939static void nvme_dev_reset(struct nvme_dev *dev)
2940{
2941 nvme_dev_shutdown(dev);
2942 if (nvme_dev_resume(dev)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002943 dev_warn(&dev->pci_dev->dev, "Device failed to resume\n");
Keith Busch9a6b9452013-12-10 13:10:36 -07002944 kref_get(&dev->kref);
2945 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2946 dev->instance))) {
2947 dev_err(&dev->pci_dev->dev,
2948 "Failed to start controller remove task\n");
2949 kref_put(&dev->kref, nvme_free_dev);
2950 }
2951 }
2952}
2953
2954static void nvme_reset_failed_dev(struct work_struct *ws)
2955{
2956 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2957 nvme_dev_reset(dev);
2958}
2959
Tejun Heo9ca97372014-03-07 10:24:49 -05002960static void nvme_reset_workfn(struct work_struct *work)
2961{
2962 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
2963 dev->reset_workfn(work);
2964}
2965
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002966static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002967{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002968 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002969 struct nvme_dev *dev;
2970
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002971 node = dev_to_node(&pdev->dev);
2972 if (node == NUMA_NO_NODE)
2973 set_dev_node(&pdev->dev, 0);
2974
2975 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002976 if (!dev)
2977 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002978 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
2979 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002980 if (!dev->entry)
2981 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002982 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2983 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002984 if (!dev->queues)
2985 goto free;
2986
2987 INIT_LIST_HEAD(&dev->namespaces);
Tejun Heo9ca97372014-03-07 10:24:49 -05002988 dev->reset_workfn = nvme_reset_failed_dev;
2989 INIT_WORK(&dev->reset_work, nvme_reset_workfn);
Keith Buscha96d4f52014-08-19 19:15:59 -06002990 dev->pci_dev = pci_dev_get(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002991 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002992 result = nvme_set_instance(dev);
2993 if (result)
Keith Buscha96d4f52014-08-19 19:15:59 -06002994 goto put_pci;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002995
Matthew Wilcox091b6092011-02-10 09:56:01 -05002996 result = nvme_setup_prp_pools(dev);
2997 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002998 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002999
Keith Buschfb35e912014-03-03 11:09:47 -07003000 kref_init(&dev->kref);
Keith Buschf0b50732013-07-15 15:02:21 -06003001 result = nvme_dev_start(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06003002 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06003003 goto release_pools;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003004
Keith Buschbadc34d2014-06-23 14:25:35 -06003005 if (dev->online_queues > 1)
3006 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06003007 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06003008 goto shutdown;
Matthew Wilcox740216f2011-02-15 16:28:20 -05003009
Keith Busch5e82e952013-02-19 10:17:58 -07003010 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
3011 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
3012 dev->miscdev.parent = &pdev->dev;
3013 dev->miscdev.name = dev->name;
3014 dev->miscdev.fops = &nvme_dev_fops;
3015 result = misc_register(&dev->miscdev);
3016 if (result)
3017 goto remove;
3018
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003019 nvme_set_irq_hints(dev);
3020
Keith Buschd4b4ff82013-12-10 13:10:37 -07003021 dev->initialized = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003022 return 0;
3023
Keith Busch5e82e952013-02-19 10:17:58 -07003024 remove:
3025 nvme_dev_remove(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003026 nvme_dev_remove_admin(dev);
Keith Busch9ac27092014-01-31 16:53:39 -07003027 nvme_free_namespaces(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06003028 shutdown:
3029 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06003030 release_pools:
Keith Buscha1a5ef92013-12-16 13:50:00 -05003031 nvme_free_queues(dev, 0);
Matthew Wilcox091b6092011-02-10 09:56:01 -05003032 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06003033 release:
3034 nvme_release_instance(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06003035 put_pci:
3036 pci_dev_put(dev->pci_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003037 free:
3038 kfree(dev->queues);
3039 kfree(dev->entry);
3040 kfree(dev);
3041 return result;
3042}
3043
Keith Buschf0d54a52014-05-02 10:40:43 -06003044static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
3045{
Keith Buscha6739472014-06-23 16:03:21 -06003046 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06003047
Keith Buscha6739472014-06-23 16:03:21 -06003048 if (prepare)
3049 nvme_dev_shutdown(dev);
3050 else
3051 nvme_dev_resume(dev);
Keith Buschf0d54a52014-05-02 10:40:43 -06003052}
3053
Keith Busch09ece142014-01-27 11:29:40 -05003054static void nvme_shutdown(struct pci_dev *pdev)
3055{
3056 struct nvme_dev *dev = pci_get_drvdata(pdev);
3057 nvme_dev_shutdown(dev);
3058}
3059
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003060static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003061{
3062 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003063
3064 spin_lock(&dev_list_lock);
3065 list_del_init(&dev->node);
3066 spin_unlock(&dev_list_lock);
3067
3068 pci_set_drvdata(pdev, NULL);
3069 flush_work(&dev->reset_work);
Keith Busch5e82e952013-02-19 10:17:58 -07003070 misc_deregister(&dev->miscdev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003071 nvme_dev_shutdown(dev);
Keith Buschc9d3bf82015-01-07 18:55:52 -07003072 nvme_dev_remove(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003073 nvme_dev_remove_admin(dev);
3074 nvme_free_queues(dev, 0);
Keith Busch9a6b9452013-12-10 13:10:36 -07003075 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07003076 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003077}
3078
3079/* These functions are yet to be implemented */
3080#define nvme_error_detected NULL
3081#define nvme_dump_registers NULL
3082#define nvme_link_reset NULL
3083#define nvme_slot_reset NULL
3084#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06003085
Jingoo Han671a6012014-02-13 11:19:14 +09003086#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06003087static int nvme_suspend(struct device *dev)
3088{
3089 struct pci_dev *pdev = to_pci_dev(dev);
3090 struct nvme_dev *ndev = pci_get_drvdata(pdev);
3091
3092 nvme_dev_shutdown(ndev);
3093 return 0;
3094}
3095
3096static int nvme_resume(struct device *dev)
3097{
3098 struct pci_dev *pdev = to_pci_dev(dev);
3099 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06003100
Keith Busch9a6b9452013-12-10 13:10:36 -07003101 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Tejun Heo9ca97372014-03-07 10:24:49 -05003102 ndev->reset_workfn = nvme_reset_failed_dev;
Keith Busch9a6b9452013-12-10 13:10:36 -07003103 queue_work(nvme_workq, &ndev->reset_work);
3104 }
3105 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06003106}
Jingoo Han671a6012014-02-13 11:19:14 +09003107#endif
Keith Buschcd638942013-07-15 15:02:23 -06003108
3109static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003110
Stephen Hemminger1d352032012-09-07 09:33:17 -07003111static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003112 .error_detected = nvme_error_detected,
3113 .mmio_enabled = nvme_dump_registers,
3114 .link_reset = nvme_link_reset,
3115 .slot_reset = nvme_slot_reset,
3116 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06003117 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003118};
3119
3120/* Move to pci_ids.h later */
3121#define PCI_CLASS_STORAGE_EXPRESS 0x010802
3122
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003123static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003124 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
3125 { 0, }
3126};
3127MODULE_DEVICE_TABLE(pci, nvme_id_table);
3128
3129static struct pci_driver nvme_driver = {
3130 .name = "nvme",
3131 .id_table = nvme_id_table,
3132 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003133 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05003134 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06003135 .driver = {
3136 .pm = &nvme_dev_pm_ops,
3137 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003138 .err_handler = &nvme_err_handler,
3139};
3140
3141static int __init nvme_init(void)
3142{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003143 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003144
Dan McLeranb9afca32014-04-07 17:10:11 -06003145 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003146
Keith Busch9a6b9452013-12-10 13:10:36 -07003147 nvme_workq = create_singlethread_workqueue("nvme");
3148 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06003149 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07003150
Keith Busch5c42ea12012-07-25 16:05:18 -06003151 result = register_blkdev(nvme_major, "nvme");
3152 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07003153 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06003154 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003155 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003156
Keith Buschf3db22f2014-06-11 11:51:35 -06003157 result = pci_register_driver(&nvme_driver);
3158 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003159 goto unregister_blkdev;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003160 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003161
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003162 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003163 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003164 kill_workq:
3165 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003166 return result;
3167}
3168
3169static void __exit nvme_exit(void)
3170{
3171 pci_unregister_driver(&nvme_driver);
Keith Buschf3db22f2014-06-11 11:51:35 -06003172 unregister_hotcpu_notifier(&nvme_nb);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003173 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003174 destroy_workqueue(nvme_workq);
Dan McLeranb9afca32014-04-07 17:10:11 -06003175 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04003176 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003177}
3178
3179MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
3180MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07003181MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003182module_init(nvme_init);
3183module_exit(nvme_exit);