blob: bb2b861cfed9f4afb569567e3a524d333d43e09f [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003 * Copyright (c) 2011-2014, Intel Corporation.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050013 */
14
15#include <linux/nvme.h>
Matthew Wilcox8de05532011-05-12 13:50:28 -040016#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050017#include <linux/blkdev.h>
Matias Bjørlinga4aea562014-11-04 08:20:14 -070018#include <linux/blk-mq.h>
Keith Busch42f61422014-03-24 10:46:25 -060019#include <linux/cpu.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040020#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050021#include <linux/errno.h>
22#include <linux/fs.h>
23#include <linux/genhd.h>
Keith Busch4cc09e22014-04-02 15:45:37 -060024#include <linux/hdreg.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040025#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050026#include <linux/init.h>
27#include <linux/interrupt.h>
28#include <linux/io.h>
29#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050030#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050031#include <linux/kernel.h>
32#include <linux/mm.h>
33#include <linux/module.h>
34#include <linux/moduleparam.h>
35#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050036#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040037#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050038#include <linux/sched.h>
39#include <linux/slab.h>
Keith Busche1e5e562015-02-19 13:39:03 -070040#include <linux/t10-pi.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050041#include <linux/types.h>
Vishal Verma5d0f6132013-03-04 18:40:58 -070042#include <scsi/sg.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090043#include <asm-generic/io-64-nonatomic-lo-hi.h>
44
Keith Buschb3fffde2015-02-03 11:21:42 -070045#define NVME_MINORS (1U << MINORBITS)
Keith Busch9d43cf62014-05-13 11:42:02 -060046#define NVME_Q_DEPTH 1024
Matias Bjørlinga4aea562014-11-04 08:20:14 -070047#define NVME_AQ_DEPTH 64
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050048#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
49#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Keith Busch9d43cf62014-05-13 11:42:02 -060050#define ADMIN_TIMEOUT (admin_timeout * HZ)
Dan McLeran2484f402014-07-01 09:33:32 -060051#define SHUTDOWN_TIMEOUT (shutdown_timeout * HZ)
Keith Busch9d43cf62014-05-13 11:42:02 -060052#define IOD_TIMEOUT (retry_time * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050053
Keith Busch9d43cf62014-05-13 11:42:02 -060054static unsigned char admin_timeout = 60;
55module_param(admin_timeout, byte, 0644);
56MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050057
Matthew Wilcoxbd676082014-06-03 23:04:30 -040058unsigned char nvme_io_timeout = 30;
59module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
Keith Buschb3550842014-04-04 11:43:36 -060060MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050061
Keith Busch61e4ce02014-05-13 11:42:01 -060062static unsigned char retry_time = 30;
63module_param(retry_time, byte, 0644);
64MODULE_PARM_DESC(retry_time, "time in seconds to retry failed I/O");
65
Dan McLeran2484f402014-07-01 09:33:32 -060066static unsigned char shutdown_timeout = 5;
67module_param(shutdown_timeout, byte, 0644);
68MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
69
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050070static int nvme_major;
71module_param(nvme_major, int, 0);
72
Keith Buschb3fffde2015-02-03 11:21:42 -070073static int nvme_char_major;
74module_param(nvme_char_major, int, 0);
75
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050076static int use_threaded_interrupts;
77module_param(use_threaded_interrupts, int, 0);
78
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050079static DEFINE_SPINLOCK(dev_list_lock);
80static LIST_HEAD(dev_list);
81static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070082static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060083static wait_queue_head_t nvme_kthread_wait;
Keith Buschf3db22f2014-06-11 11:51:35 -060084static struct notifier_block nvme_nb;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050085
Keith Buschb3fffde2015-02-03 11:21:42 -070086static struct class *nvme_class;
87
Keith Buschd4b4ff82013-12-10 13:10:37 -070088static void nvme_reset_failed_dev(struct work_struct *ws);
Matias Bjørlinga4aea562014-11-04 08:20:14 -070089static int nvme_process_cq(struct nvme_queue *nvmeq);
Keith Buschd4b4ff82013-12-10 13:10:37 -070090
Keith Busch4d115422013-12-10 13:10:40 -070091struct async_cmd_info {
92 struct kthread_work work;
93 struct kthread_worker *worker;
Matias Bjørlinga4aea562014-11-04 08:20:14 -070094 struct request *req;
Keith Busch4d115422013-12-10 13:10:40 -070095 u32 result;
96 int status;
97 void *ctx;
98};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050099
100/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500101 * An NVM Express queue. Each device has at least two (one for admin
102 * commands and one for I/O commands).
103 */
104struct nvme_queue {
Keith Buschf435c282014-07-07 09:14:42 -0600105 struct llist_node node;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500106 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500107 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500108 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500109 spinlock_t q_lock;
110 struct nvme_command *sq_cmds;
111 volatile struct nvme_completion *cqes;
112 dma_addr_t sq_dma_addr;
113 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500114 u32 __iomem *q_db;
115 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700116 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500117 u16 sq_head;
118 u16 sq_tail;
119 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700120 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400121 u8 cq_phase;
122 u8 cqe_seen;
Keith Busch4d115422013-12-10 13:10:40 -0700123 struct async_cmd_info cmdinfo;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700124 struct blk_mq_hw_ctx *hctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500125};
126
127/*
128 * Check we didin't inadvertently grow the command struct
129 */
130static inline void _nvme_check_size(void)
131{
132 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
133 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
134 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
135 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
136 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400137 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700138 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500139 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
140 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
141 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
142 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600143 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500144}
145
Keith Buschedd10d32014-04-03 16:45:23 -0600146typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400147 struct nvme_completion *);
148
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500149struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400150 nvme_completion_fn fn;
151 void *ctx;
Keith Buschc30341d2013-12-10 13:10:38 -0700152 int aborted;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700153 struct nvme_queue *nvmeq;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700154 struct nvme_iod iod[0];
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500155};
156
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700157/*
158 * Max size of iod being embedded in the request payload
159 */
160#define NVME_INT_PAGES 2
161#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->page_size)
162
163/*
164 * Will slightly overestimate the number of pages needed. This is OK
165 * as it only leads to a small amount of wasted memory for the lifetime of
166 * the I/O.
167 */
168static int nvme_npages(unsigned size, struct nvme_dev *dev)
169{
170 unsigned nprps = DIV_ROUND_UP(size + dev->page_size, dev->page_size);
171 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
172}
173
174static unsigned int nvme_cmd_size(struct nvme_dev *dev)
175{
176 unsigned int ret = sizeof(struct nvme_cmd_info);
177
178 ret += sizeof(struct nvme_iod);
179 ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
180 ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
181
182 return ret;
183}
184
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700185static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
186 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500187{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700188 struct nvme_dev *dev = data;
189 struct nvme_queue *nvmeq = dev->queues[0];
190
191 WARN_ON(nvmeq->hctx);
192 nvmeq->hctx = hctx;
193 hctx->driver_data = nvmeq;
194 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500195}
196
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700197static int nvme_admin_init_request(void *data, struct request *req,
198 unsigned int hctx_idx, unsigned int rq_idx,
199 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600200{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700201 struct nvme_dev *dev = data;
202 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
203 struct nvme_queue *nvmeq = dev->queues[0];
204
205 BUG_ON(!nvmeq);
206 cmd->nvmeq = nvmeq;
207 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600208}
209
Jens Axboe2c30540b2014-11-14 09:47:32 -0700210static void nvme_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
211{
212 struct nvme_queue *nvmeq = hctx->driver_data;
213
214 nvmeq->hctx = NULL;
215}
216
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700217static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
218 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500219{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700220 struct nvme_dev *dev = data;
221 struct nvme_queue *nvmeq = dev->queues[
222 (hctx_idx % dev->queue_count) + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500223
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700224 if (!nvmeq->hctx)
225 nvmeq->hctx = hctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500226
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700227 /* nvmeq queues are shared between namespaces. We assume here that
228 * blk-mq map the tags so they match up with the nvme queue tags. */
229 WARN_ON(nvmeq->hctx->tags != hctx->tags);
230
231 hctx->driver_data = nvmeq;
232 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500233}
234
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700235static int nvme_init_request(void *data, struct request *req,
236 unsigned int hctx_idx, unsigned int rq_idx,
237 unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500238{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700239 struct nvme_dev *dev = data;
240 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
241 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
242
243 BUG_ON(!nvmeq);
244 cmd->nvmeq = nvmeq;
245 return 0;
246}
247
248static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
249 nvme_completion_fn handler)
250{
251 cmd->fn = handler;
252 cmd->ctx = ctx;
253 cmd->aborted = 0;
Keith Buschc917dfe2015-01-07 18:55:48 -0700254 blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500255}
256
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700257static void *iod_get_private(struct nvme_iod *iod)
258{
259 return (void *) (iod->private & ~0x1UL);
260}
261
262/*
263 * If bit 0 is set, the iod is embedded in the request payload.
264 */
265static bool iod_should_kfree(struct nvme_iod *iod)
266{
267 return (iod->private & 0x01) == 0;
268}
269
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400270/* Special values must be less than 0x1000 */
271#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500272#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
273#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
274#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500275
Keith Buschedd10d32014-04-03 16:45:23 -0600276static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400277 struct nvme_completion *cqe)
278{
279 if (ctx == CMD_CTX_CANCELLED)
280 return;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400281 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600282 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400283 "completed id %d twice on queue %d\n",
284 cqe->command_id, le16_to_cpup(&cqe->sq_id));
285 return;
286 }
287 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600288 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400289 "invalid id %d completed on queue %d\n",
290 cqe->command_id, le16_to_cpup(&cqe->sq_id));
291 return;
292 }
Keith Buschedd10d32014-04-03 16:45:23 -0600293 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400294}
295
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700296static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
297{
298 void *ctx;
299
300 if (fn)
301 *fn = cmd->fn;
302 ctx = cmd->ctx;
303 cmd->fn = special_completion;
304 cmd->ctx = CMD_CTX_CANCELLED;
305 return ctx;
306}
307
308static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
309 struct nvme_completion *cqe)
310{
311 struct request *req = ctx;
312
313 u32 result = le32_to_cpup(&cqe->result);
314 u16 status = le16_to_cpup(&cqe->status) >> 1;
315
316 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
317 ++nvmeq->dev->event_limit;
318 if (status == NVME_SC_SUCCESS)
319 dev_warn(nvmeq->q_dmadev,
320 "async event result %08x\n", result);
321
Jens Axboe9d135bb2014-11-17 10:43:42 -0700322 blk_mq_free_hctx_request(nvmeq->hctx, req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700323}
324
325static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
326 struct nvme_completion *cqe)
327{
328 struct request *req = ctx;
329
330 u16 status = le16_to_cpup(&cqe->status) >> 1;
331 u32 result = le32_to_cpup(&cqe->result);
332
Jens Axboe9d135bb2014-11-17 10:43:42 -0700333 blk_mq_free_hctx_request(nvmeq->hctx, req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700334
335 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
336 ++nvmeq->dev->abort_limit;
337}
338
Keith Buschedd10d32014-04-03 16:45:23 -0600339static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700340 struct nvme_completion *cqe)
341{
342 struct async_cmd_info *cmdinfo = ctx;
343 cmdinfo->result = le32_to_cpup(&cqe->result);
344 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
345 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
Jens Axboe9d135bb2014-11-17 10:43:42 -0700346 blk_mq_free_hctx_request(nvmeq->hctx, cmdinfo->req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700347}
348
349static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
350 unsigned int tag)
351{
352 struct blk_mq_hw_ctx *hctx = nvmeq->hctx;
353 struct request *req = blk_mq_tag_to_rq(hctx->tags, tag);
354
355 return blk_mq_rq_to_pdu(req);
Keith Busch4d115422013-12-10 13:10:40 -0700356}
357
Matthew Wilcox184d2942011-05-11 21:36:38 -0400358/*
359 * Called with local interrupts disabled and the q_lock held. May not sleep.
360 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700361static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400362 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500363{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700364 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400365 void *ctx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700366 if (tag >= nvmeq->q_depth) {
367 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500368 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400369 }
Keith Busch859361a2012-08-02 14:05:59 -0600370 if (fn)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700371 *fn = cmd->fn;
372 ctx = cmd->ctx;
373 cmd->fn = special_completion;
374 cmd->ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400375 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500376}
377
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500378/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400379 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500380 * @nvmeq: The queue to use
381 * @cmd: The command to send
382 *
383 * Safe to use from interrupt context
384 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700385static int __nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500386{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700387 u16 tail = nvmeq->sq_tail;
388
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500389 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500390 if (++tail == nvmeq->q_depth)
391 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500392 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500393 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500394
395 return 0;
396}
397
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700398static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
399{
400 unsigned long flags;
401 int ret;
402 spin_lock_irqsave(&nvmeq->q_lock, flags);
403 ret = __nvme_submit_cmd(nvmeq, cmd);
404 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
405 return ret;
406}
407
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500408static __le64 **iod_list(struct nvme_iod *iod)
409{
410 return ((void *)iod) + iod->offset;
411}
412
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700413static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
414 unsigned nseg, unsigned long private)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500415{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700416 iod->private = private;
417 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
418 iod->npages = -1;
419 iod->length = nbytes;
420 iod->nents = 0;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500421}
422
423static struct nvme_iod *
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700424__nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
425 unsigned long priv, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500426{
427 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700428 sizeof(__le64 *) * nvme_npages(bytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500429 sizeof(struct scatterlist) * nseg, gfp);
430
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700431 if (iod)
432 iod_init(iod, bytes, nseg, priv);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500433
434 return iod;
435}
436
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700437static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
438 gfp_t gfp)
439{
440 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
441 sizeof(struct nvme_dsm_range);
442 unsigned long mask = 0;
443 struct nvme_iod *iod;
444
445 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
446 size <= NVME_INT_BYTES(dev)) {
447 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
448
449 iod = cmd->iod;
450 mask = 0x01;
451 iod_init(iod, size, rq->nr_phys_segments,
452 (unsigned long) rq | 0x01);
453 return iod;
454 }
455
456 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
457 (unsigned long) rq, gfp);
458}
459
Vishal Verma5d0f6132013-03-04 18:40:58 -0700460void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500461{
Keith Busch1d090622014-06-23 11:34:01 -0600462 const int last_prp = dev->page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500463 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500464 __le64 **list = iod_list(iod);
465 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500466
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500467 if (iod->npages == 0)
468 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
469 for (i = 0; i < iod->npages; i++) {
470 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500471 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500472 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500473 prp_dma = next_prp_dma;
474 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700475
476 if (iod_should_kfree(iod))
477 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500478}
479
Keith Buschb4ff9c82014-08-29 09:06:12 -0600480static int nvme_error_status(u16 status)
481{
482 switch (status & 0x7ff) {
483 case NVME_SC_SUCCESS:
484 return 0;
485 case NVME_SC_CAP_EXCEEDED:
486 return -ENOSPC;
487 default:
488 return -EIO;
489 }
490}
491
Keith Busche1e5e562015-02-19 13:39:03 -0700492static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
493{
494 if (be32_to_cpu(pi->ref_tag) == v)
495 pi->ref_tag = cpu_to_be32(p);
496}
497
498static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
499{
500 if (be32_to_cpu(pi->ref_tag) == p)
501 pi->ref_tag = cpu_to_be32(v);
502}
503
504/**
505 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
506 *
507 * The virtual start sector is the one that was originally submitted by the
508 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
509 * start sector may be different. Remap protection information to match the
510 * physical LBA on writes, and back to the original seed on reads.
511 *
512 * Type 0 and 3 do not have a ref tag, so no remapping required.
513 */
514static void nvme_dif_remap(struct request *req,
515 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
516{
517 struct nvme_ns *ns = req->rq_disk->private_data;
518 struct bio_integrity_payload *bip;
519 struct t10_pi_tuple *pi;
520 void *p, *pmap;
521 u32 i, nlb, ts, phys, virt;
522
523 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
524 return;
525
526 bip = bio_integrity(req->bio);
527 if (!bip)
528 return;
529
530 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
531 if (!pmap)
532 return;
533
534 p = pmap;
535 virt = bip_get_seed(bip);
536 phys = nvme_block_nr(ns, blk_rq_pos(req));
537 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
538 ts = ns->disk->integrity->tuple_size;
539
540 for (i = 0; i < nlb; i++, virt++, phys++) {
541 pi = (struct t10_pi_tuple *)p;
542 dif_swap(phys, virt, pi);
543 p += ts;
544 }
545 kunmap_atomic(pmap);
546}
547
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700548static void req_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500549 struct nvme_completion *cqe)
550{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500551 struct nvme_iod *iod = ctx;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700552 struct request *req = iod_get_private(iod);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700553 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
554
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500555 u16 status = le16_to_cpup(&cqe->status) >> 1;
556
Keith Buschedd10d32014-04-03 16:45:23 -0600557 if (unlikely(status)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700558 if (!(status & NVME_SC_DNR || blk_noretry_request(req))
559 && (jiffies - req->start_time) < req->timeout) {
Keith Buschc9d3bf82015-01-07 18:55:52 -0700560 unsigned long flags;
561
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700562 blk_mq_requeue_request(req);
Keith Buschc9d3bf82015-01-07 18:55:52 -0700563 spin_lock_irqsave(req->q->queue_lock, flags);
564 if (!blk_queue_stopped(req->q))
565 blk_mq_kick_requeue_list(req->q);
566 spin_unlock_irqrestore(req->q->queue_lock, flags);
Keith Buschedd10d32014-04-03 16:45:23 -0600567 return;
568 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700569 req->errors = nvme_error_status(status);
570 } else
571 req->errors = 0;
572
573 if (cmd_rq->aborted)
574 dev_warn(&nvmeq->dev->pci_dev->dev,
575 "completing aborted command with status:%04x\n",
576 status);
577
Keith Busche1e5e562015-02-19 13:39:03 -0700578 if (iod->nents) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700579 dma_unmap_sg(&nvmeq->dev->pci_dev->dev, iod->sg, iod->nents,
580 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busche1e5e562015-02-19 13:39:03 -0700581 if (blk_integrity_rq(req)) {
582 if (!rq_data_dir(req))
583 nvme_dif_remap(req, nvme_dif_complete);
584 dma_unmap_sg(&nvmeq->dev->pci_dev->dev, iod->meta_sg, 1,
585 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
586 }
587 }
Keith Buschedd10d32014-04-03 16:45:23 -0600588 nvme_free_iod(nvmeq->dev, iod);
Keith Busch3291fa52014-04-28 12:30:52 -0600589
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700590 blk_mq_complete_request(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500591}
592
Matthew Wilcox184d2942011-05-11 21:36:38 -0400593/* length is in bytes. gfp flags indicates whether we may sleep. */
Keith Buschedd10d32014-04-03 16:45:23 -0600594int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod, int total_len,
595 gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500596{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500597 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500598 int length = total_len;
599 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500600 int dma_len = sg_dma_len(sg);
601 u64 dma_addr = sg_dma_address(sg);
602 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500603 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500604 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500605 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500606 int nprps, i;
Keith Busch1d090622014-06-23 11:34:01 -0600607 u32 page_size = dev->page_size;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500608
Keith Busch1d090622014-06-23 11:34:01 -0600609 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500610 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500611 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500612
Keith Busch1d090622014-06-23 11:34:01 -0600613 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500614 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600615 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500616 } else {
617 sg = sg_next(sg);
618 dma_addr = sg_dma_address(sg);
619 dma_len = sg_dma_len(sg);
620 }
621
Keith Busch1d090622014-06-23 11:34:01 -0600622 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600623 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500624 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500625 }
626
Keith Busch1d090622014-06-23 11:34:01 -0600627 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500628 if (nprps <= (256 / 8)) {
629 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500630 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500631 } else {
632 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500633 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500634 }
635
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400636 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
637 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600638 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500639 iod->npages = -1;
Keith Busch1d090622014-06-23 11:34:01 -0600640 return (total_len - length) + page_size;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400641 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500642 list[0] = prp_list;
643 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500644 i = 0;
645 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600646 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500647 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400648 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500649 if (!prp_list)
650 return total_len - length;
651 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400652 prp_list[0] = old_prp_list[i - 1];
653 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
654 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500655 }
656 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600657 dma_len -= page_size;
658 dma_addr += page_size;
659 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500660 if (length <= 0)
661 break;
662 if (dma_len > 0)
663 continue;
664 BUG_ON(dma_len < 0);
665 sg = sg_next(sg);
666 dma_addr = sg_dma_address(sg);
667 dma_len = sg_dma_len(sg);
668 }
669
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500670 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500671}
672
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700673/*
674 * We reuse the small pool to allocate the 16-byte range here as it is not
675 * worth having a special pool for these or additional cases to handle freeing
676 * the iod.
677 */
678static void nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
679 struct request *req, struct nvme_iod *iod)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700680{
Keith Buschedd10d32014-04-03 16:45:23 -0600681 struct nvme_dsm_range *range =
682 (struct nvme_dsm_range *)iod_list(iod)[0];
Keith Busch0e5e4f02012-11-09 16:33:05 -0700683 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
684
Keith Busch0e5e4f02012-11-09 16:33:05 -0700685 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700686 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
687 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700688
689 memset(cmnd, 0, sizeof(*cmnd));
690 cmnd->dsm.opcode = nvme_cmd_dsm;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700691 cmnd->dsm.command_id = req->tag;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700692 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
693 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
694 cmnd->dsm.nr = 0;
695 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
696
697 if (++nvmeq->sq_tail == nvmeq->q_depth)
698 nvmeq->sq_tail = 0;
699 writel(nvmeq->sq_tail, nvmeq->q_db);
Keith Busch0e5e4f02012-11-09 16:33:05 -0700700}
701
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700702static void nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500703 int cmdid)
704{
705 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
706
707 memset(cmnd, 0, sizeof(*cmnd));
708 cmnd->common.opcode = nvme_cmd_flush;
709 cmnd->common.command_id = cmdid;
710 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
711
712 if (++nvmeq->sq_tail == nvmeq->q_depth)
713 nvmeq->sq_tail = 0;
714 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500715}
716
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700717static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod,
718 struct nvme_ns *ns)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500719{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700720 struct request *req = iod_get_private(iod);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500721 struct nvme_command *cmnd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700722 u16 control = 0;
723 u32 dsmgmt = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500724
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700725 if (req->cmd_flags & REQ_FUA)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500726 control |= NVME_RW_FUA;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700727 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500728 control |= NVME_RW_LR;
729
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700730 if (req->cmd_flags & REQ_RAHEAD)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500731 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
732
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500733 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500734 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500735
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700736 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
737 cmnd->rw.command_id = req->tag;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500738 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Keith Buschedd10d32014-04-03 16:45:23 -0600739 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
740 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700741 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
742 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
Keith Busche1e5e562015-02-19 13:39:03 -0700743
744 if (blk_integrity_rq(req)) {
745 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(iod->meta_sg));
746 switch (ns->pi_type) {
747 case NVME_NS_DPS_PI_TYPE3:
748 control |= NVME_RW_PRINFO_PRCHK_GUARD;
749 break;
750 case NVME_NS_DPS_PI_TYPE1:
751 case NVME_NS_DPS_PI_TYPE2:
752 control |= NVME_RW_PRINFO_PRCHK_GUARD |
753 NVME_RW_PRINFO_PRCHK_REF;
754 cmnd->rw.reftag = cpu_to_le32(
755 nvme_block_nr(ns, blk_rq_pos(req)));
756 break;
757 }
758 } else if (ns->ms)
759 control |= NVME_RW_PRINFO_PRACT;
760
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500761 cmnd->rw.control = cpu_to_le16(control);
762 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500763
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500764 if (++nvmeq->sq_tail == nvmeq->q_depth)
765 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500766 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500767
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500768 return 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600769}
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500770
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700771static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
772 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600773{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700774 struct nvme_ns *ns = hctx->queue->queuedata;
775 struct nvme_queue *nvmeq = hctx->driver_data;
776 struct request *req = bd->rq;
777 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
Keith Buschedd10d32014-04-03 16:45:23 -0600778 struct nvme_iod *iod;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700779 enum dma_data_direction dma_dir;
Keith Buschedd10d32014-04-03 16:45:23 -0600780
Keith Busche1e5e562015-02-19 13:39:03 -0700781 /*
782 * If formated with metadata, require the block layer provide a buffer
783 * unless this namespace is formated such that the metadata can be
784 * stripped/generated by the controller with PRACT=1.
785 */
786 if (ns->ms && !blk_integrity_rq(req)) {
787 if (!(ns->pi_type && ns->ms == 8)) {
788 req->errors = -EFAULT;
789 blk_mq_complete_request(req);
790 return BLK_MQ_RQ_QUEUE_OK;
791 }
792 }
793
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700794 iod = nvme_alloc_iod(req, ns->dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600795 if (!iod)
Jens Axboefe543032014-12-11 13:58:39 -0700796 return BLK_MQ_RQ_QUEUE_BUSY;
Keith Buschedd10d32014-04-03 16:45:23 -0600797
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700798 if (req->cmd_flags & REQ_DISCARD) {
Keith Buschedd10d32014-04-03 16:45:23 -0600799 void *range;
800 /*
801 * We reuse the small pool to allocate the 16-byte range here
802 * as it is not worth having a special pool for these or
803 * additional cases to handle freeing the iod.
804 */
805 range = dma_pool_alloc(nvmeq->dev->prp_small_pool,
806 GFP_ATOMIC,
807 &iod->first_dma);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700808 if (!range)
Jens Axboefe543032014-12-11 13:58:39 -0700809 goto retry_cmd;
Keith Buschedd10d32014-04-03 16:45:23 -0600810 iod_list(iod)[0] = (__le64 *)range;
811 iod->npages = 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700812 } else if (req->nr_phys_segments) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700813 dma_dir = rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
Keith Buschedd10d32014-04-03 16:45:23 -0600814
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700815 sg_init_table(iod->sg, req->nr_phys_segments);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700816 iod->nents = blk_rq_map_sg(req->q, req, iod->sg);
Jens Axboefe543032014-12-11 13:58:39 -0700817 if (!iod->nents)
818 goto error_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700819
820 if (!dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir))
Jens Axboefe543032014-12-11 13:58:39 -0700821 goto retry_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700822
Jens Axboefe543032014-12-11 13:58:39 -0700823 if (blk_rq_bytes(req) !=
824 nvme_setup_prps(nvmeq->dev, iod, blk_rq_bytes(req), GFP_ATOMIC)) {
825 dma_unmap_sg(&nvmeq->dev->pci_dev->dev, iod->sg,
826 iod->nents, dma_dir);
827 goto retry_cmd;
828 }
Keith Busche1e5e562015-02-19 13:39:03 -0700829 if (blk_integrity_rq(req)) {
830 if (blk_rq_count_integrity_sg(req->q, req->bio) != 1)
831 goto error_cmd;
832
833 sg_init_table(iod->meta_sg, 1);
834 if (blk_rq_map_integrity_sg(
835 req->q, req->bio, iod->meta_sg) != 1)
836 goto error_cmd;
837
838 if (rq_data_dir(req))
839 nvme_dif_remap(req, nvme_dif_prep);
840
841 if (!dma_map_sg(nvmeq->q_dmadev, iod->meta_sg, 1, dma_dir))
842 goto error_cmd;
843 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700844 }
845
Keith Busch9af87852014-12-03 17:07:13 -0700846 nvme_set_info(cmd, iod, req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700847 spin_lock_irq(&nvmeq->q_lock);
848 if (req->cmd_flags & REQ_DISCARD)
849 nvme_submit_discard(nvmeq, ns, req, iod);
850 else if (req->cmd_flags & REQ_FLUSH)
851 nvme_submit_flush(nvmeq, ns, req->tag);
852 else
853 nvme_submit_iod(nvmeq, iod, ns);
854
855 nvme_process_cq(nvmeq);
856 spin_unlock_irq(&nvmeq->q_lock);
857 return BLK_MQ_RQ_QUEUE_OK;
858
Jens Axboefe543032014-12-11 13:58:39 -0700859 error_cmd:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500860 nvme_free_iod(nvmeq->dev, iod);
Jens Axboefe543032014-12-11 13:58:39 -0700861 return BLK_MQ_RQ_QUEUE_ERROR;
862 retry_cmd:
863 nvme_free_iod(nvmeq->dev, iod);
864 return BLK_MQ_RQ_QUEUE_BUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500865}
866
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400867static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500868{
Matthew Wilcox82123462011-01-20 13:24:06 -0500869 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500870
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500871 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500872 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500873
874 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400875 void *ctx;
876 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500877 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500878 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500879 break;
880 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
881 if (++head == nvmeq->q_depth) {
882 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500883 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500884 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700885 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600886 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500887 }
888
889 /* If the controller ignores the cq head doorbell and continuously
890 * writes to the queue, it is theoretically possible to wrap around
891 * the queue twice and mistakenly return IRQ_NONE. Linux only
892 * requires that 0.1% of your interrupts are handled, so this isn't
893 * a big problem.
894 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500895 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400896 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500897
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800898 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500899 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500900 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500901
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400902 nvmeq->cqe_seen = 1;
903 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500904}
905
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700906/* Admin queue isn't initialized as a request queue. If at some point this
907 * happens anyway, make sure to notify the user */
908static int nvme_admin_queue_rq(struct blk_mq_hw_ctx *hctx,
909 const struct blk_mq_queue_data *bd)
Matthew Wilcox7d822452013-06-24 12:03:57 -0400910{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700911 WARN_ON_ONCE(1);
912 return BLK_MQ_RQ_QUEUE_ERROR;
Matthew Wilcox7d822452013-06-24 12:03:57 -0400913}
914
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500915static irqreturn_t nvme_irq(int irq, void *data)
916{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500917 irqreturn_t result;
918 struct nvme_queue *nvmeq = data;
919 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400920 nvme_process_cq(nvmeq);
921 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
922 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500923 spin_unlock(&nvmeq->q_lock);
924 return result;
925}
926
927static irqreturn_t nvme_irq_check(int irq, void *data)
928{
929 struct nvme_queue *nvmeq = data;
930 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
931 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
932 return IRQ_NONE;
933 return IRQ_WAKE_THREAD;
934}
935
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700936static void nvme_abort_cmd_info(struct nvme_queue *nvmeq, struct nvme_cmd_info *
937 cmd_info)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500938{
939 spin_lock_irq(&nvmeq->q_lock);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700940 cancel_cmd_info(cmd_info, NULL);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500941 spin_unlock_irq(&nvmeq->q_lock);
942}
943
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400944struct sync_cmd_info {
945 struct task_struct *task;
946 u32 result;
947 int status;
948};
949
Keith Buschedd10d32014-04-03 16:45:23 -0600950static void sync_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400951 struct nvme_completion *cqe)
952{
953 struct sync_cmd_info *cmdinfo = ctx;
954 cmdinfo->result = le32_to_cpup(&cqe->result);
955 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
956 wake_up_process(cmdinfo->task);
957}
958
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500959/*
960 * Returns 0 on success. If the result is negative, it's a Linux error code;
961 * if the result is positive, it's an NVM Express status code
962 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700963static int nvme_submit_sync_cmd(struct request *req, struct nvme_command *cmd,
Vishal Verma5d0f6132013-03-04 18:40:58 -0700964 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500965{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700966 int ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500967 struct sync_cmd_info cmdinfo;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700968 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
969 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500970
971 cmdinfo.task = current;
972 cmdinfo.status = -EINTR;
973
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700974 cmd->common.command_id = req->tag;
975
976 nvme_set_info(cmd_rq, &cmdinfo, sync_completion);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500977
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500978 set_current_state(TASK_KILLABLE);
Keith Busch4f5099a2014-03-03 16:39:13 -0700979 ret = nvme_submit_cmd(nvmeq, cmd);
980 if (ret) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700981 nvme_finish_cmd(nvmeq, req->tag, NULL);
Keith Busch4f5099a2014-03-03 16:39:13 -0700982 set_current_state(TASK_RUNNING);
Keith Busch4f5099a2014-03-03 16:39:13 -0700983 }
Jens Axboe849c6e72014-12-12 08:53:40 -0700984 ret = schedule_timeout(timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500985
Jens Axboe849c6e72014-12-12 08:53:40 -0700986 /*
987 * Ensure that sync_completion has either run, or that it will
988 * never run.
989 */
990 nvme_abort_cmd_info(nvmeq, blk_mq_rq_to_pdu(req));
991
992 /*
993 * We never got the completion
994 */
995 if (cmdinfo.status == -EINTR)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500996 return -EINTR;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500997
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500998 if (result)
999 *result = cmdinfo.result;
1000
1001 return cmdinfo.status;
1002}
1003
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001004static int nvme_submit_async_admin_req(struct nvme_dev *dev)
1005{
1006 struct nvme_queue *nvmeq = dev->queues[0];
1007 struct nvme_command c;
1008 struct nvme_cmd_info *cmd_info;
1009 struct request *req;
1010
Jens Axboe6dcc0cf2014-11-18 08:21:18 -07001011 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC, false);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001012 if (IS_ERR(req))
1013 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001014
Keith Buschc917dfe2015-01-07 18:55:48 -07001015 req->cmd_flags |= REQ_NO_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001016 cmd_info = blk_mq_rq_to_pdu(req);
1017 nvme_set_info(cmd_info, req, async_req_completion);
1018
1019 memset(&c, 0, sizeof(c));
1020 c.common.opcode = nvme_admin_async_event;
1021 c.common.command_id = req->tag;
1022
1023 return __nvme_submit_cmd(nvmeq, &c);
1024}
1025
1026static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
Keith Busch4d115422013-12-10 13:10:40 -07001027 struct nvme_command *cmd,
1028 struct async_cmd_info *cmdinfo, unsigned timeout)
1029{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001030 struct nvme_queue *nvmeq = dev->queues[0];
1031 struct request *req;
1032 struct nvme_cmd_info *cmd_rq;
Keith Busch4d115422013-12-10 13:10:40 -07001033
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001034 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_KERNEL, false);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001035 if (IS_ERR(req))
1036 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001037
1038 req->timeout = timeout;
1039 cmd_rq = blk_mq_rq_to_pdu(req);
1040 cmdinfo->req = req;
1041 nvme_set_info(cmd_rq, cmdinfo, async_completion);
Keith Busch4d115422013-12-10 13:10:40 -07001042 cmdinfo->status = -EINTR;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001043
1044 cmd->common.command_id = req->tag;
1045
Keith Busch4f5099a2014-03-03 16:39:13 -07001046 return nvme_submit_cmd(nvmeq, cmd);
Keith Busch4d115422013-12-10 13:10:40 -07001047}
1048
kbuild test robota64e6bb2014-11-05 18:47:07 +08001049static int __nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001050 u32 *result, unsigned timeout)
1051{
1052 int res;
1053 struct request *req;
1054
1055 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_KERNEL, false);
Jens Axboe97fe3832014-12-10 13:02:44 -07001056 if (IS_ERR(req))
1057 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001058 res = nvme_submit_sync_cmd(req, cmd, result, timeout);
Jens Axboe9d135bb2014-11-17 10:43:42 -07001059 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001060 return res;
1061}
1062
Vishal Verma5d0f6132013-03-04 18:40:58 -07001063int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001064 u32 *result)
1065{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001066 return __nvme_submit_admin_cmd(dev, cmd, result, ADMIN_TIMEOUT);
Keith Busch4f5099a2014-03-03 16:39:13 -07001067}
1068
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001069int nvme_submit_io_cmd(struct nvme_dev *dev, struct nvme_ns *ns,
1070 struct nvme_command *cmd, u32 *result)
Keith Busch4f5099a2014-03-03 16:39:13 -07001071{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001072 int res;
1073 struct request *req;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001074
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001075 req = blk_mq_alloc_request(ns->queue, WRITE, (GFP_KERNEL|__GFP_WAIT),
1076 false);
Jens Axboe97fe3832014-12-10 13:02:44 -07001077 if (IS_ERR(req))
1078 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001079 res = nvme_submit_sync_cmd(req, cmd, result, NVME_IO_TIMEOUT);
Jens Axboe9d135bb2014-11-17 10:43:42 -07001080 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001081 return res;
Keith Busch4d115422013-12-10 13:10:40 -07001082}
1083
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001084static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1085{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001086 struct nvme_command c;
1087
1088 memset(&c, 0, sizeof(c));
1089 c.delete_queue.opcode = opcode;
1090 c.delete_queue.qid = cpu_to_le16(id);
1091
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001092 return nvme_submit_admin_cmd(dev, &c, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001093}
1094
1095static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1096 struct nvme_queue *nvmeq)
1097{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001098 struct nvme_command c;
1099 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1100
1101 memset(&c, 0, sizeof(c));
1102 c.create_cq.opcode = nvme_admin_create_cq;
1103 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1104 c.create_cq.cqid = cpu_to_le16(qid);
1105 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1106 c.create_cq.cq_flags = cpu_to_le16(flags);
1107 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1108
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001109 return nvme_submit_admin_cmd(dev, &c, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001110}
1111
1112static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1113 struct nvme_queue *nvmeq)
1114{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001115 struct nvme_command c;
1116 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1117
1118 memset(&c, 0, sizeof(c));
1119 c.create_sq.opcode = nvme_admin_create_sq;
1120 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1121 c.create_sq.sqid = cpu_to_le16(qid);
1122 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1123 c.create_sq.sq_flags = cpu_to_le16(flags);
1124 c.create_sq.cqid = cpu_to_le16(qid);
1125
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001126 return nvme_submit_admin_cmd(dev, &c, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001127}
1128
1129static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1130{
1131 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1132}
1133
1134static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1135{
1136 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1137}
1138
Vishal Verma5d0f6132013-03-04 18:40:58 -07001139int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001140 dma_addr_t dma_addr)
1141{
1142 struct nvme_command c;
1143
1144 memset(&c, 0, sizeof(c));
1145 c.identify.opcode = nvme_admin_identify;
1146 c.identify.nsid = cpu_to_le32(nsid);
1147 c.identify.prp1 = cpu_to_le64(dma_addr);
1148 c.identify.cns = cpu_to_le32(cns);
1149
1150 return nvme_submit_admin_cmd(dev, &c, NULL);
1151}
1152
Vishal Verma5d0f6132013-03-04 18:40:58 -07001153int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -06001154 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001155{
1156 struct nvme_command c;
1157
1158 memset(&c, 0, sizeof(c));
1159 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -06001160 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001161 c.features.prp1 = cpu_to_le64(dma_addr);
1162 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001163
Keith Busch08df1e02012-09-21 10:52:13 -06001164 return nvme_submit_admin_cmd(dev, &c, result);
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001165}
1166
Vishal Verma5d0f6132013-03-04 18:40:58 -07001167int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
1168 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001169{
1170 struct nvme_command c;
1171
1172 memset(&c, 0, sizeof(c));
1173 c.features.opcode = nvme_admin_set_features;
1174 c.features.prp1 = cpu_to_le64(dma_addr);
1175 c.features.fid = cpu_to_le32(fid);
1176 c.features.dword11 = cpu_to_le32(dword11);
1177
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001178 return nvme_submit_admin_cmd(dev, &c, result);
1179}
1180
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001181/**
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001182 * nvme_abort_req - Attempt aborting a request
Keith Buschc30341d2013-12-10 13:10:38 -07001183 *
1184 * Schedule controller reset if the command was already aborted once before and
1185 * still hasn't been returned to the driver, or if this is the admin queue.
1186 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001187static void nvme_abort_req(struct request *req)
Keith Buschc30341d2013-12-10 13:10:38 -07001188{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001189 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1190 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001191 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001192 struct request *abort_req;
1193 struct nvme_cmd_info *abort_cmd;
1194 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -07001195
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001196 if (!nvmeq->qid || cmd_rq->aborted) {
Keith Busch7a509a62015-01-07 18:55:53 -07001197 unsigned long flags;
1198
1199 spin_lock_irqsave(&dev_list_lock, flags);
Keith Buschc30341d2013-12-10 13:10:38 -07001200 if (work_busy(&dev->reset_work))
Keith Busch7a509a62015-01-07 18:55:53 -07001201 goto out;
Keith Buschc30341d2013-12-10 13:10:38 -07001202 list_del_init(&dev->node);
1203 dev_warn(&dev->pci_dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001204 "I/O %d QID %d timeout, reset controller\n",
1205 req->tag, nvmeq->qid);
Tejun Heo9ca97372014-03-07 10:24:49 -05001206 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschc30341d2013-12-10 13:10:38 -07001207 queue_work(nvme_workq, &dev->reset_work);
Keith Busch7a509a62015-01-07 18:55:53 -07001208 out:
1209 spin_unlock_irqrestore(&dev_list_lock, flags);
Keith Buschc30341d2013-12-10 13:10:38 -07001210 return;
1211 }
1212
1213 if (!dev->abort_limit)
1214 return;
1215
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001216 abort_req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC,
1217 false);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001218 if (IS_ERR(abort_req))
Keith Buschc30341d2013-12-10 13:10:38 -07001219 return;
1220
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001221 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1222 nvme_set_info(abort_cmd, abort_req, abort_completion);
1223
Keith Buschc30341d2013-12-10 13:10:38 -07001224 memset(&cmd, 0, sizeof(cmd));
1225 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001226 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001227 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001228 cmd.abort.command_id = abort_req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001229
1230 --dev->abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001231 cmd_rq->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -07001232
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001233 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", req->tag,
Keith Buschc30341d2013-12-10 13:10:38 -07001234 nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001235 if (nvme_submit_cmd(dev->queues[0], &cmd) < 0) {
1236 dev_warn(nvmeq->q_dmadev,
1237 "Could not abort I/O %d QID %d",
1238 req->tag, nvmeq->qid);
Sam Bradshawc87fd542014-12-10 13:00:31 -07001239 blk_mq_free_request(abort_req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001240 }
Keith Buschc30341d2013-12-10 13:10:38 -07001241}
1242
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001243static void nvme_cancel_queue_ios(struct blk_mq_hw_ctx *hctx,
1244 struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001245{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001246 struct nvme_queue *nvmeq = data;
1247 void *ctx;
1248 nvme_completion_fn fn;
1249 struct nvme_cmd_info *cmd;
Keith Buschcef6a942015-01-07 18:55:51 -07001250 struct nvme_completion cqe;
1251
1252 if (!blk_mq_request_started(req))
1253 return;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001254
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001255 cmd = blk_mq_rq_to_pdu(req);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001256
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001257 if (cmd->ctx == CMD_CTX_CANCELLED)
1258 return;
1259
Keith Buschcef6a942015-01-07 18:55:51 -07001260 if (blk_queue_dying(req->q))
1261 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1262 else
1263 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1264
1265
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001266 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1267 req->tag, nvmeq->qid);
1268 ctx = cancel_cmd_info(cmd, &fn);
1269 fn(nvmeq, ctx, &cqe);
1270}
1271
1272static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
1273{
1274 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
1275 struct nvme_queue *nvmeq = cmd->nvmeq;
1276
Keith Busch7a509a62015-01-07 18:55:53 -07001277 /*
1278 * The aborted req will be completed on receiving the abort req.
1279 * We enable the timer again. If hit twice, it'll cause a device reset,
1280 * as the device then is in a faulty state.
1281 */
1282 int ret = BLK_EH_RESET_TIMER;
1283
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001284 dev_warn(nvmeq->q_dmadev, "Timeout I/O %d QID %d\n", req->tag,
1285 nvmeq->qid);
Keith Buschc917dfe2015-01-07 18:55:48 -07001286
Keith Busch7a509a62015-01-07 18:55:53 -07001287 spin_lock_irq(&nvmeq->q_lock);
Keith Buschc917dfe2015-01-07 18:55:48 -07001288 if (!nvmeq->dev->initialized) {
1289 /*
1290 * Force cancelled command frees the request, which requires we
1291 * return BLK_EH_NOT_HANDLED.
1292 */
1293 nvme_cancel_queue_ios(nvmeq->hctx, req, nvmeq, reserved);
Keith Busch7a509a62015-01-07 18:55:53 -07001294 ret = BLK_EH_NOT_HANDLED;
1295 } else
1296 nvme_abort_req(req);
1297 spin_unlock_irq(&nvmeq->q_lock);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001298
Keith Busch7a509a62015-01-07 18:55:53 -07001299 return ret;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001300}
1301
Keith Buschf435c282014-07-07 09:14:42 -06001302static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001303{
1304 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1305 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1306 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1307 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1308 kfree(nvmeq);
1309}
1310
Keith Buscha1a5ef92013-12-16 13:50:00 -05001311static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001312{
1313 int i;
1314
Keith Buscha1a5ef92013-12-16 13:50:00 -05001315 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001316 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001317 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001318 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001319 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001320 }
Keith Busch22404272013-07-15 15:02:20 -06001321}
1322
Keith Busch4d115422013-12-10 13:10:40 -07001323/**
1324 * nvme_suspend_queue - put queue into suspended state
1325 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001326 */
1327static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001328{
Keith Busch2b25d982014-12-22 12:59:04 -07001329 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001330
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001331 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001332 if (nvmeq->cq_vector == -1) {
1333 spin_unlock_irq(&nvmeq->q_lock);
1334 return 1;
1335 }
1336 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Keith Busch42f61422014-03-24 10:46:25 -06001337 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001338 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001339 spin_unlock_irq(&nvmeq->q_lock);
1340
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001341 irq_set_affinity_hint(vector, NULL);
1342 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001343
Keith Busch4d115422013-12-10 13:10:40 -07001344 return 0;
1345}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001346
Keith Busch4d115422013-12-10 13:10:40 -07001347static void nvme_clear_queue(struct nvme_queue *nvmeq)
1348{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001349 struct blk_mq_hw_ctx *hctx = nvmeq->hctx;
1350
Keith Busch22404272013-07-15 15:02:20 -06001351 spin_lock_irq(&nvmeq->q_lock);
1352 nvme_process_cq(nvmeq);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001353 if (hctx && hctx->tags)
1354 blk_mq_tag_busy_iter(hctx, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001355 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001356}
1357
Keith Busch4d115422013-12-10 13:10:40 -07001358static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1359{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001360 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001361
1362 if (!nvmeq)
1363 return;
1364 if (nvme_suspend_queue(nvmeq))
1365 return;
1366
Keith Busch0e53d182013-12-10 13:10:39 -07001367 /* Don't tell the adapter to delete the admin queue.
1368 * Don't tell a removed adapter to delete IO queues. */
1369 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001370 adapter_delete_sq(dev, qid);
1371 adapter_delete_cq(dev, qid);
1372 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001373 if (!qid && dev->admin_q)
1374 blk_mq_freeze_queue_start(dev->admin_q);
Keith Busch4d115422013-12-10 13:10:40 -07001375 nvme_clear_queue(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001376}
1377
1378static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Keith Busch2b25d982014-12-22 12:59:04 -07001379 int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001380{
1381 struct device *dmadev = &dev->pci_dev->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001382 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001383 if (!nvmeq)
1384 return NULL;
1385
Joe Perches4d51abf2014-06-15 13:37:33 -07001386 nvmeq->cqes = dma_zalloc_coherent(dmadev, CQ_SIZE(depth),
1387 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001388 if (!nvmeq->cqes)
1389 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001390
1391 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
1392 &nvmeq->sq_dma_addr, GFP_KERNEL);
1393 if (!nvmeq->sq_cmds)
1394 goto free_cqdma;
1395
1396 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001397 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001398 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1399 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001400 spin_lock_init(&nvmeq->q_lock);
1401 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001402 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001403 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001404 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001405 nvmeq->qid = qid;
Keith Busch22404272013-07-15 15:02:20 -06001406 dev->queue_count++;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001407 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001408
1409 return nvmeq;
1410
1411 free_cqdma:
Keith Busch68b8eca2013-05-01 13:07:47 -06001412 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001413 nvmeq->cq_dma_addr);
1414 free_nvmeq:
1415 kfree(nvmeq);
1416 return NULL;
1417}
1418
Matthew Wilcox30010822011-01-20 09:10:15 -05001419static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1420 const char *name)
1421{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001422 if (use_threaded_interrupts)
1423 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001424 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001425 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001426 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001427 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001428}
1429
Keith Busch22404272013-07-15 15:02:20 -06001430static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001431{
Keith Busch22404272013-07-15 15:02:20 -06001432 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001433
Keith Busch7be50e92014-09-10 15:48:47 -06001434 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001435 nvmeq->sq_tail = 0;
1436 nvmeq->cq_head = 0;
1437 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001438 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001439 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001440 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001441 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001442}
1443
1444static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1445{
1446 struct nvme_dev *dev = nvmeq->dev;
1447 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001448
Keith Busch2b25d982014-12-22 12:59:04 -07001449 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001450 result = adapter_alloc_cq(dev, qid, nvmeq);
1451 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001452 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001453
1454 result = adapter_alloc_sq(dev, qid, nvmeq);
1455 if (result < 0)
1456 goto release_cq;
1457
Matthew Wilcox3193f072014-01-27 15:57:22 -05001458 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001459 if (result < 0)
1460 goto release_sq;
1461
Keith Busch22404272013-07-15 15:02:20 -06001462 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001463 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001464
1465 release_sq:
1466 adapter_delete_sq(dev, qid);
1467 release_cq:
1468 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001469 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001470}
1471
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001472static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1473{
1474 unsigned long timeout;
1475 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1476
1477 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1478
1479 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1480 msleep(100);
1481 if (fatal_signal_pending(current))
1482 return -EINTR;
1483 if (time_after(jiffies, timeout)) {
1484 dev_err(&dev->pci_dev->dev,
Matthew Wilcox27e81662014-04-11 11:58:45 -04001485 "Device not ready; aborting %s\n", enabled ?
1486 "initialisation" : "reset");
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001487 return -ENODEV;
1488 }
1489 }
1490
1491 return 0;
1492}
1493
1494/*
1495 * If the device has been passed off to us in an enabled state, just clear
1496 * the enabled bit. The spec says we should set the 'shutdown notification
1497 * bits', but doing so may cause the device to complete commands to the
1498 * admin queue ... and we don't know what memory that might be pointing at!
1499 */
1500static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1501{
Dan McLeran01079522014-06-23 08:24:36 -06001502 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1503 dev->ctrl_config &= ~NVME_CC_ENABLE;
1504 writel(dev->ctrl_config, &dev->bar->cc);
Matthew Wilcox44af1462013-05-04 06:43:17 -04001505
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001506 return nvme_wait_ready(dev, cap, false);
1507}
1508
1509static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1510{
Dan McLeran01079522014-06-23 08:24:36 -06001511 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1512 dev->ctrl_config |= NVME_CC_ENABLE;
1513 writel(dev->ctrl_config, &dev->bar->cc);
1514
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001515 return nvme_wait_ready(dev, cap, true);
1516}
1517
Keith Busch1894d8f2013-07-15 15:02:22 -06001518static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1519{
1520 unsigned long timeout;
Keith Busch1894d8f2013-07-15 15:02:22 -06001521
Dan McLeran01079522014-06-23 08:24:36 -06001522 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1523 dev->ctrl_config |= NVME_CC_SHN_NORMAL;
1524
1525 writel(dev->ctrl_config, &dev->bar->cc);
Keith Busch1894d8f2013-07-15 15:02:22 -06001526
Dan McLeran2484f402014-07-01 09:33:32 -06001527 timeout = SHUTDOWN_TIMEOUT + jiffies;
Keith Busch1894d8f2013-07-15 15:02:22 -06001528 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1529 NVME_CSTS_SHST_CMPLT) {
1530 msleep(100);
1531 if (fatal_signal_pending(current))
1532 return -EINTR;
1533 if (time_after(jiffies, timeout)) {
1534 dev_err(&dev->pci_dev->dev,
1535 "Device shutdown incomplete; abort shutdown\n");
1536 return -ENODEV;
1537 }
1538 }
1539
1540 return 0;
1541}
1542
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001543static struct blk_mq_ops nvme_mq_admin_ops = {
1544 .queue_rq = nvme_admin_queue_rq,
1545 .map_queue = blk_mq_map_queue,
1546 .init_hctx = nvme_admin_init_hctx,
Jens Axboe2c30540b2014-11-14 09:47:32 -07001547 .exit_hctx = nvme_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001548 .init_request = nvme_admin_init_request,
1549 .timeout = nvme_timeout,
1550};
1551
1552static struct blk_mq_ops nvme_mq_ops = {
1553 .queue_rq = nvme_queue_rq,
1554 .map_queue = blk_mq_map_queue,
1555 .init_hctx = nvme_init_hctx,
Jens Axboe2c30540b2014-11-14 09:47:32 -07001556 .exit_hctx = nvme_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001557 .init_request = nvme_init_request,
1558 .timeout = nvme_timeout,
1559};
1560
Keith Buschea191d22015-01-07 18:55:49 -07001561static void nvme_dev_remove_admin(struct nvme_dev *dev)
1562{
1563 if (dev->admin_q && !blk_queue_dying(dev->admin_q)) {
1564 blk_cleanup_queue(dev->admin_q);
1565 blk_mq_free_tag_set(&dev->admin_tagset);
1566 }
1567}
1568
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001569static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1570{
1571 if (!dev->admin_q) {
1572 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1573 dev->admin_tagset.nr_hw_queues = 1;
1574 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1;
1575 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
1576 dev->admin_tagset.numa_node = dev_to_node(&dev->pci_dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001577 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001578 dev->admin_tagset.driver_data = dev;
1579
1580 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1581 return -ENOMEM;
1582
1583 dev->admin_q = blk_mq_init_queue(&dev->admin_tagset);
Ming Lei35b489d2015-01-02 14:25:27 +00001584 if (IS_ERR(dev->admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001585 blk_mq_free_tag_set(&dev->admin_tagset);
1586 return -ENOMEM;
1587 }
Keith Buschea191d22015-01-07 18:55:49 -07001588 if (!blk_get_queue(dev->admin_q)) {
1589 nvme_dev_remove_admin(dev);
1590 return -ENODEV;
1591 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001592 } else
1593 blk_mq_unfreeze_queue(dev->admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001594
1595 return 0;
1596}
1597
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001598static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001599{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001600 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001601 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001602 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001603 struct nvme_queue *nvmeq;
Keith Busch1d090622014-06-23 11:34:01 -06001604 unsigned page_shift = PAGE_SHIFT;
1605 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12;
1606 unsigned dev_page_max = NVME_CAP_MPSMAX(cap) + 12;
1607
1608 if (page_shift < dev_page_min) {
1609 dev_err(&dev->pci_dev->dev,
1610 "Minimum device page size (%u) too large for "
1611 "host (%u)\n", 1 << dev_page_min,
1612 1 << page_shift);
1613 return -ENODEV;
1614 }
1615 if (page_shift > dev_page_max) {
1616 dev_info(&dev->pci_dev->dev,
1617 "Device maximum page size (%u) smaller than "
1618 "host (%u); enabling work-around\n",
1619 1 << dev_page_max, 1 << page_shift);
1620 page_shift = dev_page_max;
1621 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001622
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001623 result = nvme_disable_ctrl(dev, cap);
1624 if (result < 0)
1625 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001626
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001627 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001628 if (!nvmeq) {
Keith Busch2b25d982014-12-22 12:59:04 -07001629 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001630 if (!nvmeq)
1631 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001632 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001633
1634 aqa = nvmeq->q_depth - 1;
1635 aqa |= aqa << 16;
1636
Keith Busch1d090622014-06-23 11:34:01 -06001637 dev->page_size = 1 << page_shift;
1638
Dan McLeran01079522014-06-23 08:24:36 -06001639 dev->ctrl_config = NVME_CC_CSS_NVM;
Keith Busch1d090622014-06-23 11:34:01 -06001640 dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001641 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001642 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001643
1644 writel(aqa, &dev->bar->aqa);
1645 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1646 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001647
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001648 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001649 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001650 goto free_nvmeq;
1651
Keith Busch2b25d982014-12-22 12:59:04 -07001652 nvmeq->cq_vector = 0;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001653 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Keith Busch025c5572013-05-01 13:07:51 -06001654 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07001655 goto free_nvmeq;
Keith Busch025c5572013-05-01 13:07:51 -06001656
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001657 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001658
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001659 free_nvmeq:
1660 nvme_free_queues(dev, 0);
1661 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001662}
1663
Vishal Verma5d0f6132013-03-04 18:40:58 -07001664struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001665 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001666{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001667 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001668 struct scatterlist *sg;
1669 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001670 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001671
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001672 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001673 return ERR_PTR(-EINVAL);
Dan Carpenter5460fc02013-05-13 17:59:50 +03001674 if (!length || length > INT_MAX - PAGE_SIZE)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001675 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001676
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001677 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001678 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1679 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001680 if (!pages)
1681 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001682
1683 err = get_user_pages_fast(addr, count, 1, pages);
1684 if (err < count) {
1685 count = err;
1686 err = -EFAULT;
1687 goto put_pages;
1688 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001689
Santosh Y6808c5f2014-05-29 10:01:52 +05301690 err = -ENOMEM;
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001691 iod = __nvme_alloc_iod(count, length, dev, 0, GFP_KERNEL);
Santosh Y6808c5f2014-05-29 10:01:52 +05301692 if (!iod)
1693 goto put_pages;
1694
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001695 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001696 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001697 for (i = 0; i < count; i++) {
1698 sg_set_page(&sg[i], pages[i],
Dan Carpenter5460fc02013-05-13 17:59:50 +03001699 min_t(unsigned, length, PAGE_SIZE - offset),
1700 offset);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001701 length -= (PAGE_SIZE - offset);
1702 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001703 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001704 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001705 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001706
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001707 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1708 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001709 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001710 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001711
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001712 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001713 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001714
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001715 free_iod:
1716 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001717 put_pages:
1718 for (i = 0; i < count; i++)
1719 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001720 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001721 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001722}
1723
Vishal Verma5d0f6132013-03-04 18:40:58 -07001724void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001725 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001726{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001727 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001728
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001729 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1730 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001731
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001732 for (i = 0; i < iod->nents; i++)
1733 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001734}
1735
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001736static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1737{
1738 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001739 struct nvme_user_io io;
1740 struct nvme_command c;
Keith Buschf410c682013-04-23 17:23:59 -06001741 unsigned length, meta_len;
1742 int status, i;
1743 struct nvme_iod *iod, *meta_iod = NULL;
1744 dma_addr_t meta_dma_addr;
1745 void *meta, *uninitialized_var(meta_mem);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001746
1747 if (copy_from_user(&io, uio, sizeof(io)))
1748 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001749 length = (io.nblocks + 1) << ns->lba_shift;
Keith Buschf410c682013-04-23 17:23:59 -06001750 meta_len = (io.nblocks + 1) * ns->ms;
1751
1752 if (meta_len && ((io.metadata & 3) || !io.metadata))
1753 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001754
1755 switch (io.opcode) {
1756 case nvme_cmd_write:
1757 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001758 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001759 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001760 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001761 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001762 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001763 }
1764
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001765 if (IS_ERR(iod))
1766 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001767
1768 memset(&c, 0, sizeof(c));
1769 c.rw.opcode = io.opcode;
1770 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001771 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001772 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001773 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001774 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001775 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1776 c.rw.reftag = cpu_to_le32(io.reftag);
1777 c.rw.apptag = cpu_to_le16(io.apptag);
1778 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buschf410c682013-04-23 17:23:59 -06001779
1780 if (meta_len) {
Keith Busch1b567492013-07-18 12:13:51 -06001781 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
1782 meta_len);
Keith Buschf410c682013-04-23 17:23:59 -06001783 if (IS_ERR(meta_iod)) {
1784 status = PTR_ERR(meta_iod);
1785 meta_iod = NULL;
1786 goto unmap;
1787 }
1788
1789 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1790 &meta_dma_addr, GFP_KERNEL);
1791 if (!meta_mem) {
1792 status = -ENOMEM;
1793 goto unmap;
1794 }
1795
1796 if (io.opcode & 1) {
1797 int meta_offset = 0;
1798
1799 for (i = 0; i < meta_iod->nents; i++) {
1800 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1801 meta_iod->sg[i].offset;
1802 memcpy(meta_mem + meta_offset, meta,
1803 meta_iod->sg[i].length);
1804 kunmap_atomic(meta);
1805 meta_offset += meta_iod->sg[i].length;
1806 }
1807 }
1808
1809 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1810 }
1811
Keith Buschedd10d32014-04-03 16:45:23 -06001812 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1813 c.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1814 c.rw.prp2 = cpu_to_le64(iod->first_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001815
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001816 if (length != (io.nblocks + 1) << ns->lba_shift)
1817 status = -ENOMEM;
1818 else
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001819 status = nvme_submit_io_cmd(dev, ns, &c, NULL);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001820
Keith Buschf410c682013-04-23 17:23:59 -06001821 if (meta_len) {
1822 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1823 int meta_offset = 0;
1824
1825 for (i = 0; i < meta_iod->nents; i++) {
1826 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1827 meta_iod->sg[i].offset;
1828 memcpy(meta, meta_mem + meta_offset,
1829 meta_iod->sg[i].length);
1830 kunmap_atomic(meta);
1831 meta_offset += meta_iod->sg[i].length;
1832 }
1833 }
1834
1835 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1836 meta_dma_addr);
1837 }
1838
1839 unmap:
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001840 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001841 nvme_free_iod(dev, iod);
Keith Buschf410c682013-04-23 17:23:59 -06001842
1843 if (meta_iod) {
1844 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1845 nvme_free_iod(dev, meta_iod);
1846 }
1847
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001848 return status;
1849}
1850
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001851static int nvme_user_cmd(struct nvme_dev *dev, struct nvme_ns *ns,
1852 struct nvme_passthru_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001853{
Keith Busch7963e522014-09-12 16:07:20 -06001854 struct nvme_passthru_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001855 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001856 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001857 struct nvme_iod *uninitialized_var(iod);
Keith Busch94f370c2013-05-09 14:01:38 -06001858 unsigned timeout;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001859
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001860 if (!capable(CAP_SYS_ADMIN))
1861 return -EACCES;
1862 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001863 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001864
1865 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001866 c.common.opcode = cmd.opcode;
1867 c.common.flags = cmd.flags;
1868 c.common.nsid = cpu_to_le32(cmd.nsid);
1869 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1870 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1871 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1872 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1873 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1874 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1875 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1876 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1877
1878 length = cmd.data_len;
1879 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001880 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1881 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001882 if (IS_ERR(iod))
1883 return PTR_ERR(iod);
Keith Buschedd10d32014-04-03 16:45:23 -06001884 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1885 c.common.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1886 c.common.prp2 = cpu_to_le64(iod->first_dma);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001887 }
1888
Keith Busch94f370c2013-05-09 14:01:38 -06001889 timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
1890 ADMIN_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001891
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001892 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001893 status = -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001894 else if (ns) {
1895 struct request *req;
1896
1897 req = blk_mq_alloc_request(ns->queue, WRITE,
1898 (GFP_KERNEL|__GFP_WAIT), false);
Jens Axboe97fe3832014-12-10 13:02:44 -07001899 if (IS_ERR(req))
1900 status = PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001901 else {
1902 status = nvme_submit_sync_cmd(req, &c, &cmd.result,
1903 timeout);
Jens Axboe9d135bb2014-11-17 10:43:42 -07001904 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001905 }
1906 } else
1907 status = __nvme_submit_admin_cmd(dev, &c, &cmd.result, timeout);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001908
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001909 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001910 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001911 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001912 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001913
Chayan Biswascf90bc42013-05-22 22:34:49 +00001914 if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
Keith Buschf4f117f2012-09-21 10:49:05 -06001915 sizeof(cmd.result)))
1916 status = -EFAULT;
1917
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001918 return status;
1919}
1920
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001921static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1922 unsigned long arg)
1923{
1924 struct nvme_ns *ns = bdev->bd_disk->private_data;
1925
1926 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001927 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001928 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001929 return ns->ns_id;
1930 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001931 return nvme_user_cmd(ns->dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06001932 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001933 return nvme_user_cmd(ns->dev, ns, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001934 case NVME_IOCTL_SUBMIT_IO:
1935 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001936 case SG_GET_VERSION_NUM:
1937 return nvme_sg_get_version_num((void __user *)arg);
1938 case SG_IO:
1939 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001940 default:
1941 return -ENOTTY;
1942 }
1943}
1944
Keith Busch320a3822013-10-23 13:07:34 -06001945#ifdef CONFIG_COMPAT
1946static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1947 unsigned int cmd, unsigned long arg)
1948{
Keith Busch320a3822013-10-23 13:07:34 -06001949 switch (cmd) {
1950 case SG_IO:
Keith Busche1797292014-08-27 13:55:38 -06001951 return -ENOIOCTLCMD;
Keith Busch320a3822013-10-23 13:07:34 -06001952 }
1953 return nvme_ioctl(bdev, mode, cmd, arg);
1954}
1955#else
1956#define nvme_compat_ioctl NULL
1957#endif
1958
Keith Busch9ac27092014-01-31 16:53:39 -07001959static int nvme_open(struct block_device *bdev, fmode_t mode)
1960{
Keith Busch9e603522014-10-03 11:15:47 -06001961 int ret = 0;
1962 struct nvme_ns *ns;
Keith Busch9ac27092014-01-31 16:53:39 -07001963
Keith Busch9e603522014-10-03 11:15:47 -06001964 spin_lock(&dev_list_lock);
1965 ns = bdev->bd_disk->private_data;
1966 if (!ns)
1967 ret = -ENXIO;
1968 else if (!kref_get_unless_zero(&ns->dev->kref))
1969 ret = -ENXIO;
1970 spin_unlock(&dev_list_lock);
1971
1972 return ret;
Keith Busch9ac27092014-01-31 16:53:39 -07001973}
1974
1975static void nvme_free_dev(struct kref *kref);
1976
1977static void nvme_release(struct gendisk *disk, fmode_t mode)
1978{
1979 struct nvme_ns *ns = disk->private_data;
1980 struct nvme_dev *dev = ns->dev;
1981
1982 kref_put(&dev->kref, nvme_free_dev);
1983}
1984
Keith Busch4cc09e22014-04-02 15:45:37 -06001985static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
1986{
1987 /* some standard values */
1988 geo->heads = 1 << 6;
1989 geo->sectors = 1 << 5;
1990 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1991 return 0;
1992}
1993
Keith Busche1e5e562015-02-19 13:39:03 -07001994static void nvme_config_discard(struct nvme_ns *ns)
1995{
1996 u32 logical_block_size = queue_logical_block_size(ns->queue);
1997 ns->queue->limits.discard_zeroes_data = 0;
1998 ns->queue->limits.discard_alignment = logical_block_size;
1999 ns->queue->limits.discard_granularity = logical_block_size;
2000 ns->queue->limits.max_discard_sectors = 0xffffffff;
2001 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
2002}
2003
2004static int nvme_noop_verify(struct blk_integrity_iter *iter)
2005{
2006 return 0;
2007}
2008
2009static int nvme_noop_generate(struct blk_integrity_iter *iter)
2010{
2011 return 0;
2012}
2013
2014struct blk_integrity nvme_meta_noop = {
2015 .name = "NVME_META_NOOP",
2016 .generate_fn = nvme_noop_generate,
2017 .verify_fn = nvme_noop_verify,
2018};
2019
2020static void nvme_init_integrity(struct nvme_ns *ns)
2021{
2022 struct blk_integrity integrity;
2023
2024 switch (ns->pi_type) {
2025 case NVME_NS_DPS_PI_TYPE3:
2026 integrity = t10_pi_type3_crc;
2027 break;
2028 case NVME_NS_DPS_PI_TYPE1:
2029 case NVME_NS_DPS_PI_TYPE2:
2030 integrity = t10_pi_type1_crc;
2031 break;
2032 default:
2033 integrity = nvme_meta_noop;
2034 break;
2035 }
2036 integrity.tuple_size = ns->ms;
2037 blk_integrity_register(ns->disk, &integrity);
2038 blk_queue_max_integrity_segments(ns->queue, 1);
2039}
2040
Keith Busch1b9dbf72014-09-10 17:21:14 -06002041static int nvme_revalidate_disk(struct gendisk *disk)
2042{
2043 struct nvme_ns *ns = disk->private_data;
2044 struct nvme_dev *dev = ns->dev;
2045 struct nvme_id_ns *id;
2046 dma_addr_t dma_addr;
Keith Busche1e5e562015-02-19 13:39:03 -07002047 int lbaf, pi_type, old_ms;
2048 unsigned short bs;
Keith Busch1b9dbf72014-09-10 17:21:14 -06002049
2050 id = dma_alloc_coherent(&dev->pci_dev->dev, 4096, &dma_addr,
2051 GFP_KERNEL);
2052 if (!id) {
2053 dev_warn(&dev->pci_dev->dev, "%s: Memory alocation failure\n",
2054 __func__);
2055 return 0;
2056 }
Keith Busche1e5e562015-02-19 13:39:03 -07002057 if (nvme_identify(dev, ns->ns_id, 0, dma_addr)) {
2058 dev_warn(&dev->pci_dev->dev,
2059 "identify failed ns:%d, setting capacity to 0\n",
2060 ns->ns_id);
2061 memset(id, 0, sizeof(*id));
2062 }
Keith Busch1b9dbf72014-09-10 17:21:14 -06002063
Keith Busche1e5e562015-02-19 13:39:03 -07002064 old_ms = ns->ms;
2065 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
Keith Busch1b9dbf72014-09-10 17:21:14 -06002066 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Busche1e5e562015-02-19 13:39:03 -07002067 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busch1b9dbf72014-09-10 17:21:14 -06002068
Keith Busche1e5e562015-02-19 13:39:03 -07002069 /*
2070 * If identify namespace failed, use default 512 byte block size so
2071 * block layer can use before failing read/write for 0 capacity.
2072 */
2073 if (ns->lba_shift == 0)
2074 ns->lba_shift = 9;
2075 bs = 1 << ns->lba_shift;
2076
2077 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
2078 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
2079 id->dps & NVME_NS_DPS_PI_MASK : 0;
2080
2081 if (disk->integrity && (ns->pi_type != pi_type || ns->ms != old_ms ||
2082 bs != queue_logical_block_size(disk->queue) ||
2083 (ns->ms && id->flbas & NVME_NS_FLBAS_META_EXT)))
2084 blk_integrity_unregister(disk);
2085
2086 ns->pi_type = pi_type;
2087 blk_queue_logical_block_size(ns->queue, bs);
2088
2089 if (ns->ms && !disk->integrity && (disk->flags & GENHD_FL_UP) &&
2090 !(id->flbas & NVME_NS_FLBAS_META_EXT))
2091 nvme_init_integrity(ns);
2092
2093 if (id->ncap == 0 || (ns->ms && !disk->integrity))
2094 set_capacity(disk, 0);
2095 else
2096 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
2097
2098 if (dev->oncs & NVME_CTRL_ONCS_DSM)
2099 nvme_config_discard(ns);
2100
Keith Busch1b9dbf72014-09-10 17:21:14 -06002101 dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr);
2102 return 0;
2103}
2104
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002105static const struct block_device_operations nvme_fops = {
2106 .owner = THIS_MODULE,
2107 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06002108 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07002109 .open = nvme_open,
2110 .release = nvme_release,
Keith Busch4cc09e22014-04-02 15:45:37 -06002111 .getgeo = nvme_getgeo,
Keith Busch1b9dbf72014-09-10 17:21:14 -06002112 .revalidate_disk= nvme_revalidate_disk,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002113};
2114
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002115static int nvme_kthread(void *data)
2116{
Keith Buschd4b4ff82013-12-10 13:10:37 -07002117 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002118
2119 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04002120 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002121 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07002122 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002123 int i;
Keith Buschd4b4ff82013-12-10 13:10:37 -07002124 if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
2125 dev->initialized) {
2126 if (work_busy(&dev->reset_work))
2127 continue;
2128 list_del_init(&dev->node);
2129 dev_warn(&dev->pci_dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002130 "Failed status: %x, reset controller\n",
2131 readl(&dev->bar->csts));
Tejun Heo9ca97372014-03-07 10:24:49 -05002132 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschd4b4ff82013-12-10 13:10:37 -07002133 queue_work(nvme_workq, &dev->reset_work);
2134 continue;
2135 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002136 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002137 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05002138 if (!nvmeq)
2139 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002140 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04002141 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06002142
2143 while ((i == 0) && (dev->event_limit > 0)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002144 if (nvme_submit_async_admin_req(dev))
Keith Busch6fccf932014-06-18 13:58:57 -06002145 break;
2146 dev->event_limit--;
2147 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002148 spin_unlock_irq(&nvmeq->q_lock);
2149 }
2150 }
2151 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08002152 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002153 }
2154 return 0;
2155}
2156
Keith Busche1e5e562015-02-19 13:39:03 -07002157static void nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002158{
2159 struct nvme_ns *ns;
2160 struct gendisk *disk;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002161 int node = dev_to_node(&dev->pci_dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002162
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002163 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002164 if (!ns)
Keith Busche1e5e562015-02-19 13:39:03 -07002165 return;
2166
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002167 ns->queue = blk_mq_init_queue(&dev->tagset);
Dan Carpenter9f173b32014-11-05 23:39:09 +03002168 if (IS_ERR(ns->queue))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002169 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07002170 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
2171 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002172 queue_flag_set_unlocked(QUEUE_FLAG_SG_GAPS, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002173 ns->dev = dev;
2174 ns->queue->queuedata = ns;
2175
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002176 disk = alloc_disk_node(0, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002177 if (!disk)
2178 goto out_free_queue;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002179
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002180 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002181 ns->disk = disk;
Keith Busche1e5e562015-02-19 13:39:03 -07002182 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
2183 list_add_tail(&ns->list, &dev->namespaces);
2184
Keith Busche9ef4632012-07-24 15:01:04 -06002185 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06002186 if (dev->max_hw_sectors)
2187 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002188 if (dev->stripe_size)
2189 blk_queue_chunk_sectors(ns->queue, dev->stripe_size >> 9);
Keith Buscha7d2ce22014-04-29 11:41:28 -06002190 if (dev->vwc & NVME_CTRL_VWC_PRESENT)
2191 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002192
2193 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002194 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002195 disk->fops = &nvme_fops;
2196 disk->private_data = ns;
2197 disk->queue = ns->queue;
Keith Buschb3fffde2015-02-03 11:21:42 -07002198 disk->driverfs_dev = dev->device;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002199 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002200 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002201
Keith Busche1e5e562015-02-19 13:39:03 -07002202 /*
2203 * Initialize capacity to 0 until we establish the namespace format and
2204 * setup integrity extentions if necessary. The revalidate_disk after
2205 * add_disk allows the driver to register with integrity if the format
2206 * requires it.
2207 */
2208 set_capacity(disk, 0);
2209 nvme_revalidate_disk(ns->disk);
2210 add_disk(ns->disk);
2211 if (ns->ms)
2212 revalidate_disk(ns->disk);
2213 return;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002214 out_free_queue:
2215 blk_cleanup_queue(ns->queue);
2216 out_free_ns:
2217 kfree(ns);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002218}
2219
Keith Busch42f61422014-03-24 10:46:25 -06002220static void nvme_create_io_queues(struct nvme_dev *dev)
2221{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002222 unsigned i;
Keith Busch42f61422014-03-24 10:46:25 -06002223
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002224 for (i = dev->queue_count; i <= dev->max_qid; i++)
Keith Busch2b25d982014-12-22 12:59:04 -07002225 if (!nvme_alloc_queue(dev, i, dev->q_depth))
Keith Busch42f61422014-03-24 10:46:25 -06002226 break;
2227
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002228 for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
2229 if (nvme_create_queue(dev->queues[i], i))
Keith Busch42f61422014-03-24 10:46:25 -06002230 break;
2231}
2232
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002233static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002234{
2235 int status;
2236 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002237 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002238
Matthew Wilcoxdf348132012-01-11 07:29:56 -07002239 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002240 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04002241 if (status < 0)
2242 return status;
2243 if (status > 0) {
2244 dev_err(&dev->pci_dev->dev, "Could not set queue count (%d)\n",
2245 status);
Keith Buschbadc34d2014-06-23 14:25:35 -06002246 return 0;
Matthew Wilcox27e81662014-04-11 11:58:45 -04002247 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002248 return min(result & 0xffff, result >> 16) + 1;
2249}
2250
Keith Busch9d713c22013-07-15 15:02:24 -06002251static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
2252{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002253 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06002254}
2255
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002256static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002257{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002258 struct nvme_queue *adminq = dev->queues[0];
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002259 struct pci_dev *pdev = dev->pci_dev;
Keith Busch42f61422014-03-24 10:46:25 -06002260 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002261
Keith Busch42f61422014-03-24 10:46:25 -06002262 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002263 result = set_queue_count(dev, nr_io_queues);
Keith Buschbadc34d2014-06-23 14:25:35 -06002264 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002265 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002266 if (result < nr_io_queues)
2267 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002268
Keith Busch9d713c22013-07-15 15:02:24 -06002269 size = db_bar_size(dev, nr_io_queues);
2270 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002271 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06002272 do {
2273 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
2274 if (dev->bar)
2275 break;
2276 if (!--nr_io_queues)
2277 return -ENOMEM;
2278 size = db_bar_size(dev, nr_io_queues);
2279 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002280 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07002281 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002282 }
2283
Keith Busch9d713c22013-07-15 15:02:24 -06002284 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002285 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002286
Jens Axboee32efbf2014-11-14 09:49:26 -07002287 /*
2288 * If we enable msix early due to not intx, disable it again before
2289 * setting up the full range we need.
2290 */
2291 if (!pdev->irq)
2292 pci_disable_msix(pdev);
2293
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002294 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002295 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002296 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
2297 if (vecs < 0) {
2298 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
2299 if (vecs < 0) {
2300 vecs = 1;
2301 } else {
2302 for (i = 0; i < vecs; i++)
2303 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002304 }
2305 }
2306
Matthew Wilcox063a8092013-06-20 10:53:48 -04002307 /*
2308 * Should investigate if there's a performance win from allocating
2309 * more queues than interrupt vectors; it might allow the submission
2310 * path to scale better, even if the receive path is limited by the
2311 * number of interrupts.
2312 */
2313 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002314 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002315
Matthew Wilcox3193f072014-01-27 15:57:22 -05002316 result = queue_request_irq(dev, adminq, adminq->irqname);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002317 if (result)
Keith Busch22404272013-07-15 15:02:20 -06002318 goto free_queues;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002319
Keith Buschcd638942013-07-15 15:02:23 -06002320 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002321 nvme_free_queues(dev, nr_io_queues + 1);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002322 nvme_create_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06002323
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002324 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002325
Keith Busch22404272013-07-15 15:02:20 -06002326 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002327 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002328 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002329}
2330
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002331/*
2332 * Return: error value if an error occurred setting up the queues or calling
2333 * Identify Device. 0 if these succeeded, even if adding some of the
2334 * namespaces failed. At the moment, these failures are silent. TBD which
2335 * failures should be reported.
2336 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002337static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002338{
Matthew Wilcox68608c262013-06-21 14:36:34 -04002339 struct pci_dev *pdev = dev->pci_dev;
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002340 int res;
2341 unsigned nn, i;
Matthew Wilcox51814232011-02-01 16:18:08 -05002342 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002343 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002344 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06002345 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002346
Keith Busche1e5e562015-02-19 13:39:03 -07002347 mem = dma_alloc_coherent(&pdev->dev, 4096, &dma_addr, GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06002348 if (!mem)
2349 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002350
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002351 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002352 if (res) {
Matthew Wilcox27e81662014-04-11 11:58:45 -04002353 dev_err(&pdev->dev, "Identify Controller failed (%d)\n", res);
Keith Busche1e5e562015-02-19 13:39:03 -07002354 dma_free_coherent(&dev->pci_dev->dev, 4096, mem, dma_addr);
2355 return -EIO;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002356 }
2357
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002358 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05002359 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002360 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002361 dev->abort_limit = ctrl->acl + 1;
Keith Buscha7d2ce22014-04-29 11:41:28 -06002362 dev->vwc = ctrl->vwc;
Keith Busch6fccf932014-06-18 13:58:57 -06002363 dev->event_limit = min(ctrl->aerl + 1, 8);
Matthew Wilcox51814232011-02-01 16:18:08 -05002364 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2365 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2366 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002367 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002368 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002369 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002370 (pdev->device == 0x0953) && ctrl->vs[3]) {
2371 unsigned int max_hw_sectors;
2372
Keith Busch159b67d2013-04-09 17:13:20 -06002373 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002374 max_hw_sectors = dev->stripe_size >> (shift - 9);
2375 if (dev->max_hw_sectors) {
2376 dev->max_hw_sectors = min(max_hw_sectors,
2377 dev->max_hw_sectors);
2378 } else
2379 dev->max_hw_sectors = max_hw_sectors;
2380 }
Keith Busche1e5e562015-02-19 13:39:03 -07002381 dma_free_coherent(&dev->pci_dev->dev, 4096, mem, dma_addr);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002382
2383 dev->tagset.ops = &nvme_mq_ops;
2384 dev->tagset.nr_hw_queues = dev->online_queues - 1;
2385 dev->tagset.timeout = NVME_IO_TIMEOUT;
2386 dev->tagset.numa_node = dev_to_node(&dev->pci_dev->dev);
2387 dev->tagset.queue_depth =
2388 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Jens Axboeac3dd5b2015-01-22 12:07:58 -07002389 dev->tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002390 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
2391 dev->tagset.driver_data = dev;
2392
2393 if (blk_mq_alloc_tag_set(&dev->tagset))
Keith Busche1e5e562015-02-19 13:39:03 -07002394 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002395
Keith Busche1e5e562015-02-19 13:39:03 -07002396 for (i = 1; i <= nn; i++)
2397 nvme_alloc_ns(dev, i);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002398
Keith Busche1e5e562015-02-19 13:39:03 -07002399 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002400}
2401
Keith Busch0877cb02013-07-15 15:02:19 -06002402static int nvme_dev_map(struct nvme_dev *dev)
2403{
Keith Busch42f61422014-03-24 10:46:25 -06002404 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002405 int bars, result = -ENOMEM;
2406 struct pci_dev *pdev = dev->pci_dev;
2407
2408 if (pci_enable_device_mem(pdev))
2409 return result;
2410
2411 dev->entry[0].vector = pdev->irq;
2412 pci_set_master(pdev);
2413 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07002414 if (!bars)
2415 goto disable_pci;
2416
Keith Busch0877cb02013-07-15 15:02:19 -06002417 if (pci_request_selected_regions(pdev, bars, "nvme"))
2418 goto disable_pci;
2419
Russell King052d0ef2013-06-26 23:49:11 +01002420 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
2421 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2422 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002423
Keith Busch0877cb02013-07-15 15:02:19 -06002424 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2425 if (!dev->bar)
2426 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07002427
Keith Busch0e53d182013-12-10 13:10:39 -07002428 if (readl(&dev->bar->csts) == -1) {
2429 result = -ENODEV;
2430 goto unmap;
2431 }
Jens Axboee32efbf2014-11-14 09:49:26 -07002432
2433 /*
2434 * Some devices don't advertse INTx interrupts, pre-enable a single
2435 * MSIX vec for setup. We'll adjust this later.
2436 */
2437 if (!pdev->irq) {
2438 result = pci_enable_msix(pdev, dev->entry, 1);
2439 if (result < 0)
2440 goto unmap;
2441 }
2442
Keith Busch42f61422014-03-24 10:46:25 -06002443 cap = readq(&dev->bar->cap);
2444 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2445 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Keith Busch0877cb02013-07-15 15:02:19 -06002446 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2447
2448 return 0;
2449
Keith Busch0e53d182013-12-10 13:10:39 -07002450 unmap:
2451 iounmap(dev->bar);
2452 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002453 disable:
2454 pci_release_regions(pdev);
2455 disable_pci:
2456 pci_disable_device(pdev);
2457 return result;
2458}
2459
2460static void nvme_dev_unmap(struct nvme_dev *dev)
2461{
2462 if (dev->pci_dev->msi_enabled)
2463 pci_disable_msi(dev->pci_dev);
2464 else if (dev->pci_dev->msix_enabled)
2465 pci_disable_msix(dev->pci_dev);
2466
2467 if (dev->bar) {
2468 iounmap(dev->bar);
2469 dev->bar = NULL;
Keith Busch9a6b9452013-12-10 13:10:36 -07002470 pci_release_regions(dev->pci_dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002471 }
2472
Keith Busch0877cb02013-07-15 15:02:19 -06002473 if (pci_is_enabled(dev->pci_dev))
2474 pci_disable_device(dev->pci_dev);
2475}
2476
Keith Busch4d115422013-12-10 13:10:40 -07002477struct nvme_delq_ctx {
2478 struct task_struct *waiter;
2479 struct kthread_worker *worker;
2480 atomic_t refcount;
2481};
2482
2483static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2484{
2485 dq->waiter = current;
2486 mb();
2487
2488 for (;;) {
2489 set_current_state(TASK_KILLABLE);
2490 if (!atomic_read(&dq->refcount))
2491 break;
2492 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2493 fatal_signal_pending(current)) {
Keith Busch0fb59cb2015-01-07 18:55:50 -07002494 /*
2495 * Disable the controller first since we can't trust it
2496 * at this point, but leave the admin queue enabled
2497 * until all queue deletion requests are flushed.
2498 * FIXME: This may take a while if there are more h/w
2499 * queues than admin tags.
2500 */
Keith Busch4d115422013-12-10 13:10:40 -07002501 set_current_state(TASK_RUNNING);
Keith Busch4d115422013-12-10 13:10:40 -07002502 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
Keith Busch0fb59cb2015-01-07 18:55:50 -07002503 nvme_clear_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07002504 flush_kthread_worker(dq->worker);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002505 nvme_disable_queue(dev, 0);
Keith Busch4d115422013-12-10 13:10:40 -07002506 return;
2507 }
2508 }
2509 set_current_state(TASK_RUNNING);
2510}
2511
2512static void nvme_put_dq(struct nvme_delq_ctx *dq)
2513{
2514 atomic_dec(&dq->refcount);
2515 if (dq->waiter)
2516 wake_up_process(dq->waiter);
2517}
2518
2519static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2520{
2521 atomic_inc(&dq->refcount);
2522 return dq;
2523}
2524
2525static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2526{
2527 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
2528
2529 nvme_clear_queue(nvmeq);
2530 nvme_put_dq(dq);
2531}
2532
2533static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2534 kthread_work_func_t fn)
2535{
2536 struct nvme_command c;
2537
2538 memset(&c, 0, sizeof(c));
2539 c.delete_queue.opcode = opcode;
2540 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2541
2542 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002543 return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
2544 ADMIN_TIMEOUT);
Keith Busch4d115422013-12-10 13:10:40 -07002545}
2546
2547static void nvme_del_cq_work_handler(struct kthread_work *work)
2548{
2549 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2550 cmdinfo.work);
2551 nvme_del_queue_end(nvmeq);
2552}
2553
2554static int nvme_delete_cq(struct nvme_queue *nvmeq)
2555{
2556 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2557 nvme_del_cq_work_handler);
2558}
2559
2560static void nvme_del_sq_work_handler(struct kthread_work *work)
2561{
2562 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2563 cmdinfo.work);
2564 int status = nvmeq->cmdinfo.status;
2565
2566 if (!status)
2567 status = nvme_delete_cq(nvmeq);
2568 if (status)
2569 nvme_del_queue_end(nvmeq);
2570}
2571
2572static int nvme_delete_sq(struct nvme_queue *nvmeq)
2573{
2574 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2575 nvme_del_sq_work_handler);
2576}
2577
2578static void nvme_del_queue_start(struct kthread_work *work)
2579{
2580 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2581 cmdinfo.work);
Keith Busch4d115422013-12-10 13:10:40 -07002582 if (nvme_delete_sq(nvmeq))
2583 nvme_del_queue_end(nvmeq);
2584}
2585
2586static void nvme_disable_io_queues(struct nvme_dev *dev)
2587{
2588 int i;
2589 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2590 struct nvme_delq_ctx dq;
2591 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2592 &worker, "nvme%d", dev->instance);
2593
2594 if (IS_ERR(kworker_task)) {
2595 dev_err(&dev->pci_dev->dev,
2596 "Failed to create queue del task\n");
2597 for (i = dev->queue_count - 1; i > 0; i--)
2598 nvme_disable_queue(dev, i);
2599 return;
2600 }
2601
2602 dq.waiter = NULL;
2603 atomic_set(&dq.refcount, 0);
2604 dq.worker = &worker;
2605 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002606 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002607
2608 if (nvme_suspend_queue(nvmeq))
2609 continue;
2610 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2611 nvmeq->cmdinfo.worker = dq.worker;
2612 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2613 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2614 }
2615 nvme_wait_dq(&dq, dev);
2616 kthread_stop(kworker_task);
2617}
2618
Dan McLeranb9afca32014-04-07 17:10:11 -06002619/*
2620* Remove the node from the device list and check
2621* for whether or not we need to stop the nvme_thread.
2622*/
2623static void nvme_dev_list_remove(struct nvme_dev *dev)
2624{
2625 struct task_struct *tmp = NULL;
2626
2627 spin_lock(&dev_list_lock);
2628 list_del_init(&dev->node);
2629 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2630 tmp = nvme_thread;
2631 nvme_thread = NULL;
2632 }
2633 spin_unlock(&dev_list_lock);
2634
2635 if (tmp)
2636 kthread_stop(tmp);
2637}
2638
Keith Buschc9d3bf82015-01-07 18:55:52 -07002639static void nvme_freeze_queues(struct nvme_dev *dev)
2640{
2641 struct nvme_ns *ns;
2642
2643 list_for_each_entry(ns, &dev->namespaces, list) {
2644 blk_mq_freeze_queue_start(ns->queue);
2645
2646 spin_lock(ns->queue->queue_lock);
2647 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
2648 spin_unlock(ns->queue->queue_lock);
2649
2650 blk_mq_cancel_requeue_work(ns->queue);
2651 blk_mq_stop_hw_queues(ns->queue);
2652 }
2653}
2654
2655static void nvme_unfreeze_queues(struct nvme_dev *dev)
2656{
2657 struct nvme_ns *ns;
2658
2659 list_for_each_entry(ns, &dev->namespaces, list) {
2660 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2661 blk_mq_unfreeze_queue(ns->queue);
2662 blk_mq_start_stopped_hw_queues(ns->queue, true);
2663 blk_mq_kick_requeue_list(ns->queue);
2664 }
2665}
2666
Keith Buschf0b50732013-07-15 15:02:21 -06002667static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002668{
Keith Busch22404272013-07-15 15:02:20 -06002669 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06002670 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06002671
Keith Buschd4b4ff82013-12-10 13:10:37 -07002672 dev->initialized = 0;
Dan McLeranb9afca32014-04-07 17:10:11 -06002673 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002674
Keith Buschc9d3bf82015-01-07 18:55:52 -07002675 if (dev->bar) {
2676 nvme_freeze_queues(dev);
Keith Busch7c1b2452014-06-25 11:18:12 -06002677 csts = readl(&dev->bar->csts);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002678 }
Keith Busch7c1b2452014-06-25 11:18:12 -06002679 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07002680 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002681 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002682 nvme_suspend_queue(nvmeq);
2683 nvme_clear_queue(nvmeq);
2684 }
2685 } else {
2686 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002687 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002688 nvme_disable_queue(dev, 0);
2689 }
Keith Buschf0b50732013-07-15 15:02:21 -06002690 nvme_dev_unmap(dev);
2691}
2692
2693static void nvme_dev_remove(struct nvme_dev *dev)
2694{
Keith Busch9ac27092014-01-31 16:53:39 -07002695 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002696
Keith Busch9ac27092014-01-31 16:53:39 -07002697 list_for_each_entry(ns, &dev->namespaces, list) {
Keith Busche1e5e562015-02-19 13:39:03 -07002698 if (ns->disk->flags & GENHD_FL_UP) {
2699 if (ns->disk->integrity)
2700 blk_integrity_unregister(ns->disk);
Keith Busch9ac27092014-01-31 16:53:39 -07002701 del_gendisk(ns->disk);
Keith Busche1e5e562015-02-19 13:39:03 -07002702 }
Keith Buschcef6a942015-01-07 18:55:51 -07002703 if (!blk_queue_dying(ns->queue)) {
2704 blk_mq_abort_requeue_list(ns->queue);
Keith Busch9ac27092014-01-31 16:53:39 -07002705 blk_cleanup_queue(ns->queue);
Keith Buschcef6a942015-01-07 18:55:51 -07002706 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002707 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002708}
2709
Matthew Wilcox091b6092011-02-10 09:56:01 -05002710static int nvme_setup_prp_pools(struct nvme_dev *dev)
2711{
2712 struct device *dmadev = &dev->pci_dev->dev;
2713 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2714 PAGE_SIZE, PAGE_SIZE, 0);
2715 if (!dev->prp_page_pool)
2716 return -ENOMEM;
2717
Matthew Wilcox99802a72011-02-10 10:30:34 -05002718 /* Optimisation for I/Os between 4k and 128k */
2719 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2720 256, 256, 0);
2721 if (!dev->prp_small_pool) {
2722 dma_pool_destroy(dev->prp_page_pool);
2723 return -ENOMEM;
2724 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002725 return 0;
2726}
2727
2728static void nvme_release_prp_pools(struct nvme_dev *dev)
2729{
2730 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002731 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002732}
2733
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002734static DEFINE_IDA(nvme_instance_ida);
2735
2736static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002737{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002738 int instance, error;
2739
2740 do {
2741 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2742 return -ENODEV;
2743
2744 spin_lock(&dev_list_lock);
2745 error = ida_get_new(&nvme_instance_ida, &instance);
2746 spin_unlock(&dev_list_lock);
2747 } while (error == -EAGAIN);
2748
2749 if (error)
2750 return -ENODEV;
2751
2752 dev->instance = instance;
2753 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002754}
2755
2756static void nvme_release_instance(struct nvme_dev *dev)
2757{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002758 spin_lock(&dev_list_lock);
2759 ida_remove(&nvme_instance_ida, dev->instance);
2760 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002761}
2762
Keith Busch9ac27092014-01-31 16:53:39 -07002763static void nvme_free_namespaces(struct nvme_dev *dev)
2764{
2765 struct nvme_ns *ns, *next;
2766
2767 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2768 list_del(&ns->list);
Keith Busch9e603522014-10-03 11:15:47 -06002769
2770 spin_lock(&dev_list_lock);
2771 ns->disk->private_data = NULL;
2772 spin_unlock(&dev_list_lock);
2773
Keith Busch9ac27092014-01-31 16:53:39 -07002774 put_disk(ns->disk);
2775 kfree(ns);
2776 }
2777}
2778
Keith Busch5e82e952013-02-19 10:17:58 -07002779static void nvme_free_dev(struct kref *kref)
2780{
2781 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002782
Keith Buscha96d4f52014-08-19 19:15:59 -06002783 pci_dev_put(dev->pci_dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07002784 put_device(dev->device);
Keith Busch9ac27092014-01-31 16:53:39 -07002785 nvme_free_namespaces(dev);
Indraneel M285dffc2014-12-11 08:24:18 -07002786 nvme_release_instance(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002787 blk_mq_free_tag_set(&dev->tagset);
Keith Buschea191d22015-01-07 18:55:49 -07002788 blk_put_queue(dev->admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002789 kfree(dev->queues);
2790 kfree(dev->entry);
2791 kfree(dev);
2792}
2793
2794static int nvme_dev_open(struct inode *inode, struct file *f)
2795{
Keith Buschb3fffde2015-02-03 11:21:42 -07002796 struct nvme_dev *dev;
2797 int instance = iminor(inode);
2798 int ret = -ENODEV;
2799
2800 spin_lock(&dev_list_lock);
2801 list_for_each_entry(dev, &dev_list, node) {
2802 if (dev->instance == instance) {
2803 if (!kref_get_unless_zero(&dev->kref))
2804 break;
2805 f->private_data = dev;
2806 ret = 0;
2807 break;
2808 }
2809 }
2810 spin_unlock(&dev_list_lock);
2811
2812 return ret;
Keith Busch5e82e952013-02-19 10:17:58 -07002813}
2814
2815static int nvme_dev_release(struct inode *inode, struct file *f)
2816{
2817 struct nvme_dev *dev = f->private_data;
2818 kref_put(&dev->kref, nvme_free_dev);
2819 return 0;
2820}
2821
2822static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2823{
2824 struct nvme_dev *dev = f->private_data;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002825 struct nvme_ns *ns;
2826
Keith Busch5e82e952013-02-19 10:17:58 -07002827 switch (cmd) {
2828 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002829 return nvme_user_cmd(dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06002830 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002831 if (list_empty(&dev->namespaces))
2832 return -ENOTTY;
2833 ns = list_first_entry(&dev->namespaces, struct nvme_ns, list);
2834 return nvme_user_cmd(dev, ns, (void __user *)arg);
Keith Busch5e82e952013-02-19 10:17:58 -07002835 default:
2836 return -ENOTTY;
2837 }
2838}
2839
2840static const struct file_operations nvme_dev_fops = {
2841 .owner = THIS_MODULE,
2842 .open = nvme_dev_open,
2843 .release = nvme_dev_release,
2844 .unlocked_ioctl = nvme_dev_ioctl,
2845 .compat_ioctl = nvme_dev_ioctl,
2846};
2847
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002848static void nvme_set_irq_hints(struct nvme_dev *dev)
2849{
2850 struct nvme_queue *nvmeq;
2851 int i;
2852
2853 for (i = 0; i < dev->online_queues; i++) {
2854 nvmeq = dev->queues[i];
2855
2856 if (!nvmeq->hctx)
2857 continue;
2858
2859 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
2860 nvmeq->hctx->cpumask);
2861 }
2862}
2863
Keith Buschf0b50732013-07-15 15:02:21 -06002864static int nvme_dev_start(struct nvme_dev *dev)
2865{
2866 int result;
Dan McLeranb9afca32014-04-07 17:10:11 -06002867 bool start_thread = false;
Keith Buschf0b50732013-07-15 15:02:21 -06002868
2869 result = nvme_dev_map(dev);
2870 if (result)
2871 return result;
2872
2873 result = nvme_configure_admin_queue(dev);
2874 if (result)
2875 goto unmap;
2876
2877 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002878 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2879 start_thread = true;
2880 nvme_thread = NULL;
2881 }
Keith Buschf0b50732013-07-15 15:02:21 -06002882 list_add(&dev->node, &dev_list);
2883 spin_unlock(&dev_list_lock);
2884
Dan McLeranb9afca32014-04-07 17:10:11 -06002885 if (start_thread) {
2886 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
Keith Busch387caa52014-09-22 13:46:19 -06002887 wake_up_all(&nvme_kthread_wait);
Dan McLeranb9afca32014-04-07 17:10:11 -06002888 } else
2889 wait_event_killable(nvme_kthread_wait, nvme_thread);
2890
2891 if (IS_ERR_OR_NULL(nvme_thread)) {
2892 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2893 goto disable;
2894 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002895
2896 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002897 result = nvme_alloc_admin_tags(dev);
2898 if (result)
2899 goto disable;
Dan McLeranb9afca32014-04-07 17:10:11 -06002900
Keith Buschf0b50732013-07-15 15:02:21 -06002901 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002902 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07002903 goto free_tags;
Keith Buschf0b50732013-07-15 15:02:21 -06002904
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002905 nvme_set_irq_hints(dev);
2906
Keith Buschd82e8bf2013-09-05 14:45:07 -06002907 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002908
Keith Busch0fb59cb2015-01-07 18:55:50 -07002909 free_tags:
2910 nvme_dev_remove_admin(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002911 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002912 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06002913 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002914 unmap:
2915 nvme_dev_unmap(dev);
2916 return result;
2917}
2918
Keith Busch9a6b9452013-12-10 13:10:36 -07002919static int nvme_remove_dead_ctrl(void *arg)
2920{
2921 struct nvme_dev *dev = (struct nvme_dev *)arg;
2922 struct pci_dev *pdev = dev->pci_dev;
2923
2924 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06002925 pci_stop_and_remove_bus_device_locked(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002926 kref_put(&dev->kref, nvme_free_dev);
2927 return 0;
2928}
2929
2930static void nvme_remove_disks(struct work_struct *ws)
2931{
Keith Busch9a6b9452013-12-10 13:10:36 -07002932 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2933
Keith Busch5a92e702014-02-21 14:13:44 -07002934 nvme_free_queues(dev, 1);
Keith Busch302c6722014-07-18 11:40:20 -06002935 nvme_dev_remove(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002936}
2937
2938static int nvme_dev_resume(struct nvme_dev *dev)
2939{
2940 int ret;
2941
2942 ret = nvme_dev_start(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002943 if (ret)
Keith Busch9a6b9452013-12-10 13:10:36 -07002944 return ret;
Keith Buschbadc34d2014-06-23 14:25:35 -06002945 if (dev->online_queues < 2) {
Keith Busch9a6b9452013-12-10 13:10:36 -07002946 spin_lock(&dev_list_lock);
Tejun Heo9ca97372014-03-07 10:24:49 -05002947 dev->reset_workfn = nvme_remove_disks;
Keith Busch9a6b9452013-12-10 13:10:36 -07002948 queue_work(nvme_workq, &dev->reset_work);
2949 spin_unlock(&dev_list_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002950 } else {
2951 nvme_unfreeze_queues(dev);
2952 nvme_set_irq_hints(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002953 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002954 dev->initialized = 1;
Keith Busch9a6b9452013-12-10 13:10:36 -07002955 return 0;
2956}
2957
2958static void nvme_dev_reset(struct nvme_dev *dev)
2959{
2960 nvme_dev_shutdown(dev);
2961 if (nvme_dev_resume(dev)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002962 dev_warn(&dev->pci_dev->dev, "Device failed to resume\n");
Keith Busch9a6b9452013-12-10 13:10:36 -07002963 kref_get(&dev->kref);
2964 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2965 dev->instance))) {
2966 dev_err(&dev->pci_dev->dev,
2967 "Failed to start controller remove task\n");
2968 kref_put(&dev->kref, nvme_free_dev);
2969 }
2970 }
2971}
2972
2973static void nvme_reset_failed_dev(struct work_struct *ws)
2974{
2975 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2976 nvme_dev_reset(dev);
2977}
2978
Tejun Heo9ca97372014-03-07 10:24:49 -05002979static void nvme_reset_workfn(struct work_struct *work)
2980{
2981 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
2982 dev->reset_workfn(work);
2983}
2984
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002985static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002986{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002987 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002988 struct nvme_dev *dev;
2989
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002990 node = dev_to_node(&pdev->dev);
2991 if (node == NUMA_NO_NODE)
2992 set_dev_node(&pdev->dev, 0);
2993
2994 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002995 if (!dev)
2996 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002997 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
2998 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002999 if (!dev->entry)
3000 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003001 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
3002 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003003 if (!dev->queues)
3004 goto free;
3005
3006 INIT_LIST_HEAD(&dev->namespaces);
Tejun Heo9ca97372014-03-07 10:24:49 -05003007 dev->reset_workfn = nvme_reset_failed_dev;
3008 INIT_WORK(&dev->reset_work, nvme_reset_workfn);
Keith Buscha96d4f52014-08-19 19:15:59 -06003009 dev->pci_dev = pci_dev_get(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003010 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07003011 result = nvme_set_instance(dev);
3012 if (result)
Keith Buscha96d4f52014-08-19 19:15:59 -06003013 goto put_pci;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003014
Matthew Wilcox091b6092011-02-10 09:56:01 -05003015 result = nvme_setup_prp_pools(dev);
3016 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06003017 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05003018
Keith Buschfb35e912014-03-03 11:09:47 -07003019 kref_init(&dev->kref);
Keith Buschf0b50732013-07-15 15:02:21 -06003020 result = nvme_dev_start(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06003021 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06003022 goto release_pools;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003023
Keith Buschb3fffde2015-02-03 11:21:42 -07003024 dev->device = device_create(nvme_class, &pdev->dev,
3025 MKDEV(nvme_char_major, dev->instance),
3026 dev, "nvme%d", dev->instance);
3027 if (IS_ERR(dev->device)) {
3028 result = PTR_ERR(dev->device);
3029 goto shutdown;
3030 }
3031 get_device(dev->device);
3032
Keith Buschbadc34d2014-06-23 14:25:35 -06003033 if (dev->online_queues > 1)
3034 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06003035 if (result)
Keith Buschb3fffde2015-02-03 11:21:42 -07003036 goto device_del;
Keith Busch5e82e952013-02-19 10:17:58 -07003037
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003038 nvme_set_irq_hints(dev);
Keith Buschd4b4ff82013-12-10 13:10:37 -07003039 dev->initialized = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003040 return 0;
3041
Keith Buschb3fffde2015-02-03 11:21:42 -07003042 device_del:
3043 device_destroy(nvme_class, MKDEV(nvme_char_major, dev->instance));
Keith Buschf0b50732013-07-15 15:02:21 -06003044 shutdown:
3045 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06003046 release_pools:
Keith Buscha1a5ef92013-12-16 13:50:00 -05003047 nvme_free_queues(dev, 0);
Matthew Wilcox091b6092011-02-10 09:56:01 -05003048 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06003049 release:
3050 nvme_release_instance(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06003051 put_pci:
3052 pci_dev_put(dev->pci_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003053 free:
3054 kfree(dev->queues);
3055 kfree(dev->entry);
3056 kfree(dev);
3057 return result;
3058}
3059
Keith Buschf0d54a52014-05-02 10:40:43 -06003060static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
3061{
Keith Buscha6739472014-06-23 16:03:21 -06003062 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06003063
Keith Buscha6739472014-06-23 16:03:21 -06003064 if (prepare)
3065 nvme_dev_shutdown(dev);
3066 else
3067 nvme_dev_resume(dev);
Keith Buschf0d54a52014-05-02 10:40:43 -06003068}
3069
Keith Busch09ece142014-01-27 11:29:40 -05003070static void nvme_shutdown(struct pci_dev *pdev)
3071{
3072 struct nvme_dev *dev = pci_get_drvdata(pdev);
3073 nvme_dev_shutdown(dev);
3074}
3075
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003076static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003077{
3078 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003079
3080 spin_lock(&dev_list_lock);
3081 list_del_init(&dev->node);
3082 spin_unlock(&dev_list_lock);
3083
3084 pci_set_drvdata(pdev, NULL);
3085 flush_work(&dev->reset_work);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003086 nvme_dev_shutdown(dev);
Keith Buschc9d3bf82015-01-07 18:55:52 -07003087 nvme_dev_remove(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003088 nvme_dev_remove_admin(dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07003089 device_destroy(nvme_class, MKDEV(nvme_char_major, dev->instance));
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003090 nvme_free_queues(dev, 0);
Keith Busch9a6b9452013-12-10 13:10:36 -07003091 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07003092 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003093}
3094
3095/* These functions are yet to be implemented */
3096#define nvme_error_detected NULL
3097#define nvme_dump_registers NULL
3098#define nvme_link_reset NULL
3099#define nvme_slot_reset NULL
3100#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06003101
Jingoo Han671a6012014-02-13 11:19:14 +09003102#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06003103static int nvme_suspend(struct device *dev)
3104{
3105 struct pci_dev *pdev = to_pci_dev(dev);
3106 struct nvme_dev *ndev = pci_get_drvdata(pdev);
3107
3108 nvme_dev_shutdown(ndev);
3109 return 0;
3110}
3111
3112static int nvme_resume(struct device *dev)
3113{
3114 struct pci_dev *pdev = to_pci_dev(dev);
3115 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06003116
Keith Busch9a6b9452013-12-10 13:10:36 -07003117 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Tejun Heo9ca97372014-03-07 10:24:49 -05003118 ndev->reset_workfn = nvme_reset_failed_dev;
Keith Busch9a6b9452013-12-10 13:10:36 -07003119 queue_work(nvme_workq, &ndev->reset_work);
3120 }
3121 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06003122}
Jingoo Han671a6012014-02-13 11:19:14 +09003123#endif
Keith Buschcd638942013-07-15 15:02:23 -06003124
3125static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003126
Stephen Hemminger1d352032012-09-07 09:33:17 -07003127static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003128 .error_detected = nvme_error_detected,
3129 .mmio_enabled = nvme_dump_registers,
3130 .link_reset = nvme_link_reset,
3131 .slot_reset = nvme_slot_reset,
3132 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06003133 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003134};
3135
3136/* Move to pci_ids.h later */
3137#define PCI_CLASS_STORAGE_EXPRESS 0x010802
3138
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003139static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003140 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
3141 { 0, }
3142};
3143MODULE_DEVICE_TABLE(pci, nvme_id_table);
3144
3145static struct pci_driver nvme_driver = {
3146 .name = "nvme",
3147 .id_table = nvme_id_table,
3148 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003149 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05003150 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06003151 .driver = {
3152 .pm = &nvme_dev_pm_ops,
3153 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003154 .err_handler = &nvme_err_handler,
3155};
3156
3157static int __init nvme_init(void)
3158{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003159 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003160
Dan McLeranb9afca32014-04-07 17:10:11 -06003161 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003162
Keith Busch9a6b9452013-12-10 13:10:36 -07003163 nvme_workq = create_singlethread_workqueue("nvme");
3164 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06003165 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07003166
Keith Busch5c42ea12012-07-25 16:05:18 -06003167 result = register_blkdev(nvme_major, "nvme");
3168 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07003169 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06003170 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003171 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003172
Keith Buschb3fffde2015-02-03 11:21:42 -07003173 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
3174 &nvme_dev_fops);
3175 if (result < 0)
3176 goto unregister_blkdev;
3177 else if (result > 0)
3178 nvme_char_major = result;
3179
3180 nvme_class = class_create(THIS_MODULE, "nvme");
3181 if (!nvme_class)
3182 goto unregister_chrdev;
3183
Keith Buschf3db22f2014-06-11 11:51:35 -06003184 result = pci_register_driver(&nvme_driver);
3185 if (result)
Keith Buschb3fffde2015-02-03 11:21:42 -07003186 goto destroy_class;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003187 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003188
Keith Buschb3fffde2015-02-03 11:21:42 -07003189 destroy_class:
3190 class_destroy(nvme_class);
3191 unregister_chrdev:
3192 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003193 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003194 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003195 kill_workq:
3196 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003197 return result;
3198}
3199
3200static void __exit nvme_exit(void)
3201{
3202 pci_unregister_driver(&nvme_driver);
Keith Buschf3db22f2014-06-11 11:51:35 -06003203 unregister_hotcpu_notifier(&nvme_nb);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003204 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003205 destroy_workqueue(nvme_workq);
Keith Buschb3fffde2015-02-03 11:21:42 -07003206 class_destroy(nvme_class);
3207 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Dan McLeranb9afca32014-04-07 17:10:11 -06003208 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04003209 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003210}
3211
3212MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
3213MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07003214MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003215module_init(nvme_init);
3216module_exit(nvme_exit);