blob: cae7cac6cc439cec7c35fa7b2ad98d87d2fd5052 [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003 * Copyright (c) 2011-2014, Intel Corporation.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050013 */
14
15#include <linux/nvme.h>
Matthew Wilcox8de05532011-05-12 13:50:28 -040016#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050017#include <linux/blkdev.h>
Matias Bjørlinga4aea562014-11-04 08:20:14 -070018#include <linux/blk-mq.h>
Keith Busch42f61422014-03-24 10:46:25 -060019#include <linux/cpu.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040020#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050021#include <linux/errno.h>
22#include <linux/fs.h>
23#include <linux/genhd.h>
Keith Busch4cc09e22014-04-02 15:45:37 -060024#include <linux/hdreg.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040025#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050026#include <linux/init.h>
27#include <linux/interrupt.h>
28#include <linux/io.h>
29#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050030#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050031#include <linux/kernel.h>
32#include <linux/mm.h>
33#include <linux/module.h>
34#include <linux/moduleparam.h>
35#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050036#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040037#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050038#include <linux/sched.h>
39#include <linux/slab.h>
Keith Busche1e5e562015-02-19 13:39:03 -070040#include <linux/t10-pi.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050041#include <linux/types.h>
Vishal Verma5d0f6132013-03-04 18:40:58 -070042#include <scsi/sg.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090043#include <asm-generic/io-64-nonatomic-lo-hi.h>
44
Keith Buschb3fffde2015-02-03 11:21:42 -070045#define NVME_MINORS (1U << MINORBITS)
Keith Busch9d43cf62014-05-13 11:42:02 -060046#define NVME_Q_DEPTH 1024
Jens Axboed31af0a2015-03-06 12:56:13 -070047#define NVME_AQ_DEPTH 256
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050048#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
49#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Keith Busch9d43cf62014-05-13 11:42:02 -060050#define ADMIN_TIMEOUT (admin_timeout * HZ)
Dan McLeran2484f402014-07-01 09:33:32 -060051#define SHUTDOWN_TIMEOUT (shutdown_timeout * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050052
Keith Busch9d43cf62014-05-13 11:42:02 -060053static unsigned char admin_timeout = 60;
54module_param(admin_timeout, byte, 0644);
55MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050056
Matthew Wilcoxbd676082014-06-03 23:04:30 -040057unsigned char nvme_io_timeout = 30;
58module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
Keith Buschb3550842014-04-04 11:43:36 -060059MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050060
Dan McLeran2484f402014-07-01 09:33:32 -060061static unsigned char shutdown_timeout = 5;
62module_param(shutdown_timeout, byte, 0644);
63MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
64
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050065static int nvme_major;
66module_param(nvme_major, int, 0);
67
Keith Buschb3fffde2015-02-03 11:21:42 -070068static int nvme_char_major;
69module_param(nvme_char_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;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050079
Keith Buschb3fffde2015-02-03 11:21:42 -070080static struct class *nvme_class;
81
Keith Buschd4b4ff82013-12-10 13:10:37 -070082static void nvme_reset_failed_dev(struct work_struct *ws);
Keith Busch4cc06522015-06-05 10:30:08 -060083static int nvme_reset(struct nvme_dev *dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -070084static int nvme_process_cq(struct nvme_queue *nvmeq);
Keith Buschd4b4ff82013-12-10 13:10:37 -070085
Keith Busch4d115422013-12-10 13:10:40 -070086struct async_cmd_info {
87 struct kthread_work work;
88 struct kthread_worker *worker;
Matias Bjørlinga4aea562014-11-04 08:20:14 -070089 struct request *req;
Keith Busch4d115422013-12-10 13:10:40 -070090 u32 result;
91 int status;
92 void *ctx;
93};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050094
95/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050096 * An NVM Express queue. Each device has at least two (one for admin
97 * commands and one for I/O commands).
98 */
99struct nvme_queue {
100 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500101 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500102 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500103 spinlock_t q_lock;
104 struct nvme_command *sq_cmds;
105 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600106 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500107 dma_addr_t sq_dma_addr;
108 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500109 u32 __iomem *q_db;
110 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700111 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500112 u16 sq_head;
113 u16 sq_tail;
114 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700115 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400116 u8 cq_phase;
117 u8 cqe_seen;
Keith Busch4d115422013-12-10 13:10:40 -0700118 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500119};
120
121/*
122 * Check we didin't inadvertently grow the command struct
123 */
124static inline void _nvme_check_size(void)
125{
126 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
127 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
128 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
129 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
130 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400131 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700132 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500133 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
134 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
135 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
136 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600137 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500138}
139
Keith Buschedd10d32014-04-03 16:45:23 -0600140typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400141 struct nvme_completion *);
142
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500143struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400144 nvme_completion_fn fn;
145 void *ctx;
Keith Buschc30341d2013-12-10 13:10:38 -0700146 int aborted;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700147 struct nvme_queue *nvmeq;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700148 struct nvme_iod iod[0];
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500149};
150
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700151/*
152 * Max size of iod being embedded in the request payload
153 */
154#define NVME_INT_PAGES 2
155#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->page_size)
Chong Yuanfda631f2015-03-27 09:21:32 +0800156#define NVME_INT_MASK 0x01
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700157
158/*
159 * Will slightly overestimate the number of pages needed. This is OK
160 * as it only leads to a small amount of wasted memory for the lifetime of
161 * the I/O.
162 */
163static int nvme_npages(unsigned size, struct nvme_dev *dev)
164{
165 unsigned nprps = DIV_ROUND_UP(size + dev->page_size, dev->page_size);
166 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
167}
168
169static unsigned int nvme_cmd_size(struct nvme_dev *dev)
170{
171 unsigned int ret = sizeof(struct nvme_cmd_info);
172
173 ret += sizeof(struct nvme_iod);
174 ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
175 ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
176
177 return ret;
178}
179
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700180static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
181 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500182{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700183 struct nvme_dev *dev = data;
184 struct nvme_queue *nvmeq = dev->queues[0];
185
Keith Busch42483222015-06-01 09:29:54 -0600186 WARN_ON(hctx_idx != 0);
187 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
188 WARN_ON(nvmeq->tags);
189
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700190 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600191 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700192 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500193}
194
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700195static int nvme_admin_init_request(void *data, struct request *req,
196 unsigned int hctx_idx, unsigned int rq_idx,
197 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600198{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700199 struct nvme_dev *dev = data;
200 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
201 struct nvme_queue *nvmeq = dev->queues[0];
202
203 BUG_ON(!nvmeq);
204 cmd->nvmeq = nvmeq;
205 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600206}
207
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700208static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
209 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500210{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700211 struct nvme_dev *dev = data;
Keith Busch42483222015-06-01 09:29:54 -0600212 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500213
Keith Busch42483222015-06-01 09:29:54 -0600214 if (!nvmeq->tags)
215 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500216
Keith Busch42483222015-06-01 09:29:54 -0600217 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700218 hctx->driver_data = nvmeq;
219 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500220}
221
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700222static int nvme_init_request(void *data, struct request *req,
223 unsigned int hctx_idx, unsigned int rq_idx,
224 unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500225{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700226 struct nvme_dev *dev = data;
227 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
228 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
229
230 BUG_ON(!nvmeq);
231 cmd->nvmeq = nvmeq;
232 return 0;
233}
234
235static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
236 nvme_completion_fn handler)
237{
238 cmd->fn = handler;
239 cmd->ctx = ctx;
240 cmd->aborted = 0;
Keith Buschc917dfe2015-01-07 18:55:48 -0700241 blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500242}
243
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700244static void *iod_get_private(struct nvme_iod *iod)
245{
246 return (void *) (iod->private & ~0x1UL);
247}
248
249/*
250 * If bit 0 is set, the iod is embedded in the request payload.
251 */
252static bool iod_should_kfree(struct nvme_iod *iod)
253{
Chong Yuanfda631f2015-03-27 09:21:32 +0800254 return (iod->private & NVME_INT_MASK) == 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700255}
256
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400257/* Special values must be less than 0x1000 */
258#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500259#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
260#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
261#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500262
Keith Buschedd10d32014-04-03 16:45:23 -0600263static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400264 struct nvme_completion *cqe)
265{
266 if (ctx == CMD_CTX_CANCELLED)
267 return;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400268 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600269 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400270 "completed id %d twice on queue %d\n",
271 cqe->command_id, le16_to_cpup(&cqe->sq_id));
272 return;
273 }
274 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600275 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400276 "invalid id %d completed on queue %d\n",
277 cqe->command_id, le16_to_cpup(&cqe->sq_id));
278 return;
279 }
Keith Buschedd10d32014-04-03 16:45:23 -0600280 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400281}
282
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700283static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
284{
285 void *ctx;
286
287 if (fn)
288 *fn = cmd->fn;
289 ctx = cmd->ctx;
290 cmd->fn = special_completion;
291 cmd->ctx = CMD_CTX_CANCELLED;
292 return ctx;
293}
294
295static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
296 struct nvme_completion *cqe)
297{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700298 u32 result = le32_to_cpup(&cqe->result);
299 u16 status = le16_to_cpup(&cqe->status) >> 1;
300
301 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
302 ++nvmeq->dev->event_limit;
303 if (status == NVME_SC_SUCCESS)
304 dev_warn(nvmeq->q_dmadev,
305 "async event result %08x\n", result);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700306}
307
308static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
309 struct nvme_completion *cqe)
310{
311 struct request *req = ctx;
312
313 u16 status = le16_to_cpup(&cqe->status) >> 1;
314 u32 result = le32_to_cpup(&cqe->result);
315
Keith Busch42483222015-06-01 09:29:54 -0600316 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700317
318 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
319 ++nvmeq->dev->abort_limit;
320}
321
Keith Buschedd10d32014-04-03 16:45:23 -0600322static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700323 struct nvme_completion *cqe)
324{
325 struct async_cmd_info *cmdinfo = ctx;
326 cmdinfo->result = le32_to_cpup(&cqe->result);
327 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
328 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
Keith Busch42483222015-06-01 09:29:54 -0600329 blk_mq_free_request(cmdinfo->req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700330}
331
332static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
333 unsigned int tag)
334{
Keith Busch42483222015-06-01 09:29:54 -0600335 struct request *req = blk_mq_tag_to_rq(*nvmeq->tags, tag);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700336
337 return blk_mq_rq_to_pdu(req);
Keith Busch4d115422013-12-10 13:10:40 -0700338}
339
Matthew Wilcox184d2942011-05-11 21:36:38 -0400340/*
341 * Called with local interrupts disabled and the q_lock held. May not sleep.
342 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700343static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400344 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500345{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700346 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400347 void *ctx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700348 if (tag >= nvmeq->q_depth) {
349 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500350 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400351 }
Keith Busch859361a2012-08-02 14:05:59 -0600352 if (fn)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700353 *fn = cmd->fn;
354 ctx = cmd->ctx;
355 cmd->fn = special_completion;
356 cmd->ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400357 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500358}
359
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500360/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400361 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500362 * @nvmeq: The queue to use
363 * @cmd: The command to send
364 *
365 * Safe to use from interrupt context
366 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700367static int __nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500368{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700369 u16 tail = nvmeq->sq_tail;
370
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500371 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500372 if (++tail == nvmeq->q_depth)
373 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500374 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500375 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500376
377 return 0;
378}
379
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700380static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
381{
382 unsigned long flags;
383 int ret;
384 spin_lock_irqsave(&nvmeq->q_lock, flags);
385 ret = __nvme_submit_cmd(nvmeq, cmd);
386 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
387 return ret;
388}
389
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500390static __le64 **iod_list(struct nvme_iod *iod)
391{
392 return ((void *)iod) + iod->offset;
393}
394
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700395static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
396 unsigned nseg, unsigned long private)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500397{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700398 iod->private = private;
399 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
400 iod->npages = -1;
401 iod->length = nbytes;
402 iod->nents = 0;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500403}
404
405static struct nvme_iod *
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700406__nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
407 unsigned long priv, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500408{
409 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700410 sizeof(__le64 *) * nvme_npages(bytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500411 sizeof(struct scatterlist) * nseg, gfp);
412
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700413 if (iod)
414 iod_init(iod, bytes, nseg, priv);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500415
416 return iod;
417}
418
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700419static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
420 gfp_t gfp)
421{
422 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
423 sizeof(struct nvme_dsm_range);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700424 struct nvme_iod *iod;
425
426 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
427 size <= NVME_INT_BYTES(dev)) {
428 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
429
430 iod = cmd->iod;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700431 iod_init(iod, size, rq->nr_phys_segments,
Chong Yuanfda631f2015-03-27 09:21:32 +0800432 (unsigned long) rq | NVME_INT_MASK);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700433 return iod;
434 }
435
436 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
437 (unsigned long) rq, gfp);
438}
439
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200440static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500441{
Keith Busch1d090622014-06-23 11:34:01 -0600442 const int last_prp = dev->page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500443 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500444 __le64 **list = iod_list(iod);
445 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500446
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500447 if (iod->npages == 0)
448 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
449 for (i = 0; i < iod->npages; i++) {
450 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500451 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500452 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500453 prp_dma = next_prp_dma;
454 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700455
456 if (iod_should_kfree(iod))
457 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500458}
459
Keith Buschb4ff9c82014-08-29 09:06:12 -0600460static int nvme_error_status(u16 status)
461{
462 switch (status & 0x7ff) {
463 case NVME_SC_SUCCESS:
464 return 0;
465 case NVME_SC_CAP_EXCEEDED:
466 return -ENOSPC;
467 default:
468 return -EIO;
469 }
470}
471
Keith Busch52b68d72015-02-23 09:16:21 -0700472#ifdef CONFIG_BLK_DEV_INTEGRITY
Keith Busche1e5e562015-02-19 13:39:03 -0700473static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
474{
475 if (be32_to_cpu(pi->ref_tag) == v)
476 pi->ref_tag = cpu_to_be32(p);
477}
478
479static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
480{
481 if (be32_to_cpu(pi->ref_tag) == p)
482 pi->ref_tag = cpu_to_be32(v);
483}
484
485/**
486 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
487 *
488 * The virtual start sector is the one that was originally submitted by the
489 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
490 * start sector may be different. Remap protection information to match the
491 * physical LBA on writes, and back to the original seed on reads.
492 *
493 * Type 0 and 3 do not have a ref tag, so no remapping required.
494 */
495static void nvme_dif_remap(struct request *req,
496 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
497{
498 struct nvme_ns *ns = req->rq_disk->private_data;
499 struct bio_integrity_payload *bip;
500 struct t10_pi_tuple *pi;
501 void *p, *pmap;
502 u32 i, nlb, ts, phys, virt;
503
504 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
505 return;
506
507 bip = bio_integrity(req->bio);
508 if (!bip)
509 return;
510
511 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
Keith Busche1e5e562015-02-19 13:39:03 -0700512
513 p = pmap;
514 virt = bip_get_seed(bip);
515 phys = nvme_block_nr(ns, blk_rq_pos(req));
516 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
517 ts = ns->disk->integrity->tuple_size;
518
519 for (i = 0; i < nlb; i++, virt++, phys++) {
520 pi = (struct t10_pi_tuple *)p;
521 dif_swap(phys, virt, pi);
522 p += ts;
523 }
524 kunmap_atomic(pmap);
525}
526
Keith Busch52b68d72015-02-23 09:16:21 -0700527static int nvme_noop_verify(struct blk_integrity_iter *iter)
528{
529 return 0;
530}
531
532static int nvme_noop_generate(struct blk_integrity_iter *iter)
533{
534 return 0;
535}
536
537struct blk_integrity nvme_meta_noop = {
538 .name = "NVME_META_NOOP",
539 .generate_fn = nvme_noop_generate,
540 .verify_fn = nvme_noop_verify,
541};
542
543static void nvme_init_integrity(struct nvme_ns *ns)
544{
545 struct blk_integrity integrity;
546
547 switch (ns->pi_type) {
548 case NVME_NS_DPS_PI_TYPE3:
549 integrity = t10_pi_type3_crc;
550 break;
551 case NVME_NS_DPS_PI_TYPE1:
552 case NVME_NS_DPS_PI_TYPE2:
553 integrity = t10_pi_type1_crc;
554 break;
555 default:
556 integrity = nvme_meta_noop;
557 break;
558 }
559 integrity.tuple_size = ns->ms;
560 blk_integrity_register(ns->disk, &integrity);
561 blk_queue_max_integrity_segments(ns->queue, 1);
562}
563#else /* CONFIG_BLK_DEV_INTEGRITY */
564static void nvme_dif_remap(struct request *req,
565 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
566{
567}
568static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
569{
570}
571static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
572{
573}
574static void nvme_init_integrity(struct nvme_ns *ns)
575{
576}
577#endif
578
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700579static void req_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500580 struct nvme_completion *cqe)
581{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500582 struct nvme_iod *iod = ctx;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700583 struct request *req = iod_get_private(iod);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700584 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
585
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500586 u16 status = le16_to_cpup(&cqe->status) >> 1;
587
Keith Buschedd10d32014-04-03 16:45:23 -0600588 if (unlikely(status)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700589 if (!(status & NVME_SC_DNR || blk_noretry_request(req))
590 && (jiffies - req->start_time) < req->timeout) {
Keith Buschc9d3bf82015-01-07 18:55:52 -0700591 unsigned long flags;
592
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700593 blk_mq_requeue_request(req);
Keith Buschc9d3bf82015-01-07 18:55:52 -0700594 spin_lock_irqsave(req->q->queue_lock, flags);
595 if (!blk_queue_stopped(req->q))
596 blk_mq_kick_requeue_list(req->q);
597 spin_unlock_irqrestore(req->q->queue_lock, flags);
Keith Buschedd10d32014-04-03 16:45:23 -0600598 return;
599 }
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200600 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200601 req->errors = status;
602 } else {
603 req->errors = nvme_error_status(status);
604 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700605 } else
606 req->errors = 0;
Keith Buscha0a931d2015-05-22 12:28:31 -0600607 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
608 u32 result = le32_to_cpup(&cqe->result);
609 req->special = (void *)(uintptr_t)result;
610 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700611
612 if (cmd_rq->aborted)
Christoph Hellwige75ec752015-05-22 11:12:39 +0200613 dev_warn(nvmeq->dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700614 "completing aborted command with status:%04x\n",
615 status);
616
Keith Busche1e5e562015-02-19 13:39:03 -0700617 if (iod->nents) {
Christoph Hellwige75ec752015-05-22 11:12:39 +0200618 dma_unmap_sg(nvmeq->dev->dev, iod->sg, iod->nents,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700619 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busche1e5e562015-02-19 13:39:03 -0700620 if (blk_integrity_rq(req)) {
621 if (!rq_data_dir(req))
622 nvme_dif_remap(req, nvme_dif_complete);
Christoph Hellwige75ec752015-05-22 11:12:39 +0200623 dma_unmap_sg(nvmeq->dev->dev, iod->meta_sg, 1,
Keith Busche1e5e562015-02-19 13:39:03 -0700624 rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
625 }
626 }
Keith Buschedd10d32014-04-03 16:45:23 -0600627 nvme_free_iod(nvmeq->dev, iod);
Keith Busch3291fa52014-04-28 12:30:52 -0600628
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700629 blk_mq_complete_request(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500630}
631
Matthew Wilcox184d2942011-05-11 21:36:38 -0400632/* length is in bytes. gfp flags indicates whether we may sleep. */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200633static int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod,
634 int total_len, gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500635{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500636 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500637 int length = total_len;
638 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500639 int dma_len = sg_dma_len(sg);
640 u64 dma_addr = sg_dma_address(sg);
Murali Iyerf137e0f2015-03-26 11:07:51 -0500641 u32 page_size = dev->page_size;
642 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500643 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500644 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500645 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500646 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500647
Keith Busch1d090622014-06-23 11:34:01 -0600648 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500649 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500650 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500651
Keith Busch1d090622014-06-23 11:34:01 -0600652 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500653 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600654 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500655 } else {
656 sg = sg_next(sg);
657 dma_addr = sg_dma_address(sg);
658 dma_len = sg_dma_len(sg);
659 }
660
Keith Busch1d090622014-06-23 11:34:01 -0600661 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600662 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500663 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500664 }
665
Keith Busch1d090622014-06-23 11:34:01 -0600666 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500667 if (nprps <= (256 / 8)) {
668 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500669 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500670 } else {
671 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500672 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500673 }
674
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400675 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
676 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600677 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500678 iod->npages = -1;
Keith Busch1d090622014-06-23 11:34:01 -0600679 return (total_len - length) + page_size;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400680 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500681 list[0] = prp_list;
682 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500683 i = 0;
684 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600685 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500686 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400687 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500688 if (!prp_list)
689 return total_len - length;
690 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400691 prp_list[0] = old_prp_list[i - 1];
692 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
693 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500694 }
695 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600696 dma_len -= page_size;
697 dma_addr += page_size;
698 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500699 if (length <= 0)
700 break;
701 if (dma_len > 0)
702 continue;
703 BUG_ON(dma_len < 0);
704 sg = sg_next(sg);
705 dma_addr = sg_dma_address(sg);
706 dma_len = sg_dma_len(sg);
707 }
708
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500709 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500710}
711
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200712static void nvme_submit_priv(struct nvme_queue *nvmeq, struct request *req,
713 struct nvme_iod *iod)
714{
715 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
716
717 memcpy(cmnd, req->cmd, sizeof(struct nvme_command));
718 cmnd->rw.command_id = req->tag;
719 if (req->nr_phys_segments) {
720 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
721 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
722 }
723
724 if (++nvmeq->sq_tail == nvmeq->q_depth)
725 nvmeq->sq_tail = 0;
726 writel(nvmeq->sq_tail, nvmeq->q_db);
727}
728
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700729/*
730 * We reuse the small pool to allocate the 16-byte range here as it is not
731 * worth having a special pool for these or additional cases to handle freeing
732 * the iod.
733 */
734static void nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
735 struct request *req, struct nvme_iod *iod)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700736{
Keith Buschedd10d32014-04-03 16:45:23 -0600737 struct nvme_dsm_range *range =
738 (struct nvme_dsm_range *)iod_list(iod)[0];
Keith Busch0e5e4f02012-11-09 16:33:05 -0700739 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
740
Keith Busch0e5e4f02012-11-09 16:33:05 -0700741 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700742 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
743 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700744
745 memset(cmnd, 0, sizeof(*cmnd));
746 cmnd->dsm.opcode = nvme_cmd_dsm;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700747 cmnd->dsm.command_id = req->tag;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700748 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
749 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
750 cmnd->dsm.nr = 0;
751 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
752
753 if (++nvmeq->sq_tail == nvmeq->q_depth)
754 nvmeq->sq_tail = 0;
755 writel(nvmeq->sq_tail, nvmeq->q_db);
Keith Busch0e5e4f02012-11-09 16:33:05 -0700756}
757
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700758static void nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500759 int cmdid)
760{
761 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
762
763 memset(cmnd, 0, sizeof(*cmnd));
764 cmnd->common.opcode = nvme_cmd_flush;
765 cmnd->common.command_id = cmdid;
766 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
767
768 if (++nvmeq->sq_tail == nvmeq->q_depth)
769 nvmeq->sq_tail = 0;
770 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500771}
772
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700773static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod,
774 struct nvme_ns *ns)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500775{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700776 struct request *req = iod_get_private(iod);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500777 struct nvme_command *cmnd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700778 u16 control = 0;
779 u32 dsmgmt = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500780
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700781 if (req->cmd_flags & REQ_FUA)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500782 control |= NVME_RW_FUA;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700783 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500784 control |= NVME_RW_LR;
785
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700786 if (req->cmd_flags & REQ_RAHEAD)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500787 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
788
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500789 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500790 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500791
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700792 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
793 cmnd->rw.command_id = req->tag;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500794 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Keith Buschedd10d32014-04-03 16:45:23 -0600795 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
796 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700797 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
798 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
Keith Busche1e5e562015-02-19 13:39:03 -0700799
800 if (blk_integrity_rq(req)) {
801 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(iod->meta_sg));
802 switch (ns->pi_type) {
803 case NVME_NS_DPS_PI_TYPE3:
804 control |= NVME_RW_PRINFO_PRCHK_GUARD;
805 break;
806 case NVME_NS_DPS_PI_TYPE1:
807 case NVME_NS_DPS_PI_TYPE2:
808 control |= NVME_RW_PRINFO_PRCHK_GUARD |
809 NVME_RW_PRINFO_PRCHK_REF;
810 cmnd->rw.reftag = cpu_to_le32(
811 nvme_block_nr(ns, blk_rq_pos(req)));
812 break;
813 }
814 } else if (ns->ms)
815 control |= NVME_RW_PRINFO_PRACT;
816
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500817 cmnd->rw.control = cpu_to_le16(control);
818 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500819
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500820 if (++nvmeq->sq_tail == nvmeq->q_depth)
821 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500822 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500823
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500824 return 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600825}
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500826
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200827/*
828 * NOTE: ns is NULL when called on the admin queue.
829 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700830static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
831 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600832{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700833 struct nvme_ns *ns = hctx->queue->queuedata;
834 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200835 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700836 struct request *req = bd->rq;
837 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
Keith Buschedd10d32014-04-03 16:45:23 -0600838 struct nvme_iod *iod;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700839 enum dma_data_direction dma_dir;
Keith Buschedd10d32014-04-03 16:45:23 -0600840
Keith Busche1e5e562015-02-19 13:39:03 -0700841 /*
842 * If formated with metadata, require the block layer provide a buffer
843 * unless this namespace is formated such that the metadata can be
844 * stripped/generated by the controller with PRACT=1.
845 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200846 if (ns && ns->ms && !blk_integrity_rq(req)) {
Keith Busche1e5e562015-02-19 13:39:03 -0700847 if (!(ns->pi_type && ns->ms == 8)) {
848 req->errors = -EFAULT;
849 blk_mq_complete_request(req);
850 return BLK_MQ_RQ_QUEUE_OK;
851 }
852 }
853
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200854 iod = nvme_alloc_iod(req, dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600855 if (!iod)
Jens Axboefe543032014-12-11 13:58:39 -0700856 return BLK_MQ_RQ_QUEUE_BUSY;
Keith Buschedd10d32014-04-03 16:45:23 -0600857
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700858 if (req->cmd_flags & REQ_DISCARD) {
Keith Buschedd10d32014-04-03 16:45:23 -0600859 void *range;
860 /*
861 * We reuse the small pool to allocate the 16-byte range here
862 * as it is not worth having a special pool for these or
863 * additional cases to handle freeing the iod.
864 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200865 range = dma_pool_alloc(dev->prp_small_pool, GFP_ATOMIC,
Keith Buschedd10d32014-04-03 16:45:23 -0600866 &iod->first_dma);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700867 if (!range)
Jens Axboefe543032014-12-11 13:58:39 -0700868 goto retry_cmd;
Keith Buschedd10d32014-04-03 16:45:23 -0600869 iod_list(iod)[0] = (__le64 *)range;
870 iod->npages = 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700871 } else if (req->nr_phys_segments) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700872 dma_dir = rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
Keith Buschedd10d32014-04-03 16:45:23 -0600873
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700874 sg_init_table(iod->sg, req->nr_phys_segments);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700875 iod->nents = blk_rq_map_sg(req->q, req, iod->sg);
Jens Axboefe543032014-12-11 13:58:39 -0700876 if (!iod->nents)
877 goto error_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700878
879 if (!dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir))
Jens Axboefe543032014-12-11 13:58:39 -0700880 goto retry_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700881
Jens Axboefe543032014-12-11 13:58:39 -0700882 if (blk_rq_bytes(req) !=
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200883 nvme_setup_prps(dev, iod, blk_rq_bytes(req), GFP_ATOMIC)) {
884 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
Jens Axboefe543032014-12-11 13:58:39 -0700885 goto retry_cmd;
886 }
Keith Busche1e5e562015-02-19 13:39:03 -0700887 if (blk_integrity_rq(req)) {
888 if (blk_rq_count_integrity_sg(req->q, req->bio) != 1)
889 goto error_cmd;
890
891 sg_init_table(iod->meta_sg, 1);
892 if (blk_rq_map_integrity_sg(
893 req->q, req->bio, iod->meta_sg) != 1)
894 goto error_cmd;
895
896 if (rq_data_dir(req))
897 nvme_dif_remap(req, nvme_dif_prep);
898
899 if (!dma_map_sg(nvmeq->q_dmadev, iod->meta_sg, 1, dma_dir))
900 goto error_cmd;
901 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700902 }
903
Keith Busch9af87852014-12-03 17:07:13 -0700904 nvme_set_info(cmd, iod, req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700905 spin_lock_irq(&nvmeq->q_lock);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200906 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
907 nvme_submit_priv(nvmeq, req, iod);
908 else if (req->cmd_flags & REQ_DISCARD)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700909 nvme_submit_discard(nvmeq, ns, req, iod);
910 else if (req->cmd_flags & REQ_FLUSH)
911 nvme_submit_flush(nvmeq, ns, req->tag);
912 else
913 nvme_submit_iod(nvmeq, iod, ns);
914
915 nvme_process_cq(nvmeq);
916 spin_unlock_irq(&nvmeq->q_lock);
917 return BLK_MQ_RQ_QUEUE_OK;
918
Jens Axboefe543032014-12-11 13:58:39 -0700919 error_cmd:
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200920 nvme_free_iod(dev, iod);
Jens Axboefe543032014-12-11 13:58:39 -0700921 return BLK_MQ_RQ_QUEUE_ERROR;
922 retry_cmd:
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200923 nvme_free_iod(dev, iod);
Jens Axboefe543032014-12-11 13:58:39 -0700924 return BLK_MQ_RQ_QUEUE_BUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500925}
926
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400927static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500928{
Matthew Wilcox82123462011-01-20 13:24:06 -0500929 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500930
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500931 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500932 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500933
934 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400935 void *ctx;
936 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500937 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500938 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500939 break;
940 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
941 if (++head == nvmeq->q_depth) {
942 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500943 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500944 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700945 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600946 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500947 }
948
949 /* If the controller ignores the cq head doorbell and continuously
950 * writes to the queue, it is theoretically possible to wrap around
951 * the queue twice and mistakenly return IRQ_NONE. Linux only
952 * requires that 0.1% of your interrupts are handled, so this isn't
953 * a big problem.
954 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500955 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400956 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500957
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800958 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500959 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500960 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500961
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400962 nvmeq->cqe_seen = 1;
963 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500964}
965
966static irqreturn_t nvme_irq(int irq, void *data)
967{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500968 irqreturn_t result;
969 struct nvme_queue *nvmeq = data;
970 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400971 nvme_process_cq(nvmeq);
972 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
973 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500974 spin_unlock(&nvmeq->q_lock);
975 return result;
976}
977
978static irqreturn_t nvme_irq_check(int irq, void *data)
979{
980 struct nvme_queue *nvmeq = data;
981 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
982 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
983 return IRQ_NONE;
984 return IRQ_WAKE_THREAD;
985}
986
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500987/*
988 * Returns 0 on success. If the result is negative, it's a Linux error code;
989 * if the result is positive, it's an NVM Express status code
990 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200991int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
992 void *buffer, void __user *ubuffer, unsigned bufflen,
993 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500994{
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200995 bool write = cmd->common.opcode & 1;
996 struct bio *bio = NULL;
Christoph Hellwigf705f832015-05-22 11:12:38 +0200997 struct request *req;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200998 int ret;
Christoph Hellwigf705f832015-05-22 11:12:38 +0200999
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001000 req = blk_mq_alloc_request(q, write, GFP_KERNEL, false);
Christoph Hellwigf705f832015-05-22 11:12:38 +02001001 if (IS_ERR(req))
1002 return PTR_ERR(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001003
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001004 req->cmd_type = REQ_TYPE_DRV_PRIV;
Keith Busch75619bf2015-05-28 09:48:55 -06001005 req->cmd_flags = REQ_FAILFAST_DRIVER;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001006 req->__data_len = 0;
1007 req->__sector = (sector_t) -1;
1008 req->bio = req->biotail = NULL;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001009
Keith Buschf4ff4142015-05-28 09:48:54 -06001010 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001011
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001012 req->cmd = (unsigned char *)cmd;
1013 req->cmd_len = sizeof(struct nvme_command);
Keith Buscha0a931d2015-05-22 12:28:31 -06001014 req->special = (void *)0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001015
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001016 if (buffer && bufflen) {
1017 ret = blk_rq_map_kern(q, req, buffer, bufflen, __GFP_WAIT);
1018 if (ret)
1019 goto out;
1020 } else if (ubuffer && bufflen) {
1021 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen, __GFP_WAIT);
1022 if (ret)
1023 goto out;
1024 bio = req->bio;
1025 }
Matthew Wilcox3c0cf132011-02-04 16:03:56 -05001026
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001027 blk_execute_rq(req->q, NULL, req, 0);
1028 if (bio)
1029 blk_rq_unmap_user(bio);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001030 if (result)
Keith Buscha0a931d2015-05-22 12:28:31 -06001031 *result = (u32)(uintptr_t)req->special;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001032 ret = req->errors;
1033 out:
Christoph Hellwigf705f832015-05-22 11:12:38 +02001034 blk_mq_free_request(req);
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001035 return ret;
Christoph Hellwigf705f832015-05-22 11:12:38 +02001036}
1037
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001038int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
1039 void *buffer, unsigned bufflen)
Christoph Hellwigf705f832015-05-22 11:12:38 +02001040{
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001041 return __nvme_submit_sync_cmd(q, cmd, buffer, NULL, bufflen, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001042}
1043
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001044static int nvme_submit_async_admin_req(struct nvme_dev *dev)
1045{
1046 struct nvme_queue *nvmeq = dev->queues[0];
1047 struct nvme_command c;
1048 struct nvme_cmd_info *cmd_info;
1049 struct request *req;
1050
Keith Busch1efccc92015-03-31 10:37:17 -06001051 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC, true);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001052 if (IS_ERR(req))
1053 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001054
Keith Buschc917dfe2015-01-07 18:55:48 -07001055 req->cmd_flags |= REQ_NO_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001056 cmd_info = blk_mq_rq_to_pdu(req);
Keith Busch1efccc92015-03-31 10:37:17 -06001057 nvme_set_info(cmd_info, NULL, async_req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001058
1059 memset(&c, 0, sizeof(c));
1060 c.common.opcode = nvme_admin_async_event;
1061 c.common.command_id = req->tag;
1062
Keith Busch42483222015-06-01 09:29:54 -06001063 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001064 return __nvme_submit_cmd(nvmeq, &c);
1065}
1066
1067static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
Keith Busch4d115422013-12-10 13:10:40 -07001068 struct nvme_command *cmd,
1069 struct async_cmd_info *cmdinfo, unsigned timeout)
1070{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001071 struct nvme_queue *nvmeq = dev->queues[0];
1072 struct request *req;
1073 struct nvme_cmd_info *cmd_rq;
Keith Busch4d115422013-12-10 13:10:40 -07001074
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001075 req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_KERNEL, false);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001076 if (IS_ERR(req))
1077 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001078
1079 req->timeout = timeout;
1080 cmd_rq = blk_mq_rq_to_pdu(req);
1081 cmdinfo->req = req;
1082 nvme_set_info(cmd_rq, cmdinfo, async_completion);
Keith Busch4d115422013-12-10 13:10:40 -07001083 cmdinfo->status = -EINTR;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001084
1085 cmd->common.command_id = req->tag;
1086
Keith Busch4f5099a2014-03-03 16:39:13 -07001087 return nvme_submit_cmd(nvmeq, cmd);
Keith Busch4d115422013-12-10 13:10:40 -07001088}
1089
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001090static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1091{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001092 struct nvme_command c;
1093
1094 memset(&c, 0, sizeof(c));
1095 c.delete_queue.opcode = opcode;
1096 c.delete_queue.qid = cpu_to_le16(id);
1097
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001098 return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001099}
1100
1101static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1102 struct nvme_queue *nvmeq)
1103{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001104 struct nvme_command c;
1105 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1106
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001107 /*
1108 * Note: we (ab)use the fact the the prp fields survive if no data
1109 * is attached to the request.
1110 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001111 memset(&c, 0, sizeof(c));
1112 c.create_cq.opcode = nvme_admin_create_cq;
1113 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1114 c.create_cq.cqid = cpu_to_le16(qid);
1115 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1116 c.create_cq.cq_flags = cpu_to_le16(flags);
1117 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1118
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001119 return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001120}
1121
1122static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1123 struct nvme_queue *nvmeq)
1124{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001125 struct nvme_command c;
1126 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1127
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001128 /*
1129 * Note: we (ab)use the fact the the prp fields survive if no data
1130 * is attached to the request.
1131 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001132 memset(&c, 0, sizeof(c));
1133 c.create_sq.opcode = nvme_admin_create_sq;
1134 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1135 c.create_sq.sqid = cpu_to_le16(qid);
1136 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1137 c.create_sq.sq_flags = cpu_to_le16(flags);
1138 c.create_sq.cqid = cpu_to_le16(qid);
1139
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001140 return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001141}
1142
1143static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1144{
1145 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1146}
1147
1148static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1149{
1150 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1151}
1152
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001153int nvme_identify_ctrl(struct nvme_dev *dev, struct nvme_id_ctrl **id)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001154{
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001155 struct nvme_command c = {
1156 .identify.opcode = nvme_admin_identify,
1157 .identify.cns = cpu_to_le32(1),
1158 };
1159 int error;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001160
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001161 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
1162 if (!*id)
1163 return -ENOMEM;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001164
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001165 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
1166 sizeof(struct nvme_id_ctrl));
1167 if (error)
1168 kfree(*id);
1169 return error;
1170}
1171
1172int nvme_identify_ns(struct nvme_dev *dev, unsigned nsid,
1173 struct nvme_id_ns **id)
1174{
1175 struct nvme_command c = {
1176 .identify.opcode = nvme_admin_identify,
1177 .identify.nsid = cpu_to_le32(nsid),
1178 };
1179 int error;
1180
1181 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
1182 if (!*id)
1183 return -ENOMEM;
1184
1185 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
1186 sizeof(struct nvme_id_ns));
1187 if (error)
1188 kfree(*id);
1189 return error;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001190}
1191
Vishal Verma5d0f6132013-03-04 18:40:58 -07001192int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -06001193 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001194{
1195 struct nvme_command c;
1196
1197 memset(&c, 0, sizeof(c));
1198 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -06001199 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001200 c.features.prp1 = cpu_to_le64(dma_addr);
1201 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001202
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001203 return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, NULL, 0,
1204 result, 0);
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001205}
1206
Vishal Verma5d0f6132013-03-04 18:40:58 -07001207int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
1208 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001209{
1210 struct nvme_command c;
1211
1212 memset(&c, 0, sizeof(c));
1213 c.features.opcode = nvme_admin_set_features;
1214 c.features.prp1 = cpu_to_le64(dma_addr);
1215 c.features.fid = cpu_to_le32(fid);
1216 c.features.dword11 = cpu_to_le32(dword11);
1217
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001218 return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, NULL, 0,
1219 result, 0);
1220}
1221
1222int nvme_get_log_page(struct nvme_dev *dev, struct nvme_smart_log **log)
1223{
1224 struct nvme_command c = {
1225 .common.opcode = nvme_admin_get_log_page,
1226 .common.nsid = cpu_to_le32(0xFFFFFFFF),
1227 .common.cdw10[0] = cpu_to_le32(
1228 (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
1229 NVME_LOG_SMART),
1230 };
1231 int error;
1232
1233 *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
1234 if (!*log)
1235 return -ENOMEM;
1236
1237 error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
1238 sizeof(struct nvme_smart_log));
1239 if (error)
1240 kfree(*log);
1241 return error;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001242}
1243
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001244/**
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001245 * nvme_abort_req - Attempt aborting a request
Keith Buschc30341d2013-12-10 13:10:38 -07001246 *
1247 * Schedule controller reset if the command was already aborted once before and
1248 * still hasn't been returned to the driver, or if this is the admin queue.
1249 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001250static void nvme_abort_req(struct request *req)
Keith Buschc30341d2013-12-10 13:10:38 -07001251{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001252 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1253 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001254 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001255 struct request *abort_req;
1256 struct nvme_cmd_info *abort_cmd;
1257 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -07001258
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001259 if (!nvmeq->qid || cmd_rq->aborted) {
Keith Busch7a509a62015-01-07 18:55:53 -07001260 unsigned long flags;
1261
1262 spin_lock_irqsave(&dev_list_lock, flags);
Keith Buschc30341d2013-12-10 13:10:38 -07001263 if (work_busy(&dev->reset_work))
Keith Busch7a509a62015-01-07 18:55:53 -07001264 goto out;
Keith Buschc30341d2013-12-10 13:10:38 -07001265 list_del_init(&dev->node);
Christoph Hellwige75ec752015-05-22 11:12:39 +02001266 dev_warn(dev->dev, "I/O %d QID %d timeout, reset controller\n",
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001267 req->tag, nvmeq->qid);
Tejun Heo9ca97372014-03-07 10:24:49 -05001268 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschc30341d2013-12-10 13:10:38 -07001269 queue_work(nvme_workq, &dev->reset_work);
Keith Busch7a509a62015-01-07 18:55:53 -07001270 out:
1271 spin_unlock_irqrestore(&dev_list_lock, flags);
Keith Buschc30341d2013-12-10 13:10:38 -07001272 return;
1273 }
1274
1275 if (!dev->abort_limit)
1276 return;
1277
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001278 abort_req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC,
1279 false);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001280 if (IS_ERR(abort_req))
Keith Buschc30341d2013-12-10 13:10:38 -07001281 return;
1282
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001283 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1284 nvme_set_info(abort_cmd, abort_req, abort_completion);
1285
Keith Buschc30341d2013-12-10 13:10:38 -07001286 memset(&cmd, 0, sizeof(cmd));
1287 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001288 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001289 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001290 cmd.abort.command_id = abort_req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001291
1292 --dev->abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001293 cmd_rq->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -07001294
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001295 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", req->tag,
Keith Buschc30341d2013-12-10 13:10:38 -07001296 nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001297 if (nvme_submit_cmd(dev->queues[0], &cmd) < 0) {
1298 dev_warn(nvmeq->q_dmadev,
1299 "Could not abort I/O %d QID %d",
1300 req->tag, nvmeq->qid);
Sam Bradshawc87fd542014-12-10 13:00:31 -07001301 blk_mq_free_request(abort_req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001302 }
Keith Buschc30341d2013-12-10 13:10:38 -07001303}
1304
Keith Busch42483222015-06-01 09:29:54 -06001305static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001306{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001307 struct nvme_queue *nvmeq = data;
1308 void *ctx;
1309 nvme_completion_fn fn;
1310 struct nvme_cmd_info *cmd;
Keith Buschcef6a942015-01-07 18:55:51 -07001311 struct nvme_completion cqe;
1312
1313 if (!blk_mq_request_started(req))
1314 return;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001315
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001316 cmd = blk_mq_rq_to_pdu(req);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001317
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001318 if (cmd->ctx == CMD_CTX_CANCELLED)
1319 return;
1320
Keith Buschcef6a942015-01-07 18:55:51 -07001321 if (blk_queue_dying(req->q))
1322 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1323 else
1324 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1325
1326
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001327 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1328 req->tag, nvmeq->qid);
1329 ctx = cancel_cmd_info(cmd, &fn);
1330 fn(nvmeq, ctx, &cqe);
1331}
1332
1333static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
1334{
1335 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
1336 struct nvme_queue *nvmeq = cmd->nvmeq;
1337
Keith Busch07836e62015-02-19 10:34:48 -07001338 dev_warn(nvmeq->q_dmadev, "Timeout I/O %d QID %d\n", req->tag,
1339 nvmeq->qid);
1340 spin_lock_irq(&nvmeq->q_lock);
1341 nvme_abort_req(req);
1342 spin_unlock_irq(&nvmeq->q_lock);
1343
Keith Busch7a509a62015-01-07 18:55:53 -07001344 /*
1345 * The aborted req will be completed on receiving the abort req.
1346 * We enable the timer again. If hit twice, it'll cause a device reset,
1347 * as the device then is in a faulty state.
1348 */
Keith Busch07836e62015-02-19 10:34:48 -07001349 return BLK_EH_RESET_TIMER;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001350}
1351
Keith Buschf435c282014-07-07 09:14:42 -06001352static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001353{
1354 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1355 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1356 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1357 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1358 kfree(nvmeq);
1359}
1360
Keith Buscha1a5ef92013-12-16 13:50:00 -05001361static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001362{
1363 int i;
1364
Keith Buscha1a5ef92013-12-16 13:50:00 -05001365 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001366 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001367 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001368 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001369 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001370 }
Keith Busch22404272013-07-15 15:02:20 -06001371}
1372
Keith Busch4d115422013-12-10 13:10:40 -07001373/**
1374 * nvme_suspend_queue - put queue into suspended state
1375 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001376 */
1377static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001378{
Keith Busch2b25d982014-12-22 12:59:04 -07001379 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001380
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001381 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001382 if (nvmeq->cq_vector == -1) {
1383 spin_unlock_irq(&nvmeq->q_lock);
1384 return 1;
1385 }
1386 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Keith Busch42f61422014-03-24 10:46:25 -06001387 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001388 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001389 spin_unlock_irq(&nvmeq->q_lock);
1390
Keith Busch6df3dbc2015-03-26 13:49:33 -06001391 if (!nvmeq->qid && nvmeq->dev->admin_q)
1392 blk_mq_freeze_queue_start(nvmeq->dev->admin_q);
1393
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001394 irq_set_affinity_hint(vector, NULL);
1395 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001396
Keith Busch4d115422013-12-10 13:10:40 -07001397 return 0;
1398}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001399
Keith Busch4d115422013-12-10 13:10:40 -07001400static void nvme_clear_queue(struct nvme_queue *nvmeq)
1401{
Keith Busch22404272013-07-15 15:02:20 -06001402 spin_lock_irq(&nvmeq->q_lock);
Keith Busch42483222015-06-01 09:29:54 -06001403 if (nvmeq->tags && *nvmeq->tags)
1404 blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001405 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001406}
1407
Keith Busch4d115422013-12-10 13:10:40 -07001408static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1409{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001410 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001411
1412 if (!nvmeq)
1413 return;
1414 if (nvme_suspend_queue(nvmeq))
1415 return;
1416
Keith Busch0e53d182013-12-10 13:10:39 -07001417 /* Don't tell the adapter to delete the admin queue.
1418 * Don't tell a removed adapter to delete IO queues. */
1419 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001420 adapter_delete_sq(dev, qid);
1421 adapter_delete_cq(dev, qid);
1422 }
Keith Busch07836e62015-02-19 10:34:48 -07001423
1424 spin_lock_irq(&nvmeq->q_lock);
1425 nvme_process_cq(nvmeq);
1426 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001427}
1428
1429static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Keith Busch2b25d982014-12-22 12:59:04 -07001430 int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001431{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001432 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001433 if (!nvmeq)
1434 return NULL;
1435
Christoph Hellwige75ec752015-05-22 11:12:39 +02001436 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001437 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001438 if (!nvmeq->cqes)
1439 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001440
Christoph Hellwige75ec752015-05-22 11:12:39 +02001441 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001442 &nvmeq->sq_dma_addr, GFP_KERNEL);
1443 if (!nvmeq->sq_cmds)
1444 goto free_cqdma;
1445
Christoph Hellwige75ec752015-05-22 11:12:39 +02001446 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001447 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001448 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1449 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001450 spin_lock_init(&nvmeq->q_lock);
1451 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001452 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001453 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001454 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001455 nvmeq->qid = qid;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001456 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001457
Jon Derrick36a7e992015-05-27 12:26:23 -06001458 /* make sure queue descriptor is set before queue count, for kthread */
1459 mb();
1460 dev->queue_count++;
1461
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001462 return nvmeq;
1463
1464 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001465 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001466 nvmeq->cq_dma_addr);
1467 free_nvmeq:
1468 kfree(nvmeq);
1469 return NULL;
1470}
1471
Matthew Wilcox30010822011-01-20 09:10:15 -05001472static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1473 const char *name)
1474{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001475 if (use_threaded_interrupts)
1476 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001477 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001478 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001479 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001480 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001481}
1482
Keith Busch22404272013-07-15 15:02:20 -06001483static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001484{
Keith Busch22404272013-07-15 15:02:20 -06001485 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001486
Keith Busch7be50e92014-09-10 15:48:47 -06001487 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001488 nvmeq->sq_tail = 0;
1489 nvmeq->cq_head = 0;
1490 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001491 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001492 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001493 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001494 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001495}
1496
1497static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1498{
1499 struct nvme_dev *dev = nvmeq->dev;
1500 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001501
Keith Busch2b25d982014-12-22 12:59:04 -07001502 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001503 result = adapter_alloc_cq(dev, qid, nvmeq);
1504 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001505 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001506
1507 result = adapter_alloc_sq(dev, qid, nvmeq);
1508 if (result < 0)
1509 goto release_cq;
1510
Matthew Wilcox3193f072014-01-27 15:57:22 -05001511 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001512 if (result < 0)
1513 goto release_sq;
1514
Keith Busch22404272013-07-15 15:02:20 -06001515 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001516 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001517
1518 release_sq:
1519 adapter_delete_sq(dev, qid);
1520 release_cq:
1521 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001522 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001523}
1524
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001525static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1526{
1527 unsigned long timeout;
1528 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1529
1530 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1531
1532 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1533 msleep(100);
1534 if (fatal_signal_pending(current))
1535 return -EINTR;
1536 if (time_after(jiffies, timeout)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001537 dev_err(dev->dev,
Matthew Wilcox27e81662014-04-11 11:58:45 -04001538 "Device not ready; aborting %s\n", enabled ?
1539 "initialisation" : "reset");
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001540 return -ENODEV;
1541 }
1542 }
1543
1544 return 0;
1545}
1546
1547/*
1548 * If the device has been passed off to us in an enabled state, just clear
1549 * the enabled bit. The spec says we should set the 'shutdown notification
1550 * bits', but doing so may cause the device to complete commands to the
1551 * admin queue ... and we don't know what memory that might be pointing at!
1552 */
1553static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1554{
Dan McLeran01079522014-06-23 08:24:36 -06001555 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1556 dev->ctrl_config &= ~NVME_CC_ENABLE;
1557 writel(dev->ctrl_config, &dev->bar->cc);
Matthew Wilcox44af1462013-05-04 06:43:17 -04001558
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001559 return nvme_wait_ready(dev, cap, false);
1560}
1561
1562static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1563{
Dan McLeran01079522014-06-23 08:24:36 -06001564 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1565 dev->ctrl_config |= NVME_CC_ENABLE;
1566 writel(dev->ctrl_config, &dev->bar->cc);
1567
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001568 return nvme_wait_ready(dev, cap, true);
1569}
1570
Keith Busch1894d8f2013-07-15 15:02:22 -06001571static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1572{
1573 unsigned long timeout;
Keith Busch1894d8f2013-07-15 15:02:22 -06001574
Dan McLeran01079522014-06-23 08:24:36 -06001575 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1576 dev->ctrl_config |= NVME_CC_SHN_NORMAL;
1577
1578 writel(dev->ctrl_config, &dev->bar->cc);
Keith Busch1894d8f2013-07-15 15:02:22 -06001579
Dan McLeran2484f402014-07-01 09:33:32 -06001580 timeout = SHUTDOWN_TIMEOUT + jiffies;
Keith Busch1894d8f2013-07-15 15:02:22 -06001581 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1582 NVME_CSTS_SHST_CMPLT) {
1583 msleep(100);
1584 if (fatal_signal_pending(current))
1585 return -EINTR;
1586 if (time_after(jiffies, timeout)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001587 dev_err(dev->dev,
Keith Busch1894d8f2013-07-15 15:02:22 -06001588 "Device shutdown incomplete; abort shutdown\n");
1589 return -ENODEV;
1590 }
1591 }
1592
1593 return 0;
1594}
1595
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001596static struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001597 .queue_rq = nvme_queue_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001598 .map_queue = blk_mq_map_queue,
1599 .init_hctx = nvme_admin_init_hctx,
1600 .init_request = nvme_admin_init_request,
1601 .timeout = nvme_timeout,
1602};
1603
1604static struct blk_mq_ops nvme_mq_ops = {
1605 .queue_rq = nvme_queue_rq,
1606 .map_queue = blk_mq_map_queue,
1607 .init_hctx = nvme_init_hctx,
1608 .init_request = nvme_init_request,
1609 .timeout = nvme_timeout,
1610};
1611
Keith Buschea191d22015-01-07 18:55:49 -07001612static void nvme_dev_remove_admin(struct nvme_dev *dev)
1613{
1614 if (dev->admin_q && !blk_queue_dying(dev->admin_q)) {
1615 blk_cleanup_queue(dev->admin_q);
1616 blk_mq_free_tag_set(&dev->admin_tagset);
1617 }
1618}
1619
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001620static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1621{
1622 if (!dev->admin_q) {
1623 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1624 dev->admin_tagset.nr_hw_queues = 1;
1625 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1;
Keith Busch1efccc92015-03-31 10:37:17 -06001626 dev->admin_tagset.reserved_tags = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001627 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001628 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001629 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001630 dev->admin_tagset.driver_data = dev;
1631
1632 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1633 return -ENOMEM;
1634
1635 dev->admin_q = blk_mq_init_queue(&dev->admin_tagset);
Ming Lei35b489d2015-01-02 14:25:27 +00001636 if (IS_ERR(dev->admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001637 blk_mq_free_tag_set(&dev->admin_tagset);
1638 return -ENOMEM;
1639 }
Keith Buschea191d22015-01-07 18:55:49 -07001640 if (!blk_get_queue(dev->admin_q)) {
1641 nvme_dev_remove_admin(dev);
1642 return -ENODEV;
1643 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001644 } else
1645 blk_mq_unfreeze_queue(dev->admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001646
1647 return 0;
1648}
1649
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001650static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001651{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001652 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001653 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001654 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001655 struct nvme_queue *nvmeq;
Keith Busch1d090622014-06-23 11:34:01 -06001656 unsigned page_shift = PAGE_SHIFT;
1657 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12;
1658 unsigned dev_page_max = NVME_CAP_MPSMAX(cap) + 12;
1659
1660 if (page_shift < dev_page_min) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001661 dev_err(dev->dev,
Keith Busch1d090622014-06-23 11:34:01 -06001662 "Minimum device page size (%u) too large for "
1663 "host (%u)\n", 1 << dev_page_min,
1664 1 << page_shift);
1665 return -ENODEV;
1666 }
1667 if (page_shift > dev_page_max) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001668 dev_info(dev->dev,
Keith Busch1d090622014-06-23 11:34:01 -06001669 "Device maximum page size (%u) smaller than "
1670 "host (%u); enabling work-around\n",
1671 1 << dev_page_max, 1 << page_shift);
1672 page_shift = dev_page_max;
1673 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001674
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001675 result = nvme_disable_ctrl(dev, cap);
1676 if (result < 0)
1677 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001678
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001679 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001680 if (!nvmeq) {
Keith Busch2b25d982014-12-22 12:59:04 -07001681 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001682 if (!nvmeq)
1683 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001684 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001685
1686 aqa = nvmeq->q_depth - 1;
1687 aqa |= aqa << 16;
1688
Keith Busch1d090622014-06-23 11:34:01 -06001689 dev->page_size = 1 << page_shift;
1690
Dan McLeran01079522014-06-23 08:24:36 -06001691 dev->ctrl_config = NVME_CC_CSS_NVM;
Keith Busch1d090622014-06-23 11:34:01 -06001692 dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001693 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001694 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001695
1696 writel(aqa, &dev->bar->aqa);
1697 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1698 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001699
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001700 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001701 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001702 goto free_nvmeq;
1703
Keith Busch2b25d982014-12-22 12:59:04 -07001704 nvmeq->cq_vector = 0;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001705 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Keith Busch025c5572013-05-01 13:07:51 -06001706 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07001707 goto free_nvmeq;
Keith Busch025c5572013-05-01 13:07:51 -06001708
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001709 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001710
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001711 free_nvmeq:
1712 nvme_free_queues(dev, 0);
1713 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001714}
1715
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001716static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1717{
1718 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001719 struct nvme_user_io io;
1720 struct nvme_command c;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001721 unsigned length, meta_len;
Keith Buscha67a9512015-04-07 16:57:19 -06001722 int status, write;
Keith Buscha67a9512015-04-07 16:57:19 -06001723 dma_addr_t meta_dma = 0;
1724 void *meta = NULL;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001725
1726 if (copy_from_user(&io, uio, sizeof(io)))
1727 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001728
1729 switch (io.opcode) {
1730 case nvme_cmd_write:
1731 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001732 case nvme_cmd_compare:
Matthew Wilcox64132142011-08-09 12:56:37 -04001733 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001734 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001735 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001736 }
1737
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001738 length = (io.nblocks + 1) << ns->lba_shift;
1739 meta_len = (io.nblocks + 1) * ns->ms;
1740 write = io.opcode & 1;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001741
Keith Buscha67a9512015-04-07 16:57:19 -06001742 if (meta_len) {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001743 if (((io.metadata & 3) || !io.metadata) && !ns->ext)
1744 return -EINVAL;
1745
1746 if (ns->ext) {
1747 length += meta_len;
1748 meta_len = 0;
1749 }
1750
Christoph Hellwige75ec752015-05-22 11:12:39 +02001751 meta = dma_alloc_coherent(dev->dev, meta_len,
Keith Buscha67a9512015-04-07 16:57:19 -06001752 &meta_dma, GFP_KERNEL);
1753 if (!meta) {
1754 status = -ENOMEM;
1755 goto unmap;
1756 }
1757 if (write) {
1758 if (copy_from_user(meta, (void __user *)io.metadata,
1759 meta_len)) {
1760 status = -EFAULT;
1761 goto unmap;
1762 }
1763 }
1764 }
1765
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001766 memset(&c, 0, sizeof(c));
1767 c.rw.opcode = io.opcode;
1768 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001769 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001770 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001771 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001772 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001773 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1774 c.rw.reftag = cpu_to_le32(io.reftag);
1775 c.rw.apptag = cpu_to_le16(io.apptag);
1776 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buscha67a9512015-04-07 16:57:19 -06001777 c.rw.metadata = cpu_to_le64(meta_dma);
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001778
1779 status = __nvme_submit_sync_cmd(ns->queue, &c, NULL,
1780 (void __user *)io.addr, length, NULL, 0);
Keith Buschf410c682013-04-23 17:23:59 -06001781 unmap:
Keith Buscha67a9512015-04-07 16:57:19 -06001782 if (meta) {
1783 if (status == NVME_SC_SUCCESS && !write) {
1784 if (copy_to_user((void __user *)io.metadata, meta,
1785 meta_len))
1786 status = -EFAULT;
1787 }
Christoph Hellwige75ec752015-05-22 11:12:39 +02001788 dma_free_coherent(dev->dev, meta_len, meta, meta_dma);
Keith Buschf410c682013-04-23 17:23:59 -06001789 }
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001790 return status;
1791}
1792
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001793static int nvme_user_cmd(struct nvme_dev *dev, struct nvme_ns *ns,
1794 struct nvme_passthru_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001795{
Keith Busch7963e522014-09-12 16:07:20 -06001796 struct nvme_passthru_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001797 struct nvme_command c;
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001798 unsigned timeout = 0;
1799 int status;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001800
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001801 if (!capable(CAP_SYS_ADMIN))
1802 return -EACCES;
1803 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001804 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001805
1806 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001807 c.common.opcode = cmd.opcode;
1808 c.common.flags = cmd.flags;
1809 c.common.nsid = cpu_to_le32(cmd.nsid);
1810 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1811 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1812 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1813 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1814 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1815 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1816 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1817 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1818
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001819 if (cmd.timeout_ms)
1820 timeout = msecs_to_jiffies(cmd.timeout_ms);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001821
Christoph Hellwigf705f832015-05-22 11:12:38 +02001822 status = __nvme_submit_sync_cmd(ns ? ns->queue : dev->admin_q, &c,
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001823 NULL, (void __user *)cmd.addr, cmd.data_len,
1824 &cmd.result, timeout);
1825 if (status >= 0) {
1826 if (put_user(cmd.result, &ucmd->result))
1827 return -EFAULT;
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001828 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001829
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001830 return status;
1831}
1832
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001833static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1834 unsigned long arg)
1835{
1836 struct nvme_ns *ns = bdev->bd_disk->private_data;
1837
1838 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001839 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001840 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001841 return ns->ns_id;
1842 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001843 return nvme_user_cmd(ns->dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06001844 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001845 return nvme_user_cmd(ns->dev, ns, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001846 case NVME_IOCTL_SUBMIT_IO:
1847 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001848 case SG_GET_VERSION_NUM:
1849 return nvme_sg_get_version_num((void __user *)arg);
1850 case SG_IO:
1851 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001852 default:
1853 return -ENOTTY;
1854 }
1855}
1856
Keith Busch320a3822013-10-23 13:07:34 -06001857#ifdef CONFIG_COMPAT
1858static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1859 unsigned int cmd, unsigned long arg)
1860{
Keith Busch320a3822013-10-23 13:07:34 -06001861 switch (cmd) {
1862 case SG_IO:
Keith Busche1797292014-08-27 13:55:38 -06001863 return -ENOIOCTLCMD;
Keith Busch320a3822013-10-23 13:07:34 -06001864 }
1865 return nvme_ioctl(bdev, mode, cmd, arg);
1866}
1867#else
1868#define nvme_compat_ioctl NULL
1869#endif
1870
Keith Busch9ac27092014-01-31 16:53:39 -07001871static int nvme_open(struct block_device *bdev, fmode_t mode)
1872{
Keith Busch9e603522014-10-03 11:15:47 -06001873 int ret = 0;
1874 struct nvme_ns *ns;
Keith Busch9ac27092014-01-31 16:53:39 -07001875
Keith Busch9e603522014-10-03 11:15:47 -06001876 spin_lock(&dev_list_lock);
1877 ns = bdev->bd_disk->private_data;
1878 if (!ns)
1879 ret = -ENXIO;
1880 else if (!kref_get_unless_zero(&ns->dev->kref))
1881 ret = -ENXIO;
1882 spin_unlock(&dev_list_lock);
1883
1884 return ret;
Keith Busch9ac27092014-01-31 16:53:39 -07001885}
1886
1887static void nvme_free_dev(struct kref *kref);
1888
1889static void nvme_release(struct gendisk *disk, fmode_t mode)
1890{
1891 struct nvme_ns *ns = disk->private_data;
1892 struct nvme_dev *dev = ns->dev;
1893
1894 kref_put(&dev->kref, nvme_free_dev);
1895}
1896
Keith Busch4cc09e22014-04-02 15:45:37 -06001897static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
1898{
1899 /* some standard values */
1900 geo->heads = 1 << 6;
1901 geo->sectors = 1 << 5;
1902 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1903 return 0;
1904}
1905
Keith Busche1e5e562015-02-19 13:39:03 -07001906static void nvme_config_discard(struct nvme_ns *ns)
1907{
1908 u32 logical_block_size = queue_logical_block_size(ns->queue);
1909 ns->queue->limits.discard_zeroes_data = 0;
1910 ns->queue->limits.discard_alignment = logical_block_size;
1911 ns->queue->limits.discard_granularity = logical_block_size;
1912 ns->queue->limits.max_discard_sectors = 0xffffffff;
1913 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1914}
1915
Keith Busch1b9dbf72014-09-10 17:21:14 -06001916static int nvme_revalidate_disk(struct gendisk *disk)
1917{
1918 struct nvme_ns *ns = disk->private_data;
1919 struct nvme_dev *dev = ns->dev;
1920 struct nvme_id_ns *id;
Keith Buscha67a9512015-04-07 16:57:19 -06001921 u8 lbaf, pi_type;
1922 u16 old_ms;
Keith Busche1e5e562015-02-19 13:39:03 -07001923 unsigned short bs;
Keith Busch1b9dbf72014-09-10 17:21:14 -06001924
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001925 if (nvme_identify_ns(dev, ns->ns_id, &id)) {
1926 dev_warn(dev->dev, "%s: Identify failure\n", __func__);
Keith Busch1b9dbf72014-09-10 17:21:14 -06001927 return 0;
1928 }
1929
Keith Busche1e5e562015-02-19 13:39:03 -07001930 old_ms = ns->ms;
1931 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
Keith Busch1b9dbf72014-09-10 17:21:14 -06001932 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Busche1e5e562015-02-19 13:39:03 -07001933 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Buscha67a9512015-04-07 16:57:19 -06001934 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
Keith Busch1b9dbf72014-09-10 17:21:14 -06001935
Keith Busche1e5e562015-02-19 13:39:03 -07001936 /*
1937 * If identify namespace failed, use default 512 byte block size so
1938 * block layer can use before failing read/write for 0 capacity.
1939 */
1940 if (ns->lba_shift == 0)
1941 ns->lba_shift = 9;
1942 bs = 1 << ns->lba_shift;
1943
1944 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
1945 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
1946 id->dps & NVME_NS_DPS_PI_MASK : 0;
1947
Keith Busch52b68d72015-02-23 09:16:21 -07001948 if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
1949 ns->ms != old_ms ||
Keith Busche1e5e562015-02-19 13:39:03 -07001950 bs != queue_logical_block_size(disk->queue) ||
Keith Buscha67a9512015-04-07 16:57:19 -06001951 (ns->ms && ns->ext)))
Keith Busche1e5e562015-02-19 13:39:03 -07001952 blk_integrity_unregister(disk);
1953
1954 ns->pi_type = pi_type;
1955 blk_queue_logical_block_size(ns->queue, bs);
1956
Keith Busch52b68d72015-02-23 09:16:21 -07001957 if (ns->ms && !blk_get_integrity(disk) && (disk->flags & GENHD_FL_UP) &&
Keith Buscha67a9512015-04-07 16:57:19 -06001958 !ns->ext)
Keith Busche1e5e562015-02-19 13:39:03 -07001959 nvme_init_integrity(ns);
1960
Keith Busch52b68d72015-02-23 09:16:21 -07001961 if (id->ncap == 0 || (ns->ms && !blk_get_integrity(disk)))
Keith Busche1e5e562015-02-19 13:39:03 -07001962 set_capacity(disk, 0);
1963 else
1964 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1965
1966 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1967 nvme_config_discard(ns);
1968
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001969 kfree(id);
Keith Busch1b9dbf72014-09-10 17:21:14 -06001970 return 0;
1971}
1972
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001973static const struct block_device_operations nvme_fops = {
1974 .owner = THIS_MODULE,
1975 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06001976 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07001977 .open = nvme_open,
1978 .release = nvme_release,
Keith Busch4cc09e22014-04-02 15:45:37 -06001979 .getgeo = nvme_getgeo,
Keith Busch1b9dbf72014-09-10 17:21:14 -06001980 .revalidate_disk= nvme_revalidate_disk,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001981};
1982
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001983static int nvme_kthread(void *data)
1984{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001985 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001986
1987 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001988 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001989 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001990 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001991 int i;
Keith Busch07836e62015-02-19 10:34:48 -07001992 if (readl(&dev->bar->csts) & NVME_CSTS_CFS) {
Keith Buschd4b4ff82013-12-10 13:10:37 -07001993 if (work_busy(&dev->reset_work))
1994 continue;
1995 list_del_init(&dev->node);
Christoph Hellwige75ec752015-05-22 11:12:39 +02001996 dev_warn(dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001997 "Failed status: %x, reset controller\n",
1998 readl(&dev->bar->csts));
Tejun Heo9ca97372014-03-07 10:24:49 -05001999 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschd4b4ff82013-12-10 13:10:37 -07002000 queue_work(nvme_workq, &dev->reset_work);
2001 continue;
2002 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002003 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002004 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05002005 if (!nvmeq)
2006 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002007 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04002008 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06002009
2010 while ((i == 0) && (dev->event_limit > 0)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002011 if (nvme_submit_async_admin_req(dev))
Keith Busch6fccf932014-06-18 13:58:57 -06002012 break;
2013 dev->event_limit--;
2014 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002015 spin_unlock_irq(&nvmeq->q_lock);
2016 }
2017 }
2018 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08002019 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002020 }
2021 return 0;
2022}
2023
Keith Busche1e5e562015-02-19 13:39:03 -07002024static void nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002025{
2026 struct nvme_ns *ns;
2027 struct gendisk *disk;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002028 int node = dev_to_node(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002029
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002030 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002031 if (!ns)
Keith Busche1e5e562015-02-19 13:39:03 -07002032 return;
2033
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002034 ns->queue = blk_mq_init_queue(&dev->tagset);
Dan Carpenter9f173b32014-11-05 23:39:09 +03002035 if (IS_ERR(ns->queue))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002036 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07002037 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
2038 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002039 queue_flag_set_unlocked(QUEUE_FLAG_SG_GAPS, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002040 ns->dev = dev;
2041 ns->queue->queuedata = ns;
2042
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002043 disk = alloc_disk_node(0, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002044 if (!disk)
2045 goto out_free_queue;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002046
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002047 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002048 ns->disk = disk;
Keith Busche1e5e562015-02-19 13:39:03 -07002049 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
2050 list_add_tail(&ns->list, &dev->namespaces);
2051
Keith Busche9ef4632012-07-24 15:01:04 -06002052 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06002053 if (dev->max_hw_sectors)
2054 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002055 if (dev->stripe_size)
2056 blk_queue_chunk_sectors(ns->queue, dev->stripe_size >> 9);
Keith Buscha7d2ce22014-04-29 11:41:28 -06002057 if (dev->vwc & NVME_CTRL_VWC_PRESENT)
2058 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002059
2060 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002061 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002062 disk->fops = &nvme_fops;
2063 disk->private_data = ns;
2064 disk->queue = ns->queue;
Keith Buschb3fffde2015-02-03 11:21:42 -07002065 disk->driverfs_dev = dev->device;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002066 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002067 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002068
Keith Busche1e5e562015-02-19 13:39:03 -07002069 /*
2070 * Initialize capacity to 0 until we establish the namespace format and
2071 * setup integrity extentions if necessary. The revalidate_disk after
2072 * add_disk allows the driver to register with integrity if the format
2073 * requires it.
2074 */
2075 set_capacity(disk, 0);
2076 nvme_revalidate_disk(ns->disk);
2077 add_disk(ns->disk);
2078 if (ns->ms)
2079 revalidate_disk(ns->disk);
2080 return;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002081 out_free_queue:
2082 blk_cleanup_queue(ns->queue);
2083 out_free_ns:
2084 kfree(ns);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002085}
2086
Keith Busch42f61422014-03-24 10:46:25 -06002087static void nvme_create_io_queues(struct nvme_dev *dev)
2088{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002089 unsigned i;
Keith Busch42f61422014-03-24 10:46:25 -06002090
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002091 for (i = dev->queue_count; i <= dev->max_qid; i++)
Keith Busch2b25d982014-12-22 12:59:04 -07002092 if (!nvme_alloc_queue(dev, i, dev->q_depth))
Keith Busch42f61422014-03-24 10:46:25 -06002093 break;
2094
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002095 for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
2096 if (nvme_create_queue(dev->queues[i], i))
Keith Busch42f61422014-03-24 10:46:25 -06002097 break;
2098}
2099
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002100static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002101{
2102 int status;
2103 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002104 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002105
Matthew Wilcoxdf348132012-01-11 07:29:56 -07002106 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002107 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04002108 if (status < 0)
2109 return status;
2110 if (status > 0) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002111 dev_err(dev->dev, "Could not set queue count (%d)\n", status);
Keith Buschbadc34d2014-06-23 14:25:35 -06002112 return 0;
Matthew Wilcox27e81662014-04-11 11:58:45 -04002113 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002114 return min(result & 0xffff, result >> 16) + 1;
2115}
2116
Keith Busch9d713c22013-07-15 15:02:24 -06002117static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
2118{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002119 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06002120}
2121
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002122static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002123{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002124 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02002125 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch42f61422014-03-24 10:46:25 -06002126 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002127
Keith Busch42f61422014-03-24 10:46:25 -06002128 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002129 result = set_queue_count(dev, nr_io_queues);
Keith Buschbadc34d2014-06-23 14:25:35 -06002130 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002131 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002132 if (result < nr_io_queues)
2133 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002134
Keith Busch9d713c22013-07-15 15:02:24 -06002135 size = db_bar_size(dev, nr_io_queues);
2136 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002137 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06002138 do {
2139 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
2140 if (dev->bar)
2141 break;
2142 if (!--nr_io_queues)
2143 return -ENOMEM;
2144 size = db_bar_size(dev, nr_io_queues);
2145 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002146 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07002147 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002148 }
2149
Keith Busch9d713c22013-07-15 15:02:24 -06002150 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002151 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002152
Jens Axboee32efbf2014-11-14 09:49:26 -07002153 /*
2154 * If we enable msix early due to not intx, disable it again before
2155 * setting up the full range we need.
2156 */
2157 if (!pdev->irq)
2158 pci_disable_msix(pdev);
2159
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002160 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002161 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002162 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
2163 if (vecs < 0) {
2164 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
2165 if (vecs < 0) {
2166 vecs = 1;
2167 } else {
2168 for (i = 0; i < vecs; i++)
2169 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002170 }
2171 }
2172
Matthew Wilcox063a8092013-06-20 10:53:48 -04002173 /*
2174 * Should investigate if there's a performance win from allocating
2175 * more queues than interrupt vectors; it might allow the submission
2176 * path to scale better, even if the receive path is limited by the
2177 * number of interrupts.
2178 */
2179 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002180 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002181
Matthew Wilcox3193f072014-01-27 15:57:22 -05002182 result = queue_request_irq(dev, adminq, adminq->irqname);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002183 if (result)
Keith Busch22404272013-07-15 15:02:20 -06002184 goto free_queues;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002185
Keith Buschcd638942013-07-15 15:02:23 -06002186 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002187 nvme_free_queues(dev, nr_io_queues + 1);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002188 nvme_create_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06002189
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002190 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002191
Keith Busch22404272013-07-15 15:02:20 -06002192 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002193 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002194 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002195}
2196
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002197/*
2198 * Return: error value if an error occurred setting up the queues or calling
2199 * Identify Device. 0 if these succeeded, even if adding some of the
2200 * namespaces failed. At the moment, these failures are silent. TBD which
2201 * failures should be reported.
2202 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002203static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002204{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002205 struct pci_dev *pdev = to_pci_dev(dev->dev);
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002206 int res;
2207 unsigned nn, i;
Matthew Wilcox51814232011-02-01 16:18:08 -05002208 struct nvme_id_ctrl *ctrl;
Keith Busch159b67d2013-04-09 17:13:20 -06002209 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002210
Christoph Hellwigd29ec822015-05-22 11:12:46 +02002211 res = nvme_identify_ctrl(dev, &ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002212 if (res) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002213 dev_err(dev->dev, "Identify Controller failed (%d)\n", res);
Keith Busche1e5e562015-02-19 13:39:03 -07002214 return -EIO;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002215 }
2216
Matthew Wilcox51814232011-02-01 16:18:08 -05002217 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002218 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002219 dev->abort_limit = ctrl->acl + 1;
Keith Buscha7d2ce22014-04-29 11:41:28 -06002220 dev->vwc = ctrl->vwc;
Matthew Wilcox51814232011-02-01 16:18:08 -05002221 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2222 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2223 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002224 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002225 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002226 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002227 (pdev->device == 0x0953) && ctrl->vs[3]) {
2228 unsigned int max_hw_sectors;
2229
Keith Busch159b67d2013-04-09 17:13:20 -06002230 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002231 max_hw_sectors = dev->stripe_size >> (shift - 9);
2232 if (dev->max_hw_sectors) {
2233 dev->max_hw_sectors = min(max_hw_sectors,
2234 dev->max_hw_sectors);
2235 } else
2236 dev->max_hw_sectors = max_hw_sectors;
2237 }
Christoph Hellwigd29ec822015-05-22 11:12:46 +02002238 kfree(ctrl);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002239
2240 dev->tagset.ops = &nvme_mq_ops;
2241 dev->tagset.nr_hw_queues = dev->online_queues - 1;
2242 dev->tagset.timeout = NVME_IO_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002243 dev->tagset.numa_node = dev_to_node(dev->dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002244 dev->tagset.queue_depth =
2245 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Jens Axboeac3dd5b2015-01-22 12:07:58 -07002246 dev->tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002247 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
2248 dev->tagset.driver_data = dev;
2249
2250 if (blk_mq_alloc_tag_set(&dev->tagset))
Keith Busche1e5e562015-02-19 13:39:03 -07002251 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002252
Keith Busche1e5e562015-02-19 13:39:03 -07002253 for (i = 1; i <= nn; i++)
2254 nvme_alloc_ns(dev, i);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002255
Keith Busche1e5e562015-02-19 13:39:03 -07002256 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002257}
2258
Keith Busch0877cb02013-07-15 15:02:19 -06002259static int nvme_dev_map(struct nvme_dev *dev)
2260{
Keith Busch42f61422014-03-24 10:46:25 -06002261 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002262 int bars, result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002263 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002264
2265 if (pci_enable_device_mem(pdev))
2266 return result;
2267
2268 dev->entry[0].vector = pdev->irq;
2269 pci_set_master(pdev);
2270 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07002271 if (!bars)
2272 goto disable_pci;
2273
Keith Busch0877cb02013-07-15 15:02:19 -06002274 if (pci_request_selected_regions(pdev, bars, "nvme"))
2275 goto disable_pci;
2276
Christoph Hellwige75ec752015-05-22 11:12:39 +02002277 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
2278 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01002279 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002280
Keith Busch0877cb02013-07-15 15:02:19 -06002281 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2282 if (!dev->bar)
2283 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07002284
Keith Busch0e53d182013-12-10 13:10:39 -07002285 if (readl(&dev->bar->csts) == -1) {
2286 result = -ENODEV;
2287 goto unmap;
2288 }
Jens Axboee32efbf2014-11-14 09:49:26 -07002289
2290 /*
2291 * Some devices don't advertse INTx interrupts, pre-enable a single
2292 * MSIX vec for setup. We'll adjust this later.
2293 */
2294 if (!pdev->irq) {
2295 result = pci_enable_msix(pdev, dev->entry, 1);
2296 if (result < 0)
2297 goto unmap;
2298 }
2299
Keith Busch42f61422014-03-24 10:46:25 -06002300 cap = readq(&dev->bar->cap);
2301 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2302 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Keith Busch0877cb02013-07-15 15:02:19 -06002303 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2304
2305 return 0;
2306
Keith Busch0e53d182013-12-10 13:10:39 -07002307 unmap:
2308 iounmap(dev->bar);
2309 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002310 disable:
2311 pci_release_regions(pdev);
2312 disable_pci:
2313 pci_disable_device(pdev);
2314 return result;
2315}
2316
2317static void nvme_dev_unmap(struct nvme_dev *dev)
2318{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002319 struct pci_dev *pdev = to_pci_dev(dev->dev);
2320
2321 if (pdev->msi_enabled)
2322 pci_disable_msi(pdev);
2323 else if (pdev->msix_enabled)
2324 pci_disable_msix(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002325
2326 if (dev->bar) {
2327 iounmap(dev->bar);
2328 dev->bar = NULL;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002329 pci_release_regions(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002330 }
2331
Christoph Hellwige75ec752015-05-22 11:12:39 +02002332 if (pci_is_enabled(pdev))
2333 pci_disable_device(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002334}
2335
Keith Busch4d115422013-12-10 13:10:40 -07002336struct nvme_delq_ctx {
2337 struct task_struct *waiter;
2338 struct kthread_worker *worker;
2339 atomic_t refcount;
2340};
2341
2342static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2343{
2344 dq->waiter = current;
2345 mb();
2346
2347 for (;;) {
2348 set_current_state(TASK_KILLABLE);
2349 if (!atomic_read(&dq->refcount))
2350 break;
2351 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2352 fatal_signal_pending(current)) {
Keith Busch0fb59cb2015-01-07 18:55:50 -07002353 /*
2354 * Disable the controller first since we can't trust it
2355 * at this point, but leave the admin queue enabled
2356 * until all queue deletion requests are flushed.
2357 * FIXME: This may take a while if there are more h/w
2358 * queues than admin tags.
2359 */
Keith Busch4d115422013-12-10 13:10:40 -07002360 set_current_state(TASK_RUNNING);
Keith Busch4d115422013-12-10 13:10:40 -07002361 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
Keith Busch0fb59cb2015-01-07 18:55:50 -07002362 nvme_clear_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07002363 flush_kthread_worker(dq->worker);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002364 nvme_disable_queue(dev, 0);
Keith Busch4d115422013-12-10 13:10:40 -07002365 return;
2366 }
2367 }
2368 set_current_state(TASK_RUNNING);
2369}
2370
2371static void nvme_put_dq(struct nvme_delq_ctx *dq)
2372{
2373 atomic_dec(&dq->refcount);
2374 if (dq->waiter)
2375 wake_up_process(dq->waiter);
2376}
2377
2378static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2379{
2380 atomic_inc(&dq->refcount);
2381 return dq;
2382}
2383
2384static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2385{
2386 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
Keith Busch4d115422013-12-10 13:10:40 -07002387 nvme_put_dq(dq);
2388}
2389
2390static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2391 kthread_work_func_t fn)
2392{
2393 struct nvme_command c;
2394
2395 memset(&c, 0, sizeof(c));
2396 c.delete_queue.opcode = opcode;
2397 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2398
2399 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002400 return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
2401 ADMIN_TIMEOUT);
Keith Busch4d115422013-12-10 13:10:40 -07002402}
2403
2404static void nvme_del_cq_work_handler(struct kthread_work *work)
2405{
2406 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2407 cmdinfo.work);
2408 nvme_del_queue_end(nvmeq);
2409}
2410
2411static int nvme_delete_cq(struct nvme_queue *nvmeq)
2412{
2413 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2414 nvme_del_cq_work_handler);
2415}
2416
2417static void nvme_del_sq_work_handler(struct kthread_work *work)
2418{
2419 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2420 cmdinfo.work);
2421 int status = nvmeq->cmdinfo.status;
2422
2423 if (!status)
2424 status = nvme_delete_cq(nvmeq);
2425 if (status)
2426 nvme_del_queue_end(nvmeq);
2427}
2428
2429static int nvme_delete_sq(struct nvme_queue *nvmeq)
2430{
2431 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2432 nvme_del_sq_work_handler);
2433}
2434
2435static void nvme_del_queue_start(struct kthread_work *work)
2436{
2437 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2438 cmdinfo.work);
Keith Busch4d115422013-12-10 13:10:40 -07002439 if (nvme_delete_sq(nvmeq))
2440 nvme_del_queue_end(nvmeq);
2441}
2442
2443static void nvme_disable_io_queues(struct nvme_dev *dev)
2444{
2445 int i;
2446 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2447 struct nvme_delq_ctx dq;
2448 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2449 &worker, "nvme%d", dev->instance);
2450
2451 if (IS_ERR(kworker_task)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002452 dev_err(dev->dev,
Keith Busch4d115422013-12-10 13:10:40 -07002453 "Failed to create queue del task\n");
2454 for (i = dev->queue_count - 1; i > 0; i--)
2455 nvme_disable_queue(dev, i);
2456 return;
2457 }
2458
2459 dq.waiter = NULL;
2460 atomic_set(&dq.refcount, 0);
2461 dq.worker = &worker;
2462 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002463 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002464
2465 if (nvme_suspend_queue(nvmeq))
2466 continue;
2467 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2468 nvmeq->cmdinfo.worker = dq.worker;
2469 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2470 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2471 }
2472 nvme_wait_dq(&dq, dev);
2473 kthread_stop(kworker_task);
2474}
2475
Dan McLeranb9afca32014-04-07 17:10:11 -06002476/*
2477* Remove the node from the device list and check
2478* for whether or not we need to stop the nvme_thread.
2479*/
2480static void nvme_dev_list_remove(struct nvme_dev *dev)
2481{
2482 struct task_struct *tmp = NULL;
2483
2484 spin_lock(&dev_list_lock);
2485 list_del_init(&dev->node);
2486 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2487 tmp = nvme_thread;
2488 nvme_thread = NULL;
2489 }
2490 spin_unlock(&dev_list_lock);
2491
2492 if (tmp)
2493 kthread_stop(tmp);
2494}
2495
Keith Buschc9d3bf82015-01-07 18:55:52 -07002496static void nvme_freeze_queues(struct nvme_dev *dev)
2497{
2498 struct nvme_ns *ns;
2499
2500 list_for_each_entry(ns, &dev->namespaces, list) {
2501 blk_mq_freeze_queue_start(ns->queue);
2502
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002503 spin_lock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002504 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002505 spin_unlock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002506
2507 blk_mq_cancel_requeue_work(ns->queue);
2508 blk_mq_stop_hw_queues(ns->queue);
2509 }
2510}
2511
2512static void nvme_unfreeze_queues(struct nvme_dev *dev)
2513{
2514 struct nvme_ns *ns;
2515
2516 list_for_each_entry(ns, &dev->namespaces, list) {
2517 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2518 blk_mq_unfreeze_queue(ns->queue);
2519 blk_mq_start_stopped_hw_queues(ns->queue, true);
2520 blk_mq_kick_requeue_list(ns->queue);
2521 }
2522}
2523
Keith Buschf0b50732013-07-15 15:02:21 -06002524static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002525{
Keith Busch22404272013-07-15 15:02:20 -06002526 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06002527 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06002528
Dan McLeranb9afca32014-04-07 17:10:11 -06002529 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002530
Keith Buschc9d3bf82015-01-07 18:55:52 -07002531 if (dev->bar) {
2532 nvme_freeze_queues(dev);
Keith Busch7c1b2452014-06-25 11:18:12 -06002533 csts = readl(&dev->bar->csts);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002534 }
Keith Busch7c1b2452014-06-25 11:18:12 -06002535 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07002536 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002537 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002538 nvme_suspend_queue(nvmeq);
Keith Busch4d115422013-12-10 13:10:40 -07002539 }
2540 } else {
2541 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002542 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002543 nvme_disable_queue(dev, 0);
2544 }
Keith Buschf0b50732013-07-15 15:02:21 -06002545 nvme_dev_unmap(dev);
Keith Busch07836e62015-02-19 10:34:48 -07002546
2547 for (i = dev->queue_count - 1; i >= 0; i--)
2548 nvme_clear_queue(dev->queues[i]);
Keith Buschf0b50732013-07-15 15:02:21 -06002549}
2550
2551static void nvme_dev_remove(struct nvme_dev *dev)
2552{
Keith Busch9ac27092014-01-31 16:53:39 -07002553 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002554
Keith Busch9ac27092014-01-31 16:53:39 -07002555 list_for_each_entry(ns, &dev->namespaces, list) {
Keith Busche1e5e562015-02-19 13:39:03 -07002556 if (ns->disk->flags & GENHD_FL_UP) {
Keith Busch52b68d72015-02-23 09:16:21 -07002557 if (blk_get_integrity(ns->disk))
Keith Busche1e5e562015-02-19 13:39:03 -07002558 blk_integrity_unregister(ns->disk);
Keith Busch9ac27092014-01-31 16:53:39 -07002559 del_gendisk(ns->disk);
Keith Busche1e5e562015-02-19 13:39:03 -07002560 }
Keith Buschcef6a942015-01-07 18:55:51 -07002561 if (!blk_queue_dying(ns->queue)) {
2562 blk_mq_abort_requeue_list(ns->queue);
Keith Busch9ac27092014-01-31 16:53:39 -07002563 blk_cleanup_queue(ns->queue);
Keith Buschcef6a942015-01-07 18:55:51 -07002564 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002565 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002566}
2567
Matthew Wilcox091b6092011-02-10 09:56:01 -05002568static int nvme_setup_prp_pools(struct nvme_dev *dev)
2569{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002570 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05002571 PAGE_SIZE, PAGE_SIZE, 0);
2572 if (!dev->prp_page_pool)
2573 return -ENOMEM;
2574
Matthew Wilcox99802a72011-02-10 10:30:34 -05002575 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02002576 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05002577 256, 256, 0);
2578 if (!dev->prp_small_pool) {
2579 dma_pool_destroy(dev->prp_page_pool);
2580 return -ENOMEM;
2581 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002582 return 0;
2583}
2584
2585static void nvme_release_prp_pools(struct nvme_dev *dev)
2586{
2587 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002588 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002589}
2590
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002591static DEFINE_IDA(nvme_instance_ida);
2592
2593static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002594{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002595 int instance, error;
2596
2597 do {
2598 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2599 return -ENODEV;
2600
2601 spin_lock(&dev_list_lock);
2602 error = ida_get_new(&nvme_instance_ida, &instance);
2603 spin_unlock(&dev_list_lock);
2604 } while (error == -EAGAIN);
2605
2606 if (error)
2607 return -ENODEV;
2608
2609 dev->instance = instance;
2610 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002611}
2612
2613static void nvme_release_instance(struct nvme_dev *dev)
2614{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002615 spin_lock(&dev_list_lock);
2616 ida_remove(&nvme_instance_ida, dev->instance);
2617 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002618}
2619
Keith Busch9ac27092014-01-31 16:53:39 -07002620static void nvme_free_namespaces(struct nvme_dev *dev)
2621{
2622 struct nvme_ns *ns, *next;
2623
2624 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2625 list_del(&ns->list);
Keith Busch9e603522014-10-03 11:15:47 -06002626
2627 spin_lock(&dev_list_lock);
2628 ns->disk->private_data = NULL;
2629 spin_unlock(&dev_list_lock);
2630
Keith Busch9ac27092014-01-31 16:53:39 -07002631 put_disk(ns->disk);
2632 kfree(ns);
2633 }
2634}
2635
Keith Busch5e82e952013-02-19 10:17:58 -07002636static void nvme_free_dev(struct kref *kref)
2637{
2638 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002639
Christoph Hellwige75ec752015-05-22 11:12:39 +02002640 put_device(dev->dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07002641 put_device(dev->device);
Keith Busch9ac27092014-01-31 16:53:39 -07002642 nvme_free_namespaces(dev);
Indraneel M285dffc2014-12-11 08:24:18 -07002643 nvme_release_instance(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002644 blk_mq_free_tag_set(&dev->tagset);
Keith Buschea191d22015-01-07 18:55:49 -07002645 blk_put_queue(dev->admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002646 kfree(dev->queues);
2647 kfree(dev->entry);
2648 kfree(dev);
2649}
2650
2651static int nvme_dev_open(struct inode *inode, struct file *f)
2652{
Keith Buschb3fffde2015-02-03 11:21:42 -07002653 struct nvme_dev *dev;
2654 int instance = iminor(inode);
2655 int ret = -ENODEV;
2656
2657 spin_lock(&dev_list_lock);
2658 list_for_each_entry(dev, &dev_list, node) {
2659 if (dev->instance == instance) {
Keith Busch2e1d8442015-02-12 15:33:00 -07002660 if (!dev->admin_q) {
2661 ret = -EWOULDBLOCK;
2662 break;
2663 }
Keith Buschb3fffde2015-02-03 11:21:42 -07002664 if (!kref_get_unless_zero(&dev->kref))
2665 break;
2666 f->private_data = dev;
2667 ret = 0;
2668 break;
2669 }
2670 }
2671 spin_unlock(&dev_list_lock);
2672
2673 return ret;
Keith Busch5e82e952013-02-19 10:17:58 -07002674}
2675
2676static int nvme_dev_release(struct inode *inode, struct file *f)
2677{
2678 struct nvme_dev *dev = f->private_data;
2679 kref_put(&dev->kref, nvme_free_dev);
2680 return 0;
2681}
2682
2683static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2684{
2685 struct nvme_dev *dev = f->private_data;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002686 struct nvme_ns *ns;
2687
Keith Busch5e82e952013-02-19 10:17:58 -07002688 switch (cmd) {
2689 case NVME_IOCTL_ADMIN_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002690 return nvme_user_cmd(dev, NULL, (void __user *)arg);
Keith Busch7963e522014-09-12 16:07:20 -06002691 case NVME_IOCTL_IO_CMD:
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002692 if (list_empty(&dev->namespaces))
2693 return -ENOTTY;
2694 ns = list_first_entry(&dev->namespaces, struct nvme_ns, list);
2695 return nvme_user_cmd(dev, ns, (void __user *)arg);
Keith Busch4cc06522015-06-05 10:30:08 -06002696 case NVME_IOCTL_RESET:
2697 dev_warn(dev->dev, "resetting controller\n");
2698 return nvme_reset(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002699 default:
2700 return -ENOTTY;
2701 }
2702}
2703
2704static const struct file_operations nvme_dev_fops = {
2705 .owner = THIS_MODULE,
2706 .open = nvme_dev_open,
2707 .release = nvme_dev_release,
2708 .unlocked_ioctl = nvme_dev_ioctl,
2709 .compat_ioctl = nvme_dev_ioctl,
2710};
2711
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002712static void nvme_set_irq_hints(struct nvme_dev *dev)
2713{
2714 struct nvme_queue *nvmeq;
2715 int i;
2716
2717 for (i = 0; i < dev->online_queues; i++) {
2718 nvmeq = dev->queues[i];
2719
Keith Busch42483222015-06-01 09:29:54 -06002720 if (!nvmeq->tags || !(*nvmeq->tags))
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002721 continue;
2722
2723 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
Keith Busch42483222015-06-01 09:29:54 -06002724 blk_mq_tags_cpumask(*nvmeq->tags));
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002725 }
2726}
2727
Keith Buschf0b50732013-07-15 15:02:21 -06002728static int nvme_dev_start(struct nvme_dev *dev)
2729{
2730 int result;
Dan McLeranb9afca32014-04-07 17:10:11 -06002731 bool start_thread = false;
Keith Buschf0b50732013-07-15 15:02:21 -06002732
2733 result = nvme_dev_map(dev);
2734 if (result)
2735 return result;
2736
2737 result = nvme_configure_admin_queue(dev);
2738 if (result)
2739 goto unmap;
2740
2741 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002742 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2743 start_thread = true;
2744 nvme_thread = NULL;
2745 }
Keith Buschf0b50732013-07-15 15:02:21 -06002746 list_add(&dev->node, &dev_list);
2747 spin_unlock(&dev_list_lock);
2748
Dan McLeranb9afca32014-04-07 17:10:11 -06002749 if (start_thread) {
2750 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
Keith Busch387caa52014-09-22 13:46:19 -06002751 wake_up_all(&nvme_kthread_wait);
Dan McLeranb9afca32014-04-07 17:10:11 -06002752 } else
2753 wait_event_killable(nvme_kthread_wait, nvme_thread);
2754
2755 if (IS_ERR_OR_NULL(nvme_thread)) {
2756 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2757 goto disable;
2758 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002759
2760 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002761 result = nvme_alloc_admin_tags(dev);
2762 if (result)
2763 goto disable;
Dan McLeranb9afca32014-04-07 17:10:11 -06002764
Keith Buschf0b50732013-07-15 15:02:21 -06002765 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002766 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07002767 goto free_tags;
Keith Buschf0b50732013-07-15 15:02:21 -06002768
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002769 nvme_set_irq_hints(dev);
2770
Keith Busch1efccc92015-03-31 10:37:17 -06002771 dev->event_limit = 1;
Keith Buschd82e8bf2013-09-05 14:45:07 -06002772 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002773
Keith Busch0fb59cb2015-01-07 18:55:50 -07002774 free_tags:
2775 nvme_dev_remove_admin(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002776 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002777 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06002778 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002779 unmap:
2780 nvme_dev_unmap(dev);
2781 return result;
2782}
2783
Keith Busch9a6b9452013-12-10 13:10:36 -07002784static int nvme_remove_dead_ctrl(void *arg)
2785{
2786 struct nvme_dev *dev = (struct nvme_dev *)arg;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002787 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002788
2789 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06002790 pci_stop_and_remove_bus_device_locked(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002791 kref_put(&dev->kref, nvme_free_dev);
2792 return 0;
2793}
2794
2795static void nvme_remove_disks(struct work_struct *ws)
2796{
Keith Busch9a6b9452013-12-10 13:10:36 -07002797 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2798
Keith Busch5a92e702014-02-21 14:13:44 -07002799 nvme_free_queues(dev, 1);
Keith Busch302c6722014-07-18 11:40:20 -06002800 nvme_dev_remove(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002801}
2802
2803static int nvme_dev_resume(struct nvme_dev *dev)
2804{
2805 int ret;
2806
2807 ret = nvme_dev_start(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002808 if (ret)
Keith Busch9a6b9452013-12-10 13:10:36 -07002809 return ret;
Keith Buschbadc34d2014-06-23 14:25:35 -06002810 if (dev->online_queues < 2) {
Keith Busch9a6b9452013-12-10 13:10:36 -07002811 spin_lock(&dev_list_lock);
Tejun Heo9ca97372014-03-07 10:24:49 -05002812 dev->reset_workfn = nvme_remove_disks;
Keith Busch9a6b9452013-12-10 13:10:36 -07002813 queue_work(nvme_workq, &dev->reset_work);
2814 spin_unlock(&dev_list_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002815 } else {
2816 nvme_unfreeze_queues(dev);
2817 nvme_set_irq_hints(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002818 }
2819 return 0;
2820}
2821
2822static void nvme_dev_reset(struct nvme_dev *dev)
2823{
2824 nvme_dev_shutdown(dev);
2825 if (nvme_dev_resume(dev)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002826 dev_warn(dev->dev, "Device failed to resume\n");
Keith Busch9a6b9452013-12-10 13:10:36 -07002827 kref_get(&dev->kref);
2828 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2829 dev->instance))) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02002830 dev_err(dev->dev,
Keith Busch9a6b9452013-12-10 13:10:36 -07002831 "Failed to start controller remove task\n");
2832 kref_put(&dev->kref, nvme_free_dev);
2833 }
2834 }
2835}
2836
2837static void nvme_reset_failed_dev(struct work_struct *ws)
2838{
2839 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2840 nvme_dev_reset(dev);
2841}
2842
Tejun Heo9ca97372014-03-07 10:24:49 -05002843static void nvme_reset_workfn(struct work_struct *work)
2844{
2845 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
2846 dev->reset_workfn(work);
2847}
2848
Keith Busch4cc06522015-06-05 10:30:08 -06002849static int nvme_reset(struct nvme_dev *dev)
2850{
2851 int ret = -EBUSY;
2852
2853 if (!dev->admin_q || blk_queue_dying(dev->admin_q))
2854 return -ENODEV;
2855
2856 spin_lock(&dev_list_lock);
2857 if (!work_pending(&dev->reset_work)) {
2858 dev->reset_workfn = nvme_reset_failed_dev;
2859 queue_work(nvme_workq, &dev->reset_work);
2860 ret = 0;
2861 }
2862 spin_unlock(&dev_list_lock);
2863
2864 if (!ret) {
2865 flush_work(&dev->reset_work);
2866 return 0;
2867 }
2868
2869 return ret;
2870}
2871
2872static ssize_t nvme_sysfs_reset(struct device *dev,
2873 struct device_attribute *attr, const char *buf,
2874 size_t count)
2875{
2876 struct nvme_dev *ndev = dev_get_drvdata(dev);
2877 int ret;
2878
2879 ret = nvme_reset(ndev);
2880 if (ret < 0)
2881 return ret;
2882
2883 return count;
2884}
2885static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2886
Keith Busch2e1d8442015-02-12 15:33:00 -07002887static void nvme_async_probe(struct work_struct *work);
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002888static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002889{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002890 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002891 struct nvme_dev *dev;
2892
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002893 node = dev_to_node(&pdev->dev);
2894 if (node == NUMA_NO_NODE)
2895 set_dev_node(&pdev->dev, 0);
2896
2897 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002898 if (!dev)
2899 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002900 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
2901 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002902 if (!dev->entry)
2903 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002904 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2905 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002906 if (!dev->queues)
2907 goto free;
2908
2909 INIT_LIST_HEAD(&dev->namespaces);
Tejun Heo9ca97372014-03-07 10:24:49 -05002910 dev->reset_workfn = nvme_reset_failed_dev;
2911 INIT_WORK(&dev->reset_work, nvme_reset_workfn);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002912 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002913 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002914 result = nvme_set_instance(dev);
2915 if (result)
Keith Buscha96d4f52014-08-19 19:15:59 -06002916 goto put_pci;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002917
Matthew Wilcox091b6092011-02-10 09:56:01 -05002918 result = nvme_setup_prp_pools(dev);
2919 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002920 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002921
Keith Buschfb35e912014-03-03 11:09:47 -07002922 kref_init(&dev->kref);
Keith Buschb3fffde2015-02-03 11:21:42 -07002923 dev->device = device_create(nvme_class, &pdev->dev,
2924 MKDEV(nvme_char_major, dev->instance),
2925 dev, "nvme%d", dev->instance);
2926 if (IS_ERR(dev->device)) {
2927 result = PTR_ERR(dev->device);
Keith Busch2e1d8442015-02-12 15:33:00 -07002928 goto release_pools;
Keith Buschb3fffde2015-02-03 11:21:42 -07002929 }
2930 get_device(dev->device);
Keith Busch4cc06522015-06-05 10:30:08 -06002931 dev_set_drvdata(dev->device, dev);
2932
2933 result = device_create_file(dev->device, &dev_attr_reset_controller);
2934 if (result)
2935 goto put_dev;
Keith Buschb3fffde2015-02-03 11:21:42 -07002936
Keith Busche6e96d72015-03-23 09:32:37 -06002937 INIT_LIST_HEAD(&dev->node);
Keith Busch2e1d8442015-02-12 15:33:00 -07002938 INIT_WORK(&dev->probe_work, nvme_async_probe);
2939 schedule_work(&dev->probe_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002940 return 0;
2941
Keith Busch4cc06522015-06-05 10:30:08 -06002942 put_dev:
2943 device_destroy(nvme_class, MKDEV(nvme_char_major, dev->instance));
2944 put_device(dev->device);
Keith Busch0877cb02013-07-15 15:02:19 -06002945 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05002946 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002947 release:
2948 nvme_release_instance(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002949 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02002950 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002951 free:
2952 kfree(dev->queues);
2953 kfree(dev->entry);
2954 kfree(dev);
2955 return result;
2956}
2957
Keith Busch2e1d8442015-02-12 15:33:00 -07002958static void nvme_async_probe(struct work_struct *work)
2959{
2960 struct nvme_dev *dev = container_of(work, struct nvme_dev, probe_work);
2961 int result;
2962
2963 result = nvme_dev_start(dev);
2964 if (result)
2965 goto reset;
2966
2967 if (dev->online_queues > 1)
2968 result = nvme_dev_add(dev);
2969 if (result)
2970 goto reset;
2971
2972 nvme_set_irq_hints(dev);
Keith Busch2e1d8442015-02-12 15:33:00 -07002973 return;
2974 reset:
Keith Busch4cc06522015-06-05 10:30:08 -06002975 spin_lock(&dev_list_lock);
Keith Busch07836e62015-02-19 10:34:48 -07002976 if (!work_busy(&dev->reset_work)) {
2977 dev->reset_workfn = nvme_reset_failed_dev;
2978 queue_work(nvme_workq, &dev->reset_work);
2979 }
Keith Busch4cc06522015-06-05 10:30:08 -06002980 spin_unlock(&dev_list_lock);
Keith Busch2e1d8442015-02-12 15:33:00 -07002981}
2982
Keith Buschf0d54a52014-05-02 10:40:43 -06002983static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2984{
Keith Buscha6739472014-06-23 16:03:21 -06002985 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002986
Keith Buscha6739472014-06-23 16:03:21 -06002987 if (prepare)
2988 nvme_dev_shutdown(dev);
2989 else
2990 nvme_dev_resume(dev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002991}
2992
Keith Busch09ece142014-01-27 11:29:40 -05002993static void nvme_shutdown(struct pci_dev *pdev)
2994{
2995 struct nvme_dev *dev = pci_get_drvdata(pdev);
2996 nvme_dev_shutdown(dev);
2997}
2998
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002999static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003000{
3001 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07003002
3003 spin_lock(&dev_list_lock);
3004 list_del_init(&dev->node);
3005 spin_unlock(&dev_list_lock);
3006
3007 pci_set_drvdata(pdev, NULL);
Keith Busch2e1d8442015-02-12 15:33:00 -07003008 flush_work(&dev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07003009 flush_work(&dev->reset_work);
Keith Busch4cc06522015-06-05 10:30:08 -06003010 device_remove_file(dev->device, &dev_attr_reset_controller);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003011 nvme_dev_shutdown(dev);
Keith Buschc9d3bf82015-01-07 18:55:52 -07003012 nvme_dev_remove(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003013 nvme_dev_remove_admin(dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07003014 device_destroy(nvme_class, MKDEV(nvme_char_major, dev->instance));
Matias Bjørlinga4aea562014-11-04 08:20:14 -07003015 nvme_free_queues(dev, 0);
Keith Busch9a6b9452013-12-10 13:10:36 -07003016 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07003017 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003018}
3019
3020/* These functions are yet to be implemented */
3021#define nvme_error_detected NULL
3022#define nvme_dump_registers NULL
3023#define nvme_link_reset NULL
3024#define nvme_slot_reset NULL
3025#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06003026
Jingoo Han671a6012014-02-13 11:19:14 +09003027#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06003028static int nvme_suspend(struct device *dev)
3029{
3030 struct pci_dev *pdev = to_pci_dev(dev);
3031 struct nvme_dev *ndev = pci_get_drvdata(pdev);
3032
3033 nvme_dev_shutdown(ndev);
3034 return 0;
3035}
3036
3037static int nvme_resume(struct device *dev)
3038{
3039 struct pci_dev *pdev = to_pci_dev(dev);
3040 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06003041
Keith Busch9a6b9452013-12-10 13:10:36 -07003042 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Tejun Heo9ca97372014-03-07 10:24:49 -05003043 ndev->reset_workfn = nvme_reset_failed_dev;
Keith Busch9a6b9452013-12-10 13:10:36 -07003044 queue_work(nvme_workq, &ndev->reset_work);
3045 }
3046 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06003047}
Jingoo Han671a6012014-02-13 11:19:14 +09003048#endif
Keith Buschcd638942013-07-15 15:02:23 -06003049
3050static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003051
Stephen Hemminger1d352032012-09-07 09:33:17 -07003052static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003053 .error_detected = nvme_error_detected,
3054 .mmio_enabled = nvme_dump_registers,
3055 .link_reset = nvme_link_reset,
3056 .slot_reset = nvme_slot_reset,
3057 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06003058 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003059};
3060
3061/* Move to pci_ids.h later */
3062#define PCI_CLASS_STORAGE_EXPRESS 0x010802
3063
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003064static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003065 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
3066 { 0, }
3067};
3068MODULE_DEVICE_TABLE(pci, nvme_id_table);
3069
3070static struct pci_driver nvme_driver = {
3071 .name = "nvme",
3072 .id_table = nvme_id_table,
3073 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003074 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05003075 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06003076 .driver = {
3077 .pm = &nvme_dev_pm_ops,
3078 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003079 .err_handler = &nvme_err_handler,
3080};
3081
3082static int __init nvme_init(void)
3083{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003084 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003085
Dan McLeranb9afca32014-04-07 17:10:11 -06003086 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003087
Keith Busch9a6b9452013-12-10 13:10:36 -07003088 nvme_workq = create_singlethread_workqueue("nvme");
3089 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06003090 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07003091
Keith Busch5c42ea12012-07-25 16:05:18 -06003092 result = register_blkdev(nvme_major, "nvme");
3093 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07003094 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06003095 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003096 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003097
Keith Buschb3fffde2015-02-03 11:21:42 -07003098 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
3099 &nvme_dev_fops);
3100 if (result < 0)
3101 goto unregister_blkdev;
3102 else if (result > 0)
3103 nvme_char_major = result;
3104
3105 nvme_class = class_create(THIS_MODULE, "nvme");
Alexey Khoroshilovc7270402015-03-07 01:43:41 +03003106 if (IS_ERR(nvme_class)) {
3107 result = PTR_ERR(nvme_class);
Keith Buschb3fffde2015-02-03 11:21:42 -07003108 goto unregister_chrdev;
Alexey Khoroshilovc7270402015-03-07 01:43:41 +03003109 }
Keith Buschb3fffde2015-02-03 11:21:42 -07003110
Keith Buschf3db22f2014-06-11 11:51:35 -06003111 result = pci_register_driver(&nvme_driver);
3112 if (result)
Keith Buschb3fffde2015-02-03 11:21:42 -07003113 goto destroy_class;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003114 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003115
Keith Buschb3fffde2015-02-03 11:21:42 -07003116 destroy_class:
3117 class_destroy(nvme_class);
3118 unregister_chrdev:
3119 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003120 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003121 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003122 kill_workq:
3123 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003124 return result;
3125}
3126
3127static void __exit nvme_exit(void)
3128{
3129 pci_unregister_driver(&nvme_driver);
3130 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003131 destroy_workqueue(nvme_workq);
Keith Buschb3fffde2015-02-03 11:21:42 -07003132 class_destroy(nvme_class);
3133 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Dan McLeranb9afca32014-04-07 17:10:11 -06003134 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04003135 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003136}
3137
3138MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
3139MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07003140MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003141module_init(nvme_init);
3142module_exit(nvme_exit);