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