1 /* 2 * NVM Express device driver 3 * Copyright (c) 2011-2014, 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 15 #include <linux/blkdev.h> 16 #include <linux/blk-mq.h> 17 #include <linux/delay.h> 18 #include <linux/errno.h> 19 #include <linux/hdreg.h> 20 #include <linux/kernel.h> 21 #include <linux/module.h> 22 #include <linux/list_sort.h> 23 #include <linux/slab.h> 24 #include <linux/types.h> 25 #include <linux/pr.h> 26 #include <linux/ptrace.h> 27 #include <linux/nvme_ioctl.h> 28 #include <linux/t10-pi.h> 29 #include <scsi/sg.h> 30 #include <asm/unaligned.h> 31 32 #include "nvme.h" 33 #include "fabrics.h" 34 35 #define NVME_MINORS (1U << MINORBITS) 36 37 unsigned char admin_timeout = 60; 38 module_param(admin_timeout, byte, 0644); 39 MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands"); 40 EXPORT_SYMBOL_GPL(admin_timeout); 41 42 unsigned char nvme_io_timeout = 30; 43 module_param_named(io_timeout, nvme_io_timeout, byte, 0644); 44 MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O"); 45 EXPORT_SYMBOL_GPL(nvme_io_timeout); 46 47 unsigned char shutdown_timeout = 5; 48 module_param(shutdown_timeout, byte, 0644); 49 MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown"); 50 51 unsigned int nvme_max_retries = 5; 52 module_param_named(max_retries, nvme_max_retries, uint, 0644); 53 MODULE_PARM_DESC(max_retries, "max number of retries a command may have"); 54 EXPORT_SYMBOL_GPL(nvme_max_retries); 55 56 static int nvme_char_major; 57 module_param(nvme_char_major, int, 0); 58 59 static LIST_HEAD(nvme_ctrl_list); 60 static DEFINE_SPINLOCK(dev_list_lock); 61 62 static struct class *nvme_class; 63 64 void nvme_cancel_request(struct request *req, void *data, bool reserved) 65 { 66 int status; 67 68 if (!blk_mq_request_started(req)) 69 return; 70 71 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device, 72 "Cancelling I/O %d", req->tag); 73 74 status = NVME_SC_ABORT_REQ; 75 if (blk_queue_dying(req->q)) 76 status |= NVME_SC_DNR; 77 blk_mq_complete_request(req, status); 78 } 79 EXPORT_SYMBOL_GPL(nvme_cancel_request); 80 81 bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl, 82 enum nvme_ctrl_state new_state) 83 { 84 enum nvme_ctrl_state old_state; 85 bool changed = false; 86 87 spin_lock_irq(&ctrl->lock); 88 89 old_state = ctrl->state; 90 switch (new_state) { 91 case NVME_CTRL_LIVE: 92 switch (old_state) { 93 case NVME_CTRL_NEW: 94 case NVME_CTRL_RESETTING: 95 case NVME_CTRL_RECONNECTING: 96 changed = true; 97 /* FALLTHRU */ 98 default: 99 break; 100 } 101 break; 102 case NVME_CTRL_RESETTING: 103 switch (old_state) { 104 case NVME_CTRL_NEW: 105 case NVME_CTRL_LIVE: 106 case NVME_CTRL_RECONNECTING: 107 changed = true; 108 /* FALLTHRU */ 109 default: 110 break; 111 } 112 break; 113 case NVME_CTRL_RECONNECTING: 114 switch (old_state) { 115 case NVME_CTRL_LIVE: 116 changed = true; 117 /* FALLTHRU */ 118 default: 119 break; 120 } 121 break; 122 case NVME_CTRL_DELETING: 123 switch (old_state) { 124 case NVME_CTRL_LIVE: 125 case NVME_CTRL_RESETTING: 126 case NVME_CTRL_RECONNECTING: 127 changed = true; 128 /* FALLTHRU */ 129 default: 130 break; 131 } 132 break; 133 case NVME_CTRL_DEAD: 134 switch (old_state) { 135 case NVME_CTRL_DELETING: 136 changed = true; 137 /* FALLTHRU */ 138 default: 139 break; 140 } 141 break; 142 default: 143 break; 144 } 145 146 if (changed) 147 ctrl->state = new_state; 148 149 spin_unlock_irq(&ctrl->lock); 150 151 return changed; 152 } 153 EXPORT_SYMBOL_GPL(nvme_change_ctrl_state); 154 155 static void nvme_free_ns(struct kref *kref) 156 { 157 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref); 158 159 if (ns->ndev) 160 nvme_nvm_unregister(ns); 161 162 if (ns->disk) { 163 spin_lock(&dev_list_lock); 164 ns->disk->private_data = NULL; 165 spin_unlock(&dev_list_lock); 166 } 167 168 put_disk(ns->disk); 169 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance); 170 nvme_put_ctrl(ns->ctrl); 171 kfree(ns); 172 } 173 174 static void nvme_put_ns(struct nvme_ns *ns) 175 { 176 kref_put(&ns->kref, nvme_free_ns); 177 } 178 179 static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk) 180 { 181 struct nvme_ns *ns; 182 183 spin_lock(&dev_list_lock); 184 ns = disk->private_data; 185 if (ns) { 186 if (!kref_get_unless_zero(&ns->kref)) 187 goto fail; 188 if (!try_module_get(ns->ctrl->ops->module)) 189 goto fail_put_ns; 190 } 191 spin_unlock(&dev_list_lock); 192 193 return ns; 194 195 fail_put_ns: 196 kref_put(&ns->kref, nvme_free_ns); 197 fail: 198 spin_unlock(&dev_list_lock); 199 return NULL; 200 } 201 202 void nvme_requeue_req(struct request *req) 203 { 204 blk_mq_requeue_request(req, !blk_mq_queue_stopped(req->q)); 205 } 206 EXPORT_SYMBOL_GPL(nvme_requeue_req); 207 208 struct request *nvme_alloc_request(struct request_queue *q, 209 struct nvme_command *cmd, unsigned int flags, int qid) 210 { 211 struct request *req; 212 213 if (qid == NVME_QID_ANY) { 214 req = blk_mq_alloc_request(q, nvme_is_write(cmd), flags); 215 } else { 216 req = blk_mq_alloc_request_hctx(q, nvme_is_write(cmd), flags, 217 qid ? qid - 1 : 0); 218 } 219 if (IS_ERR(req)) 220 return req; 221 222 req->cmd_type = REQ_TYPE_DRV_PRIV; 223 req->cmd_flags |= REQ_FAILFAST_DRIVER; 224 nvme_req(req)->cmd = cmd; 225 226 return req; 227 } 228 EXPORT_SYMBOL_GPL(nvme_alloc_request); 229 230 static inline void nvme_setup_flush(struct nvme_ns *ns, 231 struct nvme_command *cmnd) 232 { 233 memset(cmnd, 0, sizeof(*cmnd)); 234 cmnd->common.opcode = nvme_cmd_flush; 235 cmnd->common.nsid = cpu_to_le32(ns->ns_id); 236 } 237 238 static inline int nvme_setup_discard(struct nvme_ns *ns, struct request *req, 239 struct nvme_command *cmnd) 240 { 241 struct nvme_dsm_range *range; 242 unsigned int nr_bytes = blk_rq_bytes(req); 243 244 range = kmalloc(sizeof(*range), GFP_ATOMIC); 245 if (!range) 246 return BLK_MQ_RQ_QUEUE_BUSY; 247 248 range->cattr = cpu_to_le32(0); 249 range->nlb = cpu_to_le32(nr_bytes >> ns->lba_shift); 250 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req))); 251 252 memset(cmnd, 0, sizeof(*cmnd)); 253 cmnd->dsm.opcode = nvme_cmd_dsm; 254 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id); 255 cmnd->dsm.nr = 0; 256 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD); 257 258 req->special_vec.bv_page = virt_to_page(range); 259 req->special_vec.bv_offset = offset_in_page(range); 260 req->special_vec.bv_len = sizeof(*range); 261 req->rq_flags |= RQF_SPECIAL_PAYLOAD; 262 263 return BLK_MQ_RQ_QUEUE_OK; 264 } 265 266 static inline void nvme_setup_rw(struct nvme_ns *ns, struct request *req, 267 struct nvme_command *cmnd) 268 { 269 u16 control = 0; 270 u32 dsmgmt = 0; 271 272 if (req->cmd_flags & REQ_FUA) 273 control |= NVME_RW_FUA; 274 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD)) 275 control |= NVME_RW_LR; 276 277 if (req->cmd_flags & REQ_RAHEAD) 278 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH; 279 280 memset(cmnd, 0, sizeof(*cmnd)); 281 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read); 282 cmnd->rw.nsid = cpu_to_le32(ns->ns_id); 283 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req))); 284 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1); 285 286 if (ns->ms) { 287 switch (ns->pi_type) { 288 case NVME_NS_DPS_PI_TYPE3: 289 control |= NVME_RW_PRINFO_PRCHK_GUARD; 290 break; 291 case NVME_NS_DPS_PI_TYPE1: 292 case NVME_NS_DPS_PI_TYPE2: 293 control |= NVME_RW_PRINFO_PRCHK_GUARD | 294 NVME_RW_PRINFO_PRCHK_REF; 295 cmnd->rw.reftag = cpu_to_le32( 296 nvme_block_nr(ns, blk_rq_pos(req))); 297 break; 298 } 299 if (!blk_integrity_rq(req)) 300 control |= NVME_RW_PRINFO_PRACT; 301 } 302 303 cmnd->rw.control = cpu_to_le16(control); 304 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt); 305 } 306 307 int nvme_setup_cmd(struct nvme_ns *ns, struct request *req, 308 struct nvme_command *cmd) 309 { 310 int ret = BLK_MQ_RQ_QUEUE_OK; 311 312 if (req->cmd_type == REQ_TYPE_DRV_PRIV) 313 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd)); 314 else if (req_op(req) == REQ_OP_FLUSH) 315 nvme_setup_flush(ns, cmd); 316 else if (req_op(req) == REQ_OP_DISCARD) 317 ret = nvme_setup_discard(ns, req, cmd); 318 else 319 nvme_setup_rw(ns, req, cmd); 320 321 cmd->common.command_id = req->tag; 322 323 return ret; 324 } 325 EXPORT_SYMBOL_GPL(nvme_setup_cmd); 326 327 /* 328 * Returns 0 on success. If the result is negative, it's a Linux error code; 329 * if the result is positive, it's an NVM Express status code 330 */ 331 int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, 332 union nvme_result *result, void *buffer, unsigned bufflen, 333 unsigned timeout, int qid, int at_head, int flags) 334 { 335 struct request *req; 336 int ret; 337 338 req = nvme_alloc_request(q, cmd, flags, qid); 339 if (IS_ERR(req)) 340 return PTR_ERR(req); 341 342 req->timeout = timeout ? timeout : ADMIN_TIMEOUT; 343 344 if (buffer && bufflen) { 345 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL); 346 if (ret) 347 goto out; 348 } 349 350 blk_execute_rq(req->q, NULL, req, at_head); 351 if (result) 352 *result = nvme_req(req)->result; 353 ret = req->errors; 354 out: 355 blk_mq_free_request(req); 356 return ret; 357 } 358 EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd); 359 360 int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, 361 void *buffer, unsigned bufflen) 362 { 363 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0, 364 NVME_QID_ANY, 0, 0); 365 } 366 EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd); 367 368 int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd, 369 void __user *ubuffer, unsigned bufflen, 370 void __user *meta_buffer, unsigned meta_len, u32 meta_seed, 371 u32 *result, unsigned timeout) 372 { 373 bool write = nvme_is_write(cmd); 374 struct nvme_ns *ns = q->queuedata; 375 struct gendisk *disk = ns ? ns->disk : NULL; 376 struct request *req; 377 struct bio *bio = NULL; 378 void *meta = NULL; 379 int ret; 380 381 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY); 382 if (IS_ERR(req)) 383 return PTR_ERR(req); 384 385 req->timeout = timeout ? timeout : ADMIN_TIMEOUT; 386 387 if (ubuffer && bufflen) { 388 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen, 389 GFP_KERNEL); 390 if (ret) 391 goto out; 392 bio = req->bio; 393 394 if (!disk) 395 goto submit; 396 bio->bi_bdev = bdget_disk(disk, 0); 397 if (!bio->bi_bdev) { 398 ret = -ENODEV; 399 goto out_unmap; 400 } 401 402 if (meta_buffer && meta_len) { 403 struct bio_integrity_payload *bip; 404 405 meta = kmalloc(meta_len, GFP_KERNEL); 406 if (!meta) { 407 ret = -ENOMEM; 408 goto out_unmap; 409 } 410 411 if (write) { 412 if (copy_from_user(meta, meta_buffer, 413 meta_len)) { 414 ret = -EFAULT; 415 goto out_free_meta; 416 } 417 } 418 419 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1); 420 if (IS_ERR(bip)) { 421 ret = PTR_ERR(bip); 422 goto out_free_meta; 423 } 424 425 bip->bip_iter.bi_size = meta_len; 426 bip->bip_iter.bi_sector = meta_seed; 427 428 ret = bio_integrity_add_page(bio, virt_to_page(meta), 429 meta_len, offset_in_page(meta)); 430 if (ret != meta_len) { 431 ret = -ENOMEM; 432 goto out_free_meta; 433 } 434 } 435 } 436 submit: 437 blk_execute_rq(req->q, disk, req, 0); 438 ret = req->errors; 439 if (result) 440 *result = le32_to_cpu(nvme_req(req)->result.u32); 441 if (meta && !ret && !write) { 442 if (copy_to_user(meta_buffer, meta, meta_len)) 443 ret = -EFAULT; 444 } 445 out_free_meta: 446 kfree(meta); 447 out_unmap: 448 if (bio) { 449 if (disk && bio->bi_bdev) 450 bdput(bio->bi_bdev); 451 blk_rq_unmap_user(bio); 452 } 453 out: 454 blk_mq_free_request(req); 455 return ret; 456 } 457 458 int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd, 459 void __user *ubuffer, unsigned bufflen, u32 *result, 460 unsigned timeout) 461 { 462 return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0, 463 result, timeout); 464 } 465 466 static void nvme_keep_alive_end_io(struct request *rq, int error) 467 { 468 struct nvme_ctrl *ctrl = rq->end_io_data; 469 470 blk_mq_free_request(rq); 471 472 if (error) { 473 dev_err(ctrl->device, 474 "failed nvme_keep_alive_end_io error=%d\n", error); 475 return; 476 } 477 478 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ); 479 } 480 481 static int nvme_keep_alive(struct nvme_ctrl *ctrl) 482 { 483 struct nvme_command c; 484 struct request *rq; 485 486 memset(&c, 0, sizeof(c)); 487 c.common.opcode = nvme_admin_keep_alive; 488 489 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED, 490 NVME_QID_ANY); 491 if (IS_ERR(rq)) 492 return PTR_ERR(rq); 493 494 rq->timeout = ctrl->kato * HZ; 495 rq->end_io_data = ctrl; 496 497 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io); 498 499 return 0; 500 } 501 502 static void nvme_keep_alive_work(struct work_struct *work) 503 { 504 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work), 505 struct nvme_ctrl, ka_work); 506 507 if (nvme_keep_alive(ctrl)) { 508 /* allocation failure, reset the controller */ 509 dev_err(ctrl->device, "keep-alive failed\n"); 510 ctrl->ops->reset_ctrl(ctrl); 511 return; 512 } 513 } 514 515 void nvme_start_keep_alive(struct nvme_ctrl *ctrl) 516 { 517 if (unlikely(ctrl->kato == 0)) 518 return; 519 520 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work); 521 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ); 522 } 523 EXPORT_SYMBOL_GPL(nvme_start_keep_alive); 524 525 void nvme_stop_keep_alive(struct nvme_ctrl *ctrl) 526 { 527 if (unlikely(ctrl->kato == 0)) 528 return; 529 530 cancel_delayed_work_sync(&ctrl->ka_work); 531 } 532 EXPORT_SYMBOL_GPL(nvme_stop_keep_alive); 533 534 int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id) 535 { 536 struct nvme_command c = { }; 537 int error; 538 539 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */ 540 c.identify.opcode = nvme_admin_identify; 541 c.identify.cns = cpu_to_le32(NVME_ID_CNS_CTRL); 542 543 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL); 544 if (!*id) 545 return -ENOMEM; 546 547 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id, 548 sizeof(struct nvme_id_ctrl)); 549 if (error) 550 kfree(*id); 551 return error; 552 } 553 554 static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list) 555 { 556 struct nvme_command c = { }; 557 558 c.identify.opcode = nvme_admin_identify; 559 c.identify.cns = cpu_to_le32(NVME_ID_CNS_NS_ACTIVE_LIST); 560 c.identify.nsid = cpu_to_le32(nsid); 561 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000); 562 } 563 564 int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid, 565 struct nvme_id_ns **id) 566 { 567 struct nvme_command c = { }; 568 int error; 569 570 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */ 571 c.identify.opcode = nvme_admin_identify, 572 c.identify.nsid = cpu_to_le32(nsid), 573 574 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL); 575 if (!*id) 576 return -ENOMEM; 577 578 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id, 579 sizeof(struct nvme_id_ns)); 580 if (error) 581 kfree(*id); 582 return error; 583 } 584 585 int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid, 586 void *buffer, size_t buflen, u32 *result) 587 { 588 struct nvme_command c; 589 union nvme_result res; 590 int ret; 591 592 memset(&c, 0, sizeof(c)); 593 c.features.opcode = nvme_admin_get_features; 594 c.features.nsid = cpu_to_le32(nsid); 595 c.features.fid = cpu_to_le32(fid); 596 597 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res, buffer, buflen, 0, 598 NVME_QID_ANY, 0, 0); 599 if (ret >= 0 && result) 600 *result = le32_to_cpu(res.u32); 601 return ret; 602 } 603 604 int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11, 605 void *buffer, size_t buflen, u32 *result) 606 { 607 struct nvme_command c; 608 union nvme_result res; 609 int ret; 610 611 memset(&c, 0, sizeof(c)); 612 c.features.opcode = nvme_admin_set_features; 613 c.features.fid = cpu_to_le32(fid); 614 c.features.dword11 = cpu_to_le32(dword11); 615 616 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res, 617 buffer, buflen, 0, NVME_QID_ANY, 0, 0); 618 if (ret >= 0 && result) 619 *result = le32_to_cpu(res.u32); 620 return ret; 621 } 622 623 int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log) 624 { 625 struct nvme_command c = { }; 626 int error; 627 628 c.common.opcode = nvme_admin_get_log_page, 629 c.common.nsid = cpu_to_le32(0xFFFFFFFF), 630 c.common.cdw10[0] = cpu_to_le32( 631 (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) | 632 NVME_LOG_SMART), 633 634 *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL); 635 if (!*log) 636 return -ENOMEM; 637 638 error = nvme_submit_sync_cmd(dev->admin_q, &c, *log, 639 sizeof(struct nvme_smart_log)); 640 if (error) 641 kfree(*log); 642 return error; 643 } 644 645 int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count) 646 { 647 u32 q_count = (*count - 1) | ((*count - 1) << 16); 648 u32 result; 649 int status, nr_io_queues; 650 651 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0, 652 &result); 653 if (status < 0) 654 return status; 655 656 /* 657 * Degraded controllers might return an error when setting the queue 658 * count. We still want to be able to bring them online and offer 659 * access to the admin queue, as that might be only way to fix them up. 660 */ 661 if (status > 0) { 662 dev_err(ctrl->dev, "Could not set queue count (%d)\n", status); 663 *count = 0; 664 } else { 665 nr_io_queues = min(result & 0xffff, result >> 16) + 1; 666 *count = min(*count, nr_io_queues); 667 } 668 669 return 0; 670 } 671 EXPORT_SYMBOL_GPL(nvme_set_queue_count); 672 673 static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) 674 { 675 struct nvme_user_io io; 676 struct nvme_command c; 677 unsigned length, meta_len; 678 void __user *metadata; 679 680 if (copy_from_user(&io, uio, sizeof(io))) 681 return -EFAULT; 682 if (io.flags) 683 return -EINVAL; 684 685 switch (io.opcode) { 686 case nvme_cmd_write: 687 case nvme_cmd_read: 688 case nvme_cmd_compare: 689 break; 690 default: 691 return -EINVAL; 692 } 693 694 length = (io.nblocks + 1) << ns->lba_shift; 695 meta_len = (io.nblocks + 1) * ns->ms; 696 metadata = (void __user *)(uintptr_t)io.metadata; 697 698 if (ns->ext) { 699 length += meta_len; 700 meta_len = 0; 701 } else if (meta_len) { 702 if ((io.metadata & 3) || !io.metadata) 703 return -EINVAL; 704 } 705 706 memset(&c, 0, sizeof(c)); 707 c.rw.opcode = io.opcode; 708 c.rw.flags = io.flags; 709 c.rw.nsid = cpu_to_le32(ns->ns_id); 710 c.rw.slba = cpu_to_le64(io.slba); 711 c.rw.length = cpu_to_le16(io.nblocks); 712 c.rw.control = cpu_to_le16(io.control); 713 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt); 714 c.rw.reftag = cpu_to_le32(io.reftag); 715 c.rw.apptag = cpu_to_le16(io.apptag); 716 c.rw.appmask = cpu_to_le16(io.appmask); 717 718 return __nvme_submit_user_cmd(ns->queue, &c, 719 (void __user *)(uintptr_t)io.addr, length, 720 metadata, meta_len, io.slba, NULL, 0); 721 } 722 723 static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns, 724 struct nvme_passthru_cmd __user *ucmd) 725 { 726 struct nvme_passthru_cmd cmd; 727 struct nvme_command c; 728 unsigned timeout = 0; 729 int status; 730 731 if (!capable(CAP_SYS_ADMIN)) 732 return -EACCES; 733 if (copy_from_user(&cmd, ucmd, sizeof(cmd))) 734 return -EFAULT; 735 if (cmd.flags) 736 return -EINVAL; 737 738 memset(&c, 0, sizeof(c)); 739 c.common.opcode = cmd.opcode; 740 c.common.flags = cmd.flags; 741 c.common.nsid = cpu_to_le32(cmd.nsid); 742 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2); 743 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3); 744 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10); 745 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11); 746 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12); 747 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13); 748 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14); 749 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15); 750 751 if (cmd.timeout_ms) 752 timeout = msecs_to_jiffies(cmd.timeout_ms); 753 754 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c, 755 (void __user *)(uintptr_t)cmd.addr, cmd.data_len, 756 &cmd.result, timeout); 757 if (status >= 0) { 758 if (put_user(cmd.result, &ucmd->result)) 759 return -EFAULT; 760 } 761 762 return status; 763 } 764 765 static int nvme_ioctl(struct block_device *bdev, fmode_t mode, 766 unsigned int cmd, unsigned long arg) 767 { 768 struct nvme_ns *ns = bdev->bd_disk->private_data; 769 770 switch (cmd) { 771 case NVME_IOCTL_ID: 772 force_successful_syscall_return(); 773 return ns->ns_id; 774 case NVME_IOCTL_ADMIN_CMD: 775 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg); 776 case NVME_IOCTL_IO_CMD: 777 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg); 778 case NVME_IOCTL_SUBMIT_IO: 779 return nvme_submit_io(ns, (void __user *)arg); 780 #ifdef CONFIG_BLK_DEV_NVME_SCSI 781 case SG_GET_VERSION_NUM: 782 return nvme_sg_get_version_num((void __user *)arg); 783 case SG_IO: 784 return nvme_sg_io(ns, (void __user *)arg); 785 #endif 786 default: 787 return -ENOTTY; 788 } 789 } 790 791 #ifdef CONFIG_COMPAT 792 static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode, 793 unsigned int cmd, unsigned long arg) 794 { 795 switch (cmd) { 796 case SG_IO: 797 return -ENOIOCTLCMD; 798 } 799 return nvme_ioctl(bdev, mode, cmd, arg); 800 } 801 #else 802 #define nvme_compat_ioctl NULL 803 #endif 804 805 static int nvme_open(struct block_device *bdev, fmode_t mode) 806 { 807 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO; 808 } 809 810 static void nvme_release(struct gendisk *disk, fmode_t mode) 811 { 812 struct nvme_ns *ns = disk->private_data; 813 814 module_put(ns->ctrl->ops->module); 815 nvme_put_ns(ns); 816 } 817 818 static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo) 819 { 820 /* some standard values */ 821 geo->heads = 1 << 6; 822 geo->sectors = 1 << 5; 823 geo->cylinders = get_capacity(bdev->bd_disk) >> 11; 824 return 0; 825 } 826 827 #ifdef CONFIG_BLK_DEV_INTEGRITY 828 static void nvme_init_integrity(struct nvme_ns *ns) 829 { 830 struct blk_integrity integrity; 831 832 memset(&integrity, 0, sizeof(integrity)); 833 switch (ns->pi_type) { 834 case NVME_NS_DPS_PI_TYPE3: 835 integrity.profile = &t10_pi_type3_crc; 836 integrity.tag_size = sizeof(u16) + sizeof(u32); 837 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE; 838 break; 839 case NVME_NS_DPS_PI_TYPE1: 840 case NVME_NS_DPS_PI_TYPE2: 841 integrity.profile = &t10_pi_type1_crc; 842 integrity.tag_size = sizeof(u16); 843 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE; 844 break; 845 default: 846 integrity.profile = NULL; 847 break; 848 } 849 integrity.tuple_size = ns->ms; 850 blk_integrity_register(ns->disk, &integrity); 851 blk_queue_max_integrity_segments(ns->queue, 1); 852 } 853 #else 854 static void nvme_init_integrity(struct nvme_ns *ns) 855 { 856 } 857 #endif /* CONFIG_BLK_DEV_INTEGRITY */ 858 859 static void nvme_config_discard(struct nvme_ns *ns) 860 { 861 struct nvme_ctrl *ctrl = ns->ctrl; 862 u32 logical_block_size = queue_logical_block_size(ns->queue); 863 864 if (ctrl->quirks & NVME_QUIRK_DISCARD_ZEROES) 865 ns->queue->limits.discard_zeroes_data = 1; 866 else 867 ns->queue->limits.discard_zeroes_data = 0; 868 869 ns->queue->limits.discard_alignment = logical_block_size; 870 ns->queue->limits.discard_granularity = logical_block_size; 871 blk_queue_max_discard_sectors(ns->queue, UINT_MAX); 872 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue); 873 } 874 875 static int nvme_revalidate_ns(struct nvme_ns *ns, struct nvme_id_ns **id) 876 { 877 if (nvme_identify_ns(ns->ctrl, ns->ns_id, id)) { 878 dev_warn(ns->ctrl->dev, "%s: Identify failure\n", __func__); 879 return -ENODEV; 880 } 881 882 if ((*id)->ncap == 0) { 883 kfree(*id); 884 return -ENODEV; 885 } 886 887 if (ns->ctrl->vs >= NVME_VS(1, 1, 0)) 888 memcpy(ns->eui, (*id)->eui64, sizeof(ns->eui)); 889 if (ns->ctrl->vs >= NVME_VS(1, 2, 0)) 890 memcpy(ns->uuid, (*id)->nguid, sizeof(ns->uuid)); 891 892 return 0; 893 } 894 895 static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id) 896 { 897 struct nvme_ns *ns = disk->private_data; 898 u8 lbaf, pi_type; 899 u16 old_ms; 900 unsigned short bs; 901 902 old_ms = ns->ms; 903 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK; 904 ns->lba_shift = id->lbaf[lbaf].ds; 905 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms); 906 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT); 907 908 /* 909 * If identify namespace failed, use default 512 byte block size so 910 * block layer can use before failing read/write for 0 capacity. 911 */ 912 if (ns->lba_shift == 0) 913 ns->lba_shift = 9; 914 bs = 1 << ns->lba_shift; 915 /* XXX: PI implementation requires metadata equal t10 pi tuple size */ 916 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ? 917 id->dps & NVME_NS_DPS_PI_MASK : 0; 918 919 blk_mq_freeze_queue(disk->queue); 920 if (blk_get_integrity(disk) && (ns->pi_type != pi_type || 921 ns->ms != old_ms || 922 bs != queue_logical_block_size(disk->queue) || 923 (ns->ms && ns->ext))) 924 blk_integrity_unregister(disk); 925 926 ns->pi_type = pi_type; 927 blk_queue_logical_block_size(ns->queue, bs); 928 929 if (ns->ms && !blk_get_integrity(disk) && !ns->ext) 930 nvme_init_integrity(ns); 931 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk)) 932 set_capacity(disk, 0); 933 else 934 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9)); 935 936 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM) 937 nvme_config_discard(ns); 938 blk_mq_unfreeze_queue(disk->queue); 939 } 940 941 static int nvme_revalidate_disk(struct gendisk *disk) 942 { 943 struct nvme_ns *ns = disk->private_data; 944 struct nvme_id_ns *id = NULL; 945 int ret; 946 947 if (test_bit(NVME_NS_DEAD, &ns->flags)) { 948 set_capacity(disk, 0); 949 return -ENODEV; 950 } 951 952 ret = nvme_revalidate_ns(ns, &id); 953 if (ret) 954 return ret; 955 956 __nvme_revalidate_disk(disk, id); 957 kfree(id); 958 959 return 0; 960 } 961 962 static char nvme_pr_type(enum pr_type type) 963 { 964 switch (type) { 965 case PR_WRITE_EXCLUSIVE: 966 return 1; 967 case PR_EXCLUSIVE_ACCESS: 968 return 2; 969 case PR_WRITE_EXCLUSIVE_REG_ONLY: 970 return 3; 971 case PR_EXCLUSIVE_ACCESS_REG_ONLY: 972 return 4; 973 case PR_WRITE_EXCLUSIVE_ALL_REGS: 974 return 5; 975 case PR_EXCLUSIVE_ACCESS_ALL_REGS: 976 return 6; 977 default: 978 return 0; 979 } 980 }; 981 982 static int nvme_pr_command(struct block_device *bdev, u32 cdw10, 983 u64 key, u64 sa_key, u8 op) 984 { 985 struct nvme_ns *ns = bdev->bd_disk->private_data; 986 struct nvme_command c; 987 u8 data[16] = { 0, }; 988 989 put_unaligned_le64(key, &data[0]); 990 put_unaligned_le64(sa_key, &data[8]); 991 992 memset(&c, 0, sizeof(c)); 993 c.common.opcode = op; 994 c.common.nsid = cpu_to_le32(ns->ns_id); 995 c.common.cdw10[0] = cpu_to_le32(cdw10); 996 997 return nvme_submit_sync_cmd(ns->queue, &c, data, 16); 998 } 999 1000 static int nvme_pr_register(struct block_device *bdev, u64 old, 1001 u64 new, unsigned flags) 1002 { 1003 u32 cdw10; 1004 1005 if (flags & ~PR_FL_IGNORE_KEY) 1006 return -EOPNOTSUPP; 1007 1008 cdw10 = old ? 2 : 0; 1009 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0; 1010 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */ 1011 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register); 1012 } 1013 1014 static int nvme_pr_reserve(struct block_device *bdev, u64 key, 1015 enum pr_type type, unsigned flags) 1016 { 1017 u32 cdw10; 1018 1019 if (flags & ~PR_FL_IGNORE_KEY) 1020 return -EOPNOTSUPP; 1021 1022 cdw10 = nvme_pr_type(type) << 8; 1023 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0); 1024 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire); 1025 } 1026 1027 static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new, 1028 enum pr_type type, bool abort) 1029 { 1030 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1; 1031 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire); 1032 } 1033 1034 static int nvme_pr_clear(struct block_device *bdev, u64 key) 1035 { 1036 u32 cdw10 = 1 | (key ? 1 << 3 : 0); 1037 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register); 1038 } 1039 1040 static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type) 1041 { 1042 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0; 1043 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release); 1044 } 1045 1046 static const struct pr_ops nvme_pr_ops = { 1047 .pr_register = nvme_pr_register, 1048 .pr_reserve = nvme_pr_reserve, 1049 .pr_release = nvme_pr_release, 1050 .pr_preempt = nvme_pr_preempt, 1051 .pr_clear = nvme_pr_clear, 1052 }; 1053 1054 static const struct block_device_operations nvme_fops = { 1055 .owner = THIS_MODULE, 1056 .ioctl = nvme_ioctl, 1057 .compat_ioctl = nvme_compat_ioctl, 1058 .open = nvme_open, 1059 .release = nvme_release, 1060 .getgeo = nvme_getgeo, 1061 .revalidate_disk= nvme_revalidate_disk, 1062 .pr_ops = &nvme_pr_ops, 1063 }; 1064 1065 static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled) 1066 { 1067 unsigned long timeout = 1068 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies; 1069 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0; 1070 int ret; 1071 1072 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) { 1073 if (csts == ~0) 1074 return -ENODEV; 1075 if ((csts & NVME_CSTS_RDY) == bit) 1076 break; 1077 1078 msleep(100); 1079 if (fatal_signal_pending(current)) 1080 return -EINTR; 1081 if (time_after(jiffies, timeout)) { 1082 dev_err(ctrl->device, 1083 "Device not ready; aborting %s\n", enabled ? 1084 "initialisation" : "reset"); 1085 return -ENODEV; 1086 } 1087 } 1088 1089 return ret; 1090 } 1091 1092 /* 1093 * If the device has been passed off to us in an enabled state, just clear 1094 * the enabled bit. The spec says we should set the 'shutdown notification 1095 * bits', but doing so may cause the device to complete commands to the 1096 * admin queue ... and we don't know what memory that might be pointing at! 1097 */ 1098 int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap) 1099 { 1100 int ret; 1101 1102 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK; 1103 ctrl->ctrl_config &= ~NVME_CC_ENABLE; 1104 1105 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); 1106 if (ret) 1107 return ret; 1108 1109 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY) 1110 msleep(NVME_QUIRK_DELAY_AMOUNT); 1111 1112 return nvme_wait_ready(ctrl, cap, false); 1113 } 1114 EXPORT_SYMBOL_GPL(nvme_disable_ctrl); 1115 1116 int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap) 1117 { 1118 /* 1119 * Default to a 4K page size, with the intention to update this 1120 * path in the future to accomodate architectures with differing 1121 * kernel and IO page sizes. 1122 */ 1123 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12; 1124 int ret; 1125 1126 if (page_shift < dev_page_min) { 1127 dev_err(ctrl->device, 1128 "Minimum device page size %u too large for host (%u)\n", 1129 1 << dev_page_min, 1 << page_shift); 1130 return -ENODEV; 1131 } 1132 1133 ctrl->page_size = 1 << page_shift; 1134 1135 ctrl->ctrl_config = NVME_CC_CSS_NVM; 1136 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT; 1137 ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE; 1138 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES; 1139 ctrl->ctrl_config |= NVME_CC_ENABLE; 1140 1141 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); 1142 if (ret) 1143 return ret; 1144 return nvme_wait_ready(ctrl, cap, true); 1145 } 1146 EXPORT_SYMBOL_GPL(nvme_enable_ctrl); 1147 1148 int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl) 1149 { 1150 unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies; 1151 u32 csts; 1152 int ret; 1153 1154 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK; 1155 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL; 1156 1157 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); 1158 if (ret) 1159 return ret; 1160 1161 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) { 1162 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT) 1163 break; 1164 1165 msleep(100); 1166 if (fatal_signal_pending(current)) 1167 return -EINTR; 1168 if (time_after(jiffies, timeout)) { 1169 dev_err(ctrl->device, 1170 "Device shutdown incomplete; abort shutdown\n"); 1171 return -ENODEV; 1172 } 1173 } 1174 1175 return ret; 1176 } 1177 EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl); 1178 1179 static void nvme_set_queue_limits(struct nvme_ctrl *ctrl, 1180 struct request_queue *q) 1181 { 1182 bool vwc = false; 1183 1184 if (ctrl->max_hw_sectors) { 1185 u32 max_segments = 1186 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1; 1187 1188 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors); 1189 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX)); 1190 } 1191 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) 1192 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors); 1193 blk_queue_virt_boundary(q, ctrl->page_size - 1); 1194 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT) 1195 vwc = true; 1196 blk_queue_write_cache(q, vwc, vwc); 1197 } 1198 1199 /* 1200 * Initialize the cached copies of the Identify data and various controller 1201 * register in our nvme_ctrl structure. This should be called as soon as 1202 * the admin queue is fully up and running. 1203 */ 1204 int nvme_init_identify(struct nvme_ctrl *ctrl) 1205 { 1206 struct nvme_id_ctrl *id; 1207 u64 cap; 1208 int ret, page_shift; 1209 u32 max_hw_sectors; 1210 1211 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs); 1212 if (ret) { 1213 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret); 1214 return ret; 1215 } 1216 1217 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap); 1218 if (ret) { 1219 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret); 1220 return ret; 1221 } 1222 page_shift = NVME_CAP_MPSMIN(cap) + 12; 1223 1224 if (ctrl->vs >= NVME_VS(1, 1, 0)) 1225 ctrl->subsystem = NVME_CAP_NSSRC(cap); 1226 1227 ret = nvme_identify_ctrl(ctrl, &id); 1228 if (ret) { 1229 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret); 1230 return -EIO; 1231 } 1232 1233 ctrl->vid = le16_to_cpu(id->vid); 1234 ctrl->oncs = le16_to_cpup(&id->oncs); 1235 atomic_set(&ctrl->abort_limit, id->acl + 1); 1236 ctrl->vwc = id->vwc; 1237 ctrl->cntlid = le16_to_cpup(&id->cntlid); 1238 memcpy(ctrl->serial, id->sn, sizeof(id->sn)); 1239 memcpy(ctrl->model, id->mn, sizeof(id->mn)); 1240 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr)); 1241 if (id->mdts) 1242 max_hw_sectors = 1 << (id->mdts + page_shift - 9); 1243 else 1244 max_hw_sectors = UINT_MAX; 1245 ctrl->max_hw_sectors = 1246 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors); 1247 1248 nvme_set_queue_limits(ctrl, ctrl->admin_q); 1249 ctrl->sgls = le32_to_cpu(id->sgls); 1250 ctrl->kas = le16_to_cpu(id->kas); 1251 1252 if (ctrl->ops->is_fabrics) { 1253 ctrl->icdoff = le16_to_cpu(id->icdoff); 1254 ctrl->ioccsz = le32_to_cpu(id->ioccsz); 1255 ctrl->iorcsz = le32_to_cpu(id->iorcsz); 1256 ctrl->maxcmd = le16_to_cpu(id->maxcmd); 1257 1258 /* 1259 * In fabrics we need to verify the cntlid matches the 1260 * admin connect 1261 */ 1262 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) 1263 ret = -EINVAL; 1264 1265 if (!ctrl->opts->discovery_nqn && !ctrl->kas) { 1266 dev_err(ctrl->dev, 1267 "keep-alive support is mandatory for fabrics\n"); 1268 ret = -EINVAL; 1269 } 1270 } else { 1271 ctrl->cntlid = le16_to_cpu(id->cntlid); 1272 } 1273 1274 kfree(id); 1275 return ret; 1276 } 1277 EXPORT_SYMBOL_GPL(nvme_init_identify); 1278 1279 static int nvme_dev_open(struct inode *inode, struct file *file) 1280 { 1281 struct nvme_ctrl *ctrl; 1282 int instance = iminor(inode); 1283 int ret = -ENODEV; 1284 1285 spin_lock(&dev_list_lock); 1286 list_for_each_entry(ctrl, &nvme_ctrl_list, node) { 1287 if (ctrl->instance != instance) 1288 continue; 1289 1290 if (!ctrl->admin_q) { 1291 ret = -EWOULDBLOCK; 1292 break; 1293 } 1294 if (!kref_get_unless_zero(&ctrl->kref)) 1295 break; 1296 file->private_data = ctrl; 1297 ret = 0; 1298 break; 1299 } 1300 spin_unlock(&dev_list_lock); 1301 1302 return ret; 1303 } 1304 1305 static int nvme_dev_release(struct inode *inode, struct file *file) 1306 { 1307 nvme_put_ctrl(file->private_data); 1308 return 0; 1309 } 1310 1311 static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp) 1312 { 1313 struct nvme_ns *ns; 1314 int ret; 1315 1316 mutex_lock(&ctrl->namespaces_mutex); 1317 if (list_empty(&ctrl->namespaces)) { 1318 ret = -ENOTTY; 1319 goto out_unlock; 1320 } 1321 1322 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list); 1323 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) { 1324 dev_warn(ctrl->device, 1325 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n"); 1326 ret = -EINVAL; 1327 goto out_unlock; 1328 } 1329 1330 dev_warn(ctrl->device, 1331 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n"); 1332 kref_get(&ns->kref); 1333 mutex_unlock(&ctrl->namespaces_mutex); 1334 1335 ret = nvme_user_cmd(ctrl, ns, argp); 1336 nvme_put_ns(ns); 1337 return ret; 1338 1339 out_unlock: 1340 mutex_unlock(&ctrl->namespaces_mutex); 1341 return ret; 1342 } 1343 1344 static long nvme_dev_ioctl(struct file *file, unsigned int cmd, 1345 unsigned long arg) 1346 { 1347 struct nvme_ctrl *ctrl = file->private_data; 1348 void __user *argp = (void __user *)arg; 1349 1350 switch (cmd) { 1351 case NVME_IOCTL_ADMIN_CMD: 1352 return nvme_user_cmd(ctrl, NULL, argp); 1353 case NVME_IOCTL_IO_CMD: 1354 return nvme_dev_user_cmd(ctrl, argp); 1355 case NVME_IOCTL_RESET: 1356 dev_warn(ctrl->device, "resetting controller\n"); 1357 return ctrl->ops->reset_ctrl(ctrl); 1358 case NVME_IOCTL_SUBSYS_RESET: 1359 return nvme_reset_subsystem(ctrl); 1360 case NVME_IOCTL_RESCAN: 1361 nvme_queue_scan(ctrl); 1362 return 0; 1363 default: 1364 return -ENOTTY; 1365 } 1366 } 1367 1368 static const struct file_operations nvme_dev_fops = { 1369 .owner = THIS_MODULE, 1370 .open = nvme_dev_open, 1371 .release = nvme_dev_release, 1372 .unlocked_ioctl = nvme_dev_ioctl, 1373 .compat_ioctl = nvme_dev_ioctl, 1374 }; 1375 1376 static ssize_t nvme_sysfs_reset(struct device *dev, 1377 struct device_attribute *attr, const char *buf, 1378 size_t count) 1379 { 1380 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 1381 int ret; 1382 1383 ret = ctrl->ops->reset_ctrl(ctrl); 1384 if (ret < 0) 1385 return ret; 1386 return count; 1387 } 1388 static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset); 1389 1390 static ssize_t nvme_sysfs_rescan(struct device *dev, 1391 struct device_attribute *attr, const char *buf, 1392 size_t count) 1393 { 1394 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 1395 1396 nvme_queue_scan(ctrl); 1397 return count; 1398 } 1399 static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan); 1400 1401 static ssize_t wwid_show(struct device *dev, struct device_attribute *attr, 1402 char *buf) 1403 { 1404 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 1405 struct nvme_ctrl *ctrl = ns->ctrl; 1406 int serial_len = sizeof(ctrl->serial); 1407 int model_len = sizeof(ctrl->model); 1408 1409 if (memchr_inv(ns->uuid, 0, sizeof(ns->uuid))) 1410 return sprintf(buf, "eui.%16phN\n", ns->uuid); 1411 1412 if (memchr_inv(ns->eui, 0, sizeof(ns->eui))) 1413 return sprintf(buf, "eui.%8phN\n", ns->eui); 1414 1415 while (ctrl->serial[serial_len - 1] == ' ') 1416 serial_len--; 1417 while (ctrl->model[model_len - 1] == ' ') 1418 model_len--; 1419 1420 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid, 1421 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id); 1422 } 1423 static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL); 1424 1425 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr, 1426 char *buf) 1427 { 1428 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 1429 return sprintf(buf, "%pU\n", ns->uuid); 1430 } 1431 static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL); 1432 1433 static ssize_t eui_show(struct device *dev, struct device_attribute *attr, 1434 char *buf) 1435 { 1436 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 1437 return sprintf(buf, "%8phd\n", ns->eui); 1438 } 1439 static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL); 1440 1441 static ssize_t nsid_show(struct device *dev, struct device_attribute *attr, 1442 char *buf) 1443 { 1444 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 1445 return sprintf(buf, "%d\n", ns->ns_id); 1446 } 1447 static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL); 1448 1449 static struct attribute *nvme_ns_attrs[] = { 1450 &dev_attr_wwid.attr, 1451 &dev_attr_uuid.attr, 1452 &dev_attr_eui.attr, 1453 &dev_attr_nsid.attr, 1454 NULL, 1455 }; 1456 1457 static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj, 1458 struct attribute *a, int n) 1459 { 1460 struct device *dev = container_of(kobj, struct device, kobj); 1461 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 1462 1463 if (a == &dev_attr_uuid.attr) { 1464 if (!memchr_inv(ns->uuid, 0, sizeof(ns->uuid))) 1465 return 0; 1466 } 1467 if (a == &dev_attr_eui.attr) { 1468 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui))) 1469 return 0; 1470 } 1471 return a->mode; 1472 } 1473 1474 static const struct attribute_group nvme_ns_attr_group = { 1475 .attrs = nvme_ns_attrs, 1476 .is_visible = nvme_ns_attrs_are_visible, 1477 }; 1478 1479 #define nvme_show_str_function(field) \ 1480 static ssize_t field##_show(struct device *dev, \ 1481 struct device_attribute *attr, char *buf) \ 1482 { \ 1483 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \ 1484 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \ 1485 } \ 1486 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL); 1487 1488 #define nvme_show_int_function(field) \ 1489 static ssize_t field##_show(struct device *dev, \ 1490 struct device_attribute *attr, char *buf) \ 1491 { \ 1492 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \ 1493 return sprintf(buf, "%d\n", ctrl->field); \ 1494 } \ 1495 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL); 1496 1497 nvme_show_str_function(model); 1498 nvme_show_str_function(serial); 1499 nvme_show_str_function(firmware_rev); 1500 nvme_show_int_function(cntlid); 1501 1502 static ssize_t nvme_sysfs_delete(struct device *dev, 1503 struct device_attribute *attr, const char *buf, 1504 size_t count) 1505 { 1506 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 1507 1508 if (device_remove_file_self(dev, attr)) 1509 ctrl->ops->delete_ctrl(ctrl); 1510 return count; 1511 } 1512 static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete); 1513 1514 static ssize_t nvme_sysfs_show_transport(struct device *dev, 1515 struct device_attribute *attr, 1516 char *buf) 1517 { 1518 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 1519 1520 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name); 1521 } 1522 static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL); 1523 1524 static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev, 1525 struct device_attribute *attr, 1526 char *buf) 1527 { 1528 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 1529 1530 return snprintf(buf, PAGE_SIZE, "%s\n", 1531 ctrl->ops->get_subsysnqn(ctrl)); 1532 } 1533 static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL); 1534 1535 static ssize_t nvme_sysfs_show_address(struct device *dev, 1536 struct device_attribute *attr, 1537 char *buf) 1538 { 1539 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 1540 1541 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE); 1542 } 1543 static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL); 1544 1545 static struct attribute *nvme_dev_attrs[] = { 1546 &dev_attr_reset_controller.attr, 1547 &dev_attr_rescan_controller.attr, 1548 &dev_attr_model.attr, 1549 &dev_attr_serial.attr, 1550 &dev_attr_firmware_rev.attr, 1551 &dev_attr_cntlid.attr, 1552 &dev_attr_delete_controller.attr, 1553 &dev_attr_transport.attr, 1554 &dev_attr_subsysnqn.attr, 1555 &dev_attr_address.attr, 1556 NULL 1557 }; 1558 1559 #define CHECK_ATTR(ctrl, a, name) \ 1560 if ((a) == &dev_attr_##name.attr && \ 1561 !(ctrl)->ops->get_##name) \ 1562 return 0 1563 1564 static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj, 1565 struct attribute *a, int n) 1566 { 1567 struct device *dev = container_of(kobj, struct device, kobj); 1568 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 1569 1570 if (a == &dev_attr_delete_controller.attr) { 1571 if (!ctrl->ops->delete_ctrl) 1572 return 0; 1573 } 1574 1575 CHECK_ATTR(ctrl, a, subsysnqn); 1576 CHECK_ATTR(ctrl, a, address); 1577 1578 return a->mode; 1579 } 1580 1581 static struct attribute_group nvme_dev_attrs_group = { 1582 .attrs = nvme_dev_attrs, 1583 .is_visible = nvme_dev_attrs_are_visible, 1584 }; 1585 1586 static const struct attribute_group *nvme_dev_attr_groups[] = { 1587 &nvme_dev_attrs_group, 1588 NULL, 1589 }; 1590 1591 static int ns_cmp(void *priv, struct list_head *a, struct list_head *b) 1592 { 1593 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list); 1594 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list); 1595 1596 return nsa->ns_id - nsb->ns_id; 1597 } 1598 1599 static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid) 1600 { 1601 struct nvme_ns *ns, *ret = NULL; 1602 1603 mutex_lock(&ctrl->namespaces_mutex); 1604 list_for_each_entry(ns, &ctrl->namespaces, list) { 1605 if (ns->ns_id == nsid) { 1606 kref_get(&ns->kref); 1607 ret = ns; 1608 break; 1609 } 1610 if (ns->ns_id > nsid) 1611 break; 1612 } 1613 mutex_unlock(&ctrl->namespaces_mutex); 1614 return ret; 1615 } 1616 1617 static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid) 1618 { 1619 struct nvme_ns *ns; 1620 struct gendisk *disk; 1621 struct nvme_id_ns *id; 1622 char disk_name[DISK_NAME_LEN]; 1623 int node = dev_to_node(ctrl->dev); 1624 1625 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node); 1626 if (!ns) 1627 return; 1628 1629 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL); 1630 if (ns->instance < 0) 1631 goto out_free_ns; 1632 1633 ns->queue = blk_mq_init_queue(ctrl->tagset); 1634 if (IS_ERR(ns->queue)) 1635 goto out_release_instance; 1636 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue); 1637 ns->queue->queuedata = ns; 1638 ns->ctrl = ctrl; 1639 1640 kref_init(&ns->kref); 1641 ns->ns_id = nsid; 1642 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */ 1643 1644 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift); 1645 nvme_set_queue_limits(ctrl, ns->queue); 1646 1647 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance); 1648 1649 if (nvme_revalidate_ns(ns, &id)) 1650 goto out_free_queue; 1651 1652 if (nvme_nvm_ns_supported(ns, id) && 1653 nvme_nvm_register(ns, disk_name, node)) { 1654 dev_warn(ctrl->dev, "%s: LightNVM init failure\n", __func__); 1655 goto out_free_id; 1656 } 1657 1658 disk = alloc_disk_node(0, node); 1659 if (!disk) 1660 goto out_free_id; 1661 1662 disk->fops = &nvme_fops; 1663 disk->private_data = ns; 1664 disk->queue = ns->queue; 1665 disk->flags = GENHD_FL_EXT_DEVT; 1666 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN); 1667 ns->disk = disk; 1668 1669 __nvme_revalidate_disk(disk, id); 1670 1671 mutex_lock(&ctrl->namespaces_mutex); 1672 list_add_tail(&ns->list, &ctrl->namespaces); 1673 mutex_unlock(&ctrl->namespaces_mutex); 1674 1675 kref_get(&ctrl->kref); 1676 1677 kfree(id); 1678 1679 device_add_disk(ctrl->device, ns->disk); 1680 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj, 1681 &nvme_ns_attr_group)) 1682 pr_warn("%s: failed to create sysfs group for identification\n", 1683 ns->disk->disk_name); 1684 if (ns->ndev && nvme_nvm_register_sysfs(ns)) 1685 pr_warn("%s: failed to register lightnvm sysfs group for identification\n", 1686 ns->disk->disk_name); 1687 return; 1688 out_free_id: 1689 kfree(id); 1690 out_free_queue: 1691 blk_cleanup_queue(ns->queue); 1692 out_release_instance: 1693 ida_simple_remove(&ctrl->ns_ida, ns->instance); 1694 out_free_ns: 1695 kfree(ns); 1696 } 1697 1698 static void nvme_ns_remove(struct nvme_ns *ns) 1699 { 1700 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags)) 1701 return; 1702 1703 if (ns->disk && ns->disk->flags & GENHD_FL_UP) { 1704 if (blk_get_integrity(ns->disk)) 1705 blk_integrity_unregister(ns->disk); 1706 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj, 1707 &nvme_ns_attr_group); 1708 if (ns->ndev) 1709 nvme_nvm_unregister_sysfs(ns); 1710 del_gendisk(ns->disk); 1711 blk_mq_abort_requeue_list(ns->queue); 1712 blk_cleanup_queue(ns->queue); 1713 } 1714 1715 mutex_lock(&ns->ctrl->namespaces_mutex); 1716 list_del_init(&ns->list); 1717 mutex_unlock(&ns->ctrl->namespaces_mutex); 1718 1719 nvme_put_ns(ns); 1720 } 1721 1722 static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid) 1723 { 1724 struct nvme_ns *ns; 1725 1726 ns = nvme_find_get_ns(ctrl, nsid); 1727 if (ns) { 1728 if (ns->disk && revalidate_disk(ns->disk)) 1729 nvme_ns_remove(ns); 1730 nvme_put_ns(ns); 1731 } else 1732 nvme_alloc_ns(ctrl, nsid); 1733 } 1734 1735 static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl, 1736 unsigned nsid) 1737 { 1738 struct nvme_ns *ns, *next; 1739 1740 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) { 1741 if (ns->ns_id > nsid) 1742 nvme_ns_remove(ns); 1743 } 1744 } 1745 1746 static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn) 1747 { 1748 struct nvme_ns *ns; 1749 __le32 *ns_list; 1750 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024); 1751 int ret = 0; 1752 1753 ns_list = kzalloc(0x1000, GFP_KERNEL); 1754 if (!ns_list) 1755 return -ENOMEM; 1756 1757 for (i = 0; i < num_lists; i++) { 1758 ret = nvme_identify_ns_list(ctrl, prev, ns_list); 1759 if (ret) 1760 goto free; 1761 1762 for (j = 0; j < min(nn, 1024U); j++) { 1763 nsid = le32_to_cpu(ns_list[j]); 1764 if (!nsid) 1765 goto out; 1766 1767 nvme_validate_ns(ctrl, nsid); 1768 1769 while (++prev < nsid) { 1770 ns = nvme_find_get_ns(ctrl, prev); 1771 if (ns) { 1772 nvme_ns_remove(ns); 1773 nvme_put_ns(ns); 1774 } 1775 } 1776 } 1777 nn -= j; 1778 } 1779 out: 1780 nvme_remove_invalid_namespaces(ctrl, prev); 1781 free: 1782 kfree(ns_list); 1783 return ret; 1784 } 1785 1786 static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn) 1787 { 1788 unsigned i; 1789 1790 for (i = 1; i <= nn; i++) 1791 nvme_validate_ns(ctrl, i); 1792 1793 nvme_remove_invalid_namespaces(ctrl, nn); 1794 } 1795 1796 static void nvme_scan_work(struct work_struct *work) 1797 { 1798 struct nvme_ctrl *ctrl = 1799 container_of(work, struct nvme_ctrl, scan_work); 1800 struct nvme_id_ctrl *id; 1801 unsigned nn; 1802 1803 if (ctrl->state != NVME_CTRL_LIVE) 1804 return; 1805 1806 if (nvme_identify_ctrl(ctrl, &id)) 1807 return; 1808 1809 nn = le32_to_cpu(id->nn); 1810 if (ctrl->vs >= NVME_VS(1, 1, 0) && 1811 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) { 1812 if (!nvme_scan_ns_list(ctrl, nn)) 1813 goto done; 1814 } 1815 nvme_scan_ns_sequential(ctrl, nn); 1816 done: 1817 mutex_lock(&ctrl->namespaces_mutex); 1818 list_sort(NULL, &ctrl->namespaces, ns_cmp); 1819 mutex_unlock(&ctrl->namespaces_mutex); 1820 kfree(id); 1821 } 1822 1823 void nvme_queue_scan(struct nvme_ctrl *ctrl) 1824 { 1825 /* 1826 * Do not queue new scan work when a controller is reset during 1827 * removal. 1828 */ 1829 if (ctrl->state == NVME_CTRL_LIVE) 1830 schedule_work(&ctrl->scan_work); 1831 } 1832 EXPORT_SYMBOL_GPL(nvme_queue_scan); 1833 1834 /* 1835 * This function iterates the namespace list unlocked to allow recovery from 1836 * controller failure. It is up to the caller to ensure the namespace list is 1837 * not modified by scan work while this function is executing. 1838 */ 1839 void nvme_remove_namespaces(struct nvme_ctrl *ctrl) 1840 { 1841 struct nvme_ns *ns, *next; 1842 1843 /* 1844 * The dead states indicates the controller was not gracefully 1845 * disconnected. In that case, we won't be able to flush any data while 1846 * removing the namespaces' disks; fail all the queues now to avoid 1847 * potentially having to clean up the failed sync later. 1848 */ 1849 if (ctrl->state == NVME_CTRL_DEAD) 1850 nvme_kill_queues(ctrl); 1851 1852 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) 1853 nvme_ns_remove(ns); 1854 } 1855 EXPORT_SYMBOL_GPL(nvme_remove_namespaces); 1856 1857 static void nvme_async_event_work(struct work_struct *work) 1858 { 1859 struct nvme_ctrl *ctrl = 1860 container_of(work, struct nvme_ctrl, async_event_work); 1861 1862 spin_lock_irq(&ctrl->lock); 1863 while (ctrl->event_limit > 0) { 1864 int aer_idx = --ctrl->event_limit; 1865 1866 spin_unlock_irq(&ctrl->lock); 1867 ctrl->ops->submit_async_event(ctrl, aer_idx); 1868 spin_lock_irq(&ctrl->lock); 1869 } 1870 spin_unlock_irq(&ctrl->lock); 1871 } 1872 1873 void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status, 1874 union nvme_result *res) 1875 { 1876 u32 result = le32_to_cpu(res->u32); 1877 bool done = true; 1878 1879 switch (le16_to_cpu(status) >> 1) { 1880 case NVME_SC_SUCCESS: 1881 done = false; 1882 /*FALLTHRU*/ 1883 case NVME_SC_ABORT_REQ: 1884 ++ctrl->event_limit; 1885 schedule_work(&ctrl->async_event_work); 1886 break; 1887 default: 1888 break; 1889 } 1890 1891 if (done) 1892 return; 1893 1894 switch (result & 0xff07) { 1895 case NVME_AER_NOTICE_NS_CHANGED: 1896 dev_info(ctrl->device, "rescanning\n"); 1897 nvme_queue_scan(ctrl); 1898 break; 1899 default: 1900 dev_warn(ctrl->device, "async event result %08x\n", result); 1901 } 1902 } 1903 EXPORT_SYMBOL_GPL(nvme_complete_async_event); 1904 1905 void nvme_queue_async_events(struct nvme_ctrl *ctrl) 1906 { 1907 ctrl->event_limit = NVME_NR_AERS; 1908 schedule_work(&ctrl->async_event_work); 1909 } 1910 EXPORT_SYMBOL_GPL(nvme_queue_async_events); 1911 1912 static DEFINE_IDA(nvme_instance_ida); 1913 1914 static int nvme_set_instance(struct nvme_ctrl *ctrl) 1915 { 1916 int instance, error; 1917 1918 do { 1919 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL)) 1920 return -ENODEV; 1921 1922 spin_lock(&dev_list_lock); 1923 error = ida_get_new(&nvme_instance_ida, &instance); 1924 spin_unlock(&dev_list_lock); 1925 } while (error == -EAGAIN); 1926 1927 if (error) 1928 return -ENODEV; 1929 1930 ctrl->instance = instance; 1931 return 0; 1932 } 1933 1934 static void nvme_release_instance(struct nvme_ctrl *ctrl) 1935 { 1936 spin_lock(&dev_list_lock); 1937 ida_remove(&nvme_instance_ida, ctrl->instance); 1938 spin_unlock(&dev_list_lock); 1939 } 1940 1941 void nvme_uninit_ctrl(struct nvme_ctrl *ctrl) 1942 { 1943 flush_work(&ctrl->async_event_work); 1944 flush_work(&ctrl->scan_work); 1945 nvme_remove_namespaces(ctrl); 1946 1947 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance)); 1948 1949 spin_lock(&dev_list_lock); 1950 list_del(&ctrl->node); 1951 spin_unlock(&dev_list_lock); 1952 } 1953 EXPORT_SYMBOL_GPL(nvme_uninit_ctrl); 1954 1955 static void nvme_free_ctrl(struct kref *kref) 1956 { 1957 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref); 1958 1959 put_device(ctrl->device); 1960 nvme_release_instance(ctrl); 1961 ida_destroy(&ctrl->ns_ida); 1962 1963 ctrl->ops->free_ctrl(ctrl); 1964 } 1965 1966 void nvme_put_ctrl(struct nvme_ctrl *ctrl) 1967 { 1968 kref_put(&ctrl->kref, nvme_free_ctrl); 1969 } 1970 EXPORT_SYMBOL_GPL(nvme_put_ctrl); 1971 1972 /* 1973 * Initialize a NVMe controller structures. This needs to be called during 1974 * earliest initialization so that we have the initialized structured around 1975 * during probing. 1976 */ 1977 int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev, 1978 const struct nvme_ctrl_ops *ops, unsigned long quirks) 1979 { 1980 int ret; 1981 1982 ctrl->state = NVME_CTRL_NEW; 1983 spin_lock_init(&ctrl->lock); 1984 INIT_LIST_HEAD(&ctrl->namespaces); 1985 mutex_init(&ctrl->namespaces_mutex); 1986 kref_init(&ctrl->kref); 1987 ctrl->dev = dev; 1988 ctrl->ops = ops; 1989 ctrl->quirks = quirks; 1990 INIT_WORK(&ctrl->scan_work, nvme_scan_work); 1991 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work); 1992 1993 ret = nvme_set_instance(ctrl); 1994 if (ret) 1995 goto out; 1996 1997 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev, 1998 MKDEV(nvme_char_major, ctrl->instance), 1999 ctrl, nvme_dev_attr_groups, 2000 "nvme%d", ctrl->instance); 2001 if (IS_ERR(ctrl->device)) { 2002 ret = PTR_ERR(ctrl->device); 2003 goto out_release_instance; 2004 } 2005 get_device(ctrl->device); 2006 ida_init(&ctrl->ns_ida); 2007 2008 spin_lock(&dev_list_lock); 2009 list_add_tail(&ctrl->node, &nvme_ctrl_list); 2010 spin_unlock(&dev_list_lock); 2011 2012 return 0; 2013 out_release_instance: 2014 nvme_release_instance(ctrl); 2015 out: 2016 return ret; 2017 } 2018 EXPORT_SYMBOL_GPL(nvme_init_ctrl); 2019 2020 /** 2021 * nvme_kill_queues(): Ends all namespace queues 2022 * @ctrl: the dead controller that needs to end 2023 * 2024 * Call this function when the driver determines it is unable to get the 2025 * controller in a state capable of servicing IO. 2026 */ 2027 void nvme_kill_queues(struct nvme_ctrl *ctrl) 2028 { 2029 struct nvme_ns *ns; 2030 2031 mutex_lock(&ctrl->namespaces_mutex); 2032 list_for_each_entry(ns, &ctrl->namespaces, list) { 2033 /* 2034 * Revalidating a dead namespace sets capacity to 0. This will 2035 * end buffered writers dirtying pages that can't be synced. 2036 */ 2037 if (ns->disk && !test_and_set_bit(NVME_NS_DEAD, &ns->flags)) 2038 revalidate_disk(ns->disk); 2039 2040 blk_set_queue_dying(ns->queue); 2041 blk_mq_abort_requeue_list(ns->queue); 2042 blk_mq_start_stopped_hw_queues(ns->queue, true); 2043 } 2044 mutex_unlock(&ctrl->namespaces_mutex); 2045 } 2046 EXPORT_SYMBOL_GPL(nvme_kill_queues); 2047 2048 void nvme_stop_queues(struct nvme_ctrl *ctrl) 2049 { 2050 struct nvme_ns *ns; 2051 2052 mutex_lock(&ctrl->namespaces_mutex); 2053 list_for_each_entry(ns, &ctrl->namespaces, list) 2054 blk_mq_quiesce_queue(ns->queue); 2055 mutex_unlock(&ctrl->namespaces_mutex); 2056 } 2057 EXPORT_SYMBOL_GPL(nvme_stop_queues); 2058 2059 void nvme_start_queues(struct nvme_ctrl *ctrl) 2060 { 2061 struct nvme_ns *ns; 2062 2063 mutex_lock(&ctrl->namespaces_mutex); 2064 list_for_each_entry(ns, &ctrl->namespaces, list) { 2065 blk_mq_start_stopped_hw_queues(ns->queue, true); 2066 blk_mq_kick_requeue_list(ns->queue); 2067 } 2068 mutex_unlock(&ctrl->namespaces_mutex); 2069 } 2070 EXPORT_SYMBOL_GPL(nvme_start_queues); 2071 2072 int __init nvme_core_init(void) 2073 { 2074 int result; 2075 2076 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme", 2077 &nvme_dev_fops); 2078 if (result < 0) 2079 return result; 2080 else if (result > 0) 2081 nvme_char_major = result; 2082 2083 nvme_class = class_create(THIS_MODULE, "nvme"); 2084 if (IS_ERR(nvme_class)) { 2085 result = PTR_ERR(nvme_class); 2086 goto unregister_chrdev; 2087 } 2088 2089 return 0; 2090 2091 unregister_chrdev: 2092 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme"); 2093 return result; 2094 } 2095 2096 void nvme_core_exit(void) 2097 { 2098 class_destroy(nvme_class); 2099 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme"); 2100 } 2101 2102 MODULE_LICENSE("GPL"); 2103 MODULE_VERSION("1.0"); 2104 module_init(nvme_core_init); 2105 module_exit(nvme_core_exit); 2106