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