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 #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
80 (rq_data_dir(req) == WRITE))
81 static DEFINE_MUTEX(block_mutex);
84 * The defaults come from config options but can be overriden by module
87 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
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.
94 static int max_devices;
96 #define MAX_DEVICES 256
98 static DEFINE_IDA(mmc_blk_ida);
99 static DEFINE_IDA(mmc_rpmb_ida);
101 struct mmc_blk_busy_data {
102 struct mmc_card *card;
107 * There is one mmc_blk_data per slot.
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;
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 */
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)
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.
135 unsigned int part_curr;
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_get_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 blk_execute_rq(NULL, req, 0);
268 ret = req_to_mmc_queue_req(req)->drv_op_result;
269 blk_put_request(req);
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);
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);
287 static DEVICE_ATTR(ro_lock_until_next_power_on, 0,
288 power_ro_lock_show, power_ro_lock_store);
290 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
294 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
296 ret = snprintf(buf, PAGE_SIZE, "%d\n",
297 get_disk_ro(dev_to_disk(dev)) ^
303 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
304 const char *buf, size_t count)
308 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
309 unsigned long set = simple_strtoul(buf, &end, 0);
315 set_disk_ro(dev_to_disk(dev), set || md->read_only);
322 static DEVICE_ATTR(force_ro, 0644, force_ro_show, force_ro_store);
324 static struct attribute *mmc_disk_attrs[] = {
325 &dev_attr_force_ro.attr,
326 &dev_attr_ro_lock_until_next_power_on.attr,
330 static umode_t mmc_disk_attrs_is_visible(struct kobject *kobj,
331 struct attribute *a, int n)
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;
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) {
341 if (!(md->queue.card->ext_csd.boot_ro_lock &
342 EXT_CSD_BOOT_WP_B_PWR_WP_DIS))
350 static const struct attribute_group mmc_disk_attr_group = {
351 .is_visible = mmc_disk_attrs_is_visible,
352 .attrs = mmc_disk_attrs,
355 static const struct attribute_group *mmc_disk_attr_groups[] = {
356 &mmc_disk_attr_group,
360 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
362 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
365 mutex_lock(&block_mutex);
368 if ((mode & FMODE_WRITE) && md->read_only) {
373 mutex_unlock(&block_mutex);
378 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
380 struct mmc_blk_data *md = disk->private_data;
382 mutex_lock(&block_mutex);
384 mutex_unlock(&block_mutex);
388 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
390 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
396 struct mmc_blk_ioc_data {
397 struct mmc_ioc_cmd ic;
400 struct mmc_rpmb_data *rpmb;
403 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
404 struct mmc_ioc_cmd __user *user)
406 struct mmc_blk_ioc_data *idata;
409 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
415 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
420 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
421 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
426 if (!idata->buf_bytes) {
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);
446 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
447 struct mmc_blk_ioc_data *idata)
449 struct mmc_ioc_cmd *ic = &idata->ic;
451 if (copy_to_user(&(ic_ptr->response), ic->response,
452 sizeof(ic->response)))
455 if (!idata->ic.write_flag) {
456 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
457 idata->buf, idata->buf_bytes))
464 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
465 struct mmc_blk_ioc_data *idata)
467 struct mmc_command cmd = {}, sbc = {};
468 struct mmc_data data = {};
469 struct mmc_request mrq = {};
470 struct scatterlist sg;
472 unsigned int target_part;
474 if (!card || !md || !idata)
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
484 /* Support multiple RPMB partitions */
485 target_part = idata->rpmb->part_index;
486 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
488 target_part = md->part_type;
491 cmd.opcode = idata->ic.opcode;
492 cmd.arg = idata->ic.arg;
493 cmd.flags = idata->ic.flags;
495 if (idata->buf_bytes) {
498 data.blksz = idata->ic.blksz;
499 data.blocks = idata->ic.blocks;
501 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
503 if (idata->ic.write_flag)
504 data.flags = MMC_DATA_WRITE;
506 data.flags = MMC_DATA_READ;
508 /* data.flags must already be set before doing this. */
509 mmc_set_data_timeout(&data, card);
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;
515 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
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
523 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
525 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
533 err = mmc_blk_part_switch(card, target_part);
537 if (idata->ic.is_acmd) {
538 err = mmc_app_cmd(card->host, card);
544 sbc.opcode = MMC_SET_BLOCK_COUNT;
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.
550 sbc.arg = data.blocks | (idata->ic.write_flag & BIT(31));
551 sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
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);
559 mmc_wait_for_req(card->host, &mrq);
560 memcpy(&idata->ic.response, cmd.resp, sizeof(cmd.resp));
563 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
564 __func__, cmd.error);
568 dev_err(mmc_dev(card->host), "%s: data error %d\n",
569 __func__, data.error);
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.
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);
584 * Update cache so the next mmc_blk_part_switch call operates
585 * on up-to-date data.
587 card->ext_csd.part_config = value;
588 main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
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.
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;
600 card->ext_csd.cache_ctrl = value;
604 * According to the SD specs, some commands require a delay after
605 * issuing the command.
607 if (idata->ic.postsleep_min_us)
608 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
610 if (idata->rpmb || (cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
612 * Ensure RPMB/R1B command has completed by polling CMD13
615 err = mmc_poll_for_busy(card, MMC_BLK_TIMEOUT_MS, false,
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)
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;
633 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
635 return PTR_ERR(idata);
636 /* This will be NULL on non-RPMB ioctl():s */
639 card = md->queue.card;
646 * Dispatch the ioctl() into the block request queue.
649 req = blk_get_request(mq->queue,
650 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
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(NULL, req, 0);
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_put_request(req);
668 return ioc_err ? ioc_err : err;
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)
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 i, err = 0, ioc_err = 0;
683 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
684 sizeof(num_of_cmds)))
690 if (num_of_cmds > MMC_IOC_MAX_CMDS)
693 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
697 for (i = 0; i < num_of_cmds; 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_get_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_data = idata;
728 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
729 blk_execute_rq(NULL, req, 0);
730 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
732 /* copy to user if data and response */
733 for (i = 0; i < num_of_cmds && !err; i++)
734 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
736 blk_put_request(req);
739 for (i = 0; i < num_of_cmds; i++) {
740 kfree(idata[i]->buf);
744 return ioc_err ? ioc_err : err;
747 static int mmc_blk_check_blkdev(struct block_device *bdev)
750 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
751 * whole block device, not on a partition. This prevents overspray
752 * between sibling partitions.
754 if (!capable(CAP_SYS_RAWIO) || bdev_is_partition(bdev))
759 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
760 unsigned int cmd, unsigned long arg)
762 struct mmc_blk_data *md;
767 ret = mmc_blk_check_blkdev(bdev);
770 md = mmc_blk_get(bdev->bd_disk);
773 ret = mmc_blk_ioctl_cmd(md,
774 (struct mmc_ioc_cmd __user *)arg,
778 case MMC_IOC_MULTI_CMD:
779 ret = mmc_blk_check_blkdev(bdev);
782 md = mmc_blk_get(bdev->bd_disk);
785 ret = mmc_blk_ioctl_multi_cmd(md,
786 (struct mmc_ioc_multi_cmd __user *)arg,
796 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
797 unsigned int cmd, unsigned long arg)
799 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
803 static int mmc_blk_alternative_gpt_sector(struct gendisk *disk,
806 struct mmc_blk_data *md;
809 md = mmc_blk_get(disk);
814 ret = mmc_card_alternative_gpt_sector(md->queue.card, sector);
823 static const struct block_device_operations mmc_bdops = {
824 .open = mmc_blk_open,
825 .release = mmc_blk_release,
826 .getgeo = mmc_blk_getgeo,
827 .owner = THIS_MODULE,
828 .ioctl = mmc_blk_ioctl,
830 .compat_ioctl = mmc_blk_compat_ioctl,
832 .alternative_gpt_sector = mmc_blk_alternative_gpt_sector,
835 static int mmc_blk_part_switch_pre(struct mmc_card *card,
836 unsigned int part_type)
840 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
841 if (card->ext_csd.cmdq_en) {
842 ret = mmc_cmdq_disable(card);
846 mmc_retune_pause(card->host);
852 static int mmc_blk_part_switch_post(struct mmc_card *card,
853 unsigned int part_type)
857 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
858 mmc_retune_unpause(card->host);
859 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
860 ret = mmc_cmdq_enable(card);
866 static inline int mmc_blk_part_switch(struct mmc_card *card,
867 unsigned int part_type)
870 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
872 if (main_md->part_curr == part_type)
875 if (mmc_card_mmc(card)) {
876 u8 part_config = card->ext_csd.part_config;
878 ret = mmc_blk_part_switch_pre(card, part_type);
882 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
883 part_config |= part_type;
885 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
886 EXT_CSD_PART_CONFIG, part_config,
887 card->ext_csd.part_time);
889 mmc_blk_part_switch_post(card, part_type);
893 card->ext_csd.part_config = part_config;
895 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
898 main_md->part_curr = part_type;
902 static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
908 struct mmc_request mrq = {};
909 struct mmc_command cmd = {};
910 struct mmc_data data = {};
912 struct scatterlist sg;
914 cmd.opcode = MMC_APP_CMD;
915 cmd.arg = card->rca << 16;
916 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
918 err = mmc_wait_for_cmd(card->host, &cmd, 0);
921 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
924 memset(&cmd, 0, sizeof(struct mmc_command));
926 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
928 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
932 data.flags = MMC_DATA_READ;
935 mmc_set_data_timeout(&data, card);
940 blocks = kmalloc(4, GFP_KERNEL);
944 sg_init_one(&sg, blocks, 4);
946 mmc_wait_for_req(card->host, &mrq);
948 result = ntohl(*blocks);
951 if (cmd.error || data.error)
954 *written_blocks = result;
959 static unsigned int mmc_blk_clock_khz(struct mmc_host *host)
961 if (host->actual_clock)
962 return host->actual_clock / 1000;
964 /* Clock may be subject to a divisor, fudge it by a factor of 2. */
966 return host->ios.clock / 2000;
968 /* How can there be no clock */
970 return 100; /* 100 kHz is minimum possible value */
973 static unsigned int mmc_blk_data_timeout_ms(struct mmc_host *host,
974 struct mmc_data *data)
976 unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000);
979 if (data->timeout_clks) {
980 khz = mmc_blk_clock_khz(host);
981 ms += DIV_ROUND_UP(data->timeout_clks, khz);
987 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
992 if (md->reset_done & type)
995 md->reset_done |= type;
996 err = mmc_hw_reset(host);
997 /* Ensure we switch back to the correct partition */
999 struct mmc_blk_data *main_md =
1000 dev_get_drvdata(&host->card->dev);
1003 main_md->part_curr = main_md->part_type;
1004 part_err = mmc_blk_part_switch(host->card, md->part_type);
1007 * We have failed to get back into the correct
1008 * partition, so we need to abort the whole request.
1016 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1018 md->reset_done &= ~type;
1022 * The non-block commands come back from the block layer after it queued it and
1023 * processed it with all other requests and then they get issued in this
1026 static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1028 struct mmc_queue_req *mq_rq;
1029 struct mmc_card *card = mq->card;
1030 struct mmc_blk_data *md = mq->blkdata;
1031 struct mmc_blk_ioc_data **idata;
1038 mq_rq = req_to_mmc_queue_req(req);
1039 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
1041 switch (mq_rq->drv_op) {
1042 case MMC_DRV_OP_IOCTL:
1043 if (card->ext_csd.cmdq_en) {
1044 ret = mmc_cmdq_disable(card);
1049 case MMC_DRV_OP_IOCTL_RPMB:
1050 idata = mq_rq->drv_op_data;
1051 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
1052 ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
1056 /* Always switch back to main area after RPMB access */
1058 mmc_blk_part_switch(card, 0);
1059 else if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
1060 mmc_cmdq_enable(card);
1062 case MMC_DRV_OP_BOOT_WP:
1063 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1064 card->ext_csd.boot_ro_lock |
1065 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1066 card->ext_csd.part_time);
1068 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1069 md->disk->disk_name, ret);
1071 card->ext_csd.boot_ro_lock |=
1072 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
1074 case MMC_DRV_OP_GET_CARD_STATUS:
1075 ret = mmc_send_status(card, &status);
1079 case MMC_DRV_OP_GET_EXT_CSD:
1080 ext_csd = mq_rq->drv_op_data;
1081 ret = mmc_get_ext_csd(card, ext_csd);
1084 pr_err("%s: unknown driver specific operation\n",
1085 md->disk->disk_name);
1089 mq_rq->drv_op_result = ret;
1090 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1093 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1095 struct mmc_blk_data *md = mq->blkdata;
1096 struct mmc_card *card = md->queue.card;
1097 unsigned int from, nr;
1098 int err = 0, type = MMC_BLK_DISCARD;
1099 blk_status_t status = BLK_STS_OK;
1101 if (!mmc_can_erase(card)) {
1102 status = BLK_STS_NOTSUPP;
1106 from = blk_rq_pos(req);
1107 nr = blk_rq_sectors(req);
1111 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1112 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1113 INAND_CMD38_ARG_EXT_CSD,
1114 card->erase_arg == MMC_TRIM_ARG ?
1115 INAND_CMD38_ARG_TRIM :
1116 INAND_CMD38_ARG_ERASE,
1117 card->ext_csd.generic_cmd6_time);
1120 err = mmc_erase(card, from, nr, card->erase_arg);
1121 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
1123 status = BLK_STS_IOERR;
1125 mmc_blk_reset_success(md, type);
1127 blk_mq_end_request(req, status);
1130 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1131 struct request *req)
1133 struct mmc_blk_data *md = mq->blkdata;
1134 struct mmc_card *card = md->queue.card;
1135 unsigned int from, nr, arg;
1136 int err = 0, type = MMC_BLK_SECDISCARD;
1137 blk_status_t status = BLK_STS_OK;
1139 if (!(mmc_can_secure_erase_trim(card))) {
1140 status = BLK_STS_NOTSUPP;
1144 from = blk_rq_pos(req);
1145 nr = blk_rq_sectors(req);
1147 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1148 arg = MMC_SECURE_TRIM1_ARG;
1150 arg = MMC_SECURE_ERASE_ARG;
1153 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1154 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1155 INAND_CMD38_ARG_EXT_CSD,
1156 arg == MMC_SECURE_TRIM1_ARG ?
1157 INAND_CMD38_ARG_SECTRIM1 :
1158 INAND_CMD38_ARG_SECERASE,
1159 card->ext_csd.generic_cmd6_time);
1164 err = mmc_erase(card, from, nr, arg);
1168 status = BLK_STS_IOERR;
1172 if (arg == MMC_SECURE_TRIM1_ARG) {
1173 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1174 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1175 INAND_CMD38_ARG_EXT_CSD,
1176 INAND_CMD38_ARG_SECTRIM2,
1177 card->ext_csd.generic_cmd6_time);
1182 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1186 status = BLK_STS_IOERR;
1192 if (err && !mmc_blk_reset(md, card->host, type))
1195 mmc_blk_reset_success(md, type);
1197 blk_mq_end_request(req, status);
1200 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1202 struct mmc_blk_data *md = mq->blkdata;
1203 struct mmc_card *card = md->queue.card;
1206 ret = mmc_flush_cache(card->host);
1207 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1211 * Reformat current write as a reliable write, supporting
1212 * both legacy and the enhanced reliable write MMC cards.
1213 * In each transfer we'll handle only as much as a single
1214 * reliable write can handle, thus finish the request in
1215 * partial completions.
1217 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1218 struct mmc_card *card,
1219 struct request *req)
1221 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1222 /* Legacy mode imposes restrictions on transfers. */
1223 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
1224 brq->data.blocks = 1;
1226 if (brq->data.blocks > card->ext_csd.rel_sectors)
1227 brq->data.blocks = card->ext_csd.rel_sectors;
1228 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1229 brq->data.blocks = 1;
1233 #define CMD_ERRORS_EXCL_OOR \
1234 (R1_ADDRESS_ERROR | /* Misaligned address */ \
1235 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1236 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1237 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
1238 R1_CC_ERROR | /* Card controller error */ \
1239 R1_ERROR) /* General/unknown error */
1241 #define CMD_ERRORS \
1242 (CMD_ERRORS_EXCL_OOR | \
1243 R1_OUT_OF_RANGE) /* Command argument out of range */ \
1245 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
1250 * Per the SD specification(physical layer version 4.10)[1],
1251 * section 4.3.3, it explicitly states that "When the last
1252 * block of user area is read using CMD18, the host should
1253 * ignore OUT_OF_RANGE error that may occur even the sequence
1254 * is correct". And JESD84-B51 for eMMC also has a similar
1255 * statement on section 6.8.3.
1257 * Multiple block read/write could be done by either predefined
1258 * method, namely CMD23, or open-ending mode. For open-ending mode,
1259 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1261 * However the spec[1] doesn't tell us whether we should also
1262 * ignore that for predefined method. But per the spec[1], section
1263 * 4.15 Set Block Count Command, it says"If illegal block count
1264 * is set, out of range error will be indicated during read/write
1265 * operation (For example, data transfer is stopped at user area
1266 * boundary)." In another word, we could expect a out of range error
1267 * in the response for the following CMD18/25. And if argument of
1268 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1269 * we could also expect to get a -ETIMEDOUT or any error number from
1270 * the host drivers due to missing data response(for write)/data(for
1271 * read), as the cards will stop the data transfer by itself per the
1272 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1275 if (!brq->stop.error) {
1276 bool oor_with_open_end;
1277 /* If there is no error yet, check R1 response */
1279 val = brq->stop.resp[0] & CMD_ERRORS;
1280 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1282 if (val && !oor_with_open_end)
1283 brq->stop.error = -EIO;
1287 static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
1288 int disable_multi, bool *do_rel_wr_p,
1289 bool *do_data_tag_p)
1291 struct mmc_blk_data *md = mq->blkdata;
1292 struct mmc_card *card = md->queue.card;
1293 struct mmc_blk_request *brq = &mqrq->brq;
1294 struct request *req = mmc_queue_req_to_req(mqrq);
1295 bool do_rel_wr, do_data_tag;
1298 * Reliable writes are used to implement Forced Unit Access and
1299 * are supported only on MMCs.
1301 do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1302 rq_data_dir(req) == WRITE &&
1303 (md->flags & MMC_BLK_REL_WR);
1305 memset(brq, 0, sizeof(struct mmc_blk_request));
1307 mmc_crypto_prepare_req(mqrq);
1309 brq->mrq.data = &brq->data;
1310 brq->mrq.tag = req->tag;
1312 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1315 if (rq_data_dir(req) == READ) {
1316 brq->data.flags = MMC_DATA_READ;
1317 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1319 brq->data.flags = MMC_DATA_WRITE;
1320 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1323 brq->data.blksz = 512;
1324 brq->data.blocks = blk_rq_sectors(req);
1325 brq->data.blk_addr = blk_rq_pos(req);
1328 * The command queue supports 2 priorities: "high" (1) and "simple" (0).
1329 * The eMMC will give "high" priority tasks priority over "simple"
1330 * priority tasks. Here we always set "simple" priority by not setting
1335 * The block layer doesn't support all sector count
1336 * restrictions, so we need to be prepared for too big
1339 if (brq->data.blocks > card->host->max_blk_count)
1340 brq->data.blocks = card->host->max_blk_count;
1342 if (brq->data.blocks > 1) {
1344 * Some SD cards in SPI mode return a CRC error or even lock up
1345 * completely when trying to read the last block using a
1346 * multiblock read command.
1348 if (mmc_host_is_spi(card->host) && (rq_data_dir(req) == READ) &&
1349 (blk_rq_pos(req) + blk_rq_sectors(req) ==
1350 get_capacity(md->disk)))
1354 * After a read error, we redo the request one sector
1355 * at a time in order to accurately determine which
1356 * sectors can be read successfully.
1359 brq->data.blocks = 1;
1362 * Some controllers have HW issues while operating
1363 * in multiple I/O mode
1365 if (card->host->ops->multi_io_quirk)
1366 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1367 (rq_data_dir(req) == READ) ?
1368 MMC_DATA_READ : MMC_DATA_WRITE,
1373 mmc_apply_rel_rw(brq, card, req);
1374 brq->data.flags |= MMC_DATA_REL_WR;
1378 * Data tag is used only during writing meta data to speed
1379 * up write and any subsequent read of this meta data
1381 do_data_tag = card->ext_csd.data_tag_unit_size &&
1382 (req->cmd_flags & REQ_META) &&
1383 (rq_data_dir(req) == WRITE) &&
1384 ((brq->data.blocks * brq->data.blksz) >=
1385 card->ext_csd.data_tag_unit_size);
1388 brq->data.flags |= MMC_DATA_DAT_TAG;
1390 mmc_set_data_timeout(&brq->data, card);
1392 brq->data.sg = mqrq->sg;
1393 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1396 * Adjust the sg list so it is the same size as the
1399 if (brq->data.blocks != blk_rq_sectors(req)) {
1400 int i, data_size = brq->data.blocks << 9;
1401 struct scatterlist *sg;
1403 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1404 data_size -= sg->length;
1405 if (data_size <= 0) {
1406 sg->length += data_size;
1411 brq->data.sg_len = i;
1415 *do_rel_wr_p = do_rel_wr;
1418 *do_data_tag_p = do_data_tag;
1421 #define MMC_CQE_RETRIES 2
1423 static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req)
1425 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1426 struct mmc_request *mrq = &mqrq->brq.mrq;
1427 struct request_queue *q = req->q;
1428 struct mmc_host *host = mq->card->host;
1429 enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
1430 unsigned long flags;
1434 mmc_cqe_post_req(host, mrq);
1436 if (mrq->cmd && mrq->cmd->error)
1437 err = mrq->cmd->error;
1438 else if (mrq->data && mrq->data->error)
1439 err = mrq->data->error;
1444 if (mqrq->retries++ < MMC_CQE_RETRIES)
1445 blk_mq_requeue_request(req, true);
1447 blk_mq_end_request(req, BLK_STS_IOERR);
1448 } else if (mrq->data) {
1449 if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered))
1450 blk_mq_requeue_request(req, true);
1452 __blk_mq_end_request(req, BLK_STS_OK);
1454 blk_mq_end_request(req, BLK_STS_OK);
1457 spin_lock_irqsave(&mq->lock, flags);
1459 mq->in_flight[issue_type] -= 1;
1461 put_card = (mmc_tot_in_flight(mq) == 0);
1463 mmc_cqe_check_busy(mq);
1465 spin_unlock_irqrestore(&mq->lock, flags);
1468 blk_mq_run_hw_queues(q, true);
1471 mmc_put_card(mq->card, &mq->ctx);
1474 void mmc_blk_cqe_recovery(struct mmc_queue *mq)
1476 struct mmc_card *card = mq->card;
1477 struct mmc_host *host = card->host;
1480 pr_debug("%s: CQE recovery start\n", mmc_hostname(host));
1482 err = mmc_cqe_recovery(host);
1484 mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY);
1486 mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY);
1488 pr_debug("%s: CQE recovery done\n", mmc_hostname(host));
1491 static void mmc_blk_cqe_req_done(struct mmc_request *mrq)
1493 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
1495 struct request *req = mmc_queue_req_to_req(mqrq);
1496 struct request_queue *q = req->q;
1497 struct mmc_queue *mq = q->queuedata;
1500 * Block layer timeouts race with completions which means the normal
1501 * completion path cannot be used during recovery.
1503 if (mq->in_recovery)
1504 mmc_blk_cqe_complete_rq(mq, req);
1505 else if (likely(!blk_should_fake_timeout(req->q)))
1506 blk_mq_complete_request(req);
1509 static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
1511 mrq->done = mmc_blk_cqe_req_done;
1512 mrq->recovery_notifier = mmc_cqe_recovery_notifier;
1514 return mmc_cqe_start_req(host, mrq);
1517 static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq,
1518 struct request *req)
1520 struct mmc_blk_request *brq = &mqrq->brq;
1522 memset(brq, 0, sizeof(*brq));
1524 brq->mrq.cmd = &brq->cmd;
1525 brq->mrq.tag = req->tag;
1530 static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
1532 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1533 struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);
1535 mrq->cmd->opcode = MMC_SWITCH;
1536 mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1537 (EXT_CSD_FLUSH_CACHE << 16) |
1539 EXT_CSD_CMD_SET_NORMAL;
1540 mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B;
1542 return mmc_blk_cqe_start_req(mq->card->host, mrq);
1545 static int mmc_blk_hsq_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1547 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1548 struct mmc_host *host = mq->card->host;
1551 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
1552 mqrq->brq.mrq.done = mmc_blk_hsq_req_done;
1553 mmc_pre_req(host, &mqrq->brq.mrq);
1555 err = mmc_cqe_start_req(host, &mqrq->brq.mrq);
1557 mmc_post_req(host, &mqrq->brq.mrq, err);
1562 static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1564 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1565 struct mmc_host *host = mq->card->host;
1567 if (host->hsq_enabled)
1568 return mmc_blk_hsq_issue_rw_rq(mq, req);
1570 mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL);
1572 return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq);
1575 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1576 struct mmc_card *card,
1578 struct mmc_queue *mq)
1580 u32 readcmd, writecmd;
1581 struct mmc_blk_request *brq = &mqrq->brq;
1582 struct request *req = mmc_queue_req_to_req(mqrq);
1583 struct mmc_blk_data *md = mq->blkdata;
1584 bool do_rel_wr, do_data_tag;
1586 mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1588 brq->mrq.cmd = &brq->cmd;
1590 brq->cmd.arg = blk_rq_pos(req);
1591 if (!mmc_card_blockaddr(card))
1593 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1595 if (brq->data.blocks > 1 || do_rel_wr) {
1596 /* SPI multiblock writes terminate using a special
1597 * token, not a STOP_TRANSMISSION request.
1599 if (!mmc_host_is_spi(card->host) ||
1600 rq_data_dir(req) == READ)
1601 brq->mrq.stop = &brq->stop;
1602 readcmd = MMC_READ_MULTIPLE_BLOCK;
1603 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1605 brq->mrq.stop = NULL;
1606 readcmd = MMC_READ_SINGLE_BLOCK;
1607 writecmd = MMC_WRITE_BLOCK;
1609 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
1612 * Pre-defined multi-block transfers are preferable to
1613 * open ended-ones (and necessary for reliable writes).
1614 * However, it is not sufficient to just send CMD23,
1615 * and avoid the final CMD12, as on an error condition
1616 * CMD12 (stop) needs to be sent anyway. This, coupled
1617 * with Auto-CMD23 enhancements provided by some
1618 * hosts, means that the complexity of dealing
1619 * with this is best left to the host. If CMD23 is
1620 * supported by card and host, we'll fill sbc in and let
1621 * the host deal with handling it correctly. This means
1622 * that for hosts that don't expose MMC_CAP_CMD23, no
1623 * change of behavior will be observed.
1625 * N.B: Some MMC cards experience perf degradation.
1626 * We'll avoid using CMD23-bounded multiblock writes for
1627 * these, while retaining features like reliable writes.
1629 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1630 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1632 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1633 brq->sbc.arg = brq->data.blocks |
1634 (do_rel_wr ? (1 << 31) : 0) |
1635 (do_data_tag ? (1 << 29) : 0);
1636 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1637 brq->mrq.sbc = &brq->sbc;
1641 #define MMC_MAX_RETRIES 5
1642 #define MMC_DATA_RETRIES 2
1643 #define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1)
1645 static int mmc_blk_send_stop(struct mmc_card *card, unsigned int timeout)
1647 struct mmc_command cmd = {
1648 .opcode = MMC_STOP_TRANSMISSION,
1649 .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC,
1650 /* Some hosts wait for busy anyway, so provide a busy timeout */
1651 .busy_timeout = timeout,
1654 return mmc_wait_for_cmd(card->host, &cmd, 5);
1657 static int mmc_blk_fix_state(struct mmc_card *card, struct request *req)
1659 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1660 struct mmc_blk_request *brq = &mqrq->brq;
1661 unsigned int timeout = mmc_blk_data_timeout_ms(card->host, &brq->data);
1664 mmc_retune_hold_now(card->host);
1666 mmc_blk_send_stop(card, timeout);
1668 err = mmc_poll_for_busy(card, timeout, false, MMC_BUSY_IO);
1670 mmc_retune_release(card->host);
1675 #define MMC_READ_SINGLE_RETRIES 2
1677 /* Single sector read during recovery */
1678 static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req)
1680 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1681 struct mmc_request *mrq = &mqrq->brq.mrq;
1682 struct mmc_card *card = mq->card;
1683 struct mmc_host *host = card->host;
1684 blk_status_t error = BLK_STS_OK;
1691 mmc_blk_rw_rq_prep(mqrq, card, 1, mq);
1693 mmc_wait_for_req(host, mrq);
1695 err = mmc_send_status(card, &status);
1699 if (!mmc_host_is_spi(host) &&
1700 !mmc_ready_for_data(status)) {
1701 err = mmc_blk_fix_state(card, req);
1706 if (mrq->cmd->error && retries++ < MMC_READ_SINGLE_RETRIES)
1711 if (mrq->cmd->error ||
1713 (!mmc_host_is_spi(host) &&
1714 (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS)))
1715 error = BLK_STS_IOERR;
1719 } while (blk_update_request(req, error, 512));
1724 mrq->data->bytes_xfered = 0;
1725 blk_update_request(req, BLK_STS_IOERR, 512);
1726 /* Let it try the remaining request again */
1727 if (mqrq->retries > MMC_MAX_RETRIES - 1)
1728 mqrq->retries = MMC_MAX_RETRIES - 1;
1731 static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq)
1733 return !!brq->mrq.sbc;
1736 static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq)
1738 return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR;
1742 * Check for errors the host controller driver might not have seen such as
1743 * response mode errors or invalid card state.
1745 static bool mmc_blk_status_error(struct request *req, u32 status)
1747 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1748 struct mmc_blk_request *brq = &mqrq->brq;
1749 struct mmc_queue *mq = req->q->queuedata;
1752 if (mmc_host_is_spi(mq->card->host))
1755 stop_err_bits = mmc_blk_stop_err_bits(brq);
1757 return brq->cmd.resp[0] & CMD_ERRORS ||
1758 brq->stop.resp[0] & stop_err_bits ||
1759 status & stop_err_bits ||
1760 (rq_data_dir(req) == WRITE && !mmc_ready_for_data(status));
1763 static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq)
1765 return !brq->sbc.error && !brq->cmd.error &&
1766 !(brq->cmd.resp[0] & CMD_ERRORS);
1770 * Requests are completed by mmc_blk_mq_complete_rq() which sets simple
1772 * 1. A request that has transferred at least some data is considered
1773 * successful and will be requeued if there is remaining data to
1775 * 2. Otherwise the number of retries is incremented and the request
1776 * will be requeued if there are remaining retries.
1777 * 3. Otherwise the request will be errored out.
1778 * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and
1779 * mqrq->retries. So there are only 4 possible actions here:
1780 * 1. do not accept the bytes_xfered value i.e. set it to zero
1781 * 2. change mqrq->retries to determine the number of retries
1782 * 3. try to reset the card
1783 * 4. read one sector at a time
1785 static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req)
1787 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1788 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1789 struct mmc_blk_request *brq = &mqrq->brq;
1790 struct mmc_blk_data *md = mq->blkdata;
1791 struct mmc_card *card = mq->card;
1797 * Some errors the host driver might not have seen. Set the number of
1798 * bytes transferred to zero in that case.
1800 err = __mmc_send_status(card, &status, 0);
1801 if (err || mmc_blk_status_error(req, status))
1802 brq->data.bytes_xfered = 0;
1804 mmc_retune_release(card->host);
1807 * Try again to get the status. This also provides an opportunity for
1811 err = __mmc_send_status(card, &status, 0);
1814 * Nothing more to do after the number of bytes transferred has been
1815 * updated and there is no card.
1817 if (err && mmc_detect_card_removed(card->host))
1820 /* Try to get back to "tran" state */
1821 if (!mmc_host_is_spi(mq->card->host) &&
1822 (err || !mmc_ready_for_data(status)))
1823 err = mmc_blk_fix_state(mq->card, req);
1826 * Special case for SD cards where the card might record the number of
1829 if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) &&
1830 rq_data_dir(req) == WRITE) {
1831 if (mmc_sd_num_wr_blocks(card, &blocks))
1832 brq->data.bytes_xfered = 0;
1834 brq->data.bytes_xfered = blocks << 9;
1837 /* Reset if the card is in a bad state */
1838 if (!mmc_host_is_spi(mq->card->host) &&
1839 err && mmc_blk_reset(md, card->host, type)) {
1840 pr_err("%s: recovery failed!\n", req->rq_disk->disk_name);
1841 mqrq->retries = MMC_NO_RETRIES;
1846 * If anything was done, just return and if there is anything remaining
1847 * on the request it will get requeued.
1849 if (brq->data.bytes_xfered)
1852 /* Reset before last retry */
1853 if (mqrq->retries + 1 == MMC_MAX_RETRIES)
1854 mmc_blk_reset(md, card->host, type);
1856 /* Command errors fail fast, so use all MMC_MAX_RETRIES */
1857 if (brq->sbc.error || brq->cmd.error)
1860 /* Reduce the remaining retries for data errors */
1861 if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) {
1862 mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES;
1866 /* FIXME: Missing single sector read for large sector size */
1867 if (!mmc_large_sector(card) && rq_data_dir(req) == READ &&
1868 brq->data.blocks > 1) {
1869 /* Read one sector at a time */
1870 mmc_blk_read_single(mq, req);
1875 static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq)
1877 mmc_blk_eval_resp_error(brq);
1879 return brq->sbc.error || brq->cmd.error || brq->stop.error ||
1880 brq->data.error || brq->cmd.resp[0] & CMD_ERRORS;
1883 static int mmc_blk_busy_cb(void *cb_data, bool *busy)
1885 struct mmc_blk_busy_data *data = cb_data;
1889 err = mmc_send_status(data->card, &status);
1893 /* Accumulate response error bits. */
1894 data->status |= status;
1896 *busy = !mmc_ready_for_data(status);
1900 static int mmc_blk_card_busy(struct mmc_card *card, struct request *req)
1902 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1903 struct mmc_blk_busy_data cb_data;
1906 if (mmc_host_is_spi(card->host) || rq_data_dir(req) == READ)
1909 cb_data.card = card;
1911 err = __mmc_poll_for_busy(card, MMC_BLK_TIMEOUT_MS, &mmc_blk_busy_cb,
1915 * Do not assume data transferred correctly if there are any error bits
1918 if (cb_data.status & mmc_blk_stop_err_bits(&mqrq->brq)) {
1919 mqrq->brq.data.bytes_xfered = 0;
1920 err = err ? err : -EIO;
1923 /* Copy the exception bit so it will be seen later on */
1924 if (mmc_card_mmc(card) && cb_data.status & R1_EXCEPTION_EVENT)
1925 mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
1930 static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
1931 struct request *req)
1933 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1935 mmc_blk_reset_success(mq->blkdata, type);
1938 static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)
1940 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1941 unsigned int nr_bytes = mqrq->brq.data.bytes_xfered;
1944 if (blk_update_request(req, BLK_STS_OK, nr_bytes))
1945 blk_mq_requeue_request(req, true);
1947 __blk_mq_end_request(req, BLK_STS_OK);
1948 } else if (!blk_rq_bytes(req)) {
1949 __blk_mq_end_request(req, BLK_STS_IOERR);
1950 } else if (mqrq->retries++ < MMC_MAX_RETRIES) {
1951 blk_mq_requeue_request(req, true);
1953 if (mmc_card_removed(mq->card))
1954 req->rq_flags |= RQF_QUIET;
1955 blk_mq_end_request(req, BLK_STS_IOERR);
1959 static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
1960 struct mmc_queue_req *mqrq)
1962 return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) &&
1963 (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
1964 mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
1967 static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
1968 struct mmc_queue_req *mqrq)
1970 if (mmc_blk_urgent_bkops_needed(mq, mqrq))
1971 mmc_run_bkops(mq->card);
1974 static void mmc_blk_hsq_req_done(struct mmc_request *mrq)
1976 struct mmc_queue_req *mqrq =
1977 container_of(mrq, struct mmc_queue_req, brq.mrq);
1978 struct request *req = mmc_queue_req_to_req(mqrq);
1979 struct request_queue *q = req->q;
1980 struct mmc_queue *mq = q->queuedata;
1981 struct mmc_host *host = mq->card->host;
1982 unsigned long flags;
1984 if (mmc_blk_rq_error(&mqrq->brq) ||
1985 mmc_blk_urgent_bkops_needed(mq, mqrq)) {
1986 spin_lock_irqsave(&mq->lock, flags);
1987 mq->recovery_needed = true;
1988 mq->recovery_req = req;
1989 spin_unlock_irqrestore(&mq->lock, flags);
1991 host->cqe_ops->cqe_recovery_start(host);
1993 schedule_work(&mq->recovery_work);
1997 mmc_blk_rw_reset_success(mq, req);
2000 * Block layer timeouts race with completions which means the normal
2001 * completion path cannot be used during recovery.
2003 if (mq->in_recovery)
2004 mmc_blk_cqe_complete_rq(mq, req);
2005 else if (likely(!blk_should_fake_timeout(req->q)))
2006 blk_mq_complete_request(req);
2009 void mmc_blk_mq_complete(struct request *req)
2011 struct mmc_queue *mq = req->q->queuedata;
2012 struct mmc_host *host = mq->card->host;
2014 if (host->cqe_enabled)
2015 mmc_blk_cqe_complete_rq(mq, req);
2016 else if (likely(!blk_should_fake_timeout(req->q)))
2017 mmc_blk_mq_complete_rq(mq, req);
2020 static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,
2021 struct request *req)
2023 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2024 struct mmc_host *host = mq->card->host;
2026 if (mmc_blk_rq_error(&mqrq->brq) ||
2027 mmc_blk_card_busy(mq->card, req)) {
2028 mmc_blk_mq_rw_recovery(mq, req);
2030 mmc_blk_rw_reset_success(mq, req);
2031 mmc_retune_release(host);
2034 mmc_blk_urgent_bkops(mq, mqrq);
2037 static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, struct request *req)
2039 unsigned long flags;
2042 spin_lock_irqsave(&mq->lock, flags);
2044 mq->in_flight[mmc_issue_type(mq, req)] -= 1;
2046 put_card = (mmc_tot_in_flight(mq) == 0);
2048 spin_unlock_irqrestore(&mq->lock, flags);
2051 mmc_put_card(mq->card, &mq->ctx);
2054 static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req)
2056 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2057 struct mmc_request *mrq = &mqrq->brq.mrq;
2058 struct mmc_host *host = mq->card->host;
2060 mmc_post_req(host, mrq, 0);
2063 * Block layer timeouts race with completions which means the normal
2064 * completion path cannot be used during recovery.
2066 if (mq->in_recovery)
2067 mmc_blk_mq_complete_rq(mq, req);
2068 else if (likely(!blk_should_fake_timeout(req->q)))
2069 blk_mq_complete_request(req);
2071 mmc_blk_mq_dec_in_flight(mq, req);
2074 void mmc_blk_mq_recovery(struct mmc_queue *mq)
2076 struct request *req = mq->recovery_req;
2077 struct mmc_host *host = mq->card->host;
2078 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2080 mq->recovery_req = NULL;
2081 mq->rw_wait = false;
2083 if (mmc_blk_rq_error(&mqrq->brq)) {
2084 mmc_retune_hold_now(host);
2085 mmc_blk_mq_rw_recovery(mq, req);
2088 mmc_blk_urgent_bkops(mq, mqrq);
2090 mmc_blk_mq_post_req(mq, req);
2093 static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq,
2094 struct request **prev_req)
2096 if (mmc_host_done_complete(mq->card->host))
2099 mutex_lock(&mq->complete_lock);
2101 if (!mq->complete_req)
2104 mmc_blk_mq_poll_completion(mq, mq->complete_req);
2107 *prev_req = mq->complete_req;
2109 mmc_blk_mq_post_req(mq, mq->complete_req);
2111 mq->complete_req = NULL;
2114 mutex_unlock(&mq->complete_lock);
2117 void mmc_blk_mq_complete_work(struct work_struct *work)
2119 struct mmc_queue *mq = container_of(work, struct mmc_queue,
2122 mmc_blk_mq_complete_prev_req(mq, NULL);
2125 static void mmc_blk_mq_req_done(struct mmc_request *mrq)
2127 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
2129 struct request *req = mmc_queue_req_to_req(mqrq);
2130 struct request_queue *q = req->q;
2131 struct mmc_queue *mq = q->queuedata;
2132 struct mmc_host *host = mq->card->host;
2133 unsigned long flags;
2135 if (!mmc_host_done_complete(host)) {
2139 * We cannot complete the request in this context, so record
2140 * that there is a request to complete, and that a following
2141 * request does not need to wait (although it does need to
2142 * complete complete_req first).
2144 spin_lock_irqsave(&mq->lock, flags);
2145 mq->complete_req = req;
2146 mq->rw_wait = false;
2147 waiting = mq->waiting;
2148 spin_unlock_irqrestore(&mq->lock, flags);
2151 * If 'waiting' then the waiting task will complete this
2152 * request, otherwise queue a work to do it. Note that
2153 * complete_work may still race with the dispatch of a following
2159 queue_work(mq->card->complete_wq, &mq->complete_work);
2164 /* Take the recovery path for errors or urgent background operations */
2165 if (mmc_blk_rq_error(&mqrq->brq) ||
2166 mmc_blk_urgent_bkops_needed(mq, mqrq)) {
2167 spin_lock_irqsave(&mq->lock, flags);
2168 mq->recovery_needed = true;
2169 mq->recovery_req = req;
2170 spin_unlock_irqrestore(&mq->lock, flags);
2172 schedule_work(&mq->recovery_work);
2176 mmc_blk_rw_reset_success(mq, req);
2178 mq->rw_wait = false;
2181 mmc_blk_mq_post_req(mq, req);
2184 static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err)
2186 unsigned long flags;
2190 * Wait while there is another request in progress, but not if recovery
2191 * is needed. Also indicate whether there is a request waiting to start.
2193 spin_lock_irqsave(&mq->lock, flags);
2194 if (mq->recovery_needed) {
2198 done = !mq->rw_wait;
2200 mq->waiting = !done;
2201 spin_unlock_irqrestore(&mq->lock, flags);
2206 static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req)
2210 wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err));
2212 /* Always complete the previous request if there is one */
2213 mmc_blk_mq_complete_prev_req(mq, prev_req);
2218 static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq,
2219 struct request *req)
2221 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2222 struct mmc_host *host = mq->card->host;
2223 struct request *prev_req = NULL;
2226 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
2228 mqrq->brq.mrq.done = mmc_blk_mq_req_done;
2230 mmc_pre_req(host, &mqrq->brq.mrq);
2232 err = mmc_blk_rw_wait(mq, &prev_req);
2238 err = mmc_start_request(host, &mqrq->brq.mrq);
2241 mmc_blk_mq_post_req(mq, prev_req);
2244 mq->rw_wait = false;
2246 /* Release re-tuning here where there is no synchronization required */
2247 if (err || mmc_host_done_complete(host))
2248 mmc_retune_release(host);
2252 mmc_post_req(host, &mqrq->brq.mrq, err);
2257 static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host)
2259 if (host->cqe_enabled)
2260 return host->cqe_ops->cqe_wait_for_idle(host);
2262 return mmc_blk_rw_wait(mq, NULL);
2265 enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
2267 struct mmc_blk_data *md = mq->blkdata;
2268 struct mmc_card *card = md->queue.card;
2269 struct mmc_host *host = card->host;
2272 ret = mmc_blk_part_switch(card, md->part_type);
2274 return MMC_REQ_FAILED_TO_START;
2276 switch (mmc_issue_type(mq, req)) {
2277 case MMC_ISSUE_SYNC:
2278 ret = mmc_blk_wait_for_idle(mq, host);
2280 return MMC_REQ_BUSY;
2281 switch (req_op(req)) {
2283 case REQ_OP_DRV_OUT:
2284 mmc_blk_issue_drv_op(mq, req);
2286 case REQ_OP_DISCARD:
2287 mmc_blk_issue_discard_rq(mq, req);
2289 case REQ_OP_SECURE_ERASE:
2290 mmc_blk_issue_secdiscard_rq(mq, req);
2293 mmc_blk_issue_flush(mq, req);
2297 return MMC_REQ_FAILED_TO_START;
2299 return MMC_REQ_FINISHED;
2300 case MMC_ISSUE_DCMD:
2301 case MMC_ISSUE_ASYNC:
2302 switch (req_op(req)) {
2304 if (!mmc_cache_enabled(host)) {
2305 blk_mq_end_request(req, BLK_STS_OK);
2306 return MMC_REQ_FINISHED;
2308 ret = mmc_blk_cqe_issue_flush(mq, req);
2312 if (host->cqe_enabled)
2313 ret = mmc_blk_cqe_issue_rw_rq(mq, req);
2315 ret = mmc_blk_mq_issue_rw_rq(mq, req);
2322 return MMC_REQ_STARTED;
2323 return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START;
2326 return MMC_REQ_FAILED_TO_START;
2330 static inline int mmc_blk_readonly(struct mmc_card *card)
2332 return mmc_card_readonly(card) ||
2333 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2336 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2337 struct device *parent,
2340 const char *subname,
2342 unsigned int part_type)
2344 struct mmc_blk_data *md;
2348 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
2351 * We get -ENOSPC because there are no more any available
2352 * devidx. The reason may be that, either userspace haven't yet
2353 * unmounted the partitions, which postpones mmc_blk_release()
2354 * from being called, or the device has more partitions than
2357 if (devidx == -ENOSPC)
2358 dev_err(mmc_dev(card->host),
2359 "no more device IDs available\n");
2361 return ERR_PTR(devidx);
2364 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2370 md->area_type = area_type;
2373 * Set the read-only status based on the supported commands
2374 * and the write protect switch.
2376 md->read_only = mmc_blk_readonly(card);
2378 md->disk = mmc_init_queue(&md->queue, card);
2379 if (IS_ERR(md->disk)) {
2380 ret = PTR_ERR(md->disk);
2384 INIT_LIST_HEAD(&md->part);
2385 INIT_LIST_HEAD(&md->rpmbs);
2386 kref_init(&md->kref);
2388 md->queue.blkdata = md;
2389 md->part_type = part_type;
2391 md->disk->major = MMC_BLOCK_MAJOR;
2392 md->disk->minors = perdev_minors;
2393 md->disk->first_minor = devidx * perdev_minors;
2394 md->disk->fops = &mmc_bdops;
2395 md->disk->private_data = md;
2396 md->parent = parent;
2397 set_disk_ro(md->disk, md->read_only || default_ro);
2398 md->disk->flags = GENHD_FL_EXT_DEVT;
2399 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2400 md->disk->flags |= GENHD_FL_NO_PART_SCAN
2401 | GENHD_FL_SUPPRESS_PARTITION_INFO;
2404 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2406 * - be set for removable media with permanent block devices
2407 * - be unset for removable block devices with permanent media
2409 * Since MMC block devices clearly fall under the second
2410 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2411 * should use the block device creation/destruction hotplug
2412 * messages to tell when the card is present.
2415 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2416 "mmcblk%u%s", card->host->index, subname ? subname : "");
2418 set_capacity(md->disk, size);
2420 if (mmc_host_cmd23(card->host)) {
2421 if ((mmc_card_mmc(card) &&
2422 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
2423 (mmc_card_sd(card) &&
2424 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2425 md->flags |= MMC_BLK_CMD23;
2428 if (mmc_card_mmc(card) &&
2429 md->flags & MMC_BLK_CMD23 &&
2430 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2431 card->ext_csd.rel_sectors)) {
2432 md->flags |= MMC_BLK_REL_WR;
2433 blk_queue_write_cache(md->queue.queue, true, true);
2436 string_get_size((u64)size, 512, STRING_UNITS_2,
2437 cap_str, sizeof(cap_str));
2438 pr_info("%s: %s %s %s %s\n",
2439 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2440 cap_str, md->read_only ? "(ro)" : "");
2442 /* used in ->open, must be set before add_disk: */
2443 if (area_type == MMC_BLK_DATA_AREA_MAIN)
2444 dev_set_drvdata(&card->dev, md);
2445 device_add_disk(md->parent, md->disk, mmc_disk_attr_groups);
2451 ida_simple_remove(&mmc_blk_ida, devidx);
2452 return ERR_PTR(ret);
2455 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2459 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2461 * The EXT_CSD sector count is in number or 512 byte
2464 size = card->ext_csd.sectors;
2467 * The CSD capacity field is in units of read_blkbits.
2468 * set_capacity takes units of 512 bytes.
2470 size = (typeof(sector_t))card->csd.capacity
2471 << (card->csd.read_blkbits - 9);
2474 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2475 MMC_BLK_DATA_AREA_MAIN, 0);
2478 static int mmc_blk_alloc_part(struct mmc_card *card,
2479 struct mmc_blk_data *md,
2480 unsigned int part_type,
2483 const char *subname,
2486 struct mmc_blk_data *part_md;
2488 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2489 subname, area_type, part_type);
2490 if (IS_ERR(part_md))
2491 return PTR_ERR(part_md);
2492 list_add(&part_md->part, &md->part);
2498 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2499 * @filp: the character device file
2500 * @cmd: the ioctl() command
2501 * @arg: the argument from userspace
2503 * This will essentially just redirect the ioctl()s coming in over to
2504 * the main block device spawning the RPMB character device.
2506 static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
2509 struct mmc_rpmb_data *rpmb = filp->private_data;
2514 ret = mmc_blk_ioctl_cmd(rpmb->md,
2515 (struct mmc_ioc_cmd __user *)arg,
2518 case MMC_IOC_MULTI_CMD:
2519 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
2520 (struct mmc_ioc_multi_cmd __user *)arg,
2531 #ifdef CONFIG_COMPAT
2532 static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
2535 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
2539 static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
2541 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2542 struct mmc_rpmb_data, chrdev);
2544 get_device(&rpmb->dev);
2545 filp->private_data = rpmb;
2546 mmc_blk_get(rpmb->md->disk);
2548 return nonseekable_open(inode, filp);
2551 static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
2553 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2554 struct mmc_rpmb_data, chrdev);
2556 mmc_blk_put(rpmb->md);
2557 put_device(&rpmb->dev);
2562 static const struct file_operations mmc_rpmb_fileops = {
2563 .release = mmc_rpmb_chrdev_release,
2564 .open = mmc_rpmb_chrdev_open,
2565 .owner = THIS_MODULE,
2566 .llseek = no_llseek,
2567 .unlocked_ioctl = mmc_rpmb_ioctl,
2568 #ifdef CONFIG_COMPAT
2569 .compat_ioctl = mmc_rpmb_ioctl_compat,
2573 static void mmc_blk_rpmb_device_release(struct device *dev)
2575 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2577 ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2581 static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
2582 struct mmc_blk_data *md,
2583 unsigned int part_index,
2585 const char *subname)
2588 char rpmb_name[DISK_NAME_LEN];
2590 struct mmc_rpmb_data *rpmb;
2592 /* This creates the minor number for the RPMB char device */
2593 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
2597 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
2599 ida_simple_remove(&mmc_rpmb_ida, devidx);
2603 snprintf(rpmb_name, sizeof(rpmb_name),
2604 "mmcblk%u%s", card->host->index, subname ? subname : "");
2607 rpmb->part_index = part_index;
2608 rpmb->dev.init_name = rpmb_name;
2609 rpmb->dev.bus = &mmc_rpmb_bus_type;
2610 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
2611 rpmb->dev.parent = &card->dev;
2612 rpmb->dev.release = mmc_blk_rpmb_device_release;
2613 device_initialize(&rpmb->dev);
2614 dev_set_drvdata(&rpmb->dev, rpmb);
2617 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
2618 rpmb->chrdev.owner = THIS_MODULE;
2619 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
2621 pr_err("%s: could not add character device\n", rpmb_name);
2622 goto out_put_device;
2625 list_add(&rpmb->node, &md->rpmbs);
2627 string_get_size((u64)size, 512, STRING_UNITS_2,
2628 cap_str, sizeof(cap_str));
2630 pr_info("%s: %s %s %s, chardev (%d:%d)\n",
2631 rpmb_name, mmc_card_id(card), mmc_card_name(card), cap_str,
2632 MAJOR(mmc_rpmb_devt), rpmb->id);
2637 put_device(&rpmb->dev);
2641 static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
2644 cdev_device_del(&rpmb->chrdev, &rpmb->dev);
2645 put_device(&rpmb->dev);
2648 /* MMC Physical partitions consist of two boot partitions and
2649 * up to four general purpose partitions.
2650 * For each partition enabled in EXT_CSD a block device will be allocatedi
2651 * to provide access to the partition.
2654 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2658 if (!mmc_card_mmc(card))
2661 for (idx = 0; idx < card->nr_parts; idx++) {
2662 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
2664 * RPMB partitions does not provide block access, they
2665 * are only accessed using ioctl():s. Thus create
2666 * special RPMB block devices that do not have a
2667 * backing block queue for these.
2669 ret = mmc_blk_alloc_rpmb_part(card, md,
2670 card->part[idx].part_cfg,
2671 card->part[idx].size >> 9,
2672 card->part[idx].name);
2675 } else if (card->part[idx].size) {
2676 ret = mmc_blk_alloc_part(card, md,
2677 card->part[idx].part_cfg,
2678 card->part[idx].size >> 9,
2679 card->part[idx].force_ro,
2680 card->part[idx].name,
2681 card->part[idx].area_type);
2690 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2693 * Flush remaining requests and free queues. It is freeing the queue
2694 * that stops new requests from being accepted.
2696 del_gendisk(md->disk);
2697 mmc_cleanup_queue(&md->queue);
2701 static void mmc_blk_remove_parts(struct mmc_card *card,
2702 struct mmc_blk_data *md)
2704 struct list_head *pos, *q;
2705 struct mmc_blk_data *part_md;
2706 struct mmc_rpmb_data *rpmb;
2708 /* Remove RPMB partitions */
2709 list_for_each_safe(pos, q, &md->rpmbs) {
2710 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
2712 mmc_blk_remove_rpmb_part(rpmb);
2714 /* Remove block partitions */
2715 list_for_each_safe(pos, q, &md->part) {
2716 part_md = list_entry(pos, struct mmc_blk_data, part);
2718 mmc_blk_remove_req(part_md);
2722 #ifdef CONFIG_DEBUG_FS
2724 static int mmc_dbg_card_status_get(void *data, u64 *val)
2726 struct mmc_card *card = data;
2727 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2728 struct mmc_queue *mq = &md->queue;
2729 struct request *req;
2732 /* Ask the block layer about the card status */
2733 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0);
2735 return PTR_ERR(req);
2736 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2737 blk_execute_rq(NULL, req, 0);
2738 ret = req_to_mmc_queue_req(req)->drv_op_result;
2743 blk_put_request(req);
2747 DEFINE_DEBUGFS_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2750 /* That is two digits * 512 + 1 for newline */
2751 #define EXT_CSD_STR_LEN 1025
2753 static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2755 struct mmc_card *card = inode->i_private;
2756 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2757 struct mmc_queue *mq = &md->queue;
2758 struct request *req;
2764 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2768 /* Ask the block layer for the EXT CSD */
2769 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0);
2774 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2775 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2776 blk_execute_rq(NULL, req, 0);
2777 err = req_to_mmc_queue_req(req)->drv_op_result;
2778 blk_put_request(req);
2780 pr_err("FAILED %d\n", err);
2784 for (i = 0; i < 512; i++)
2785 n += sprintf(buf + n, "%02x", ext_csd[i]);
2786 n += sprintf(buf + n, "\n");
2788 if (n != EXT_CSD_STR_LEN) {
2794 filp->private_data = buf;
2803 static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2804 size_t cnt, loff_t *ppos)
2806 char *buf = filp->private_data;
2808 return simple_read_from_buffer(ubuf, cnt, ppos,
2809 buf, EXT_CSD_STR_LEN);
2812 static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2814 kfree(file->private_data);
2818 static const struct file_operations mmc_dbg_ext_csd_fops = {
2819 .open = mmc_ext_csd_open,
2820 .read = mmc_ext_csd_read,
2821 .release = mmc_ext_csd_release,
2822 .llseek = default_llseek,
2825 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2827 struct dentry *root;
2829 if (!card->debugfs_root)
2832 root = card->debugfs_root;
2834 if (mmc_card_mmc(card) || mmc_card_sd(card)) {
2836 debugfs_create_file_unsafe("status", 0400, root,
2838 &mmc_dbg_card_status_fops);
2839 if (!md->status_dentry)
2843 if (mmc_card_mmc(card)) {
2844 md->ext_csd_dentry =
2845 debugfs_create_file("ext_csd", S_IRUSR, root, card,
2846 &mmc_dbg_ext_csd_fops);
2847 if (!md->ext_csd_dentry)
2854 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2855 struct mmc_blk_data *md)
2857 if (!card->debugfs_root)
2860 if (!IS_ERR_OR_NULL(md->status_dentry)) {
2861 debugfs_remove(md->status_dentry);
2862 md->status_dentry = NULL;
2865 if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) {
2866 debugfs_remove(md->ext_csd_dentry);
2867 md->ext_csd_dentry = NULL;
2873 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2878 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2879 struct mmc_blk_data *md)
2883 #endif /* CONFIG_DEBUG_FS */
2885 static int mmc_blk_probe(struct mmc_card *card)
2887 struct mmc_blk_data *md;
2891 * Check that the card supports the command class(es) we need.
2893 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2896 mmc_fixup_device(card, mmc_blk_fixups);
2898 card->complete_wq = alloc_workqueue("mmc_complete",
2899 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2900 if (!card->complete_wq) {
2901 pr_err("Failed to create mmc completion workqueue");
2905 md = mmc_blk_alloc(card);
2911 ret = mmc_blk_alloc_parts(card, md);
2915 /* Add two debugfs entries */
2916 mmc_blk_add_debugfs(card, md);
2918 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2919 pm_runtime_use_autosuspend(&card->dev);
2922 * Don't enable runtime PM for SD-combo cards here. Leave that
2923 * decision to be taken during the SDIO init sequence instead.
2925 if (card->type != MMC_TYPE_SD_COMBO) {
2926 pm_runtime_set_active(&card->dev);
2927 pm_runtime_enable(&card->dev);
2933 mmc_blk_remove_parts(card, md);
2934 mmc_blk_remove_req(md);
2936 destroy_workqueue(card->complete_wq);
2940 static void mmc_blk_remove(struct mmc_card *card)
2942 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2944 mmc_blk_remove_debugfs(card, md);
2945 mmc_blk_remove_parts(card, md);
2946 pm_runtime_get_sync(&card->dev);
2947 if (md->part_curr != md->part_type) {
2948 mmc_claim_host(card->host);
2949 mmc_blk_part_switch(card, md->part_type);
2950 mmc_release_host(card->host);
2952 if (card->type != MMC_TYPE_SD_COMBO)
2953 pm_runtime_disable(&card->dev);
2954 pm_runtime_put_noidle(&card->dev);
2955 mmc_blk_remove_req(md);
2956 dev_set_drvdata(&card->dev, NULL);
2957 destroy_workqueue(card->complete_wq);
2960 static int _mmc_blk_suspend(struct mmc_card *card)
2962 struct mmc_blk_data *part_md;
2963 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2966 mmc_queue_suspend(&md->queue);
2967 list_for_each_entry(part_md, &md->part, part) {
2968 mmc_queue_suspend(&part_md->queue);
2974 static void mmc_blk_shutdown(struct mmc_card *card)
2976 _mmc_blk_suspend(card);
2979 #ifdef CONFIG_PM_SLEEP
2980 static int mmc_blk_suspend(struct device *dev)
2982 struct mmc_card *card = mmc_dev_to_card(dev);
2984 return _mmc_blk_suspend(card);
2987 static int mmc_blk_resume(struct device *dev)
2989 struct mmc_blk_data *part_md;
2990 struct mmc_blk_data *md = dev_get_drvdata(dev);
2994 * Resume involves the card going into idle state,
2995 * so current partition is always the main one.
2997 md->part_curr = md->part_type;
2998 mmc_queue_resume(&md->queue);
2999 list_for_each_entry(part_md, &md->part, part) {
3000 mmc_queue_resume(&part_md->queue);
3007 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3009 static struct mmc_driver mmc_driver = {
3012 .pm = &mmc_blk_pm_ops,
3014 .probe = mmc_blk_probe,
3015 .remove = mmc_blk_remove,
3016 .shutdown = mmc_blk_shutdown,
3019 static int __init mmc_blk_init(void)
3023 res = bus_register(&mmc_rpmb_bus_type);
3025 pr_err("mmcblk: could not register RPMB bus type\n");
3028 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
3030 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
3034 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3035 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3037 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
3039 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3041 goto out_chrdev_unreg;
3043 res = mmc_register_driver(&mmc_driver);
3045 goto out_blkdev_unreg;
3050 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3052 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3054 bus_unregister(&mmc_rpmb_bus_type);
3058 static void __exit mmc_blk_exit(void)
3060 mmc_unregister_driver(&mmc_driver);
3061 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3062 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3063 bus_unregister(&mmc_rpmb_bus_type);
3066 module_init(mmc_blk_init);
3067 module_exit(mmc_blk_exit);
3069 MODULE_LICENSE("GPL");
3070 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");