1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Block driver for media (i.e., flash cards) 4 * 5 * Copyright 2002 Hewlett-Packard Company 6 * Copyright 2005-2008 Pierre Ossman 7 * 8 * Use consistent with the GNU GPL is permitted, 9 * provided that this copyright notice is 10 * preserved in its entirety in all copies and derived works. 11 * 12 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED, 13 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS 14 * FITNESS FOR ANY PARTICULAR PURPOSE. 15 * 16 * Many thanks to Alessandro Rubini and Jonathan Corbet! 17 * 18 * Author: Andrew Christian 19 * 28 May 2002 20 */ 21 #include <linux/moduleparam.h> 22 #include <linux/module.h> 23 #include <linux/init.h> 24 25 #include <linux/kernel.h> 26 #include <linux/fs.h> 27 #include <linux/slab.h> 28 #include <linux/errno.h> 29 #include <linux/hdreg.h> 30 #include <linux/kdev_t.h> 31 #include <linux/blkdev.h> 32 #include <linux/cdev.h> 33 #include <linux/mutex.h> 34 #include <linux/scatterlist.h> 35 #include <linux/string_helpers.h> 36 #include <linux/delay.h> 37 #include <linux/capability.h> 38 #include <linux/compat.h> 39 #include <linux/pm_runtime.h> 40 #include <linux/idr.h> 41 #include <linux/debugfs.h> 42 43 #include <linux/mmc/ioctl.h> 44 #include <linux/mmc/card.h> 45 #include <linux/mmc/host.h> 46 #include <linux/mmc/mmc.h> 47 #include <linux/mmc/sd.h> 48 49 #include <linux/uaccess.h> 50 51 #include "queue.h" 52 #include "block.h" 53 #include "core.h" 54 #include "card.h" 55 #include "crypto.h" 56 #include "host.h" 57 #include "bus.h" 58 #include "mmc_ops.h" 59 #include "quirks.h" 60 #include "sd_ops.h" 61 62 MODULE_ALIAS("mmc:block"); 63 #ifdef MODULE_PARAM_PREFIX 64 #undef MODULE_PARAM_PREFIX 65 #endif 66 #define MODULE_PARAM_PREFIX "mmcblk." 67 68 /* 69 * Set a 10 second timeout for polling write request busy state. Note, mmc core 70 * is setting a 3 second timeout for SD cards, and SDHCI has long had a 10 71 * second software timer to timeout the whole request, so 10 seconds should be 72 * ample. 73 */ 74 #define MMC_BLK_TIMEOUT_MS (10 * 1000) 75 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16) 76 #define MMC_EXTRACT_VALUE_FROM_ARG(x) ((x & 0x0000FF00) >> 8) 77 78 #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \ 79 (rq_data_dir(req) == WRITE)) 80 static DEFINE_MUTEX(block_mutex); 81 82 /* 83 * The defaults come from config options but can be overriden by module 84 * or bootarg options. 85 */ 86 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS; 87 88 /* 89 * We've only got one major, so number of mmcblk devices is 90 * limited to (1 << 20) / number of minors per device. It is also 91 * limited by the MAX_DEVICES below. 92 */ 93 static int max_devices; 94 95 #define MAX_DEVICES 256 96 97 static DEFINE_IDA(mmc_blk_ida); 98 static DEFINE_IDA(mmc_rpmb_ida); 99 100 /* 101 * There is one mmc_blk_data per slot. 102 */ 103 struct mmc_blk_data { 104 struct device *parent; 105 struct gendisk *disk; 106 struct mmc_queue queue; 107 struct list_head part; 108 struct list_head rpmbs; 109 110 unsigned int flags; 111 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */ 112 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */ 113 114 unsigned int usage; 115 unsigned int read_only; 116 unsigned int part_type; 117 unsigned int reset_done; 118 #define MMC_BLK_READ BIT(0) 119 #define MMC_BLK_WRITE BIT(1) 120 #define MMC_BLK_DISCARD BIT(2) 121 #define MMC_BLK_SECDISCARD BIT(3) 122 #define MMC_BLK_CQE_RECOVERY BIT(4) 123 124 /* 125 * Only set in main mmc_blk_data associated 126 * with mmc_card with dev_set_drvdata, and keeps 127 * track of the current selected device partition. 128 */ 129 unsigned int part_curr; 130 struct device_attribute force_ro; 131 struct device_attribute power_ro_lock; 132 int area_type; 133 134 /* debugfs files (only in main mmc_blk_data) */ 135 struct dentry *status_dentry; 136 struct dentry *ext_csd_dentry; 137 }; 138 139 /* Device type for RPMB character devices */ 140 static dev_t mmc_rpmb_devt; 141 142 /* Bus type for RPMB character devices */ 143 static struct bus_type mmc_rpmb_bus_type = { 144 .name = "mmc_rpmb", 145 }; 146 147 /** 148 * struct mmc_rpmb_data - special RPMB device type for these areas 149 * @dev: the device for the RPMB area 150 * @chrdev: character device for the RPMB area 151 * @id: unique device ID number 152 * @part_index: partition index (0 on first) 153 * @md: parent MMC block device 154 * @node: list item, so we can put this device on a list 155 */ 156 struct mmc_rpmb_data { 157 struct device dev; 158 struct cdev chrdev; 159 int id; 160 unsigned int part_index; 161 struct mmc_blk_data *md; 162 struct list_head node; 163 }; 164 165 static DEFINE_MUTEX(open_lock); 166 167 module_param(perdev_minors, int, 0444); 168 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device"); 169 170 static inline int mmc_blk_part_switch(struct mmc_card *card, 171 unsigned int part_type); 172 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, 173 struct mmc_card *card, 174 int disable_multi, 175 struct mmc_queue *mq); 176 static void mmc_blk_hsq_req_done(struct mmc_request *mrq); 177 178 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk) 179 { 180 struct mmc_blk_data *md; 181 182 mutex_lock(&open_lock); 183 md = disk->private_data; 184 if (md && md->usage == 0) 185 md = NULL; 186 if (md) 187 md->usage++; 188 mutex_unlock(&open_lock); 189 190 return md; 191 } 192 193 static inline int mmc_get_devidx(struct gendisk *disk) 194 { 195 int devidx = disk->first_minor / perdev_minors; 196 return devidx; 197 } 198 199 static void mmc_blk_put(struct mmc_blk_data *md) 200 { 201 mutex_lock(&open_lock); 202 md->usage--; 203 if (md->usage == 0) { 204 int devidx = mmc_get_devidx(md->disk); 205 206 ida_simple_remove(&mmc_blk_ida, devidx); 207 put_disk(md->disk); 208 kfree(md); 209 } 210 mutex_unlock(&open_lock); 211 } 212 213 static ssize_t power_ro_lock_show(struct device *dev, 214 struct device_attribute *attr, char *buf) 215 { 216 int ret; 217 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); 218 struct mmc_card *card = md->queue.card; 219 int locked = 0; 220 221 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN) 222 locked = 2; 223 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN) 224 locked = 1; 225 226 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked); 227 228 mmc_blk_put(md); 229 230 return ret; 231 } 232 233 static ssize_t power_ro_lock_store(struct device *dev, 234 struct device_attribute *attr, const char *buf, size_t count) 235 { 236 int ret; 237 struct mmc_blk_data *md, *part_md; 238 struct mmc_queue *mq; 239 struct request *req; 240 unsigned long set; 241 242 if (kstrtoul(buf, 0, &set)) 243 return -EINVAL; 244 245 if (set != 1) 246 return count; 247 248 md = mmc_blk_get(dev_to_disk(dev)); 249 mq = &md->queue; 250 251 /* Dispatch locking to the block layer */ 252 req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, 0); 253 if (IS_ERR(req)) { 254 count = PTR_ERR(req); 255 goto out_put; 256 } 257 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP; 258 blk_execute_rq(NULL, req, 0); 259 ret = req_to_mmc_queue_req(req)->drv_op_result; 260 blk_put_request(req); 261 262 if (!ret) { 263 pr_info("%s: Locking boot partition ro until next power on\n", 264 md->disk->disk_name); 265 set_disk_ro(md->disk, 1); 266 267 list_for_each_entry(part_md, &md->part, part) 268 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) { 269 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name); 270 set_disk_ro(part_md->disk, 1); 271 } 272 } 273 out_put: 274 mmc_blk_put(md); 275 return count; 276 } 277 278 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr, 279 char *buf) 280 { 281 int ret; 282 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); 283 284 ret = snprintf(buf, PAGE_SIZE, "%d\n", 285 get_disk_ro(dev_to_disk(dev)) ^ 286 md->read_only); 287 mmc_blk_put(md); 288 return ret; 289 } 290 291 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr, 292 const char *buf, size_t count) 293 { 294 int ret; 295 char *end; 296 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); 297 unsigned long set = simple_strtoul(buf, &end, 0); 298 if (end == buf) { 299 ret = -EINVAL; 300 goto out; 301 } 302 303 set_disk_ro(dev_to_disk(dev), set || md->read_only); 304 ret = count; 305 out: 306 mmc_blk_put(md); 307 return ret; 308 } 309 310 static int mmc_blk_open(struct block_device *bdev, fmode_t mode) 311 { 312 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk); 313 int ret = -ENXIO; 314 315 mutex_lock(&block_mutex); 316 if (md) { 317 ret = 0; 318 if ((mode & FMODE_WRITE) && md->read_only) { 319 mmc_blk_put(md); 320 ret = -EROFS; 321 } 322 } 323 mutex_unlock(&block_mutex); 324 325 return ret; 326 } 327 328 static void mmc_blk_release(struct gendisk *disk, fmode_t mode) 329 { 330 struct mmc_blk_data *md = disk->private_data; 331 332 mutex_lock(&block_mutex); 333 mmc_blk_put(md); 334 mutex_unlock(&block_mutex); 335 } 336 337 static int 338 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 339 { 340 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16); 341 geo->heads = 4; 342 geo->sectors = 16; 343 return 0; 344 } 345 346 struct mmc_blk_ioc_data { 347 struct mmc_ioc_cmd ic; 348 unsigned char *buf; 349 u64 buf_bytes; 350 struct mmc_rpmb_data *rpmb; 351 }; 352 353 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user( 354 struct mmc_ioc_cmd __user *user) 355 { 356 struct mmc_blk_ioc_data *idata; 357 int err; 358 359 idata = kmalloc(sizeof(*idata), GFP_KERNEL); 360 if (!idata) { 361 err = -ENOMEM; 362 goto out; 363 } 364 365 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) { 366 err = -EFAULT; 367 goto idata_err; 368 } 369 370 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks; 371 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) { 372 err = -EOVERFLOW; 373 goto idata_err; 374 } 375 376 if (!idata->buf_bytes) { 377 idata->buf = NULL; 378 return idata; 379 } 380 381 idata->buf = memdup_user((void __user *)(unsigned long) 382 idata->ic.data_ptr, idata->buf_bytes); 383 if (IS_ERR(idata->buf)) { 384 err = PTR_ERR(idata->buf); 385 goto idata_err; 386 } 387 388 return idata; 389 390 idata_err: 391 kfree(idata); 392 out: 393 return ERR_PTR(err); 394 } 395 396 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr, 397 struct mmc_blk_ioc_data *idata) 398 { 399 struct mmc_ioc_cmd *ic = &idata->ic; 400 401 if (copy_to_user(&(ic_ptr->response), ic->response, 402 sizeof(ic->response))) 403 return -EFAULT; 404 405 if (!idata->ic.write_flag) { 406 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr, 407 idata->buf, idata->buf_bytes)) 408 return -EFAULT; 409 } 410 411 return 0; 412 } 413 414 static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms, 415 u32 *resp_errs) 416 { 417 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); 418 int err = 0; 419 u32 status; 420 421 do { 422 bool done = time_after(jiffies, timeout); 423 424 err = __mmc_send_status(card, &status, 5); 425 if (err) { 426 dev_err(mmc_dev(card->host), 427 "error %d requesting status\n", err); 428 return err; 429 } 430 431 /* Accumulate any response error bits seen */ 432 if (resp_errs) 433 *resp_errs |= status; 434 435 /* 436 * Timeout if the device never becomes ready for data and never 437 * leaves the program state. 438 */ 439 if (done) { 440 dev_err(mmc_dev(card->host), 441 "Card stuck in wrong state! %s status: %#x\n", 442 __func__, status); 443 return -ETIMEDOUT; 444 } 445 } while (!mmc_ready_for_data(status)); 446 447 return err; 448 } 449 450 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md, 451 struct mmc_blk_ioc_data *idata) 452 { 453 struct mmc_command cmd = {}, sbc = {}; 454 struct mmc_data data = {}; 455 struct mmc_request mrq = {}; 456 struct scatterlist sg; 457 int err; 458 unsigned int target_part; 459 460 if (!card || !md || !idata) 461 return -EINVAL; 462 463 /* 464 * The RPMB accesses comes in from the character device, so we 465 * need to target these explicitly. Else we just target the 466 * partition type for the block device the ioctl() was issued 467 * on. 468 */ 469 if (idata->rpmb) { 470 /* Support multiple RPMB partitions */ 471 target_part = idata->rpmb->part_index; 472 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB; 473 } else { 474 target_part = md->part_type; 475 } 476 477 cmd.opcode = idata->ic.opcode; 478 cmd.arg = idata->ic.arg; 479 cmd.flags = idata->ic.flags; 480 481 if (idata->buf_bytes) { 482 data.sg = &sg; 483 data.sg_len = 1; 484 data.blksz = idata->ic.blksz; 485 data.blocks = idata->ic.blocks; 486 487 sg_init_one(data.sg, idata->buf, idata->buf_bytes); 488 489 if (idata->ic.write_flag) 490 data.flags = MMC_DATA_WRITE; 491 else 492 data.flags = MMC_DATA_READ; 493 494 /* data.flags must already be set before doing this. */ 495 mmc_set_data_timeout(&data, card); 496 497 /* Allow overriding the timeout_ns for empirical tuning. */ 498 if (idata->ic.data_timeout_ns) 499 data.timeout_ns = idata->ic.data_timeout_ns; 500 501 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) { 502 /* 503 * Pretend this is a data transfer and rely on the 504 * host driver to compute timeout. When all host 505 * drivers support cmd.cmd_timeout for R1B, this 506 * can be changed to: 507 * 508 * mrq.data = NULL; 509 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms; 510 */ 511 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000; 512 } 513 514 mrq.data = &data; 515 } 516 517 mrq.cmd = &cmd; 518 519 err = mmc_blk_part_switch(card, target_part); 520 if (err) 521 return err; 522 523 if (idata->ic.is_acmd) { 524 err = mmc_app_cmd(card->host, card); 525 if (err) 526 return err; 527 } 528 529 if (idata->rpmb) { 530 sbc.opcode = MMC_SET_BLOCK_COUNT; 531 /* 532 * We don't do any blockcount validation because the max size 533 * may be increased by a future standard. We just copy the 534 * 'Reliable Write' bit here. 535 */ 536 sbc.arg = data.blocks | (idata->ic.write_flag & BIT(31)); 537 sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; 538 mrq.sbc = &sbc; 539 } 540 541 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) && 542 (cmd.opcode == MMC_SWITCH)) 543 return mmc_sanitize(card, idata->ic.cmd_timeout_ms); 544 545 mmc_wait_for_req(card->host, &mrq); 546 547 if (cmd.error) { 548 dev_err(mmc_dev(card->host), "%s: cmd error %d\n", 549 __func__, cmd.error); 550 return cmd.error; 551 } 552 if (data.error) { 553 dev_err(mmc_dev(card->host), "%s: data error %d\n", 554 __func__, data.error); 555 return data.error; 556 } 557 558 /* 559 * Make sure the cache of the PARTITION_CONFIG register and 560 * PARTITION_ACCESS bits is updated in case the ioctl ext_csd write 561 * changed it successfully. 562 */ 563 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_PART_CONFIG) && 564 (cmd.opcode == MMC_SWITCH)) { 565 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev); 566 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg); 567 568 /* 569 * Update cache so the next mmc_blk_part_switch call operates 570 * on up-to-date data. 571 */ 572 card->ext_csd.part_config = value; 573 main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK; 574 } 575 576 /* 577 * Make sure to update CACHE_CTRL in case it was changed. The cache 578 * will get turned back on if the card is re-initialized, e.g. 579 * suspend/resume or hw reset in recovery. 580 */ 581 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_CACHE_CTRL) && 582 (cmd.opcode == MMC_SWITCH)) { 583 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg) & 1; 584 585 card->ext_csd.cache_ctrl = value; 586 } 587 588 /* 589 * According to the SD specs, some commands require a delay after 590 * issuing the command. 591 */ 592 if (idata->ic.postsleep_min_us) 593 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us); 594 595 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp)); 596 597 if (idata->rpmb || (cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) { 598 /* 599 * Ensure RPMB/R1B command has completed by polling CMD13 600 * "Send Status". 601 */ 602 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, NULL); 603 } 604 605 return err; 606 } 607 608 static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md, 609 struct mmc_ioc_cmd __user *ic_ptr, 610 struct mmc_rpmb_data *rpmb) 611 { 612 struct mmc_blk_ioc_data *idata; 613 struct mmc_blk_ioc_data *idatas[1]; 614 struct mmc_queue *mq; 615 struct mmc_card *card; 616 int err = 0, ioc_err = 0; 617 struct request *req; 618 619 idata = mmc_blk_ioctl_copy_from_user(ic_ptr); 620 if (IS_ERR(idata)) 621 return PTR_ERR(idata); 622 /* This will be NULL on non-RPMB ioctl():s */ 623 idata->rpmb = rpmb; 624 625 card = md->queue.card; 626 if (IS_ERR(card)) { 627 err = PTR_ERR(card); 628 goto cmd_done; 629 } 630 631 /* 632 * Dispatch the ioctl() into the block request queue. 633 */ 634 mq = &md->queue; 635 req = blk_get_request(mq->queue, 636 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0); 637 if (IS_ERR(req)) { 638 err = PTR_ERR(req); 639 goto cmd_done; 640 } 641 idatas[0] = idata; 642 req_to_mmc_queue_req(req)->drv_op = 643 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL; 644 req_to_mmc_queue_req(req)->drv_op_data = idatas; 645 req_to_mmc_queue_req(req)->ioc_count = 1; 646 blk_execute_rq(NULL, req, 0); 647 ioc_err = req_to_mmc_queue_req(req)->drv_op_result; 648 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata); 649 blk_put_request(req); 650 651 cmd_done: 652 kfree(idata->buf); 653 kfree(idata); 654 return ioc_err ? ioc_err : err; 655 } 656 657 static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md, 658 struct mmc_ioc_multi_cmd __user *user, 659 struct mmc_rpmb_data *rpmb) 660 { 661 struct mmc_blk_ioc_data **idata = NULL; 662 struct mmc_ioc_cmd __user *cmds = user->cmds; 663 struct mmc_card *card; 664 struct mmc_queue *mq; 665 int i, err = 0, ioc_err = 0; 666 __u64 num_of_cmds; 667 struct request *req; 668 669 if (copy_from_user(&num_of_cmds, &user->num_of_cmds, 670 sizeof(num_of_cmds))) 671 return -EFAULT; 672 673 if (!num_of_cmds) 674 return 0; 675 676 if (num_of_cmds > MMC_IOC_MAX_CMDS) 677 return -EINVAL; 678 679 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL); 680 if (!idata) 681 return -ENOMEM; 682 683 for (i = 0; i < num_of_cmds; i++) { 684 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]); 685 if (IS_ERR(idata[i])) { 686 err = PTR_ERR(idata[i]); 687 num_of_cmds = i; 688 goto cmd_err; 689 } 690 /* This will be NULL on non-RPMB ioctl():s */ 691 idata[i]->rpmb = rpmb; 692 } 693 694 card = md->queue.card; 695 if (IS_ERR(card)) { 696 err = PTR_ERR(card); 697 goto cmd_err; 698 } 699 700 701 /* 702 * Dispatch the ioctl()s into the block request queue. 703 */ 704 mq = &md->queue; 705 req = blk_get_request(mq->queue, 706 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0); 707 if (IS_ERR(req)) { 708 err = PTR_ERR(req); 709 goto cmd_err; 710 } 711 req_to_mmc_queue_req(req)->drv_op = 712 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL; 713 req_to_mmc_queue_req(req)->drv_op_data = idata; 714 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds; 715 blk_execute_rq(NULL, req, 0); 716 ioc_err = req_to_mmc_queue_req(req)->drv_op_result; 717 718 /* copy to user if data and response */ 719 for (i = 0; i < num_of_cmds && !err; i++) 720 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]); 721 722 blk_put_request(req); 723 724 cmd_err: 725 for (i = 0; i < num_of_cmds; i++) { 726 kfree(idata[i]->buf); 727 kfree(idata[i]); 728 } 729 kfree(idata); 730 return ioc_err ? ioc_err : err; 731 } 732 733 static int mmc_blk_check_blkdev(struct block_device *bdev) 734 { 735 /* 736 * The caller must have CAP_SYS_RAWIO, and must be calling this on the 737 * whole block device, not on a partition. This prevents overspray 738 * between sibling partitions. 739 */ 740 if (!capable(CAP_SYS_RAWIO) || bdev_is_partition(bdev)) 741 return -EPERM; 742 return 0; 743 } 744 745 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode, 746 unsigned int cmd, unsigned long arg) 747 { 748 struct mmc_blk_data *md; 749 int ret; 750 751 switch (cmd) { 752 case MMC_IOC_CMD: 753 ret = mmc_blk_check_blkdev(bdev); 754 if (ret) 755 return ret; 756 md = mmc_blk_get(bdev->bd_disk); 757 if (!md) 758 return -EINVAL; 759 ret = mmc_blk_ioctl_cmd(md, 760 (struct mmc_ioc_cmd __user *)arg, 761 NULL); 762 mmc_blk_put(md); 763 return ret; 764 case MMC_IOC_MULTI_CMD: 765 ret = mmc_blk_check_blkdev(bdev); 766 if (ret) 767 return ret; 768 md = mmc_blk_get(bdev->bd_disk); 769 if (!md) 770 return -EINVAL; 771 ret = mmc_blk_ioctl_multi_cmd(md, 772 (struct mmc_ioc_multi_cmd __user *)arg, 773 NULL); 774 mmc_blk_put(md); 775 return ret; 776 default: 777 return -EINVAL; 778 } 779 } 780 781 #ifdef CONFIG_COMPAT 782 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode, 783 unsigned int cmd, unsigned long arg) 784 { 785 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg)); 786 } 787 #endif 788 789 static const struct block_device_operations mmc_bdops = { 790 .open = mmc_blk_open, 791 .release = mmc_blk_release, 792 .getgeo = mmc_blk_getgeo, 793 .owner = THIS_MODULE, 794 .ioctl = mmc_blk_ioctl, 795 #ifdef CONFIG_COMPAT 796 .compat_ioctl = mmc_blk_compat_ioctl, 797 #endif 798 }; 799 800 static int mmc_blk_part_switch_pre(struct mmc_card *card, 801 unsigned int part_type) 802 { 803 int ret = 0; 804 805 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 806 if (card->ext_csd.cmdq_en) { 807 ret = mmc_cmdq_disable(card); 808 if (ret) 809 return ret; 810 } 811 mmc_retune_pause(card->host); 812 } 813 814 return ret; 815 } 816 817 static int mmc_blk_part_switch_post(struct mmc_card *card, 818 unsigned int part_type) 819 { 820 int ret = 0; 821 822 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 823 mmc_retune_unpause(card->host); 824 if (card->reenable_cmdq && !card->ext_csd.cmdq_en) 825 ret = mmc_cmdq_enable(card); 826 } 827 828 return ret; 829 } 830 831 static inline int mmc_blk_part_switch(struct mmc_card *card, 832 unsigned int part_type) 833 { 834 int ret = 0; 835 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev); 836 837 if (main_md->part_curr == part_type) 838 return 0; 839 840 if (mmc_card_mmc(card)) { 841 u8 part_config = card->ext_csd.part_config; 842 843 ret = mmc_blk_part_switch_pre(card, part_type); 844 if (ret) 845 return ret; 846 847 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK; 848 part_config |= part_type; 849 850 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 851 EXT_CSD_PART_CONFIG, part_config, 852 card->ext_csd.part_time); 853 if (ret) { 854 mmc_blk_part_switch_post(card, part_type); 855 return ret; 856 } 857 858 card->ext_csd.part_config = part_config; 859 860 ret = mmc_blk_part_switch_post(card, main_md->part_curr); 861 } 862 863 main_md->part_curr = part_type; 864 return ret; 865 } 866 867 static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks) 868 { 869 int err; 870 u32 result; 871 __be32 *blocks; 872 873 struct mmc_request mrq = {}; 874 struct mmc_command cmd = {}; 875 struct mmc_data data = {}; 876 877 struct scatterlist sg; 878 879 cmd.opcode = MMC_APP_CMD; 880 cmd.arg = card->rca << 16; 881 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; 882 883 err = mmc_wait_for_cmd(card->host, &cmd, 0); 884 if (err) 885 return err; 886 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD)) 887 return -EIO; 888 889 memset(&cmd, 0, sizeof(struct mmc_command)); 890 891 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS; 892 cmd.arg = 0; 893 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; 894 895 data.blksz = 4; 896 data.blocks = 1; 897 data.flags = MMC_DATA_READ; 898 data.sg = &sg; 899 data.sg_len = 1; 900 mmc_set_data_timeout(&data, card); 901 902 mrq.cmd = &cmd; 903 mrq.data = &data; 904 905 blocks = kmalloc(4, GFP_KERNEL); 906 if (!blocks) 907 return -ENOMEM; 908 909 sg_init_one(&sg, blocks, 4); 910 911 mmc_wait_for_req(card->host, &mrq); 912 913 result = ntohl(*blocks); 914 kfree(blocks); 915 916 if (cmd.error || data.error) 917 return -EIO; 918 919 *written_blocks = result; 920 921 return 0; 922 } 923 924 static unsigned int mmc_blk_clock_khz(struct mmc_host *host) 925 { 926 if (host->actual_clock) 927 return host->actual_clock / 1000; 928 929 /* Clock may be subject to a divisor, fudge it by a factor of 2. */ 930 if (host->ios.clock) 931 return host->ios.clock / 2000; 932 933 /* How can there be no clock */ 934 WARN_ON_ONCE(1); 935 return 100; /* 100 kHz is minimum possible value */ 936 } 937 938 static unsigned int mmc_blk_data_timeout_ms(struct mmc_host *host, 939 struct mmc_data *data) 940 { 941 unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000); 942 unsigned int khz; 943 944 if (data->timeout_clks) { 945 khz = mmc_blk_clock_khz(host); 946 ms += DIV_ROUND_UP(data->timeout_clks, khz); 947 } 948 949 return ms; 950 } 951 952 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host, 953 int type) 954 { 955 int err; 956 957 if (md->reset_done & type) 958 return -EEXIST; 959 960 md->reset_done |= type; 961 err = mmc_hw_reset(host); 962 /* Ensure we switch back to the correct partition */ 963 if (err) { 964 struct mmc_blk_data *main_md = 965 dev_get_drvdata(&host->card->dev); 966 int part_err; 967 968 main_md->part_curr = main_md->part_type; 969 part_err = mmc_blk_part_switch(host->card, md->part_type); 970 if (part_err) { 971 /* 972 * We have failed to get back into the correct 973 * partition, so we need to abort the whole request. 974 */ 975 return -ENODEV; 976 } 977 } 978 return err; 979 } 980 981 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type) 982 { 983 md->reset_done &= ~type; 984 } 985 986 /* 987 * The non-block commands come back from the block layer after it queued it and 988 * processed it with all other requests and then they get issued in this 989 * function. 990 */ 991 static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req) 992 { 993 struct mmc_queue_req *mq_rq; 994 struct mmc_card *card = mq->card; 995 struct mmc_blk_data *md = mq->blkdata; 996 struct mmc_blk_ioc_data **idata; 997 bool rpmb_ioctl; 998 u8 **ext_csd; 999 u32 status; 1000 int ret; 1001 int i; 1002 1003 mq_rq = req_to_mmc_queue_req(req); 1004 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB); 1005 1006 switch (mq_rq->drv_op) { 1007 case MMC_DRV_OP_IOCTL: 1008 if (card->ext_csd.cmdq_en) { 1009 ret = mmc_cmdq_disable(card); 1010 if (ret) 1011 break; 1012 } 1013 fallthrough; 1014 case MMC_DRV_OP_IOCTL_RPMB: 1015 idata = mq_rq->drv_op_data; 1016 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) { 1017 ret = __mmc_blk_ioctl_cmd(card, md, idata[i]); 1018 if (ret) 1019 break; 1020 } 1021 /* Always switch back to main area after RPMB access */ 1022 if (rpmb_ioctl) 1023 mmc_blk_part_switch(card, 0); 1024 else if (card->reenable_cmdq && !card->ext_csd.cmdq_en) 1025 mmc_cmdq_enable(card); 1026 break; 1027 case MMC_DRV_OP_BOOT_WP: 1028 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP, 1029 card->ext_csd.boot_ro_lock | 1030 EXT_CSD_BOOT_WP_B_PWR_WP_EN, 1031 card->ext_csd.part_time); 1032 if (ret) 1033 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", 1034 md->disk->disk_name, ret); 1035 else 1036 card->ext_csd.boot_ro_lock |= 1037 EXT_CSD_BOOT_WP_B_PWR_WP_EN; 1038 break; 1039 case MMC_DRV_OP_GET_CARD_STATUS: 1040 ret = mmc_send_status(card, &status); 1041 if (!ret) 1042 ret = status; 1043 break; 1044 case MMC_DRV_OP_GET_EXT_CSD: 1045 ext_csd = mq_rq->drv_op_data; 1046 ret = mmc_get_ext_csd(card, ext_csd); 1047 break; 1048 default: 1049 pr_err("%s: unknown driver specific operation\n", 1050 md->disk->disk_name); 1051 ret = -EINVAL; 1052 break; 1053 } 1054 mq_rq->drv_op_result = ret; 1055 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK); 1056 } 1057 1058 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req) 1059 { 1060 struct mmc_blk_data *md = mq->blkdata; 1061 struct mmc_card *card = md->queue.card; 1062 unsigned int from, nr; 1063 int err = 0, type = MMC_BLK_DISCARD; 1064 blk_status_t status = BLK_STS_OK; 1065 1066 if (!mmc_can_erase(card)) { 1067 status = BLK_STS_NOTSUPP; 1068 goto fail; 1069 } 1070 1071 from = blk_rq_pos(req); 1072 nr = blk_rq_sectors(req); 1073 1074 do { 1075 err = 0; 1076 if (card->quirks & MMC_QUIRK_INAND_CMD38) { 1077 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 1078 INAND_CMD38_ARG_EXT_CSD, 1079 card->erase_arg == MMC_TRIM_ARG ? 1080 INAND_CMD38_ARG_TRIM : 1081 INAND_CMD38_ARG_ERASE, 1082 card->ext_csd.generic_cmd6_time); 1083 } 1084 if (!err) 1085 err = mmc_erase(card, from, nr, card->erase_arg); 1086 } while (err == -EIO && !mmc_blk_reset(md, card->host, type)); 1087 if (err) 1088 status = BLK_STS_IOERR; 1089 else 1090 mmc_blk_reset_success(md, type); 1091 fail: 1092 blk_mq_end_request(req, status); 1093 } 1094 1095 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq, 1096 struct request *req) 1097 { 1098 struct mmc_blk_data *md = mq->blkdata; 1099 struct mmc_card *card = md->queue.card; 1100 unsigned int from, nr, arg; 1101 int err = 0, type = MMC_BLK_SECDISCARD; 1102 blk_status_t status = BLK_STS_OK; 1103 1104 if (!(mmc_can_secure_erase_trim(card))) { 1105 status = BLK_STS_NOTSUPP; 1106 goto out; 1107 } 1108 1109 from = blk_rq_pos(req); 1110 nr = blk_rq_sectors(req); 1111 1112 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr)) 1113 arg = MMC_SECURE_TRIM1_ARG; 1114 else 1115 arg = MMC_SECURE_ERASE_ARG; 1116 1117 retry: 1118 if (card->quirks & MMC_QUIRK_INAND_CMD38) { 1119 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 1120 INAND_CMD38_ARG_EXT_CSD, 1121 arg == MMC_SECURE_TRIM1_ARG ? 1122 INAND_CMD38_ARG_SECTRIM1 : 1123 INAND_CMD38_ARG_SECERASE, 1124 card->ext_csd.generic_cmd6_time); 1125 if (err) 1126 goto out_retry; 1127 } 1128 1129 err = mmc_erase(card, from, nr, arg); 1130 if (err == -EIO) 1131 goto out_retry; 1132 if (err) { 1133 status = BLK_STS_IOERR; 1134 goto out; 1135 } 1136 1137 if (arg == MMC_SECURE_TRIM1_ARG) { 1138 if (card->quirks & MMC_QUIRK_INAND_CMD38) { 1139 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 1140 INAND_CMD38_ARG_EXT_CSD, 1141 INAND_CMD38_ARG_SECTRIM2, 1142 card->ext_csd.generic_cmd6_time); 1143 if (err) 1144 goto out_retry; 1145 } 1146 1147 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG); 1148 if (err == -EIO) 1149 goto out_retry; 1150 if (err) { 1151 status = BLK_STS_IOERR; 1152 goto out; 1153 } 1154 } 1155 1156 out_retry: 1157 if (err && !mmc_blk_reset(md, card->host, type)) 1158 goto retry; 1159 if (!err) 1160 mmc_blk_reset_success(md, type); 1161 out: 1162 blk_mq_end_request(req, status); 1163 } 1164 1165 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req) 1166 { 1167 struct mmc_blk_data *md = mq->blkdata; 1168 struct mmc_card *card = md->queue.card; 1169 int ret = 0; 1170 1171 ret = mmc_flush_cache(card->host); 1172 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK); 1173 } 1174 1175 /* 1176 * Reformat current write as a reliable write, supporting 1177 * both legacy and the enhanced reliable write MMC cards. 1178 * In each transfer we'll handle only as much as a single 1179 * reliable write can handle, thus finish the request in 1180 * partial completions. 1181 */ 1182 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, 1183 struct mmc_card *card, 1184 struct request *req) 1185 { 1186 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) { 1187 /* Legacy mode imposes restrictions on transfers. */ 1188 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors)) 1189 brq->data.blocks = 1; 1190 1191 if (brq->data.blocks > card->ext_csd.rel_sectors) 1192 brq->data.blocks = card->ext_csd.rel_sectors; 1193 else if (brq->data.blocks < card->ext_csd.rel_sectors) 1194 brq->data.blocks = 1; 1195 } 1196 } 1197 1198 #define CMD_ERRORS_EXCL_OOR \ 1199 (R1_ADDRESS_ERROR | /* Misaligned address */ \ 1200 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\ 1201 R1_WP_VIOLATION | /* Tried to write to protected block */ \ 1202 R1_CARD_ECC_FAILED | /* Card ECC failed */ \ 1203 R1_CC_ERROR | /* Card controller error */ \ 1204 R1_ERROR) /* General/unknown error */ 1205 1206 #define CMD_ERRORS \ 1207 (CMD_ERRORS_EXCL_OOR | \ 1208 R1_OUT_OF_RANGE) /* Command argument out of range */ \ 1209 1210 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq) 1211 { 1212 u32 val; 1213 1214 /* 1215 * Per the SD specification(physical layer version 4.10)[1], 1216 * section 4.3.3, it explicitly states that "When the last 1217 * block of user area is read using CMD18, the host should 1218 * ignore OUT_OF_RANGE error that may occur even the sequence 1219 * is correct". And JESD84-B51 for eMMC also has a similar 1220 * statement on section 6.8.3. 1221 * 1222 * Multiple block read/write could be done by either predefined 1223 * method, namely CMD23, or open-ending mode. For open-ending mode, 1224 * we should ignore the OUT_OF_RANGE error as it's normal behaviour. 1225 * 1226 * However the spec[1] doesn't tell us whether we should also 1227 * ignore that for predefined method. But per the spec[1], section 1228 * 4.15 Set Block Count Command, it says"If illegal block count 1229 * is set, out of range error will be indicated during read/write 1230 * operation (For example, data transfer is stopped at user area 1231 * boundary)." In another word, we could expect a out of range error 1232 * in the response for the following CMD18/25. And if argument of 1233 * CMD23 + the argument of CMD18/25 exceed the max number of blocks, 1234 * we could also expect to get a -ETIMEDOUT or any error number from 1235 * the host drivers due to missing data response(for write)/data(for 1236 * read), as the cards will stop the data transfer by itself per the 1237 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode. 1238 */ 1239 1240 if (!brq->stop.error) { 1241 bool oor_with_open_end; 1242 /* If there is no error yet, check R1 response */ 1243 1244 val = brq->stop.resp[0] & CMD_ERRORS; 1245 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc; 1246 1247 if (val && !oor_with_open_end) 1248 brq->stop.error = -EIO; 1249 } 1250 } 1251 1252 static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq, 1253 int disable_multi, bool *do_rel_wr_p, 1254 bool *do_data_tag_p) 1255 { 1256 struct mmc_blk_data *md = mq->blkdata; 1257 struct mmc_card *card = md->queue.card; 1258 struct mmc_blk_request *brq = &mqrq->brq; 1259 struct request *req = mmc_queue_req_to_req(mqrq); 1260 bool do_rel_wr, do_data_tag; 1261 1262 /* 1263 * Reliable writes are used to implement Forced Unit Access and 1264 * are supported only on MMCs. 1265 */ 1266 do_rel_wr = (req->cmd_flags & REQ_FUA) && 1267 rq_data_dir(req) == WRITE && 1268 (md->flags & MMC_BLK_REL_WR); 1269 1270 memset(brq, 0, sizeof(struct mmc_blk_request)); 1271 1272 mmc_crypto_prepare_req(mqrq); 1273 1274 brq->mrq.data = &brq->data; 1275 brq->mrq.tag = req->tag; 1276 1277 brq->stop.opcode = MMC_STOP_TRANSMISSION; 1278 brq->stop.arg = 0; 1279 1280 if (rq_data_dir(req) == READ) { 1281 brq->data.flags = MMC_DATA_READ; 1282 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; 1283 } else { 1284 brq->data.flags = MMC_DATA_WRITE; 1285 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; 1286 } 1287 1288 brq->data.blksz = 512; 1289 brq->data.blocks = blk_rq_sectors(req); 1290 brq->data.blk_addr = blk_rq_pos(req); 1291 1292 /* 1293 * The command queue supports 2 priorities: "high" (1) and "simple" (0). 1294 * The eMMC will give "high" priority tasks priority over "simple" 1295 * priority tasks. Here we always set "simple" priority by not setting 1296 * MMC_DATA_PRIO. 1297 */ 1298 1299 /* 1300 * The block layer doesn't support all sector count 1301 * restrictions, so we need to be prepared for too big 1302 * requests. 1303 */ 1304 if (brq->data.blocks > card->host->max_blk_count) 1305 brq->data.blocks = card->host->max_blk_count; 1306 1307 if (brq->data.blocks > 1) { 1308 /* 1309 * Some SD cards in SPI mode return a CRC error or even lock up 1310 * completely when trying to read the last block using a 1311 * multiblock read command. 1312 */ 1313 if (mmc_host_is_spi(card->host) && (rq_data_dir(req) == READ) && 1314 (blk_rq_pos(req) + blk_rq_sectors(req) == 1315 get_capacity(md->disk))) 1316 brq->data.blocks--; 1317 1318 /* 1319 * After a read error, we redo the request one sector 1320 * at a time in order to accurately determine which 1321 * sectors can be read successfully. 1322 */ 1323 if (disable_multi) 1324 brq->data.blocks = 1; 1325 1326 /* 1327 * Some controllers have HW issues while operating 1328 * in multiple I/O mode 1329 */ 1330 if (card->host->ops->multi_io_quirk) 1331 brq->data.blocks = card->host->ops->multi_io_quirk(card, 1332 (rq_data_dir(req) == READ) ? 1333 MMC_DATA_READ : MMC_DATA_WRITE, 1334 brq->data.blocks); 1335 } 1336 1337 if (do_rel_wr) { 1338 mmc_apply_rel_rw(brq, card, req); 1339 brq->data.flags |= MMC_DATA_REL_WR; 1340 } 1341 1342 /* 1343 * Data tag is used only during writing meta data to speed 1344 * up write and any subsequent read of this meta data 1345 */ 1346 do_data_tag = card->ext_csd.data_tag_unit_size && 1347 (req->cmd_flags & REQ_META) && 1348 (rq_data_dir(req) == WRITE) && 1349 ((brq->data.blocks * brq->data.blksz) >= 1350 card->ext_csd.data_tag_unit_size); 1351 1352 if (do_data_tag) 1353 brq->data.flags |= MMC_DATA_DAT_TAG; 1354 1355 mmc_set_data_timeout(&brq->data, card); 1356 1357 brq->data.sg = mqrq->sg; 1358 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq); 1359 1360 /* 1361 * Adjust the sg list so it is the same size as the 1362 * request. 1363 */ 1364 if (brq->data.blocks != blk_rq_sectors(req)) { 1365 int i, data_size = brq->data.blocks << 9; 1366 struct scatterlist *sg; 1367 1368 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) { 1369 data_size -= sg->length; 1370 if (data_size <= 0) { 1371 sg->length += data_size; 1372 i++; 1373 break; 1374 } 1375 } 1376 brq->data.sg_len = i; 1377 } 1378 1379 if (do_rel_wr_p) 1380 *do_rel_wr_p = do_rel_wr; 1381 1382 if (do_data_tag_p) 1383 *do_data_tag_p = do_data_tag; 1384 } 1385 1386 #define MMC_CQE_RETRIES 2 1387 1388 static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req) 1389 { 1390 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1391 struct mmc_request *mrq = &mqrq->brq.mrq; 1392 struct request_queue *q = req->q; 1393 struct mmc_host *host = mq->card->host; 1394 enum mmc_issue_type issue_type = mmc_issue_type(mq, req); 1395 unsigned long flags; 1396 bool put_card; 1397 int err; 1398 1399 mmc_cqe_post_req(host, mrq); 1400 1401 if (mrq->cmd && mrq->cmd->error) 1402 err = mrq->cmd->error; 1403 else if (mrq->data && mrq->data->error) 1404 err = mrq->data->error; 1405 else 1406 err = 0; 1407 1408 if (err) { 1409 if (mqrq->retries++ < MMC_CQE_RETRIES) 1410 blk_mq_requeue_request(req, true); 1411 else 1412 blk_mq_end_request(req, BLK_STS_IOERR); 1413 } else if (mrq->data) { 1414 if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered)) 1415 blk_mq_requeue_request(req, true); 1416 else 1417 __blk_mq_end_request(req, BLK_STS_OK); 1418 } else { 1419 blk_mq_end_request(req, BLK_STS_OK); 1420 } 1421 1422 spin_lock_irqsave(&mq->lock, flags); 1423 1424 mq->in_flight[issue_type] -= 1; 1425 1426 put_card = (mmc_tot_in_flight(mq) == 0); 1427 1428 mmc_cqe_check_busy(mq); 1429 1430 spin_unlock_irqrestore(&mq->lock, flags); 1431 1432 if (!mq->cqe_busy) 1433 blk_mq_run_hw_queues(q, true); 1434 1435 if (put_card) 1436 mmc_put_card(mq->card, &mq->ctx); 1437 } 1438 1439 void mmc_blk_cqe_recovery(struct mmc_queue *mq) 1440 { 1441 struct mmc_card *card = mq->card; 1442 struct mmc_host *host = card->host; 1443 int err; 1444 1445 pr_debug("%s: CQE recovery start\n", mmc_hostname(host)); 1446 1447 err = mmc_cqe_recovery(host); 1448 if (err) 1449 mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY); 1450 else 1451 mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY); 1452 1453 pr_debug("%s: CQE recovery done\n", mmc_hostname(host)); 1454 } 1455 1456 static void mmc_blk_cqe_req_done(struct mmc_request *mrq) 1457 { 1458 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req, 1459 brq.mrq); 1460 struct request *req = mmc_queue_req_to_req(mqrq); 1461 struct request_queue *q = req->q; 1462 struct mmc_queue *mq = q->queuedata; 1463 1464 /* 1465 * Block layer timeouts race with completions which means the normal 1466 * completion path cannot be used during recovery. 1467 */ 1468 if (mq->in_recovery) 1469 mmc_blk_cqe_complete_rq(mq, req); 1470 else if (likely(!blk_should_fake_timeout(req->q))) 1471 blk_mq_complete_request(req); 1472 } 1473 1474 static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq) 1475 { 1476 mrq->done = mmc_blk_cqe_req_done; 1477 mrq->recovery_notifier = mmc_cqe_recovery_notifier; 1478 1479 return mmc_cqe_start_req(host, mrq); 1480 } 1481 1482 static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq, 1483 struct request *req) 1484 { 1485 struct mmc_blk_request *brq = &mqrq->brq; 1486 1487 memset(brq, 0, sizeof(*brq)); 1488 1489 brq->mrq.cmd = &brq->cmd; 1490 brq->mrq.tag = req->tag; 1491 1492 return &brq->mrq; 1493 } 1494 1495 static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req) 1496 { 1497 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1498 struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req); 1499 1500 mrq->cmd->opcode = MMC_SWITCH; 1501 mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | 1502 (EXT_CSD_FLUSH_CACHE << 16) | 1503 (1 << 8) | 1504 EXT_CSD_CMD_SET_NORMAL; 1505 mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B; 1506 1507 return mmc_blk_cqe_start_req(mq->card->host, mrq); 1508 } 1509 1510 static int mmc_blk_hsq_issue_rw_rq(struct mmc_queue *mq, struct request *req) 1511 { 1512 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1513 struct mmc_host *host = mq->card->host; 1514 int err; 1515 1516 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq); 1517 mqrq->brq.mrq.done = mmc_blk_hsq_req_done; 1518 mmc_pre_req(host, &mqrq->brq.mrq); 1519 1520 err = mmc_cqe_start_req(host, &mqrq->brq.mrq); 1521 if (err) 1522 mmc_post_req(host, &mqrq->brq.mrq, err); 1523 1524 return err; 1525 } 1526 1527 static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req) 1528 { 1529 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1530 struct mmc_host *host = mq->card->host; 1531 1532 if (host->hsq_enabled) 1533 return mmc_blk_hsq_issue_rw_rq(mq, req); 1534 1535 mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL); 1536 1537 return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq); 1538 } 1539 1540 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, 1541 struct mmc_card *card, 1542 int disable_multi, 1543 struct mmc_queue *mq) 1544 { 1545 u32 readcmd, writecmd; 1546 struct mmc_blk_request *brq = &mqrq->brq; 1547 struct request *req = mmc_queue_req_to_req(mqrq); 1548 struct mmc_blk_data *md = mq->blkdata; 1549 bool do_rel_wr, do_data_tag; 1550 1551 mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag); 1552 1553 brq->mrq.cmd = &brq->cmd; 1554 1555 brq->cmd.arg = blk_rq_pos(req); 1556 if (!mmc_card_blockaddr(card)) 1557 brq->cmd.arg <<= 9; 1558 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; 1559 1560 if (brq->data.blocks > 1 || do_rel_wr) { 1561 /* SPI multiblock writes terminate using a special 1562 * token, not a STOP_TRANSMISSION request. 1563 */ 1564 if (!mmc_host_is_spi(card->host) || 1565 rq_data_dir(req) == READ) 1566 brq->mrq.stop = &brq->stop; 1567 readcmd = MMC_READ_MULTIPLE_BLOCK; 1568 writecmd = MMC_WRITE_MULTIPLE_BLOCK; 1569 } else { 1570 brq->mrq.stop = NULL; 1571 readcmd = MMC_READ_SINGLE_BLOCK; 1572 writecmd = MMC_WRITE_BLOCK; 1573 } 1574 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd; 1575 1576 /* 1577 * Pre-defined multi-block transfers are preferable to 1578 * open ended-ones (and necessary for reliable writes). 1579 * However, it is not sufficient to just send CMD23, 1580 * and avoid the final CMD12, as on an error condition 1581 * CMD12 (stop) needs to be sent anyway. This, coupled 1582 * with Auto-CMD23 enhancements provided by some 1583 * hosts, means that the complexity of dealing 1584 * with this is best left to the host. If CMD23 is 1585 * supported by card and host, we'll fill sbc in and let 1586 * the host deal with handling it correctly. This means 1587 * that for hosts that don't expose MMC_CAP_CMD23, no 1588 * change of behavior will be observed. 1589 * 1590 * N.B: Some MMC cards experience perf degradation. 1591 * We'll avoid using CMD23-bounded multiblock writes for 1592 * these, while retaining features like reliable writes. 1593 */ 1594 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) && 1595 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) || 1596 do_data_tag)) { 1597 brq->sbc.opcode = MMC_SET_BLOCK_COUNT; 1598 brq->sbc.arg = brq->data.blocks | 1599 (do_rel_wr ? (1 << 31) : 0) | 1600 (do_data_tag ? (1 << 29) : 0); 1601 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; 1602 brq->mrq.sbc = &brq->sbc; 1603 } 1604 } 1605 1606 #define MMC_MAX_RETRIES 5 1607 #define MMC_DATA_RETRIES 2 1608 #define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1) 1609 1610 static int mmc_blk_send_stop(struct mmc_card *card, unsigned int timeout) 1611 { 1612 struct mmc_command cmd = { 1613 .opcode = MMC_STOP_TRANSMISSION, 1614 .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC, 1615 /* Some hosts wait for busy anyway, so provide a busy timeout */ 1616 .busy_timeout = timeout, 1617 }; 1618 1619 return mmc_wait_for_cmd(card->host, &cmd, 5); 1620 } 1621 1622 static int mmc_blk_fix_state(struct mmc_card *card, struct request *req) 1623 { 1624 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1625 struct mmc_blk_request *brq = &mqrq->brq; 1626 unsigned int timeout = mmc_blk_data_timeout_ms(card->host, &brq->data); 1627 int err; 1628 1629 mmc_retune_hold_now(card->host); 1630 1631 mmc_blk_send_stop(card, timeout); 1632 1633 err = card_busy_detect(card, timeout, NULL); 1634 1635 mmc_retune_release(card->host); 1636 1637 return err; 1638 } 1639 1640 #define MMC_READ_SINGLE_RETRIES 2 1641 1642 /* Single sector read during recovery */ 1643 static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req) 1644 { 1645 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1646 struct mmc_request *mrq = &mqrq->brq.mrq; 1647 struct mmc_card *card = mq->card; 1648 struct mmc_host *host = card->host; 1649 blk_status_t error = BLK_STS_OK; 1650 int retries = 0; 1651 1652 do { 1653 u32 status; 1654 int err; 1655 1656 mmc_blk_rw_rq_prep(mqrq, card, 1, mq); 1657 1658 mmc_wait_for_req(host, mrq); 1659 1660 err = mmc_send_status(card, &status); 1661 if (err) 1662 goto error_exit; 1663 1664 if (!mmc_host_is_spi(host) && 1665 !mmc_ready_for_data(status)) { 1666 err = mmc_blk_fix_state(card, req); 1667 if (err) 1668 goto error_exit; 1669 } 1670 1671 if (mrq->cmd->error && retries++ < MMC_READ_SINGLE_RETRIES) 1672 continue; 1673 1674 retries = 0; 1675 1676 if (mrq->cmd->error || 1677 mrq->data->error || 1678 (!mmc_host_is_spi(host) && 1679 (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS))) 1680 error = BLK_STS_IOERR; 1681 else 1682 error = BLK_STS_OK; 1683 1684 } while (blk_update_request(req, error, 512)); 1685 1686 return; 1687 1688 error_exit: 1689 mrq->data->bytes_xfered = 0; 1690 blk_update_request(req, BLK_STS_IOERR, 512); 1691 /* Let it try the remaining request again */ 1692 if (mqrq->retries > MMC_MAX_RETRIES - 1) 1693 mqrq->retries = MMC_MAX_RETRIES - 1; 1694 } 1695 1696 static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq) 1697 { 1698 return !!brq->mrq.sbc; 1699 } 1700 1701 static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq) 1702 { 1703 return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR; 1704 } 1705 1706 /* 1707 * Check for errors the host controller driver might not have seen such as 1708 * response mode errors or invalid card state. 1709 */ 1710 static bool mmc_blk_status_error(struct request *req, u32 status) 1711 { 1712 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1713 struct mmc_blk_request *brq = &mqrq->brq; 1714 struct mmc_queue *mq = req->q->queuedata; 1715 u32 stop_err_bits; 1716 1717 if (mmc_host_is_spi(mq->card->host)) 1718 return false; 1719 1720 stop_err_bits = mmc_blk_stop_err_bits(brq); 1721 1722 return brq->cmd.resp[0] & CMD_ERRORS || 1723 brq->stop.resp[0] & stop_err_bits || 1724 status & stop_err_bits || 1725 (rq_data_dir(req) == WRITE && !mmc_ready_for_data(status)); 1726 } 1727 1728 static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq) 1729 { 1730 return !brq->sbc.error && !brq->cmd.error && 1731 !(brq->cmd.resp[0] & CMD_ERRORS); 1732 } 1733 1734 /* 1735 * Requests are completed by mmc_blk_mq_complete_rq() which sets simple 1736 * policy: 1737 * 1. A request that has transferred at least some data is considered 1738 * successful and will be requeued if there is remaining data to 1739 * transfer. 1740 * 2. Otherwise the number of retries is incremented and the request 1741 * will be requeued if there are remaining retries. 1742 * 3. Otherwise the request will be errored out. 1743 * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and 1744 * mqrq->retries. So there are only 4 possible actions here: 1745 * 1. do not accept the bytes_xfered value i.e. set it to zero 1746 * 2. change mqrq->retries to determine the number of retries 1747 * 3. try to reset the card 1748 * 4. read one sector at a time 1749 */ 1750 static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req) 1751 { 1752 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; 1753 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1754 struct mmc_blk_request *brq = &mqrq->brq; 1755 struct mmc_blk_data *md = mq->blkdata; 1756 struct mmc_card *card = mq->card; 1757 u32 status; 1758 u32 blocks; 1759 int err; 1760 1761 /* 1762 * Some errors the host driver might not have seen. Set the number of 1763 * bytes transferred to zero in that case. 1764 */ 1765 err = __mmc_send_status(card, &status, 0); 1766 if (err || mmc_blk_status_error(req, status)) 1767 brq->data.bytes_xfered = 0; 1768 1769 mmc_retune_release(card->host); 1770 1771 /* 1772 * Try again to get the status. This also provides an opportunity for 1773 * re-tuning. 1774 */ 1775 if (err) 1776 err = __mmc_send_status(card, &status, 0); 1777 1778 /* 1779 * Nothing more to do after the number of bytes transferred has been 1780 * updated and there is no card. 1781 */ 1782 if (err && mmc_detect_card_removed(card->host)) 1783 return; 1784 1785 /* Try to get back to "tran" state */ 1786 if (!mmc_host_is_spi(mq->card->host) && 1787 (err || !mmc_ready_for_data(status))) 1788 err = mmc_blk_fix_state(mq->card, req); 1789 1790 /* 1791 * Special case for SD cards where the card might record the number of 1792 * blocks written. 1793 */ 1794 if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) && 1795 rq_data_dir(req) == WRITE) { 1796 if (mmc_sd_num_wr_blocks(card, &blocks)) 1797 brq->data.bytes_xfered = 0; 1798 else 1799 brq->data.bytes_xfered = blocks << 9; 1800 } 1801 1802 /* Reset if the card is in a bad state */ 1803 if (!mmc_host_is_spi(mq->card->host) && 1804 err && mmc_blk_reset(md, card->host, type)) { 1805 pr_err("%s: recovery failed!\n", req->rq_disk->disk_name); 1806 mqrq->retries = MMC_NO_RETRIES; 1807 return; 1808 } 1809 1810 /* 1811 * If anything was done, just return and if there is anything remaining 1812 * on the request it will get requeued. 1813 */ 1814 if (brq->data.bytes_xfered) 1815 return; 1816 1817 /* Reset before last retry */ 1818 if (mqrq->retries + 1 == MMC_MAX_RETRIES) 1819 mmc_blk_reset(md, card->host, type); 1820 1821 /* Command errors fail fast, so use all MMC_MAX_RETRIES */ 1822 if (brq->sbc.error || brq->cmd.error) 1823 return; 1824 1825 /* Reduce the remaining retries for data errors */ 1826 if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) { 1827 mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES; 1828 return; 1829 } 1830 1831 /* FIXME: Missing single sector read for large sector size */ 1832 if (!mmc_large_sector(card) && rq_data_dir(req) == READ && 1833 brq->data.blocks > 1) { 1834 /* Read one sector at a time */ 1835 mmc_blk_read_single(mq, req); 1836 return; 1837 } 1838 } 1839 1840 static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq) 1841 { 1842 mmc_blk_eval_resp_error(brq); 1843 1844 return brq->sbc.error || brq->cmd.error || brq->stop.error || 1845 brq->data.error || brq->cmd.resp[0] & CMD_ERRORS; 1846 } 1847 1848 static int mmc_blk_card_busy(struct mmc_card *card, struct request *req) 1849 { 1850 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1851 u32 status = 0; 1852 int err; 1853 1854 if (mmc_host_is_spi(card->host) || rq_data_dir(req) == READ) 1855 return 0; 1856 1857 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, &status); 1858 1859 /* 1860 * Do not assume data transferred correctly if there are any error bits 1861 * set. 1862 */ 1863 if (status & mmc_blk_stop_err_bits(&mqrq->brq)) { 1864 mqrq->brq.data.bytes_xfered = 0; 1865 err = err ? err : -EIO; 1866 } 1867 1868 /* Copy the exception bit so it will be seen later on */ 1869 if (mmc_card_mmc(card) && status & R1_EXCEPTION_EVENT) 1870 mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT; 1871 1872 return err; 1873 } 1874 1875 static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq, 1876 struct request *req) 1877 { 1878 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; 1879 1880 mmc_blk_reset_success(mq->blkdata, type); 1881 } 1882 1883 static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req) 1884 { 1885 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1886 unsigned int nr_bytes = mqrq->brq.data.bytes_xfered; 1887 1888 if (nr_bytes) { 1889 if (blk_update_request(req, BLK_STS_OK, nr_bytes)) 1890 blk_mq_requeue_request(req, true); 1891 else 1892 __blk_mq_end_request(req, BLK_STS_OK); 1893 } else if (!blk_rq_bytes(req)) { 1894 __blk_mq_end_request(req, BLK_STS_IOERR); 1895 } else if (mqrq->retries++ < MMC_MAX_RETRIES) { 1896 blk_mq_requeue_request(req, true); 1897 } else { 1898 if (mmc_card_removed(mq->card)) 1899 req->rq_flags |= RQF_QUIET; 1900 blk_mq_end_request(req, BLK_STS_IOERR); 1901 } 1902 } 1903 1904 static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq, 1905 struct mmc_queue_req *mqrq) 1906 { 1907 return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) && 1908 (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT || 1909 mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT); 1910 } 1911 1912 static void mmc_blk_urgent_bkops(struct mmc_queue *mq, 1913 struct mmc_queue_req *mqrq) 1914 { 1915 if (mmc_blk_urgent_bkops_needed(mq, mqrq)) 1916 mmc_run_bkops(mq->card); 1917 } 1918 1919 static void mmc_blk_hsq_req_done(struct mmc_request *mrq) 1920 { 1921 struct mmc_queue_req *mqrq = 1922 container_of(mrq, struct mmc_queue_req, brq.mrq); 1923 struct request *req = mmc_queue_req_to_req(mqrq); 1924 struct request_queue *q = req->q; 1925 struct mmc_queue *mq = q->queuedata; 1926 struct mmc_host *host = mq->card->host; 1927 unsigned long flags; 1928 1929 if (mmc_blk_rq_error(&mqrq->brq) || 1930 mmc_blk_urgent_bkops_needed(mq, mqrq)) { 1931 spin_lock_irqsave(&mq->lock, flags); 1932 mq->recovery_needed = true; 1933 mq->recovery_req = req; 1934 spin_unlock_irqrestore(&mq->lock, flags); 1935 1936 host->cqe_ops->cqe_recovery_start(host); 1937 1938 schedule_work(&mq->recovery_work); 1939 return; 1940 } 1941 1942 mmc_blk_rw_reset_success(mq, req); 1943 1944 /* 1945 * Block layer timeouts race with completions which means the normal 1946 * completion path cannot be used during recovery. 1947 */ 1948 if (mq->in_recovery) 1949 mmc_blk_cqe_complete_rq(mq, req); 1950 else if (likely(!blk_should_fake_timeout(req->q))) 1951 blk_mq_complete_request(req); 1952 } 1953 1954 void mmc_blk_mq_complete(struct request *req) 1955 { 1956 struct mmc_queue *mq = req->q->queuedata; 1957 struct mmc_host *host = mq->card->host; 1958 1959 if (host->cqe_enabled) 1960 mmc_blk_cqe_complete_rq(mq, req); 1961 else if (likely(!blk_should_fake_timeout(req->q))) 1962 mmc_blk_mq_complete_rq(mq, req); 1963 } 1964 1965 static void mmc_blk_mq_poll_completion(struct mmc_queue *mq, 1966 struct request *req) 1967 { 1968 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1969 struct mmc_host *host = mq->card->host; 1970 1971 if (mmc_blk_rq_error(&mqrq->brq) || 1972 mmc_blk_card_busy(mq->card, req)) { 1973 mmc_blk_mq_rw_recovery(mq, req); 1974 } else { 1975 mmc_blk_rw_reset_success(mq, req); 1976 mmc_retune_release(host); 1977 } 1978 1979 mmc_blk_urgent_bkops(mq, mqrq); 1980 } 1981 1982 static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, struct request *req) 1983 { 1984 unsigned long flags; 1985 bool put_card; 1986 1987 spin_lock_irqsave(&mq->lock, flags); 1988 1989 mq->in_flight[mmc_issue_type(mq, req)] -= 1; 1990 1991 put_card = (mmc_tot_in_flight(mq) == 0); 1992 1993 spin_unlock_irqrestore(&mq->lock, flags); 1994 1995 if (put_card) 1996 mmc_put_card(mq->card, &mq->ctx); 1997 } 1998 1999 static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req) 2000 { 2001 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 2002 struct mmc_request *mrq = &mqrq->brq.mrq; 2003 struct mmc_host *host = mq->card->host; 2004 2005 mmc_post_req(host, mrq, 0); 2006 2007 /* 2008 * Block layer timeouts race with completions which means the normal 2009 * completion path cannot be used during recovery. 2010 */ 2011 if (mq->in_recovery) 2012 mmc_blk_mq_complete_rq(mq, req); 2013 else if (likely(!blk_should_fake_timeout(req->q))) 2014 blk_mq_complete_request(req); 2015 2016 mmc_blk_mq_dec_in_flight(mq, req); 2017 } 2018 2019 void mmc_blk_mq_recovery(struct mmc_queue *mq) 2020 { 2021 struct request *req = mq->recovery_req; 2022 struct mmc_host *host = mq->card->host; 2023 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 2024 2025 mq->recovery_req = NULL; 2026 mq->rw_wait = false; 2027 2028 if (mmc_blk_rq_error(&mqrq->brq)) { 2029 mmc_retune_hold_now(host); 2030 mmc_blk_mq_rw_recovery(mq, req); 2031 } 2032 2033 mmc_blk_urgent_bkops(mq, mqrq); 2034 2035 mmc_blk_mq_post_req(mq, req); 2036 } 2037 2038 static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq, 2039 struct request **prev_req) 2040 { 2041 if (mmc_host_done_complete(mq->card->host)) 2042 return; 2043 2044 mutex_lock(&mq->complete_lock); 2045 2046 if (!mq->complete_req) 2047 goto out_unlock; 2048 2049 mmc_blk_mq_poll_completion(mq, mq->complete_req); 2050 2051 if (prev_req) 2052 *prev_req = mq->complete_req; 2053 else 2054 mmc_blk_mq_post_req(mq, mq->complete_req); 2055 2056 mq->complete_req = NULL; 2057 2058 out_unlock: 2059 mutex_unlock(&mq->complete_lock); 2060 } 2061 2062 void mmc_blk_mq_complete_work(struct work_struct *work) 2063 { 2064 struct mmc_queue *mq = container_of(work, struct mmc_queue, 2065 complete_work); 2066 2067 mmc_blk_mq_complete_prev_req(mq, NULL); 2068 } 2069 2070 static void mmc_blk_mq_req_done(struct mmc_request *mrq) 2071 { 2072 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req, 2073 brq.mrq); 2074 struct request *req = mmc_queue_req_to_req(mqrq); 2075 struct request_queue *q = req->q; 2076 struct mmc_queue *mq = q->queuedata; 2077 struct mmc_host *host = mq->card->host; 2078 unsigned long flags; 2079 2080 if (!mmc_host_done_complete(host)) { 2081 bool waiting; 2082 2083 /* 2084 * We cannot complete the request in this context, so record 2085 * that there is a request to complete, and that a following 2086 * request does not need to wait (although it does need to 2087 * complete complete_req first). 2088 */ 2089 spin_lock_irqsave(&mq->lock, flags); 2090 mq->complete_req = req; 2091 mq->rw_wait = false; 2092 waiting = mq->waiting; 2093 spin_unlock_irqrestore(&mq->lock, flags); 2094 2095 /* 2096 * If 'waiting' then the waiting task will complete this 2097 * request, otherwise queue a work to do it. Note that 2098 * complete_work may still race with the dispatch of a following 2099 * request. 2100 */ 2101 if (waiting) 2102 wake_up(&mq->wait); 2103 else 2104 queue_work(mq->card->complete_wq, &mq->complete_work); 2105 2106 return; 2107 } 2108 2109 /* Take the recovery path for errors or urgent background operations */ 2110 if (mmc_blk_rq_error(&mqrq->brq) || 2111 mmc_blk_urgent_bkops_needed(mq, mqrq)) { 2112 spin_lock_irqsave(&mq->lock, flags); 2113 mq->recovery_needed = true; 2114 mq->recovery_req = req; 2115 spin_unlock_irqrestore(&mq->lock, flags); 2116 wake_up(&mq->wait); 2117 schedule_work(&mq->recovery_work); 2118 return; 2119 } 2120 2121 mmc_blk_rw_reset_success(mq, req); 2122 2123 mq->rw_wait = false; 2124 wake_up(&mq->wait); 2125 2126 mmc_blk_mq_post_req(mq, req); 2127 } 2128 2129 static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err) 2130 { 2131 unsigned long flags; 2132 bool done; 2133 2134 /* 2135 * Wait while there is another request in progress, but not if recovery 2136 * is needed. Also indicate whether there is a request waiting to start. 2137 */ 2138 spin_lock_irqsave(&mq->lock, flags); 2139 if (mq->recovery_needed) { 2140 *err = -EBUSY; 2141 done = true; 2142 } else { 2143 done = !mq->rw_wait; 2144 } 2145 mq->waiting = !done; 2146 spin_unlock_irqrestore(&mq->lock, flags); 2147 2148 return done; 2149 } 2150 2151 static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req) 2152 { 2153 int err = 0; 2154 2155 wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err)); 2156 2157 /* Always complete the previous request if there is one */ 2158 mmc_blk_mq_complete_prev_req(mq, prev_req); 2159 2160 return err; 2161 } 2162 2163 static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq, 2164 struct request *req) 2165 { 2166 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 2167 struct mmc_host *host = mq->card->host; 2168 struct request *prev_req = NULL; 2169 int err = 0; 2170 2171 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq); 2172 2173 mqrq->brq.mrq.done = mmc_blk_mq_req_done; 2174 2175 mmc_pre_req(host, &mqrq->brq.mrq); 2176 2177 err = mmc_blk_rw_wait(mq, &prev_req); 2178 if (err) 2179 goto out_post_req; 2180 2181 mq->rw_wait = true; 2182 2183 err = mmc_start_request(host, &mqrq->brq.mrq); 2184 2185 if (prev_req) 2186 mmc_blk_mq_post_req(mq, prev_req); 2187 2188 if (err) 2189 mq->rw_wait = false; 2190 2191 /* Release re-tuning here where there is no synchronization required */ 2192 if (err || mmc_host_done_complete(host)) 2193 mmc_retune_release(host); 2194 2195 out_post_req: 2196 if (err) 2197 mmc_post_req(host, &mqrq->brq.mrq, err); 2198 2199 return err; 2200 } 2201 2202 static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host) 2203 { 2204 if (host->cqe_enabled) 2205 return host->cqe_ops->cqe_wait_for_idle(host); 2206 2207 return mmc_blk_rw_wait(mq, NULL); 2208 } 2209 2210 enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req) 2211 { 2212 struct mmc_blk_data *md = mq->blkdata; 2213 struct mmc_card *card = md->queue.card; 2214 struct mmc_host *host = card->host; 2215 int ret; 2216 2217 ret = mmc_blk_part_switch(card, md->part_type); 2218 if (ret) 2219 return MMC_REQ_FAILED_TO_START; 2220 2221 switch (mmc_issue_type(mq, req)) { 2222 case MMC_ISSUE_SYNC: 2223 ret = mmc_blk_wait_for_idle(mq, host); 2224 if (ret) 2225 return MMC_REQ_BUSY; 2226 switch (req_op(req)) { 2227 case REQ_OP_DRV_IN: 2228 case REQ_OP_DRV_OUT: 2229 mmc_blk_issue_drv_op(mq, req); 2230 break; 2231 case REQ_OP_DISCARD: 2232 mmc_blk_issue_discard_rq(mq, req); 2233 break; 2234 case REQ_OP_SECURE_ERASE: 2235 mmc_blk_issue_secdiscard_rq(mq, req); 2236 break; 2237 case REQ_OP_FLUSH: 2238 mmc_blk_issue_flush(mq, req); 2239 break; 2240 default: 2241 WARN_ON_ONCE(1); 2242 return MMC_REQ_FAILED_TO_START; 2243 } 2244 return MMC_REQ_FINISHED; 2245 case MMC_ISSUE_DCMD: 2246 case MMC_ISSUE_ASYNC: 2247 switch (req_op(req)) { 2248 case REQ_OP_FLUSH: 2249 if (!mmc_cache_enabled(host)) { 2250 blk_mq_end_request(req, BLK_STS_OK); 2251 return MMC_REQ_FINISHED; 2252 } 2253 ret = mmc_blk_cqe_issue_flush(mq, req); 2254 break; 2255 case REQ_OP_READ: 2256 case REQ_OP_WRITE: 2257 if (host->cqe_enabled) 2258 ret = mmc_blk_cqe_issue_rw_rq(mq, req); 2259 else 2260 ret = mmc_blk_mq_issue_rw_rq(mq, req); 2261 break; 2262 default: 2263 WARN_ON_ONCE(1); 2264 ret = -EINVAL; 2265 } 2266 if (!ret) 2267 return MMC_REQ_STARTED; 2268 return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START; 2269 default: 2270 WARN_ON_ONCE(1); 2271 return MMC_REQ_FAILED_TO_START; 2272 } 2273 } 2274 2275 static inline int mmc_blk_readonly(struct mmc_card *card) 2276 { 2277 return mmc_card_readonly(card) || 2278 !(card->csd.cmdclass & CCC_BLOCK_WRITE); 2279 } 2280 2281 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card, 2282 struct device *parent, 2283 sector_t size, 2284 bool default_ro, 2285 const char *subname, 2286 int area_type) 2287 { 2288 struct mmc_blk_data *md; 2289 int devidx, ret; 2290 char cap_str[10]; 2291 2292 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL); 2293 if (devidx < 0) { 2294 /* 2295 * We get -ENOSPC because there are no more any available 2296 * devidx. The reason may be that, either userspace haven't yet 2297 * unmounted the partitions, which postpones mmc_blk_release() 2298 * from being called, or the device has more partitions than 2299 * what we support. 2300 */ 2301 if (devidx == -ENOSPC) 2302 dev_err(mmc_dev(card->host), 2303 "no more device IDs available\n"); 2304 2305 return ERR_PTR(devidx); 2306 } 2307 2308 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); 2309 if (!md) { 2310 ret = -ENOMEM; 2311 goto out; 2312 } 2313 2314 md->area_type = area_type; 2315 2316 /* 2317 * Set the read-only status based on the supported commands 2318 * and the write protect switch. 2319 */ 2320 md->read_only = mmc_blk_readonly(card); 2321 2322 md->disk = mmc_init_queue(&md->queue, card); 2323 if (IS_ERR(md->disk)) { 2324 ret = PTR_ERR(md->disk); 2325 goto err_kfree; 2326 } 2327 2328 INIT_LIST_HEAD(&md->part); 2329 INIT_LIST_HEAD(&md->rpmbs); 2330 md->usage = 1; 2331 md->queue.blkdata = md; 2332 2333 md->disk->major = MMC_BLOCK_MAJOR; 2334 md->disk->minors = perdev_minors; 2335 md->disk->first_minor = devidx * perdev_minors; 2336 md->disk->fops = &mmc_bdops; 2337 md->disk->private_data = md; 2338 md->parent = parent; 2339 set_disk_ro(md->disk, md->read_only || default_ro); 2340 md->disk->flags = GENHD_FL_EXT_DEVT; 2341 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT)) 2342 md->disk->flags |= GENHD_FL_NO_PART_SCAN 2343 | GENHD_FL_SUPPRESS_PARTITION_INFO; 2344 2345 /* 2346 * As discussed on lkml, GENHD_FL_REMOVABLE should: 2347 * 2348 * - be set for removable media with permanent block devices 2349 * - be unset for removable block devices with permanent media 2350 * 2351 * Since MMC block devices clearly fall under the second 2352 * case, we do not set GENHD_FL_REMOVABLE. Userspace 2353 * should use the block device creation/destruction hotplug 2354 * messages to tell when the card is present. 2355 */ 2356 2357 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), 2358 "mmcblk%u%s", card->host->index, subname ? subname : ""); 2359 2360 set_capacity(md->disk, size); 2361 2362 if (mmc_host_cmd23(card->host)) { 2363 if ((mmc_card_mmc(card) && 2364 card->csd.mmca_vsn >= CSD_SPEC_VER_3) || 2365 (mmc_card_sd(card) && 2366 card->scr.cmds & SD_SCR_CMD23_SUPPORT)) 2367 md->flags |= MMC_BLK_CMD23; 2368 } 2369 2370 if (mmc_card_mmc(card) && 2371 md->flags & MMC_BLK_CMD23 && 2372 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || 2373 card->ext_csd.rel_sectors)) { 2374 md->flags |= MMC_BLK_REL_WR; 2375 blk_queue_write_cache(md->queue.queue, true, true); 2376 } 2377 2378 string_get_size((u64)size, 512, STRING_UNITS_2, 2379 cap_str, sizeof(cap_str)); 2380 pr_info("%s: %s %s %s %s\n", 2381 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), 2382 cap_str, md->read_only ? "(ro)" : ""); 2383 2384 return md; 2385 2386 err_kfree: 2387 kfree(md); 2388 out: 2389 ida_simple_remove(&mmc_blk_ida, devidx); 2390 return ERR_PTR(ret); 2391 } 2392 2393 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) 2394 { 2395 sector_t size; 2396 2397 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { 2398 /* 2399 * The EXT_CSD sector count is in number or 512 byte 2400 * sectors. 2401 */ 2402 size = card->ext_csd.sectors; 2403 } else { 2404 /* 2405 * The CSD capacity field is in units of read_blkbits. 2406 * set_capacity takes units of 512 bytes. 2407 */ 2408 size = (typeof(sector_t))card->csd.capacity 2409 << (card->csd.read_blkbits - 9); 2410 } 2411 2412 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL, 2413 MMC_BLK_DATA_AREA_MAIN); 2414 } 2415 2416 static int mmc_blk_alloc_part(struct mmc_card *card, 2417 struct mmc_blk_data *md, 2418 unsigned int part_type, 2419 sector_t size, 2420 bool default_ro, 2421 const char *subname, 2422 int area_type) 2423 { 2424 struct mmc_blk_data *part_md; 2425 2426 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro, 2427 subname, area_type); 2428 if (IS_ERR(part_md)) 2429 return PTR_ERR(part_md); 2430 part_md->part_type = part_type; 2431 list_add(&part_md->part, &md->part); 2432 2433 return 0; 2434 } 2435 2436 /** 2437 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev 2438 * @filp: the character device file 2439 * @cmd: the ioctl() command 2440 * @arg: the argument from userspace 2441 * 2442 * This will essentially just redirect the ioctl()s coming in over to 2443 * the main block device spawning the RPMB character device. 2444 */ 2445 static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd, 2446 unsigned long arg) 2447 { 2448 struct mmc_rpmb_data *rpmb = filp->private_data; 2449 int ret; 2450 2451 switch (cmd) { 2452 case MMC_IOC_CMD: 2453 ret = mmc_blk_ioctl_cmd(rpmb->md, 2454 (struct mmc_ioc_cmd __user *)arg, 2455 rpmb); 2456 break; 2457 case MMC_IOC_MULTI_CMD: 2458 ret = mmc_blk_ioctl_multi_cmd(rpmb->md, 2459 (struct mmc_ioc_multi_cmd __user *)arg, 2460 rpmb); 2461 break; 2462 default: 2463 ret = -EINVAL; 2464 break; 2465 } 2466 2467 return ret; 2468 } 2469 2470 #ifdef CONFIG_COMPAT 2471 static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd, 2472 unsigned long arg) 2473 { 2474 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); 2475 } 2476 #endif 2477 2478 static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp) 2479 { 2480 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev, 2481 struct mmc_rpmb_data, chrdev); 2482 2483 get_device(&rpmb->dev); 2484 filp->private_data = rpmb; 2485 mmc_blk_get(rpmb->md->disk); 2486 2487 return nonseekable_open(inode, filp); 2488 } 2489 2490 static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp) 2491 { 2492 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev, 2493 struct mmc_rpmb_data, chrdev); 2494 2495 mmc_blk_put(rpmb->md); 2496 put_device(&rpmb->dev); 2497 2498 return 0; 2499 } 2500 2501 static const struct file_operations mmc_rpmb_fileops = { 2502 .release = mmc_rpmb_chrdev_release, 2503 .open = mmc_rpmb_chrdev_open, 2504 .owner = THIS_MODULE, 2505 .llseek = no_llseek, 2506 .unlocked_ioctl = mmc_rpmb_ioctl, 2507 #ifdef CONFIG_COMPAT 2508 .compat_ioctl = mmc_rpmb_ioctl_compat, 2509 #endif 2510 }; 2511 2512 static void mmc_blk_rpmb_device_release(struct device *dev) 2513 { 2514 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev); 2515 2516 ida_simple_remove(&mmc_rpmb_ida, rpmb->id); 2517 kfree(rpmb); 2518 } 2519 2520 static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, 2521 struct mmc_blk_data *md, 2522 unsigned int part_index, 2523 sector_t size, 2524 const char *subname) 2525 { 2526 int devidx, ret; 2527 char rpmb_name[DISK_NAME_LEN]; 2528 char cap_str[10]; 2529 struct mmc_rpmb_data *rpmb; 2530 2531 /* This creates the minor number for the RPMB char device */ 2532 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL); 2533 if (devidx < 0) 2534 return devidx; 2535 2536 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL); 2537 if (!rpmb) { 2538 ida_simple_remove(&mmc_rpmb_ida, devidx); 2539 return -ENOMEM; 2540 } 2541 2542 snprintf(rpmb_name, sizeof(rpmb_name), 2543 "mmcblk%u%s", card->host->index, subname ? subname : ""); 2544 2545 rpmb->id = devidx; 2546 rpmb->part_index = part_index; 2547 rpmb->dev.init_name = rpmb_name; 2548 rpmb->dev.bus = &mmc_rpmb_bus_type; 2549 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id); 2550 rpmb->dev.parent = &card->dev; 2551 rpmb->dev.release = mmc_blk_rpmb_device_release; 2552 device_initialize(&rpmb->dev); 2553 dev_set_drvdata(&rpmb->dev, rpmb); 2554 rpmb->md = md; 2555 2556 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops); 2557 rpmb->chrdev.owner = THIS_MODULE; 2558 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev); 2559 if (ret) { 2560 pr_err("%s: could not add character device\n", rpmb_name); 2561 goto out_put_device; 2562 } 2563 2564 list_add(&rpmb->node, &md->rpmbs); 2565 2566 string_get_size((u64)size, 512, STRING_UNITS_2, 2567 cap_str, sizeof(cap_str)); 2568 2569 pr_info("%s: %s %s %s, chardev (%d:%d)\n", 2570 rpmb_name, mmc_card_id(card), mmc_card_name(card), cap_str, 2571 MAJOR(mmc_rpmb_devt), rpmb->id); 2572 2573 return 0; 2574 2575 out_put_device: 2576 put_device(&rpmb->dev); 2577 return ret; 2578 } 2579 2580 static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb) 2581 2582 { 2583 cdev_device_del(&rpmb->chrdev, &rpmb->dev); 2584 put_device(&rpmb->dev); 2585 } 2586 2587 /* MMC Physical partitions consist of two boot partitions and 2588 * up to four general purpose partitions. 2589 * For each partition enabled in EXT_CSD a block device will be allocatedi 2590 * to provide access to the partition. 2591 */ 2592 2593 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md) 2594 { 2595 int idx, ret; 2596 2597 if (!mmc_card_mmc(card)) 2598 return 0; 2599 2600 for (idx = 0; idx < card->nr_parts; idx++) { 2601 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) { 2602 /* 2603 * RPMB partitions does not provide block access, they 2604 * are only accessed using ioctl():s. Thus create 2605 * special RPMB block devices that do not have a 2606 * backing block queue for these. 2607 */ 2608 ret = mmc_blk_alloc_rpmb_part(card, md, 2609 card->part[idx].part_cfg, 2610 card->part[idx].size >> 9, 2611 card->part[idx].name); 2612 if (ret) 2613 return ret; 2614 } else if (card->part[idx].size) { 2615 ret = mmc_blk_alloc_part(card, md, 2616 card->part[idx].part_cfg, 2617 card->part[idx].size >> 9, 2618 card->part[idx].force_ro, 2619 card->part[idx].name, 2620 card->part[idx].area_type); 2621 if (ret) 2622 return ret; 2623 } 2624 } 2625 2626 return 0; 2627 } 2628 2629 static void mmc_blk_remove_req(struct mmc_blk_data *md) 2630 { 2631 struct mmc_card *card; 2632 2633 if (md) { 2634 /* 2635 * Flush remaining requests and free queues. It 2636 * is freeing the queue that stops new requests 2637 * from being accepted. 2638 */ 2639 card = md->queue.card; 2640 if (md->disk->flags & GENHD_FL_UP) { 2641 device_remove_file(disk_to_dev(md->disk), &md->force_ro); 2642 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && 2643 card->ext_csd.boot_ro_lockable) 2644 device_remove_file(disk_to_dev(md->disk), 2645 &md->power_ro_lock); 2646 2647 del_gendisk(md->disk); 2648 } 2649 mmc_cleanup_queue(&md->queue); 2650 mmc_blk_put(md); 2651 } 2652 } 2653 2654 static void mmc_blk_remove_parts(struct mmc_card *card, 2655 struct mmc_blk_data *md) 2656 { 2657 struct list_head *pos, *q; 2658 struct mmc_blk_data *part_md; 2659 struct mmc_rpmb_data *rpmb; 2660 2661 /* Remove RPMB partitions */ 2662 list_for_each_safe(pos, q, &md->rpmbs) { 2663 rpmb = list_entry(pos, struct mmc_rpmb_data, node); 2664 list_del(pos); 2665 mmc_blk_remove_rpmb_part(rpmb); 2666 } 2667 /* Remove block partitions */ 2668 list_for_each_safe(pos, q, &md->part) { 2669 part_md = list_entry(pos, struct mmc_blk_data, part); 2670 list_del(pos); 2671 mmc_blk_remove_req(part_md); 2672 } 2673 } 2674 2675 static int mmc_add_disk(struct mmc_blk_data *md) 2676 { 2677 int ret; 2678 struct mmc_card *card = md->queue.card; 2679 2680 device_add_disk(md->parent, md->disk, NULL); 2681 md->force_ro.show = force_ro_show; 2682 md->force_ro.store = force_ro_store; 2683 sysfs_attr_init(&md->force_ro.attr); 2684 md->force_ro.attr.name = "force_ro"; 2685 md->force_ro.attr.mode = S_IRUGO | S_IWUSR; 2686 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro); 2687 if (ret) 2688 goto force_ro_fail; 2689 2690 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && 2691 card->ext_csd.boot_ro_lockable) { 2692 umode_t mode; 2693 2694 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS) 2695 mode = S_IRUGO; 2696 else 2697 mode = S_IRUGO | S_IWUSR; 2698 2699 md->power_ro_lock.show = power_ro_lock_show; 2700 md->power_ro_lock.store = power_ro_lock_store; 2701 sysfs_attr_init(&md->power_ro_lock.attr); 2702 md->power_ro_lock.attr.mode = mode; 2703 md->power_ro_lock.attr.name = 2704 "ro_lock_until_next_power_on"; 2705 ret = device_create_file(disk_to_dev(md->disk), 2706 &md->power_ro_lock); 2707 if (ret) 2708 goto power_ro_lock_fail; 2709 } 2710 return ret; 2711 2712 power_ro_lock_fail: 2713 device_remove_file(disk_to_dev(md->disk), &md->force_ro); 2714 force_ro_fail: 2715 del_gendisk(md->disk); 2716 2717 return ret; 2718 } 2719 2720 #ifdef CONFIG_DEBUG_FS 2721 2722 static int mmc_dbg_card_status_get(void *data, u64 *val) 2723 { 2724 struct mmc_card *card = data; 2725 struct mmc_blk_data *md = dev_get_drvdata(&card->dev); 2726 struct mmc_queue *mq = &md->queue; 2727 struct request *req; 2728 int ret; 2729 2730 /* Ask the block layer about the card status */ 2731 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0); 2732 if (IS_ERR(req)) 2733 return PTR_ERR(req); 2734 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS; 2735 blk_execute_rq(NULL, req, 0); 2736 ret = req_to_mmc_queue_req(req)->drv_op_result; 2737 if (ret >= 0) { 2738 *val = ret; 2739 ret = 0; 2740 } 2741 blk_put_request(req); 2742 2743 return ret; 2744 } 2745 DEFINE_DEBUGFS_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get, 2746 NULL, "%08llx\n"); 2747 2748 /* That is two digits * 512 + 1 for newline */ 2749 #define EXT_CSD_STR_LEN 1025 2750 2751 static int mmc_ext_csd_open(struct inode *inode, struct file *filp) 2752 { 2753 struct mmc_card *card = inode->i_private; 2754 struct mmc_blk_data *md = dev_get_drvdata(&card->dev); 2755 struct mmc_queue *mq = &md->queue; 2756 struct request *req; 2757 char *buf; 2758 ssize_t n = 0; 2759 u8 *ext_csd; 2760 int err, i; 2761 2762 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL); 2763 if (!buf) 2764 return -ENOMEM; 2765 2766 /* Ask the block layer for the EXT CSD */ 2767 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0); 2768 if (IS_ERR(req)) { 2769 err = PTR_ERR(req); 2770 goto out_free; 2771 } 2772 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD; 2773 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd; 2774 blk_execute_rq(NULL, req, 0); 2775 err = req_to_mmc_queue_req(req)->drv_op_result; 2776 blk_put_request(req); 2777 if (err) { 2778 pr_err("FAILED %d\n", err); 2779 goto out_free; 2780 } 2781 2782 for (i = 0; i < 512; i++) 2783 n += sprintf(buf + n, "%02x", ext_csd[i]); 2784 n += sprintf(buf + n, "\n"); 2785 2786 if (n != EXT_CSD_STR_LEN) { 2787 err = -EINVAL; 2788 kfree(ext_csd); 2789 goto out_free; 2790 } 2791 2792 filp->private_data = buf; 2793 kfree(ext_csd); 2794 return 0; 2795 2796 out_free: 2797 kfree(buf); 2798 return err; 2799 } 2800 2801 static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf, 2802 size_t cnt, loff_t *ppos) 2803 { 2804 char *buf = filp->private_data; 2805 2806 return simple_read_from_buffer(ubuf, cnt, ppos, 2807 buf, EXT_CSD_STR_LEN); 2808 } 2809 2810 static int mmc_ext_csd_release(struct inode *inode, struct file *file) 2811 { 2812 kfree(file->private_data); 2813 return 0; 2814 } 2815 2816 static const struct file_operations mmc_dbg_ext_csd_fops = { 2817 .open = mmc_ext_csd_open, 2818 .read = mmc_ext_csd_read, 2819 .release = mmc_ext_csd_release, 2820 .llseek = default_llseek, 2821 }; 2822 2823 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md) 2824 { 2825 struct dentry *root; 2826 2827 if (!card->debugfs_root) 2828 return 0; 2829 2830 root = card->debugfs_root; 2831 2832 if (mmc_card_mmc(card) || mmc_card_sd(card)) { 2833 md->status_dentry = 2834 debugfs_create_file_unsafe("status", 0400, root, 2835 card, 2836 &mmc_dbg_card_status_fops); 2837 if (!md->status_dentry) 2838 return -EIO; 2839 } 2840 2841 if (mmc_card_mmc(card)) { 2842 md->ext_csd_dentry = 2843 debugfs_create_file("ext_csd", S_IRUSR, root, card, 2844 &mmc_dbg_ext_csd_fops); 2845 if (!md->ext_csd_dentry) 2846 return -EIO; 2847 } 2848 2849 return 0; 2850 } 2851 2852 static void mmc_blk_remove_debugfs(struct mmc_card *card, 2853 struct mmc_blk_data *md) 2854 { 2855 if (!card->debugfs_root) 2856 return; 2857 2858 if (!IS_ERR_OR_NULL(md->status_dentry)) { 2859 debugfs_remove(md->status_dentry); 2860 md->status_dentry = NULL; 2861 } 2862 2863 if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) { 2864 debugfs_remove(md->ext_csd_dentry); 2865 md->ext_csd_dentry = NULL; 2866 } 2867 } 2868 2869 #else 2870 2871 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md) 2872 { 2873 return 0; 2874 } 2875 2876 static void mmc_blk_remove_debugfs(struct mmc_card *card, 2877 struct mmc_blk_data *md) 2878 { 2879 } 2880 2881 #endif /* CONFIG_DEBUG_FS */ 2882 2883 static int mmc_blk_probe(struct mmc_card *card) 2884 { 2885 struct mmc_blk_data *md, *part_md; 2886 int ret = 0; 2887 2888 /* 2889 * Check that the card supports the command class(es) we need. 2890 */ 2891 if (!(card->csd.cmdclass & CCC_BLOCK_READ)) 2892 return -ENODEV; 2893 2894 mmc_fixup_device(card, mmc_blk_fixups); 2895 2896 card->complete_wq = alloc_workqueue("mmc_complete", 2897 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0); 2898 if (!card->complete_wq) { 2899 pr_err("Failed to create mmc completion workqueue"); 2900 return -ENOMEM; 2901 } 2902 2903 md = mmc_blk_alloc(card); 2904 if (IS_ERR(md)) { 2905 ret = PTR_ERR(md); 2906 goto out_free; 2907 } 2908 2909 ret = mmc_blk_alloc_parts(card, md); 2910 if (ret) 2911 goto out; 2912 2913 dev_set_drvdata(&card->dev, md); 2914 2915 ret = mmc_add_disk(md); 2916 if (ret) 2917 goto out; 2918 2919 list_for_each_entry(part_md, &md->part, part) { 2920 ret = mmc_add_disk(part_md); 2921 if (ret) 2922 goto out; 2923 } 2924 2925 /* Add two debugfs entries */ 2926 mmc_blk_add_debugfs(card, md); 2927 2928 pm_runtime_set_autosuspend_delay(&card->dev, 3000); 2929 pm_runtime_use_autosuspend(&card->dev); 2930 2931 /* 2932 * Don't enable runtime PM for SD-combo cards here. Leave that 2933 * decision to be taken during the SDIO init sequence instead. 2934 */ 2935 if (card->type != MMC_TYPE_SD_COMBO) { 2936 pm_runtime_set_active(&card->dev); 2937 pm_runtime_enable(&card->dev); 2938 } 2939 2940 return 0; 2941 2942 out: 2943 mmc_blk_remove_parts(card, md); 2944 mmc_blk_remove_req(md); 2945 out_free: 2946 destroy_workqueue(card->complete_wq); 2947 return ret; 2948 } 2949 2950 static void mmc_blk_remove(struct mmc_card *card) 2951 { 2952 struct mmc_blk_data *md = dev_get_drvdata(&card->dev); 2953 2954 mmc_blk_remove_debugfs(card, md); 2955 mmc_blk_remove_parts(card, md); 2956 pm_runtime_get_sync(&card->dev); 2957 if (md->part_curr != md->part_type) { 2958 mmc_claim_host(card->host); 2959 mmc_blk_part_switch(card, md->part_type); 2960 mmc_release_host(card->host); 2961 } 2962 if (card->type != MMC_TYPE_SD_COMBO) 2963 pm_runtime_disable(&card->dev); 2964 pm_runtime_put_noidle(&card->dev); 2965 mmc_blk_remove_req(md); 2966 dev_set_drvdata(&card->dev, NULL); 2967 destroy_workqueue(card->complete_wq); 2968 } 2969 2970 static int _mmc_blk_suspend(struct mmc_card *card) 2971 { 2972 struct mmc_blk_data *part_md; 2973 struct mmc_blk_data *md = dev_get_drvdata(&card->dev); 2974 2975 if (md) { 2976 mmc_queue_suspend(&md->queue); 2977 list_for_each_entry(part_md, &md->part, part) { 2978 mmc_queue_suspend(&part_md->queue); 2979 } 2980 } 2981 return 0; 2982 } 2983 2984 static void mmc_blk_shutdown(struct mmc_card *card) 2985 { 2986 _mmc_blk_suspend(card); 2987 } 2988 2989 #ifdef CONFIG_PM_SLEEP 2990 static int mmc_blk_suspend(struct device *dev) 2991 { 2992 struct mmc_card *card = mmc_dev_to_card(dev); 2993 2994 return _mmc_blk_suspend(card); 2995 } 2996 2997 static int mmc_blk_resume(struct device *dev) 2998 { 2999 struct mmc_blk_data *part_md; 3000 struct mmc_blk_data *md = dev_get_drvdata(dev); 3001 3002 if (md) { 3003 /* 3004 * Resume involves the card going into idle state, 3005 * so current partition is always the main one. 3006 */ 3007 md->part_curr = md->part_type; 3008 mmc_queue_resume(&md->queue); 3009 list_for_each_entry(part_md, &md->part, part) { 3010 mmc_queue_resume(&part_md->queue); 3011 } 3012 } 3013 return 0; 3014 } 3015 #endif 3016 3017 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume); 3018 3019 static struct mmc_driver mmc_driver = { 3020 .drv = { 3021 .name = "mmcblk", 3022 .pm = &mmc_blk_pm_ops, 3023 }, 3024 .probe = mmc_blk_probe, 3025 .remove = mmc_blk_remove, 3026 .shutdown = mmc_blk_shutdown, 3027 }; 3028 3029 static int __init mmc_blk_init(void) 3030 { 3031 int res; 3032 3033 res = bus_register(&mmc_rpmb_bus_type); 3034 if (res < 0) { 3035 pr_err("mmcblk: could not register RPMB bus type\n"); 3036 return res; 3037 } 3038 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb"); 3039 if (res < 0) { 3040 pr_err("mmcblk: failed to allocate rpmb chrdev region\n"); 3041 goto out_bus_unreg; 3042 } 3043 3044 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) 3045 pr_info("mmcblk: using %d minors per device\n", perdev_minors); 3046 3047 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors); 3048 3049 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); 3050 if (res) 3051 goto out_chrdev_unreg; 3052 3053 res = mmc_register_driver(&mmc_driver); 3054 if (res) 3055 goto out_blkdev_unreg; 3056 3057 return 0; 3058 3059 out_blkdev_unreg: 3060 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); 3061 out_chrdev_unreg: 3062 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES); 3063 out_bus_unreg: 3064 bus_unregister(&mmc_rpmb_bus_type); 3065 return res; 3066 } 3067 3068 static void __exit mmc_blk_exit(void) 3069 { 3070 mmc_unregister_driver(&mmc_driver); 3071 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); 3072 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES); 3073 bus_unregister(&mmc_rpmb_bus_type); 3074 } 3075 3076 module_init(mmc_blk_init); 3077 module_exit(mmc_blk_exit); 3078 3079 MODULE_LICENSE("GPL"); 3080 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); 3081 3082