nvme-core.c revision 1974b1ae8852324a75fb8cfecbc7b758fd5a2c3c
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> 29b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/kernel.h> 30b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/mm.h> 31b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/module.h> 32b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/moduleparam.h> 33b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/pci.h> 34be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox#include <linux/poison.h> 35b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/sched.h> 36b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/slab.h> 37b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/types.h> 38b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/version.h> 39b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 40b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define NVME_Q_DEPTH 1024 41b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command)) 42b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion)) 43b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define NVME_MINORS 64 44e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox#define IO_TIMEOUT (5 * HZ) 45e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox#define ADMIN_TIMEOUT (60 * HZ) 46b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 47b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_major; 48b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_param(nvme_major, int, 0); 49b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 5058ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxstatic int use_threaded_interrupts; 5158ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxmodule_param(use_threaded_interrupts, int, 0); 5258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox 53b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 54b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Represents an NVM Express device. Each nvme_dev is a PCI function. 55b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 56b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct nvme_dev { 57b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue **queues; 58b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 __iomem *dbs; 59b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct pci_dev *pci_dev; 60091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox struct dma_pool *prp_page_pool; 6199802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox struct dma_pool *prp_small_pool; 62b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int instance; 63b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int queue_count; 64b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 ctrl_config; 65b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct msix_entry *entry; 66b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_bar __iomem *bar; 67b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct list_head namespaces; 6851814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox char serial[20]; 6951814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox char model[40]; 7051814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox char firmware_rev[8]; 71b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 72b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 73b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 74b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * An NVM Express namespace is equivalent to a SCSI LUN 75b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 76b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct nvme_ns { 77b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct list_head list; 78b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 79b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_dev *dev; 80b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct request_queue *queue; 81b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct gendisk *disk; 82b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 83b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int ns_id; 84b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int lba_shift; 85b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 86b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 87b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 88b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * An NVM Express queue. Each device has at least two (one for admin 89b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * commands and one for I/O commands). 90b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 91b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct nvme_queue { 92b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct device *q_dmadev; 93091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox struct nvme_dev *dev; 94b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox spinlock_t q_lock; 95b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command *sq_cmds; 96b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox volatile struct nvme_completion *cqes; 97b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_addr_t sq_dma_addr; 98b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_addr_t cq_dma_addr; 99b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox wait_queue_head_t sq_full; 100b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct bio_list sq_cong; 101b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 __iomem *q_db; 102b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 q_depth; 103b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 cq_vector; 104b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 sq_head; 105b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 sq_tail; 106b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 cq_head; 107821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox u16 cq_phase; 108b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long cmdid_data[]; 109b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 110b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1119294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcoxstatic void nvme_resubmit_bio(struct nvme_queue *nvmeq, struct bio *bio); 1129294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox 113b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 114b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Check we didin't inadvertently grow the command struct 115b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 116b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic inline void _nvme_check_size(void) 117b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 118b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64); 119b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64); 120b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64); 121b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64); 122b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_features) != 64); 123b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_command) != 64); 124b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096); 125b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096); 126b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64); 127b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 128b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 129e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstruct nvme_cmd_info { 130e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox unsigned long ctx; 131e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox unsigned long timeout; 132e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox}; 133e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox 134e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstatic struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq) 135e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox{ 136e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)]; 137e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox} 138e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox 139b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/** 140b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * alloc_cmdid - Allocate a Command ID 141b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @param nvmeq The queue that will be used for this command 142b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @param ctx A pointer that will be passed to the handler 143b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @param handler The ID of the handler to call 144b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * 145b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Allocate a Command ID for a queue. The data passed in will 146b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * be passed to the completion handler. This is implemented by using 147b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * the bottom two bits of the ctx pointer to store the handler ID. 148b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Passing in a pointer that's not 4-byte aligned will cause a BUG. 149b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * We can change this if it becomes a problem. 150b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 151e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstatic int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx, int handler, 152e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox unsigned timeout) 153b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 154b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int depth = nvmeq->q_depth; 155e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); 156b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int cmdid; 157b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 158b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUG_ON((unsigned long)ctx & 3); 159b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 160b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox do { 161b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth); 162b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (cmdid >= depth) 163b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EBUSY; 164b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } while (test_and_set_bit(cmdid, nvmeq->cmdid_data)); 165b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 166e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox info[cmdid].ctx = (unsigned long)ctx | handler; 167e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox info[cmdid].timeout = jiffies + timeout; 168b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return cmdid; 169b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 170b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 171b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx, 172e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox int handler, unsigned timeout) 173b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 174b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int cmdid; 175b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox wait_event_killable(nvmeq->sq_full, 176e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0); 177b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return (cmdid < 0) ? -EINTR : cmdid; 178b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 179b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 180b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* If you need more than four handlers, you'll need to change how 181be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox * alloc_cmdid and nvme_process_cq work. Consider using a special 182be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox * CMD_CTX value instead, if that works for your situation. 183b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 184b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxenum { 185b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox sync_completion_id = 0, 186b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_completion_id, 187b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 188b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 189be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox#define CMD_CTX_BASE (POISON_POINTER_DELTA + sync_completion_id) 190d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE) 191d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE) 192d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE) 193be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox 194b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic unsigned long free_cmdid(struct nvme_queue *nvmeq, int cmdid) 195b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 196b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long data; 197e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); 198b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 199e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox if (cmdid >= nvmeq->q_depth) 20048e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox return CMD_CTX_INVALID; 201e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox data = info[cmdid].ctx; 202e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox info[cmdid].ctx = CMD_CTX_COMPLETED; 203b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox clear_bit(cmdid, nvmeq->cmdid_data); 204b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox wake_up(&nvmeq->sq_full); 205b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return data; 206b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 207b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 208be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcoxstatic void cancel_cmdid_data(struct nvme_queue *nvmeq, int cmdid) 2093c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox{ 210e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); 211e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox info[cmdid].ctx = CMD_CTX_CANCELLED; 2123c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox} 2133c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox 214b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct nvme_queue *get_nvmeq(struct nvme_ns *ns) 215b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 2161b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox int qid, cpu = get_cpu(); 2171b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox if (cpu < ns->dev->queue_count) 2181b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox qid = cpu + 1; 2191b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox else 2201b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox qid = (cpu % rounddown_pow_of_two(ns->dev->queue_count)) + 1; 2211b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox return ns->dev->queues[qid]; 222b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 223b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 224b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void put_nvmeq(struct nvme_queue *nvmeq) 225b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 2261b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox put_cpu(); 227b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 228b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 229b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/** 230b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * nvme_submit_cmd: Copy a command into a queue and ring the doorbell 231b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @nvmeq: The queue to use 232b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @cmd: The command to send 233b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * 234b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Safe to use from interrupt context 235b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 236b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd) 237b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 238b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long flags; 239b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 tail; 240b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox /* XXX: Need to check tail isn't going to overrun head */ 241b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox spin_lock_irqsave(&nvmeq->q_lock, flags); 242b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox tail = nvmeq->sq_tail; 243b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd)); 244b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writel(tail, nvmeq->q_db); 245b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (++tail == nvmeq->q_depth) 246b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox tail = 0; 247b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_tail = tail; 248b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox spin_unlock_irqrestore(&nvmeq->q_lock, flags); 249b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 250b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 251b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 252b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 253e025344c56e08b155f43ea09647969286c78377cShane Michael Matthewsstruct nvme_prps { 254e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews int npages; 255e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr_t first_dma; 256e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews __le64 *list[0]; 257e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews}; 258e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews 259d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcoxstatic void nvme_free_prps(struct nvme_dev *dev, struct nvme_prps *prps) 260e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews{ 261e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews const int last_prp = PAGE_SIZE / 8 - 1; 262e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews int i; 263e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr_t prp_dma; 264e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews 265e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews if (!prps) 266e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews return; 267e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews 268e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prp_dma = prps->first_dma; 26999802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox 27099802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox if (prps->npages == 0) 27199802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox dma_pool_free(dev->prp_small_pool, prps->list[0], prp_dma); 272e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews for (i = 0; i < prps->npages; i++) { 273e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews __le64 *prp_list = prps->list[i]; 274e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]); 275091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox dma_pool_free(dev->prp_page_pool, prp_list, prp_dma); 276e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prp_dma = next_prp_dma; 277e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews } 278e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews kfree(prps); 279e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews} 280e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews 281d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstruct nvme_bio { 282b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct bio *bio; 283b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int nents; 284e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews struct nvme_prps *prps; 285b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct scatterlist sg[0]; 286b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 287b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 288b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* XXX: use a mempool */ 289d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstatic struct nvme_bio *alloc_nbio(unsigned nseg, gfp_t gfp) 290b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 291d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox return kzalloc(sizeof(struct nvme_bio) + 292b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox sizeof(struct scatterlist) * nseg, gfp); 293b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 294b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 295d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstatic void free_nbio(struct nvme_queue *nvmeq, struct nvme_bio *nbio) 296b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 297d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox nvme_free_prps(nvmeq->dev, nbio->prps); 298d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox kfree(nbio); 299b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 300b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 301b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void bio_completion(struct nvme_queue *nvmeq, void *ctx, 302b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_completion *cqe) 303b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 304d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox struct nvme_bio *nbio = ctx; 305d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox struct bio *bio = nbio->bio; 306b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 status = le16_to_cpup(&cqe->status) >> 1; 307b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 308d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox dma_unmap_sg(nvmeq->q_dmadev, nbio->sg, nbio->nents, 309b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 310d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox free_nbio(nvmeq, nbio); 311b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_endio(bio, status ? -EIO : 0); 3129294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox bio = bio_list_pop(&nvmeq->sq_cong); 3139294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox if (bio) 3149294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox nvme_resubmit_bio(nvmeq, bio); 315b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 316b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 317ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox/* length is in bytes */ 318d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcoxstatic struct nvme_prps *nvme_setup_prps(struct nvme_dev *dev, 319e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews struct nvme_common_command *cmd, 320ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox struct scatterlist *sg, int length) 321ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox{ 32299802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox struct dma_pool *pool; 323ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox int dma_len = sg_dma_len(sg); 324ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox u64 dma_addr = sg_dma_address(sg); 325ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox int offset = offset_in_page(dma_addr); 326e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews __le64 *prp_list; 327e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr_t prp_dma; 328e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews int nprps, npages, i, prp_page; 329e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews struct nvme_prps *prps = NULL; 330ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 331ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmd->prp1 = cpu_to_le64(dma_addr); 332ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox length -= (PAGE_SIZE - offset); 333ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox if (length <= 0) 334e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews return prps; 335ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 336ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox dma_len -= (PAGE_SIZE - offset); 337ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox if (dma_len) { 338ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox dma_addr += (PAGE_SIZE - offset); 339ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox } else { 340ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox sg = sg_next(sg); 341ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox dma_addr = sg_dma_address(sg); 342ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox dma_len = sg_dma_len(sg); 343ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox } 344ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 345ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox if (length <= PAGE_SIZE) { 346ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmd->prp2 = cpu_to_le64(dma_addr); 347e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews return prps; 348e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews } 349e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews 350e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews nprps = DIV_ROUND_UP(length, PAGE_SIZE); 351e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews npages = DIV_ROUND_UP(8 * nprps, PAGE_SIZE); 352e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prps = kmalloc(sizeof(*prps) + sizeof(__le64 *) * npages, GFP_ATOMIC); 353e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prp_page = 0; 35499802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox if (nprps <= (256 / 8)) { 35599802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox pool = dev->prp_small_pool; 35699802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox prps->npages = 0; 35799802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox } else { 35899802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox pool = dev->prp_page_pool; 35999802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox prps->npages = npages; 36099802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox } 36199802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox 36299802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma); 363e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prps->list[prp_page++] = prp_list; 364e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prps->first_dma = prp_dma; 365e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews cmd->prp2 = cpu_to_le64(prp_dma); 366e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews i = 0; 367e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews for (;;) { 368e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews if (i == PAGE_SIZE / 8 - 1) { 369e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews __le64 *old_prp_list = prp_list; 37099802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma); 371e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prps->list[prp_page++] = prp_list; 372e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews old_prp_list[i] = cpu_to_le64(prp_dma); 373e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews i = 0; 374e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews } 375e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prp_list[i++] = cpu_to_le64(dma_addr); 376e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_len -= PAGE_SIZE; 377e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr += PAGE_SIZE; 378e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews length -= PAGE_SIZE; 379e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews if (length <= 0) 380e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews break; 381e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews if (dma_len > 0) 382e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews continue; 383e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews BUG_ON(dma_len < 0); 384e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews sg = sg_next(sg); 385e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr = sg_dma_address(sg); 386e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_len = sg_dma_len(sg); 387ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox } 388ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 389e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews return prps; 390ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox} 391ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 392d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstatic int nvme_map_bio(struct device *dev, struct nvme_bio *nbio, 393b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct bio *bio, enum dma_data_direction dma_dir, int psegs) 394b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 395b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct bio_vec *bvec; 396d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox struct scatterlist *sg = nbio->sg; 397b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int i, nsegs; 398b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 399b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox sg_init_table(sg, psegs); 400b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_for_each_segment(bvec, bio, i) { 401b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox sg_set_page(sg, bvec->bv_page, bvec->bv_len, bvec->bv_offset); 40251882d00f07da9601cc962a3596e48aafb4f4163Matthew Wilcox sg++; 403b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox /* XXX: handle non-mergable here */ 404b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nsegs++; 405b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 406d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox nbio->nents = nsegs; 407b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 408d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox return dma_map_sg(dev, nbio->sg, nbio->nents, dma_dir); 409b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 410b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 411b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns, 412b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct bio *bio) 413b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 414ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox struct nvme_command *cmnd; 415d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox struct nvme_bio *nbio; 416b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox enum dma_data_direction dma_dir; 417b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int cmdid; 418b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 control; 419b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 dsmgmt; 420b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long flags; 421b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int psegs = bio_phys_segments(ns->queue, bio); 422b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 423d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox nbio = alloc_nbio(psegs, GFP_NOIO); 424d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox if (!nbio) 425b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto congestion; 426d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox nbio->bio = bio; 427b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 428d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox cmdid = alloc_cmdid(nvmeq, nbio, bio_completion_id, IO_TIMEOUT); 429b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (unlikely(cmdid < 0)) 430d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox goto free_nbio; 431b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 432b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox control = 0; 433b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (bio->bi_rw & REQ_FUA) 434b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox control |= NVME_RW_FUA; 435b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD)) 436b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox control |= NVME_RW_LR; 437b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 438b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dsmgmt = 0; 439b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (bio->bi_rw & REQ_RAHEAD) 440b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH; 441b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 442b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox spin_lock_irqsave(&nvmeq->q_lock, flags); 443ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; 444b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 445b8deb62cf271fa9381edc8cf52bcae2f0225c55aMatthew Wilcox memset(cmnd, 0, sizeof(*cmnd)); 446b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (bio_data_dir(bio)) { 447ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.opcode = nvme_cmd_write; 448b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_dir = DMA_TO_DEVICE; 449b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } else { 450ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.opcode = nvme_cmd_read; 451b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_dir = DMA_FROM_DEVICE; 452b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 453b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 4541974b1ae8852324a75fb8cfecbc7b758fd5a2c3cMatthew Wilcox if (nvme_map_bio(nvmeq->q_dmadev, nbio, bio, dma_dir, psegs) == 0) 4551974b1ae8852324a75fb8cfecbc7b758fd5a2c3cMatthew Wilcox goto mapping_failed; 456b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 457ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.flags = 1; 458ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.command_id = cmdid; 459ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.nsid = cpu_to_le32(ns->ns_id); 460d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox nbio->prps = nvme_setup_prps(nvmeq->dev, &cmnd->common, nbio->sg, 461e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews bio->bi_size); 462ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.slba = cpu_to_le64(bio->bi_sector >> (ns->lba_shift - 9)); 463ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.length = cpu_to_le16((bio->bi_size >> ns->lba_shift) - 1); 464ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.control = cpu_to_le16(control); 465ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt); 466b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 467b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writel(nvmeq->sq_tail, nvmeq->q_db); 468b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (++nvmeq->sq_tail == nvmeq->q_depth) 469b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_tail = 0; 470b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 471b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox spin_unlock_irqrestore(&nvmeq->q_lock, flags); 472b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 473b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 474b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 4751974b1ae8852324a75fb8cfecbc7b758fd5a2c3cMatthew Wilcox mapping_failed: 4761974b1ae8852324a75fb8cfecbc7b758fd5a2c3cMatthew Wilcox free_nbio(nvmeq, nbio); 4771974b1ae8852324a75fb8cfecbc7b758fd5a2c3cMatthew Wilcox bio_endio(bio, -ENOMEM); 4781974b1ae8852324a75fb8cfecbc7b758fd5a2c3cMatthew Wilcox return 0; 4791974b1ae8852324a75fb8cfecbc7b758fd5a2c3cMatthew Wilcox 480d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox free_nbio: 481d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox free_nbio(nvmeq, nbio); 482b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox congestion: 483b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EBUSY; 484b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 485b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 4869294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcoxstatic void nvme_resubmit_bio(struct nvme_queue *nvmeq, struct bio *bio) 4879294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox{ 4889294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data; 4899294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox if (nvme_submit_bio_queue(nvmeq, ns, bio)) 4909294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox bio_list_add_head(&nvmeq->sq_cong, bio); 4919294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox else if (bio_list_empty(&nvmeq->sq_cong)) 4929294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox blk_clear_queue_congested(ns->queue, rw_is_sync(bio->bi_rw)); 4939294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox /* XXX: Need to duplicate the logic from __freed_request here */ 4949294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox} 4959294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox 496b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 497b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * NB: return value of non-zero would mean that we were a stacking driver. 498b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * make_request must always succeed. 499b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 500b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_make_request(struct request_queue *q, struct bio *bio) 501b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 502b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_ns *ns = q->queuedata; 503b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq = get_nvmeq(ns); 504b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 505b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (nvme_submit_bio_queue(nvmeq, ns, bio)) { 506b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox blk_set_queue_congested(q, rw_is_sync(bio->bi_rw)); 5079294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox spin_lock_irq(&nvmeq->q_lock); 508b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_list_add(&nvmeq->sq_cong, bio); 5099294bbed78926a895516ec016ba23033f58d1a88Matthew Wilcox spin_unlock_irq(&nvmeq->q_lock); 510b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 511b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox put_nvmeq(nvmeq); 512b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 513b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 514b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 515b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 516b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct sync_cmd_info { 517b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct task_struct *task; 518b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 result; 519b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 520b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 521b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 522b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void sync_completion(struct nvme_queue *nvmeq, void *ctx, 523b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_completion *cqe) 524b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 525b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct sync_cmd_info *cmdinfo = ctx; 526be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox if ((unsigned long)cmdinfo == CMD_CTX_CANCELLED) 527be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox return; 528b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox if (unlikely((unsigned long)cmdinfo == CMD_CTX_COMPLETED)) { 529b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox dev_warn(nvmeq->q_dmadev, 530b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox "completed id %d twice on queue %d\n", 531b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox cqe->command_id, le16_to_cpup(&cqe->sq_id)); 532b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox return; 533b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox } 53448e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox if (unlikely((unsigned long)cmdinfo == CMD_CTX_INVALID)) { 53548e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox dev_warn(nvmeq->q_dmadev, 53648e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox "invalid id %d completed on queue %d\n", 53748e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox cqe->command_id, le16_to_cpup(&cqe->sq_id)); 53848e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox return; 53948e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox } 540b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmdinfo->result = le32_to_cpup(&cqe->result); 541b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmdinfo->status = le16_to_cpup(&cqe->status) >> 1; 542b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox wake_up_process(cmdinfo->task); 543b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 544b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 545b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxtypedef void (*completion_fn)(struct nvme_queue *, void *, 546b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_completion *); 547b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 548b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic irqreturn_t nvme_process_cq(struct nvme_queue *nvmeq) 549b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 550821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox u16 head, phase; 551b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 552b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox static const completion_fn completions[4] = { 553b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox [sync_completion_id] = sync_completion, 554b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox [bio_completion_id] = bio_completion, 555b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox }; 556b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 557b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox head = nvmeq->cq_head; 558821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox phase = nvmeq->cq_phase; 559b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 560b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox for (;;) { 561b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long data; 562b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox void *ptr; 563b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned char handler; 564b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_completion cqe = nvmeq->cqes[head]; 565821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox if ((le16_to_cpu(cqe.status) & 1) != phase) 566b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox break; 567b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_head = le16_to_cpu(cqe.sq_head); 568b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (++head == nvmeq->q_depth) { 569b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox head = 0; 570821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox phase = !phase; 571b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 572b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 573b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox data = free_cmdid(nvmeq, cqe.command_id); 574b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox handler = data & 3; 575b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ptr = (void *)(data & ~3UL); 576b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox completions[handler](nvmeq, ptr, &cqe); 577b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 578b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 579b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox /* If the controller ignores the cq head doorbell and continuously 580b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * writes to the queue, it is theoretically possible to wrap around 581b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * the queue twice and mistakenly return IRQ_NONE. Linux only 582b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * requires that 0.1% of your interrupts are handled, so this isn't 583b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * a big problem. 584b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 585821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox if (head == nvmeq->cq_head && phase == nvmeq->cq_phase) 586b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return IRQ_NONE; 587b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 588b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writel(head, nvmeq->q_db + 1); 589b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cq_head = head; 590821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox nvmeq->cq_phase = phase; 591b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 592b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return IRQ_HANDLED; 593b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 594b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 595b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic irqreturn_t nvme_irq(int irq, void *data) 596b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 59758ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox irqreturn_t result; 59858ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox struct nvme_queue *nvmeq = data; 59958ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox spin_lock(&nvmeq->q_lock); 60058ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox result = nvme_process_cq(nvmeq); 60158ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox spin_unlock(&nvmeq->q_lock); 60258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox return result; 60358ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox} 60458ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox 60558ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxstatic irqreturn_t nvme_irq_check(int irq, void *data) 60658ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox{ 60758ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox struct nvme_queue *nvmeq = data; 60858ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head]; 60958ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase) 61058ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox return IRQ_NONE; 61158ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox return IRQ_WAKE_THREAD; 61258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox} 61358ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox 6143c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcoxstatic void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid) 6153c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox{ 6163c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox spin_lock_irq(&nvmeq->q_lock); 617be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox cancel_cmdid_data(nvmeq, cmdid); 6183c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox spin_unlock_irq(&nvmeq->q_lock); 6193c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox} 6203c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox 621b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 622b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Returns 0 on success. If the result is negative, it's a Linux error code; 623b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * if the result is positive, it's an NVM Express status code 624b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 6253c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcoxstatic int nvme_submit_sync_cmd(struct nvme_queue *nvmeq, 626e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox struct nvme_command *cmd, u32 *result, unsigned timeout) 627b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 628b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int cmdid; 629b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct sync_cmd_info cmdinfo; 630b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 631b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmdinfo.task = current; 632b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmdinfo.status = -EINTR; 633b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 634e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox cmdid = alloc_cmdid_killable(nvmeq, &cmdinfo, sync_completion_id, 635e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox timeout); 636b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (cmdid < 0) 637b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return cmdid; 638b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmd->common.command_id = cmdid; 639b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 6403c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox set_current_state(TASK_KILLABLE); 6413c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox nvme_submit_cmd(nvmeq, cmd); 642b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox schedule(); 643b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 6443c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox if (cmdinfo.status == -EINTR) { 6453c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox nvme_abort_command(nvmeq, cmdid); 6463c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox return -EINTR; 6473c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox } 6483c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox 649b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result) 650b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox *result = cmdinfo.result; 651b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 652b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return cmdinfo.status; 653b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 654b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 655b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd, 656b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 *result) 657b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 658e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT); 659b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 660b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 661b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id) 662b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 663b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 664b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command c; 665b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 666b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&c, 0, sizeof(c)); 667b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.delete_queue.opcode = opcode; 668b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.delete_queue.qid = cpu_to_le16(id); 669b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 670b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox status = nvme_submit_admin_cmd(dev, &c, NULL); 671b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (status) 672b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EIO; 673b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 674b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 675b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 676b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_alloc_cq(struct nvme_dev *dev, u16 qid, 677b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq) 678b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 679b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 680b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command c; 681b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED; 682b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 683b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&c, 0, sizeof(c)); 684b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.opcode = nvme_admin_create_cq; 685b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr); 686b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.cqid = cpu_to_le16(qid); 687b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1); 688b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.cq_flags = cpu_to_le16(flags); 689b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector); 690b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 691b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox status = nvme_submit_admin_cmd(dev, &c, NULL); 692b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (status) 693b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EIO; 694b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 695b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 696b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 697b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_alloc_sq(struct nvme_dev *dev, u16 qid, 698b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq) 699b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 700b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 701b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command c; 702b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM; 703b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 704b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&c, 0, sizeof(c)); 705b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.opcode = nvme_admin_create_sq; 706b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr); 707b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.sqid = cpu_to_le16(qid); 708b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1); 709b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.sq_flags = cpu_to_le16(flags); 710b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.cqid = cpu_to_le16(qid); 711b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 712b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox status = nvme_submit_admin_cmd(dev, &c, NULL); 713b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (status) 714b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EIO; 715b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 716b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 717b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 718b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_cq(struct nvme_dev *dev, u16 cqid) 719b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 720b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid); 721b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 722b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 723b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_sq(struct nvme_dev *dev, u16 sqid) 724b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 725b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid); 726b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 727b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 728b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_free_queue(struct nvme_dev *dev, int qid) 729b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 730b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq = dev->queues[qid]; 731b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 732b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_irq(dev->entry[nvmeq->cq_vector].vector, nvmeq); 733b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 734b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox /* Don't tell the adapter to delete the admin queue */ 735b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (qid) { 736b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox adapter_delete_sq(dev, qid); 737b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox adapter_delete_cq(dev, qid); 738b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 739b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 740b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth), 741b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox (void *)nvmeq->cqes, nvmeq->cq_dma_addr); 742b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth), 743b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_cmds, nvmeq->sq_dma_addr); 744b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(nvmeq); 745b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 746b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 747b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid, 748b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int depth, int vector) 749b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 750b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct device *dmadev = &dev->pci_dev->dev; 751e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox unsigned extra = (depth / 8) + (depth * sizeof(struct nvme_cmd_info)); 752b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL); 753b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!nvmeq) 754b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 755b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 756b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth), 757b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox &nvmeq->cq_dma_addr, GFP_KERNEL); 758b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!nvmeq->cqes) 759b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free_nvmeq; 760b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth)); 761b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 762b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth), 763b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox &nvmeq->sq_dma_addr, GFP_KERNEL); 764b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!nvmeq->sq_cmds) 765b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free_cqdma; 766b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 767b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->q_dmadev = dmadev; 768091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox nvmeq->dev = dev; 769b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox spin_lock_init(&nvmeq->q_lock); 770b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cq_head = 0; 771821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox nvmeq->cq_phase = 1; 772b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox init_waitqueue_head(&nvmeq->sq_full); 773b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_list_init(&nvmeq->sq_cong); 774b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->q_db = &dev->dbs[qid * 2]; 775b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->q_depth = depth; 776b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cq_vector = vector; 777b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 778b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return nvmeq; 779b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 780b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_cqdma: 781b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(dmadev, CQ_SIZE(nvmeq->q_depth), (void *)nvmeq->cqes, 782b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cq_dma_addr); 783b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_nvmeq: 784b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(nvmeq); 785b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 786b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 787b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 7883001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcoxstatic int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq, 7893001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox const char *name) 7903001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox{ 79158ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox if (use_threaded_interrupts) 79258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector, 793ec6ce618d65b5ce1bef83a5509255107a0feac44Matthew Wilcox nvme_irq_check, nvme_irq, 79458ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox IRQF_DISABLED | IRQF_SHARED, 79558ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox name, nvmeq); 7963001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq, 7973001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox IRQF_DISABLED | IRQF_SHARED, name, nvmeq); 7983001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox} 7993001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox 800b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic __devinit struct nvme_queue *nvme_create_queue(struct nvme_dev *dev, 801b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int qid, int cq_size, int vector) 802b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 803b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int result; 804b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq = nvme_alloc_queue(dev, qid, cq_size, vector); 805b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 8063f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox if (!nvmeq) 8073f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox return NULL; 8083f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox 809b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = adapter_alloc_cq(dev, qid, nvmeq); 810b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result < 0) 811b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free_nvmeq; 812b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 813b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = adapter_alloc_sq(dev, qid, nvmeq); 814b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result < 0) 815b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto release_cq; 816b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 8173001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox result = queue_request_irq(dev, nvmeq, "nvme"); 818b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result < 0) 819b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto release_sq; 820b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 821b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return nvmeq; 822b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 823b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox release_sq: 824b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox adapter_delete_sq(dev, qid); 825b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox release_cq: 826b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox adapter_delete_cq(dev, qid); 827b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_nvmeq: 828b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth), 829b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox (void *)nvmeq->cqes, nvmeq->cq_dma_addr); 830b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth), 831b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_cmds, nvmeq->sq_dma_addr); 832b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(nvmeq); 833b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 834b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 835b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 836b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_configure_admin_queue(struct nvme_dev *dev) 837b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 838b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int result; 839b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 aqa; 840b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq; 841b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 842b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->dbs = ((void __iomem *)dev->bar) + 4096; 843b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 844b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq = nvme_alloc_queue(dev, 0, 64, 0); 8453f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox if (!nvmeq) 8463f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox return -ENOMEM; 847b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 848b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox aqa = nvmeq->q_depth - 1; 849b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox aqa |= aqa << 16; 850b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 851b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM; 852b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT; 853b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE; 854b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 8555911f20039ce59d7e7834f0c42151cf759b6f786Shane Michael Matthews writel(0, &dev->bar->cc); 856b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writel(aqa, &dev->bar->aqa); 857b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writeq(nvmeq->sq_dma_addr, &dev->bar->asq); 858b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writeq(nvmeq->cq_dma_addr, &dev->bar->acq); 859b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writel(dev->ctrl_config, &dev->bar->cc); 860b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 861b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox while (!(readl(&dev->bar->csts) & NVME_CSTS_RDY)) { 862b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox msleep(100); 863b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (fatal_signal_pending(current)) 864b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EINTR; 865b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 866b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 8673001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox result = queue_request_irq(dev, nvmeq, "nvme admin"); 868b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->queues[0] = nvmeq; 869b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return result; 870b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 871b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 8727fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcoxstatic int nvme_map_user_pages(struct nvme_dev *dev, int write, 8737fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox unsigned long addr, unsigned length, 8747fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox struct scatterlist **sgp) 875b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 87636c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox int i, err, count, nents, offset; 8777fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox struct scatterlist *sg; 8787fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox struct page **pages; 87936c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox 88036c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox if (addr & 3) 88136c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox return -EINVAL; 8827fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox if (!length) 8837fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox return -EINVAL; 8847fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox 88536c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox offset = offset_in_page(addr); 8867fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox count = DIV_ROUND_UP(offset + length, PAGE_SIZE); 8877fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox pages = kcalloc(count, sizeof(*pages), GFP_KERNEL); 88836c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox 88936c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox err = get_user_pages_fast(addr, count, 1, pages); 89036c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox if (err < count) { 89136c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox count = err; 89236c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox err = -EFAULT; 89336c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox goto put_pages; 89436c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox } 8957fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox 8967fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox sg = kcalloc(count, sizeof(*sg), GFP_KERNEL); 89736c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox sg_init_table(sg, count); 898ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox sg_set_page(&sg[0], pages[0], PAGE_SIZE - offset, offset); 8997fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox length -= (PAGE_SIZE - offset); 9007fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox for (i = 1; i < count; i++) { 9017fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox sg_set_page(&sg[i], pages[i], min_t(int, length, PAGE_SIZE), 0); 9027fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox length -= PAGE_SIZE; 9037fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox } 9047fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox 9057fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox err = -ENOMEM; 9067fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox nents = dma_map_sg(&dev->pci_dev->dev, sg, count, 9077fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox write ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 90836c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox if (!nents) 90936c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox goto put_pages; 910b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9117fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox kfree(pages); 9127fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox *sgp = sg; 9137fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox return nents; 914b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9157fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox put_pages: 9167fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox for (i = 0; i < count; i++) 9177fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox put_page(pages[i]); 9187fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox kfree(pages); 9197fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox return err; 9207fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox} 921b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9227fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcoxstatic void nvme_unmap_user_pages(struct nvme_dev *dev, int write, 9237fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox unsigned long addr, int length, 9247fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox struct scatterlist *sg, int nents) 9257fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox{ 9267fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox int i, count; 927b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9287fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox count = DIV_ROUND_UP(offset_in_page(addr) + length, PAGE_SIZE); 92936c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox dma_unmap_sg(&dev->pci_dev->dev, sg, nents, DMA_FROM_DEVICE); 9307fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox 93136c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox for (i = 0; i < count; i++) 9327fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox put_page(sg_page(&sg[i])); 9337fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox} 934b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9357fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcoxstatic int nvme_submit_user_admin_command(struct nvme_dev *dev, 9367fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox unsigned long addr, unsigned length, 9377fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox struct nvme_command *cmd) 9387fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox{ 9397fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox int err, nents; 9407fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox struct scatterlist *sg; 941e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews struct nvme_prps *prps; 9427fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox 9437fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox nents = nvme_map_user_pages(dev, 0, addr, length, &sg); 9447fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox if (nents < 0) 9457fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox return nents; 946d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox prps = nvme_setup_prps(dev, &cmd->common, sg, length); 9477fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox err = nvme_submit_admin_cmd(dev, cmd, NULL); 9487fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox nvme_unmap_user_pages(dev, 0, addr, length, sg, nents); 949d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox nvme_free_prps(dev, prps); 9507fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox return err ? -EIO : 0; 951b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 952b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 953bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcoxstatic int nvme_identify(struct nvme_ns *ns, unsigned long addr, int cns) 954b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 955b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command c; 956b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 957bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox memset(&c, 0, sizeof(c)); 958bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox c.identify.opcode = nvme_admin_identify; 959bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox c.identify.nsid = cns ? 0 : cpu_to_le32(ns->ns_id); 960bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox c.identify.cns = cpu_to_le32(cns); 961bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox 962bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox return nvme_submit_user_admin_command(ns->dev, addr, 4096, &c); 963bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox} 964bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox 965bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcoxstatic int nvme_get_range_type(struct nvme_ns *ns, unsigned long addr) 966bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox{ 967bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox struct nvme_command c; 968b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 969b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&c, 0, sizeof(c)); 970b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.features.opcode = nvme_admin_get_features; 971b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.features.nsid = cpu_to_le32(ns->ns_id); 972b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.features.fid = cpu_to_le32(NVME_FEAT_LBA_RANGE); 973b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 974bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox return nvme_submit_user_admin_command(ns->dev, addr, 4096, &c); 975b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 976b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 977a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcoxstatic int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) 978a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox{ 979a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct nvme_dev *dev = ns->dev; 980a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct nvme_queue *nvmeq; 981a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct nvme_user_io io; 982a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct nvme_command c; 983a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox unsigned length; 984a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox u32 result; 985a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox int nents, status; 986a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct scatterlist *sg; 987e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews struct nvme_prps *prps; 988a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 989a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox if (copy_from_user(&io, uio, sizeof(io))) 990a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox return -EFAULT; 991a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox length = io.nblocks << io.block_shift; 992a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox nents = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length, &sg); 993a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox if (nents < 0) 994a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox return nents; 995a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 996a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox memset(&c, 0, sizeof(c)); 997a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.opcode = io.opcode; 998a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.flags = io.flags; 999a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.nsid = cpu_to_le32(io.nsid); 1000a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.slba = cpu_to_le64(io.slba); 1001a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.length = cpu_to_le16(io.nblocks - 1); 1002a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.control = cpu_to_le16(io.control); 1003a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.dsmgmt = cpu_to_le16(io.dsmgmt); 1004a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.reftag = cpu_to_le32(io.reftag); /* XXX: endian? */ 1005a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.apptag = cpu_to_le16(io.apptag); 1006a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.appmask = cpu_to_le16(io.appmask); 1007a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox /* XXX: metadata */ 1008d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox prps = nvme_setup_prps(dev, &c.common, sg, length); 1009a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 1010d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox nvmeq = get_nvmeq(ns); 1011b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox /* Since nvme_submit_sync_cmd sleeps, we can't keep preemption 1012b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox * disabled. We may be preempted at any point, and be rescheduled 1013b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox * to a different CPU. That will cause cacheline bouncing, but no 1014b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox * additional races since q_lock already protects against other CPUs. 1015b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox */ 1016a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox put_nvmeq(nvmeq); 1017e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox status = nvme_submit_sync_cmd(nvmeq, &c, &result, IO_TIMEOUT); 1018a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 1019a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox nvme_unmap_user_pages(dev, io.opcode & 1, io.addr, length, sg, nents); 1020d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox nvme_free_prps(dev, prps); 1021a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox put_user(result, &uio->result); 1022a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox return status; 1023a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox} 1024a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 10256ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcoxstatic int nvme_download_firmware(struct nvme_ns *ns, 10266ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox struct nvme_dlfw __user *udlfw) 10276ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox{ 10286ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox struct nvme_dev *dev = ns->dev; 10296ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox struct nvme_dlfw dlfw; 10306ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox struct nvme_command c; 10316ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox int nents, status; 10326ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox struct scatterlist *sg; 1033e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews struct nvme_prps *prps; 10346ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 10356ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox if (copy_from_user(&dlfw, udlfw, sizeof(dlfw))) 10366ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox return -EFAULT; 10376ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox if (dlfw.length >= (1 << 30)) 10386ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox return -EINVAL; 10396ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 10406ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox nents = nvme_map_user_pages(dev, 1, dlfw.addr, dlfw.length * 4, &sg); 10416ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox if (nents < 0) 10426ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox return nents; 10436ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 10446ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox memset(&c, 0, sizeof(c)); 10456ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox c.dlfw.opcode = nvme_admin_download_fw; 10466ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox c.dlfw.numd = cpu_to_le32(dlfw.length); 10476ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox c.dlfw.offset = cpu_to_le32(dlfw.offset); 1048d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox prps = nvme_setup_prps(dev, &c.common, sg, dlfw.length * 4); 10496ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 10506ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox status = nvme_submit_admin_cmd(dev, &c, NULL); 10516ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox nvme_unmap_user_pages(dev, 0, dlfw.addr, dlfw.length * 4, sg, nents); 1052d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox nvme_free_prps(dev, prps); 10536ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox return status; 10546ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox} 10556ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 10566ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcoxstatic int nvme_activate_firmware(struct nvme_ns *ns, unsigned long arg) 10576ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox{ 10586ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox struct nvme_dev *dev = ns->dev; 10596ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox struct nvme_command c; 10606ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 10616ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox memset(&c, 0, sizeof(c)); 10626ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox c.common.opcode = nvme_admin_activate_fw; 10636ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox c.common.rsvd10[0] = cpu_to_le32(arg); 10646ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 10656ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox return nvme_submit_admin_cmd(dev, &c, NULL); 10666ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox} 10676ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 1068b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, 1069b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long arg) 1070b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1071b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_ns *ns = bdev->bd_disk->private_data; 1072b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1073b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox switch (cmd) { 1074b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox case NVME_IOCTL_IDENTIFY_NS: 107536c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox return nvme_identify(ns, arg, 0); 1076b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox case NVME_IOCTL_IDENTIFY_CTRL: 107736c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox return nvme_identify(ns, arg, 1); 1078b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox case NVME_IOCTL_GET_RANGE_TYPE: 1079bd38c5557cf482fc195e2264b32ea62eed60730aMatthew Wilcox return nvme_get_range_type(ns, arg); 1080a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox case NVME_IOCTL_SUBMIT_IO: 1081a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox return nvme_submit_io(ns, (void __user *)arg); 10826ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox case NVME_IOCTL_DOWNLOAD_FW: 10836ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox return nvme_download_firmware(ns, (void __user *)arg); 10846ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox case NVME_IOCTL_ACTIVATE_FW: 10856ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox return nvme_activate_firmware(ns, arg); 1086b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox default: 1087b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -ENOTTY; 1088b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1089b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1090b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1091b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic const struct block_device_operations nvme_fops = { 1092b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .owner = THIS_MODULE, 1093b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .ioctl = nvme_ioctl, 1094b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 1095b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1096b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, int index, 1097b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_id_ns *id, struct nvme_lba_range_type *rt) 1098b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1099b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_ns *ns; 1100b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct gendisk *disk; 1101b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int lbaf; 1102b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1103b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (rt->attributes & NVME_LBART_ATTRIB_HIDE) 1104b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 1105b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1106b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns = kzalloc(sizeof(*ns), GFP_KERNEL); 1107b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!ns) 1108b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 1109b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->queue = blk_alloc_queue(GFP_KERNEL); 1110b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!ns->queue) 1111b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto out_free_ns; 1112b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->queue->queue_flags = QUEUE_FLAG_DEFAULT | QUEUE_FLAG_NOMERGES | 1113b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox QUEUE_FLAG_NONROT | QUEUE_FLAG_DISCARD; 1114b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox blk_queue_make_request(ns->queue, nvme_make_request); 1115b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->dev = dev; 1116b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->queue->queuedata = ns; 1117b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1118b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk = alloc_disk(NVME_MINORS); 1119b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!disk) 1120b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto out_free_queue; 1121b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->ns_id = index; 1122b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->disk = disk; 1123b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox lbaf = id->flbas & 0xf; 1124b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->lba_shift = id->lbaf[lbaf].ds; 1125b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1126b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->major = nvme_major; 1127b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->minors = NVME_MINORS; 1128b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->first_minor = NVME_MINORS * index; 1129b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->fops = &nvme_fops; 1130b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->private_data = ns; 1131b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->queue = ns->queue; 1132388f037f4e7f0a24bac6b1a24f144f5d939f58cfMatthew Wilcox disk->driverfs_dev = &dev->pci_dev->dev; 1133b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox sprintf(disk->disk_name, "nvme%dn%d", dev->instance, index); 1134b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9)); 1135b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1136b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return ns; 1137b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1138b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free_queue: 1139b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox blk_cleanup_queue(ns->queue); 1140b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free_ns: 1141b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(ns); 1142b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 1143b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1144b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1145b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_ns_free(struct nvme_ns *ns) 1146b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1147b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox put_disk(ns->disk); 1148b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox blk_cleanup_queue(ns->queue); 1149b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(ns); 1150b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1151b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1152b3b06812e199f248561ce7824a4a8a9cd573c05aMatthew Wilcoxstatic int set_queue_count(struct nvme_dev *dev, int count) 1153b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1154b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 1155b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 result; 1156b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command c; 1157b3b06812e199f248561ce7824a4a8a9cd573c05aMatthew Wilcox u32 q_count = (count - 1) | ((count - 1) << 16); 1158b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1159b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&c, 0, sizeof(c)); 1160b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.features.opcode = nvme_admin_get_features; 1161b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.features.fid = cpu_to_le32(NVME_FEAT_NUM_QUEUES); 1162b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.features.dword11 = cpu_to_le32(q_count); 1163b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1164b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox status = nvme_submit_admin_cmd(dev, &c, &result); 1165b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (status) 1166b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EIO; 1167b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return min(result & 0xffff, result >> 16) + 1; 1168b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1169b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1170b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_setup_io_queues(struct nvme_dev *dev) 1171b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 11721b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox int result, cpu, i, nr_queues; 1173b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 11741b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox nr_queues = num_online_cpus(); 11751b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox result = set_queue_count(dev, nr_queues); 11761b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox if (result < 0) 11771b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox return result; 11781b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox if (result < nr_queues) 11791b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox nr_queues = result; 1180b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 11811b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox /* Deregister the admin queue's interrupt */ 11821b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox free_irq(dev->entry[0].vector, dev->queues[0]); 11831b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox 11841b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox for (i = 0; i < nr_queues; i++) 11851b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox dev->entry[i].entry = i; 11861b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox for (;;) { 11871b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox result = pci_enable_msix(dev->pci_dev, dev->entry, nr_queues); 11881b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox if (result == 0) { 11891b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox break; 11901b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } else if (result > 0) { 11911b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox nr_queues = result; 11921b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox continue; 11931b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } else { 11941b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox nr_queues = 1; 11951b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox break; 11961b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } 11971b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } 11981b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox 11991b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox result = queue_request_irq(dev, dev->queues[0], "nvme admin"); 12001b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox /* XXX: handle failure here */ 12011b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox 12021b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox cpu = cpumask_first(cpu_online_mask); 12031b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox for (i = 0; i < nr_queues; i++) { 12041b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu)); 12051b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox cpu = cpumask_next(cpu, cpu_online_mask); 12061b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } 12071b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox 12081b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox for (i = 0; i < nr_queues; i++) { 12091b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox dev->queues[i + 1] = nvme_create_queue(dev, i + 1, 12101b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox NVME_Q_DEPTH, i); 12111b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox if (!dev->queues[i + 1]) 12121b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox return -ENOMEM; 12131b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox dev->queue_count++; 12141b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } 1215b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1216b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 1217b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1218b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1219b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_free_queues(struct nvme_dev *dev) 1220b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1221b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int i; 1222b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1223b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox for (i = dev->queue_count - 1; i >= 0; i--) 1224b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_free_queue(dev, i); 1225b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1226b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1227b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_dev_add(struct nvme_dev *dev) 1228b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1229b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int res, nn, i; 1230b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_ns *ns, *next; 123151814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox struct nvme_id_ctrl *ctrl; 1232b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox void *id; 1233b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_addr_t dma_addr; 1234b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command cid, crt; 1235b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1236b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox res = nvme_setup_io_queues(dev); 1237b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (res) 1238b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return res; 1239b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1240b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox /* XXX: Switch to a SG list once prp2 works */ 1241b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox id = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr, 1242b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox GFP_KERNEL); 1243b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1244b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&cid, 0, sizeof(cid)); 1245b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cid.identify.opcode = nvme_admin_identify; 1246b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cid.identify.nsid = 0; 1247b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cid.identify.prp1 = cpu_to_le64(dma_addr); 1248b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cid.identify.cns = cpu_to_le32(1); 1249b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1250b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox res = nvme_submit_admin_cmd(dev, &cid, NULL); 1251b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (res) { 1252b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox res = -EIO; 1253b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto out_free; 1254b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1255b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 125651814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox ctrl = id; 125751814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox nn = le32_to_cpup(&ctrl->nn); 125851814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn)); 125951814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn)); 126051814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr)); 1261b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1262b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cid.identify.cns = 0; 1263b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&crt, 0, sizeof(crt)); 1264b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox crt.features.opcode = nvme_admin_get_features; 1265b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox crt.features.prp1 = cpu_to_le64(dma_addr + 4096); 1266b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox crt.features.fid = cpu_to_le32(NVME_FEAT_LBA_RANGE); 1267b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1268b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox for (i = 0; i < nn; i++) { 1269b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cid.identify.nsid = cpu_to_le32(i); 1270b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox res = nvme_submit_admin_cmd(dev, &cid, NULL); 1271b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (res) 1272b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox continue; 1273b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1274b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (((struct nvme_id_ns *)id)->ncap == 0) 1275b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox continue; 1276b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1277b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox crt.features.nsid = cpu_to_le32(i); 1278b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox res = nvme_submit_admin_cmd(dev, &crt, NULL); 1279b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (res) 1280b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox continue; 1281b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1282b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns = nvme_alloc_ns(dev, i, id, id + 4096); 1283b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (ns) 1284b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_add_tail(&ns->list, &dev->namespaces); 1285b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1286b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_for_each_entry(ns, &dev->namespaces, list) 1287b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox add_disk(ns->disk); 1288b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1289b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr); 1290b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 1291b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1292b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free: 1293b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_for_each_entry_safe(ns, next, &dev->namespaces, list) { 1294b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_del(&ns->list); 1295b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_ns_free(ns); 1296b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1297b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1298b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr); 1299b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return res; 1300b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1301b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1302b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_dev_remove(struct nvme_dev *dev) 1303b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1304b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_ns *ns, *next; 1305b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1306b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox /* TODO: wait all I/O finished or cancel them */ 1307b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1308b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_for_each_entry_safe(ns, next, &dev->namespaces, list) { 1309b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_del(&ns->list); 1310b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox del_gendisk(ns->disk); 1311b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_ns_free(ns); 1312b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1313b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1314b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_free_queues(dev); 1315b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1316b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 1317b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1318b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1319091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcoxstatic int nvme_setup_prp_pools(struct nvme_dev *dev) 1320091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox{ 1321091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox struct device *dmadev = &dev->pci_dev->dev; 1322091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox dev->prp_page_pool = dma_pool_create("prp list page", dmadev, 1323091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox PAGE_SIZE, PAGE_SIZE, 0); 1324091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox if (!dev->prp_page_pool) 1325091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox return -ENOMEM; 1326091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox 132799802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox /* Optimisation for I/Os between 4k and 128k */ 132899802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox dev->prp_small_pool = dma_pool_create("prp list 256", dmadev, 132999802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox 256, 256, 0); 133099802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox if (!dev->prp_small_pool) { 133199802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox dma_pool_destroy(dev->prp_page_pool); 133299802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox return -ENOMEM; 133399802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox } 1334091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox return 0; 1335091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox} 1336091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox 1337091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcoxstatic void nvme_release_prp_pools(struct nvme_dev *dev) 1338091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox{ 1339091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox dma_pool_destroy(dev->prp_page_pool); 134099802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox dma_pool_destroy(dev->prp_small_pool); 1341091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox} 1342091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox 1343b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* XXX: Use an ida or something to let remove / add work correctly */ 1344b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_set_instance(struct nvme_dev *dev) 1345b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1346b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox static int instance; 1347b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->instance = instance++; 1348b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1349b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1350b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_release_instance(struct nvme_dev *dev) 1351b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1352b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1353b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1354b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_probe(struct pci_dev *pdev, 1355b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox const struct pci_device_id *id) 1356b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1357574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox int bars, result = -ENOMEM; 1358b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_dev *dev; 1359b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1360b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1361b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!dev) 1362b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -ENOMEM; 1363b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry), 1364b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox GFP_KERNEL); 1365b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!dev->entry) 1366b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free; 13671b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *), 13681b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox GFP_KERNEL); 1369b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!dev->queues) 1370b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free; 1371b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 13720ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews if (pci_enable_device_mem(pdev)) 13730ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews goto free; 1374f64d3365a3e5cb46e69db7e2c82a7cb9a5bed1b8Matthew Wilcox pci_set_master(pdev); 1375574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox bars = pci_select_bars(pdev, IORESOURCE_MEM); 1376574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox if (pci_request_selected_regions(pdev, bars, "nvme")) 1377574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox goto disable; 13780ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews 1379b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox INIT_LIST_HEAD(&dev->namespaces); 1380b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->pci_dev = pdev; 1381b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox pci_set_drvdata(pdev, dev); 13822930353f9f2b9e4629e935acd970cb73c1171229Matthew Wilcox dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)); 13832930353f9f2b9e4629e935acd970cb73c1171229Matthew Wilcox dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64)); 1384b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_set_instance(dev); 138553c9577e9ca68a633c6e9df2b54eaecacfa77f62Matthew Wilcox dev->entry[0].vector = pdev->irq; 1386b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1387091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox result = nvme_setup_prp_pools(dev); 1388091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox if (result) 1389091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox goto disable_msix; 1390091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox 1391b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->bar = ioremap(pci_resource_start(pdev, 0), 8192); 1392b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!dev->bar) { 1393b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = -ENOMEM; 1394574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox goto disable_msix; 1395b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1396b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1397b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = nvme_configure_admin_queue(dev); 1398b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result) 1399b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto unmap; 1400b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->queue_count++; 1401b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1402b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = nvme_dev_add(dev); 1403b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result) 1404b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto delete; 1405b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 1406b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1407b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox delete: 1408b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_free_queues(dev); 1409b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unmap: 1410b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox iounmap(dev->bar); 1411574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox disable_msix: 1412b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox pci_disable_msix(pdev); 1413b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_release_instance(dev); 1414091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox nvme_release_prp_pools(dev); 1415574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox disable: 14160ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews pci_disable_device(pdev); 1417574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox pci_release_regions(pdev); 1418b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free: 1419b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev->queues); 1420b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev->entry); 1421b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev); 1422b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return result; 1423b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1424b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1425b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void __devexit nvme_remove(struct pci_dev *pdev) 1426b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1427b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_dev *dev = pci_get_drvdata(pdev); 1428b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_dev_remove(dev); 1429b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox pci_disable_msix(pdev); 1430b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox iounmap(dev->bar); 1431b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_release_instance(dev); 1432091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox nvme_release_prp_pools(dev); 14330ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews pci_disable_device(pdev); 1434574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox pci_release_regions(pdev); 1435b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev->queues); 1436b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev->entry); 1437b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev); 1438b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1439b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1440b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* These functions are yet to be implemented */ 1441b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_error_detected NULL 1442b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_dump_registers NULL 1443b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_link_reset NULL 1444b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_slot_reset NULL 1445b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_error_resume NULL 1446b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_suspend NULL 1447b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_resume NULL 1448b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1449b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct pci_error_handlers nvme_err_handler = { 1450b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .error_detected = nvme_error_detected, 1451b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .mmio_enabled = nvme_dump_registers, 1452b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .link_reset = nvme_link_reset, 1453b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .slot_reset = nvme_slot_reset, 1454b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .resume = nvme_error_resume, 1455b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 1456b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1457b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* Move to pci_ids.h later */ 1458b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define PCI_CLASS_STORAGE_EXPRESS 0x010802 1459b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1460b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = { 1461b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) }, 1462b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox { 0, } 1463b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 1464b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_DEVICE_TABLE(pci, nvme_id_table); 1465b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1466b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct pci_driver nvme_driver = { 1467b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .name = "nvme", 1468b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .id_table = nvme_id_table, 1469b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .probe = nvme_probe, 1470b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .remove = __devexit_p(nvme_remove), 1471b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .suspend = nvme_suspend, 1472b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .resume = nvme_resume, 1473b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .err_handler = &nvme_err_handler, 1474b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 1475b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1476b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __init nvme_init(void) 1477b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1478b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int result; 1479b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1480b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_major = register_blkdev(nvme_major, "nvme"); 1481b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (nvme_major <= 0) 1482b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EBUSY; 1483b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1484b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = pci_register_driver(&nvme_driver); 1485b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!result) 1486b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 1487b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1488b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unregister_blkdev(nvme_major, "nvme"); 1489b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return result; 1490b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1491b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1492b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void __exit nvme_exit(void) 1493b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1494b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox pci_unregister_driver(&nvme_driver); 1495b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unregister_blkdev(nvme_major, "nvme"); 1496b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1497b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1498b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>"); 1499b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_LICENSE("GPL"); 1500db5d0c198d673b6a932b449d4db95a2ad50c755eMatthew WilcoxMODULE_VERSION("0.2"); 1501b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_init(nvme_init); 1502b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_exit(nvme_exit); 1503