blob: e92bdf4c68fcd50a19d84a2aae8f3d2eed10afc0 [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003 * Copyright (c) 2011-2014, Intel Corporation.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050013 */
14
15#include <linux/nvme.h>
Matthew Wilcox8de05532011-05-12 13:50:28 -040016#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050017#include <linux/blkdev.h>
Matias Bjørlinga4aea562014-11-04 08:20:14 -070018#include <linux/blk-mq.h>
Keith Busch42f61422014-03-24 10:46:25 -060019#include <linux/cpu.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040020#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050021#include <linux/errno.h>
22#include <linux/fs.h>
23#include <linux/genhd.h>
Keith Busch4cc09e22014-04-02 15:45:37 -060024#include <linux/hdreg.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040025#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050026#include <linux/init.h>
27#include <linux/interrupt.h>
28#include <linux/io.h>
29#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050030#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050031#include <linux/kernel.h>
32#include <linux/mm.h>
33#include <linux/module.h>
34#include <linux/moduleparam.h>
35#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050036#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040037#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050038#include <linux/sched.h>
39#include <linux/slab.h>
40#include <linux/types.h>
Vishal Verma5d0f6132013-03-04 18:40:58 -070041#include <scsi/sg.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090042#include <asm-generic/io-64-nonatomic-lo-hi.h>
43
Keith Busch9d43cf62014-05-13 11:42:02 -060044#define NVME_Q_DEPTH 1024
Matias Bjørlinga4aea562014-11-04 08:20:14 -070045#define NVME_AQ_DEPTH 64
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050046#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
47#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Keith Busch9d43cf62014-05-13 11:42:02 -060048#define ADMIN_TIMEOUT (admin_timeout * HZ)
Dan McLeran2484f402014-07-01 09:33:32 -060049#define SHUTDOWN_TIMEOUT (shutdown_timeout * HZ)
Keith Busch9d43cf62014-05-13 11:42:02 -060050#define IOD_TIMEOUT (retry_time * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050051
Keith Busch9d43cf62014-05-13 11:42:02 -060052static unsigned char admin_timeout = 60;
53module_param(admin_timeout, byte, 0644);
54MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050055
Matthew Wilcoxbd676082014-06-03 23:04:30 -040056unsigned char nvme_io_timeout = 30;
57module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
Keith Buschb3550842014-04-04 11:43:36 -060058MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050059
Keith Busch61e4ce02014-05-13 11:42:01 -060060static unsigned char retry_time = 30;
61module_param(retry_time, byte, 0644);
62MODULE_PARM_DESC(retry_time, "time in seconds to retry failed I/O");
63
Dan McLeran2484f402014-07-01 09:33:32 -060064static unsigned char shutdown_timeout = 5;
65module_param(shutdown_timeout, byte, 0644);
66MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
67
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050068static int nvme_major;
69module_param(nvme_major, int, 0);
70
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050071static int use_threaded_interrupts;
72module_param(use_threaded_interrupts, int, 0);
73
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050074static DEFINE_SPINLOCK(dev_list_lock);
75static LIST_HEAD(dev_list);
76static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070077static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060078static wait_queue_head_t nvme_kthread_wait;
Keith Buschf3db22f2014-06-11 11:51:35 -060079static struct notifier_block nvme_nb;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050080
Keith Buschd4b4ff82013-12-10 13:10:37 -070081static void nvme_reset_failed_dev(struct work_struct *ws);
Matias Bjørlinga4aea562014-11-04 08:20:14 -070082static int nvme_process_cq(struct nvme_queue *nvmeq);
Keith Buschd4b4ff82013-12-10 13:10:37 -070083
Keith Busch4d115422013-12-10 13:10:40 -070084struct async_cmd_info {
85 struct kthread_work work;
86 struct kthread_worker *worker;
Matias Bjørlinga4aea562014-11-04 08:20:14 -070087 struct request *req;
Keith Busch4d115422013-12-10 13:10:40 -070088 u32 result;
89 int status;
90 void *ctx;
91};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050092
93/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050094 * An NVM Express queue. Each device has at least two (one for admin
95 * commands and one for I/O commands).
96 */
97struct nvme_queue {
Keith Buschf435c282014-07-07 09:14:42 -060098 struct llist_node node;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050099 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500100 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500101 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500102 spinlock_t q_lock;
103 struct nvme_command *sq_cmds;
104 volatile struct nvme_completion *cqes;
105 dma_addr_t sq_dma_addr;
106 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500107 u32 __iomem *q_db;
108 u16 q_depth;
109 u16 cq_vector;
110 u16 sq_head;
111 u16 sq_tail;
112 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700113 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400114 u8 cq_phase;
115 u8 cqe_seen;
Keith Busch4d115422013-12-10 13:10:40 -0700116 struct async_cmd_info cmdinfo;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700117 struct blk_mq_hw_ctx *hctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500118};
119
120/*
121 * Check we didin't inadvertently grow the command struct
122 */
123static inline void _nvme_check_size(void)
124{
125 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
126 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
127 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
128 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
129 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400130 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700131 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500132 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
133 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
134 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
135 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600136 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500137}
138
Keith Buschedd10d32014-04-03 16:45:23 -0600139typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400140 struct nvme_completion *);
141
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500142struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400143 nvme_completion_fn fn;
144 void *ctx;
Keith Buschc30341d2013-12-10 13:10:38 -0700145 int aborted;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700146 struct nvme_queue *nvmeq;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500147};
148
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700149static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
150 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500151{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700152 struct nvme_dev *dev = data;
153 struct nvme_queue *nvmeq = dev->queues[0];
154
155 WARN_ON(nvmeq->hctx);
156 nvmeq->hctx = hctx;
157 hctx->driver_data = nvmeq;
158 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500159}
160
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700161static int nvme_admin_init_request(void *data, struct request *req,
162 unsigned int hctx_idx, unsigned int rq_idx,
163 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600164{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700165 struct nvme_dev *dev = data;
166 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
167 struct nvme_queue *nvmeq = dev->queues[0];
168
169 BUG_ON(!nvmeq);
170 cmd->nvmeq = nvmeq;
171 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600172}
173
Jens Axboe2c30540b2014-11-14 09:47:32 -0700174static void nvme_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
175{
176 struct nvme_queue *nvmeq = hctx->driver_data;
177
178 nvmeq->hctx = NULL;
179}
180
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700181static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
182 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500183{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700184 struct nvme_dev *dev = data;
185 struct nvme_queue *nvmeq = dev->queues[
186 (hctx_idx % dev->queue_count) + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500187
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700188 if (!nvmeq->hctx)
189 nvmeq->hctx = hctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500190
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700191 /* nvmeq queues are shared between namespaces. We assume here that
192 * blk-mq map the tags so they match up with the nvme queue tags. */
193 WARN_ON(nvmeq->hctx->tags != hctx->tags);
194
195 hctx->driver_data = nvmeq;
196 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500197}
198
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700199static int nvme_init_request(void *data, struct request *req,
200 unsigned int hctx_idx, unsigned int rq_idx,
201 unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500202{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700203 struct nvme_dev *dev = data;
204 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
205 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
206
207 BUG_ON(!nvmeq);
208 cmd->nvmeq = nvmeq;
209 return 0;
210}
211
212static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
213 nvme_completion_fn handler)
214{
215 cmd->fn = handler;
216 cmd->ctx = ctx;
217 cmd->aborted = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500218}
219
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400220/* Special values must be less than 0x1000 */
221#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500222#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
223#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
224#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500225
Keith Buschedd10d32014-04-03 16:45:23 -0600226static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400227 struct nvme_completion *cqe)
228{
229 if (ctx == CMD_CTX_CANCELLED)
230 return;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400231 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600232 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400233 "completed id %d twice on queue %d\n",
234 cqe->command_id, le16_to_cpup(&cqe->sq_id));
235 return;
236 }
237 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600238 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400239 "invalid id %d completed on queue %d\n",
240 cqe->command_id, le16_to_cpup(&cqe->sq_id));
241 return;
242 }
Keith Buschedd10d32014-04-03 16:45:23 -0600243 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400244}
245
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700246static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
247{
248 void *ctx;
249
250 if (fn)
251 *fn = cmd->fn;
252 ctx = cmd->ctx;
253 cmd->fn = special_completion;
254 cmd->ctx = CMD_CTX_CANCELLED;
255 return ctx;
256}
257
258static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
259 struct nvme_completion *cqe)
260{
261 struct request *req = ctx;
262
263 u32 result = le32_to_cpup(&cqe->result);
264 u16 status = le16_to_cpup(&cqe->status) >> 1;
265
266 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
267 ++nvmeq->dev->event_limit;
268 if (status == NVME_SC_SUCCESS)
269 dev_warn(nvmeq->q_dmadev,
270 "async event result %08x\n", result);
271
Jens Axboe9d135bb2014-11-17 10:43:42 -0700272 blk_mq_free_hctx_request(nvmeq->hctx, req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700273}
274
275static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
276 struct nvme_completion *cqe)
277{
278 struct request *req = ctx;
279
280 u16 status = le16_to_cpup(&cqe->status) >> 1;
281 u32 result = le32_to_cpup(&cqe->result);
282
Jens Axboe9d135bb2014-11-17 10:43:42 -0700283 blk_mq_free_hctx_request(nvmeq->hctx, req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700284
285 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
286 ++nvmeq->dev->abort_limit;
287}
288
Keith Buschedd10d32014-04-03 16:45:23 -0600289static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700290 struct nvme_completion *cqe)
291{
292 struct async_cmd_info *cmdinfo = ctx;
293 cmdinfo->result = le32_to_cpup(&cqe->result);
294 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
295 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
Jens Axboe9d135bb2014-11-17 10:43:42 -0700296 blk_mq_free_hctx_request(nvmeq->hctx, cmdinfo->req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700297}
298
299static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
300 unsigned int tag)
301{
302 struct blk_mq_hw_ctx *hctx = nvmeq->hctx;
303 struct request *req = blk_mq_tag_to_rq(hctx->tags, tag);
304
305 return blk_mq_rq_to_pdu(req);
Keith Busch4d115422013-12-10 13:10:40 -0700306}
307
Matthew Wilcox184d2942011-05-11 21:36:38 -0400308/*
309 * Called with local interrupts disabled and the q_lock held. May not sleep.
310 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700311static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400312 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500313{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700314 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400315 void *ctx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700316 if (tag >= nvmeq->q_depth) {
317 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500318 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400319 }
Keith Busch859361a2012-08-02 14:05:59 -0600320 if (fn)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700321 *fn = cmd->fn;
322 ctx = cmd->ctx;
323 cmd->fn = special_completion;
324 cmd->ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400325 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500326}
327
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500328/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400329 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500330 * @nvmeq: The queue to use
331 * @cmd: The command to send
332 *
333 * Safe to use from interrupt context
334 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700335static int __nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500336{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700337 u16 tail = nvmeq->sq_tail;
338
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500339 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500340 if (++tail == nvmeq->q_depth)
341 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500342 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500343 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500344
345 return 0;
346}
347
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700348static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
349{
350 unsigned long flags;
351 int ret;
352 spin_lock_irqsave(&nvmeq->q_lock, flags);
353 ret = __nvme_submit_cmd(nvmeq, cmd);
354 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
355 return ret;
356}
357
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500358static __le64 **iod_list(struct nvme_iod *iod)
359{
360 return ((void *)iod) + iod->offset;
361}
362
363/*
364 * Will slightly overestimate the number of pages needed. This is OK
365 * as it only leads to a small amount of wasted memory for the lifetime of
366 * the I/O.
367 */
Keith Busch1d090622014-06-23 11:34:01 -0600368static int nvme_npages(unsigned size, struct nvme_dev *dev)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500369{
Keith Busch1d090622014-06-23 11:34:01 -0600370 unsigned nprps = DIV_ROUND_UP(size + dev->page_size, dev->page_size);
371 return DIV_ROUND_UP(8 * nprps, dev->page_size - 8);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500372}
373
374static struct nvme_iod *
Keith Busch1d090622014-06-23 11:34:01 -0600375nvme_alloc_iod(unsigned nseg, unsigned nbytes, struct nvme_dev *dev, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500376{
377 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Keith Busch1d090622014-06-23 11:34:01 -0600378 sizeof(__le64 *) * nvme_npages(nbytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500379 sizeof(struct scatterlist) * nseg, gfp);
380
381 if (iod) {
382 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
383 iod->npages = -1;
384 iod->length = nbytes;
Keith Busch2b196032012-11-06 11:59:23 -0700385 iod->nents = 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600386 iod->first_dma = 0ULL;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500387 }
388
389 return iod;
390}
391
Vishal Verma5d0f6132013-03-04 18:40:58 -0700392void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500393{
Keith Busch1d090622014-06-23 11:34:01 -0600394 const int last_prp = dev->page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500395 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500396 __le64 **list = iod_list(iod);
397 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500398
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500399 if (iod->npages == 0)
400 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
401 for (i = 0; i < iod->npages; i++) {
402 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500403 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500404 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500405 prp_dma = next_prp_dma;
406 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500407 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500408}
409
Keith Buschb4ff9c82014-08-29 09:06:12 -0600410static int nvme_error_status(u16 status)
411{
412 switch (status & 0x7ff) {
413 case NVME_SC_SUCCESS:
414 return 0;
415 case NVME_SC_CAP_EXCEEDED:
416 return -ENOSPC;
417 default:
418 return -EIO;
419 }
420}
421
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700422static void req_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500423 struct nvme_completion *cqe)
424{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500425 struct nvme_iod *iod = ctx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700426 struct request *req = iod->private;
427 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
428
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500429 u16 status = le16_to_cpup(&cqe->status) >> 1;
430
Keith Buschedd10d32014-04-03 16:45:23 -0600431 if (unlikely(status)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700432 if (!(status & NVME_SC_DNR || blk_noretry_request(req))
433 && (jiffies - req->start_time) < req->timeout) {
434 blk_mq_requeue_request(req);
435 blk_mq_kick_requeue_list(req->q);
Keith Buschedd10d32014-04-03 16:45:23 -0600436 return;
437 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700438 req->errors = nvme_error_status(status);
439 } else
440 req->errors = 0;
441
442 if (cmd_rq->aborted)
443 dev_warn(&nvmeq->dev->pci_dev->dev,
444 "completing aborted command with status:%04x\n",
445 status);
446
447 if (iod->nents)
448 dma_unmap_sg(&nvmeq->dev->pci_dev->dev, iod->sg, iod->nents,
449 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Buschedd10d32014-04-03 16:45:23 -0600450 nvme_free_iod(nvmeq->dev, iod);
Keith Busch3291fa52014-04-28 12:30:52 -0600451
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700452 blk_mq_complete_request(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500453}
454
Matthew Wilcox184d2942011-05-11 21:36:38 -0400455/* length is in bytes. gfp flags indicates whether we may sleep. */
Keith Buschedd10d32014-04-03 16:45:23 -0600456int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod, int total_len,
457 gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500458{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500459 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500460 int length = total_len;
461 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500462 int dma_len = sg_dma_len(sg);
463 u64 dma_addr = sg_dma_address(sg);
464 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500465 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500466 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500467 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500468 int nprps, i;
Keith Busch1d090622014-06-23 11:34:01 -0600469 u32 page_size = dev->page_size;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500470
Keith Busch1d090622014-06-23 11:34:01 -0600471 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500472 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500473 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500474
Keith Busch1d090622014-06-23 11:34:01 -0600475 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500476 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600477 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500478 } else {
479 sg = sg_next(sg);
480 dma_addr = sg_dma_address(sg);
481 dma_len = sg_dma_len(sg);
482 }
483
Keith Busch1d090622014-06-23 11:34:01 -0600484 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600485 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500486 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500487 }
488
Keith Busch1d090622014-06-23 11:34:01 -0600489 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500490 if (nprps <= (256 / 8)) {
491 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500492 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500493 } else {
494 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500495 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500496 }
497
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400498 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
499 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600500 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500501 iod->npages = -1;
Keith Busch1d090622014-06-23 11:34:01 -0600502 return (total_len - length) + page_size;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400503 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500504 list[0] = prp_list;
505 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500506 i = 0;
507 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600508 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500509 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400510 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500511 if (!prp_list)
512 return total_len - length;
513 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400514 prp_list[0] = old_prp_list[i - 1];
515 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
516 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500517 }
518 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600519 dma_len -= page_size;
520 dma_addr += page_size;
521 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500522 if (length <= 0)
523 break;
524 if (dma_len > 0)
525 continue;
526 BUG_ON(dma_len < 0);
527 sg = sg_next(sg);
528 dma_addr = sg_dma_address(sg);
529 dma_len = sg_dma_len(sg);
530 }
531
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500532 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500533}
534
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700535/*
536 * We reuse the small pool to allocate the 16-byte range here as it is not
537 * worth having a special pool for these or additional cases to handle freeing
538 * the iod.
539 */
540static void nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
541 struct request *req, struct nvme_iod *iod)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700542{
Keith Buschedd10d32014-04-03 16:45:23 -0600543 struct nvme_dsm_range *range =
544 (struct nvme_dsm_range *)iod_list(iod)[0];
Keith Busch0e5e4f02012-11-09 16:33:05 -0700545 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
546
Keith Busch0e5e4f02012-11-09 16:33:05 -0700547 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700548 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
549 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700550
551 memset(cmnd, 0, sizeof(*cmnd));
552 cmnd->dsm.opcode = nvme_cmd_dsm;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700553 cmnd->dsm.command_id = req->tag;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700554 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
555 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
556 cmnd->dsm.nr = 0;
557 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
558
559 if (++nvmeq->sq_tail == nvmeq->q_depth)
560 nvmeq->sq_tail = 0;
561 writel(nvmeq->sq_tail, nvmeq->q_db);
Keith Busch0e5e4f02012-11-09 16:33:05 -0700562}
563
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700564static void nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500565 int cmdid)
566{
567 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
568
569 memset(cmnd, 0, sizeof(*cmnd));
570 cmnd->common.opcode = nvme_cmd_flush;
571 cmnd->common.command_id = cmdid;
572 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
573
574 if (++nvmeq->sq_tail == nvmeq->q_depth)
575 nvmeq->sq_tail = 0;
576 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500577}
578
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700579static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod,
580 struct nvme_ns *ns)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500581{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700582 struct request *req = iod->private;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500583 struct nvme_command *cmnd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700584 u16 control = 0;
585 u32 dsmgmt = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500586
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700587 if (req->cmd_flags & REQ_FUA)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500588 control |= NVME_RW_FUA;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700589 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500590 control |= NVME_RW_LR;
591
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700592 if (req->cmd_flags & REQ_RAHEAD)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500593 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
594
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500595 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500596 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500597
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700598 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
599 cmnd->rw.command_id = req->tag;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500600 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Keith Buschedd10d32014-04-03 16:45:23 -0600601 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
602 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700603 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
604 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500605 cmnd->rw.control = cpu_to_le16(control);
606 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500607
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500608 if (++nvmeq->sq_tail == nvmeq->q_depth)
609 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500610 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500611
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500612 return 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600613}
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500614
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700615static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
616 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600617{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700618 struct nvme_ns *ns = hctx->queue->queuedata;
619 struct nvme_queue *nvmeq = hctx->driver_data;
620 struct request *req = bd->rq;
621 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
Keith Buschedd10d32014-04-03 16:45:23 -0600622 struct nvme_iod *iod;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700623 int psegs = req->nr_phys_segments;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700624 enum dma_data_direction dma_dir;
625 unsigned size = !(req->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(req) :
Keith Busch9dbbfab2014-10-06 15:23:06 -0600626 sizeof(struct nvme_dsm_range);
Keith Buschedd10d32014-04-03 16:45:23 -0600627
Keith Busch9dbbfab2014-10-06 15:23:06 -0600628 iod = nvme_alloc_iod(psegs, size, ns->dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600629 if (!iod)
Jens Axboefe543032014-12-11 13:58:39 -0700630 return BLK_MQ_RQ_QUEUE_BUSY;
Keith Buschedd10d32014-04-03 16:45:23 -0600631
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700632 iod->private = req;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700633
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700634 if (req->cmd_flags & REQ_DISCARD) {
Keith Buschedd10d32014-04-03 16:45:23 -0600635 void *range;
636 /*
637 * We reuse the small pool to allocate the 16-byte range here
638 * as it is not worth having a special pool for these or
639 * additional cases to handle freeing the iod.
640 */
641 range = dma_pool_alloc(nvmeq->dev->prp_small_pool,
642 GFP_ATOMIC,
643 &iod->first_dma);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700644 if (!range)
Jens Axboefe543032014-12-11 13:58:39 -0700645 goto retry_cmd;
Keith Buschedd10d32014-04-03 16:45:23 -0600646 iod_list(iod)[0] = (__le64 *)range;
647 iod->npages = 0;
648 } else if (psegs) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700649 dma_dir = rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
Keith Buschedd10d32014-04-03 16:45:23 -0600650
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700651 sg_init_table(iod->sg, psegs);
652 iod->nents = blk_rq_map_sg(req->q, req, iod->sg);
Jens Axboefe543032014-12-11 13:58:39 -0700653 if (!iod->nents)
654 goto error_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700655
656 if (!dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir))
Jens Axboefe543032014-12-11 13:58:39 -0700657 goto retry_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700658
Jens Axboefe543032014-12-11 13:58:39 -0700659 if (blk_rq_bytes(req) !=
660 nvme_setup_prps(nvmeq->dev, iod, blk_rq_bytes(req), GFP_ATOMIC)) {
661 dma_unmap_sg(&nvmeq->dev->pci_dev->dev, iod->sg,
662 iod->nents, dma_dir);
663 goto retry_cmd;
664 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700665 }
666
667 blk_mq_start_request(req);
668
Keith Busch9af87852014-12-03 17:07:13 -0700669 nvme_set_info(cmd, iod, req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700670 spin_lock_irq(&nvmeq->q_lock);
671 if (req->cmd_flags & REQ_DISCARD)
672 nvme_submit_discard(nvmeq, ns, req, iod);
673 else if (req->cmd_flags & REQ_FLUSH)
674 nvme_submit_flush(nvmeq, ns, req->tag);
675 else
676 nvme_submit_iod(nvmeq, iod, ns);
677
678 nvme_process_cq(nvmeq);
679 spin_unlock_irq(&nvmeq->q_lock);
680 return BLK_MQ_RQ_QUEUE_OK;
681
Jens Axboefe543032014-12-11 13:58:39 -0700682 error_cmd:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500683 nvme_free_iod(nvmeq->dev, iod);
Jens Axboefe543032014-12-11 13:58:39 -0700684 return BLK_MQ_RQ_QUEUE_ERROR;
685 retry_cmd:
686 nvme_free_iod(nvmeq->dev, iod);
687 return BLK_MQ_RQ_QUEUE_BUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500688}
689
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400690static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500691{
Matthew Wilcox82123462011-01-20 13:24:06 -0500692 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500693
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500694 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500695 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500696
697 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400698 void *ctx;
699 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500700 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500701 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500702 break;
703 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
704 if (++head == nvmeq->q_depth) {
705 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500706 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500707 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700708 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600709 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500710 }
711
712 /* If the controller ignores the cq head doorbell and continuously
713 * writes to the queue, it is theoretically possible to wrap around
714 * the queue twice and mistakenly return IRQ_NONE. Linux only
715 * requires that 0.1% of your interrupts are handled, so this isn't
716 * a big problem.
717 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500718 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400719 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500720
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800721 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500722 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500723 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500724
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400725 nvmeq->cqe_seen = 1;
726 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500727}
728
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700729/* Admin queue isn't initialized as a request queue. If at some point this
730 * happens anyway, make sure to notify the user */
731static int nvme_admin_queue_rq(struct blk_mq_hw_ctx *hctx,
732 const struct blk_mq_queue_data *bd)
Matthew Wilcox7d822452013-06-24 12:03:57 -0400733{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700734 WARN_ON_ONCE(1);
735 return BLK_MQ_RQ_QUEUE_ERROR;
Matthew Wilcox7d822452013-06-24 12:03:57 -0400736}
737
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500738static irqreturn_t nvme_irq(int irq, void *data)
739{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500740 irqreturn_t result;
741 struct nvme_queue *nvmeq = data;
742 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400743 nvme_process_cq(nvmeq);
744 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
745 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500746 spin_unlock(&nvmeq->q_lock);
747 return result;
748}
749
750static irqreturn_t nvme_irq_check(int irq, void *data)
751{
752 struct nvme_queue *nvmeq = data;
753 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
754 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
755 return IRQ_NONE;
756 return IRQ_WAKE_THREAD;
757}
758
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700759static void nvme_abort_cmd_info(struct nvme_queue *nvmeq, struct nvme_cmd_info *
760 cmd_info)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500761{
762 spin_lock_irq(&nvmeq->q_lock);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700763 cancel_cmd_info(cmd_info, NULL);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500764 spin_unlock_irq(&nvmeq->q_lock);
765}
766
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400767struct sync_cmd_info {
768 struct task_struct *task;
769 u32 result;
770 int status;
771};
772
Keith Buschedd10d32014-04-03 16:45:23 -0600773static void sync_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400774 struct nvme_completion *cqe)
775{
776 struct sync_cmd_info *cmdinfo = ctx;
777 cmdinfo->result = le32_to_cpup(&cqe->result);
778 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
779 wake_up_process(cmdinfo->task);
780}
781
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500782/*
783 * Returns 0 on success. If the result is negative, it's a Linux error code;
784 * if the result is positive, it's an NVM Express status code
785 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700786static int nvme_submit_sync_cmd(struct request *req, struct nvme_command *cmd,
Vishal Verma5d0f6132013-03-04 18:40:58 -0700787 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500788{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700789 int ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500790 struct sync_cmd_info cmdinfo;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700791 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
792 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500793
794 cmdinfo.task = current;
795 cmdinfo.status = -EINTR;
796
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700797 cmd->common.command_id = req->tag;
798
799 nvme_set_info(cmd_rq, &cmdinfo, sync_completion);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500800
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500801 set_current_state(TASK_KILLABLE);
Keith Busch4f5099a2014-03-03 16:39:13 -0700802 ret = nvme_submit_cmd(nvmeq, cmd);
803 if (ret) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700804 nvme_finish_cmd(nvmeq, req->tag, NULL);
Keith Busch4f5099a2014-03-03 16:39:13 -0700805 set_current_state(TASK_RUNNING);
Keith Busch4f5099a2014-03-03 16:39:13 -0700806 }
Keith Busch78f8d252013-04-19 14:11:06 -0600807 schedule_timeout(timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500808
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500809 if (cmdinfo.status == -EINTR) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700810 nvme_abort_cmd_info(nvmeq, blk_mq_rq_to_pdu(req));
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500811 return -EINTR;
812 }
813
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500814 if (result)
815 *result = cmdinfo.result;
816
817 return cmdinfo.status;
818}
819
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700820static int nvme_submit_async_admin_req(struct nvme_dev *dev)
821{
822 struct nvme_queue *nvmeq = dev->queues[0];
823 struct nvme_command c;
824 struct nvme_cmd_info *cmd_info;
825 struct request *req;
826
Jens Axboe6dcc0cf2014-11-18 08:21:18 -0700827 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC, false);
Dan Carpenter9f173b32014-11-05 23:39:09 +0300828 if (IS_ERR(req))
829 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700830
831 cmd_info = blk_mq_rq_to_pdu(req);
832 nvme_set_info(cmd_info, req, async_req_completion);
833
834 memset(&c, 0, sizeof(c));
835 c.common.opcode = nvme_admin_async_event;
836 c.common.command_id = req->tag;
837
838 return __nvme_submit_cmd(nvmeq, &c);
839}
840
841static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
Keith Busch4d115422013-12-10 13:10:40 -0700842 struct nvme_command *cmd,
843 struct async_cmd_info *cmdinfo, unsigned timeout)
844{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700845 struct nvme_queue *nvmeq = dev->queues[0];
846 struct request *req;
847 struct nvme_cmd_info *cmd_rq;
Keith Busch4d115422013-12-10 13:10:40 -0700848
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700849 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_KERNEL, false);
Dan Carpenter9f173b32014-11-05 23:39:09 +0300850 if (IS_ERR(req))
851 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700852
853 req->timeout = timeout;
854 cmd_rq = blk_mq_rq_to_pdu(req);
855 cmdinfo->req = req;
856 nvme_set_info(cmd_rq, cmdinfo, async_completion);
Keith Busch4d115422013-12-10 13:10:40 -0700857 cmdinfo->status = -EINTR;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700858
859 cmd->common.command_id = req->tag;
860
Keith Busch4f5099a2014-03-03 16:39:13 -0700861 return nvme_submit_cmd(nvmeq, cmd);
Keith Busch4d115422013-12-10 13:10:40 -0700862}
863
kbuild test robota64e6bb2014-11-05 18:47:07 +0800864static int __nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700865 u32 *result, unsigned timeout)
866{
867 int res;
868 struct request *req;
869
870 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_KERNEL, false);
Jens Axboe97fe3832014-12-10 13:02:44 -0700871 if (IS_ERR(req))
872 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700873 res = nvme_submit_sync_cmd(req, cmd, result, timeout);
Jens Axboe9d135bb2014-11-17 10:43:42 -0700874 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700875 return res;
876}
877
Vishal Verma5d0f6132013-03-04 18:40:58 -0700878int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500879 u32 *result)
880{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700881 return __nvme_submit_admin_cmd(dev, cmd, result, ADMIN_TIMEOUT);
Keith Busch4f5099a2014-03-03 16:39:13 -0700882}
883
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700884int nvme_submit_io_cmd(struct nvme_dev *dev, struct nvme_ns *ns,
885 struct nvme_command *cmd, u32 *result)
Keith Busch4f5099a2014-03-03 16:39:13 -0700886{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700887 int res;
888 struct request *req;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500889
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700890 req = blk_mq_alloc_request(ns->queue, WRITE, (GFP_KERNEL|__GFP_WAIT),
891 false);
Jens Axboe97fe3832014-12-10 13:02:44 -0700892 if (IS_ERR(req))
893 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700894 res = nvme_submit_sync_cmd(req, cmd, result, NVME_IO_TIMEOUT);
Jens Axboe9d135bb2014-11-17 10:43:42 -0700895 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700896 return res;
Keith Busch4d115422013-12-10 13:10:40 -0700897}
898
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500899static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
900{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500901 struct nvme_command c;
902
903 memset(&c, 0, sizeof(c));
904 c.delete_queue.opcode = opcode;
905 c.delete_queue.qid = cpu_to_le16(id);
906
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700907 return nvme_submit_admin_cmd(dev, &c, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500908}
909
910static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
911 struct nvme_queue *nvmeq)
912{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500913 struct nvme_command c;
914 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
915
916 memset(&c, 0, sizeof(c));
917 c.create_cq.opcode = nvme_admin_create_cq;
918 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
919 c.create_cq.cqid = cpu_to_le16(qid);
920 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
921 c.create_cq.cq_flags = cpu_to_le16(flags);
922 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
923
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700924 return nvme_submit_admin_cmd(dev, &c, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500925}
926
927static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
928 struct nvme_queue *nvmeq)
929{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500930 struct nvme_command c;
931 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
932
933 memset(&c, 0, sizeof(c));
934 c.create_sq.opcode = nvme_admin_create_sq;
935 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
936 c.create_sq.sqid = cpu_to_le16(qid);
937 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
938 c.create_sq.sq_flags = cpu_to_le16(flags);
939 c.create_sq.cqid = cpu_to_le16(qid);
940
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700941 return nvme_submit_admin_cmd(dev, &c, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500942}
943
944static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
945{
946 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
947}
948
949static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
950{
951 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
952}
953
Vishal Verma5d0f6132013-03-04 18:40:58 -0700954int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400955 dma_addr_t dma_addr)
956{
957 struct nvme_command c;
958
959 memset(&c, 0, sizeof(c));
960 c.identify.opcode = nvme_admin_identify;
961 c.identify.nsid = cpu_to_le32(nsid);
962 c.identify.prp1 = cpu_to_le64(dma_addr);
963 c.identify.cns = cpu_to_le32(cns);
964
965 return nvme_submit_admin_cmd(dev, &c, NULL);
966}
967
Vishal Verma5d0f6132013-03-04 18:40:58 -0700968int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -0600969 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400970{
971 struct nvme_command c;
972
973 memset(&c, 0, sizeof(c));
974 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -0600975 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400976 c.features.prp1 = cpu_to_le64(dma_addr);
977 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400978
Keith Busch08df1e02012-09-21 10:52:13 -0600979 return nvme_submit_admin_cmd(dev, &c, result);
Matthew Wilcoxdf348132012-01-11 07:29:56 -0700980}
981
Vishal Verma5d0f6132013-03-04 18:40:58 -0700982int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
983 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -0700984{
985 struct nvme_command c;
986
987 memset(&c, 0, sizeof(c));
988 c.features.opcode = nvme_admin_set_features;
989 c.features.prp1 = cpu_to_le64(dma_addr);
990 c.features.fid = cpu_to_le32(fid);
991 c.features.dword11 = cpu_to_le32(dword11);
992
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400993 return nvme_submit_admin_cmd(dev, &c, result);
994}
995
Matthew Wilcoxa09115b2012-08-07 15:56:23 -0400996/**
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700997 * nvme_abort_req - Attempt aborting a request
Keith Buschc30341d2013-12-10 13:10:38 -0700998 *
999 * Schedule controller reset if the command was already aborted once before and
1000 * still hasn't been returned to the driver, or if this is the admin queue.
1001 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001002static void nvme_abort_req(struct request *req)
Keith Buschc30341d2013-12-10 13:10:38 -07001003{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001004 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1005 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001006 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001007 struct request *abort_req;
1008 struct nvme_cmd_info *abort_cmd;
1009 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -07001010
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001011 if (!nvmeq->qid || cmd_rq->aborted) {
Keith Buschc30341d2013-12-10 13:10:38 -07001012 if (work_busy(&dev->reset_work))
1013 return;
1014 list_del_init(&dev->node);
1015 dev_warn(&dev->pci_dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001016 "I/O %d QID %d timeout, reset controller\n",
1017 req->tag, nvmeq->qid);
Tejun Heo9ca97372014-03-07 10:24:49 -05001018 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschc30341d2013-12-10 13:10:38 -07001019 queue_work(nvme_workq, &dev->reset_work);
1020 return;
1021 }
1022
1023 if (!dev->abort_limit)
1024 return;
1025
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001026 abort_req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC,
1027 false);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001028 if (IS_ERR(abort_req))
Keith Buschc30341d2013-12-10 13:10:38 -07001029 return;
1030
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001031 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1032 nvme_set_info(abort_cmd, abort_req, abort_completion);
1033
Keith Buschc30341d2013-12-10 13:10:38 -07001034 memset(&cmd, 0, sizeof(cmd));
1035 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001036 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001037 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001038 cmd.abort.command_id = abort_req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001039
1040 --dev->abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001041 cmd_rq->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -07001042
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001043 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", req->tag,
Keith Buschc30341d2013-12-10 13:10:38 -07001044 nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001045 if (nvme_submit_cmd(dev->queues[0], &cmd) < 0) {
1046 dev_warn(nvmeq->q_dmadev,
1047 "Could not abort I/O %d QID %d",
1048 req->tag, nvmeq->qid);
Sam Bradshawc87fd542014-12-10 13:00:31 -07001049 blk_mq_free_request(abort_req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001050 }
Keith Buschc30341d2013-12-10 13:10:38 -07001051}
1052
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001053static void nvme_cancel_queue_ios(struct blk_mq_hw_ctx *hctx,
1054 struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001055{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001056 struct nvme_queue *nvmeq = data;
1057 void *ctx;
1058 nvme_completion_fn fn;
1059 struct nvme_cmd_info *cmd;
1060 static struct nvme_completion cqe = {
1061 .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1),
1062 };
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001063
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001064 cmd = blk_mq_rq_to_pdu(req);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001065
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001066 if (cmd->ctx == CMD_CTX_CANCELLED)
1067 return;
1068
1069 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1070 req->tag, nvmeq->qid);
1071 ctx = cancel_cmd_info(cmd, &fn);
1072 fn(nvmeq, ctx, &cqe);
1073}
1074
1075static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
1076{
1077 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
1078 struct nvme_queue *nvmeq = cmd->nvmeq;
1079
1080 dev_warn(nvmeq->q_dmadev, "Timeout I/O %d QID %d\n", req->tag,
1081 nvmeq->qid);
1082 if (nvmeq->dev->initialized)
1083 nvme_abort_req(req);
1084
1085 /*
1086 * The aborted req will be completed on receiving the abort req.
1087 * We enable the timer again. If hit twice, it'll cause a device reset,
1088 * as the device then is in a faulty state.
1089 */
1090 return BLK_EH_RESET_TIMER;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001091}
1092
Keith Buschf435c282014-07-07 09:14:42 -06001093static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001094{
1095 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1096 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1097 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1098 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1099 kfree(nvmeq);
1100}
1101
Keith Buscha1a5ef92013-12-16 13:50:00 -05001102static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001103{
Keith Buschf435c282014-07-07 09:14:42 -06001104 LLIST_HEAD(q_list);
1105 struct nvme_queue *nvmeq, *next;
1106 struct llist_node *entry;
Keith Busch22404272013-07-15 15:02:20 -06001107 int i;
1108
Keith Buscha1a5ef92013-12-16 13:50:00 -05001109 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001110 struct nvme_queue *nvmeq = dev->queues[i];
Keith Buschf435c282014-07-07 09:14:42 -06001111 llist_add(&nvmeq->node, &q_list);
Keith Busch22404272013-07-15 15:02:20 -06001112 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001113 dev->queues[i] = NULL;
Keith Busch22404272013-07-15 15:02:20 -06001114 }
Keith Buschf435c282014-07-07 09:14:42 -06001115 synchronize_rcu();
1116 entry = llist_del_all(&q_list);
1117 llist_for_each_entry_safe(nvmeq, next, entry, node)
1118 nvme_free_queue(nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001119}
1120
Keith Busch4d115422013-12-10 13:10:40 -07001121/**
1122 * nvme_suspend_queue - put queue into suspended state
1123 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001124 */
1125static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001126{
Keith Busch4d115422013-12-10 13:10:40 -07001127 int vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001128
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001129 spin_lock_irq(&nvmeq->q_lock);
Keith Busch42f61422014-03-24 10:46:25 -06001130 nvmeq->dev->online_queues--;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001131 spin_unlock_irq(&nvmeq->q_lock);
1132
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001133 irq_set_affinity_hint(vector, NULL);
1134 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001135
Keith Busch4d115422013-12-10 13:10:40 -07001136 return 0;
1137}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001138
Keith Busch4d115422013-12-10 13:10:40 -07001139static void nvme_clear_queue(struct nvme_queue *nvmeq)
1140{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001141 struct blk_mq_hw_ctx *hctx = nvmeq->hctx;
1142
Keith Busch22404272013-07-15 15:02:20 -06001143 spin_lock_irq(&nvmeq->q_lock);
1144 nvme_process_cq(nvmeq);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001145 if (hctx && hctx->tags)
1146 blk_mq_tag_busy_iter(hctx, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001147 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001148}
1149
Keith Busch4d115422013-12-10 13:10:40 -07001150static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1151{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001152 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001153
1154 if (!nvmeq)
1155 return;
1156 if (nvme_suspend_queue(nvmeq))
1157 return;
1158
Keith Busch0e53d182013-12-10 13:10:39 -07001159 /* Don't tell the adapter to delete the admin queue.
1160 * Don't tell a removed adapter to delete IO queues. */
1161 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001162 adapter_delete_sq(dev, qid);
1163 adapter_delete_cq(dev, qid);
1164 }
Keith Busch4d115422013-12-10 13:10:40 -07001165 nvme_clear_queue(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001166}
1167
1168static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
1169 int depth, int vector)
1170{
1171 struct device *dmadev = &dev->pci_dev->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001172 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001173 if (!nvmeq)
1174 return NULL;
1175
Joe Perches4d51abf2014-06-15 13:37:33 -07001176 nvmeq->cqes = dma_zalloc_coherent(dmadev, CQ_SIZE(depth),
1177 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001178 if (!nvmeq->cqes)
1179 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001180
1181 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
1182 &nvmeq->sq_dma_addr, GFP_KERNEL);
1183 if (!nvmeq->sq_cmds)
1184 goto free_cqdma;
1185
1186 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001187 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001188 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1189 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001190 spin_lock_init(&nvmeq->q_lock);
1191 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001192 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001193 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001194 nvmeq->q_depth = depth;
1195 nvmeq->cq_vector = vector;
Keith Buschc30341d2013-12-10 13:10:38 -07001196 nvmeq->qid = qid;
Keith Busch22404272013-07-15 15:02:20 -06001197 dev->queue_count++;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001198 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001199
1200 return nvmeq;
1201
1202 free_cqdma:
Keith Busch68b8eca2013-05-01 13:07:47 -06001203 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001204 nvmeq->cq_dma_addr);
1205 free_nvmeq:
1206 kfree(nvmeq);
1207 return NULL;
1208}
1209
Matthew Wilcox30010822011-01-20 09:10:15 -05001210static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1211 const char *name)
1212{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001213 if (use_threaded_interrupts)
1214 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001215 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001216 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001217 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001218 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001219}
1220
Keith Busch22404272013-07-15 15:02:20 -06001221static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001222{
Keith Busch22404272013-07-15 15:02:20 -06001223 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001224
Keith Busch7be50e92014-09-10 15:48:47 -06001225 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001226 nvmeq->sq_tail = 0;
1227 nvmeq->cq_head = 0;
1228 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001229 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001230 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001231 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001232 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001233}
1234
1235static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1236{
1237 struct nvme_dev *dev = nvmeq->dev;
1238 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001239
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001240 result = adapter_alloc_cq(dev, qid, nvmeq);
1241 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001242 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001243
1244 result = adapter_alloc_sq(dev, qid, nvmeq);
1245 if (result < 0)
1246 goto release_cq;
1247
Matthew Wilcox3193f072014-01-27 15:57:22 -05001248 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001249 if (result < 0)
1250 goto release_sq;
1251
Keith Busch22404272013-07-15 15:02:20 -06001252 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001253 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001254
1255 release_sq:
1256 adapter_delete_sq(dev, qid);
1257 release_cq:
1258 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001259 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001260}
1261
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001262static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1263{
1264 unsigned long timeout;
1265 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1266
1267 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1268
1269 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1270 msleep(100);
1271 if (fatal_signal_pending(current))
1272 return -EINTR;
1273 if (time_after(jiffies, timeout)) {
1274 dev_err(&dev->pci_dev->dev,
Matthew Wilcox27e81662014-04-11 11:58:45 -04001275 "Device not ready; aborting %s\n", enabled ?
1276 "initialisation" : "reset");
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001277 return -ENODEV;
1278 }
1279 }
1280
1281 return 0;
1282}
1283
1284/*
1285 * If the device has been passed off to us in an enabled state, just clear
1286 * the enabled bit. The spec says we should set the 'shutdown notification
1287 * bits', but doing so may cause the device to complete commands to the
1288 * admin queue ... and we don't know what memory that might be pointing at!
1289 */
1290static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1291{
Dan McLeran01079522014-06-23 08:24:36 -06001292 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1293 dev->ctrl_config &= ~NVME_CC_ENABLE;
1294 writel(dev->ctrl_config, &dev->bar->cc);
Matthew Wilcox44af1462013-05-04 06:43:17 -04001295
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001296 return nvme_wait_ready(dev, cap, false);
1297}
1298
1299static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1300{
Dan McLeran01079522014-06-23 08:24:36 -06001301 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1302 dev->ctrl_config |= NVME_CC_ENABLE;
1303 writel(dev->ctrl_config, &dev->bar->cc);
1304
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001305 return nvme_wait_ready(dev, cap, true);
1306}
1307
Keith Busch1894d8f2013-07-15 15:02:22 -06001308static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1309{
1310 unsigned long timeout;
Keith Busch1894d8f2013-07-15 15:02:22 -06001311
Dan McLeran01079522014-06-23 08:24:36 -06001312 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1313 dev->ctrl_config |= NVME_CC_SHN_NORMAL;
1314
1315 writel(dev->ctrl_config, &dev->bar->cc);
Keith Busch1894d8f2013-07-15 15:02:22 -06001316
Dan McLeran2484f402014-07-01 09:33:32 -06001317 timeout = SHUTDOWN_TIMEOUT + jiffies;
Keith Busch1894d8f2013-07-15 15:02:22 -06001318 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1319 NVME_CSTS_SHST_CMPLT) {
1320 msleep(100);
1321 if (fatal_signal_pending(current))
1322 return -EINTR;
1323 if (time_after(jiffies, timeout)) {
1324 dev_err(&dev->pci_dev->dev,
1325 "Device shutdown incomplete; abort shutdown\n");
1326 return -ENODEV;
1327 }
1328 }
1329
1330 return 0;
1331}
1332
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001333static struct blk_mq_ops nvme_mq_admin_ops = {
1334 .queue_rq = nvme_admin_queue_rq,
1335 .map_queue = blk_mq_map_queue,
1336 .init_hctx = nvme_admin_init_hctx,
Jens Axboe2c30540b2014-11-14 09:47:32 -07001337 .exit_hctx = nvme_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001338 .init_request = nvme_admin_init_request,
1339 .timeout = nvme_timeout,
1340};
1341
1342static struct blk_mq_ops nvme_mq_ops = {
1343 .queue_rq = nvme_queue_rq,
1344 .map_queue = blk_mq_map_queue,
1345 .init_hctx = nvme_init_hctx,
Jens Axboe2c30540b2014-11-14 09:47:32 -07001346 .exit_hctx = nvme_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001347 .init_request = nvme_init_request,
1348 .timeout = nvme_timeout,
1349};
1350
1351static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1352{
1353 if (!dev->admin_q) {
1354 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1355 dev->admin_tagset.nr_hw_queues = 1;
1356 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1;
1357 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
1358 dev->admin_tagset.numa_node = dev_to_node(&dev->pci_dev->dev);
1359 dev->admin_tagset.cmd_size = sizeof(struct nvme_cmd_info);
1360 dev->admin_tagset.driver_data = dev;
1361
1362 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1363 return -ENOMEM;
1364
1365 dev->admin_q = blk_mq_init_queue(&dev->admin_tagset);
1366 if (!dev->admin_q) {
1367 blk_mq_free_tag_set(&dev->admin_tagset);
1368 return -ENOMEM;
1369 }
1370 }
1371
1372 return 0;
1373}
1374
1375static void nvme_free_admin_tags(struct nvme_dev *dev)
1376{
1377 if (dev->admin_q)
1378 blk_mq_free_tag_set(&dev->admin_tagset);
1379}
1380
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001381static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001382{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001383 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001384 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001385 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001386 struct nvme_queue *nvmeq;
Keith Busch1d090622014-06-23 11:34:01 -06001387 unsigned page_shift = PAGE_SHIFT;
1388 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12;
1389 unsigned dev_page_max = NVME_CAP_MPSMAX(cap) + 12;
1390
1391 if (page_shift < dev_page_min) {
1392 dev_err(&dev->pci_dev->dev,
1393 "Minimum device page size (%u) too large for "
1394 "host (%u)\n", 1 << dev_page_min,
1395 1 << page_shift);
1396 return -ENODEV;
1397 }
1398 if (page_shift > dev_page_max) {
1399 dev_info(&dev->pci_dev->dev,
1400 "Device maximum page size (%u) smaller than "
1401 "host (%u); enabling work-around\n",
1402 1 << dev_page_max, 1 << page_shift);
1403 page_shift = dev_page_max;
1404 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001405
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001406 result = nvme_disable_ctrl(dev, cap);
1407 if (result < 0)
1408 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001409
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001410 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001411 if (!nvmeq) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001412 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH, 0);
Keith Buschcd638942013-07-15 15:02:23 -06001413 if (!nvmeq)
1414 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001415 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001416
1417 aqa = nvmeq->q_depth - 1;
1418 aqa |= aqa << 16;
1419
Keith Busch1d090622014-06-23 11:34:01 -06001420 dev->page_size = 1 << page_shift;
1421
Dan McLeran01079522014-06-23 08:24:36 -06001422 dev->ctrl_config = NVME_CC_CSS_NVM;
Keith Busch1d090622014-06-23 11:34:01 -06001423 dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001424 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001425 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001426
1427 writel(aqa, &dev->bar->aqa);
1428 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1429 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001430
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001431 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001432 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001433 goto free_nvmeq;
1434
1435 result = nvme_alloc_admin_tags(dev);
1436 if (result)
1437 goto free_nvmeq;
Matthew Wilcox9e866772012-08-03 13:55:56 -04001438
Matthew Wilcox3193f072014-01-27 15:57:22 -05001439 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Keith Busch025c5572013-05-01 13:07:51 -06001440 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001441 goto free_tags;
Keith Busch025c5572013-05-01 13:07:51 -06001442
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001443 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001444
1445 free_tags:
1446 nvme_free_admin_tags(dev);
1447 free_nvmeq:
1448 nvme_free_queues(dev, 0);
1449 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001450}
1451
Vishal Verma5d0f6132013-03-04 18:40:58 -07001452struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001453 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001454{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001455 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001456 struct scatterlist *sg;
1457 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001458 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001459
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001460 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001461 return ERR_PTR(-EINVAL);
Dan Carpenter5460fc02013-05-13 17:59:50 +03001462 if (!length || length > INT_MAX - PAGE_SIZE)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001463 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001464
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001465 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001466 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1467 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001468 if (!pages)
1469 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001470
1471 err = get_user_pages_fast(addr, count, 1, pages);
1472 if (err < count) {
1473 count = err;
1474 err = -EFAULT;
1475 goto put_pages;
1476 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001477
Santosh Y6808c5f2014-05-29 10:01:52 +05301478 err = -ENOMEM;
Keith Busch1d090622014-06-23 11:34:01 -06001479 iod = nvme_alloc_iod(count, length, dev, GFP_KERNEL);
Santosh Y6808c5f2014-05-29 10:01:52 +05301480 if (!iod)
1481 goto put_pages;
1482
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001483 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001484 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001485 for (i = 0; i < count; i++) {
1486 sg_set_page(&sg[i], pages[i],
Dan Carpenter5460fc02013-05-13 17:59:50 +03001487 min_t(unsigned, length, PAGE_SIZE - offset),
1488 offset);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001489 length -= (PAGE_SIZE - offset);
1490 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001491 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001492 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001493 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001494
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001495 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1496 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001497 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001498 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001499
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001500 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001501 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001502
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001503 free_iod:
1504 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001505 put_pages:
1506 for (i = 0; i < count; i++)
1507 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001508 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001509 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001510}
1511
Vishal Verma5d0f6132013-03-04 18:40:58 -07001512void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001513 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001514{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001515 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001516
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001517 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1518 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001519
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001520 for (i = 0; i < iod->nents; i++)
1521 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001522}
1523
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001524static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1525{
1526 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001527 struct nvme_user_io io;
1528 struct nvme_command c;
Keith Buschf410c682013-04-23 17:23:59 -06001529 unsigned length, meta_len;
1530 int status, i;
1531 struct nvme_iod *iod, *meta_iod = NULL;
1532 dma_addr_t meta_dma_addr;
1533 void *meta, *uninitialized_var(meta_mem);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001534
1535 if (copy_from_user(&io, uio, sizeof(io)))
1536 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001537 length = (io.nblocks + 1) << ns->lba_shift;
Keith Buschf410c682013-04-23 17:23:59 -06001538 meta_len = (io.nblocks + 1) * ns->ms;
1539
1540 if (meta_len && ((io.metadata & 3) || !io.metadata))
1541 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001542
1543 switch (io.opcode) {
1544 case nvme_cmd_write:
1545 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001546 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001547 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001548 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001549 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001550 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001551 }
1552
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001553 if (IS_ERR(iod))
1554 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001555
1556 memset(&c, 0, sizeof(c));
1557 c.rw.opcode = io.opcode;
1558 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001559 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001560 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001561 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001562 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001563 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1564 c.rw.reftag = cpu_to_le32(io.reftag);
1565 c.rw.apptag = cpu_to_le16(io.apptag);
1566 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buschf410c682013-04-23 17:23:59 -06001567
1568 if (meta_len) {
Keith Busch1b567492013-07-18 12:13:51 -06001569 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
1570 meta_len);
Keith Buschf410c682013-04-23 17:23:59 -06001571 if (IS_ERR(meta_iod)) {
1572 status = PTR_ERR(meta_iod);
1573 meta_iod = NULL;
1574 goto unmap;
1575 }
1576
1577 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1578 &meta_dma_addr, GFP_KERNEL);
1579 if (!meta_mem) {
1580 status = -ENOMEM;
1581 goto unmap;
1582 }
1583
1584 if (io.opcode & 1) {
1585 int meta_offset = 0;
1586
1587 for (i = 0; i < meta_iod->nents; i++) {
1588 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1589 meta_iod->sg[i].offset;
1590 memcpy(meta_mem + meta_offset, meta,
1591 meta_iod->sg[i].length);
1592 kunmap_atomic(meta);
1593 meta_offset += meta_iod->sg[i].length;
1594 }
1595 }
1596
1597 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1598 }
1599
Keith Buschedd10d32014-04-03 16:45:23 -06001600 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1601 c.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1602 c.rw.prp2 = cpu_to_le64(iod->first_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001603
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001604 if (length != (io.nblocks + 1) << ns->lba_shift)
1605 status = -ENOMEM;
1606 else
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001607 status = nvme_submit_io_cmd(dev, ns, &c, NULL);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001608
Keith Buschf410c682013-04-23 17:23:59 -06001609 if (meta_len) {
1610 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1611 int meta_offset = 0;
1612
1613 for (i = 0; i < meta_iod->nents; i++) {
1614 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1615 meta_iod->sg[i].offset;
1616 memcpy(meta, meta_mem + meta_offset,
1617 meta_iod->sg[i].length);
1618 kunmap_atomic(meta);
1619 meta_offset += meta_iod->sg[i].length;
1620 }
1621 }
1622
1623 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1624 meta_dma_addr);
1625 }
1626
1627 unmap:
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001628 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001629 nvme_free_iod(dev, iod);
Keith Buschf410c682013-04-23 17:23:59 -06001630
1631 if (meta_iod) {
1632 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1633 nvme_free_iod(dev, meta_iod);
1634 }
1635
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001636 return status;
1637}
1638
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001639static int nvme_user_cmd(struct nvme_dev *dev, struct nvme_ns *ns,
1640 struct nvme_passthru_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001641{
Keith Busch7963e522014-09-12 16:07:20 -06001642 struct nvme_passthru_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001643 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001644 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001645 struct nvme_iod *uninitialized_var(iod);
Keith Busch94f370c2013-05-09 14:01:38 -06001646 unsigned timeout;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001647
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001648 if (!capable(CAP_SYS_ADMIN))
1649 return -EACCES;
1650 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001651 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001652
1653 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001654 c.common.opcode = cmd.opcode;
1655 c.common.flags = cmd.flags;
1656 c.common.nsid = cpu_to_le32(cmd.nsid);
1657 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1658 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1659 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1660 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1661 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1662 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1663 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1664 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1665
1666 length = cmd.data_len;
1667 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001668 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1669 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001670 if (IS_ERR(iod))
1671 return PTR_ERR(iod);
Keith Buschedd10d32014-04-03 16:45:23 -06001672 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1673 c.common.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1674 c.common.prp2 = cpu_to_le64(iod->first_dma);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001675 }
1676
Keith Busch94f370c2013-05-09 14:01:38 -06001677 timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
1678 ADMIN_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001679
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001680 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001681 status = -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001682 else if (ns) {
1683 struct request *req;
1684
1685 req = blk_mq_alloc_request(ns->queue, WRITE,
1686 (GFP_KERNEL|__GFP_WAIT), false);
Jens Axboe97fe3832014-12-10 13:02:44 -07001687 if (IS_ERR(req))
1688 status = PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001689 else {
1690 status = nvme_submit_sync_cmd(req, &c, &cmd.result,
1691 timeout);
Jens Axboe9d135bb2014-11-17 10:43:42 -07001692 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001693 }
1694 } else
1695 status = __nvme_submit_admin_cmd(dev, &c, &cmd.result, timeout);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001696
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001697 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001698 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001699 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001700 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001701
Chayan Biswascf90bc42013-05-22 22:34:49 +00001702 if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
Keith Buschf4f117f2012-09-21 10:49:05 -06001703 sizeof(cmd.result)))
1704 status = -EFAULT;
1705
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001706 return status;
1707}
1708
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001709static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1710 unsigned long arg)
1711{
1712 struct nvme_ns *ns = bdev->bd_disk->private_data;
1713
1714 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001715 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001716 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001717 return ns->ns_id;
1718 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001719 return nvme_user_cmd(ns->dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06001720 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001721 return nvme_user_cmd(ns->dev, ns, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001722 case NVME_IOCTL_SUBMIT_IO:
1723 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001724 case SG_GET_VERSION_NUM:
1725 return nvme_sg_get_version_num((void __user *)arg);
1726 case SG_IO:
1727 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001728 default:
1729 return -ENOTTY;
1730 }
1731}
1732
Keith Busch320a3822013-10-23 13:07:34 -06001733#ifdef CONFIG_COMPAT
1734static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1735 unsigned int cmd, unsigned long arg)
1736{
Keith Busch320a3822013-10-23 13:07:34 -06001737 switch (cmd) {
1738 case SG_IO:
Keith Busche1797292014-08-27 13:55:38 -06001739 return -ENOIOCTLCMD;
Keith Busch320a3822013-10-23 13:07:34 -06001740 }
1741 return nvme_ioctl(bdev, mode, cmd, arg);
1742}
1743#else
1744#define nvme_compat_ioctl NULL
1745#endif
1746
Keith Busch9ac27092014-01-31 16:53:39 -07001747static int nvme_open(struct block_device *bdev, fmode_t mode)
1748{
Keith Busch9e603522014-10-03 11:15:47 -06001749 int ret = 0;
1750 struct nvme_ns *ns;
Keith Busch9ac27092014-01-31 16:53:39 -07001751
Keith Busch9e603522014-10-03 11:15:47 -06001752 spin_lock(&dev_list_lock);
1753 ns = bdev->bd_disk->private_data;
1754 if (!ns)
1755 ret = -ENXIO;
1756 else if (!kref_get_unless_zero(&ns->dev->kref))
1757 ret = -ENXIO;
1758 spin_unlock(&dev_list_lock);
1759
1760 return ret;
Keith Busch9ac27092014-01-31 16:53:39 -07001761}
1762
1763static void nvme_free_dev(struct kref *kref);
1764
1765static void nvme_release(struct gendisk *disk, fmode_t mode)
1766{
1767 struct nvme_ns *ns = disk->private_data;
1768 struct nvme_dev *dev = ns->dev;
1769
1770 kref_put(&dev->kref, nvme_free_dev);
1771}
1772
Keith Busch4cc09e22014-04-02 15:45:37 -06001773static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
1774{
1775 /* some standard values */
1776 geo->heads = 1 << 6;
1777 geo->sectors = 1 << 5;
1778 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1779 return 0;
1780}
1781
Keith Busch1b9dbf72014-09-10 17:21:14 -06001782static int nvme_revalidate_disk(struct gendisk *disk)
1783{
1784 struct nvme_ns *ns = disk->private_data;
1785 struct nvme_dev *dev = ns->dev;
1786 struct nvme_id_ns *id;
1787 dma_addr_t dma_addr;
1788 int lbaf;
1789
1790 id = dma_alloc_coherent(&dev->pci_dev->dev, 4096, &dma_addr,
1791 GFP_KERNEL);
1792 if (!id) {
1793 dev_warn(&dev->pci_dev->dev, "%s: Memory alocation failure\n",
1794 __func__);
1795 return 0;
1796 }
1797
1798 if (nvme_identify(dev, ns->ns_id, 0, dma_addr))
1799 goto free;
1800
1801 lbaf = id->flbas & 0xf;
1802 ns->lba_shift = id->lbaf[lbaf].ds;
1803
1804 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
1805 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1806 free:
1807 dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr);
1808 return 0;
1809}
1810
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001811static const struct block_device_operations nvme_fops = {
1812 .owner = THIS_MODULE,
1813 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06001814 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07001815 .open = nvme_open,
1816 .release = nvme_release,
Keith Busch4cc09e22014-04-02 15:45:37 -06001817 .getgeo = nvme_getgeo,
Keith Busch1b9dbf72014-09-10 17:21:14 -06001818 .revalidate_disk= nvme_revalidate_disk,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001819};
1820
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001821static int nvme_kthread(void *data)
1822{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001823 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001824
1825 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001826 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001827 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001828 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001829 int i;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001830 if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
1831 dev->initialized) {
1832 if (work_busy(&dev->reset_work))
1833 continue;
1834 list_del_init(&dev->node);
1835 dev_warn(&dev->pci_dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001836 "Failed status: %x, reset controller\n",
1837 readl(&dev->bar->csts));
Tejun Heo9ca97372014-03-07 10:24:49 -05001838 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001839 queue_work(nvme_workq, &dev->reset_work);
1840 continue;
1841 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001842 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001843 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001844 if (!nvmeq)
1845 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001846 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001847 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06001848
1849 while ((i == 0) && (dev->event_limit > 0)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001850 if (nvme_submit_async_admin_req(dev))
Keith Busch6fccf932014-06-18 13:58:57 -06001851 break;
1852 dev->event_limit--;
1853 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001854 spin_unlock_irq(&nvmeq->q_lock);
1855 }
1856 }
1857 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001858 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001859 }
1860 return 0;
1861}
1862
Keith Busch0e5e4f02012-11-09 16:33:05 -07001863static void nvme_config_discard(struct nvme_ns *ns)
1864{
1865 u32 logical_block_size = queue_logical_block_size(ns->queue);
1866 ns->queue->limits.discard_zeroes_data = 0;
1867 ns->queue->limits.discard_alignment = logical_block_size;
1868 ns->queue->limits.discard_granularity = logical_block_size;
1869 ns->queue->limits.max_discard_sectors = 0xffffffff;
1870 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1871}
1872
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001873static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001874 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1875{
1876 struct nvme_ns *ns;
1877 struct gendisk *disk;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001878 int node = dev_to_node(&dev->pci_dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001879 int lbaf;
1880
1881 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1882 return NULL;
1883
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001884 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001885 if (!ns)
1886 return NULL;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001887 ns->queue = blk_mq_init_queue(&dev->tagset);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001888 if (IS_ERR(ns->queue))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001889 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001890 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1891 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001892 queue_flag_set_unlocked(QUEUE_FLAG_SG_GAPS, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001893 ns->dev = dev;
1894 ns->queue->queuedata = ns;
1895
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001896 disk = alloc_disk_node(0, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001897 if (!disk)
1898 goto out_free_queue;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001899
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001900 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001901 ns->disk = disk;
1902 lbaf = id->flbas & 0xf;
1903 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Buschf410c682013-04-23 17:23:59 -06001904 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busche9ef4632012-07-24 15:01:04 -06001905 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06001906 if (dev->max_hw_sectors)
1907 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001908 if (dev->stripe_size)
1909 blk_queue_chunk_sectors(ns->queue, dev->stripe_size >> 9);
Keith Buscha7d2ce22014-04-29 11:41:28 -06001910 if (dev->vwc & NVME_CTRL_VWC_PRESENT)
1911 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001912
1913 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001914 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001915 disk->fops = &nvme_fops;
1916 disk->private_data = ns;
1917 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05001918 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001919 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001920 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001921 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1922
Keith Busch0e5e4f02012-11-09 16:33:05 -07001923 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1924 nvme_config_discard(ns);
1925
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001926 return ns;
1927
1928 out_free_queue:
1929 blk_cleanup_queue(ns->queue);
1930 out_free_ns:
1931 kfree(ns);
1932 return NULL;
1933}
1934
Keith Busch42f61422014-03-24 10:46:25 -06001935static void nvme_create_io_queues(struct nvme_dev *dev)
1936{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001937 unsigned i;
Keith Busch42f61422014-03-24 10:46:25 -06001938
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001939 for (i = dev->queue_count; i <= dev->max_qid; i++)
Keith Busch42f61422014-03-24 10:46:25 -06001940 if (!nvme_alloc_queue(dev, i, dev->q_depth, i - 1))
1941 break;
1942
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001943 for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
1944 if (nvme_create_queue(dev->queues[i], i))
Keith Busch42f61422014-03-24 10:46:25 -06001945 break;
1946}
1947
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001948static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001949{
1950 int status;
1951 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001952 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001953
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001954 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001955 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04001956 if (status < 0)
1957 return status;
1958 if (status > 0) {
1959 dev_err(&dev->pci_dev->dev, "Could not set queue count (%d)\n",
1960 status);
Keith Buschbadc34d2014-06-23 14:25:35 -06001961 return 0;
Matthew Wilcox27e81662014-04-11 11:58:45 -04001962 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001963 return min(result & 0xffff, result >> 16) + 1;
1964}
1965
Keith Busch9d713c22013-07-15 15:02:24 -06001966static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1967{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001968 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06001969}
1970
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001971static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001972{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001973 struct nvme_queue *adminq = dev->queues[0];
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001974 struct pci_dev *pdev = dev->pci_dev;
Keith Busch42f61422014-03-24 10:46:25 -06001975 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001976
Keith Busch42f61422014-03-24 10:46:25 -06001977 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001978 result = set_queue_count(dev, nr_io_queues);
Keith Buschbadc34d2014-06-23 14:25:35 -06001979 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001980 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001981 if (result < nr_io_queues)
1982 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001983
Keith Busch9d713c22013-07-15 15:02:24 -06001984 size = db_bar_size(dev, nr_io_queues);
1985 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001986 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06001987 do {
1988 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1989 if (dev->bar)
1990 break;
1991 if (!--nr_io_queues)
1992 return -ENOMEM;
1993 size = db_bar_size(dev, nr_io_queues);
1994 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001995 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07001996 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001997 }
1998
Keith Busch9d713c22013-07-15 15:02:24 -06001999 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002000 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002001
Jens Axboee32efbf2014-11-14 09:49:26 -07002002 /*
2003 * If we enable msix early due to not intx, disable it again before
2004 * setting up the full range we need.
2005 */
2006 if (!pdev->irq)
2007 pci_disable_msix(pdev);
2008
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002009 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002010 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002011 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
2012 if (vecs < 0) {
2013 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
2014 if (vecs < 0) {
2015 vecs = 1;
2016 } else {
2017 for (i = 0; i < vecs; i++)
2018 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002019 }
2020 }
2021
Matthew Wilcox063a8092013-06-20 10:53:48 -04002022 /*
2023 * Should investigate if there's a performance win from allocating
2024 * more queues than interrupt vectors; it might allow the submission
2025 * path to scale better, even if the receive path is limited by the
2026 * number of interrupts.
2027 */
2028 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002029 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002030
Matthew Wilcox3193f072014-01-27 15:57:22 -05002031 result = queue_request_irq(dev, adminq, adminq->irqname);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002032 if (result)
Keith Busch22404272013-07-15 15:02:20 -06002033 goto free_queues;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002034
Keith Buschcd638942013-07-15 15:02:23 -06002035 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002036 nvme_free_queues(dev, nr_io_queues + 1);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002037 nvme_create_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06002038
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002039 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002040
Keith Busch22404272013-07-15 15:02:20 -06002041 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002042 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002043 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002044}
2045
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002046/*
2047 * Return: error value if an error occurred setting up the queues or calling
2048 * Identify Device. 0 if these succeeded, even if adding some of the
2049 * namespaces failed. At the moment, these failures are silent. TBD which
2050 * failures should be reported.
2051 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002052static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002053{
Matthew Wilcox68608c262013-06-21 14:36:34 -04002054 struct pci_dev *pdev = dev->pci_dev;
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002055 int res;
2056 unsigned nn, i;
Keith Buschcbb62182013-05-01 13:07:49 -06002057 struct nvme_ns *ns;
Matthew Wilcox51814232011-02-01 16:18:08 -05002058 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002059 struct nvme_id_ns *id_ns;
2060 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002061 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06002062 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002063
Matthew Wilcox68608c262013-06-21 14:36:34 -04002064 mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06002065 if (!mem)
2066 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002067
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002068 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002069 if (res) {
Matthew Wilcox27e81662014-04-11 11:58:45 -04002070 dev_err(&pdev->dev, "Identify Controller failed (%d)\n", res);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002071 res = -EIO;
Keith Buschcbb62182013-05-01 13:07:49 -06002072 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002073 }
2074
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002075 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05002076 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002077 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002078 dev->abort_limit = ctrl->acl + 1;
Keith Buscha7d2ce22014-04-29 11:41:28 -06002079 dev->vwc = ctrl->vwc;
Keith Busch6fccf932014-06-18 13:58:57 -06002080 dev->event_limit = min(ctrl->aerl + 1, 8);
Matthew Wilcox51814232011-02-01 16:18:08 -05002081 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2082 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2083 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002084 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002085 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002086 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002087 (pdev->device == 0x0953) && ctrl->vs[3]) {
2088 unsigned int max_hw_sectors;
2089
Keith Busch159b67d2013-04-09 17:13:20 -06002090 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002091 max_hw_sectors = dev->stripe_size >> (shift - 9);
2092 if (dev->max_hw_sectors) {
2093 dev->max_hw_sectors = min(max_hw_sectors,
2094 dev->max_hw_sectors);
2095 } else
2096 dev->max_hw_sectors = max_hw_sectors;
2097 }
2098
2099 dev->tagset.ops = &nvme_mq_ops;
2100 dev->tagset.nr_hw_queues = dev->online_queues - 1;
2101 dev->tagset.timeout = NVME_IO_TIMEOUT;
2102 dev->tagset.numa_node = dev_to_node(&dev->pci_dev->dev);
2103 dev->tagset.queue_depth =
2104 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
2105 dev->tagset.cmd_size = sizeof(struct nvme_cmd_info);
2106 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
2107 dev->tagset.driver_data = dev;
2108
2109 if (blk_mq_alloc_tag_set(&dev->tagset))
2110 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002111
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002112 id_ns = mem;
Matthew Wilcox2b2c1892011-10-07 13:10:13 -04002113 for (i = 1; i <= nn; i++) {
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002114 res = nvme_identify(dev, i, 0, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002115 if (res)
2116 continue;
2117
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002118 if (id_ns->ncap == 0)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002119 continue;
2120
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002121 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
Keith Busch08df1e02012-09-21 10:52:13 -06002122 dma_addr + 4096, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002123 if (res)
Keith Busch12209032013-01-31 14:40:38 -07002124 memset(mem + 4096, 0, 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002125
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002126 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002127 if (ns)
2128 list_add_tail(&ns->list, &dev->namespaces);
2129 }
2130 list_for_each_entry(ns, &dev->namespaces, list)
2131 add_disk(ns->disk);
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002132 res = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002133
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002134 out:
Matthew Wilcox684f5c22011-09-19 17:14:53 -04002135 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002136 return res;
2137}
2138
Keith Busch0877cb02013-07-15 15:02:19 -06002139static int nvme_dev_map(struct nvme_dev *dev)
2140{
Keith Busch42f61422014-03-24 10:46:25 -06002141 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002142 int bars, result = -ENOMEM;
2143 struct pci_dev *pdev = dev->pci_dev;
2144
2145 if (pci_enable_device_mem(pdev))
2146 return result;
2147
2148 dev->entry[0].vector = pdev->irq;
2149 pci_set_master(pdev);
2150 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07002151 if (!bars)
2152 goto disable_pci;
2153
Keith Busch0877cb02013-07-15 15:02:19 -06002154 if (pci_request_selected_regions(pdev, bars, "nvme"))
2155 goto disable_pci;
2156
Russell King052d0ef2013-06-26 23:49:11 +01002157 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
2158 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2159 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002160
Keith Busch0877cb02013-07-15 15:02:19 -06002161 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2162 if (!dev->bar)
2163 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07002164
Keith Busch0e53d182013-12-10 13:10:39 -07002165 if (readl(&dev->bar->csts) == -1) {
2166 result = -ENODEV;
2167 goto unmap;
2168 }
Jens Axboee32efbf2014-11-14 09:49:26 -07002169
2170 /*
2171 * Some devices don't advertse INTx interrupts, pre-enable a single
2172 * MSIX vec for setup. We'll adjust this later.
2173 */
2174 if (!pdev->irq) {
2175 result = pci_enable_msix(pdev, dev->entry, 1);
2176 if (result < 0)
2177 goto unmap;
2178 }
2179
Keith Busch42f61422014-03-24 10:46:25 -06002180 cap = readq(&dev->bar->cap);
2181 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2182 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Keith Busch0877cb02013-07-15 15:02:19 -06002183 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2184
2185 return 0;
2186
Keith Busch0e53d182013-12-10 13:10:39 -07002187 unmap:
2188 iounmap(dev->bar);
2189 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002190 disable:
2191 pci_release_regions(pdev);
2192 disable_pci:
2193 pci_disable_device(pdev);
2194 return result;
2195}
2196
2197static void nvme_dev_unmap(struct nvme_dev *dev)
2198{
2199 if (dev->pci_dev->msi_enabled)
2200 pci_disable_msi(dev->pci_dev);
2201 else if (dev->pci_dev->msix_enabled)
2202 pci_disable_msix(dev->pci_dev);
2203
2204 if (dev->bar) {
2205 iounmap(dev->bar);
2206 dev->bar = NULL;
Keith Busch9a6b9452013-12-10 13:10:36 -07002207 pci_release_regions(dev->pci_dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002208 }
2209
Keith Busch0877cb02013-07-15 15:02:19 -06002210 if (pci_is_enabled(dev->pci_dev))
2211 pci_disable_device(dev->pci_dev);
2212}
2213
Keith Busch4d115422013-12-10 13:10:40 -07002214struct nvme_delq_ctx {
2215 struct task_struct *waiter;
2216 struct kthread_worker *worker;
2217 atomic_t refcount;
2218};
2219
2220static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2221{
2222 dq->waiter = current;
2223 mb();
2224
2225 for (;;) {
2226 set_current_state(TASK_KILLABLE);
2227 if (!atomic_read(&dq->refcount))
2228 break;
2229 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2230 fatal_signal_pending(current)) {
2231 set_current_state(TASK_RUNNING);
2232
2233 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
2234 nvme_disable_queue(dev, 0);
2235
2236 send_sig(SIGKILL, dq->worker->task, 1);
2237 flush_kthread_worker(dq->worker);
2238 return;
2239 }
2240 }
2241 set_current_state(TASK_RUNNING);
2242}
2243
2244static void nvme_put_dq(struct nvme_delq_ctx *dq)
2245{
2246 atomic_dec(&dq->refcount);
2247 if (dq->waiter)
2248 wake_up_process(dq->waiter);
2249}
2250
2251static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2252{
2253 atomic_inc(&dq->refcount);
2254 return dq;
2255}
2256
2257static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2258{
2259 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
2260
2261 nvme_clear_queue(nvmeq);
2262 nvme_put_dq(dq);
2263}
2264
2265static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2266 kthread_work_func_t fn)
2267{
2268 struct nvme_command c;
2269
2270 memset(&c, 0, sizeof(c));
2271 c.delete_queue.opcode = opcode;
2272 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2273
2274 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002275 return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
2276 ADMIN_TIMEOUT);
Keith Busch4d115422013-12-10 13:10:40 -07002277}
2278
2279static void nvme_del_cq_work_handler(struct kthread_work *work)
2280{
2281 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2282 cmdinfo.work);
2283 nvme_del_queue_end(nvmeq);
2284}
2285
2286static int nvme_delete_cq(struct nvme_queue *nvmeq)
2287{
2288 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2289 nvme_del_cq_work_handler);
2290}
2291
2292static void nvme_del_sq_work_handler(struct kthread_work *work)
2293{
2294 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2295 cmdinfo.work);
2296 int status = nvmeq->cmdinfo.status;
2297
2298 if (!status)
2299 status = nvme_delete_cq(nvmeq);
2300 if (status)
2301 nvme_del_queue_end(nvmeq);
2302}
2303
2304static int nvme_delete_sq(struct nvme_queue *nvmeq)
2305{
2306 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2307 nvme_del_sq_work_handler);
2308}
2309
2310static void nvme_del_queue_start(struct kthread_work *work)
2311{
2312 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2313 cmdinfo.work);
2314 allow_signal(SIGKILL);
2315 if (nvme_delete_sq(nvmeq))
2316 nvme_del_queue_end(nvmeq);
2317}
2318
2319static void nvme_disable_io_queues(struct nvme_dev *dev)
2320{
2321 int i;
2322 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2323 struct nvme_delq_ctx dq;
2324 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2325 &worker, "nvme%d", dev->instance);
2326
2327 if (IS_ERR(kworker_task)) {
2328 dev_err(&dev->pci_dev->dev,
2329 "Failed to create queue del task\n");
2330 for (i = dev->queue_count - 1; i > 0; i--)
2331 nvme_disable_queue(dev, i);
2332 return;
2333 }
2334
2335 dq.waiter = NULL;
2336 atomic_set(&dq.refcount, 0);
2337 dq.worker = &worker;
2338 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002339 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002340
2341 if (nvme_suspend_queue(nvmeq))
2342 continue;
2343 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2344 nvmeq->cmdinfo.worker = dq.worker;
2345 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2346 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2347 }
2348 nvme_wait_dq(&dq, dev);
2349 kthread_stop(kworker_task);
2350}
2351
Dan McLeranb9afca32014-04-07 17:10:11 -06002352/*
2353* Remove the node from the device list and check
2354* for whether or not we need to stop the nvme_thread.
2355*/
2356static void nvme_dev_list_remove(struct nvme_dev *dev)
2357{
2358 struct task_struct *tmp = NULL;
2359
2360 spin_lock(&dev_list_lock);
2361 list_del_init(&dev->node);
2362 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2363 tmp = nvme_thread;
2364 nvme_thread = NULL;
2365 }
2366 spin_unlock(&dev_list_lock);
2367
2368 if (tmp)
2369 kthread_stop(tmp);
2370}
2371
Keith Buschf0b50732013-07-15 15:02:21 -06002372static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002373{
Keith Busch22404272013-07-15 15:02:20 -06002374 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06002375 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06002376
Keith Buschd4b4ff82013-12-10 13:10:37 -07002377 dev->initialized = 0;
Dan McLeranb9afca32014-04-07 17:10:11 -06002378 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002379
Keith Busch7c1b2452014-06-25 11:18:12 -06002380 if (dev->bar)
2381 csts = readl(&dev->bar->csts);
2382 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07002383 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002384 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002385 nvme_suspend_queue(nvmeq);
2386 nvme_clear_queue(nvmeq);
2387 }
2388 } else {
2389 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002390 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002391 nvme_disable_queue(dev, 0);
2392 }
Keith Buschf0b50732013-07-15 15:02:21 -06002393 nvme_dev_unmap(dev);
2394}
2395
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002396static void nvme_dev_remove_admin(struct nvme_dev *dev)
2397{
2398 if (dev->admin_q && !blk_queue_dying(dev->admin_q))
2399 blk_cleanup_queue(dev->admin_q);
2400}
2401
Keith Buschf0b50732013-07-15 15:02:21 -06002402static void nvme_dev_remove(struct nvme_dev *dev)
2403{
Keith Busch9ac27092014-01-31 16:53:39 -07002404 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002405
Keith Busch9ac27092014-01-31 16:53:39 -07002406 list_for_each_entry(ns, &dev->namespaces, list) {
2407 if (ns->disk->flags & GENHD_FL_UP)
2408 del_gendisk(ns->disk);
2409 if (!blk_queue_dying(ns->queue))
2410 blk_cleanup_queue(ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002411 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002412}
2413
Matthew Wilcox091b6092011-02-10 09:56:01 -05002414static int nvme_setup_prp_pools(struct nvme_dev *dev)
2415{
2416 struct device *dmadev = &dev->pci_dev->dev;
2417 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2418 PAGE_SIZE, PAGE_SIZE, 0);
2419 if (!dev->prp_page_pool)
2420 return -ENOMEM;
2421
Matthew Wilcox99802a72011-02-10 10:30:34 -05002422 /* Optimisation for I/Os between 4k and 128k */
2423 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2424 256, 256, 0);
2425 if (!dev->prp_small_pool) {
2426 dma_pool_destroy(dev->prp_page_pool);
2427 return -ENOMEM;
2428 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002429 return 0;
2430}
2431
2432static void nvme_release_prp_pools(struct nvme_dev *dev)
2433{
2434 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002435 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002436}
2437
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002438static DEFINE_IDA(nvme_instance_ida);
2439
2440static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002441{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002442 int instance, error;
2443
2444 do {
2445 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2446 return -ENODEV;
2447
2448 spin_lock(&dev_list_lock);
2449 error = ida_get_new(&nvme_instance_ida, &instance);
2450 spin_unlock(&dev_list_lock);
2451 } while (error == -EAGAIN);
2452
2453 if (error)
2454 return -ENODEV;
2455
2456 dev->instance = instance;
2457 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002458}
2459
2460static void nvme_release_instance(struct nvme_dev *dev)
2461{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002462 spin_lock(&dev_list_lock);
2463 ida_remove(&nvme_instance_ida, dev->instance);
2464 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002465}
2466
Keith Busch9ac27092014-01-31 16:53:39 -07002467static void nvme_free_namespaces(struct nvme_dev *dev)
2468{
2469 struct nvme_ns *ns, *next;
2470
2471 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2472 list_del(&ns->list);
Keith Busch9e603522014-10-03 11:15:47 -06002473
2474 spin_lock(&dev_list_lock);
2475 ns->disk->private_data = NULL;
2476 spin_unlock(&dev_list_lock);
2477
Keith Busch9ac27092014-01-31 16:53:39 -07002478 put_disk(ns->disk);
2479 kfree(ns);
2480 }
2481}
2482
Keith Busch5e82e952013-02-19 10:17:58 -07002483static void nvme_free_dev(struct kref *kref)
2484{
2485 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002486
Keith Buscha96d4f52014-08-19 19:15:59 -06002487 pci_dev_put(dev->pci_dev);
Keith Busch9ac27092014-01-31 16:53:39 -07002488 nvme_free_namespaces(dev);
Indraneel M285dffc2014-12-11 08:24:18 -07002489 nvme_release_instance(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002490 blk_mq_free_tag_set(&dev->tagset);
Keith Busch5e82e952013-02-19 10:17:58 -07002491 kfree(dev->queues);
2492 kfree(dev->entry);
2493 kfree(dev);
2494}
2495
2496static int nvme_dev_open(struct inode *inode, struct file *f)
2497{
2498 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
2499 miscdev);
2500 kref_get(&dev->kref);
2501 f->private_data = dev;
2502 return 0;
2503}
2504
2505static int nvme_dev_release(struct inode *inode, struct file *f)
2506{
2507 struct nvme_dev *dev = f->private_data;
2508 kref_put(&dev->kref, nvme_free_dev);
2509 return 0;
2510}
2511
2512static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2513{
2514 struct nvme_dev *dev = f->private_data;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002515 struct nvme_ns *ns;
2516
Keith Busch5e82e952013-02-19 10:17:58 -07002517 switch (cmd) {
2518 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002519 return nvme_user_cmd(dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06002520 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002521 if (list_empty(&dev->namespaces))
2522 return -ENOTTY;
2523 ns = list_first_entry(&dev->namespaces, struct nvme_ns, list);
2524 return nvme_user_cmd(dev, ns, (void __user *)arg);
Keith Busch5e82e952013-02-19 10:17:58 -07002525 default:
2526 return -ENOTTY;
2527 }
2528}
2529
2530static const struct file_operations nvme_dev_fops = {
2531 .owner = THIS_MODULE,
2532 .open = nvme_dev_open,
2533 .release = nvme_dev_release,
2534 .unlocked_ioctl = nvme_dev_ioctl,
2535 .compat_ioctl = nvme_dev_ioctl,
2536};
2537
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002538static void nvme_set_irq_hints(struct nvme_dev *dev)
2539{
2540 struct nvme_queue *nvmeq;
2541 int i;
2542
2543 for (i = 0; i < dev->online_queues; i++) {
2544 nvmeq = dev->queues[i];
2545
2546 if (!nvmeq->hctx)
2547 continue;
2548
2549 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
2550 nvmeq->hctx->cpumask);
2551 }
2552}
2553
Keith Buschf0b50732013-07-15 15:02:21 -06002554static int nvme_dev_start(struct nvme_dev *dev)
2555{
2556 int result;
Dan McLeranb9afca32014-04-07 17:10:11 -06002557 bool start_thread = false;
Keith Buschf0b50732013-07-15 15:02:21 -06002558
2559 result = nvme_dev_map(dev);
2560 if (result)
2561 return result;
2562
2563 result = nvme_configure_admin_queue(dev);
2564 if (result)
2565 goto unmap;
2566
2567 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002568 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2569 start_thread = true;
2570 nvme_thread = NULL;
2571 }
Keith Buschf0b50732013-07-15 15:02:21 -06002572 list_add(&dev->node, &dev_list);
2573 spin_unlock(&dev_list_lock);
2574
Dan McLeranb9afca32014-04-07 17:10:11 -06002575 if (start_thread) {
2576 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
Keith Busch387caa52014-09-22 13:46:19 -06002577 wake_up_all(&nvme_kthread_wait);
Dan McLeranb9afca32014-04-07 17:10:11 -06002578 } else
2579 wait_event_killable(nvme_kthread_wait, nvme_thread);
2580
2581 if (IS_ERR_OR_NULL(nvme_thread)) {
2582 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2583 goto disable;
2584 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002585
2586 nvme_init_queue(dev->queues[0], 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06002587
Keith Buschf0b50732013-07-15 15:02:21 -06002588 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002589 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002590 goto disable;
2591
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002592 nvme_set_irq_hints(dev);
2593
Keith Buschd82e8bf2013-09-05 14:45:07 -06002594 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002595
2596 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002597 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06002598 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002599 unmap:
2600 nvme_dev_unmap(dev);
2601 return result;
2602}
2603
Keith Busch9a6b9452013-12-10 13:10:36 -07002604static int nvme_remove_dead_ctrl(void *arg)
2605{
2606 struct nvme_dev *dev = (struct nvme_dev *)arg;
2607 struct pci_dev *pdev = dev->pci_dev;
2608
2609 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06002610 pci_stop_and_remove_bus_device_locked(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002611 kref_put(&dev->kref, nvme_free_dev);
2612 return 0;
2613}
2614
2615static void nvme_remove_disks(struct work_struct *ws)
2616{
Keith Busch9a6b9452013-12-10 13:10:36 -07002617 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2618
Keith Busch5a92e702014-02-21 14:13:44 -07002619 nvme_free_queues(dev, 1);
Keith Busch302c6722014-07-18 11:40:20 -06002620 nvme_dev_remove(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002621}
2622
2623static int nvme_dev_resume(struct nvme_dev *dev)
2624{
2625 int ret;
2626
2627 ret = nvme_dev_start(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002628 if (ret)
Keith Busch9a6b9452013-12-10 13:10:36 -07002629 return ret;
Keith Buschbadc34d2014-06-23 14:25:35 -06002630 if (dev->online_queues < 2) {
Keith Busch9a6b9452013-12-10 13:10:36 -07002631 spin_lock(&dev_list_lock);
Tejun Heo9ca97372014-03-07 10:24:49 -05002632 dev->reset_workfn = nvme_remove_disks;
Keith Busch9a6b9452013-12-10 13:10:36 -07002633 queue_work(nvme_workq, &dev->reset_work);
2634 spin_unlock(&dev_list_lock);
2635 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002636 dev->initialized = 1;
Keith Busch9a6b9452013-12-10 13:10:36 -07002637 return 0;
2638}
2639
2640static void nvme_dev_reset(struct nvme_dev *dev)
2641{
2642 nvme_dev_shutdown(dev);
2643 if (nvme_dev_resume(dev)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002644 dev_warn(&dev->pci_dev->dev, "Device failed to resume\n");
Keith Busch9a6b9452013-12-10 13:10:36 -07002645 kref_get(&dev->kref);
2646 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2647 dev->instance))) {
2648 dev_err(&dev->pci_dev->dev,
2649 "Failed to start controller remove task\n");
2650 kref_put(&dev->kref, nvme_free_dev);
2651 }
2652 }
2653}
2654
2655static void nvme_reset_failed_dev(struct work_struct *ws)
2656{
2657 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2658 nvme_dev_reset(dev);
2659}
2660
Tejun Heo9ca97372014-03-07 10:24:49 -05002661static void nvme_reset_workfn(struct work_struct *work)
2662{
2663 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
2664 dev->reset_workfn(work);
2665}
2666
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002667static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002668{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002669 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002670 struct nvme_dev *dev;
2671
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002672 node = dev_to_node(&pdev->dev);
2673 if (node == NUMA_NO_NODE)
2674 set_dev_node(&pdev->dev, 0);
2675
2676 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002677 if (!dev)
2678 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002679 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
2680 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002681 if (!dev->entry)
2682 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002683 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2684 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002685 if (!dev->queues)
2686 goto free;
2687
2688 INIT_LIST_HEAD(&dev->namespaces);
Tejun Heo9ca97372014-03-07 10:24:49 -05002689 dev->reset_workfn = nvme_reset_failed_dev;
2690 INIT_WORK(&dev->reset_work, nvme_reset_workfn);
Keith Buscha96d4f52014-08-19 19:15:59 -06002691 dev->pci_dev = pci_dev_get(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002692 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002693 result = nvme_set_instance(dev);
2694 if (result)
Keith Buscha96d4f52014-08-19 19:15:59 -06002695 goto put_pci;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002696
Matthew Wilcox091b6092011-02-10 09:56:01 -05002697 result = nvme_setup_prp_pools(dev);
2698 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002699 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002700
Keith Buschfb35e912014-03-03 11:09:47 -07002701 kref_init(&dev->kref);
Keith Buschf0b50732013-07-15 15:02:21 -06002702 result = nvme_dev_start(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002703 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002704 goto release_pools;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002705
Keith Buschbadc34d2014-06-23 14:25:35 -06002706 if (dev->online_queues > 1)
2707 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002708 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002709 goto shutdown;
Matthew Wilcox740216f2011-02-15 16:28:20 -05002710
Keith Busch5e82e952013-02-19 10:17:58 -07002711 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
2712 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2713 dev->miscdev.parent = &pdev->dev;
2714 dev->miscdev.name = dev->name;
2715 dev->miscdev.fops = &nvme_dev_fops;
2716 result = misc_register(&dev->miscdev);
2717 if (result)
2718 goto remove;
2719
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002720 nvme_set_irq_hints(dev);
2721
Keith Buschd4b4ff82013-12-10 13:10:37 -07002722 dev->initialized = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002723 return 0;
2724
Keith Busch5e82e952013-02-19 10:17:58 -07002725 remove:
2726 nvme_dev_remove(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002727 nvme_dev_remove_admin(dev);
Keith Busch9ac27092014-01-31 16:53:39 -07002728 nvme_free_namespaces(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002729 shutdown:
2730 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002731 release_pools:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002732 nvme_free_queues(dev, 0);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002733 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002734 release:
2735 nvme_release_instance(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002736 put_pci:
2737 pci_dev_put(dev->pci_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002738 free:
2739 kfree(dev->queues);
2740 kfree(dev->entry);
2741 kfree(dev);
2742 return result;
2743}
2744
Keith Buschf0d54a52014-05-02 10:40:43 -06002745static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2746{
Keith Buscha6739472014-06-23 16:03:21 -06002747 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002748
Keith Buscha6739472014-06-23 16:03:21 -06002749 if (prepare)
2750 nvme_dev_shutdown(dev);
2751 else
2752 nvme_dev_resume(dev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002753}
2754
Keith Busch09ece142014-01-27 11:29:40 -05002755static void nvme_shutdown(struct pci_dev *pdev)
2756{
2757 struct nvme_dev *dev = pci_get_drvdata(pdev);
2758 nvme_dev_shutdown(dev);
2759}
2760
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002761static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002762{
2763 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002764
2765 spin_lock(&dev_list_lock);
2766 list_del_init(&dev->node);
2767 spin_unlock(&dev_list_lock);
2768
2769 pci_set_drvdata(pdev, NULL);
2770 flush_work(&dev->reset_work);
Keith Busch5e82e952013-02-19 10:17:58 -07002771 misc_deregister(&dev->miscdev);
Keith Busch302c6722014-07-18 11:40:20 -06002772 nvme_dev_remove(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002773 nvme_dev_shutdown(dev);
2774 nvme_dev_remove_admin(dev);
2775 nvme_free_queues(dev, 0);
2776 nvme_free_admin_tags(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002777 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002778 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002779}
2780
2781/* These functions are yet to be implemented */
2782#define nvme_error_detected NULL
2783#define nvme_dump_registers NULL
2784#define nvme_link_reset NULL
2785#define nvme_slot_reset NULL
2786#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002787
Jingoo Han671a6012014-02-13 11:19:14 +09002788#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002789static int nvme_suspend(struct device *dev)
2790{
2791 struct pci_dev *pdev = to_pci_dev(dev);
2792 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2793
2794 nvme_dev_shutdown(ndev);
2795 return 0;
2796}
2797
2798static int nvme_resume(struct device *dev)
2799{
2800 struct pci_dev *pdev = to_pci_dev(dev);
2801 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002802
Keith Busch9a6b9452013-12-10 13:10:36 -07002803 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Tejun Heo9ca97372014-03-07 10:24:49 -05002804 ndev->reset_workfn = nvme_reset_failed_dev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002805 queue_work(nvme_workq, &ndev->reset_work);
2806 }
2807 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002808}
Jingoo Han671a6012014-02-13 11:19:14 +09002809#endif
Keith Buschcd638942013-07-15 15:02:23 -06002810
2811static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002812
Stephen Hemminger1d352032012-09-07 09:33:17 -07002813static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002814 .error_detected = nvme_error_detected,
2815 .mmio_enabled = nvme_dump_registers,
2816 .link_reset = nvme_link_reset,
2817 .slot_reset = nvme_slot_reset,
2818 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06002819 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002820};
2821
2822/* Move to pci_ids.h later */
2823#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2824
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002825static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002826 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2827 { 0, }
2828};
2829MODULE_DEVICE_TABLE(pci, nvme_id_table);
2830
2831static struct pci_driver nvme_driver = {
2832 .name = "nvme",
2833 .id_table = nvme_id_table,
2834 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002835 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002836 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002837 .driver = {
2838 .pm = &nvme_dev_pm_ops,
2839 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002840 .err_handler = &nvme_err_handler,
2841};
2842
2843static int __init nvme_init(void)
2844{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002845 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002846
Dan McLeranb9afca32014-04-07 17:10:11 -06002847 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002848
Keith Busch9a6b9452013-12-10 13:10:36 -07002849 nvme_workq = create_singlethread_workqueue("nvme");
2850 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06002851 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07002852
Keith Busch5c42ea12012-07-25 16:05:18 -06002853 result = register_blkdev(nvme_major, "nvme");
2854 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002855 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06002856 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002857 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002858
Keith Buschf3db22f2014-06-11 11:51:35 -06002859 result = pci_register_driver(&nvme_driver);
2860 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002861 goto unregister_blkdev;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002862 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002863
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002864 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002865 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002866 kill_workq:
2867 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002868 return result;
2869}
2870
2871static void __exit nvme_exit(void)
2872{
2873 pci_unregister_driver(&nvme_driver);
Keith Buschf3db22f2014-06-11 11:51:35 -06002874 unregister_hotcpu_notifier(&nvme_nb);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002875 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002876 destroy_workqueue(nvme_workq);
Dan McLeranb9afca32014-04-07 17:10:11 -06002877 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002878 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002879}
2880
2881MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2882MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07002883MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002884module_init(nvme_init);
2885module_exit(nvme_exit);