blob: 16a886c2b73d21f795b6953817e705b9f9192ab9 [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>
16#include <linux/bio.h>
Matthew Wilcox8de05532011-05-12 13:50:28 -040017#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050018#include <linux/blkdev.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>
Keith Busch42f61422014-03-24 10:46:25 -060036#include <linux/percpu.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050037#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040038#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050039#include <linux/sched.h>
40#include <linux/slab.h>
41#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 Busch3291fa52014-04-28 12:30:52 -060045#include <trace/events/block.h>
46
Keith Busch9d43cf62014-05-13 11:42:02 -060047#define NVME_Q_DEPTH 1024
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050048#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
49#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Keith Busch9d43cf62014-05-13 11:42:02 -060050#define ADMIN_TIMEOUT (admin_timeout * HZ)
Dan McLeran2484f402014-07-01 09:33:32 -060051#define SHUTDOWN_TIMEOUT (shutdown_timeout * HZ)
Keith Busch9d43cf62014-05-13 11:42:02 -060052#define IOD_TIMEOUT (retry_time * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050053
Keith Busch9d43cf62014-05-13 11:42:02 -060054static unsigned char admin_timeout = 60;
55module_param(admin_timeout, byte, 0644);
56MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050057
Matthew Wilcoxbd676082014-06-03 23:04:30 -040058unsigned char nvme_io_timeout = 30;
59module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
Keith Buschb3550842014-04-04 11:43:36 -060060MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050061
Keith Busch61e4ce02014-05-13 11:42:01 -060062static unsigned char retry_time = 30;
63module_param(retry_time, byte, 0644);
64MODULE_PARM_DESC(retry_time, "time in seconds to retry failed I/O");
65
Dan McLeran2484f402014-07-01 09:33:32 -060066static unsigned char shutdown_timeout = 5;
67module_param(shutdown_timeout, byte, 0644);
68MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
69
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050070static int nvme_major;
71module_param(nvme_major, int, 0);
72
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050073static int use_threaded_interrupts;
74module_param(use_threaded_interrupts, int, 0);
75
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050076static DEFINE_SPINLOCK(dev_list_lock);
77static LIST_HEAD(dev_list);
78static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070079static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060080static wait_queue_head_t nvme_kthread_wait;
Keith Buschf3db22f2014-06-11 11:51:35 -060081static struct notifier_block nvme_nb;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050082
Keith Buschd4b4ff82013-12-10 13:10:37 -070083static void nvme_reset_failed_dev(struct work_struct *ws);
84
Keith Busch4d115422013-12-10 13:10:40 -070085struct async_cmd_info {
86 struct kthread_work work;
87 struct kthread_worker *worker;
88 u32 result;
89 int status;
90 void *ctx;
91};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050092
93/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050094 * An NVM Express queue. Each device has at least two (one for admin
95 * commands and one for I/O commands).
96 */
97struct nvme_queue {
Keith Buschf435c282014-07-07 09:14:42 -060098 struct llist_node node;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050099 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500100 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500101 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500102 spinlock_t q_lock;
103 struct nvme_command *sq_cmds;
104 volatile struct nvme_completion *cqes;
105 dma_addr_t sq_dma_addr;
106 dma_addr_t cq_dma_addr;
107 wait_queue_head_t sq_full;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -0500108 wait_queue_t sq_cong_wait;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500109 struct bio_list sq_cong;
Keith Buschedd10d32014-04-03 16:45:23 -0600110 struct list_head iod_bio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500111 u32 __iomem *q_db;
112 u16 q_depth;
113 u16 cq_vector;
114 u16 sq_head;
115 u16 sq_tail;
116 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700117 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400118 u8 cq_phase;
119 u8 cqe_seen;
Keith Busch22404272013-07-15 15:02:20 -0600120 u8 q_suspended;
Keith Busch42f61422014-03-24 10:46:25 -0600121 cpumask_var_t cpu_mask;
Keith Busch4d115422013-12-10 13:10:40 -0700122 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500123 unsigned long cmdid_data[];
124};
125
126/*
127 * Check we didin't inadvertently grow the command struct
128 */
129static inline void _nvme_check_size(void)
130{
131 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
132 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
133 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
134 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
135 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400136 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700137 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500138 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
139 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
140 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
141 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600142 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500143}
144
Keith Buschedd10d32014-04-03 16:45:23 -0600145typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400146 struct nvme_completion *);
147
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500148struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400149 nvme_completion_fn fn;
150 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500151 unsigned long timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700152 int aborted;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500153};
154
155static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
156{
157 return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
158}
159
Keith Busch22404272013-07-15 15:02:20 -0600160static unsigned nvme_queue_extra(int depth)
161{
162 return DIV_ROUND_UP(depth, 8) + (depth * sizeof(struct nvme_cmd_info));
163}
164
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500165/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400166 * alloc_cmdid() - Allocate a Command ID
167 * @nvmeq: The queue that will be used for this command
168 * @ctx: A pointer that will be passed to the handler
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400169 * @handler: The function to call on completion
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500170 *
171 * Allocate a Command ID for a queue. The data passed in will
172 * be passed to the completion handler. This is implemented by using
173 * the bottom two bits of the ctx pointer to store the handler ID.
174 * Passing in a pointer that's not 4-byte aligned will cause a BUG.
175 * We can change this if it becomes a problem.
Matthew Wilcox184d2942011-05-11 21:36:38 -0400176 *
177 * May be called with local interrupts disabled and the q_lock held,
178 * or with interrupts enabled and no locks held.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500179 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400180static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx,
181 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500182{
Matthew Wilcoxe6d15f72011-02-24 08:49:41 -0500183 int depth = nvmeq->q_depth - 1;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500184 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500185 int cmdid;
186
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500187 do {
188 cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
189 if (cmdid >= depth)
190 return -EBUSY;
191 } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
192
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400193 info[cmdid].fn = handler;
194 info[cmdid].ctx = ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500195 info[cmdid].timeout = jiffies + timeout;
Keith Buschc30341d2013-12-10 13:10:38 -0700196 info[cmdid].aborted = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500197 return cmdid;
198}
199
200static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400201 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500202{
203 int cmdid;
204 wait_event_killable(nvmeq->sq_full,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500205 (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500206 return (cmdid < 0) ? -EINTR : cmdid;
207}
208
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400209/* Special values must be less than 0x1000 */
210#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500211#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
212#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
213#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Keith Busch53562be2014-04-29 11:41:29 -0600214#define CMD_CTX_ABORT (0x318 + CMD_CTX_BASE)
Keith Busch6fccf932014-06-18 13:58:57 -0600215#define CMD_CTX_ASYNC (0x31C + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500216
Keith Buschedd10d32014-04-03 16:45:23 -0600217static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400218 struct nvme_completion *cqe)
219{
220 if (ctx == CMD_CTX_CANCELLED)
221 return;
Keith Buschc30341d2013-12-10 13:10:38 -0700222 if (ctx == CMD_CTX_ABORT) {
Keith Buschedd10d32014-04-03 16:45:23 -0600223 ++nvmeq->dev->abort_limit;
Keith Buschc30341d2013-12-10 13:10:38 -0700224 return;
225 }
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400226 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600227 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400228 "completed id %d twice on queue %d\n",
229 cqe->command_id, le16_to_cpup(&cqe->sq_id));
230 return;
231 }
232 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600233 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400234 "invalid id %d completed on queue %d\n",
235 cqe->command_id, le16_to_cpup(&cqe->sq_id));
236 return;
237 }
Keith Busch6fccf932014-06-18 13:58:57 -0600238 if (ctx == CMD_CTX_ASYNC) {
239 u32 result = le32_to_cpup(&cqe->result);
240 u16 status = le16_to_cpup(&cqe->status) >> 1;
241
242 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
243 ++nvmeq->dev->event_limit;
244 if (status == NVME_SC_SUCCESS)
245 dev_warn(nvmeq->q_dmadev,
246 "async event result %08x\n", result);
247 return;
248 }
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400249
Keith Buschedd10d32014-04-03 16:45:23 -0600250 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400251}
252
Keith Buschedd10d32014-04-03 16:45:23 -0600253static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700254 struct nvme_completion *cqe)
255{
256 struct async_cmd_info *cmdinfo = ctx;
257 cmdinfo->result = le32_to_cpup(&cqe->result);
258 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
259 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
260}
261
Matthew Wilcox184d2942011-05-11 21:36:38 -0400262/*
263 * Called with local interrupts disabled and the q_lock held. May not sleep.
264 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400265static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid,
266 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500267{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400268 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500269 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500270
Keith Busch94bbac42014-04-24 18:53:50 -0600271 if (cmdid >= nvmeq->q_depth || !info[cmdid].fn) {
272 if (fn)
273 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500274 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400275 }
Keith Busch859361a2012-08-02 14:05:59 -0600276 if (fn)
277 *fn = info[cmdid].fn;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400278 ctx = info[cmdid].ctx;
279 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500280 info[cmdid].ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500281 clear_bit(cmdid, nvmeq->cmdid_data);
282 wake_up(&nvmeq->sq_full);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400283 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500284}
285
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400286static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
287 nvme_completion_fn *fn)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500288{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400289 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500290 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400291 if (fn)
292 *fn = info[cmdid].fn;
293 ctx = info[cmdid].ctx;
294 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500295 info[cmdid].ctx = CMD_CTX_CANCELLED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400296 return ctx;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500297}
298
Keith Busch5a92e702014-02-21 14:13:44 -0700299static struct nvme_queue *raw_nvmeq(struct nvme_dev *dev, int qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500300{
Keith Busch5a92e702014-02-21 14:13:44 -0700301 return rcu_dereference_raw(dev->queues[qid]);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500302}
303
Keith Busch4f5099a2014-03-03 16:39:13 -0700304static struct nvme_queue *get_nvmeq(struct nvme_dev *dev) __acquires(RCU)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500305{
Keith Buscha51afb52014-05-13 10:32:46 -0600306 struct nvme_queue *nvmeq;
Keith Busch42f61422014-03-24 10:46:25 -0600307 unsigned queue_id = get_cpu_var(*dev->io_queue);
Keith Buscha51afb52014-05-13 10:32:46 -0600308
Keith Busch5a92e702014-02-21 14:13:44 -0700309 rcu_read_lock();
Keith Buscha51afb52014-05-13 10:32:46 -0600310 nvmeq = rcu_dereference(dev->queues[queue_id]);
311 if (nvmeq)
312 return nvmeq;
313
314 rcu_read_unlock();
315 put_cpu_var(*dev->io_queue);
316 return NULL;
Keith Busch5a92e702014-02-21 14:13:44 -0700317}
318
Keith Busch4f5099a2014-03-03 16:39:13 -0700319static void put_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500320{
Keith Busch5a92e702014-02-21 14:13:44 -0700321 rcu_read_unlock();
Keith Busch42f61422014-03-24 10:46:25 -0600322 put_cpu_var(nvmeq->dev->io_queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500323}
324
Keith Busch4f5099a2014-03-03 16:39:13 -0700325static struct nvme_queue *lock_nvmeq(struct nvme_dev *dev, int q_idx)
326 __acquires(RCU)
327{
Keith Buscha51afb52014-05-13 10:32:46 -0600328 struct nvme_queue *nvmeq;
329
Keith Busch4f5099a2014-03-03 16:39:13 -0700330 rcu_read_lock();
Keith Buscha51afb52014-05-13 10:32:46 -0600331 nvmeq = rcu_dereference(dev->queues[q_idx]);
332 if (nvmeq)
333 return nvmeq;
334
335 rcu_read_unlock();
336 return NULL;
Keith Busch4f5099a2014-03-03 16:39:13 -0700337}
338
339static void unlock_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
340{
341 rcu_read_unlock();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500342}
343
344/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400345 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500346 * @nvmeq: The queue to use
347 * @cmd: The command to send
348 *
349 * Safe to use from interrupt context
350 */
351static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
352{
353 unsigned long flags;
354 u16 tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500355 spin_lock_irqsave(&nvmeq->q_lock, flags);
Keith Busch4f5099a2014-03-03 16:39:13 -0700356 if (nvmeq->q_suspended) {
357 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
358 return -EBUSY;
359 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500360 tail = nvmeq->sq_tail;
361 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500362 if (++tail == nvmeq->q_depth)
363 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500364 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500365 nvmeq->sq_tail = tail;
366 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
367
368 return 0;
369}
370
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500371static __le64 **iod_list(struct nvme_iod *iod)
372{
373 return ((void *)iod) + iod->offset;
374}
375
376/*
377 * Will slightly overestimate the number of pages needed. This is OK
378 * as it only leads to a small amount of wasted memory for the lifetime of
379 * the I/O.
380 */
Keith Busch1d090622014-06-23 11:34:01 -0600381static int nvme_npages(unsigned size, struct nvme_dev *dev)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500382{
Keith Busch1d090622014-06-23 11:34:01 -0600383 unsigned nprps = DIV_ROUND_UP(size + dev->page_size, dev->page_size);
384 return DIV_ROUND_UP(8 * nprps, dev->page_size - 8);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500385}
386
387static struct nvme_iod *
Keith Busch1d090622014-06-23 11:34:01 -0600388nvme_alloc_iod(unsigned nseg, unsigned nbytes, struct nvme_dev *dev, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500389{
390 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Keith Busch1d090622014-06-23 11:34:01 -0600391 sizeof(__le64 *) * nvme_npages(nbytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500392 sizeof(struct scatterlist) * nseg, gfp);
393
394 if (iod) {
395 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
396 iod->npages = -1;
397 iod->length = nbytes;
Keith Busch2b196032012-11-06 11:59:23 -0700398 iod->nents = 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600399 iod->first_dma = 0ULL;
Keith Busch61982212013-05-29 15:59:39 -0600400 iod->start_time = jiffies;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500401 }
402
403 return iod;
404}
405
Vishal Verma5d0f6132013-03-04 18:40:58 -0700406void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500407{
Keith Busch1d090622014-06-23 11:34:01 -0600408 const int last_prp = dev->page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500409 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500410 __le64 **list = iod_list(iod);
411 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500412
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500413 if (iod->npages == 0)
414 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
415 for (i = 0; i < iod->npages; i++) {
416 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500417 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500418 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500419 prp_dma = next_prp_dma;
420 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500421 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500422}
423
Keith Busch61982212013-05-29 15:59:39 -0600424static void nvme_start_io_acct(struct bio *bio)
425{
426 struct gendisk *disk = bio->bi_bdev->bd_disk;
Sam Bradshawb4e75cb2014-05-09 13:27:07 -0700427 if (blk_queue_io_stat(disk->queue)) {
428 const int rw = bio_data_dir(bio);
429 int cpu = part_stat_lock();
430 part_round_stats(cpu, &disk->part0);
431 part_stat_inc(cpu, &disk->part0, ios[rw]);
432 part_stat_add(cpu, &disk->part0, sectors[rw],
433 bio_sectors(bio));
434 part_inc_in_flight(&disk->part0, rw);
435 part_stat_unlock();
436 }
Keith Busch61982212013-05-29 15:59:39 -0600437}
438
439static void nvme_end_io_acct(struct bio *bio, unsigned long start_time)
440{
441 struct gendisk *disk = bio->bi_bdev->bd_disk;
Sam Bradshawb4e75cb2014-05-09 13:27:07 -0700442 if (blk_queue_io_stat(disk->queue)) {
443 const int rw = bio_data_dir(bio);
444 unsigned long duration = jiffies - start_time;
445 int cpu = part_stat_lock();
446 part_stat_add(cpu, &disk->part0, ticks[rw], duration);
447 part_round_stats(cpu, &disk->part0);
448 part_dec_in_flight(&disk->part0, rw);
449 part_stat_unlock();
450 }
Keith Busch61982212013-05-29 15:59:39 -0600451}
452
Keith Buschb4ff9c82014-08-29 09:06:12 -0600453static int nvme_error_status(u16 status)
454{
455 switch (status & 0x7ff) {
456 case NVME_SC_SUCCESS:
457 return 0;
458 case NVME_SC_CAP_EXCEEDED:
459 return -ENOSPC;
460 default:
461 return -EIO;
462 }
463}
464
Keith Buschedd10d32014-04-03 16:45:23 -0600465static void bio_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500466 struct nvme_completion *cqe)
467{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500468 struct nvme_iod *iod = ctx;
469 struct bio *bio = iod->private;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500470 u16 status = le16_to_cpup(&cqe->status) >> 1;
Keith Busch3291fa52014-04-28 12:30:52 -0600471 int error = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500472
Keith Buschedd10d32014-04-03 16:45:23 -0600473 if (unlikely(status)) {
474 if (!(status & NVME_SC_DNR ||
475 bio->bi_rw & REQ_FAILFAST_MASK) &&
476 (jiffies - iod->start_time) < IOD_TIMEOUT) {
477 if (!waitqueue_active(&nvmeq->sq_full))
478 add_wait_queue(&nvmeq->sq_full,
479 &nvmeq->sq_cong_wait);
480 list_add_tail(&iod->node, &nvmeq->iod_bio);
481 wake_up(&nvmeq->sq_full);
482 return;
483 }
Keith Buschb4ff9c82014-08-29 09:06:12 -0600484 error = nvme_error_status(status);
Keith Buschedd10d32014-04-03 16:45:23 -0600485 }
Keith Busch9e59d092013-08-08 10:25:38 -0600486 if (iod->nents) {
Keith Buschedd10d32014-04-03 16:45:23 -0600487 dma_unmap_sg(nvmeq->q_dmadev, iod->sg, iod->nents,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500488 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busch9e59d092013-08-08 10:25:38 -0600489 nvme_end_io_acct(bio, iod->start_time);
490 }
Keith Buschedd10d32014-04-03 16:45:23 -0600491 nvme_free_iod(nvmeq->dev, iod);
Keith Busch3291fa52014-04-28 12:30:52 -0600492
493 trace_block_bio_complete(bdev_get_queue(bio->bi_bdev), bio, error);
494 bio_endio(bio, error);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500495}
496
Matthew Wilcox184d2942011-05-11 21:36:38 -0400497/* length is in bytes. gfp flags indicates whether we may sleep. */
Keith Buschedd10d32014-04-03 16:45:23 -0600498int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod, int total_len,
499 gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500500{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500501 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500502 int length = total_len;
503 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500504 int dma_len = sg_dma_len(sg);
505 u64 dma_addr = sg_dma_address(sg);
506 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500507 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500508 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500509 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500510 int nprps, i;
Keith Busch1d090622014-06-23 11:34:01 -0600511 u32 page_size = dev->page_size;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500512
Keith Busch1d090622014-06-23 11:34:01 -0600513 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500514 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500515 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500516
Keith Busch1d090622014-06-23 11:34:01 -0600517 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500518 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600519 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500520 } else {
521 sg = sg_next(sg);
522 dma_addr = sg_dma_address(sg);
523 dma_len = sg_dma_len(sg);
524 }
525
Keith Busch1d090622014-06-23 11:34:01 -0600526 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600527 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500528 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500529 }
530
Keith Busch1d090622014-06-23 11:34:01 -0600531 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500532 if (nprps <= (256 / 8)) {
533 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500534 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500535 } else {
536 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500537 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500538 }
539
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400540 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
541 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600542 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500543 iod->npages = -1;
Keith Busch1d090622014-06-23 11:34:01 -0600544 return (total_len - length) + page_size;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400545 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500546 list[0] = prp_list;
547 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500548 i = 0;
549 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600550 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500551 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400552 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500553 if (!prp_list)
554 return total_len - length;
555 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400556 prp_list[0] = old_prp_list[i - 1];
557 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
558 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500559 }
560 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600561 dma_len -= page_size;
562 dma_addr += page_size;
563 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500564 if (length <= 0)
565 break;
566 if (dma_len > 0)
567 continue;
568 BUG_ON(dma_len < 0);
569 sg = sg_next(sg);
570 dma_addr = sg_dma_address(sg);
571 dma_len = sg_dma_len(sg);
572 }
573
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500574 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500575}
576
Keith Busch427e9702013-04-09 11:59:32 -0600577static int nvme_split_and_submit(struct bio *bio, struct nvme_queue *nvmeq,
Kent Overstreet20d01892013-11-23 18:21:01 -0800578 int len)
Keith Busch427e9702013-04-09 11:59:32 -0600579{
Kent Overstreet20d01892013-11-23 18:21:01 -0800580 struct bio *split = bio_split(bio, len >> 9, GFP_ATOMIC, NULL);
581 if (!split)
Keith Busch427e9702013-04-09 11:59:32 -0600582 return -ENOMEM;
583
Keith Busch3291fa52014-04-28 12:30:52 -0600584 trace_block_split(bdev_get_queue(bio->bi_bdev), bio,
585 split->bi_iter.bi_sector);
Kent Overstreet20d01892013-11-23 18:21:01 -0800586 bio_chain(split, bio);
587
Keith Buschedd10d32014-04-03 16:45:23 -0600588 if (!waitqueue_active(&nvmeq->sq_full))
Keith Busch427e9702013-04-09 11:59:32 -0600589 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
Kent Overstreet20d01892013-11-23 18:21:01 -0800590 bio_list_add(&nvmeq->sq_cong, split);
591 bio_list_add(&nvmeq->sq_cong, bio);
Keith Buschedd10d32014-04-03 16:45:23 -0600592 wake_up(&nvmeq->sq_full);
Keith Busch427e9702013-04-09 11:59:32 -0600593
594 return 0;
595}
596
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500597/* NVMe scatterlists require no holes in the virtual address */
598#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
599 (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
600
Keith Busch427e9702013-04-09 11:59:32 -0600601static int nvme_map_bio(struct nvme_queue *nvmeq, struct nvme_iod *iod,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500602 struct bio *bio, enum dma_data_direction dma_dir, int psegs)
603{
Kent Overstreet79886132013-11-23 17:19:00 -0800604 struct bio_vec bvec, bvprv;
605 struct bvec_iter iter;
Matthew Wilcox76830842011-02-10 13:55:39 -0500606 struct scatterlist *sg = NULL;
Kent Overstreet79886132013-11-23 17:19:00 -0800607 int length = 0, nsegs = 0, split_len = bio->bi_iter.bi_size;
608 int first = 1;
Keith Busch159b67d2013-04-09 17:13:20 -0600609
610 if (nvmeq->dev->stripe_size)
611 split_len = nvmeq->dev->stripe_size -
Kent Overstreet4f024f32013-10-11 15:44:27 -0700612 ((bio->bi_iter.bi_sector << 9) &
613 (nvmeq->dev->stripe_size - 1));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500614
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500615 sg_init_table(iod->sg, psegs);
Kent Overstreet79886132013-11-23 17:19:00 -0800616 bio_for_each_segment(bvec, bio, iter) {
617 if (!first && BIOVEC_PHYS_MERGEABLE(&bvprv, &bvec)) {
618 sg->length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500619 } else {
Kent Overstreet79886132013-11-23 17:19:00 -0800620 if (!first && BIOVEC_NOT_VIRT_MERGEABLE(&bvprv, &bvec))
621 return nvme_split_and_submit(bio, nvmeq,
Kent Overstreet20d01892013-11-23 18:21:01 -0800622 length);
Keith Busch427e9702013-04-09 11:59:32 -0600623
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500624 sg = sg ? sg + 1 : iod->sg;
Kent Overstreet79886132013-11-23 17:19:00 -0800625 sg_set_page(sg, bvec.bv_page,
626 bvec.bv_len, bvec.bv_offset);
Matthew Wilcox76830842011-02-10 13:55:39 -0500627 nsegs++;
628 }
Keith Busch159b67d2013-04-09 17:13:20 -0600629
Kent Overstreet79886132013-11-23 17:19:00 -0800630 if (split_len - length < bvec.bv_len)
Kent Overstreet20d01892013-11-23 18:21:01 -0800631 return nvme_split_and_submit(bio, nvmeq, split_len);
Kent Overstreet79886132013-11-23 17:19:00 -0800632 length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500633 bvprv = bvec;
Kent Overstreet79886132013-11-23 17:19:00 -0800634 first = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500635 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500636 iod->nents = nsegs;
Matthew Wilcox76830842011-02-10 13:55:39 -0500637 sg_mark_end(sg);
Keith Busch427e9702013-04-09 11:59:32 -0600638 if (dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir) == 0)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500639 return -ENOMEM;
Keith Busch427e9702013-04-09 11:59:32 -0600640
Kent Overstreet4f024f32013-10-11 15:44:27 -0700641 BUG_ON(length != bio->bi_iter.bi_size);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500642 return length;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500643}
644
Keith Busch0e5e4f02012-11-09 16:33:05 -0700645static int nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
646 struct bio *bio, struct nvme_iod *iod, int cmdid)
647{
Keith Buschedd10d32014-04-03 16:45:23 -0600648 struct nvme_dsm_range *range =
649 (struct nvme_dsm_range *)iod_list(iod)[0];
Keith Busch0e5e4f02012-11-09 16:33:05 -0700650 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
651
Keith Busch0e5e4f02012-11-09 16:33:05 -0700652 range->cattr = cpu_to_le32(0);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700653 range->nlb = cpu_to_le32(bio->bi_iter.bi_size >> ns->lba_shift);
654 range->slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700655
656 memset(cmnd, 0, sizeof(*cmnd));
657 cmnd->dsm.opcode = nvme_cmd_dsm;
658 cmnd->dsm.command_id = cmdid;
659 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
660 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
661 cmnd->dsm.nr = 0;
662 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
663
664 if (++nvmeq->sq_tail == nvmeq->q_depth)
665 nvmeq->sq_tail = 0;
666 writel(nvmeq->sq_tail, nvmeq->q_db);
667
668 return 0;
669}
670
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500671static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
672 int cmdid)
673{
674 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
675
676 memset(cmnd, 0, sizeof(*cmnd));
677 cmnd->common.opcode = nvme_cmd_flush;
678 cmnd->common.command_id = cmdid;
679 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
680
681 if (++nvmeq->sq_tail == nvmeq->q_depth)
682 nvmeq->sq_tail = 0;
683 writel(nvmeq->sq_tail, nvmeq->q_db);
684
685 return 0;
686}
687
Keith Buschedd10d32014-04-03 16:45:23 -0600688static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500689{
Keith Buschedd10d32014-04-03 16:45:23 -0600690 struct bio *bio = iod->private;
691 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500692 struct nvme_command *cmnd;
Keith Buschedd10d32014-04-03 16:45:23 -0600693 int cmdid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500694 u16 control;
695 u32 dsmgmt;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500696
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500697 cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500698 if (unlikely(cmdid < 0))
Keith Buschedd10d32014-04-03 16:45:23 -0600699 return cmdid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500700
Keith Buschedd10d32014-04-03 16:45:23 -0600701 if (bio->bi_rw & REQ_DISCARD)
702 return nvme_submit_discard(nvmeq, ns, bio, iod, cmdid);
Keith Busch53562be2014-04-29 11:41:29 -0600703 if (bio->bi_rw & REQ_FLUSH)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500704 return nvme_submit_flush(nvmeq, ns, cmdid);
705
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500706 control = 0;
707 if (bio->bi_rw & REQ_FUA)
708 control |= NVME_RW_FUA;
709 if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
710 control |= NVME_RW_LR;
711
712 dsmgmt = 0;
713 if (bio->bi_rw & REQ_RAHEAD)
714 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
715
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500716 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500717 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500718
Keith Buschedd10d32014-04-03 16:45:23 -0600719 cmnd->rw.opcode = bio_data_dir(bio) ? nvme_cmd_write : nvme_cmd_read;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500720 cmnd->rw.command_id = cmdid;
721 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Keith Buschedd10d32014-04-03 16:45:23 -0600722 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
723 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700724 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Keith Buschedd10d32014-04-03 16:45:23 -0600725 cmnd->rw.length =
726 cpu_to_le16((bio->bi_iter.bi_size >> ns->lba_shift) - 1);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500727 cmnd->rw.control = cpu_to_le16(control);
728 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500729
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500730 if (++nvmeq->sq_tail == nvmeq->q_depth)
731 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500732 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500733
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500734 return 0;
Keith Buschedd10d32014-04-03 16:45:23 -0600735}
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500736
Keith Busch53562be2014-04-29 11:41:29 -0600737static int nvme_split_flush_data(struct nvme_queue *nvmeq, struct bio *bio)
738{
739 struct bio *split = bio_clone(bio, GFP_ATOMIC);
740 if (!split)
741 return -ENOMEM;
742
743 split->bi_iter.bi_size = 0;
744 split->bi_phys_segments = 0;
745 bio->bi_rw &= ~REQ_FLUSH;
746 bio_chain(split, bio);
747
748 if (!waitqueue_active(&nvmeq->sq_full))
749 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
750 bio_list_add(&nvmeq->sq_cong, split);
751 bio_list_add(&nvmeq->sq_cong, bio);
752 wake_up_process(nvme_thread);
753
754 return 0;
755}
756
Keith Buschedd10d32014-04-03 16:45:23 -0600757/*
758 * Called with local interrupts disabled and the q_lock held. May not sleep.
759 */
760static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
761 struct bio *bio)
762{
763 struct nvme_iod *iod;
764 int psegs = bio_phys_segments(ns->queue, bio);
765 int result;
766
Keith Busch53562be2014-04-29 11:41:29 -0600767 if ((bio->bi_rw & REQ_FLUSH) && psegs)
768 return nvme_split_flush_data(nvmeq, bio);
Keith Buschedd10d32014-04-03 16:45:23 -0600769
Keith Busch1d090622014-06-23 11:34:01 -0600770 iod = nvme_alloc_iod(psegs, bio->bi_iter.bi_size, ns->dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600771 if (!iod)
772 return -ENOMEM;
773
774 iod->private = bio;
775 if (bio->bi_rw & REQ_DISCARD) {
776 void *range;
777 /*
778 * We reuse the small pool to allocate the 16-byte range here
779 * as it is not worth having a special pool for these or
780 * additional cases to handle freeing the iod.
781 */
782 range = dma_pool_alloc(nvmeq->dev->prp_small_pool,
783 GFP_ATOMIC,
784 &iod->first_dma);
785 if (!range) {
786 result = -ENOMEM;
787 goto free_iod;
788 }
789 iod_list(iod)[0] = (__le64 *)range;
790 iod->npages = 0;
791 } else if (psegs) {
792 result = nvme_map_bio(nvmeq, iod, bio,
793 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE,
794 psegs);
795 if (result <= 0)
796 goto free_iod;
797 if (nvme_setup_prps(nvmeq->dev, iod, result, GFP_ATOMIC) !=
798 result) {
799 result = -ENOMEM;
800 goto free_iod;
801 }
802 nvme_start_io_acct(bio);
803 }
804 if (unlikely(nvme_submit_iod(nvmeq, iod))) {
805 if (!waitqueue_active(&nvmeq->sq_full))
806 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
807 list_add_tail(&iod->node, &nvmeq->iod_bio);
808 }
809 return 0;
810
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500811 free_iod:
812 nvme_free_iod(nvmeq->dev, iod);
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500813 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500814}
815
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400816static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500817{
Matthew Wilcox82123462011-01-20 13:24:06 -0500818 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500819
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500820 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500821 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500822
823 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400824 void *ctx;
825 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500826 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500827 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500828 break;
829 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
830 if (++head == nvmeq->q_depth) {
831 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500832 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500833 }
834
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400835 ctx = free_cmdid(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600836 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500837 }
838
839 /* If the controller ignores the cq head doorbell and continuously
840 * writes to the queue, it is theoretically possible to wrap around
841 * the queue twice and mistakenly return IRQ_NONE. Linux only
842 * requires that 0.1% of your interrupts are handled, so this isn't
843 * a big problem.
844 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500845 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400846 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500847
Haiyan Hub80d5cc2013-09-10 11:25:37 +0800848 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500849 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500850 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500851
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400852 nvmeq->cqe_seen = 1;
853 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500854}
855
Matthew Wilcox7d822452013-06-24 12:03:57 -0400856static void nvme_make_request(struct request_queue *q, struct bio *bio)
857{
858 struct nvme_ns *ns = q->queuedata;
859 struct nvme_queue *nvmeq = get_nvmeq(ns->dev);
860 int result = -EBUSY;
861
Keith Buschcd638942013-07-15 15:02:23 -0600862 if (!nvmeq) {
Keith Buschcd638942013-07-15 15:02:23 -0600863 bio_endio(bio, -EIO);
864 return;
865 }
866
Matthew Wilcox7d822452013-06-24 12:03:57 -0400867 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -0600868 if (!nvmeq->q_suspended && bio_list_empty(&nvmeq->sq_cong))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400869 result = nvme_submit_bio_queue(nvmeq, ns, bio);
870 if (unlikely(result)) {
Keith Buschedd10d32014-04-03 16:45:23 -0600871 if (!waitqueue_active(&nvmeq->sq_full))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400872 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
873 bio_list_add(&nvmeq->sq_cong, bio);
874 }
875
876 nvme_process_cq(nvmeq);
877 spin_unlock_irq(&nvmeq->q_lock);
878 put_nvmeq(nvmeq);
879}
880
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500881static irqreturn_t nvme_irq(int irq, void *data)
882{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500883 irqreturn_t result;
884 struct nvme_queue *nvmeq = data;
885 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400886 nvme_process_cq(nvmeq);
887 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
888 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500889 spin_unlock(&nvmeq->q_lock);
890 return result;
891}
892
893static irqreturn_t nvme_irq_check(int irq, void *data)
894{
895 struct nvme_queue *nvmeq = data;
896 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
897 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
898 return IRQ_NONE;
899 return IRQ_WAKE_THREAD;
900}
901
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500902static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
903{
904 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400905 cancel_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500906 spin_unlock_irq(&nvmeq->q_lock);
907}
908
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400909struct sync_cmd_info {
910 struct task_struct *task;
911 u32 result;
912 int status;
913};
914
Keith Buschedd10d32014-04-03 16:45:23 -0600915static void sync_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400916 struct nvme_completion *cqe)
917{
918 struct sync_cmd_info *cmdinfo = ctx;
919 cmdinfo->result = le32_to_cpup(&cqe->result);
920 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
921 wake_up_process(cmdinfo->task);
922}
923
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500924/*
925 * Returns 0 on success. If the result is negative, it's a Linux error code;
926 * if the result is positive, it's an NVM Express status code
927 */
Keith Busch4f5099a2014-03-03 16:39:13 -0700928static int nvme_submit_sync_cmd(struct nvme_dev *dev, int q_idx,
929 struct nvme_command *cmd,
Vishal Verma5d0f6132013-03-04 18:40:58 -0700930 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500931{
Keith Busch4f5099a2014-03-03 16:39:13 -0700932 int cmdid, ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500933 struct sync_cmd_info cmdinfo;
Keith Busch4f5099a2014-03-03 16:39:13 -0700934 struct nvme_queue *nvmeq;
935
936 nvmeq = lock_nvmeq(dev, q_idx);
Keith Buscha51afb52014-05-13 10:32:46 -0600937 if (!nvmeq)
Keith Busch4f5099a2014-03-03 16:39:13 -0700938 return -ENODEV;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500939
940 cmdinfo.task = current;
941 cmdinfo.status = -EINTR;
942
Keith Busch4f5099a2014-03-03 16:39:13 -0700943 cmdid = alloc_cmdid(nvmeq, &cmdinfo, sync_completion, timeout);
944 if (cmdid < 0) {
945 unlock_nvmeq(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500946 return cmdid;
Keith Busch4f5099a2014-03-03 16:39:13 -0700947 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500948 cmd->common.command_id = cmdid;
949
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500950 set_current_state(TASK_KILLABLE);
Keith Busch4f5099a2014-03-03 16:39:13 -0700951 ret = nvme_submit_cmd(nvmeq, cmd);
952 if (ret) {
953 free_cmdid(nvmeq, cmdid, NULL);
954 unlock_nvmeq(nvmeq);
955 set_current_state(TASK_RUNNING);
956 return ret;
957 }
958 unlock_nvmeq(nvmeq);
Keith Busch78f8d252013-04-19 14:11:06 -0600959 schedule_timeout(timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500960
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500961 if (cmdinfo.status == -EINTR) {
Keith Busch4f5099a2014-03-03 16:39:13 -0700962 nvmeq = lock_nvmeq(dev, q_idx);
Keith Buscha51afb52014-05-13 10:32:46 -0600963 if (nvmeq) {
Keith Busch4f5099a2014-03-03 16:39:13 -0700964 nvme_abort_command(nvmeq, cmdid);
Keith Buscha51afb52014-05-13 10:32:46 -0600965 unlock_nvmeq(nvmeq);
966 }
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500967 return -EINTR;
968 }
969
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500970 if (result)
971 *result = cmdinfo.result;
972
973 return cmdinfo.status;
974}
975
Keith Busch4d115422013-12-10 13:10:40 -0700976static int nvme_submit_async_cmd(struct nvme_queue *nvmeq,
977 struct nvme_command *cmd,
978 struct async_cmd_info *cmdinfo, unsigned timeout)
979{
980 int cmdid;
981
982 cmdid = alloc_cmdid_killable(nvmeq, cmdinfo, async_completion, timeout);
983 if (cmdid < 0)
984 return cmdid;
985 cmdinfo->status = -EINTR;
986 cmd->common.command_id = cmdid;
Keith Busch4f5099a2014-03-03 16:39:13 -0700987 return nvme_submit_cmd(nvmeq, cmd);
Keith Busch4d115422013-12-10 13:10:40 -0700988}
989
Vishal Verma5d0f6132013-03-04 18:40:58 -0700990int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500991 u32 *result)
992{
Keith Busch4f5099a2014-03-03 16:39:13 -0700993 return nvme_submit_sync_cmd(dev, 0, cmd, result, ADMIN_TIMEOUT);
994}
995
996int nvme_submit_io_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
997 u32 *result)
998{
Sam Bradshaw59055352014-07-29 13:31:36 -0700999 return nvme_submit_sync_cmd(dev, this_cpu_read(*dev->io_queue), cmd,
1000 result, NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001001}
1002
Keith Busch4d115422013-12-10 13:10:40 -07001003static int nvme_submit_admin_cmd_async(struct nvme_dev *dev,
1004 struct nvme_command *cmd, struct async_cmd_info *cmdinfo)
1005{
Keith Busch5a92e702014-02-21 14:13:44 -07001006 return nvme_submit_async_cmd(raw_nvmeq(dev, 0), cmd, cmdinfo,
Keith Busch4d115422013-12-10 13:10:40 -07001007 ADMIN_TIMEOUT);
1008}
1009
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001010static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1011{
1012 int status;
1013 struct nvme_command c;
1014
1015 memset(&c, 0, sizeof(c));
1016 c.delete_queue.opcode = opcode;
1017 c.delete_queue.qid = cpu_to_le16(id);
1018
1019 status = nvme_submit_admin_cmd(dev, &c, NULL);
1020 if (status)
1021 return -EIO;
1022 return 0;
1023}
1024
1025static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1026 struct nvme_queue *nvmeq)
1027{
1028 int status;
1029 struct nvme_command c;
1030 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1031
1032 memset(&c, 0, sizeof(c));
1033 c.create_cq.opcode = nvme_admin_create_cq;
1034 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1035 c.create_cq.cqid = cpu_to_le16(qid);
1036 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1037 c.create_cq.cq_flags = cpu_to_le16(flags);
1038 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1039
1040 status = nvme_submit_admin_cmd(dev, &c, NULL);
1041 if (status)
1042 return -EIO;
1043 return 0;
1044}
1045
1046static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1047 struct nvme_queue *nvmeq)
1048{
1049 int status;
1050 struct nvme_command c;
1051 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1052
1053 memset(&c, 0, sizeof(c));
1054 c.create_sq.opcode = nvme_admin_create_sq;
1055 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1056 c.create_sq.sqid = cpu_to_le16(qid);
1057 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1058 c.create_sq.sq_flags = cpu_to_le16(flags);
1059 c.create_sq.cqid = cpu_to_le16(qid);
1060
1061 status = nvme_submit_admin_cmd(dev, &c, NULL);
1062 if (status)
1063 return -EIO;
1064 return 0;
1065}
1066
1067static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1068{
1069 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1070}
1071
1072static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1073{
1074 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1075}
1076
Vishal Verma5d0f6132013-03-04 18:40:58 -07001077int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001078 dma_addr_t dma_addr)
1079{
1080 struct nvme_command c;
1081
1082 memset(&c, 0, sizeof(c));
1083 c.identify.opcode = nvme_admin_identify;
1084 c.identify.nsid = cpu_to_le32(nsid);
1085 c.identify.prp1 = cpu_to_le64(dma_addr);
1086 c.identify.cns = cpu_to_le32(cns);
1087
1088 return nvme_submit_admin_cmd(dev, &c, NULL);
1089}
1090
Vishal Verma5d0f6132013-03-04 18:40:58 -07001091int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -06001092 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001093{
1094 struct nvme_command c;
1095
1096 memset(&c, 0, sizeof(c));
1097 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -06001098 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001099 c.features.prp1 = cpu_to_le64(dma_addr);
1100 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001101
Keith Busch08df1e02012-09-21 10:52:13 -06001102 return nvme_submit_admin_cmd(dev, &c, result);
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001103}
1104
Vishal Verma5d0f6132013-03-04 18:40:58 -07001105int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
1106 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001107{
1108 struct nvme_command c;
1109
1110 memset(&c, 0, sizeof(c));
1111 c.features.opcode = nvme_admin_set_features;
1112 c.features.prp1 = cpu_to_le64(dma_addr);
1113 c.features.fid = cpu_to_le32(fid);
1114 c.features.dword11 = cpu_to_le32(dword11);
1115
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001116 return nvme_submit_admin_cmd(dev, &c, result);
1117}
1118
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001119/**
Keith Buschc30341d2013-12-10 13:10:38 -07001120 * nvme_abort_cmd - Attempt aborting a command
1121 * @cmdid: Command id of a timed out IO
1122 * @queue: The queue with timed out IO
1123 *
1124 * Schedule controller reset if the command was already aborted once before and
1125 * still hasn't been returned to the driver, or if this is the admin queue.
1126 */
1127static void nvme_abort_cmd(int cmdid, struct nvme_queue *nvmeq)
1128{
1129 int a_cmdid;
1130 struct nvme_command cmd;
1131 struct nvme_dev *dev = nvmeq->dev;
1132 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Keith Busch5a92e702014-02-21 14:13:44 -07001133 struct nvme_queue *adminq;
Keith Buschc30341d2013-12-10 13:10:38 -07001134
1135 if (!nvmeq->qid || info[cmdid].aborted) {
1136 if (work_busy(&dev->reset_work))
1137 return;
1138 list_del_init(&dev->node);
1139 dev_warn(&dev->pci_dev->dev,
1140 "I/O %d QID %d timeout, reset controller\n", cmdid,
1141 nvmeq->qid);
Tejun Heo9ca97372014-03-07 10:24:49 -05001142 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschc30341d2013-12-10 13:10:38 -07001143 queue_work(nvme_workq, &dev->reset_work);
1144 return;
1145 }
1146
1147 if (!dev->abort_limit)
1148 return;
1149
Keith Busch5a92e702014-02-21 14:13:44 -07001150 adminq = rcu_dereference(dev->queues[0]);
1151 a_cmdid = alloc_cmdid(adminq, CMD_CTX_ABORT, special_completion,
Keith Buschc30341d2013-12-10 13:10:38 -07001152 ADMIN_TIMEOUT);
1153 if (a_cmdid < 0)
1154 return;
1155
1156 memset(&cmd, 0, sizeof(cmd));
1157 cmd.abort.opcode = nvme_admin_abort_cmd;
1158 cmd.abort.cid = cmdid;
1159 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
1160 cmd.abort.command_id = a_cmdid;
1161
1162 --dev->abort_limit;
1163 info[cmdid].aborted = 1;
1164 info[cmdid].timeout = jiffies + ADMIN_TIMEOUT;
1165
1166 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", cmdid,
1167 nvmeq->qid);
Keith Busch5a92e702014-02-21 14:13:44 -07001168 nvme_submit_cmd(adminq, &cmd);
Keith Buschc30341d2013-12-10 13:10:38 -07001169}
1170
1171/**
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001172 * nvme_cancel_ios - Cancel outstanding I/Os
1173 * @queue: The queue to cancel I/Os on
1174 * @timeout: True to only cancel I/Os which have timed out
1175 */
1176static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout)
1177{
1178 int depth = nvmeq->q_depth - 1;
1179 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
1180 unsigned long now = jiffies;
1181 int cmdid;
1182
1183 for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
1184 void *ctx;
1185 nvme_completion_fn fn;
1186 static struct nvme_completion cqe = {
Matthew Wilcoxaf2d9ca2013-04-16 15:18:30 -04001187 .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1),
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001188 };
1189
1190 if (timeout && !time_after(now, info[cmdid].timeout))
1191 continue;
Keith Busch053ab7022013-04-30 11:19:38 -06001192 if (info[cmdid].ctx == CMD_CTX_CANCELLED)
1193 continue;
Keith Busch6fccf932014-06-18 13:58:57 -06001194 if (timeout && info[cmdid].ctx == CMD_CTX_ASYNC)
1195 continue;
Keith Buschc30341d2013-12-10 13:10:38 -07001196 if (timeout && nvmeq->dev->initialized) {
1197 nvme_abort_cmd(cmdid, nvmeq);
1198 continue;
1199 }
1200 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n", cmdid,
1201 nvmeq->qid);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001202 ctx = cancel_cmdid(nvmeq, cmdid, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -06001203 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001204 }
1205}
1206
Keith Buschf435c282014-07-07 09:14:42 -06001207static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001208{
Keith Busch22404272013-07-15 15:02:20 -06001209 spin_lock_irq(&nvmeq->q_lock);
1210 while (bio_list_peek(&nvmeq->sq_cong)) {
1211 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1212 bio_endio(bio, -EIO);
1213 }
Keith Buschedd10d32014-04-03 16:45:23 -06001214 while (!list_empty(&nvmeq->iod_bio)) {
1215 static struct nvme_completion cqe = {
1216 .status = cpu_to_le16(
1217 (NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1),
1218 };
1219 struct nvme_iod *iod = list_first_entry(&nvmeq->iod_bio,
1220 struct nvme_iod,
1221 node);
1222 list_del(&iod->node);
1223 bio_completion(nvmeq, iod, &cqe);
1224 }
Keith Busch22404272013-07-15 15:02:20 -06001225 spin_unlock_irq(&nvmeq->q_lock);
1226
Matthew Wilcox9e866772012-08-03 13:55:56 -04001227 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1228 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1229 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1230 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
Keith Busch42f61422014-03-24 10:46:25 -06001231 if (nvmeq->qid)
1232 free_cpumask_var(nvmeq->cpu_mask);
Matthew Wilcox9e866772012-08-03 13:55:56 -04001233 kfree(nvmeq);
1234}
1235
Keith Buscha1a5ef92013-12-16 13:50:00 -05001236static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001237{
Keith Buschf435c282014-07-07 09:14:42 -06001238 LLIST_HEAD(q_list);
1239 struct nvme_queue *nvmeq, *next;
1240 struct llist_node *entry;
Keith Busch22404272013-07-15 15:02:20 -06001241 int i;
1242
Keith Buscha1a5ef92013-12-16 13:50:00 -05001243 for (i = dev->queue_count - 1; i >= lowest; i--) {
Keith Buschf435c282014-07-07 09:14:42 -06001244 nvmeq = raw_nvmeq(dev, i);
Andreea-Cristina Bernat062261b2014-08-18 17:41:01 +03001245 RCU_INIT_POINTER(dev->queues[i], NULL);
Keith Buschf435c282014-07-07 09:14:42 -06001246 llist_add(&nvmeq->node, &q_list);
Keith Busch22404272013-07-15 15:02:20 -06001247 dev->queue_count--;
Keith Busch22404272013-07-15 15:02:20 -06001248 }
Keith Buschf435c282014-07-07 09:14:42 -06001249 synchronize_rcu();
1250 entry = llist_del_all(&q_list);
1251 llist_for_each_entry_safe(nvmeq, next, entry, node)
1252 nvme_free_queue(nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001253}
1254
Keith Busch4d115422013-12-10 13:10:40 -07001255/**
1256 * nvme_suspend_queue - put queue into suspended state
1257 * @nvmeq - queue to suspend
1258 *
1259 * Returns 1 if already suspended, 0 otherwise.
1260 */
1261static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001262{
Keith Busch4d115422013-12-10 13:10:40 -07001263 int vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001264
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001265 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001266 if (nvmeq->q_suspended) {
1267 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07001268 return 1;
Keith Busch32958742012-08-20 14:57:49 -06001269 }
Keith Busch22404272013-07-15 15:02:20 -06001270 nvmeq->q_suspended = 1;
Keith Busch42f61422014-03-24 10:46:25 -06001271 nvmeq->dev->online_queues--;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001272 spin_unlock_irq(&nvmeq->q_lock);
1273
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001274 irq_set_affinity_hint(vector, NULL);
1275 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001276
Keith Busch4d115422013-12-10 13:10:40 -07001277 return 0;
1278}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001279
Keith Busch4d115422013-12-10 13:10:40 -07001280static void nvme_clear_queue(struct nvme_queue *nvmeq)
1281{
Keith Busch22404272013-07-15 15:02:20 -06001282 spin_lock_irq(&nvmeq->q_lock);
1283 nvme_process_cq(nvmeq);
1284 nvme_cancel_ios(nvmeq, false);
1285 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001286}
1287
Keith Busch4d115422013-12-10 13:10:40 -07001288static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1289{
Keith Busch5a92e702014-02-21 14:13:44 -07001290 struct nvme_queue *nvmeq = raw_nvmeq(dev, qid);
Keith Busch4d115422013-12-10 13:10:40 -07001291
1292 if (!nvmeq)
1293 return;
1294 if (nvme_suspend_queue(nvmeq))
1295 return;
1296
Keith Busch0e53d182013-12-10 13:10:39 -07001297 /* Don't tell the adapter to delete the admin queue.
1298 * Don't tell a removed adapter to delete IO queues. */
1299 if (qid && readl(&dev->bar->csts) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001300 adapter_delete_sq(dev, qid);
1301 adapter_delete_cq(dev, qid);
1302 }
Keith Busch4d115422013-12-10 13:10:40 -07001303 nvme_clear_queue(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001304}
1305
1306static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
1307 int depth, int vector)
1308{
1309 struct device *dmadev = &dev->pci_dev->dev;
Keith Busch22404272013-07-15 15:02:20 -06001310 unsigned extra = nvme_queue_extra(depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001311 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
1312 if (!nvmeq)
1313 return NULL;
1314
Joe Perches4d51abf2014-06-15 13:37:33 -07001315 nvmeq->cqes = dma_zalloc_coherent(dmadev, CQ_SIZE(depth),
1316 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001317 if (!nvmeq->cqes)
1318 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001319
1320 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
1321 &nvmeq->sq_dma_addr, GFP_KERNEL);
1322 if (!nvmeq->sq_cmds)
1323 goto free_cqdma;
1324
Keith Busch42f61422014-03-24 10:46:25 -06001325 if (qid && !zalloc_cpumask_var(&nvmeq->cpu_mask, GFP_KERNEL))
1326 goto free_sqdma;
1327
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001328 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001329 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001330 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1331 dev->instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001332 spin_lock_init(&nvmeq->q_lock);
1333 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001334 nvmeq->cq_phase = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001335 init_waitqueue_head(&nvmeq->sq_full);
1336 bio_list_init(&nvmeq->sq_cong);
Keith Buschedd10d32014-04-03 16:45:23 -06001337 INIT_LIST_HEAD(&nvmeq->iod_bio);
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001338 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001339 nvmeq->q_depth = depth;
1340 nvmeq->cq_vector = vector;
Keith Buschc30341d2013-12-10 13:10:38 -07001341 nvmeq->qid = qid;
Keith Busch22404272013-07-15 15:02:20 -06001342 nvmeq->q_suspended = 1;
1343 dev->queue_count++;
Keith Busch5a92e702014-02-21 14:13:44 -07001344 rcu_assign_pointer(dev->queues[qid], nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001345
1346 return nvmeq;
1347
Keith Busch42f61422014-03-24 10:46:25 -06001348 free_sqdma:
1349 dma_free_coherent(dmadev, SQ_SIZE(depth), (void *)nvmeq->sq_cmds,
1350 nvmeq->sq_dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001351 free_cqdma:
Keith Busch68b8eca2013-05-01 13:07:47 -06001352 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001353 nvmeq->cq_dma_addr);
1354 free_nvmeq:
1355 kfree(nvmeq);
1356 return NULL;
1357}
1358
Matthew Wilcox30010822011-01-20 09:10:15 -05001359static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1360 const char *name)
1361{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001362 if (use_threaded_interrupts)
1363 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001364 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001365 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001366 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001367 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001368}
1369
Keith Busch22404272013-07-15 15:02:20 -06001370static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001371{
Keith Busch22404272013-07-15 15:02:20 -06001372 struct nvme_dev *dev = nvmeq->dev;
1373 unsigned extra = nvme_queue_extra(nvmeq->q_depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001374
Keith Busch7be50e92014-09-10 15:48:47 -06001375 spin_lock_irq(&nvmeq->q_lock);
1376 init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
Keith Busch22404272013-07-15 15:02:20 -06001377 nvmeq->sq_tail = 0;
1378 nvmeq->cq_head = 0;
1379 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001380 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001381 memset(nvmeq->cmdid_data, 0, extra);
1382 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
1383 nvme_cancel_ios(nvmeq, false);
1384 nvmeq->q_suspended = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001385 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001386 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001387}
1388
1389static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1390{
1391 struct nvme_dev *dev = nvmeq->dev;
1392 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001393
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001394 result = adapter_alloc_cq(dev, qid, nvmeq);
1395 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001396 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001397
1398 result = adapter_alloc_sq(dev, qid, nvmeq);
1399 if (result < 0)
1400 goto release_cq;
1401
Matthew Wilcox3193f072014-01-27 15:57:22 -05001402 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001403 if (result < 0)
1404 goto release_sq;
1405
Keith Busch22404272013-07-15 15:02:20 -06001406 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001407 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001408
1409 release_sq:
1410 adapter_delete_sq(dev, qid);
1411 release_cq:
1412 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001413 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001414}
1415
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001416static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1417{
1418 unsigned long timeout;
1419 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1420
1421 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1422
1423 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1424 msleep(100);
1425 if (fatal_signal_pending(current))
1426 return -EINTR;
1427 if (time_after(jiffies, timeout)) {
1428 dev_err(&dev->pci_dev->dev,
Matthew Wilcox27e81662014-04-11 11:58:45 -04001429 "Device not ready; aborting %s\n", enabled ?
1430 "initialisation" : "reset");
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001431 return -ENODEV;
1432 }
1433 }
1434
1435 return 0;
1436}
1437
1438/*
1439 * If the device has been passed off to us in an enabled state, just clear
1440 * the enabled bit. The spec says we should set the 'shutdown notification
1441 * bits', but doing so may cause the device to complete commands to the
1442 * admin queue ... and we don't know what memory that might be pointing at!
1443 */
1444static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1445{
Dan McLeran01079522014-06-23 08:24:36 -06001446 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1447 dev->ctrl_config &= ~NVME_CC_ENABLE;
1448 writel(dev->ctrl_config, &dev->bar->cc);
Matthew Wilcox44af1462013-05-04 06:43:17 -04001449
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001450 return nvme_wait_ready(dev, cap, false);
1451}
1452
1453static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1454{
Dan McLeran01079522014-06-23 08:24:36 -06001455 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1456 dev->ctrl_config |= NVME_CC_ENABLE;
1457 writel(dev->ctrl_config, &dev->bar->cc);
1458
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001459 return nvme_wait_ready(dev, cap, true);
1460}
1461
Keith Busch1894d8f2013-07-15 15:02:22 -06001462static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1463{
1464 unsigned long timeout;
Keith Busch1894d8f2013-07-15 15:02:22 -06001465
Dan McLeran01079522014-06-23 08:24:36 -06001466 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1467 dev->ctrl_config |= NVME_CC_SHN_NORMAL;
1468
1469 writel(dev->ctrl_config, &dev->bar->cc);
Keith Busch1894d8f2013-07-15 15:02:22 -06001470
Dan McLeran2484f402014-07-01 09:33:32 -06001471 timeout = SHUTDOWN_TIMEOUT + jiffies;
Keith Busch1894d8f2013-07-15 15:02:22 -06001472 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1473 NVME_CSTS_SHST_CMPLT) {
1474 msleep(100);
1475 if (fatal_signal_pending(current))
1476 return -EINTR;
1477 if (time_after(jiffies, timeout)) {
1478 dev_err(&dev->pci_dev->dev,
1479 "Device shutdown incomplete; abort shutdown\n");
1480 return -ENODEV;
1481 }
1482 }
1483
1484 return 0;
1485}
1486
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001487static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001488{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001489 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001490 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001491 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001492 struct nvme_queue *nvmeq;
Keith Busch1d090622014-06-23 11:34:01 -06001493 unsigned page_shift = PAGE_SHIFT;
1494 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12;
1495 unsigned dev_page_max = NVME_CAP_MPSMAX(cap) + 12;
1496
1497 if (page_shift < dev_page_min) {
1498 dev_err(&dev->pci_dev->dev,
1499 "Minimum device page size (%u) too large for "
1500 "host (%u)\n", 1 << dev_page_min,
1501 1 << page_shift);
1502 return -ENODEV;
1503 }
1504 if (page_shift > dev_page_max) {
1505 dev_info(&dev->pci_dev->dev,
1506 "Device maximum page size (%u) smaller than "
1507 "host (%u); enabling work-around\n",
1508 1 << dev_page_max, 1 << page_shift);
1509 page_shift = dev_page_max;
1510 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001511
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001512 result = nvme_disable_ctrl(dev, cap);
1513 if (result < 0)
1514 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001515
Keith Busch5a92e702014-02-21 14:13:44 -07001516 nvmeq = raw_nvmeq(dev, 0);
Keith Buschcd638942013-07-15 15:02:23 -06001517 if (!nvmeq) {
1518 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
1519 if (!nvmeq)
1520 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001521 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001522
1523 aqa = nvmeq->q_depth - 1;
1524 aqa |= aqa << 16;
1525
Keith Busch1d090622014-06-23 11:34:01 -06001526 dev->page_size = 1 << page_shift;
1527
Dan McLeran01079522014-06-23 08:24:36 -06001528 dev->ctrl_config = NVME_CC_CSS_NVM;
Keith Busch1d090622014-06-23 11:34:01 -06001529 dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001530 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001531 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001532
1533 writel(aqa, &dev->bar->aqa);
1534 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1535 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001536
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001537 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001538 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001539 return result;
Matthew Wilcox9e866772012-08-03 13:55:56 -04001540
Matthew Wilcox3193f072014-01-27 15:57:22 -05001541 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Keith Busch025c5572013-05-01 13:07:51 -06001542 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001543 return result;
Keith Busch025c5572013-05-01 13:07:51 -06001544
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001545 return result;
1546}
1547
Vishal Verma5d0f6132013-03-04 18:40:58 -07001548struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001549 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001550{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001551 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001552 struct scatterlist *sg;
1553 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001554 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001555
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001556 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001557 return ERR_PTR(-EINVAL);
Dan Carpenter5460fc02013-05-13 17:59:50 +03001558 if (!length || length > INT_MAX - PAGE_SIZE)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001559 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001560
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001561 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001562 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1563 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001564 if (!pages)
1565 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001566
1567 err = get_user_pages_fast(addr, count, 1, pages);
1568 if (err < count) {
1569 count = err;
1570 err = -EFAULT;
1571 goto put_pages;
1572 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001573
Santosh Y6808c5f2014-05-29 10:01:52 +05301574 err = -ENOMEM;
Keith Busch1d090622014-06-23 11:34:01 -06001575 iod = nvme_alloc_iod(count, length, dev, GFP_KERNEL);
Santosh Y6808c5f2014-05-29 10:01:52 +05301576 if (!iod)
1577 goto put_pages;
1578
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001579 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001580 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001581 for (i = 0; i < count; i++) {
1582 sg_set_page(&sg[i], pages[i],
Dan Carpenter5460fc02013-05-13 17:59:50 +03001583 min_t(unsigned, length, PAGE_SIZE - offset),
1584 offset);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001585 length -= (PAGE_SIZE - offset);
1586 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001587 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001588 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001589 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001590
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001591 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1592 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001593 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001594 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001595
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001596 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001597 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001598
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001599 free_iod:
1600 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001601 put_pages:
1602 for (i = 0; i < count; i++)
1603 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001604 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001605 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001606}
1607
Vishal Verma5d0f6132013-03-04 18:40:58 -07001608void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001609 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001610{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001611 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001612
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001613 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1614 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001615
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001616 for (i = 0; i < iod->nents; i++)
1617 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001618}
1619
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001620static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1621{
1622 struct nvme_dev *dev = ns->dev;
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001623 struct nvme_user_io io;
1624 struct nvme_command c;
Keith Buschf410c682013-04-23 17:23:59 -06001625 unsigned length, meta_len;
1626 int status, i;
1627 struct nvme_iod *iod, *meta_iod = NULL;
1628 dma_addr_t meta_dma_addr;
1629 void *meta, *uninitialized_var(meta_mem);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001630
1631 if (copy_from_user(&io, uio, sizeof(io)))
1632 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001633 length = (io.nblocks + 1) << ns->lba_shift;
Keith Buschf410c682013-04-23 17:23:59 -06001634 meta_len = (io.nblocks + 1) * ns->ms;
1635
1636 if (meta_len && ((io.metadata & 3) || !io.metadata))
1637 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001638
1639 switch (io.opcode) {
1640 case nvme_cmd_write:
1641 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001642 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001643 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001644 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001645 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001646 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001647 }
1648
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001649 if (IS_ERR(iod))
1650 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001651
1652 memset(&c, 0, sizeof(c));
1653 c.rw.opcode = io.opcode;
1654 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001655 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001656 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001657 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001658 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001659 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1660 c.rw.reftag = cpu_to_le32(io.reftag);
1661 c.rw.apptag = cpu_to_le16(io.apptag);
1662 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buschf410c682013-04-23 17:23:59 -06001663
1664 if (meta_len) {
Keith Busch1b567492013-07-18 12:13:51 -06001665 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
1666 meta_len);
Keith Buschf410c682013-04-23 17:23:59 -06001667 if (IS_ERR(meta_iod)) {
1668 status = PTR_ERR(meta_iod);
1669 meta_iod = NULL;
1670 goto unmap;
1671 }
1672
1673 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1674 &meta_dma_addr, GFP_KERNEL);
1675 if (!meta_mem) {
1676 status = -ENOMEM;
1677 goto unmap;
1678 }
1679
1680 if (io.opcode & 1) {
1681 int meta_offset = 0;
1682
1683 for (i = 0; i < meta_iod->nents; i++) {
1684 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1685 meta_iod->sg[i].offset;
1686 memcpy(meta_mem + meta_offset, meta,
1687 meta_iod->sg[i].length);
1688 kunmap_atomic(meta);
1689 meta_offset += meta_iod->sg[i].length;
1690 }
1691 }
1692
1693 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1694 }
1695
Keith Buschedd10d32014-04-03 16:45:23 -06001696 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1697 c.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1698 c.rw.prp2 = cpu_to_le64(iod->first_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001699
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001700 if (length != (io.nblocks + 1) << ns->lba_shift)
1701 status = -ENOMEM;
1702 else
Keith Busch4f5099a2014-03-03 16:39:13 -07001703 status = nvme_submit_io_cmd(dev, &c, NULL);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001704
Keith Buschf410c682013-04-23 17:23:59 -06001705 if (meta_len) {
1706 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1707 int meta_offset = 0;
1708
1709 for (i = 0; i < meta_iod->nents; i++) {
1710 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1711 meta_iod->sg[i].offset;
1712 memcpy(meta, meta_mem + meta_offset,
1713 meta_iod->sg[i].length);
1714 kunmap_atomic(meta);
1715 meta_offset += meta_iod->sg[i].length;
1716 }
1717 }
1718
1719 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1720 meta_dma_addr);
1721 }
1722
1723 unmap:
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001724 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001725 nvme_free_iod(dev, iod);
Keith Buschf410c682013-04-23 17:23:59 -06001726
1727 if (meta_iod) {
1728 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1729 nvme_free_iod(dev, meta_iod);
1730 }
1731
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001732 return status;
1733}
1734
Keith Busch50af8ba2012-07-25 16:07:55 -06001735static int nvme_user_admin_cmd(struct nvme_dev *dev,
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001736 struct nvme_admin_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001737{
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001738 struct nvme_admin_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001739 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001740 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001741 struct nvme_iod *uninitialized_var(iod);
Keith Busch94f370c2013-05-09 14:01:38 -06001742 unsigned timeout;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001743
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001744 if (!capable(CAP_SYS_ADMIN))
1745 return -EACCES;
1746 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001747 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001748
1749 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001750 c.common.opcode = cmd.opcode;
1751 c.common.flags = cmd.flags;
1752 c.common.nsid = cpu_to_le32(cmd.nsid);
1753 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1754 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1755 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1756 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1757 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1758 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1759 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1760 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1761
1762 length = cmd.data_len;
1763 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001764 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1765 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001766 if (IS_ERR(iod))
1767 return PTR_ERR(iod);
Keith Buschedd10d32014-04-03 16:45:23 -06001768 length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
1769 c.common.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1770 c.common.prp2 = cpu_to_le64(iod->first_dma);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001771 }
1772
Keith Busch94f370c2013-05-09 14:01:38 -06001773 timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
1774 ADMIN_TIMEOUT;
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001775 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001776 status = -ENOMEM;
1777 else
Keith Busch4f5099a2014-03-03 16:39:13 -07001778 status = nvme_submit_sync_cmd(dev, 0, &c, &cmd.result, timeout);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001779
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001780 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001781 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001782 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001783 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001784
Chayan Biswascf90bc42013-05-22 22:34:49 +00001785 if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
Keith Buschf4f117f2012-09-21 10:49:05 -06001786 sizeof(cmd.result)))
1787 status = -EFAULT;
1788
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001789 return status;
1790}
1791
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001792static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1793 unsigned long arg)
1794{
1795 struct nvme_ns *ns = bdev->bd_disk->private_data;
1796
1797 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001798 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001799 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001800 return ns->ns_id;
1801 case NVME_IOCTL_ADMIN_CMD:
Keith Busch50af8ba2012-07-25 16:07:55 -06001802 return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001803 case NVME_IOCTL_SUBMIT_IO:
1804 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001805 case SG_GET_VERSION_NUM:
1806 return nvme_sg_get_version_num((void __user *)arg);
1807 case SG_IO:
1808 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001809 default:
1810 return -ENOTTY;
1811 }
1812}
1813
Keith Busch320a3822013-10-23 13:07:34 -06001814#ifdef CONFIG_COMPAT
1815static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1816 unsigned int cmd, unsigned long arg)
1817{
Keith Busch320a3822013-10-23 13:07:34 -06001818 switch (cmd) {
1819 case SG_IO:
Keith Busche1797292014-08-27 13:55:38 -06001820 return -ENOIOCTLCMD;
Keith Busch320a3822013-10-23 13:07:34 -06001821 }
1822 return nvme_ioctl(bdev, mode, cmd, arg);
1823}
1824#else
1825#define nvme_compat_ioctl NULL
1826#endif
1827
Keith Busch9ac27092014-01-31 16:53:39 -07001828static int nvme_open(struct block_device *bdev, fmode_t mode)
1829{
1830 struct nvme_ns *ns = bdev->bd_disk->private_data;
1831 struct nvme_dev *dev = ns->dev;
1832
1833 kref_get(&dev->kref);
1834 return 0;
1835}
1836
1837static void nvme_free_dev(struct kref *kref);
1838
1839static void nvme_release(struct gendisk *disk, fmode_t mode)
1840{
1841 struct nvme_ns *ns = disk->private_data;
1842 struct nvme_dev *dev = ns->dev;
1843
1844 kref_put(&dev->kref, nvme_free_dev);
1845}
1846
Keith Busch4cc09e22014-04-02 15:45:37 -06001847static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
1848{
1849 /* some standard values */
1850 geo->heads = 1 << 6;
1851 geo->sectors = 1 << 5;
1852 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1853 return 0;
1854}
1855
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001856static const struct block_device_operations nvme_fops = {
1857 .owner = THIS_MODULE,
1858 .ioctl = nvme_ioctl,
Keith Busch320a3822013-10-23 13:07:34 -06001859 .compat_ioctl = nvme_compat_ioctl,
Keith Busch9ac27092014-01-31 16:53:39 -07001860 .open = nvme_open,
1861 .release = nvme_release,
Keith Busch4cc09e22014-04-02 15:45:37 -06001862 .getgeo = nvme_getgeo,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001863};
1864
Keith Buschedd10d32014-04-03 16:45:23 -06001865static void nvme_resubmit_iods(struct nvme_queue *nvmeq)
1866{
1867 struct nvme_iod *iod, *next;
1868
1869 list_for_each_entry_safe(iod, next, &nvmeq->iod_bio, node) {
1870 if (unlikely(nvme_submit_iod(nvmeq, iod)))
1871 break;
1872 list_del(&iod->node);
1873 if (bio_list_empty(&nvmeq->sq_cong) &&
1874 list_empty(&nvmeq->iod_bio))
1875 remove_wait_queue(&nvmeq->sq_full,
1876 &nvmeq->sq_cong_wait);
1877 }
1878}
1879
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001880static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1881{
1882 while (bio_list_peek(&nvmeq->sq_cong)) {
1883 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1884 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Keith Busch427e9702013-04-09 11:59:32 -06001885
Keith Buschedd10d32014-04-03 16:45:23 -06001886 if (bio_list_empty(&nvmeq->sq_cong) &&
1887 list_empty(&nvmeq->iod_bio))
Matthew Wilcox3cb967c2011-03-16 16:45:49 -04001888 remove_wait_queue(&nvmeq->sq_full,
1889 &nvmeq->sq_cong_wait);
Keith Busch427e9702013-04-09 11:59:32 -06001890 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
Keith Buschedd10d32014-04-03 16:45:23 -06001891 if (!waitqueue_active(&nvmeq->sq_full))
Keith Busch427e9702013-04-09 11:59:32 -06001892 add_wait_queue(&nvmeq->sq_full,
1893 &nvmeq->sq_cong_wait);
1894 bio_list_add_head(&nvmeq->sq_cong, bio);
1895 break;
1896 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001897 }
1898}
1899
Keith Busch6fccf932014-06-18 13:58:57 -06001900static int nvme_submit_async_req(struct nvme_queue *nvmeq)
1901{
1902 struct nvme_command *c;
1903 int cmdid;
1904
1905 cmdid = alloc_cmdid(nvmeq, CMD_CTX_ASYNC, special_completion, 0);
1906 if (cmdid < 0)
1907 return cmdid;
1908
1909 c = &nvmeq->sq_cmds[nvmeq->sq_tail];
1910 memset(c, 0, sizeof(*c));
1911 c->common.opcode = nvme_admin_async_event;
1912 c->common.command_id = cmdid;
1913
1914 if (++nvmeq->sq_tail == nvmeq->q_depth)
1915 nvmeq->sq_tail = 0;
1916 writel(nvmeq->sq_tail, nvmeq->q_db);
1917
1918 return 0;
1919}
1920
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001921static int nvme_kthread(void *data)
1922{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001923 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001924
1925 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001926 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001927 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001928 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001929 int i;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001930 if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
1931 dev->initialized) {
1932 if (work_busy(&dev->reset_work))
1933 continue;
1934 list_del_init(&dev->node);
1935 dev_warn(&dev->pci_dev->dev,
1936 "Failed status, reset controller\n");
Tejun Heo9ca97372014-03-07 10:24:49 -05001937 dev->reset_workfn = nvme_reset_failed_dev;
Keith Buschd4b4ff82013-12-10 13:10:37 -07001938 queue_work(nvme_workq, &dev->reset_work);
1939 continue;
1940 }
Keith Busch5a92e702014-02-21 14:13:44 -07001941 rcu_read_lock();
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001942 for (i = 0; i < dev->queue_count; i++) {
Keith Busch5a92e702014-02-21 14:13:44 -07001943 struct nvme_queue *nvmeq =
1944 rcu_dereference(dev->queues[i]);
Matthew Wilcox740216f2011-02-15 16:28:20 -05001945 if (!nvmeq)
1946 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001947 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001948 if (nvmeq->q_suspended)
1949 goto unlock;
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001950 nvme_process_cq(nvmeq);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001951 nvme_cancel_ios(nvmeq, true);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001952 nvme_resubmit_bios(nvmeq);
Keith Buschedd10d32014-04-03 16:45:23 -06001953 nvme_resubmit_iods(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06001954
1955 while ((i == 0) && (dev->event_limit > 0)) {
1956 if (nvme_submit_async_req(nvmeq))
1957 break;
1958 dev->event_limit--;
1959 }
Keith Busch22404272013-07-15 15:02:20 -06001960 unlock:
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001961 spin_unlock_irq(&nvmeq->q_lock);
1962 }
Keith Busch5a92e702014-02-21 14:13:44 -07001963 rcu_read_unlock();
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001964 }
1965 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001966 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001967 }
1968 return 0;
1969}
1970
Keith Busch0e5e4f02012-11-09 16:33:05 -07001971static void nvme_config_discard(struct nvme_ns *ns)
1972{
1973 u32 logical_block_size = queue_logical_block_size(ns->queue);
1974 ns->queue->limits.discard_zeroes_data = 0;
1975 ns->queue->limits.discard_alignment = logical_block_size;
1976 ns->queue->limits.discard_granularity = logical_block_size;
1977 ns->queue->limits.max_discard_sectors = 0xffffffff;
1978 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1979}
1980
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001981static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001982 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1983{
1984 struct nvme_ns *ns;
1985 struct gendisk *disk;
1986 int lbaf;
1987
1988 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1989 return NULL;
1990
1991 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1992 if (!ns)
1993 return NULL;
1994 ns->queue = blk_alloc_queue(GFP_KERNEL);
1995 if (!ns->queue)
1996 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001997 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
1998 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1999 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -06002000 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002001 blk_queue_make_request(ns->queue, nvme_make_request);
2002 ns->dev = dev;
2003 ns->queue->queuedata = ns;
2004
Matthew Wilcox469071a2013-12-09 12:58:46 -05002005 disk = alloc_disk(0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002006 if (!disk)
2007 goto out_free_queue;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002008 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002009 ns->disk = disk;
2010 lbaf = id->flbas & 0xf;
2011 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Buschf410c682013-04-23 17:23:59 -06002012 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busche9ef4632012-07-24 15:01:04 -06002013 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06002014 if (dev->max_hw_sectors)
2015 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Keith Buscha7d2ce22014-04-29 11:41:28 -06002016 if (dev->vwc & NVME_CTRL_VWC_PRESENT)
2017 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002018
2019 disk->major = nvme_major;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002020 disk->first_minor = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002021 disk->fops = &nvme_fops;
2022 disk->private_data = ns;
2023 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05002024 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox469071a2013-12-09 12:58:46 -05002025 disk->flags = GENHD_FL_EXT_DEVT;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04002026 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002027 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
2028
Keith Busch0e5e4f02012-11-09 16:33:05 -07002029 if (dev->oncs & NVME_CTRL_ONCS_DSM)
2030 nvme_config_discard(ns);
2031
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002032 return ns;
2033
2034 out_free_queue:
2035 blk_cleanup_queue(ns->queue);
2036 out_free_ns:
2037 kfree(ns);
2038 return NULL;
2039}
2040
Keith Busch42f61422014-03-24 10:46:25 -06002041static int nvme_find_closest_node(int node)
2042{
2043 int n, val, min_val = INT_MAX, best_node = node;
2044
2045 for_each_online_node(n) {
2046 if (n == node)
2047 continue;
2048 val = node_distance(node, n);
2049 if (val < min_val) {
2050 min_val = val;
2051 best_node = n;
2052 }
2053 }
2054 return best_node;
2055}
2056
2057static void nvme_set_queue_cpus(cpumask_t *qmask, struct nvme_queue *nvmeq,
2058 int count)
2059{
2060 int cpu;
2061 for_each_cpu(cpu, qmask) {
2062 if (cpumask_weight(nvmeq->cpu_mask) >= count)
2063 break;
2064 if (!cpumask_test_and_set_cpu(cpu, nvmeq->cpu_mask))
2065 *per_cpu_ptr(nvmeq->dev->io_queue, cpu) = nvmeq->qid;
2066 }
2067}
2068
2069static void nvme_add_cpus(cpumask_t *mask, const cpumask_t *unassigned_cpus,
2070 const cpumask_t *new_mask, struct nvme_queue *nvmeq, int cpus_per_queue)
2071{
2072 int next_cpu;
2073 for_each_cpu(next_cpu, new_mask) {
2074 cpumask_or(mask, mask, get_cpu_mask(next_cpu));
2075 cpumask_or(mask, mask, topology_thread_cpumask(next_cpu));
2076 cpumask_and(mask, mask, unassigned_cpus);
2077 nvme_set_queue_cpus(mask, nvmeq, cpus_per_queue);
2078 }
2079}
2080
2081static void nvme_create_io_queues(struct nvme_dev *dev)
2082{
2083 unsigned i, max;
2084
2085 max = min(dev->max_qid, num_online_cpus());
2086 for (i = dev->queue_count; i <= max; i++)
2087 if (!nvme_alloc_queue(dev, i, dev->q_depth, i - 1))
2088 break;
2089
2090 max = min(dev->queue_count - 1, num_online_cpus());
2091 for (i = dev->online_queues; i <= max; i++)
2092 if (nvme_create_queue(raw_nvmeq(dev, i), i))
2093 break;
2094}
2095
2096/*
2097 * If there are fewer queues than online cpus, this will try to optimally
2098 * assign a queue to multiple cpus by grouping cpus that are "close" together:
2099 * thread siblings, core, socket, closest node, then whatever else is
2100 * available.
2101 */
2102static void nvme_assign_io_queues(struct nvme_dev *dev)
2103{
2104 unsigned cpu, cpus_per_queue, queues, remainder, i;
2105 cpumask_var_t unassigned_cpus;
2106
2107 nvme_create_io_queues(dev);
2108
2109 queues = min(dev->online_queues - 1, num_online_cpus());
2110 if (!queues)
2111 return;
2112
2113 cpus_per_queue = num_online_cpus() / queues;
2114 remainder = queues - (num_online_cpus() - queues * cpus_per_queue);
2115
2116 if (!alloc_cpumask_var(&unassigned_cpus, GFP_KERNEL))
2117 return;
2118
2119 cpumask_copy(unassigned_cpus, cpu_online_mask);
2120 cpu = cpumask_first(unassigned_cpus);
2121 for (i = 1; i <= queues; i++) {
2122 struct nvme_queue *nvmeq = lock_nvmeq(dev, i);
2123 cpumask_t mask;
2124
2125 cpumask_clear(nvmeq->cpu_mask);
2126 if (!cpumask_weight(unassigned_cpus)) {
2127 unlock_nvmeq(nvmeq);
2128 break;
2129 }
2130
2131 mask = *get_cpu_mask(cpu);
2132 nvme_set_queue_cpus(&mask, nvmeq, cpus_per_queue);
2133 if (cpus_weight(mask) < cpus_per_queue)
2134 nvme_add_cpus(&mask, unassigned_cpus,
2135 topology_thread_cpumask(cpu),
2136 nvmeq, cpus_per_queue);
2137 if (cpus_weight(mask) < cpus_per_queue)
2138 nvme_add_cpus(&mask, unassigned_cpus,
2139 topology_core_cpumask(cpu),
2140 nvmeq, cpus_per_queue);
2141 if (cpus_weight(mask) < cpus_per_queue)
2142 nvme_add_cpus(&mask, unassigned_cpus,
2143 cpumask_of_node(cpu_to_node(cpu)),
2144 nvmeq, cpus_per_queue);
2145 if (cpus_weight(mask) < cpus_per_queue)
2146 nvme_add_cpus(&mask, unassigned_cpus,
2147 cpumask_of_node(
2148 nvme_find_closest_node(
2149 cpu_to_node(cpu))),
2150 nvmeq, cpus_per_queue);
2151 if (cpus_weight(mask) < cpus_per_queue)
2152 nvme_add_cpus(&mask, unassigned_cpus,
2153 unassigned_cpus,
2154 nvmeq, cpus_per_queue);
2155
2156 WARN(cpumask_weight(nvmeq->cpu_mask) != cpus_per_queue,
2157 "nvme%d qid:%d mis-matched queue-to-cpu assignment\n",
2158 dev->instance, i);
2159
2160 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
2161 nvmeq->cpu_mask);
2162 cpumask_andnot(unassigned_cpus, unassigned_cpus,
2163 nvmeq->cpu_mask);
2164 cpu = cpumask_next(cpu, unassigned_cpus);
2165 if (remainder && !--remainder)
2166 cpus_per_queue++;
2167 unlock_nvmeq(nvmeq);
2168 }
2169 WARN(cpumask_weight(unassigned_cpus), "nvme%d unassigned online cpus\n",
2170 dev->instance);
2171 i = 0;
2172 cpumask_andnot(unassigned_cpus, cpu_possible_mask, cpu_online_mask);
2173 for_each_cpu(cpu, unassigned_cpus)
2174 *per_cpu_ptr(dev->io_queue, cpu) = (i++ % queues) + 1;
2175 free_cpumask_var(unassigned_cpus);
2176}
2177
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002178static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002179{
2180 int status;
2181 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05002182 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002183
Matthew Wilcoxdf348132012-01-11 07:29:56 -07002184 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002185 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04002186 if (status < 0)
2187 return status;
2188 if (status > 0) {
2189 dev_err(&dev->pci_dev->dev, "Could not set queue count (%d)\n",
2190 status);
Keith Buschbadc34d2014-06-23 14:25:35 -06002191 return 0;
Matthew Wilcox27e81662014-04-11 11:58:45 -04002192 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002193 return min(result & 0xffff, result >> 16) + 1;
2194}
2195
Keith Busch9d713c22013-07-15 15:02:24 -06002196static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
2197{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08002198 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06002199}
2200
Keith Buschf3db22f2014-06-11 11:51:35 -06002201static void nvme_cpu_workfn(struct work_struct *work)
2202{
2203 struct nvme_dev *dev = container_of(work, struct nvme_dev, cpu_work);
2204 if (dev->initialized)
2205 nvme_assign_io_queues(dev);
2206}
2207
Keith Busch33b1e952014-03-24 10:46:26 -06002208static int nvme_cpu_notify(struct notifier_block *self,
2209 unsigned long action, void *hcpu)
2210{
Keith Buschf3db22f2014-06-11 11:51:35 -06002211 struct nvme_dev *dev;
2212
Keith Busch33b1e952014-03-24 10:46:26 -06002213 switch (action) {
2214 case CPU_ONLINE:
2215 case CPU_DEAD:
Keith Buschf3db22f2014-06-11 11:51:35 -06002216 spin_lock(&dev_list_lock);
2217 list_for_each_entry(dev, &dev_list, node)
2218 schedule_work(&dev->cpu_work);
2219 spin_unlock(&dev_list_lock);
Keith Busch33b1e952014-03-24 10:46:26 -06002220 break;
2221 }
2222 return NOTIFY_OK;
2223}
2224
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002225static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002226{
Keith Busch5a92e702014-02-21 14:13:44 -07002227 struct nvme_queue *adminq = raw_nvmeq(dev, 0);
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002228 struct pci_dev *pdev = dev->pci_dev;
Keith Busch42f61422014-03-24 10:46:25 -06002229 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002230
Keith Busch42f61422014-03-24 10:46:25 -06002231 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002232 result = set_queue_count(dev, nr_io_queues);
Keith Buschbadc34d2014-06-23 14:25:35 -06002233 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002234 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05002235 if (result < nr_io_queues)
2236 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002237
Keith Busch9d713c22013-07-15 15:02:24 -06002238 size = db_bar_size(dev, nr_io_queues);
2239 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002240 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06002241 do {
2242 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
2243 if (dev->bar)
2244 break;
2245 if (!--nr_io_queues)
2246 return -ENOMEM;
2247 size = db_bar_size(dev, nr_io_queues);
2248 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002249 dev->dbs = ((void __iomem *)dev->bar) + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07002250 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002251 }
2252
Keith Busch9d713c22013-07-15 15:02:24 -06002253 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05002254 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002255
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002256 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002257 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01002258 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
2259 if (vecs < 0) {
2260 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
2261 if (vecs < 0) {
2262 vecs = 1;
2263 } else {
2264 for (i = 0; i < vecs; i++)
2265 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002266 }
2267 }
2268
Matthew Wilcox063a8092013-06-20 10:53:48 -04002269 /*
2270 * Should investigate if there's a performance win from allocating
2271 * more queues than interrupt vectors; it might allow the submission
2272 * path to scale better, even if the receive path is limited by the
2273 * number of interrupts.
2274 */
2275 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06002276 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002277
Matthew Wilcox3193f072014-01-27 15:57:22 -05002278 result = queue_request_irq(dev, adminq, adminq->irqname);
Keith Busch9d713c22013-07-15 15:02:24 -06002279 if (result) {
Matthew Wilcox3193f072014-01-27 15:57:22 -05002280 adminq->q_suspended = 1;
Keith Busch22404272013-07-15 15:02:20 -06002281 goto free_queues;
Keith Busch9d713c22013-07-15 15:02:24 -06002282 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05002283
Keith Buschcd638942013-07-15 15:02:23 -06002284 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06002285 nvme_free_queues(dev, nr_io_queues + 1);
2286 nvme_assign_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06002287
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002288 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002289
Keith Busch22404272013-07-15 15:02:20 -06002290 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002291 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06002292 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002293}
2294
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002295/*
2296 * Return: error value if an error occurred setting up the queues or calling
2297 * Identify Device. 0 if these succeeded, even if adding some of the
2298 * namespaces failed. At the moment, these failures are silent. TBD which
2299 * failures should be reported.
2300 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002301static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002302{
Matthew Wilcox68608c262013-06-21 14:36:34 -04002303 struct pci_dev *pdev = dev->pci_dev;
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04002304 int res;
2305 unsigned nn, i;
Keith Buschcbb62182013-05-01 13:07:49 -06002306 struct nvme_ns *ns;
Matthew Wilcox51814232011-02-01 16:18:08 -05002307 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002308 struct nvme_id_ns *id_ns;
2309 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002310 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06002311 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002312
Matthew Wilcox68608c262013-06-21 14:36:34 -04002313 mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06002314 if (!mem)
2315 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002316
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002317 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002318 if (res) {
Matthew Wilcox27e81662014-04-11 11:58:45 -04002319 dev_err(&pdev->dev, "Identify Controller failed (%d)\n", res);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002320 res = -EIO;
Keith Buschcbb62182013-05-01 13:07:49 -06002321 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002322 }
2323
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002324 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05002325 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07002326 dev->oncs = le16_to_cpup(&ctrl->oncs);
Keith Buschc30341d2013-12-10 13:10:38 -07002327 dev->abort_limit = ctrl->acl + 1;
Keith Buscha7d2ce22014-04-29 11:41:28 -06002328 dev->vwc = ctrl->vwc;
Keith Busch6fccf932014-06-18 13:58:57 -06002329 dev->event_limit = min(ctrl->aerl + 1, 8);
Matthew Wilcox51814232011-02-01 16:18:08 -05002330 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
2331 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
2332 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06002333 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06002334 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Matthew Wilcox68608c262013-06-21 14:36:34 -04002335 if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
2336 (pdev->device == 0x0953) && ctrl->vs[3])
Keith Busch159b67d2013-04-09 17:13:20 -06002337 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002338
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002339 id_ns = mem;
Matthew Wilcox2b2c1892011-10-07 13:10:13 -04002340 for (i = 1; i <= nn; i++) {
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002341 res = nvme_identify(dev, i, 0, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002342 if (res)
2343 continue;
2344
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002345 if (id_ns->ncap == 0)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002346 continue;
2347
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002348 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
Keith Busch08df1e02012-09-21 10:52:13 -06002349 dma_addr + 4096, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002350 if (res)
Keith Busch12209032013-01-31 14:40:38 -07002351 memset(mem + 4096, 0, 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002352
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002353 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002354 if (ns)
2355 list_add_tail(&ns->list, &dev->namespaces);
2356 }
2357 list_for_each_entry(ns, &dev->namespaces, list)
2358 add_disk(ns->disk);
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002359 res = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002360
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04002361 out:
Matthew Wilcox684f5c22011-09-19 17:14:53 -04002362 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002363 return res;
2364}
2365
Keith Busch0877cb02013-07-15 15:02:19 -06002366static int nvme_dev_map(struct nvme_dev *dev)
2367{
Keith Busch42f61422014-03-24 10:46:25 -06002368 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06002369 int bars, result = -ENOMEM;
2370 struct pci_dev *pdev = dev->pci_dev;
2371
2372 if (pci_enable_device_mem(pdev))
2373 return result;
2374
2375 dev->entry[0].vector = pdev->irq;
2376 pci_set_master(pdev);
2377 bars = pci_select_bars(pdev, IORESOURCE_MEM);
2378 if (pci_request_selected_regions(pdev, bars, "nvme"))
2379 goto disable_pci;
2380
Russell King052d0ef2013-06-26 23:49:11 +01002381 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
2382 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2383 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002384
Keith Busch0877cb02013-07-15 15:02:19 -06002385 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2386 if (!dev->bar)
2387 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07002388 if (readl(&dev->bar->csts) == -1) {
2389 result = -ENODEV;
2390 goto unmap;
2391 }
Keith Busch42f61422014-03-24 10:46:25 -06002392 cap = readq(&dev->bar->cap);
2393 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2394 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Keith Busch0877cb02013-07-15 15:02:19 -06002395 dev->dbs = ((void __iomem *)dev->bar) + 4096;
2396
2397 return 0;
2398
Keith Busch0e53d182013-12-10 13:10:39 -07002399 unmap:
2400 iounmap(dev->bar);
2401 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06002402 disable:
2403 pci_release_regions(pdev);
2404 disable_pci:
2405 pci_disable_device(pdev);
2406 return result;
2407}
2408
2409static void nvme_dev_unmap(struct nvme_dev *dev)
2410{
2411 if (dev->pci_dev->msi_enabled)
2412 pci_disable_msi(dev->pci_dev);
2413 else if (dev->pci_dev->msix_enabled)
2414 pci_disable_msix(dev->pci_dev);
2415
2416 if (dev->bar) {
2417 iounmap(dev->bar);
2418 dev->bar = NULL;
Keith Busch9a6b9452013-12-10 13:10:36 -07002419 pci_release_regions(dev->pci_dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002420 }
2421
Keith Busch0877cb02013-07-15 15:02:19 -06002422 if (pci_is_enabled(dev->pci_dev))
2423 pci_disable_device(dev->pci_dev);
2424}
2425
Keith Busch4d115422013-12-10 13:10:40 -07002426struct nvme_delq_ctx {
2427 struct task_struct *waiter;
2428 struct kthread_worker *worker;
2429 atomic_t refcount;
2430};
2431
2432static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2433{
2434 dq->waiter = current;
2435 mb();
2436
2437 for (;;) {
2438 set_current_state(TASK_KILLABLE);
2439 if (!atomic_read(&dq->refcount))
2440 break;
2441 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2442 fatal_signal_pending(current)) {
2443 set_current_state(TASK_RUNNING);
2444
2445 nvme_disable_ctrl(dev, readq(&dev->bar->cap));
2446 nvme_disable_queue(dev, 0);
2447
2448 send_sig(SIGKILL, dq->worker->task, 1);
2449 flush_kthread_worker(dq->worker);
2450 return;
2451 }
2452 }
2453 set_current_state(TASK_RUNNING);
2454}
2455
2456static void nvme_put_dq(struct nvme_delq_ctx *dq)
2457{
2458 atomic_dec(&dq->refcount);
2459 if (dq->waiter)
2460 wake_up_process(dq->waiter);
2461}
2462
2463static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2464{
2465 atomic_inc(&dq->refcount);
2466 return dq;
2467}
2468
2469static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2470{
2471 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
2472
2473 nvme_clear_queue(nvmeq);
2474 nvme_put_dq(dq);
2475}
2476
2477static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2478 kthread_work_func_t fn)
2479{
2480 struct nvme_command c;
2481
2482 memset(&c, 0, sizeof(c));
2483 c.delete_queue.opcode = opcode;
2484 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2485
2486 init_kthread_work(&nvmeq->cmdinfo.work, fn);
2487 return nvme_submit_admin_cmd_async(nvmeq->dev, &c, &nvmeq->cmdinfo);
2488}
2489
2490static void nvme_del_cq_work_handler(struct kthread_work *work)
2491{
2492 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2493 cmdinfo.work);
2494 nvme_del_queue_end(nvmeq);
2495}
2496
2497static int nvme_delete_cq(struct nvme_queue *nvmeq)
2498{
2499 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2500 nvme_del_cq_work_handler);
2501}
2502
2503static void nvme_del_sq_work_handler(struct kthread_work *work)
2504{
2505 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2506 cmdinfo.work);
2507 int status = nvmeq->cmdinfo.status;
2508
2509 if (!status)
2510 status = nvme_delete_cq(nvmeq);
2511 if (status)
2512 nvme_del_queue_end(nvmeq);
2513}
2514
2515static int nvme_delete_sq(struct nvme_queue *nvmeq)
2516{
2517 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2518 nvme_del_sq_work_handler);
2519}
2520
2521static void nvme_del_queue_start(struct kthread_work *work)
2522{
2523 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2524 cmdinfo.work);
2525 allow_signal(SIGKILL);
2526 if (nvme_delete_sq(nvmeq))
2527 nvme_del_queue_end(nvmeq);
2528}
2529
2530static void nvme_disable_io_queues(struct nvme_dev *dev)
2531{
2532 int i;
2533 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2534 struct nvme_delq_ctx dq;
2535 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
2536 &worker, "nvme%d", dev->instance);
2537
2538 if (IS_ERR(kworker_task)) {
2539 dev_err(&dev->pci_dev->dev,
2540 "Failed to create queue del task\n");
2541 for (i = dev->queue_count - 1; i > 0; i--)
2542 nvme_disable_queue(dev, i);
2543 return;
2544 }
2545
2546 dq.waiter = NULL;
2547 atomic_set(&dq.refcount, 0);
2548 dq.worker = &worker;
2549 for (i = dev->queue_count - 1; i > 0; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07002550 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
Keith Busch4d115422013-12-10 13:10:40 -07002551
2552 if (nvme_suspend_queue(nvmeq))
2553 continue;
2554 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2555 nvmeq->cmdinfo.worker = dq.worker;
2556 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2557 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2558 }
2559 nvme_wait_dq(&dq, dev);
2560 kthread_stop(kworker_task);
2561}
2562
Dan McLeranb9afca32014-04-07 17:10:11 -06002563/*
2564* Remove the node from the device list and check
2565* for whether or not we need to stop the nvme_thread.
2566*/
2567static void nvme_dev_list_remove(struct nvme_dev *dev)
2568{
2569 struct task_struct *tmp = NULL;
2570
2571 spin_lock(&dev_list_lock);
2572 list_del_init(&dev->node);
2573 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2574 tmp = nvme_thread;
2575 nvme_thread = NULL;
2576 }
2577 spin_unlock(&dev_list_lock);
2578
2579 if (tmp)
2580 kthread_stop(tmp);
2581}
2582
Keith Buschf0b50732013-07-15 15:02:21 -06002583static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002584{
Keith Busch22404272013-07-15 15:02:20 -06002585 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06002586 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06002587
Keith Buschd4b4ff82013-12-10 13:10:37 -07002588 dev->initialized = 0;
Dan McLeranb9afca32014-04-07 17:10:11 -06002589 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002590
Keith Busch7c1b2452014-06-25 11:18:12 -06002591 if (dev->bar)
2592 csts = readl(&dev->bar->csts);
2593 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07002594 for (i = dev->queue_count - 1; i >= 0; i--) {
Keith Busch5a92e702014-02-21 14:13:44 -07002595 struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
Keith Busch4d115422013-12-10 13:10:40 -07002596 nvme_suspend_queue(nvmeq);
2597 nvme_clear_queue(nvmeq);
2598 }
2599 } else {
2600 nvme_disable_io_queues(dev);
Keith Busch1894d8f2013-07-15 15:02:22 -06002601 nvme_shutdown_ctrl(dev);
Keith Busch4d115422013-12-10 13:10:40 -07002602 nvme_disable_queue(dev, 0);
2603 }
Keith Buschf0b50732013-07-15 15:02:21 -06002604 nvme_dev_unmap(dev);
2605}
2606
2607static void nvme_dev_remove(struct nvme_dev *dev)
2608{
Keith Busch9ac27092014-01-31 16:53:39 -07002609 struct nvme_ns *ns;
Keith Buschf0b50732013-07-15 15:02:21 -06002610
Keith Busch9ac27092014-01-31 16:53:39 -07002611 list_for_each_entry(ns, &dev->namespaces, list) {
2612 if (ns->disk->flags & GENHD_FL_UP)
2613 del_gendisk(ns->disk);
2614 if (!blk_queue_dying(ns->queue))
2615 blk_cleanup_queue(ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002616 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002617}
2618
Matthew Wilcox091b6092011-02-10 09:56:01 -05002619static int nvme_setup_prp_pools(struct nvme_dev *dev)
2620{
2621 struct device *dmadev = &dev->pci_dev->dev;
2622 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2623 PAGE_SIZE, PAGE_SIZE, 0);
2624 if (!dev->prp_page_pool)
2625 return -ENOMEM;
2626
Matthew Wilcox99802a72011-02-10 10:30:34 -05002627 /* Optimisation for I/Os between 4k and 128k */
2628 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2629 256, 256, 0);
2630 if (!dev->prp_small_pool) {
2631 dma_pool_destroy(dev->prp_page_pool);
2632 return -ENOMEM;
2633 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002634 return 0;
2635}
2636
2637static void nvme_release_prp_pools(struct nvme_dev *dev)
2638{
2639 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002640 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002641}
2642
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002643static DEFINE_IDA(nvme_instance_ida);
2644
2645static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002646{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002647 int instance, error;
2648
2649 do {
2650 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2651 return -ENODEV;
2652
2653 spin_lock(&dev_list_lock);
2654 error = ida_get_new(&nvme_instance_ida, &instance);
2655 spin_unlock(&dev_list_lock);
2656 } while (error == -EAGAIN);
2657
2658 if (error)
2659 return -ENODEV;
2660
2661 dev->instance = instance;
2662 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002663}
2664
2665static void nvme_release_instance(struct nvme_dev *dev)
2666{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002667 spin_lock(&dev_list_lock);
2668 ida_remove(&nvme_instance_ida, dev->instance);
2669 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002670}
2671
Keith Busch9ac27092014-01-31 16:53:39 -07002672static void nvme_free_namespaces(struct nvme_dev *dev)
2673{
2674 struct nvme_ns *ns, *next;
2675
2676 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2677 list_del(&ns->list);
2678 put_disk(ns->disk);
2679 kfree(ns);
2680 }
2681}
2682
Keith Busch5e82e952013-02-19 10:17:58 -07002683static void nvme_free_dev(struct kref *kref)
2684{
2685 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
Keith Busch9ac27092014-01-31 16:53:39 -07002686
Keith Buscha96d4f52014-08-19 19:15:59 -06002687 pci_dev_put(dev->pci_dev);
Keith Busch9ac27092014-01-31 16:53:39 -07002688 nvme_free_namespaces(dev);
Keith Busch42f61422014-03-24 10:46:25 -06002689 free_percpu(dev->io_queue);
Keith Busch5e82e952013-02-19 10:17:58 -07002690 kfree(dev->queues);
2691 kfree(dev->entry);
2692 kfree(dev);
2693}
2694
2695static int nvme_dev_open(struct inode *inode, struct file *f)
2696{
2697 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
2698 miscdev);
2699 kref_get(&dev->kref);
2700 f->private_data = dev;
2701 return 0;
2702}
2703
2704static int nvme_dev_release(struct inode *inode, struct file *f)
2705{
2706 struct nvme_dev *dev = f->private_data;
2707 kref_put(&dev->kref, nvme_free_dev);
2708 return 0;
2709}
2710
2711static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2712{
2713 struct nvme_dev *dev = f->private_data;
2714 switch (cmd) {
2715 case NVME_IOCTL_ADMIN_CMD:
2716 return nvme_user_admin_cmd(dev, (void __user *)arg);
2717 default:
2718 return -ENOTTY;
2719 }
2720}
2721
2722static const struct file_operations nvme_dev_fops = {
2723 .owner = THIS_MODULE,
2724 .open = nvme_dev_open,
2725 .release = nvme_dev_release,
2726 .unlocked_ioctl = nvme_dev_ioctl,
2727 .compat_ioctl = nvme_dev_ioctl,
2728};
2729
Keith Buschf0b50732013-07-15 15:02:21 -06002730static int nvme_dev_start(struct nvme_dev *dev)
2731{
2732 int result;
Dan McLeranb9afca32014-04-07 17:10:11 -06002733 bool start_thread = false;
Keith Buschf0b50732013-07-15 15:02:21 -06002734
2735 result = nvme_dev_map(dev);
2736 if (result)
2737 return result;
2738
2739 result = nvme_configure_admin_queue(dev);
2740 if (result)
2741 goto unmap;
2742
2743 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002744 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2745 start_thread = true;
2746 nvme_thread = NULL;
2747 }
Keith Buschf0b50732013-07-15 15:02:21 -06002748 list_add(&dev->node, &dev_list);
2749 spin_unlock(&dev_list_lock);
2750
Dan McLeranb9afca32014-04-07 17:10:11 -06002751 if (start_thread) {
2752 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2753 wake_up(&nvme_kthread_wait);
2754 } else
2755 wait_event_killable(nvme_kthread_wait, nvme_thread);
2756
2757 if (IS_ERR_OR_NULL(nvme_thread)) {
2758 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2759 goto disable;
2760 }
Keith Busch7be50e92014-09-10 15:48:47 -06002761 nvme_init_queue(raw_nvmeq(dev, 0), 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06002762
Keith Buschf0b50732013-07-15 15:02:21 -06002763 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002764 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002765 goto disable;
2766
Keith Buschd82e8bf2013-09-05 14:45:07 -06002767 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002768
2769 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002770 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06002771 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002772 unmap:
2773 nvme_dev_unmap(dev);
2774 return result;
2775}
2776
Keith Busch9a6b9452013-12-10 13:10:36 -07002777static int nvme_remove_dead_ctrl(void *arg)
2778{
2779 struct nvme_dev *dev = (struct nvme_dev *)arg;
2780 struct pci_dev *pdev = dev->pci_dev;
2781
2782 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06002783 pci_stop_and_remove_bus_device_locked(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002784 kref_put(&dev->kref, nvme_free_dev);
2785 return 0;
2786}
2787
2788static void nvme_remove_disks(struct work_struct *ws)
2789{
Keith Busch9a6b9452013-12-10 13:10:36 -07002790 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2791
Keith Busch5a92e702014-02-21 14:13:44 -07002792 nvme_free_queues(dev, 1);
Keith Busch302c6722014-07-18 11:40:20 -06002793 nvme_dev_remove(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002794}
2795
2796static int nvme_dev_resume(struct nvme_dev *dev)
2797{
2798 int ret;
2799
2800 ret = nvme_dev_start(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002801 if (ret)
Keith Busch9a6b9452013-12-10 13:10:36 -07002802 return ret;
Keith Buschbadc34d2014-06-23 14:25:35 -06002803 if (dev->online_queues < 2) {
Keith Busch9a6b9452013-12-10 13:10:36 -07002804 spin_lock(&dev_list_lock);
Tejun Heo9ca97372014-03-07 10:24:49 -05002805 dev->reset_workfn = nvme_remove_disks;
Keith Busch9a6b9452013-12-10 13:10:36 -07002806 queue_work(nvme_workq, &dev->reset_work);
2807 spin_unlock(&dev_list_lock);
2808 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07002809 dev->initialized = 1;
Keith Busch9a6b9452013-12-10 13:10:36 -07002810 return 0;
2811}
2812
2813static void nvme_dev_reset(struct nvme_dev *dev)
2814{
2815 nvme_dev_shutdown(dev);
2816 if (nvme_dev_resume(dev)) {
2817 dev_err(&dev->pci_dev->dev, "Device failed to resume\n");
2818 kref_get(&dev->kref);
2819 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
2820 dev->instance))) {
2821 dev_err(&dev->pci_dev->dev,
2822 "Failed to start controller remove task\n");
2823 kref_put(&dev->kref, nvme_free_dev);
2824 }
2825 }
2826}
2827
2828static void nvme_reset_failed_dev(struct work_struct *ws)
2829{
2830 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
2831 nvme_dev_reset(dev);
2832}
2833
Tejun Heo9ca97372014-03-07 10:24:49 -05002834static void nvme_reset_workfn(struct work_struct *work)
2835{
2836 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
2837 dev->reset_workfn(work);
2838}
2839
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002840static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002841{
Keith Busch0877cb02013-07-15 15:02:19 -06002842 int result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002843 struct nvme_dev *dev;
2844
2845 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2846 if (!dev)
2847 return -ENOMEM;
2848 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
2849 GFP_KERNEL);
2850 if (!dev->entry)
2851 goto free;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002852 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
2853 GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002854 if (!dev->queues)
2855 goto free;
Keith Busch42f61422014-03-24 10:46:25 -06002856 dev->io_queue = alloc_percpu(unsigned short);
2857 if (!dev->io_queue)
2858 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002859
2860 INIT_LIST_HEAD(&dev->namespaces);
Tejun Heo9ca97372014-03-07 10:24:49 -05002861 dev->reset_workfn = nvme_reset_failed_dev;
2862 INIT_WORK(&dev->reset_work, nvme_reset_workfn);
Keith Buschf3db22f2014-06-11 11:51:35 -06002863 INIT_WORK(&dev->cpu_work, nvme_cpu_workfn);
Keith Buscha96d4f52014-08-19 19:15:59 -06002864 dev->pci_dev = pci_dev_get(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002865 pci_set_drvdata(pdev, dev);
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002866 result = nvme_set_instance(dev);
2867 if (result)
Keith Buscha96d4f52014-08-19 19:15:59 -06002868 goto put_pci;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002869
Matthew Wilcox091b6092011-02-10 09:56:01 -05002870 result = nvme_setup_prp_pools(dev);
2871 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002872 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002873
Keith Buschfb35e912014-03-03 11:09:47 -07002874 kref_init(&dev->kref);
Keith Buschf0b50732013-07-15 15:02:21 -06002875 result = nvme_dev_start(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002876 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002877 goto release_pools;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002878
Keith Buschbadc34d2014-06-23 14:25:35 -06002879 if (dev->online_queues > 1)
2880 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002881 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002882 goto shutdown;
Matthew Wilcox740216f2011-02-15 16:28:20 -05002883
Keith Busch5e82e952013-02-19 10:17:58 -07002884 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
2885 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2886 dev->miscdev.parent = &pdev->dev;
2887 dev->miscdev.name = dev->name;
2888 dev->miscdev.fops = &nvme_dev_fops;
2889 result = misc_register(&dev->miscdev);
2890 if (result)
2891 goto remove;
2892
Keith Buschd4b4ff82013-12-10 13:10:37 -07002893 dev->initialized = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002894 return 0;
2895
Keith Busch5e82e952013-02-19 10:17:58 -07002896 remove:
2897 nvme_dev_remove(dev);
Keith Busch9ac27092014-01-31 16:53:39 -07002898 nvme_free_namespaces(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002899 shutdown:
2900 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002901 release_pools:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002902 nvme_free_queues(dev, 0);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002903 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002904 release:
2905 nvme_release_instance(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002906 put_pci:
2907 pci_dev_put(dev->pci_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002908 free:
Keith Busch42f61422014-03-24 10:46:25 -06002909 free_percpu(dev->io_queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002910 kfree(dev->queues);
2911 kfree(dev->entry);
2912 kfree(dev);
2913 return result;
2914}
2915
Keith Buschf0d54a52014-05-02 10:40:43 -06002916static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2917{
Keith Buscha6739472014-06-23 16:03:21 -06002918 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002919
Keith Buscha6739472014-06-23 16:03:21 -06002920 if (prepare)
2921 nvme_dev_shutdown(dev);
2922 else
2923 nvme_dev_resume(dev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002924}
2925
Keith Busch09ece142014-01-27 11:29:40 -05002926static void nvme_shutdown(struct pci_dev *pdev)
2927{
2928 struct nvme_dev *dev = pci_get_drvdata(pdev);
2929 nvme_dev_shutdown(dev);
2930}
2931
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002932static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002933{
2934 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002935
2936 spin_lock(&dev_list_lock);
2937 list_del_init(&dev->node);
2938 spin_unlock(&dev_list_lock);
2939
2940 pci_set_drvdata(pdev, NULL);
2941 flush_work(&dev->reset_work);
Keith Buschf3db22f2014-06-11 11:51:35 -06002942 flush_work(&dev->cpu_work);
Keith Busch5e82e952013-02-19 10:17:58 -07002943 misc_deregister(&dev->miscdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002944 nvme_dev_shutdown(dev);
Keith Buscha1a5ef92013-12-16 13:50:00 -05002945 nvme_free_queues(dev, 0);
Keith Busch302c6722014-07-18 11:40:20 -06002946 nvme_dev_remove(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002947 nvme_release_instance(dev);
2948 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002949 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002950}
2951
2952/* These functions are yet to be implemented */
2953#define nvme_error_detected NULL
2954#define nvme_dump_registers NULL
2955#define nvme_link_reset NULL
2956#define nvme_slot_reset NULL
2957#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002958
Jingoo Han671a6012014-02-13 11:19:14 +09002959#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002960static int nvme_suspend(struct device *dev)
2961{
2962 struct pci_dev *pdev = to_pci_dev(dev);
2963 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2964
2965 nvme_dev_shutdown(ndev);
2966 return 0;
2967}
2968
2969static int nvme_resume(struct device *dev)
2970{
2971 struct pci_dev *pdev = to_pci_dev(dev);
2972 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002973
Keith Busch9a6b9452013-12-10 13:10:36 -07002974 if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
Tejun Heo9ca97372014-03-07 10:24:49 -05002975 ndev->reset_workfn = nvme_reset_failed_dev;
Keith Busch9a6b9452013-12-10 13:10:36 -07002976 queue_work(nvme_workq, &ndev->reset_work);
2977 }
2978 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002979}
Jingoo Han671a6012014-02-13 11:19:14 +09002980#endif
Keith Buschcd638942013-07-15 15:02:23 -06002981
2982static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002983
Stephen Hemminger1d352032012-09-07 09:33:17 -07002984static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002985 .error_detected = nvme_error_detected,
2986 .mmio_enabled = nvme_dump_registers,
2987 .link_reset = nvme_link_reset,
2988 .slot_reset = nvme_slot_reset,
2989 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06002990 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002991};
2992
2993/* Move to pci_ids.h later */
2994#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2995
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002996static const struct pci_device_id nvme_id_table[] = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002997 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2998 { 0, }
2999};
3000MODULE_DEVICE_TABLE(pci, nvme_id_table);
3001
3002static struct pci_driver nvme_driver = {
3003 .name = "nvme",
3004 .id_table = nvme_id_table,
3005 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08003006 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05003007 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06003008 .driver = {
3009 .pm = &nvme_dev_pm_ops,
3010 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003011 .err_handler = &nvme_err_handler,
3012};
3013
3014static int __init nvme_init(void)
3015{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003016 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003017
Dan McLeranb9afca32014-04-07 17:10:11 -06003018 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003019
Keith Busch9a6b9452013-12-10 13:10:36 -07003020 nvme_workq = create_singlethread_workqueue("nvme");
3021 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06003022 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07003023
Keith Busch5c42ea12012-07-25 16:05:18 -06003024 result = register_blkdev(nvme_major, "nvme");
3025 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07003026 goto kill_workq;
Keith Busch5c42ea12012-07-25 16:05:18 -06003027 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04003028 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003029
Keith Buschf3db22f2014-06-11 11:51:35 -06003030 nvme_nb.notifier_call = &nvme_cpu_notify;
3031 result = register_hotcpu_notifier(&nvme_nb);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003032 if (result)
3033 goto unregister_blkdev;
Keith Buschf3db22f2014-06-11 11:51:35 -06003034
3035 result = pci_register_driver(&nvme_driver);
3036 if (result)
3037 goto unregister_hotcpu;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003038 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003039
Keith Buschf3db22f2014-06-11 11:51:35 -06003040 unregister_hotcpu:
3041 unregister_hotcpu_notifier(&nvme_nb);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05003042 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003043 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003044 kill_workq:
3045 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003046 return result;
3047}
3048
3049static void __exit nvme_exit(void)
3050{
3051 pci_unregister_driver(&nvme_driver);
Keith Buschf3db22f2014-06-11 11:51:35 -06003052 unregister_hotcpu_notifier(&nvme_nb);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003053 unregister_blkdev(nvme_major, "nvme");
Keith Busch9a6b9452013-12-10 13:10:36 -07003054 destroy_workqueue(nvme_workq);
Dan McLeranb9afca32014-04-07 17:10:11 -06003055 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04003056 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003057}
3058
3059MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
3060MODULE_LICENSE("GPL");
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003061MODULE_VERSION("0.9");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003062module_init(nvme_init);
3063module_exit(nvme_exit);