nvme-core.c revision 0877cb0d285c7f1d53d0b84b360bdea4be4f3f59
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> 39c3bfe7176c035a0a2c70bc79180fb13a6c57142aMatthew Wilcox#include <linux/ptrace.h> 40b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/sched.h> 41b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/slab.h> 42b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#include <linux/types.h> 435d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Verma#include <scsi/sg.h> 44797a796a13df6b84a4791e57306737059b5b2384Hitoshi Mitake#include <asm-generic/io-64-nonatomic-lo-hi.h> 45797a796a13df6b84a4791e57306737059b5b2384Hitoshi Mitake 46b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define NVME_Q_DEPTH 1024 47b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command)) 48b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion)) 49b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define NVME_MINORS 64 50e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox#define ADMIN_TIMEOUT (60 * HZ) 51b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 52b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_major; 53b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_param(nvme_major, int, 0); 54b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 5558ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxstatic int use_threaded_interrupts; 5658ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxmodule_param(use_threaded_interrupts, int, 0); 5758ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox 581fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic DEFINE_SPINLOCK(dev_list_lock); 591fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic LIST_HEAD(dev_list); 601fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic struct task_struct *nvme_thread; 611fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 62b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 63b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * An NVM Express queue. Each device has at least two (one for admin 64b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * commands and one for I/O commands). 65b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 66b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstruct nvme_queue { 67b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct device *q_dmadev; 68091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox struct nvme_dev *dev; 69b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox spinlock_t q_lock; 70b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command *sq_cmds; 71b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox volatile struct nvme_completion *cqes; 72b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_addr_t sq_dma_addr; 73b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_addr_t cq_dma_addr; 74b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox wait_queue_head_t sq_full; 751fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox wait_queue_t sq_cong_wait; 76b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct bio_list sq_cong; 77b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 __iomem *q_db; 78b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 q_depth; 79b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 cq_vector; 80b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 sq_head; 81b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 sq_tail; 82b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 cq_head; 83e9539f47525ecee05c9f22c3565885f3e9492c52Matthew Wilcox u8 cq_phase; 84e9539f47525ecee05c9f22c3565885f3e9492c52Matthew Wilcox u8 cqe_seen; 85b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long cmdid_data[]; 86b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 87b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 88b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 89b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Check we didin't inadvertently grow the command struct 90b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 91b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic inline void _nvme_check_size(void) 92b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 93b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64); 94b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64); 95b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64); 96b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64); 97b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_features) != 64); 98f8ebf8409abfdaeeb8c847381629a2a8b8e3d816Vishal Verma BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64); 99b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_command) != 64); 100b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096); 101b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096); 102b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64); 1036ecec74520d8a357726e6c12f99080dbe7b347ddKeith Busch BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512); 104b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 105b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1065c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcoxtypedef void (*nvme_completion_fn)(struct nvme_dev *, void *, 107c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox struct nvme_completion *); 108c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox 109e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstruct nvme_cmd_info { 110c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox nvme_completion_fn fn; 111c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox void *ctx; 112e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox unsigned long timeout; 113e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox}; 114e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox 115e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcoxstatic struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq) 116e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox{ 117e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)]; 118e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox} 119e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox 120b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/** 121714a7a22884b74862540bc84955274d86b2f6040Matthew Wilcox * alloc_cmdid() - Allocate a Command ID 122714a7a22884b74862540bc84955274d86b2f6040Matthew Wilcox * @nvmeq: The queue that will be used for this command 123714a7a22884b74862540bc84955274d86b2f6040Matthew Wilcox * @ctx: A pointer that will be passed to the handler 124c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox * @handler: The function to call on completion 125b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * 126b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Allocate a Command ID for a queue. The data passed in will 127b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * be passed to the completion handler. This is implemented by using 128b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * the bottom two bits of the ctx pointer to store the handler ID. 129b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Passing in a pointer that's not 4-byte aligned will cause a BUG. 130b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * We can change this if it becomes a problem. 131184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox * 132184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox * May be called with local interrupts disabled and the q_lock held, 133184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox * or with interrupts enabled and no locks held. 134b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 135c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcoxstatic int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx, 136c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox nvme_completion_fn handler, unsigned timeout) 137b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 138e6d15f79f997a98b3a69abbc462fc9041cc1a7b4Matthew Wilcox int depth = nvmeq->q_depth - 1; 139e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); 140b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int cmdid; 141b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 142b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox do { 143b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth); 144b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (cmdid >= depth) 145b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EBUSY; 146b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } while (test_and_set_bit(cmdid, nvmeq->cmdid_data)); 147b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 148c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox info[cmdid].fn = handler; 149c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox info[cmdid].ctx = ctx; 150e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox info[cmdid].timeout = jiffies + timeout; 151b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return cmdid; 152b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 153b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 154b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx, 155c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox nvme_completion_fn handler, unsigned timeout) 156b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 157b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int cmdid; 158b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox wait_event_killable(nvmeq->sq_full, 159e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0); 160b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return (cmdid < 0) ? -EINTR : cmdid; 161b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 162b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 163c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox/* Special values must be less than 0x1000 */ 164c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA) 165d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE) 166d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE) 167d2d8703481f60d67f49e3177196cbe474b11377cMatthew Wilcox#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE) 16800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox#define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE) 169be7b62754e097adc0cb16c25c9ee86ee20de62fbMatthew Wilcox 1705c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcoxstatic void special_completion(struct nvme_dev *dev, void *ctx, 171c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox struct nvme_completion *cqe) 172c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox{ 173c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox if (ctx == CMD_CTX_CANCELLED) 174c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox return; 175c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox if (ctx == CMD_CTX_FLUSH) 176c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox return; 177c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox if (ctx == CMD_CTX_COMPLETED) { 1785c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox dev_warn(&dev->pci_dev->dev, 179c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox "completed id %d twice on queue %d\n", 180c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox cqe->command_id, le16_to_cpup(&cqe->sq_id)); 181c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox return; 182c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox } 183c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox if (ctx == CMD_CTX_INVALID) { 1845c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox dev_warn(&dev->pci_dev->dev, 185c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox "invalid id %d completed on queue %d\n", 186c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox cqe->command_id, le16_to_cpup(&cqe->sq_id)); 187c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox return; 188c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox } 189c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox 1905c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox dev_warn(&dev->pci_dev->dev, "Unknown special completion %p\n", ctx); 191c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox} 192c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox 193184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox/* 194184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox * Called with local interrupts disabled and the q_lock held. May not sleep. 195184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox */ 196c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcoxstatic void *free_cmdid(struct nvme_queue *nvmeq, int cmdid, 197c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox nvme_completion_fn *fn) 198b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 199c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox void *ctx; 200e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); 201b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 202c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox if (cmdid >= nvmeq->q_depth) { 203c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox *fn = special_completion; 20448e3d39816416b3bf03dee3a796c0c04427c1a31Matthew Wilcox return CMD_CTX_INVALID; 205c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox } 206859361a228258edf4821d9f5635825033eca78e8Keith Busch if (fn) 207859361a228258edf4821d9f5635825033eca78e8Keith Busch *fn = info[cmdid].fn; 208c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox ctx = info[cmdid].ctx; 209c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox info[cmdid].fn = special_completion; 210e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox info[cmdid].ctx = CMD_CTX_COMPLETED; 211b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox clear_bit(cmdid, nvmeq->cmdid_data); 212b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox wake_up(&nvmeq->sq_full); 213c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox return ctx; 214b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 215b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 216c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcoxstatic void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid, 217c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox nvme_completion_fn *fn) 2183c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox{ 219c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox void *ctx; 220e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); 221c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox if (fn) 222c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox *fn = info[cmdid].fn; 223c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox ctx = info[cmdid].ctx; 224c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox info[cmdid].fn = special_completion; 225e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox info[cmdid].ctx = CMD_CTX_CANCELLED; 226c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox return ctx; 2273c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox} 2283c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox 2295d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Vermastruct nvme_queue *get_nvmeq(struct nvme_dev *dev) 230b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 231040a93b52a9eee8177ebaf2ba0ee0f9f518d1bf8Matthew Wilcox return dev->queues[get_cpu() + 1]; 232b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 233b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 2345d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Vermavoid put_nvmeq(struct nvme_queue *nvmeq) 235b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 2361b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox put_cpu(); 237b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 238b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 239b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/** 240714a7a22884b74862540bc84955274d86b2f6040Matthew Wilcox * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell 241b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @nvmeq: The queue to use 242b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * @cmd: The command to send 243b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * 244b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Safe to use from interrupt context 245b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 246b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd) 247b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 248b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long flags; 249b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 tail; 250b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox spin_lock_irqsave(&nvmeq->q_lock, flags); 251b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox tail = nvmeq->sq_tail; 252b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd)); 253b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (++tail == nvmeq->q_depth) 254b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox tail = 0; 2557547881d0951384f9833ec3a80fac8f3f16f3b98Matthew Wilcox writel(tail, nvmeq->q_db); 256b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_tail = tail; 257b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox spin_unlock_irqrestore(&nvmeq->q_lock, flags); 258b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 259b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 260b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 261b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 262eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcoxstatic __le64 **iod_list(struct nvme_iod *iod) 263e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews{ 264eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox return ((void *)iod) + iod->offset; 265e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews} 266e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews 267eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox/* 268eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox * Will slightly overestimate the number of pages needed. This is OK 269eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox * as it only leads to a small amount of wasted memory for the lifetime of 270eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox * the I/O. 271eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox */ 272eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcoxstatic int nvme_npages(unsigned size) 273eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox{ 274eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE); 275eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8); 276eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox} 277b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 278eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcoxstatic struct nvme_iod * 279eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcoxnvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp) 280b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 281eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) + 282eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox sizeof(__le64 *) * nvme_npages(nbytes) + 283eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox sizeof(struct scatterlist) * nseg, gfp); 284eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox 285eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox if (iod) { 286eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox iod->offset = offsetof(struct nvme_iod, sg[nseg]); 287eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox iod->npages = -1; 288eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox iod->length = nbytes; 2892b1960341576bf51c01b12fefeb1cc53820923e7Keith Busch iod->nents = 0; 2906198221fa0df0298513b35796f63f242ea97134eKeith Busch iod->start_time = jiffies; 291eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox } 292eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox 293eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox return iod; 294b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 295b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 2965d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Vermavoid nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod) 297b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 298eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox const int last_prp = PAGE_SIZE / 8 - 1; 299eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox int i; 300eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox __le64 **list = iod_list(iod); 301eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox dma_addr_t prp_dma = iod->first_dma; 302eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox 303eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox if (iod->npages == 0) 304eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox dma_pool_free(dev->prp_small_pool, list[0], prp_dma); 305eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox for (i = 0; i < iod->npages; i++) { 306eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox __le64 *prp_list = list[i]; 307eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]); 308eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox dma_pool_free(dev->prp_page_pool, prp_list, prp_dma); 309eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox prp_dma = next_prp_dma; 310eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox } 311eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox kfree(iod); 312b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 313b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 3146198221fa0df0298513b35796f63f242ea97134eKeith Buschstatic void nvme_start_io_acct(struct bio *bio) 3156198221fa0df0298513b35796f63f242ea97134eKeith Busch{ 3166198221fa0df0298513b35796f63f242ea97134eKeith Busch struct gendisk *disk = bio->bi_bdev->bd_disk; 3176198221fa0df0298513b35796f63f242ea97134eKeith Busch const int rw = bio_data_dir(bio); 3186198221fa0df0298513b35796f63f242ea97134eKeith Busch int cpu = part_stat_lock(); 3196198221fa0df0298513b35796f63f242ea97134eKeith Busch part_round_stats(cpu, &disk->part0); 3206198221fa0df0298513b35796f63f242ea97134eKeith Busch part_stat_inc(cpu, &disk->part0, ios[rw]); 3216198221fa0df0298513b35796f63f242ea97134eKeith Busch part_stat_add(cpu, &disk->part0, sectors[rw], bio_sectors(bio)); 3226198221fa0df0298513b35796f63f242ea97134eKeith Busch part_inc_in_flight(&disk->part0, rw); 3236198221fa0df0298513b35796f63f242ea97134eKeith Busch part_stat_unlock(); 3246198221fa0df0298513b35796f63f242ea97134eKeith Busch} 3256198221fa0df0298513b35796f63f242ea97134eKeith Busch 3266198221fa0df0298513b35796f63f242ea97134eKeith Buschstatic void nvme_end_io_acct(struct bio *bio, unsigned long start_time) 3276198221fa0df0298513b35796f63f242ea97134eKeith Busch{ 3286198221fa0df0298513b35796f63f242ea97134eKeith Busch struct gendisk *disk = bio->bi_bdev->bd_disk; 3296198221fa0df0298513b35796f63f242ea97134eKeith Busch const int rw = bio_data_dir(bio); 3306198221fa0df0298513b35796f63f242ea97134eKeith Busch unsigned long duration = jiffies - start_time; 3316198221fa0df0298513b35796f63f242ea97134eKeith Busch int cpu = part_stat_lock(); 3326198221fa0df0298513b35796f63f242ea97134eKeith Busch part_stat_add(cpu, &disk->part0, ticks[rw], duration); 3336198221fa0df0298513b35796f63f242ea97134eKeith Busch part_round_stats(cpu, &disk->part0); 3346198221fa0df0298513b35796f63f242ea97134eKeith Busch part_dec_in_flight(&disk->part0, rw); 3356198221fa0df0298513b35796f63f242ea97134eKeith Busch part_stat_unlock(); 3366198221fa0df0298513b35796f63f242ea97134eKeith Busch} 3376198221fa0df0298513b35796f63f242ea97134eKeith Busch 3385c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcoxstatic void bio_completion(struct nvme_dev *dev, void *ctx, 339b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_completion *cqe) 340b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 341eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox struct nvme_iod *iod = ctx; 342eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox struct bio *bio = iod->private; 343b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 status = le16_to_cpup(&cqe->status) >> 1; 344b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 3459e59d091b0eb04f223ed037348e3d9e36f30e72bKeith Busch if (iod->nents) { 3462b1960341576bf51c01b12fefeb1cc53820923e7Keith Busch dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents, 347b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 3489e59d091b0eb04f223ed037348e3d9e36f30e72bKeith Busch nvme_end_io_acct(bio, iod->start_time); 3499e59d091b0eb04f223ed037348e3d9e36f30e72bKeith Busch } 350eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox nvme_free_iod(dev, iod); 351427e97080196548557b288517537ab7eb48c309fKeith Busch if (status) 3521ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox bio_endio(bio, -EIO); 353427e97080196548557b288517537ab7eb48c309fKeith Busch else 3541ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox bio_endio(bio, 0); 355b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 356b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 357184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox/* length is in bytes. gfp flags indicates whether we may sleep. */ 3585d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Vermaint nvme_setup_prps(struct nvme_dev *dev, struct nvme_common_command *cmd, 3595d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Verma struct nvme_iod *iod, int total_len, gfp_t gfp) 360ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox{ 36199802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox struct dma_pool *pool; 362eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox int length = total_len; 363eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox struct scatterlist *sg = iod->sg; 364ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox int dma_len = sg_dma_len(sg); 365ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox u64 dma_addr = sg_dma_address(sg); 366ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox int offset = offset_in_page(dma_addr); 367e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews __le64 *prp_list; 368eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox __le64 **list = iod_list(iod); 369e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr_t prp_dma; 370eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox int nprps, i; 371ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 372ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmd->prp1 = cpu_to_le64(dma_addr); 373ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox length -= (PAGE_SIZE - offset); 374ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox if (length <= 0) 375eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox return total_len; 376ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 377ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox dma_len -= (PAGE_SIZE - offset); 378ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox if (dma_len) { 379ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox dma_addr += (PAGE_SIZE - offset); 380ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox } else { 381ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox sg = sg_next(sg); 382ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox dma_addr = sg_dma_address(sg); 383ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox dma_len = sg_dma_len(sg); 384ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox } 385ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 386ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox if (length <= PAGE_SIZE) { 387ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmd->prp2 = cpu_to_le64(dma_addr); 388eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox return total_len; 389e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews } 390e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews 391e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews nprps = DIV_ROUND_UP(length, PAGE_SIZE); 39299802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox if (nprps <= (256 / 8)) { 39399802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox pool = dev->prp_small_pool; 394eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox iod->npages = 0; 39599802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox } else { 39699802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox pool = dev->prp_page_pool; 397eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox iod->npages = 1; 39899802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox } 39999802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox 400b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox prp_list = dma_pool_alloc(pool, gfp, &prp_dma); 401b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox if (!prp_list) { 402b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox cmd->prp2 = cpu_to_le64(dma_addr); 403eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox iod->npages = -1; 404eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox return (total_len - length) + PAGE_SIZE; 405b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox } 406eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox list[0] = prp_list; 407eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox iod->first_dma = prp_dma; 408e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews cmd->prp2 = cpu_to_le64(prp_dma); 409e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews i = 0; 410e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews for (;;) { 4117523d834dd1573610078eb1ac0933f6490232f90Matthew Wilcox if (i == PAGE_SIZE / 8) { 412e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews __le64 *old_prp_list = prp_list; 413b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox prp_list = dma_pool_alloc(pool, gfp, &prp_dma); 414eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox if (!prp_list) 415eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox return total_len - length; 416eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox list[iod->npages++] = prp_list; 4177523d834dd1573610078eb1ac0933f6490232f90Matthew Wilcox prp_list[0] = old_prp_list[i - 1]; 4187523d834dd1573610078eb1ac0933f6490232f90Matthew Wilcox old_prp_list[i - 1] = cpu_to_le64(prp_dma); 4197523d834dd1573610078eb1ac0933f6490232f90Matthew Wilcox i = 1; 420e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews } 421e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews prp_list[i++] = cpu_to_le64(dma_addr); 422e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_len -= PAGE_SIZE; 423e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr += PAGE_SIZE; 424e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews length -= PAGE_SIZE; 425e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews if (length <= 0) 426e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews break; 427e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews if (dma_len > 0) 428e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews continue; 429e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews BUG_ON(dma_len < 0); 430e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews sg = sg_next(sg); 431e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_addr = sg_dma_address(sg); 432e025344c56e08b155f43ea09647969286c78377cShane Michael Matthews dma_len = sg_dma_len(sg); 433ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox } 434ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 435eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox return total_len; 436ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox} 437ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox 438427e97080196548557b288517537ab7eb48c309fKeith Buschstruct nvme_bio_pair { 439427e97080196548557b288517537ab7eb48c309fKeith Busch struct bio b1, b2, *parent; 440427e97080196548557b288517537ab7eb48c309fKeith Busch struct bio_vec *bv1, *bv2; 441427e97080196548557b288517537ab7eb48c309fKeith Busch int err; 442427e97080196548557b288517537ab7eb48c309fKeith Busch atomic_t cnt; 443427e97080196548557b288517537ab7eb48c309fKeith Busch}; 444427e97080196548557b288517537ab7eb48c309fKeith Busch 445427e97080196548557b288517537ab7eb48c309fKeith Buschstatic void nvme_bio_pair_endio(struct bio *bio, int err) 446427e97080196548557b288517537ab7eb48c309fKeith Busch{ 447427e97080196548557b288517537ab7eb48c309fKeith Busch struct nvme_bio_pair *bp = bio->bi_private; 448427e97080196548557b288517537ab7eb48c309fKeith Busch 449427e97080196548557b288517537ab7eb48c309fKeith Busch if (err) 450427e97080196548557b288517537ab7eb48c309fKeith Busch bp->err = err; 451427e97080196548557b288517537ab7eb48c309fKeith Busch 452427e97080196548557b288517537ab7eb48c309fKeith Busch if (atomic_dec_and_test(&bp->cnt)) { 453427e97080196548557b288517537ab7eb48c309fKeith Busch bio_endio(bp->parent, bp->err); 4541b56749e541ad59068582f2a28297843e243b856Keith Busch kfree(bp->bv1); 4551b56749e541ad59068582f2a28297843e243b856Keith Busch kfree(bp->bv2); 456427e97080196548557b288517537ab7eb48c309fKeith Busch kfree(bp); 457427e97080196548557b288517537ab7eb48c309fKeith Busch } 458427e97080196548557b288517537ab7eb48c309fKeith Busch} 459427e97080196548557b288517537ab7eb48c309fKeith Busch 460427e97080196548557b288517537ab7eb48c309fKeith Buschstatic struct nvme_bio_pair *nvme_bio_split(struct bio *bio, int idx, 461427e97080196548557b288517537ab7eb48c309fKeith Busch int len, int offset) 462427e97080196548557b288517537ab7eb48c309fKeith Busch{ 463427e97080196548557b288517537ab7eb48c309fKeith Busch struct nvme_bio_pair *bp; 464427e97080196548557b288517537ab7eb48c309fKeith Busch 465427e97080196548557b288517537ab7eb48c309fKeith Busch BUG_ON(len > bio->bi_size); 466427e97080196548557b288517537ab7eb48c309fKeith Busch BUG_ON(idx > bio->bi_vcnt); 467427e97080196548557b288517537ab7eb48c309fKeith Busch 468427e97080196548557b288517537ab7eb48c309fKeith Busch bp = kmalloc(sizeof(*bp), GFP_ATOMIC); 469427e97080196548557b288517537ab7eb48c309fKeith Busch if (!bp) 470427e97080196548557b288517537ab7eb48c309fKeith Busch return NULL; 471427e97080196548557b288517537ab7eb48c309fKeith Busch bp->err = 0; 472427e97080196548557b288517537ab7eb48c309fKeith Busch 473427e97080196548557b288517537ab7eb48c309fKeith Busch bp->b1 = *bio; 474427e97080196548557b288517537ab7eb48c309fKeith Busch bp->b2 = *bio; 475427e97080196548557b288517537ab7eb48c309fKeith Busch 476427e97080196548557b288517537ab7eb48c309fKeith Busch bp->b1.bi_size = len; 477427e97080196548557b288517537ab7eb48c309fKeith Busch bp->b2.bi_size -= len; 478427e97080196548557b288517537ab7eb48c309fKeith Busch bp->b1.bi_vcnt = idx; 479427e97080196548557b288517537ab7eb48c309fKeith Busch bp->b2.bi_idx = idx; 480427e97080196548557b288517537ab7eb48c309fKeith Busch bp->b2.bi_sector += len >> 9; 481427e97080196548557b288517537ab7eb48c309fKeith Busch 482427e97080196548557b288517537ab7eb48c309fKeith Busch if (offset) { 483427e97080196548557b288517537ab7eb48c309fKeith Busch bp->bv1 = kmalloc(bio->bi_max_vecs * sizeof(struct bio_vec), 484427e97080196548557b288517537ab7eb48c309fKeith Busch GFP_ATOMIC); 485427e97080196548557b288517537ab7eb48c309fKeith Busch if (!bp->bv1) 486427e97080196548557b288517537ab7eb48c309fKeith Busch goto split_fail_1; 487427e97080196548557b288517537ab7eb48c309fKeith Busch 488427e97080196548557b288517537ab7eb48c309fKeith Busch bp->bv2 = kmalloc(bio->bi_max_vecs * sizeof(struct bio_vec), 489427e97080196548557b288517537ab7eb48c309fKeith Busch GFP_ATOMIC); 490427e97080196548557b288517537ab7eb48c309fKeith Busch if (!bp->bv2) 491427e97080196548557b288517537ab7eb48c309fKeith Busch goto split_fail_2; 492427e97080196548557b288517537ab7eb48c309fKeith Busch 493427e97080196548557b288517537ab7eb48c309fKeith Busch memcpy(bp->bv1, bio->bi_io_vec, 494427e97080196548557b288517537ab7eb48c309fKeith Busch bio->bi_max_vecs * sizeof(struct bio_vec)); 495427e97080196548557b288517537ab7eb48c309fKeith Busch memcpy(bp->bv2, bio->bi_io_vec, 496427e97080196548557b288517537ab7eb48c309fKeith Busch bio->bi_max_vecs * sizeof(struct bio_vec)); 497427e97080196548557b288517537ab7eb48c309fKeith Busch 498427e97080196548557b288517537ab7eb48c309fKeith Busch bp->b1.bi_io_vec = bp->bv1; 499427e97080196548557b288517537ab7eb48c309fKeith Busch bp->b2.bi_io_vec = bp->bv2; 500427e97080196548557b288517537ab7eb48c309fKeith Busch bp->b2.bi_io_vec[idx].bv_offset += offset; 501427e97080196548557b288517537ab7eb48c309fKeith Busch bp->b2.bi_io_vec[idx].bv_len -= offset; 502427e97080196548557b288517537ab7eb48c309fKeith Busch bp->b1.bi_io_vec[idx].bv_len = offset; 503427e97080196548557b288517537ab7eb48c309fKeith Busch bp->b1.bi_vcnt++; 504427e97080196548557b288517537ab7eb48c309fKeith Busch } else 505427e97080196548557b288517537ab7eb48c309fKeith Busch bp->bv1 = bp->bv2 = NULL; 506427e97080196548557b288517537ab7eb48c309fKeith Busch 507427e97080196548557b288517537ab7eb48c309fKeith Busch bp->b1.bi_private = bp; 508427e97080196548557b288517537ab7eb48c309fKeith Busch bp->b2.bi_private = bp; 509427e97080196548557b288517537ab7eb48c309fKeith Busch 510427e97080196548557b288517537ab7eb48c309fKeith Busch bp->b1.bi_end_io = nvme_bio_pair_endio; 511427e97080196548557b288517537ab7eb48c309fKeith Busch bp->b2.bi_end_io = nvme_bio_pair_endio; 512427e97080196548557b288517537ab7eb48c309fKeith Busch 513427e97080196548557b288517537ab7eb48c309fKeith Busch bp->parent = bio; 514427e97080196548557b288517537ab7eb48c309fKeith Busch atomic_set(&bp->cnt, 2); 515427e97080196548557b288517537ab7eb48c309fKeith Busch 516427e97080196548557b288517537ab7eb48c309fKeith Busch return bp; 517427e97080196548557b288517537ab7eb48c309fKeith Busch 518427e97080196548557b288517537ab7eb48c309fKeith Busch split_fail_2: 519427e97080196548557b288517537ab7eb48c309fKeith Busch kfree(bp->bv1); 520427e97080196548557b288517537ab7eb48c309fKeith Busch split_fail_1: 521427e97080196548557b288517537ab7eb48c309fKeith Busch kfree(bp); 522427e97080196548557b288517537ab7eb48c309fKeith Busch return NULL; 523427e97080196548557b288517537ab7eb48c309fKeith Busch} 524427e97080196548557b288517537ab7eb48c309fKeith Busch 525427e97080196548557b288517537ab7eb48c309fKeith Buschstatic int nvme_split_and_submit(struct bio *bio, struct nvme_queue *nvmeq, 526427e97080196548557b288517537ab7eb48c309fKeith Busch int idx, int len, int offset) 527427e97080196548557b288517537ab7eb48c309fKeith Busch{ 528427e97080196548557b288517537ab7eb48c309fKeith Busch struct nvme_bio_pair *bp = nvme_bio_split(bio, idx, len, offset); 529427e97080196548557b288517537ab7eb48c309fKeith Busch if (!bp) 530427e97080196548557b288517537ab7eb48c309fKeith Busch return -ENOMEM; 531427e97080196548557b288517537ab7eb48c309fKeith Busch 532427e97080196548557b288517537ab7eb48c309fKeith Busch if (bio_list_empty(&nvmeq->sq_cong)) 533427e97080196548557b288517537ab7eb48c309fKeith Busch add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait); 534427e97080196548557b288517537ab7eb48c309fKeith Busch bio_list_add(&nvmeq->sq_cong, &bp->b1); 535427e97080196548557b288517537ab7eb48c309fKeith Busch bio_list_add(&nvmeq->sq_cong, &bp->b2); 536427e97080196548557b288517537ab7eb48c309fKeith Busch 537427e97080196548557b288517537ab7eb48c309fKeith Busch return 0; 538427e97080196548557b288517537ab7eb48c309fKeith Busch} 539427e97080196548557b288517537ab7eb48c309fKeith Busch 5401ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox/* NVMe scatterlists require no holes in the virtual address */ 5411ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox#define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \ 5421ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE)) 5431ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox 544427e97080196548557b288517537ab7eb48c309fKeith Buschstatic int nvme_map_bio(struct nvme_queue *nvmeq, struct nvme_iod *iod, 545b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct bio *bio, enum dma_data_direction dma_dir, int psegs) 546b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 547768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox struct bio_vec *bvec, *bvprv = NULL; 548768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox struct scatterlist *sg = NULL; 549159b67d7aefe3902df91075be5d80943c1570aa8Keith Busch int i, length = 0, nsegs = 0, split_len = bio->bi_size; 550159b67d7aefe3902df91075be5d80943c1570aa8Keith Busch 551159b67d7aefe3902df91075be5d80943c1570aa8Keith Busch if (nvmeq->dev->stripe_size) 552159b67d7aefe3902df91075be5d80943c1570aa8Keith Busch split_len = nvmeq->dev->stripe_size - 553159b67d7aefe3902df91075be5d80943c1570aa8Keith Busch ((bio->bi_sector << 9) & (nvmeq->dev->stripe_size - 1)); 554b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 555eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox sg_init_table(iod->sg, psegs); 556b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_for_each_segment(bvec, bio, i) { 557768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox if (bvprv && BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) { 558768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox sg->length += bvec->bv_len; 559768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox } else { 5601ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox if (bvprv && BIOVEC_NOT_VIRT_MERGEABLE(bvprv, bvec)) 561427e97080196548557b288517537ab7eb48c309fKeith Busch return nvme_split_and_submit(bio, nvmeq, i, 562427e97080196548557b288517537ab7eb48c309fKeith Busch length, 0); 563427e97080196548557b288517537ab7eb48c309fKeith Busch 564eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox sg = sg ? sg + 1 : iod->sg; 565768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox sg_set_page(sg, bvec->bv_page, bvec->bv_len, 566768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox bvec->bv_offset); 567768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox nsegs++; 568768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox } 569159b67d7aefe3902df91075be5d80943c1570aa8Keith Busch 570159b67d7aefe3902df91075be5d80943c1570aa8Keith Busch if (split_len - length < bvec->bv_len) 571159b67d7aefe3902df91075be5d80943c1570aa8Keith Busch return nvme_split_and_submit(bio, nvmeq, i, split_len, 572159b67d7aefe3902df91075be5d80943c1570aa8Keith Busch split_len - length); 5731ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox length += bvec->bv_len; 574768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox bvprv = bvec; 575b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 576eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox iod->nents = nsegs; 577768308400f5b4ce665a072eb976a851978b7706eMatthew Wilcox sg_mark_end(sg); 578427e97080196548557b288517537ab7eb48c309fKeith Busch if (dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir) == 0) 5791ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox return -ENOMEM; 580427e97080196548557b288517537ab7eb48c309fKeith Busch 581159b67d7aefe3902df91075be5d80943c1570aa8Keith Busch BUG_ON(length != bio->bi_size); 5821ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox return length; 583b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 584b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 5850e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch/* 5860e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch * We reuse the small pool to allocate the 16-byte range here as it is not 5870e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch * worth having a special pool for these or additional cases to handle freeing 5880e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch * the iod. 5890e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch */ 5900e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Buschstatic int nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns, 5910e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch struct bio *bio, struct nvme_iod *iod, int cmdid) 5920e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch{ 5930e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch struct nvme_dsm_range *range; 5940e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; 5950e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch 5960e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC, 5970e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch &iod->first_dma); 5980e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch if (!range) 5990e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch return -ENOMEM; 6000e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch 6010e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch iod_list(iod)[0] = (__le64 *)range; 6020e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch iod->npages = 0; 6030e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch 6040e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch range->cattr = cpu_to_le32(0); 6050e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch range->nlb = cpu_to_le32(bio->bi_size >> ns->lba_shift); 606063cc6d5591ea9c0631b81ac5c7b829d99738b2fMatthew Wilcox range->slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_sector)); 6070e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch 6080e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch memset(cmnd, 0, sizeof(*cmnd)); 6090e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch cmnd->dsm.opcode = nvme_cmd_dsm; 6100e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch cmnd->dsm.command_id = cmdid; 6110e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch cmnd->dsm.nsid = cpu_to_le32(ns->ns_id); 6120e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma); 6130e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch cmnd->dsm.nr = 0; 6140e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD); 6150e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch 6160e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch if (++nvmeq->sq_tail == nvmeq->q_depth) 6170e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch nvmeq->sq_tail = 0; 6180e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch writel(nvmeq->sq_tail, nvmeq->q_db); 6190e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch 6200e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch return 0; 6210e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch} 6220e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch 62300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcoxstatic int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns, 62400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox int cmdid) 62500df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox{ 62600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; 62700df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 62800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox memset(cmnd, 0, sizeof(*cmnd)); 62900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox cmnd->common.opcode = nvme_cmd_flush; 63000df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox cmnd->common.command_id = cmdid; 63100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox cmnd->common.nsid = cpu_to_le32(ns->ns_id); 63200df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 63300df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox if (++nvmeq->sq_tail == nvmeq->q_depth) 63400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox nvmeq->sq_tail = 0; 63500df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox writel(nvmeq->sq_tail, nvmeq->q_db); 63600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 63700df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox return 0; 63800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox} 63900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 6405d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Vermaint nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns) 64100df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox{ 64200df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH, 643ff976d724a74e4522e9ca2de1fb37ac4520f454fMatthew Wilcox special_completion, NVME_IO_TIMEOUT); 64400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox if (unlikely(cmdid < 0)) 64500df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox return cmdid; 64600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 64700df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox return nvme_submit_flush(nvmeq, ns, cmdid); 64800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox} 64900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 650184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox/* 651184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox * Called with local interrupts disabled and the q_lock held. May not sleep. 652184d2944cb3b92a2e8e1733c59d1e531ad6e924aMatthew Wilcox */ 653b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns, 654b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct bio *bio) 655b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 656ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox struct nvme_command *cmnd; 657eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox struct nvme_iod *iod; 658b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox enum dma_data_direction dma_dir; 6591287dabd345f447bbe0f7a99fc95ab89bcfc0f5dWei Yongjun int cmdid, length, result; 660b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u16 control; 661b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 dsmgmt; 662b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int psegs = bio_phys_segments(ns->queue, bio); 663b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 66400df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox if ((bio->bi_rw & REQ_FLUSH) && psegs) { 66500df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox result = nvme_submit_flush_data(nvmeq, ns); 66600df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox if (result) 66700df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox return result; 66800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox } 66900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 6701287dabd345f447bbe0f7a99fc95ab89bcfc0f5dWei Yongjun result = -ENOMEM; 671eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox iod = nvme_alloc_iod(psegs, bio->bi_size, GFP_ATOMIC); 672eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox if (!iod) 673eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox goto nomem; 674eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox iod->private = bio; 675b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 676eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox result = -EBUSY; 677ff976d724a74e4522e9ca2de1fb37ac4520f454fMatthew Wilcox cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT); 678b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (unlikely(cmdid < 0)) 679eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox goto free_iod; 680b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 6810e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch if (bio->bi_rw & REQ_DISCARD) { 6820e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch result = nvme_submit_discard(nvmeq, ns, bio, iod, cmdid); 6830e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch if (result) 6840e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch goto free_cmdid; 6850e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch return result; 6860e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch } 68700df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox if ((bio->bi_rw & REQ_FLUSH) && !psegs) 68800df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox return nvme_submit_flush(nvmeq, ns, cmdid); 68900df5cb4eb927078850086f8becc3286a69ea12eMatthew Wilcox 690b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox control = 0; 691b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (bio->bi_rw & REQ_FUA) 692b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox control |= NVME_RW_FUA; 693b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD)) 694b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox control |= NVME_RW_LR; 695b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 696b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dsmgmt = 0; 697b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (bio->bi_rw & REQ_RAHEAD) 698b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH; 699b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 700ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; 701b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 702b8deb62cf271fa9381edc8cf52bcae2f0225c55aMatthew Wilcox memset(cmnd, 0, sizeof(*cmnd)); 703b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (bio_data_dir(bio)) { 704ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.opcode = nvme_cmd_write; 705b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_dir = DMA_TO_DEVICE; 706b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } else { 707ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.opcode = nvme_cmd_read; 708b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_dir = DMA_FROM_DEVICE; 709b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 710b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 711427e97080196548557b288517537ab7eb48c309fKeith Busch result = nvme_map_bio(nvmeq, iod, bio, dma_dir, psegs); 712427e97080196548557b288517537ab7eb48c309fKeith Busch if (result <= 0) 713859361a228258edf4821d9f5635825033eca78e8Keith Busch goto free_cmdid; 7141ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox length = result; 715b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 716ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.command_id = cmdid; 717ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.nsid = cpu_to_le32(ns->ns_id); 718eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox length = nvme_setup_prps(nvmeq->dev, &cmnd->common, iod, length, 719eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox GFP_ATOMIC); 720063cc6d5591ea9c0631b81ac5c7b829d99738b2fMatthew Wilcox cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_sector)); 7211ad2f8932a72bf375361727949ced2cb4e8cfcefMatthew Wilcox cmnd->rw.length = cpu_to_le16((length >> ns->lba_shift) - 1); 722ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.control = cpu_to_le16(control); 723ff22b54fda2078fc3cd1bcdcb7a5ce5d08fd6591Matthew Wilcox cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt); 724b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 7256198221fa0df0298513b35796f63f242ea97134eKeith Busch nvme_start_io_acct(bio); 726b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (++nvmeq->sq_tail == nvmeq->q_depth) 727b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_tail = 0; 7287547881d0951384f9833ec3a80fac8f3f16f3b98Matthew Wilcox writel(nvmeq->sq_tail, nvmeq->q_db); 729b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 7301974b1ae8852324a75fb8cfecbc7b758fd5a2c3cMatthew Wilcox return 0; 7311974b1ae8852324a75fb8cfecbc7b758fd5a2c3cMatthew Wilcox 732859361a228258edf4821d9f5635825033eca78e8Keith Busch free_cmdid: 733859361a228258edf4821d9f5635825033eca78e8Keith Busch free_cmdid(nvmeq, cmdid, NULL); 734eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox free_iod: 735eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox nvme_free_iod(nvmeq->dev, iod); 736eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox nomem: 737eeee322647a67c20d9277c5e02c42b2126ea74bcMatthew Wilcox return result; 738b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 739b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 740e9539f47525ecee05c9f22c3565885f3e9492c52Matthew Wilcoxstatic int nvme_process_cq(struct nvme_queue *nvmeq) 741b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 742821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox u16 head, phase; 743b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 744b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox head = nvmeq->cq_head; 745821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox phase = nvmeq->cq_phase; 746b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 747b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox for (;;) { 748c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox void *ctx; 749c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox nvme_completion_fn fn; 750b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_completion cqe = nvmeq->cqes[head]; 751821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox if ((le16_to_cpu(cqe.status) & 1) != phase) 752b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox break; 753b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_head = le16_to_cpu(cqe.sq_head); 754b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (++head == nvmeq->q_depth) { 755b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox head = 0; 756821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox phase = !phase; 757b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 758b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 759c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox ctx = free_cmdid(nvmeq, cqe.command_id, &fn); 7605c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcox fn(nvmeq->dev, ctx, &cqe); 761b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 762b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 763b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox /* If the controller ignores the cq head doorbell and continuously 764b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * writes to the queue, it is theoretically possible to wrap around 765b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * the queue twice and mistakenly return IRQ_NONE. Linux only 766b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * requires that 0.1% of your interrupts are handled, so this isn't 767b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * a big problem. 768b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 769821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox if (head == nvmeq->cq_head && phase == nvmeq->cq_phase) 770e9539f47525ecee05c9f22c3565885f3e9492c52Matthew Wilcox return 0; 771b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 772f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox writel(head, nvmeq->q_db + (1 << nvmeq->dev->db_stride)); 773b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cq_head = head; 774821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox nvmeq->cq_phase = phase; 775b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 776e9539f47525ecee05c9f22c3565885f3e9492c52Matthew Wilcox nvmeq->cqe_seen = 1; 777e9539f47525ecee05c9f22c3565885f3e9492c52Matthew Wilcox return 1; 778b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 779b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 7807d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcoxstatic void nvme_make_request(struct request_queue *q, struct bio *bio) 7817d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox{ 7827d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox struct nvme_ns *ns = q->queuedata; 7837d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox struct nvme_queue *nvmeq = get_nvmeq(ns->dev); 7847d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox int result = -EBUSY; 7857d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox 7867d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox spin_lock_irq(&nvmeq->q_lock); 7877d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox if (bio_list_empty(&nvmeq->sq_cong)) 7887d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox result = nvme_submit_bio_queue(nvmeq, ns, bio); 7897d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox if (unlikely(result)) { 7907d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox if (bio_list_empty(&nvmeq->sq_cong)) 7917d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait); 7927d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox bio_list_add(&nvmeq->sq_cong, bio); 7937d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox } 7947d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox 7957d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox nvme_process_cq(nvmeq); 7967d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox spin_unlock_irq(&nvmeq->q_lock); 7977d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox put_nvmeq(nvmeq); 7987d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox} 7997d8224574cbd2326a6be00f319f5f7597abec3f6Matthew Wilcox 800b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic irqreturn_t nvme_irq(int irq, void *data) 801b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 80258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox irqreturn_t result; 80358ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox struct nvme_queue *nvmeq = data; 80458ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox spin_lock(&nvmeq->q_lock); 805e9539f47525ecee05c9f22c3565885f3e9492c52Matthew Wilcox nvme_process_cq(nvmeq); 806e9539f47525ecee05c9f22c3565885f3e9492c52Matthew Wilcox result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE; 807e9539f47525ecee05c9f22c3565885f3e9492c52Matthew Wilcox nvmeq->cqe_seen = 0; 80858ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox spin_unlock(&nvmeq->q_lock); 80958ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox return result; 81058ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox} 81158ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox 81258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcoxstatic irqreturn_t nvme_irq_check(int irq, void *data) 81358ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox{ 81458ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox struct nvme_queue *nvmeq = data; 81558ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head]; 81658ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase) 81758ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox return IRQ_NONE; 81858ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox return IRQ_WAKE_THREAD; 81958ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox} 82058ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox 8213c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcoxstatic void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid) 8223c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox{ 8233c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox spin_lock_irq(&nvmeq->q_lock); 824c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox cancel_cmdid(nvmeq, cmdid, NULL); 8253c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox spin_unlock_irq(&nvmeq->q_lock); 8263c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox} 8273c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox 828c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcoxstruct sync_cmd_info { 829c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox struct task_struct *task; 830c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox u32 result; 831c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox int status; 832c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox}; 833c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox 8345c1281a3bf5655ec1b90db495da3a2b77826ba88Matthew Wilcoxstatic void sync_completion(struct nvme_dev *dev, void *ctx, 835c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox struct nvme_completion *cqe) 836c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox{ 837c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox struct sync_cmd_info *cmdinfo = ctx; 838c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox cmdinfo->result = le32_to_cpup(&cqe->result); 839c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox cmdinfo->status = le16_to_cpup(&cqe->status) >> 1; 840c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox wake_up_process(cmdinfo->task); 841c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox} 842c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox 843b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* 844b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * Returns 0 on success. If the result is negative, it's a Linux error code; 845b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox * if the result is positive, it's an NVM Express status code 846b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox */ 8475d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Vermaint nvme_submit_sync_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd, 8485d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Verma u32 *result, unsigned timeout) 849b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 850b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int cmdid; 851b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct sync_cmd_info cmdinfo; 852b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 853b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmdinfo.task = current; 854b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmdinfo.status = -EINTR; 855b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 856c2f5b65020869215814df03c3941dac9436f99fbMatthew Wilcox cmdid = alloc_cmdid_killable(nvmeq, &cmdinfo, sync_completion, 857e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox timeout); 858b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (cmdid < 0) 859b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return cmdid; 860b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox cmd->common.command_id = cmdid; 861b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 8623c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox set_current_state(TASK_KILLABLE); 8633c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox nvme_submit_cmd(nvmeq, cmd); 86478f8d2577bd79ce9d62f4d9e6d3b895bd1dd1d1dKeith Busch schedule_timeout(timeout); 865b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 8663c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox if (cmdinfo.status == -EINTR) { 8673c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox nvme_abort_command(nvmeq, cmdid); 8683c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox return -EINTR; 8693c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox } 8703c0cf138d7789feb3f335f6f1d24ad8fc8b3a23fMatthew Wilcox 871b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result) 872b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox *result = cmdinfo.result; 873b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 874b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return cmdinfo.status; 875b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 876b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 8775d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Vermaint nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd, 878b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 *result) 879b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 880e85248e516c550382ba33ca325c272a0ca397e44Matthew Wilcox return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT); 881b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 882b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 883b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id) 884b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 885b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 886b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command c; 887b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 888b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&c, 0, sizeof(c)); 889b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.delete_queue.opcode = opcode; 890b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.delete_queue.qid = cpu_to_le16(id); 891b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 892b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox status = nvme_submit_admin_cmd(dev, &c, NULL); 893b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (status) 894b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EIO; 895b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 896b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 897b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 898b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_alloc_cq(struct nvme_dev *dev, u16 qid, 899b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq) 900b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 901b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 902b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command c; 903b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED; 904b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 905b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&c, 0, sizeof(c)); 906b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.opcode = nvme_admin_create_cq; 907b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr); 908b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.cqid = cpu_to_le16(qid); 909b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1); 910b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.cq_flags = cpu_to_le16(flags); 911b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector); 912b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 913b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox status = nvme_submit_admin_cmd(dev, &c, NULL); 914b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (status) 915b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EIO; 916b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 917b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 918b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 919b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_alloc_sq(struct nvme_dev *dev, u16 qid, 920b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq) 921b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 922b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 923b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_command c; 924b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM; 925b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 926b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset(&c, 0, sizeof(c)); 927b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.opcode = nvme_admin_create_sq; 928b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr); 929b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.sqid = cpu_to_le16(qid); 930b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1); 931b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.sq_flags = cpu_to_le16(flags); 932b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox c.create_sq.cqid = cpu_to_le16(qid); 933b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 934b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox status = nvme_submit_admin_cmd(dev, &c, NULL); 935b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (status) 936b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -EIO; 937b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 938b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 939b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 940b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_cq(struct nvme_dev *dev, u16 cqid) 941b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 942b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid); 943b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 944b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 945b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int adapter_delete_sq(struct nvme_dev *dev, u16 sqid) 946b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 947b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid); 948b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 949b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 9505d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Vermaint nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns, 951bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox dma_addr_t dma_addr) 952bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox{ 953bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox struct nvme_command c; 954bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox 955bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox memset(&c, 0, sizeof(c)); 956bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox c.identify.opcode = nvme_admin_identify; 957bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox c.identify.nsid = cpu_to_le32(nsid); 958bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox c.identify.prp1 = cpu_to_le64(dma_addr); 959bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox c.identify.cns = cpu_to_le32(cns); 960bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox 961bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox return nvme_submit_admin_cmd(dev, &c, NULL); 962bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox} 963bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox 9645d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Vermaint nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid, 96508df1e05657fc6712e520e7c09cc6c86160ceb35Keith Busch dma_addr_t dma_addr, u32 *result) 966bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox{ 967bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox struct nvme_command c; 968bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox 969bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox memset(&c, 0, sizeof(c)); 970bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox c.features.opcode = nvme_admin_get_features; 971a42ceccef0c43b46ff6bc1b12a7c1076ef243df1Keith Busch c.features.nsid = cpu_to_le32(nsid); 972bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox c.features.prp1 = cpu_to_le64(dma_addr); 973bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox c.features.fid = cpu_to_le32(fid); 974bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox 97508df1e05657fc6712e520e7c09cc6c86160ceb35Keith Busch return nvme_submit_admin_cmd(dev, &c, result); 976df3481399042200792822b6243e36a95a557b57eMatthew Wilcox} 977df3481399042200792822b6243e36a95a557b57eMatthew Wilcox 9785d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Vermaint nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11, 9795d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Verma dma_addr_t dma_addr, u32 *result) 980df3481399042200792822b6243e36a95a557b57eMatthew Wilcox{ 981df3481399042200792822b6243e36a95a557b57eMatthew Wilcox struct nvme_command c; 982df3481399042200792822b6243e36a95a557b57eMatthew Wilcox 983df3481399042200792822b6243e36a95a557b57eMatthew Wilcox memset(&c, 0, sizeof(c)); 984df3481399042200792822b6243e36a95a557b57eMatthew Wilcox c.features.opcode = nvme_admin_set_features; 985df3481399042200792822b6243e36a95a557b57eMatthew Wilcox c.features.prp1 = cpu_to_le64(dma_addr); 986df3481399042200792822b6243e36a95a557b57eMatthew Wilcox c.features.fid = cpu_to_le32(fid); 987df3481399042200792822b6243e36a95a557b57eMatthew Wilcox c.features.dword11 = cpu_to_le32(dword11); 988df3481399042200792822b6243e36a95a557b57eMatthew Wilcox 989bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox return nvme_submit_admin_cmd(dev, &c, result); 990bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox} 991bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox 992a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox/** 993a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox * nvme_cancel_ios - Cancel outstanding I/Os 994a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox * @queue: The queue to cancel I/Os on 995a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox * @timeout: True to only cancel I/Os which have timed out 996a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox */ 997a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcoxstatic void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout) 998a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox{ 999a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox int depth = nvmeq->q_depth - 1; 1000a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); 1001a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox unsigned long now = jiffies; 1002a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox int cmdid; 1003a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox 1004a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) { 1005a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox void *ctx; 1006a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox nvme_completion_fn fn; 1007a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox static struct nvme_completion cqe = { 1008af2d9ca744af5e03390eeb3864d08ce75c860899Matthew Wilcox .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1), 1009a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox }; 1010a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox 1011a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox if (timeout && !time_after(now, info[cmdid].timeout)) 1012a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox continue; 1013053ab702cc2702f25a97ead087ed344b864785b7Keith Busch if (info[cmdid].ctx == CMD_CTX_CANCELLED) 1014053ab702cc2702f25a97ead087ed344b864785b7Keith Busch continue; 1015a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d\n", cmdid); 1016a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox ctx = cancel_cmdid(nvmeq, cmdid, &fn); 1017a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox fn(nvmeq->dev, ctx, &cqe); 1018a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox } 1019a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox} 1020a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox 10219e866774aab5d2654b0fa8f97890f68913f05700Matthew Wilcoxstatic void nvme_free_queue_mem(struct nvme_queue *nvmeq) 10229e866774aab5d2654b0fa8f97890f68913f05700Matthew Wilcox{ 10239e866774aab5d2654b0fa8f97890f68913f05700Matthew Wilcox dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth), 10249e866774aab5d2654b0fa8f97890f68913f05700Matthew Wilcox (void *)nvmeq->cqes, nvmeq->cq_dma_addr); 10259e866774aab5d2654b0fa8f97890f68913f05700Matthew Wilcox dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth), 10269e866774aab5d2654b0fa8f97890f68913f05700Matthew Wilcox nvmeq->sq_cmds, nvmeq->sq_dma_addr); 10279e866774aab5d2654b0fa8f97890f68913f05700Matthew Wilcox kfree(nvmeq); 10289e866774aab5d2654b0fa8f97890f68913f05700Matthew Wilcox} 10299e866774aab5d2654b0fa8f97890f68913f05700Matthew Wilcox 1030b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_free_queue(struct nvme_dev *dev, int qid) 1031b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1032b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq = dev->queues[qid]; 1033aba2080f3f1639f9202f1a52993669844abcfb80Matthew Wilcox int vector = dev->entry[nvmeq->cq_vector].vector; 1034b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1035a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox spin_lock_irq(&nvmeq->q_lock); 1036a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox nvme_cancel_ios(nvmeq, false); 10373295874b6074d749516d6decd43afad7bf6e38ffKeith Busch while (bio_list_peek(&nvmeq->sq_cong)) { 10383295874b6074d749516d6decd43afad7bf6e38ffKeith Busch struct bio *bio = bio_list_pop(&nvmeq->sq_cong); 10393295874b6074d749516d6decd43afad7bf6e38ffKeith Busch bio_endio(bio, -EIO); 10403295874b6074d749516d6decd43afad7bf6e38ffKeith Busch } 1041a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox spin_unlock_irq(&nvmeq->q_lock); 1042a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox 1043aba2080f3f1639f9202f1a52993669844abcfb80Matthew Wilcox irq_set_affinity_hint(vector, NULL); 1044aba2080f3f1639f9202f1a52993669844abcfb80Matthew Wilcox free_irq(vector, nvmeq); 1045b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1046b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox /* Don't tell the adapter to delete the admin queue */ 1047b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (qid) { 1048b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox adapter_delete_sq(dev, qid); 1049b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox adapter_delete_cq(dev, qid); 1050b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1051b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 10529e866774aab5d2654b0fa8f97890f68913f05700Matthew Wilcox nvme_free_queue_mem(nvmeq); 1053b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1054b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1055b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid, 1056b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int depth, int vector) 1057b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1058b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct device *dmadev = &dev->pci_dev->dev; 1059a0cadb85b8b758608ae0759151e29de7581c6731Keith Busch unsigned extra = DIV_ROUND_UP(depth, 8) + (depth * 1060a0cadb85b8b758608ae0759151e29de7581c6731Keith Busch sizeof(struct nvme_cmd_info)); 1061b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL); 1062b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!nvmeq) 1063b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 1064b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1065b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth), 1066b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox &nvmeq->cq_dma_addr, GFP_KERNEL); 1067b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!nvmeq->cqes) 1068b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free_nvmeq; 1069b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth)); 1070b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1071b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth), 1072b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox &nvmeq->sq_dma_addr, GFP_KERNEL); 1073b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!nvmeq->sq_cmds) 1074b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free_cqdma; 1075b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1076b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->q_dmadev = dmadev; 1077091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox nvmeq->dev = dev; 1078b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox spin_lock_init(&nvmeq->q_lock); 1079b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cq_head = 0; 1080821234603b265f59d7eebce16d9e8beca2a5752dMatthew Wilcox nvmeq->cq_phase = 1; 1081b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox init_waitqueue_head(&nvmeq->sq_full); 10821fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread); 1083b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox bio_list_init(&nvmeq->sq_cong); 1084f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox nvmeq->q_db = &dev->dbs[qid << (dev->db_stride + 1)]; 1085b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->q_depth = depth; 1086b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cq_vector = vector; 1087b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1088b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return nvmeq; 1089b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1090b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_cqdma: 109168b8eca5f882230e4991df0db0a3094f49420a90Keith Busch dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes, 1092b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->cq_dma_addr); 1093b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_nvmeq: 1094b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(nvmeq); 1095b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 1096b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1097b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 10983001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcoxstatic int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq, 10993001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox const char *name) 11003001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox{ 110158ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox if (use_threaded_interrupts) 110258ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector, 1103ec6ce618d65b5ce1bef83a5509255107a0feac44Matthew Wilcox nvme_irq_check, nvme_irq, 110458ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox IRQF_DISABLED | IRQF_SHARED, 110558ffacb545f76fc2c65d1fbfa5acf5184a2a09e6Matthew Wilcox name, nvmeq); 11063001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq, 11073001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox IRQF_DISABLED | IRQF_SHARED, name, nvmeq); 11083001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox} 11093001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox 11108d85fce77edfc22f1d6dbf78e3af723b4b556f3dGreg Kroah-Hartmanstatic struct nvme_queue *nvme_create_queue(struct nvme_dev *dev, int qid, 11118d85fce77edfc22f1d6dbf78e3af723b4b556f3dGreg Kroah-Hartman int cq_size, int vector) 1112b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1113b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int result; 1114b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq = nvme_alloc_queue(dev, qid, cq_size, vector); 1115b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 11163f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox if (!nvmeq) 11176f0f54499f2edf7e25410cdd99e6f030f3485fd1Matthew Wilcox return ERR_PTR(-ENOMEM); 11183f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox 1119b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = adapter_alloc_cq(dev, qid, nvmeq); 1120b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result < 0) 1121b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free_nvmeq; 1122b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1123b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = adapter_alloc_sq(dev, qid, nvmeq); 1124b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result < 0) 1125b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto release_cq; 1126b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 11273001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox result = queue_request_irq(dev, nvmeq, "nvme"); 1128b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result < 0) 1129b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto release_sq; 1130b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1131b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return nvmeq; 1132b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1133b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox release_sq: 1134b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox adapter_delete_sq(dev, qid); 1135b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox release_cq: 1136b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox adapter_delete_cq(dev, qid); 1137b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free_nvmeq: 1138b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth), 1139b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox (void *)nvmeq->cqes, nvmeq->cq_dma_addr); 1140b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth), 1141b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq->sq_cmds, nvmeq->sq_dma_addr); 1142b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(nvmeq); 11436f0f54499f2edf7e25410cdd99e6f030f3485fd1Matthew Wilcox return ERR_PTR(result); 1144b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1145b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1146ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcoxstatic int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled) 1147ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox{ 1148ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox unsigned long timeout; 1149ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox u32 bit = enabled ? NVME_CSTS_RDY : 0; 1150ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox 1151ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies; 1152ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox 1153ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) { 1154ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox msleep(100); 1155ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox if (fatal_signal_pending(current)) 1156ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox return -EINTR; 1157ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox if (time_after(jiffies, timeout)) { 1158ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox dev_err(&dev->pci_dev->dev, 1159ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox "Device not ready; aborting initialisation\n"); 1160ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox return -ENODEV; 1161ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox } 1162ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox } 1163ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox 1164ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox return 0; 1165ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox} 1166ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox 1167ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox/* 1168ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox * If the device has been passed off to us in an enabled state, just clear 1169ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox * the enabled bit. The spec says we should set the 'shutdown notification 1170ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox * bits', but doing so may cause the device to complete commands to the 1171ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox * admin queue ... and we don't know what memory that might be pointing at! 1172ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox */ 1173ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcoxstatic int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap) 1174ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox{ 117544af146a84fa4a8e136d824207dcd356958a112bMatthew Wilcox u32 cc = readl(&dev->bar->cc); 117644af146a84fa4a8e136d824207dcd356958a112bMatthew Wilcox 117744af146a84fa4a8e136d824207dcd356958a112bMatthew Wilcox if (cc & NVME_CC_ENABLE) 117844af146a84fa4a8e136d824207dcd356958a112bMatthew Wilcox writel(cc & ~NVME_CC_ENABLE, &dev->bar->cc); 1179ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox return nvme_wait_ready(dev, cap, false); 1180ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox} 1181ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox 1182ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcoxstatic int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap) 1183ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox{ 1184ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox return nvme_wait_ready(dev, cap, true); 1185ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox} 1186ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox 11878d85fce77edfc22f1d6dbf78e3af723b4b556f3dGreg Kroah-Hartmanstatic int nvme_configure_admin_queue(struct nvme_dev *dev) 1188b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1189ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox int result; 1190b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 aqa; 1191ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox u64 cap = readq(&dev->bar->cap); 1192b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_queue *nvmeq; 1193b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1194ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox result = nvme_disable_ctrl(dev, cap); 1195ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox if (result < 0) 1196ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox return result; 1197b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1198b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvmeq = nvme_alloc_queue(dev, 0, 64, 0); 11993f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox if (!nvmeq) 12003f85d50b609e8a5ef151656210203a6e94c19538Matthew Wilcox return -ENOMEM; 1201b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1202b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox aqa = nvmeq->q_depth - 1; 1203b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox aqa |= aqa << 16; 1204b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1205b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM; 1206b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT; 1207b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE; 12087f53f9d2424533256ae86f7df5661a17de743de8Matthew Wilcox dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES; 1209b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1210b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writel(aqa, &dev->bar->aqa); 1211b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writeq(nvmeq->sq_dma_addr, &dev->bar->asq); 1212b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writeq(nvmeq->cq_dma_addr, &dev->bar->acq); 1213b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox writel(dev->ctrl_config, &dev->bar->cc); 1214b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1215ba47e3865e8023e79b670793a41508222b5f0322Matthew Wilcox result = nvme_enable_ctrl(dev, cap); 1216025c557a71bd06a9f6d32259f00e2218b15bf0a4Keith Busch if (result) 1217025c557a71bd06a9f6d32259f00e2218b15bf0a4Keith Busch goto free_q; 12189e866774aab5d2654b0fa8f97890f68913f05700Matthew Wilcox 12193001082cac4bf6ffd09f72b39e6292ad6394ef17Matthew Wilcox result = queue_request_irq(dev, nvmeq, "nvme admin"); 1220025c557a71bd06a9f6d32259f00e2218b15bf0a4Keith Busch if (result) 1221025c557a71bd06a9f6d32259f00e2218b15bf0a4Keith Busch goto free_q; 1222025c557a71bd06a9f6d32259f00e2218b15bf0a4Keith Busch 1223b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->queues[0] = nvmeq; 1224b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return result; 1225025c557a71bd06a9f6d32259f00e2218b15bf0a4Keith Busch 1226025c557a71bd06a9f6d32259f00e2218b15bf0a4Keith Busch free_q: 1227025c557a71bd06a9f6d32259f00e2218b15bf0a4Keith Busch nvme_free_queue_mem(nvmeq); 1228025c557a71bd06a9f6d32259f00e2218b15bf0a4Keith Busch return result; 1229b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1230b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 12315d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Vermastruct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write, 1232eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox unsigned long addr, unsigned length) 1233b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 123436c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox int i, err, count, nents, offset; 12357fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox struct scatterlist *sg; 12367fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox struct page **pages; 1237eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox struct nvme_iod *iod; 123836c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox 123936c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox if (addr & 3) 1240eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox return ERR_PTR(-EINVAL); 12415460fc03105fbed01fe27aa572d9f65bb410a61dDan Carpenter if (!length || length > INT_MAX - PAGE_SIZE) 1242eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox return ERR_PTR(-EINVAL); 12437fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox 124436c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox offset = offset_in_page(addr); 12457fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox count = DIV_ROUND_UP(offset + length, PAGE_SIZE); 12467fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox pages = kcalloc(count, sizeof(*pages), GFP_KERNEL); 124722fff826e715e9727d3c7a69f15e602a9801b673Dan Carpenter if (!pages) 124822fff826e715e9727d3c7a69f15e602a9801b673Dan Carpenter return ERR_PTR(-ENOMEM); 124936c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox 125036c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox err = get_user_pages_fast(addr, count, 1, pages); 125136c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox if (err < count) { 125236c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox count = err; 125336c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox err = -EFAULT; 125436c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox goto put_pages; 125536c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox } 12567fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox 1257eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox iod = nvme_alloc_iod(count, length, GFP_KERNEL); 1258eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox sg = iod->sg; 125936c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox sg_init_table(sg, count); 1260d0ba1e497bca83a3d353eb47c9658afc54d83228Matthew Wilcox for (i = 0; i < count; i++) { 1261d0ba1e497bca83a3d353eb47c9658afc54d83228Matthew Wilcox sg_set_page(&sg[i], pages[i], 12625460fc03105fbed01fe27aa572d9f65bb410a61dDan Carpenter min_t(unsigned, length, PAGE_SIZE - offset), 12635460fc03105fbed01fe27aa572d9f65bb410a61dDan Carpenter offset); 1264d0ba1e497bca83a3d353eb47c9658afc54d83228Matthew Wilcox length -= (PAGE_SIZE - offset); 1265d0ba1e497bca83a3d353eb47c9658afc54d83228Matthew Wilcox offset = 0; 12667fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox } 1267fe304c43c6d63e29ed4fc46a874d7a74313788c5Matthew Wilcox sg_mark_end(&sg[i - 1]); 12681c2ad9faaf662b4a525348775deca3ac8e6c35a0Matthew Wilcox iod->nents = count; 12697fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox 12707fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox err = -ENOMEM; 12717fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox nents = dma_map_sg(&dev->pci_dev->dev, sg, count, 12727fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox write ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 127336c14ed9caa957c686d4a48fd598a5ec2aa0331bMatthew Wilcox if (!nents) 1274eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox goto free_iod; 1275b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 12767fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox kfree(pages); 1277eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox return iod; 1278b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1279eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox free_iod: 1280eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox kfree(iod); 12817fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox put_pages: 12827fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox for (i = 0; i < count; i++) 12837fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox put_page(pages[i]); 12847fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox kfree(pages); 1285eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox return ERR_PTR(err); 12867fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox} 1287b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 12885d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Vermavoid nvme_unmap_user_pages(struct nvme_dev *dev, int write, 12891c2ad9faaf662b4a525348775deca3ac8e6c35a0Matthew Wilcox struct nvme_iod *iod) 12907fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox{ 12911c2ad9faaf662b4a525348775deca3ac8e6c35a0Matthew Wilcox int i; 1292b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 12931c2ad9faaf662b4a525348775deca3ac8e6c35a0Matthew Wilcox dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents, 12941c2ad9faaf662b4a525348775deca3ac8e6c35a0Matthew Wilcox write ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 12957fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox 12961c2ad9faaf662b4a525348775deca3ac8e6c35a0Matthew Wilcox for (i = 0; i < iod->nents; i++) 12971c2ad9faaf662b4a525348775deca3ac8e6c35a0Matthew Wilcox put_page(sg_page(&iod->sg[i])); 12987fc3cdabba75c2516b8b645eb0ca7907aea70415Matthew Wilcox} 1299b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1300a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcoxstatic int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) 1301a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox{ 1302a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct nvme_dev *dev = ns->dev; 1303a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct nvme_queue *nvmeq; 1304a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct nvme_user_io io; 1305a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox struct nvme_command c; 1306f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch unsigned length, meta_len; 1307f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch int status, i; 1308f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch struct nvme_iod *iod, *meta_iod = NULL; 1309f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch dma_addr_t meta_dma_addr; 1310f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch void *meta, *uninitialized_var(meta_mem); 1311a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 1312a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox if (copy_from_user(&io, uio, sizeof(io))) 1313a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox return -EFAULT; 13146c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox length = (io.nblocks + 1) << ns->lba_shift; 1315f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch meta_len = (io.nblocks + 1) * ns->ms; 1316f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch 1317f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch if (meta_len && ((io.metadata & 3) || !io.metadata)) 1318f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch return -EINVAL; 13196c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox 13206c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox switch (io.opcode) { 13216c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox case nvme_cmd_write: 13226c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox case nvme_cmd_read: 13236bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox case nvme_cmd_compare: 1324eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length); 13256413214c5d424fd5aae6567848340f962ad2ce0fMatthew Wilcox break; 13266c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox default: 13276bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox return -EINVAL; 13286c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox } 13296c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox 1330eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox if (IS_ERR(iod)) 1331eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox return PTR_ERR(iod); 1332a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 1333a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox memset(&c, 0, sizeof(c)); 1334a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.opcode = io.opcode; 1335a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.flags = io.flags; 13366c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox c.rw.nsid = cpu_to_le32(ns->ns_id); 1337a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.slba = cpu_to_le64(io.slba); 13386c7d49455ceb63064f992347d9185ff5bf43497aMatthew Wilcox c.rw.length = cpu_to_le16(io.nblocks); 1339a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox c.rw.control = cpu_to_le16(io.control); 13401c9b52651dad0ff1fa71fc6205c86d972f25bcc0Matthew Wilcox c.rw.dsmgmt = cpu_to_le32(io.dsmgmt); 13411c9b52651dad0ff1fa71fc6205c86d972f25bcc0Matthew Wilcox c.rw.reftag = cpu_to_le32(io.reftag); 13421c9b52651dad0ff1fa71fc6205c86d972f25bcc0Matthew Wilcox c.rw.apptag = cpu_to_le16(io.apptag); 13431c9b52651dad0ff1fa71fc6205c86d972f25bcc0Matthew Wilcox c.rw.appmask = cpu_to_le16(io.appmask); 1344f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch 1345f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch if (meta_len) { 13461b56749e541ad59068582f2a28297843e243b856Keith Busch meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata, 13471b56749e541ad59068582f2a28297843e243b856Keith Busch meta_len); 1348f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch if (IS_ERR(meta_iod)) { 1349f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch status = PTR_ERR(meta_iod); 1350f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch meta_iod = NULL; 1351f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch goto unmap; 1352f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch } 1353f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch 1354f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len, 1355f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch &meta_dma_addr, GFP_KERNEL); 1356f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch if (!meta_mem) { 1357f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch status = -ENOMEM; 1358f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch goto unmap; 1359f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch } 1360f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch 1361f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch if (io.opcode & 1) { 1362f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch int meta_offset = 0; 1363f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch 1364f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch for (i = 0; i < meta_iod->nents; i++) { 1365f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch meta = kmap_atomic(sg_page(&meta_iod->sg[i])) + 1366f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch meta_iod->sg[i].offset; 1367f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch memcpy(meta_mem + meta_offset, meta, 1368f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch meta_iod->sg[i].length); 1369f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch kunmap_atomic(meta); 1370f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch meta_offset += meta_iod->sg[i].length; 1371f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch } 1372f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch } 1373f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch 1374f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch c.rw.metadata = cpu_to_le64(meta_dma_addr); 1375f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch } 1376f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch 1377eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox length = nvme_setup_prps(dev, &c.common, iod, length, GFP_KERNEL); 1378a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 1379040a93b52a9eee8177ebaf2ba0ee0f9f518d1bf8Matthew Wilcox nvmeq = get_nvmeq(dev); 1380fa92282149842645931580225647238428374758Matthew Wilcox /* 1381fa92282149842645931580225647238428374758Matthew Wilcox * Since nvme_submit_sync_cmd sleeps, we can't keep preemption 1382b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox * disabled. We may be preempted at any point, and be rescheduled 1383b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox * to a different CPU. That will cause cacheline bouncing, but no 1384b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox * additional races since q_lock already protects against other CPUs. 1385b1ad37efcafe396ac3944853589688dd0ec3c64eMatthew Wilcox */ 1386a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox put_nvmeq(nvmeq); 1387b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox if (length != (io.nblocks + 1) << ns->lba_shift) 1388b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox status = -ENOMEM; 1389b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox else 1390ff976d724a74e4522e9ca2de1fb37ac4520f454fMatthew Wilcox status = nvme_submit_sync_cmd(nvmeq, &c, NULL, NVME_IO_TIMEOUT); 1391a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 1392f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch if (meta_len) { 1393f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) { 1394f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch int meta_offset = 0; 1395f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch 1396f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch for (i = 0; i < meta_iod->nents; i++) { 1397f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch meta = kmap_atomic(sg_page(&meta_iod->sg[i])) + 1398f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch meta_iod->sg[i].offset; 1399f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch memcpy(meta, meta_mem + meta_offset, 1400f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch meta_iod->sg[i].length); 1401f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch kunmap_atomic(meta); 1402f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch meta_offset += meta_iod->sg[i].length; 1403f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch } 1404f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch } 1405f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch 1406f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem, 1407f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch meta_dma_addr); 1408f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch } 1409f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch 1410f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch unmap: 14111c2ad9faaf662b4a525348775deca3ac8e6c35a0Matthew Wilcox nvme_unmap_user_pages(dev, io.opcode & 1, iod); 1412eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox nvme_free_iod(dev, iod); 1413f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch 1414f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch if (meta_iod) { 1415f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod); 1416f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch nvme_free_iod(dev, meta_iod); 1417f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch } 1418f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch 1419a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox return status; 1420a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox} 1421a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox 142250af8baec46a99a9b81a4600c0374f83a5a590a9Keith Buschstatic int nvme_user_admin_cmd(struct nvme_dev *dev, 14236bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox struct nvme_admin_cmd __user *ucmd) 14246ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox{ 14256bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox struct nvme_admin_cmd cmd; 14266ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox struct nvme_command c; 1427eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox int status, length; 1428c7d36ab8fa04c213328119a9c0d66985fe204ee5Keith Busch struct nvme_iod *uninitialized_var(iod); 142994f370cab6e5ac514b658c6b2b3aa308cefc5c7aKeith Busch unsigned timeout; 14306ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 14316bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox if (!capable(CAP_SYS_ADMIN)) 14326bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox return -EACCES; 14336bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox if (copy_from_user(&cmd, ucmd, sizeof(cmd))) 14346ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox return -EFAULT; 14356ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 14366ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox memset(&c, 0, sizeof(c)); 14376bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.opcode = cmd.opcode; 14386bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.flags = cmd.flags; 14396bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.nsid = cpu_to_le32(cmd.nsid); 14406bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.cdw2[0] = cpu_to_le32(cmd.cdw2); 14416bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.cdw2[1] = cpu_to_le32(cmd.cdw3); 14426bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.cdw10[0] = cpu_to_le32(cmd.cdw10); 14436bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.cdw10[1] = cpu_to_le32(cmd.cdw11); 14446bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.cdw10[2] = cpu_to_le32(cmd.cdw12); 14456bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.cdw10[3] = cpu_to_le32(cmd.cdw13); 14466bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.cdw10[4] = cpu_to_le32(cmd.cdw14); 14476bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox c.common.cdw10[5] = cpu_to_le32(cmd.cdw15); 14486bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox 14496bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox length = cmd.data_len; 14506bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox if (cmd.data_len) { 1451497421880acecd0281d3182d534f3d28c927caecMatthew Wilcox iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr, 1452497421880acecd0281d3182d534f3d28c927caecMatthew Wilcox length); 1453eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox if (IS_ERR(iod)) 1454eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox return PTR_ERR(iod); 1455eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox length = nvme_setup_prps(dev, &c.common, iod, length, 1456eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox GFP_KERNEL); 14576bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox } 14586bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox 145994f370cab6e5ac514b658c6b2b3aa308cefc5c7aKeith Busch timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) : 146094f370cab6e5ac514b658c6b2b3aa308cefc5c7aKeith Busch ADMIN_TIMEOUT; 14616bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox if (length != cmd.data_len) 1462b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox status = -ENOMEM; 1463b77954cbddff28d55a36fad3c16f4daebb0f01dfMatthew Wilcox else 146494f370cab6e5ac514b658c6b2b3aa308cefc5c7aKeith Busch status = nvme_submit_sync_cmd(dev->queues[0], &c, &cmd.result, 146594f370cab6e5ac514b658c6b2b3aa308cefc5c7aKeith Busch timeout); 1466eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox 14676bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox if (cmd.data_len) { 14681c2ad9faaf662b4a525348775deca3ac8e6c35a0Matthew Wilcox nvme_unmap_user_pages(dev, cmd.opcode & 1, iod); 1469eca18b2394a9387feeaf14cd884ddddd7a809d19Matthew Wilcox nvme_free_iod(dev, iod); 14706bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox } 1471f4f117f64baf8840d22266d518227b2a186d294bKeith Busch 1472cf90bc4830b858487fe4b9b9ecd0031e23ca3e83Chayan Biswas if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result, 1473f4f117f64baf8840d22266d518227b2a186d294bKeith Busch sizeof(cmd.result))) 1474f4f117f64baf8840d22266d518227b2a186d294bKeith Busch status = -EFAULT; 1475f4f117f64baf8840d22266d518227b2a186d294bKeith Busch 14766ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox return status; 14776ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox} 14786ee44cdced04a53dc4f27eb97067e6cd33784726Matthew Wilcox 1479b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, 1480b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unsigned long arg) 1481b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1482b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_ns *ns = bdev->bd_disk->private_data; 1483b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1484b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox switch (cmd) { 14856bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox case NVME_IOCTL_ID: 1486c3bfe7176c035a0a2c70bc79180fb13a6c57142aMatthew Wilcox force_successful_syscall_return(); 14876bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox return ns->ns_id; 14886bbf1acddeed0bfb345a5578f9fcada16f1e514fMatthew Wilcox case NVME_IOCTL_ADMIN_CMD: 148950af8baec46a99a9b81a4600c0374f83a5a590a9Keith Busch return nvme_user_admin_cmd(ns->dev, (void __user *)arg); 1490a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox case NVME_IOCTL_SUBMIT_IO: 1491a53295b6998f62d961c29e54051c1cf1d738c2b3Matthew Wilcox return nvme_submit_io(ns, (void __user *)arg); 14925d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Verma case SG_GET_VERSION_NUM: 14935d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Verma return nvme_sg_get_version_num((void __user *)arg); 14945d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Verma case SG_IO: 14955d0f6131a79adfa1fb51309c5f81a2a4ef879dd4Vishal Verma return nvme_sg_io(ns, (void __user *)arg); 1496b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox default: 1497b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -ENOTTY; 1498b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1499b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1500b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1501b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic const struct block_device_operations nvme_fops = { 1502b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .owner = THIS_MODULE, 1503b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .ioctl = nvme_ioctl, 15044948168280b269a514045766ddd872cfac5968e1Matthew Wilcox .compat_ioctl = nvme_ioctl, 1505b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 1506b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 15071fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic void nvme_resubmit_bios(struct nvme_queue *nvmeq) 15081fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox{ 15091fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox while (bio_list_peek(&nvmeq->sq_cong)) { 15101fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox struct bio *bio = bio_list_pop(&nvmeq->sq_cong); 15111fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data; 1512427e97080196548557b288517537ab7eb48c309fKeith Busch 1513427e97080196548557b288517537ab7eb48c309fKeith Busch if (bio_list_empty(&nvmeq->sq_cong)) 1514427e97080196548557b288517537ab7eb48c309fKeith Busch remove_wait_queue(&nvmeq->sq_full, 1515427e97080196548557b288517537ab7eb48c309fKeith Busch &nvmeq->sq_cong_wait); 15161fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox if (nvme_submit_bio_queue(nvmeq, ns, bio)) { 1517427e97080196548557b288517537ab7eb48c309fKeith Busch if (bio_list_empty(&nvmeq->sq_cong)) 1518427e97080196548557b288517537ab7eb48c309fKeith Busch add_wait_queue(&nvmeq->sq_full, 1519427e97080196548557b288517537ab7eb48c309fKeith Busch &nvmeq->sq_cong_wait); 15201fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox bio_list_add_head(&nvmeq->sq_cong, bio); 15211fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox break; 15221fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox } 15231fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox } 15241fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox} 15251fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 15261fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcoxstatic int nvme_kthread(void *data) 15271fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox{ 15281fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox struct nvme_dev *dev; 15291fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 15301fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox while (!kthread_should_stop()) { 1531564a232c059913d91b491e04c2b2d670b8f94615Arjan van de Ven set_current_state(TASK_INTERRUPTIBLE); 15321fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_lock(&dev_list_lock); 15331fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox list_for_each_entry(dev, &dev_list, node) { 15341fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox int i; 15351fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox for (i = 0; i < dev->queue_count; i++) { 15361fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox struct nvme_queue *nvmeq = dev->queues[i]; 1537740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox if (!nvmeq) 1538740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox continue; 15391fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_lock_irq(&nvmeq->q_lock); 1540bc57a0f7a44cfcf3e9873f6c6b8dcecdca486b1fMatthew Wilcox nvme_process_cq(nvmeq); 1541a09115b23e2002bb35b7bfd337683f00875671ecMatthew Wilcox nvme_cancel_ios(nvmeq, true); 15421fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox nvme_resubmit_bios(nvmeq); 15431fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_unlock_irq(&nvmeq->q_lock); 15441fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox } 15451fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox } 15461fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_unlock(&dev_list_lock); 1547acb7aa0db09b8abd38abeb84334a8a27a52fbb1bArjan van de Ven schedule_timeout(round_jiffies_relative(HZ)); 15481fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox } 15491fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox return 0; 15501fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox} 15511fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 15525aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcoxstatic DEFINE_IDA(nvme_index_ida); 15535aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox 15545aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcoxstatic int nvme_get_ns_idx(void) 15555aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox{ 15565aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox int index, error; 15575aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox 15585aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox do { 15595aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox if (!ida_pre_get(&nvme_index_ida, GFP_KERNEL)) 15605aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox return -1; 15615aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox 15625aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox spin_lock(&dev_list_lock); 15635aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox error = ida_get_new(&nvme_index_ida, &index); 15645aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox spin_unlock(&dev_list_lock); 15655aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox } while (error == -EAGAIN); 15665aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox 15675aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox if (error) 15685aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox index = -1; 15695aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox return index; 15705aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox} 15715aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox 15725aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcoxstatic void nvme_put_ns_idx(int index) 15735aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox{ 15745aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox spin_lock(&dev_list_lock); 15755aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox ida_remove(&nvme_index_ida, index); 15765aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox spin_unlock(&dev_list_lock); 15775aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox} 15785aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox 15790e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Buschstatic void nvme_config_discard(struct nvme_ns *ns) 15800e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch{ 15810e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch u32 logical_block_size = queue_logical_block_size(ns->queue); 15820e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch ns->queue->limits.discard_zeroes_data = 0; 15830e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch ns->queue->limits.discard_alignment = logical_block_size; 15840e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch ns->queue->limits.discard_granularity = logical_block_size; 15850e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch ns->queue->limits.max_discard_sectors = 0xffffffff; 15860e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue); 15870e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch} 15880e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch 1589c3bfe7176c035a0a2c70bc79180fb13a6c57142aMatthew Wilcoxstatic struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid, 1590b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_id_ns *id, struct nvme_lba_range_type *rt) 1591b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1592b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_ns *ns; 1593b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct gendisk *disk; 1594b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int lbaf; 1595b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1596b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (rt->attributes & NVME_LBART_ATTRIB_HIDE) 1597b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 1598b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1599b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns = kzalloc(sizeof(*ns), GFP_KERNEL); 1600b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!ns) 1601b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 1602b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->queue = blk_alloc_queue(GFP_KERNEL); 1603b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!ns->queue) 1604b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto out_free_ns; 16054eeb9215a0d5c9494ca8b20158cc8ee82618840cMatthew Wilcox ns->queue->queue_flags = QUEUE_FLAG_DEFAULT; 16064eeb9215a0d5c9494ca8b20158cc8ee82618840cMatthew Wilcox queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue); 16074eeb9215a0d5c9494ca8b20158cc8ee82618840cMatthew Wilcox queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue); 1608b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox blk_queue_make_request(ns->queue, nvme_make_request); 1609b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->dev = dev; 1610b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->queue->queuedata = ns; 1611b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1612b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk = alloc_disk(NVME_MINORS); 1613b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!disk) 1614b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto out_free_queue; 16155aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox ns->ns_id = nsid; 1616b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->disk = disk; 1617b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox lbaf = id->flbas & 0xf; 1618b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox ns->lba_shift = id->lbaf[lbaf].ds; 1619f410c680b5344f1cb63ff011c6ae3d4963f45c30Keith Busch ns->ms = le16_to_cpu(id->lbaf[lbaf].ms); 1620e9ef46369f5107e634a93b7fc4e62a1f53343197Keith Busch blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift); 16218fc23e032debd682f5ba9fc524a5846c10d2c522Keith Busch if (dev->max_hw_sectors) 16228fc23e032debd682f5ba9fc524a5846c10d2c522Keith Busch blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors); 1623b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1624b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->major = nvme_major; 1625b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->minors = NVME_MINORS; 16265aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox disk->first_minor = NVME_MINORS * nvme_get_ns_idx(); 1627b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->fops = &nvme_fops; 1628b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->private_data = ns; 1629b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox disk->queue = ns->queue; 1630388f037f4e7f0a24bac6b1a24f144f5d939f58cfMatthew Wilcox disk->driverfs_dev = &dev->pci_dev->dev; 16315aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid); 1632b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9)); 1633b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 16340e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch if (dev->oncs & NVME_CTRL_ONCS_DSM) 16350e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch nvme_config_discard(ns); 16360e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch 1637b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return ns; 1638b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1639b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free_queue: 1640b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox blk_cleanup_queue(ns->queue); 1641b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox out_free_ns: 1642b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(ns); 1643b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return NULL; 1644b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1645b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1646b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_ns_free(struct nvme_ns *ns) 1647b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 16485aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox int index = ns->disk->first_minor / NVME_MINORS; 1649b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox put_disk(ns->disk); 16505aff9382ddc8aac6eb0c70ffbb351652d71da69aMatthew Wilcox nvme_put_ns_idx(index); 1651b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox blk_cleanup_queue(ns->queue); 1652b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(ns); 1653b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1654b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1655b3b06812e199f248561ce7824a4a8a9cd573c05aMatthew Wilcoxstatic int set_queue_count(struct nvme_dev *dev, int count) 1656b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1657b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int status; 1658b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox u32 result; 1659b3b06812e199f248561ce7824a4a8a9cd573c05aMatthew Wilcox u32 q_count = (count - 1) | ((count - 1) << 16); 1660b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1661df3481399042200792822b6243e36a95a557b57eMatthew Wilcox status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0, 1662bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox &result); 1663b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (status) 16647e03b124065507e72008ef294c30001eca74a031Keith Busch return status < 0 ? -EIO : -EBUSY; 1665b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return min(result & 0xffff, result >> 16) + 1; 1666b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1667b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 16688d85fce77edfc22f1d6dbf78e3af723b4b556f3dGreg Kroah-Hartmanstatic int nvme_setup_io_queues(struct nvme_dev *dev) 1669b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1670fa08a396647767abd24a9e7015cb177121d0cf15Ramachandra Rao Gajula struct pci_dev *pdev = dev->pci_dev; 1671063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox int result, cpu, i, vecs, nr_io_queues, db_bar_size, q_depth; 1672b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1673b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox nr_io_queues = num_online_cpus(); 1674b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox result = set_queue_count(dev, nr_io_queues); 16751b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox if (result < 0) 16761b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox return result; 1677b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox if (result < nr_io_queues) 1678b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox nr_io_queues = result; 1679b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 16801b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox /* Deregister the admin queue's interrupt */ 16811b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox free_irq(dev->entry[0].vector, dev->queues[0]); 16821b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox 1683f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox db_bar_size = 4096 + ((nr_io_queues + 1) << (dev->db_stride + 3)); 1684f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox if (db_bar_size > 8192) { 1685f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox iounmap(dev->bar); 1686fa08a396647767abd24a9e7015cb177121d0cf15Ramachandra Rao Gajula dev->bar = ioremap(pci_resource_start(pdev, 0), db_bar_size); 1687f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox dev->dbs = ((void __iomem *)dev->bar) + 4096; 1688f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox dev->queues[0]->q_db = dev->dbs; 1689f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox } 1690f1938f6e1ee1583c87ec74dc406fdd8694e99ac8Matthew Wilcox 1691063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox vecs = nr_io_queues; 1692063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox for (i = 0; i < vecs; i++) 16931b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox dev->entry[i].entry = i; 16941b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox for (;;) { 1695063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox result = pci_enable_msix(pdev, dev->entry, vecs); 1696063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox if (result <= 0) 16971b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox break; 1698063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox vecs = result; 16991b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } 17001b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox 1701063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox if (result < 0) { 1702063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox vecs = nr_io_queues; 1703063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox if (vecs > 32) 1704063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox vecs = 32; 1705fa08a396647767abd24a9e7015cb177121d0cf15Ramachandra Rao Gajula for (;;) { 1706063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox result = pci_enable_msi_block(pdev, vecs); 1707fa08a396647767abd24a9e7015cb177121d0cf15Ramachandra Rao Gajula if (result == 0) { 1708063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox for (i = 0; i < vecs; i++) 1709fa08a396647767abd24a9e7015cb177121d0cf15Ramachandra Rao Gajula dev->entry[i].vector = i + pdev->irq; 1710fa08a396647767abd24a9e7015cb177121d0cf15Ramachandra Rao Gajula break; 1711063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox } else if (result < 0) { 1712063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox vecs = 1; 1713fa08a396647767abd24a9e7015cb177121d0cf15Ramachandra Rao Gajula break; 1714fa08a396647767abd24a9e7015cb177121d0cf15Ramachandra Rao Gajula } 1715063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox vecs = result; 1716fa08a396647767abd24a9e7015cb177121d0cf15Ramachandra Rao Gajula } 1717fa08a396647767abd24a9e7015cb177121d0cf15Ramachandra Rao Gajula } 1718fa08a396647767abd24a9e7015cb177121d0cf15Ramachandra Rao Gajula 1719063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox /* 1720063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox * Should investigate if there's a performance win from allocating 1721063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox * more queues than interrupt vectors; it might allow the submission 1722063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox * path to scale better, even if the receive path is limited by the 1723063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox * number of interrupts. 1724063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox */ 1725063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox nr_io_queues = vecs; 1726063a8096f3dbca7521d5918b3aea7ab46c5d2fe9Matthew Wilcox 17271b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox result = queue_request_irq(dev, dev->queues[0], "nvme admin"); 17281b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox /* XXX: handle failure here */ 17291b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox 17301b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox cpu = cpumask_first(cpu_online_mask); 1731b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox for (i = 0; i < nr_io_queues; i++) { 17321b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu)); 17331b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox cpu = cpumask_next(cpu, cpu_online_mask); 17341b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } 17351b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox 1736a0cadb85b8b758608ae0759151e29de7581c6731Keith Busch q_depth = min_t(int, NVME_CAP_MQES(readq(&dev->bar->cap)) + 1, 1737a0cadb85b8b758608ae0759151e29de7581c6731Keith Busch NVME_Q_DEPTH); 1738b348b7d54368c87811907a8e88f0d96713c43009Matthew Wilcox for (i = 0; i < nr_io_queues; i++) { 1739a0cadb85b8b758608ae0759151e29de7581c6731Keith Busch dev->queues[i + 1] = nvme_create_queue(dev, i + 1, q_depth, i); 17406f0f54499f2edf7e25410cdd99e6f030f3485fd1Matthew Wilcox if (IS_ERR(dev->queues[i + 1])) 17416f0f54499f2edf7e25410cdd99e6f030f3485fd1Matthew Wilcox return PTR_ERR(dev->queues[i + 1]); 17421b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox dev->queue_count++; 17431b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox } 1744b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 17459ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox for (; i < num_possible_cpus(); i++) { 17469ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox int target = i % rounddown_pow_of_two(dev->queue_count - 1); 17479ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox dev->queues[i + 1] = dev->queues[target + 1]; 17489ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox } 17499ecdc946212f7cd592986b2c519b470404caa6b8Matthew Wilcox 1750b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 1751b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1752b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1753b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_free_queues(struct nvme_dev *dev) 1754b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1755b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox int i; 1756b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1757b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox for (i = dev->queue_count - 1; i >= 0; i--) 1758b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_free_queue(dev, i); 1759b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1760b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1761422ef0c7c81934c3344e1623d2dcfda5fa2fab8dMatthew Wilcox/* 1762422ef0c7c81934c3344e1623d2dcfda5fa2fab8dMatthew Wilcox * Return: error value if an error occurred setting up the queues or calling 1763422ef0c7c81934c3344e1623d2dcfda5fa2fab8dMatthew Wilcox * Identify Device. 0 if these succeeded, even if adding some of the 1764422ef0c7c81934c3344e1623d2dcfda5fa2fab8dMatthew Wilcox * namespaces failed. At the moment, these failures are silent. TBD which 1765422ef0c7c81934c3344e1623d2dcfda5fa2fab8dMatthew Wilcox * failures should be reported. 1766422ef0c7c81934c3344e1623d2dcfda5fa2fab8dMatthew Wilcox */ 17678d85fce77edfc22f1d6dbf78e3af723b4b556f3dGreg Kroah-Hartmanstatic int nvme_dev_add(struct nvme_dev *dev) 1768b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1769c3bfe7176c035a0a2c70bc79180fb13a6c57142aMatthew Wilcox int res; 1770c3bfe7176c035a0a2c70bc79180fb13a6c57142aMatthew Wilcox unsigned nn, i; 1771cbb6218fd4ae8f98ddf0d66f0826a7a3a9c88298Keith Busch struct nvme_ns *ns; 177251814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox struct nvme_id_ctrl *ctrl; 1773bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox struct nvme_id_ns *id_ns; 1774bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox void *mem; 1775b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dma_addr_t dma_addr; 1776159b67d7aefe3902df91075be5d80943c1570aa8Keith Busch int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12; 1777b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1778b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox res = nvme_setup_io_queues(dev); 1779b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (res) 1780b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return res; 1781b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1782bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox mem = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr, 1783b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox GFP_KERNEL); 1784a9ef4343afbe67a6abf83c0f0294e80db48e513aKeith Busch if (!mem) 1785a9ef4343afbe67a6abf83c0f0294e80db48e513aKeith Busch return -ENOMEM; 1786b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1787bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox res = nvme_identify(dev, 0, 1, dma_addr); 1788b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (res) { 1789b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox res = -EIO; 1790cbb6218fd4ae8f98ddf0d66f0826a7a3a9c88298Keith Busch goto out; 1791b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1792b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1793bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox ctrl = mem; 179451814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox nn = le32_to_cpup(&ctrl->nn); 17950e5e4f0e56aca0df1d5648db0be9028bd573b25cKeith Busch dev->oncs = le16_to_cpup(&ctrl->oncs); 179651814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn)); 179751814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn)); 179851814232ecae90f888c902e252306df8d017f0ddMatthew Wilcox memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr)); 1799159b67d7aefe3902df91075be5d80943c1570aa8Keith Busch if (ctrl->mdts) 18008fc23e032debd682f5ba9fc524a5846c10d2c522Keith Busch dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9); 1801159b67d7aefe3902df91075be5d80943c1570aa8Keith Busch if ((dev->pci_dev->vendor == PCI_VENDOR_ID_INTEL) && 1802159b67d7aefe3902df91075be5d80943c1570aa8Keith Busch (dev->pci_dev->device == 0x0953) && ctrl->vs[3]) 1803159b67d7aefe3902df91075be5d80943c1570aa8Keith Busch dev->stripe_size = 1 << (ctrl->vs[3] + shift); 1804b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1805bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox id_ns = mem; 18062b2c1896871838cdf549442e8ad0264be5fa74e3Matthew Wilcox for (i = 1; i <= nn; i++) { 1807bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox res = nvme_identify(dev, i, 0, dma_addr); 1808b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (res) 1809b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox continue; 1810b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1811bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox if (id_ns->ncap == 0) 1812b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox continue; 1813b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1814bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i, 181508df1e05657fc6712e520e7c09cc6c86160ceb35Keith Busch dma_addr + 4096, NULL); 1816b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (res) 1817122090366d1d5c6ec1bfb6dfdb3a6d121ff074aaKeith Busch memset(mem + 4096, 0, 4096); 1818b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1819bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox ns = nvme_alloc_ns(dev, i, mem, mem + 4096); 1820b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (ns) 1821b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_add_tail(&ns->list, &dev->namespaces); 1822b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1823b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_for_each_entry(ns, &dev->namespaces, list) 1824b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox add_disk(ns->disk); 1825422ef0c7c81934c3344e1623d2dcfda5fa2fab8dMatthew Wilcox res = 0; 1826b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1827bc5fc7e4b22ca855902aba02b28c96f09b446407Matthew Wilcox out: 1828684f5c2025b067a23722e620d0b3b858d8dc5d01Matthew Wilcox dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr); 1829b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return res; 1830b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1831b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 18320877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Buschstatic int nvme_dev_map(struct nvme_dev *dev) 18330877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch{ 18340877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch int bars, result = -ENOMEM; 18350877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch struct pci_dev *pdev = dev->pci_dev; 18360877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch 18370877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch if (pci_enable_device_mem(pdev)) 18380877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch return result; 18390877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch 18400877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch dev->entry[0].vector = pdev->irq; 18410877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch pci_set_master(pdev); 18420877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch bars = pci_select_bars(pdev, IORESOURCE_MEM); 18430877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch if (pci_request_selected_regions(pdev, bars, "nvme")) 18440877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch goto disable_pci; 18450877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch 18460877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) 18470877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64)); 18480877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) 18490877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32)); 18500877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch else 18510877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch goto disable_pci; 18520877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch 18530877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch pci_set_drvdata(pdev, dev); 18540877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch dev->bar = ioremap(pci_resource_start(pdev, 0), 8192); 18550877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch if (!dev->bar) 18560877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch goto disable; 18570877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch 18580877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch dev->db_stride = NVME_CAP_STRIDE(readq(&dev->bar->cap)); 18590877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch dev->dbs = ((void __iomem *)dev->bar) + 4096; 18600877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch 18610877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch return 0; 18620877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch 18630877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch disable: 18640877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch pci_release_regions(pdev); 18650877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch disable_pci: 18660877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch pci_disable_device(pdev); 18670877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch return result; 18680877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch} 18690877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch 18700877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Buschstatic void nvme_dev_unmap(struct nvme_dev *dev) 18710877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch{ 18720877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch if (dev->pci_dev->msi_enabled) 18730877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch pci_disable_msi(dev->pci_dev); 18740877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch else if (dev->pci_dev->msix_enabled) 18750877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch pci_disable_msix(dev->pci_dev); 18760877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch 18770877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch if (dev->bar) { 18780877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch iounmap(dev->bar); 18790877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch dev->bar = NULL; 18800877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch } 18810877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch 18820877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch pci_release_regions(dev->pci_dev); 18830877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch if (pci_is_enabled(dev->pci_dev)) 18840877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch pci_disable_device(dev->pci_dev); 18850877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch} 18860877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch 1887b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int nvme_dev_remove(struct nvme_dev *dev) 1888b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1889b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_ns *ns, *next; 1890b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 18911fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_lock(&dev_list_lock); 18921fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox list_del(&dev->node); 18931fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_unlock(&dev_list_lock); 18941fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 1895b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_for_each_entry_safe(ns, next, &dev->namespaces, list) { 1896b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox list_del(&ns->list); 1897b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox del_gendisk(ns->disk); 1898b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_ns_free(ns); 1899b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox } 1900b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1901b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_free_queues(dev); 1902b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1903b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 1904b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1905b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1906091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcoxstatic int nvme_setup_prp_pools(struct nvme_dev *dev) 1907091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox{ 1908091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox struct device *dmadev = &dev->pci_dev->dev; 1909091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox dev->prp_page_pool = dma_pool_create("prp list page", dmadev, 1910091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox PAGE_SIZE, PAGE_SIZE, 0); 1911091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox if (!dev->prp_page_pool) 1912091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox return -ENOMEM; 1913091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox 191499802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox /* Optimisation for I/Os between 4k and 128k */ 191599802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox dev->prp_small_pool = dma_pool_create("prp list 256", dmadev, 191699802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox 256, 256, 0); 191799802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox if (!dev->prp_small_pool) { 191899802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox dma_pool_destroy(dev->prp_page_pool); 191999802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox return -ENOMEM; 192099802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox } 1921091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox return 0; 1922091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox} 1923091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox 1924091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcoxstatic void nvme_release_prp_pools(struct nvme_dev *dev) 1925091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox{ 1926091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox dma_pool_destroy(dev->prp_page_pool); 192799802a7aee2b3dd720e382c52b892cc6a8122b11Matthew Wilcox dma_pool_destroy(dev->prp_small_pool); 1928091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox} 1929091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox 1930cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anhstatic DEFINE_IDA(nvme_instance_ida); 1931cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh 1932cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anhstatic int nvme_set_instance(struct nvme_dev *dev) 1933b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1934cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh int instance, error; 1935cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh 1936cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh do { 1937cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL)) 1938cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh return -ENODEV; 1939cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh 1940cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh spin_lock(&dev_list_lock); 1941cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh error = ida_get_new(&nvme_instance_ida, &instance); 1942cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh spin_unlock(&dev_list_lock); 1943cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh } while (error == -EAGAIN); 1944cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh 1945cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh if (error) 1946cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh return -ENODEV; 1947cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh 1948cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh dev->instance = instance; 1949cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh return 0; 1950b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1951b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 1952b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void nvme_release_instance(struct nvme_dev *dev) 1953b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 1954cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh spin_lock(&dev_list_lock); 1955cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh ida_remove(&nvme_instance_ida, dev->instance); 1956cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh spin_unlock(&dev_list_lock); 1957b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 1958b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 19595e82e952f04681c10f35e02ee0a4a43ec027137aKeith Buschstatic void nvme_free_dev(struct kref *kref) 19605e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch{ 19615e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref); 19625e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch nvme_dev_remove(dev); 19630877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch nvme_dev_unmap(dev); 19645e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch nvme_release_instance(dev); 19655e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch nvme_release_prp_pools(dev); 19665e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch kfree(dev->queues); 19675e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch kfree(dev->entry); 19685e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch kfree(dev); 19695e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch} 19705e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch 19715e82e952f04681c10f35e02ee0a4a43ec027137aKeith Buschstatic int nvme_dev_open(struct inode *inode, struct file *f) 19725e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch{ 19735e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev, 19745e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch miscdev); 19755e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch kref_get(&dev->kref); 19765e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch f->private_data = dev; 19775e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch return 0; 19785e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch} 19795e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch 19805e82e952f04681c10f35e02ee0a4a43ec027137aKeith Buschstatic int nvme_dev_release(struct inode *inode, struct file *f) 19815e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch{ 19825e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch struct nvme_dev *dev = f->private_data; 19835e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch kref_put(&dev->kref, nvme_free_dev); 19845e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch return 0; 19855e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch} 19865e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch 19875e82e952f04681c10f35e02ee0a4a43ec027137aKeith Buschstatic long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg) 19885e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch{ 19895e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch struct nvme_dev *dev = f->private_data; 19905e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch switch (cmd) { 19915e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch case NVME_IOCTL_ADMIN_CMD: 19925e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch return nvme_user_admin_cmd(dev, (void __user *)arg); 19935e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch default: 19945e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch return -ENOTTY; 19955e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch } 19965e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch} 19975e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch 19985e82e952f04681c10f35e02ee0a4a43ec027137aKeith Buschstatic const struct file_operations nvme_dev_fops = { 19995e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch .owner = THIS_MODULE, 20005e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch .open = nvme_dev_open, 20015e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch .release = nvme_dev_release, 20025e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch .unlocked_ioctl = nvme_dev_ioctl, 20035e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch .compat_ioctl = nvme_dev_ioctl, 20045e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch}; 20055e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch 20068d85fce77edfc22f1d6dbf78e3af723b4b556f3dGreg Kroah-Hartmanstatic int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id) 2007b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 20080877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch int result = -ENOMEM; 2009b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_dev *dev; 2010b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 2011b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev = kzalloc(sizeof(*dev), GFP_KERNEL); 2012b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!dev) 2013b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return -ENOMEM; 2014b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry), 2015b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox GFP_KERNEL); 2016b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!dev->entry) 2017b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free; 20181b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *), 20191b23484bd012c078de2ea939249e2fb2e85a0a6eMatthew Wilcox GFP_KERNEL); 2020b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (!dev->queues) 2021b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto free; 2022b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 2023b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox INIT_LIST_HEAD(&dev->namespaces); 2024b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->pci_dev = pdev; 2025cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh result = nvme_set_instance(dev); 2026cd58ad7d188c643cf572b038909c2f7dd96fdafeQuoc-Son Anh if (result) 20270877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch goto free; 2028b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 2029091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox result = nvme_setup_prp_pools(dev); 2030091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox if (result) 20310877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch goto release; 2032091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox 20330877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch result = nvme_dev_map(dev); 20340877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch if (result) 20350877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch goto release_pools; 2036b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 2037b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = nvme_configure_admin_queue(dev); 2038b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox if (result) 2039b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox goto unmap; 2040b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox dev->queue_count++; 2041b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 20421fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_lock(&dev_list_lock); 20431fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox list_add(&dev->node, &dev_list); 20441fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox spin_unlock(&dev_list_lock); 20451fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 2046740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox result = nvme_dev_add(dev); 20477e03b124065507e72008ef294c30001eca74a031Keith Busch if (result && result != -EBUSY) 2048740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox goto delete; 2049740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox 20505e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance); 20515e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch dev->miscdev.minor = MISC_DYNAMIC_MINOR; 20525e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch dev->miscdev.parent = &pdev->dev; 20535e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch dev->miscdev.name = dev->name; 20545e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch dev->miscdev.fops = &nvme_dev_fops; 20555e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch result = misc_register(&dev->miscdev); 20565e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch if (result) 20575e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch goto remove; 20585e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch 20595e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch kref_init(&dev->kref); 2060b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return 0; 2061b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 20625e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch remove: 20635e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch nvme_dev_remove(dev); 2064b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox delete: 2065740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox spin_lock(&dev_list_lock); 2066740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox list_del(&dev->node); 2067740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox spin_unlock(&dev_list_lock); 2068740216fc59cba54f65187c9ed92f29bce3cf8778Matthew Wilcox 2069b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox nvme_free_queues(dev); 2070b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unmap: 20710877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch nvme_dev_unmap(dev); 20720877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch release_pools: 2073091b609258b8e01cc45b01a41ca5e496f674d989Matthew Wilcox nvme_release_prp_pools(dev); 20740877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch release: 20750877cb0d285c7f1d53d0b84b360bdea4be4f3f59Keith Busch nvme_release_instance(dev); 2076b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox free: 2077b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev->queues); 2078b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev->entry); 2079b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox kfree(dev); 2080b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return result; 2081b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 2082b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 20838d85fce77edfc22f1d6dbf78e3af723b4b556f3dGreg Kroah-Hartmanstatic void nvme_remove(struct pci_dev *pdev) 2084b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 2085b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox struct nvme_dev *dev = pci_get_drvdata(pdev); 20865e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch misc_deregister(&dev->miscdev); 20875e82e952f04681c10f35e02ee0a4a43ec027137aKeith Busch kref_put(&dev->kref, nvme_free_dev); 2088b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 2089b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 2090b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* These functions are yet to be implemented */ 2091b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_error_detected NULL 2092b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_dump_registers NULL 2093b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_link_reset NULL 2094b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_slot_reset NULL 2095b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_error_resume NULL 2096b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_suspend NULL 2097b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define nvme_resume NULL 2098b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 20991d3520357df99baf4ad89f86268ac96cd38092d9Stephen Hemmingerstatic const struct pci_error_handlers nvme_err_handler = { 2100b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .error_detected = nvme_error_detected, 2101b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .mmio_enabled = nvme_dump_registers, 2102b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .link_reset = nvme_link_reset, 2103b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .slot_reset = nvme_slot_reset, 2104b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .resume = nvme_error_resume, 2105b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 2106b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 2107b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox/* Move to pci_ids.h later */ 2108b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox#define PCI_CLASS_STORAGE_EXPRESS 0x010802 2109b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 2110b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = { 2111b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) }, 2112b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox { 0, } 2113b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 2114b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_DEVICE_TABLE(pci, nvme_id_table); 2115b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 2116b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic struct pci_driver nvme_driver = { 2117b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .name = "nvme", 2118b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .id_table = nvme_id_table, 2119b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .probe = nvme_probe, 21208d85fce77edfc22f1d6dbf78e3af723b4b556f3dGreg Kroah-Hartman .remove = nvme_remove, 2121b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .suspend = nvme_suspend, 2122b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .resume = nvme_resume, 2123b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox .err_handler = &nvme_err_handler, 2124b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox}; 2125b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 2126b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic int __init nvme_init(void) 2127b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 21280ac13140d796eb1e2f8956aea97a6e5e4ebcf981Matthew Wilcox int result; 21291fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox 21301fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox nvme_thread = kthread_run(nvme_kthread, NULL, "nvme"); 21311fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox if (IS_ERR(nvme_thread)) 21321fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox return PTR_ERR(nvme_thread); 2133b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 21345c42ea1643a630060f9e71e06d3933d244970967Keith Busch result = register_blkdev(nvme_major, "nvme"); 21355c42ea1643a630060f9e71e06d3933d244970967Keith Busch if (result < 0) 21361fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox goto kill_kthread; 21375c42ea1643a630060f9e71e06d3933d244970967Keith Busch else if (result > 0) 21380ac13140d796eb1e2f8956aea97a6e5e4ebcf981Matthew Wilcox nvme_major = result; 2139b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 2140b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox result = pci_register_driver(&nvme_driver); 21411fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox if (result) 21421fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox goto unregister_blkdev; 21431fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox return 0; 2144b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 21451fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox unregister_blkdev: 2146b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unregister_blkdev(nvme_major, "nvme"); 21471fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox kill_kthread: 21481fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox kthread_stop(nvme_thread); 2149b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox return result; 2150b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 2151b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 2152b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxstatic void __exit nvme_exit(void) 2153b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox{ 2154b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox pci_unregister_driver(&nvme_driver); 2155b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox unregister_blkdev(nvme_major, "nvme"); 21561fa6aeadf18aeebd7a217d7a3a933856448375b6Matthew Wilcox kthread_stop(nvme_thread); 2157b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox} 2158b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcox 2159b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>"); 2160b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew WilcoxMODULE_LICENSE("GPL"); 2161366e8217e5ec6ce9f73aec19c46d983110fb4a98Matthew WilcoxMODULE_VERSION("0.8"); 2162b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_init(nvme_init); 2163b60503ba432b16fc84442a84e29a7aad2c0c363dMatthew Wilcoxmodule_exit(nvme_exit); 2164