blob: cb529e9a82dd685b5b372bea2ed272c59fae5bc5 [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 Axboe2c305402014-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;
Keith Buschc917dfe2015-01-07 18:55:48 -0700218 blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500219}
220
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400221/* Special values must be less than 0x1000 */
222#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500223#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
224#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
225#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500226
Keith Buschedd10d32014-04-03 16:45:23 -0600227static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400228 struct nvme_completion *cqe)
229{
230 if (ctx == CMD_CTX_CANCELLED)
231 return;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400232 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600233 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400234 "completed id %d twice on queue %d\n",
235 cqe->command_id, le16_to_cpup(&cqe->sq_id));
236 return;
237 }
238 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600239 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400240 "invalid id %d completed on queue %d\n",
241 cqe->command_id, le16_to_cpup(&cqe->sq_id));
242 return;
243 }
Keith Buschedd10d32014-04-03 16:45:23 -0600244 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400245}
246
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700247static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
248{
249 void *ctx;
250
251 if (fn)
252 *fn = cmd->fn;
253 ctx = cmd->ctx;
254 cmd->fn = special_completion;
255 cmd->ctx = CMD_CTX_CANCELLED;
256 return ctx;
257}
258
259static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
260 struct nvme_completion *cqe)
261{
262 struct request *req = ctx;
263
264 u32 result = le32_to_cpup(&cqe->result);
265 u16 status = le16_to_cpup(&cqe->status) >> 1;
266
267 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
268 ++nvmeq->dev->event_limit;
269 if (status == NVME_SC_SUCCESS)
270 dev_warn(nvmeq->q_dmadev,
271 "async event result %08x\n", result);
272
Jens Axboe9d135bb2014-11-17 10:43:42 -0700273 blk_mq_free_hctx_request(nvmeq->hctx, req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700274}
275
276static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
277 struct nvme_completion *cqe)
278{
279 struct request *req = ctx;
280
281 u16 status = le16_to_cpup(&cqe->status) >> 1;
282 u32 result = le32_to_cpup(&cqe->result);
283
Jens Axboe9d135bb2014-11-17 10:43:42 -0700284 blk_mq_free_hctx_request(nvmeq->hctx, req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700285
286 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
287 ++nvmeq->dev->abort_limit;
288}
289
Keith Buschedd10d32014-04-03 16:45:23 -0600290static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700291 struct nvme_completion *cqe)
292{
293 struct async_cmd_info *cmdinfo = ctx;
294 cmdinfo->result = le32_to_cpup(&cqe->result);
295 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
296 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
Jens Axboe9d135bb2014-11-17 10:43:42 -0700297 blk_mq_free_hctx_request(nvmeq->hctx, cmdinfo->req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700298}
299
300static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
301 unsigned int tag)
302{
303 struct blk_mq_hw_ctx *hctx = nvmeq->hctx;
304 struct request *req = blk_mq_tag_to_rq(hctx->tags, tag);
305
306 return blk_mq_rq_to_pdu(req);
Keith Busch4d115422013-12-10 13:10:40 -0700307}
308
Matthew Wilcox184d2942011-05-11 21:36:38 -0400309/*
310 * Called with local interrupts disabled and the q_lock held. May not sleep.
311 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700312static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400313 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500314{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700315 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400316 void *ctx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700317 if (tag >= nvmeq->q_depth) {
318 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500319 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400320 }
Keith Busch859361a2012-08-02 14:05:59 -0600321 if (fn)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700322 *fn = cmd->fn;
323 ctx = cmd->ctx;
324 cmd->fn = special_completion;
325 cmd->ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400326 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500327}
328
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500329/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400330 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500331 * @nvmeq: The queue to use
332 * @cmd: The command to send
333 *
334 * Safe to use from interrupt context
335 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700336static int __nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500337{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700338 u16 tail = nvmeq->sq_tail;
339
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500340 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500341 if (++tail == nvmeq->q_depth)
342 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500343 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500344 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500345
346 return 0;
347}
348
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700349static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
350{
351 unsigned long flags;
352 int ret;
353 spin_lock_irqsave(&nvmeq->q_lock, flags);
354 ret = __nvme_submit_cmd(nvmeq, cmd);
355 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
356 return ret;
357}
358
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500359static __le64 **iod_list(struct nvme_iod *iod)
360{
361 return ((void *)iod) + iod->offset;
362}
363
364/*
365 * Will slightly overestimate the number of pages needed. This is OK
366 * as it only leads to a small amount of wasted memory for the lifetime of
367 * the I/O.
368 */
Keith Busch1d090622014-06-23 11:34:01 -0600369static int nvme_npages(unsigned size, struct nvme_dev *dev)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500370{
Keith Busch1d090622014-06-23 11:34:01 -0600371 unsigned nprps = DIV_ROUND_UP(size + dev->page_size, dev->page_size);
372 return DIV_ROUND_UP(8 * nprps, dev->page_size - 8);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500373}
374
375static struct nvme_iod *
Keith Busch1d090622014-06-23 11:34:01 -0600376nvme_alloc_iod(unsigned nseg, unsigned nbytes, struct nvme_dev *dev, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500377{
378 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Keith Busch1d090622014-06-23 11:34:01 -0600379 sizeof(__le64 *) * nvme_npages(nbytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500380 sizeof(struct scatterlist) * nseg, gfp);
381
382 if (iod) {
383 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
384 iod->npages = -1;
385 iod->length = nbytes;
Keith Busch2b196032012-11-06 11:59:23 -0700386 iod->nents = 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600387 iod->first_dma = 0ULL;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500388 }
389
390 return iod;
391}
392
Vishal Verma5d0f6132013-03-04 18:40:58 -0700393void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500394{
Keith Busch1d090622014-06-23 11:34:01 -0600395 const int last_prp = dev->page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500396 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500397 __le64 **list = iod_list(iod);
398 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500399
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500400 if (iod->npages == 0)
401 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
402 for (i = 0; i < iod->npages; i++) {
403 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500404 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500405 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500406 prp_dma = next_prp_dma;
407 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500408 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500409}
410
Keith Buschb4ff9c82014-08-29 09:06:12 -0600411static int nvme_error_status(u16 status)
412{
413 switch (status & 0x7ff) {
414 case NVME_SC_SUCCESS:
415 return 0;
416 case NVME_SC_CAP_EXCEEDED:
417 return -ENOSPC;
418 default:
419 return -EIO;
420 }
421}
422
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700423static void req_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500424 struct nvme_completion *cqe)
425{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500426 struct nvme_iod *iod = ctx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700427 struct request *req = iod->private;
428 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
429
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500430 u16 status = le16_to_cpup(&cqe->status) >> 1;
431
Keith Buschedd10d32014-04-03 16:45:23 -0600432 if (unlikely(status)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700433 if (!(status & NVME_SC_DNR || blk_noretry_request(req))
434 && (jiffies - req->start_time) < req->timeout) {
Keith Buschc9d3bf82015-01-07 18:55:52 -0700435 unsigned long flags;
436
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700437 blk_mq_requeue_request(req);
Keith Buschc9d3bf82015-01-07 18:55:52 -0700438 spin_lock_irqsave(req->q->queue_lock, flags);
439 if (!blk_queue_stopped(req->q))
440 blk_mq_kick_requeue_list(req->q);
441 spin_unlock_irqrestore(req->q->queue_lock, flags);
Keith Buschedd10d32014-04-03 16:45:23 -0600442 return;
443 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700444 req->errors = nvme_error_status(status);
445 } else
446 req->errors = 0;
447
448 if (cmd_rq->aborted)
449 dev_warn(&nvmeq->dev->pci_dev->dev,
450 "completing aborted command with status:%04x\n",
451 status);
452
453 if (iod->nents)
454 dma_unmap_sg(&nvmeq->dev->pci_dev->dev, iod->sg, iod->nents,
455 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Buschedd10d32014-04-03 16:45:23 -0600456 nvme_free_iod(nvmeq->dev, iod);
Keith Busch3291fa52014-04-28 12:30:52 -0600457
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700458 blk_mq_complete_request(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500459}
460
Matthew Wilcox184d2942011-05-11 21:36:38 -0400461/* length is in bytes. gfp flags indicates whether we may sleep. */
Keith Buschedd10d32014-04-03 16:45:23 -0600462int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod, int total_len,
463 gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500464{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500465 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500466 int length = total_len;
467 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500468 int dma_len = sg_dma_len(sg);
469 u64 dma_addr = sg_dma_address(sg);
470 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500471 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500472 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500473 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500474 int nprps, i;
Keith Busch1d090622014-06-23 11:34:01 -0600475 u32 page_size = dev->page_size;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500476
Keith Busch1d090622014-06-23 11:34:01 -0600477 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500478 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500479 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500480
Keith Busch1d090622014-06-23 11:34:01 -0600481 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500482 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600483 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500484 } else {
485 sg = sg_next(sg);
486 dma_addr = sg_dma_address(sg);
487 dma_len = sg_dma_len(sg);
488 }
489
Keith Busch1d090622014-06-23 11:34:01 -0600490 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600491 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500492 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500493 }
494
Keith Busch1d090622014-06-23 11:34:01 -0600495 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500496 if (nprps <= (256 / 8)) {
497 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500498 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500499 } else {
500 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500501 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500502 }
503
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400504 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
505 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600506 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500507 iod->npages = -1;
Keith Busch1d090622014-06-23 11:34:01 -0600508 return (total_len - length) + page_size;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400509 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500510 list[0] = prp_list;
511 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500512 i = 0;
513 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600514 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500515 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400516 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500517 if (!prp_list)
518 return total_len - length;
519 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400520 prp_list[0] = old_prp_list[i - 1];
521 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
522 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500523 }
524 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600525 dma_len -= page_size;
526 dma_addr += page_size;
527 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500528 if (length <= 0)
529 break;
530 if (dma_len > 0)
531 continue;
532 BUG_ON(dma_len < 0);
533 sg = sg_next(sg);
534 dma_addr = sg_dma_address(sg);
535 dma_len = sg_dma_len(sg);
536 }
537
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500538 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500539}
540
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700541/*
542 * We reuse the small pool to allocate the 16-byte range here as it is not
543 * worth having a special pool for these or additional cases to handle freeing
544 * the iod.
545 */
546static void nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
547 struct request *req, struct nvme_iod *iod)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700548{
Keith Buschedd10d32014-04-03 16:45:23 -0600549 struct nvme_dsm_range *range =
550 (struct nvme_dsm_range *)iod_list(iod)[0];
Keith Busch0e5e4f02012-11-09 16:33:05 -0700551 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
552
Keith Busch0e5e4f02012-11-09 16:33:05 -0700553 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700554 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
555 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700556
557 memset(cmnd, 0, sizeof(*cmnd));
558 cmnd->dsm.opcode = nvme_cmd_dsm;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700559 cmnd->dsm.command_id = req->tag;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700560 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
561 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
562 cmnd->dsm.nr = 0;
563 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
564
565 if (++nvmeq->sq_tail == nvmeq->q_depth)
566 nvmeq->sq_tail = 0;
567 writel(nvmeq->sq_tail, nvmeq->q_db);
Keith Busch0e5e4f02012-11-09 16:33:05 -0700568}
569
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700570static void nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500571 int cmdid)
572{
573 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
574
575 memset(cmnd, 0, sizeof(*cmnd));
576 cmnd->common.opcode = nvme_cmd_flush;
577 cmnd->common.command_id = cmdid;
578 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
579
580 if (++nvmeq->sq_tail == nvmeq->q_depth)
581 nvmeq->sq_tail = 0;
582 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500583}
584
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700585static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod,
586 struct nvme_ns *ns)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500587{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700588 struct request *req = iod->private;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500589 struct nvme_command *cmnd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700590 u16 control = 0;
591 u32 dsmgmt = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500592
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700593 if (req->cmd_flags & REQ_FUA)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500594 control |= NVME_RW_FUA;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700595 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500596 control |= NVME_RW_LR;
597
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700598 if (req->cmd_flags & REQ_RAHEAD)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500599 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
600
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500601 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500602 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500603
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700604 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
605 cmnd->rw.command_id = req->tag;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500606 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Keith Buschedd10d32014-04-03 16:45:23 -0600607 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
608 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700609 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
610 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500611 cmnd->rw.control = cpu_to_le16(control);
612 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500613
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500614 if (++nvmeq->sq_tail == nvmeq->q_depth)
615 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500616 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500617
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500618 return 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600619}
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500620
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700621static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
622 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600623{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700624 struct nvme_ns *ns = hctx->queue->queuedata;
625 struct nvme_queue *nvmeq = hctx->driver_data;
626 struct request *req = bd->rq;
627 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
Keith Buschedd10d32014-04-03 16:45:23 -0600628 struct nvme_iod *iod;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700629 int psegs = req->nr_phys_segments;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700630 enum dma_data_direction dma_dir;
631 unsigned size = !(req->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(req) :
Keith Busch9dbbfab2014-10-06 15:23:06 -0600632 sizeof(struct nvme_dsm_range);
Keith Buschedd10d32014-04-03 16:45:23 -0600633
Keith Busch9dbbfab2014-10-06 15:23:06 -0600634 iod = nvme_alloc_iod(psegs, size, ns->dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600635 if (!iod)
Jens Axboefe543032014-12-11 13:58:39 -0700636 return BLK_MQ_RQ_QUEUE_BUSY;
Keith Buschedd10d32014-04-03 16:45:23 -0600637
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700638 iod->private = req;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700639
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700640 if (req->cmd_flags & REQ_DISCARD) {
Keith Buschedd10d32014-04-03 16:45:23 -0600641 void *range;
642 /*
643 * We reuse the small pool to allocate the 16-byte range here
644 * as it is not worth having a special pool for these or
645 * additional cases to handle freeing the iod.
646 */
647 range = dma_pool_alloc(nvmeq->dev->prp_small_pool,
648 GFP_ATOMIC,
649 &iod->first_dma);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700650 if (!range)
Jens Axboefe543032014-12-11 13:58:39 -0700651 goto retry_cmd;
Keith Buschedd10d32014-04-03 16:45:23 -0600652 iod_list(iod)[0] = (__le64 *)range;
653 iod->npages = 0;
654 } else if (psegs) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700655 dma_dir = rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
Keith Buschedd10d32014-04-03 16:45:23 -0600656
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700657 sg_init_table(iod->sg, psegs);
658 iod->nents = blk_rq_map_sg(req->q, req, iod->sg);
Jens Axboefe543032014-12-11 13:58:39 -0700659 if (!iod->nents)
660 goto error_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700661
662 if (!dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir))
Jens Axboefe543032014-12-11 13:58:39 -0700663 goto retry_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700664
Jens Axboefe543032014-12-11 13:58:39 -0700665 if (blk_rq_bytes(req) !=
666 nvme_setup_prps(nvmeq->dev, iod, blk_rq_bytes(req), GFP_ATOMIC)) {
667 dma_unmap_sg(&nvmeq->dev->pci_dev->dev, iod->sg,
668 iod->nents, dma_dir);
669 goto retry_cmd;
670 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700671 }
672
Keith Busch9af87852014-12-03 17:07:13 -0700673 nvme_set_info(cmd, iod, req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700674 spin_lock_irq(&nvmeq->q_lock);
675 if (req->cmd_flags & REQ_DISCARD)
676 nvme_submit_discard(nvmeq, ns, req, iod);
677 else if (req->cmd_flags & REQ_FLUSH)
678 nvme_submit_flush(nvmeq, ns, req->tag);
679 else
680 nvme_submit_iod(nvmeq, iod, ns);
681
682 nvme_process_cq(nvmeq);
683 spin_unlock_irq(&nvmeq->q_lock);
684 return BLK_MQ_RQ_QUEUE_OK;
685
Jens Axboefe543032014-12-11 13:58:39 -0700686 error_cmd:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500687 nvme_free_iod(nvmeq->dev, iod);
Jens Axboefe543032014-12-11 13:58:39 -0700688 return BLK_MQ_RQ_QUEUE_ERROR;
689 retry_cmd:
690 nvme_free_iod(nvmeq->dev, iod);
691 return BLK_MQ_RQ_QUEUE_BUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500692}
693
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400694static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500695{
Matthew Wilcox82123462011-01-20 13:24:06 -0500696 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500697
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500698 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500699 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500700
701 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400702 void *ctx;
703 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500704 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500705 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500706 break;
707 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
708 if (++head == nvmeq->q_depth) {
709 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500710 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500711 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700712 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600713 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500714 }
715
716 /* If the controller ignores the cq head doorbell and continuously
717 * writes to the queue, it is theoretically possible to wrap around
718 * the queue twice and mistakenly return IRQ_NONE. Linux only
719 * requires that 0.1% of your interrupts are handled, so this isn't
720 * a big problem.
721 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500722 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400723 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500724
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800725 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500726 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500727 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500728
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400729 nvmeq->cqe_seen = 1;
730 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500731}
732
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700733/* Admin queue isn't initialized as a request queue. If at some point this
734 * happens anyway, make sure to notify the user */
735static int nvme_admin_queue_rq(struct blk_mq_hw_ctx *hctx,
736 const struct blk_mq_queue_data *bd)
Matthew Wilcox7d822452013-06-24 12:03:57 -0400737{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700738 WARN_ON_ONCE(1);
739 return BLK_MQ_RQ_QUEUE_ERROR;
Matthew Wilcox7d822452013-06-24 12:03:57 -0400740}
741
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500742static irqreturn_t nvme_irq(int irq, void *data)
743{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500744 irqreturn_t result;
745 struct nvme_queue *nvmeq = data;
746 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400747 nvme_process_cq(nvmeq);
748 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
749 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500750 spin_unlock(&nvmeq->q_lock);
751 return result;
752}
753
754static irqreturn_t nvme_irq_check(int irq, void *data)
755{
756 struct nvme_queue *nvmeq = data;
757 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
758 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
759 return IRQ_NONE;
760 return IRQ_WAKE_THREAD;
761}
762
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700763static void nvme_abort_cmd_info(struct nvme_queue *nvmeq, struct nvme_cmd_info *
764 cmd_info)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500765{
766 spin_lock_irq(&nvmeq->q_lock);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700767 cancel_cmd_info(cmd_info, NULL);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500768 spin_unlock_irq(&nvmeq->q_lock);
769}
770
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400771struct sync_cmd_info {
772 struct task_struct *task;
773 u32 result;
774 int status;
775};
776
Keith Buschedd10d32014-04-03 16:45:23 -0600777static void sync_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400778 struct nvme_completion *cqe)
779{
780 struct sync_cmd_info *cmdinfo = ctx;
781 cmdinfo->result = le32_to_cpup(&cqe->result);
782 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
783 wake_up_process(cmdinfo->task);
784}
785
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500786/*
787 * Returns 0 on success. If the result is negative, it's a Linux error code;
788 * if the result is positive, it's an NVM Express status code
789 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700790static int nvme_submit_sync_cmd(struct request *req, struct nvme_command *cmd,
Vishal Verma5d0f6132013-03-04 18:40:58 -0700791 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500792{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700793 int ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500794 struct sync_cmd_info cmdinfo;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700795 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
796 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500797
798 cmdinfo.task = current;
799 cmdinfo.status = -EINTR;
800
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700801 cmd->common.command_id = req->tag;
802
803 nvme_set_info(cmd_rq, &cmdinfo, sync_completion);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500804
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500805 set_current_state(TASK_KILLABLE);
Keith Busch4f5099a2014-03-03 16:39:13 -0700806 ret = nvme_submit_cmd(nvmeq, cmd);
807 if (ret) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700808 nvme_finish_cmd(nvmeq, req->tag, NULL);
Keith Busch4f5099a2014-03-03 16:39:13 -0700809 set_current_state(TASK_RUNNING);
Keith Busch4f5099a2014-03-03 16:39:13 -0700810 }
Jens Axboe849c6e72014-12-12 08:53:40 -0700811 ret = schedule_timeout(timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500812
Jens Axboe849c6e72014-12-12 08:53:40 -0700813 /*
814 * Ensure that sync_completion has either run, or that it will
815 * never run.
816 */
817 nvme_abort_cmd_info(nvmeq, blk_mq_rq_to_pdu(req));
818
819 /*
820 * We never got the completion
821 */
822 if (cmdinfo.status == -EINTR)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500823 return -EINTR;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500824
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500825 if (result)
826 *result = cmdinfo.result;
827
828 return cmdinfo.status;
829}
830
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700831static int nvme_submit_async_admin_req(struct nvme_dev *dev)
832{
833 struct nvme_queue *nvmeq = dev->queues[0];
834 struct nvme_command c;
835 struct nvme_cmd_info *cmd_info;
836 struct request *req;
837
Jens Axboe6dcc0cf2014-11-18 08:21:18 -0700838 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC, false);
Dan Carpenter9f173b32014-11-05 23:39:09 +0300839 if (IS_ERR(req))
840 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700841
Keith Buschc917dfe2015-01-07 18:55:48 -0700842 req->cmd_flags |= REQ_NO_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700843 cmd_info = blk_mq_rq_to_pdu(req);
844 nvme_set_info(cmd_info, req, async_req_completion);
845
846 memset(&c, 0, sizeof(c));
847 c.common.opcode = nvme_admin_async_event;
848 c.common.command_id = req->tag;
849
850 return __nvme_submit_cmd(nvmeq, &c);
851}
852
853static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
Keith Busch4d115422013-12-10 13:10:40 -0700854 struct nvme_command *cmd,
855 struct async_cmd_info *cmdinfo, unsigned timeout)
856{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700857 struct nvme_queue *nvmeq = dev->queues[0];
858 struct request *req;
859 struct nvme_cmd_info *cmd_rq;
Keith Busch4d115422013-12-10 13:10:40 -0700860
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700861 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_KERNEL, false);
Dan Carpenter9f173b32014-11-05 23:39:09 +0300862 if (IS_ERR(req))
863 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700864
865 req->timeout = timeout;
866 cmd_rq = blk_mq_rq_to_pdu(req);
867 cmdinfo->req = req;
868 nvme_set_info(cmd_rq, cmdinfo, async_completion);
Keith Busch4d115422013-12-10 13:10:40 -0700869 cmdinfo->status = -EINTR;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700870
871 cmd->common.command_id = req->tag;
872
Keith Busch4f5099a2014-03-03 16:39:13 -0700873 return nvme_submit_cmd(nvmeq, cmd);
Keith Busch4d115422013-12-10 13:10:40 -0700874}
875
kbuild test robota64e6bb2014-11-05 18:47:07 +0800876static int __nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700877 u32 *result, unsigned timeout)
878{
879 int res;
880 struct request *req;
881
882 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_KERNEL, false);
Jens Axboe97fe3832014-12-10 13:02:44 -0700883 if (IS_ERR(req))
884 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700885 res = nvme_submit_sync_cmd(req, cmd, result, timeout);
Jens Axboe9d135bb2014-11-17 10:43:42 -0700886 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700887 return res;
888}
889
Vishal Verma5d0f6132013-03-04 18:40:58 -0700890int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500891 u32 *result)
892{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700893 return __nvme_submit_admin_cmd(dev, cmd, result, ADMIN_TIMEOUT);
Keith Busch4f5099a2014-03-03 16:39:13 -0700894}
895
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700896int nvme_submit_io_cmd(struct nvme_dev *dev, struct nvme_ns *ns,
897 struct nvme_command *cmd, u32 *result)
Keith Busch4f5099a2014-03-03 16:39:13 -0700898{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700899 int res;
900 struct request *req;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500901
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700902 req = blk_mq_alloc_request(ns->queue, WRITE, (GFP_KERNEL|__GFP_WAIT),
903 false);
Jens Axboe97fe3832014-12-10 13:02:44 -0700904 if (IS_ERR(req))
905 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700906 res = nvme_submit_sync_cmd(req, cmd, result, NVME_IO_TIMEOUT);
Jens Axboe9d135bb2014-11-17 10:43:42 -0700907 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700908 return res;
Keith Busch4d115422013-12-10 13:10:40 -0700909}
910
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500911static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
912{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500913 struct nvme_command c;
914
915 memset(&c, 0, sizeof(c));
916 c.delete_queue.opcode = opcode;
917 c.delete_queue.qid = cpu_to_le16(id);
918
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700919 return nvme_submit_admin_cmd(dev, &c, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500920}
921
922static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
923 struct nvme_queue *nvmeq)
924{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500925 struct nvme_command c;
926 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
927
928 memset(&c, 0, sizeof(c));
929 c.create_cq.opcode = nvme_admin_create_cq;
930 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
931 c.create_cq.cqid = cpu_to_le16(qid);
932 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
933 c.create_cq.cq_flags = cpu_to_le16(flags);
934 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
935
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700936 return nvme_submit_admin_cmd(dev, &c, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500937}
938
939static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
940 struct nvme_queue *nvmeq)
941{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500942 struct nvme_command c;
943 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
944
945 memset(&c, 0, sizeof(c));
946 c.create_sq.opcode = nvme_admin_create_sq;
947 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
948 c.create_sq.sqid = cpu_to_le16(qid);
949 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
950 c.create_sq.sq_flags = cpu_to_le16(flags);
951 c.create_sq.cqid = cpu_to_le16(qid);
952
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700953 return nvme_submit_admin_cmd(dev, &c, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500954}
955
956static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
957{
958 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
959}
960
961static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
962{
963 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
964}
965
Vishal Verma5d0f6132013-03-04 18:40:58 -0700966int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400967 dma_addr_t dma_addr)
968{
969 struct nvme_command c;
970
971 memset(&c, 0, sizeof(c));
972 c.identify.opcode = nvme_admin_identify;
973 c.identify.nsid = cpu_to_le32(nsid);
974 c.identify.prp1 = cpu_to_le64(dma_addr);
975 c.identify.cns = cpu_to_le32(cns);
976
977 return nvme_submit_admin_cmd(dev, &c, NULL);
978}
979
Vishal Verma5d0f6132013-03-04 18:40:58 -0700980int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -0600981 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400982{
983 struct nvme_command c;
984
985 memset(&c, 0, sizeof(c));
986 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -0600987 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400988 c.features.prp1 = cpu_to_le64(dma_addr);
989 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400990
Keith Busch08df1e02012-09-21 10:52:13 -0600991 return nvme_submit_admin_cmd(dev, &c, result);
Matthew Wilcoxdf348132012-01-11 07:29:56 -0700992}
993
Vishal Verma5d0f6132013-03-04 18:40:58 -0700994int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
995 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -0700996{
997 struct nvme_command c;
998
999 memset(&c, 0, sizeof(c));
1000 c.features.opcode = nvme_admin_set_features;
1001 c.features.prp1 = cpu_to_le64(dma_addr);
1002 c.features.fid = cpu_to_le32(fid);
1003 c.features.dword11 = cpu_to_le32(dword11);
1004
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001005 return nvme_submit_admin_cmd(dev, &c, result);
1006}
1007
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001008/**
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001009 * nvme_abort_req - Attempt aborting a request
Keith Buschc30341d2013-12-10 13:10:38 -07001010 *
1011 * Schedule controller reset if the command was already aborted once before and
1012 * still hasn't been returned to the driver, or if this is the admin queue.
1013 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001014static void nvme_abort_req(struct request *req)
Keith Buschc30341d2013-12-10 13:10:38 -07001015{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001016 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1017 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001018 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001019 struct request *abort_req;
1020 struct nvme_cmd_info *abort_cmd;
1021 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -07001022
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001023 if (!nvmeq->qid || cmd_rq->aborted) {
Keith Busch7a509a62015-01-07 18:55:53 -07001024 unsigned long flags;
1025
1026 spin_lock_irqsave(&dev_list_lock, flags);
Keith Buschc30341d2013-12-10 13:10:38 -07001027 if (work_busy(&dev->reset_work))
Keith Busch7a509a62015-01-07 18:55:53 -07001028 goto out;
Keith Buschc30341d2013-12-10 13:10:38 -07001029 list_del_init(&dev->node);
1030 dev_warn(&dev->pci_dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001031 "I/O %d QID %d timeout, reset controller\n",
1032 req->tag, nvmeq->qid);
Tejun Heo9ca97372014-03-07 10:24:49 -05001033 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschc30341d2013-12-10 13:10:38 -07001034 queue_work(nvme_workq, &dev->reset_work);
Keith Busch7a509a62015-01-07 18:55:53 -07001035 out:
1036 spin_unlock_irqrestore(&dev_list_lock, flags);
Keith Buschc30341d2013-12-10 13:10:38 -07001037 return;
1038 }
1039
1040 if (!dev->abort_limit)
1041 return;
1042
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001043 abort_req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC,
1044 false);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001045 if (IS_ERR(abort_req))
Keith Buschc30341d2013-12-10 13:10:38 -07001046 return;
1047
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001048 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1049 nvme_set_info(abort_cmd, abort_req, abort_completion);
1050
Keith Buschc30341d2013-12-10 13:10:38 -07001051 memset(&cmd, 0, sizeof(cmd));
1052 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001053 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001054 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001055 cmd.abort.command_id = abort_req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001056
1057 --dev->abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001058 cmd_rq->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -07001059
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001060 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", req->tag,
Keith Buschc30341d2013-12-10 13:10:38 -07001061 nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001062 if (nvme_submit_cmd(dev->queues[0], &cmd) < 0) {
1063 dev_warn(nvmeq->q_dmadev,
1064 "Could not abort I/O %d QID %d",
1065 req->tag, nvmeq->qid);
Sam Bradshawc87fd542014-12-10 13:00:31 -07001066 blk_mq_free_request(abort_req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001067 }
Keith Buschc30341d2013-12-10 13:10:38 -07001068}
1069
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001070static void nvme_cancel_queue_ios(struct blk_mq_hw_ctx *hctx,
1071 struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001072{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001073 struct nvme_queue *nvmeq = data;
1074 void *ctx;
1075 nvme_completion_fn fn;
1076 struct nvme_cmd_info *cmd;
Keith Buschcef6a942015-01-07 18:55:51 -07001077 struct nvme_completion cqe;
1078
1079 if (!blk_mq_request_started(req))
1080 return;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001081
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001082 cmd = blk_mq_rq_to_pdu(req);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001083
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001084 if (cmd->ctx == CMD_CTX_CANCELLED)
1085 return;
1086
Keith Buschcef6a942015-01-07 18:55:51 -07001087 if (blk_queue_dying(req->q))
1088 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1089 else
1090 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1091
1092
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001093 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1094 req->tag, nvmeq->qid);
1095 ctx = cancel_cmd_info(cmd, &fn);
1096 fn(nvmeq, ctx, &cqe);
1097}
1098
1099static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
1100{
1101 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
1102 struct nvme_queue *nvmeq = cmd->nvmeq;
1103
Keith Busch7a509a62015-01-07 18:55:53 -07001104 /*
1105 * The aborted req will be completed on receiving the abort req.
1106 * We enable the timer again. If hit twice, it'll cause a device reset,
1107 * as the device then is in a faulty state.
1108 */
1109 int ret = BLK_EH_RESET_TIMER;
1110
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001111 dev_warn(nvmeq->q_dmadev, "Timeout I/O %d QID %d\n", req->tag,
1112 nvmeq->qid);
Keith Buschc917dfe2015-01-07 18:55:48 -07001113
Keith Busch7a509a62015-01-07 18:55:53 -07001114 spin_lock_irq(&nvmeq->q_lock);
Keith Buschc917dfe2015-01-07 18:55:48 -07001115 if (!nvmeq->dev->initialized) {
1116 /*
1117 * Force cancelled command frees the request, which requires we
1118 * return BLK_EH_NOT_HANDLED.
1119 */
1120 nvme_cancel_queue_ios(nvmeq->hctx, req, nvmeq, reserved);
Keith Busch7a509a62015-01-07 18:55:53 -07001121 ret = BLK_EH_NOT_HANDLED;
1122 } else
1123 nvme_abort_req(req);
1124 spin_unlock_irq(&nvmeq->q_lock);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001125
Keith Busch7a509a62015-01-07 18:55:53 -07001126 return ret;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001127}
1128
Keith Buschf435c282014-07-07 09:14:42 -06001129static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001130{
1131 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1132 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1133 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1134 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1135 kfree(nvmeq);
1136}
1137
Keith Buscha1a5ef92013-12-16 13:50:00 -05001138static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001139{
Keith Buschf435c282014-07-07 09:14:42 -06001140 LLIST_HEAD(q_list);
1141 struct nvme_queue *nvmeq, *next;
1142 struct llist_node *entry;
Keith Busch22404272013-07-15 15:02:20 -06001143 int i;
1144
Keith Buscha1a5ef92013-12-16 13:50:00 -05001145 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001146 struct nvme_queue *nvmeq = dev->queues[i];
Keith Buschf435c282014-07-07 09:14:42 -06001147 llist_add(&nvmeq->node, &q_list);
Keith Busch22404272013-07-15 15:02:20 -06001148 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001149 dev->queues[i] = NULL;
Keith Busch22404272013-07-15 15:02:20 -06001150 }
Keith Buschf435c282014-07-07 09:14:42 -06001151 synchronize_rcu();
1152 entry = llist_del_all(&q_list);
1153 llist_for_each_entry_safe(nvmeq, next, entry, node)
1154 nvme_free_queue(nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001155}
1156
Keith Busch4d115422013-12-10 13:10:40 -07001157/**
1158 * nvme_suspend_queue - put queue into suspended state
1159 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001160 */
1161static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001162{
Keith Busch2b25d982014-12-22 12:59:04 -07001163 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001164
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001165 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001166 if (nvmeq->cq_vector == -1) {
1167 spin_unlock_irq(&nvmeq->q_lock);
1168 return 1;
1169 }
1170 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Keith Busch42f61422014-03-24 10:46:25 -06001171 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001172 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001173 spin_unlock_irq(&nvmeq->q_lock);
1174
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001175 irq_set_affinity_hint(vector, NULL);
1176 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001177
Keith Busch4d115422013-12-10 13:10:40 -07001178 return 0;
1179}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001180
Keith Busch4d115422013-12-10 13:10:40 -07001181static void nvme_clear_queue(struct nvme_queue *nvmeq)
1182{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001183 struct blk_mq_hw_ctx *hctx = nvmeq->hctx;
1184
Keith Busch22404272013-07-15 15:02:20 -06001185 spin_lock_irq(&nvmeq->q_lock);
1186 nvme_process_cq(nvmeq);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001187 if (hctx && hctx->tags)
1188 blk_mq_tag_busy_iter(hctx, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001189 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001190}
1191
Keith Busch4d115422013-12-10 13:10:40 -07001192static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1193{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001194 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001195
1196 if (!nvmeq)
1197 return;
1198 if (nvme_suspend_queue(nvmeq))
1199 return;
1200
Keith Busch0e53d182013-12-10 13:10:39 -07001201 /* Don't tell the adapter to delete the admin queue.
1202 * Don't tell a removed adapter to delete IO queues. */
1203 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001204 adapter_delete_sq(dev, qid);
1205 adapter_delete_cq(dev, qid);
1206 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001207 if (!qid && dev->admin_q)
1208 blk_mq_freeze_queue_start(dev->admin_q);
Keith Busch4d115422013-12-10 13:10:40 -07001209 nvme_clear_queue(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001210}
1211
1212static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Keith Busch2b25d982014-12-22 12:59:04 -07001213 int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001214{
1215 struct device *dmadev = &dev->pci_dev->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001216 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001217 if (!nvmeq)
1218 return NULL;
1219
Joe Perches4d51abf2014-06-15 13:37:33 -07001220 nvmeq->cqes = dma_zalloc_coherent(dmadev, CQ_SIZE(depth),
1221 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001222 if (!nvmeq->cqes)
1223 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001224
1225 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
1226 &nvmeq->sq_dma_addr, GFP_KERNEL);
1227 if (!nvmeq->sq_cmds)
1228 goto free_cqdma;
1229
1230 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001231 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001232 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1233 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001234 spin_lock_init(&nvmeq->q_lock);
1235 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001236 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001237 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001238 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001239 nvmeq->qid = qid;
Keith Busch22404272013-07-15 15:02:20 -06001240 dev->queue_count++;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001241 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001242
1243 return nvmeq;
1244
1245 free_cqdma:
Keith Busch68b8eca2013-05-01 13:07:47 -06001246 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001247 nvmeq->cq_dma_addr);
1248 free_nvmeq:
1249 kfree(nvmeq);
1250 return NULL;
1251}
1252
Matthew Wilcox30010822011-01-20 09:10:15 -05001253static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1254 const char *name)
1255{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001256 if (use_threaded_interrupts)
1257 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001258 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001259 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001260 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001261 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001262}
1263
Keith Busch22404272013-07-15 15:02:20 -06001264static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001265{
Keith Busch22404272013-07-15 15:02:20 -06001266 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001267
Keith Busch7be50e92014-09-10 15:48:47 -06001268 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001269 nvmeq->sq_tail = 0;
1270 nvmeq->cq_head = 0;
1271 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001272 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001273 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001274 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001275 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001276}
1277
1278static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1279{
1280 struct nvme_dev *dev = nvmeq->dev;
1281 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001282
Keith Busch2b25d982014-12-22 12:59:04 -07001283 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001284 result = adapter_alloc_cq(dev, qid, nvmeq);
1285 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001286 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001287
1288 result = adapter_alloc_sq(dev, qid, nvmeq);
1289 if (result < 0)
1290 goto release_cq;
1291
Matthew Wilcox3193f072014-01-27 15:57:22 -05001292 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001293 if (result < 0)
1294 goto release_sq;
1295
Keith Busch22404272013-07-15 15:02:20 -06001296 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001297 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001298
1299 release_sq:
1300 adapter_delete_sq(dev, qid);
1301 release_cq:
1302 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001303 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001304}
1305
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001306static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1307{
1308 unsigned long timeout;
1309 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1310
1311 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1312
1313 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1314 msleep(100);
1315 if (fatal_signal_pending(current))
1316 return -EINTR;
1317 if (time_after(jiffies, timeout)) {
1318 dev_err(&dev->pci_dev->dev,
Matthew Wilcox27e81662014-04-11 11:58:45 -04001319 "Device not ready; aborting %s\n", enabled ?
1320 "initialisation" : "reset");
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001321 return -ENODEV;
1322 }
1323 }
1324
1325 return 0;
1326}
1327
1328/*
1329 * If the device has been passed off to us in an enabled state, just clear
1330 * the enabled bit. The spec says we should set the 'shutdown notification
1331 * bits', but doing so may cause the device to complete commands to the
1332 * admin queue ... and we don't know what memory that might be pointing at!
1333 */
1334static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1335{
Dan McLeran01079522014-06-23 08:24:36 -06001336 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1337 dev->ctrl_config &= ~NVME_CC_ENABLE;
1338 writel(dev->ctrl_config, &dev->bar->cc);
Matthew Wilcox44af1462013-05-04 06:43:17 -04001339
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001340 return nvme_wait_ready(dev, cap, false);
1341}
1342
1343static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1344{
Dan McLeran01079522014-06-23 08:24:36 -06001345 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1346 dev->ctrl_config |= NVME_CC_ENABLE;
1347 writel(dev->ctrl_config, &dev->bar->cc);
1348
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001349 return nvme_wait_ready(dev, cap, true);
1350}
1351
Keith Busch1894d8f2013-07-15 15:02:22 -06001352static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1353{
1354 unsigned long timeout;
Keith Busch1894d8f2013-07-15 15:02:22 -06001355
Dan McLeran01079522014-06-23 08:24:36 -06001356 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1357 dev->ctrl_config |= NVME_CC_SHN_NORMAL;
1358
1359 writel(dev->ctrl_config, &dev->bar->cc);
Keith Busch1894d8f2013-07-15 15:02:22 -06001360
Dan McLeran2484f402014-07-01 09:33:32 -06001361 timeout = SHUTDOWN_TIMEOUT + jiffies;
Keith Busch1894d8f2013-07-15 15:02:22 -06001362 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1363 NVME_CSTS_SHST_CMPLT) {
1364 msleep(100);
1365 if (fatal_signal_pending(current))
1366 return -EINTR;
1367 if (time_after(jiffies, timeout)) {
1368 dev_err(&dev->pci_dev->dev,
1369 "Device shutdown incomplete; abort shutdown\n");
1370 return -ENODEV;
1371 }
1372 }
1373
1374 return 0;
1375}
1376
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001377static struct blk_mq_ops nvme_mq_admin_ops = {
1378 .queue_rq = nvme_admin_queue_rq,
1379 .map_queue = blk_mq_map_queue,
1380 .init_hctx = nvme_admin_init_hctx,
Jens Axboe2c305402014-11-14 09:47:32 -07001381 .exit_hctx = nvme_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001382 .init_request = nvme_admin_init_request,
1383 .timeout = nvme_timeout,
1384};
1385
1386static struct blk_mq_ops nvme_mq_ops = {
1387 .queue_rq = nvme_queue_rq,
1388 .map_queue = blk_mq_map_queue,
1389 .init_hctx = nvme_init_hctx,
Jens Axboe2c305402014-11-14 09:47:32 -07001390 .exit_hctx = nvme_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001391 .init_request = nvme_init_request,
1392 .timeout = nvme_timeout,
1393};
1394
Keith Buschea191d22015-01-07 18:55:49 -07001395static void nvme_dev_remove_admin(struct nvme_dev *dev)
1396{
1397 if (dev->admin_q && !blk_queue_dying(dev->admin_q)) {
1398 blk_cleanup_queue(dev->admin_q);
1399 blk_mq_free_tag_set(&dev->admin_tagset);
1400 }
1401}
1402
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001403static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1404{
1405 if (!dev->admin_q) {
1406 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1407 dev->admin_tagset.nr_hw_queues = 1;
1408 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1;
1409 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
1410 dev->admin_tagset.numa_node = dev_to_node(&dev->pci_dev->dev);
1411 dev->admin_tagset.cmd_size = sizeof(struct nvme_cmd_info);
1412 dev->admin_tagset.driver_data = dev;
1413
1414 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1415 return -ENOMEM;
1416
1417 dev->admin_q = blk_mq_init_queue(&dev->admin_tagset);
Ming Lei35b489d2015-01-02 14:25:27 +00001418 if (IS_ERR(dev->admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001419 blk_mq_free_tag_set(&dev->admin_tagset);
1420 return -ENOMEM;
1421 }
Keith Buschea191d22015-01-07 18:55:49 -07001422 if (!blk_get_queue(dev->admin_q)) {
1423 nvme_dev_remove_admin(dev);
1424 return -ENODEV;
1425 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001426 } else
1427 blk_mq_unfreeze_queue(dev->admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001428
1429 return 0;
1430}
1431
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001432static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001433{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001434 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001435 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001436 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001437 struct nvme_queue *nvmeq;
Keith Busch1d090622014-06-23 11:34:01 -06001438 unsigned page_shift = PAGE_SHIFT;
1439 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12;
1440 unsigned dev_page_max = NVME_CAP_MPSMAX(cap) + 12;
1441
1442 if (page_shift < dev_page_min) {
1443 dev_err(&dev->pci_dev->dev,
1444 "Minimum device page size (%u) too large for "
1445 "host (%u)\n", 1 << dev_page_min,
1446 1 << page_shift);
1447 return -ENODEV;
1448 }
1449 if (page_shift > dev_page_max) {
1450 dev_info(&dev->pci_dev->dev,
1451 "Device maximum page size (%u) smaller than "
1452 "host (%u); enabling work-around\n",
1453 1 << dev_page_max, 1 << page_shift);
1454 page_shift = dev_page_max;
1455 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001456
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001457 result = nvme_disable_ctrl(dev, cap);
1458 if (result < 0)
1459 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001460
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001461 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001462 if (!nvmeq) {
Keith Busch2b25d982014-12-22 12:59:04 -07001463 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001464 if (!nvmeq)
1465 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001466 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001467
1468 aqa = nvmeq->q_depth - 1;
1469 aqa |= aqa << 16;
1470
Keith Busch1d090622014-06-23 11:34:01 -06001471 dev->page_size = 1 << page_shift;
1472
Dan McLeran01079522014-06-23 08:24:36 -06001473 dev->ctrl_config = NVME_CC_CSS_NVM;
Keith Busch1d090622014-06-23 11:34:01 -06001474 dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001475 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001476 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001477
1478 writel(aqa, &dev->bar->aqa);
1479 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1480 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001481
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001482 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001483 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001484 goto free_nvmeq;
1485
Keith Busch2b25d982014-12-22 12:59:04 -07001486 nvmeq->cq_vector = 0;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001487 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Keith Busch025c5572013-05-01 13:07:51 -06001488 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07001489 goto free_nvmeq;
Keith Busch025c5572013-05-01 13:07:51 -06001490
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001491 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001492
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001493 free_nvmeq:
1494 nvme_free_queues(dev, 0);
1495 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001496}
1497
Vishal Verma5d0f6132013-03-04 18:40:58 -07001498struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001499 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001500{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001501 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001502 struct scatterlist *sg;
1503 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001504 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001505
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001506 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001507 return ERR_PTR(-EINVAL);
Dan Carpenter5460fc02013-05-13 17:59:50 +03001508 if (!length || length > INT_MAX - PAGE_SIZE)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001509 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001510
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001511 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001512 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1513 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001514 if (!pages)
1515 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001516
1517 err = get_user_pages_fast(addr, count, 1, pages);
1518 if (err < count) {
1519 count = err;
1520 err = -EFAULT;
1521 goto put_pages;
1522 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001523
Santosh Y6808c5f2014-05-29 10:01:52 +05301524 err = -ENOMEM;
Keith Busch1d090622014-06-23 11:34:01 -06001525 iod = nvme_alloc_iod(count, length, dev, GFP_KERNEL);
Santosh Y6808c5f2014-05-29 10:01:52 +05301526 if (!iod)
1527 goto put_pages;
1528
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001529 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001530 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001531 for (i = 0; i < count; i++) {
1532 sg_set_page(&sg[i], pages[i],
Dan Carpenter5460fc02013-05-13 17:59:50 +03001533 min_t(unsigned, length, PAGE_SIZE - offset),
1534 offset);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001535 length -= (PAGE_SIZE - offset);
1536 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001537 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001538 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001539 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001540
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001541 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1542 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001543 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001544 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001545
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001546 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001547 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001548
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001549 free_iod:
1550 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001551 put_pages:
1552 for (i = 0; i < count; i++)
1553 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001554 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001555 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001556}
1557
Vishal Verma5d0f6132013-03-04 18:40:58 -07001558void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001559 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001560{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001561 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001562
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001563 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1564 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001565
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001566 for (i = 0; i < iod->nents; i++)
1567 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001568}
1569
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001570static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1571{
1572 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001573 struct nvme_user_io io;
1574 struct nvme_command c;
Keith Buschf410c682013-04-23 17:23:59 -06001575 unsigned length, meta_len;
1576 int status, i;
1577 struct nvme_iod *iod, *meta_iod = NULL;
1578 dma_addr_t meta_dma_addr;
1579 void *meta, *uninitialized_var(meta_mem);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001580
1581 if (copy_from_user(&io, uio, sizeof(io)))
1582 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001583 length = (io.nblocks + 1) << ns->lba_shift;
Keith Buschf410c682013-04-23 17:23:59 -06001584 meta_len = (io.nblocks + 1) * ns->ms;
1585
1586 if (meta_len && ((io.metadata & 3) || !io.metadata))
1587 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001588
1589 switch (io.opcode) {
1590 case nvme_cmd_write:
1591 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001592 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001593 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001594 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001595 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001596 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001597 }
1598
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001599 if (IS_ERR(iod))
1600 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001601
1602 memset(&c, 0, sizeof(c));
1603 c.rw.opcode = io.opcode;
1604 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001605 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001606 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001607 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001608 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001609 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1610 c.rw.reftag = cpu_to_le32(io.reftag);
1611 c.rw.apptag = cpu_to_le16(io.apptag);
1612 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buschf410c682013-04-23 17:23:59 -06001613
1614 if (meta_len) {
Keith Busch1b567492013-07-18 12:13:51 -06001615 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
1616 meta_len);
Keith Buschf410c682013-04-23 17:23:59 -06001617 if (IS_ERR(meta_iod)) {
1618 status = PTR_ERR(meta_iod);
1619 meta_iod = NULL;
1620 goto unmap;
1621 }
1622
1623 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1624 &meta_dma_addr, GFP_KERNEL);
1625 if (!meta_mem) {
1626 status = -ENOMEM;
1627 goto unmap;
1628 }
1629
1630 if (io.opcode & 1) {
1631 int meta_offset = 0;
1632
1633 for (i = 0; i < meta_iod->nents; i++) {
1634 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1635 meta_iod->sg[i].offset;
1636 memcpy(meta_mem + meta_offset, meta,
1637 meta_iod->sg[i].length);
1638 kunmap_atomic(meta);
1639 meta_offset += meta_iod->sg[i].length;
1640 }
1641 }
1642
1643 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1644 }
1645
Keith Buschedd10d32014-04-03 16:45:23 -06001646 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1647 c.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1648 c.rw.prp2 = cpu_to_le64(iod->first_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001649
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001650 if (length != (io.nblocks + 1) << ns->lba_shift)
1651 status = -ENOMEM;
1652 else
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001653 status = nvme_submit_io_cmd(dev, ns, &c, NULL);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001654
Keith Buschf410c682013-04-23 17:23:59 -06001655 if (meta_len) {
1656 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1657 int meta_offset = 0;
1658
1659 for (i = 0; i < meta_iod->nents; i++) {
1660 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1661 meta_iod->sg[i].offset;
1662 memcpy(meta, meta_mem + meta_offset,
1663 meta_iod->sg[i].length);
1664 kunmap_atomic(meta);
1665 meta_offset += meta_iod->sg[i].length;
1666 }
1667 }
1668
1669 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1670 meta_dma_addr);
1671 }
1672
1673 unmap:
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001674 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001675 nvme_free_iod(dev, iod);
Keith Buschf410c682013-04-23 17:23:59 -06001676
1677 if (meta_iod) {
1678 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1679 nvme_free_iod(dev, meta_iod);
1680 }
1681
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001682 return status;
1683}
1684
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001685static int nvme_user_cmd(struct nvme_dev *dev, struct nvme_ns *ns,
1686 struct nvme_passthru_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001687{
Keith Busch7963e522014-09-12 16:07:20 -06001688 struct nvme_passthru_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001689 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001690 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001691 struct nvme_iod *uninitialized_var(iod);
Keith Busch94f370c2013-05-09 14:01:38 -06001692 unsigned timeout;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001693
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001694 if (!capable(CAP_SYS_ADMIN))
1695 return -EACCES;
1696 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001697 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001698
1699 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001700 c.common.opcode = cmd.opcode;
1701 c.common.flags = cmd.flags;
1702 c.common.nsid = cpu_to_le32(cmd.nsid);
1703 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1704 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1705 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1706 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1707 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1708 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1709 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1710 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1711
1712 length = cmd.data_len;
1713 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001714 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1715 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001716 if (IS_ERR(iod))
1717 return PTR_ERR(iod);
Keith Buschedd10d32014-04-03 16:45:23 -06001718 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1719 c.common.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1720 c.common.prp2 = cpu_to_le64(iod->first_dma);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001721 }
1722
Keith Busch94f370c2013-05-09 14:01:38 -06001723 timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
1724 ADMIN_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001725
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001726 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001727 status = -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001728 else if (ns) {
1729 struct request *req;
1730
1731 req = blk_mq_alloc_request(ns->queue, WRITE,
1732 (GFP_KERNEL|__GFP_WAIT), false);
Jens Axboe97fe3832014-12-10 13:02:44 -07001733 if (IS_ERR(req))
1734 status = PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001735 else {
1736 status = nvme_submit_sync_cmd(req, &c, &cmd.result,
1737 timeout);
Jens Axboe9d135bb2014-11-17 10:43:42 -07001738 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001739 }
1740 } else
1741 status = __nvme_submit_admin_cmd(dev, &c, &cmd.result, timeout);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001742
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001743 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001744 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001745 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001746 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001747
Chayan Biswascf90bc42013-05-22 22:34:49 +00001748 if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
Keith Buschf4f117f2012-09-21 10:49:05 -06001749 sizeof(cmd.result)))
1750 status = -EFAULT;
1751
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001752 return status;
1753}
1754
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001755static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1756 unsigned long arg)
1757{
1758 struct nvme_ns *ns = bdev->bd_disk->private_data;
1759
1760 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001761 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001762 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001763 return ns->ns_id;
1764 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001765 return nvme_user_cmd(ns->dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06001766 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001767 return nvme_user_cmd(ns->dev, ns, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001768 case NVME_IOCTL_SUBMIT_IO:
1769 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001770 case SG_GET_VERSION_NUM:
1771 return nvme_sg_get_version_num((void __user *)arg);
1772 case SG_IO:
1773 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001774 default:
1775 return -ENOTTY;
1776 }
1777}
1778
Keith Busch320a3822013-10-23 13:07:34 -06001779#ifdef CONFIG_COMPAT
1780static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1781 unsigned int cmd, unsigned long arg)
1782{
Keith Busch320a3822013-10-23 13:07:34 -06001783 switch (cmd) {
1784 case SG_IO:
Keith Busche1797292014-08-27 13:55:38 -06001785 return -ENOIOCTLCMD;
Keith Busch320a3822013-10-23 13:07:34 -06001786 }
1787 return nvme_ioctl(bdev, mode, cmd, arg);
1788}
1789#else
1790#define nvme_compat_ioctl NULL
1791#endif
1792
Keith Busch9ac27092014-01-31 16:53:39 -07001793static int nvme_open(struct block_device *bdev, fmode_t mode)
1794{
Keith Busch9e603522014-10-03 11:15:47 -06001795 int ret = 0;
1796 struct nvme_ns *ns;
Keith Busch9ac27092014-01-31 16:53:39 -07001797
Keith Busch9e603522014-10-03 11:15:47 -06001798 spin_lock(&dev_list_lock);
1799 ns = bdev->bd_disk->private_data;
1800 if (!ns)
1801 ret = -ENXIO;
1802 else if (!kref_get_unless_zero(&ns->dev->kref))
1803 ret = -ENXIO;
1804 spin_unlock(&dev_list_lock);
1805
1806 return ret;
Keith Busch9ac27092014-01-31 16:53:39 -07001807}
1808
1809static void nvme_free_dev(struct kref *kref);
1810
1811static void nvme_release(struct gendisk *disk, fmode_t mode)
1812{
1813 struct nvme_ns *ns = disk->private_data;
1814 struct nvme_dev *dev = ns->dev;
1815
1816 kref_put(&dev->kref, nvme_free_dev);
1817}
1818
Keith Busch4cc09e22014-04-02 15:45:37 -06001819static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
1820{
1821 /* some standard values */
1822 geo->heads = 1 << 6;
1823 geo->sectors = 1 << 5;
1824 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1825 return 0;
1826}
1827
Keith Busch1b9dbf72014-09-10 17:21:14 -06001828static int nvme_revalidate_disk(struct gendisk *disk)
1829{
1830 struct nvme_ns *ns = disk->private_data;
1831 struct nvme_dev *dev = ns->dev;
1832 struct nvme_id_ns *id;
1833 dma_addr_t dma_addr;
1834 int lbaf;
1835
1836 id = dma_alloc_coherent(&dev->pci_dev->dev, 4096, &dma_addr,
1837 GFP_KERNEL);
1838 if (!id) {
1839 dev_warn(&dev->pci_dev->dev, "%s: Memory alocation failure\n",
1840 __func__);
1841 return 0;
1842 }
1843
1844 if (nvme_identify(dev, ns->ns_id, 0, dma_addr))
1845 goto free;
1846
1847 lbaf = id->flbas & 0xf;
1848 ns->lba_shift = id->lbaf[lbaf].ds;
1849
1850 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
1851 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1852 free:
1853 dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr);
1854 return 0;
1855}
1856
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001857static const struct block_device_operations nvme_fops = {
1858 .owner = THIS_MODULE,
1859 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06001860 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07001861 .open = nvme_open,
1862 .release = nvme_release,
Keith Busch4cc09e22014-04-02 15:45:37 -06001863 .getgeo = nvme_getgeo,
Keith Busch1b9dbf72014-09-10 17:21:14 -06001864 .revalidate_disk= nvme_revalidate_disk,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001865};
1866
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001867static int nvme_kthread(void *data)
1868{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001869 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001870
1871 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001872 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001873 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001874 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001875 int i;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001876 if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
1877 dev->initialized) {
1878 if (work_busy(&dev->reset_work))
1879 continue;
1880 list_del_init(&dev->node);
1881 dev_warn(&dev->pci_dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001882 "Failed status: %x, reset controller\n",
1883 readl(&dev->bar->csts));
Tejun Heo9ca97372014-03-07 10:24:49 -05001884 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001885 queue_work(nvme_workq, &dev->reset_work);
1886 continue;
1887 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001888 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001889 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001890 if (!nvmeq)
1891 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001892 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001893 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06001894
1895 while ((i == 0) && (dev->event_limit > 0)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001896 if (nvme_submit_async_admin_req(dev))
Keith Busch6fccf932014-06-18 13:58:57 -06001897 break;
1898 dev->event_limit--;
1899 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001900 spin_unlock_irq(&nvmeq->q_lock);
1901 }
1902 }
1903 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001904 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001905 }
1906 return 0;
1907}
1908
Keith Busch0e5e4f02012-11-09 16:33:05 -07001909static void nvme_config_discard(struct nvme_ns *ns)
1910{
1911 u32 logical_block_size = queue_logical_block_size(ns->queue);
1912 ns->queue->limits.discard_zeroes_data = 0;
1913 ns->queue->limits.discard_alignment = logical_block_size;
1914 ns->queue->limits.discard_granularity = logical_block_size;
1915 ns->queue->limits.max_discard_sectors = 0xffffffff;
1916 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1917}
1918
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001919static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001920 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1921{
1922 struct nvme_ns *ns;
1923 struct gendisk *disk;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001924 int node = dev_to_node(&dev->pci_dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001925 int lbaf;
1926
1927 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1928 return NULL;
1929
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001930 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001931 if (!ns)
1932 return NULL;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001933 ns->queue = blk_mq_init_queue(&dev->tagset);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001934 if (IS_ERR(ns->queue))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001935 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001936 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1937 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001938 queue_flag_set_unlocked(QUEUE_FLAG_SG_GAPS, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001939 ns->dev = dev;
1940 ns->queue->queuedata = ns;
1941
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001942 disk = alloc_disk_node(0, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001943 if (!disk)
1944 goto out_free_queue;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001945
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001946 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001947 ns->disk = disk;
1948 lbaf = id->flbas & 0xf;
1949 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Buschf410c682013-04-23 17:23:59 -06001950 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busche9ef4632012-07-24 15:01:04 -06001951 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06001952 if (dev->max_hw_sectors)
1953 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001954 if (dev->stripe_size)
1955 blk_queue_chunk_sectors(ns->queue, dev->stripe_size >> 9);
Keith Buscha7d2ce22014-04-29 11:41:28 -06001956 if (dev->vwc & NVME_CTRL_VWC_PRESENT)
1957 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001958
1959 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001960 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001961 disk->fops = &nvme_fops;
1962 disk->private_data = ns;
1963 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05001964 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox469071a2013-12-09 12:58:46 -05001965 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001966 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001967 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1968
Keith Busch0e5e4f02012-11-09 16:33:05 -07001969 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1970 nvme_config_discard(ns);
1971
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001972 return ns;
1973
1974 out_free_queue:
1975 blk_cleanup_queue(ns->queue);
1976 out_free_ns:
1977 kfree(ns);
1978 return NULL;
1979}
1980
Keith Busch42f61422014-03-24 10:46:25 -06001981static void nvme_create_io_queues(struct nvme_dev *dev)
1982{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001983 unsigned i;
Keith Busch42f61422014-03-24 10:46:25 -06001984
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001985 for (i = dev->queue_count; i <= dev->max_qid; i++)
Keith Busch2b25d982014-12-22 12:59:04 -07001986 if (!nvme_alloc_queue(dev, i, dev->q_depth))
Keith Busch42f61422014-03-24 10:46:25 -06001987 break;
1988
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001989 for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
1990 if (nvme_create_queue(dev->queues[i], i))
Keith Busch42f61422014-03-24 10:46:25 -06001991 break;
1992}
1993
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001994static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001995{
1996 int status;
1997 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001998 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001999
Matthew Wilcoxdf348132012-01-11 07:29:56 -07002000 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002001 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04002002 if (status < 0)
2003 return status;
2004 if (status > 0) {
2005 dev_err(&dev->pci_dev->dev, "Could not set queue count (%d)\n",
2006 status);
Keith Buschbadc34d2014-06-23 14:25:35 -06002007 return 0;
Matthew Wilcox27e81662014-04-11 11:58:45 -04002008 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002009 return min(result & 0xffff, result >> 16) + 1;
2010}
2011
Keith Busch9d713c22013-07-15 15:02:24 -06002012static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
2013{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002014 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06002015}
2016
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002017static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002018{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002019 struct nvme_queue *adminq = dev->queues[0];
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002020 struct pci_dev *pdev = dev->pci_dev;
Keith Busch42f61422014-03-24 10:46:25 -06002021 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002022
Keith Busch42f61422014-03-24 10:46:25 -06002023 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002024 result = set_queue_count(dev, nr_io_queues);
Keith Buschbadc34d2014-06-23 14:25:35 -06002025 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002026 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002027 if (result < nr_io_queues)
2028 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002029
Keith Busch9d713c22013-07-15 15:02:24 -06002030 size = db_bar_size(dev, nr_io_queues);
2031 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002032 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06002033 do {
2034 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
2035 if (dev->bar)
2036 break;
2037 if (!--nr_io_queues)
2038 return -ENOMEM;
2039 size = db_bar_size(dev, nr_io_queues);
2040 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002041 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07002042 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002043 }
2044
Keith Busch9d713c22013-07-15 15:02:24 -06002045 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002046 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002047
Jens Axboee32efbf2014-11-14 09:49:26 -07002048 /*
2049 * If we enable msix early due to not intx, disable it again before
2050 * setting up the full range we need.
2051 */
2052 if (!pdev->irq)
2053 pci_disable_msix(pdev);
2054
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002055 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002056 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002057 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
2058 if (vecs < 0) {
2059 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
2060 if (vecs < 0) {
2061 vecs = 1;
2062 } else {
2063 for (i = 0; i < vecs; i++)
2064 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002065 }
2066 }
2067
Matthew Wilcox063a8092013-06-20 10:53:48 -04002068 /*
2069 * Should investigate if there's a performance win from allocating
2070 * more queues than interrupt vectors; it might allow the submission
2071 * path to scale better, even if the receive path is limited by the
2072 * number of interrupts.
2073 */
2074 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002075 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002076
Matthew Wilcox3193f072014-01-27 15:57:22 -05002077 result = queue_request_irq(dev, adminq, adminq->irqname);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002078 if (result)
Keith Busch22404272013-07-15 15:02:20 -06002079 goto free_queues;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002080
Keith Buschcd638942013-07-15 15:02:23 -06002081 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002082 nvme_free_queues(dev, nr_io_queues + 1);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002083 nvme_create_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06002084
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002085 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002086
Keith Busch22404272013-07-15 15:02:20 -06002087 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002088 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002089 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002090}
2091
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002092/*
2093 * Return: error value if an error occurred setting up the queues or calling
2094 * Identify Device. 0 if these succeeded, even if adding some of the
2095 * namespaces failed. At the moment, these failures are silent. TBD which
2096 * failures should be reported.
2097 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002098static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002099{
Matthew Wilcox68608c262013-06-21 14:36:34 -04002100 struct pci_dev *pdev = dev->pci_dev;
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002101 int res;
2102 unsigned nn, i;
Keith Buschcbb62182013-05-01 13:07:49 -06002103 struct nvme_ns *ns;
Matthew Wilcox51814232011-02-01 16:18:08 -05002104 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002105 struct nvme_id_ns *id_ns;
2106 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002107 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06002108 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002109
Matthew Wilcox68608c262013-06-21 14:36:34 -04002110 mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06002111 if (!mem)
2112 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002113
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002114 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002115 if (res) {
Matthew Wilcox27e81662014-04-11 11:58:45 -04002116 dev_err(&pdev->dev, "Identify Controller failed (%d)\n", res);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002117 res = -EIO;
Keith Buschcbb62182013-05-01 13:07:49 -06002118 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002119 }
2120
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002121 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05002122 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002123 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002124 dev->abort_limit = ctrl->acl + 1;
Keith Buscha7d2ce22014-04-29 11:41:28 -06002125 dev->vwc = ctrl->vwc;
Keith Busch6fccf932014-06-18 13:58:57 -06002126 dev->event_limit = min(ctrl->aerl + 1, 8);
Matthew Wilcox51814232011-02-01 16:18:08 -05002127 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2128 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2129 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002130 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002131 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002132 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002133 (pdev->device == 0x0953) && ctrl->vs[3]) {
2134 unsigned int max_hw_sectors;
2135
Keith Busch159b67d2013-04-09 17:13:20 -06002136 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002137 max_hw_sectors = dev->stripe_size >> (shift - 9);
2138 if (dev->max_hw_sectors) {
2139 dev->max_hw_sectors = min(max_hw_sectors,
2140 dev->max_hw_sectors);
2141 } else
2142 dev->max_hw_sectors = max_hw_sectors;
2143 }
2144
2145 dev->tagset.ops = &nvme_mq_ops;
2146 dev->tagset.nr_hw_queues = dev->online_queues - 1;
2147 dev->tagset.timeout = NVME_IO_TIMEOUT;
2148 dev->tagset.numa_node = dev_to_node(&dev->pci_dev->dev);
2149 dev->tagset.queue_depth =
2150 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
2151 dev->tagset.cmd_size = sizeof(struct nvme_cmd_info);
2152 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
2153 dev->tagset.driver_data = dev;
2154
2155 if (blk_mq_alloc_tag_set(&dev->tagset))
2156 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002157
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002158 id_ns = mem;
Matthew Wilcox2b2c1892011-10-07 13:10:13 -04002159 for (i = 1; i <= nn; i++) {
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002160 res = nvme_identify(dev, i, 0, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002161 if (res)
2162 continue;
2163
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002164 if (id_ns->ncap == 0)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002165 continue;
2166
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002167 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
Keith Busch08df1e02012-09-21 10:52:13 -06002168 dma_addr + 4096, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002169 if (res)
Keith Busch12209032013-01-31 14:40:38 -07002170 memset(mem + 4096, 0, 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002171
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002172 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002173 if (ns)
2174 list_add_tail(&ns->list, &dev->namespaces);
2175 }
2176 list_for_each_entry(ns, &dev->namespaces, list)
2177 add_disk(ns->disk);
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002178 res = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002179
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002180 out:
Matthew Wilcox684f5c22011-09-19 17:14:53 -04002181 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002182 return res;
2183}
2184
Keith Busch0877cb02013-07-15 15:02:19 -06002185static int nvme_dev_map(struct nvme_dev *dev)
2186{
Keith Busch42f61422014-03-24 10:46:25 -06002187 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002188 int bars, result = -ENOMEM;
2189 struct pci_dev *pdev = dev->pci_dev;
2190
2191 if (pci_enable_device_mem(pdev))
2192 return result;
2193
2194 dev->entry[0].vector = pdev->irq;
2195 pci_set_master(pdev);
2196 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07002197 if (!bars)
2198 goto disable_pci;
2199
Keith Busch0877cb02013-07-15 15:02:19 -06002200 if (pci_request_selected_regions(pdev, bars, "nvme"))
2201 goto disable_pci;
2202
Russell King052d0ef2013-06-26 23:49:11 +01002203 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
2204 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2205 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002206
Keith Busch0877cb02013-07-15 15:02:19 -06002207 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2208 if (!dev->bar)
2209 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07002210
Keith Busch0e53d182013-12-10 13:10:39 -07002211 if (readl(&dev->bar->csts) == -1) {
2212 result = -ENODEV;
2213 goto unmap;
2214 }
Jens Axboee32efbf2014-11-14 09:49:26 -07002215
2216 /*
2217 * Some devices don't advertse INTx interrupts, pre-enable a single
2218 * MSIX vec for setup. We'll adjust this later.
2219 */
2220 if (!pdev->irq) {
2221 result = pci_enable_msix(pdev, dev->entry, 1);
2222 if (result < 0)
2223 goto unmap;
2224 }
2225
Keith Busch42f61422014-03-24 10:46:25 -06002226 cap = readq(&dev->bar->cap);
2227 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2228 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Keith Busch0877cb02013-07-15 15:02:19 -06002229 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2230
2231 return 0;
2232
Keith Busch0e53d182013-12-10 13:10:39 -07002233 unmap:
2234 iounmap(dev->bar);
2235 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002236 disable:
2237 pci_release_regions(pdev);
2238 disable_pci:
2239 pci_disable_device(pdev);
2240 return result;
2241}
2242
2243static void nvme_dev_unmap(struct nvme_dev *dev)
2244{
2245 if (dev->pci_dev->msi_enabled)
2246 pci_disable_msi(dev->pci_dev);
2247 else if (dev->pci_dev->msix_enabled)
2248 pci_disable_msix(dev->pci_dev);
2249
2250 if (dev->bar) {
2251 iounmap(dev->bar);
2252 dev->bar = NULL;
Keith Busch9a6b9452013-12-10 13:10:36 -07002253 pci_release_regions(dev->pci_dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002254 }
2255
Keith Busch0877cb02013-07-15 15:02:19 -06002256 if (pci_is_enabled(dev->pci_dev))
2257 pci_disable_device(dev->pci_dev);
2258}
2259
Keith Busch4d115422013-12-10 13:10:40 -07002260struct nvme_delq_ctx {
2261 struct task_struct *waiter;
2262 struct kthread_worker *worker;
2263 atomic_t refcount;
2264};
2265
2266static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2267{
2268 dq->waiter = current;
2269 mb();
2270
2271 for (;;) {
2272 set_current_state(TASK_KILLABLE);
2273 if (!atomic_read(&dq->refcount))
2274 break;
2275 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2276 fatal_signal_pending(current)) {
Keith Busch0fb59cb2015-01-07 18:55:50 -07002277 /*
2278 * Disable the controller first since we can't trust it
2279 * at this point, but leave the admin queue enabled
2280 * until all queue deletion requests are flushed.
2281 * FIXME: This may take a while if there are more h/w
2282 * queues than admin tags.
2283 */
Keith Busch4d115422013-12-10 13:10:40 -07002284 set_current_state(TASK_RUNNING);
Keith Busch4d115422013-12-10 13:10:40 -07002285 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
Keith Busch0fb59cb2015-01-07 18:55:50 -07002286 nvme_clear_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07002287 flush_kthread_worker(dq->worker);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002288 nvme_disable_queue(dev, 0);
Keith Busch4d115422013-12-10 13:10:40 -07002289 return;
2290 }
2291 }
2292 set_current_state(TASK_RUNNING);
2293}
2294
2295static void nvme_put_dq(struct nvme_delq_ctx *dq)
2296{
2297 atomic_dec(&dq->refcount);
2298 if (dq->waiter)
2299 wake_up_process(dq->waiter);
2300}
2301
2302static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2303{
2304 atomic_inc(&dq->refcount);
2305 return dq;
2306}
2307
2308static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2309{
2310 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
2311
2312 nvme_clear_queue(nvmeq);
2313 nvme_put_dq(dq);
2314}
2315
2316static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2317 kthread_work_func_t fn)
2318{
2319 struct nvme_command c;
2320
2321 memset(&c, 0, sizeof(c));
2322 c.delete_queue.opcode = opcode;
2323 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2324
2325 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002326 return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
2327 ADMIN_TIMEOUT);
Keith Busch4d115422013-12-10 13:10:40 -07002328}
2329
2330static void nvme_del_cq_work_handler(struct kthread_work *work)
2331{
2332 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2333 cmdinfo.work);
2334 nvme_del_queue_end(nvmeq);
2335}
2336
2337static int nvme_delete_cq(struct nvme_queue *nvmeq)
2338{
2339 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2340 nvme_del_cq_work_handler);
2341}
2342
2343static void nvme_del_sq_work_handler(struct kthread_work *work)
2344{
2345 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2346 cmdinfo.work);
2347 int status = nvmeq->cmdinfo.status;
2348
2349 if (!status)
2350 status = nvme_delete_cq(nvmeq);
2351 if (status)
2352 nvme_del_queue_end(nvmeq);
2353}
2354
2355static int nvme_delete_sq(struct nvme_queue *nvmeq)
2356{
2357 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2358 nvme_del_sq_work_handler);
2359}
2360
2361static void nvme_del_queue_start(struct kthread_work *work)
2362{
2363 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2364 cmdinfo.work);
Keith Busch4d115422013-12-10 13:10:40 -07002365 if (nvme_delete_sq(nvmeq))
2366 nvme_del_queue_end(nvmeq);
2367}
2368
2369static void nvme_disable_io_queues(struct nvme_dev *dev)
2370{
2371 int i;
2372 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2373 struct nvme_delq_ctx dq;
2374 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2375 &worker, "nvme%d", dev->instance);
2376
2377 if (IS_ERR(kworker_task)) {
2378 dev_err(&dev->pci_dev->dev,
2379 "Failed to create queue del task\n");
2380 for (i = dev->queue_count - 1; i > 0; i--)
2381 nvme_disable_queue(dev, i);
2382 return;
2383 }
2384
2385 dq.waiter = NULL;
2386 atomic_set(&dq.refcount, 0);
2387 dq.worker = &worker;
2388 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002389 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002390
2391 if (nvme_suspend_queue(nvmeq))
2392 continue;
2393 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2394 nvmeq->cmdinfo.worker = dq.worker;
2395 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2396 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2397 }
2398 nvme_wait_dq(&dq, dev);
2399 kthread_stop(kworker_task);
2400}
2401
Dan McLeranb9afca32014-04-07 17:10:11 -06002402/*
2403* Remove the node from the device list and check
2404* for whether or not we need to stop the nvme_thread.
2405*/
2406static void nvme_dev_list_remove(struct nvme_dev *dev)
2407{
2408 struct task_struct *tmp = NULL;
2409
2410 spin_lock(&dev_list_lock);
2411 list_del_init(&dev->node);
2412 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2413 tmp = nvme_thread;
2414 nvme_thread = NULL;
2415 }
2416 spin_unlock(&dev_list_lock);
2417
2418 if (tmp)
2419 kthread_stop(tmp);
2420}
2421
Keith Buschc9d3bf82015-01-07 18:55:52 -07002422static void nvme_freeze_queues(struct nvme_dev *dev)
2423{
2424 struct nvme_ns *ns;
2425
2426 list_for_each_entry(ns, &dev->namespaces, list) {
2427 blk_mq_freeze_queue_start(ns->queue);
2428
2429 spin_lock(ns->queue->queue_lock);
2430 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
2431 spin_unlock(ns->queue->queue_lock);
2432
2433 blk_mq_cancel_requeue_work(ns->queue);
2434 blk_mq_stop_hw_queues(ns->queue);
2435 }
2436}
2437
2438static void nvme_unfreeze_queues(struct nvme_dev *dev)
2439{
2440 struct nvme_ns *ns;
2441
2442 list_for_each_entry(ns, &dev->namespaces, list) {
2443 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2444 blk_mq_unfreeze_queue(ns->queue);
2445 blk_mq_start_stopped_hw_queues(ns->queue, true);
2446 blk_mq_kick_requeue_list(ns->queue);
2447 }
2448}
2449
Keith Buschf0b50732013-07-15 15:02:21 -06002450static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002451{
Keith Busch22404272013-07-15 15:02:20 -06002452 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06002453 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06002454
Keith Buschd4b4ff82013-12-10 13:10:37 -07002455 dev->initialized = 0;
Dan McLeranb9afca32014-04-07 17:10:11 -06002456 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002457
Keith Buschc9d3bf82015-01-07 18:55:52 -07002458 if (dev->bar) {
2459 nvme_freeze_queues(dev);
Keith Busch7c1b2452014-06-25 11:18:12 -06002460 csts = readl(&dev->bar->csts);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002461 }
Keith Busch7c1b2452014-06-25 11:18:12 -06002462 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07002463 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002464 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002465 nvme_suspend_queue(nvmeq);
2466 nvme_clear_queue(nvmeq);
2467 }
2468 } else {
2469 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002470 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002471 nvme_disable_queue(dev, 0);
2472 }
Keith Buschf0b50732013-07-15 15:02:21 -06002473 nvme_dev_unmap(dev);
2474}
2475
2476static void nvme_dev_remove(struct nvme_dev *dev)
2477{
Keith Busch9ac27092014-01-31 16:53:39 -07002478 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002479
Keith Busch9ac27092014-01-31 16:53:39 -07002480 list_for_each_entry(ns, &dev->namespaces, list) {
2481 if (ns->disk->flags & GENHD_FL_UP)
2482 del_gendisk(ns->disk);
Keith Buschcef6a942015-01-07 18:55:51 -07002483 if (!blk_queue_dying(ns->queue)) {
2484 blk_mq_abort_requeue_list(ns->queue);
Keith Busch9ac27092014-01-31 16:53:39 -07002485 blk_cleanup_queue(ns->queue);
Keith Buschcef6a942015-01-07 18:55:51 -07002486 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002487 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002488}
2489
Matthew Wilcox091b6092011-02-10 09:56:01 -05002490static int nvme_setup_prp_pools(struct nvme_dev *dev)
2491{
2492 struct device *dmadev = &dev->pci_dev->dev;
2493 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2494 PAGE_SIZE, PAGE_SIZE, 0);
2495 if (!dev->prp_page_pool)
2496 return -ENOMEM;
2497
Matthew Wilcox99802a72011-02-10 10:30:34 -05002498 /* Optimisation for I/Os between 4k and 128k */
2499 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2500 256, 256, 0);
2501 if (!dev->prp_small_pool) {
2502 dma_pool_destroy(dev->prp_page_pool);
2503 return -ENOMEM;
2504 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002505 return 0;
2506}
2507
2508static void nvme_release_prp_pools(struct nvme_dev *dev)
2509{
2510 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002511 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002512}
2513
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002514static DEFINE_IDA(nvme_instance_ida);
2515
2516static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002517{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002518 int instance, error;
2519
2520 do {
2521 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2522 return -ENODEV;
2523
2524 spin_lock(&dev_list_lock);
2525 error = ida_get_new(&nvme_instance_ida, &instance);
2526 spin_unlock(&dev_list_lock);
2527 } while (error == -EAGAIN);
2528
2529 if (error)
2530 return -ENODEV;
2531
2532 dev->instance = instance;
2533 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002534}
2535
2536static void nvme_release_instance(struct nvme_dev *dev)
2537{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002538 spin_lock(&dev_list_lock);
2539 ida_remove(&nvme_instance_ida, dev->instance);
2540 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002541}
2542
Keith Busch9ac27092014-01-31 16:53:39 -07002543static void nvme_free_namespaces(struct nvme_dev *dev)
2544{
2545 struct nvme_ns *ns, *next;
2546
2547 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2548 list_del(&ns->list);
Keith Busch9e603522014-10-03 11:15:47 -06002549
2550 spin_lock(&dev_list_lock);
2551 ns->disk->private_data = NULL;
2552 spin_unlock(&dev_list_lock);
2553
Keith Busch9ac27092014-01-31 16:53:39 -07002554 put_disk(ns->disk);
2555 kfree(ns);
2556 }
2557}
2558
Keith Busch5e82e952013-02-19 10:17:58 -07002559static void nvme_free_dev(struct kref *kref)
2560{
2561 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002562
Keith Buscha96d4f52014-08-19 19:15:59 -06002563 pci_dev_put(dev->pci_dev);
Keith Busch9ac27092014-01-31 16:53:39 -07002564 nvme_free_namespaces(dev);
Indraneel M285dffc2014-12-11 08:24:18 -07002565 nvme_release_instance(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002566 blk_mq_free_tag_set(&dev->tagset);
Keith Buschea191d22015-01-07 18:55:49 -07002567 blk_put_queue(dev->admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002568 kfree(dev->queues);
2569 kfree(dev->entry);
2570 kfree(dev);
2571}
2572
2573static int nvme_dev_open(struct inode *inode, struct file *f)
2574{
2575 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
2576 miscdev);
2577 kref_get(&dev->kref);
2578 f->private_data = dev;
2579 return 0;
2580}
2581
2582static int nvme_dev_release(struct inode *inode, struct file *f)
2583{
2584 struct nvme_dev *dev = f->private_data;
2585 kref_put(&dev->kref, nvme_free_dev);
2586 return 0;
2587}
2588
2589static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2590{
2591 struct nvme_dev *dev = f->private_data;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002592 struct nvme_ns *ns;
2593
Keith Busch5e82e952013-02-19 10:17:58 -07002594 switch (cmd) {
2595 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002596 return nvme_user_cmd(dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06002597 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002598 if (list_empty(&dev->namespaces))
2599 return -ENOTTY;
2600 ns = list_first_entry(&dev->namespaces, struct nvme_ns, list);
2601 return nvme_user_cmd(dev, ns, (void __user *)arg);
Keith Busch5e82e952013-02-19 10:17:58 -07002602 default:
2603 return -ENOTTY;
2604 }
2605}
2606
2607static const struct file_operations nvme_dev_fops = {
2608 .owner = THIS_MODULE,
2609 .open = nvme_dev_open,
2610 .release = nvme_dev_release,
2611 .unlocked_ioctl = nvme_dev_ioctl,
2612 .compat_ioctl = nvme_dev_ioctl,
2613};
2614
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002615static void nvme_set_irq_hints(struct nvme_dev *dev)
2616{
2617 struct nvme_queue *nvmeq;
2618 int i;
2619
2620 for (i = 0; i < dev->online_queues; i++) {
2621 nvmeq = dev->queues[i];
2622
2623 if (!nvmeq->hctx)
2624 continue;
2625
2626 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
2627 nvmeq->hctx->cpumask);
2628 }
2629}
2630
Keith Buschf0b50732013-07-15 15:02:21 -06002631static int nvme_dev_start(struct nvme_dev *dev)
2632{
2633 int result;
Dan McLeranb9afca32014-04-07 17:10:11 -06002634 bool start_thread = false;
Keith Buschf0b50732013-07-15 15:02:21 -06002635
2636 result = nvme_dev_map(dev);
2637 if (result)
2638 return result;
2639
2640 result = nvme_configure_admin_queue(dev);
2641 if (result)
2642 goto unmap;
2643
2644 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002645 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2646 start_thread = true;
2647 nvme_thread = NULL;
2648 }
Keith Buschf0b50732013-07-15 15:02:21 -06002649 list_add(&dev->node, &dev_list);
2650 spin_unlock(&dev_list_lock);
2651
Dan McLeranb9afca32014-04-07 17:10:11 -06002652 if (start_thread) {
2653 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
Keith Busch387caa52014-09-22 13:46:19 -06002654 wake_up_all(&nvme_kthread_wait);
Dan McLeranb9afca32014-04-07 17:10:11 -06002655 } else
2656 wait_event_killable(nvme_kthread_wait, nvme_thread);
2657
2658 if (IS_ERR_OR_NULL(nvme_thread)) {
2659 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2660 goto disable;
2661 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002662
2663 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002664 result = nvme_alloc_admin_tags(dev);
2665 if (result)
2666 goto disable;
Dan McLeranb9afca32014-04-07 17:10:11 -06002667
Keith Buschf0b50732013-07-15 15:02:21 -06002668 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002669 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07002670 goto free_tags;
Keith Buschf0b50732013-07-15 15:02:21 -06002671
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002672 nvme_set_irq_hints(dev);
2673
Keith Buschd82e8bf2013-09-05 14:45:07 -06002674 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002675
Keith Busch0fb59cb2015-01-07 18:55:50 -07002676 free_tags:
2677 nvme_dev_remove_admin(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002678 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002679 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06002680 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002681 unmap:
2682 nvme_dev_unmap(dev);
2683 return result;
2684}
2685
Keith Busch9a6b9452013-12-10 13:10:36 -07002686static int nvme_remove_dead_ctrl(void *arg)
2687{
2688 struct nvme_dev *dev = (struct nvme_dev *)arg;
2689 struct pci_dev *pdev = dev->pci_dev;
2690
2691 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06002692 pci_stop_and_remove_bus_device_locked(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002693 kref_put(&dev->kref, nvme_free_dev);
2694 return 0;
2695}
2696
2697static void nvme_remove_disks(struct work_struct *ws)
2698{
Keith Busch9a6b9452013-12-10 13:10:36 -07002699 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2700
Keith Busch5a92e702014-02-21 14:13:44 -07002701 nvme_free_queues(dev, 1);
Keith Busch302c6722014-07-18 11:40:20 -06002702 nvme_dev_remove(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002703}
2704
2705static int nvme_dev_resume(struct nvme_dev *dev)
2706{
2707 int ret;
2708
2709 ret = nvme_dev_start(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002710 if (ret)
Keith Busch9a6b9452013-12-10 13:10:36 -07002711 return ret;
Keith Buschbadc34d2014-06-23 14:25:35 -06002712 if (dev->online_queues < 2) {
Keith Busch9a6b9452013-12-10 13:10:36 -07002713 spin_lock(&dev_list_lock);
Tejun Heo9ca97372014-03-07 10:24:49 -05002714 dev->reset_workfn = nvme_remove_disks;
Keith Busch9a6b9452013-12-10 13:10:36 -07002715 queue_work(nvme_workq, &dev->reset_work);
2716 spin_unlock(&dev_list_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002717 } else {
2718 nvme_unfreeze_queues(dev);
2719 nvme_set_irq_hints(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002720 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002721 dev->initialized = 1;
Keith Busch9a6b9452013-12-10 13:10:36 -07002722 return 0;
2723}
2724
2725static void nvme_dev_reset(struct nvme_dev *dev)
2726{
2727 nvme_dev_shutdown(dev);
2728 if (nvme_dev_resume(dev)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002729 dev_warn(&dev->pci_dev->dev, "Device failed to resume\n");
Keith Busch9a6b9452013-12-10 13:10:36 -07002730 kref_get(&dev->kref);
2731 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2732 dev->instance))) {
2733 dev_err(&dev->pci_dev->dev,
2734 "Failed to start controller remove task\n");
2735 kref_put(&dev->kref, nvme_free_dev);
2736 }
2737 }
2738}
2739
2740static void nvme_reset_failed_dev(struct work_struct *ws)
2741{
2742 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2743 nvme_dev_reset(dev);
2744}
2745
Tejun Heo9ca97372014-03-07 10:24:49 -05002746static void nvme_reset_workfn(struct work_struct *work)
2747{
2748 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
2749 dev->reset_workfn(work);
2750}
2751
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002752static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002753{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002754 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002755 struct nvme_dev *dev;
2756
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002757 node = dev_to_node(&pdev->dev);
2758 if (node == NUMA_NO_NODE)
2759 set_dev_node(&pdev->dev, 0);
2760
2761 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002762 if (!dev)
2763 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002764 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
2765 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002766 if (!dev->entry)
2767 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002768 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2769 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002770 if (!dev->queues)
2771 goto free;
2772
2773 INIT_LIST_HEAD(&dev->namespaces);
Tejun Heo9ca97372014-03-07 10:24:49 -05002774 dev->reset_workfn = nvme_reset_failed_dev;
2775 INIT_WORK(&dev->reset_work, nvme_reset_workfn);
Keith Buscha96d4f52014-08-19 19:15:59 -06002776 dev->pci_dev = pci_dev_get(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002777 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002778 result = nvme_set_instance(dev);
2779 if (result)
Keith Buscha96d4f52014-08-19 19:15:59 -06002780 goto put_pci;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002781
Matthew Wilcox091b6092011-02-10 09:56:01 -05002782 result = nvme_setup_prp_pools(dev);
2783 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002784 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002785
Keith Buschfb35e912014-03-03 11:09:47 -07002786 kref_init(&dev->kref);
Keith Buschf0b50732013-07-15 15:02:21 -06002787 result = nvme_dev_start(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002788 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002789 goto release_pools;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002790
Keith Buschbadc34d2014-06-23 14:25:35 -06002791 if (dev->online_queues > 1)
2792 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002793 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002794 goto shutdown;
Matthew Wilcox740216f2011-02-15 16:28:20 -05002795
Keith Busch5e82e952013-02-19 10:17:58 -07002796 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
2797 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2798 dev->miscdev.parent = &pdev->dev;
2799 dev->miscdev.name = dev->name;
2800 dev->miscdev.fops = &nvme_dev_fops;
2801 result = misc_register(&dev->miscdev);
2802 if (result)
2803 goto remove;
2804
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002805 nvme_set_irq_hints(dev);
2806
Keith Buschd4b4ff82013-12-10 13:10:37 -07002807 dev->initialized = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002808 return 0;
2809
Keith Busch5e82e952013-02-19 10:17:58 -07002810 remove:
2811 nvme_dev_remove(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002812 nvme_dev_remove_admin(dev);
Keith Busch9ac27092014-01-31 16:53:39 -07002813 nvme_free_namespaces(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002814 shutdown:
2815 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002816 release_pools:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002817 nvme_free_queues(dev, 0);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002818 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002819 release:
2820 nvme_release_instance(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002821 put_pci:
2822 pci_dev_put(dev->pci_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002823 free:
2824 kfree(dev->queues);
2825 kfree(dev->entry);
2826 kfree(dev);
2827 return result;
2828}
2829
Keith Buschf0d54a52014-05-02 10:40:43 -06002830static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2831{
Keith Buscha6739472014-06-23 16:03:21 -06002832 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002833
Keith Buscha6739472014-06-23 16:03:21 -06002834 if (prepare)
2835 nvme_dev_shutdown(dev);
2836 else
2837 nvme_dev_resume(dev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002838}
2839
Keith Busch09ece142014-01-27 11:29:40 -05002840static void nvme_shutdown(struct pci_dev *pdev)
2841{
2842 struct nvme_dev *dev = pci_get_drvdata(pdev);
2843 nvme_dev_shutdown(dev);
2844}
2845
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002846static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002847{
2848 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002849
2850 spin_lock(&dev_list_lock);
2851 list_del_init(&dev->node);
2852 spin_unlock(&dev_list_lock);
2853
2854 pci_set_drvdata(pdev, NULL);
2855 flush_work(&dev->reset_work);
Keith Busch5e82e952013-02-19 10:17:58 -07002856 misc_deregister(&dev->miscdev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002857 nvme_dev_shutdown(dev);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002858 nvme_dev_remove(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002859 nvme_dev_remove_admin(dev);
2860 nvme_free_queues(dev, 0);
Keith Busch9a6b9452013-12-10 13:10:36 -07002861 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002862 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002863}
2864
2865/* These functions are yet to be implemented */
2866#define nvme_error_detected NULL
2867#define nvme_dump_registers NULL
2868#define nvme_link_reset NULL
2869#define nvme_slot_reset NULL
2870#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002871
Jingoo Han671a6012014-02-13 11:19:14 +09002872#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002873static int nvme_suspend(struct device *dev)
2874{
2875 struct pci_dev *pdev = to_pci_dev(dev);
2876 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2877
2878 nvme_dev_shutdown(ndev);
2879 return 0;
2880}
2881
2882static int nvme_resume(struct device *dev)
2883{
2884 struct pci_dev *pdev = to_pci_dev(dev);
2885 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002886
Keith Busch9a6b9452013-12-10 13:10:36 -07002887 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Tejun Heo9ca97372014-03-07 10:24:49 -05002888 ndev->reset_workfn = nvme_reset_failed_dev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002889 queue_work(nvme_workq, &ndev->reset_work);
2890 }
2891 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002892}
Jingoo Han671a6012014-02-13 11:19:14 +09002893#endif
Keith Buschcd638942013-07-15 15:02:23 -06002894
2895static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002896
Stephen Hemminger1d352032012-09-07 09:33:17 -07002897static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002898 .error_detected = nvme_error_detected,
2899 .mmio_enabled = nvme_dump_registers,
2900 .link_reset = nvme_link_reset,
2901 .slot_reset = nvme_slot_reset,
2902 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06002903 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002904};
2905
2906/* Move to pci_ids.h later */
2907#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2908
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002909static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002910 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2911 { 0, }
2912};
2913MODULE_DEVICE_TABLE(pci, nvme_id_table);
2914
2915static struct pci_driver nvme_driver = {
2916 .name = "nvme",
2917 .id_table = nvme_id_table,
2918 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002919 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002920 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002921 .driver = {
2922 .pm = &nvme_dev_pm_ops,
2923 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002924 .err_handler = &nvme_err_handler,
2925};
2926
2927static int __init nvme_init(void)
2928{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002929 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002930
Dan McLeranb9afca32014-04-07 17:10:11 -06002931 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002932
Keith Busch9a6b9452013-12-10 13:10:36 -07002933 nvme_workq = create_singlethread_workqueue("nvme");
2934 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06002935 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07002936
Keith Busch5c42ea12012-07-25 16:05:18 -06002937 result = register_blkdev(nvme_major, "nvme");
2938 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002939 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06002940 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002941 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002942
Keith Buschf3db22f2014-06-11 11:51:35 -06002943 result = pci_register_driver(&nvme_driver);
2944 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002945 goto unregister_blkdev;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002946 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002947
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002948 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002949 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002950 kill_workq:
2951 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002952 return result;
2953}
2954
2955static void __exit nvme_exit(void)
2956{
2957 pci_unregister_driver(&nvme_driver);
Keith Buschf3db22f2014-06-11 11:51:35 -06002958 unregister_hotcpu_notifier(&nvme_nb);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002959 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07002960 destroy_workqueue(nvme_workq);
Dan McLeranb9afca32014-04-07 17:10:11 -06002961 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002962 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002963}
2964
2965MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2966MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07002967MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002968module_init(nvme_init);
2969module_exit(nvme_exit);