blob: 5539d292087284474aa727278277b86b566b0082 [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
3 * Copyright (c) 2011, Intel Corporation.
4 *
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.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#include <linux/nvme.h>
20#include <linux/bio.h>
Matthew Wilcox8de05532011-05-12 13:50:28 -040021#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050022#include <linux/blkdev.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040023#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050024#include <linux/errno.h>
25#include <linux/fs.h>
26#include <linux/genhd.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040027#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050028#include <linux/init.h>
29#include <linux/interrupt.h>
30#include <linux/io.h>
31#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050032#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050033#include <linux/kernel.h>
34#include <linux/mm.h>
35#include <linux/module.h>
36#include <linux/moduleparam.h>
37#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050038#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040039#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050040#include <linux/sched.h>
41#include <linux/slab.h>
42#include <linux/types.h>
Vishal Verma5d0f6132013-03-04 18:40:58 -070043#include <scsi/sg.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090044#include <asm-generic/io-64-nonatomic-lo-hi.h>
45
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050046#define NVME_Q_DEPTH 1024
47#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
48#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
49#define NVME_MINORS 64
Matthew Wilcoxe85248e2011-02-06 18:30:16 -050050#define ADMIN_TIMEOUT (60 * HZ)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050051
52static int nvme_major;
53module_param(nvme_major, int, 0);
54
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050055static int use_threaded_interrupts;
56module_param(use_threaded_interrupts, int, 0);
57
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050058static DEFINE_SPINLOCK(dev_list_lock);
59static LIST_HEAD(dev_list);
60static struct task_struct *nvme_thread;
61
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050062/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050063 * An NVM Express queue. Each device has at least two (one for admin
64 * commands and one for I/O commands).
65 */
66struct nvme_queue {
67 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -050068 struct nvme_dev *dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050069 spinlock_t q_lock;
70 struct nvme_command *sq_cmds;
71 volatile struct nvme_completion *cqes;
72 dma_addr_t sq_dma_addr;
73 dma_addr_t cq_dma_addr;
74 wait_queue_head_t sq_full;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050075 wait_queue_t sq_cong_wait;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050076 struct bio_list sq_cong;
77 u32 __iomem *q_db;
78 u16 q_depth;
79 u16 cq_vector;
80 u16 sq_head;
81 u16 sq_tail;
82 u16 cq_head;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -040083 u8 cq_phase;
84 u8 cqe_seen;
Keith Busch22404272013-07-15 15:02:20 -060085 u8 q_suspended;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050086 unsigned long cmdid_data[];
87};
88
89/*
90 * Check we didin't inadvertently grow the command struct
91 */
92static inline void _nvme_check_size(void)
93{
94 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
95 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
96 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
97 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
98 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -040099 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500100 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
101 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
102 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
103 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600104 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500105}
106
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500107typedef void (*nvme_completion_fn)(struct nvme_dev *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400108 struct nvme_completion *);
109
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500110struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400111 nvme_completion_fn fn;
112 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500113 unsigned long timeout;
114};
115
116static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
117{
118 return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
119}
120
Keith Busch22404272013-07-15 15:02:20 -0600121static unsigned nvme_queue_extra(int depth)
122{
123 return DIV_ROUND_UP(depth, 8) + (depth * sizeof(struct nvme_cmd_info));
124}
125
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500126/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400127 * alloc_cmdid() - Allocate a Command ID
128 * @nvmeq: The queue that will be used for this command
129 * @ctx: A pointer that will be passed to the handler
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400130 * @handler: The function to call on completion
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500131 *
132 * Allocate a Command ID for a queue. The data passed in will
133 * be passed to the completion handler. This is implemented by using
134 * the bottom two bits of the ctx pointer to store the handler ID.
135 * Passing in a pointer that's not 4-byte aligned will cause a BUG.
136 * We can change this if it becomes a problem.
Matthew Wilcox184d2942011-05-11 21:36:38 -0400137 *
138 * May be called with local interrupts disabled and the q_lock held,
139 * or with interrupts enabled and no locks held.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500140 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400141static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx,
142 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500143{
Matthew Wilcoxe6d15f72011-02-24 08:49:41 -0500144 int depth = nvmeq->q_depth - 1;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500145 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500146 int cmdid;
147
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500148 do {
149 cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
150 if (cmdid >= depth)
151 return -EBUSY;
152 } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
153
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400154 info[cmdid].fn = handler;
155 info[cmdid].ctx = ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500156 info[cmdid].timeout = jiffies + timeout;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500157 return cmdid;
158}
159
160static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400161 nvme_completion_fn handler, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500162{
163 int cmdid;
164 wait_event_killable(nvmeq->sq_full,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500165 (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500166 return (cmdid < 0) ? -EINTR : cmdid;
167}
168
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400169/* Special values must be less than 0x1000 */
170#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500171#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
172#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
173#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500174#define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500175
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500176static void special_completion(struct nvme_dev *dev, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400177 struct nvme_completion *cqe)
178{
179 if (ctx == CMD_CTX_CANCELLED)
180 return;
181 if (ctx == CMD_CTX_FLUSH)
182 return;
183 if (ctx == CMD_CTX_COMPLETED) {
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500184 dev_warn(&dev->pci_dev->dev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400185 "completed id %d twice on queue %d\n",
186 cqe->command_id, le16_to_cpup(&cqe->sq_id));
187 return;
188 }
189 if (ctx == CMD_CTX_INVALID) {
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500190 dev_warn(&dev->pci_dev->dev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400191 "invalid id %d completed on queue %d\n",
192 cqe->command_id, le16_to_cpup(&cqe->sq_id));
193 return;
194 }
195
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500196 dev_warn(&dev->pci_dev->dev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400197}
198
Matthew Wilcox184d2942011-05-11 21:36:38 -0400199/*
200 * Called with local interrupts disabled and the q_lock held. May not sleep.
201 */
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400202static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid,
203 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500204{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400205 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500206 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500207
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400208 if (cmdid >= nvmeq->q_depth) {
209 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500210 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400211 }
Keith Busch859361a2012-08-02 14:05:59 -0600212 if (fn)
213 *fn = info[cmdid].fn;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400214 ctx = info[cmdid].ctx;
215 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500216 info[cmdid].ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500217 clear_bit(cmdid, nvmeq->cmdid_data);
218 wake_up(&nvmeq->sq_full);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400219 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500220}
221
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400222static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
223 nvme_completion_fn *fn)
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500224{
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400225 void *ctx;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500226 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400227 if (fn)
228 *fn = info[cmdid].fn;
229 ctx = info[cmdid].ctx;
230 info[cmdid].fn = special_completion;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500231 info[cmdid].ctx = CMD_CTX_CANCELLED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400232 return ctx;
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500233}
234
Vishal Verma5d0f6132013-03-04 18:40:58 -0700235struct nvme_queue *get_nvmeq(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500236{
Matthew Wilcox040a93b2011-12-20 11:04:12 -0500237 return dev->queues[get_cpu() + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500238}
239
Vishal Verma5d0f6132013-03-04 18:40:58 -0700240void put_nvmeq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500241{
Matthew Wilcox1b234842011-01-20 13:01:49 -0500242 put_cpu();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500243}
244
245/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400246 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500247 * @nvmeq: The queue to use
248 * @cmd: The command to send
249 *
250 * Safe to use from interrupt context
251 */
252static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
253{
254 unsigned long flags;
255 u16 tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500256 spin_lock_irqsave(&nvmeq->q_lock, flags);
257 tail = nvmeq->sq_tail;
258 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500259 if (++tail == nvmeq->q_depth)
260 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500261 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500262 nvmeq->sq_tail = tail;
263 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
264
265 return 0;
266}
267
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500268static __le64 **iod_list(struct nvme_iod *iod)
269{
270 return ((void *)iod) + iod->offset;
271}
272
273/*
274 * Will slightly overestimate the number of pages needed. This is OK
275 * as it only leads to a small amount of wasted memory for the lifetime of
276 * the I/O.
277 */
278static int nvme_npages(unsigned size)
279{
280 unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE);
281 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
282}
283
284static struct nvme_iod *
285nvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp)
286{
287 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
288 sizeof(__le64 *) * nvme_npages(nbytes) +
289 sizeof(struct scatterlist) * nseg, gfp);
290
291 if (iod) {
292 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
293 iod->npages = -1;
294 iod->length = nbytes;
Keith Busch2b196032012-11-06 11:59:23 -0700295 iod->nents = 0;
Keith Busch61982212013-05-29 15:59:39 -0600296 iod->start_time = jiffies;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500297 }
298
299 return iod;
300}
301
Vishal Verma5d0f6132013-03-04 18:40:58 -0700302void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500303{
304 const int last_prp = PAGE_SIZE / 8 - 1;
305 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500306 __le64 **list = iod_list(iod);
307 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500308
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500309 if (iod->npages == 0)
310 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
311 for (i = 0; i < iod->npages; i++) {
312 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500313 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500314 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500315 prp_dma = next_prp_dma;
316 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500317 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500318}
319
Keith Busch61982212013-05-29 15:59:39 -0600320static void nvme_start_io_acct(struct bio *bio)
321{
322 struct gendisk *disk = bio->bi_bdev->bd_disk;
323 const int rw = bio_data_dir(bio);
324 int cpu = part_stat_lock();
325 part_round_stats(cpu, &disk->part0);
326 part_stat_inc(cpu, &disk->part0, ios[rw]);
327 part_stat_add(cpu, &disk->part0, sectors[rw], bio_sectors(bio));
328 part_inc_in_flight(&disk->part0, rw);
329 part_stat_unlock();
330}
331
332static void nvme_end_io_acct(struct bio *bio, unsigned long start_time)
333{
334 struct gendisk *disk = bio->bi_bdev->bd_disk;
335 const int rw = bio_data_dir(bio);
336 unsigned long duration = jiffies - start_time;
337 int cpu = part_stat_lock();
338 part_stat_add(cpu, &disk->part0, ticks[rw], duration);
339 part_round_stats(cpu, &disk->part0);
340 part_dec_in_flight(&disk->part0, rw);
341 part_stat_unlock();
342}
343
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500344static void bio_completion(struct nvme_dev *dev, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500345 struct nvme_completion *cqe)
346{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500347 struct nvme_iod *iod = ctx;
348 struct bio *bio = iod->private;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500349 u16 status = le16_to_cpup(&cqe->status) >> 1;
350
Keith Busch9e59d092013-08-08 10:25:38 -0600351 if (iod->nents) {
Keith Busch2b196032012-11-06 11:59:23 -0700352 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500353 bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Keith Busch9e59d092013-08-08 10:25:38 -0600354 nvme_end_io_acct(bio, iod->start_time);
355 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500356 nvme_free_iod(dev, iod);
Keith Busch427e9702013-04-09 11:59:32 -0600357 if (status)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500358 bio_endio(bio, -EIO);
Keith Busch427e9702013-04-09 11:59:32 -0600359 else
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500360 bio_endio(bio, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500361}
362
Matthew Wilcox184d2942011-05-11 21:36:38 -0400363/* length is in bytes. gfp flags indicates whether we may sleep. */
Vishal Verma5d0f6132013-03-04 18:40:58 -0700364int nvme_setup_prps(struct nvme_dev *dev, struct nvme_common_command *cmd,
365 struct nvme_iod *iod, int total_len, gfp_t gfp)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500366{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500367 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500368 int length = total_len;
369 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500370 int dma_len = sg_dma_len(sg);
371 u64 dma_addr = sg_dma_address(sg);
372 int offset = offset_in_page(dma_addr);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500373 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500374 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500375 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500376 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500377
378 cmd->prp1 = cpu_to_le64(dma_addr);
379 length -= (PAGE_SIZE - offset);
380 if (length <= 0)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500381 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500382
383 dma_len -= (PAGE_SIZE - offset);
384 if (dma_len) {
385 dma_addr += (PAGE_SIZE - offset);
386 } else {
387 sg = sg_next(sg);
388 dma_addr = sg_dma_address(sg);
389 dma_len = sg_dma_len(sg);
390 }
391
392 if (length <= PAGE_SIZE) {
393 cmd->prp2 = cpu_to_le64(dma_addr);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500394 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500395 }
396
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500397 nprps = DIV_ROUND_UP(length, PAGE_SIZE);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500398 if (nprps <= (256 / 8)) {
399 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500400 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500401 } else {
402 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500403 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500404 }
405
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400406 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
407 if (!prp_list) {
408 cmd->prp2 = cpu_to_le64(dma_addr);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500409 iod->npages = -1;
410 return (total_len - length) + PAGE_SIZE;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400411 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500412 list[0] = prp_list;
413 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500414 cmd->prp2 = cpu_to_le64(prp_dma);
415 i = 0;
416 for (;;) {
Matthew Wilcox7523d832011-03-16 16:43:40 -0400417 if (i == PAGE_SIZE / 8) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500418 __le64 *old_prp_list = prp_list;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400419 prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500420 if (!prp_list)
421 return total_len - length;
422 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400423 prp_list[0] = old_prp_list[i - 1];
424 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
425 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500426 }
427 prp_list[i++] = cpu_to_le64(dma_addr);
428 dma_len -= PAGE_SIZE;
429 dma_addr += PAGE_SIZE;
430 length -= PAGE_SIZE;
431 if (length <= 0)
432 break;
433 if (dma_len > 0)
434 continue;
435 BUG_ON(dma_len < 0);
436 sg = sg_next(sg);
437 dma_addr = sg_dma_address(sg);
438 dma_len = sg_dma_len(sg);
439 }
440
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500441 return total_len;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500442}
443
Keith Busch427e9702013-04-09 11:59:32 -0600444struct nvme_bio_pair {
445 struct bio b1, b2, *parent;
446 struct bio_vec *bv1, *bv2;
447 int err;
448 atomic_t cnt;
449};
450
451static void nvme_bio_pair_endio(struct bio *bio, int err)
452{
453 struct nvme_bio_pair *bp = bio->bi_private;
454
455 if (err)
456 bp->err = err;
457
458 if (atomic_dec_and_test(&bp->cnt)) {
459 bio_endio(bp->parent, bp->err);
Keith Busch1b567492013-07-18 12:13:51 -0600460 kfree(bp->bv1);
461 kfree(bp->bv2);
Keith Busch427e9702013-04-09 11:59:32 -0600462 kfree(bp);
463 }
464}
465
466static struct nvme_bio_pair *nvme_bio_split(struct bio *bio, int idx,
467 int len, int offset)
468{
469 struct nvme_bio_pair *bp;
470
Kent Overstreet4f024f32013-10-11 15:44:27 -0700471 BUG_ON(len > bio->bi_iter.bi_size);
Keith Busch427e9702013-04-09 11:59:32 -0600472 BUG_ON(idx > bio->bi_vcnt);
473
474 bp = kmalloc(sizeof(*bp), GFP_ATOMIC);
475 if (!bp)
476 return NULL;
477 bp->err = 0;
478
479 bp->b1 = *bio;
480 bp->b2 = *bio;
481
Kent Overstreet4f024f32013-10-11 15:44:27 -0700482 bp->b1.bi_iter.bi_size = len;
483 bp->b2.bi_iter.bi_size -= len;
Keith Busch427e9702013-04-09 11:59:32 -0600484 bp->b1.bi_vcnt = idx;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700485 bp->b2.bi_iter.bi_idx = idx;
486 bp->b2.bi_iter.bi_sector += len >> 9;
Keith Busch427e9702013-04-09 11:59:32 -0600487
488 if (offset) {
489 bp->bv1 = kmalloc(bio->bi_max_vecs * sizeof(struct bio_vec),
490 GFP_ATOMIC);
491 if (!bp->bv1)
492 goto split_fail_1;
493
494 bp->bv2 = kmalloc(bio->bi_max_vecs * sizeof(struct bio_vec),
495 GFP_ATOMIC);
496 if (!bp->bv2)
497 goto split_fail_2;
498
499 memcpy(bp->bv1, bio->bi_io_vec,
500 bio->bi_max_vecs * sizeof(struct bio_vec));
501 memcpy(bp->bv2, bio->bi_io_vec,
502 bio->bi_max_vecs * sizeof(struct bio_vec));
503
504 bp->b1.bi_io_vec = bp->bv1;
505 bp->b2.bi_io_vec = bp->bv2;
506 bp->b2.bi_io_vec[idx].bv_offset += offset;
507 bp->b2.bi_io_vec[idx].bv_len -= offset;
508 bp->b1.bi_io_vec[idx].bv_len = offset;
509 bp->b1.bi_vcnt++;
510 } else
511 bp->bv1 = bp->bv2 = NULL;
512
513 bp->b1.bi_private = bp;
514 bp->b2.bi_private = bp;
515
516 bp->b1.bi_end_io = nvme_bio_pair_endio;
517 bp->b2.bi_end_io = nvme_bio_pair_endio;
518
519 bp->parent = bio;
520 atomic_set(&bp->cnt, 2);
521
522 return bp;
523
524 split_fail_2:
525 kfree(bp->bv1);
526 split_fail_1:
527 kfree(bp);
528 return NULL;
529}
530
531static int nvme_split_and_submit(struct bio *bio, struct nvme_queue *nvmeq,
532 int idx, int len, int offset)
533{
534 struct nvme_bio_pair *bp = nvme_bio_split(bio, idx, len, offset);
535 if (!bp)
536 return -ENOMEM;
537
538 if (bio_list_empty(&nvmeq->sq_cong))
539 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
540 bio_list_add(&nvmeq->sq_cong, &bp->b1);
541 bio_list_add(&nvmeq->sq_cong, &bp->b2);
542
543 return 0;
544}
545
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500546/* NVMe scatterlists require no holes in the virtual address */
547#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
548 (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
549
Keith Busch427e9702013-04-09 11:59:32 -0600550static int nvme_map_bio(struct nvme_queue *nvmeq, struct nvme_iod *iod,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500551 struct bio *bio, enum dma_data_direction dma_dir, int psegs)
552{
Kent Overstreet79886132013-11-23 17:19:00 -0800553 struct bio_vec bvec, bvprv;
554 struct bvec_iter iter;
Matthew Wilcox76830842011-02-10 13:55:39 -0500555 struct scatterlist *sg = NULL;
Kent Overstreet79886132013-11-23 17:19:00 -0800556 int length = 0, nsegs = 0, split_len = bio->bi_iter.bi_size;
557 int first = 1;
Keith Busch159b67d2013-04-09 17:13:20 -0600558
559 if (nvmeq->dev->stripe_size)
560 split_len = nvmeq->dev->stripe_size -
Kent Overstreet4f024f32013-10-11 15:44:27 -0700561 ((bio->bi_iter.bi_sector << 9) &
562 (nvmeq->dev->stripe_size - 1));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500563
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500564 sg_init_table(iod->sg, psegs);
Kent Overstreet79886132013-11-23 17:19:00 -0800565 bio_for_each_segment(bvec, bio, iter) {
566 if (!first && BIOVEC_PHYS_MERGEABLE(&bvprv, &bvec)) {
567 sg->length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500568 } else {
Kent Overstreet79886132013-11-23 17:19:00 -0800569 if (!first && BIOVEC_NOT_VIRT_MERGEABLE(&bvprv, &bvec))
570 return nvme_split_and_submit(bio, nvmeq,
571 iter.bi_idx,
572 length, 0);
Keith Busch427e9702013-04-09 11:59:32 -0600573
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500574 sg = sg ? sg + 1 : iod->sg;
Kent Overstreet79886132013-11-23 17:19:00 -0800575 sg_set_page(sg, bvec.bv_page,
576 bvec.bv_len, bvec.bv_offset);
Matthew Wilcox76830842011-02-10 13:55:39 -0500577 nsegs++;
578 }
Keith Busch159b67d2013-04-09 17:13:20 -0600579
Kent Overstreet79886132013-11-23 17:19:00 -0800580 if (split_len - length < bvec.bv_len)
581 return nvme_split_and_submit(bio, nvmeq, iter.bi_idx,
582 split_len,
583 split_len - length);
584 length += bvec.bv_len;
Matthew Wilcox76830842011-02-10 13:55:39 -0500585 bvprv = bvec;
Kent Overstreet79886132013-11-23 17:19:00 -0800586 first = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500587 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500588 iod->nents = nsegs;
Matthew Wilcox76830842011-02-10 13:55:39 -0500589 sg_mark_end(sg);
Keith Busch427e9702013-04-09 11:59:32 -0600590 if (dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir) == 0)
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500591 return -ENOMEM;
Keith Busch427e9702013-04-09 11:59:32 -0600592
Kent Overstreet4f024f32013-10-11 15:44:27 -0700593 BUG_ON(length != bio->bi_iter.bi_size);
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500594 return length;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500595}
596
Keith Busch0e5e4f02012-11-09 16:33:05 -0700597/*
598 * We reuse the small pool to allocate the 16-byte range here as it is not
599 * worth having a special pool for these or additional cases to handle freeing
600 * the iod.
601 */
602static int nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
603 struct bio *bio, struct nvme_iod *iod, int cmdid)
604{
605 struct nvme_dsm_range *range;
606 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
607
608 range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC,
609 &iod->first_dma);
610 if (!range)
611 return -ENOMEM;
612
613 iod_list(iod)[0] = (__le64 *)range;
614 iod->npages = 0;
615
616 range->cattr = cpu_to_le32(0);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700617 range->nlb = cpu_to_le32(bio->bi_iter.bi_size >> ns->lba_shift);
618 range->slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700619
620 memset(cmnd, 0, sizeof(*cmnd));
621 cmnd->dsm.opcode = nvme_cmd_dsm;
622 cmnd->dsm.command_id = cmdid;
623 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
624 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
625 cmnd->dsm.nr = 0;
626 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
627
628 if (++nvmeq->sq_tail == nvmeq->q_depth)
629 nvmeq->sq_tail = 0;
630 writel(nvmeq->sq_tail, nvmeq->q_db);
631
632 return 0;
633}
634
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500635static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
636 int cmdid)
637{
638 struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
639
640 memset(cmnd, 0, sizeof(*cmnd));
641 cmnd->common.opcode = nvme_cmd_flush;
642 cmnd->common.command_id = cmdid;
643 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
644
645 if (++nvmeq->sq_tail == nvmeq->q_depth)
646 nvmeq->sq_tail = 0;
647 writel(nvmeq->sq_tail, nvmeq->q_db);
648
649 return 0;
650}
651
Vishal Verma5d0f6132013-03-04 18:40:58 -0700652int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns)
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500653{
654 int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH,
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500655 special_completion, NVME_IO_TIMEOUT);
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500656 if (unlikely(cmdid < 0))
657 return cmdid;
658
659 return nvme_submit_flush(nvmeq, ns, cmdid);
660}
661
Matthew Wilcox184d2942011-05-11 21:36:38 -0400662/*
663 * Called with local interrupts disabled and the q_lock held. May not sleep.
664 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500665static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
666 struct bio *bio)
667{
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500668 struct nvme_command *cmnd;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500669 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500670 enum dma_data_direction dma_dir;
Wei Yongjun1287dab2013-05-13 22:29:04 +0800671 int cmdid, length, result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500672 u16 control;
673 u32 dsmgmt;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500674 int psegs = bio_phys_segments(ns->queue, bio);
675
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500676 if ((bio->bi_rw & REQ_FLUSH) && psegs) {
677 result = nvme_submit_flush_data(nvmeq, ns);
678 if (result)
679 return result;
680 }
681
Wei Yongjun1287dab2013-05-13 22:29:04 +0800682 result = -ENOMEM;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700683 iod = nvme_alloc_iod(psegs, bio->bi_iter.bi_size, GFP_ATOMIC);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500684 if (!iod)
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500685 goto nomem;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500686 iod->private = bio;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500687
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500688 result = -EBUSY;
Matthew Wilcoxff976d72011-12-20 13:53:01 -0500689 cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500690 if (unlikely(cmdid < 0))
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500691 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500692
Keith Busch0e5e4f02012-11-09 16:33:05 -0700693 if (bio->bi_rw & REQ_DISCARD) {
694 result = nvme_submit_discard(nvmeq, ns, bio, iod, cmdid);
695 if (result)
696 goto free_cmdid;
697 return result;
698 }
Matthew Wilcox00df5cb2011-02-22 14:18:30 -0500699 if ((bio->bi_rw & REQ_FLUSH) && !psegs)
700 return nvme_submit_flush(nvmeq, ns, cmdid);
701
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500702 control = 0;
703 if (bio->bi_rw & REQ_FUA)
704 control |= NVME_RW_FUA;
705 if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
706 control |= NVME_RW_LR;
707
708 dsmgmt = 0;
709 if (bio->bi_rw & REQ_RAHEAD)
710 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
711
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500712 cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500713
Matthew Wilcoxb8deb622011-01-26 10:08:25 -0500714 memset(cmnd, 0, sizeof(*cmnd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500715 if (bio_data_dir(bio)) {
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500716 cmnd->rw.opcode = nvme_cmd_write;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500717 dma_dir = DMA_TO_DEVICE;
718 } else {
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500719 cmnd->rw.opcode = nvme_cmd_read;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500720 dma_dir = DMA_FROM_DEVICE;
721 }
722
Keith Busch427e9702013-04-09 11:59:32 -0600723 result = nvme_map_bio(nvmeq, iod, bio, dma_dir, psegs);
724 if (result <= 0)
Keith Busch859361a2012-08-02 14:05:59 -0600725 goto free_cmdid;
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500726 length = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500727
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500728 cmnd->rw.command_id = cmdid;
729 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500730 length = nvme_setup_prps(nvmeq->dev, &cmnd->common, iod, length,
731 GFP_ATOMIC);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700732 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
Matthew Wilcox1ad2f892011-02-23 15:20:00 -0500733 cmnd->rw.length = cpu_to_le16((length >> ns->lba_shift) - 1);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500734 cmnd->rw.control = cpu_to_le16(control);
735 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500736
Keith Busch61982212013-05-29 15:59:39 -0600737 nvme_start_io_acct(bio);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500738 if (++nvmeq->sq_tail == nvmeq->q_depth)
739 nvmeq->sq_tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500740 writel(nvmeq->sq_tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500741
Matthew Wilcox1974b1a2011-02-10 12:01:09 -0500742 return 0;
743
Keith Busch859361a2012-08-02 14:05:59 -0600744 free_cmdid:
745 free_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500746 free_iod:
747 nvme_free_iod(nvmeq->dev, iod);
Matthew Wilcoxeeee3222011-02-14 15:55:33 -0500748 nomem:
749 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500750}
751
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400752static int nvme_process_cq(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500753{
Matthew Wilcox82123462011-01-20 13:24:06 -0500754 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500755
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500756 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500757 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500758
759 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400760 void *ctx;
761 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500762 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500763 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500764 break;
765 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
766 if (++head == nvmeq->q_depth) {
767 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500768 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500769 }
770
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400771 ctx = free_cmdid(nvmeq, cqe.command_id, &fn);
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500772 fn(nvmeq->dev, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500773 }
774
775 /* If the controller ignores the cq head doorbell and continuously
776 * writes to the queue, it is theoretically possible to wrap around
777 * the queue twice and mistakenly return IRQ_NONE. Linux only
778 * requires that 0.1% of your interrupts are handled, so this isn't
779 * a big problem.
780 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500781 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400782 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500783
Matthew Wilcoxf1938f62011-10-20 17:00:41 -0400784 writel(head, nvmeq->q_db + (1 << nvmeq->dev->db_stride));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500785 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500786 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500787
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400788 nvmeq->cqe_seen = 1;
789 return 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500790}
791
Matthew Wilcox7d822452013-06-24 12:03:57 -0400792static void nvme_make_request(struct request_queue *q, struct bio *bio)
793{
794 struct nvme_ns *ns = q->queuedata;
795 struct nvme_queue *nvmeq = get_nvmeq(ns->dev);
796 int result = -EBUSY;
797
Keith Buschcd638942013-07-15 15:02:23 -0600798 if (!nvmeq) {
799 put_nvmeq(NULL);
800 bio_endio(bio, -EIO);
801 return;
802 }
803
Matthew Wilcox7d822452013-06-24 12:03:57 -0400804 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -0600805 if (!nvmeq->q_suspended && bio_list_empty(&nvmeq->sq_cong))
Matthew Wilcox7d822452013-06-24 12:03:57 -0400806 result = nvme_submit_bio_queue(nvmeq, ns, bio);
807 if (unlikely(result)) {
808 if (bio_list_empty(&nvmeq->sq_cong))
809 add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
810 bio_list_add(&nvmeq->sq_cong, bio);
811 }
812
813 nvme_process_cq(nvmeq);
814 spin_unlock_irq(&nvmeq->q_lock);
815 put_nvmeq(nvmeq);
816}
817
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500818static irqreturn_t nvme_irq(int irq, void *data)
819{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500820 irqreturn_t result;
821 struct nvme_queue *nvmeq = data;
822 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400823 nvme_process_cq(nvmeq);
824 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
825 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500826 spin_unlock(&nvmeq->q_lock);
827 return result;
828}
829
830static irqreturn_t nvme_irq_check(int irq, void *data)
831{
832 struct nvme_queue *nvmeq = data;
833 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
834 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
835 return IRQ_NONE;
836 return IRQ_WAKE_THREAD;
837}
838
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500839static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
840{
841 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400842 cancel_cmdid(nvmeq, cmdid, NULL);
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500843 spin_unlock_irq(&nvmeq->q_lock);
844}
845
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400846struct sync_cmd_info {
847 struct task_struct *task;
848 u32 result;
849 int status;
850};
851
Matthew Wilcox5c1281a2011-12-20 11:54:53 -0500852static void sync_completion(struct nvme_dev *dev, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400853 struct nvme_completion *cqe)
854{
855 struct sync_cmd_info *cmdinfo = ctx;
856 cmdinfo->result = le32_to_cpup(&cqe->result);
857 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
858 wake_up_process(cmdinfo->task);
859}
860
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500861/*
862 * Returns 0 on success. If the result is negative, it's a Linux error code;
863 * if the result is positive, it's an NVM Express status code
864 */
Vishal Verma5d0f6132013-03-04 18:40:58 -0700865int nvme_submit_sync_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd,
866 u32 *result, unsigned timeout)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500867{
868 int cmdid;
869 struct sync_cmd_info cmdinfo;
870
871 cmdinfo.task = current;
872 cmdinfo.status = -EINTR;
873
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400874 cmdid = alloc_cmdid_killable(nvmeq, &cmdinfo, sync_completion,
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500875 timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500876 if (cmdid < 0)
877 return cmdid;
878 cmd->common.command_id = cmdid;
879
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500880 set_current_state(TASK_KILLABLE);
881 nvme_submit_cmd(nvmeq, cmd);
Keith Busch78f8d252013-04-19 14:11:06 -0600882 schedule_timeout(timeout);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500883
Matthew Wilcox3c0cf132011-02-04 16:03:56 -0500884 if (cmdinfo.status == -EINTR) {
885 nvme_abort_command(nvmeq, cmdid);
886 return -EINTR;
887 }
888
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500889 if (result)
890 *result = cmdinfo.result;
891
892 return cmdinfo.status;
893}
894
Vishal Verma5d0f6132013-03-04 18:40:58 -0700895int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500896 u32 *result)
897{
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500898 return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500899}
900
901static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
902{
903 int status;
904 struct nvme_command c;
905
906 memset(&c, 0, sizeof(c));
907 c.delete_queue.opcode = opcode;
908 c.delete_queue.qid = cpu_to_le16(id);
909
910 status = nvme_submit_admin_cmd(dev, &c, NULL);
911 if (status)
912 return -EIO;
913 return 0;
914}
915
916static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
917 struct nvme_queue *nvmeq)
918{
919 int status;
920 struct nvme_command c;
921 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
922
923 memset(&c, 0, sizeof(c));
924 c.create_cq.opcode = nvme_admin_create_cq;
925 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
926 c.create_cq.cqid = cpu_to_le16(qid);
927 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
928 c.create_cq.cq_flags = cpu_to_le16(flags);
929 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
930
931 status = nvme_submit_admin_cmd(dev, &c, NULL);
932 if (status)
933 return -EIO;
934 return 0;
935}
936
937static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
938 struct nvme_queue *nvmeq)
939{
940 int status;
941 struct nvme_command c;
942 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
943
944 memset(&c, 0, sizeof(c));
945 c.create_sq.opcode = nvme_admin_create_sq;
946 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
947 c.create_sq.sqid = cpu_to_le16(qid);
948 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
949 c.create_sq.sq_flags = cpu_to_le16(flags);
950 c.create_sq.cqid = cpu_to_le16(qid);
951
952 status = nvme_submit_admin_cmd(dev, &c, NULL);
953 if (status)
954 return -EIO;
955 return 0;
956}
957
958static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
959{
960 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
961}
962
963static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
964{
965 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
966}
967
Vishal Verma5d0f6132013-03-04 18:40:58 -0700968int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400969 dma_addr_t dma_addr)
970{
971 struct nvme_command c;
972
973 memset(&c, 0, sizeof(c));
974 c.identify.opcode = nvme_admin_identify;
975 c.identify.nsid = cpu_to_le32(nsid);
976 c.identify.prp1 = cpu_to_le64(dma_addr);
977 c.identify.cns = cpu_to_le32(cns);
978
979 return nvme_submit_admin_cmd(dev, &c, NULL);
980}
981
Vishal Verma5d0f6132013-03-04 18:40:58 -0700982int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
Keith Busch08df1e02012-09-21 10:52:13 -0600983 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400984{
985 struct nvme_command c;
986
987 memset(&c, 0, sizeof(c));
988 c.features.opcode = nvme_admin_get_features;
Keith Buscha42cecc2012-07-25 16:06:38 -0600989 c.features.nsid = cpu_to_le32(nsid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400990 c.features.prp1 = cpu_to_le64(dma_addr);
991 c.features.fid = cpu_to_le32(fid);
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400992
Keith Busch08df1e02012-09-21 10:52:13 -0600993 return nvme_submit_admin_cmd(dev, &c, result);
Matthew Wilcoxdf348132012-01-11 07:29:56 -0700994}
995
Vishal Verma5d0f6132013-03-04 18:40:58 -0700996int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
997 dma_addr_t dma_addr, u32 *result)
Matthew Wilcoxdf348132012-01-11 07:29:56 -0700998{
999 struct nvme_command c;
1000
1001 memset(&c, 0, sizeof(c));
1002 c.features.opcode = nvme_admin_set_features;
1003 c.features.prp1 = cpu_to_le64(dma_addr);
1004 c.features.fid = cpu_to_le32(fid);
1005 c.features.dword11 = cpu_to_le32(dword11);
1006
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001007 return nvme_submit_admin_cmd(dev, &c, result);
1008}
1009
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001010/**
1011 * nvme_cancel_ios - Cancel outstanding I/Os
1012 * @queue: The queue to cancel I/Os on
1013 * @timeout: True to only cancel I/Os which have timed out
1014 */
1015static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout)
1016{
1017 int depth = nvmeq->q_depth - 1;
1018 struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
1019 unsigned long now = jiffies;
1020 int cmdid;
1021
1022 for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
1023 void *ctx;
1024 nvme_completion_fn fn;
1025 static struct nvme_completion cqe = {
Matthew Wilcoxaf2d9ca2013-04-16 15:18:30 -04001026 .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1),
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001027 };
1028
1029 if (timeout && !time_after(now, info[cmdid].timeout))
1030 continue;
Keith Busch053ab7022013-04-30 11:19:38 -06001031 if (info[cmdid].ctx == CMD_CTX_CANCELLED)
1032 continue;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001033 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d\n", cmdid);
1034 ctx = cancel_cmdid(nvmeq, cmdid, &fn);
1035 fn(nvmeq->dev, ctx, &cqe);
1036 }
1037}
1038
Keith Busch22404272013-07-15 15:02:20 -06001039static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001040{
Keith Busch22404272013-07-15 15:02:20 -06001041 spin_lock_irq(&nvmeq->q_lock);
1042 while (bio_list_peek(&nvmeq->sq_cong)) {
1043 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1044 bio_endio(bio, -EIO);
1045 }
1046 spin_unlock_irq(&nvmeq->q_lock);
1047
Matthew Wilcox9e866772012-08-03 13:55:56 -04001048 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1049 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1050 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1051 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1052 kfree(nvmeq);
1053}
1054
Keith Busch22404272013-07-15 15:02:20 -06001055static void nvme_free_queues(struct nvme_dev *dev)
1056{
1057 int i;
1058
1059 for (i = dev->queue_count - 1; i >= 0; i--) {
1060 nvme_free_queue(dev->queues[i]);
1061 dev->queue_count--;
1062 dev->queues[i] = NULL;
1063 }
1064}
1065
1066static void nvme_disable_queue(struct nvme_dev *dev, int qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001067{
1068 struct nvme_queue *nvmeq = dev->queues[qid];
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001069 int vector = dev->entry[nvmeq->cq_vector].vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001070
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001071 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001072 if (nvmeq->q_suspended) {
1073 spin_unlock_irq(&nvmeq->q_lock);
1074 return;
Keith Busch32958742012-08-20 14:57:49 -06001075 }
Keith Busch22404272013-07-15 15:02:20 -06001076 nvmeq->q_suspended = 1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001077 spin_unlock_irq(&nvmeq->q_lock);
1078
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001079 irq_set_affinity_hint(vector, NULL);
1080 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001081
1082 /* Don't tell the adapter to delete the admin queue */
1083 if (qid) {
1084 adapter_delete_sq(dev, qid);
1085 adapter_delete_cq(dev, qid);
1086 }
1087
Keith Busch22404272013-07-15 15:02:20 -06001088 spin_lock_irq(&nvmeq->q_lock);
1089 nvme_process_cq(nvmeq);
1090 nvme_cancel_ios(nvmeq, false);
1091 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001092}
1093
1094static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
1095 int depth, int vector)
1096{
1097 struct device *dmadev = &dev->pci_dev->dev;
Keith Busch22404272013-07-15 15:02:20 -06001098 unsigned extra = nvme_queue_extra(depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001099 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
1100 if (!nvmeq)
1101 return NULL;
1102
1103 nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
1104 &nvmeq->cq_dma_addr, GFP_KERNEL);
1105 if (!nvmeq->cqes)
1106 goto free_nvmeq;
1107 memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
1108
1109 nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
1110 &nvmeq->sq_dma_addr, GFP_KERNEL);
1111 if (!nvmeq->sq_cmds)
1112 goto free_cqdma;
1113
1114 nvmeq->q_dmadev = dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001115 nvmeq->dev = dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001116 spin_lock_init(&nvmeq->q_lock);
1117 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001118 nvmeq->cq_phase = 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001119 init_waitqueue_head(&nvmeq->sq_full);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001120 init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001121 bio_list_init(&nvmeq->sq_cong);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001122 nvmeq->q_db = &dev->dbs[qid << (dev->db_stride + 1)];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001123 nvmeq->q_depth = depth;
1124 nvmeq->cq_vector = vector;
Keith Busch22404272013-07-15 15:02:20 -06001125 nvmeq->q_suspended = 1;
1126 dev->queue_count++;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001127
1128 return nvmeq;
1129
1130 free_cqdma:
Keith Busch68b8eca2013-05-01 13:07:47 -06001131 dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001132 nvmeq->cq_dma_addr);
1133 free_nvmeq:
1134 kfree(nvmeq);
1135 return NULL;
1136}
1137
Matthew Wilcox30010822011-01-20 09:10:15 -05001138static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1139 const char *name)
1140{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001141 if (use_threaded_interrupts)
1142 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Matthew Wilcoxec6ce612011-02-06 09:01:00 -05001143 nvme_irq_check, nvme_irq,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001144 IRQF_DISABLED | IRQF_SHARED,
1145 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001146 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
1147 IRQF_DISABLED | IRQF_SHARED, name, nvmeq);
1148}
1149
Keith Busch22404272013-07-15 15:02:20 -06001150static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001151{
Keith Busch22404272013-07-15 15:02:20 -06001152 struct nvme_dev *dev = nvmeq->dev;
1153 unsigned extra = nvme_queue_extra(nvmeq->q_depth);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001154
Keith Busch22404272013-07-15 15:02:20 -06001155 nvmeq->sq_tail = 0;
1156 nvmeq->cq_head = 0;
1157 nvmeq->cq_phase = 1;
1158 nvmeq->q_db = &dev->dbs[qid << (dev->db_stride + 1)];
1159 memset(nvmeq->cmdid_data, 0, extra);
1160 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
1161 nvme_cancel_ios(nvmeq, false);
1162 nvmeq->q_suspended = 0;
1163}
1164
1165static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1166{
1167 struct nvme_dev *dev = nvmeq->dev;
1168 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001169
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001170 result = adapter_alloc_cq(dev, qid, nvmeq);
1171 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001172 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001173
1174 result = adapter_alloc_sq(dev, qid, nvmeq);
1175 if (result < 0)
1176 goto release_cq;
1177
Matthew Wilcox30010822011-01-20 09:10:15 -05001178 result = queue_request_irq(dev, nvmeq, "nvme");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001179 if (result < 0)
1180 goto release_sq;
1181
Keith Busch22404272013-07-15 15:02:20 -06001182 spin_lock(&nvmeq->q_lock);
1183 nvme_init_queue(nvmeq, qid);
1184 spin_unlock(&nvmeq->q_lock);
1185
1186 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001187
1188 release_sq:
1189 adapter_delete_sq(dev, qid);
1190 release_cq:
1191 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001192 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001193}
1194
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001195static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1196{
1197 unsigned long timeout;
1198 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1199
1200 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1201
1202 while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
1203 msleep(100);
1204 if (fatal_signal_pending(current))
1205 return -EINTR;
1206 if (time_after(jiffies, timeout)) {
1207 dev_err(&dev->pci_dev->dev,
1208 "Device not ready; aborting initialisation\n");
1209 return -ENODEV;
1210 }
1211 }
1212
1213 return 0;
1214}
1215
1216/*
1217 * If the device has been passed off to us in an enabled state, just clear
1218 * the enabled bit. The spec says we should set the 'shutdown notification
1219 * bits', but doing so may cause the device to complete commands to the
1220 * admin queue ... and we don't know what memory that might be pointing at!
1221 */
1222static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1223{
Matthew Wilcox44af1462013-05-04 06:43:17 -04001224 u32 cc = readl(&dev->bar->cc);
1225
1226 if (cc & NVME_CC_ENABLE)
1227 writel(cc & ~NVME_CC_ENABLE, &dev->bar->cc);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001228 return nvme_wait_ready(dev, cap, false);
1229}
1230
1231static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1232{
1233 return nvme_wait_ready(dev, cap, true);
1234}
1235
Keith Busch1894d8f2013-07-15 15:02:22 -06001236static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1237{
1238 unsigned long timeout;
1239 u32 cc;
1240
1241 cc = (readl(&dev->bar->cc) & ~NVME_CC_SHN_MASK) | NVME_CC_SHN_NORMAL;
1242 writel(cc, &dev->bar->cc);
1243
1244 timeout = 2 * HZ + jiffies;
1245 while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
1246 NVME_CSTS_SHST_CMPLT) {
1247 msleep(100);
1248 if (fatal_signal_pending(current))
1249 return -EINTR;
1250 if (time_after(jiffies, timeout)) {
1251 dev_err(&dev->pci_dev->dev,
1252 "Device shutdown incomplete; abort shutdown\n");
1253 return -ENODEV;
1254 }
1255 }
1256
1257 return 0;
1258}
1259
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001260static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001261{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001262 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001263 u32 aqa;
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001264 u64 cap = readq(&dev->bar->cap);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001265 struct nvme_queue *nvmeq;
1266
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001267 result = nvme_disable_ctrl(dev, cap);
1268 if (result < 0)
1269 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001270
Keith Buschcd638942013-07-15 15:02:23 -06001271 nvmeq = dev->queues[0];
1272 if (!nvmeq) {
1273 nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
1274 if (!nvmeq)
1275 return -ENOMEM;
1276 dev->queues[0] = nvmeq;
1277 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001278
1279 aqa = nvmeq->q_depth - 1;
1280 aqa |= aqa << 16;
1281
1282 dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
1283 dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
1284 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
Matthew Wilcox7f53f9d2011-03-22 15:55:45 -04001285 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001286
1287 writel(aqa, &dev->bar->aqa);
1288 writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
1289 writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
1290 writel(dev->ctrl_config, &dev->bar->cc);
1291
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001292 result = nvme_enable_ctrl(dev, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001293 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001294 return result;
Matthew Wilcox9e866772012-08-03 13:55:56 -04001295
Matthew Wilcox30010822011-01-20 09:10:15 -05001296 result = queue_request_irq(dev, nvmeq, "nvme admin");
Keith Busch025c5572013-05-01 13:07:51 -06001297 if (result)
Keith Buschcd638942013-07-15 15:02:23 -06001298 return result;
Keith Busch025c5572013-05-01 13:07:51 -06001299
Keith Busch22404272013-07-15 15:02:20 -06001300 spin_lock(&nvmeq->q_lock);
1301 nvme_init_queue(nvmeq, 0);
1302 spin_unlock(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001303 return result;
1304}
1305
Vishal Verma5d0f6132013-03-04 18:40:58 -07001306struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001307 unsigned long addr, unsigned length)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001308{
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001309 int i, err, count, nents, offset;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001310 struct scatterlist *sg;
1311 struct page **pages;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001312 struct nvme_iod *iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001313
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001314 if (addr & 3)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001315 return ERR_PTR(-EINVAL);
Dan Carpenter5460fc02013-05-13 17:59:50 +03001316 if (!length || length > INT_MAX - PAGE_SIZE)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001317 return ERR_PTR(-EINVAL);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001318
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001319 offset = offset_in_page(addr);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001320 count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1321 pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
Dan Carpenter22fff822012-01-20 07:55:30 -05001322 if (!pages)
1323 return ERR_PTR(-ENOMEM);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001324
1325 err = get_user_pages_fast(addr, count, 1, pages);
1326 if (err < count) {
1327 count = err;
1328 err = -EFAULT;
1329 goto put_pages;
1330 }
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001331
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001332 iod = nvme_alloc_iod(count, length, GFP_KERNEL);
1333 sg = iod->sg;
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001334 sg_init_table(sg, count);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001335 for (i = 0; i < count; i++) {
1336 sg_set_page(&sg[i], pages[i],
Dan Carpenter5460fc02013-05-13 17:59:50 +03001337 min_t(unsigned, length, PAGE_SIZE - offset),
1338 offset);
Matthew Wilcoxd0ba1e42011-09-13 17:01:39 -04001339 length -= (PAGE_SIZE - offset);
1340 offset = 0;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001341 }
Matthew Wilcoxfe304c42012-01-06 13:49:25 -07001342 sg_mark_end(&sg[i - 1]);
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001343 iod->nents = count;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001344
1345 err = -ENOMEM;
1346 nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
1347 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001348 if (!nents)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001349 goto free_iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001350
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001351 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001352 return iod;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001353
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001354 free_iod:
1355 kfree(iod);
Matthew Wilcox36c14ed2011-01-24 07:52:07 -05001356 put_pages:
1357 for (i = 0; i < count; i++)
1358 put_page(pages[i]);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001359 kfree(pages);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001360 return ERR_PTR(err);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001361}
1362
Vishal Verma5d0f6132013-03-04 18:40:58 -07001363void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001364 struct nvme_iod *iod)
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001365{
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001366 int i;
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001367
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001368 dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
1369 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001370
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001371 for (i = 0; i < iod->nents; i++)
1372 put_page(sg_page(&iod->sg[i]));
Matthew Wilcox7fc3cda2011-01-26 17:05:50 -05001373}
1374
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001375static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1376{
1377 struct nvme_dev *dev = ns->dev;
1378 struct nvme_queue *nvmeq;
1379 struct nvme_user_io io;
1380 struct nvme_command c;
Keith Buschf410c682013-04-23 17:23:59 -06001381 unsigned length, meta_len;
1382 int status, i;
1383 struct nvme_iod *iod, *meta_iod = NULL;
1384 dma_addr_t meta_dma_addr;
1385 void *meta, *uninitialized_var(meta_mem);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001386
1387 if (copy_from_user(&io, uio, sizeof(io)))
1388 return -EFAULT;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001389 length = (io.nblocks + 1) << ns->lba_shift;
Keith Buschf410c682013-04-23 17:23:59 -06001390 meta_len = (io.nblocks + 1) * ns->ms;
1391
1392 if (meta_len && ((io.metadata & 3) || !io.metadata))
1393 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001394
1395 switch (io.opcode) {
1396 case nvme_cmd_write:
1397 case nvme_cmd_read:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001398 case nvme_cmd_compare:
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001399 iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
Matthew Wilcox64132142011-08-09 12:56:37 -04001400 break;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001401 default:
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001402 return -EINVAL;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001403 }
1404
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001405 if (IS_ERR(iod))
1406 return PTR_ERR(iod);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001407
1408 memset(&c, 0, sizeof(c));
1409 c.rw.opcode = io.opcode;
1410 c.rw.flags = io.flags;
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001411 c.rw.nsid = cpu_to_le32(ns->ns_id);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001412 c.rw.slba = cpu_to_le64(io.slba);
Matthew Wilcox6c7d4942011-03-21 09:48:57 -04001413 c.rw.length = cpu_to_le16(io.nblocks);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001414 c.rw.control = cpu_to_le16(io.control);
Matthew Wilcox1c9b5262013-04-16 15:21:06 -04001415 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1416 c.rw.reftag = cpu_to_le32(io.reftag);
1417 c.rw.apptag = cpu_to_le16(io.apptag);
1418 c.rw.appmask = cpu_to_le16(io.appmask);
Keith Buschf410c682013-04-23 17:23:59 -06001419
1420 if (meta_len) {
Keith Busch1b567492013-07-18 12:13:51 -06001421 meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
1422 meta_len);
Keith Buschf410c682013-04-23 17:23:59 -06001423 if (IS_ERR(meta_iod)) {
1424 status = PTR_ERR(meta_iod);
1425 meta_iod = NULL;
1426 goto unmap;
1427 }
1428
1429 meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
1430 &meta_dma_addr, GFP_KERNEL);
1431 if (!meta_mem) {
1432 status = -ENOMEM;
1433 goto unmap;
1434 }
1435
1436 if (io.opcode & 1) {
1437 int meta_offset = 0;
1438
1439 for (i = 0; i < meta_iod->nents; i++) {
1440 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1441 meta_iod->sg[i].offset;
1442 memcpy(meta_mem + meta_offset, meta,
1443 meta_iod->sg[i].length);
1444 kunmap_atomic(meta);
1445 meta_offset += meta_iod->sg[i].length;
1446 }
1447 }
1448
1449 c.rw.metadata = cpu_to_le64(meta_dma_addr);
1450 }
1451
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001452 length = nvme_setup_prps(dev, &c.common, iod, length, GFP_KERNEL);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -05001453
Matthew Wilcox040a93b2011-12-20 11:04:12 -05001454 nvmeq = get_nvmeq(dev);
Matthew Wilcoxfa922822011-03-16 16:29:00 -04001455 /*
1456 * Since nvme_submit_sync_cmd sleeps, we can't keep preemption
Matthew Wilcoxb1ad37e2011-02-04 16:14:30 -05001457 * disabled. We may be preempted at any point, and be rescheduled
1458 * to a different CPU. That will cause cacheline bouncing, but no
1459 * additional races since q_lock already protects against other CPUs.
1460 */
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001461 put_nvmeq(nvmeq);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001462 if (length != (io.nblocks + 1) << ns->lba_shift)
1463 status = -ENOMEM;
Keith Busch22404272013-07-15 15:02:20 -06001464 else if (!nvmeq || nvmeq->q_suspended)
1465 status = -EBUSY;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001466 else
Matthew Wilcoxff976d72011-12-20 13:53:01 -05001467 status = nvme_submit_sync_cmd(nvmeq, &c, NULL, NVME_IO_TIMEOUT);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001468
Keith Buschf410c682013-04-23 17:23:59 -06001469 if (meta_len) {
1470 if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
1471 int meta_offset = 0;
1472
1473 for (i = 0; i < meta_iod->nents; i++) {
1474 meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
1475 meta_iod->sg[i].offset;
1476 memcpy(meta, meta_mem + meta_offset,
1477 meta_iod->sg[i].length);
1478 kunmap_atomic(meta);
1479 meta_offset += meta_iod->sg[i].length;
1480 }
1481 }
1482
1483 dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
1484 meta_dma_addr);
1485 }
1486
1487 unmap:
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001488 nvme_unmap_user_pages(dev, io.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001489 nvme_free_iod(dev, iod);
Keith Buschf410c682013-04-23 17:23:59 -06001490
1491 if (meta_iod) {
1492 nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
1493 nvme_free_iod(dev, meta_iod);
1494 }
1495
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001496 return status;
1497}
1498
Keith Busch50af8ba2012-07-25 16:07:55 -06001499static int nvme_user_admin_cmd(struct nvme_dev *dev,
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001500 struct nvme_admin_cmd __user *ucmd)
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001501{
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001502 struct nvme_admin_cmd cmd;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001503 struct nvme_command c;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001504 int status, length;
Keith Buschc7d36ab2012-07-27 11:53:28 -06001505 struct nvme_iod *uninitialized_var(iod);
Keith Busch94f370c2013-05-09 14:01:38 -06001506 unsigned timeout;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001507
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001508 if (!capable(CAP_SYS_ADMIN))
1509 return -EACCES;
1510 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001511 return -EFAULT;
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001512
1513 memset(&c, 0, sizeof(c));
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001514 c.common.opcode = cmd.opcode;
1515 c.common.flags = cmd.flags;
1516 c.common.nsid = cpu_to_le32(cmd.nsid);
1517 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1518 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1519 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1520 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1521 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1522 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1523 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1524 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1525
1526 length = cmd.data_len;
1527 if (cmd.data_len) {
Matthew Wilcox49742182012-01-06 13:42:45 -07001528 iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
1529 length);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001530 if (IS_ERR(iod))
1531 return PTR_ERR(iod);
1532 length = nvme_setup_prps(dev, &c.common, iod, length,
1533 GFP_KERNEL);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001534 }
1535
Keith Busch94f370c2013-05-09 14:01:38 -06001536 timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
1537 ADMIN_TIMEOUT;
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001538 if (length != cmd.data_len)
Matthew Wilcoxb77954c2011-05-12 13:51:41 -04001539 status = -ENOMEM;
1540 else
Keith Busch94f370c2013-05-09 14:01:38 -06001541 status = nvme_submit_sync_cmd(dev->queues[0], &c, &cmd.result,
1542 timeout);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001543
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001544 if (cmd.data_len) {
Matthew Wilcox1c2ad9f2012-01-06 13:52:56 -07001545 nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -05001546 nvme_free_iod(dev, iod);
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001547 }
Keith Buschf4f117f2012-09-21 10:49:05 -06001548
Chayan Biswascf90bc42013-05-22 22:34:49 +00001549 if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
Keith Buschf4f117f2012-09-21 10:49:05 -06001550 sizeof(cmd.result)))
1551 status = -EFAULT;
1552
Matthew Wilcox6ee44cd2011-02-03 10:58:26 -05001553 return status;
1554}
1555
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001556static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1557 unsigned long arg)
1558{
1559 struct nvme_ns *ns = bdev->bd_disk->private_data;
1560
1561 switch (cmd) {
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001562 case NVME_IOCTL_ID:
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001563 force_successful_syscall_return();
Matthew Wilcox6bbf1ac2011-05-20 13:03:42 -04001564 return ns->ns_id;
1565 case NVME_IOCTL_ADMIN_CMD:
Keith Busch50af8ba2012-07-25 16:07:55 -06001566 return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
Matthew Wilcoxa53295b2011-02-01 16:13:29 -05001567 case NVME_IOCTL_SUBMIT_IO:
1568 return nvme_submit_io(ns, (void __user *)arg);
Vishal Verma5d0f6132013-03-04 18:40:58 -07001569 case SG_GET_VERSION_NUM:
1570 return nvme_sg_get_version_num((void __user *)arg);
1571 case SG_IO:
1572 return nvme_sg_io(ns, (void __user *)arg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001573 default:
1574 return -ENOTTY;
1575 }
1576}
1577
1578static const struct block_device_operations nvme_fops = {
1579 .owner = THIS_MODULE,
1580 .ioctl = nvme_ioctl,
Matthew Wilcox49481682011-03-19 14:55:38 -04001581 .compat_ioctl = nvme_ioctl,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001582};
1583
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001584static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
1585{
1586 while (bio_list_peek(&nvmeq->sq_cong)) {
1587 struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
1588 struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
Keith Busch427e9702013-04-09 11:59:32 -06001589
Matthew Wilcox3cb967c2011-03-16 16:45:49 -04001590 if (bio_list_empty(&nvmeq->sq_cong))
1591 remove_wait_queue(&nvmeq->sq_full,
1592 &nvmeq->sq_cong_wait);
Keith Busch427e9702013-04-09 11:59:32 -06001593 if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
1594 if (bio_list_empty(&nvmeq->sq_cong))
1595 add_wait_queue(&nvmeq->sq_full,
1596 &nvmeq->sq_cong_wait);
1597 bio_list_add_head(&nvmeq->sq_cong, bio);
1598 break;
1599 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001600 }
1601}
1602
1603static int nvme_kthread(void *data)
1604{
1605 struct nvme_dev *dev;
1606
1607 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001608 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001609 spin_lock(&dev_list_lock);
1610 list_for_each_entry(dev, &dev_list, node) {
1611 int i;
1612 for (i = 0; i < dev->queue_count; i++) {
1613 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001614 if (!nvmeq)
1615 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001616 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001617 if (nvmeq->q_suspended)
1618 goto unlock;
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001619 nvme_process_cq(nvmeq);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001620 nvme_cancel_ios(nvmeq, true);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001621 nvme_resubmit_bios(nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001622 unlock:
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001623 spin_unlock_irq(&nvmeq->q_lock);
1624 }
1625 }
1626 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001627 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001628 }
1629 return 0;
1630}
1631
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001632static DEFINE_IDA(nvme_index_ida);
1633
1634static int nvme_get_ns_idx(void)
1635{
1636 int index, error;
1637
1638 do {
1639 if (!ida_pre_get(&nvme_index_ida, GFP_KERNEL))
1640 return -1;
1641
1642 spin_lock(&dev_list_lock);
1643 error = ida_get_new(&nvme_index_ida, &index);
1644 spin_unlock(&dev_list_lock);
1645 } while (error == -EAGAIN);
1646
1647 if (error)
1648 index = -1;
1649 return index;
1650}
1651
1652static void nvme_put_ns_idx(int index)
1653{
1654 spin_lock(&dev_list_lock);
1655 ida_remove(&nvme_index_ida, index);
1656 spin_unlock(&dev_list_lock);
1657}
1658
Keith Busch0e5e4f02012-11-09 16:33:05 -07001659static void nvme_config_discard(struct nvme_ns *ns)
1660{
1661 u32 logical_block_size = queue_logical_block_size(ns->queue);
1662 ns->queue->limits.discard_zeroes_data = 0;
1663 ns->queue->limits.discard_alignment = logical_block_size;
1664 ns->queue->limits.discard_granularity = logical_block_size;
1665 ns->queue->limits.max_discard_sectors = 0xffffffff;
1666 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
1667}
1668
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001669static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001670 struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
1671{
1672 struct nvme_ns *ns;
1673 struct gendisk *disk;
1674 int lbaf;
1675
1676 if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
1677 return NULL;
1678
1679 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1680 if (!ns)
1681 return NULL;
1682 ns->queue = blk_alloc_queue(GFP_KERNEL);
1683 if (!ns->queue)
1684 goto out_free_ns;
Matthew Wilcox4eeb9212012-01-10 14:35:08 -07001685 ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
1686 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1687 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001688 blk_queue_make_request(ns->queue, nvme_make_request);
1689 ns->dev = dev;
1690 ns->queue->queuedata = ns;
1691
1692 disk = alloc_disk(NVME_MINORS);
1693 if (!disk)
1694 goto out_free_queue;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001695 ns->ns_id = nsid;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001696 ns->disk = disk;
1697 lbaf = id->flbas & 0xf;
1698 ns->lba_shift = id->lbaf[lbaf].ds;
Keith Buschf410c682013-04-23 17:23:59 -06001699 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
Keith Busche9ef4632012-07-24 15:01:04 -06001700 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Keith Busch8fc23e02012-07-26 11:29:57 -06001701 if (dev->max_hw_sectors)
1702 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001703
1704 disk->major = nvme_major;
1705 disk->minors = NVME_MINORS;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001706 disk->first_minor = NVME_MINORS * nvme_get_ns_idx();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001707 disk->fops = &nvme_fops;
1708 disk->private_data = ns;
1709 disk->queue = ns->queue;
Matthew Wilcox388f0372011-02-01 12:49:38 -05001710 disk->driverfs_dev = &dev->pci_dev->dev;
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001711 sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001712 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1713
Keith Busch0e5e4f02012-11-09 16:33:05 -07001714 if (dev->oncs & NVME_CTRL_ONCS_DSM)
1715 nvme_config_discard(ns);
1716
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001717 return ns;
1718
1719 out_free_queue:
1720 blk_cleanup_queue(ns->queue);
1721 out_free_ns:
1722 kfree(ns);
1723 return NULL;
1724}
1725
1726static void nvme_ns_free(struct nvme_ns *ns)
1727{
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001728 int index = ns->disk->first_minor / NVME_MINORS;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001729 put_disk(ns->disk);
Matthew Wilcox5aff9382011-05-06 08:45:47 -04001730 nvme_put_ns_idx(index);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001731 blk_cleanup_queue(ns->queue);
1732 kfree(ns);
1733}
1734
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001735static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001736{
1737 int status;
1738 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001739 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001740
Matthew Wilcoxdf348132012-01-11 07:29:56 -07001741 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001742 &result);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001743 if (status)
Keith Busch7e03b122013-07-29 16:20:56 -06001744 return status < 0 ? -EIO : -EBUSY;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001745 return min(result & 0xffff, result >> 16) + 1;
1746}
1747
Keith Busch9d713c22013-07-15 15:02:24 -06001748static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1749{
1750 return 4096 + ((nr_io_queues + 1) << (dev->db_stride + 3));
1751}
1752
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001753static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001754{
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001755 struct pci_dev *pdev = dev->pci_dev;
Keith Busch9d713c22013-07-15 15:02:24 -06001756 int result, cpu, i, vecs, nr_io_queues, size, q_depth;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001757
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001758 nr_io_queues = num_online_cpus();
1759 result = set_queue_count(dev, nr_io_queues);
Matthew Wilcox1b234842011-01-20 13:01:49 -05001760 if (result < 0)
1761 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001762 if (result < nr_io_queues)
1763 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001764
Keith Busch9d713c22013-07-15 15:02:24 -06001765 size = db_bar_size(dev, nr_io_queues);
1766 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001767 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06001768 do {
1769 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1770 if (dev->bar)
1771 break;
1772 if (!--nr_io_queues)
1773 return -ENOMEM;
1774 size = db_bar_size(dev, nr_io_queues);
1775 } while (1);
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001776 dev->dbs = ((void __iomem *)dev->bar) + 4096;
1777 dev->queues[0]->q_db = dev->dbs;
1778 }
1779
Keith Busch9d713c22013-07-15 15:02:24 -06001780 /* Deregister the admin queue's interrupt */
1781 free_irq(dev->entry[0].vector, dev->queues[0]);
1782
Matthew Wilcox063a8092013-06-20 10:53:48 -04001783 vecs = nr_io_queues;
1784 for (i = 0; i < vecs; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001785 dev->entry[i].entry = i;
1786 for (;;) {
Matthew Wilcox063a8092013-06-20 10:53:48 -04001787 result = pci_enable_msix(pdev, dev->entry, vecs);
1788 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001789 break;
Matthew Wilcox063a8092013-06-20 10:53:48 -04001790 vecs = result;
1791 }
1792
1793 if (result < 0) {
1794 vecs = nr_io_queues;
1795 if (vecs > 32)
1796 vecs = 32;
1797 for (;;) {
1798 result = pci_enable_msi_block(pdev, vecs);
1799 if (result == 0) {
1800 for (i = 0; i < vecs; i++)
1801 dev->entry[i].vector = i + pdev->irq;
1802 break;
1803 } else if (result < 0) {
1804 vecs = 1;
1805 break;
1806 }
1807 vecs = result;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001808 }
1809 }
1810
Matthew Wilcox063a8092013-06-20 10:53:48 -04001811 /*
1812 * Should investigate if there's a performance win from allocating
1813 * more queues than interrupt vectors; it might allow the submission
1814 * path to scale better, even if the receive path is limited by the
1815 * number of interrupts.
1816 */
1817 nr_io_queues = vecs;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001818
Matthew Wilcox1b234842011-01-20 13:01:49 -05001819 result = queue_request_irq(dev, dev->queues[0], "nvme admin");
Keith Busch9d713c22013-07-15 15:02:24 -06001820 if (result) {
1821 dev->queues[0]->q_suspended = 1;
Keith Busch22404272013-07-15 15:02:20 -06001822 goto free_queues;
Keith Busch9d713c22013-07-15 15:02:24 -06001823 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001824
Keith Buschcd638942013-07-15 15:02:23 -06001825 /* Free previously allocated queues that are no longer usable */
1826 spin_lock(&dev_list_lock);
1827 for (i = dev->queue_count - 1; i > nr_io_queues; i--) {
1828 struct nvme_queue *nvmeq = dev->queues[i];
1829
1830 spin_lock(&nvmeq->q_lock);
1831 nvme_cancel_ios(nvmeq, false);
1832 spin_unlock(&nvmeq->q_lock);
1833
1834 nvme_free_queue(nvmeq);
1835 dev->queue_count--;
1836 dev->queues[i] = NULL;
1837 }
1838 spin_unlock(&dev_list_lock);
1839
Matthew Wilcox1b234842011-01-20 13:01:49 -05001840 cpu = cpumask_first(cpu_online_mask);
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001841 for (i = 0; i < nr_io_queues; i++) {
Matthew Wilcox1b234842011-01-20 13:01:49 -05001842 irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu));
1843 cpu = cpumask_next(cpu, cpu_online_mask);
1844 }
1845
Keith Buscha0cadb82012-07-27 13:57:23 -04001846 q_depth = min_t(int, NVME_CAP_MQES(readq(&dev->bar->cap)) + 1,
1847 NVME_Q_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001848 for (i = dev->queue_count - 1; i < nr_io_queues; i++) {
Keith Busch22404272013-07-15 15:02:20 -06001849 dev->queues[i + 1] = nvme_alloc_queue(dev, i + 1, q_depth, i);
1850 if (!dev->queues[i + 1]) {
1851 result = -ENOMEM;
1852 goto free_queues;
1853 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001854 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001855
Matthew Wilcox9ecdc942011-03-16 16:52:19 -04001856 for (; i < num_possible_cpus(); i++) {
1857 int target = i % rounddown_pow_of_two(dev->queue_count - 1);
1858 dev->queues[i + 1] = dev->queues[target + 1];
1859 }
1860
Keith Busch22404272013-07-15 15:02:20 -06001861 for (i = 1; i < dev->queue_count; i++) {
1862 result = nvme_create_queue(dev->queues[i], i);
1863 if (result) {
1864 for (--i; i > 0; i--)
1865 nvme_disable_queue(dev, i);
1866 goto free_queues;
1867 }
1868 }
1869
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001870 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001871
Keith Busch22404272013-07-15 15:02:20 -06001872 free_queues:
1873 nvme_free_queues(dev);
1874 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001875}
1876
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04001877/*
1878 * Return: error value if an error occurred setting up the queues or calling
1879 * Identify Device. 0 if these succeeded, even if adding some of the
1880 * namespaces failed. At the moment, these failures are silent. TBD which
1881 * failures should be reported.
1882 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001883static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001884{
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -04001885 int res;
1886 unsigned nn, i;
Keith Buschcbb62182013-05-01 13:07:49 -06001887 struct nvme_ns *ns;
Matthew Wilcox51814232011-02-01 16:18:08 -05001888 struct nvme_id_ctrl *ctrl;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001889 struct nvme_id_ns *id_ns;
1890 void *mem;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001891 dma_addr_t dma_addr;
Keith Busch159b67d2013-04-09 17:13:20 -06001892 int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001893
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001894 mem = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001895 GFP_KERNEL);
Keith Buscha9ef4342013-05-01 13:07:48 -06001896 if (!mem)
1897 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001898
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001899 res = nvme_identify(dev, 0, 1, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001900 if (res) {
1901 res = -EIO;
Keith Buschcbb62182013-05-01 13:07:49 -06001902 goto out;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001903 }
1904
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001905 ctrl = mem;
Matthew Wilcox51814232011-02-01 16:18:08 -05001906 nn = le32_to_cpup(&ctrl->nn);
Keith Busch0e5e4f02012-11-09 16:33:05 -07001907 dev->oncs = le16_to_cpup(&ctrl->oncs);
Matthew Wilcox51814232011-02-01 16:18:08 -05001908 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
1909 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
1910 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
Keith Busch159b67d2013-04-09 17:13:20 -06001911 if (ctrl->mdts)
Keith Busch8fc23e02012-07-26 11:29:57 -06001912 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
Keith Busch159b67d2013-04-09 17:13:20 -06001913 if ((dev->pci_dev->vendor == PCI_VENDOR_ID_INTEL) &&
1914 (dev->pci_dev->device == 0x0953) && ctrl->vs[3])
1915 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001916
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001917 id_ns = mem;
Matthew Wilcox2b2c1892011-10-07 13:10:13 -04001918 for (i = 1; i <= nn; i++) {
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001919 res = nvme_identify(dev, i, 0, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001920 if (res)
1921 continue;
1922
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001923 if (id_ns->ncap == 0)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001924 continue;
1925
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001926 res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
Keith Busch08df1e02012-09-21 10:52:13 -06001927 dma_addr + 4096, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001928 if (res)
Keith Busch12209032013-01-31 14:40:38 -07001929 memset(mem + 4096, 0, 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001930
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001931 ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001932 if (ns)
1933 list_add_tail(&ns->list, &dev->namespaces);
1934 }
1935 list_for_each_entry(ns, &dev->namespaces, list)
1936 add_disk(ns->disk);
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04001937 res = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001938
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001939 out:
Matthew Wilcox684f5c22011-09-19 17:14:53 -04001940 dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001941 return res;
1942}
1943
Keith Busch0877cb02013-07-15 15:02:19 -06001944static int nvme_dev_map(struct nvme_dev *dev)
1945{
1946 int bars, result = -ENOMEM;
1947 struct pci_dev *pdev = dev->pci_dev;
1948
1949 if (pci_enable_device_mem(pdev))
1950 return result;
1951
1952 dev->entry[0].vector = pdev->irq;
1953 pci_set_master(pdev);
1954 bars = pci_select_bars(pdev, IORESOURCE_MEM);
1955 if (pci_request_selected_regions(pdev, bars, "nvme"))
1956 goto disable_pci;
1957
Russell King052d0ef2013-06-26 23:49:11 +01001958 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
1959 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
1960 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06001961
1962 pci_set_drvdata(pdev, dev);
1963 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1964 if (!dev->bar)
1965 goto disable;
1966
1967 dev->db_stride = NVME_CAP_STRIDE(readq(&dev->bar->cap));
1968 dev->dbs = ((void __iomem *)dev->bar) + 4096;
1969
1970 return 0;
1971
1972 disable:
1973 pci_release_regions(pdev);
1974 disable_pci:
1975 pci_disable_device(pdev);
1976 return result;
1977}
1978
1979static void nvme_dev_unmap(struct nvme_dev *dev)
1980{
1981 if (dev->pci_dev->msi_enabled)
1982 pci_disable_msi(dev->pci_dev);
1983 else if (dev->pci_dev->msix_enabled)
1984 pci_disable_msix(dev->pci_dev);
1985
1986 if (dev->bar) {
1987 iounmap(dev->bar);
1988 dev->bar = NULL;
1989 }
1990
1991 pci_release_regions(dev->pci_dev);
1992 if (pci_is_enabled(dev->pci_dev))
1993 pci_disable_device(dev->pci_dev);
1994}
1995
Keith Buschf0b50732013-07-15 15:02:21 -06001996static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001997{
Keith Busch22404272013-07-15 15:02:20 -06001998 int i;
1999
2000 for (i = dev->queue_count - 1; i >= 0; i--)
2001 nvme_disable_queue(dev, i);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002002
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002003 spin_lock(&dev_list_lock);
Keith Buschf0b50732013-07-15 15:02:21 -06002004 list_del_init(&dev->node);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002005 spin_unlock(&dev_list_lock);
2006
Keith Busch1894d8f2013-07-15 15:02:22 -06002007 if (dev->bar)
2008 nvme_shutdown_ctrl(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002009 nvme_dev_unmap(dev);
2010}
2011
2012static void nvme_dev_remove(struct nvme_dev *dev)
2013{
2014 struct nvme_ns *ns, *next;
2015
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002016 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
2017 list_del(&ns->list);
2018 del_gendisk(ns->disk);
2019 nvme_ns_free(ns);
2020 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002021}
2022
Matthew Wilcox091b6092011-02-10 09:56:01 -05002023static int nvme_setup_prp_pools(struct nvme_dev *dev)
2024{
2025 struct device *dmadev = &dev->pci_dev->dev;
2026 dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
2027 PAGE_SIZE, PAGE_SIZE, 0);
2028 if (!dev->prp_page_pool)
2029 return -ENOMEM;
2030
Matthew Wilcox99802a72011-02-10 10:30:34 -05002031 /* Optimisation for I/Os between 4k and 128k */
2032 dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
2033 256, 256, 0);
2034 if (!dev->prp_small_pool) {
2035 dma_pool_destroy(dev->prp_page_pool);
2036 return -ENOMEM;
2037 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002038 return 0;
2039}
2040
2041static void nvme_release_prp_pools(struct nvme_dev *dev)
2042{
2043 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002044 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002045}
2046
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002047static DEFINE_IDA(nvme_instance_ida);
2048
2049static int nvme_set_instance(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002050{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002051 int instance, error;
2052
2053 do {
2054 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2055 return -ENODEV;
2056
2057 spin_lock(&dev_list_lock);
2058 error = ida_get_new(&nvme_instance_ida, &instance);
2059 spin_unlock(&dev_list_lock);
2060 } while (error == -EAGAIN);
2061
2062 if (error)
2063 return -ENODEV;
2064
2065 dev->instance = instance;
2066 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002067}
2068
2069static void nvme_release_instance(struct nvme_dev *dev)
2070{
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002071 spin_lock(&dev_list_lock);
2072 ida_remove(&nvme_instance_ida, dev->instance);
2073 spin_unlock(&dev_list_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002074}
2075
Keith Busch5e82e952013-02-19 10:17:58 -07002076static void nvme_free_dev(struct kref *kref)
2077{
2078 struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
2079 nvme_dev_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002080 nvme_dev_shutdown(dev);
2081 nvme_free_queues(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002082 nvme_release_instance(dev);
2083 nvme_release_prp_pools(dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002084 kfree(dev->queues);
2085 kfree(dev->entry);
2086 kfree(dev);
2087}
2088
2089static int nvme_dev_open(struct inode *inode, struct file *f)
2090{
2091 struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
2092 miscdev);
2093 kref_get(&dev->kref);
2094 f->private_data = dev;
2095 return 0;
2096}
2097
2098static int nvme_dev_release(struct inode *inode, struct file *f)
2099{
2100 struct nvme_dev *dev = f->private_data;
2101 kref_put(&dev->kref, nvme_free_dev);
2102 return 0;
2103}
2104
2105static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2106{
2107 struct nvme_dev *dev = f->private_data;
2108 switch (cmd) {
2109 case NVME_IOCTL_ADMIN_CMD:
2110 return nvme_user_admin_cmd(dev, (void __user *)arg);
2111 default:
2112 return -ENOTTY;
2113 }
2114}
2115
2116static const struct file_operations nvme_dev_fops = {
2117 .owner = THIS_MODULE,
2118 .open = nvme_dev_open,
2119 .release = nvme_dev_release,
2120 .unlocked_ioctl = nvme_dev_ioctl,
2121 .compat_ioctl = nvme_dev_ioctl,
2122};
2123
Keith Buschf0b50732013-07-15 15:02:21 -06002124static int nvme_dev_start(struct nvme_dev *dev)
2125{
2126 int result;
2127
2128 result = nvme_dev_map(dev);
2129 if (result)
2130 return result;
2131
2132 result = nvme_configure_admin_queue(dev);
2133 if (result)
2134 goto unmap;
2135
2136 spin_lock(&dev_list_lock);
2137 list_add(&dev->node, &dev_list);
2138 spin_unlock(&dev_list_lock);
2139
2140 result = nvme_setup_io_queues(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002141 if (result && result != -EBUSY)
Keith Buschf0b50732013-07-15 15:02:21 -06002142 goto disable;
2143
Keith Buschd82e8bf2013-09-05 14:45:07 -06002144 return result;
Keith Buschf0b50732013-07-15 15:02:21 -06002145
2146 disable:
2147 spin_lock(&dev_list_lock);
2148 list_del_init(&dev->node);
2149 spin_unlock(&dev_list_lock);
2150 unmap:
2151 nvme_dev_unmap(dev);
2152 return result;
2153}
2154
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002155static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002156{
Keith Busch0877cb02013-07-15 15:02:19 -06002157 int result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002158 struct nvme_dev *dev;
2159
2160 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2161 if (!dev)
2162 return -ENOMEM;
2163 dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
2164 GFP_KERNEL);
2165 if (!dev->entry)
2166 goto free;
Matthew Wilcox1b234842011-01-20 13:01:49 -05002167 dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
2168 GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002169 if (!dev->queues)
2170 goto free;
2171
2172 INIT_LIST_HEAD(&dev->namespaces);
2173 dev->pci_dev = pdev;
Russell King052d0ef2013-06-26 23:49:11 +01002174
Quoc-Son Anhcd58ad72012-02-21 16:50:53 -07002175 result = nvme_set_instance(dev);
2176 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002177 goto free;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002178
Matthew Wilcox091b6092011-02-10 09:56:01 -05002179 result = nvme_setup_prp_pools(dev);
2180 if (result)
Keith Busch0877cb02013-07-15 15:02:19 -06002181 goto release;
Matthew Wilcox091b6092011-02-10 09:56:01 -05002182
Keith Buschf0b50732013-07-15 15:02:21 -06002183 result = nvme_dev_start(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002184 if (result) {
2185 if (result == -EBUSY)
2186 goto create_cdev;
Keith Busch0877cb02013-07-15 15:02:19 -06002187 goto release_pools;
Keith Buschd82e8bf2013-09-05 14:45:07 -06002188 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002189
Matthew Wilcox740216f2011-02-15 16:28:20 -05002190 result = nvme_dev_add(dev);
Keith Buschd82e8bf2013-09-05 14:45:07 -06002191 if (result)
Keith Buschf0b50732013-07-15 15:02:21 -06002192 goto shutdown;
Matthew Wilcox740216f2011-02-15 16:28:20 -05002193
Keith Buschd82e8bf2013-09-05 14:45:07 -06002194 create_cdev:
Keith Busch5e82e952013-02-19 10:17:58 -07002195 scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
2196 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2197 dev->miscdev.parent = &pdev->dev;
2198 dev->miscdev.name = dev->name;
2199 dev->miscdev.fops = &nvme_dev_fops;
2200 result = misc_register(&dev->miscdev);
2201 if (result)
2202 goto remove;
2203
2204 kref_init(&dev->kref);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002205 return 0;
2206
Keith Busch5e82e952013-02-19 10:17:58 -07002207 remove:
2208 nvme_dev_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002209 shutdown:
2210 nvme_dev_shutdown(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002211 release_pools:
Keith Buschf0b50732013-07-15 15:02:21 -06002212 nvme_free_queues(dev);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002213 nvme_release_prp_pools(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002214 release:
2215 nvme_release_instance(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002216 free:
2217 kfree(dev->queues);
2218 kfree(dev->entry);
2219 kfree(dev);
2220 return result;
2221}
2222
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002223static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002224{
2225 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch5e82e952013-02-19 10:17:58 -07002226 misc_deregister(&dev->miscdev);
2227 kref_put(&dev->kref, nvme_free_dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002228}
2229
2230/* These functions are yet to be implemented */
2231#define nvme_error_detected NULL
2232#define nvme_dump_registers NULL
2233#define nvme_link_reset NULL
2234#define nvme_slot_reset NULL
2235#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002236
2237static int nvme_suspend(struct device *dev)
2238{
2239 struct pci_dev *pdev = to_pci_dev(dev);
2240 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2241
2242 nvme_dev_shutdown(ndev);
2243 return 0;
2244}
2245
2246static int nvme_resume(struct device *dev)
2247{
2248 struct pci_dev *pdev = to_pci_dev(dev);
2249 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2250 int ret;
2251
2252 ret = nvme_dev_start(ndev);
2253 /* XXX: should remove gendisks if resume fails */
2254 if (ret)
2255 nvme_free_queues(ndev);
2256 return ret;
2257}
2258
2259static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002260
Stephen Hemminger1d352032012-09-07 09:33:17 -07002261static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002262 .error_detected = nvme_error_detected,
2263 .mmio_enabled = nvme_dump_registers,
2264 .link_reset = nvme_link_reset,
2265 .slot_reset = nvme_slot_reset,
2266 .resume = nvme_error_resume,
2267};
2268
2269/* Move to pci_ids.h later */
2270#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2271
2272static DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
2273 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2274 { 0, }
2275};
2276MODULE_DEVICE_TABLE(pci, nvme_id_table);
2277
2278static struct pci_driver nvme_driver = {
2279 .name = "nvme",
2280 .id_table = nvme_id_table,
2281 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002282 .remove = nvme_remove,
Keith Buschcd638942013-07-15 15:02:23 -06002283 .driver = {
2284 .pm = &nvme_dev_pm_ops,
2285 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002286 .err_handler = &nvme_err_handler,
2287};
2288
2289static int __init nvme_init(void)
2290{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002291 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002292
2293 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2294 if (IS_ERR(nvme_thread))
2295 return PTR_ERR(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002296
Keith Busch5c42ea12012-07-25 16:05:18 -06002297 result = register_blkdev(nvme_major, "nvme");
2298 if (result < 0)
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002299 goto kill_kthread;
Keith Busch5c42ea12012-07-25 16:05:18 -06002300 else if (result > 0)
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002301 nvme_major = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002302
2303 result = pci_register_driver(&nvme_driver);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002304 if (result)
2305 goto unregister_blkdev;
2306 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002307
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002308 unregister_blkdev:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002309 unregister_blkdev(nvme_major, "nvme");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002310 kill_kthread:
2311 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002312 return result;
2313}
2314
2315static void __exit nvme_exit(void)
2316{
2317 pci_unregister_driver(&nvme_driver);
2318 unregister_blkdev(nvme_major, "nvme");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002319 kthread_stop(nvme_thread);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002320}
2321
2322MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2323MODULE_LICENSE("GPL");
Matthew Wilcox366e8212012-01-10 16:30:15 -05002324MODULE_VERSION("0.8");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002325module_init(nvme_init);
2326module_exit(nvme_exit);