nvme-core.c revision bc5fc7e4b22ca855902aba02b28c96f09b446407
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> 218de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox#include <linux/bitops.h> 22b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/blkdev.h> 23fd63e9ceeeae58cfe877c2d49d41c1bf7532303cMatthew Wilcox#include <linux/delay.h> 24b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/errno.h> 25b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/fs.h> 26b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/genhd.h> 275aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox#include <linux/idr.h> 28b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/init.h> 29b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/interrupt.h> 30b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/io.h> 31b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/kdev_t.h> 321fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox#include <linux/kthread.h> 33b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/kernel.h> 34b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/mm.h> 35b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/module.h> 36b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/moduleparam.h> 37b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/pci.h> 38be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox#include <linux/poison.h> 39b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/sched.h> 40b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/slab.h> 41b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/types.h> 42b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/version.h> 43b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 44b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define NVME_Q_DEPTH 1024 45b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command)) 46b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion)) 47b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define NVME_MINORS 64 48e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox#define IO_TIMEOUT (5 * HZ) 49e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox#define ADMIN_TIMEOUT (60 * HZ) 50b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 51b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_major; 52b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_param(nvme_major, int, 0); 53b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 5458ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxstatic int use_threaded_interrupts; 5558ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxmodule_param(use_threaded_interrupts, int, 0); 5658ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox 571fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic DEFINE_SPINLOCK(dev_list_lock); 581fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic LIST_HEAD(dev_list); 591fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic struct task_struct *nvme_thread; 601fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 61b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 62b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Represents an NVM Express device. Each nvme_dev is a PCI function. 63b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 64b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct nvme_dev { 651fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox struct list_head node; 66b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue **queues; 67b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 __iomem *dbs; 68b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct pci_dev *pci_dev; 69091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox struct dma_pool *prp_page_pool; 7099802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox struct dma_pool *prp_small_pool; 71b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int instance; 72b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int queue_count; 73b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 ctrl_config; 74b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct msix_entry *entry; 75b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_bar __iomem *bar; 76b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct list_head namespaces; 7751814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox char serial[20]; 7851814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox char model[40]; 7951814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox char firmware_rev[8]; 80b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 81b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 82b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 83b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * An NVM Express namespace is equivalent to a SCSI LUN 84b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 85b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct nvme_ns { 86b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct list_head list; 87b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 88b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_dev *dev; 89b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct request_queue *queue; 90b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct gendisk *disk; 91b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 92b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int ns_id; 93b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int lba_shift; 94b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 95b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 96b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 97b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * An NVM Express queue. Each device has at least two (one for admin 98b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * commands and one for I/O commands). 99b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 100b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct nvme_queue { 101b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct device *q_dmadev; 102091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox struct nvme_dev *dev; 103b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox spinlock_t q_lock; 104b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command *sq_cmds; 105b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox volatile struct nvme_completion *cqes; 106b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_addr_t sq_dma_addr; 107b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_addr_t cq_dma_addr; 108b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox wait_queue_head_t sq_full; 1091fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox wait_queue_t sq_cong_wait; 110b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct bio_list sq_cong; 111b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 __iomem *q_db; 112b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 q_depth; 113b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 cq_vector; 114b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 sq_head; 115b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 sq_tail; 116b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 cq_head; 117821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox u16 cq_phase; 118b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long cmdid_data[]; 119b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 120b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 121b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 122b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Check we didin't inadvertently grow the command struct 123b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 124b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic inline void _nvme_check_size(void) 125b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 126b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64); 127b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64); 128b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64); 129b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64); 130b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_features) != 64); 131b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_command) != 64); 132b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096); 133b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096); 134b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64); 135b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 136b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 137e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstruct nvme_cmd_info { 138e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox unsigned long ctx; 139e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox unsigned long timeout; 140e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox}; 141e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox 142e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstatic struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq) 143e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox{ 144e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)]; 145e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox} 146e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox 147b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/** 148714a7a22884b74862540bc84955274d86b2f6040Matthew Wilcox * alloc_cmdid() - Allocate a Command ID 149714a7a22884b74862540bc84955274d86b2f6040Matthew Wilcox * @nvmeq: The queue that will be used for this command 150714a7a22884b74862540bc84955274d86b2f6040Matthew Wilcox * @ctx: A pointer that will be passed to the handler 151714a7a22884b74862540bc84955274d86b2f6040Matthew Wilcox * @handler: The ID of the handler to call 152b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * 153b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Allocate a Command ID for a queue. The data passed in will 154b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * be passed to the completion handler. This is implemented by using 155b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * the bottom two bits of the ctx pointer to store the handler ID. 156b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Passing in a pointer that's not 4-byte aligned will cause a BUG. 157b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * We can change this if it becomes a problem. 158184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox * 159184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox * May be called with local interrupts disabled and the q_lock held, 160184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox * or with interrupts enabled and no locks held. 161b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 162e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstatic int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx, int handler, 163e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox unsigned timeout) 164b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 165e6d15f79f997a98b3a69abbc462fc9041cc1a7b4Matthew Wilcox int depth = nvmeq->q_depth - 1; 166e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); 167b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int cmdid; 168b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 169b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUG_ON((unsigned long)ctx & 3); 170b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 171b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox do { 172b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth); 173b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (cmdid >= depth) 174b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EBUSY; 175b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } while (test_and_set_bit(cmdid, nvmeq->cmdid_data)); 176b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 177e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox info[cmdid].ctx = (unsigned long)ctx | handler; 178e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox info[cmdid].timeout = jiffies + timeout; 179b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return cmdid; 180b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 181b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 182b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx, 183e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox int handler, unsigned timeout) 184b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 185b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int cmdid; 186b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox wait_event_killable(nvmeq->sq_full, 187e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0); 188b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return (cmdid < 0) ? -EINTR : cmdid; 189b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 190b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 191fa92282149842645931580225647238428374758Matthew Wilcox/* 192fa92282149842645931580225647238428374758Matthew Wilcox * If you need more than four handlers, you'll need to change how 193be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox * alloc_cmdid and nvme_process_cq work. Consider using a special 194be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox * CMD_CTX value instead, if that works for your situation. 195b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 196b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxenum { 197b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox sync_completion_id = 0, 198b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_completion_id, 199b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 200b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 20100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox/* Special values must be a multiple of 4, and less than 0x1000 */ 202be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox#define CMD_CTX_BASE (POISON_POINTER_DELTA + sync_completion_id) 203d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE) 204d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE) 205d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE) 20600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox#define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE) 207be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox 208184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox/* 209184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox * Called with local interrupts disabled and the q_lock held. May not sleep. 210184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox */ 211b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic unsigned long free_cmdid(struct nvme_queue *nvmeq, int cmdid) 212b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 213b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long data; 214e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); 215b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 216e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox if (cmdid >= nvmeq->q_depth) 21748e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox return CMD_CTX_INVALID; 218e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox data = info[cmdid].ctx; 219e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox info[cmdid].ctx = CMD_CTX_COMPLETED; 220b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox clear_bit(cmdid, nvmeq->cmdid_data); 221b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox wake_up(&nvmeq->sq_full); 222b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return data; 223b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 224b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 22521075bdee0a6f56058920d889df4ae561bfed754Matthew Wilcoxstatic unsigned long cancel_cmdid(struct nvme_queue *nvmeq, int cmdid) 2263c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox{ 22721075bdee0a6f56058920d889df4ae561bfed754Matthew Wilcox unsigned long data; 228e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); 22921075bdee0a6f56058920d889df4ae561bfed754Matthew Wilcox data = info[cmdid].ctx; 230e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox info[cmdid].ctx = CMD_CTX_CANCELLED; 23121075bdee0a6f56058920d889df4ae561bfed754Matthew Wilcox return data; 2323c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox} 2333c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox 234b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct nvme_queue *get_nvmeq(struct nvme_ns *ns) 235b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 2369ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox return ns->dev->queues[get_cpu() + 1]; 237b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 238b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 239b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void put_nvmeq(struct nvme_queue *nvmeq) 240b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 2411b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox put_cpu(); 242b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 243b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 244b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/** 245714a7a22884b74862540bc84955274d86b2f6040Matthew Wilcox * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell 246b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @nvmeq: The queue to use 247b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @cmd: The command to send 248b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * 249b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Safe to use from interrupt context 250b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 251b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd) 252b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 253b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long flags; 254b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 tail; 255b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox spin_lock_irqsave(&nvmeq->q_lock, flags); 256b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox tail = nvmeq->sq_tail; 257b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd)); 258b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (++tail == nvmeq->q_depth) 259b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox tail = 0; 2607547881d0951384f9833ec3a80fac8f3f16f3b98Matthew Wilcox writel(tail, nvmeq->q_db); 261b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_tail = tail; 262b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox spin_unlock_irqrestore(&nvmeq->q_lock, flags); 263b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 264b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 265b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 266b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 267e025344c56e08b155f43ea09647969286c78377cShane Michael Matthewsstruct nvme_prps { 268e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews int npages; 269e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr_t first_dma; 270e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews __le64 *list[0]; 271e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews}; 272e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews 273d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcoxstatic void nvme_free_prps(struct nvme_dev *dev, struct nvme_prps *prps) 274e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews{ 275e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews const int last_prp = PAGE_SIZE / 8 - 1; 276e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews int i; 277e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr_t prp_dma; 278e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews 279e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews if (!prps) 280e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews return; 281e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews 282e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prp_dma = prps->first_dma; 28399802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox 28499802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox if (prps->npages == 0) 28599802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox dma_pool_free(dev->prp_small_pool, prps->list[0], prp_dma); 286e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews for (i = 0; i < prps->npages; i++) { 287e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews __le64 *prp_list = prps->list[i]; 288e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]); 289091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox dma_pool_free(dev->prp_page_pool, prp_list, prp_dma); 290e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prp_dma = next_prp_dma; 291e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews } 292e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews kfree(prps); 293e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews} 294e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews 295d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstruct nvme_bio { 296b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct bio *bio; 297b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int nents; 298e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews struct nvme_prps *prps; 299b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct scatterlist sg[0]; 300b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 301b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 302b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* XXX: use a mempool */ 303d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstatic struct nvme_bio *alloc_nbio(unsigned nseg, gfp_t gfp) 304b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 305d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox return kzalloc(sizeof(struct nvme_bio) + 306b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox sizeof(struct scatterlist) * nseg, gfp); 307b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 308b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 309d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstatic void free_nbio(struct nvme_queue *nvmeq, struct nvme_bio *nbio) 310b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 311d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox nvme_free_prps(nvmeq->dev, nbio->prps); 312d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox kfree(nbio); 313b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 314b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 315b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void bio_completion(struct nvme_queue *nvmeq, void *ctx, 316b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_completion *cqe) 317b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 318d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox struct nvme_bio *nbio = ctx; 319d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox struct bio *bio = nbio->bio; 320b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 status = le16_to_cpup(&cqe->status) >> 1; 321b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 322d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox dma_unmap_sg(nvmeq->q_dmadev, nbio->sg, nbio->nents, 323b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 324d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox free_nbio(nvmeq, nbio); 32509a58f536436efed02ead722e835cb4ce7674afcMatthew Wilcox if (status) { 3261ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox bio_endio(bio, -EIO); 32709a58f536436efed02ead722e835cb4ce7674afcMatthew Wilcox } else if (bio->bi_vcnt > bio->bi_idx) { 328eac623ba7a91474a688eb5d0fcd0eaa6a56dc41cMatthew Wilcox if (bio_list_empty(&nvmeq->sq_cong)) 329eac623ba7a91474a688eb5d0fcd0eaa6a56dc41cMatthew Wilcox add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait); 3301ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox bio_list_add(&nvmeq->sq_cong, bio); 3311ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox wake_up_process(nvme_thread); 3321ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox } else { 3331ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox bio_endio(bio, 0); 3341ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox } 335b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 336b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 337184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox/* length is in bytes. gfp flags indicates whether we may sleep. */ 338d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcoxstatic struct nvme_prps *nvme_setup_prps(struct nvme_dev *dev, 339e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews struct nvme_common_command *cmd, 340b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox struct scatterlist *sg, int *len, 341b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox gfp_t gfp) 342ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox{ 34399802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox struct dma_pool *pool; 344b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox int length = *len; 345ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox int dma_len = sg_dma_len(sg); 346ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox u64 dma_addr = sg_dma_address(sg); 347ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox int offset = offset_in_page(dma_addr); 348e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews __le64 *prp_list; 349e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr_t prp_dma; 350e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews int nprps, npages, i, prp_page; 351e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews struct nvme_prps *prps = NULL; 352ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 353ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmd->prp1 = cpu_to_le64(dma_addr); 354ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox length -= (PAGE_SIZE - offset); 355ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox if (length <= 0) 356e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews return prps; 357ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 358ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox dma_len -= (PAGE_SIZE - offset); 359ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox if (dma_len) { 360ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox dma_addr += (PAGE_SIZE - offset); 361ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox } else { 362ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox sg = sg_next(sg); 363ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox dma_addr = sg_dma_address(sg); 364ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox dma_len = sg_dma_len(sg); 365ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox } 366ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 367ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox if (length <= PAGE_SIZE) { 368ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmd->prp2 = cpu_to_le64(dma_addr); 369e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews return prps; 370e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews } 371e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews 372e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews nprps = DIV_ROUND_UP(length, PAGE_SIZE); 373e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews npages = DIV_ROUND_UP(8 * nprps, PAGE_SIZE); 374b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox prps = kmalloc(sizeof(*prps) + sizeof(__le64 *) * npages, gfp); 375b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox if (!prps) { 376b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox cmd->prp2 = cpu_to_le64(dma_addr); 377b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox *len = (*len - length) + PAGE_SIZE; 378b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox return prps; 379b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox } 380e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prp_page = 0; 38199802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox if (nprps <= (256 / 8)) { 38299802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox pool = dev->prp_small_pool; 38399802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox prps->npages = 0; 38499802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox } else { 38599802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox pool = dev->prp_page_pool; 38699802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox prps->npages = npages; 38799802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox } 38899802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox 389b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox prp_list = dma_pool_alloc(pool, gfp, &prp_dma); 390b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox if (!prp_list) { 391b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox cmd->prp2 = cpu_to_le64(dma_addr); 392b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox *len = (*len - length) + PAGE_SIZE; 393b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox kfree(prps); 394b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox return NULL; 395b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox } 396e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prps->list[prp_page++] = prp_list; 397e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prps->first_dma = prp_dma; 398e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews cmd->prp2 = cpu_to_le64(prp_dma); 399e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews i = 0; 400e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews for (;;) { 4017523d834dd1573610078eb1ac0933f6490232f90Matthew Wilcox if (i == PAGE_SIZE / 8) { 402e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews __le64 *old_prp_list = prp_list; 403b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox prp_list = dma_pool_alloc(pool, gfp, &prp_dma); 404b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox if (!prp_list) { 405b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox *len = (*len - length); 406b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox return prps; 407b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox } 408e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prps->list[prp_page++] = prp_list; 4097523d834dd1573610078eb1ac0933f6490232f90Matthew Wilcox prp_list[0] = old_prp_list[i - 1]; 4107523d834dd1573610078eb1ac0933f6490232f90Matthew Wilcox old_prp_list[i - 1] = cpu_to_le64(prp_dma); 4117523d834dd1573610078eb1ac0933f6490232f90Matthew Wilcox i = 1; 412e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews } 413e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prp_list[i++] = cpu_to_le64(dma_addr); 414e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_len -= PAGE_SIZE; 415e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr += PAGE_SIZE; 416e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews length -= PAGE_SIZE; 417e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews if (length <= 0) 418e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews break; 419e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews if (dma_len > 0) 420e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews continue; 421e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews BUG_ON(dma_len < 0); 422e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews sg = sg_next(sg); 423e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr = sg_dma_address(sg); 424e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_len = sg_dma_len(sg); 425ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox } 426ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 427e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews return prps; 428ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox} 429ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 4301ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox/* NVMe scatterlists require no holes in the virtual address */ 4311ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \ 4321ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE)) 4331ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox 434d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcoxstatic int nvme_map_bio(struct device *dev, struct nvme_bio *nbio, 435b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct bio *bio, enum dma_data_direction dma_dir, int psegs) 436b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 437768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox struct bio_vec *bvec, *bvprv = NULL; 438768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox struct scatterlist *sg = NULL; 4391ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox int i, old_idx, length = 0, nsegs = 0; 440b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 441768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox sg_init_table(nbio->sg, psegs); 4421ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox old_idx = bio->bi_idx; 443b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_for_each_segment(bvec, bio, i) { 444768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox if (bvprv && BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) { 445768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox sg->length += bvec->bv_len; 446768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox } else { 4471ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox if (bvprv && BIOVEC_NOT_VIRT_MERGEABLE(bvprv, bvec)) 4481ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox break; 449768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox sg = sg ? sg + 1 : nbio->sg; 450768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox sg_set_page(sg, bvec->bv_page, bvec->bv_len, 451768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox bvec->bv_offset); 452768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox nsegs++; 453768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox } 4541ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox length += bvec->bv_len; 455768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox bvprv = bvec; 456b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 4571ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox bio->bi_idx = i; 458d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox nbio->nents = nsegs; 459768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox sg_mark_end(sg); 4601ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox if (dma_map_sg(dev, nbio->sg, nbio->nents, dma_dir) == 0) { 4611ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox bio->bi_idx = old_idx; 4621ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox return -ENOMEM; 4631ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox } 4641ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox return length; 465b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 466b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 46700df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcoxstatic int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns, 46800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox int cmdid) 46900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox{ 47000df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; 47100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 47200df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox memset(cmnd, 0, sizeof(*cmnd)); 47300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox cmnd->common.opcode = nvme_cmd_flush; 47400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox cmnd->common.command_id = cmdid; 47500df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox cmnd->common.nsid = cpu_to_le32(ns->ns_id); 47600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 47700df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox if (++nvmeq->sq_tail == nvmeq->q_depth) 47800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox nvmeq->sq_tail = 0; 47900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox writel(nvmeq->sq_tail, nvmeq->q_db); 48000df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 48100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox return 0; 48200df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox} 48300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 48400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcoxstatic int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns) 48500df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox{ 48600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH, 48700df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox sync_completion_id, IO_TIMEOUT); 48800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox if (unlikely(cmdid < 0)) 48900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox return cmdid; 49000df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 49100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox return nvme_submit_flush(nvmeq, ns, cmdid); 49200df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox} 49300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 494184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox/* 495184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox * Called with local interrupts disabled and the q_lock held. May not sleep. 496184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox */ 497b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns, 498b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct bio *bio) 499b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 500ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox struct nvme_command *cmnd; 501d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox struct nvme_bio *nbio; 502b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox enum dma_data_direction dma_dir; 5031ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox int cmdid, length, result = -ENOMEM; 504b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 control; 505b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 dsmgmt; 506b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int psegs = bio_phys_segments(ns->queue, bio); 507b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 50800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox if ((bio->bi_rw & REQ_FLUSH) && psegs) { 50900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox result = nvme_submit_flush_data(nvmeq, ns); 51000df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox if (result) 51100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox return result; 51200df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox } 51300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 514eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox nbio = alloc_nbio(psegs, GFP_ATOMIC); 515d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox if (!nbio) 516eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox goto nomem; 517d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox nbio->bio = bio; 518b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 519eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox result = -EBUSY; 520d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox cmdid = alloc_cmdid(nvmeq, nbio, bio_completion_id, IO_TIMEOUT); 521b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (unlikely(cmdid < 0)) 522d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox goto free_nbio; 523b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 52400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox if ((bio->bi_rw & REQ_FLUSH) && !psegs) 52500df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox return nvme_submit_flush(nvmeq, ns, cmdid); 52600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 527b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox control = 0; 528b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (bio->bi_rw & REQ_FUA) 529b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox control |= NVME_RW_FUA; 530b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD)) 531b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox control |= NVME_RW_LR; 532b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 533b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dsmgmt = 0; 534b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (bio->bi_rw & REQ_RAHEAD) 535b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH; 536b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 537ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; 538b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 539b8deb62cf271fa9381edc8cf52bcae2f0225c55aMatthew Wilcox memset(cmnd, 0, sizeof(*cmnd)); 540b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (bio_data_dir(bio)) { 541ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.opcode = nvme_cmd_write; 542b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_dir = DMA_TO_DEVICE; 543b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } else { 544ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.opcode = nvme_cmd_read; 545b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_dir = DMA_FROM_DEVICE; 546b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 547b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 5481ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox result = nvme_map_bio(nvmeq->q_dmadev, nbio, bio, dma_dir, psegs); 5491ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox if (result < 0) 550eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox goto free_nbio; 5511ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox length = result; 552b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 553ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.command_id = cmdid; 554ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.nsid = cpu_to_le32(ns->ns_id); 555d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox nbio->prps = nvme_setup_prps(nvmeq->dev, &cmnd->common, nbio->sg, 556b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox &length, GFP_ATOMIC); 557ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.slba = cpu_to_le64(bio->bi_sector >> (ns->lba_shift - 9)); 5581ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox cmnd->rw.length = cpu_to_le16((length >> ns->lba_shift) - 1); 559ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.control = cpu_to_le16(control); 560ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt); 561b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 562d8ee9d69f275769aaad40ef7c944565ff8d2d24fMatthew Wilcox bio->bi_sector += length >> 9; 563d8ee9d69f275769aaad40ef7c944565ff8d2d24fMatthew Wilcox 564b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (++nvmeq->sq_tail == nvmeq->q_depth) 565b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_tail = 0; 5667547881d0951384f9833ec3a80fac8f3f16f3b98Matthew Wilcox writel(nvmeq->sq_tail, nvmeq->q_db); 567b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 5681974b1ae8852324a75fb8cfecbc7b758fd5a2c3cMatthew Wilcox return 0; 5691974b1ae8852324a75fb8cfecbc7b758fd5a2c3cMatthew Wilcox 570d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox free_nbio: 571d534df3c730af9073a9ddc076d9fd65cbdca22b3Matthew Wilcox free_nbio(nvmeq, nbio); 572eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox nomem: 573eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox return result; 574b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 575b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 576b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 577b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * NB: return value of non-zero would mean that we were a stacking driver. 578b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * make_request must always succeed. 579b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 580b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_make_request(struct request_queue *q, struct bio *bio) 581b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 582b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_ns *ns = q->queuedata; 583b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq = get_nvmeq(ns); 584eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox int result = -EBUSY; 585eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox 586eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox spin_lock_irq(&nvmeq->q_lock); 587eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox if (bio_list_empty(&nvmeq->sq_cong)) 588eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox result = nvme_submit_bio_queue(nvmeq, ns, bio); 589eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox if (unlikely(result)) { 590eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox if (bio_list_empty(&nvmeq->sq_cong)) 591eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait); 592b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_list_add(&nvmeq->sq_cong, bio); 593b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 594eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox 595eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox spin_unlock_irq(&nvmeq->q_lock); 596b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox put_nvmeq(nvmeq); 597b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 598b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 599b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 600b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 601b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct sync_cmd_info { 602b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct task_struct *task; 603b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 result; 604b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 605b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 606b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 607b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void sync_completion(struct nvme_queue *nvmeq, void *ctx, 608b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_completion *cqe) 609b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 610b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct sync_cmd_info *cmdinfo = ctx; 611c42705592be2a539f3027b6f3907de8e8f9591a8Matthew Wilcox if (unlikely((unsigned long)cmdinfo == CMD_CTX_CANCELLED)) 612be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox return; 61300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox if ((unsigned long)cmdinfo == CMD_CTX_FLUSH) 61400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox return; 615b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox if (unlikely((unsigned long)cmdinfo == CMD_CTX_COMPLETED)) { 616b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox dev_warn(nvmeq->q_dmadev, 617b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox "completed id %d twice on queue %d\n", 618b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox cqe->command_id, le16_to_cpup(&cqe->sq_id)); 619b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox return; 620b36235df01ec4141b4e589571d6789076c346d88Matthew Wilcox } 62148e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox if (unlikely((unsigned long)cmdinfo == CMD_CTX_INVALID)) { 62248e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox dev_warn(nvmeq->q_dmadev, 62348e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox "invalid id %d completed on queue %d\n", 62448e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox cqe->command_id, le16_to_cpup(&cqe->sq_id)); 62548e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox return; 62648e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox } 627b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmdinfo->result = le32_to_cpup(&cqe->result); 628b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmdinfo->status = le16_to_cpup(&cqe->status) >> 1; 629b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox wake_up_process(cmdinfo->task); 630b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 631b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 632b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxtypedef void (*completion_fn)(struct nvme_queue *, void *, 633b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_completion *); 634b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 6358de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcoxstatic const completion_fn nvme_completions[4] = { 6368de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox [sync_completion_id] = sync_completion, 6378de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox [bio_completion_id] = bio_completion, 6388de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox}; 6398de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox 640b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic irqreturn_t nvme_process_cq(struct nvme_queue *nvmeq) 641b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 642821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox u16 head, phase; 643b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 644b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox head = nvmeq->cq_head; 645821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox phase = nvmeq->cq_phase; 646b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 647b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox for (;;) { 648b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long data; 649b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox void *ptr; 650b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned char handler; 651b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_completion cqe = nvmeq->cqes[head]; 652821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox if ((le16_to_cpu(cqe.status) & 1) != phase) 653b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox break; 654b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_head = le16_to_cpu(cqe.sq_head); 655b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (++head == nvmeq->q_depth) { 656b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox head = 0; 657821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox phase = !phase; 658b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 659b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 660b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox data = free_cmdid(nvmeq, cqe.command_id); 661b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox handler = data & 3; 662b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ptr = (void *)(data & ~3UL); 6638de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox nvme_completions[handler](nvmeq, ptr, &cqe); 664b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 665b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 666b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox /* If the controller ignores the cq head doorbell and continuously 667b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * writes to the queue, it is theoretically possible to wrap around 668b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * the queue twice and mistakenly return IRQ_NONE. Linux only 669b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * requires that 0.1% of your interrupts are handled, so this isn't 670b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * a big problem. 671b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 672821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox if (head == nvmeq->cq_head && phase == nvmeq->cq_phase) 673b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return IRQ_NONE; 674b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 675b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writel(head, nvmeq->q_db + 1); 676b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cq_head = head; 677821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox nvmeq->cq_phase = phase; 678b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 679b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return IRQ_HANDLED; 680b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 681b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 682b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic irqreturn_t nvme_irq(int irq, void *data) 683b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 68458ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox irqreturn_t result; 68558ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox struct nvme_queue *nvmeq = data; 68658ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox spin_lock(&nvmeq->q_lock); 68758ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox result = nvme_process_cq(nvmeq); 68858ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox spin_unlock(&nvmeq->q_lock); 68958ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox return result; 69058ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox} 69158ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox 69258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxstatic irqreturn_t nvme_irq_check(int irq, void *data) 69358ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox{ 69458ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox struct nvme_queue *nvmeq = data; 69558ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head]; 69658ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase) 69758ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox return IRQ_NONE; 69858ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox return IRQ_WAKE_THREAD; 69958ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox} 70058ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox 7013c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcoxstatic void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid) 7023c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox{ 7033c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox spin_lock_irq(&nvmeq->q_lock); 70421075bdee0a6f56058920d889df4ae561bfed754Matthew Wilcox cancel_cmdid(nvmeq, cmdid); 7053c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox spin_unlock_irq(&nvmeq->q_lock); 7063c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox} 7073c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox 708b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 709b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Returns 0 on success. If the result is negative, it's a Linux error code; 710b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * if the result is positive, it's an NVM Express status code 711b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 7123c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcoxstatic int nvme_submit_sync_cmd(struct nvme_queue *nvmeq, 713e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox struct nvme_command *cmd, u32 *result, unsigned timeout) 714b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 715b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int cmdid; 716b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct sync_cmd_info cmdinfo; 717b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 718b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmdinfo.task = current; 719b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmdinfo.status = -EINTR; 720b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 721e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox cmdid = alloc_cmdid_killable(nvmeq, &cmdinfo, sync_completion_id, 722e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox timeout); 723b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (cmdid < 0) 724b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return cmdid; 725b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmd->common.command_id = cmdid; 726b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 7273c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox set_current_state(TASK_KILLABLE); 7283c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox nvme_submit_cmd(nvmeq, cmd); 729b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox schedule(); 730b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 7313c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox if (cmdinfo.status == -EINTR) { 7323c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox nvme_abort_command(nvmeq, cmdid); 7333c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox return -EINTR; 7343c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox } 7353c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox 736b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result) 737b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox *result = cmdinfo.result; 738b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 739b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return cmdinfo.status; 740b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 741b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 742b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd, 743b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 *result) 744b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 745e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT); 746b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 747b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 748b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id) 749b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 750b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 751b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command c; 752b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 753b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&c, 0, sizeof(c)); 754b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.delete_queue.opcode = opcode; 755b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.delete_queue.qid = cpu_to_le16(id); 756b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 757b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox status = nvme_submit_admin_cmd(dev, &c, NULL); 758b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (status) 759b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EIO; 760b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 761b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 762b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 763b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_alloc_cq(struct nvme_dev *dev, u16 qid, 764b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq) 765b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 766b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 767b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command c; 768b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED; 769b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 770b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&c, 0, sizeof(c)); 771b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.opcode = nvme_admin_create_cq; 772b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr); 773b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.cqid = cpu_to_le16(qid); 774b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1); 775b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.cq_flags = cpu_to_le16(flags); 776b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector); 777b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 778b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox status = nvme_submit_admin_cmd(dev, &c, NULL); 779b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (status) 780b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EIO; 781b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 782b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 783b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 784b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_alloc_sq(struct nvme_dev *dev, u16 qid, 785b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq) 786b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 787b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 788b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command c; 789b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM; 790b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 791b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&c, 0, sizeof(c)); 792b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.opcode = nvme_admin_create_sq; 793b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr); 794b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.sqid = cpu_to_le16(qid); 795b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1); 796b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.sq_flags = cpu_to_le16(flags); 797b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.cqid = cpu_to_le16(qid); 798b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 799b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox status = nvme_submit_admin_cmd(dev, &c, NULL); 800b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (status) 801b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EIO; 802b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 803b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 804b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 805b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_cq(struct nvme_dev *dev, u16 cqid) 806b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 807b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid); 808b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 809b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 810b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_sq(struct nvme_dev *dev, u16 sqid) 811b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 812b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid); 813b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 814b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 815bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcoxstatic int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns, 816bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox dma_addr_t dma_addr) 817bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox{ 818bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox struct nvme_command c; 819bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox 820bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox memset(&c, 0, sizeof(c)); 821bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox c.identify.opcode = nvme_admin_identify; 822bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox c.identify.nsid = cpu_to_le32(nsid); 823bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox c.identify.prp1 = cpu_to_le64(dma_addr); 824bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox c.identify.cns = cpu_to_le32(cns); 825bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox 826bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox return nvme_submit_admin_cmd(dev, &c, NULL); 827bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox} 828bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox 829bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcoxstatic int nvme_get_features(struct nvme_dev *dev, unsigned fid, 830bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox unsigned dword11, dma_addr_t dma_addr, u32 *result) 831bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox{ 832bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox struct nvme_command c; 833bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox 834bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox memset(&c, 0, sizeof(c)); 835bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox c.features.opcode = nvme_admin_get_features; 836bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox c.features.prp1 = cpu_to_le64(dma_addr); 837bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox c.features.fid = cpu_to_le32(fid); 838bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox c.features.dword11 = cpu_to_le32(dword11); 839bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox 840bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox return nvme_submit_admin_cmd(dev, &c, result); 841bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox} 842bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox 843b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_free_queue(struct nvme_dev *dev, int qid) 844b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 845b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq = dev->queues[qid]; 846aba2080f3f1639f9202f1a52993669844abcfb80Matthew Wilcox int vector = dev->entry[nvmeq->cq_vector].vector; 847b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 848aba2080f3f1639f9202f1a52993669844abcfb80Matthew Wilcox irq_set_affinity_hint(vector, NULL); 849aba2080f3f1639f9202f1a52993669844abcfb80Matthew Wilcox free_irq(vector, nvmeq); 850b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 851b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox /* Don't tell the adapter to delete the admin queue */ 852b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (qid) { 853b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox adapter_delete_sq(dev, qid); 854b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox adapter_delete_cq(dev, qid); 855b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 856b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 857b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth), 858b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox (void *)nvmeq->cqes, nvmeq->cq_dma_addr); 859b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth), 860b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_cmds, nvmeq->sq_dma_addr); 861b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(nvmeq); 862b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 863b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 864b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid, 865b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int depth, int vector) 866b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 867b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct device *dmadev = &dev->pci_dev->dev; 868e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox unsigned extra = (depth / 8) + (depth * sizeof(struct nvme_cmd_info)); 869b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL); 870b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!nvmeq) 871b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 872b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 873b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth), 874b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox &nvmeq->cq_dma_addr, GFP_KERNEL); 875b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!nvmeq->cqes) 876b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free_nvmeq; 877b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth)); 878b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 879b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth), 880b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox &nvmeq->sq_dma_addr, GFP_KERNEL); 881b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!nvmeq->sq_cmds) 882b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free_cqdma; 883b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 884b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->q_dmadev = dmadev; 885091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox nvmeq->dev = dev; 886b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox spin_lock_init(&nvmeq->q_lock); 887b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cq_head = 0; 888821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox nvmeq->cq_phase = 1; 889b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox init_waitqueue_head(&nvmeq->sq_full); 8901fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread); 891b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_list_init(&nvmeq->sq_cong); 892b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->q_db = &dev->dbs[qid * 2]; 893b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->q_depth = depth; 894b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cq_vector = vector; 895b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 896b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return nvmeq; 897b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 898b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_cqdma: 899b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(dmadev, CQ_SIZE(nvmeq->q_depth), (void *)nvmeq->cqes, 900b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cq_dma_addr); 901b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_nvmeq: 902b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(nvmeq); 903b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 904b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 905b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9063001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcoxstatic int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq, 9073001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox const char *name) 9083001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox{ 90958ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox if (use_threaded_interrupts) 91058ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector, 911ec6ce618d65b5ce1bef83a5509255107a0feac44Matthew Wilcox nvme_irq_check, nvme_irq, 91258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox IRQF_DISABLED | IRQF_SHARED, 91358ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox name, nvmeq); 9143001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq, 9153001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox IRQF_DISABLED | IRQF_SHARED, name, nvmeq); 9163001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox} 9173001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox 918b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic __devinit struct nvme_queue *nvme_create_queue(struct nvme_dev *dev, 919b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int qid, int cq_size, int vector) 920b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 921b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int result; 922b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq = nvme_alloc_queue(dev, qid, cq_size, vector); 923b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9243f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox if (!nvmeq) 9256f0f54499f2edf7e25410cdd99e6f030f3485fd1Matthew Wilcox return ERR_PTR(-ENOMEM); 9263f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox 927b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = adapter_alloc_cq(dev, qid, nvmeq); 928b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result < 0) 929b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free_nvmeq; 930b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 931b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = adapter_alloc_sq(dev, qid, nvmeq); 932b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result < 0) 933b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto release_cq; 934b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9353001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox result = queue_request_irq(dev, nvmeq, "nvme"); 936b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result < 0) 937b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto release_sq; 938b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 939b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return nvmeq; 940b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 941b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox release_sq: 942b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox adapter_delete_sq(dev, qid); 943b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox release_cq: 944b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox adapter_delete_cq(dev, qid); 945b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_nvmeq: 946b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth), 947b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox (void *)nvmeq->cqes, nvmeq->cq_dma_addr); 948b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth), 949b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_cmds, nvmeq->sq_dma_addr); 950b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(nvmeq); 9516f0f54499f2edf7e25410cdd99e6f030f3485fd1Matthew Wilcox return ERR_PTR(result); 952b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 953b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 954b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_configure_admin_queue(struct nvme_dev *dev) 955b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 956b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int result; 957b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 aqa; 95822605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox u64 cap; 95922605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox unsigned long timeout; 960b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq; 961b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 962b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->dbs = ((void __iomem *)dev->bar) + 4096; 963b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 964b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq = nvme_alloc_queue(dev, 0, 64, 0); 9653f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox if (!nvmeq) 9663f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox return -ENOMEM; 967b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 968b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox aqa = nvmeq->q_depth - 1; 969b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox aqa |= aqa << 16; 970b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 971b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM; 972b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT; 973b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE; 9747f53f9d2424533256ae86f7df5661a17de743de8Matthew Wilcox dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES; 975b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9765911f20039ce59d7e7834f0c42151cf759b6f786Shane Michael Matthews writel(0, &dev->bar->cc); 977b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writel(aqa, &dev->bar->aqa); 978b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writeq(nvmeq->sq_dma_addr, &dev->bar->asq); 979b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writeq(nvmeq->cq_dma_addr, &dev->bar->acq); 980b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writel(dev->ctrl_config, &dev->bar->cc); 981b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 98222605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox cap = readq(&dev->bar->cap); 98322605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies; 98422605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox 985b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox while (!(readl(&dev->bar->csts) & NVME_CSTS_RDY)) { 986b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox msleep(100); 987b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (fatal_signal_pending(current)) 988b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EINTR; 98922605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox if (time_after(jiffies, timeout)) { 99022605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox dev_err(&dev->pci_dev->dev, 99122605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox "Device not ready; aborting initialisation\n"); 99222605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox return -ENODEV; 99322605f96810d073eb74051d0295b6577d6a6a563Matthew Wilcox } 994b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 995b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9963001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox result = queue_request_irq(dev, nvmeq, "nvme admin"); 997b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->queues[0] = nvmeq; 998b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return result; 999b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1000b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 10017fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcoxstatic int nvme_map_user_pages(struct nvme_dev *dev, int write, 10027fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox unsigned long addr, unsigned length, 10037fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox struct scatterlist **sgp) 1004b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 100536c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox int i, err, count, nents, offset; 10067fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox struct scatterlist *sg; 10077fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox struct page **pages; 100836c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox 100936c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox if (addr & 3) 101036c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox return -EINVAL; 10117fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox if (!length) 10127fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox return -EINVAL; 10137fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox 101436c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox offset = offset_in_page(addr); 10157fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox count = DIV_ROUND_UP(offset + length, PAGE_SIZE); 10167fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox pages = kcalloc(count, sizeof(*pages), GFP_KERNEL); 101736c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox 101836c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox err = get_user_pages_fast(addr, count, 1, pages); 101936c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox if (err < count) { 102036c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox count = err; 102136c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox err = -EFAULT; 102236c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox goto put_pages; 102336c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox } 10247fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox 10257fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox sg = kcalloc(count, sizeof(*sg), GFP_KERNEL); 102636c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox sg_init_table(sg, count); 1027d0ba1e497bca83a3d353eb47c9658afc54d83228Matthew Wilcox for (i = 0; i < count; i++) { 1028d0ba1e497bca83a3d353eb47c9658afc54d83228Matthew Wilcox sg_set_page(&sg[i], pages[i], 1029d0ba1e497bca83a3d353eb47c9658afc54d83228Matthew Wilcox min_t(int, length, PAGE_SIZE - offset), offset); 1030d0ba1e497bca83a3d353eb47c9658afc54d83228Matthew Wilcox length -= (PAGE_SIZE - offset); 1031d0ba1e497bca83a3d353eb47c9658afc54d83228Matthew Wilcox offset = 0; 10327fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox } 10337fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox 10347fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox err = -ENOMEM; 10357fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox nents = dma_map_sg(&dev->pci_dev->dev, sg, count, 10367fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox write ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 103736c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox if (!nents) 103836c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox goto put_pages; 1039b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 10407fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox kfree(pages); 10417fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox *sgp = sg; 10427fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox return nents; 1043b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 10447fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox put_pages: 10457fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox for (i = 0; i < count; i++) 10467fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox put_page(pages[i]); 10477fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox kfree(pages); 10487fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox return err; 10497fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox} 1050b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 10517fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcoxstatic void nvme_unmap_user_pages(struct nvme_dev *dev, int write, 1052d1a490e026efb22851ed60588b5fad1281d80ec3Nisheeth Bhat unsigned long addr, int length, struct scatterlist *sg) 10537fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox{ 10547fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox int i, count; 1055b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 10567fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox count = DIV_ROUND_UP(offset_in_page(addr) + length, PAGE_SIZE); 1057d1a490e026efb22851ed60588b5fad1281d80ec3Nisheeth Bhat dma_unmap_sg(&dev->pci_dev->dev, sg, count, DMA_FROM_DEVICE); 10587fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox 105936c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox for (i = 0; i < count; i++) 10607fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox put_page(sg_page(&sg[i])); 10617fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox} 1062b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1063a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcoxstatic int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) 1064a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox{ 1065a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct nvme_dev *dev = ns->dev; 1066a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct nvme_queue *nvmeq; 1067a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct nvme_user_io io; 1068a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct nvme_command c; 1069a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox unsigned length; 1070a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox int nents, status; 1071a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct scatterlist *sg; 1072e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews struct nvme_prps *prps; 1073a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 1074a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox if (copy_from_user(&io, uio, sizeof(io))) 1075a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox return -EFAULT; 10766c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox length = (io.nblocks + 1) << ns->lba_shift; 10776c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox 10786c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox switch (io.opcode) { 10796c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox case nvme_cmd_write: 10806c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox case nvme_cmd_read: 10816bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox case nvme_cmd_compare: 10826c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox nents = nvme_map_user_pages(dev, io.opcode & 1, io.addr, 10836c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox length, &sg); 10846413214c5d424fd5aae6567848340f962ad2ce0fMatthew Wilcox break; 10856c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox default: 10866bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox return -EINVAL; 10876c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox } 10886c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox 1089a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox if (nents < 0) 1090a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox return nents; 1091a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 1092a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox memset(&c, 0, sizeof(c)); 1093a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.opcode = io.opcode; 1094a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.flags = io.flags; 10956c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox c.rw.nsid = cpu_to_le32(ns->ns_id); 1096a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.slba = cpu_to_le64(io.slba); 10976c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox c.rw.length = cpu_to_le16(io.nblocks); 1098a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.control = cpu_to_le16(io.control); 1099a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.dsmgmt = cpu_to_le16(io.dsmgmt); 11006c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox c.rw.reftag = io.reftag; 11016c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox c.rw.apptag = io.apptag; 11026c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox c.rw.appmask = io.appmask; 1103a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox /* XXX: metadata */ 1104b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox prps = nvme_setup_prps(dev, &c.common, sg, &length, GFP_KERNEL); 1105a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 1106d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox nvmeq = get_nvmeq(ns); 1107fa92282149842645931580225647238428374758Matthew Wilcox /* 1108fa92282149842645931580225647238428374758Matthew Wilcox * Since nvme_submit_sync_cmd sleeps, we can't keep preemption 1109b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox * disabled. We may be preempted at any point, and be rescheduled 1110b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox * to a different CPU. That will cause cacheline bouncing, but no 1111b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox * additional races since q_lock already protects against other CPUs. 1112b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox */ 1113a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox put_nvmeq(nvmeq); 1114b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox if (length != (io.nblocks + 1) << ns->lba_shift) 1115b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox status = -ENOMEM; 1116b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox else 1117b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox status = nvme_submit_sync_cmd(nvmeq, &c, NULL, IO_TIMEOUT); 1118a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 1119d1a490e026efb22851ed60588b5fad1281d80ec3Nisheeth Bhat nvme_unmap_user_pages(dev, io.opcode & 1, io.addr, length, sg); 1120d567760c409f981d35fc755b51d5bf56a99a467bMatthew Wilcox nvme_free_prps(dev, prps); 1121a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox return status; 1122a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox} 1123a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 11246bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcoxstatic int nvme_user_admin_cmd(struct nvme_ns *ns, 11256bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox struct nvme_admin_cmd __user *ucmd) 11266ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox{ 11276ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox struct nvme_dev *dev = ns->dev; 11286bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox struct nvme_admin_cmd cmd; 11296ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox struct nvme_command c; 11306bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox int status, length, nents = 0; 11316ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox struct scatterlist *sg; 11326bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox struct nvme_prps *prps = NULL; 11336ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 11346bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox if (!capable(CAP_SYS_ADMIN)) 11356bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox return -EACCES; 11366bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox if (copy_from_user(&cmd, ucmd, sizeof(cmd))) 11376ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox return -EFAULT; 11386ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 11396ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox memset(&c, 0, sizeof(c)); 11406bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.opcode = cmd.opcode; 11416bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.flags = cmd.flags; 11426bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.nsid = cpu_to_le32(cmd.nsid); 11436bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.cdw2[0] = cpu_to_le32(cmd.cdw2); 11446bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.cdw2[1] = cpu_to_le32(cmd.cdw3); 11456bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.cdw10[0] = cpu_to_le32(cmd.cdw10); 11466bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.cdw10[1] = cpu_to_le32(cmd.cdw11); 11476bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.cdw10[2] = cpu_to_le32(cmd.cdw12); 11486bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.cdw10[3] = cpu_to_le32(cmd.cdw13); 11496bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.cdw10[4] = cpu_to_le32(cmd.cdw14); 11506bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.cdw10[5] = cpu_to_le32(cmd.cdw15); 11516bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox 11526bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox length = cmd.data_len; 11536bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox if (cmd.data_len) { 11546bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox nents = nvme_map_user_pages(dev, 1, cmd.addr, length, &sg); 11556bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox if (nents < 0) 11566bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox return nents; 11576bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox prps = nvme_setup_prps(dev, &c.common, sg, &length, GFP_KERNEL); 11586bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox } 11596bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox 11606bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox if (length != cmd.data_len) 1161b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox status = -ENOMEM; 1162b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox else 1163b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox status = nvme_submit_admin_cmd(dev, &c, NULL); 11646bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox if (cmd.data_len) { 1165d1a490e026efb22851ed60588b5fad1281d80ec3Nisheeth Bhat nvme_unmap_user_pages(dev, 0, cmd.addr, cmd.data_len, sg); 11666bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox nvme_free_prps(dev, prps); 11676bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox } 11686ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox return status; 11696ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox} 11706ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 1171b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, 1172b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long arg) 1173b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1174b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_ns *ns = bdev->bd_disk->private_data; 1175b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1176b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox switch (cmd) { 11776bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox case NVME_IOCTL_ID: 11786bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox return ns->ns_id; 11796bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox case NVME_IOCTL_ADMIN_CMD: 11806bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox return nvme_user_admin_cmd(ns, (void __user *)arg); 1181a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox case NVME_IOCTL_SUBMIT_IO: 1182a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox return nvme_submit_io(ns, (void __user *)arg); 1183b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox default: 1184b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -ENOTTY; 1185b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1186b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1187b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1188b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic const struct block_device_operations nvme_fops = { 1189b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .owner = THIS_MODULE, 1190b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .ioctl = nvme_ioctl, 11914948168280b269a514045766ddd872cfac5968e1Matthew Wilcox .compat_ioctl = nvme_ioctl, 1192b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 1193b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 11948de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcoxstatic void nvme_timeout_ios(struct nvme_queue *nvmeq) 11958de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox{ 11968de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox int depth = nvmeq->q_depth - 1; 11978de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); 11988de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox unsigned long now = jiffies; 11998de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox int cmdid; 12008de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox 12018de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) { 12028de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox unsigned long data; 12038de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox void *ptr; 12048de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox unsigned char handler; 12058de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox static struct nvme_completion cqe = { .status = cpu_to_le16(NVME_SC_ABORT_REQ) << 1, }; 12068de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox 12078de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox if (!time_after(now, info[cmdid].timeout)) 12088de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox continue; 12098de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox dev_warn(nvmeq->q_dmadev, "Timing out I/O %d\n", cmdid); 12108de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox data = cancel_cmdid(nvmeq, cmdid); 12118de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox handler = data & 3; 12128de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox ptr = (void *)(data & ~3UL); 12138de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox nvme_completions[handler](nvmeq, ptr, &cqe); 12148de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox } 12158de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox} 12168de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox 12171fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic void nvme_resubmit_bios(struct nvme_queue *nvmeq) 12181fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox{ 12191fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox while (bio_list_peek(&nvmeq->sq_cong)) { 12201fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox struct bio *bio = bio_list_pop(&nvmeq->sq_cong); 12211fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data; 12221fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox if (nvme_submit_bio_queue(nvmeq, ns, bio)) { 12231fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox bio_list_add_head(&nvmeq->sq_cong, bio); 12241fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox break; 12251fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox } 12263cb967c03926edd2c414082f4cc0feb7b372edaeMatthew Wilcox if (bio_list_empty(&nvmeq->sq_cong)) 12273cb967c03926edd2c414082f4cc0feb7b372edaeMatthew Wilcox remove_wait_queue(&nvmeq->sq_full, 12283cb967c03926edd2c414082f4cc0feb7b372edaeMatthew Wilcox &nvmeq->sq_cong_wait); 12291fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox } 12301fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox} 12311fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 12321fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic int nvme_kthread(void *data) 12331fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox{ 12341fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox struct nvme_dev *dev; 12351fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 12361fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox while (!kthread_should_stop()) { 12371fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox __set_current_state(TASK_RUNNING); 12381fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_lock(&dev_list_lock); 12391fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox list_for_each_entry(dev, &dev_list, node) { 12401fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox int i; 12411fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox for (i = 0; i < dev->queue_count; i++) { 12421fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox struct nvme_queue *nvmeq = dev->queues[i]; 1243740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox if (!nvmeq) 1244740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox continue; 12451fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_lock_irq(&nvmeq->q_lock); 12461fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox if (nvme_process_cq(nvmeq)) 12471fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox printk("process_cq did something\n"); 12488de055350fbaa96b6563892c195a60be583faa9cMatthew Wilcox nvme_timeout_ios(nvmeq); 12491fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox nvme_resubmit_bios(nvmeq); 12501fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_unlock_irq(&nvmeq->q_lock); 12511fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox } 12521fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox } 12531fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_unlock(&dev_list_lock); 12541fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox set_current_state(TASK_INTERRUPTIBLE); 12551fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox schedule_timeout(HZ); 12561fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox } 12571fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox return 0; 12581fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox} 12591fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 12605aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcoxstatic DEFINE_IDA(nvme_index_ida); 12615aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox 12625aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcoxstatic int nvme_get_ns_idx(void) 12635aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox{ 12645aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox int index, error; 12655aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox 12665aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox do { 12675aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox if (!ida_pre_get(&nvme_index_ida, GFP_KERNEL)) 12685aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox return -1; 12695aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox 12705aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox spin_lock(&dev_list_lock); 12715aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox error = ida_get_new(&nvme_index_ida, &index); 12725aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox spin_unlock(&dev_list_lock); 12735aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox } while (error == -EAGAIN); 12745aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox 12755aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox if (error) 12765aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox index = -1; 12775aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox return index; 12785aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox} 12795aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox 12805aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcoxstatic void nvme_put_ns_idx(int index) 12815aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox{ 12825aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox spin_lock(&dev_list_lock); 12835aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox ida_remove(&nvme_index_ida, index); 12845aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox spin_unlock(&dev_list_lock); 12855aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox} 12865aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox 12875aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcoxstatic struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, int nsid, 1288b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_id_ns *id, struct nvme_lba_range_type *rt) 1289b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1290b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_ns *ns; 1291b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct gendisk *disk; 1292b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int lbaf; 1293b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1294b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (rt->attributes & NVME_LBART_ATTRIB_HIDE) 1295b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 1296b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1297b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns = kzalloc(sizeof(*ns), GFP_KERNEL); 1298b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!ns) 1299b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 1300b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->queue = blk_alloc_queue(GFP_KERNEL); 1301b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!ns->queue) 1302b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto out_free_ns; 1303b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->queue->queue_flags = QUEUE_FLAG_DEFAULT | QUEUE_FLAG_NOMERGES | 1304b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox QUEUE_FLAG_NONROT | QUEUE_FLAG_DISCARD; 1305b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox blk_queue_make_request(ns->queue, nvme_make_request); 1306b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->dev = dev; 1307b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->queue->queuedata = ns; 1308b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1309b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk = alloc_disk(NVME_MINORS); 1310b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!disk) 1311b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto out_free_queue; 13125aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox ns->ns_id = nsid; 1313b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->disk = disk; 1314b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox lbaf = id->flbas & 0xf; 1315b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->lba_shift = id->lbaf[lbaf].ds; 1316b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1317b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->major = nvme_major; 1318b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->minors = NVME_MINORS; 13195aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox disk->first_minor = NVME_MINORS * nvme_get_ns_idx(); 1320b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->fops = &nvme_fops; 1321b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->private_data = ns; 1322b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->queue = ns->queue; 1323388f037f4e7f0a24bac6b1a24f144f5d939f58cfMatthew Wilcox disk->driverfs_dev = &dev->pci_dev->dev; 13245aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid); 1325b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9)); 1326b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1327b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return ns; 1328b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1329b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free_queue: 1330b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox blk_cleanup_queue(ns->queue); 1331b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free_ns: 1332b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(ns); 1333b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 1334b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1335b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1336b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_ns_free(struct nvme_ns *ns) 1337b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 13385aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox int index = ns->disk->first_minor / NVME_MINORS; 1339b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox put_disk(ns->disk); 13405aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox nvme_put_ns_idx(index); 1341b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox blk_cleanup_queue(ns->queue); 1342b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(ns); 1343b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1344b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1345b3b06812e199f248561ce7824a4a8a9cd573c05aMatthew Wilcoxstatic int set_queue_count(struct nvme_dev *dev, int count) 1346b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1347b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 1348b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 result; 1349b3b06812e199f248561ce7824a4a8a9cd573c05aMatthew Wilcox u32 q_count = (count - 1) | ((count - 1) << 16); 1350b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1351bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox status = nvme_get_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0, 1352bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox &result); 1353b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (status) 1354b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EIO; 1355b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return min(result & 0xffff, result >> 16) + 1; 1356b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1357b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1358b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_setup_io_queues(struct nvme_dev *dev) 1359b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1360b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox int result, cpu, i, nr_io_queues; 1361b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1362b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox nr_io_queues = num_online_cpus(); 1363b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox result = set_queue_count(dev, nr_io_queues); 13641b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox if (result < 0) 13651b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox return result; 1366b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox if (result < nr_io_queues) 1367b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox nr_io_queues = result; 1368b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 13691b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox /* Deregister the admin queue's interrupt */ 13701b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox free_irq(dev->entry[0].vector, dev->queues[0]); 13711b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox 1372b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox for (i = 0; i < nr_io_queues; i++) 13731b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox dev->entry[i].entry = i; 13741b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox for (;;) { 1375b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox result = pci_enable_msix(dev->pci_dev, dev->entry, 1376b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox nr_io_queues); 13771b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox if (result == 0) { 13781b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox break; 13791b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } else if (result > 0) { 1380b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox nr_io_queues = result; 13811b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox continue; 13821b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } else { 1383b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox nr_io_queues = 1; 13841b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox break; 13851b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } 13861b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } 13871b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox 13881b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox result = queue_request_irq(dev, dev->queues[0], "nvme admin"); 13891b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox /* XXX: handle failure here */ 13901b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox 13911b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox cpu = cpumask_first(cpu_online_mask); 1392b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox for (i = 0; i < nr_io_queues; i++) { 13931b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu)); 13941b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox cpu = cpumask_next(cpu, cpu_online_mask); 13951b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } 13961b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox 1397b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox for (i = 0; i < nr_io_queues; i++) { 13981b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox dev->queues[i + 1] = nvme_create_queue(dev, i + 1, 13991b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox NVME_Q_DEPTH, i); 14006f0f54499f2edf7e25410cdd99e6f030f3485fd1Matthew Wilcox if (IS_ERR(dev->queues[i + 1])) 14016f0f54499f2edf7e25410cdd99e6f030f3485fd1Matthew Wilcox return PTR_ERR(dev->queues[i + 1]); 14021b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox dev->queue_count++; 14031b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } 1404b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 14059ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox for (; i < num_possible_cpus(); i++) { 14069ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox int target = i % rounddown_pow_of_two(dev->queue_count - 1); 14079ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox dev->queues[i + 1] = dev->queues[target + 1]; 14089ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox } 14099ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox 1410b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 1411b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1412b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1413b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_free_queues(struct nvme_dev *dev) 1414b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1415b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int i; 1416b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1417b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox for (i = dev->queue_count - 1; i >= 0; i--) 1418b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_free_queue(dev, i); 1419b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1420b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1421b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_dev_add(struct nvme_dev *dev) 1422b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1423b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int res, nn, i; 1424b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_ns *ns, *next; 142551814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox struct nvme_id_ctrl *ctrl; 1426bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox struct nvme_id_ns *id_ns; 1427bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox void *mem; 1428b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_addr_t dma_addr; 1429b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1430b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox res = nvme_setup_io_queues(dev); 1431b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (res) 1432b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return res; 1433b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1434bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox mem = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr, 1435b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox GFP_KERNEL); 1436b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1437bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox res = nvme_identify(dev, 0, 1, dma_addr); 1438b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (res) { 1439b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox res = -EIO; 1440b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto out_free; 1441b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1442b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1443bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox ctrl = mem; 144451814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox nn = le32_to_cpup(&ctrl->nn); 144551814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn)); 144651814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn)); 144751814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr)); 1448b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1449bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox id_ns = mem; 1450ac88c36a385b848cb9efcb877fdfc4153a60bcabMatthew Wilcox for (i = 0; i <= nn; i++) { 1451bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox res = nvme_identify(dev, i, 0, dma_addr); 1452b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (res) 1453b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox continue; 1454b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1455bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox if (id_ns->ncap == 0) 1456b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox continue; 1457b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1458bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i, 1459bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox dma_addr + 4096, NULL); 1460b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (res) 1461b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox continue; 1462b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1463bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox ns = nvme_alloc_ns(dev, i, mem, mem + 4096); 1464b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (ns) 1465b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_add_tail(&ns->list, &dev->namespaces); 1466b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1467b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_for_each_entry(ns, &dev->namespaces, list) 1468b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox add_disk(ns->disk); 1469b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1470bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox goto out; 1471b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1472b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free: 1473b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_for_each_entry_safe(ns, next, &dev->namespaces, list) { 1474b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_del(&ns->list); 1475b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_ns_free(ns); 1476b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1477b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1478bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox out: 1479684f5c2025b067a23722e620d0b3b858d8dc5d01Matthew Wilcox dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr); 1480b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return res; 1481b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1482b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1483b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_dev_remove(struct nvme_dev *dev) 1484b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1485b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_ns *ns, *next; 1486b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 14871fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_lock(&dev_list_lock); 14881fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox list_del(&dev->node); 14891fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_unlock(&dev_list_lock); 14901fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 1491b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox /* TODO: wait all I/O finished or cancel them */ 1492b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1493b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_for_each_entry_safe(ns, next, &dev->namespaces, list) { 1494b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_del(&ns->list); 1495b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox del_gendisk(ns->disk); 1496b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_ns_free(ns); 1497b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1498b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1499b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_free_queues(dev); 1500b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1501b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 1502b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1503b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1504091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcoxstatic int nvme_setup_prp_pools(struct nvme_dev *dev) 1505091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox{ 1506091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox struct device *dmadev = &dev->pci_dev->dev; 1507091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox dev->prp_page_pool = dma_pool_create("prp list page", dmadev, 1508091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox PAGE_SIZE, PAGE_SIZE, 0); 1509091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox if (!dev->prp_page_pool) 1510091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox return -ENOMEM; 1511091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox 151299802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox /* Optimisation for I/Os between 4k and 128k */ 151399802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox dev->prp_small_pool = dma_pool_create("prp list 256", dmadev, 151499802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox 256, 256, 0); 151599802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox if (!dev->prp_small_pool) { 151699802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox dma_pool_destroy(dev->prp_page_pool); 151799802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox return -ENOMEM; 151899802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox } 1519091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox return 0; 1520091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox} 1521091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox 1522091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcoxstatic void nvme_release_prp_pools(struct nvme_dev *dev) 1523091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox{ 1524091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox dma_pool_destroy(dev->prp_page_pool); 152599802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox dma_pool_destroy(dev->prp_small_pool); 1526091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox} 1527091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox 1528b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* XXX: Use an ida or something to let remove / add work correctly */ 1529b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_set_instance(struct nvme_dev *dev) 1530b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1531b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox static int instance; 1532b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->instance = instance++; 1533b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1534b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1535b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_release_instance(struct nvme_dev *dev) 1536b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1537b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1538b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1539b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __devinit nvme_probe(struct pci_dev *pdev, 1540b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox const struct pci_device_id *id) 1541b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1542574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox int bars, result = -ENOMEM; 1543b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_dev *dev; 1544b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1545b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1546b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!dev) 1547b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -ENOMEM; 1548b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry), 1549b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox GFP_KERNEL); 1550b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!dev->entry) 1551b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free; 15521b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *), 15531b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox GFP_KERNEL); 1554b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!dev->queues) 1555b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free; 1556b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 15570ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews if (pci_enable_device_mem(pdev)) 15580ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews goto free; 1559f64d3365a3e5cb46e69db7e2c82a7cb9a5bed1b8Matthew Wilcox pci_set_master(pdev); 1560574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox bars = pci_select_bars(pdev, IORESOURCE_MEM); 1561574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox if (pci_request_selected_regions(pdev, bars, "nvme")) 1562574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox goto disable; 15630ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews 1564b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox INIT_LIST_HEAD(&dev->namespaces); 1565b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->pci_dev = pdev; 1566b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox pci_set_drvdata(pdev, dev); 15672930353f9f2b9e4629e935acd970cb73c1171229Matthew Wilcox dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)); 15682930353f9f2b9e4629e935acd970cb73c1171229Matthew Wilcox dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64)); 1569b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_set_instance(dev); 157053c9577e9ca68a633c6e9df2b54eaecacfa77f62Matthew Wilcox dev->entry[0].vector = pdev->irq; 1571b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1572091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox result = nvme_setup_prp_pools(dev); 1573091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox if (result) 1574091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox goto disable_msix; 1575091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox 1576b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->bar = ioremap(pci_resource_start(pdev, 0), 8192); 1577b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!dev->bar) { 1578b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = -ENOMEM; 1579574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox goto disable_msix; 1580b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1581b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1582b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = nvme_configure_admin_queue(dev); 1583b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result) 1584b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto unmap; 1585b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->queue_count++; 1586b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 15871fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_lock(&dev_list_lock); 15881fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox list_add(&dev->node, &dev_list); 15891fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_unlock(&dev_list_lock); 15901fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 1591740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox result = nvme_dev_add(dev); 1592740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox if (result) 1593740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox goto delete; 1594740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox 1595b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 1596b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1597b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox delete: 1598740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox spin_lock(&dev_list_lock); 1599740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox list_del(&dev->node); 1600740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox spin_unlock(&dev_list_lock); 1601740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox 1602b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_free_queues(dev); 1603b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unmap: 1604b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox iounmap(dev->bar); 1605574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox disable_msix: 1606b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox pci_disable_msix(pdev); 1607b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_release_instance(dev); 1608091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox nvme_release_prp_pools(dev); 1609574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox disable: 16100ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews pci_disable_device(pdev); 1611574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox pci_release_regions(pdev); 1612b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free: 1613b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev->queues); 1614b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev->entry); 1615b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev); 1616b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return result; 1617b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1618b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1619b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void __devexit nvme_remove(struct pci_dev *pdev) 1620b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1621b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_dev *dev = pci_get_drvdata(pdev); 1622b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_dev_remove(dev); 1623b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox pci_disable_msix(pdev); 1624b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox iounmap(dev->bar); 1625b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_release_instance(dev); 1626091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox nvme_release_prp_pools(dev); 16270ee5a7d7cb9309bd393a25c395f19fb12a842602Shane Michael Matthews pci_disable_device(pdev); 1628574e8b95bc3780e10e9b5e9d51074d503dd3d5d9Matthew Wilcox pci_release_regions(pdev); 1629b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev->queues); 1630b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev->entry); 1631b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev); 1632b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1633b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1634b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* These functions are yet to be implemented */ 1635b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_error_detected NULL 1636b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_dump_registers NULL 1637b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_link_reset NULL 1638b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_slot_reset NULL 1639b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_error_resume NULL 1640b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_suspend NULL 1641b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_resume NULL 1642b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1643b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct pci_error_handlers nvme_err_handler = { 1644b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .error_detected = nvme_error_detected, 1645b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .mmio_enabled = nvme_dump_registers, 1646b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .link_reset = nvme_link_reset, 1647b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .slot_reset = nvme_slot_reset, 1648b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .resume = nvme_error_resume, 1649b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 1650b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1651b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* Move to pci_ids.h later */ 1652b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define PCI_CLASS_STORAGE_EXPRESS 0x010802 1653b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1654b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = { 1655b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) }, 1656b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox { 0, } 1657b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 1658b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_DEVICE_TABLE(pci, nvme_id_table); 1659b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1660b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct pci_driver nvme_driver = { 1661b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .name = "nvme", 1662b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .id_table = nvme_id_table, 1663b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .probe = nvme_probe, 1664b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .remove = __devexit_p(nvme_remove), 1665b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .suspend = nvme_suspend, 1666b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .resume = nvme_resume, 1667b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .err_handler = &nvme_err_handler, 1668b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 1669b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1670b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __init nvme_init(void) 1671b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 16721fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox int result = -EBUSY; 16731fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 16741fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox nvme_thread = kthread_run(nvme_kthread, NULL, "nvme"); 16751fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox if (IS_ERR(nvme_thread)) 16761fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox return PTR_ERR(nvme_thread); 1677b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1678b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_major = register_blkdev(nvme_major, "nvme"); 1679b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (nvme_major <= 0) 16801fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox goto kill_kthread; 1681b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1682b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = pci_register_driver(&nvme_driver); 16831fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox if (result) 16841fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox goto unregister_blkdev; 16851fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox return 0; 1686b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 16871fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox unregister_blkdev: 1688b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unregister_blkdev(nvme_major, "nvme"); 16891fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox kill_kthread: 16901fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox kthread_stop(nvme_thread); 1691b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return result; 1692b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1693b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1694b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void __exit nvme_exit(void) 1695b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1696b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox pci_unregister_driver(&nvme_driver); 1697b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unregister_blkdev(nvme_major, "nvme"); 16981fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox kthread_stop(nvme_thread); 1699b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1700b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1701b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>"); 1702b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_LICENSE("GPL"); 1703be5e09484078e95af20acb13e215cd8aec705893Matthew WilcoxMODULE_VERSION("0.6"); 1704b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_init(nvme_init); 1705b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_exit(nvme_exit); 1706