nvme-core.c revision d8ee9d69f275769aaad40ef7c944565ff8d2d24f
1b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 2b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * NVM Express device driver 3b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Copyright (c) 2011, Intel Corporation. 4b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * 5b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * This program is free software; you can redistribute it and/or modify it 6b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * under the terms and conditions of the GNU General Public License, 7b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * version 2, as published by the Free Software Foundation. 8b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * 9b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * This program is distributed in the hope it will be useful, but WITHOUT 10b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * more details. 13b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * 14b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * You should have received a copy of the GNU General Public License along with 15b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * this program; if not, write to the Free Software Foundation, Inc., 16b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 17b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 18b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 19b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/nvme.h> 20b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/bio.h> 21b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/blkdev.h> 22b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/errno.h> 23b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/fs.h> 24b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/genhd.h> 25b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/init.h> 26b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/interrupt.h> 27b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/io.h> 28b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/kdev_t.h> 291fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox#include <linux/kthread.h> 30b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/kernel.h> 31b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/mm.h> 32b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/module.h> 33b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/moduleparam.h> 34b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/pci.h> 35be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox#include <linux/poison.h> 36b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/sched.h> 37b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/slab.h> 38b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/types.h> 39b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/version.h> 40b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 41b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define NVME_Q_DEPTH 1024 42b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command)) 43b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion)) 44b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define NVME_MINORS 64 45e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox#define IO_TIMEOUT (5 * HZ) 46e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox#define ADMIN_TIMEOUT (60 * HZ) 47b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 48b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_major; 49b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_param(nvme_major, int, 0); 50b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 5158ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxstatic int use_threaded_interrupts; 5258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxmodule_param(use_threaded_interrupts, int, 0); 5358ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox 541fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic DEFINE_SPINLOCK(dev_list_lock); 551fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic LIST_HEAD(dev_list); 561fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic struct task_struct *nvme_thread; 571fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 58b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 59b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Represents an NVM Express device. Each nvme_dev is a PCI function. 60b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 61b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct nvme_dev { 621fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox struct list_head node; 63b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue **queues; 64b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 __iomem *dbs; 65b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct pci_dev *pci_dev; 66091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox struct dma_pool *prp_page_pool; 6799802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox struct dma_pool *prp_small_pool; 68b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int instance; 69b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int queue_count; 70b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 ctrl_config; 71b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct msix_entry *entry; 72b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_bar __iomem *bar; 73b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct list_head namespaces; 7451814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox char serial[20]; 7551814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox char model[40]; 7651814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox char firmware_rev[8]; 77b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 78b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 79b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 80b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * An NVM Express namespace is equivalent to a SCSI LUN 81b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 82b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct nvme_ns { 83b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct list_head list; 84b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 85b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_dev *dev; 86b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct request_queue *queue; 87b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct gendisk *disk; 88b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 89b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int ns_id; 90b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int lba_shift; 91b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 92b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 93b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 94b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * An NVM Express queue. Each device has at least two (one for admin 95b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * commands and one for I/O commands). 96b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 97b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct nvme_queue { 98b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct device *q_dmadev; 99091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox struct nvme_dev *dev; 100b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox spinlock_t q_lock; 101b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command *sq_cmds; 102b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox volatile struct nvme_completion *cqes; 103b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_addr_t sq_dma_addr; 104b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_addr_t cq_dma_addr; 105b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox wait_queue_head_t sq_full; 1061fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox wait_queue_t sq_cong_wait; 107b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct bio_list sq_cong; 108b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 __iomem *q_db; 109b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 q_depth; 110b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 cq_vector; 111b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 sq_head; 112b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 sq_tail; 113b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 cq_head; 114821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox u16 cq_phase; 115b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long cmdid_data[]; 116b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 117b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 118b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 119b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Check we didin't inadvertently grow the command struct 120b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 121b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic inline void _nvme_check_size(void) 122b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 123b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64); 124b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64); 125b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64); 126b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64); 127b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_features) != 64); 128b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_command) != 64); 129b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096); 130b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096); 131b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64); 132b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 133b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 134e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstruct nvme_cmd_info { 135e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox unsigned long ctx; 136e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox unsigned long timeout; 137e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox}; 138e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox 139e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstatic struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq) 140e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox{ 141e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)]; 142e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox} 143e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox 144b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/** 145b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * alloc_cmdid - Allocate a Command ID 146b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @param nvmeq The queue that will be used for this command 147b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @param ctx A pointer that will be passed to the handler 148b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @param handler The ID of the handler to call 149b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * 150b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Allocate a Command ID for a queue. The data passed in will 151b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * be passed to the completion handler. This is implemented by using 152b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * the bottom two bits of the ctx pointer to store the handler ID. 153b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Passing in a pointer that's not 4-byte aligned will cause a BUG. 154b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * We can change this if it becomes a problem. 155b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 156e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstatic int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx, int handler, 157e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox unsigned timeout) 158b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 159b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int depth = nvmeq->q_depth; 160e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); 161b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int cmdid; 162b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 163b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUG_ON((unsigned long)ctx & 3); 164b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 165b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox do { 166b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth); 167b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (cmdid >= depth) 168b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EBUSY; 169b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } while (test_and_set_bit(cmdid, nvmeq->cmdid_data)); 170b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 171e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox info[cmdid].ctx = (unsigned long)ctx | handler; 172e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox info[cmdid].timeout = jiffies + timeout; 173b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return cmdid; 174b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 175b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 176b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx, 177e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox int handler, unsigned timeout) 178b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 179b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int cmdid; 180b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox wait_event_killable(nvmeq->sq_full, 181e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0); 182b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return (cmdid < 0) ? -EINTR : cmdid; 183b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 184b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 185b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* If you need more than four handlers, you'll need to change how 186be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox * alloc_cmdid and nvme_process_cq work. Consider using a special 187be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox * CMD_CTX value instead, if that works for your situation. 188b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 189b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxenum { 190b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox sync_completion_id = 0, 191b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_completion_id, 192b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 193b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 19400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox/* Special values must be a multiple of 4, and less than 0x1000 */ 195be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox#define CMD_CTX_BASE (POISON_POINTER_DELTA + sync_completion_id) 196d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE) 197d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE) 198d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE) 19900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox#define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE) 200be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox 201b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic unsigned long free_cmdid(struct nvme_queue *nvmeq, int cmdid) 202b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 203b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long data; 204e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); 205b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 206e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox if (cmdid >= nvmeq->q_depth) 20748e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox return CMD_CTX_INVALID; 208e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox data = info[cmdid].ctx; 209e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox info[cmdid].ctx = CMD_CTX_COMPLETED; 210b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox clear_bit(cmdid, nvmeq->cmdid_data); 211b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox wake_up(&nvmeq->sq_full); 212b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return data; 213b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 214b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 215be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcoxstatic void cancel_cmdid_data(struct nvme_queue *nvmeq, int cmdid) 2163c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox{ 217e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); 218e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox info[cmdid].ctx = CMD_CTX_CANCELLED; 2193c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox} 2203c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox 221b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct nvme_queue *get_nvmeq(struct nvme_ns *ns) 222b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 2231b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox int qid, cpu = get_cpu(); 2241b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox if (cpu < ns->dev->queue_count) 2251b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox qid = cpu + 1; 2261b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox else 2271b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox qid = (cpu % rounddown_pow_of_two(ns->dev->queue_count)) + 1; 2281b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox return ns->dev->queues[qid]; 229b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 230b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 231b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void put_nvmeq(struct nvme_queue *nvmeq) 232b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 2331b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox put_cpu(); 234b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 235b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 236b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/** 237b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * nvme_submit_cmd: Copy a command into a queue and ring the doorbell 238b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @nvmeq: The queue to use 239b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @cmd: The command to send 240b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * 241b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Safe to use from interrupt context 242b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 243b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd) 244b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 245b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long flags; 246b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 tail; 247b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox /* XXX: Need to check tail isn't going to overrun head */ 248b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox spin_lock_irqsave(&nvmeq->q_lock, flags); 249b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox tail = nvmeq->sq_tail; 250b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd)); 251b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (++tail == nvmeq->q_depth) 252b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox tail = 0; 2537547881d0951384f9833ec3a80fac8f3f16f3b98Matthew Wilcox writel(tail, nvmeq->q_db); 254b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_tail = tail; 255b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox spin_unlock_irqrestore(&nvmeq->q_lock, flags); 256b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 257b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 258b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 259b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 260e025344c56e08b155f43ea09647969286c78377cShane Michael Matthewsstruct nvme_prps { 261e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews int npages; 262e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr_t first_dma; 263e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews __le64 *list[0]; 264e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews}; 265e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews 266d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcoxstatic void nvme_free_prps(struct nvme_dev *dev, struct nvme_prps *prps) 267e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews{ 268e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews const int last_prp = PAGE_SIZE / 8 - 1; 269e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews int i; 270e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr_t prp_dma; 271e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews 272e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews if (!prps) 273e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews return; 274e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews 275e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prp_dma = prps->first_dma; 27699802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox 27799802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox if (prps->npages == 0) 27899802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox dma_pool_free(dev->prp_small_pool, prps->list[0], prp_dma); 279e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews for (i = 0; i < prps->npages; i++) { 280e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews __le64 *prp_list = prps->list[i]; 281e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]); 282091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox dma_pool_free(dev->prp_page_pool, prp_list, prp_dma); 283e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prp_dma = next_prp_dma; 284e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews } 285e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews kfree(prps); 286e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews} 287e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews 288d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstruct nvme_bio { 289b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct bio *bio; 290b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int nents; 291e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews struct nvme_prps *prps; 292b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct scatterlist sg[0]; 293b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 294b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 295b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* XXX: use a mempool */ 296d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstatic struct nvme_bio *alloc_nbio(unsigned nseg, gfp_t gfp) 297b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 298d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox return kzalloc(sizeof(struct nvme_bio) + 299b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox sizeof(struct scatterlist) * nseg, gfp); 300b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 301b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 302d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstatic void free_nbio(struct nvme_queue *nvmeq, struct nvme_bio *nbio) 303b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 304d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox nvme_free_prps(nvmeq->dev, nbio->prps); 305d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox kfree(nbio); 306b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 307b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 308b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void bio_completion(struct nvme_queue *nvmeq, void *ctx, 309b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_completion *cqe) 310b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 311d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox struct nvme_bio *nbio = ctx; 312d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox struct bio *bio = nbio->bio; 313b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 status = le16_to_cpup(&cqe->status) >> 1; 314b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 315d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox dma_unmap_sg(nvmeq->q_dmadev, nbio->sg, nbio->nents, 316b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 317d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox free_nbio(nvmeq, nbio); 3181ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox if (status) 3191ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox bio_endio(bio, -EIO); 3201ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox if (bio->bi_vcnt > bio->bi_idx) { 3211ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox bio_list_add(&nvmeq->sq_cong, bio); 3221ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox wake_up_process(nvme_thread); 3231ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox } else { 3241ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox bio_endio(bio, 0); 3251ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox } 326b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 327b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 328ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox/* length is in bytes */ 329d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcoxstatic struct nvme_prps *nvme_setup_prps(struct nvme_dev *dev, 330e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews struct nvme_common_command *cmd, 331ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox struct scatterlist *sg, int length) 332ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox{ 33399802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox struct dma_pool *pool; 334ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox int dma_len = sg_dma_len(sg); 335ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox u64 dma_addr = sg_dma_address(sg); 336ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox int offset = offset_in_page(dma_addr); 337e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews __le64 *prp_list; 338e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr_t prp_dma; 339e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews int nprps, npages, i, prp_page; 340e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews struct nvme_prps *prps = NULL; 341ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 342ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmd->prp1 = cpu_to_le64(dma_addr); 343ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox length -= (PAGE_SIZE - offset); 344ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox if (length <= 0) 345e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews return prps; 346ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 347ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox dma_len -= (PAGE_SIZE - offset); 348ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox if (dma_len) { 349ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox dma_addr += (PAGE_SIZE - offset); 350ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox } else { 351ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox sg = sg_next(sg); 352ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox dma_addr = sg_dma_address(sg); 353ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox dma_len = sg_dma_len(sg); 354ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox } 355ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 356ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox if (length <= PAGE_SIZE) { 357ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmd->prp2 = cpu_to_le64(dma_addr); 358e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews return prps; 359e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews } 360e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews 361e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews nprps = DIV_ROUND_UP(length, PAGE_SIZE); 362e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews npages = DIV_ROUND_UP(8 * nprps, PAGE_SIZE); 363e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prps = kmalloc(sizeof(*prps) + sizeof(__le64 *) * npages, GFP_ATOMIC); 364e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prp_page = 0; 36599802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox if (nprps <= (256 / 8)) { 36699802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox pool = dev->prp_small_pool; 36799802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox prps->npages = 0; 36899802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox } else { 36999802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox pool = dev->prp_page_pool; 37099802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox prps->npages = npages; 37199802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox } 37299802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox 37399802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma); 374e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prps->list[prp_page++] = prp_list; 375e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prps->first_dma = prp_dma; 376e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews cmd->prp2 = cpu_to_le64(prp_dma); 377e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews i = 0; 378e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews for (;;) { 379e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews if (i == PAGE_SIZE / 8 - 1) { 380e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews __le64 *old_prp_list = prp_list; 38199802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma); 382e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prps->list[prp_page++] = prp_list; 383e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews old_prp_list[i] = cpu_to_le64(prp_dma); 384e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews i = 0; 385e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews } 386e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prp_list[i++] = cpu_to_le64(dma_addr); 387e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_len -= PAGE_SIZE; 388e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr += PAGE_SIZE; 389e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews length -= PAGE_SIZE; 390e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews if (length <= 0) 391e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews break; 392e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews if (dma_len > 0) 393e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews continue; 394e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews BUG_ON(dma_len < 0); 395e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews sg = sg_next(sg); 396e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr = sg_dma_address(sg); 397e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_len = sg_dma_len(sg); 398ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox } 399ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 400e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews return prps; 401ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox} 402ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 4031ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox/* NVMe scatterlists require no holes in the virtual address */ 4041ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \ 4051ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE)) 4061ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox 407d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstatic int nvme_map_bio(struct device *dev, struct nvme_bio *nbio, 408b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct bio *bio, enum dma_data_direction dma_dir, int psegs) 409b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 410768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox struct bio_vec *bvec, *bvprv = NULL; 411768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox struct scatterlist *sg = NULL; 4121ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox int i, old_idx, length = 0, nsegs = 0; 413b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 414768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox sg_init_table(nbio->sg, psegs); 4151ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox old_idx = bio->bi_idx; 416b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_for_each_segment(bvec, bio, i) { 417768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox if (bvprv && BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) { 418768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox sg->length += bvec->bv_len; 419768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox } else { 4201ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox if (bvprv && BIOVEC_NOT_VIRT_MERGEABLE(bvprv, bvec)) 4211ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox break; 422768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox sg = sg ? sg + 1 : nbio->sg; 423768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox sg_set_page(sg, bvec->bv_page, bvec->bv_len, 424768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox bvec->bv_offset); 425768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox nsegs++; 426768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox } 4271ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox length += bvec->bv_len; 428768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox bvprv = bvec; 429b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 4301ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox bio->bi_idx = i; 431d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox nbio->nents = nsegs; 432768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox sg_mark_end(sg); 4331ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox if (dma_map_sg(dev, nbio->sg, nbio->nents, dma_dir) == 0) { 4341ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox bio->bi_idx = old_idx; 4351ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox return -ENOMEM; 4361ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox } 4371ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox return length; 438b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 439b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 44000df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcoxstatic int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns, 44100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox int cmdid) 44200df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox{ 44300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; 44400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 44500df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox memset(cmnd, 0, sizeof(*cmnd)); 44600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox cmnd->common.opcode = nvme_cmd_flush; 44700df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox cmnd->common.command_id = cmdid; 44800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox cmnd->common.nsid = cpu_to_le32(ns->ns_id); 44900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 45000df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox if (++nvmeq->sq_tail == nvmeq->q_depth) 45100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox nvmeq->sq_tail = 0; 45200df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox writel(nvmeq->sq_tail, nvmeq->q_db); 45300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 45400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox return 0; 45500df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox} 45600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 45700df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcoxstatic int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns) 45800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox{ 45900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH, 46000df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox sync_completion_id, IO_TIMEOUT); 46100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox if (unlikely(cmdid < 0)) 46200df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox return cmdid; 46300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 46400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox return nvme_submit_flush(nvmeq, ns, cmdid); 46500df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox} 46600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 467b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns, 468b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct bio *bio) 469b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 470ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox struct nvme_command *cmnd; 471d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox struct nvme_bio *nbio; 472b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox enum dma_data_direction dma_dir; 4731ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox int cmdid, length, result = -ENOMEM; 474b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 control; 475b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 dsmgmt; 476b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int psegs = bio_phys_segments(ns->queue, bio); 477b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 47800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox if ((bio->bi_rw & REQ_FLUSH) && psegs) { 47900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox result = nvme_submit_flush_data(nvmeq, ns); 48000df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox if (result) 48100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox return result; 48200df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox } 48300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 484eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox nbio = alloc_nbio(psegs, GFP_ATOMIC); 485d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox if (!nbio) 486eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox goto nomem; 487d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox nbio->bio = bio; 488b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 489eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox result = -EBUSY; 490d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox cmdid = alloc_cmdid(nvmeq, nbio, bio_completion_id, IO_TIMEOUT); 491b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (unlikely(cmdid < 0)) 492d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox goto free_nbio; 493b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 49400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox if ((bio->bi_rw & REQ_FLUSH) && !psegs) 49500df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox return nvme_submit_flush(nvmeq, ns, cmdid); 49600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 497b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox control = 0; 498b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (bio->bi_rw & REQ_FUA) 499b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox control |= NVME_RW_FUA; 500b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD)) 501b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox control |= NVME_RW_LR; 502b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 503b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dsmgmt = 0; 504b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (bio->bi_rw & REQ_RAHEAD) 505b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH; 506b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 507ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; 508b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 509b8deb62cf271fa9381edc8cf52bcae2f0225c55aMatthew Wilcox memset(cmnd, 0, sizeof(*cmnd)); 510b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (bio_data_dir(bio)) { 511ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.opcode = nvme_cmd_write; 512b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_dir = DMA_TO_DEVICE; 513b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } else { 514ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.opcode = nvme_cmd_read; 515b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_dir = DMA_FROM_DEVICE; 516b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 517b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 5181ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox result = nvme_map_bio(nvmeq->q_dmadev, nbio, bio, dma_dir, psegs); 5191ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox if (result < 0) 520eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox goto free_nbio; 5211ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox length = result; 522b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 523ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.command_id = cmdid; 524ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.nsid = cpu_to_le32(ns->ns_id); 525d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox nbio->prps = nvme_setup_prps(nvmeq->dev, &cmnd->common, nbio->sg, 5261ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox length); 527ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.slba = cpu_to_le64(bio->bi_sector >> (ns->lba_shift - 9)); 5281ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox cmnd->rw.length = cpu_to_le16((length >> ns->lba_shift) - 1); 529ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.control = cpu_to_le16(control); 530ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt); 531b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 532d8ee9d69f275769aaad40ef7c944565ff8d2d24fMatthew Wilcox bio->bi_sector += length >> 9; 533d8ee9d69f275769aaad40ef7c944565ff8d2d24fMatthew Wilcox 534b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (++nvmeq->sq_tail == nvmeq->q_depth) 535b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_tail = 0; 5367547881d0951384f9833ec3a80fac8f3f16f3b98Matthew Wilcox writel(nvmeq->sq_tail, nvmeq->q_db); 537b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 5381974b1ae8852324a75fb8cfecbc7b758fd5a2c3cMatthew Wilcox return 0; 5391974b1ae8852324a75fb8cfecbc7b758fd5a2c3cMatthew Wilcox 540d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox free_nbio: 541d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox free_nbio(nvmeq, nbio); 542eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox nomem: 543eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox return result; 544b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 545b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 546b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 547b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * NB: return value of non-zero would mean that we were a stacking driver. 548b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * make_request must always succeed. 549b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 550b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_make_request(struct request_queue *q, struct bio *bio) 551b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 552b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_ns *ns = q->queuedata; 553b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq = get_nvmeq(ns); 554eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox int result = -EBUSY; 555eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox 556eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox spin_lock_irq(&nvmeq->q_lock); 557eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox if (bio_list_empty(&nvmeq->sq_cong)) 558eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox result = nvme_submit_bio_queue(nvmeq, ns, bio); 559eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox if (unlikely(result)) { 560eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox if (bio_list_empty(&nvmeq->sq_cong)) 561eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait); 562b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_list_add(&nvmeq->sq_cong, bio); 563b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 564eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox 565eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox spin_unlock_irq(&nvmeq->q_lock); 566b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox put_nvmeq(nvmeq); 567b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 568b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 569b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 570b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 571b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct sync_cmd_info { 572b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct task_struct *task; 573b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 result; 574b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 575b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 576b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 577b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void sync_completion(struct nvme_queue *nvmeq, void *ctx, 578b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_completion *cqe) 579b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 580b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct sync_cmd_info *cmdinfo = ctx; 581c42705592be2a539f3027b6f3907de8e8f9591a8Matthew Wilcox if (unlikely((unsigned long)cmdinfo == CMD_CTX_CANCELLED)) 582be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox return; 58300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox if ((unsigned long)cmdinfo == CMD_CTX_FLUSH) 58400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox return; 585b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox if (unlikely((unsigned long)cmdinfo == CMD_CTX_COMPLETED)) { 586b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox dev_warn(nvmeq->q_dmadev, 587b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox "completed id %d twice on queue %d\n", 588b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox cqe->command_id, le16_to_cpup(&cqe->sq_id)); 589b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox return; 590b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox } 59148e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox if (unlikely((unsigned long)cmdinfo == CMD_CTX_INVALID)) { 59248e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox dev_warn(nvmeq->q_dmadev, 59348e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox "invalid id %d completed on queue %d\n", 59448e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox cqe->command_id, le16_to_cpup(&cqe->sq_id)); 59548e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox return; 59648e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox } 597b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmdinfo->result = le32_to_cpup(&cqe->result); 598b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmdinfo->status = le16_to_cpup(&cqe->status) >> 1; 599b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox wake_up_process(cmdinfo->task); 600b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 601b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 602b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxtypedef void (*completion_fn)(struct nvme_queue *, void *, 603b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_completion *); 604b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 605b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic irqreturn_t nvme_process_cq(struct nvme_queue *nvmeq) 606b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 607821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox u16 head, phase; 608b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 609b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox static const completion_fn completions[4] = { 610b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox [sync_completion_id] = sync_completion, 611b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox [bio_completion_id] = bio_completion, 612b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox }; 613b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 614b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox head = nvmeq->cq_head; 615821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox phase = nvmeq->cq_phase; 616b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 617b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox for (;;) { 618b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long data; 619b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox void *ptr; 620b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned char handler; 621b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_completion cqe = nvmeq->cqes[head]; 622821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox if ((le16_to_cpu(cqe.status) & 1) != phase) 623b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox break; 624b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_head = le16_to_cpu(cqe.sq_head); 625b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (++head == nvmeq->q_depth) { 626b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox head = 0; 627821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox phase = !phase; 628b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 629b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 630b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox data = free_cmdid(nvmeq, cqe.command_id); 631b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox handler = data & 3; 632b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ptr = (void *)(data & ~3UL); 633b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox completions[handler](nvmeq, ptr, &cqe); 634b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 635b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 636b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox /* If the controller ignores the cq head doorbell and continuously 637b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * writes to the queue, it is theoretically possible to wrap around 638b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * the queue twice and mistakenly return IRQ_NONE. Linux only 639b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * requires that 0.1% of your interrupts are handled, so this isn't 640b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * a big problem. 641b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 642821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox if (head == nvmeq->cq_head && phase == nvmeq->cq_phase) 643b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return IRQ_NONE; 644b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 645b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writel(head, nvmeq->q_db + 1); 646b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cq_head = head; 647821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox nvmeq->cq_phase = phase; 648b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 649b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return IRQ_HANDLED; 650b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 651b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 652b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic irqreturn_t nvme_irq(int irq, void *data) 653b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 65458ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox irqreturn_t result; 65558ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox struct nvme_queue *nvmeq = data; 65658ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox spin_lock(&nvmeq->q_lock); 65758ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox result = nvme_process_cq(nvmeq); 65858ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox spin_unlock(&nvmeq->q_lock); 65958ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox return result; 66058ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox} 66158ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox 66258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxstatic irqreturn_t nvme_irq_check(int irq, void *data) 66358ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox{ 66458ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox struct nvme_queue *nvmeq = data; 66558ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head]; 66658ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase) 66758ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox return IRQ_NONE; 66858ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox return IRQ_WAKE_THREAD; 66958ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox} 67058ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox 6713c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcoxstatic void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid) 6723c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox{ 6733c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox spin_lock_irq(&nvmeq->q_lock); 674be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox cancel_cmdid_data(nvmeq, cmdid); 6753c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox spin_unlock_irq(&nvmeq->q_lock); 6763c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox} 6773c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox 678b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 679b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Returns 0 on success. If the result is negative, it's a Linux error code; 680b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * if the result is positive, it's an NVM Express status code 681b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 6823c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcoxstatic int nvme_submit_sync_cmd(struct nvme_queue *nvmeq, 683e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox struct nvme_command *cmd, u32 *result, unsigned timeout) 684b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 685b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int cmdid; 686b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct sync_cmd_info cmdinfo; 687b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 688b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmdinfo.task = current; 689b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmdinfo.status = -EINTR; 690b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 691e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox cmdid = alloc_cmdid_killable(nvmeq, &cmdinfo, sync_completion_id, 692e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox timeout); 693b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (cmdid < 0) 694b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return cmdid; 695b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmd->common.command_id = cmdid; 696b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 6973c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox set_current_state(TASK_KILLABLE); 6983c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox nvme_submit_cmd(nvmeq, cmd); 699b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox schedule(); 700b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 7013c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox if (cmdinfo.status == -EINTR) { 7023c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox nvme_abort_command(nvmeq, cmdid); 7033c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox return -EINTR; 7043c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox } 7053c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox 706b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result) 707b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox *result = cmdinfo.result; 708b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 709b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return cmdinfo.status; 710b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 711b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 712b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd, 713b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 *result) 714b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 715e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT); 716b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 717b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 718b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id) 719b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 720b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 721b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command c; 722b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 723b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&c, 0, sizeof(c)); 724b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.delete_queue.opcode = opcode; 725b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.delete_queue.qid = cpu_to_le16(id); 726b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 727b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox status = nvme_submit_admin_cmd(dev, &c, NULL); 728b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (status) 729b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EIO; 730b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 731b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 732b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 733b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_alloc_cq(struct nvme_dev *dev, u16 qid, 734b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq) 735b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 736b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 737b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command c; 738b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED; 739b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 740b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&c, 0, sizeof(c)); 741b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.opcode = nvme_admin_create_cq; 742b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr); 743b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.cqid = cpu_to_le16(qid); 744b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1); 745b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.cq_flags = cpu_to_le16(flags); 746b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector); 747b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 748b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox status = nvme_submit_admin_cmd(dev, &c, NULL); 749b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (status) 750b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EIO; 751b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 752b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 753b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 754b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_alloc_sq(struct nvme_dev *dev, u16 qid, 755b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq) 756b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 757b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 758b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command c; 759b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM; 760b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 761b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&c, 0, sizeof(c)); 762b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.opcode = nvme_admin_create_sq; 763b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr); 764b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.sqid = cpu_to_le16(qid); 765b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1); 766b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.sq_flags = cpu_to_le16(flags); 767b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.cqid = cpu_to_le16(qid); 768b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 769b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox status = nvme_submit_admin_cmd(dev, &c, NULL); 770b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (status) 771b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EIO; 772b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 773b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 774b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 775b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_cq(struct nvme_dev *dev, u16 cqid) 776b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 777b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid); 778b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 779b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 780b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_sq(struct nvme_dev *dev, u16 sqid) 781b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 782b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid); 783b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 784b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 785b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_free_queue(struct nvme_dev *dev, int qid) 786b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 787b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq = dev->queues[qid]; 788b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 789b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_irq(dev->entry[nvmeq->cq_vector].vector, nvmeq); 790b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 791b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox /* Don't tell the adapter to delete the admin queue */ 792b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (qid) { 793b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox adapter_delete_sq(dev, qid); 794b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox adapter_delete_cq(dev, qid); 795b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 796b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 797b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth), 798b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox (void *)nvmeq->cqes, nvmeq->cq_dma_addr); 799b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth), 800b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_cmds, nvmeq->sq_dma_addr); 801b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(nvmeq); 802b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 803b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 804b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid, 805b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int depth, int vector) 806b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 807b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct device *dmadev = &dev->pci_dev->dev; 808e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox unsigned extra = (depth / 8) + (depth * sizeof(struct nvme_cmd_info)); 809b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL); 810b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!nvmeq) 811b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 812b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 813b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth), 814b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox &nvmeq->cq_dma_addr, GFP_KERNEL); 815b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!nvmeq->cqes) 816b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free_nvmeq; 817b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth)); 818b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 819b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth), 820b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox &nvmeq->sq_dma_addr, GFP_KERNEL); 821b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!nvmeq->sq_cmds) 822b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free_cqdma; 823b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 824b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->q_dmadev = dmadev; 825091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox nvmeq->dev = dev; 826b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox spin_lock_init(&nvmeq->q_lock); 827b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cq_head = 0; 828821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox nvmeq->cq_phase = 1; 829b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox init_waitqueue_head(&nvmeq->sq_full); 8301fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread); 831b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_list_init(&nvmeq->sq_cong); 832b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->q_db = &dev->dbs[qid * 2]; 833b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->q_depth = depth; 834b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cq_vector = vector; 835b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 836b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return nvmeq; 837b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 838b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_cqdma: 839b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(dmadev, CQ_SIZE(nvmeq->q_depth), (void *)nvmeq->cqes, 840b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cq_dma_addr); 841b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_nvmeq: 842b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(nvmeq); 843b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 844b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 845b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 8463001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcoxstatic int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq, 8473001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox const char *name) 8483001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox{ 84958ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox if (use_threaded_interrupts) 85058ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector, 851ec6ce618d65b5ce1bef83a5509255107a0feac44Matthew Wilcox nvme_irq_check, nvme_irq, 85258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox IRQF_DISABLED | IRQF_SHARED, 85358ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox name, nvmeq); 8543001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq, 8553001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox IRQF_DISABLED | IRQF_SHARED, name, nvmeq); 8563001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox} 8573001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox 858b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic __devinit struct nvme_queue *nvme_create_queue(struct nvme_dev *dev, 859b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int qid, int cq_size, int vector) 860b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 861b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int result; 862b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq = nvme_alloc_queue(dev, qid, cq_size, vector); 863b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 8643f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox if (!nvmeq) 8653f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox return NULL; 8663f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox 867b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = adapter_alloc_cq(dev, qid, nvmeq); 868b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result < 0) 869b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free_nvmeq; 870b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 871b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = adapter_alloc_sq(dev, qid, nvmeq); 872b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result < 0) 873b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto release_cq; 874b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 8753001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox result = queue_request_irq(dev, nvmeq, "nvme"); 876b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result < 0) 877b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto release_sq; 878b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 879b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return nvmeq; 880b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 881b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox release_sq: 882b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox adapter_delete_sq(dev, qid); 883b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox release_cq: 884b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox adapter_delete_cq(dev, qid); 885b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_nvmeq: 886b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth), 887b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox (void *)nvmeq->cqes, nvmeq->cq_dma_addr); 888b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth), 889b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_cmds, nvmeq->sq_dma_addr); 890b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(nvmeq); 891b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 892b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 893b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 894b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_configure_admin_queue(struct nvme_dev *dev) 895b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 896b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int result; 897b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 aqa; 898b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq; 899b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 900b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->dbs = ((void __iomem *)dev->bar) + 4096; 901b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 902b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq = nvme_alloc_queue(dev, 0, 64, 0); 9033f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox if (!nvmeq) 9043f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox return -ENOMEM; 905b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 906b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox aqa = nvmeq->q_depth - 1; 907b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox aqa |= aqa << 16; 908b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 909b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM; 910b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT; 911b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE; 912b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9135911f20039ce59d7e7834f0c42151cf759b6f786Shane Michael Matthews writel(0, &dev->bar->cc); 914b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writel(aqa, &dev->bar->aqa); 915b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writeq(nvmeq->sq_dma_addr, &dev->bar->asq); 916b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writeq(nvmeq->cq_dma_addr, &dev->bar->acq); 917b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writel(dev->ctrl_config, &dev->bar->cc); 918b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 919b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox while (!(readl(&dev->bar->csts) & NVME_CSTS_RDY)) { 920b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox msleep(100); 921b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (fatal_signal_pending(current)) 922b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EINTR; 923b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 924b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9253001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox result = queue_request_irq(dev, nvmeq, "nvme admin"); 926b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->queues[0] = nvmeq; 927b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return result; 928b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 929b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9307fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcoxstatic int nvme_map_user_pages(struct nvme_dev *dev, int write, 9317fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox unsigned long addr, unsigned length, 9327fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox struct scatterlist **sgp) 933b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 93436c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox int i, err, count, nents, offset; 9357fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox struct scatterlist *sg; 9367fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox struct page **pages; 93736c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox 93836c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox if (addr & 3) 93936c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox return -EINVAL; 9407fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox if (!length) 9417fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox return -EINVAL; 9427fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox 94336c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox offset = offset_in_page(addr); 9447fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox count = DIV_ROUND_UP(offset + length, PAGE_SIZE); 9457fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox pages = kcalloc(count, sizeof(*pages), GFP_KERNEL); 94636c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox 94736c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox err = get_user_pages_fast(addr, count, 1, pages); 94836c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox if (err < count) { 94936c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox count = err; 95036c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox err = -EFAULT; 95136c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox goto put_pages; 95236c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox } 9537fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox 9547fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox sg = kcalloc(count, sizeof(*sg), GFP_KERNEL); 95536c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox sg_init_table(sg, count); 956ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox sg_set_page(&sg[0], pages[0], PAGE_SIZE - offset, offset); 9577fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox length -= (PAGE_SIZE - offset); 9587fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox for (i = 1; i < count; i++) { 9597fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox sg_set_page(&sg[i], pages[i], min_t(int, length, PAGE_SIZE), 0); 9607fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox length -= PAGE_SIZE; 9617fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox } 9627fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox 9637fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox err = -ENOMEM; 9647fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox nents = dma_map_sg(&dev->pci_dev->dev, sg, count, 9657fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox write ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 96636c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox if (!nents) 96736c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox goto put_pages; 968b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9697fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox kfree(pages); 9707fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox *sgp = sg; 9717fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox return nents; 972b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9737fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox put_pages: 9747fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox for (i = 0; i < count; i++) 9757fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox put_page(pages[i]); 9767fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox kfree(pages); 9777fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox return err; 9787fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox} 979b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9807fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcoxstatic void nvme_unmap_user_pages(struct nvme_dev *dev, int write, 9817fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox unsigned long addr, int length, 9827fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox struct scatterlist *sg, int nents) 9837fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox{ 9847fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox int i, count; 985b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9867fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox count = DIV_ROUND_UP(offset_in_page(addr) + length, PAGE_SIZE); 98736c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox dma_unmap_sg(&dev->pci_dev->dev, sg, nents, DMA_FROM_DEVICE); 9887fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox 98936c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox for (i = 0; i < count; i++) 9907fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox put_page(sg_page(&sg[i])); 9917fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox} 992b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9937fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcoxstatic int nvme_submit_user_admin_command(struct nvme_dev *dev, 9947fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox unsigned long addr, unsigned length, 9957fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox struct nvme_command *cmd) 9967fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox{ 9977fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox int err, nents; 9987fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox struct scatterlist *sg; 999e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews struct nvme_prps *prps; 10007fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox 10017fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox nents = nvme_map_user_pages(dev, 0, addr, length, &sg); 10027fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox if (nents < 0) 10037fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox return nents; 1004d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox prps = nvme_setup_prps(dev, &cmd->common, sg, length); 10057fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox err = nvme_submit_admin_cmd(dev, cmd, NULL); 10067fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox nvme_unmap_user_pages(dev, 0, addr, length, sg, nents); 1007d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox nvme_free_prps(dev, prps); 10087fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox return err ? -EIO : 0; 1009b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1010b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1011bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcoxstatic int nvme_identify(struct nvme_ns *ns, unsigned long addr, int cns) 1012b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1013b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command c; 1014b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1015bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox memset(&c, 0, sizeof(c)); 1016bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox c.identify.opcode = nvme_admin_identify; 1017bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox c.identify.nsid = cns ? 0 : cpu_to_le32(ns->ns_id); 1018bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox c.identify.cns = cpu_to_le32(cns); 1019bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox 1020bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox return nvme_submit_user_admin_command(ns->dev, addr, 4096, &c); 1021bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox} 1022bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox 1023bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcoxstatic int nvme_get_range_type(struct nvme_ns *ns, unsigned long addr) 1024bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox{ 1025bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox struct nvme_command c; 1026b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1027b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&c, 0, sizeof(c)); 1028b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.features.opcode = nvme_admin_get_features; 1029b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.features.nsid = cpu_to_le32(ns->ns_id); 1030b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.features.fid = cpu_to_le32(NVME_FEAT_LBA_RANGE); 1031b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1032bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox return nvme_submit_user_admin_command(ns->dev, addr, 4096, &c); 1033b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1034b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1035a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcoxstatic int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) 1036a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox{ 1037a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct nvme_dev *dev = ns->dev; 1038a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct nvme_queue *nvmeq; 1039a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct nvme_user_io io; 1040a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct nvme_command c; 1041a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox unsigned length; 1042a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox u32 result; 1043a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox int nents, status; 1044a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct scatterlist *sg; 1045e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews struct nvme_prps *prps; 1046a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 1047a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox if (copy_from_user(&io, uio, sizeof(io))) 1048a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox return -EFAULT; 1049a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox length = io.nblocks << io.block_shift; 1050a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox nents = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length, &sg); 1051a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox if (nents < 0) 1052a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox return nents; 1053a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 1054a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox memset(&c, 0, sizeof(c)); 1055a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.opcode = io.opcode; 1056a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.flags = io.flags; 1057a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.nsid = cpu_to_le32(io.nsid); 1058a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.slba = cpu_to_le64(io.slba); 1059a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.length = cpu_to_le16(io.nblocks - 1); 1060a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.control = cpu_to_le16(io.control); 1061a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.dsmgmt = cpu_to_le16(io.dsmgmt); 1062a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.reftag = cpu_to_le32(io.reftag); /* XXX: endian? */ 1063a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.apptag = cpu_to_le16(io.apptag); 1064a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.appmask = cpu_to_le16(io.appmask); 1065a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox /* XXX: metadata */ 1066d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox prps = nvme_setup_prps(dev, &c.common, sg, length); 1067a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 1068d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox nvmeq = get_nvmeq(ns); 1069b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox /* Since nvme_submit_sync_cmd sleeps, we can't keep preemption 1070b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox * disabled. We may be preempted at any point, and be rescheduled 1071b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox * to a different CPU. That will cause cacheline bouncing, but no 1072b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox * additional races since q_lock already protects against other CPUs. 1073b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox */ 1074a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox put_nvmeq(nvmeq); 1075e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox status = nvme_submit_sync_cmd(nvmeq, &c, &result, IO_TIMEOUT); 1076a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 1077a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox nvme_unmap_user_pages(dev, io.opcode & 1, io.addr, length, sg, nents); 1078d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox nvme_free_prps(dev, prps); 1079a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox put_user(result, &uio->result); 1080a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox return status; 1081a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox} 1082a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 10836ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcoxstatic int nvme_download_firmware(struct nvme_ns *ns, 10846ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox struct nvme_dlfw __user *udlfw) 10856ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox{ 10866ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox struct nvme_dev *dev = ns->dev; 10876ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox struct nvme_dlfw dlfw; 10886ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox struct nvme_command c; 10896ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox int nents, status; 10906ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox struct scatterlist *sg; 1091e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews struct nvme_prps *prps; 10926ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 10936ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox if (copy_from_user(&dlfw, udlfw, sizeof(dlfw))) 10946ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox return -EFAULT; 10956ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox if (dlfw.length >= (1 << 30)) 10966ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox return -EINVAL; 10976ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 10986ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox nents = nvme_map_user_pages(dev, 1, dlfw.addr, dlfw.length * 4, &sg); 10996ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox if (nents < 0) 11006ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox return nents; 11016ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 11026ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox memset(&c, 0, sizeof(c)); 11036ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox c.dlfw.opcode = nvme_admin_download_fw; 11046ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox c.dlfw.numd = cpu_to_le32(dlfw.length); 11056ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox c.dlfw.offset = cpu_to_le32(dlfw.offset); 1106d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox prps = nvme_setup_prps(dev, &c.common, sg, dlfw.length * 4); 11076ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 11086ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox status = nvme_submit_admin_cmd(dev, &c, NULL); 11096ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox nvme_unmap_user_pages(dev, 0, dlfw.addr, dlfw.length * 4, sg, nents); 1110d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox nvme_free_prps(dev, prps); 11116ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox return status; 11126ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox} 11136ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 11146ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcoxstatic int nvme_activate_firmware(struct nvme_ns *ns, unsigned long arg) 11156ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox{ 11166ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox struct nvme_dev *dev = ns->dev; 11176ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox struct nvme_command c; 11186ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 11196ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox memset(&c, 0, sizeof(c)); 11206ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox c.common.opcode = nvme_admin_activate_fw; 11216ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox c.common.rsvd10[0] = cpu_to_le32(arg); 11226ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 11236ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox return nvme_submit_admin_cmd(dev, &c, NULL); 11246ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox} 11256ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 1126b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, 1127b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long arg) 1128b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1129b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_ns *ns = bdev->bd_disk->private_data; 1130b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1131b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox switch (cmd) { 1132b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox case NVME_IOCTL_IDENTIFY_NS: 113336c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox return nvme_identify(ns, arg, 0); 1134b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox case NVME_IOCTL_IDENTIFY_CTRL: 113536c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox return nvme_identify(ns, arg, 1); 1136b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox case NVME_IOCTL_GET_RANGE_TYPE: 1137bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox return nvme_get_range_type(ns, arg); 1138a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox case NVME_IOCTL_SUBMIT_IO: 1139a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox return nvme_submit_io(ns, (void __user *)arg); 11406ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox case NVME_IOCTL_DOWNLOAD_FW: 11416ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox return nvme_download_firmware(ns, (void __user *)arg); 11426ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox case NVME_IOCTL_ACTIVATE_FW: 11436ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox return nvme_activate_firmware(ns, arg); 1144b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox default: 1145b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -ENOTTY; 1146b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1147b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1148b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1149b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic const struct block_device_operations nvme_fops = { 1150b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .owner = THIS_MODULE, 1151b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .ioctl = nvme_ioctl, 1152b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 1153b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 11541fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic void nvme_resubmit_bios(struct nvme_queue *nvmeq) 11551fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox{ 11561fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox while (bio_list_peek(&nvmeq->sq_cong)) { 11571fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox struct bio *bio = bio_list_pop(&nvmeq->sq_cong); 11581fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data; 11591fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox if (nvme_submit_bio_queue(nvmeq, ns, bio)) { 11601fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox bio_list_add_head(&nvmeq->sq_cong, bio); 11611fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox break; 11621fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox } 11631fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox } 11641fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox} 11651fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 11661fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic int nvme_kthread(void *data) 11671fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox{ 11681fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox struct nvme_dev *dev; 11691fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 11701fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox while (!kthread_should_stop()) { 11711fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox __set_current_state(TASK_RUNNING); 11721fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_lock(&dev_list_lock); 11731fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox list_for_each_entry(dev, &dev_list, node) { 11741fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox int i; 11751fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox for (i = 0; i < dev->queue_count; i++) { 11761fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox struct nvme_queue *nvmeq = dev->queues[i]; 1177740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox if (!nvmeq) 1178740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox continue; 11791fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_lock_irq(&nvmeq->q_lock); 11801fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox if (nvme_process_cq(nvmeq)) 11811fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox printk("process_cq did something\n"); 11821fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox nvme_resubmit_bios(nvmeq); 11831fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_unlock_irq(&nvmeq->q_lock); 11841fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox } 11851fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox } 11861fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_unlock(&dev_list_lock); 11871fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox set_current_state(TASK_INTERRUPTIBLE); 11881fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox schedule_timeout(HZ); 11891fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox } 11901fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox return 0; 11911fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox} 11921fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 1193b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, int index, 1194b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_id_ns *id, struct nvme_lba_range_type *rt) 1195b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1196b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_ns *ns; 1197b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct gendisk *disk; 1198b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int lbaf; 1199b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1200b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (rt->attributes & NVME_LBART_ATTRIB_HIDE) 1201b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 1202b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1203b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns = kzalloc(sizeof(*ns), GFP_KERNEL); 1204b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!ns) 1205b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 1206b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->queue = blk_alloc_queue(GFP_KERNEL); 1207b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!ns->queue) 1208b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto out_free_ns; 1209b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->queue->queue_flags = QUEUE_FLAG_DEFAULT | QUEUE_FLAG_NOMERGES | 1210b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox QUEUE_FLAG_NONROT | QUEUE_FLAG_DISCARD; 1211b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox blk_queue_make_request(ns->queue, nvme_make_request); 1212b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->dev = dev; 1213b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->queue->queuedata = ns; 1214b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1215b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk = alloc_disk(NVME_MINORS); 1216b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!disk) 1217b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto out_free_queue; 1218b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->ns_id = index; 1219b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->disk = disk; 1220b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox lbaf = id->flbas & 0xf; 1221b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->lba_shift = id->lbaf[lbaf].ds; 1222b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1223b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->major = nvme_major; 1224b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->minors = NVME_MINORS; 1225b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->first_minor = NVME_MINORS * index; 1226b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->fops = &nvme_fops; 1227b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->private_data = ns; 1228b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->queue = ns->queue; 1229388f037f4e7f0a24bac6b1a24f144f5d939f58cfMatthew Wilcox disk->driverfs_dev = &dev->pci_dev->dev; 1230b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox sprintf(disk->disk_name, "nvme%dn%d", dev->instance, index); 1231b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9)); 1232b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1233b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return ns; 1234b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1235b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free_queue: 1236b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox blk_cleanup_queue(ns->queue); 1237b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free_ns: 1238b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(ns); 1239b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 1240b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1241b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1242b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_ns_free(struct nvme_ns *ns) 1243b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1244b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox put_disk(ns->disk); 1245b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox blk_cleanup_queue(ns->queue); 1246b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(ns); 1247b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1248b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1249b3b06812e199f248561ce7824a4a8a9cd573c05aMatthew Wilcoxstatic int set_queue_count(struct nvme_dev *dev, int count) 1250b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1251b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 1252b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 result; 1253b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command c; 1254b3b06812e199f248561ce7824a4a8a9cd573c05aMatthew Wilcox u32 q_count = (count - 1) | ((count - 1) << 16); 1255b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1256b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&c, 0, sizeof(c)); 1257b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.features.opcode = nvme_admin_get_features; 1258b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.features.fid = cpu_to_le32(NVME_FEAT_NUM_QUEUES); 1259b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.features.dword11 = cpu_to_le32(q_count); 1260b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1261b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox status = nvme_submit_admin_cmd(dev, &c, &result); 1262b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (status) 1263b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EIO; 1264b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return min(result & 0xffff, result >> 16) + 1; 1265b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1266b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1267b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_setup_io_queues(struct nvme_dev *dev) 1268b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1269b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox int result, cpu, i, nr_io_queues; 1270b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1271b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox nr_io_queues = num_online_cpus(); 1272b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox result = set_queue_count(dev, nr_io_queues); 12731b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox if (result < 0) 12741b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox return result; 1275b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox if (result < nr_io_queues) 1276b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox nr_io_queues = result; 1277b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 12781b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox /* Deregister the admin queue's interrupt */ 12791b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox free_irq(dev->entry[0].vector, dev->queues[0]); 12801b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox 1281b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox for (i = 0; i < nr_io_queues; i++) 12821b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox dev->entry[i].entry = i; 12831b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox for (;;) { 1284b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox result = pci_enable_msix(dev->pci_dev, dev->entry, 1285b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox nr_io_queues); 12861b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox if (result == 0) { 12871b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox break; 12881b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } else if (result > 0) { 1289b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox nr_io_queues = result; 12901b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox continue; 12911b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } else { 1292b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox nr_io_queues = 1; 12931b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox break; 12941b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } 12951b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } 12961b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox 12971b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox result = queue_request_irq(dev, dev->queues[0], "nvme admin"); 12981b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox /* XXX: handle failure here */ 12991b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox 13001b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox cpu = cpumask_first(cpu_online_mask); 1301b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox for (i = 0; i < nr_io_queues; i++) { 13021b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu)); 13031b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox cpu = cpumask_next(cpu, cpu_online_mask); 13041b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } 13051b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox 1306b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox for (i = 0; i < nr_io_queues; i++) { 13071b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox dev->queues[i + 1] = nvme_create_queue(dev, i + 1, 13081b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox NVME_Q_DEPTH, i); 13091b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox if (!dev->queues[i + 1]) 13101b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox return -ENOMEM; 13111b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox dev->queue_count++; 13121b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } 1313b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1314b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 1315b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1316b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1317b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_free_queues(struct nvme_dev *dev) 1318b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1319b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int i; 1320b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1321b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox for (i = dev->queue_count - 1; i >= 0; i--) 1322b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_free_queue(dev, i); 1323b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1324b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1325b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_dev_add(struct nvme_dev *dev) 1326b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1327b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int res, nn, i; 1328b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_ns *ns, *next; 132951814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox struct nvme_id_ctrl *ctrl; 1330b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox void *id; 1331b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_addr_t dma_addr; 1332b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command cid, crt; 1333b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1334b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox res = nvme_setup_io_queues(dev); 1335b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (res) 1336b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return res; 1337b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1338b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox /* XXX: Switch to a SG list once prp2 works */ 1339b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox id = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr, 1340b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox GFP_KERNEL); 1341b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1342b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&cid, 0, sizeof(cid)); 1343b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cid.identify.opcode = nvme_admin_identify; 1344b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cid.identify.nsid = 0; 1345b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cid.identify.prp1 = cpu_to_le64(dma_addr); 1346b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cid.identify.cns = cpu_to_le32(1); 1347b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1348b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox res = nvme_submit_admin_cmd(dev, &cid, NULL); 1349b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (res) { 1350b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox res = -EIO; 1351b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto out_free; 1352b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1353b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 135451814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox ctrl = id; 135551814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox nn = le32_to_cpup(&ctrl->nn); 135651814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn)); 135751814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn)); 135851814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr)); 1359b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1360b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cid.identify.cns = 0; 1361b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&crt, 0, sizeof(crt)); 1362b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox crt.features.opcode = nvme_admin_get_features; 1363b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox crt.features.prp1 = cpu_to_le64(dma_addr + 4096); 1364b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox crt.features.fid = cpu_to_le32(NVME_FEAT_LBA_RANGE); 1365b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1366b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox for (i = 0; i < nn; i++) { 1367b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cid.identify.nsid = cpu_to_le32(i); 1368b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox res = nvme_submit_admin_cmd(dev, &cid, NULL); 1369b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (res) 1370b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox continue; 1371b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1372b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (((struct nvme_id_ns *)id)->ncap == 0) 1373b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox continue; 1374b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1375b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox crt.features.nsid = cpu_to_le32(i); 1376b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox res = nvme_submit_admin_cmd(dev, &crt, NULL); 1377b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (res) 1378b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox continue; 1379b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1380b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns = nvme_alloc_ns(dev, i, id, id + 4096); 1381b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (ns) 1382b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_add_tail(&ns->list, &dev->namespaces); 1383b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1384b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_for_each_entry(ns, &dev->namespaces, list) 1385b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox add_disk(ns->disk); 1386b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1387b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr); 1388b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 1389b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1390b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free: 1391b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_for_each_entry_safe(ns, next, &dev->namespaces, list) { 1392b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_del(&ns->list); 1393b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_ns_free(ns); 1394b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1395b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1396b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr); 1397b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return res; 1398b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1399b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1400b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_dev_remove(struct nvme_dev *dev) 1401b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1402b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_ns *ns, *next; 1403b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 14041fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_lock(&dev_list_lock); 14051fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox list_del(&dev->node); 14061fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_unlock(&dev_list_lock); 14071fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 1408b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox /* TODO: wait all I/O finished or cancel them */ 1409b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1410b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_for_each_entry_safe(ns, next, &dev->namespaces, list) { 1411b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_del(&ns->list); 1412b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox del_gendisk(ns->disk); 1413b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_ns_free(ns); 1414b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1415b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1416b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_free_queues(dev); 1417b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1418b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 1419b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1420b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1421091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcoxstatic int nvme_setup_prp_pools(struct nvme_dev *dev) 1422091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox{ 1423091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox struct device *dmadev = &dev->pci_dev->dev; 1424091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox dev->prp_page_pool = dma_pool_create("prp list page", dmadev, 1425091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox PAGE_SIZE, PAGE_SIZE, 0); 1426091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox if (!dev->prp_page_pool) 1427091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox return -ENOMEM; 1428091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox 142999802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox /* Optimisation for I/Os between 4k and 128k */ 143099802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox dev->prp_small_pool = dma_pool_create("prp list 256", dmadev, 143199802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox 256, 256, 0); 143299802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox if (!dev->prp_small_pool) { 143399802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox dma_pool_destroy(dev->prp_page_pool); 143499802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox return -ENOMEM; 143599802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox } 1436091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox return 0; 1437091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox} 1438091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox 1439091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcoxstatic void nvme_release_prp_pools(struct nvme_dev *dev) 1440091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox{ 1441091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox dma_pool_destroy(dev->prp_page_pool); 144299802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox dma_pool_destroy(dev->prp_small_pool); 1443091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox} 1444091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox 1445b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* XXX: Use an ida or something to let remove / add work correctly */ 1446b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_set_instance(struct nvme_dev *dev) 1447b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1448b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox static int instance; 1449b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->instance = instance++; 1450b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1451b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1452b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_release_instance(struct nvme_dev *dev) 1453b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1454b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1455b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1456b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_probe(struct pci_dev *pdev, 1457b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox const struct pci_device_id *id) 1458b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1459574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox int bars, result = -ENOMEM; 1460b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_dev *dev; 1461b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1462b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1463b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!dev) 1464b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -ENOMEM; 1465b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry), 1466b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox GFP_KERNEL); 1467b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!dev->entry) 1468b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free; 14691b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *), 14701b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox GFP_KERNEL); 1471b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!dev->queues) 1472b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free; 1473b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 14740ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews if (pci_enable_device_mem(pdev)) 14750ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews goto free; 1476f64d3365a3e5cb46e69db7e2c82a7cb9a5bed1b8Matthew Wilcox pci_set_master(pdev); 1477574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox bars = pci_select_bars(pdev, IORESOURCE_MEM); 1478574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox if (pci_request_selected_regions(pdev, bars, "nvme")) 1479574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox goto disable; 14800ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews 1481b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox INIT_LIST_HEAD(&dev->namespaces); 1482b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->pci_dev = pdev; 1483b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox pci_set_drvdata(pdev, dev); 14842930353f9f2b9e4629e935acd970cb73c1171229Matthew Wilcox dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)); 14852930353f9f2b9e4629e935acd970cb73c1171229Matthew Wilcox dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64)); 1486b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_set_instance(dev); 148753c9577e9ca68a633c6e9df2b54eaecacfa77f62Matthew Wilcox dev->entry[0].vector = pdev->irq; 1488b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1489091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox result = nvme_setup_prp_pools(dev); 1490091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox if (result) 1491091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox goto disable_msix; 1492091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox 1493b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->bar = ioremap(pci_resource_start(pdev, 0), 8192); 1494b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!dev->bar) { 1495b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = -ENOMEM; 1496574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox goto disable_msix; 1497b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1498b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1499b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = nvme_configure_admin_queue(dev); 1500b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result) 1501b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto unmap; 1502b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->queue_count++; 1503b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 15041fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_lock(&dev_list_lock); 15051fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox list_add(&dev->node, &dev_list); 15061fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_unlock(&dev_list_lock); 15071fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 1508740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox result = nvme_dev_add(dev); 1509740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox if (result) 1510740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox goto delete; 1511740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox 1512b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 1513b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1514b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox delete: 1515740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox spin_lock(&dev_list_lock); 1516740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox list_del(&dev->node); 1517740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox spin_unlock(&dev_list_lock); 1518740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox 1519b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_free_queues(dev); 1520b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unmap: 1521b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox iounmap(dev->bar); 1522574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox disable_msix: 1523b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox pci_disable_msix(pdev); 1524b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_release_instance(dev); 1525091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox nvme_release_prp_pools(dev); 1526574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox disable: 15270ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews pci_disable_device(pdev); 1528574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox pci_release_regions(pdev); 1529b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free: 1530b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev->queues); 1531b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev->entry); 1532b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev); 1533b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return result; 1534b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1535b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1536b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void __devexit nvme_remove(struct pci_dev *pdev) 1537b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1538b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_dev *dev = pci_get_drvdata(pdev); 1539b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_dev_remove(dev); 1540b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox pci_disable_msix(pdev); 1541b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox iounmap(dev->bar); 1542b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_release_instance(dev); 1543091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox nvme_release_prp_pools(dev); 15440ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews pci_disable_device(pdev); 1545574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox pci_release_regions(pdev); 1546b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev->queues); 1547b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev->entry); 1548b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev); 1549b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1550b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1551b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* These functions are yet to be implemented */ 1552b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_error_detected NULL 1553b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_dump_registers NULL 1554b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_link_reset NULL 1555b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_slot_reset NULL 1556b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_error_resume NULL 1557b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_suspend NULL 1558b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_resume NULL 1559b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1560b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct pci_error_handlers nvme_err_handler = { 1561b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .error_detected = nvme_error_detected, 1562b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .mmio_enabled = nvme_dump_registers, 1563b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .link_reset = nvme_link_reset, 1564b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .slot_reset = nvme_slot_reset, 1565b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .resume = nvme_error_resume, 1566b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 1567b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1568b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* Move to pci_ids.h later */ 1569b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define PCI_CLASS_STORAGE_EXPRESS 0x010802 1570b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1571b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = { 1572b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) }, 1573b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox { 0, } 1574b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 1575b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_DEVICE_TABLE(pci, nvme_id_table); 1576b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1577b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct pci_driver nvme_driver = { 1578b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .name = "nvme", 1579b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .id_table = nvme_id_table, 1580b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .probe = nvme_probe, 1581b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .remove = __devexit_p(nvme_remove), 1582b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .suspend = nvme_suspend, 1583b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .resume = nvme_resume, 1584b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .err_handler = &nvme_err_handler, 1585b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 1586b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1587b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __init nvme_init(void) 1588b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 15891fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox int result = -EBUSY; 15901fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 15911fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox nvme_thread = kthread_run(nvme_kthread, NULL, "nvme"); 15921fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox if (IS_ERR(nvme_thread)) 15931fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox return PTR_ERR(nvme_thread); 1594b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1595b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_major = register_blkdev(nvme_major, "nvme"); 1596b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (nvme_major <= 0) 15971fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox goto kill_kthread; 1598b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1599b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = pci_register_driver(&nvme_driver); 16001fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox if (result) 16011fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox goto unregister_blkdev; 16021fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox return 0; 1603b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 16041fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox unregister_blkdev: 1605b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unregister_blkdev(nvme_major, "nvme"); 16061fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox kill_kthread: 16071fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox kthread_stop(nvme_thread); 1608b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return result; 1609b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1610b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1611b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void __exit nvme_exit(void) 1612b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1613b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox pci_unregister_driver(&nvme_driver); 1614b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unregister_blkdev(nvme_major, "nvme"); 16151fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox kthread_stop(nvme_thread); 1616b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1617b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1618b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>"); 1619b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_LICENSE("GPL"); 1620ad8a5df97cb060aa4d817af25587c99e2d2fda97Matthew WilcoxMODULE_VERSION("0.3"); 1621b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_init(nvme_init); 1622b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_exit(nvme_exit); 1623