Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1 | /* |
| 2 | * NVM Express device driver |
| 3 | * Copyright (c) 2011, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/nvme.h> |
| 20 | #include <linux/bio.h> |
| 21 | #include <linux/blkdev.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/fs.h> |
| 24 | #include <linux/genhd.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/io.h> |
| 28 | #include <linux/kdev_t.h> |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/mm.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/moduleparam.h> |
| 33 | #include <linux/pci.h> |
| 34 | #include <linux/sched.h> |
| 35 | #include <linux/slab.h> |
| 36 | #include <linux/types.h> |
| 37 | #include <linux/version.h> |
| 38 | |
| 39 | #define NVME_Q_DEPTH 1024 |
| 40 | #define SQ_SIZE(depth) (depth * sizeof(struct nvme_command)) |
| 41 | #define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion)) |
| 42 | #define NVME_MINORS 64 |
| 43 | |
| 44 | static int nvme_major; |
| 45 | module_param(nvme_major, int, 0); |
| 46 | |
| 47 | /* |
| 48 | * Represents an NVM Express device. Each nvme_dev is a PCI function. |
| 49 | */ |
| 50 | struct nvme_dev { |
| 51 | struct list_head node; |
| 52 | struct nvme_queue **queues; |
| 53 | u32 __iomem *dbs; |
| 54 | struct pci_dev *pci_dev; |
| 55 | int instance; |
| 56 | int queue_count; |
| 57 | u32 ctrl_config; |
| 58 | struct msix_entry *entry; |
| 59 | struct nvme_bar __iomem *bar; |
| 60 | struct list_head namespaces; |
| 61 | }; |
| 62 | |
| 63 | /* |
| 64 | * An NVM Express namespace is equivalent to a SCSI LUN |
| 65 | */ |
| 66 | struct nvme_ns { |
| 67 | struct list_head list; |
| 68 | |
| 69 | struct nvme_dev *dev; |
| 70 | struct request_queue *queue; |
| 71 | struct gendisk *disk; |
| 72 | |
| 73 | int ns_id; |
| 74 | int lba_shift; |
| 75 | }; |
| 76 | |
| 77 | /* |
| 78 | * An NVM Express queue. Each device has at least two (one for admin |
| 79 | * commands and one for I/O commands). |
| 80 | */ |
| 81 | struct nvme_queue { |
| 82 | struct device *q_dmadev; |
| 83 | spinlock_t q_lock; |
| 84 | struct nvme_command *sq_cmds; |
| 85 | volatile struct nvme_completion *cqes; |
| 86 | dma_addr_t sq_dma_addr; |
| 87 | dma_addr_t cq_dma_addr; |
| 88 | wait_queue_head_t sq_full; |
| 89 | struct bio_list sq_cong; |
| 90 | u32 __iomem *q_db; |
| 91 | u16 q_depth; |
| 92 | u16 cq_vector; |
| 93 | u16 sq_head; |
| 94 | u16 sq_tail; |
| 95 | u16 cq_head; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 96 | u16 cq_phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 97 | unsigned long cmdid_data[]; |
| 98 | }; |
| 99 | |
| 100 | /* |
| 101 | * Check we didin't inadvertently grow the command struct |
| 102 | */ |
| 103 | static inline void _nvme_check_size(void) |
| 104 | { |
| 105 | BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64); |
| 106 | BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64); |
| 107 | BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64); |
| 108 | BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64); |
| 109 | BUILD_BUG_ON(sizeof(struct nvme_features) != 64); |
| 110 | BUILD_BUG_ON(sizeof(struct nvme_command) != 64); |
| 111 | BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096); |
| 112 | BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096); |
| 113 | BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * alloc_cmdid - Allocate a Command ID |
| 118 | * @param nvmeq The queue that will be used for this command |
| 119 | * @param ctx A pointer that will be passed to the handler |
| 120 | * @param handler The ID of the handler to call |
| 121 | * |
| 122 | * Allocate a Command ID for a queue. The data passed in will |
| 123 | * be passed to the completion handler. This is implemented by using |
| 124 | * the bottom two bits of the ctx pointer to store the handler ID. |
| 125 | * Passing in a pointer that's not 4-byte aligned will cause a BUG. |
| 126 | * We can change this if it becomes a problem. |
| 127 | */ |
| 128 | static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx, int handler) |
| 129 | { |
| 130 | int depth = nvmeq->q_depth; |
| 131 | unsigned long data = (unsigned long)ctx | handler; |
| 132 | int cmdid; |
| 133 | |
| 134 | BUG_ON((unsigned long)ctx & 3); |
| 135 | |
| 136 | do { |
| 137 | cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth); |
| 138 | if (cmdid >= depth) |
| 139 | return -EBUSY; |
| 140 | } while (test_and_set_bit(cmdid, nvmeq->cmdid_data)); |
| 141 | |
| 142 | nvmeq->cmdid_data[cmdid + BITS_TO_LONGS(depth)] = data; |
| 143 | return cmdid; |
| 144 | } |
| 145 | |
| 146 | static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx, |
| 147 | int handler) |
| 148 | { |
| 149 | int cmdid; |
| 150 | wait_event_killable(nvmeq->sq_full, |
| 151 | (cmdid = alloc_cmdid(nvmeq, ctx, handler)) >= 0); |
| 152 | return (cmdid < 0) ? -EINTR : cmdid; |
| 153 | } |
| 154 | |
| 155 | /* If you need more than four handlers, you'll need to change how |
| 156 | * alloc_cmdid and nvme_process_cq work |
| 157 | */ |
| 158 | enum { |
| 159 | sync_completion_id = 0, |
| 160 | bio_completion_id, |
| 161 | }; |
| 162 | |
| 163 | static unsigned long free_cmdid(struct nvme_queue *nvmeq, int cmdid) |
| 164 | { |
| 165 | unsigned long data; |
| 166 | |
| 167 | data = nvmeq->cmdid_data[cmdid + BITS_TO_LONGS(nvmeq->q_depth)]; |
| 168 | clear_bit(cmdid, nvmeq->cmdid_data); |
| 169 | wake_up(&nvmeq->sq_full); |
| 170 | return data; |
| 171 | } |
| 172 | |
| 173 | static struct nvme_queue *get_nvmeq(struct nvme_ns *ns) |
| 174 | { |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 175 | int qid, cpu = get_cpu(); |
| 176 | if (cpu < ns->dev->queue_count) |
| 177 | qid = cpu + 1; |
| 178 | else |
| 179 | qid = (cpu % rounddown_pow_of_two(ns->dev->queue_count)) + 1; |
| 180 | return ns->dev->queues[qid]; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static void put_nvmeq(struct nvme_queue *nvmeq) |
| 184 | { |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 185 | put_cpu(); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
| 189 | * nvme_submit_cmd: Copy a command into a queue and ring the doorbell |
| 190 | * @nvmeq: The queue to use |
| 191 | * @cmd: The command to send |
| 192 | * |
| 193 | * Safe to use from interrupt context |
| 194 | */ |
| 195 | static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd) |
| 196 | { |
| 197 | unsigned long flags; |
| 198 | u16 tail; |
| 199 | /* XXX: Need to check tail isn't going to overrun head */ |
| 200 | spin_lock_irqsave(&nvmeq->q_lock, flags); |
| 201 | tail = nvmeq->sq_tail; |
| 202 | memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd)); |
| 203 | writel(tail, nvmeq->q_db); |
| 204 | if (++tail == nvmeq->q_depth) |
| 205 | tail = 0; |
| 206 | nvmeq->sq_tail = tail; |
| 207 | spin_unlock_irqrestore(&nvmeq->q_lock, flags); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | struct nvme_req_info { |
| 213 | struct bio *bio; |
| 214 | int nents; |
| 215 | struct scatterlist sg[0]; |
| 216 | }; |
| 217 | |
| 218 | /* XXX: use a mempool */ |
| 219 | static struct nvme_req_info *alloc_info(unsigned nseg, gfp_t gfp) |
| 220 | { |
| 221 | return kmalloc(sizeof(struct nvme_req_info) + |
| 222 | sizeof(struct scatterlist) * nseg, gfp); |
| 223 | } |
| 224 | |
| 225 | static void free_info(struct nvme_req_info *info) |
| 226 | { |
| 227 | kfree(info); |
| 228 | } |
| 229 | |
| 230 | static void bio_completion(struct nvme_queue *nvmeq, void *ctx, |
| 231 | struct nvme_completion *cqe) |
| 232 | { |
| 233 | struct nvme_req_info *info = ctx; |
| 234 | struct bio *bio = info->bio; |
| 235 | u16 status = le16_to_cpup(&cqe->status) >> 1; |
| 236 | |
| 237 | dma_unmap_sg(nvmeq->q_dmadev, info->sg, info->nents, |
| 238 | bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 239 | free_info(info); |
| 240 | bio_endio(bio, status ? -EIO : 0); |
| 241 | } |
| 242 | |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 243 | /* length is in bytes */ |
| 244 | static void nvme_setup_prps(struct nvme_common_command *cmd, |
| 245 | struct scatterlist *sg, int length) |
| 246 | { |
| 247 | int dma_len = sg_dma_len(sg); |
| 248 | u64 dma_addr = sg_dma_address(sg); |
| 249 | int offset = offset_in_page(dma_addr); |
| 250 | |
| 251 | cmd->prp1 = cpu_to_le64(dma_addr); |
| 252 | length -= (PAGE_SIZE - offset); |
| 253 | if (length <= 0) |
| 254 | return; |
| 255 | |
| 256 | dma_len -= (PAGE_SIZE - offset); |
| 257 | if (dma_len) { |
| 258 | dma_addr += (PAGE_SIZE - offset); |
| 259 | } else { |
| 260 | sg = sg_next(sg); |
| 261 | dma_addr = sg_dma_address(sg); |
| 262 | dma_len = sg_dma_len(sg); |
| 263 | } |
| 264 | |
| 265 | if (length <= PAGE_SIZE) { |
| 266 | cmd->prp2 = cpu_to_le64(dma_addr); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | /* XXX: support PRP lists */ |
| 271 | } |
| 272 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 273 | static int nvme_map_bio(struct device *dev, struct nvme_req_info *info, |
| 274 | struct bio *bio, enum dma_data_direction dma_dir, int psegs) |
| 275 | { |
| 276 | struct bio_vec *bvec; |
| 277 | struct scatterlist *sg = info->sg; |
| 278 | int i, nsegs; |
| 279 | |
| 280 | sg_init_table(sg, psegs); |
| 281 | bio_for_each_segment(bvec, bio, i) { |
| 282 | sg_set_page(sg, bvec->bv_page, bvec->bv_len, bvec->bv_offset); |
| 283 | /* XXX: handle non-mergable here */ |
| 284 | nsegs++; |
| 285 | } |
| 286 | info->nents = nsegs; |
| 287 | |
| 288 | return dma_map_sg(dev, info->sg, info->nents, dma_dir); |
| 289 | } |
| 290 | |
| 291 | static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns, |
| 292 | struct bio *bio) |
| 293 | { |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 294 | struct nvme_command *cmnd; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 295 | struct nvme_req_info *info; |
| 296 | enum dma_data_direction dma_dir; |
| 297 | int cmdid; |
| 298 | u16 control; |
| 299 | u32 dsmgmt; |
| 300 | unsigned long flags; |
| 301 | int psegs = bio_phys_segments(ns->queue, bio); |
| 302 | |
| 303 | info = alloc_info(psegs, GFP_NOIO); |
| 304 | if (!info) |
| 305 | goto congestion; |
| 306 | info->bio = bio; |
| 307 | |
| 308 | cmdid = alloc_cmdid(nvmeq, info, bio_completion_id); |
| 309 | if (unlikely(cmdid < 0)) |
| 310 | goto free_info; |
| 311 | |
| 312 | control = 0; |
| 313 | if (bio->bi_rw & REQ_FUA) |
| 314 | control |= NVME_RW_FUA; |
| 315 | if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD)) |
| 316 | control |= NVME_RW_LR; |
| 317 | |
| 318 | dsmgmt = 0; |
| 319 | if (bio->bi_rw & REQ_RAHEAD) |
| 320 | dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH; |
| 321 | |
| 322 | spin_lock_irqsave(&nvmeq->q_lock, flags); |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 323 | cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 324 | |
Matthew Wilcox | b8deb62 | 2011-01-26 10:08:25 -0500 | [diff] [blame] | 325 | memset(cmnd, 0, sizeof(*cmnd)); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 326 | if (bio_data_dir(bio)) { |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 327 | cmnd->rw.opcode = nvme_cmd_write; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 328 | dma_dir = DMA_TO_DEVICE; |
| 329 | } else { |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 330 | cmnd->rw.opcode = nvme_cmd_read; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 331 | dma_dir = DMA_FROM_DEVICE; |
| 332 | } |
| 333 | |
| 334 | nvme_map_bio(nvmeq->q_dmadev, info, bio, dma_dir, psegs); |
| 335 | |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 336 | cmnd->rw.flags = 1; |
| 337 | cmnd->rw.command_id = cmdid; |
| 338 | cmnd->rw.nsid = cpu_to_le32(ns->ns_id); |
| 339 | nvme_setup_prps(&cmnd->common, info->sg, bio->bi_size); |
| 340 | cmnd->rw.slba = cpu_to_le64(bio->bi_sector >> (ns->lba_shift - 9)); |
| 341 | cmnd->rw.length = cpu_to_le16((bio->bi_size >> ns->lba_shift) - 1); |
| 342 | cmnd->rw.control = cpu_to_le16(control); |
| 343 | cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 344 | |
| 345 | writel(nvmeq->sq_tail, nvmeq->q_db); |
| 346 | if (++nvmeq->sq_tail == nvmeq->q_depth) |
| 347 | nvmeq->sq_tail = 0; |
| 348 | |
| 349 | spin_unlock_irqrestore(&nvmeq->q_lock, flags); |
| 350 | |
| 351 | return 0; |
| 352 | |
| 353 | free_info: |
| 354 | free_info(info); |
| 355 | congestion: |
| 356 | return -EBUSY; |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * NB: return value of non-zero would mean that we were a stacking driver. |
| 361 | * make_request must always succeed. |
| 362 | */ |
| 363 | static int nvme_make_request(struct request_queue *q, struct bio *bio) |
| 364 | { |
| 365 | struct nvme_ns *ns = q->queuedata; |
| 366 | struct nvme_queue *nvmeq = get_nvmeq(ns); |
| 367 | |
| 368 | if (nvme_submit_bio_queue(nvmeq, ns, bio)) { |
| 369 | blk_set_queue_congested(q, rw_is_sync(bio->bi_rw)); |
| 370 | bio_list_add(&nvmeq->sq_cong, bio); |
| 371 | } |
| 372 | put_nvmeq(nvmeq); |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | struct sync_cmd_info { |
| 378 | struct task_struct *task; |
| 379 | u32 result; |
| 380 | int status; |
| 381 | }; |
| 382 | |
| 383 | static void sync_completion(struct nvme_queue *nvmeq, void *ctx, |
| 384 | struct nvme_completion *cqe) |
| 385 | { |
| 386 | struct sync_cmd_info *cmdinfo = ctx; |
| 387 | cmdinfo->result = le32_to_cpup(&cqe->result); |
| 388 | cmdinfo->status = le16_to_cpup(&cqe->status) >> 1; |
| 389 | wake_up_process(cmdinfo->task); |
| 390 | } |
| 391 | |
| 392 | typedef void (*completion_fn)(struct nvme_queue *, void *, |
| 393 | struct nvme_completion *); |
| 394 | |
| 395 | static irqreturn_t nvme_process_cq(struct nvme_queue *nvmeq) |
| 396 | { |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 397 | u16 head, phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 398 | |
| 399 | static const completion_fn completions[4] = { |
| 400 | [sync_completion_id] = sync_completion, |
| 401 | [bio_completion_id] = bio_completion, |
| 402 | }; |
| 403 | |
| 404 | head = nvmeq->cq_head; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 405 | phase = nvmeq->cq_phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 406 | |
| 407 | for (;;) { |
| 408 | unsigned long data; |
| 409 | void *ptr; |
| 410 | unsigned char handler; |
| 411 | struct nvme_completion cqe = nvmeq->cqes[head]; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 412 | if ((le16_to_cpu(cqe.status) & 1) != phase) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 413 | break; |
| 414 | nvmeq->sq_head = le16_to_cpu(cqe.sq_head); |
| 415 | if (++head == nvmeq->q_depth) { |
| 416 | head = 0; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 417 | phase = !phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | data = free_cmdid(nvmeq, cqe.command_id); |
| 421 | handler = data & 3; |
| 422 | ptr = (void *)(data & ~3UL); |
| 423 | completions[handler](nvmeq, ptr, &cqe); |
| 424 | } |
| 425 | |
| 426 | /* If the controller ignores the cq head doorbell and continuously |
| 427 | * writes to the queue, it is theoretically possible to wrap around |
| 428 | * the queue twice and mistakenly return IRQ_NONE. Linux only |
| 429 | * requires that 0.1% of your interrupts are handled, so this isn't |
| 430 | * a big problem. |
| 431 | */ |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 432 | if (head == nvmeq->cq_head && phase == nvmeq->cq_phase) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 433 | return IRQ_NONE; |
| 434 | |
| 435 | writel(head, nvmeq->q_db + 1); |
| 436 | nvmeq->cq_head = head; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 437 | nvmeq->cq_phase = phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 438 | |
| 439 | return IRQ_HANDLED; |
| 440 | } |
| 441 | |
| 442 | static irqreturn_t nvme_irq(int irq, void *data) |
| 443 | { |
| 444 | return nvme_process_cq(data); |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * Returns 0 on success. If the result is negative, it's a Linux error code; |
| 449 | * if the result is positive, it's an NVM Express status code |
| 450 | */ |
| 451 | static int nvme_submit_sync_cmd(struct nvme_queue *q, struct nvme_command *cmd, |
| 452 | u32 *result) |
| 453 | { |
| 454 | int cmdid; |
| 455 | struct sync_cmd_info cmdinfo; |
| 456 | |
| 457 | cmdinfo.task = current; |
| 458 | cmdinfo.status = -EINTR; |
| 459 | |
| 460 | cmdid = alloc_cmdid_killable(q, &cmdinfo, sync_completion_id); |
| 461 | if (cmdid < 0) |
| 462 | return cmdid; |
| 463 | cmd->common.command_id = cmdid; |
| 464 | |
| 465 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 466 | nvme_submit_cmd(q, cmd); |
| 467 | schedule(); |
| 468 | |
| 469 | if (result) |
| 470 | *result = cmdinfo.result; |
| 471 | |
| 472 | return cmdinfo.status; |
| 473 | } |
| 474 | |
| 475 | static int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd, |
| 476 | u32 *result) |
| 477 | { |
| 478 | return nvme_submit_sync_cmd(dev->queues[0], cmd, result); |
| 479 | } |
| 480 | |
| 481 | static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id) |
| 482 | { |
| 483 | int status; |
| 484 | struct nvme_command c; |
| 485 | |
| 486 | memset(&c, 0, sizeof(c)); |
| 487 | c.delete_queue.opcode = opcode; |
| 488 | c.delete_queue.qid = cpu_to_le16(id); |
| 489 | |
| 490 | status = nvme_submit_admin_cmd(dev, &c, NULL); |
| 491 | if (status) |
| 492 | return -EIO; |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid, |
| 497 | struct nvme_queue *nvmeq) |
| 498 | { |
| 499 | int status; |
| 500 | struct nvme_command c; |
| 501 | int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED; |
| 502 | |
| 503 | memset(&c, 0, sizeof(c)); |
| 504 | c.create_cq.opcode = nvme_admin_create_cq; |
| 505 | c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr); |
| 506 | c.create_cq.cqid = cpu_to_le16(qid); |
| 507 | c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1); |
| 508 | c.create_cq.cq_flags = cpu_to_le16(flags); |
| 509 | c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector); |
| 510 | |
| 511 | status = nvme_submit_admin_cmd(dev, &c, NULL); |
| 512 | if (status) |
| 513 | return -EIO; |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid, |
| 518 | struct nvme_queue *nvmeq) |
| 519 | { |
| 520 | int status; |
| 521 | struct nvme_command c; |
| 522 | int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM; |
| 523 | |
| 524 | memset(&c, 0, sizeof(c)); |
| 525 | c.create_sq.opcode = nvme_admin_create_sq; |
| 526 | c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr); |
| 527 | c.create_sq.sqid = cpu_to_le16(qid); |
| 528 | c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1); |
| 529 | c.create_sq.sq_flags = cpu_to_le16(flags); |
| 530 | c.create_sq.cqid = cpu_to_le16(qid); |
| 531 | |
| 532 | status = nvme_submit_admin_cmd(dev, &c, NULL); |
| 533 | if (status) |
| 534 | return -EIO; |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid) |
| 539 | { |
| 540 | return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid); |
| 541 | } |
| 542 | |
| 543 | static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid) |
| 544 | { |
| 545 | return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid); |
| 546 | } |
| 547 | |
| 548 | static void nvme_free_queue(struct nvme_dev *dev, int qid) |
| 549 | { |
| 550 | struct nvme_queue *nvmeq = dev->queues[qid]; |
| 551 | |
| 552 | free_irq(dev->entry[nvmeq->cq_vector].vector, nvmeq); |
| 553 | |
| 554 | /* Don't tell the adapter to delete the admin queue */ |
| 555 | if (qid) { |
| 556 | adapter_delete_sq(dev, qid); |
| 557 | adapter_delete_cq(dev, qid); |
| 558 | } |
| 559 | |
| 560 | dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth), |
| 561 | (void *)nvmeq->cqes, nvmeq->cq_dma_addr); |
| 562 | dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth), |
| 563 | nvmeq->sq_cmds, nvmeq->sq_dma_addr); |
| 564 | kfree(nvmeq); |
| 565 | } |
| 566 | |
| 567 | static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid, |
| 568 | int depth, int vector) |
| 569 | { |
| 570 | struct device *dmadev = &dev->pci_dev->dev; |
| 571 | unsigned extra = (depth + BITS_TO_LONGS(depth)) * sizeof(long); |
| 572 | struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL); |
| 573 | if (!nvmeq) |
| 574 | return NULL; |
| 575 | |
| 576 | nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth), |
| 577 | &nvmeq->cq_dma_addr, GFP_KERNEL); |
| 578 | if (!nvmeq->cqes) |
| 579 | goto free_nvmeq; |
| 580 | memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth)); |
| 581 | |
| 582 | nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth), |
| 583 | &nvmeq->sq_dma_addr, GFP_KERNEL); |
| 584 | if (!nvmeq->sq_cmds) |
| 585 | goto free_cqdma; |
| 586 | |
| 587 | nvmeq->q_dmadev = dmadev; |
| 588 | spin_lock_init(&nvmeq->q_lock); |
| 589 | nvmeq->cq_head = 0; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 590 | nvmeq->cq_phase = 1; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 591 | init_waitqueue_head(&nvmeq->sq_full); |
| 592 | bio_list_init(&nvmeq->sq_cong); |
| 593 | nvmeq->q_db = &dev->dbs[qid * 2]; |
| 594 | nvmeq->q_depth = depth; |
| 595 | nvmeq->cq_vector = vector; |
| 596 | |
| 597 | return nvmeq; |
| 598 | |
| 599 | free_cqdma: |
| 600 | dma_free_coherent(dmadev, CQ_SIZE(nvmeq->q_depth), (void *)nvmeq->cqes, |
| 601 | nvmeq->cq_dma_addr); |
| 602 | free_nvmeq: |
| 603 | kfree(nvmeq); |
| 604 | return NULL; |
| 605 | } |
| 606 | |
Matthew Wilcox | 3001082 | 2011-01-20 09:10:15 -0500 | [diff] [blame] | 607 | static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq, |
| 608 | const char *name) |
| 609 | { |
| 610 | return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq, |
| 611 | IRQF_DISABLED | IRQF_SHARED, name, nvmeq); |
| 612 | } |
| 613 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 614 | static __devinit struct nvme_queue *nvme_create_queue(struct nvme_dev *dev, |
| 615 | int qid, int cq_size, int vector) |
| 616 | { |
| 617 | int result; |
| 618 | struct nvme_queue *nvmeq = nvme_alloc_queue(dev, qid, cq_size, vector); |
| 619 | |
| 620 | result = adapter_alloc_cq(dev, qid, nvmeq); |
| 621 | if (result < 0) |
| 622 | goto free_nvmeq; |
| 623 | |
| 624 | result = adapter_alloc_sq(dev, qid, nvmeq); |
| 625 | if (result < 0) |
| 626 | goto release_cq; |
| 627 | |
Matthew Wilcox | 3001082 | 2011-01-20 09:10:15 -0500 | [diff] [blame] | 628 | result = queue_request_irq(dev, nvmeq, "nvme"); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 629 | if (result < 0) |
| 630 | goto release_sq; |
| 631 | |
| 632 | return nvmeq; |
| 633 | |
| 634 | release_sq: |
| 635 | adapter_delete_sq(dev, qid); |
| 636 | release_cq: |
| 637 | adapter_delete_cq(dev, qid); |
| 638 | free_nvmeq: |
| 639 | dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth), |
| 640 | (void *)nvmeq->cqes, nvmeq->cq_dma_addr); |
| 641 | dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth), |
| 642 | nvmeq->sq_cmds, nvmeq->sq_dma_addr); |
| 643 | kfree(nvmeq); |
| 644 | return NULL; |
| 645 | } |
| 646 | |
| 647 | static int __devinit nvme_configure_admin_queue(struct nvme_dev *dev) |
| 648 | { |
| 649 | int result; |
| 650 | u32 aqa; |
| 651 | struct nvme_queue *nvmeq; |
| 652 | |
| 653 | dev->dbs = ((void __iomem *)dev->bar) + 4096; |
| 654 | |
| 655 | nvmeq = nvme_alloc_queue(dev, 0, 64, 0); |
| 656 | |
| 657 | aqa = nvmeq->q_depth - 1; |
| 658 | aqa |= aqa << 16; |
| 659 | |
| 660 | dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM; |
| 661 | dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT; |
| 662 | dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE; |
| 663 | |
| 664 | writel(aqa, &dev->bar->aqa); |
| 665 | writeq(nvmeq->sq_dma_addr, &dev->bar->asq); |
| 666 | writeq(nvmeq->cq_dma_addr, &dev->bar->acq); |
| 667 | writel(dev->ctrl_config, &dev->bar->cc); |
| 668 | |
| 669 | while (!(readl(&dev->bar->csts) & NVME_CSTS_RDY)) { |
| 670 | msleep(100); |
| 671 | if (fatal_signal_pending(current)) |
| 672 | return -EINTR; |
| 673 | } |
| 674 | |
Matthew Wilcox | 3001082 | 2011-01-20 09:10:15 -0500 | [diff] [blame] | 675 | result = queue_request_irq(dev, nvmeq, "nvme admin"); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 676 | dev->queues[0] = nvmeq; |
| 677 | return result; |
| 678 | } |
| 679 | |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame^] | 680 | static int nvme_map_user_pages(struct nvme_dev *dev, int write, |
| 681 | unsigned long addr, unsigned length, |
| 682 | struct scatterlist **sgp) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 683 | { |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 684 | int i, err, count, nents, offset; |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame^] | 685 | struct scatterlist *sg; |
| 686 | struct page **pages; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 687 | |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 688 | if (addr & 3) |
| 689 | return -EINVAL; |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame^] | 690 | if (!length) |
| 691 | return -EINVAL; |
| 692 | |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 693 | offset = offset_in_page(addr); |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame^] | 694 | count = DIV_ROUND_UP(offset + length, PAGE_SIZE); |
| 695 | pages = kcalloc(count, sizeof(*pages), GFP_KERNEL); |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 696 | |
| 697 | err = get_user_pages_fast(addr, count, 1, pages); |
| 698 | if (err < count) { |
| 699 | count = err; |
| 700 | err = -EFAULT; |
| 701 | goto put_pages; |
| 702 | } |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame^] | 703 | |
| 704 | sg = kcalloc(count, sizeof(*sg), GFP_KERNEL); |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 705 | sg_init_table(sg, count); |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 706 | sg_set_page(&sg[0], pages[0], PAGE_SIZE - offset, offset); |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame^] | 707 | length -= (PAGE_SIZE - offset); |
| 708 | for (i = 1; i < count; i++) { |
| 709 | sg_set_page(&sg[i], pages[i], min_t(int, length, PAGE_SIZE), 0); |
| 710 | length -= PAGE_SIZE; |
| 711 | } |
| 712 | |
| 713 | err = -ENOMEM; |
| 714 | nents = dma_map_sg(&dev->pci_dev->dev, sg, count, |
| 715 | write ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 716 | if (!nents) |
| 717 | goto put_pages; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 718 | |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame^] | 719 | kfree(pages); |
| 720 | *sgp = sg; |
| 721 | return nents; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 722 | |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 723 | put_pages: |
| 724 | for (i = 0; i < count; i++) |
| 725 | put_page(pages[i]); |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame^] | 726 | kfree(pages); |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 727 | return err; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 728 | } |
| 729 | |
Matthew Wilcox | 7fc3cda | 2011-01-26 17:05:50 -0500 | [diff] [blame^] | 730 | static void nvme_unmap_user_pages(struct nvme_dev *dev, int write, |
| 731 | unsigned long addr, int length, |
| 732 | struct scatterlist *sg, int nents) |
| 733 | { |
| 734 | int i, count; |
| 735 | |
| 736 | count = DIV_ROUND_UP(offset_in_page(addr) + length, PAGE_SIZE); |
| 737 | dma_unmap_sg(&dev->pci_dev->dev, sg, nents, DMA_FROM_DEVICE); |
| 738 | |
| 739 | for (i = 0; i < count; i++) |
| 740 | put_page(sg_page(&sg[i])); |
| 741 | } |
| 742 | |
| 743 | static int nvme_submit_user_admin_command(struct nvme_dev *dev, |
| 744 | unsigned long addr, unsigned length, |
| 745 | struct nvme_command *cmd) |
| 746 | { |
| 747 | int err, nents; |
| 748 | struct scatterlist *sg; |
| 749 | |
| 750 | nents = nvme_map_user_pages(dev, 0, addr, length, &sg); |
| 751 | if (nents < 0) |
| 752 | return nents; |
| 753 | nvme_setup_prps(&cmd->common, sg, length); |
| 754 | err = nvme_submit_admin_cmd(dev, cmd, NULL); |
| 755 | nvme_unmap_user_pages(dev, 0, addr, length, sg, nents); |
| 756 | return err ? -EIO : 0; |
| 757 | } |
| 758 | |
Matthew Wilcox | bd38c55 | 2011-01-26 14:34:32 -0500 | [diff] [blame] | 759 | static int nvme_identify(struct nvme_ns *ns, unsigned long addr, int cns) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 760 | { |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 761 | struct nvme_command c; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 762 | |
Matthew Wilcox | bd38c55 | 2011-01-26 14:34:32 -0500 | [diff] [blame] | 763 | memset(&c, 0, sizeof(c)); |
| 764 | c.identify.opcode = nvme_admin_identify; |
| 765 | c.identify.nsid = cns ? 0 : cpu_to_le32(ns->ns_id); |
| 766 | c.identify.cns = cpu_to_le32(cns); |
| 767 | |
| 768 | return nvme_submit_user_admin_command(ns->dev, addr, 4096, &c); |
| 769 | } |
| 770 | |
| 771 | static int nvme_get_range_type(struct nvme_ns *ns, unsigned long addr) |
| 772 | { |
| 773 | struct nvme_command c; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 774 | |
| 775 | memset(&c, 0, sizeof(c)); |
| 776 | c.features.opcode = nvme_admin_get_features; |
| 777 | c.features.nsid = cpu_to_le32(ns->ns_id); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 778 | c.features.fid = cpu_to_le32(NVME_FEAT_LBA_RANGE); |
| 779 | |
Matthew Wilcox | bd38c55 | 2011-01-26 14:34:32 -0500 | [diff] [blame] | 780 | return nvme_submit_user_admin_command(ns->dev, addr, 4096, &c); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, |
| 784 | unsigned long arg) |
| 785 | { |
| 786 | struct nvme_ns *ns = bdev->bd_disk->private_data; |
| 787 | |
| 788 | switch (cmd) { |
| 789 | case NVME_IOCTL_IDENTIFY_NS: |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 790 | return nvme_identify(ns, arg, 0); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 791 | case NVME_IOCTL_IDENTIFY_CTRL: |
Matthew Wilcox | 36c14ed | 2011-01-24 07:52:07 -0500 | [diff] [blame] | 792 | return nvme_identify(ns, arg, 1); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 793 | case NVME_IOCTL_GET_RANGE_TYPE: |
Matthew Wilcox | bd38c55 | 2011-01-26 14:34:32 -0500 | [diff] [blame] | 794 | return nvme_get_range_type(ns, arg); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 795 | default: |
| 796 | return -ENOTTY; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | static const struct block_device_operations nvme_fops = { |
| 801 | .owner = THIS_MODULE, |
| 802 | .ioctl = nvme_ioctl, |
| 803 | }; |
| 804 | |
| 805 | static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, int index, |
| 806 | struct nvme_id_ns *id, struct nvme_lba_range_type *rt) |
| 807 | { |
| 808 | struct nvme_ns *ns; |
| 809 | struct gendisk *disk; |
| 810 | int lbaf; |
| 811 | |
| 812 | if (rt->attributes & NVME_LBART_ATTRIB_HIDE) |
| 813 | return NULL; |
| 814 | |
| 815 | ns = kzalloc(sizeof(*ns), GFP_KERNEL); |
| 816 | if (!ns) |
| 817 | return NULL; |
| 818 | ns->queue = blk_alloc_queue(GFP_KERNEL); |
| 819 | if (!ns->queue) |
| 820 | goto out_free_ns; |
| 821 | ns->queue->queue_flags = QUEUE_FLAG_DEFAULT | QUEUE_FLAG_NOMERGES | |
| 822 | QUEUE_FLAG_NONROT | QUEUE_FLAG_DISCARD; |
| 823 | blk_queue_make_request(ns->queue, nvme_make_request); |
| 824 | ns->dev = dev; |
| 825 | ns->queue->queuedata = ns; |
| 826 | |
| 827 | disk = alloc_disk(NVME_MINORS); |
| 828 | if (!disk) |
| 829 | goto out_free_queue; |
| 830 | ns->ns_id = index; |
| 831 | ns->disk = disk; |
| 832 | lbaf = id->flbas & 0xf; |
| 833 | ns->lba_shift = id->lbaf[lbaf].ds; |
| 834 | |
| 835 | disk->major = nvme_major; |
| 836 | disk->minors = NVME_MINORS; |
| 837 | disk->first_minor = NVME_MINORS * index; |
| 838 | disk->fops = &nvme_fops; |
| 839 | disk->private_data = ns; |
| 840 | disk->queue = ns->queue; |
| 841 | sprintf(disk->disk_name, "nvme%dn%d", dev->instance, index); |
| 842 | set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9)); |
| 843 | |
| 844 | return ns; |
| 845 | |
| 846 | out_free_queue: |
| 847 | blk_cleanup_queue(ns->queue); |
| 848 | out_free_ns: |
| 849 | kfree(ns); |
| 850 | return NULL; |
| 851 | } |
| 852 | |
| 853 | static void nvme_ns_free(struct nvme_ns *ns) |
| 854 | { |
| 855 | put_disk(ns->disk); |
| 856 | blk_cleanup_queue(ns->queue); |
| 857 | kfree(ns); |
| 858 | } |
| 859 | |
Matthew Wilcox | b3b0681 | 2011-01-20 09:14:34 -0500 | [diff] [blame] | 860 | static int set_queue_count(struct nvme_dev *dev, int count) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 861 | { |
| 862 | int status; |
| 863 | u32 result; |
| 864 | struct nvme_command c; |
Matthew Wilcox | b3b0681 | 2011-01-20 09:14:34 -0500 | [diff] [blame] | 865 | u32 q_count = (count - 1) | ((count - 1) << 16); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 866 | |
| 867 | memset(&c, 0, sizeof(c)); |
| 868 | c.features.opcode = nvme_admin_get_features; |
| 869 | c.features.fid = cpu_to_le32(NVME_FEAT_NUM_QUEUES); |
| 870 | c.features.dword11 = cpu_to_le32(q_count); |
| 871 | |
| 872 | status = nvme_submit_admin_cmd(dev, &c, &result); |
| 873 | if (status) |
| 874 | return -EIO; |
| 875 | return min(result & 0xffff, result >> 16) + 1; |
| 876 | } |
| 877 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 878 | static int __devinit nvme_setup_io_queues(struct nvme_dev *dev) |
| 879 | { |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 880 | int result, cpu, i, nr_queues; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 881 | |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 882 | nr_queues = num_online_cpus(); |
| 883 | result = set_queue_count(dev, nr_queues); |
| 884 | if (result < 0) |
| 885 | return result; |
| 886 | if (result < nr_queues) |
| 887 | nr_queues = result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 888 | |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 889 | /* Deregister the admin queue's interrupt */ |
| 890 | free_irq(dev->entry[0].vector, dev->queues[0]); |
| 891 | |
| 892 | for (i = 0; i < nr_queues; i++) |
| 893 | dev->entry[i].entry = i; |
| 894 | for (;;) { |
| 895 | result = pci_enable_msix(dev->pci_dev, dev->entry, nr_queues); |
| 896 | if (result == 0) { |
| 897 | break; |
| 898 | } else if (result > 0) { |
| 899 | nr_queues = result; |
| 900 | continue; |
| 901 | } else { |
| 902 | nr_queues = 1; |
| 903 | break; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | result = queue_request_irq(dev, dev->queues[0], "nvme admin"); |
| 908 | /* XXX: handle failure here */ |
| 909 | |
| 910 | cpu = cpumask_first(cpu_online_mask); |
| 911 | for (i = 0; i < nr_queues; i++) { |
| 912 | irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu)); |
| 913 | cpu = cpumask_next(cpu, cpu_online_mask); |
| 914 | } |
| 915 | |
| 916 | for (i = 0; i < nr_queues; i++) { |
| 917 | dev->queues[i + 1] = nvme_create_queue(dev, i + 1, |
| 918 | NVME_Q_DEPTH, i); |
| 919 | if (!dev->queues[i + 1]) |
| 920 | return -ENOMEM; |
| 921 | dev->queue_count++; |
| 922 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 923 | |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | static void nvme_free_queues(struct nvme_dev *dev) |
| 928 | { |
| 929 | int i; |
| 930 | |
| 931 | for (i = dev->queue_count - 1; i >= 0; i--) |
| 932 | nvme_free_queue(dev, i); |
| 933 | } |
| 934 | |
| 935 | static int __devinit nvme_dev_add(struct nvme_dev *dev) |
| 936 | { |
| 937 | int res, nn, i; |
| 938 | struct nvme_ns *ns, *next; |
| 939 | void *id; |
| 940 | dma_addr_t dma_addr; |
| 941 | struct nvme_command cid, crt; |
| 942 | |
| 943 | res = nvme_setup_io_queues(dev); |
| 944 | if (res) |
| 945 | return res; |
| 946 | |
| 947 | /* XXX: Switch to a SG list once prp2 works */ |
| 948 | id = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr, |
| 949 | GFP_KERNEL); |
| 950 | |
| 951 | memset(&cid, 0, sizeof(cid)); |
| 952 | cid.identify.opcode = nvme_admin_identify; |
| 953 | cid.identify.nsid = 0; |
| 954 | cid.identify.prp1 = cpu_to_le64(dma_addr); |
| 955 | cid.identify.cns = cpu_to_le32(1); |
| 956 | |
| 957 | res = nvme_submit_admin_cmd(dev, &cid, NULL); |
| 958 | if (res) { |
| 959 | res = -EIO; |
| 960 | goto out_free; |
| 961 | } |
| 962 | |
| 963 | nn = le32_to_cpup(&((struct nvme_id_ctrl *)id)->nn); |
| 964 | |
| 965 | cid.identify.cns = 0; |
| 966 | memset(&crt, 0, sizeof(crt)); |
| 967 | crt.features.opcode = nvme_admin_get_features; |
| 968 | crt.features.prp1 = cpu_to_le64(dma_addr + 4096); |
| 969 | crt.features.fid = cpu_to_le32(NVME_FEAT_LBA_RANGE); |
| 970 | |
| 971 | for (i = 0; i < nn; i++) { |
| 972 | cid.identify.nsid = cpu_to_le32(i); |
| 973 | res = nvme_submit_admin_cmd(dev, &cid, NULL); |
| 974 | if (res) |
| 975 | continue; |
| 976 | |
| 977 | if (((struct nvme_id_ns *)id)->ncap == 0) |
| 978 | continue; |
| 979 | |
| 980 | crt.features.nsid = cpu_to_le32(i); |
| 981 | res = nvme_submit_admin_cmd(dev, &crt, NULL); |
| 982 | if (res) |
| 983 | continue; |
| 984 | |
| 985 | ns = nvme_alloc_ns(dev, i, id, id + 4096); |
| 986 | if (ns) |
| 987 | list_add_tail(&ns->list, &dev->namespaces); |
| 988 | } |
| 989 | list_for_each_entry(ns, &dev->namespaces, list) |
| 990 | add_disk(ns->disk); |
| 991 | |
| 992 | dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr); |
| 993 | return 0; |
| 994 | |
| 995 | out_free: |
| 996 | list_for_each_entry_safe(ns, next, &dev->namespaces, list) { |
| 997 | list_del(&ns->list); |
| 998 | nvme_ns_free(ns); |
| 999 | } |
| 1000 | |
| 1001 | dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr); |
| 1002 | return res; |
| 1003 | } |
| 1004 | |
| 1005 | static int nvme_dev_remove(struct nvme_dev *dev) |
| 1006 | { |
| 1007 | struct nvme_ns *ns, *next; |
| 1008 | |
| 1009 | /* TODO: wait all I/O finished or cancel them */ |
| 1010 | |
| 1011 | list_for_each_entry_safe(ns, next, &dev->namespaces, list) { |
| 1012 | list_del(&ns->list); |
| 1013 | del_gendisk(ns->disk); |
| 1014 | nvme_ns_free(ns); |
| 1015 | } |
| 1016 | |
| 1017 | nvme_free_queues(dev); |
| 1018 | |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
| 1022 | /* XXX: Use an ida or something to let remove / add work correctly */ |
| 1023 | static void nvme_set_instance(struct nvme_dev *dev) |
| 1024 | { |
| 1025 | static int instance; |
| 1026 | dev->instance = instance++; |
| 1027 | } |
| 1028 | |
| 1029 | static void nvme_release_instance(struct nvme_dev *dev) |
| 1030 | { |
| 1031 | } |
| 1032 | |
| 1033 | static int __devinit nvme_probe(struct pci_dev *pdev, |
| 1034 | const struct pci_device_id *id) |
| 1035 | { |
| 1036 | int result = -ENOMEM; |
| 1037 | struct nvme_dev *dev; |
| 1038 | |
| 1039 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 1040 | if (!dev) |
| 1041 | return -ENOMEM; |
| 1042 | dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry), |
| 1043 | GFP_KERNEL); |
| 1044 | if (!dev->entry) |
| 1045 | goto free; |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 1046 | dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *), |
| 1047 | GFP_KERNEL); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1048 | if (!dev->queues) |
| 1049 | goto free; |
| 1050 | |
| 1051 | INIT_LIST_HEAD(&dev->namespaces); |
| 1052 | dev->pci_dev = pdev; |
| 1053 | pci_set_drvdata(pdev, dev); |
| 1054 | dma_set_mask(&dev->pci_dev->dev, DMA_BIT_MASK(64)); |
| 1055 | nvme_set_instance(dev); |
Matthew Wilcox | 53c9577 | 2011-01-20 13:42:34 -0500 | [diff] [blame] | 1056 | dev->entry[0].vector = pdev->irq; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1057 | |
| 1058 | dev->bar = ioremap(pci_resource_start(pdev, 0), 8192); |
| 1059 | if (!dev->bar) { |
| 1060 | result = -ENOMEM; |
| 1061 | goto disable; |
| 1062 | } |
| 1063 | |
| 1064 | result = nvme_configure_admin_queue(dev); |
| 1065 | if (result) |
| 1066 | goto unmap; |
| 1067 | dev->queue_count++; |
| 1068 | |
| 1069 | result = nvme_dev_add(dev); |
| 1070 | if (result) |
| 1071 | goto delete; |
| 1072 | return 0; |
| 1073 | |
| 1074 | delete: |
| 1075 | nvme_free_queues(dev); |
| 1076 | unmap: |
| 1077 | iounmap(dev->bar); |
| 1078 | disable: |
| 1079 | pci_disable_msix(pdev); |
| 1080 | nvme_release_instance(dev); |
| 1081 | free: |
| 1082 | kfree(dev->queues); |
| 1083 | kfree(dev->entry); |
| 1084 | kfree(dev); |
| 1085 | return result; |
| 1086 | } |
| 1087 | |
| 1088 | static void __devexit nvme_remove(struct pci_dev *pdev) |
| 1089 | { |
| 1090 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
| 1091 | nvme_dev_remove(dev); |
| 1092 | pci_disable_msix(pdev); |
| 1093 | iounmap(dev->bar); |
| 1094 | nvme_release_instance(dev); |
| 1095 | kfree(dev->queues); |
| 1096 | kfree(dev->entry); |
| 1097 | kfree(dev); |
| 1098 | } |
| 1099 | |
| 1100 | /* These functions are yet to be implemented */ |
| 1101 | #define nvme_error_detected NULL |
| 1102 | #define nvme_dump_registers NULL |
| 1103 | #define nvme_link_reset NULL |
| 1104 | #define nvme_slot_reset NULL |
| 1105 | #define nvme_error_resume NULL |
| 1106 | #define nvme_suspend NULL |
| 1107 | #define nvme_resume NULL |
| 1108 | |
| 1109 | static struct pci_error_handlers nvme_err_handler = { |
| 1110 | .error_detected = nvme_error_detected, |
| 1111 | .mmio_enabled = nvme_dump_registers, |
| 1112 | .link_reset = nvme_link_reset, |
| 1113 | .slot_reset = nvme_slot_reset, |
| 1114 | .resume = nvme_error_resume, |
| 1115 | }; |
| 1116 | |
| 1117 | /* Move to pci_ids.h later */ |
| 1118 | #define PCI_CLASS_STORAGE_EXPRESS 0x010802 |
| 1119 | |
| 1120 | static DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = { |
| 1121 | { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) }, |
| 1122 | { 0, } |
| 1123 | }; |
| 1124 | MODULE_DEVICE_TABLE(pci, nvme_id_table); |
| 1125 | |
| 1126 | static struct pci_driver nvme_driver = { |
| 1127 | .name = "nvme", |
| 1128 | .id_table = nvme_id_table, |
| 1129 | .probe = nvme_probe, |
| 1130 | .remove = __devexit_p(nvme_remove), |
| 1131 | .suspend = nvme_suspend, |
| 1132 | .resume = nvme_resume, |
| 1133 | .err_handler = &nvme_err_handler, |
| 1134 | }; |
| 1135 | |
| 1136 | static int __init nvme_init(void) |
| 1137 | { |
| 1138 | int result; |
| 1139 | |
| 1140 | nvme_major = register_blkdev(nvme_major, "nvme"); |
| 1141 | if (nvme_major <= 0) |
| 1142 | return -EBUSY; |
| 1143 | |
| 1144 | result = pci_register_driver(&nvme_driver); |
| 1145 | if (!result) |
| 1146 | return 0; |
| 1147 | |
| 1148 | unregister_blkdev(nvme_major, "nvme"); |
| 1149 | return result; |
| 1150 | } |
| 1151 | |
| 1152 | static void __exit nvme_exit(void) |
| 1153 | { |
| 1154 | pci_unregister_driver(&nvme_driver); |
| 1155 | unregister_blkdev(nvme_major, "nvme"); |
| 1156 | } |
| 1157 | |
| 1158 | MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>"); |
| 1159 | MODULE_LICENSE("GPL"); |
| 1160 | MODULE_VERSION("0.1"); |
| 1161 | module_init(nvme_init); |
| 1162 | module_exit(nvme_exit); |