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 nr; 1203 sector_t from; 1204 int err = 0; 1205 blk_status_t status = BLK_STS_OK; 1206 1207 if (!mmc_can_erase(card)) { 1208 status = BLK_STS_NOTSUPP; 1209 goto fail; 1210 } 1211 1212 from = blk_rq_pos(req); 1213 nr = blk_rq_sectors(req); 1214 1215 do { 1216 err = 0; 1217 if (card->quirks & MMC_QUIRK_INAND_CMD38) { 1218 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 1219 INAND_CMD38_ARG_EXT_CSD, 1220 erase_arg == MMC_TRIM_ARG ? 1221 INAND_CMD38_ARG_TRIM : 1222 INAND_CMD38_ARG_ERASE, 1223 card->ext_csd.generic_cmd6_time); 1224 } 1225 if (!err) 1226 err = mmc_erase(card, from, nr, erase_arg); 1227 } while (err == -EIO && !mmc_blk_reset(md, card->host, type)); 1228 if (err) 1229 status = BLK_STS_IOERR; 1230 else 1231 mmc_blk_reset_success(md, type); 1232 fail: 1233 blk_mq_end_request(req, status); 1234 } 1235 1236 static void mmc_blk_issue_trim_rq(struct mmc_queue *mq, struct request *req) 1237 { 1238 mmc_blk_issue_erase_rq(mq, req, MMC_BLK_TRIM, MMC_TRIM_ARG); 1239 } 1240 1241 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req) 1242 { 1243 struct mmc_blk_data *md = mq->blkdata; 1244 struct mmc_card *card = md->queue.card; 1245 unsigned int arg = card->erase_arg; 1246 1247 if (mmc_card_broken_sd_discard(card)) 1248 arg = SD_ERASE_ARG; 1249 1250 mmc_blk_issue_erase_rq(mq, req, MMC_BLK_DISCARD, arg); 1251 } 1252 1253 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq, 1254 struct request *req) 1255 { 1256 struct mmc_blk_data *md = mq->blkdata; 1257 struct mmc_card *card = md->queue.card; 1258 unsigned int nr, arg; 1259 sector_t from; 1260 int err = 0, type = MMC_BLK_SECDISCARD; 1261 blk_status_t status = BLK_STS_OK; 1262 1263 if (!(mmc_can_secure_erase_trim(card))) { 1264 status = BLK_STS_NOTSUPP; 1265 goto out; 1266 } 1267 1268 from = blk_rq_pos(req); 1269 nr = blk_rq_sectors(req); 1270 1271 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr)) 1272 arg = MMC_SECURE_TRIM1_ARG; 1273 else 1274 arg = MMC_SECURE_ERASE_ARG; 1275 1276 retry: 1277 if (card->quirks & MMC_QUIRK_INAND_CMD38) { 1278 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 1279 INAND_CMD38_ARG_EXT_CSD, 1280 arg == MMC_SECURE_TRIM1_ARG ? 1281 INAND_CMD38_ARG_SECTRIM1 : 1282 INAND_CMD38_ARG_SECERASE, 1283 card->ext_csd.generic_cmd6_time); 1284 if (err) 1285 goto out_retry; 1286 } 1287 1288 err = mmc_erase(card, from, nr, arg); 1289 if (err == -EIO) 1290 goto out_retry; 1291 if (err) { 1292 status = BLK_STS_IOERR; 1293 goto out; 1294 } 1295 1296 if (arg == MMC_SECURE_TRIM1_ARG) { 1297 if (card->quirks & MMC_QUIRK_INAND_CMD38) { 1298 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 1299 INAND_CMD38_ARG_EXT_CSD, 1300 INAND_CMD38_ARG_SECTRIM2, 1301 card->ext_csd.generic_cmd6_time); 1302 if (err) 1303 goto out_retry; 1304 } 1305 1306 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG); 1307 if (err == -EIO) 1308 goto out_retry; 1309 if (err) { 1310 status = BLK_STS_IOERR; 1311 goto out; 1312 } 1313 } 1314 1315 out_retry: 1316 if (err && !mmc_blk_reset(md, card->host, type)) 1317 goto retry; 1318 if (!err) 1319 mmc_blk_reset_success(md, type); 1320 out: 1321 blk_mq_end_request(req, status); 1322 } 1323 1324 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req) 1325 { 1326 struct mmc_blk_data *md = mq->blkdata; 1327 struct mmc_card *card = md->queue.card; 1328 int ret = 0; 1329 1330 ret = mmc_flush_cache(card->host); 1331 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK); 1332 } 1333 1334 /* 1335 * Reformat current write as a reliable write, supporting 1336 * both legacy and the enhanced reliable write MMC cards. 1337 * In each transfer we'll handle only as much as a single 1338 * reliable write can handle, thus finish the request in 1339 * partial completions. 1340 */ 1341 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, 1342 struct mmc_card *card, 1343 struct request *req) 1344 { 1345 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) { 1346 /* Legacy mode imposes restrictions on transfers. */ 1347 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors)) 1348 brq->data.blocks = 1; 1349 1350 if (brq->data.blocks > card->ext_csd.rel_sectors) 1351 brq->data.blocks = card->ext_csd.rel_sectors; 1352 else if (brq->data.blocks < card->ext_csd.rel_sectors) 1353 brq->data.blocks = 1; 1354 } 1355 } 1356 1357 #define CMD_ERRORS_EXCL_OOR \ 1358 (R1_ADDRESS_ERROR | /* Misaligned address */ \ 1359 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\ 1360 R1_WP_VIOLATION | /* Tried to write to protected block */ \ 1361 R1_CARD_ECC_FAILED | /* Card ECC failed */ \ 1362 R1_CC_ERROR | /* Card controller error */ \ 1363 R1_ERROR) /* General/unknown error */ 1364 1365 #define CMD_ERRORS \ 1366 (CMD_ERRORS_EXCL_OOR | \ 1367 R1_OUT_OF_RANGE) /* Command argument out of range */ \ 1368 1369 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq) 1370 { 1371 u32 val; 1372 1373 /* 1374 * Per the SD specification(physical layer version 4.10)[1], 1375 * section 4.3.3, it explicitly states that "When the last 1376 * block of user area is read using CMD18, the host should 1377 * ignore OUT_OF_RANGE error that may occur even the sequence 1378 * is correct". And JESD84-B51 for eMMC also has a similar 1379 * statement on section 6.8.3. 1380 * 1381 * Multiple block read/write could be done by either predefined 1382 * method, namely CMD23, or open-ending mode. For open-ending mode, 1383 * we should ignore the OUT_OF_RANGE error as it's normal behaviour. 1384 * 1385 * However the spec[1] doesn't tell us whether we should also 1386 * ignore that for predefined method. But per the spec[1], section 1387 * 4.15 Set Block Count Command, it says"If illegal block count 1388 * is set, out of range error will be indicated during read/write 1389 * operation (For example, data transfer is stopped at user area 1390 * boundary)." In another word, we could expect a out of range error 1391 * in the response for the following CMD18/25. And if argument of 1392 * CMD23 + the argument of CMD18/25 exceed the max number of blocks, 1393 * we could also expect to get a -ETIMEDOUT or any error number from 1394 * the host drivers due to missing data response(for write)/data(for 1395 * read), as the cards will stop the data transfer by itself per the 1396 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode. 1397 */ 1398 1399 if (!brq->stop.error) { 1400 bool oor_with_open_end; 1401 /* If there is no error yet, check R1 response */ 1402 1403 val = brq->stop.resp[0] & CMD_ERRORS; 1404 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc; 1405 1406 if (val && !oor_with_open_end) 1407 brq->stop.error = -EIO; 1408 } 1409 } 1410 1411 static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq, 1412 int recovery_mode, bool *do_rel_wr_p, 1413 bool *do_data_tag_p) 1414 { 1415 struct mmc_blk_data *md = mq->blkdata; 1416 struct mmc_card *card = md->queue.card; 1417 struct mmc_blk_request *brq = &mqrq->brq; 1418 struct request *req = mmc_queue_req_to_req(mqrq); 1419 bool do_rel_wr, do_data_tag; 1420 1421 /* 1422 * Reliable writes are used to implement Forced Unit Access and 1423 * are supported only on MMCs. 1424 */ 1425 do_rel_wr = (req->cmd_flags & REQ_FUA) && 1426 rq_data_dir(req) == WRITE && 1427 (md->flags & MMC_BLK_REL_WR); 1428 1429 memset(brq, 0, sizeof(struct mmc_blk_request)); 1430 1431 mmc_crypto_prepare_req(mqrq); 1432 1433 brq->mrq.data = &brq->data; 1434 brq->mrq.tag = req->tag; 1435 1436 brq->stop.opcode = MMC_STOP_TRANSMISSION; 1437 brq->stop.arg = 0; 1438 1439 if (rq_data_dir(req) == READ) { 1440 brq->data.flags = MMC_DATA_READ; 1441 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; 1442 } else { 1443 brq->data.flags = MMC_DATA_WRITE; 1444 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; 1445 } 1446 1447 brq->data.blksz = 512; 1448 brq->data.blocks = blk_rq_sectors(req); 1449 brq->data.blk_addr = blk_rq_pos(req); 1450 1451 /* 1452 * The command queue supports 2 priorities: "high" (1) and "simple" (0). 1453 * The eMMC will give "high" priority tasks priority over "simple" 1454 * priority tasks. Here we always set "simple" priority by not setting 1455 * MMC_DATA_PRIO. 1456 */ 1457 1458 /* 1459 * The block layer doesn't support all sector count 1460 * restrictions, so we need to be prepared for too big 1461 * requests. 1462 */ 1463 if (brq->data.blocks > card->host->max_blk_count) 1464 brq->data.blocks = card->host->max_blk_count; 1465 1466 if (brq->data.blocks > 1) { 1467 /* 1468 * Some SD cards in SPI mode return a CRC error or even lock up 1469 * completely when trying to read the last block using a 1470 * multiblock read command. 1471 */ 1472 if (mmc_host_is_spi(card->host) && (rq_data_dir(req) == READ) && 1473 (blk_rq_pos(req) + blk_rq_sectors(req) == 1474 get_capacity(md->disk))) 1475 brq->data.blocks--; 1476 1477 /* 1478 * After a read error, we redo the request one (native) sector 1479 * at a time in order to accurately determine which 1480 * sectors can be read successfully. 1481 */ 1482 if (recovery_mode) 1483 brq->data.blocks = queue_physical_block_size(mq->queue) >> 9; 1484 1485 /* 1486 * Some controllers have HW issues while operating 1487 * in multiple I/O mode 1488 */ 1489 if (card->host->ops->multi_io_quirk) 1490 brq->data.blocks = card->host->ops->multi_io_quirk(card, 1491 (rq_data_dir(req) == READ) ? 1492 MMC_DATA_READ : MMC_DATA_WRITE, 1493 brq->data.blocks); 1494 } 1495 1496 if (do_rel_wr) { 1497 mmc_apply_rel_rw(brq, card, req); 1498 brq->data.flags |= MMC_DATA_REL_WR; 1499 } 1500 1501 /* 1502 * Data tag is used only during writing meta data to speed 1503 * up write and any subsequent read of this meta data 1504 */ 1505 do_data_tag = card->ext_csd.data_tag_unit_size && 1506 (req->cmd_flags & REQ_META) && 1507 (rq_data_dir(req) == WRITE) && 1508 ((brq->data.blocks * brq->data.blksz) >= 1509 card->ext_csd.data_tag_unit_size); 1510 1511 if (do_data_tag) 1512 brq->data.flags |= MMC_DATA_DAT_TAG; 1513 1514 mmc_set_data_timeout(&brq->data, card); 1515 1516 brq->data.sg = mqrq->sg; 1517 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq); 1518 1519 /* 1520 * Adjust the sg list so it is the same size as the 1521 * request. 1522 */ 1523 if (brq->data.blocks != blk_rq_sectors(req)) { 1524 int i, data_size = brq->data.blocks << 9; 1525 struct scatterlist *sg; 1526 1527 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) { 1528 data_size -= sg->length; 1529 if (data_size <= 0) { 1530 sg->length += data_size; 1531 i++; 1532 break; 1533 } 1534 } 1535 brq->data.sg_len = i; 1536 } 1537 1538 if (do_rel_wr_p) 1539 *do_rel_wr_p = do_rel_wr; 1540 1541 if (do_data_tag_p) 1542 *do_data_tag_p = do_data_tag; 1543 } 1544 1545 #define MMC_CQE_RETRIES 2 1546 1547 static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req) 1548 { 1549 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1550 struct mmc_request *mrq = &mqrq->brq.mrq; 1551 struct request_queue *q = req->q; 1552 struct mmc_host *host = mq->card->host; 1553 enum mmc_issue_type issue_type = mmc_issue_type(mq, req); 1554 unsigned long flags; 1555 bool put_card; 1556 int err; 1557 1558 mmc_cqe_post_req(host, mrq); 1559 1560 if (mrq->cmd && mrq->cmd->error) 1561 err = mrq->cmd->error; 1562 else if (mrq->data && mrq->data->error) 1563 err = mrq->data->error; 1564 else 1565 err = 0; 1566 1567 if (err) { 1568 if (mqrq->retries++ < MMC_CQE_RETRIES) 1569 blk_mq_requeue_request(req, true); 1570 else 1571 blk_mq_end_request(req, BLK_STS_IOERR); 1572 } else if (mrq->data) { 1573 if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered)) 1574 blk_mq_requeue_request(req, true); 1575 else 1576 __blk_mq_end_request(req, BLK_STS_OK); 1577 } else if (mq->in_recovery) { 1578 blk_mq_requeue_request(req, true); 1579 } else { 1580 blk_mq_end_request(req, BLK_STS_OK); 1581 } 1582 1583 spin_lock_irqsave(&mq->lock, flags); 1584 1585 mq->in_flight[issue_type] -= 1; 1586 1587 put_card = (mmc_tot_in_flight(mq) == 0); 1588 1589 mmc_cqe_check_busy(mq); 1590 1591 spin_unlock_irqrestore(&mq->lock, flags); 1592 1593 if (!mq->cqe_busy) 1594 blk_mq_run_hw_queues(q, true); 1595 1596 if (put_card) 1597 mmc_put_card(mq->card, &mq->ctx); 1598 } 1599 1600 void mmc_blk_cqe_recovery(struct mmc_queue *mq) 1601 { 1602 struct mmc_card *card = mq->card; 1603 struct mmc_host *host = card->host; 1604 int err; 1605 1606 pr_debug("%s: CQE recovery start\n", mmc_hostname(host)); 1607 1608 err = mmc_cqe_recovery(host); 1609 if (err) 1610 mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY); 1611 mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY); 1612 1613 pr_debug("%s: CQE recovery done\n", mmc_hostname(host)); 1614 } 1615 1616 static void mmc_blk_cqe_req_done(struct mmc_request *mrq) 1617 { 1618 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req, 1619 brq.mrq); 1620 struct request *req = mmc_queue_req_to_req(mqrq); 1621 struct request_queue *q = req->q; 1622 struct mmc_queue *mq = q->queuedata; 1623 1624 /* 1625 * Block layer timeouts race with completions which means the normal 1626 * completion path cannot be used during recovery. 1627 */ 1628 if (mq->in_recovery) 1629 mmc_blk_cqe_complete_rq(mq, req); 1630 else if (likely(!blk_should_fake_timeout(req->q))) 1631 blk_mq_complete_request(req); 1632 } 1633 1634 static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq) 1635 { 1636 mrq->done = mmc_blk_cqe_req_done; 1637 mrq->recovery_notifier = mmc_cqe_recovery_notifier; 1638 1639 return mmc_cqe_start_req(host, mrq); 1640 } 1641 1642 static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq, 1643 struct request *req) 1644 { 1645 struct mmc_blk_request *brq = &mqrq->brq; 1646 1647 memset(brq, 0, sizeof(*brq)); 1648 1649 brq->mrq.cmd = &brq->cmd; 1650 brq->mrq.tag = req->tag; 1651 1652 return &brq->mrq; 1653 } 1654 1655 static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req) 1656 { 1657 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1658 struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req); 1659 1660 mrq->cmd->opcode = MMC_SWITCH; 1661 mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | 1662 (EXT_CSD_FLUSH_CACHE << 16) | 1663 (1 << 8) | 1664 EXT_CSD_CMD_SET_NORMAL; 1665 mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B; 1666 1667 return mmc_blk_cqe_start_req(mq->card->host, mrq); 1668 } 1669 1670 static int mmc_blk_hsq_issue_rw_rq(struct mmc_queue *mq, struct request *req) 1671 { 1672 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1673 struct mmc_host *host = mq->card->host; 1674 int err; 1675 1676 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq); 1677 mqrq->brq.mrq.done = mmc_blk_hsq_req_done; 1678 mmc_pre_req(host, &mqrq->brq.mrq); 1679 1680 err = mmc_cqe_start_req(host, &mqrq->brq.mrq); 1681 if (err) 1682 mmc_post_req(host, &mqrq->brq.mrq, err); 1683 1684 return err; 1685 } 1686 1687 static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req) 1688 { 1689 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1690 struct mmc_host *host = mq->card->host; 1691 1692 if (host->hsq_enabled) 1693 return mmc_blk_hsq_issue_rw_rq(mq, req); 1694 1695 mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL); 1696 1697 return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq); 1698 } 1699 1700 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, 1701 struct mmc_card *card, 1702 int recovery_mode, 1703 struct mmc_queue *mq) 1704 { 1705 u32 readcmd, writecmd; 1706 struct mmc_blk_request *brq = &mqrq->brq; 1707 struct request *req = mmc_queue_req_to_req(mqrq); 1708 struct mmc_blk_data *md = mq->blkdata; 1709 bool do_rel_wr, do_data_tag; 1710 1711 mmc_blk_data_prep(mq, mqrq, recovery_mode, &do_rel_wr, &do_data_tag); 1712 1713 brq->mrq.cmd = &brq->cmd; 1714 1715 brq->cmd.arg = blk_rq_pos(req); 1716 if (!mmc_card_blockaddr(card)) 1717 brq->cmd.arg <<= 9; 1718 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; 1719 1720 if (brq->data.blocks > 1 || do_rel_wr) { 1721 /* SPI multiblock writes terminate using a special 1722 * token, not a STOP_TRANSMISSION request. 1723 */ 1724 if (!mmc_host_is_spi(card->host) || 1725 rq_data_dir(req) == READ) 1726 brq->mrq.stop = &brq->stop; 1727 readcmd = MMC_READ_MULTIPLE_BLOCK; 1728 writecmd = MMC_WRITE_MULTIPLE_BLOCK; 1729 } else { 1730 brq->mrq.stop = NULL; 1731 readcmd = MMC_READ_SINGLE_BLOCK; 1732 writecmd = MMC_WRITE_BLOCK; 1733 } 1734 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd; 1735 1736 /* 1737 * Pre-defined multi-block transfers are preferable to 1738 * open ended-ones (and necessary for reliable writes). 1739 * However, it is not sufficient to just send CMD23, 1740 * and avoid the final CMD12, as on an error condition 1741 * CMD12 (stop) needs to be sent anyway. This, coupled 1742 * with Auto-CMD23 enhancements provided by some 1743 * hosts, means that the complexity of dealing 1744 * with this is best left to the host. If CMD23 is 1745 * supported by card and host, we'll fill sbc in and let 1746 * the host deal with handling it correctly. This means 1747 * that for hosts that don't expose MMC_CAP_CMD23, no 1748 * change of behavior will be observed. 1749 * 1750 * N.B: Some MMC cards experience perf degradation. 1751 * We'll avoid using CMD23-bounded multiblock writes for 1752 * these, while retaining features like reliable writes. 1753 */ 1754 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) && 1755 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) || 1756 do_data_tag)) { 1757 brq->sbc.opcode = MMC_SET_BLOCK_COUNT; 1758 brq->sbc.arg = brq->data.blocks | 1759 (do_rel_wr ? (1 << 31) : 0) | 1760 (do_data_tag ? (1 << 29) : 0); 1761 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; 1762 brq->mrq.sbc = &brq->sbc; 1763 } 1764 1765 if (mmc_card_ult_capacity(card)) { 1766 brq->cmd.ext_addr = blk_rq_pos(req) >> 32; 1767 brq->cmd.has_ext_addr = true; 1768 } 1769 } 1770 1771 #define MMC_MAX_RETRIES 5 1772 #define MMC_DATA_RETRIES 2 1773 #define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1) 1774 1775 static int mmc_blk_send_stop(struct mmc_card *card, unsigned int timeout) 1776 { 1777 struct mmc_command cmd = { 1778 .opcode = MMC_STOP_TRANSMISSION, 1779 .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC, 1780 /* Some hosts wait for busy anyway, so provide a busy timeout */ 1781 .busy_timeout = timeout, 1782 }; 1783 1784 return mmc_wait_for_cmd(card->host, &cmd, 5); 1785 } 1786 1787 static int mmc_blk_fix_state(struct mmc_card *card, struct request *req) 1788 { 1789 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1790 struct mmc_blk_request *brq = &mqrq->brq; 1791 unsigned int timeout = mmc_blk_data_timeout_ms(card->host, &brq->data); 1792 int err; 1793 1794 mmc_retune_hold_now(card->host); 1795 1796 mmc_blk_send_stop(card, timeout); 1797 1798 err = mmc_poll_for_busy(card, timeout, false, MMC_BUSY_IO); 1799 1800 mmc_retune_release(card->host); 1801 1802 return err; 1803 } 1804 1805 #define MMC_READ_SINGLE_RETRIES 2 1806 1807 /* Single (native) sector read during recovery */ 1808 static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req) 1809 { 1810 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1811 struct mmc_request *mrq = &mqrq->brq.mrq; 1812 struct mmc_card *card = mq->card; 1813 struct mmc_host *host = card->host; 1814 blk_status_t error = BLK_STS_OK; 1815 size_t bytes_per_read = queue_physical_block_size(mq->queue); 1816 1817 do { 1818 u32 status; 1819 int err; 1820 int retries = 0; 1821 1822 while (retries++ <= MMC_READ_SINGLE_RETRIES) { 1823 mmc_blk_rw_rq_prep(mqrq, card, 1, mq); 1824 1825 mmc_wait_for_req(host, mrq); 1826 1827 err = mmc_send_status(card, &status); 1828 if (err) 1829 goto error_exit; 1830 1831 if (!mmc_host_is_spi(host) && 1832 !mmc_ready_for_data(status)) { 1833 err = mmc_blk_fix_state(card, req); 1834 if (err) 1835 goto error_exit; 1836 } 1837 1838 if (!mrq->cmd->error) 1839 break; 1840 } 1841 1842 if (mrq->cmd->error || 1843 mrq->data->error || 1844 (!mmc_host_is_spi(host) && 1845 (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS))) 1846 error = BLK_STS_IOERR; 1847 else 1848 error = BLK_STS_OK; 1849 1850 } while (blk_update_request(req, error, bytes_per_read)); 1851 1852 return; 1853 1854 error_exit: 1855 mrq->data->bytes_xfered = 0; 1856 blk_update_request(req, BLK_STS_IOERR, bytes_per_read); 1857 /* Let it try the remaining request again */ 1858 if (mqrq->retries > MMC_MAX_RETRIES - 1) 1859 mqrq->retries = MMC_MAX_RETRIES - 1; 1860 } 1861 1862 static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq) 1863 { 1864 return !!brq->mrq.sbc; 1865 } 1866 1867 static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq) 1868 { 1869 return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR; 1870 } 1871 1872 /* 1873 * Check for errors the host controller driver might not have seen such as 1874 * response mode errors or invalid card state. 1875 */ 1876 static bool mmc_blk_status_error(struct request *req, u32 status) 1877 { 1878 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1879 struct mmc_blk_request *brq = &mqrq->brq; 1880 struct mmc_queue *mq = req->q->queuedata; 1881 u32 stop_err_bits; 1882 1883 if (mmc_host_is_spi(mq->card->host)) 1884 return false; 1885 1886 stop_err_bits = mmc_blk_stop_err_bits(brq); 1887 1888 return brq->cmd.resp[0] & CMD_ERRORS || 1889 brq->stop.resp[0] & stop_err_bits || 1890 status & stop_err_bits || 1891 (rq_data_dir(req) == WRITE && !mmc_ready_for_data(status)); 1892 } 1893 1894 static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq) 1895 { 1896 return !brq->sbc.error && !brq->cmd.error && 1897 !(brq->cmd.resp[0] & CMD_ERRORS); 1898 } 1899 1900 /* 1901 * Requests are completed by mmc_blk_mq_complete_rq() which sets simple 1902 * policy: 1903 * 1. A request that has transferred at least some data is considered 1904 * successful and will be requeued if there is remaining data to 1905 * transfer. 1906 * 2. Otherwise the number of retries is incremented and the request 1907 * will be requeued if there are remaining retries. 1908 * 3. Otherwise the request will be errored out. 1909 * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and 1910 * mqrq->retries. So there are only 4 possible actions here: 1911 * 1. do not accept the bytes_xfered value i.e. set it to zero 1912 * 2. change mqrq->retries to determine the number of retries 1913 * 3. try to reset the card 1914 * 4. read one sector at a time 1915 */ 1916 static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req) 1917 { 1918 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; 1919 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 1920 struct mmc_blk_request *brq = &mqrq->brq; 1921 struct mmc_blk_data *md = mq->blkdata; 1922 struct mmc_card *card = mq->card; 1923 u32 status; 1924 u32 blocks; 1925 int err; 1926 1927 /* 1928 * Some errors the host driver might not have seen. Set the number of 1929 * bytes transferred to zero in that case. 1930 */ 1931 err = __mmc_send_status(card, &status, 0); 1932 if (err || mmc_blk_status_error(req, status)) 1933 brq->data.bytes_xfered = 0; 1934 1935 mmc_retune_release(card->host); 1936 1937 /* 1938 * Try again to get the status. This also provides an opportunity for 1939 * re-tuning. 1940 */ 1941 if (err) 1942 err = __mmc_send_status(card, &status, 0); 1943 1944 /* 1945 * Nothing more to do after the number of bytes transferred has been 1946 * updated and there is no card. 1947 */ 1948 if (err && mmc_detect_card_removed(card->host)) 1949 return; 1950 1951 /* Try to get back to "tran" state */ 1952 if (!mmc_host_is_spi(mq->card->host) && 1953 (err || !mmc_ready_for_data(status))) 1954 err = mmc_blk_fix_state(mq->card, req); 1955 1956 /* 1957 * Special case for SD cards where the card might record the number of 1958 * blocks written. 1959 */ 1960 if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) && 1961 rq_data_dir(req) == WRITE) { 1962 if (mmc_sd_num_wr_blocks(card, &blocks)) 1963 brq->data.bytes_xfered = 0; 1964 else 1965 brq->data.bytes_xfered = blocks << 9; 1966 } 1967 1968 /* Reset if the card is in a bad state */ 1969 if (!mmc_host_is_spi(mq->card->host) && 1970 err && mmc_blk_reset(md, card->host, type)) { 1971 pr_err("%s: recovery failed!\n", req->q->disk->disk_name); 1972 mqrq->retries = MMC_NO_RETRIES; 1973 return; 1974 } 1975 1976 /* 1977 * If anything was done, just return and if there is anything remaining 1978 * on the request it will get requeued. 1979 */ 1980 if (brq->data.bytes_xfered) 1981 return; 1982 1983 /* Reset before last retry */ 1984 if (mqrq->retries + 1 == MMC_MAX_RETRIES && 1985 mmc_blk_reset(md, card->host, type)) 1986 return; 1987 1988 /* Command errors fail fast, so use all MMC_MAX_RETRIES */ 1989 if (brq->sbc.error || brq->cmd.error) 1990 return; 1991 1992 /* Reduce the remaining retries for data errors */ 1993 if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) { 1994 mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES; 1995 return; 1996 } 1997 1998 if (rq_data_dir(req) == READ && brq->data.blocks > 1999 queue_physical_block_size(mq->queue) >> 9) { 2000 /* Read one (native) sector at a time */ 2001 mmc_blk_read_single(mq, req); 2002 return; 2003 } 2004 } 2005 2006 static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq) 2007 { 2008 mmc_blk_eval_resp_error(brq); 2009 2010 return brq->sbc.error || brq->cmd.error || brq->stop.error || 2011 brq->data.error || brq->cmd.resp[0] & CMD_ERRORS; 2012 } 2013 2014 static int mmc_spi_err_check(struct mmc_card *card) 2015 { 2016 u32 status = 0; 2017 int err; 2018 2019 /* 2020 * SPI does not have a TRAN state we have to wait on, instead the 2021 * card is ready again when it no longer holds the line LOW. 2022 * We still have to ensure two things here before we know the write 2023 * was successful: 2024 * 1. The card has not disconnected during busy and we actually read our 2025 * own pull-up, thinking it was still connected, so ensure it 2026 * still responds. 2027 * 2. Check for any error bits, in particular R1_SPI_IDLE to catch a 2028 * just reconnected card after being disconnected during busy. 2029 */ 2030 err = __mmc_send_status(card, &status, 0); 2031 if (err) 2032 return err; 2033 /* All R1 and R2 bits of SPI are errors in our case */ 2034 if (status) 2035 return -EIO; 2036 return 0; 2037 } 2038 2039 static int mmc_blk_busy_cb(void *cb_data, bool *busy) 2040 { 2041 struct mmc_blk_busy_data *data = cb_data; 2042 u32 status = 0; 2043 int err; 2044 2045 err = mmc_send_status(data->card, &status); 2046 if (err) 2047 return err; 2048 2049 /* Accumulate response error bits. */ 2050 data->status |= status; 2051 2052 *busy = !mmc_ready_for_data(status); 2053 return 0; 2054 } 2055 2056 static int mmc_blk_card_busy(struct mmc_card *card, struct request *req) 2057 { 2058 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 2059 struct mmc_blk_busy_data cb_data; 2060 int err; 2061 2062 if (rq_data_dir(req) == READ) 2063 return 0; 2064 2065 if (mmc_host_is_spi(card->host)) { 2066 err = mmc_spi_err_check(card); 2067 if (err) 2068 mqrq->brq.data.bytes_xfered = 0; 2069 return err; 2070 } 2071 2072 cb_data.card = card; 2073 cb_data.status = 0; 2074 err = __mmc_poll_for_busy(card->host, 0, MMC_BLK_TIMEOUT_MS, 2075 &mmc_blk_busy_cb, &cb_data); 2076 2077 /* 2078 * Do not assume data transferred correctly if there are any error bits 2079 * set. 2080 */ 2081 if (cb_data.status & mmc_blk_stop_err_bits(&mqrq->brq)) { 2082 mqrq->brq.data.bytes_xfered = 0; 2083 err = err ? err : -EIO; 2084 } 2085 2086 /* Copy the exception bit so it will be seen later on */ 2087 if (mmc_card_mmc(card) && cb_data.status & R1_EXCEPTION_EVENT) 2088 mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT; 2089 2090 return err; 2091 } 2092 2093 static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq, 2094 struct request *req) 2095 { 2096 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; 2097 2098 mmc_blk_reset_success(mq->blkdata, type); 2099 } 2100 2101 static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req) 2102 { 2103 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 2104 unsigned int nr_bytes = mqrq->brq.data.bytes_xfered; 2105 2106 if (nr_bytes) { 2107 if (blk_update_request(req, BLK_STS_OK, nr_bytes)) 2108 blk_mq_requeue_request(req, true); 2109 else 2110 __blk_mq_end_request(req, BLK_STS_OK); 2111 } else if (!blk_rq_bytes(req)) { 2112 __blk_mq_end_request(req, BLK_STS_IOERR); 2113 } else if (mqrq->retries++ < MMC_MAX_RETRIES) { 2114 blk_mq_requeue_request(req, true); 2115 } else { 2116 if (mmc_card_removed(mq->card)) 2117 req->rq_flags |= RQF_QUIET; 2118 blk_mq_end_request(req, BLK_STS_IOERR); 2119 } 2120 } 2121 2122 static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq, 2123 struct mmc_queue_req *mqrq) 2124 { 2125 return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) && 2126 (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT || 2127 mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT); 2128 } 2129 2130 static void mmc_blk_urgent_bkops(struct mmc_queue *mq, 2131 struct mmc_queue_req *mqrq) 2132 { 2133 if (mmc_blk_urgent_bkops_needed(mq, mqrq)) 2134 mmc_run_bkops(mq->card); 2135 } 2136 2137 static void mmc_blk_hsq_req_done(struct mmc_request *mrq) 2138 { 2139 struct mmc_queue_req *mqrq = 2140 container_of(mrq, struct mmc_queue_req, brq.mrq); 2141 struct request *req = mmc_queue_req_to_req(mqrq); 2142 struct request_queue *q = req->q; 2143 struct mmc_queue *mq = q->queuedata; 2144 struct mmc_host *host = mq->card->host; 2145 unsigned long flags; 2146 2147 if (mmc_blk_rq_error(&mqrq->brq) || 2148 mmc_blk_urgent_bkops_needed(mq, mqrq)) { 2149 spin_lock_irqsave(&mq->lock, flags); 2150 mq->recovery_needed = true; 2151 mq->recovery_req = req; 2152 spin_unlock_irqrestore(&mq->lock, flags); 2153 2154 host->cqe_ops->cqe_recovery_start(host); 2155 2156 schedule_work(&mq->recovery_work); 2157 return; 2158 } 2159 2160 mmc_blk_rw_reset_success(mq, req); 2161 2162 /* 2163 * Block layer timeouts race with completions which means the normal 2164 * completion path cannot be used during recovery. 2165 */ 2166 if (mq->in_recovery) 2167 mmc_blk_cqe_complete_rq(mq, req); 2168 else if (likely(!blk_should_fake_timeout(req->q))) 2169 blk_mq_complete_request(req); 2170 } 2171 2172 void mmc_blk_mq_complete(struct request *req) 2173 { 2174 struct mmc_queue *mq = req->q->queuedata; 2175 struct mmc_host *host = mq->card->host; 2176 2177 if (host->cqe_enabled) 2178 mmc_blk_cqe_complete_rq(mq, req); 2179 else if (likely(!blk_should_fake_timeout(req->q))) 2180 mmc_blk_mq_complete_rq(mq, req); 2181 } 2182 2183 static void mmc_blk_mq_poll_completion(struct mmc_queue *mq, 2184 struct request *req) 2185 { 2186 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 2187 struct mmc_host *host = mq->card->host; 2188 2189 if (mmc_blk_rq_error(&mqrq->brq) || 2190 mmc_blk_card_busy(mq->card, req)) { 2191 mmc_blk_mq_rw_recovery(mq, req); 2192 } else { 2193 mmc_blk_rw_reset_success(mq, req); 2194 mmc_retune_release(host); 2195 } 2196 2197 mmc_blk_urgent_bkops(mq, mqrq); 2198 } 2199 2200 static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, enum mmc_issue_type issue_type) 2201 { 2202 unsigned long flags; 2203 bool put_card; 2204 2205 spin_lock_irqsave(&mq->lock, flags); 2206 2207 mq->in_flight[issue_type] -= 1; 2208 2209 put_card = (mmc_tot_in_flight(mq) == 0); 2210 2211 spin_unlock_irqrestore(&mq->lock, flags); 2212 2213 if (put_card) 2214 mmc_put_card(mq->card, &mq->ctx); 2215 } 2216 2217 static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req, 2218 bool can_sleep) 2219 { 2220 enum mmc_issue_type issue_type = mmc_issue_type(mq, req); 2221 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 2222 struct mmc_request *mrq = &mqrq->brq.mrq; 2223 struct mmc_host *host = mq->card->host; 2224 2225 mmc_post_req(host, mrq, 0); 2226 2227 /* 2228 * Block layer timeouts race with completions which means the normal 2229 * completion path cannot be used during recovery. 2230 */ 2231 if (mq->in_recovery) { 2232 mmc_blk_mq_complete_rq(mq, req); 2233 } else if (likely(!blk_should_fake_timeout(req->q))) { 2234 if (can_sleep) 2235 blk_mq_complete_request_direct(req, mmc_blk_mq_complete); 2236 else 2237 blk_mq_complete_request(req); 2238 } 2239 2240 mmc_blk_mq_dec_in_flight(mq, issue_type); 2241 } 2242 2243 void mmc_blk_mq_recovery(struct mmc_queue *mq) 2244 { 2245 struct request *req = mq->recovery_req; 2246 struct mmc_host *host = mq->card->host; 2247 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 2248 2249 mq->recovery_req = NULL; 2250 mq->rw_wait = false; 2251 2252 if (mmc_blk_rq_error(&mqrq->brq)) { 2253 mmc_retune_hold_now(host); 2254 mmc_blk_mq_rw_recovery(mq, req); 2255 } 2256 2257 mmc_blk_urgent_bkops(mq, mqrq); 2258 2259 mmc_blk_mq_post_req(mq, req, true); 2260 } 2261 2262 static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq, 2263 struct request **prev_req) 2264 { 2265 if (mmc_host_done_complete(mq->card->host)) 2266 return; 2267 2268 mutex_lock(&mq->complete_lock); 2269 2270 if (!mq->complete_req) 2271 goto out_unlock; 2272 2273 mmc_blk_mq_poll_completion(mq, mq->complete_req); 2274 2275 if (prev_req) 2276 *prev_req = mq->complete_req; 2277 else 2278 mmc_blk_mq_post_req(mq, mq->complete_req, true); 2279 2280 mq->complete_req = NULL; 2281 2282 out_unlock: 2283 mutex_unlock(&mq->complete_lock); 2284 } 2285 2286 void mmc_blk_mq_complete_work(struct work_struct *work) 2287 { 2288 struct mmc_queue *mq = container_of(work, struct mmc_queue, 2289 complete_work); 2290 2291 mmc_blk_mq_complete_prev_req(mq, NULL); 2292 } 2293 2294 static void mmc_blk_mq_req_done(struct mmc_request *mrq) 2295 { 2296 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req, 2297 brq.mrq); 2298 struct request *req = mmc_queue_req_to_req(mqrq); 2299 struct request_queue *q = req->q; 2300 struct mmc_queue *mq = q->queuedata; 2301 struct mmc_host *host = mq->card->host; 2302 unsigned long flags; 2303 2304 if (!mmc_host_done_complete(host)) { 2305 bool waiting; 2306 2307 /* 2308 * We cannot complete the request in this context, so record 2309 * that there is a request to complete, and that a following 2310 * request does not need to wait (although it does need to 2311 * complete complete_req first). 2312 */ 2313 spin_lock_irqsave(&mq->lock, flags); 2314 mq->complete_req = req; 2315 mq->rw_wait = false; 2316 waiting = mq->waiting; 2317 spin_unlock_irqrestore(&mq->lock, flags); 2318 2319 /* 2320 * If 'waiting' then the waiting task will complete this 2321 * request, otherwise queue a work to do it. Note that 2322 * complete_work may still race with the dispatch of a following 2323 * request. 2324 */ 2325 if (waiting) 2326 wake_up(&mq->wait); 2327 else 2328 queue_work(mq->card->complete_wq, &mq->complete_work); 2329 2330 return; 2331 } 2332 2333 /* Take the recovery path for errors or urgent background operations */ 2334 if (mmc_blk_rq_error(&mqrq->brq) || 2335 mmc_blk_urgent_bkops_needed(mq, mqrq)) { 2336 spin_lock_irqsave(&mq->lock, flags); 2337 mq->recovery_needed = true; 2338 mq->recovery_req = req; 2339 spin_unlock_irqrestore(&mq->lock, flags); 2340 wake_up(&mq->wait); 2341 schedule_work(&mq->recovery_work); 2342 return; 2343 } 2344 2345 mmc_blk_rw_reset_success(mq, req); 2346 2347 mq->rw_wait = false; 2348 wake_up(&mq->wait); 2349 2350 /* context unknown */ 2351 mmc_blk_mq_post_req(mq, req, false); 2352 } 2353 2354 static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err) 2355 { 2356 unsigned long flags; 2357 bool done; 2358 2359 /* 2360 * Wait while there is another request in progress, but not if recovery 2361 * is needed. Also indicate whether there is a request waiting to start. 2362 */ 2363 spin_lock_irqsave(&mq->lock, flags); 2364 if (mq->recovery_needed) { 2365 *err = -EBUSY; 2366 done = true; 2367 } else { 2368 done = !mq->rw_wait; 2369 } 2370 mq->waiting = !done; 2371 spin_unlock_irqrestore(&mq->lock, flags); 2372 2373 return done; 2374 } 2375 2376 static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req) 2377 { 2378 int err = 0; 2379 2380 wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err)); 2381 2382 /* Always complete the previous request if there is one */ 2383 mmc_blk_mq_complete_prev_req(mq, prev_req); 2384 2385 return err; 2386 } 2387 2388 static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq, 2389 struct request *req) 2390 { 2391 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 2392 struct mmc_host *host = mq->card->host; 2393 struct request *prev_req = NULL; 2394 int err = 0; 2395 2396 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq); 2397 2398 mqrq->brq.mrq.done = mmc_blk_mq_req_done; 2399 2400 mmc_pre_req(host, &mqrq->brq.mrq); 2401 2402 err = mmc_blk_rw_wait(mq, &prev_req); 2403 if (err) 2404 goto out_post_req; 2405 2406 mq->rw_wait = true; 2407 2408 err = mmc_start_request(host, &mqrq->brq.mrq); 2409 2410 if (prev_req) 2411 mmc_blk_mq_post_req(mq, prev_req, true); 2412 2413 if (err) 2414 mq->rw_wait = false; 2415 2416 /* Release re-tuning here where there is no synchronization required */ 2417 if (err || mmc_host_done_complete(host)) 2418 mmc_retune_release(host); 2419 2420 out_post_req: 2421 if (err) 2422 mmc_post_req(host, &mqrq->brq.mrq, err); 2423 2424 return err; 2425 } 2426 2427 static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host) 2428 { 2429 if (host->cqe_enabled) 2430 return host->cqe_ops->cqe_wait_for_idle(host); 2431 2432 return mmc_blk_rw_wait(mq, NULL); 2433 } 2434 2435 enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req) 2436 { 2437 struct mmc_blk_data *md = mq->blkdata; 2438 struct mmc_card *card = md->queue.card; 2439 struct mmc_host *host = card->host; 2440 int ret; 2441 2442 ret = mmc_blk_part_switch(card, md->part_type); 2443 if (ret) 2444 return MMC_REQ_FAILED_TO_START; 2445 2446 switch (mmc_issue_type(mq, req)) { 2447 case MMC_ISSUE_SYNC: 2448 ret = mmc_blk_wait_for_idle(mq, host); 2449 if (ret) 2450 return MMC_REQ_BUSY; 2451 switch (req_op(req)) { 2452 case REQ_OP_DRV_IN: 2453 case REQ_OP_DRV_OUT: 2454 mmc_blk_issue_drv_op(mq, req); 2455 break; 2456 case REQ_OP_DISCARD: 2457 mmc_blk_issue_discard_rq(mq, req); 2458 break; 2459 case REQ_OP_SECURE_ERASE: 2460 mmc_blk_issue_secdiscard_rq(mq, req); 2461 break; 2462 case REQ_OP_WRITE_ZEROES: 2463 mmc_blk_issue_trim_rq(mq, req); 2464 break; 2465 case REQ_OP_FLUSH: 2466 mmc_blk_issue_flush(mq, req); 2467 break; 2468 default: 2469 WARN_ON_ONCE(1); 2470 return MMC_REQ_FAILED_TO_START; 2471 } 2472 return MMC_REQ_FINISHED; 2473 case MMC_ISSUE_DCMD: 2474 case MMC_ISSUE_ASYNC: 2475 switch (req_op(req)) { 2476 case REQ_OP_FLUSH: 2477 if (!mmc_cache_enabled(host)) { 2478 blk_mq_end_request(req, BLK_STS_OK); 2479 return MMC_REQ_FINISHED; 2480 } 2481 ret = mmc_blk_cqe_issue_flush(mq, req); 2482 break; 2483 case REQ_OP_WRITE: 2484 card->written_flag = true; 2485 fallthrough; 2486 case REQ_OP_READ: 2487 if (host->cqe_enabled) 2488 ret = mmc_blk_cqe_issue_rw_rq(mq, req); 2489 else 2490 ret = mmc_blk_mq_issue_rw_rq(mq, req); 2491 break; 2492 default: 2493 WARN_ON_ONCE(1); 2494 ret = -EINVAL; 2495 } 2496 if (!ret) 2497 return MMC_REQ_STARTED; 2498 return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START; 2499 default: 2500 WARN_ON_ONCE(1); 2501 return MMC_REQ_FAILED_TO_START; 2502 } 2503 } 2504 2505 static inline int mmc_blk_readonly(struct mmc_card *card) 2506 { 2507 return mmc_card_readonly(card) || 2508 !(card->csd.cmdclass & CCC_BLOCK_WRITE); 2509 } 2510 2511 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card, 2512 struct device *parent, 2513 sector_t size, 2514 bool default_ro, 2515 const char *subname, 2516 int area_type, 2517 unsigned int part_type) 2518 { 2519 struct mmc_blk_data *md; 2520 int devidx, ret; 2521 char cap_str[10]; 2522 unsigned int features = 0; 2523 2524 devidx = ida_alloc_max(&mmc_blk_ida, max_devices - 1, GFP_KERNEL); 2525 if (devidx < 0) { 2526 /* 2527 * We get -ENOSPC because there are no more any available 2528 * devidx. The reason may be that, either userspace haven't yet 2529 * unmounted the partitions, which postpones mmc_blk_release() 2530 * from being called, or the device has more partitions than 2531 * what we support. 2532 */ 2533 if (devidx == -ENOSPC) 2534 dev_err(mmc_dev(card->host), 2535 "no more device IDs available\n"); 2536 2537 return ERR_PTR(devidx); 2538 } 2539 2540 md = kzalloc(sizeof(*md), GFP_KERNEL); 2541 if (!md) { 2542 ret = -ENOMEM; 2543 goto out; 2544 } 2545 2546 md->area_type = area_type; 2547 2548 /* 2549 * Set the read-only status based on the supported commands 2550 * and the write protect switch. 2551 */ 2552 md->read_only = mmc_blk_readonly(card); 2553 2554 if (mmc_host_cmd23(card->host)) { 2555 if ((mmc_card_mmc(card) && 2556 card->csd.mmca_vsn >= CSD_SPEC_VER_3) || 2557 (mmc_card_sd(card) && !mmc_card_ult_capacity(card) && 2558 card->scr.cmds & SD_SCR_CMD23_SUPPORT)) 2559 md->flags |= MMC_BLK_CMD23; 2560 } 2561 2562 if (md->flags & MMC_BLK_CMD23 && 2563 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || 2564 card->ext_csd.rel_sectors)) { 2565 md->flags |= MMC_BLK_REL_WR; 2566 features |= (BLK_FEAT_WRITE_CACHE | BLK_FEAT_FUA); 2567 } else if (mmc_cache_enabled(card->host)) { 2568 features |= BLK_FEAT_WRITE_CACHE; 2569 } 2570 2571 md->disk = mmc_init_queue(&md->queue, card, features); 2572 if (IS_ERR(md->disk)) { 2573 ret = PTR_ERR(md->disk); 2574 goto err_kfree; 2575 } 2576 2577 INIT_LIST_HEAD(&md->part); 2578 INIT_LIST_HEAD(&md->rpmbs); 2579 kref_init(&md->kref); 2580 2581 md->queue.blkdata = md; 2582 md->part_type = part_type; 2583 2584 md->disk->major = MMC_BLOCK_MAJOR; 2585 md->disk->minors = perdev_minors; 2586 md->disk->first_minor = devidx * perdev_minors; 2587 md->disk->fops = &mmc_bdops; 2588 md->disk->private_data = md; 2589 md->parent = parent; 2590 set_disk_ro(md->disk, md->read_only || default_ro); 2591 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT)) 2592 md->disk->flags |= GENHD_FL_NO_PART; 2593 2594 /* 2595 * As discussed on lkml, GENHD_FL_REMOVABLE should: 2596 * 2597 * - be set for removable media with permanent block devices 2598 * - be unset for removable block devices with permanent media 2599 * 2600 * Since MMC block devices clearly fall under the second 2601 * case, we do not set GENHD_FL_REMOVABLE. Userspace 2602 * should use the block device creation/destruction hotplug 2603 * messages to tell when the card is present. 2604 */ 2605 2606 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), 2607 "mmcblk%u%s", card->host->index, subname ? subname : ""); 2608 2609 set_capacity(md->disk, size); 2610 2611 string_get_size((u64)size, 512, STRING_UNITS_2, 2612 cap_str, sizeof(cap_str)); 2613 pr_info("%s: %s %s %s%s\n", 2614 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), 2615 cap_str, md->read_only ? " (ro)" : ""); 2616 2617 /* used in ->open, must be set before add_disk: */ 2618 if (area_type == MMC_BLK_DATA_AREA_MAIN) 2619 dev_set_drvdata(&card->dev, md); 2620 ret = device_add_disk(md->parent, md->disk, mmc_disk_attr_groups); 2621 if (ret) 2622 goto err_put_disk; 2623 return md; 2624 2625 err_put_disk: 2626 put_disk(md->disk); 2627 blk_mq_free_tag_set(&md->queue.tag_set); 2628 err_kfree: 2629 kfree(md); 2630 out: 2631 ida_free(&mmc_blk_ida, devidx); 2632 return ERR_PTR(ret); 2633 } 2634 2635 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) 2636 { 2637 sector_t size; 2638 2639 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { 2640 /* 2641 * The EXT_CSD sector count is in number or 512 byte 2642 * sectors. 2643 */ 2644 size = card->ext_csd.sectors; 2645 } else { 2646 /* 2647 * The CSD capacity field is in units of read_blkbits. 2648 * set_capacity takes units of 512 bytes. 2649 */ 2650 size = (typeof(sector_t))card->csd.capacity 2651 << (card->csd.read_blkbits - 9); 2652 } 2653 2654 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL, 2655 MMC_BLK_DATA_AREA_MAIN, 0); 2656 } 2657 2658 static int mmc_blk_alloc_part(struct mmc_card *card, 2659 struct mmc_blk_data *md, 2660 unsigned int part_type, 2661 sector_t size, 2662 bool default_ro, 2663 const char *subname, 2664 int area_type) 2665 { 2666 struct mmc_blk_data *part_md; 2667 2668 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro, 2669 subname, area_type, part_type); 2670 if (IS_ERR(part_md)) 2671 return PTR_ERR(part_md); 2672 list_add(&part_md->part, &md->part); 2673 2674 return 0; 2675 } 2676 2677 /** 2678 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev 2679 * @filp: the character device file 2680 * @cmd: the ioctl() command 2681 * @arg: the argument from userspace 2682 * 2683 * This will essentially just redirect the ioctl()s coming in over to 2684 * the main block device spawning the RPMB character device. 2685 */ 2686 static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd, 2687 unsigned long arg) 2688 { 2689 struct mmc_rpmb_data *rpmb = filp->private_data; 2690 int ret; 2691 2692 switch (cmd) { 2693 case MMC_IOC_CMD: 2694 ret = mmc_blk_ioctl_cmd(rpmb->md, 2695 (struct mmc_ioc_cmd __user *)arg, 2696 rpmb); 2697 break; 2698 case MMC_IOC_MULTI_CMD: 2699 ret = mmc_blk_ioctl_multi_cmd(rpmb->md, 2700 (struct mmc_ioc_multi_cmd __user *)arg, 2701 rpmb); 2702 break; 2703 default: 2704 ret = -EINVAL; 2705 break; 2706 } 2707 2708 return ret; 2709 } 2710 2711 #ifdef CONFIG_COMPAT 2712 static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd, 2713 unsigned long arg) 2714 { 2715 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); 2716 } 2717 #endif 2718 2719 static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp) 2720 { 2721 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev, 2722 struct mmc_rpmb_data, chrdev); 2723 2724 get_device(&rpmb->dev); 2725 filp->private_data = rpmb; 2726 2727 return nonseekable_open(inode, filp); 2728 } 2729 2730 static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp) 2731 { 2732 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev, 2733 struct mmc_rpmb_data, chrdev); 2734 2735 put_device(&rpmb->dev); 2736 2737 return 0; 2738 } 2739 2740 static const struct file_operations mmc_rpmb_fileops = { 2741 .release = mmc_rpmb_chrdev_release, 2742 .open = mmc_rpmb_chrdev_open, 2743 .owner = THIS_MODULE, 2744 .unlocked_ioctl = mmc_rpmb_ioctl, 2745 #ifdef CONFIG_COMPAT 2746 .compat_ioctl = mmc_rpmb_ioctl_compat, 2747 #endif 2748 }; 2749 2750 static void mmc_blk_rpmb_device_release(struct device *dev) 2751 { 2752 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev); 2753 2754 rpmb_dev_unregister(rpmb->rdev); 2755 mmc_blk_put(rpmb->md); 2756 ida_free(&mmc_rpmb_ida, rpmb->id); 2757 kfree(rpmb); 2758 } 2759 2760 static void free_idata(struct mmc_blk_ioc_data **idata, unsigned int cmd_count) 2761 { 2762 unsigned int n; 2763 2764 for (n = 0; n < cmd_count; n++) 2765 kfree(idata[n]); 2766 kfree(idata); 2767 } 2768 2769 static struct mmc_blk_ioc_data **alloc_idata(struct mmc_rpmb_data *rpmb, 2770 unsigned int cmd_count) 2771 { 2772 struct mmc_blk_ioc_data **idata; 2773 unsigned int n; 2774 2775 idata = kcalloc(cmd_count, sizeof(*idata), GFP_KERNEL); 2776 if (!idata) 2777 return NULL; 2778 2779 for (n = 0; n < cmd_count; n++) { 2780 idata[n] = kcalloc(1, sizeof(**idata), GFP_KERNEL); 2781 if (!idata[n]) { 2782 free_idata(idata, n); 2783 return NULL; 2784 } 2785 idata[n]->rpmb = rpmb; 2786 } 2787 2788 return idata; 2789 } 2790 2791 static void set_idata(struct mmc_blk_ioc_data *idata, u32 opcode, 2792 int write_flag, u8 *buf, unsigned int buf_bytes) 2793 { 2794 /* 2795 * The size of an RPMB frame must match what's expected by the 2796 * hardware. 2797 */ 2798 BUILD_BUG_ON(sizeof(struct rpmb_frame) != 512); 2799 2800 idata->ic.opcode = opcode; 2801 idata->ic.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 2802 idata->ic.write_flag = write_flag; 2803 idata->ic.blksz = sizeof(struct rpmb_frame); 2804 idata->ic.blocks = buf_bytes / idata->ic.blksz; 2805 idata->buf = buf; 2806 idata->buf_bytes = buf_bytes; 2807 } 2808 2809 static int mmc_route_rpmb_frames(struct device *dev, u8 *req, 2810 unsigned int req_len, u8 *resp, 2811 unsigned int resp_len) 2812 { 2813 struct rpmb_frame *frm = (struct rpmb_frame *)req; 2814 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev); 2815 struct mmc_blk_data *md = rpmb->md; 2816 struct mmc_blk_ioc_data **idata; 2817 struct mmc_queue_req *mq_rq; 2818 unsigned int cmd_count; 2819 struct request *rq; 2820 u16 req_type; 2821 bool write; 2822 int ret; 2823 2824 if (IS_ERR(md->queue.card)) 2825 return PTR_ERR(md->queue.card); 2826 2827 if (req_len < sizeof(*frm)) 2828 return -EINVAL; 2829 2830 req_type = be16_to_cpu(frm->req_resp); 2831 switch (req_type) { 2832 case RPMB_PROGRAM_KEY: 2833 if (req_len != sizeof(struct rpmb_frame) || 2834 resp_len != sizeof(struct rpmb_frame)) 2835 return -EINVAL; 2836 write = true; 2837 break; 2838 case RPMB_GET_WRITE_COUNTER: 2839 if (req_len != sizeof(struct rpmb_frame) || 2840 resp_len != sizeof(struct rpmb_frame)) 2841 return -EINVAL; 2842 write = false; 2843 break; 2844 case RPMB_WRITE_DATA: 2845 if (req_len % sizeof(struct rpmb_frame) || 2846 resp_len != sizeof(struct rpmb_frame)) 2847 return -EINVAL; 2848 write = true; 2849 break; 2850 case RPMB_READ_DATA: 2851 if (req_len != sizeof(struct rpmb_frame) || 2852 resp_len % sizeof(struct rpmb_frame)) 2853 return -EINVAL; 2854 write = false; 2855 break; 2856 default: 2857 return -EINVAL; 2858 } 2859 2860 if (write) 2861 cmd_count = 3; 2862 else 2863 cmd_count = 2; 2864 2865 idata = alloc_idata(rpmb, cmd_count); 2866 if (!idata) 2867 return -ENOMEM; 2868 2869 if (write) { 2870 struct rpmb_frame *frm = (struct rpmb_frame *)resp; 2871 2872 /* Send write request frame(s) */ 2873 set_idata(idata[0], MMC_WRITE_MULTIPLE_BLOCK, 2874 1 | MMC_CMD23_ARG_REL_WR, req, req_len); 2875 2876 /* Send result request frame */ 2877 memset(frm, 0, sizeof(*frm)); 2878 frm->req_resp = cpu_to_be16(RPMB_RESULT_READ); 2879 set_idata(idata[1], MMC_WRITE_MULTIPLE_BLOCK, 1, resp, 2880 resp_len); 2881 2882 /* Read response frame */ 2883 set_idata(idata[2], MMC_READ_MULTIPLE_BLOCK, 0, resp, resp_len); 2884 } else { 2885 /* Send write request frame(s) */ 2886 set_idata(idata[0], MMC_WRITE_MULTIPLE_BLOCK, 1, req, req_len); 2887 2888 /* Read response frame */ 2889 set_idata(idata[1], MMC_READ_MULTIPLE_BLOCK, 0, resp, resp_len); 2890 } 2891 2892 rq = blk_mq_alloc_request(md->queue.queue, REQ_OP_DRV_OUT, 0); 2893 if (IS_ERR(rq)) { 2894 ret = PTR_ERR(rq); 2895 goto out; 2896 } 2897 2898 mq_rq = req_to_mmc_queue_req(rq); 2899 mq_rq->drv_op = MMC_DRV_OP_IOCTL_RPMB; 2900 mq_rq->drv_op_result = -EIO; 2901 mq_rq->drv_op_data = idata; 2902 mq_rq->ioc_count = cmd_count; 2903 blk_execute_rq(rq, false); 2904 ret = req_to_mmc_queue_req(rq)->drv_op_result; 2905 2906 blk_mq_free_request(rq); 2907 2908 out: 2909 free_idata(idata, cmd_count); 2910 return ret; 2911 } 2912 2913 static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, 2914 struct mmc_blk_data *md, 2915 unsigned int part_index, 2916 sector_t size, 2917 const char *subname) 2918 { 2919 int devidx, ret; 2920 char rpmb_name[DISK_NAME_LEN]; 2921 char cap_str[10]; 2922 struct mmc_rpmb_data *rpmb; 2923 2924 /* This creates the minor number for the RPMB char device */ 2925 devidx = ida_alloc_max(&mmc_rpmb_ida, max_devices - 1, GFP_KERNEL); 2926 if (devidx < 0) 2927 return devidx; 2928 2929 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL); 2930 if (!rpmb) { 2931 ida_free(&mmc_rpmb_ida, devidx); 2932 return -ENOMEM; 2933 } 2934 2935 snprintf(rpmb_name, sizeof(rpmb_name), 2936 "mmcblk%u%s", card->host->index, subname ? subname : ""); 2937 2938 rpmb->id = devidx; 2939 rpmb->part_index = part_index; 2940 rpmb->dev.init_name = rpmb_name; 2941 rpmb->dev.bus = &mmc_rpmb_bus_type; 2942 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id); 2943 rpmb->dev.parent = &card->dev; 2944 rpmb->dev.release = mmc_blk_rpmb_device_release; 2945 device_initialize(&rpmb->dev); 2946 dev_set_drvdata(&rpmb->dev, rpmb); 2947 mmc_blk_get(md->disk); 2948 rpmb->md = md; 2949 2950 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops); 2951 rpmb->chrdev.owner = THIS_MODULE; 2952 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev); 2953 if (ret) { 2954 pr_err("%s: could not add character device\n", rpmb_name); 2955 goto out_put_device; 2956 } 2957 2958 list_add(&rpmb->node, &md->rpmbs); 2959 2960 string_get_size((u64)size, 512, STRING_UNITS_2, 2961 cap_str, sizeof(cap_str)); 2962 2963 pr_info("%s: %s %s %s, chardev (%d:%d)\n", 2964 rpmb_name, mmc_card_id(card), mmc_card_name(card), cap_str, 2965 MAJOR(mmc_rpmb_devt), rpmb->id); 2966 2967 return 0; 2968 2969 out_put_device: 2970 put_device(&rpmb->dev); 2971 return ret; 2972 } 2973 2974 static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb) 2975 2976 { 2977 cdev_device_del(&rpmb->chrdev, &rpmb->dev); 2978 put_device(&rpmb->dev); 2979 } 2980 2981 /* MMC Physical partitions consist of two boot partitions and 2982 * up to four general purpose partitions. 2983 * For each partition enabled in EXT_CSD a block device will be allocatedi 2984 * to provide access to the partition. 2985 */ 2986 2987 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md) 2988 { 2989 int idx, ret; 2990 2991 if (!mmc_card_mmc(card)) 2992 return 0; 2993 2994 for (idx = 0; idx < card->nr_parts; idx++) { 2995 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) { 2996 /* 2997 * RPMB partitions does not provide block access, they 2998 * are only accessed using ioctl():s. Thus create 2999 * special RPMB block devices that do not have a 3000 * backing block queue for these. 3001 */ 3002 ret = mmc_blk_alloc_rpmb_part(card, md, 3003 card->part[idx].part_cfg, 3004 card->part[idx].size >> 9, 3005 card->part[idx].name); 3006 if (ret) 3007 return ret; 3008 } else if (card->part[idx].size) { 3009 ret = mmc_blk_alloc_part(card, md, 3010 card->part[idx].part_cfg, 3011 card->part[idx].size >> 9, 3012 card->part[idx].force_ro, 3013 card->part[idx].name, 3014 card->part[idx].area_type); 3015 if (ret) 3016 return ret; 3017 } 3018 } 3019 3020 return 0; 3021 } 3022 3023 static void mmc_blk_remove_req(struct mmc_blk_data *md) 3024 { 3025 /* 3026 * Flush remaining requests and free queues. It is freeing the queue 3027 * that stops new requests from being accepted. 3028 */ 3029 del_gendisk(md->disk); 3030 mmc_cleanup_queue(&md->queue); 3031 mmc_blk_put(md); 3032 } 3033 3034 static void mmc_blk_remove_parts(struct mmc_card *card, 3035 struct mmc_blk_data *md) 3036 { 3037 struct list_head *pos, *q; 3038 struct mmc_blk_data *part_md; 3039 struct mmc_rpmb_data *rpmb; 3040 3041 /* Remove RPMB partitions */ 3042 list_for_each_safe(pos, q, &md->rpmbs) { 3043 rpmb = list_entry(pos, struct mmc_rpmb_data, node); 3044 list_del(pos); 3045 mmc_blk_remove_rpmb_part(rpmb); 3046 } 3047 /* Remove block partitions */ 3048 list_for_each_safe(pos, q, &md->part) { 3049 part_md = list_entry(pos, struct mmc_blk_data, part); 3050 list_del(pos); 3051 mmc_blk_remove_req(part_md); 3052 } 3053 } 3054 3055 #ifdef CONFIG_DEBUG_FS 3056 3057 static int mmc_dbg_card_status_get(void *data, u64 *val) 3058 { 3059 struct mmc_card *card = data; 3060 struct mmc_blk_data *md = dev_get_drvdata(&card->dev); 3061 struct mmc_queue *mq = &md->queue; 3062 struct request *req; 3063 int ret; 3064 3065 /* Ask the block layer about the card status */ 3066 req = blk_mq_alloc_request(mq->queue, REQ_OP_DRV_IN, 0); 3067 if (IS_ERR(req)) 3068 return PTR_ERR(req); 3069 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS; 3070 req_to_mmc_queue_req(req)->drv_op_result = -EIO; 3071 blk_execute_rq(req, false); 3072 ret = req_to_mmc_queue_req(req)->drv_op_result; 3073 if (ret >= 0) { 3074 *val = ret; 3075 ret = 0; 3076 } 3077 blk_mq_free_request(req); 3078 3079 return ret; 3080 } 3081 DEFINE_DEBUGFS_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get, 3082 NULL, "%08llx\n"); 3083 3084 /* That is two digits * 512 + 1 for newline */ 3085 #define EXT_CSD_STR_LEN 1025 3086 3087 static int mmc_ext_csd_open(struct inode *inode, struct file *filp) 3088 { 3089 struct mmc_card *card = inode->i_private; 3090 struct mmc_blk_data *md = dev_get_drvdata(&card->dev); 3091 struct mmc_queue *mq = &md->queue; 3092 struct request *req; 3093 char *buf; 3094 ssize_t n = 0; 3095 u8 *ext_csd; 3096 int err, i; 3097 3098 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL); 3099 if (!buf) 3100 return -ENOMEM; 3101 3102 /* Ask the block layer for the EXT CSD */ 3103 req = blk_mq_alloc_request(mq->queue, REQ_OP_DRV_IN, 0); 3104 if (IS_ERR(req)) { 3105 err = PTR_ERR(req); 3106 goto out_free; 3107 } 3108 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD; 3109 req_to_mmc_queue_req(req)->drv_op_result = -EIO; 3110 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd; 3111 blk_execute_rq(req, false); 3112 err = req_to_mmc_queue_req(req)->drv_op_result; 3113 blk_mq_free_request(req); 3114 if (err) { 3115 pr_err("FAILED %d\n", err); 3116 goto out_free; 3117 } 3118 3119 for (i = 0; i < 512; i++) 3120 n += sprintf(buf + n, "%02x", ext_csd[i]); 3121 n += sprintf(buf + n, "\n"); 3122 3123 if (n != EXT_CSD_STR_LEN) { 3124 err = -EINVAL; 3125 kfree(ext_csd); 3126 goto out_free; 3127 } 3128 3129 filp->private_data = buf; 3130 kfree(ext_csd); 3131 return 0; 3132 3133 out_free: 3134 kfree(buf); 3135 return err; 3136 } 3137 3138 static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf, 3139 size_t cnt, loff_t *ppos) 3140 { 3141 char *buf = filp->private_data; 3142 3143 return simple_read_from_buffer(ubuf, cnt, ppos, 3144 buf, EXT_CSD_STR_LEN); 3145 } 3146 3147 static int mmc_ext_csd_release(struct inode *inode, struct file *file) 3148 { 3149 kfree(file->private_data); 3150 return 0; 3151 } 3152 3153 static const struct file_operations mmc_dbg_ext_csd_fops = { 3154 .open = mmc_ext_csd_open, 3155 .read = mmc_ext_csd_read, 3156 .release = mmc_ext_csd_release, 3157 .llseek = default_llseek, 3158 }; 3159 3160 static void mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md) 3161 { 3162 struct dentry *root; 3163 3164 if (!card->debugfs_root) 3165 return; 3166 3167 root = card->debugfs_root; 3168 3169 if (mmc_card_mmc(card) || mmc_card_sd(card)) { 3170 md->status_dentry = 3171 debugfs_create_file_unsafe("status", 0400, root, 3172 card, 3173 &mmc_dbg_card_status_fops); 3174 } 3175 3176 if (mmc_card_mmc(card)) { 3177 md->ext_csd_dentry = 3178 debugfs_create_file("ext_csd", S_IRUSR, root, card, 3179 &mmc_dbg_ext_csd_fops); 3180 } 3181 } 3182 3183 static void mmc_blk_remove_debugfs(struct mmc_card *card, 3184 struct mmc_blk_data *md) 3185 { 3186 if (!card->debugfs_root) 3187 return; 3188 3189 debugfs_remove(md->status_dentry); 3190 md->status_dentry = NULL; 3191 3192 debugfs_remove(md->ext_csd_dentry); 3193 md->ext_csd_dentry = NULL; 3194 } 3195 3196 #else 3197 3198 static void mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md) 3199 { 3200 } 3201 3202 static void mmc_blk_remove_debugfs(struct mmc_card *card, 3203 struct mmc_blk_data *md) 3204 { 3205 } 3206 3207 #endif /* CONFIG_DEBUG_FS */ 3208 3209 static void mmc_blk_rpmb_add(struct mmc_card *card) 3210 { 3211 struct mmc_blk_data *md = dev_get_drvdata(&card->dev); 3212 struct mmc_rpmb_data *rpmb; 3213 struct rpmb_dev *rdev; 3214 unsigned int n; 3215 u32 cid[4]; 3216 struct rpmb_descr descr = { 3217 .type = RPMB_TYPE_EMMC, 3218 .route_frames = mmc_route_rpmb_frames, 3219 .reliable_wr_count = card->ext_csd.enhanced_rpmb_supported ? 3220 2 : 32, 3221 .capacity = card->ext_csd.raw_rpmb_size_mult, 3222 .dev_id = (void *)cid, 3223 .dev_id_len = sizeof(cid), 3224 }; 3225 3226 /* 3227 * Provice CID as an octet array. The CID needs to be interpreted 3228 * when used as input to derive the RPMB key since some fields 3229 * will change due to firmware updates. 3230 */ 3231 for (n = 0; n < 4; n++) 3232 cid[n] = be32_to_cpu((__force __be32)card->raw_cid[n]); 3233 3234 list_for_each_entry(rpmb, &md->rpmbs, node) { 3235 rdev = rpmb_dev_register(&rpmb->dev, &descr); 3236 if (IS_ERR(rdev)) { 3237 pr_warn("%s: could not register RPMB device\n", 3238 dev_name(&rpmb->dev)); 3239 continue; 3240 } 3241 rpmb->rdev = rdev; 3242 } 3243 } 3244 3245 static int mmc_blk_probe(struct mmc_card *card) 3246 { 3247 struct mmc_blk_data *md; 3248 int ret = 0; 3249 3250 /* 3251 * Check that the card supports the command class(es) we need. 3252 */ 3253 if (!(card->csd.cmdclass & CCC_BLOCK_READ)) 3254 return -ENODEV; 3255 3256 mmc_fixup_device(card, mmc_blk_fixups); 3257 3258 card->complete_wq = alloc_workqueue("mmc_complete", 3259 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0); 3260 if (!card->complete_wq) { 3261 pr_err("Failed to create mmc completion workqueue"); 3262 return -ENOMEM; 3263 } 3264 3265 md = mmc_blk_alloc(card); 3266 if (IS_ERR(md)) { 3267 ret = PTR_ERR(md); 3268 goto out_free; 3269 } 3270 3271 ret = mmc_blk_alloc_parts(card, md); 3272 if (ret) 3273 goto out; 3274 3275 /* Add two debugfs entries */ 3276 mmc_blk_add_debugfs(card, md); 3277 3278 pm_runtime_set_autosuspend_delay(&card->dev, 3000); 3279 pm_runtime_use_autosuspend(&card->dev); 3280 3281 /* 3282 * Don't enable runtime PM for SD-combo cards here. Leave that 3283 * decision to be taken during the SDIO init sequence instead. 3284 */ 3285 if (!mmc_card_sd_combo(card)) { 3286 pm_runtime_set_active(&card->dev); 3287 pm_runtime_enable(&card->dev); 3288 } 3289 3290 mmc_blk_rpmb_add(card); 3291 3292 return 0; 3293 3294 out: 3295 mmc_blk_remove_parts(card, md); 3296 mmc_blk_remove_req(md); 3297 out_free: 3298 destroy_workqueue(card->complete_wq); 3299 return ret; 3300 } 3301 3302 static void mmc_blk_remove(struct mmc_card *card) 3303 { 3304 struct mmc_blk_data *md = dev_get_drvdata(&card->dev); 3305 3306 mmc_blk_remove_debugfs(card, md); 3307 mmc_blk_remove_parts(card, md); 3308 pm_runtime_get_sync(&card->dev); 3309 if (md->part_curr != md->part_type) { 3310 mmc_claim_host(card->host); 3311 mmc_blk_part_switch(card, md->part_type); 3312 mmc_release_host(card->host); 3313 } 3314 if (!mmc_card_sd_combo(card)) 3315 pm_runtime_disable(&card->dev); 3316 pm_runtime_put_noidle(&card->dev); 3317 mmc_blk_remove_req(md); 3318 destroy_workqueue(card->complete_wq); 3319 } 3320 3321 static int _mmc_blk_suspend(struct mmc_card *card) 3322 { 3323 struct mmc_blk_data *part_md; 3324 struct mmc_blk_data *md = dev_get_drvdata(&card->dev); 3325 3326 if (md) { 3327 mmc_queue_suspend(&md->queue); 3328 list_for_each_entry(part_md, &md->part, part) { 3329 mmc_queue_suspend(&part_md->queue); 3330 } 3331 } 3332 return 0; 3333 } 3334 3335 static void mmc_blk_shutdown(struct mmc_card *card) 3336 { 3337 _mmc_blk_suspend(card); 3338 } 3339 3340 #ifdef CONFIG_PM_SLEEP 3341 static int mmc_blk_suspend(struct device *dev) 3342 { 3343 struct mmc_card *card = mmc_dev_to_card(dev); 3344 3345 return _mmc_blk_suspend(card); 3346 } 3347 3348 static int mmc_blk_resume(struct device *dev) 3349 { 3350 struct mmc_blk_data *part_md; 3351 struct mmc_blk_data *md = dev_get_drvdata(dev); 3352 3353 if (md) { 3354 /* 3355 * Resume involves the card going into idle state, 3356 * so current partition is always the main one. 3357 */ 3358 md->part_curr = md->part_type; 3359 mmc_queue_resume(&md->queue); 3360 list_for_each_entry(part_md, &md->part, part) { 3361 mmc_queue_resume(&part_md->queue); 3362 } 3363 } 3364 return 0; 3365 } 3366 #endif 3367 3368 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume); 3369 3370 static struct mmc_driver mmc_driver = { 3371 .drv = { 3372 .name = "mmcblk", 3373 .pm = &mmc_blk_pm_ops, 3374 }, 3375 .probe = mmc_blk_probe, 3376 .remove = mmc_blk_remove, 3377 .shutdown = mmc_blk_shutdown, 3378 }; 3379 3380 static int __init mmc_blk_init(void) 3381 { 3382 int res; 3383 3384 res = bus_register(&mmc_rpmb_bus_type); 3385 if (res < 0) { 3386 pr_err("mmcblk: could not register RPMB bus type\n"); 3387 return res; 3388 } 3389 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb"); 3390 if (res < 0) { 3391 pr_err("mmcblk: failed to allocate rpmb chrdev region\n"); 3392 goto out_bus_unreg; 3393 } 3394 3395 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) 3396 pr_info("mmcblk: using %d minors per device\n", perdev_minors); 3397 3398 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors); 3399 3400 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); 3401 if (res) 3402 goto out_chrdev_unreg; 3403 3404 res = mmc_register_driver(&mmc_driver); 3405 if (res) 3406 goto out_blkdev_unreg; 3407 3408 return 0; 3409 3410 out_blkdev_unreg: 3411 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); 3412 out_chrdev_unreg: 3413 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES); 3414 out_bus_unreg: 3415 bus_unregister(&mmc_rpmb_bus_type); 3416 return res; 3417 } 3418 3419 static void __exit mmc_blk_exit(void) 3420 { 3421 mmc_unregister_driver(&mmc_driver); 3422 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); 3423 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES); 3424 bus_unregister(&mmc_rpmb_bus_type); 3425 } 3426 3427 module_init(mmc_blk_init); 3428 module_exit(mmc_blk_exit); 3429 3430 MODULE_LICENSE("GPL"); 3431 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); 3432