blob: cbdfbbf983927e85a4a83d94d20047f2fadf6357 [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>
40#include <linux/types.h>
Vishal Verma5d0f6132013-03-04 18:40:58 -070041#include <scsi/sg.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090042#include <asm-generic/io-64-nonatomic-lo-hi.h>
43
Keith Busch9d43cf62014-05-13 11:42:02 -060044#define NVME_Q_DEPTH 1024
Matias Bjørlinga4aea562014-11-04 08:20:14 -070045#define NVME_AQ_DEPTH 64
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050046#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
47#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Keith Busch9d43cf62014-05-13 11:42:02 -060048#define ADMIN_TIMEOUT (admin_timeout * HZ)
Dan McLeran2484f402014-07-01 09:33:32 -060049#define SHUTDOWN_TIMEOUT (shutdown_timeout * HZ)
Keith Busch9d43cf62014-05-13 11:42:02 -060050#define IOD_TIMEOUT (retry_time * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050051
Keith Busch9d43cf62014-05-13 11:42:02 -060052static unsigned char admin_timeout = 60;
53module_param(admin_timeout, byte, 0644);
54MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050055
Matthew Wilcoxbd676082014-06-03 23:04:30 -040056unsigned char nvme_io_timeout = 30;
57module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
Keith Buschb3550842014-04-04 11:43:36 -060058MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050059
Keith Busch61e4ce02014-05-13 11:42:01 -060060static unsigned char retry_time = 30;
61module_param(retry_time, byte, 0644);
62MODULE_PARM_DESC(retry_time, "time in seconds to retry failed I/O");
63
Dan McLeran2484f402014-07-01 09:33:32 -060064static unsigned char shutdown_timeout = 5;
65module_param(shutdown_timeout, byte, 0644);
66MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
67
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050068static int nvme_major;
69module_param(nvme_major, int, 0);
70
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050071static int use_threaded_interrupts;
72module_param(use_threaded_interrupts, int, 0);
73
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050074static DEFINE_SPINLOCK(dev_list_lock);
75static LIST_HEAD(dev_list);
76static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070077static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060078static wait_queue_head_t nvme_kthread_wait;
Keith Buschf3db22f2014-06-11 11:51:35 -060079static struct notifier_block nvme_nb;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050080
Keith Buschd4b4ff82013-12-10 13:10:37 -070081static void nvme_reset_failed_dev(struct work_struct *ws);
Matias Bjørlinga4aea562014-11-04 08:20:14 -070082static int nvme_process_cq(struct nvme_queue *nvmeq);
Keith Buschd4b4ff82013-12-10 13:10:37 -070083
Keith Busch4d115422013-12-10 13:10:40 -070084struct async_cmd_info {
85 struct kthread_work work;
86 struct kthread_worker *worker;
Matias Bjørlinga4aea562014-11-04 08:20:14 -070087 struct request *req;
Keith Busch4d115422013-12-10 13:10:40 -070088 u32 result;
89 int status;
90 void *ctx;
91};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050092
93/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050094 * An NVM Express queue. Each device has at least two (one for admin
95 * commands and one for I/O commands).
96 */
97struct nvme_queue {
Keith Buschf435c282014-07-07 09:14:42 -060098 struct llist_node node;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050099 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500100 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500101 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500102 spinlock_t q_lock;
103 struct nvme_command *sq_cmds;
104 volatile struct nvme_completion *cqes;
105 dma_addr_t sq_dma_addr;
106 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500107 u32 __iomem *q_db;
108 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700109 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500110 u16 sq_head;
111 u16 sq_tail;
112 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700113 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400114 u8 cq_phase;
115 u8 cqe_seen;
Keith Busch4d115422013-12-10 13:10:40 -0700116 struct async_cmd_info cmdinfo;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700117 struct blk_mq_hw_ctx *hctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500118};
119
120/*
121 * Check we didin't inadvertently grow the command struct
122 */
123static inline void _nvme_check_size(void)
124{
125 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
126 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
127 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
128 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
129 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400130 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700131 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500132 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
133 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
134 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
135 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600136 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500137}
138
Keith Buschedd10d32014-04-03 16:45:23 -0600139typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400140 struct nvme_completion *);
141
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500142struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400143 nvme_completion_fn fn;
144 void *ctx;
Keith Buschc30341d2013-12-10 13:10:38 -0700145 int aborted;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700146 struct nvme_queue *nvmeq;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700147 struct nvme_iod iod[0];
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500148};
149
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700150/*
151 * Max size of iod being embedded in the request payload
152 */
153#define NVME_INT_PAGES 2
154#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->page_size)
155
156/*
157 * Will slightly overestimate the number of pages needed. This is OK
158 * as it only leads to a small amount of wasted memory for the lifetime of
159 * the I/O.
160 */
161static int nvme_npages(unsigned size, struct nvme_dev *dev)
162{
163 unsigned nprps = DIV_ROUND_UP(size + dev->page_size, dev->page_size);
164 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
165}
166
167static unsigned int nvme_cmd_size(struct nvme_dev *dev)
168{
169 unsigned int ret = sizeof(struct nvme_cmd_info);
170
171 ret += sizeof(struct nvme_iod);
172 ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
173 ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
174
175 return ret;
176}
177
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700178static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
179 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500180{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700181 struct nvme_dev *dev = data;
182 struct nvme_queue *nvmeq = dev->queues[0];
183
184 WARN_ON(nvmeq->hctx);
185 nvmeq->hctx = hctx;
186 hctx->driver_data = nvmeq;
187 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500188}
189
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700190static int nvme_admin_init_request(void *data, struct request *req,
191 unsigned int hctx_idx, unsigned int rq_idx,
192 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600193{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700194 struct nvme_dev *dev = data;
195 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
196 struct nvme_queue *nvmeq = dev->queues[0];
197
198 BUG_ON(!nvmeq);
199 cmd->nvmeq = nvmeq;
200 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600201}
202
Jens Axboe2c30540b2014-11-14 09:47:32 -0700203static void nvme_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
204{
205 struct nvme_queue *nvmeq = hctx->driver_data;
206
207 nvmeq->hctx = NULL;
208}
209
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700210static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
211 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500212{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700213 struct nvme_dev *dev = data;
214 struct nvme_queue *nvmeq = dev->queues[
215 (hctx_idx % dev->queue_count) + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500216
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700217 if (!nvmeq->hctx)
218 nvmeq->hctx = hctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500219
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700220 /* nvmeq queues are shared between namespaces. We assume here that
221 * blk-mq map the tags so they match up with the nvme queue tags. */
222 WARN_ON(nvmeq->hctx->tags != hctx->tags);
223
224 hctx->driver_data = nvmeq;
225 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500226}
227
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700228static int nvme_init_request(void *data, struct request *req,
229 unsigned int hctx_idx, unsigned int rq_idx,
230 unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500231{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700232 struct nvme_dev *dev = data;
233 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
234 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
235
236 BUG_ON(!nvmeq);
237 cmd->nvmeq = nvmeq;
238 return 0;
239}
240
241static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
242 nvme_completion_fn handler)
243{
244 cmd->fn = handler;
245 cmd->ctx = ctx;
246 cmd->aborted = 0;
Keith Buschc917dfe2015-01-07 18:55:48 -0700247 blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500248}
249
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700250static void *iod_get_private(struct nvme_iod *iod)
251{
252 return (void *) (iod->private & ~0x1UL);
253}
254
255/*
256 * If bit 0 is set, the iod is embedded in the request payload.
257 */
258static bool iod_should_kfree(struct nvme_iod *iod)
259{
260 return (iod->private & 0x01) == 0;
261}
262
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400263/* Special values must be less than 0x1000 */
264#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500265#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
266#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
267#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500268
Keith Buschedd10d32014-04-03 16:45:23 -0600269static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400270 struct nvme_completion *cqe)
271{
272 if (ctx == CMD_CTX_CANCELLED)
273 return;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400274 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600275 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400276 "completed id %d twice on queue %d\n",
277 cqe->command_id, le16_to_cpup(&cqe->sq_id));
278 return;
279 }
280 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600281 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400282 "invalid id %d completed on queue %d\n",
283 cqe->command_id, le16_to_cpup(&cqe->sq_id));
284 return;
285 }
Keith Buschedd10d32014-04-03 16:45:23 -0600286 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400287}
288
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700289static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
290{
291 void *ctx;
292
293 if (fn)
294 *fn = cmd->fn;
295 ctx = cmd->ctx;
296 cmd->fn = special_completion;
297 cmd->ctx = CMD_CTX_CANCELLED;
298 return ctx;
299}
300
301static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
302 struct nvme_completion *cqe)
303{
304 struct request *req = ctx;
305
306 u32 result = le32_to_cpup(&cqe->result);
307 u16 status = le16_to_cpup(&cqe->status) >> 1;
308
309 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
310 ++nvmeq->dev->event_limit;
311 if (status == NVME_SC_SUCCESS)
312 dev_warn(nvmeq->q_dmadev,
313 "async event result %08x\n", result);
314
Jens Axboe9d135bb2014-11-17 10:43:42 -0700315 blk_mq_free_hctx_request(nvmeq->hctx, req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700316}
317
318static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
319 struct nvme_completion *cqe)
320{
321 struct request *req = ctx;
322
323 u16 status = le16_to_cpup(&cqe->status) >> 1;
324 u32 result = le32_to_cpup(&cqe->result);
325
Jens Axboe9d135bb2014-11-17 10:43:42 -0700326 blk_mq_free_hctx_request(nvmeq->hctx, req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700327
328 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
329 ++nvmeq->dev->abort_limit;
330}
331
Keith Buschedd10d32014-04-03 16:45:23 -0600332static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700333 struct nvme_completion *cqe)
334{
335 struct async_cmd_info *cmdinfo = ctx;
336 cmdinfo->result = le32_to_cpup(&cqe->result);
337 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
338 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
Jens Axboe9d135bb2014-11-17 10:43:42 -0700339 blk_mq_free_hctx_request(nvmeq->hctx, cmdinfo->req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700340}
341
342static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
343 unsigned int tag)
344{
345 struct blk_mq_hw_ctx *hctx = nvmeq->hctx;
346 struct request *req = blk_mq_tag_to_rq(hctx->tags, tag);
347
348 return blk_mq_rq_to_pdu(req);
Keith Busch4d115422013-12-10 13:10:40 -0700349}
350
Matthew Wilcox184d2942011-05-11 21:36:38 -0400351/*
352 * Called with local interrupts disabled and the q_lock held. May not sleep.
353 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700354static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400355 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500356{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700357 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400358 void *ctx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700359 if (tag >= nvmeq->q_depth) {
360 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500361 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400362 }
Keith Busch859361a2012-08-02 14:05:59 -0600363 if (fn)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700364 *fn = cmd->fn;
365 ctx = cmd->ctx;
366 cmd->fn = special_completion;
367 cmd->ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400368 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500369}
370
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500371/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400372 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500373 * @nvmeq: The queue to use
374 * @cmd: The command to send
375 *
376 * Safe to use from interrupt context
377 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700378static int __nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500379{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700380 u16 tail = nvmeq->sq_tail;
381
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500382 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500383 if (++tail == nvmeq->q_depth)
384 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500385 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500386 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500387
388 return 0;
389}
390
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700391static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
392{
393 unsigned long flags;
394 int ret;
395 spin_lock_irqsave(&nvmeq->q_lock, flags);
396 ret = __nvme_submit_cmd(nvmeq, cmd);
397 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
398 return ret;
399}
400
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500401static __le64 **iod_list(struct nvme_iod *iod)
402{
403 return ((void *)iod) + iod->offset;
404}
405
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700406static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
407 unsigned nseg, unsigned long private)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500408{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700409 iod->private = private;
410 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
411 iod->npages = -1;
412 iod->length = nbytes;
413 iod->nents = 0;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500414}
415
416static struct nvme_iod *
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700417__nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
418 unsigned long priv, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500419{
420 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700421 sizeof(__le64 *) * nvme_npages(bytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500422 sizeof(struct scatterlist) * nseg, gfp);
423
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700424 if (iod)
425 iod_init(iod, bytes, nseg, priv);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500426
427 return iod;
428}
429
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700430static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
431 gfp_t gfp)
432{
433 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
434 sizeof(struct nvme_dsm_range);
435 unsigned long mask = 0;
436 struct nvme_iod *iod;
437
438 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
439 size <= NVME_INT_BYTES(dev)) {
440 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
441
442 iod = cmd->iod;
443 mask = 0x01;
444 iod_init(iod, size, rq->nr_phys_segments,
445 (unsigned long) rq | 0x01);
446 return iod;
447 }
448
449 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
450 (unsigned long) rq, gfp);
451}
452
Vishal Verma5d0f6132013-03-04 18:40:58 -0700453void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500454{
Keith Busch1d090622014-06-23 11:34:01 -0600455 const int last_prp = dev->page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500456 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500457 __le64 **list = iod_list(iod);
458 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500459
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500460 if (iod->npages == 0)
461 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
462 for (i = 0; i < iod->npages; i++) {
463 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500464 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500465 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500466 prp_dma = next_prp_dma;
467 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700468
469 if (iod_should_kfree(iod))
470 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500471}
472
Keith Buschb4ff9c82014-08-29 09:06:12 -0600473static int nvme_error_status(u16 status)
474{
475 switch (status & 0x7ff) {
476 case NVME_SC_SUCCESS:
477 return 0;
478 case NVME_SC_CAP_EXCEEDED:
479 return -ENOSPC;
480 default:
481 return -EIO;
482 }
483}
484
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700485static void req_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500486 struct nvme_completion *cqe)
487{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500488 struct nvme_iod *iod = ctx;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700489 struct request *req = iod_get_private(iod);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700490 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
491
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500492 u16 status = le16_to_cpup(&cqe->status) >> 1;
493
Keith Buschedd10d32014-04-03 16:45:23 -0600494 if (unlikely(status)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700495 if (!(status & NVME_SC_DNR || blk_noretry_request(req))
496 && (jiffies - req->start_time) < req->timeout) {
Keith Buschc9d3bf82015-01-07 18:55:52 -0700497 unsigned long flags;
498
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700499 blk_mq_requeue_request(req);
Keith Buschc9d3bf82015-01-07 18:55:52 -0700500 spin_lock_irqsave(req->q->queue_lock, flags);
501 if (!blk_queue_stopped(req->q))
502 blk_mq_kick_requeue_list(req->q);
503 spin_unlock_irqrestore(req->q->queue_lock, flags);
Keith Buschedd10d32014-04-03 16:45:23 -0600504 return;
505 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700506 req->errors = nvme_error_status(status);
507 } else
508 req->errors = 0;
509
510 if (cmd_rq->aborted)
511 dev_warn(&nvmeq->dev->pci_dev->dev,
512 "completing aborted command with status:%04x\n",
513 status);
514
515 if (iod->nents)
516 dma_unmap_sg(&nvmeq->dev->pci_dev->dev, iod->sg, iod->nents,
517 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Buschedd10d32014-04-03 16:45:23 -0600518 nvme_free_iod(nvmeq->dev, iod);
Keith Busch3291fa52014-04-28 12:30:52 -0600519
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700520 blk_mq_complete_request(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500521}
522
Matthew Wilcox184d2942011-05-11 21:36:38 -0400523/* length is in bytes. gfp flags indicates whether we may sleep. */
Keith Buschedd10d32014-04-03 16:45:23 -0600524int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod, int total_len,
525 gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500526{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500527 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500528 int length = total_len;
529 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500530 int dma_len = sg_dma_len(sg);
531 u64 dma_addr = sg_dma_address(sg);
532 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500533 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500534 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500535 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500536 int nprps, i;
Keith Busch1d090622014-06-23 11:34:01 -0600537 u32 page_size = dev->page_size;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500538
Keith Busch1d090622014-06-23 11:34:01 -0600539 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500540 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500541 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500542
Keith Busch1d090622014-06-23 11:34:01 -0600543 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500544 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600545 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500546 } else {
547 sg = sg_next(sg);
548 dma_addr = sg_dma_address(sg);
549 dma_len = sg_dma_len(sg);
550 }
551
Keith Busch1d090622014-06-23 11:34:01 -0600552 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600553 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500554 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500555 }
556
Keith Busch1d090622014-06-23 11:34:01 -0600557 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500558 if (nprps <= (256 / 8)) {
559 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500560 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500561 } else {
562 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500563 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500564 }
565
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400566 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
567 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600568 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500569 iod->npages = -1;
Keith Busch1d090622014-06-23 11:34:01 -0600570 return (total_len - length) + page_size;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400571 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500572 list[0] = prp_list;
573 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500574 i = 0;
575 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600576 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500577 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400578 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500579 if (!prp_list)
580 return total_len - length;
581 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400582 prp_list[0] = old_prp_list[i - 1];
583 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
584 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500585 }
586 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600587 dma_len -= page_size;
588 dma_addr += page_size;
589 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500590 if (length <= 0)
591 break;
592 if (dma_len > 0)
593 continue;
594 BUG_ON(dma_len < 0);
595 sg = sg_next(sg);
596 dma_addr = sg_dma_address(sg);
597 dma_len = sg_dma_len(sg);
598 }
599
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500600 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500601}
602
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700603/*
604 * We reuse the small pool to allocate the 16-byte range here as it is not
605 * worth having a special pool for these or additional cases to handle freeing
606 * the iod.
607 */
608static void nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
609 struct request *req, struct nvme_iod *iod)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700610{
Keith Buschedd10d32014-04-03 16:45:23 -0600611 struct nvme_dsm_range *range =
612 (struct nvme_dsm_range *)iod_list(iod)[0];
Keith Busch0e5e4f02012-11-09 16:33:05 -0700613 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
614
Keith Busch0e5e4f02012-11-09 16:33:05 -0700615 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700616 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
617 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700618
619 memset(cmnd, 0, sizeof(*cmnd));
620 cmnd->dsm.opcode = nvme_cmd_dsm;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700621 cmnd->dsm.command_id = req->tag;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700622 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
623 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
624 cmnd->dsm.nr = 0;
625 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
626
627 if (++nvmeq->sq_tail == nvmeq->q_depth)
628 nvmeq->sq_tail = 0;
629 writel(nvmeq->sq_tail, nvmeq->q_db);
Keith Busch0e5e4f02012-11-09 16:33:05 -0700630}
631
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700632static void nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500633 int cmdid)
634{
635 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
636
637 memset(cmnd, 0, sizeof(*cmnd));
638 cmnd->common.opcode = nvme_cmd_flush;
639 cmnd->common.command_id = cmdid;
640 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
641
642 if (++nvmeq->sq_tail == nvmeq->q_depth)
643 nvmeq->sq_tail = 0;
644 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500645}
646
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700647static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod,
648 struct nvme_ns *ns)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500649{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700650 struct request *req = iod_get_private(iod);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500651 struct nvme_command *cmnd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700652 u16 control = 0;
653 u32 dsmgmt = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500654
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700655 if (req->cmd_flags & REQ_FUA)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500656 control |= NVME_RW_FUA;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700657 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500658 control |= NVME_RW_LR;
659
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700660 if (req->cmd_flags & REQ_RAHEAD)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500661 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
662
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500663 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500664 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500665
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700666 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
667 cmnd->rw.command_id = req->tag;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500668 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Keith Buschedd10d32014-04-03 16:45:23 -0600669 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
670 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700671 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
672 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500673 cmnd->rw.control = cpu_to_le16(control);
674 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500675
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500676 if (++nvmeq->sq_tail == nvmeq->q_depth)
677 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500678 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500679
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500680 return 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600681}
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500682
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700683static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
684 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600685{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700686 struct nvme_ns *ns = hctx->queue->queuedata;
687 struct nvme_queue *nvmeq = hctx->driver_data;
688 struct request *req = bd->rq;
689 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
Keith Buschedd10d32014-04-03 16:45:23 -0600690 struct nvme_iod *iod;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700691 enum dma_data_direction dma_dir;
Keith Buschedd10d32014-04-03 16:45:23 -0600692
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700693 iod = nvme_alloc_iod(req, ns->dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600694 if (!iod)
Jens Axboefe543032014-12-11 13:58:39 -0700695 return BLK_MQ_RQ_QUEUE_BUSY;
Keith Buschedd10d32014-04-03 16:45:23 -0600696
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700697 if (req->cmd_flags & REQ_DISCARD) {
Keith Buschedd10d32014-04-03 16:45:23 -0600698 void *range;
699 /*
700 * We reuse the small pool to allocate the 16-byte range here
701 * as it is not worth having a special pool for these or
702 * additional cases to handle freeing the iod.
703 */
704 range = dma_pool_alloc(nvmeq->dev->prp_small_pool,
705 GFP_ATOMIC,
706 &iod->first_dma);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700707 if (!range)
Jens Axboefe543032014-12-11 13:58:39 -0700708 goto retry_cmd;
Keith Buschedd10d32014-04-03 16:45:23 -0600709 iod_list(iod)[0] = (__le64 *)range;
710 iod->npages = 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700711 } else if (req->nr_phys_segments) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700712 dma_dir = rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
Keith Buschedd10d32014-04-03 16:45:23 -0600713
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700714 sg_init_table(iod->sg, req->nr_phys_segments);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700715 iod->nents = blk_rq_map_sg(req->q, req, iod->sg);
Jens Axboefe543032014-12-11 13:58:39 -0700716 if (!iod->nents)
717 goto error_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700718
719 if (!dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir))
Jens Axboefe543032014-12-11 13:58:39 -0700720 goto retry_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700721
Jens Axboefe543032014-12-11 13:58:39 -0700722 if (blk_rq_bytes(req) !=
723 nvme_setup_prps(nvmeq->dev, iod, blk_rq_bytes(req), GFP_ATOMIC)) {
724 dma_unmap_sg(&nvmeq->dev->pci_dev->dev, iod->sg,
725 iod->nents, dma_dir);
726 goto retry_cmd;
727 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700728 }
729
Keith Busch9af87852014-12-03 17:07:13 -0700730 nvme_set_info(cmd, iod, req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700731 spin_lock_irq(&nvmeq->q_lock);
732 if (req->cmd_flags & REQ_DISCARD)
733 nvme_submit_discard(nvmeq, ns, req, iod);
734 else if (req->cmd_flags & REQ_FLUSH)
735 nvme_submit_flush(nvmeq, ns, req->tag);
736 else
737 nvme_submit_iod(nvmeq, iod, ns);
738
739 nvme_process_cq(nvmeq);
740 spin_unlock_irq(&nvmeq->q_lock);
741 return BLK_MQ_RQ_QUEUE_OK;
742
Jens Axboefe543032014-12-11 13:58:39 -0700743 error_cmd:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500744 nvme_free_iod(nvmeq->dev, iod);
Jens Axboefe543032014-12-11 13:58:39 -0700745 return BLK_MQ_RQ_QUEUE_ERROR;
746 retry_cmd:
747 nvme_free_iod(nvmeq->dev, iod);
748 return BLK_MQ_RQ_QUEUE_BUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500749}
750
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400751static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500752{
Matthew Wilcox82123462011-01-20 13:24:06 -0500753 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500754
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500755 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500756 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500757
758 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400759 void *ctx;
760 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500761 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500762 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500763 break;
764 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
765 if (++head == nvmeq->q_depth) {
766 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500767 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500768 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700769 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600770 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500771 }
772
773 /* If the controller ignores the cq head doorbell and continuously
774 * writes to the queue, it is theoretically possible to wrap around
775 * the queue twice and mistakenly return IRQ_NONE. Linux only
776 * requires that 0.1% of your interrupts are handled, so this isn't
777 * a big problem.
778 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500779 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400780 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500781
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800782 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500783 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500784 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500785
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400786 nvmeq->cqe_seen = 1;
787 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500788}
789
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700790/* Admin queue isn't initialized as a request queue. If at some point this
791 * happens anyway, make sure to notify the user */
792static int nvme_admin_queue_rq(struct blk_mq_hw_ctx *hctx,
793 const struct blk_mq_queue_data *bd)
Matthew Wilcox7d822452013-06-24 12:03:57 -0400794{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700795 WARN_ON_ONCE(1);
796 return BLK_MQ_RQ_QUEUE_ERROR;
Matthew Wilcox7d822452013-06-24 12:03:57 -0400797}
798
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500799static irqreturn_t nvme_irq(int irq, void *data)
800{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500801 irqreturn_t result;
802 struct nvme_queue *nvmeq = data;
803 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400804 nvme_process_cq(nvmeq);
805 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
806 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500807 spin_unlock(&nvmeq->q_lock);
808 return result;
809}
810
811static irqreturn_t nvme_irq_check(int irq, void *data)
812{
813 struct nvme_queue *nvmeq = data;
814 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
815 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
816 return IRQ_NONE;
817 return IRQ_WAKE_THREAD;
818}
819
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700820static void nvme_abort_cmd_info(struct nvme_queue *nvmeq, struct nvme_cmd_info *
821 cmd_info)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500822{
823 spin_lock_irq(&nvmeq->q_lock);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700824 cancel_cmd_info(cmd_info, NULL);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500825 spin_unlock_irq(&nvmeq->q_lock);
826}
827
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400828struct sync_cmd_info {
829 struct task_struct *task;
830 u32 result;
831 int status;
832};
833
Keith Buschedd10d32014-04-03 16:45:23 -0600834static void sync_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400835 struct nvme_completion *cqe)
836{
837 struct sync_cmd_info *cmdinfo = ctx;
838 cmdinfo->result = le32_to_cpup(&cqe->result);
839 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
840 wake_up_process(cmdinfo->task);
841}
842
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500843/*
844 * Returns 0 on success. If the result is negative, it's a Linux error code;
845 * if the result is positive, it's an NVM Express status code
846 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700847static int nvme_submit_sync_cmd(struct request *req, struct nvme_command *cmd,
Vishal Verma5d0f6132013-03-04 18:40:58 -0700848 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500849{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700850 int ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500851 struct sync_cmd_info cmdinfo;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700852 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
853 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500854
855 cmdinfo.task = current;
856 cmdinfo.status = -EINTR;
857
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700858 cmd->common.command_id = req->tag;
859
860 nvme_set_info(cmd_rq, &cmdinfo, sync_completion);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500861
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500862 set_current_state(TASK_KILLABLE);
Keith Busch4f5099a2014-03-03 16:39:13 -0700863 ret = nvme_submit_cmd(nvmeq, cmd);
864 if (ret) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700865 nvme_finish_cmd(nvmeq, req->tag, NULL);
Keith Busch4f5099a2014-03-03 16:39:13 -0700866 set_current_state(TASK_RUNNING);
Keith Busch4f5099a2014-03-03 16:39:13 -0700867 }
Jens Axboe849c6e72014-12-12 08:53:40 -0700868 ret = schedule_timeout(timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500869
Jens Axboe849c6e72014-12-12 08:53:40 -0700870 /*
871 * Ensure that sync_completion has either run, or that it will
872 * never run.
873 */
874 nvme_abort_cmd_info(nvmeq, blk_mq_rq_to_pdu(req));
875
876 /*
877 * We never got the completion
878 */
879 if (cmdinfo.status == -EINTR)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500880 return -EINTR;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500881
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500882 if (result)
883 *result = cmdinfo.result;
884
885 return cmdinfo.status;
886}
887
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700888static int nvme_submit_async_admin_req(struct nvme_dev *dev)
889{
890 struct nvme_queue *nvmeq = dev->queues[0];
891 struct nvme_command c;
892 struct nvme_cmd_info *cmd_info;
893 struct request *req;
894
Jens Axboe6dcc0cf2014-11-18 08:21:18 -0700895 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC, false);
Dan Carpenter9f173b32014-11-05 23:39:09 +0300896 if (IS_ERR(req))
897 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700898
Keith Buschc917dfe2015-01-07 18:55:48 -0700899 req->cmd_flags |= REQ_NO_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700900 cmd_info = blk_mq_rq_to_pdu(req);
901 nvme_set_info(cmd_info, req, async_req_completion);
902
903 memset(&c, 0, sizeof(c));
904 c.common.opcode = nvme_admin_async_event;
905 c.common.command_id = req->tag;
906
907 return __nvme_submit_cmd(nvmeq, &c);
908}
909
910static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
Keith Busch4d115422013-12-10 13:10:40 -0700911 struct nvme_command *cmd,
912 struct async_cmd_info *cmdinfo, unsigned timeout)
913{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700914 struct nvme_queue *nvmeq = dev->queues[0];
915 struct request *req;
916 struct nvme_cmd_info *cmd_rq;
Keith Busch4d115422013-12-10 13:10:40 -0700917
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700918 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_KERNEL, false);
Dan Carpenter9f173b32014-11-05 23:39:09 +0300919 if (IS_ERR(req))
920 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700921
922 req->timeout = timeout;
923 cmd_rq = blk_mq_rq_to_pdu(req);
924 cmdinfo->req = req;
925 nvme_set_info(cmd_rq, cmdinfo, async_completion);
Keith Busch4d115422013-12-10 13:10:40 -0700926 cmdinfo->status = -EINTR;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700927
928 cmd->common.command_id = req->tag;
929
Keith Busch4f5099a2014-03-03 16:39:13 -0700930 return nvme_submit_cmd(nvmeq, cmd);
Keith Busch4d115422013-12-10 13:10:40 -0700931}
932
kbuild test robota64e6bb2014-11-05 18:47:07 +0800933static int __nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700934 u32 *result, unsigned timeout)
935{
936 int res;
937 struct request *req;
938
939 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_KERNEL, false);
Jens Axboe97fe3832014-12-10 13:02:44 -0700940 if (IS_ERR(req))
941 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700942 res = nvme_submit_sync_cmd(req, cmd, result, timeout);
Jens Axboe9d135bb2014-11-17 10:43:42 -0700943 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700944 return res;
945}
946
Vishal Verma5d0f6132013-03-04 18:40:58 -0700947int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500948 u32 *result)
949{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700950 return __nvme_submit_admin_cmd(dev, cmd, result, ADMIN_TIMEOUT);
Keith Busch4f5099a2014-03-03 16:39:13 -0700951}
952
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700953int nvme_submit_io_cmd(struct nvme_dev *dev, struct nvme_ns *ns,
954 struct nvme_command *cmd, u32 *result)
Keith Busch4f5099a2014-03-03 16:39:13 -0700955{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700956 int res;
957 struct request *req;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500958
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700959 req = blk_mq_alloc_request(ns->queue, WRITE, (GFP_KERNEL|__GFP_WAIT),
960 false);
Jens Axboe97fe3832014-12-10 13:02:44 -0700961 if (IS_ERR(req))
962 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700963 res = nvme_submit_sync_cmd(req, cmd, result, NVME_IO_TIMEOUT);
Jens Axboe9d135bb2014-11-17 10:43:42 -0700964 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700965 return res;
Keith Busch4d115422013-12-10 13:10:40 -0700966}
967
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500968static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
969{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500970 struct nvme_command c;
971
972 memset(&c, 0, sizeof(c));
973 c.delete_queue.opcode = opcode;
974 c.delete_queue.qid = cpu_to_le16(id);
975
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700976 return nvme_submit_admin_cmd(dev, &c, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500977}
978
979static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
980 struct nvme_queue *nvmeq)
981{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500982 struct nvme_command c;
983 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
984
985 memset(&c, 0, sizeof(c));
986 c.create_cq.opcode = nvme_admin_create_cq;
987 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
988 c.create_cq.cqid = cpu_to_le16(qid);
989 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
990 c.create_cq.cq_flags = cpu_to_le16(flags);
991 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
992
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700993 return nvme_submit_admin_cmd(dev, &c, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500994}
995
996static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
997 struct nvme_queue *nvmeq)
998{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500999 struct nvme_command c;
1000 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1001
1002 memset(&c, 0, sizeof(c));
1003 c.create_sq.opcode = nvme_admin_create_sq;
1004 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1005 c.create_sq.sqid = cpu_to_le16(qid);
1006 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1007 c.create_sq.sq_flags = cpu_to_le16(flags);
1008 c.create_sq.cqid = cpu_to_le16(qid);
1009
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001010 return nvme_submit_admin_cmd(dev, &c, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001011}
1012
1013static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1014{
1015 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1016}
1017
1018static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1019{
1020 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1021}
1022
Vishal Verma5d0f6132013-03-04 18:40:58 -07001023int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001024 dma_addr_t dma_addr)
1025{
1026 struct nvme_command c;
1027
1028 memset(&c, 0, sizeof(c));
1029 c.identify.opcode = nvme_admin_identify;
1030 c.identify.nsid = cpu_to_le32(nsid);
1031 c.identify.prp1 = cpu_to_le64(dma_addr);
1032 c.identify.cns = cpu_to_le32(cns);
1033
1034 return nvme_submit_admin_cmd(dev, &c, NULL);
1035}
1036
Vishal Verma5d0f6132013-03-04 18:40:58 -07001037int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -06001038 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001039{
1040 struct nvme_command c;
1041
1042 memset(&c, 0, sizeof(c));
1043 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -06001044 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001045 c.features.prp1 = cpu_to_le64(dma_addr);
1046 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001047
Keith Busch08df1e02012-09-21 10:52:13 -06001048 return nvme_submit_admin_cmd(dev, &c, result);
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001049}
1050
Vishal Verma5d0f6132013-03-04 18:40:58 -07001051int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
1052 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001053{
1054 struct nvme_command c;
1055
1056 memset(&c, 0, sizeof(c));
1057 c.features.opcode = nvme_admin_set_features;
1058 c.features.prp1 = cpu_to_le64(dma_addr);
1059 c.features.fid = cpu_to_le32(fid);
1060 c.features.dword11 = cpu_to_le32(dword11);
1061
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001062 return nvme_submit_admin_cmd(dev, &c, result);
1063}
1064
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001065/**
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001066 * nvme_abort_req - Attempt aborting a request
Keith Buschc30341d2013-12-10 13:10:38 -07001067 *
1068 * Schedule controller reset if the command was already aborted once before and
1069 * still hasn't been returned to the driver, or if this is the admin queue.
1070 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001071static void nvme_abort_req(struct request *req)
Keith Buschc30341d2013-12-10 13:10:38 -07001072{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001073 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1074 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001075 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001076 struct request *abort_req;
1077 struct nvme_cmd_info *abort_cmd;
1078 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -07001079
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001080 if (!nvmeq->qid || cmd_rq->aborted) {
Keith Busch7a509a62015-01-07 18:55:53 -07001081 unsigned long flags;
1082
1083 spin_lock_irqsave(&dev_list_lock, flags);
Keith Buschc30341d2013-12-10 13:10:38 -07001084 if (work_busy(&dev->reset_work))
Keith Busch7a509a62015-01-07 18:55:53 -07001085 goto out;
Keith Buschc30341d2013-12-10 13:10:38 -07001086 list_del_init(&dev->node);
1087 dev_warn(&dev->pci_dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001088 "I/O %d QID %d timeout, reset controller\n",
1089 req->tag, nvmeq->qid);
Tejun Heo9ca97372014-03-07 10:24:49 -05001090 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschc30341d2013-12-10 13:10:38 -07001091 queue_work(nvme_workq, &dev->reset_work);
Keith Busch7a509a62015-01-07 18:55:53 -07001092 out:
1093 spin_unlock_irqrestore(&dev_list_lock, flags);
Keith Buschc30341d2013-12-10 13:10:38 -07001094 return;
1095 }
1096
1097 if (!dev->abort_limit)
1098 return;
1099
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001100 abort_req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC,
1101 false);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001102 if (IS_ERR(abort_req))
Keith Buschc30341d2013-12-10 13:10:38 -07001103 return;
1104
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001105 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1106 nvme_set_info(abort_cmd, abort_req, abort_completion);
1107
Keith Buschc30341d2013-12-10 13:10:38 -07001108 memset(&cmd, 0, sizeof(cmd));
1109 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001110 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001111 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001112 cmd.abort.command_id = abort_req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001113
1114 --dev->abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001115 cmd_rq->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -07001116
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001117 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", req->tag,
Keith Buschc30341d2013-12-10 13:10:38 -07001118 nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001119 if (nvme_submit_cmd(dev->queues[0], &cmd) < 0) {
1120 dev_warn(nvmeq->q_dmadev,
1121 "Could not abort I/O %d QID %d",
1122 req->tag, nvmeq->qid);
Sam Bradshawc87fd542014-12-10 13:00:31 -07001123 blk_mq_free_request(abort_req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001124 }
Keith Buschc30341d2013-12-10 13:10:38 -07001125}
1126
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001127static void nvme_cancel_queue_ios(struct blk_mq_hw_ctx *hctx,
1128 struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001129{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001130 struct nvme_queue *nvmeq = data;
1131 void *ctx;
1132 nvme_completion_fn fn;
1133 struct nvme_cmd_info *cmd;
Keith Buschcef6a942015-01-07 18:55:51 -07001134 struct nvme_completion cqe;
1135
1136 if (!blk_mq_request_started(req))
1137 return;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001138
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001139 cmd = blk_mq_rq_to_pdu(req);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001140
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001141 if (cmd->ctx == CMD_CTX_CANCELLED)
1142 return;
1143
Keith Buschcef6a942015-01-07 18:55:51 -07001144 if (blk_queue_dying(req->q))
1145 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1146 else
1147 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1148
1149
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001150 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1151 req->tag, nvmeq->qid);
1152 ctx = cancel_cmd_info(cmd, &fn);
1153 fn(nvmeq, ctx, &cqe);
1154}
1155
1156static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
1157{
1158 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
1159 struct nvme_queue *nvmeq = cmd->nvmeq;
1160
Keith Busch7a509a62015-01-07 18:55:53 -07001161 /*
1162 * The aborted req will be completed on receiving the abort req.
1163 * We enable the timer again. If hit twice, it'll cause a device reset,
1164 * as the device then is in a faulty state.
1165 */
1166 int ret = BLK_EH_RESET_TIMER;
1167
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001168 dev_warn(nvmeq->q_dmadev, "Timeout I/O %d QID %d\n", req->tag,
1169 nvmeq->qid);
Keith Buschc917dfe2015-01-07 18:55:48 -07001170
Keith Busch7a509a62015-01-07 18:55:53 -07001171 spin_lock_irq(&nvmeq->q_lock);
Keith Buschc917dfe2015-01-07 18:55:48 -07001172 if (!nvmeq->dev->initialized) {
1173 /*
1174 * Force cancelled command frees the request, which requires we
1175 * return BLK_EH_NOT_HANDLED.
1176 */
1177 nvme_cancel_queue_ios(nvmeq->hctx, req, nvmeq, reserved);
Keith Busch7a509a62015-01-07 18:55:53 -07001178 ret = BLK_EH_NOT_HANDLED;
1179 } else
1180 nvme_abort_req(req);
1181 spin_unlock_irq(&nvmeq->q_lock);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001182
Keith Busch7a509a62015-01-07 18:55:53 -07001183 return ret;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001184}
1185
Keith Buschf435c282014-07-07 09:14:42 -06001186static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001187{
1188 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1189 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1190 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1191 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1192 kfree(nvmeq);
1193}
1194
Keith Buscha1a5ef92013-12-16 13:50:00 -05001195static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001196{
1197 int i;
1198
Keith Buscha1a5ef92013-12-16 13:50:00 -05001199 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001200 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001201 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001202 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001203 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001204 }
Keith Busch22404272013-07-15 15:02:20 -06001205}
1206
Keith Busch4d115422013-12-10 13:10:40 -07001207/**
1208 * nvme_suspend_queue - put queue into suspended state
1209 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001210 */
1211static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001212{
Keith Busch2b25d982014-12-22 12:59:04 -07001213 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001214
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001215 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001216 if (nvmeq->cq_vector == -1) {
1217 spin_unlock_irq(&nvmeq->q_lock);
1218 return 1;
1219 }
1220 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Keith Busch42f61422014-03-24 10:46:25 -06001221 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001222 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001223 spin_unlock_irq(&nvmeq->q_lock);
1224
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001225 irq_set_affinity_hint(vector, NULL);
1226 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001227
Keith Busch4d115422013-12-10 13:10:40 -07001228 return 0;
1229}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001230
Keith Busch4d115422013-12-10 13:10:40 -07001231static void nvme_clear_queue(struct nvme_queue *nvmeq)
1232{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001233 struct blk_mq_hw_ctx *hctx = nvmeq->hctx;
1234
Keith Busch22404272013-07-15 15:02:20 -06001235 spin_lock_irq(&nvmeq->q_lock);
1236 nvme_process_cq(nvmeq);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001237 if (hctx && hctx->tags)
1238 blk_mq_tag_busy_iter(hctx, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001239 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001240}
1241
Keith Busch4d115422013-12-10 13:10:40 -07001242static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1243{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001244 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001245
1246 if (!nvmeq)
1247 return;
1248 if (nvme_suspend_queue(nvmeq))
1249 return;
1250
Keith Busch0e53d182013-12-10 13:10:39 -07001251 /* Don't tell the adapter to delete the admin queue.
1252 * Don't tell a removed adapter to delete IO queues. */
1253 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001254 adapter_delete_sq(dev, qid);
1255 adapter_delete_cq(dev, qid);
1256 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001257 if (!qid && dev->admin_q)
1258 blk_mq_freeze_queue_start(dev->admin_q);
Keith Busch4d115422013-12-10 13:10:40 -07001259 nvme_clear_queue(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001260}
1261
1262static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Keith Busch2b25d982014-12-22 12:59:04 -07001263 int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001264{
1265 struct device *dmadev = &dev->pci_dev->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001266 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001267 if (!nvmeq)
1268 return NULL;
1269
Joe Perches4d51abf2014-06-15 13:37:33 -07001270 nvmeq->cqes = dma_zalloc_coherent(dmadev, CQ_SIZE(depth),
1271 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001272 if (!nvmeq->cqes)
1273 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001274
1275 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
1276 &nvmeq->sq_dma_addr, GFP_KERNEL);
1277 if (!nvmeq->sq_cmds)
1278 goto free_cqdma;
1279
1280 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001281 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001282 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1283 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001284 spin_lock_init(&nvmeq->q_lock);
1285 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001286 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001287 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001288 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001289 nvmeq->qid = qid;
Keith Busch22404272013-07-15 15:02:20 -06001290 dev->queue_count++;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001291 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001292
1293 return nvmeq;
1294
1295 free_cqdma:
Keith Busch68b8eca2013-05-01 13:07:47 -06001296 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001297 nvmeq->cq_dma_addr);
1298 free_nvmeq:
1299 kfree(nvmeq);
1300 return NULL;
1301}
1302
Matthew Wilcox30010822011-01-20 09:10:15 -05001303static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1304 const char *name)
1305{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001306 if (use_threaded_interrupts)
1307 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001308 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001309 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001310 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001311 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001312}
1313
Keith Busch22404272013-07-15 15:02:20 -06001314static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001315{
Keith Busch22404272013-07-15 15:02:20 -06001316 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001317
Keith Busch7be50e92014-09-10 15:48:47 -06001318 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001319 nvmeq->sq_tail = 0;
1320 nvmeq->cq_head = 0;
1321 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001322 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001323 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001324 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001325 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001326}
1327
1328static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1329{
1330 struct nvme_dev *dev = nvmeq->dev;
1331 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001332
Keith Busch2b25d982014-12-22 12:59:04 -07001333 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001334 result = adapter_alloc_cq(dev, qid, nvmeq);
1335 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001336 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001337
1338 result = adapter_alloc_sq(dev, qid, nvmeq);
1339 if (result < 0)
1340 goto release_cq;
1341
Matthew Wilcox3193f072014-01-27 15:57:22 -05001342 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001343 if (result < 0)
1344 goto release_sq;
1345
Keith Busch22404272013-07-15 15:02:20 -06001346 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001347 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001348
1349 release_sq:
1350 adapter_delete_sq(dev, qid);
1351 release_cq:
1352 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001353 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001354}
1355
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001356static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1357{
1358 unsigned long timeout;
1359 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1360
1361 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1362
1363 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1364 msleep(100);
1365 if (fatal_signal_pending(current))
1366 return -EINTR;
1367 if (time_after(jiffies, timeout)) {
1368 dev_err(&dev->pci_dev->dev,
Matthew Wilcox27e81662014-04-11 11:58:45 -04001369 "Device not ready; aborting %s\n", enabled ?
1370 "initialisation" : "reset");
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001371 return -ENODEV;
1372 }
1373 }
1374
1375 return 0;
1376}
1377
1378/*
1379 * If the device has been passed off to us in an enabled state, just clear
1380 * the enabled bit. The spec says we should set the 'shutdown notification
1381 * bits', but doing so may cause the device to complete commands to the
1382 * admin queue ... and we don't know what memory that might be pointing at!
1383 */
1384static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1385{
Dan McLeran01079522014-06-23 08:24:36 -06001386 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1387 dev->ctrl_config &= ~NVME_CC_ENABLE;
1388 writel(dev->ctrl_config, &dev->bar->cc);
Matthew Wilcox44af1462013-05-04 06:43:17 -04001389
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001390 return nvme_wait_ready(dev, cap, false);
1391}
1392
1393static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1394{
Dan McLeran01079522014-06-23 08:24:36 -06001395 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1396 dev->ctrl_config |= NVME_CC_ENABLE;
1397 writel(dev->ctrl_config, &dev->bar->cc);
1398
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001399 return nvme_wait_ready(dev, cap, true);
1400}
1401
Keith Busch1894d8f2013-07-15 15:02:22 -06001402static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1403{
1404 unsigned long timeout;
Keith Busch1894d8f2013-07-15 15:02:22 -06001405
Dan McLeran01079522014-06-23 08:24:36 -06001406 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1407 dev->ctrl_config |= NVME_CC_SHN_NORMAL;
1408
1409 writel(dev->ctrl_config, &dev->bar->cc);
Keith Busch1894d8f2013-07-15 15:02:22 -06001410
Dan McLeran2484f402014-07-01 09:33:32 -06001411 timeout = SHUTDOWN_TIMEOUT + jiffies;
Keith Busch1894d8f2013-07-15 15:02:22 -06001412 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1413 NVME_CSTS_SHST_CMPLT) {
1414 msleep(100);
1415 if (fatal_signal_pending(current))
1416 return -EINTR;
1417 if (time_after(jiffies, timeout)) {
1418 dev_err(&dev->pci_dev->dev,
1419 "Device shutdown incomplete; abort shutdown\n");
1420 return -ENODEV;
1421 }
1422 }
1423
1424 return 0;
1425}
1426
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001427static struct blk_mq_ops nvme_mq_admin_ops = {
1428 .queue_rq = nvme_admin_queue_rq,
1429 .map_queue = blk_mq_map_queue,
1430 .init_hctx = nvme_admin_init_hctx,
Jens Axboe2c30540b2014-11-14 09:47:32 -07001431 .exit_hctx = nvme_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001432 .init_request = nvme_admin_init_request,
1433 .timeout = nvme_timeout,
1434};
1435
1436static struct blk_mq_ops nvme_mq_ops = {
1437 .queue_rq = nvme_queue_rq,
1438 .map_queue = blk_mq_map_queue,
1439 .init_hctx = nvme_init_hctx,
Jens Axboe2c30540b2014-11-14 09:47:32 -07001440 .exit_hctx = nvme_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001441 .init_request = nvme_init_request,
1442 .timeout = nvme_timeout,
1443};
1444
Keith Buschea191d22015-01-07 18:55:49 -07001445static void nvme_dev_remove_admin(struct nvme_dev *dev)
1446{
1447 if (dev->admin_q && !blk_queue_dying(dev->admin_q)) {
1448 blk_cleanup_queue(dev->admin_q);
1449 blk_mq_free_tag_set(&dev->admin_tagset);
1450 }
1451}
1452
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001453static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1454{
1455 if (!dev->admin_q) {
1456 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1457 dev->admin_tagset.nr_hw_queues = 1;
1458 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1;
1459 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
1460 dev->admin_tagset.numa_node = dev_to_node(&dev->pci_dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001461 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001462 dev->admin_tagset.driver_data = dev;
1463
1464 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1465 return -ENOMEM;
1466
1467 dev->admin_q = blk_mq_init_queue(&dev->admin_tagset);
Ming Lei35b489d2015-01-02 14:25:27 +00001468 if (IS_ERR(dev->admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001469 blk_mq_free_tag_set(&dev->admin_tagset);
1470 return -ENOMEM;
1471 }
Keith Buschea191d22015-01-07 18:55:49 -07001472 if (!blk_get_queue(dev->admin_q)) {
1473 nvme_dev_remove_admin(dev);
1474 return -ENODEV;
1475 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001476 } else
1477 blk_mq_unfreeze_queue(dev->admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001478
1479 return 0;
1480}
1481
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001482static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001483{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001484 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001485 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001486 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001487 struct nvme_queue *nvmeq;
Keith Busch1d090622014-06-23 11:34:01 -06001488 unsigned page_shift = PAGE_SHIFT;
1489 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12;
1490 unsigned dev_page_max = NVME_CAP_MPSMAX(cap) + 12;
1491
1492 if (page_shift < dev_page_min) {
1493 dev_err(&dev->pci_dev->dev,
1494 "Minimum device page size (%u) too large for "
1495 "host (%u)\n", 1 << dev_page_min,
1496 1 << page_shift);
1497 return -ENODEV;
1498 }
1499 if (page_shift > dev_page_max) {
1500 dev_info(&dev->pci_dev->dev,
1501 "Device maximum page size (%u) smaller than "
1502 "host (%u); enabling work-around\n",
1503 1 << dev_page_max, 1 << page_shift);
1504 page_shift = dev_page_max;
1505 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001506
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001507 result = nvme_disable_ctrl(dev, cap);
1508 if (result < 0)
1509 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001510
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001511 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001512 if (!nvmeq) {
Keith Busch2b25d982014-12-22 12:59:04 -07001513 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001514 if (!nvmeq)
1515 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001516 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001517
1518 aqa = nvmeq->q_depth - 1;
1519 aqa |= aqa << 16;
1520
Keith Busch1d090622014-06-23 11:34:01 -06001521 dev->page_size = 1 << page_shift;
1522
Dan McLeran01079522014-06-23 08:24:36 -06001523 dev->ctrl_config = NVME_CC_CSS_NVM;
Keith Busch1d090622014-06-23 11:34:01 -06001524 dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001525 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001526 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001527
1528 writel(aqa, &dev->bar->aqa);
1529 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1530 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001531
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001532 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001533 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001534 goto free_nvmeq;
1535
Keith Busch2b25d982014-12-22 12:59:04 -07001536 nvmeq->cq_vector = 0;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001537 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Keith Busch025c5572013-05-01 13:07:51 -06001538 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07001539 goto free_nvmeq;
Keith Busch025c5572013-05-01 13:07:51 -06001540
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001541 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001542
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001543 free_nvmeq:
1544 nvme_free_queues(dev, 0);
1545 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001546}
1547
Vishal Verma5d0f6132013-03-04 18:40:58 -07001548struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001549 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001550{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001551 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001552 struct scatterlist *sg;
1553 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001554 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001555
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001556 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001557 return ERR_PTR(-EINVAL);
Dan Carpenter5460fc02013-05-13 17:59:50 +03001558 if (!length || length > INT_MAX - PAGE_SIZE)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001559 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001560
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001561 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001562 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1563 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001564 if (!pages)
1565 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001566
1567 err = get_user_pages_fast(addr, count, 1, pages);
1568 if (err < count) {
1569 count = err;
1570 err = -EFAULT;
1571 goto put_pages;
1572 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001573
Santosh Y6808c5f2014-05-29 10:01:52 +05301574 err = -ENOMEM;
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001575 iod = __nvme_alloc_iod(count, length, dev, 0, GFP_KERNEL);
Santosh Y6808c5f2014-05-29 10:01:52 +05301576 if (!iod)
1577 goto put_pages;
1578
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001579 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001580 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001581 for (i = 0; i < count; i++) {
1582 sg_set_page(&sg[i], pages[i],
Dan Carpenter5460fc02013-05-13 17:59:50 +03001583 min_t(unsigned, length, PAGE_SIZE - offset),
1584 offset);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001585 length -= (PAGE_SIZE - offset);
1586 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001587 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001588 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001589 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001590
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001591 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1592 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001593 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001594 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001595
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001596 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001597 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001598
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001599 free_iod:
1600 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001601 put_pages:
1602 for (i = 0; i < count; i++)
1603 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001604 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001605 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001606}
1607
Vishal Verma5d0f6132013-03-04 18:40:58 -07001608void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001609 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001610{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001611 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001612
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001613 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1614 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001615
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001616 for (i = 0; i < iod->nents; i++)
1617 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001618}
1619
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001620static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1621{
1622 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001623 struct nvme_user_io io;
1624 struct nvme_command c;
Keith Buschf410c682013-04-23 17:23:59 -06001625 unsigned length, meta_len;
1626 int status, i;
1627 struct nvme_iod *iod, *meta_iod = NULL;
1628 dma_addr_t meta_dma_addr;
1629 void *meta, *uninitialized_var(meta_mem);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001630
1631 if (copy_from_user(&io, uio, sizeof(io)))
1632 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001633 length = (io.nblocks + 1) << ns->lba_shift;
Keith Buschf410c682013-04-23 17:23:59 -06001634 meta_len = (io.nblocks + 1) * ns->ms;
1635
1636 if (meta_len && ((io.metadata & 3) || !io.metadata))
1637 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001638
1639 switch (io.opcode) {
1640 case nvme_cmd_write:
1641 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001642 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001643 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001644 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001645 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001646 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001647 }
1648
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001649 if (IS_ERR(iod))
1650 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001651
1652 memset(&c, 0, sizeof(c));
1653 c.rw.opcode = io.opcode;
1654 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001655 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001656 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001657 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001658 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001659 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1660 c.rw.reftag = cpu_to_le32(io.reftag);
1661 c.rw.apptag = cpu_to_le16(io.apptag);
1662 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buschf410c682013-04-23 17:23:59 -06001663
1664 if (meta_len) {
Keith Busch1b567492013-07-18 12:13:51 -06001665 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
1666 meta_len);
Keith Buschf410c682013-04-23 17:23:59 -06001667 if (IS_ERR(meta_iod)) {
1668 status = PTR_ERR(meta_iod);
1669 meta_iod = NULL;
1670 goto unmap;
1671 }
1672
1673 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1674 &meta_dma_addr, GFP_KERNEL);
1675 if (!meta_mem) {
1676 status = -ENOMEM;
1677 goto unmap;
1678 }
1679
1680 if (io.opcode & 1) {
1681 int meta_offset = 0;
1682
1683 for (i = 0; i < meta_iod->nents; i++) {
1684 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1685 meta_iod->sg[i].offset;
1686 memcpy(meta_mem + meta_offset, meta,
1687 meta_iod->sg[i].length);
1688 kunmap_atomic(meta);
1689 meta_offset += meta_iod->sg[i].length;
1690 }
1691 }
1692
1693 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1694 }
1695
Keith Buschedd10d32014-04-03 16:45:23 -06001696 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1697 c.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1698 c.rw.prp2 = cpu_to_le64(iod->first_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001699
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001700 if (length != (io.nblocks + 1) << ns->lba_shift)
1701 status = -ENOMEM;
1702 else
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001703 status = nvme_submit_io_cmd(dev, ns, &c, NULL);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001704
Keith Buschf410c682013-04-23 17:23:59 -06001705 if (meta_len) {
1706 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1707 int meta_offset = 0;
1708
1709 for (i = 0; i < meta_iod->nents; i++) {
1710 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1711 meta_iod->sg[i].offset;
1712 memcpy(meta, meta_mem + meta_offset,
1713 meta_iod->sg[i].length);
1714 kunmap_atomic(meta);
1715 meta_offset += meta_iod->sg[i].length;
1716 }
1717 }
1718
1719 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1720 meta_dma_addr);
1721 }
1722
1723 unmap:
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001724 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001725 nvme_free_iod(dev, iod);
Keith Buschf410c682013-04-23 17:23:59 -06001726
1727 if (meta_iod) {
1728 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1729 nvme_free_iod(dev, meta_iod);
1730 }
1731
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001732 return status;
1733}
1734
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001735static int nvme_user_cmd(struct nvme_dev *dev, struct nvme_ns *ns,
1736 struct nvme_passthru_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001737{
Keith Busch7963e522014-09-12 16:07:20 -06001738 struct nvme_passthru_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001739 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001740 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001741 struct nvme_iod *uninitialized_var(iod);
Keith Busch94f370c2013-05-09 14:01:38 -06001742 unsigned timeout;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001743
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001744 if (!capable(CAP_SYS_ADMIN))
1745 return -EACCES;
1746 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001747 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001748
1749 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001750 c.common.opcode = cmd.opcode;
1751 c.common.flags = cmd.flags;
1752 c.common.nsid = cpu_to_le32(cmd.nsid);
1753 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1754 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1755 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1756 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1757 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1758 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1759 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1760 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1761
1762 length = cmd.data_len;
1763 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001764 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1765 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001766 if (IS_ERR(iod))
1767 return PTR_ERR(iod);
Keith Buschedd10d32014-04-03 16:45:23 -06001768 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1769 c.common.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1770 c.common.prp2 = cpu_to_le64(iod->first_dma);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001771 }
1772
Keith Busch94f370c2013-05-09 14:01:38 -06001773 timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
1774 ADMIN_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001775
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001776 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001777 status = -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001778 else if (ns) {
1779 struct request *req;
1780
1781 req = blk_mq_alloc_request(ns->queue, WRITE,
1782 (GFP_KERNEL|__GFP_WAIT), false);
Jens Axboe97fe3832014-12-10 13:02:44 -07001783 if (IS_ERR(req))
1784 status = PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001785 else {
1786 status = nvme_submit_sync_cmd(req, &c, &cmd.result,
1787 timeout);
Jens Axboe9d135bb2014-11-17 10:43:42 -07001788 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001789 }
1790 } else
1791 status = __nvme_submit_admin_cmd(dev, &c, &cmd.result, timeout);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001792
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001793 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001794 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001795 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001796 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001797
Chayan Biswascf90bc42013-05-22 22:34:49 +00001798 if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
Keith Buschf4f117f2012-09-21 10:49:05 -06001799 sizeof(cmd.result)))
1800 status = -EFAULT;
1801
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001802 return status;
1803}
1804
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001805static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1806 unsigned long arg)
1807{
1808 struct nvme_ns *ns = bdev->bd_disk->private_data;
1809
1810 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001811 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001812 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001813 return ns->ns_id;
1814 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001815 return nvme_user_cmd(ns->dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06001816 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001817 return nvme_user_cmd(ns->dev, ns, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001818 case NVME_IOCTL_SUBMIT_IO:
1819 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001820 case SG_GET_VERSION_NUM:
1821 return nvme_sg_get_version_num((void __user *)arg);
1822 case SG_IO:
1823 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001824 default:
1825 return -ENOTTY;
1826 }
1827}
1828
Keith Busch320a3822013-10-23 13:07:34 -06001829#ifdef CONFIG_COMPAT
1830static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1831 unsigned int cmd, unsigned long arg)
1832{
Keith Busch320a3822013-10-23 13:07:34 -06001833 switch (cmd) {
1834 case SG_IO:
Keith Busche1797292014-08-27 13:55:38 -06001835 return -ENOIOCTLCMD;
Keith Busch320a3822013-10-23 13:07:34 -06001836 }
1837 return nvme_ioctl(bdev, mode, cmd, arg);
1838}
1839#else
1840#define nvme_compat_ioctl NULL
1841#endif
1842
Keith Busch9ac27092014-01-31 16:53:39 -07001843static int nvme_open(struct block_device *bdev, fmode_t mode)
1844{
Keith Busch9e603522014-10-03 11:15:47 -06001845 int ret = 0;
1846 struct nvme_ns *ns;
Keith Busch9ac27092014-01-31 16:53:39 -07001847
Keith Busch9e603522014-10-03 11:15:47 -06001848 spin_lock(&dev_list_lock);
1849 ns = bdev->bd_disk->private_data;
1850 if (!ns)
1851 ret = -ENXIO;
1852 else if (!kref_get_unless_zero(&ns->dev->kref))
1853 ret = -ENXIO;
1854 spin_unlock(&dev_list_lock);
1855
1856 return ret;
Keith Busch9ac27092014-01-31 16:53:39 -07001857}
1858
1859static void nvme_free_dev(struct kref *kref);
1860
1861static void nvme_release(struct gendisk *disk, fmode_t mode)
1862{
1863 struct nvme_ns *ns = disk->private_data;
1864 struct nvme_dev *dev = ns->dev;
1865
1866 kref_put(&dev->kref, nvme_free_dev);
1867}
1868
Keith Busch4cc09e22014-04-02 15:45:37 -06001869static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
1870{
1871 /* some standard values */
1872 geo->heads = 1 << 6;
1873 geo->sectors = 1 << 5;
1874 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1875 return 0;
1876}
1877
Keith Busch1b9dbf72014-09-10 17:21:14 -06001878static int nvme_revalidate_disk(struct gendisk *disk)
1879{
1880 struct nvme_ns *ns = disk->private_data;
1881 struct nvme_dev *dev = ns->dev;
1882 struct nvme_id_ns *id;
1883 dma_addr_t dma_addr;
1884 int lbaf;
1885
1886 id = dma_alloc_coherent(&dev->pci_dev->dev, 4096, &dma_addr,
1887 GFP_KERNEL);
1888 if (!id) {
1889 dev_warn(&dev->pci_dev->dev, "%s: Memory alocation failure\n",
1890 __func__);
1891 return 0;
1892 }
1893
1894 if (nvme_identify(dev, ns->ns_id, 0, dma_addr))
1895 goto free;
1896
1897 lbaf = id->flbas & 0xf;
1898 ns->lba_shift = id->lbaf[lbaf].ds;
1899
1900 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
1901 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1902 free:
1903 dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr);
1904 return 0;
1905}
1906
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001907static const struct block_device_operations nvme_fops = {
1908 .owner = THIS_MODULE,
1909 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06001910 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07001911 .open = nvme_open,
1912 .release = nvme_release,
Keith Busch4cc09e22014-04-02 15:45:37 -06001913 .getgeo = nvme_getgeo,
Keith Busch1b9dbf72014-09-10 17:21:14 -06001914 .revalidate_disk= nvme_revalidate_disk,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001915};
1916
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001917static int nvme_kthread(void *data)
1918{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001919 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001920
1921 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001922 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001923 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001924 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001925 int i;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001926 if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
1927 dev->initialized) {
1928 if (work_busy(&dev->reset_work))
1929 continue;
1930 list_del_init(&dev->node);
1931 dev_warn(&dev->pci_dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001932 "Failed status: %x, reset controller\n",
1933 readl(&dev->bar->csts));
Tejun Heo9ca97372014-03-07 10:24:49 -05001934 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001935 queue_work(nvme_workq, &dev->reset_work);
1936 continue;
1937 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001938 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001939 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001940 if (!nvmeq)
1941 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001942 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001943 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06001944
1945 while ((i == 0) && (dev->event_limit > 0)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001946 if (nvme_submit_async_admin_req(dev))
Keith Busch6fccf932014-06-18 13:58:57 -06001947 break;
1948 dev->event_limit--;
1949 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001950 spin_unlock_irq(&nvmeq->q_lock);
1951 }
1952 }
1953 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001954 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001955 }
1956 return 0;
1957}
1958
Keith Busch0e5e4f02012-11-09 16:33:05 -07001959static void nvme_config_discard(struct nvme_ns *ns)
1960{
1961 u32 logical_block_size = queue_logical_block_size(ns->queue);
1962 ns->queue->limits.discard_zeroes_data = 0;
1963 ns->queue->limits.discard_alignment = logical_block_size;
1964 ns->queue->limits.discard_granularity = logical_block_size;
1965 ns->queue->limits.max_discard_sectors = 0xffffffff;
1966 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1967}
1968
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001969static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001970 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1971{
1972 struct nvme_ns *ns;
1973 struct gendisk *disk;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001974 int node = dev_to_node(&dev->pci_dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001975 int lbaf;
1976
1977 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1978 return NULL;
1979
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001980 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001981 if (!ns)
1982 return NULL;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001983 ns->queue = blk_mq_init_queue(&dev->tagset);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001984 if (IS_ERR(ns->queue))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001985 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001986 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1987 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001988 queue_flag_set_unlocked(QUEUE_FLAG_SG_GAPS, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001989 ns->dev = dev;
1990 ns->queue->queuedata = ns;
1991
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001992 disk = alloc_disk_node(0, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001993 if (!disk)
1994 goto out_free_queue;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001995
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001996 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001997 ns->disk = disk;
1998 lbaf = id->flbas & 0xf;
1999 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Buschf410c682013-04-23 17:23:59 -06002000 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busche9ef4632012-07-24 15:01:04 -06002001 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06002002 if (dev->max_hw_sectors)
2003 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002004 if (dev->stripe_size)
2005 blk_queue_chunk_sectors(ns->queue, dev->stripe_size >> 9);
Keith Buscha7d2ce22014-04-29 11:41:28 -06002006 if (dev->vwc & NVME_CTRL_VWC_PRESENT)
2007 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002008
2009 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002010 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002011 disk->fops = &nvme_fops;
2012 disk->private_data = ns;
2013 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05002014 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002015 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002016 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002017 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
2018
Keith Busch0e5e4f02012-11-09 16:33:05 -07002019 if (dev->oncs & NVME_CTRL_ONCS_DSM)
2020 nvme_config_discard(ns);
2021
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002022 return ns;
2023
2024 out_free_queue:
2025 blk_cleanup_queue(ns->queue);
2026 out_free_ns:
2027 kfree(ns);
2028 return NULL;
2029}
2030
Keith Busch42f61422014-03-24 10:46:25 -06002031static void nvme_create_io_queues(struct nvme_dev *dev)
2032{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002033 unsigned i;
Keith Busch42f61422014-03-24 10:46:25 -06002034
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002035 for (i = dev->queue_count; i <= dev->max_qid; i++)
Keith Busch2b25d982014-12-22 12:59:04 -07002036 if (!nvme_alloc_queue(dev, i, dev->q_depth))
Keith Busch42f61422014-03-24 10:46:25 -06002037 break;
2038
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002039 for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
2040 if (nvme_create_queue(dev->queues[i], i))
Keith Busch42f61422014-03-24 10:46:25 -06002041 break;
2042}
2043
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002044static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002045{
2046 int status;
2047 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002048 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002049
Matthew Wilcoxdf348132012-01-11 07:29:56 -07002050 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002051 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04002052 if (status < 0)
2053 return status;
2054 if (status > 0) {
2055 dev_err(&dev->pci_dev->dev, "Could not set queue count (%d)\n",
2056 status);
Keith Buschbadc34d2014-06-23 14:25:35 -06002057 return 0;
Matthew Wilcox27e81662014-04-11 11:58:45 -04002058 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002059 return min(result & 0xffff, result >> 16) + 1;
2060}
2061
Keith Busch9d713c22013-07-15 15:02:24 -06002062static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
2063{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002064 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06002065}
2066
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002067static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002068{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002069 struct nvme_queue *adminq = dev->queues[0];
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002070 struct pci_dev *pdev = dev->pci_dev;
Keith Busch42f61422014-03-24 10:46:25 -06002071 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002072
Keith Busch42f61422014-03-24 10:46:25 -06002073 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002074 result = set_queue_count(dev, nr_io_queues);
Keith Buschbadc34d2014-06-23 14:25:35 -06002075 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002076 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002077 if (result < nr_io_queues)
2078 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002079
Keith Busch9d713c22013-07-15 15:02:24 -06002080 size = db_bar_size(dev, nr_io_queues);
2081 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002082 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06002083 do {
2084 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
2085 if (dev->bar)
2086 break;
2087 if (!--nr_io_queues)
2088 return -ENOMEM;
2089 size = db_bar_size(dev, nr_io_queues);
2090 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002091 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07002092 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002093 }
2094
Keith Busch9d713c22013-07-15 15:02:24 -06002095 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002096 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002097
Jens Axboee32efbf2014-11-14 09:49:26 -07002098 /*
2099 * If we enable msix early due to not intx, disable it again before
2100 * setting up the full range we need.
2101 */
2102 if (!pdev->irq)
2103 pci_disable_msix(pdev);
2104
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002105 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002106 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002107 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
2108 if (vecs < 0) {
2109 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
2110 if (vecs < 0) {
2111 vecs = 1;
2112 } else {
2113 for (i = 0; i < vecs; i++)
2114 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002115 }
2116 }
2117
Matthew Wilcox063a8092013-06-20 10:53:48 -04002118 /*
2119 * Should investigate if there's a performance win from allocating
2120 * more queues than interrupt vectors; it might allow the submission
2121 * path to scale better, even if the receive path is limited by the
2122 * number of interrupts.
2123 */
2124 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002125 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002126
Matthew Wilcox3193f072014-01-27 15:57:22 -05002127 result = queue_request_irq(dev, adminq, adminq->irqname);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002128 if (result)
Keith Busch22404272013-07-15 15:02:20 -06002129 goto free_queues;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002130
Keith Buschcd638942013-07-15 15:02:23 -06002131 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002132 nvme_free_queues(dev, nr_io_queues + 1);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002133 nvme_create_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06002134
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002135 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002136
Keith Busch22404272013-07-15 15:02:20 -06002137 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002138 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002139 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002140}
2141
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002142/*
2143 * Return: error value if an error occurred setting up the queues or calling
2144 * Identify Device. 0 if these succeeded, even if adding some of the
2145 * namespaces failed. At the moment, these failures are silent. TBD which
2146 * failures should be reported.
2147 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002148static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002149{
Matthew Wilcox68608c262013-06-21 14:36:34 -04002150 struct pci_dev *pdev = dev->pci_dev;
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002151 int res;
2152 unsigned nn, i;
Keith Buschcbb62182013-05-01 13:07:49 -06002153 struct nvme_ns *ns;
Matthew Wilcox51814232011-02-01 16:18:08 -05002154 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002155 struct nvme_id_ns *id_ns;
2156 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002157 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06002158 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002159
Matthew Wilcox68608c262013-06-21 14:36:34 -04002160 mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06002161 if (!mem)
2162 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002163
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002164 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002165 if (res) {
Matthew Wilcox27e81662014-04-11 11:58:45 -04002166 dev_err(&pdev->dev, "Identify Controller failed (%d)\n", res);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002167 res = -EIO;
Keith Buschcbb62182013-05-01 13:07:49 -06002168 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002169 }
2170
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002171 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05002172 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002173 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002174 dev->abort_limit = ctrl->acl + 1;
Keith Buscha7d2ce22014-04-29 11:41:28 -06002175 dev->vwc = ctrl->vwc;
Keith Busch6fccf932014-06-18 13:58:57 -06002176 dev->event_limit = min(ctrl->aerl + 1, 8);
Matthew Wilcox51814232011-02-01 16:18:08 -05002177 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2178 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2179 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002180 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002181 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002182 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002183 (pdev->device == 0x0953) && ctrl->vs[3]) {
2184 unsigned int max_hw_sectors;
2185
Keith Busch159b67d2013-04-09 17:13:20 -06002186 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002187 max_hw_sectors = dev->stripe_size >> (shift - 9);
2188 if (dev->max_hw_sectors) {
2189 dev->max_hw_sectors = min(max_hw_sectors,
2190 dev->max_hw_sectors);
2191 } else
2192 dev->max_hw_sectors = max_hw_sectors;
2193 }
2194
2195 dev->tagset.ops = &nvme_mq_ops;
2196 dev->tagset.nr_hw_queues = dev->online_queues - 1;
2197 dev->tagset.timeout = NVME_IO_TIMEOUT;
2198 dev->tagset.numa_node = dev_to_node(&dev->pci_dev->dev);
2199 dev->tagset.queue_depth =
2200 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Jens Axboeac3dd5b2015-01-22 12:07:58 -07002201 dev->tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002202 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
2203 dev->tagset.driver_data = dev;
2204
2205 if (blk_mq_alloc_tag_set(&dev->tagset))
2206 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002207
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002208 id_ns = mem;
Matthew Wilcox2b2c1892011-10-07 13:10:13 -04002209 for (i = 1; i <= nn; i++) {
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002210 res = nvme_identify(dev, i, 0, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002211 if (res)
2212 continue;
2213
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002214 if (id_ns->ncap == 0)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002215 continue;
2216
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002217 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
Keith Busch08df1e02012-09-21 10:52:13 -06002218 dma_addr + 4096, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002219 if (res)
Keith Busch12209032013-01-31 14:40:38 -07002220 memset(mem + 4096, 0, 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002221
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002222 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002223 if (ns)
2224 list_add_tail(&ns->list, &dev->namespaces);
2225 }
2226 list_for_each_entry(ns, &dev->namespaces, list)
2227 add_disk(ns->disk);
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002228 res = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002229
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002230 out:
Matthew Wilcox684f5c22011-09-19 17:14:53 -04002231 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002232 return res;
2233}
2234
Keith Busch0877cb02013-07-15 15:02:19 -06002235static int nvme_dev_map(struct nvme_dev *dev)
2236{
Keith Busch42f61422014-03-24 10:46:25 -06002237 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002238 int bars, result = -ENOMEM;
2239 struct pci_dev *pdev = dev->pci_dev;
2240
2241 if (pci_enable_device_mem(pdev))
2242 return result;
2243
2244 dev->entry[0].vector = pdev->irq;
2245 pci_set_master(pdev);
2246 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07002247 if (!bars)
2248 goto disable_pci;
2249
Keith Busch0877cb02013-07-15 15:02:19 -06002250 if (pci_request_selected_regions(pdev, bars, "nvme"))
2251 goto disable_pci;
2252
Russell King052d0ef2013-06-26 23:49:11 +01002253 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
2254 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2255 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002256
Keith Busch0877cb02013-07-15 15:02:19 -06002257 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2258 if (!dev->bar)
2259 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07002260
Keith Busch0e53d182013-12-10 13:10:39 -07002261 if (readl(&dev->bar->csts) == -1) {
2262 result = -ENODEV;
2263 goto unmap;
2264 }
Jens Axboee32efbf2014-11-14 09:49:26 -07002265
2266 /*
2267 * Some devices don't advertse INTx interrupts, pre-enable a single
2268 * MSIX vec for setup. We'll adjust this later.
2269 */
2270 if (!pdev->irq) {
2271 result = pci_enable_msix(pdev, dev->entry, 1);
2272 if (result < 0)
2273 goto unmap;
2274 }
2275
Keith Busch42f61422014-03-24 10:46:25 -06002276 cap = readq(&dev->bar->cap);
2277 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2278 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Keith Busch0877cb02013-07-15 15:02:19 -06002279 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2280
2281 return 0;
2282
Keith Busch0e53d182013-12-10 13:10:39 -07002283 unmap:
2284 iounmap(dev->bar);
2285 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002286 disable:
2287 pci_release_regions(pdev);
2288 disable_pci:
2289 pci_disable_device(pdev);
2290 return result;
2291}
2292
2293static void nvme_dev_unmap(struct nvme_dev *dev)
2294{
2295 if (dev->pci_dev->msi_enabled)
2296 pci_disable_msi(dev->pci_dev);
2297 else if (dev->pci_dev->msix_enabled)
2298 pci_disable_msix(dev->pci_dev);
2299
2300 if (dev->bar) {
2301 iounmap(dev->bar);
2302 dev->bar = NULL;
Keith Busch9a6b9452013-12-10 13:10:36 -07002303 pci_release_regions(dev->pci_dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002304 }
2305
Keith Busch0877cb02013-07-15 15:02:19 -06002306 if (pci_is_enabled(dev->pci_dev))
2307 pci_disable_device(dev->pci_dev);
2308}
2309
Keith Busch4d115422013-12-10 13:10:40 -07002310struct nvme_delq_ctx {
2311 struct task_struct *waiter;
2312 struct kthread_worker *worker;
2313 atomic_t refcount;
2314};
2315
2316static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2317{
2318 dq->waiter = current;
2319 mb();
2320
2321 for (;;) {
2322 set_current_state(TASK_KILLABLE);
2323 if (!atomic_read(&dq->refcount))
2324 break;
2325 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2326 fatal_signal_pending(current)) {
Keith Busch0fb59cb2015-01-07 18:55:50 -07002327 /*
2328 * Disable the controller first since we can't trust it
2329 * at this point, but leave the admin queue enabled
2330 * until all queue deletion requests are flushed.
2331 * FIXME: This may take a while if there are more h/w
2332 * queues than admin tags.
2333 */
Keith Busch4d115422013-12-10 13:10:40 -07002334 set_current_state(TASK_RUNNING);
Keith Busch4d115422013-12-10 13:10:40 -07002335 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
Keith Busch0fb59cb2015-01-07 18:55:50 -07002336 nvme_clear_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07002337 flush_kthread_worker(dq->worker);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002338 nvme_disable_queue(dev, 0);
Keith Busch4d115422013-12-10 13:10:40 -07002339 return;
2340 }
2341 }
2342 set_current_state(TASK_RUNNING);
2343}
2344
2345static void nvme_put_dq(struct nvme_delq_ctx *dq)
2346{
2347 atomic_dec(&dq->refcount);
2348 if (dq->waiter)
2349 wake_up_process(dq->waiter);
2350}
2351
2352static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2353{
2354 atomic_inc(&dq->refcount);
2355 return dq;
2356}
2357
2358static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2359{
2360 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
2361
2362 nvme_clear_queue(nvmeq);
2363 nvme_put_dq(dq);
2364}
2365
2366static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2367 kthread_work_func_t fn)
2368{
2369 struct nvme_command c;
2370
2371 memset(&c, 0, sizeof(c));
2372 c.delete_queue.opcode = opcode;
2373 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2374
2375 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002376 return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
2377 ADMIN_TIMEOUT);
Keith Busch4d115422013-12-10 13:10:40 -07002378}
2379
2380static void nvme_del_cq_work_handler(struct kthread_work *work)
2381{
2382 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2383 cmdinfo.work);
2384 nvme_del_queue_end(nvmeq);
2385}
2386
2387static int nvme_delete_cq(struct nvme_queue *nvmeq)
2388{
2389 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2390 nvme_del_cq_work_handler);
2391}
2392
2393static void nvme_del_sq_work_handler(struct kthread_work *work)
2394{
2395 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2396 cmdinfo.work);
2397 int status = nvmeq->cmdinfo.status;
2398
2399 if (!status)
2400 status = nvme_delete_cq(nvmeq);
2401 if (status)
2402 nvme_del_queue_end(nvmeq);
2403}
2404
2405static int nvme_delete_sq(struct nvme_queue *nvmeq)
2406{
2407 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2408 nvme_del_sq_work_handler);
2409}
2410
2411static void nvme_del_queue_start(struct kthread_work *work)
2412{
2413 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2414 cmdinfo.work);
Keith Busch4d115422013-12-10 13:10:40 -07002415 if (nvme_delete_sq(nvmeq))
2416 nvme_del_queue_end(nvmeq);
2417}
2418
2419static void nvme_disable_io_queues(struct nvme_dev *dev)
2420{
2421 int i;
2422 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2423 struct nvme_delq_ctx dq;
2424 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2425 &worker, "nvme%d", dev->instance);
2426
2427 if (IS_ERR(kworker_task)) {
2428 dev_err(&dev->pci_dev->dev,
2429 "Failed to create queue del task\n");
2430 for (i = dev->queue_count - 1; i > 0; i--)
2431 nvme_disable_queue(dev, i);
2432 return;
2433 }
2434
2435 dq.waiter = NULL;
2436 atomic_set(&dq.refcount, 0);
2437 dq.worker = &worker;
2438 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002439 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002440
2441 if (nvme_suspend_queue(nvmeq))
2442 continue;
2443 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2444 nvmeq->cmdinfo.worker = dq.worker;
2445 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2446 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2447 }
2448 nvme_wait_dq(&dq, dev);
2449 kthread_stop(kworker_task);
2450}
2451
Dan McLeranb9afca32014-04-07 17:10:11 -06002452/*
2453* Remove the node from the device list and check
2454* for whether or not we need to stop the nvme_thread.
2455*/
2456static void nvme_dev_list_remove(struct nvme_dev *dev)
2457{
2458 struct task_struct *tmp = NULL;
2459
2460 spin_lock(&dev_list_lock);
2461 list_del_init(&dev->node);
2462 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2463 tmp = nvme_thread;
2464 nvme_thread = NULL;
2465 }
2466 spin_unlock(&dev_list_lock);
2467
2468 if (tmp)
2469 kthread_stop(tmp);
2470}
2471
Keith Buschc9d3bf82015-01-07 18:55:52 -07002472static void nvme_freeze_queues(struct nvme_dev *dev)
2473{
2474 struct nvme_ns *ns;
2475
2476 list_for_each_entry(ns, &dev->namespaces, list) {
2477 blk_mq_freeze_queue_start(ns->queue);
2478
2479 spin_lock(ns->queue->queue_lock);
2480 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
2481 spin_unlock(ns->queue->queue_lock);
2482
2483 blk_mq_cancel_requeue_work(ns->queue);
2484 blk_mq_stop_hw_queues(ns->queue);
2485 }
2486}
2487
2488static void nvme_unfreeze_queues(struct nvme_dev *dev)
2489{
2490 struct nvme_ns *ns;
2491
2492 list_for_each_entry(ns, &dev->namespaces, list) {
2493 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2494 blk_mq_unfreeze_queue(ns->queue);
2495 blk_mq_start_stopped_hw_queues(ns->queue, true);
2496 blk_mq_kick_requeue_list(ns->queue);
2497 }
2498}
2499
Keith Buschf0b50732013-07-15 15:02:21 -06002500static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002501{
Keith Busch22404272013-07-15 15:02:20 -06002502 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06002503 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06002504
Keith Buschd4b4ff82013-12-10 13:10:37 -07002505 dev->initialized = 0;
Dan McLeranb9afca32014-04-07 17:10:11 -06002506 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002507
Keith Buschc9d3bf82015-01-07 18:55:52 -07002508 if (dev->bar) {
2509 nvme_freeze_queues(dev);
Keith Busch7c1b2452014-06-25 11:18:12 -06002510 csts = readl(&dev->bar->csts);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002511 }
Keith Busch7c1b2452014-06-25 11:18:12 -06002512 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07002513 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002514 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002515 nvme_suspend_queue(nvmeq);
2516 nvme_clear_queue(nvmeq);
2517 }
2518 } else {
2519 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002520 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002521 nvme_disable_queue(dev, 0);
2522 }
Keith Buschf0b50732013-07-15 15:02:21 -06002523 nvme_dev_unmap(dev);
2524}
2525
2526static void nvme_dev_remove(struct nvme_dev *dev)
2527{
Keith Busch9ac27092014-01-31 16:53:39 -07002528 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002529
Keith Busch9ac27092014-01-31 16:53:39 -07002530 list_for_each_entry(ns, &dev->namespaces, list) {
2531 if (ns->disk->flags & GENHD_FL_UP)
2532 del_gendisk(ns->disk);
Keith Buschcef6a942015-01-07 18:55:51 -07002533 if (!blk_queue_dying(ns->queue)) {
2534 blk_mq_abort_requeue_list(ns->queue);
Keith Busch9ac27092014-01-31 16:53:39 -07002535 blk_cleanup_queue(ns->queue);
Keith Buschcef6a942015-01-07 18:55:51 -07002536 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002537 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002538}
2539
Matthew Wilcox091b6092011-02-10 09:56:01 -05002540static int nvme_setup_prp_pools(struct nvme_dev *dev)
2541{
2542 struct device *dmadev = &dev->pci_dev->dev;
2543 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2544 PAGE_SIZE, PAGE_SIZE, 0);
2545 if (!dev->prp_page_pool)
2546 return -ENOMEM;
2547
Matthew Wilcox99802a72011-02-10 10:30:34 -05002548 /* Optimisation for I/Os between 4k and 128k */
2549 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2550 256, 256, 0);
2551 if (!dev->prp_small_pool) {
2552 dma_pool_destroy(dev->prp_page_pool);
2553 return -ENOMEM;
2554 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002555 return 0;
2556}
2557
2558static void nvme_release_prp_pools(struct nvme_dev *dev)
2559{
2560 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002561 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002562}
2563
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002564static DEFINE_IDA(nvme_instance_ida);
2565
2566static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002567{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002568 int instance, error;
2569
2570 do {
2571 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2572 return -ENODEV;
2573
2574 spin_lock(&dev_list_lock);
2575 error = ida_get_new(&nvme_instance_ida, &instance);
2576 spin_unlock(&dev_list_lock);
2577 } while (error == -EAGAIN);
2578
2579 if (error)
2580 return -ENODEV;
2581
2582 dev->instance = instance;
2583 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002584}
2585
2586static void nvme_release_instance(struct nvme_dev *dev)
2587{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002588 spin_lock(&dev_list_lock);
2589 ida_remove(&nvme_instance_ida, dev->instance);
2590 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002591}
2592
Keith Busch9ac27092014-01-31 16:53:39 -07002593static void nvme_free_namespaces(struct nvme_dev *dev)
2594{
2595 struct nvme_ns *ns, *next;
2596
2597 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2598 list_del(&ns->list);
Keith Busch9e603522014-10-03 11:15:47 -06002599
2600 spin_lock(&dev_list_lock);
2601 ns->disk->private_data = NULL;
2602 spin_unlock(&dev_list_lock);
2603
Keith Busch9ac27092014-01-31 16:53:39 -07002604 put_disk(ns->disk);
2605 kfree(ns);
2606 }
2607}
2608
Keith Busch5e82e952013-02-19 10:17:58 -07002609static void nvme_free_dev(struct kref *kref)
2610{
2611 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002612
Keith Buscha96d4f52014-08-19 19:15:59 -06002613 pci_dev_put(dev->pci_dev);
Keith Busch9ac27092014-01-31 16:53:39 -07002614 nvme_free_namespaces(dev);
Indraneel M285dffc2014-12-11 08:24:18 -07002615 nvme_release_instance(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002616 blk_mq_free_tag_set(&dev->tagset);
Keith Buschea191d22015-01-07 18:55:49 -07002617 blk_put_queue(dev->admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002618 kfree(dev->queues);
2619 kfree(dev->entry);
2620 kfree(dev);
2621}
2622
2623static int nvme_dev_open(struct inode *inode, struct file *f)
2624{
2625 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
2626 miscdev);
2627 kref_get(&dev->kref);
2628 f->private_data = dev;
2629 return 0;
2630}
2631
2632static int nvme_dev_release(struct inode *inode, struct file *f)
2633{
2634 struct nvme_dev *dev = f->private_data;
2635 kref_put(&dev->kref, nvme_free_dev);
2636 return 0;
2637}
2638
2639static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2640{
2641 struct nvme_dev *dev = f->private_data;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002642 struct nvme_ns *ns;
2643
Keith Busch5e82e952013-02-19 10:17:58 -07002644 switch (cmd) {
2645 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002646 return nvme_user_cmd(dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06002647 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002648 if (list_empty(&dev->namespaces))
2649 return -ENOTTY;
2650 ns = list_first_entry(&dev->namespaces, struct nvme_ns, list);
2651 return nvme_user_cmd(dev, ns, (void __user *)arg);
Keith Busch5e82e952013-02-19 10:17:58 -07002652 default:
2653 return -ENOTTY;
2654 }
2655}
2656
2657static const struct file_operations nvme_dev_fops = {
2658 .owner = THIS_MODULE,
2659 .open = nvme_dev_open,
2660 .release = nvme_dev_release,
2661 .unlocked_ioctl = nvme_dev_ioctl,
2662 .compat_ioctl = nvme_dev_ioctl,
2663};
2664
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002665static void nvme_set_irq_hints(struct nvme_dev *dev)
2666{
2667 struct nvme_queue *nvmeq;
2668 int i;
2669
2670 for (i = 0; i < dev->online_queues; i++) {
2671 nvmeq = dev->queues[i];
2672
2673 if (!nvmeq->hctx)
2674 continue;
2675
2676 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
2677 nvmeq->hctx->cpumask);
2678 }
2679}
2680
Keith Buschf0b50732013-07-15 15:02:21 -06002681static int nvme_dev_start(struct nvme_dev *dev)
2682{
2683 int result;
Dan McLeranb9afca32014-04-07 17:10:11 -06002684 bool start_thread = false;
Keith Buschf0b50732013-07-15 15:02:21 -06002685
2686 result = nvme_dev_map(dev);
2687 if (result)
2688 return result;
2689
2690 result = nvme_configure_admin_queue(dev);
2691 if (result)
2692 goto unmap;
2693
2694 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002695 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2696 start_thread = true;
2697 nvme_thread = NULL;
2698 }
Keith Buschf0b50732013-07-15 15:02:21 -06002699 list_add(&dev->node, &dev_list);
2700 spin_unlock(&dev_list_lock);
2701
Dan McLeranb9afca32014-04-07 17:10:11 -06002702 if (start_thread) {
2703 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
Keith Busch387caa52014-09-22 13:46:19 -06002704 wake_up_all(&nvme_kthread_wait);
Dan McLeranb9afca32014-04-07 17:10:11 -06002705 } else
2706 wait_event_killable(nvme_kthread_wait, nvme_thread);
2707
2708 if (IS_ERR_OR_NULL(nvme_thread)) {
2709 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2710 goto disable;
2711 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002712
2713 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002714 result = nvme_alloc_admin_tags(dev);
2715 if (result)
2716 goto disable;
Dan McLeranb9afca32014-04-07 17:10:11 -06002717
Keith Buschf0b50732013-07-15 15:02:21 -06002718 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002719 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07002720 goto free_tags;
Keith Buschf0b50732013-07-15 15:02:21 -06002721
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002722 nvme_set_irq_hints(dev);
2723
Keith Buschd82e8bf2013-09-05 14:45:07 -06002724 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002725
Keith Busch0fb59cb2015-01-07 18:55:50 -07002726 free_tags:
2727 nvme_dev_remove_admin(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002728 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002729 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06002730 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002731 unmap:
2732 nvme_dev_unmap(dev);
2733 return result;
2734}
2735
Keith Busch9a6b9452013-12-10 13:10:36 -07002736static int nvme_remove_dead_ctrl(void *arg)
2737{
2738 struct nvme_dev *dev = (struct nvme_dev *)arg;
2739 struct pci_dev *pdev = dev->pci_dev;
2740
2741 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06002742 pci_stop_and_remove_bus_device_locked(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002743 kref_put(&dev->kref, nvme_free_dev);
2744 return 0;
2745}
2746
2747static void nvme_remove_disks(struct work_struct *ws)
2748{
Keith Busch9a6b9452013-12-10 13:10:36 -07002749 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2750
Keith Busch5a92e702014-02-21 14:13:44 -07002751 nvme_free_queues(dev, 1);
Keith Busch302c6722014-07-18 11:40:20 -06002752 nvme_dev_remove(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002753}
2754
2755static int nvme_dev_resume(struct nvme_dev *dev)
2756{
2757 int ret;
2758
2759 ret = nvme_dev_start(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002760 if (ret)
Keith Busch9a6b9452013-12-10 13:10:36 -07002761 return ret;
Keith Buschbadc34d2014-06-23 14:25:35 -06002762 if (dev->online_queues < 2) {
Keith Busch9a6b9452013-12-10 13:10:36 -07002763 spin_lock(&dev_list_lock);
Tejun Heo9ca97372014-03-07 10:24:49 -05002764 dev->reset_workfn = nvme_remove_disks;
Keith Busch9a6b9452013-12-10 13:10:36 -07002765 queue_work(nvme_workq, &dev->reset_work);
2766 spin_unlock(&dev_list_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002767 } else {
2768 nvme_unfreeze_queues(dev);
2769 nvme_set_irq_hints(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002770 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002771 dev->initialized = 1;
Keith Busch9a6b9452013-12-10 13:10:36 -07002772 return 0;
2773}
2774
2775static void nvme_dev_reset(struct nvme_dev *dev)
2776{
2777 nvme_dev_shutdown(dev);
2778 if (nvme_dev_resume(dev)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002779 dev_warn(&dev->pci_dev->dev, "Device failed to resume\n");
Keith Busch9a6b9452013-12-10 13:10:36 -07002780 kref_get(&dev->kref);
2781 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2782 dev->instance))) {
2783 dev_err(&dev->pci_dev->dev,
2784 "Failed to start controller remove task\n");
2785 kref_put(&dev->kref, nvme_free_dev);
2786 }
2787 }
2788}
2789
2790static void nvme_reset_failed_dev(struct work_struct *ws)
2791{
2792 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2793 nvme_dev_reset(dev);
2794}
2795
Tejun Heo9ca97372014-03-07 10:24:49 -05002796static void nvme_reset_workfn(struct work_struct *work)
2797{
2798 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
2799 dev->reset_workfn(work);
2800}
2801
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002802static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002803{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002804 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002805 struct nvme_dev *dev;
2806
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002807 node = dev_to_node(&pdev->dev);
2808 if (node == NUMA_NO_NODE)
2809 set_dev_node(&pdev->dev, 0);
2810
2811 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002812 if (!dev)
2813 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002814 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
2815 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002816 if (!dev->entry)
2817 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002818 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2819 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002820 if (!dev->queues)
2821 goto free;
2822
2823 INIT_LIST_HEAD(&dev->namespaces);
Tejun Heo9ca97372014-03-07 10:24:49 -05002824 dev->reset_workfn = nvme_reset_failed_dev;
2825 INIT_WORK(&dev->reset_work, nvme_reset_workfn);
Keith Buscha96d4f52014-08-19 19:15:59 -06002826 dev->pci_dev = pci_dev_get(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002827 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002828 result = nvme_set_instance(dev);
2829 if (result)
Keith Buscha96d4f52014-08-19 19:15:59 -06002830 goto put_pci;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002831
Matthew Wilcox091b6092011-02-10 09:56:01 -05002832 result = nvme_setup_prp_pools(dev);
2833 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002834 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002835
Keith Buschfb35e912014-03-03 11:09:47 -07002836 kref_init(&dev->kref);
Keith Buschf0b50732013-07-15 15:02:21 -06002837 result = nvme_dev_start(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002838 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002839 goto release_pools;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002840
Keith Buschbadc34d2014-06-23 14:25:35 -06002841 if (dev->online_queues > 1)
2842 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002843 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002844 goto shutdown;
Matthew Wilcox740216f2011-02-15 16:28:20 -05002845
Keith Busch5e82e952013-02-19 10:17:58 -07002846 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
2847 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2848 dev->miscdev.parent = &pdev->dev;
2849 dev->miscdev.name = dev->name;
2850 dev->miscdev.fops = &nvme_dev_fops;
2851 result = misc_register(&dev->miscdev);
2852 if (result)
2853 goto remove;
2854
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002855 nvme_set_irq_hints(dev);
2856
Keith Buschd4b4ff82013-12-10 13:10:37 -07002857 dev->initialized = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002858 return 0;
2859
Keith Busch5e82e952013-02-19 10:17:58 -07002860 remove:
2861 nvme_dev_remove(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002862 nvme_dev_remove_admin(dev);
Keith Busch9ac27092014-01-31 16:53:39 -07002863 nvme_free_namespaces(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002864 shutdown:
2865 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002866 release_pools:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002867 nvme_free_queues(dev, 0);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002868 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002869 release:
2870 nvme_release_instance(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002871 put_pci:
2872 pci_dev_put(dev->pci_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002873 free:
2874 kfree(dev->queues);
2875 kfree(dev->entry);
2876 kfree(dev);
2877 return result;
2878}
2879
Keith Buschf0d54a52014-05-02 10:40:43 -06002880static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2881{
Keith Buscha6739472014-06-23 16:03:21 -06002882 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002883
Keith Buscha6739472014-06-23 16:03:21 -06002884 if (prepare)
2885 nvme_dev_shutdown(dev);
2886 else
2887 nvme_dev_resume(dev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002888}
2889
Keith Busch09ece142014-01-27 11:29:40 -05002890static void nvme_shutdown(struct pci_dev *pdev)
2891{
2892 struct nvme_dev *dev = pci_get_drvdata(pdev);
2893 nvme_dev_shutdown(dev);
2894}
2895
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002896static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002897{
2898 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002899
2900 spin_lock(&dev_list_lock);
2901 list_del_init(&dev->node);
2902 spin_unlock(&dev_list_lock);
2903
2904 pci_set_drvdata(pdev, NULL);
2905 flush_work(&dev->reset_work);
Keith Busch5e82e952013-02-19 10:17:58 -07002906 misc_deregister(&dev->miscdev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002907 nvme_dev_shutdown(dev);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002908 nvme_dev_remove(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002909 nvme_dev_remove_admin(dev);
2910 nvme_free_queues(dev, 0);
Keith Busch9a6b9452013-12-10 13:10:36 -07002911 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002912 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002913}
2914
2915/* These functions are yet to be implemented */
2916#define nvme_error_detected NULL
2917#define nvme_dump_registers NULL
2918#define nvme_link_reset NULL
2919#define nvme_slot_reset NULL
2920#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002921
Jingoo Han671a6012014-02-13 11:19:14 +09002922#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002923static int nvme_suspend(struct device *dev)
2924{
2925 struct pci_dev *pdev = to_pci_dev(dev);
2926 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2927
2928 nvme_dev_shutdown(ndev);
2929 return 0;
2930}
2931
2932static int nvme_resume(struct device *dev)
2933{
2934 struct pci_dev *pdev = to_pci_dev(dev);
2935 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002936
Keith Busch9a6b9452013-12-10 13:10:36 -07002937 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Tejun Heo9ca97372014-03-07 10:24:49 -05002938 ndev->reset_workfn = nvme_reset_failed_dev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002939 queue_work(nvme_workq, &ndev->reset_work);
2940 }
2941 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002942}
Jingoo Han671a6012014-02-13 11:19:14 +09002943#endif
Keith Buschcd638942013-07-15 15:02:23 -06002944
2945static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002946
Stephen Hemminger1d352032012-09-07 09:33:17 -07002947static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002948 .error_detected = nvme_error_detected,
2949 .mmio_enabled = nvme_dump_registers,
2950 .link_reset = nvme_link_reset,
2951 .slot_reset = nvme_slot_reset,
2952 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06002953 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002954};
2955
2956/* Move to pci_ids.h later */
2957#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2958
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002959static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002960 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2961 { 0, }
2962};
2963MODULE_DEVICE_TABLE(pci, nvme_id_table);
2964
2965static struct pci_driver nvme_driver = {
2966 .name = "nvme",
2967 .id_table = nvme_id_table,
2968 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002969 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002970 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002971 .driver = {
2972 .pm = &nvme_dev_pm_ops,
2973 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002974 .err_handler = &nvme_err_handler,
2975};
2976
2977static int __init nvme_init(void)
2978{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002979 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002980
Dan McLeranb9afca32014-04-07 17:10:11 -06002981 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002982
Keith Busch9a6b9452013-12-10 13:10:36 -07002983 nvme_workq = create_singlethread_workqueue("nvme");
2984 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06002985 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07002986
Keith Busch5c42ea12012-07-25 16:05:18 -06002987 result = register_blkdev(nvme_major, "nvme");
2988 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002989 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06002990 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002991 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002992
Keith Buschf3db22f2014-06-11 11:51:35 -06002993 result = pci_register_driver(&nvme_driver);
2994 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002995 goto unregister_blkdev;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002996 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002997
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002998 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002999 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003000 kill_workq:
3001 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003002 return result;
3003}
3004
3005static void __exit nvme_exit(void)
3006{
3007 pci_unregister_driver(&nvme_driver);
Keith Buschf3db22f2014-06-11 11:51:35 -06003008 unregister_hotcpu_notifier(&nvme_nb);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003009 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003010 destroy_workqueue(nvme_workq);
Dan McLeranb9afca32014-04-07 17:10:11 -06003011 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04003012 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003013}
3014
3015MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
3016MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07003017MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003018module_init(nvme_init);
3019module_exit(nvme_exit);