1 // SPDX-License-Identifier: GPL-2.0
3 * Block driver for media (i.e., flash cards)
5 * Copyright 2002 Hewlett-Packard Company
6 * Copyright 2005-2008 Pierre Ossman
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.
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.
16 * Many thanks to Alessandro Rubini and Jonathan Corbet!
18 * Author: Andrew Christian
21 #include <linux/moduleparam.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
25 #include <linux/kernel.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>
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>
50 #include <linux/uaccess.h>
63 MODULE_ALIAS("mmc:block");
64 #ifdef MODULE_PARAM_PREFIX
65 #undef MODULE_PARAM_PREFIX
67 #define MODULE_PARAM_PREFIX "mmcblk."
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
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)
79 static DEFINE_MUTEX(block_mutex);
82 * The defaults come from config options but can be overriden by module
85 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
88 * We've only got one major, so number of mmcblk devices is
89 * limited to (1 << 20) / number of minors per device. It is also
90 * limited by the MAX_DEVICES below.
92 static int max_devices;
94 #define MAX_DEVICES 256
96 static DEFINE_IDA(mmc_blk_ida);
97 static DEFINE_IDA(mmc_rpmb_ida);
99 struct mmc_blk_busy_data {
100 struct mmc_card *card;
105 * There is one mmc_blk_data per slot.
107 struct mmc_blk_data {
108 struct device *parent;
109 struct gendisk *disk;
110 struct mmc_queue queue;
111 struct list_head part;
112 struct list_head rpmbs;
115 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
116 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
119 unsigned int read_only;
120 unsigned int part_type;
121 unsigned int reset_done;
122 #define MMC_BLK_READ BIT(0)
123 #define MMC_BLK_WRITE BIT(1)
124 #define MMC_BLK_DISCARD BIT(2)
125 #define MMC_BLK_SECDISCARD BIT(3)
126 #define MMC_BLK_CQE_RECOVERY BIT(4)
127 #define MMC_BLK_TRIM BIT(5)
130 * Only set in main mmc_blk_data associated
131 * with mmc_card with dev_set_drvdata, and keeps
132 * track of the current selected device partition.
134 unsigned int part_curr;
135 #define MMC_BLK_PART_INVALID UINT_MAX /* Unknown partition active */
138 /* debugfs files (only in main mmc_blk_data) */
139 struct dentry *status_dentry;
140 struct dentry *ext_csd_dentry;
143 /* Device type for RPMB character devices */
144 static dev_t mmc_rpmb_devt;
146 /* Bus type for RPMB character devices */
147 static struct bus_type mmc_rpmb_bus_type = {
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
160 struct mmc_rpmb_data {
164 unsigned int part_index;
165 struct mmc_blk_data *md;
166 struct list_head node;
169 static DEFINE_MUTEX(open_lock);
171 module_param(perdev_minors, int, 0444);
172 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
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,
179 struct mmc_queue *mq);
180 static void mmc_blk_hsq_req_done(struct mmc_request *mrq);
182 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
184 struct mmc_blk_data *md;
186 mutex_lock(&open_lock);
187 md = disk->private_data;
188 if (md && !kref_get_unless_zero(&md->kref))
190 mutex_unlock(&open_lock);
195 static inline int mmc_get_devidx(struct gendisk *disk)
197 int devidx = disk->first_minor / perdev_minors;
201 static void mmc_blk_kref_release(struct kref *ref)
203 struct mmc_blk_data *md = container_of(ref, struct mmc_blk_data, kref);
206 devidx = mmc_get_devidx(md->disk);
207 ida_simple_remove(&mmc_blk_ida, devidx);
209 mutex_lock(&open_lock);
210 md->disk->private_data = NULL;
211 mutex_unlock(&open_lock);
217 static void mmc_blk_put(struct mmc_blk_data *md)
219 kref_put(&md->kref, mmc_blk_kref_release);
222 static ssize_t power_ro_lock_show(struct device *dev,
223 struct device_attribute *attr, char *buf)
226 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
227 struct mmc_card *card = md->queue.card;
230 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
232 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
235 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
242 static ssize_t power_ro_lock_store(struct device *dev,
243 struct device_attribute *attr, const char *buf, size_t count)
246 struct mmc_blk_data *md, *part_md;
247 struct mmc_queue *mq;
251 if (kstrtoul(buf, 0, &set))
257 md = mmc_blk_get(dev_to_disk(dev));
260 /* Dispatch locking to the block layer */
261 req = blk_mq_alloc_request(mq->queue, REQ_OP_DRV_OUT, 0);
263 count = PTR_ERR(req);
266 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
267 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
268 blk_execute_rq(req, false);
269 ret = req_to_mmc_queue_req(req)->drv_op_result;
270 blk_mq_free_request(req);
273 pr_info("%s: Locking boot partition ro until next power on\n",
274 md->disk->disk_name);
275 set_disk_ro(md->disk, 1);
277 list_for_each_entry(part_md, &md->part, part)
278 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
279 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
280 set_disk_ro(part_md->disk, 1);
288 static DEVICE_ATTR(ro_lock_until_next_power_on, 0,
289 power_ro_lock_show, power_ro_lock_store);
291 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
295 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
297 ret = snprintf(buf, PAGE_SIZE, "%d\n",
298 get_disk_ro(dev_to_disk(dev)) ^
304 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
305 const char *buf, size_t count)
309 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
310 unsigned long set = simple_strtoul(buf, &end, 0);
316 set_disk_ro(dev_to_disk(dev), set || md->read_only);
323 static DEVICE_ATTR(force_ro, 0644, force_ro_show, force_ro_store);
325 static struct attribute *mmc_disk_attrs[] = {
326 &dev_attr_force_ro.attr,
327 &dev_attr_ro_lock_until_next_power_on.attr,
331 static umode_t mmc_disk_attrs_is_visible(struct kobject *kobj,
332 struct attribute *a, int n)
334 struct device *dev = kobj_to_dev(kobj);
335 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
336 umode_t mode = a->mode;
338 if (a == &dev_attr_ro_lock_until_next_power_on.attr &&
339 (md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
340 md->queue.card->ext_csd.boot_ro_lockable) {
342 if (!(md->queue.card->ext_csd.boot_ro_lock &
343 EXT_CSD_BOOT_WP_B_PWR_WP_DIS))
351 static const struct attribute_group mmc_disk_attr_group = {
352 .is_visible = mmc_disk_attrs_is_visible,
353 .attrs = mmc_disk_attrs,
356 static const struct attribute_group *mmc_disk_attr_groups[] = {
357 &mmc_disk_attr_group,
361 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
363 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
366 mutex_lock(&block_mutex);
369 if ((mode & FMODE_WRITE) && md->read_only) {
374 mutex_unlock(&block_mutex);
379 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
381 struct mmc_blk_data *md = disk->private_data;
383 mutex_lock(&block_mutex);
385 mutex_unlock(&block_mutex);
389 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
391 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
397 struct mmc_blk_ioc_data {
398 struct mmc_ioc_cmd ic;
401 struct mmc_rpmb_data *rpmb;
404 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
405 struct mmc_ioc_cmd __user *user)
407 struct mmc_blk_ioc_data *idata;
410 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
416 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
421 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
422 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
427 if (!idata->buf_bytes) {
432 idata->buf = memdup_user((void __user *)(unsigned long)
433 idata->ic.data_ptr, idata->buf_bytes);
434 if (IS_ERR(idata->buf)) {
435 err = PTR_ERR(idata->buf);
447 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
448 struct mmc_blk_ioc_data *idata)
450 struct mmc_ioc_cmd *ic = &idata->ic;
452 if (copy_to_user(&(ic_ptr->response), ic->response,
453 sizeof(ic->response)))
456 if (!idata->ic.write_flag) {
457 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
458 idata->buf, idata->buf_bytes))
465 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
466 struct mmc_blk_ioc_data *idata)
468 struct mmc_command cmd = {}, sbc = {};
469 struct mmc_data data = {};
470 struct mmc_request mrq = {};
471 struct scatterlist sg;
472 bool r1b_resp, use_r1b_resp = false;
473 unsigned int busy_timeout_ms;
475 unsigned int target_part;
477 if (!card || !md || !idata)
481 * The RPMB accesses comes in from the character device, so we
482 * need to target these explicitly. Else we just target the
483 * partition type for the block device the ioctl() was issued
487 /* Support multiple RPMB partitions */
488 target_part = idata->rpmb->part_index;
489 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
491 target_part = md->part_type;
494 cmd.opcode = idata->ic.opcode;
495 cmd.arg = idata->ic.arg;
496 cmd.flags = idata->ic.flags;
498 if (idata->buf_bytes) {
501 data.blksz = idata->ic.blksz;
502 data.blocks = idata->ic.blocks;
504 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
506 if (idata->ic.write_flag)
507 data.flags = MMC_DATA_WRITE;
509 data.flags = MMC_DATA_READ;
511 /* data.flags must already be set before doing this. */
512 mmc_set_data_timeout(&data, card);
514 /* Allow overriding the timeout_ns for empirical tuning. */
515 if (idata->ic.data_timeout_ns)
516 data.timeout_ns = idata->ic.data_timeout_ns;
523 err = mmc_blk_part_switch(card, target_part);
527 if (idata->ic.is_acmd) {
528 err = mmc_app_cmd(card->host, card);
534 sbc.opcode = MMC_SET_BLOCK_COUNT;
536 * We don't do any blockcount validation because the max size
537 * may be increased by a future standard. We just copy the
538 * 'Reliable Write' bit here.
540 sbc.arg = data.blocks | (idata->ic.write_flag & BIT(31));
541 sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
545 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
546 (cmd.opcode == MMC_SWITCH))
547 return mmc_sanitize(card, idata->ic.cmd_timeout_ms);
549 /* If it's an R1B response we need some more preparations. */
550 busy_timeout_ms = idata->ic.cmd_timeout_ms ? : MMC_BLK_TIMEOUT_MS;
551 r1b_resp = (cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B;
553 use_r1b_resp = mmc_prepare_busy_cmd(card->host, &cmd,
556 mmc_wait_for_req(card->host, &mrq);
557 memcpy(&idata->ic.response, cmd.resp, sizeof(cmd.resp));
560 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
561 __func__, cmd.error);
565 dev_err(mmc_dev(card->host), "%s: data error %d\n",
566 __func__, data.error);
571 * Make sure the cache of the PARTITION_CONFIG register and
572 * PARTITION_ACCESS bits is updated in case the ioctl ext_csd write
573 * changed it successfully.
575 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_PART_CONFIG) &&
576 (cmd.opcode == MMC_SWITCH)) {
577 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
578 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg);
581 * Update cache so the next mmc_blk_part_switch call operates
582 * on up-to-date data.
584 card->ext_csd.part_config = value;
585 main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
589 * Make sure to update CACHE_CTRL in case it was changed. The cache
590 * will get turned back on if the card is re-initialized, e.g.
591 * suspend/resume or hw reset in recovery.
593 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_CACHE_CTRL) &&
594 (cmd.opcode == MMC_SWITCH)) {
595 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg) & 1;
597 card->ext_csd.cache_ctrl = value;
601 * According to the SD specs, some commands require a delay after
602 * issuing the command.
604 if (idata->ic.postsleep_min_us)
605 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
607 /* No need to poll when using HW busy detection. */
608 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
611 /* Ensure RPMB/R1B command has completed by polling with CMD13. */
612 if (idata->rpmb || r1b_resp)
613 err = mmc_poll_for_busy(card, busy_timeout_ms, false,
619 static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
620 struct mmc_ioc_cmd __user *ic_ptr,
621 struct mmc_rpmb_data *rpmb)
623 struct mmc_blk_ioc_data *idata;
624 struct mmc_blk_ioc_data *idatas[1];
625 struct mmc_queue *mq;
626 struct mmc_card *card;
627 int err = 0, ioc_err = 0;
630 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
632 return PTR_ERR(idata);
633 /* This will be NULL on non-RPMB ioctl():s */
636 card = md->queue.card;
643 * Dispatch the ioctl() into the block request queue.
646 req = blk_mq_alloc_request(mq->queue,
647 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
653 req_to_mmc_queue_req(req)->drv_op =
654 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
655 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
656 req_to_mmc_queue_req(req)->drv_op_data = idatas;
657 req_to_mmc_queue_req(req)->ioc_count = 1;
658 blk_execute_rq(req, false);
659 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
660 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
661 blk_mq_free_request(req);
666 return ioc_err ? ioc_err : err;
669 static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
670 struct mmc_ioc_multi_cmd __user *user,
671 struct mmc_rpmb_data *rpmb)
673 struct mmc_blk_ioc_data **idata = NULL;
674 struct mmc_ioc_cmd __user *cmds = user->cmds;
675 struct mmc_card *card;
676 struct mmc_queue *mq;
677 int err = 0, ioc_err = 0;
682 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
683 sizeof(num_of_cmds)))
689 if (num_of_cmds > MMC_IOC_MAX_CMDS)
693 idata = kcalloc(n, sizeof(*idata), GFP_KERNEL);
697 for (i = 0; i < n; i++) {
698 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
699 if (IS_ERR(idata[i])) {
700 err = PTR_ERR(idata[i]);
704 /* This will be NULL on non-RPMB ioctl():s */
705 idata[i]->rpmb = rpmb;
708 card = md->queue.card;
716 * Dispatch the ioctl()s into the block request queue.
719 req = blk_mq_alloc_request(mq->queue,
720 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
725 req_to_mmc_queue_req(req)->drv_op =
726 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
727 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
728 req_to_mmc_queue_req(req)->drv_op_data = idata;
729 req_to_mmc_queue_req(req)->ioc_count = n;
730 blk_execute_rq(req, false);
731 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
733 /* copy to user if data and response */
734 for (i = 0; i < n && !err; i++)
735 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
737 blk_mq_free_request(req);
740 for (i = 0; i < n; i++) {
741 kfree(idata[i]->buf);
745 return ioc_err ? ioc_err : err;
748 static int mmc_blk_check_blkdev(struct block_device *bdev)
751 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
752 * whole block device, not on a partition. This prevents overspray
753 * between sibling partitions.
755 if (!capable(CAP_SYS_RAWIO) || bdev_is_partition(bdev))
760 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
761 unsigned int cmd, unsigned long arg)
763 struct mmc_blk_data *md;
768 ret = mmc_blk_check_blkdev(bdev);
771 md = mmc_blk_get(bdev->bd_disk);
774 ret = mmc_blk_ioctl_cmd(md,
775 (struct mmc_ioc_cmd __user *)arg,
779 case MMC_IOC_MULTI_CMD:
780 ret = mmc_blk_check_blkdev(bdev);
783 md = mmc_blk_get(bdev->bd_disk);
786 ret = mmc_blk_ioctl_multi_cmd(md,
787 (struct mmc_ioc_multi_cmd __user *)arg,
797 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
798 unsigned int cmd, unsigned long arg)
800 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
804 static int mmc_blk_alternative_gpt_sector(struct gendisk *disk,
807 struct mmc_blk_data *md;
810 md = mmc_blk_get(disk);
815 ret = mmc_card_alternative_gpt_sector(md->queue.card, sector);
824 static const struct block_device_operations mmc_bdops = {
825 .open = mmc_blk_open,
826 .release = mmc_blk_release,
827 .getgeo = mmc_blk_getgeo,
828 .owner = THIS_MODULE,
829 .ioctl = mmc_blk_ioctl,
831 .compat_ioctl = mmc_blk_compat_ioctl,
833 .alternative_gpt_sector = mmc_blk_alternative_gpt_sector,
836 static int mmc_blk_part_switch_pre(struct mmc_card *card,
837 unsigned int part_type)
841 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
842 if (card->ext_csd.cmdq_en) {
843 ret = mmc_cmdq_disable(card);
847 mmc_retune_pause(card->host);
853 static int mmc_blk_part_switch_post(struct mmc_card *card,
854 unsigned int part_type)
858 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
859 mmc_retune_unpause(card->host);
860 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
861 ret = mmc_cmdq_enable(card);
867 static inline int mmc_blk_part_switch(struct mmc_card *card,
868 unsigned int part_type)
871 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
873 if (main_md->part_curr == part_type)
876 if (mmc_card_mmc(card)) {
877 u8 part_config = card->ext_csd.part_config;
879 ret = mmc_blk_part_switch_pre(card, part_type);
883 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
884 part_config |= part_type;
886 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
887 EXT_CSD_PART_CONFIG, part_config,
888 card->ext_csd.part_time);
890 mmc_blk_part_switch_post(card, part_type);
894 card->ext_csd.part_config = part_config;
896 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
899 main_md->part_curr = part_type;
903 static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
909 struct mmc_request mrq = {};
910 struct mmc_command cmd = {};
911 struct mmc_data data = {};
913 struct scatterlist sg;
915 cmd.opcode = MMC_APP_CMD;
916 cmd.arg = card->rca << 16;
917 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
919 err = mmc_wait_for_cmd(card->host, &cmd, 0);
922 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
925 memset(&cmd, 0, sizeof(struct mmc_command));
927 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
929 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
933 data.flags = MMC_DATA_READ;
936 mmc_set_data_timeout(&data, card);
941 blocks = kmalloc(4, GFP_KERNEL);
945 sg_init_one(&sg, blocks, 4);
947 mmc_wait_for_req(card->host, &mrq);
949 result = ntohl(*blocks);
952 if (cmd.error || data.error)
955 *written_blocks = result;
960 static unsigned int mmc_blk_clock_khz(struct mmc_host *host)
962 if (host->actual_clock)
963 return host->actual_clock / 1000;
965 /* Clock may be subject to a divisor, fudge it by a factor of 2. */
967 return host->ios.clock / 2000;
969 /* How can there be no clock */
971 return 100; /* 100 kHz is minimum possible value */
974 static unsigned int mmc_blk_data_timeout_ms(struct mmc_host *host,
975 struct mmc_data *data)
977 unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000);
980 if (data->timeout_clks) {
981 khz = mmc_blk_clock_khz(host);
982 ms += DIV_ROUND_UP(data->timeout_clks, khz);
989 * Attempts to reset the card and get back to the requested partition.
990 * Therefore any error here must result in cancelling the block layer
991 * request, it must not be reattempted without going through the mmc_blk
992 * partition sanity checks.
994 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
998 struct mmc_blk_data *main_md = dev_get_drvdata(&host->card->dev);
1000 if (md->reset_done & type)
1003 md->reset_done |= type;
1004 err = mmc_hw_reset(host->card);
1006 * A successful reset will leave the card in the main partition, but
1007 * upon failure it might not be, so set it to MMC_BLK_PART_INVALID
1010 main_md->part_curr = err ? MMC_BLK_PART_INVALID : main_md->part_type;
1013 /* Ensure we switch back to the correct partition */
1014 if (mmc_blk_part_switch(host->card, md->part_type))
1016 * We have failed to get back into the correct
1017 * partition, so we need to abort the whole request.
1023 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1025 md->reset_done &= ~type;
1029 * The non-block commands come back from the block layer after it queued it and
1030 * processed it with all other requests and then they get issued in this
1033 static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1035 struct mmc_queue_req *mq_rq;
1036 struct mmc_card *card = mq->card;
1037 struct mmc_blk_data *md = mq->blkdata;
1038 struct mmc_blk_ioc_data **idata;
1045 mq_rq = req_to_mmc_queue_req(req);
1046 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
1048 switch (mq_rq->drv_op) {
1049 case MMC_DRV_OP_IOCTL:
1050 if (card->ext_csd.cmdq_en) {
1051 ret = mmc_cmdq_disable(card);
1056 case MMC_DRV_OP_IOCTL_RPMB:
1057 idata = mq_rq->drv_op_data;
1058 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
1059 ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
1063 /* Always switch back to main area after RPMB access */
1065 mmc_blk_part_switch(card, 0);
1066 else if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
1067 mmc_cmdq_enable(card);
1069 case MMC_DRV_OP_BOOT_WP:
1070 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1071 card->ext_csd.boot_ro_lock |
1072 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1073 card->ext_csd.part_time);
1075 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1076 md->disk->disk_name, ret);
1078 card->ext_csd.boot_ro_lock |=
1079 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
1081 case MMC_DRV_OP_GET_CARD_STATUS:
1082 ret = mmc_send_status(card, &status);
1086 case MMC_DRV_OP_GET_EXT_CSD:
1087 ext_csd = mq_rq->drv_op_data;
1088 ret = mmc_get_ext_csd(card, ext_csd);
1091 pr_err("%s: unknown driver specific operation\n",
1092 md->disk->disk_name);
1096 mq_rq->drv_op_result = ret;
1097 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1100 static void mmc_blk_issue_erase_rq(struct mmc_queue *mq, struct request *req,
1101 int type, unsigned int erase_arg)
1103 struct mmc_blk_data *md = mq->blkdata;
1104 struct mmc_card *card = md->queue.card;
1105 unsigned int from, nr;
1107 blk_status_t status = BLK_STS_OK;
1109 if (!mmc_can_erase(card)) {
1110 status = BLK_STS_NOTSUPP;
1114 from = blk_rq_pos(req);
1115 nr = blk_rq_sectors(req);
1119 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1120 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1121 INAND_CMD38_ARG_EXT_CSD,
1122 erase_arg == MMC_TRIM_ARG ?
1123 INAND_CMD38_ARG_TRIM :
1124 INAND_CMD38_ARG_ERASE,
1125 card->ext_csd.generic_cmd6_time);
1128 err = mmc_erase(card, from, nr, erase_arg);
1129 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
1131 status = BLK_STS_IOERR;
1133 mmc_blk_reset_success(md, type);
1135 blk_mq_end_request(req, status);
1138 static void mmc_blk_issue_trim_rq(struct mmc_queue *mq, struct request *req)
1140 mmc_blk_issue_erase_rq(mq, req, MMC_BLK_TRIM, MMC_TRIM_ARG);
1143 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1145 struct mmc_blk_data *md = mq->blkdata;
1146 struct mmc_card *card = md->queue.card;
1147 unsigned int arg = card->erase_arg;
1149 if (mmc_card_broken_sd_discard(card))
1152 mmc_blk_issue_erase_rq(mq, req, MMC_BLK_DISCARD, arg);
1155 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1156 struct request *req)
1158 struct mmc_blk_data *md = mq->blkdata;
1159 struct mmc_card *card = md->queue.card;
1160 unsigned int from, nr, arg;
1161 int err = 0, type = MMC_BLK_SECDISCARD;
1162 blk_status_t status = BLK_STS_OK;
1164 if (!(mmc_can_secure_erase_trim(card))) {
1165 status = BLK_STS_NOTSUPP;
1169 from = blk_rq_pos(req);
1170 nr = blk_rq_sectors(req);
1172 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1173 arg = MMC_SECURE_TRIM1_ARG;
1175 arg = MMC_SECURE_ERASE_ARG;
1178 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1179 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1180 INAND_CMD38_ARG_EXT_CSD,
1181 arg == MMC_SECURE_TRIM1_ARG ?
1182 INAND_CMD38_ARG_SECTRIM1 :
1183 INAND_CMD38_ARG_SECERASE,
1184 card->ext_csd.generic_cmd6_time);
1189 err = mmc_erase(card, from, nr, arg);
1193 status = BLK_STS_IOERR;
1197 if (arg == MMC_SECURE_TRIM1_ARG) {
1198 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1199 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1200 INAND_CMD38_ARG_EXT_CSD,
1201 INAND_CMD38_ARG_SECTRIM2,
1202 card->ext_csd.generic_cmd6_time);
1207 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1211 status = BLK_STS_IOERR;
1217 if (err && !mmc_blk_reset(md, card->host, type))
1220 mmc_blk_reset_success(md, type);
1222 blk_mq_end_request(req, status);
1225 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1227 struct mmc_blk_data *md = mq->blkdata;
1228 struct mmc_card *card = md->queue.card;
1231 ret = mmc_flush_cache(card->host);
1232 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1236 * Reformat current write as a reliable write, supporting
1237 * both legacy and the enhanced reliable write MMC cards.
1238 * In each transfer we'll handle only as much as a single
1239 * reliable write can handle, thus finish the request in
1240 * partial completions.
1242 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1243 struct mmc_card *card,
1244 struct request *req)
1246 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1247 /* Legacy mode imposes restrictions on transfers. */
1248 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
1249 brq->data.blocks = 1;
1251 if (brq->data.blocks > card->ext_csd.rel_sectors)
1252 brq->data.blocks = card->ext_csd.rel_sectors;
1253 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1254 brq->data.blocks = 1;
1258 #define CMD_ERRORS_EXCL_OOR \
1259 (R1_ADDRESS_ERROR | /* Misaligned address */ \
1260 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1261 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1262 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
1263 R1_CC_ERROR | /* Card controller error */ \
1264 R1_ERROR) /* General/unknown error */
1266 #define CMD_ERRORS \
1267 (CMD_ERRORS_EXCL_OOR | \
1268 R1_OUT_OF_RANGE) /* Command argument out of range */ \
1270 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
1275 * Per the SD specification(physical layer version 4.10)[1],
1276 * section 4.3.3, it explicitly states that "When the last
1277 * block of user area is read using CMD18, the host should
1278 * ignore OUT_OF_RANGE error that may occur even the sequence
1279 * is correct". And JESD84-B51 for eMMC also has a similar
1280 * statement on section 6.8.3.
1282 * Multiple block read/write could be done by either predefined
1283 * method, namely CMD23, or open-ending mode. For open-ending mode,
1284 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1286 * However the spec[1] doesn't tell us whether we should also
1287 * ignore that for predefined method. But per the spec[1], section
1288 * 4.15 Set Block Count Command, it says"If illegal block count
1289 * is set, out of range error will be indicated during read/write
1290 * operation (For example, data transfer is stopped at user area
1291 * boundary)." In another word, we could expect a out of range error
1292 * in the response for the following CMD18/25. And if argument of
1293 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1294 * we could also expect to get a -ETIMEDOUT or any error number from
1295 * the host drivers due to missing data response(for write)/data(for
1296 * read), as the cards will stop the data transfer by itself per the
1297 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1300 if (!brq->stop.error) {
1301 bool oor_with_open_end;
1302 /* If there is no error yet, check R1 response */
1304 val = brq->stop.resp[0] & CMD_ERRORS;
1305 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1307 if (val && !oor_with_open_end)
1308 brq->stop.error = -EIO;
1312 static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
1313 int recovery_mode, bool *do_rel_wr_p,
1314 bool *do_data_tag_p)
1316 struct mmc_blk_data *md = mq->blkdata;
1317 struct mmc_card *card = md->queue.card;
1318 struct mmc_blk_request *brq = &mqrq->brq;
1319 struct request *req = mmc_queue_req_to_req(mqrq);
1320 bool do_rel_wr, do_data_tag;
1323 * Reliable writes are used to implement Forced Unit Access and
1324 * are supported only on MMCs.
1326 do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1327 rq_data_dir(req) == WRITE &&
1328 (md->flags & MMC_BLK_REL_WR);
1330 memset(brq, 0, sizeof(struct mmc_blk_request));
1332 mmc_crypto_prepare_req(mqrq);
1334 brq->mrq.data = &brq->data;
1335 brq->mrq.tag = req->tag;
1337 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1340 if (rq_data_dir(req) == READ) {
1341 brq->data.flags = MMC_DATA_READ;
1342 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1344 brq->data.flags = MMC_DATA_WRITE;
1345 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1348 brq->data.blksz = 512;
1349 brq->data.blocks = blk_rq_sectors(req);
1350 brq->data.blk_addr = blk_rq_pos(req);
1353 * The command queue supports 2 priorities: "high" (1) and "simple" (0).
1354 * The eMMC will give "high" priority tasks priority over "simple"
1355 * priority tasks. Here we always set "simple" priority by not setting
1360 * The block layer doesn't support all sector count
1361 * restrictions, so we need to be prepared for too big
1364 if (brq->data.blocks > card->host->max_blk_count)
1365 brq->data.blocks = card->host->max_blk_count;
1367 if (brq->data.blocks > 1) {
1369 * Some SD cards in SPI mode return a CRC error or even lock up
1370 * completely when trying to read the last block using a
1371 * multiblock read command.
1373 if (mmc_host_is_spi(card->host) && (rq_data_dir(req) == READ) &&
1374 (blk_rq_pos(req) + blk_rq_sectors(req) ==
1375 get_capacity(md->disk)))
1379 * After a read error, we redo the request one (native) sector
1380 * at a time in order to accurately determine which
1381 * sectors can be read successfully.
1384 brq->data.blocks = queue_physical_block_size(mq->queue) >> 9;
1387 * Some controllers have HW issues while operating
1388 * in multiple I/O mode
1390 if (card->host->ops->multi_io_quirk)
1391 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1392 (rq_data_dir(req) == READ) ?
1393 MMC_DATA_READ : MMC_DATA_WRITE,
1398 mmc_apply_rel_rw(brq, card, req);
1399 brq->data.flags |= MMC_DATA_REL_WR;
1403 * Data tag is used only during writing meta data to speed
1404 * up write and any subsequent read of this meta data
1406 do_data_tag = card->ext_csd.data_tag_unit_size &&
1407 (req->cmd_flags & REQ_META) &&
1408 (rq_data_dir(req) == WRITE) &&
1409 ((brq->data.blocks * brq->data.blksz) >=
1410 card->ext_csd.data_tag_unit_size);
1413 brq->data.flags |= MMC_DATA_DAT_TAG;
1415 mmc_set_data_timeout(&brq->data, card);
1417 brq->data.sg = mqrq->sg;
1418 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1421 * Adjust the sg list so it is the same size as the
1424 if (brq->data.blocks != blk_rq_sectors(req)) {
1425 int i, data_size = brq->data.blocks << 9;
1426 struct scatterlist *sg;
1428 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1429 data_size -= sg->length;
1430 if (data_size <= 0) {
1431 sg->length += data_size;
1436 brq->data.sg_len = i;
1440 *do_rel_wr_p = do_rel_wr;
1443 *do_data_tag_p = do_data_tag;
1446 #define MMC_CQE_RETRIES 2
1448 static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req)
1450 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1451 struct mmc_request *mrq = &mqrq->brq.mrq;
1452 struct request_queue *q = req->q;
1453 struct mmc_host *host = mq->card->host;
1454 enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
1455 unsigned long flags;
1459 mmc_cqe_post_req(host, mrq);
1461 if (mrq->cmd && mrq->cmd->error)
1462 err = mrq->cmd->error;
1463 else if (mrq->data && mrq->data->error)
1464 err = mrq->data->error;
1469 if (mqrq->retries++ < MMC_CQE_RETRIES)
1470 blk_mq_requeue_request(req, true);
1472 blk_mq_end_request(req, BLK_STS_IOERR);
1473 } else if (mrq->data) {
1474 if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered))
1475 blk_mq_requeue_request(req, true);
1477 __blk_mq_end_request(req, BLK_STS_OK);
1479 blk_mq_end_request(req, BLK_STS_OK);
1482 spin_lock_irqsave(&mq->lock, flags);
1484 mq->in_flight[issue_type] -= 1;
1486 put_card = (mmc_tot_in_flight(mq) == 0);
1488 mmc_cqe_check_busy(mq);
1490 spin_unlock_irqrestore(&mq->lock, flags);
1493 blk_mq_run_hw_queues(q, true);
1496 mmc_put_card(mq->card, &mq->ctx);
1499 void mmc_blk_cqe_recovery(struct mmc_queue *mq)
1501 struct mmc_card *card = mq->card;
1502 struct mmc_host *host = card->host;
1505 pr_debug("%s: CQE recovery start\n", mmc_hostname(host));
1507 err = mmc_cqe_recovery(host);
1509 mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY);
1510 mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY);
1512 pr_debug("%s: CQE recovery done\n", mmc_hostname(host));
1515 static void mmc_blk_cqe_req_done(struct mmc_request *mrq)
1517 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
1519 struct request *req = mmc_queue_req_to_req(mqrq);
1520 struct request_queue *q = req->q;
1521 struct mmc_queue *mq = q->queuedata;
1524 * Block layer timeouts race with completions which means the normal
1525 * completion path cannot be used during recovery.
1527 if (mq->in_recovery)
1528 mmc_blk_cqe_complete_rq(mq, req);
1529 else if (likely(!blk_should_fake_timeout(req->q)))
1530 blk_mq_complete_request(req);
1533 static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
1535 mrq->done = mmc_blk_cqe_req_done;
1536 mrq->recovery_notifier = mmc_cqe_recovery_notifier;
1538 return mmc_cqe_start_req(host, mrq);
1541 static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq,
1542 struct request *req)
1544 struct mmc_blk_request *brq = &mqrq->brq;
1546 memset(brq, 0, sizeof(*brq));
1548 brq->mrq.cmd = &brq->cmd;
1549 brq->mrq.tag = req->tag;
1554 static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
1556 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1557 struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);
1559 mrq->cmd->opcode = MMC_SWITCH;
1560 mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1561 (EXT_CSD_FLUSH_CACHE << 16) |
1563 EXT_CSD_CMD_SET_NORMAL;
1564 mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B;
1566 return mmc_blk_cqe_start_req(mq->card->host, mrq);
1569 static int mmc_blk_hsq_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1571 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1572 struct mmc_host *host = mq->card->host;
1575 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
1576 mqrq->brq.mrq.done = mmc_blk_hsq_req_done;
1577 mmc_pre_req(host, &mqrq->brq.mrq);
1579 err = mmc_cqe_start_req(host, &mqrq->brq.mrq);
1581 mmc_post_req(host, &mqrq->brq.mrq, err);
1586 static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1588 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1589 struct mmc_host *host = mq->card->host;
1591 if (host->hsq_enabled)
1592 return mmc_blk_hsq_issue_rw_rq(mq, req);
1594 mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL);
1596 return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq);
1599 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1600 struct mmc_card *card,
1602 struct mmc_queue *mq)
1604 u32 readcmd, writecmd;
1605 struct mmc_blk_request *brq = &mqrq->brq;
1606 struct request *req = mmc_queue_req_to_req(mqrq);
1607 struct mmc_blk_data *md = mq->blkdata;
1608 bool do_rel_wr, do_data_tag;
1610 mmc_blk_data_prep(mq, mqrq, recovery_mode, &do_rel_wr, &do_data_tag);
1612 brq->mrq.cmd = &brq->cmd;
1614 brq->cmd.arg = blk_rq_pos(req);
1615 if (!mmc_card_blockaddr(card))
1617 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1619 if (brq->data.blocks > 1 || do_rel_wr) {
1620 /* SPI multiblock writes terminate using a special
1621 * token, not a STOP_TRANSMISSION request.
1623 if (!mmc_host_is_spi(card->host) ||
1624 rq_data_dir(req) == READ)
1625 brq->mrq.stop = &brq->stop;
1626 readcmd = MMC_READ_MULTIPLE_BLOCK;
1627 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1629 brq->mrq.stop = NULL;
1630 readcmd = MMC_READ_SINGLE_BLOCK;
1631 writecmd = MMC_WRITE_BLOCK;
1633 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
1636 * Pre-defined multi-block transfers are preferable to
1637 * open ended-ones (and necessary for reliable writes).
1638 * However, it is not sufficient to just send CMD23,
1639 * and avoid the final CMD12, as on an error condition
1640 * CMD12 (stop) needs to be sent anyway. This, coupled
1641 * with Auto-CMD23 enhancements provided by some
1642 * hosts, means that the complexity of dealing
1643 * with this is best left to the host. If CMD23 is
1644 * supported by card and host, we'll fill sbc in and let
1645 * the host deal with handling it correctly. This means
1646 * that for hosts that don't expose MMC_CAP_CMD23, no
1647 * change of behavior will be observed.
1649 * N.B: Some MMC cards experience perf degradation.
1650 * We'll avoid using CMD23-bounded multiblock writes for
1651 * these, while retaining features like reliable writes.
1653 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1654 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1656 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1657 brq->sbc.arg = brq->data.blocks |
1658 (do_rel_wr ? (1 << 31) : 0) |
1659 (do_data_tag ? (1 << 29) : 0);
1660 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1661 brq->mrq.sbc = &brq->sbc;
1665 #define MMC_MAX_RETRIES 5
1666 #define MMC_DATA_RETRIES 2
1667 #define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1)
1669 static int mmc_blk_send_stop(struct mmc_card *card, unsigned int timeout)
1671 struct mmc_command cmd = {
1672 .opcode = MMC_STOP_TRANSMISSION,
1673 .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC,
1674 /* Some hosts wait for busy anyway, so provide a busy timeout */
1675 .busy_timeout = timeout,
1678 return mmc_wait_for_cmd(card->host, &cmd, 5);
1681 static int mmc_blk_fix_state(struct mmc_card *card, struct request *req)
1683 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1684 struct mmc_blk_request *brq = &mqrq->brq;
1685 unsigned int timeout = mmc_blk_data_timeout_ms(card->host, &brq->data);
1688 mmc_retune_hold_now(card->host);
1690 mmc_blk_send_stop(card, timeout);
1692 err = mmc_poll_for_busy(card, timeout, false, MMC_BUSY_IO);
1694 mmc_retune_release(card->host);
1699 #define MMC_READ_SINGLE_RETRIES 2
1701 /* Single (native) sector read during recovery */
1702 static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req)
1704 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1705 struct mmc_request *mrq = &mqrq->brq.mrq;
1706 struct mmc_card *card = mq->card;
1707 struct mmc_host *host = card->host;
1708 blk_status_t error = BLK_STS_OK;
1709 size_t bytes_per_read = queue_physical_block_size(mq->queue);
1716 while (retries++ <= MMC_READ_SINGLE_RETRIES) {
1717 mmc_blk_rw_rq_prep(mqrq, card, 1, mq);
1719 mmc_wait_for_req(host, mrq);
1721 err = mmc_send_status(card, &status);
1725 if (!mmc_host_is_spi(host) &&
1726 !mmc_ready_for_data(status)) {
1727 err = mmc_blk_fix_state(card, req);
1732 if (!mrq->cmd->error)
1736 if (mrq->cmd->error ||
1738 (!mmc_host_is_spi(host) &&
1739 (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS)))
1740 error = BLK_STS_IOERR;
1744 } while (blk_update_request(req, error, bytes_per_read));
1749 mrq->data->bytes_xfered = 0;
1750 blk_update_request(req, BLK_STS_IOERR, bytes_per_read);
1751 /* Let it try the remaining request again */
1752 if (mqrq->retries > MMC_MAX_RETRIES - 1)
1753 mqrq->retries = MMC_MAX_RETRIES - 1;
1756 static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq)
1758 return !!brq->mrq.sbc;
1761 static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq)
1763 return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR;
1767 * Check for errors the host controller driver might not have seen such as
1768 * response mode errors or invalid card state.
1770 static bool mmc_blk_status_error(struct request *req, u32 status)
1772 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1773 struct mmc_blk_request *brq = &mqrq->brq;
1774 struct mmc_queue *mq = req->q->queuedata;
1777 if (mmc_host_is_spi(mq->card->host))
1780 stop_err_bits = mmc_blk_stop_err_bits(brq);
1782 return brq->cmd.resp[0] & CMD_ERRORS ||
1783 brq->stop.resp[0] & stop_err_bits ||
1784 status & stop_err_bits ||
1785 (rq_data_dir(req) == WRITE && !mmc_ready_for_data(status));
1788 static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq)
1790 return !brq->sbc.error && !brq->cmd.error &&
1791 !(brq->cmd.resp[0] & CMD_ERRORS);
1795 * Requests are completed by mmc_blk_mq_complete_rq() which sets simple
1797 * 1. A request that has transferred at least some data is considered
1798 * successful and will be requeued if there is remaining data to
1800 * 2. Otherwise the number of retries is incremented and the request
1801 * will be requeued if there are remaining retries.
1802 * 3. Otherwise the request will be errored out.
1803 * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and
1804 * mqrq->retries. So there are only 4 possible actions here:
1805 * 1. do not accept the bytes_xfered value i.e. set it to zero
1806 * 2. change mqrq->retries to determine the number of retries
1807 * 3. try to reset the card
1808 * 4. read one sector at a time
1810 static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req)
1812 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1813 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1814 struct mmc_blk_request *brq = &mqrq->brq;
1815 struct mmc_blk_data *md = mq->blkdata;
1816 struct mmc_card *card = mq->card;
1822 * Some errors the host driver might not have seen. Set the number of
1823 * bytes transferred to zero in that case.
1825 err = __mmc_send_status(card, &status, 0);
1826 if (err || mmc_blk_status_error(req, status))
1827 brq->data.bytes_xfered = 0;
1829 mmc_retune_release(card->host);
1832 * Try again to get the status. This also provides an opportunity for
1836 err = __mmc_send_status(card, &status, 0);
1839 * Nothing more to do after the number of bytes transferred has been
1840 * updated and there is no card.
1842 if (err && mmc_detect_card_removed(card->host))
1845 /* Try to get back to "tran" state */
1846 if (!mmc_host_is_spi(mq->card->host) &&
1847 (err || !mmc_ready_for_data(status)))
1848 err = mmc_blk_fix_state(mq->card, req);
1851 * Special case for SD cards where the card might record the number of
1854 if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) &&
1855 rq_data_dir(req) == WRITE) {
1856 if (mmc_sd_num_wr_blocks(card, &blocks))
1857 brq->data.bytes_xfered = 0;
1859 brq->data.bytes_xfered = blocks << 9;
1862 /* Reset if the card is in a bad state */
1863 if (!mmc_host_is_spi(mq->card->host) &&
1864 err && mmc_blk_reset(md, card->host, type)) {
1865 pr_err("%s: recovery failed!\n", req->q->disk->disk_name);
1866 mqrq->retries = MMC_NO_RETRIES;
1871 * If anything was done, just return and if there is anything remaining
1872 * on the request it will get requeued.
1874 if (brq->data.bytes_xfered)
1877 /* Reset before last retry */
1878 if (mqrq->retries + 1 == MMC_MAX_RETRIES &&
1879 mmc_blk_reset(md, card->host, type))
1882 /* Command errors fail fast, so use all MMC_MAX_RETRIES */
1883 if (brq->sbc.error || brq->cmd.error)
1886 /* Reduce the remaining retries for data errors */
1887 if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) {
1888 mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES;
1892 if (rq_data_dir(req) == READ && brq->data.blocks >
1893 queue_physical_block_size(mq->queue) >> 9) {
1894 /* Read one (native) sector at a time */
1895 mmc_blk_read_single(mq, req);
1900 static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq)
1902 mmc_blk_eval_resp_error(brq);
1904 return brq->sbc.error || brq->cmd.error || brq->stop.error ||
1905 brq->data.error || brq->cmd.resp[0] & CMD_ERRORS;
1908 static int mmc_spi_err_check(struct mmc_card *card)
1914 * SPI does not have a TRAN state we have to wait on, instead the
1915 * card is ready again when it no longer holds the line LOW.
1916 * We still have to ensure two things here before we know the write
1918 * 1. The card has not disconnected during busy and we actually read our
1919 * own pull-up, thinking it was still connected, so ensure it
1921 * 2. Check for any error bits, in particular R1_SPI_IDLE to catch a
1922 * just reconnected card after being disconnected during busy.
1924 err = __mmc_send_status(card, &status, 0);
1927 /* All R1 and R2 bits of SPI are errors in our case */
1933 static int mmc_blk_busy_cb(void *cb_data, bool *busy)
1935 struct mmc_blk_busy_data *data = cb_data;
1939 err = mmc_send_status(data->card, &status);
1943 /* Accumulate response error bits. */
1944 data->status |= status;
1946 *busy = !mmc_ready_for_data(status);
1950 static int mmc_blk_card_busy(struct mmc_card *card, struct request *req)
1952 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1953 struct mmc_blk_busy_data cb_data;
1956 if (rq_data_dir(req) == READ)
1959 if (mmc_host_is_spi(card->host)) {
1960 err = mmc_spi_err_check(card);
1962 mqrq->brq.data.bytes_xfered = 0;
1966 cb_data.card = card;
1968 err = __mmc_poll_for_busy(card->host, 0, MMC_BLK_TIMEOUT_MS,
1969 &mmc_blk_busy_cb, &cb_data);
1972 * Do not assume data transferred correctly if there are any error bits
1975 if (cb_data.status & mmc_blk_stop_err_bits(&mqrq->brq)) {
1976 mqrq->brq.data.bytes_xfered = 0;
1977 err = err ? err : -EIO;
1980 /* Copy the exception bit so it will be seen later on */
1981 if (mmc_card_mmc(card) && cb_data.status & R1_EXCEPTION_EVENT)
1982 mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
1987 static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
1988 struct request *req)
1990 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1992 mmc_blk_reset_success(mq->blkdata, type);
1995 static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)
1997 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1998 unsigned int nr_bytes = mqrq->brq.data.bytes_xfered;
2001 if (blk_update_request(req, BLK_STS_OK, nr_bytes))
2002 blk_mq_requeue_request(req, true);
2004 __blk_mq_end_request(req, BLK_STS_OK);
2005 } else if (!blk_rq_bytes(req)) {
2006 __blk_mq_end_request(req, BLK_STS_IOERR);
2007 } else if (mqrq->retries++ < MMC_MAX_RETRIES) {
2008 blk_mq_requeue_request(req, true);
2010 if (mmc_card_removed(mq->card))
2011 req->rq_flags |= RQF_QUIET;
2012 blk_mq_end_request(req, BLK_STS_IOERR);
2016 static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
2017 struct mmc_queue_req *mqrq)
2019 return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) &&
2020 (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
2021 mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
2024 static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
2025 struct mmc_queue_req *mqrq)
2027 if (mmc_blk_urgent_bkops_needed(mq, mqrq))
2028 mmc_run_bkops(mq->card);
2031 static void mmc_blk_hsq_req_done(struct mmc_request *mrq)
2033 struct mmc_queue_req *mqrq =
2034 container_of(mrq, struct mmc_queue_req, brq.mrq);
2035 struct request *req = mmc_queue_req_to_req(mqrq);
2036 struct request_queue *q = req->q;
2037 struct mmc_queue *mq = q->queuedata;
2038 struct mmc_host *host = mq->card->host;
2039 unsigned long flags;
2041 if (mmc_blk_rq_error(&mqrq->brq) ||
2042 mmc_blk_urgent_bkops_needed(mq, mqrq)) {
2043 spin_lock_irqsave(&mq->lock, flags);
2044 mq->recovery_needed = true;
2045 mq->recovery_req = req;
2046 spin_unlock_irqrestore(&mq->lock, flags);
2048 host->cqe_ops->cqe_recovery_start(host);
2050 schedule_work(&mq->recovery_work);
2054 mmc_blk_rw_reset_success(mq, req);
2057 * Block layer timeouts race with completions which means the normal
2058 * completion path cannot be used during recovery.
2060 if (mq->in_recovery)
2061 mmc_blk_cqe_complete_rq(mq, req);
2062 else if (likely(!blk_should_fake_timeout(req->q)))
2063 blk_mq_complete_request(req);
2066 void mmc_blk_mq_complete(struct request *req)
2068 struct mmc_queue *mq = req->q->queuedata;
2069 struct mmc_host *host = mq->card->host;
2071 if (host->cqe_enabled)
2072 mmc_blk_cqe_complete_rq(mq, req);
2073 else if (likely(!blk_should_fake_timeout(req->q)))
2074 mmc_blk_mq_complete_rq(mq, req);
2077 static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,
2078 struct request *req)
2080 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2081 struct mmc_host *host = mq->card->host;
2083 if (mmc_blk_rq_error(&mqrq->brq) ||
2084 mmc_blk_card_busy(mq->card, req)) {
2085 mmc_blk_mq_rw_recovery(mq, req);
2087 mmc_blk_rw_reset_success(mq, req);
2088 mmc_retune_release(host);
2091 mmc_blk_urgent_bkops(mq, mqrq);
2094 static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, struct request *req)
2096 unsigned long flags;
2099 spin_lock_irqsave(&mq->lock, flags);
2101 mq->in_flight[mmc_issue_type(mq, req)] -= 1;
2103 put_card = (mmc_tot_in_flight(mq) == 0);
2105 spin_unlock_irqrestore(&mq->lock, flags);
2108 mmc_put_card(mq->card, &mq->ctx);
2111 static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req,
2114 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2115 struct mmc_request *mrq = &mqrq->brq.mrq;
2116 struct mmc_host *host = mq->card->host;
2118 mmc_post_req(host, mrq, 0);
2121 * Block layer timeouts race with completions which means the normal
2122 * completion path cannot be used during recovery.
2124 if (mq->in_recovery) {
2125 mmc_blk_mq_complete_rq(mq, req);
2126 } else if (likely(!blk_should_fake_timeout(req->q))) {
2128 blk_mq_complete_request_direct(req, mmc_blk_mq_complete);
2130 blk_mq_complete_request(req);
2133 mmc_blk_mq_dec_in_flight(mq, req);
2136 void mmc_blk_mq_recovery(struct mmc_queue *mq)
2138 struct request *req = mq->recovery_req;
2139 struct mmc_host *host = mq->card->host;
2140 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2142 mq->recovery_req = NULL;
2143 mq->rw_wait = false;
2145 if (mmc_blk_rq_error(&mqrq->brq)) {
2146 mmc_retune_hold_now(host);
2147 mmc_blk_mq_rw_recovery(mq, req);
2150 mmc_blk_urgent_bkops(mq, mqrq);
2152 mmc_blk_mq_post_req(mq, req, true);
2155 static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq,
2156 struct request **prev_req)
2158 if (mmc_host_done_complete(mq->card->host))
2161 mutex_lock(&mq->complete_lock);
2163 if (!mq->complete_req)
2166 mmc_blk_mq_poll_completion(mq, mq->complete_req);
2169 *prev_req = mq->complete_req;
2171 mmc_blk_mq_post_req(mq, mq->complete_req, true);
2173 mq->complete_req = NULL;
2176 mutex_unlock(&mq->complete_lock);
2179 void mmc_blk_mq_complete_work(struct work_struct *work)
2181 struct mmc_queue *mq = container_of(work, struct mmc_queue,
2184 mmc_blk_mq_complete_prev_req(mq, NULL);
2187 static void mmc_blk_mq_req_done(struct mmc_request *mrq)
2189 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
2191 struct request *req = mmc_queue_req_to_req(mqrq);
2192 struct request_queue *q = req->q;
2193 struct mmc_queue *mq = q->queuedata;
2194 struct mmc_host *host = mq->card->host;
2195 unsigned long flags;
2197 if (!mmc_host_done_complete(host)) {
2201 * We cannot complete the request in this context, so record
2202 * that there is a request to complete, and that a following
2203 * request does not need to wait (although it does need to
2204 * complete complete_req first).
2206 spin_lock_irqsave(&mq->lock, flags);
2207 mq->complete_req = req;
2208 mq->rw_wait = false;
2209 waiting = mq->waiting;
2210 spin_unlock_irqrestore(&mq->lock, flags);
2213 * If 'waiting' then the waiting task will complete this
2214 * request, otherwise queue a work to do it. Note that
2215 * complete_work may still race with the dispatch of a following
2221 queue_work(mq->card->complete_wq, &mq->complete_work);
2226 /* Take the recovery path for errors or urgent background operations */
2227 if (mmc_blk_rq_error(&mqrq->brq) ||
2228 mmc_blk_urgent_bkops_needed(mq, mqrq)) {
2229 spin_lock_irqsave(&mq->lock, flags);
2230 mq->recovery_needed = true;
2231 mq->recovery_req = req;
2232 spin_unlock_irqrestore(&mq->lock, flags);
2234 schedule_work(&mq->recovery_work);
2238 mmc_blk_rw_reset_success(mq, req);
2240 mq->rw_wait = false;
2243 /* context unknown */
2244 mmc_blk_mq_post_req(mq, req, false);
2247 static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err)
2249 unsigned long flags;
2253 * Wait while there is another request in progress, but not if recovery
2254 * is needed. Also indicate whether there is a request waiting to start.
2256 spin_lock_irqsave(&mq->lock, flags);
2257 if (mq->recovery_needed) {
2261 done = !mq->rw_wait;
2263 mq->waiting = !done;
2264 spin_unlock_irqrestore(&mq->lock, flags);
2269 static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req)
2273 wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err));
2275 /* Always complete the previous request if there is one */
2276 mmc_blk_mq_complete_prev_req(mq, prev_req);
2281 static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq,
2282 struct request *req)
2284 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2285 struct mmc_host *host = mq->card->host;
2286 struct request *prev_req = NULL;
2289 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
2291 mqrq->brq.mrq.done = mmc_blk_mq_req_done;
2293 mmc_pre_req(host, &mqrq->brq.mrq);
2295 err = mmc_blk_rw_wait(mq, &prev_req);
2301 err = mmc_start_request(host, &mqrq->brq.mrq);
2304 mmc_blk_mq_post_req(mq, prev_req, true);
2307 mq->rw_wait = false;
2309 /* Release re-tuning here where there is no synchronization required */
2310 if (err || mmc_host_done_complete(host))
2311 mmc_retune_release(host);
2315 mmc_post_req(host, &mqrq->brq.mrq, err);
2320 static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host)
2322 if (host->cqe_enabled)
2323 return host->cqe_ops->cqe_wait_for_idle(host);
2325 return mmc_blk_rw_wait(mq, NULL);
2328 enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
2330 struct mmc_blk_data *md = mq->blkdata;
2331 struct mmc_card *card = md->queue.card;
2332 struct mmc_host *host = card->host;
2335 ret = mmc_blk_part_switch(card, md->part_type);
2337 return MMC_REQ_FAILED_TO_START;
2339 switch (mmc_issue_type(mq, req)) {
2340 case MMC_ISSUE_SYNC:
2341 ret = mmc_blk_wait_for_idle(mq, host);
2343 return MMC_REQ_BUSY;
2344 switch (req_op(req)) {
2346 case REQ_OP_DRV_OUT:
2347 mmc_blk_issue_drv_op(mq, req);
2349 case REQ_OP_DISCARD:
2350 mmc_blk_issue_discard_rq(mq, req);
2352 case REQ_OP_SECURE_ERASE:
2353 mmc_blk_issue_secdiscard_rq(mq, req);
2355 case REQ_OP_WRITE_ZEROES:
2356 mmc_blk_issue_trim_rq(mq, req);
2359 mmc_blk_issue_flush(mq, req);
2363 return MMC_REQ_FAILED_TO_START;
2365 return MMC_REQ_FINISHED;
2366 case MMC_ISSUE_DCMD:
2367 case MMC_ISSUE_ASYNC:
2368 switch (req_op(req)) {
2370 if (!mmc_cache_enabled(host)) {
2371 blk_mq_end_request(req, BLK_STS_OK);
2372 return MMC_REQ_FINISHED;
2374 ret = mmc_blk_cqe_issue_flush(mq, req);
2378 if (host->cqe_enabled)
2379 ret = mmc_blk_cqe_issue_rw_rq(mq, req);
2381 ret = mmc_blk_mq_issue_rw_rq(mq, req);
2388 return MMC_REQ_STARTED;
2389 return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START;
2392 return MMC_REQ_FAILED_TO_START;
2396 static inline int mmc_blk_readonly(struct mmc_card *card)
2398 return mmc_card_readonly(card) ||
2399 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2402 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2403 struct device *parent,
2406 const char *subname,
2408 unsigned int part_type)
2410 struct mmc_blk_data *md;
2413 bool cache_enabled = false;
2414 bool fua_enabled = false;
2416 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
2419 * We get -ENOSPC because there are no more any available
2420 * devidx. The reason may be that, either userspace haven't yet
2421 * unmounted the partitions, which postpones mmc_blk_release()
2422 * from being called, or the device has more partitions than
2425 if (devidx == -ENOSPC)
2426 dev_err(mmc_dev(card->host),
2427 "no more device IDs available\n");
2429 return ERR_PTR(devidx);
2432 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2438 md->area_type = area_type;
2441 * Set the read-only status based on the supported commands
2442 * and the write protect switch.
2444 md->read_only = mmc_blk_readonly(card);
2446 md->disk = mmc_init_queue(&md->queue, card);
2447 if (IS_ERR(md->disk)) {
2448 ret = PTR_ERR(md->disk);
2452 INIT_LIST_HEAD(&md->part);
2453 INIT_LIST_HEAD(&md->rpmbs);
2454 kref_init(&md->kref);
2456 md->queue.blkdata = md;
2457 md->part_type = part_type;
2459 md->disk->major = MMC_BLOCK_MAJOR;
2460 md->disk->minors = perdev_minors;
2461 md->disk->first_minor = devidx * perdev_minors;
2462 md->disk->fops = &mmc_bdops;
2463 md->disk->private_data = md;
2464 md->parent = parent;
2465 set_disk_ro(md->disk, md->read_only || default_ro);
2466 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2467 md->disk->flags |= GENHD_FL_NO_PART;
2470 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2472 * - be set for removable media with permanent block devices
2473 * - be unset for removable block devices with permanent media
2475 * Since MMC block devices clearly fall under the second
2476 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2477 * should use the block device creation/destruction hotplug
2478 * messages to tell when the card is present.
2481 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2482 "mmcblk%u%s", card->host->index, subname ? subname : "");
2484 set_capacity(md->disk, size);
2486 if (mmc_host_cmd23(card->host)) {
2487 if ((mmc_card_mmc(card) &&
2488 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
2489 (mmc_card_sd(card) &&
2490 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2491 md->flags |= MMC_BLK_CMD23;
2494 if (md->flags & MMC_BLK_CMD23 &&
2495 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2496 card->ext_csd.rel_sectors)) {
2497 md->flags |= MMC_BLK_REL_WR;
2499 cache_enabled = true;
2501 if (mmc_cache_enabled(card->host))
2502 cache_enabled = true;
2504 blk_queue_write_cache(md->queue.queue, cache_enabled, fua_enabled);
2506 string_get_size((u64)size, 512, STRING_UNITS_2,
2507 cap_str, sizeof(cap_str));
2508 pr_info("%s: %s %s %s %s\n",
2509 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2510 cap_str, md->read_only ? "(ro)" : "");
2512 /* used in ->open, must be set before add_disk: */
2513 if (area_type == MMC_BLK_DATA_AREA_MAIN)
2514 dev_set_drvdata(&card->dev, md);
2515 ret = device_add_disk(md->parent, md->disk, mmc_disk_attr_groups);
2522 blk_mq_free_tag_set(&md->queue.tag_set);
2526 ida_simple_remove(&mmc_blk_ida, devidx);
2527 return ERR_PTR(ret);
2530 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2534 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2536 * The EXT_CSD sector count is in number or 512 byte
2539 size = card->ext_csd.sectors;
2542 * The CSD capacity field is in units of read_blkbits.
2543 * set_capacity takes units of 512 bytes.
2545 size = (typeof(sector_t))card->csd.capacity
2546 << (card->csd.read_blkbits - 9);
2549 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2550 MMC_BLK_DATA_AREA_MAIN, 0);
2553 static int mmc_blk_alloc_part(struct mmc_card *card,
2554 struct mmc_blk_data *md,
2555 unsigned int part_type,
2558 const char *subname,
2561 struct mmc_blk_data *part_md;
2563 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2564 subname, area_type, part_type);
2565 if (IS_ERR(part_md))
2566 return PTR_ERR(part_md);
2567 list_add(&part_md->part, &md->part);
2573 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2574 * @filp: the character device file
2575 * @cmd: the ioctl() command
2576 * @arg: the argument from userspace
2578 * This will essentially just redirect the ioctl()s coming in over to
2579 * the main block device spawning the RPMB character device.
2581 static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
2584 struct mmc_rpmb_data *rpmb = filp->private_data;
2589 ret = mmc_blk_ioctl_cmd(rpmb->md,
2590 (struct mmc_ioc_cmd __user *)arg,
2593 case MMC_IOC_MULTI_CMD:
2594 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
2595 (struct mmc_ioc_multi_cmd __user *)arg,
2606 #ifdef CONFIG_COMPAT
2607 static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
2610 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
2614 static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
2616 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2617 struct mmc_rpmb_data, chrdev);
2619 get_device(&rpmb->dev);
2620 filp->private_data = rpmb;
2621 mmc_blk_get(rpmb->md->disk);
2623 return nonseekable_open(inode, filp);
2626 static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
2628 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2629 struct mmc_rpmb_data, chrdev);
2631 mmc_blk_put(rpmb->md);
2632 put_device(&rpmb->dev);
2637 static const struct file_operations mmc_rpmb_fileops = {
2638 .release = mmc_rpmb_chrdev_release,
2639 .open = mmc_rpmb_chrdev_open,
2640 .owner = THIS_MODULE,
2641 .llseek = no_llseek,
2642 .unlocked_ioctl = mmc_rpmb_ioctl,
2643 #ifdef CONFIG_COMPAT
2644 .compat_ioctl = mmc_rpmb_ioctl_compat,
2648 static void mmc_blk_rpmb_device_release(struct device *dev)
2650 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2652 ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2656 static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
2657 struct mmc_blk_data *md,
2658 unsigned int part_index,
2660 const char *subname)
2663 char rpmb_name[DISK_NAME_LEN];
2665 struct mmc_rpmb_data *rpmb;
2667 /* This creates the minor number for the RPMB char device */
2668 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
2672 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
2674 ida_simple_remove(&mmc_rpmb_ida, devidx);
2678 snprintf(rpmb_name, sizeof(rpmb_name),
2679 "mmcblk%u%s", card->host->index, subname ? subname : "");
2682 rpmb->part_index = part_index;
2683 rpmb->dev.init_name = rpmb_name;
2684 rpmb->dev.bus = &mmc_rpmb_bus_type;
2685 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
2686 rpmb->dev.parent = &card->dev;
2687 rpmb->dev.release = mmc_blk_rpmb_device_release;
2688 device_initialize(&rpmb->dev);
2689 dev_set_drvdata(&rpmb->dev, rpmb);
2692 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
2693 rpmb->chrdev.owner = THIS_MODULE;
2694 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
2696 pr_err("%s: could not add character device\n", rpmb_name);
2697 goto out_put_device;
2700 list_add(&rpmb->node, &md->rpmbs);
2702 string_get_size((u64)size, 512, STRING_UNITS_2,
2703 cap_str, sizeof(cap_str));
2705 pr_info("%s: %s %s %s, chardev (%d:%d)\n",
2706 rpmb_name, mmc_card_id(card), mmc_card_name(card), cap_str,
2707 MAJOR(mmc_rpmb_devt), rpmb->id);
2712 put_device(&rpmb->dev);
2716 static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
2719 cdev_device_del(&rpmb->chrdev, &rpmb->dev);
2720 put_device(&rpmb->dev);
2723 /* MMC Physical partitions consist of two boot partitions and
2724 * up to four general purpose partitions.
2725 * For each partition enabled in EXT_CSD a block device will be allocatedi
2726 * to provide access to the partition.
2729 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2733 if (!mmc_card_mmc(card))
2736 for (idx = 0; idx < card->nr_parts; idx++) {
2737 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
2739 * RPMB partitions does not provide block access, they
2740 * are only accessed using ioctl():s. Thus create
2741 * special RPMB block devices that do not have a
2742 * backing block queue for these.
2744 ret = mmc_blk_alloc_rpmb_part(card, md,
2745 card->part[idx].part_cfg,
2746 card->part[idx].size >> 9,
2747 card->part[idx].name);
2750 } else if (card->part[idx].size) {
2751 ret = mmc_blk_alloc_part(card, md,
2752 card->part[idx].part_cfg,
2753 card->part[idx].size >> 9,
2754 card->part[idx].force_ro,
2755 card->part[idx].name,
2756 card->part[idx].area_type);
2765 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2768 * Flush remaining requests and free queues. It is freeing the queue
2769 * that stops new requests from being accepted.
2771 del_gendisk(md->disk);
2772 mmc_cleanup_queue(&md->queue);
2776 static void mmc_blk_remove_parts(struct mmc_card *card,
2777 struct mmc_blk_data *md)
2779 struct list_head *pos, *q;
2780 struct mmc_blk_data *part_md;
2781 struct mmc_rpmb_data *rpmb;
2783 /* Remove RPMB partitions */
2784 list_for_each_safe(pos, q, &md->rpmbs) {
2785 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
2787 mmc_blk_remove_rpmb_part(rpmb);
2789 /* Remove block partitions */
2790 list_for_each_safe(pos, q, &md->part) {
2791 part_md = list_entry(pos, struct mmc_blk_data, part);
2793 mmc_blk_remove_req(part_md);
2797 #ifdef CONFIG_DEBUG_FS
2799 static int mmc_dbg_card_status_get(void *data, u64 *val)
2801 struct mmc_card *card = data;
2802 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2803 struct mmc_queue *mq = &md->queue;
2804 struct request *req;
2807 /* Ask the block layer about the card status */
2808 req = blk_mq_alloc_request(mq->queue, REQ_OP_DRV_IN, 0);
2810 return PTR_ERR(req);
2811 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2812 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
2813 blk_execute_rq(req, false);
2814 ret = req_to_mmc_queue_req(req)->drv_op_result;
2819 blk_mq_free_request(req);
2823 DEFINE_DEBUGFS_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2826 /* That is two digits * 512 + 1 for newline */
2827 #define EXT_CSD_STR_LEN 1025
2829 static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2831 struct mmc_card *card = inode->i_private;
2832 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2833 struct mmc_queue *mq = &md->queue;
2834 struct request *req;
2840 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2844 /* Ask the block layer for the EXT CSD */
2845 req = blk_mq_alloc_request(mq->queue, REQ_OP_DRV_IN, 0);
2850 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2851 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
2852 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2853 blk_execute_rq(req, false);
2854 err = req_to_mmc_queue_req(req)->drv_op_result;
2855 blk_mq_free_request(req);
2857 pr_err("FAILED %d\n", err);
2861 for (i = 0; i < 512; i++)
2862 n += sprintf(buf + n, "%02x", ext_csd[i]);
2863 n += sprintf(buf + n, "\n");
2865 if (n != EXT_CSD_STR_LEN) {
2871 filp->private_data = buf;
2880 static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2881 size_t cnt, loff_t *ppos)
2883 char *buf = filp->private_data;
2885 return simple_read_from_buffer(ubuf, cnt, ppos,
2886 buf, EXT_CSD_STR_LEN);
2889 static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2891 kfree(file->private_data);
2895 static const struct file_operations mmc_dbg_ext_csd_fops = {
2896 .open = mmc_ext_csd_open,
2897 .read = mmc_ext_csd_read,
2898 .release = mmc_ext_csd_release,
2899 .llseek = default_llseek,
2902 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2904 struct dentry *root;
2906 if (!card->debugfs_root)
2909 root = card->debugfs_root;
2911 if (mmc_card_mmc(card) || mmc_card_sd(card)) {
2913 debugfs_create_file_unsafe("status", 0400, root,
2915 &mmc_dbg_card_status_fops);
2916 if (!md->status_dentry)
2920 if (mmc_card_mmc(card)) {
2921 md->ext_csd_dentry =
2922 debugfs_create_file("ext_csd", S_IRUSR, root, card,
2923 &mmc_dbg_ext_csd_fops);
2924 if (!md->ext_csd_dentry)
2931 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2932 struct mmc_blk_data *md)
2934 if (!card->debugfs_root)
2937 if (!IS_ERR_OR_NULL(md->status_dentry)) {
2938 debugfs_remove(md->status_dentry);
2939 md->status_dentry = NULL;
2942 if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) {
2943 debugfs_remove(md->ext_csd_dentry);
2944 md->ext_csd_dentry = NULL;
2950 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2955 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2956 struct mmc_blk_data *md)
2960 #endif /* CONFIG_DEBUG_FS */
2962 static int mmc_blk_probe(struct mmc_card *card)
2964 struct mmc_blk_data *md;
2968 * Check that the card supports the command class(es) we need.
2970 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2973 mmc_fixup_device(card, mmc_blk_fixups);
2975 card->complete_wq = alloc_workqueue("mmc_complete",
2976 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2977 if (!card->complete_wq) {
2978 pr_err("Failed to create mmc completion workqueue");
2982 md = mmc_blk_alloc(card);
2988 ret = mmc_blk_alloc_parts(card, md);
2992 /* Add two debugfs entries */
2993 mmc_blk_add_debugfs(card, md);
2995 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2996 pm_runtime_use_autosuspend(&card->dev);
2999 * Don't enable runtime PM for SD-combo cards here. Leave that
3000 * decision to be taken during the SDIO init sequence instead.
3002 if (!mmc_card_sd_combo(card)) {
3003 pm_runtime_set_active(&card->dev);
3004 pm_runtime_enable(&card->dev);
3010 mmc_blk_remove_parts(card, md);
3011 mmc_blk_remove_req(md);
3013 destroy_workqueue(card->complete_wq);
3017 static void mmc_blk_remove(struct mmc_card *card)
3019 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
3021 mmc_blk_remove_debugfs(card, md);
3022 mmc_blk_remove_parts(card, md);
3023 pm_runtime_get_sync(&card->dev);
3024 if (md->part_curr != md->part_type) {
3025 mmc_claim_host(card->host);
3026 mmc_blk_part_switch(card, md->part_type);
3027 mmc_release_host(card->host);
3029 if (!mmc_card_sd_combo(card))
3030 pm_runtime_disable(&card->dev);
3031 pm_runtime_put_noidle(&card->dev);
3032 mmc_blk_remove_req(md);
3033 dev_set_drvdata(&card->dev, NULL);
3034 destroy_workqueue(card->complete_wq);
3037 static int _mmc_blk_suspend(struct mmc_card *card)
3039 struct mmc_blk_data *part_md;
3040 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
3043 mmc_queue_suspend(&md->queue);
3044 list_for_each_entry(part_md, &md->part, part) {
3045 mmc_queue_suspend(&part_md->queue);
3051 static void mmc_blk_shutdown(struct mmc_card *card)
3053 _mmc_blk_suspend(card);
3056 #ifdef CONFIG_PM_SLEEP
3057 static int mmc_blk_suspend(struct device *dev)
3059 struct mmc_card *card = mmc_dev_to_card(dev);
3061 return _mmc_blk_suspend(card);
3064 static int mmc_blk_resume(struct device *dev)
3066 struct mmc_blk_data *part_md;
3067 struct mmc_blk_data *md = dev_get_drvdata(dev);
3071 * Resume involves the card going into idle state,
3072 * so current partition is always the main one.
3074 md->part_curr = md->part_type;
3075 mmc_queue_resume(&md->queue);
3076 list_for_each_entry(part_md, &md->part, part) {
3077 mmc_queue_resume(&part_md->queue);
3084 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3086 static struct mmc_driver mmc_driver = {
3089 .pm = &mmc_blk_pm_ops,
3091 .probe = mmc_blk_probe,
3092 .remove = mmc_blk_remove,
3093 .shutdown = mmc_blk_shutdown,
3096 static int __init mmc_blk_init(void)
3100 res = bus_register(&mmc_rpmb_bus_type);
3102 pr_err("mmcblk: could not register RPMB bus type\n");
3105 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
3107 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
3111 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3112 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3114 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
3116 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3118 goto out_chrdev_unreg;
3120 res = mmc_register_driver(&mmc_driver);
3122 goto out_blkdev_unreg;
3127 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3129 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3131 bus_unregister(&mmc_rpmb_bus_type);
3135 static void __exit mmc_blk_exit(void)
3137 mmc_unregister_driver(&mmc_driver);
3138 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3139 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3140 bus_unregister(&mmc_rpmb_bus_type);
3143 module_init(mmc_blk_init);
3144 module_exit(mmc_blk_exit);
3146 MODULE_LICENSE("GPL");
3147 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");