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");
175 * Allow quirks to be overridden for the current card
177 static char *card_quirks;
178 module_param(card_quirks, charp, 0644);
179 MODULE_PARM_DESC(card_quirks, "Force the use of the indicated quirks (a bitfield)");
181 static inline int mmc_blk_part_switch(struct mmc_card *card,
182 unsigned int part_type);
183 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
184 struct mmc_card *card,
186 struct mmc_queue *mq);
187 static void mmc_blk_hsq_req_done(struct mmc_request *mrq);
188 static int mmc_spi_err_check(struct mmc_card *card);
189 static int mmc_blk_busy_cb(void *cb_data, bool *busy);
191 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
193 struct mmc_blk_data *md;
195 mutex_lock(&open_lock);
196 md = disk->private_data;
197 if (md && !kref_get_unless_zero(&md->kref))
199 mutex_unlock(&open_lock);
204 static inline int mmc_get_devidx(struct gendisk *disk)
206 int devidx = disk->first_minor / perdev_minors;
210 static void mmc_blk_kref_release(struct kref *ref)
212 struct mmc_blk_data *md = container_of(ref, struct mmc_blk_data, kref);
215 devidx = mmc_get_devidx(md->disk);
216 ida_simple_remove(&mmc_blk_ida, devidx);
218 mutex_lock(&open_lock);
219 md->disk->private_data = NULL;
220 mutex_unlock(&open_lock);
226 static void mmc_blk_put(struct mmc_blk_data *md)
228 kref_put(&md->kref, mmc_blk_kref_release);
231 static ssize_t power_ro_lock_show(struct device *dev,
232 struct device_attribute *attr, char *buf)
235 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
236 struct mmc_card *card = md->queue.card;
239 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
241 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
244 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
251 static ssize_t power_ro_lock_store(struct device *dev,
252 struct device_attribute *attr, const char *buf, size_t count)
255 struct mmc_blk_data *md, *part_md;
256 struct mmc_queue *mq;
260 if (kstrtoul(buf, 0, &set))
266 md = mmc_blk_get(dev_to_disk(dev));
269 /* Dispatch locking to the block layer */
270 req = blk_mq_alloc_request(mq->queue, REQ_OP_DRV_OUT, 0);
272 count = PTR_ERR(req);
275 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
276 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
277 blk_execute_rq(req, false);
278 ret = req_to_mmc_queue_req(req)->drv_op_result;
279 blk_mq_free_request(req);
282 pr_info("%s: Locking boot partition ro until next power on\n",
283 md->disk->disk_name);
284 set_disk_ro(md->disk, 1);
286 list_for_each_entry(part_md, &md->part, part)
287 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
288 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
289 set_disk_ro(part_md->disk, 1);
297 static DEVICE_ATTR(ro_lock_until_next_power_on, 0,
298 power_ro_lock_show, power_ro_lock_store);
300 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
304 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
306 ret = snprintf(buf, PAGE_SIZE, "%d\n",
307 get_disk_ro(dev_to_disk(dev)) ^
313 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
314 const char *buf, size_t count)
318 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
319 unsigned long set = simple_strtoul(buf, &end, 0);
325 set_disk_ro(dev_to_disk(dev), set || md->read_only);
332 static DEVICE_ATTR(force_ro, 0644, force_ro_show, force_ro_store);
334 static struct attribute *mmc_disk_attrs[] = {
335 &dev_attr_force_ro.attr,
336 &dev_attr_ro_lock_until_next_power_on.attr,
340 static umode_t mmc_disk_attrs_is_visible(struct kobject *kobj,
341 struct attribute *a, int n)
343 struct device *dev = kobj_to_dev(kobj);
344 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
345 umode_t mode = a->mode;
347 if (a == &dev_attr_ro_lock_until_next_power_on.attr &&
348 (md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
349 md->queue.card->ext_csd.boot_ro_lockable) {
351 if (!(md->queue.card->ext_csd.boot_ro_lock &
352 EXT_CSD_BOOT_WP_B_PWR_WP_DIS))
360 static const struct attribute_group mmc_disk_attr_group = {
361 .is_visible = mmc_disk_attrs_is_visible,
362 .attrs = mmc_disk_attrs,
365 static const struct attribute_group *mmc_disk_attr_groups[] = {
366 &mmc_disk_attr_group,
370 static int mmc_blk_open(struct gendisk *disk, blk_mode_t mode)
372 struct mmc_blk_data *md = mmc_blk_get(disk);
375 mutex_lock(&block_mutex);
378 if ((mode & BLK_OPEN_WRITE) && md->read_only) {
383 mutex_unlock(&block_mutex);
388 static void mmc_blk_release(struct gendisk *disk)
390 struct mmc_blk_data *md = disk->private_data;
392 mutex_lock(&block_mutex);
394 mutex_unlock(&block_mutex);
398 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
400 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
406 struct mmc_blk_ioc_data {
407 struct mmc_ioc_cmd ic;
411 #define MMC_BLK_IOC_DROP BIT(0) /* drop this mrq */
412 #define MMC_BLK_IOC_SBC BIT(1) /* use mrq.sbc */
414 struct mmc_rpmb_data *rpmb;
417 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
418 struct mmc_ioc_cmd __user *user)
420 struct mmc_blk_ioc_data *idata;
423 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
429 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
434 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
435 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
440 if (!idata->buf_bytes) {
445 idata->buf = memdup_user((void __user *)(unsigned long)
446 idata->ic.data_ptr, idata->buf_bytes);
447 if (IS_ERR(idata->buf)) {
448 err = PTR_ERR(idata->buf);
460 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
461 struct mmc_blk_ioc_data *idata)
463 struct mmc_ioc_cmd *ic = &idata->ic;
465 if (copy_to_user(&(ic_ptr->response), ic->response,
466 sizeof(ic->response)))
469 if (!idata->ic.write_flag) {
470 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
471 idata->buf, idata->buf_bytes))
478 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
479 struct mmc_blk_ioc_data **idatas, int i)
481 struct mmc_command cmd = {}, sbc = {};
482 struct mmc_data data = {};
483 struct mmc_request mrq = {};
484 struct scatterlist sg;
486 unsigned int busy_timeout_ms;
488 unsigned int target_part;
489 struct mmc_blk_ioc_data *idata = idatas[i];
490 struct mmc_blk_ioc_data *prev_idata = NULL;
492 if (!card || !md || !idata)
495 if (idata->flags & MMC_BLK_IOC_DROP)
498 if (idata->flags & MMC_BLK_IOC_SBC)
499 prev_idata = idatas[i - 1];
502 * The RPMB accesses comes in from the character device, so we
503 * need to target these explicitly. Else we just target the
504 * partition type for the block device the ioctl() was issued
508 /* Support multiple RPMB partitions */
509 target_part = idata->rpmb->part_index;
510 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
512 target_part = md->part_type;
515 cmd.opcode = idata->ic.opcode;
516 cmd.arg = idata->ic.arg;
517 cmd.flags = idata->ic.flags;
519 if (idata->buf_bytes) {
522 data.blksz = idata->ic.blksz;
523 data.blocks = idata->ic.blocks;
525 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
527 if (idata->ic.write_flag)
528 data.flags = MMC_DATA_WRITE;
530 data.flags = MMC_DATA_READ;
532 /* data.flags must already be set before doing this. */
533 mmc_set_data_timeout(&data, card);
535 /* Allow overriding the timeout_ns for empirical tuning. */
536 if (idata->ic.data_timeout_ns)
537 data.timeout_ns = idata->ic.data_timeout_ns;
544 err = mmc_blk_part_switch(card, target_part);
548 if (idata->ic.is_acmd) {
549 err = mmc_app_cmd(card->host, card);
554 if (idata->rpmb || prev_idata) {
555 sbc.opcode = MMC_SET_BLOCK_COUNT;
557 * We don't do any blockcount validation because the max size
558 * may be increased by a future standard. We just copy the
559 * 'Reliable Write' bit here.
561 sbc.arg = data.blocks | (idata->ic.write_flag & BIT(31));
563 sbc.arg = prev_idata->ic.arg;
564 sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
568 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
569 (cmd.opcode == MMC_SWITCH))
570 return mmc_sanitize(card, idata->ic.cmd_timeout_ms);
572 /* If it's an R1B response we need some more preparations. */
573 busy_timeout_ms = idata->ic.cmd_timeout_ms ? : MMC_BLK_TIMEOUT_MS;
574 r1b_resp = (cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B;
576 mmc_prepare_busy_cmd(card->host, &cmd, busy_timeout_ms);
578 mmc_wait_for_req(card->host, &mrq);
579 memcpy(&idata->ic.response, cmd.resp, sizeof(cmd.resp));
582 memcpy(&prev_idata->ic.response, sbc.resp, sizeof(sbc.resp));
584 dev_err(mmc_dev(card->host), "%s: sbc error %d\n",
585 __func__, sbc.error);
591 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
592 __func__, cmd.error);
596 dev_err(mmc_dev(card->host), "%s: data error %d\n",
597 __func__, data.error);
602 * Make sure the cache of the PARTITION_CONFIG register and
603 * PARTITION_ACCESS bits is updated in case the ioctl ext_csd write
604 * changed it successfully.
606 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_PART_CONFIG) &&
607 (cmd.opcode == MMC_SWITCH)) {
608 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
609 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg);
612 * Update cache so the next mmc_blk_part_switch call operates
613 * on up-to-date data.
615 card->ext_csd.part_config = value;
616 main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
620 * Make sure to update CACHE_CTRL in case it was changed. The cache
621 * will get turned back on if the card is re-initialized, e.g.
622 * suspend/resume or hw reset in recovery.
624 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_CACHE_CTRL) &&
625 (cmd.opcode == MMC_SWITCH)) {
626 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg) & 1;
628 card->ext_csd.cache_ctrl = value;
632 * According to the SD specs, some commands require a delay after
633 * issuing the command.
635 if (idata->ic.postsleep_min_us)
636 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
638 if (mmc_host_is_spi(card->host)) {
639 if (idata->ic.write_flag || r1b_resp || cmd.flags & MMC_RSP_SPI_BUSY)
640 return mmc_spi_err_check(card);
645 * Ensure RPMB, writes and R1B responses are completed by polling with
646 * CMD13. Note that, usually we don't need to poll when using HW busy
647 * detection, but here it's needed since some commands may indicate the
648 * error through the R1 status bits.
650 if (idata->rpmb || idata->ic.write_flag || r1b_resp) {
651 struct mmc_blk_busy_data cb_data = {
655 err = __mmc_poll_for_busy(card->host, 0, busy_timeout_ms,
656 &mmc_blk_busy_cb, &cb_data);
658 idata->ic.response[0] = cb_data.status;
664 static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
665 struct mmc_ioc_cmd __user *ic_ptr,
666 struct mmc_rpmb_data *rpmb)
668 struct mmc_blk_ioc_data *idata;
669 struct mmc_blk_ioc_data *idatas[1];
670 struct mmc_queue *mq;
671 struct mmc_card *card;
672 int err = 0, ioc_err = 0;
675 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
677 return PTR_ERR(idata);
678 /* This will be NULL on non-RPMB ioctl():s */
681 card = md->queue.card;
688 * Dispatch the ioctl() into the block request queue.
691 req = blk_mq_alloc_request(mq->queue,
692 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
698 req_to_mmc_queue_req(req)->drv_op =
699 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
700 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
701 req_to_mmc_queue_req(req)->drv_op_data = idatas;
702 req_to_mmc_queue_req(req)->ioc_count = 1;
703 blk_execute_rq(req, false);
704 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
705 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
706 blk_mq_free_request(req);
711 return ioc_err ? ioc_err : err;
714 static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
715 struct mmc_ioc_multi_cmd __user *user,
716 struct mmc_rpmb_data *rpmb)
718 struct mmc_blk_ioc_data **idata = NULL;
719 struct mmc_ioc_cmd __user *cmds = user->cmds;
720 struct mmc_card *card;
721 struct mmc_queue *mq;
722 int err = 0, ioc_err = 0;
727 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
728 sizeof(num_of_cmds)))
734 if (num_of_cmds > MMC_IOC_MAX_CMDS)
738 idata = kcalloc(n, sizeof(*idata), GFP_KERNEL);
742 for (i = 0; i < n; i++) {
743 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
744 if (IS_ERR(idata[i])) {
745 err = PTR_ERR(idata[i]);
749 /* This will be NULL on non-RPMB ioctl():s */
750 idata[i]->rpmb = rpmb;
753 card = md->queue.card;
761 * Dispatch the ioctl()s into the block request queue.
764 req = blk_mq_alloc_request(mq->queue,
765 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
770 req_to_mmc_queue_req(req)->drv_op =
771 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
772 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
773 req_to_mmc_queue_req(req)->drv_op_data = idata;
774 req_to_mmc_queue_req(req)->ioc_count = n;
775 blk_execute_rq(req, false);
776 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
778 /* copy to user if data and response */
779 for (i = 0; i < n && !err; i++)
780 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
782 blk_mq_free_request(req);
785 for (i = 0; i < n; i++) {
786 kfree(idata[i]->buf);
790 return ioc_err ? ioc_err : err;
793 static int mmc_blk_check_blkdev(struct block_device *bdev)
796 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
797 * whole block device, not on a partition. This prevents overspray
798 * between sibling partitions.
800 if (!capable(CAP_SYS_RAWIO) || bdev_is_partition(bdev))
805 static int mmc_blk_ioctl(struct block_device *bdev, blk_mode_t mode,
806 unsigned int cmd, unsigned long arg)
808 struct mmc_blk_data *md;
813 ret = mmc_blk_check_blkdev(bdev);
816 md = mmc_blk_get(bdev->bd_disk);
819 ret = mmc_blk_ioctl_cmd(md,
820 (struct mmc_ioc_cmd __user *)arg,
824 case MMC_IOC_MULTI_CMD:
825 ret = mmc_blk_check_blkdev(bdev);
828 md = mmc_blk_get(bdev->bd_disk);
831 ret = mmc_blk_ioctl_multi_cmd(md,
832 (struct mmc_ioc_multi_cmd __user *)arg,
842 static int mmc_blk_compat_ioctl(struct block_device *bdev, blk_mode_t mode,
843 unsigned int cmd, unsigned long arg)
845 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
849 static int mmc_blk_alternative_gpt_sector(struct gendisk *disk,
852 struct mmc_blk_data *md;
855 md = mmc_blk_get(disk);
860 ret = mmc_card_alternative_gpt_sector(md->queue.card, sector);
869 static const struct block_device_operations mmc_bdops = {
870 .open = mmc_blk_open,
871 .release = mmc_blk_release,
872 .getgeo = mmc_blk_getgeo,
873 .owner = THIS_MODULE,
874 .ioctl = mmc_blk_ioctl,
876 .compat_ioctl = mmc_blk_compat_ioctl,
878 .alternative_gpt_sector = mmc_blk_alternative_gpt_sector,
881 static int mmc_blk_part_switch_pre(struct mmc_card *card,
882 unsigned int part_type)
884 const unsigned int mask = EXT_CSD_PART_CONFIG_ACC_RPMB;
887 if ((part_type & mask) == mask) {
888 if (card->ext_csd.cmdq_en) {
889 ret = mmc_cmdq_disable(card);
893 mmc_retune_pause(card->host);
899 static int mmc_blk_part_switch_post(struct mmc_card *card,
900 unsigned int part_type)
902 const unsigned int mask = EXT_CSD_PART_CONFIG_ACC_RPMB;
905 if ((part_type & mask) == mask) {
906 mmc_retune_unpause(card->host);
907 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
908 ret = mmc_cmdq_enable(card);
914 static inline int mmc_blk_part_switch(struct mmc_card *card,
915 unsigned int part_type)
918 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
920 if (main_md->part_curr == part_type)
923 if (mmc_card_mmc(card)) {
924 u8 part_config = card->ext_csd.part_config;
926 ret = mmc_blk_part_switch_pre(card, part_type);
930 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
931 part_config |= part_type;
933 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
934 EXT_CSD_PART_CONFIG, part_config,
935 card->ext_csd.part_time);
937 mmc_blk_part_switch_post(card, part_type);
941 card->ext_csd.part_config = part_config;
943 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
946 main_md->part_curr = part_type;
950 static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
956 struct mmc_request mrq = {};
957 struct mmc_command cmd = {};
958 struct mmc_data data = {};
960 struct scatterlist sg;
962 err = mmc_app_cmd(card->host, card);
966 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
968 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
972 data.flags = MMC_DATA_READ;
975 mmc_set_data_timeout(&data, card);
980 blocks = kmalloc(4, GFP_KERNEL);
984 sg_init_one(&sg, blocks, 4);
986 mmc_wait_for_req(card->host, &mrq);
988 result = ntohl(*blocks);
991 if (cmd.error || data.error)
994 *written_blocks = result;
999 static unsigned int mmc_blk_clock_khz(struct mmc_host *host)
1001 if (host->actual_clock)
1002 return host->actual_clock / 1000;
1004 /* Clock may be subject to a divisor, fudge it by a factor of 2. */
1005 if (host->ios.clock)
1006 return host->ios.clock / 2000;
1008 /* How can there be no clock */
1010 return 100; /* 100 kHz is minimum possible value */
1013 static unsigned int mmc_blk_data_timeout_ms(struct mmc_host *host,
1014 struct mmc_data *data)
1016 unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000);
1019 if (data->timeout_clks) {
1020 khz = mmc_blk_clock_khz(host);
1021 ms += DIV_ROUND_UP(data->timeout_clks, khz);
1028 * Attempts to reset the card and get back to the requested partition.
1029 * Therefore any error here must result in cancelling the block layer
1030 * request, it must not be reattempted without going through the mmc_blk
1031 * partition sanity checks.
1033 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1037 struct mmc_blk_data *main_md = dev_get_drvdata(&host->card->dev);
1039 if (md->reset_done & type)
1042 md->reset_done |= type;
1043 err = mmc_hw_reset(host->card);
1045 * A successful reset will leave the card in the main partition, but
1046 * upon failure it might not be, so set it to MMC_BLK_PART_INVALID
1049 main_md->part_curr = err ? MMC_BLK_PART_INVALID : main_md->part_type;
1052 /* Ensure we switch back to the correct partition */
1053 if (mmc_blk_part_switch(host->card, md->part_type))
1055 * We have failed to get back into the correct
1056 * partition, so we need to abort the whole request.
1062 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1064 md->reset_done &= ~type;
1067 static void mmc_blk_check_sbc(struct mmc_queue_req *mq_rq)
1069 struct mmc_blk_ioc_data **idata = mq_rq->drv_op_data;
1072 for (i = 1; i < mq_rq->ioc_count; i++) {
1073 if (idata[i - 1]->ic.opcode == MMC_SET_BLOCK_COUNT &&
1074 mmc_op_multi(idata[i]->ic.opcode)) {
1075 idata[i - 1]->flags |= MMC_BLK_IOC_DROP;
1076 idata[i]->flags |= MMC_BLK_IOC_SBC;
1082 * The non-block commands come back from the block layer after it queued it and
1083 * processed it with all other requests and then they get issued in this
1086 static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1088 struct mmc_queue_req *mq_rq;
1089 struct mmc_card *card = mq->card;
1090 struct mmc_blk_data *md = mq->blkdata;
1091 struct mmc_blk_ioc_data **idata;
1098 mq_rq = req_to_mmc_queue_req(req);
1099 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
1101 switch (mq_rq->drv_op) {
1102 case MMC_DRV_OP_IOCTL:
1103 if (card->ext_csd.cmdq_en) {
1104 ret = mmc_cmdq_disable(card);
1109 mmc_blk_check_sbc(mq_rq);
1112 case MMC_DRV_OP_IOCTL_RPMB:
1113 idata = mq_rq->drv_op_data;
1114 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
1115 ret = __mmc_blk_ioctl_cmd(card, md, idata, i);
1119 /* Always switch back to main area after RPMB access */
1121 mmc_blk_part_switch(card, 0);
1122 else if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
1123 mmc_cmdq_enable(card);
1125 case MMC_DRV_OP_BOOT_WP:
1126 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1127 card->ext_csd.boot_ro_lock |
1128 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1129 card->ext_csd.part_time);
1131 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1132 md->disk->disk_name, ret);
1134 card->ext_csd.boot_ro_lock |=
1135 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
1137 case MMC_DRV_OP_GET_CARD_STATUS:
1138 ret = mmc_send_status(card, &status);
1142 case MMC_DRV_OP_GET_EXT_CSD:
1143 ext_csd = mq_rq->drv_op_data;
1144 ret = mmc_get_ext_csd(card, ext_csd);
1147 pr_err("%s: unknown driver specific operation\n",
1148 md->disk->disk_name);
1152 mq_rq->drv_op_result = ret;
1153 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1156 static void mmc_blk_issue_erase_rq(struct mmc_queue *mq, struct request *req,
1157 int type, unsigned int erase_arg)
1159 struct mmc_blk_data *md = mq->blkdata;
1160 struct mmc_card *card = md->queue.card;
1161 unsigned int from, nr;
1163 blk_status_t status = BLK_STS_OK;
1165 if (!mmc_can_erase(card)) {
1166 status = BLK_STS_NOTSUPP;
1170 from = blk_rq_pos(req);
1171 nr = blk_rq_sectors(req);
1175 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1176 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1177 INAND_CMD38_ARG_EXT_CSD,
1178 erase_arg == MMC_TRIM_ARG ?
1179 INAND_CMD38_ARG_TRIM :
1180 INAND_CMD38_ARG_ERASE,
1181 card->ext_csd.generic_cmd6_time);
1184 err = mmc_erase(card, from, nr, erase_arg);
1185 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
1187 status = BLK_STS_IOERR;
1189 mmc_blk_reset_success(md, type);
1191 blk_mq_end_request(req, status);
1194 static void mmc_blk_issue_trim_rq(struct mmc_queue *mq, struct request *req)
1196 mmc_blk_issue_erase_rq(mq, req, MMC_BLK_TRIM, MMC_TRIM_ARG);
1199 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1201 struct mmc_blk_data *md = mq->blkdata;
1202 struct mmc_card *card = md->queue.card;
1203 unsigned int arg = card->erase_arg;
1205 if (mmc_card_broken_sd_discard(card))
1208 mmc_blk_issue_erase_rq(mq, req, MMC_BLK_DISCARD, arg);
1211 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1212 struct request *req)
1214 struct mmc_blk_data *md = mq->blkdata;
1215 struct mmc_card *card = md->queue.card;
1216 unsigned int from, nr, arg;
1217 int err = 0, type = MMC_BLK_SECDISCARD;
1218 blk_status_t status = BLK_STS_OK;
1220 if (!(mmc_can_secure_erase_trim(card))) {
1221 status = BLK_STS_NOTSUPP;
1225 from = blk_rq_pos(req);
1226 nr = blk_rq_sectors(req);
1228 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1229 arg = MMC_SECURE_TRIM1_ARG;
1231 arg = MMC_SECURE_ERASE_ARG;
1234 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1235 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1236 INAND_CMD38_ARG_EXT_CSD,
1237 arg == MMC_SECURE_TRIM1_ARG ?
1238 INAND_CMD38_ARG_SECTRIM1 :
1239 INAND_CMD38_ARG_SECERASE,
1240 card->ext_csd.generic_cmd6_time);
1245 err = mmc_erase(card, from, nr, arg);
1249 status = BLK_STS_IOERR;
1253 if (arg == MMC_SECURE_TRIM1_ARG) {
1254 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1255 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1256 INAND_CMD38_ARG_EXT_CSD,
1257 INAND_CMD38_ARG_SECTRIM2,
1258 card->ext_csd.generic_cmd6_time);
1263 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1267 status = BLK_STS_IOERR;
1273 if (err && !mmc_blk_reset(md, card->host, type))
1276 mmc_blk_reset_success(md, type);
1278 blk_mq_end_request(req, status);
1281 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1283 struct mmc_blk_data *md = mq->blkdata;
1284 struct mmc_card *card = md->queue.card;
1287 ret = mmc_flush_cache(card->host);
1288 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1292 * Reformat current write as a reliable write, supporting
1293 * both legacy and the enhanced reliable write MMC cards.
1294 * In each transfer we'll handle only as much as a single
1295 * reliable write can handle, thus finish the request in
1296 * partial completions.
1298 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1299 struct mmc_card *card,
1300 struct request *req)
1302 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1303 /* Legacy mode imposes restrictions on transfers. */
1304 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
1305 brq->data.blocks = 1;
1307 if (brq->data.blocks > card->ext_csd.rel_sectors)
1308 brq->data.blocks = card->ext_csd.rel_sectors;
1309 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1310 brq->data.blocks = 1;
1314 #define CMD_ERRORS_EXCL_OOR \
1315 (R1_ADDRESS_ERROR | /* Misaligned address */ \
1316 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1317 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1318 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
1319 R1_CC_ERROR | /* Card controller error */ \
1320 R1_ERROR) /* General/unknown error */
1322 #define CMD_ERRORS \
1323 (CMD_ERRORS_EXCL_OOR | \
1324 R1_OUT_OF_RANGE) /* Command argument out of range */ \
1326 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
1331 * Per the SD specification(physical layer version 4.10)[1],
1332 * section 4.3.3, it explicitly states that "When the last
1333 * block of user area is read using CMD18, the host should
1334 * ignore OUT_OF_RANGE error that may occur even the sequence
1335 * is correct". And JESD84-B51 for eMMC also has a similar
1336 * statement on section 6.8.3.
1338 * Multiple block read/write could be done by either predefined
1339 * method, namely CMD23, or open-ending mode. For open-ending mode,
1340 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1342 * However the spec[1] doesn't tell us whether we should also
1343 * ignore that for predefined method. But per the spec[1], section
1344 * 4.15 Set Block Count Command, it says"If illegal block count
1345 * is set, out of range error will be indicated during read/write
1346 * operation (For example, data transfer is stopped at user area
1347 * boundary)." In another word, we could expect a out of range error
1348 * in the response for the following CMD18/25. And if argument of
1349 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1350 * we could also expect to get a -ETIMEDOUT or any error number from
1351 * the host drivers due to missing data response(for write)/data(for
1352 * read), as the cards will stop the data transfer by itself per the
1353 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1356 if (!brq->stop.error) {
1357 bool oor_with_open_end;
1358 /* If there is no error yet, check R1 response */
1360 val = brq->stop.resp[0] & CMD_ERRORS;
1361 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1363 if (val && !oor_with_open_end)
1364 brq->stop.error = -EIO;
1368 static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
1369 int recovery_mode, bool *do_rel_wr_p,
1370 bool *do_data_tag_p)
1372 struct mmc_blk_data *md = mq->blkdata;
1373 struct mmc_card *card = md->queue.card;
1374 struct mmc_blk_request *brq = &mqrq->brq;
1375 struct request *req = mmc_queue_req_to_req(mqrq);
1376 bool do_rel_wr, do_data_tag;
1379 * Reliable writes are used to implement Forced Unit Access and
1380 * are supported only on MMCs.
1382 do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1383 rq_data_dir(req) == WRITE &&
1384 (md->flags & MMC_BLK_REL_WR);
1386 memset(brq, 0, sizeof(struct mmc_blk_request));
1388 mmc_crypto_prepare_req(mqrq);
1390 brq->mrq.data = &brq->data;
1391 brq->mrq.tag = req->tag;
1393 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1396 if (rq_data_dir(req) == READ) {
1397 brq->data.flags = MMC_DATA_READ;
1398 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1400 brq->data.flags = MMC_DATA_WRITE;
1401 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1404 brq->data.blksz = 512;
1405 brq->data.blocks = blk_rq_sectors(req);
1406 brq->data.blk_addr = blk_rq_pos(req);
1409 * The command queue supports 2 priorities: "high" (1) and "simple" (0).
1410 * The eMMC will give "high" priority tasks priority over "simple"
1411 * priority tasks. Here we always set "simple" priority by not setting
1416 * The block layer doesn't support all sector count
1417 * restrictions, so we need to be prepared for too big
1420 if (brq->data.blocks > card->host->max_blk_count)
1421 brq->data.blocks = card->host->max_blk_count;
1423 if (brq->data.blocks > 1) {
1425 * Some SD cards in SPI mode return a CRC error or even lock up
1426 * completely when trying to read the last block using a
1427 * multiblock read command.
1429 if (mmc_host_is_spi(card->host) && (rq_data_dir(req) == READ) &&
1430 (blk_rq_pos(req) + blk_rq_sectors(req) ==
1431 get_capacity(md->disk)))
1435 * After a read error, we redo the request one (native) sector
1436 * at a time in order to accurately determine which
1437 * sectors can be read successfully.
1440 brq->data.blocks = queue_physical_block_size(mq->queue) >> 9;
1443 * Some controllers have HW issues while operating
1444 * in multiple I/O mode
1446 if (card->host->ops->multi_io_quirk)
1447 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1448 (rq_data_dir(req) == READ) ?
1449 MMC_DATA_READ : MMC_DATA_WRITE,
1454 mmc_apply_rel_rw(brq, card, req);
1455 brq->data.flags |= MMC_DATA_REL_WR;
1459 * Data tag is used only during writing meta data to speed
1460 * up write and any subsequent read of this meta data
1462 do_data_tag = card->ext_csd.data_tag_unit_size &&
1463 (req->cmd_flags & REQ_META) &&
1464 (rq_data_dir(req) == WRITE) &&
1465 ((brq->data.blocks * brq->data.blksz) >=
1466 card->ext_csd.data_tag_unit_size);
1469 brq->data.flags |= MMC_DATA_DAT_TAG;
1471 mmc_set_data_timeout(&brq->data, card);
1473 brq->data.sg = mqrq->sg;
1474 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1477 * Adjust the sg list so it is the same size as the
1480 if (brq->data.blocks != blk_rq_sectors(req)) {
1481 int i, data_size = brq->data.blocks << 9;
1482 struct scatterlist *sg;
1484 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1485 data_size -= sg->length;
1486 if (data_size <= 0) {
1487 sg->length += data_size;
1492 brq->data.sg_len = i;
1496 *do_rel_wr_p = do_rel_wr;
1499 *do_data_tag_p = do_data_tag;
1502 #define MMC_CQE_RETRIES 2
1504 static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req)
1506 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1507 struct mmc_request *mrq = &mqrq->brq.mrq;
1508 struct request_queue *q = req->q;
1509 struct mmc_host *host = mq->card->host;
1510 enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
1511 unsigned long flags;
1515 mmc_cqe_post_req(host, mrq);
1517 if (mrq->cmd && mrq->cmd->error)
1518 err = mrq->cmd->error;
1519 else if (mrq->data && mrq->data->error)
1520 err = mrq->data->error;
1525 if (mqrq->retries++ < MMC_CQE_RETRIES)
1526 blk_mq_requeue_request(req, true);
1528 blk_mq_end_request(req, BLK_STS_IOERR);
1529 } else if (mrq->data) {
1530 if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered))
1531 blk_mq_requeue_request(req, true);
1533 __blk_mq_end_request(req, BLK_STS_OK);
1534 } else if (mq->in_recovery) {
1535 blk_mq_requeue_request(req, true);
1537 blk_mq_end_request(req, BLK_STS_OK);
1540 spin_lock_irqsave(&mq->lock, flags);
1542 mq->in_flight[issue_type] -= 1;
1544 put_card = (mmc_tot_in_flight(mq) == 0);
1546 mmc_cqe_check_busy(mq);
1548 spin_unlock_irqrestore(&mq->lock, flags);
1551 blk_mq_run_hw_queues(q, true);
1554 mmc_put_card(mq->card, &mq->ctx);
1557 void mmc_blk_cqe_recovery(struct mmc_queue *mq)
1559 struct mmc_card *card = mq->card;
1560 struct mmc_host *host = card->host;
1563 pr_debug("%s: CQE recovery start\n", mmc_hostname(host));
1565 err = mmc_cqe_recovery(host);
1567 mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY);
1568 mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY);
1570 pr_debug("%s: CQE recovery done\n", mmc_hostname(host));
1573 static void mmc_blk_cqe_req_done(struct mmc_request *mrq)
1575 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
1577 struct request *req = mmc_queue_req_to_req(mqrq);
1578 struct request_queue *q = req->q;
1579 struct mmc_queue *mq = q->queuedata;
1582 * Block layer timeouts race with completions which means the normal
1583 * completion path cannot be used during recovery.
1585 if (mq->in_recovery)
1586 mmc_blk_cqe_complete_rq(mq, req);
1587 else if (likely(!blk_should_fake_timeout(req->q)))
1588 blk_mq_complete_request(req);
1591 static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
1593 mrq->done = mmc_blk_cqe_req_done;
1594 mrq->recovery_notifier = mmc_cqe_recovery_notifier;
1596 return mmc_cqe_start_req(host, mrq);
1599 static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq,
1600 struct request *req)
1602 struct mmc_blk_request *brq = &mqrq->brq;
1604 memset(brq, 0, sizeof(*brq));
1606 brq->mrq.cmd = &brq->cmd;
1607 brq->mrq.tag = req->tag;
1612 static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
1614 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1615 struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);
1617 mrq->cmd->opcode = MMC_SWITCH;
1618 mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1619 (EXT_CSD_FLUSH_CACHE << 16) |
1621 EXT_CSD_CMD_SET_NORMAL;
1622 mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B;
1624 return mmc_blk_cqe_start_req(mq->card->host, mrq);
1627 static int mmc_blk_hsq_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1629 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1630 struct mmc_host *host = mq->card->host;
1633 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
1634 mqrq->brq.mrq.done = mmc_blk_hsq_req_done;
1635 mmc_pre_req(host, &mqrq->brq.mrq);
1637 err = mmc_cqe_start_req(host, &mqrq->brq.mrq);
1639 mmc_post_req(host, &mqrq->brq.mrq, err);
1644 static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1646 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1647 struct mmc_host *host = mq->card->host;
1649 if (host->hsq_enabled)
1650 return mmc_blk_hsq_issue_rw_rq(mq, req);
1652 mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL);
1654 return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq);
1657 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1658 struct mmc_card *card,
1660 struct mmc_queue *mq)
1662 u32 readcmd, writecmd;
1663 struct mmc_blk_request *brq = &mqrq->brq;
1664 struct request *req = mmc_queue_req_to_req(mqrq);
1665 struct mmc_blk_data *md = mq->blkdata;
1666 bool do_rel_wr, do_data_tag;
1668 mmc_blk_data_prep(mq, mqrq, recovery_mode, &do_rel_wr, &do_data_tag);
1670 brq->mrq.cmd = &brq->cmd;
1672 brq->cmd.arg = blk_rq_pos(req);
1673 if (!mmc_card_blockaddr(card))
1675 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1677 if (brq->data.blocks > 1 || do_rel_wr) {
1678 /* SPI multiblock writes terminate using a special
1679 * token, not a STOP_TRANSMISSION request.
1681 if (!mmc_host_is_spi(card->host) ||
1682 rq_data_dir(req) == READ)
1683 brq->mrq.stop = &brq->stop;
1684 readcmd = MMC_READ_MULTIPLE_BLOCK;
1685 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1687 brq->mrq.stop = NULL;
1688 readcmd = MMC_READ_SINGLE_BLOCK;
1689 writecmd = MMC_WRITE_BLOCK;
1691 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
1694 * Pre-defined multi-block transfers are preferable to
1695 * open ended-ones (and necessary for reliable writes).
1696 * However, it is not sufficient to just send CMD23,
1697 * and avoid the final CMD12, as on an error condition
1698 * CMD12 (stop) needs to be sent anyway. This, coupled
1699 * with Auto-CMD23 enhancements provided by some
1700 * hosts, means that the complexity of dealing
1701 * with this is best left to the host. If CMD23 is
1702 * supported by card and host, we'll fill sbc in and let
1703 * the host deal with handling it correctly. This means
1704 * that for hosts that don't expose MMC_CAP_CMD23, no
1705 * change of behavior will be observed.
1707 * N.B: Some MMC cards experience perf degradation.
1708 * We'll avoid using CMD23-bounded multiblock writes for
1709 * these, while retaining features like reliable writes.
1711 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1712 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1714 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1715 brq->sbc.arg = brq->data.blocks |
1716 (do_rel_wr ? (1 << 31) : 0) |
1717 (do_data_tag ? (1 << 29) : 0);
1718 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1719 brq->mrq.sbc = &brq->sbc;
1723 #define MMC_MAX_RETRIES 5
1724 #define MMC_DATA_RETRIES 2
1725 #define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1)
1727 static int mmc_blk_send_stop(struct mmc_card *card, unsigned int timeout)
1729 struct mmc_command cmd = {
1730 .opcode = MMC_STOP_TRANSMISSION,
1731 .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC,
1732 /* Some hosts wait for busy anyway, so provide a busy timeout */
1733 .busy_timeout = timeout,
1736 return mmc_wait_for_cmd(card->host, &cmd, 5);
1739 static int mmc_blk_fix_state(struct mmc_card *card, struct request *req)
1741 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1742 struct mmc_blk_request *brq = &mqrq->brq;
1743 unsigned int timeout = mmc_blk_data_timeout_ms(card->host, &brq->data);
1746 mmc_retune_hold_now(card->host);
1748 mmc_blk_send_stop(card, timeout);
1750 err = mmc_poll_for_busy(card, timeout, false, MMC_BUSY_IO);
1752 mmc_retune_release(card->host);
1757 #define MMC_READ_SINGLE_RETRIES 2
1759 /* Single (native) sector read during recovery */
1760 static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req)
1762 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1763 struct mmc_request *mrq = &mqrq->brq.mrq;
1764 struct mmc_card *card = mq->card;
1765 struct mmc_host *host = card->host;
1766 blk_status_t error = BLK_STS_OK;
1767 size_t bytes_per_read = queue_physical_block_size(mq->queue);
1774 while (retries++ <= MMC_READ_SINGLE_RETRIES) {
1775 mmc_blk_rw_rq_prep(mqrq, card, 1, mq);
1777 mmc_wait_for_req(host, mrq);
1779 err = mmc_send_status(card, &status);
1783 if (!mmc_host_is_spi(host) &&
1784 !mmc_ready_for_data(status)) {
1785 err = mmc_blk_fix_state(card, req);
1790 if (!mrq->cmd->error)
1794 if (mrq->cmd->error ||
1796 (!mmc_host_is_spi(host) &&
1797 (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS)))
1798 error = BLK_STS_IOERR;
1802 } while (blk_update_request(req, error, bytes_per_read));
1807 mrq->data->bytes_xfered = 0;
1808 blk_update_request(req, BLK_STS_IOERR, bytes_per_read);
1809 /* Let it try the remaining request again */
1810 if (mqrq->retries > MMC_MAX_RETRIES - 1)
1811 mqrq->retries = MMC_MAX_RETRIES - 1;
1814 static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq)
1816 return !!brq->mrq.sbc;
1819 static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq)
1821 return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR;
1825 * Check for errors the host controller driver might not have seen such as
1826 * response mode errors or invalid card state.
1828 static bool mmc_blk_status_error(struct request *req, u32 status)
1830 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1831 struct mmc_blk_request *brq = &mqrq->brq;
1832 struct mmc_queue *mq = req->q->queuedata;
1835 if (mmc_host_is_spi(mq->card->host))
1838 stop_err_bits = mmc_blk_stop_err_bits(brq);
1840 return brq->cmd.resp[0] & CMD_ERRORS ||
1841 brq->stop.resp[0] & stop_err_bits ||
1842 status & stop_err_bits ||
1843 (rq_data_dir(req) == WRITE && !mmc_ready_for_data(status));
1846 static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq)
1848 return !brq->sbc.error && !brq->cmd.error &&
1849 !(brq->cmd.resp[0] & CMD_ERRORS);
1853 * Requests are completed by mmc_blk_mq_complete_rq() which sets simple
1855 * 1. A request that has transferred at least some data is considered
1856 * successful and will be requeued if there is remaining data to
1858 * 2. Otherwise the number of retries is incremented and the request
1859 * will be requeued if there are remaining retries.
1860 * 3. Otherwise the request will be errored out.
1861 * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and
1862 * mqrq->retries. So there are only 4 possible actions here:
1863 * 1. do not accept the bytes_xfered value i.e. set it to zero
1864 * 2. change mqrq->retries to determine the number of retries
1865 * 3. try to reset the card
1866 * 4. read one sector at a time
1868 static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req)
1870 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1871 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1872 struct mmc_blk_request *brq = &mqrq->brq;
1873 struct mmc_blk_data *md = mq->blkdata;
1874 struct mmc_card *card = mq->card;
1880 * Some errors the host driver might not have seen. Set the number of
1881 * bytes transferred to zero in that case.
1883 err = __mmc_send_status(card, &status, 0);
1884 if (err || mmc_blk_status_error(req, status))
1885 brq->data.bytes_xfered = 0;
1887 mmc_retune_release(card->host);
1890 * Try again to get the status. This also provides an opportunity for
1894 err = __mmc_send_status(card, &status, 0);
1897 * Nothing more to do after the number of bytes transferred has been
1898 * updated and there is no card.
1900 if (err && mmc_detect_card_removed(card->host))
1903 /* Try to get back to "tran" state */
1904 if (!mmc_host_is_spi(mq->card->host) &&
1905 (err || !mmc_ready_for_data(status)))
1906 err = mmc_blk_fix_state(mq->card, req);
1909 * Special case for SD cards where the card might record the number of
1912 if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) &&
1913 rq_data_dir(req) == WRITE) {
1914 if (mmc_sd_num_wr_blocks(card, &blocks))
1915 brq->data.bytes_xfered = 0;
1917 brq->data.bytes_xfered = blocks << 9;
1920 /* Reset if the card is in a bad state */
1921 if (!mmc_host_is_spi(mq->card->host) &&
1922 err && mmc_blk_reset(md, card->host, type)) {
1923 pr_err("%s: recovery failed!\n", req->q->disk->disk_name);
1924 mqrq->retries = MMC_NO_RETRIES;
1929 * If anything was done, just return and if there is anything remaining
1930 * on the request it will get requeued.
1932 if (brq->data.bytes_xfered)
1935 /* Reset before last retry */
1936 if (mqrq->retries + 1 == MMC_MAX_RETRIES &&
1937 mmc_blk_reset(md, card->host, type))
1940 /* Command errors fail fast, so use all MMC_MAX_RETRIES */
1941 if (brq->sbc.error || brq->cmd.error)
1944 /* Reduce the remaining retries for data errors */
1945 if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) {
1946 mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES;
1950 if (0 && rq_data_dir(req) == READ && brq->data.blocks >
1951 queue_physical_block_size(mq->queue) >> 9) {
1952 /* Read one (native) sector at a time */
1953 mmc_blk_read_single(mq, req);
1958 static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq)
1960 mmc_blk_eval_resp_error(brq);
1962 return brq->sbc.error || brq->cmd.error || brq->stop.error ||
1963 brq->data.error || brq->cmd.resp[0] & CMD_ERRORS;
1966 static int mmc_spi_err_check(struct mmc_card *card)
1972 * SPI does not have a TRAN state we have to wait on, instead the
1973 * card is ready again when it no longer holds the line LOW.
1974 * We still have to ensure two things here before we know the write
1976 * 1. The card has not disconnected during busy and we actually read our
1977 * own pull-up, thinking it was still connected, so ensure it
1979 * 2. Check for any error bits, in particular R1_SPI_IDLE to catch a
1980 * just reconnected card after being disconnected during busy.
1982 err = __mmc_send_status(card, &status, 0);
1985 /* All R1 and R2 bits of SPI are errors in our case */
1991 static int mmc_blk_busy_cb(void *cb_data, bool *busy)
1993 struct mmc_blk_busy_data *data = cb_data;
1997 err = mmc_send_status(data->card, &status);
2001 /* Accumulate response error bits. */
2002 data->status |= status;
2004 *busy = !mmc_ready_for_data(status);
2008 static int mmc_blk_card_busy(struct mmc_card *card, struct request *req)
2010 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2011 struct mmc_blk_busy_data cb_data;
2014 if (rq_data_dir(req) == READ)
2017 if (mmc_host_is_spi(card->host)) {
2018 err = mmc_spi_err_check(card);
2020 mqrq->brq.data.bytes_xfered = 0;
2024 cb_data.card = card;
2026 err = __mmc_poll_for_busy(card->host, 0, MMC_BLK_TIMEOUT_MS,
2027 &mmc_blk_busy_cb, &cb_data);
2030 * Do not assume data transferred correctly if there are any error bits
2033 if (cb_data.status & mmc_blk_stop_err_bits(&mqrq->brq)) {
2034 mqrq->brq.data.bytes_xfered = 0;
2035 err = err ? err : -EIO;
2038 /* Copy the exception bit so it will be seen later on */
2039 if (mmc_card_mmc(card) && cb_data.status & R1_EXCEPTION_EVENT)
2040 mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
2045 static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
2046 struct request *req)
2048 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
2050 mmc_blk_reset_success(mq->blkdata, type);
2053 static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)
2055 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2056 unsigned int nr_bytes = mqrq->brq.data.bytes_xfered;
2059 if (blk_update_request(req, BLK_STS_OK, nr_bytes))
2060 blk_mq_requeue_request(req, true);
2062 __blk_mq_end_request(req, BLK_STS_OK);
2063 } else if (!blk_rq_bytes(req)) {
2064 __blk_mq_end_request(req, BLK_STS_IOERR);
2065 } else if (mqrq->retries++ < MMC_MAX_RETRIES) {
2066 blk_mq_requeue_request(req, true);
2068 if (mmc_card_removed(mq->card))
2069 req->rq_flags |= RQF_QUIET;
2070 blk_mq_end_request(req, BLK_STS_IOERR);
2074 static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
2075 struct mmc_queue_req *mqrq)
2077 return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) &&
2078 (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
2079 mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
2082 static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
2083 struct mmc_queue_req *mqrq)
2085 if (mmc_blk_urgent_bkops_needed(mq, mqrq))
2086 mmc_run_bkops(mq->card);
2089 static void mmc_blk_hsq_req_done(struct mmc_request *mrq)
2091 struct mmc_queue_req *mqrq =
2092 container_of(mrq, struct mmc_queue_req, brq.mrq);
2093 struct request *req = mmc_queue_req_to_req(mqrq);
2094 struct request_queue *q = req->q;
2095 struct mmc_queue *mq = q->queuedata;
2096 struct mmc_host *host = mq->card->host;
2097 unsigned long flags;
2099 if (mmc_blk_rq_error(&mqrq->brq) ||
2100 mmc_blk_urgent_bkops_needed(mq, mqrq)) {
2101 spin_lock_irqsave(&mq->lock, flags);
2102 mq->recovery_needed = true;
2103 mq->recovery_req = req;
2104 spin_unlock_irqrestore(&mq->lock, flags);
2106 host->cqe_ops->cqe_recovery_start(host);
2108 schedule_work(&mq->recovery_work);
2112 mmc_blk_rw_reset_success(mq, req);
2115 * Block layer timeouts race with completions which means the normal
2116 * completion path cannot be used during recovery.
2118 if (mq->in_recovery)
2119 mmc_blk_cqe_complete_rq(mq, req);
2120 else if (likely(!blk_should_fake_timeout(req->q)))
2121 blk_mq_complete_request(req);
2124 void mmc_blk_mq_complete(struct request *req)
2126 struct mmc_queue *mq = req->q->queuedata;
2127 struct mmc_host *host = mq->card->host;
2129 if (host->cqe_enabled)
2130 mmc_blk_cqe_complete_rq(mq, req);
2131 else if (likely(!blk_should_fake_timeout(req->q)))
2132 mmc_blk_mq_complete_rq(mq, req);
2135 static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,
2136 struct request *req)
2138 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2139 struct mmc_host *host = mq->card->host;
2141 if (mmc_blk_rq_error(&mqrq->brq) ||
2142 mmc_blk_card_busy(mq->card, req)) {
2143 mmc_blk_mq_rw_recovery(mq, req);
2145 mmc_blk_rw_reset_success(mq, req);
2146 mmc_retune_release(host);
2149 mmc_blk_urgent_bkops(mq, mqrq);
2152 static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, enum mmc_issue_type issue_type)
2154 unsigned long flags;
2157 spin_lock_irqsave(&mq->lock, flags);
2159 mq->in_flight[issue_type] -= 1;
2161 put_card = (mmc_tot_in_flight(mq) == 0);
2163 spin_unlock_irqrestore(&mq->lock, flags);
2166 mmc_put_card(mq->card, &mq->ctx);
2169 static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req,
2172 enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
2173 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2174 struct mmc_request *mrq = &mqrq->brq.mrq;
2175 struct mmc_host *host = mq->card->host;
2177 mmc_post_req(host, mrq, 0);
2180 * Block layer timeouts race with completions which means the normal
2181 * completion path cannot be used during recovery.
2183 if (mq->in_recovery) {
2184 mmc_blk_mq_complete_rq(mq, req);
2185 } else if (likely(!blk_should_fake_timeout(req->q))) {
2187 blk_mq_complete_request_direct(req, mmc_blk_mq_complete);
2189 blk_mq_complete_request(req);
2192 mmc_blk_mq_dec_in_flight(mq, issue_type);
2195 void mmc_blk_mq_recovery(struct mmc_queue *mq)
2197 struct request *req = mq->recovery_req;
2198 struct mmc_host *host = mq->card->host;
2199 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2201 mq->recovery_req = NULL;
2202 mq->rw_wait = false;
2204 if (mmc_blk_rq_error(&mqrq->brq)) {
2205 mmc_retune_hold_now(host);
2206 mmc_blk_mq_rw_recovery(mq, req);
2209 mmc_blk_urgent_bkops(mq, mqrq);
2211 mmc_blk_mq_post_req(mq, req, true);
2214 static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq,
2215 struct request **prev_req)
2217 if (mmc_host_done_complete(mq->card->host))
2220 mutex_lock(&mq->complete_lock);
2222 if (!mq->complete_req)
2225 mmc_blk_mq_poll_completion(mq, mq->complete_req);
2228 *prev_req = mq->complete_req;
2230 mmc_blk_mq_post_req(mq, mq->complete_req, true);
2232 mq->complete_req = NULL;
2235 mutex_unlock(&mq->complete_lock);
2238 void mmc_blk_mq_complete_work(struct work_struct *work)
2240 struct mmc_queue *mq = container_of(work, struct mmc_queue,
2243 mmc_blk_mq_complete_prev_req(mq, NULL);
2246 static void mmc_blk_mq_req_done(struct mmc_request *mrq)
2248 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
2250 struct request *req = mmc_queue_req_to_req(mqrq);
2251 struct request_queue *q = req->q;
2252 struct mmc_queue *mq = q->queuedata;
2253 struct mmc_host *host = mq->card->host;
2254 unsigned long flags;
2256 if (!mmc_host_done_complete(host)) {
2260 * We cannot complete the request in this context, so record
2261 * that there is a request to complete, and that a following
2262 * request does not need to wait (although it does need to
2263 * complete complete_req first).
2265 spin_lock_irqsave(&mq->lock, flags);
2266 mq->complete_req = req;
2267 mq->rw_wait = false;
2268 waiting = mq->waiting;
2269 spin_unlock_irqrestore(&mq->lock, flags);
2272 * If 'waiting' then the waiting task will complete this
2273 * request, otherwise queue a work to do it. Note that
2274 * complete_work may still race with the dispatch of a following
2280 queue_work(mq->card->complete_wq, &mq->complete_work);
2285 /* Take the recovery path for errors or urgent background operations */
2286 if (mmc_blk_rq_error(&mqrq->brq) ||
2287 mmc_blk_urgent_bkops_needed(mq, mqrq)) {
2288 spin_lock_irqsave(&mq->lock, flags);
2289 mq->recovery_needed = true;
2290 mq->recovery_req = req;
2291 spin_unlock_irqrestore(&mq->lock, flags);
2293 schedule_work(&mq->recovery_work);
2297 mmc_blk_rw_reset_success(mq, req);
2299 mq->rw_wait = false;
2302 /* context unknown */
2303 mmc_blk_mq_post_req(mq, req, false);
2306 static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err)
2308 unsigned long flags;
2312 * Wait while there is another request in progress, but not if recovery
2313 * is needed. Also indicate whether there is a request waiting to start.
2315 spin_lock_irqsave(&mq->lock, flags);
2316 if (mq->recovery_needed) {
2320 done = !mq->rw_wait;
2322 mq->waiting = !done;
2323 spin_unlock_irqrestore(&mq->lock, flags);
2328 static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req)
2332 wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err));
2334 /* Always complete the previous request if there is one */
2335 mmc_blk_mq_complete_prev_req(mq, prev_req);
2340 static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq,
2341 struct request *req)
2343 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2344 struct mmc_host *host = mq->card->host;
2345 struct request *prev_req = NULL;
2348 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
2350 mqrq->brq.mrq.done = mmc_blk_mq_req_done;
2352 mmc_pre_req(host, &mqrq->brq.mrq);
2354 err = mmc_blk_rw_wait(mq, &prev_req);
2360 err = mmc_start_request(host, &mqrq->brq.mrq);
2363 mmc_blk_mq_post_req(mq, prev_req, true);
2366 mq->rw_wait = false;
2368 /* Release re-tuning here where there is no synchronization required */
2369 if (err || mmc_host_done_complete(host))
2370 mmc_retune_release(host);
2374 mmc_post_req(host, &mqrq->brq.mrq, err);
2379 static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host)
2381 if (host->cqe_enabled)
2382 return host->cqe_ops->cqe_wait_for_idle(host);
2384 return mmc_blk_rw_wait(mq, NULL);
2387 enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
2389 struct mmc_blk_data *md = mq->blkdata;
2390 struct mmc_card *card = md->queue.card;
2391 struct mmc_host *host = card->host;
2394 ret = mmc_blk_part_switch(card, md->part_type);
2396 return MMC_REQ_FAILED_TO_START;
2398 switch (mmc_issue_type(mq, req)) {
2399 case MMC_ISSUE_SYNC:
2400 ret = mmc_blk_wait_for_idle(mq, host);
2402 return MMC_REQ_BUSY;
2403 switch (req_op(req)) {
2405 case REQ_OP_DRV_OUT:
2406 mmc_blk_issue_drv_op(mq, req);
2408 case REQ_OP_DISCARD:
2409 mmc_blk_issue_discard_rq(mq, req);
2411 case REQ_OP_SECURE_ERASE:
2412 mmc_blk_issue_secdiscard_rq(mq, req);
2414 case REQ_OP_WRITE_ZEROES:
2415 mmc_blk_issue_trim_rq(mq, req);
2418 mmc_blk_issue_flush(mq, req);
2422 return MMC_REQ_FAILED_TO_START;
2424 return MMC_REQ_FINISHED;
2425 case MMC_ISSUE_DCMD:
2426 case MMC_ISSUE_ASYNC:
2427 switch (req_op(req)) {
2429 if (!mmc_cache_enabled(host)) {
2430 blk_mq_end_request(req, BLK_STS_OK);
2431 return MMC_REQ_FINISHED;
2433 ret = mmc_blk_cqe_issue_flush(mq, req);
2436 card->written_flag = true;
2439 if (host->cqe_enabled)
2440 ret = mmc_blk_cqe_issue_rw_rq(mq, req);
2442 ret = mmc_blk_mq_issue_rw_rq(mq, req);
2449 return MMC_REQ_STARTED;
2450 return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START;
2453 return MMC_REQ_FAILED_TO_START;
2457 static inline int mmc_blk_readonly(struct mmc_card *card)
2459 return mmc_card_readonly(card) ||
2460 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2463 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2464 struct device *parent,
2467 const char *subname,
2469 unsigned int part_type)
2471 struct mmc_blk_data *md;
2474 bool cache_enabled = false;
2475 bool fua_enabled = false;
2477 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
2480 * We get -ENOSPC because there are no more any available
2481 * devidx. The reason may be that, either userspace haven't yet
2482 * unmounted the partitions, which postpones mmc_blk_release()
2483 * from being called, or the device has more partitions than
2486 if (devidx == -ENOSPC)
2487 dev_err(mmc_dev(card->host),
2488 "no more device IDs available\n");
2490 return ERR_PTR(devidx);
2493 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2499 md->area_type = area_type;
2502 * Set the read-only status based on the supported commands
2503 * and the write protect switch.
2505 md->read_only = mmc_blk_readonly(card);
2507 md->disk = mmc_init_queue(&md->queue, card);
2508 if (IS_ERR(md->disk)) {
2509 ret = PTR_ERR(md->disk);
2513 INIT_LIST_HEAD(&md->part);
2514 INIT_LIST_HEAD(&md->rpmbs);
2515 kref_init(&md->kref);
2517 md->queue.blkdata = md;
2518 md->part_type = part_type;
2520 md->disk->major = MMC_BLOCK_MAJOR;
2521 md->disk->minors = perdev_minors;
2522 md->disk->first_minor = devidx * perdev_minors;
2523 md->disk->fops = &mmc_bdops;
2524 md->disk->private_data = md;
2525 md->parent = parent;
2526 set_disk_ro(md->disk, md->read_only || default_ro);
2527 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2528 md->disk->flags |= GENHD_FL_NO_PART;
2531 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2533 * - be set for removable media with permanent block devices
2534 * - be unset for removable block devices with permanent media
2536 * Since MMC block devices clearly fall under the second
2537 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2538 * should use the block device creation/destruction hotplug
2539 * messages to tell when the card is present.
2542 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2543 "mmcblk%u%s", card->host->index, subname ? subname : "");
2545 set_capacity(md->disk, size);
2547 if (mmc_host_cmd23(card->host)) {
2548 if ((mmc_card_mmc(card) &&
2549 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
2550 (mmc_card_sd(card) &&
2551 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2552 md->flags |= MMC_BLK_CMD23;
2555 if (md->flags & MMC_BLK_CMD23 &&
2556 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2557 card->ext_csd.rel_sectors)) {
2558 md->flags |= MMC_BLK_REL_WR;
2560 cache_enabled = true;
2562 if (mmc_cache_enabled(card->host))
2563 cache_enabled = true;
2565 blk_queue_write_cache(md->queue.queue, cache_enabled, fua_enabled);
2567 string_get_size((u64)size, 512, STRING_UNITS_2,
2568 cap_str, sizeof(cap_str));
2569 pr_info("%s: %s %s %s%s\n",
2570 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2571 cap_str, md->read_only ? " (ro)" : "");
2573 /* used in ->open, must be set before add_disk: */
2574 if (area_type == MMC_BLK_DATA_AREA_MAIN)
2575 dev_set_drvdata(&card->dev, md);
2576 ret = device_add_disk(md->parent, md->disk, mmc_disk_attr_groups);
2583 blk_mq_free_tag_set(&md->queue.tag_set);
2587 ida_simple_remove(&mmc_blk_ida, devidx);
2588 return ERR_PTR(ret);
2591 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2595 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2597 * The EXT_CSD sector count is in number or 512 byte
2600 size = card->ext_csd.sectors;
2603 * The CSD capacity field is in units of read_blkbits.
2604 * set_capacity takes units of 512 bytes.
2606 size = (typeof(sector_t))card->csd.capacity
2607 << (card->csd.read_blkbits - 9);
2610 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2611 MMC_BLK_DATA_AREA_MAIN, 0);
2614 static int mmc_blk_alloc_part(struct mmc_card *card,
2615 struct mmc_blk_data *md,
2616 unsigned int part_type,
2619 const char *subname,
2622 struct mmc_blk_data *part_md;
2624 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2625 subname, area_type, part_type);
2626 if (IS_ERR(part_md))
2627 return PTR_ERR(part_md);
2628 list_add(&part_md->part, &md->part);
2634 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2635 * @filp: the character device file
2636 * @cmd: the ioctl() command
2637 * @arg: the argument from userspace
2639 * This will essentially just redirect the ioctl()s coming in over to
2640 * the main block device spawning the RPMB character device.
2642 static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
2645 struct mmc_rpmb_data *rpmb = filp->private_data;
2650 ret = mmc_blk_ioctl_cmd(rpmb->md,
2651 (struct mmc_ioc_cmd __user *)arg,
2654 case MMC_IOC_MULTI_CMD:
2655 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
2656 (struct mmc_ioc_multi_cmd __user *)arg,
2667 #ifdef CONFIG_COMPAT
2668 static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
2671 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
2675 static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
2677 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2678 struct mmc_rpmb_data, chrdev);
2680 get_device(&rpmb->dev);
2681 filp->private_data = rpmb;
2682 mmc_blk_get(rpmb->md->disk);
2684 return nonseekable_open(inode, filp);
2687 static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
2689 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2690 struct mmc_rpmb_data, chrdev);
2692 mmc_blk_put(rpmb->md);
2693 put_device(&rpmb->dev);
2698 static const struct file_operations mmc_rpmb_fileops = {
2699 .release = mmc_rpmb_chrdev_release,
2700 .open = mmc_rpmb_chrdev_open,
2701 .owner = THIS_MODULE,
2702 .llseek = no_llseek,
2703 .unlocked_ioctl = mmc_rpmb_ioctl,
2704 #ifdef CONFIG_COMPAT
2705 .compat_ioctl = mmc_rpmb_ioctl_compat,
2709 static void mmc_blk_rpmb_device_release(struct device *dev)
2711 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2713 ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2717 static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
2718 struct mmc_blk_data *md,
2719 unsigned int part_index,
2721 const char *subname)
2724 char rpmb_name[DISK_NAME_LEN];
2726 struct mmc_rpmb_data *rpmb;
2728 /* This creates the minor number for the RPMB char device */
2729 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
2733 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
2735 ida_simple_remove(&mmc_rpmb_ida, devidx);
2739 snprintf(rpmb_name, sizeof(rpmb_name),
2740 "mmcblk%u%s", card->host->index, subname ? subname : "");
2743 rpmb->part_index = part_index;
2744 rpmb->dev.init_name = rpmb_name;
2745 rpmb->dev.bus = &mmc_rpmb_bus_type;
2746 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
2747 rpmb->dev.parent = &card->dev;
2748 rpmb->dev.release = mmc_blk_rpmb_device_release;
2749 device_initialize(&rpmb->dev);
2750 dev_set_drvdata(&rpmb->dev, rpmb);
2753 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
2754 rpmb->chrdev.owner = THIS_MODULE;
2755 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
2757 pr_err("%s: could not add character device\n", rpmb_name);
2758 goto out_put_device;
2761 list_add(&rpmb->node, &md->rpmbs);
2763 string_get_size((u64)size, 512, STRING_UNITS_2,
2764 cap_str, sizeof(cap_str));
2766 pr_info("%s: %s %s %s, chardev (%d:%d)\n",
2767 rpmb_name, mmc_card_id(card), mmc_card_name(card), cap_str,
2768 MAJOR(mmc_rpmb_devt), rpmb->id);
2773 put_device(&rpmb->dev);
2777 static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
2780 cdev_device_del(&rpmb->chrdev, &rpmb->dev);
2781 put_device(&rpmb->dev);
2784 /* MMC Physical partitions consist of two boot partitions and
2785 * up to four general purpose partitions.
2786 * For each partition enabled in EXT_CSD a block device will be allocatedi
2787 * to provide access to the partition.
2790 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2794 if (!mmc_card_mmc(card))
2797 for (idx = 0; idx < card->nr_parts; idx++) {
2798 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
2800 * RPMB partitions does not provide block access, they
2801 * are only accessed using ioctl():s. Thus create
2802 * special RPMB block devices that do not have a
2803 * backing block queue for these.
2805 ret = mmc_blk_alloc_rpmb_part(card, md,
2806 card->part[idx].part_cfg,
2807 card->part[idx].size >> 9,
2808 card->part[idx].name);
2811 } else if (card->part[idx].size) {
2812 ret = mmc_blk_alloc_part(card, md,
2813 card->part[idx].part_cfg,
2814 card->part[idx].size >> 9,
2815 card->part[idx].force_ro,
2816 card->part[idx].name,
2817 card->part[idx].area_type);
2826 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2829 * Flush remaining requests and free queues. It is freeing the queue
2830 * that stops new requests from being accepted.
2832 del_gendisk(md->disk);
2833 mmc_cleanup_queue(&md->queue);
2837 static void mmc_blk_remove_parts(struct mmc_card *card,
2838 struct mmc_blk_data *md)
2840 struct list_head *pos, *q;
2841 struct mmc_blk_data *part_md;
2842 struct mmc_rpmb_data *rpmb;
2844 /* Remove RPMB partitions */
2845 list_for_each_safe(pos, q, &md->rpmbs) {
2846 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
2848 mmc_blk_remove_rpmb_part(rpmb);
2850 /* Remove block partitions */
2851 list_for_each_safe(pos, q, &md->part) {
2852 part_md = list_entry(pos, struct mmc_blk_data, part);
2854 mmc_blk_remove_req(part_md);
2858 #ifdef CONFIG_DEBUG_FS
2860 static int mmc_dbg_card_status_get(void *data, u64 *val)
2862 struct mmc_card *card = data;
2863 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2864 struct mmc_queue *mq = &md->queue;
2865 struct request *req;
2868 /* Ask the block layer about the card status */
2869 req = blk_mq_alloc_request(mq->queue, REQ_OP_DRV_IN, 0);
2871 return PTR_ERR(req);
2872 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2873 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
2874 blk_execute_rq(req, false);
2875 ret = req_to_mmc_queue_req(req)->drv_op_result;
2880 blk_mq_free_request(req);
2884 DEFINE_DEBUGFS_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2887 /* That is two digits * 512 + 1 for newline */
2888 #define EXT_CSD_STR_LEN 1025
2890 static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2892 struct mmc_card *card = inode->i_private;
2893 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2894 struct mmc_queue *mq = &md->queue;
2895 struct request *req;
2901 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2905 /* Ask the block layer for the EXT CSD */
2906 req = blk_mq_alloc_request(mq->queue, REQ_OP_DRV_IN, 0);
2911 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2912 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
2913 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2914 blk_execute_rq(req, false);
2915 err = req_to_mmc_queue_req(req)->drv_op_result;
2916 blk_mq_free_request(req);
2918 pr_err("FAILED %d\n", err);
2922 for (i = 0; i < 512; i++)
2923 n += sprintf(buf + n, "%02x", ext_csd[i]);
2924 n += sprintf(buf + n, "\n");
2926 if (n != EXT_CSD_STR_LEN) {
2932 filp->private_data = buf;
2941 static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2942 size_t cnt, loff_t *ppos)
2944 char *buf = filp->private_data;
2946 return simple_read_from_buffer(ubuf, cnt, ppos,
2947 buf, EXT_CSD_STR_LEN);
2950 static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2952 kfree(file->private_data);
2956 static const struct file_operations mmc_dbg_ext_csd_fops = {
2957 .open = mmc_ext_csd_open,
2958 .read = mmc_ext_csd_read,
2959 .release = mmc_ext_csd_release,
2960 .llseek = default_llseek,
2963 static void mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2965 struct dentry *root;
2967 if (!card->debugfs_root)
2970 root = card->debugfs_root;
2972 if (mmc_card_mmc(card) || mmc_card_sd(card)) {
2974 debugfs_create_file_unsafe("status", 0400, root,
2976 &mmc_dbg_card_status_fops);
2979 if (mmc_card_mmc(card)) {
2980 md->ext_csd_dentry =
2981 debugfs_create_file("ext_csd", S_IRUSR, root, card,
2982 &mmc_dbg_ext_csd_fops);
2986 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2987 struct mmc_blk_data *md)
2989 if (!card->debugfs_root)
2992 debugfs_remove(md->status_dentry);
2993 md->status_dentry = NULL;
2995 debugfs_remove(md->ext_csd_dentry);
2996 md->ext_csd_dentry = NULL;
3001 static void mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
3005 static void mmc_blk_remove_debugfs(struct mmc_card *card,
3006 struct mmc_blk_data *md)
3010 #endif /* CONFIG_DEBUG_FS */
3012 static int mmc_blk_probe(struct mmc_card *card)
3014 struct mmc_blk_data *md;
3020 * Check that the card supports the command class(es) we need.
3022 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
3026 unsigned long quirks;
3027 if (kstrtoul(card_quirks, 0, &quirks) == 0)
3028 card->quirks = (unsigned int)quirks;
3030 pr_err("mmc_block: Invalid card_quirks parameter '%s'\n",
3034 mmc_fixup_device(card, mmc_blk_fixups);
3036 card->complete_wq = alloc_workqueue("mmc_complete",
3037 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
3038 if (!card->complete_wq) {
3039 pr_err("Failed to create mmc completion workqueue");
3043 md = mmc_blk_alloc(card);
3049 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
3050 cap_str, sizeof(cap_str));
3052 snprintf(quirk_str, sizeof(quirk_str),
3053 " (quirks 0x%08x)", card->quirks);
3055 quirk_str[0] = '\0';
3056 pr_info("%s: %s %s %s%s%s\n",
3057 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
3058 cap_str, md->read_only ? " (ro)" : "", quirk_str);
3060 ret = mmc_blk_alloc_parts(card, md);
3064 /* Add two debugfs entries */
3065 mmc_blk_add_debugfs(card, md);
3067 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
3068 pm_runtime_use_autosuspend(&card->dev);
3071 * Don't enable runtime PM for SD-combo cards here. Leave that
3072 * decision to be taken during the SDIO init sequence instead.
3074 if (!mmc_card_sd_combo(card)) {
3075 pm_runtime_set_active(&card->dev);
3076 pm_runtime_enable(&card->dev);
3082 mmc_blk_remove_parts(card, md);
3083 mmc_blk_remove_req(md);
3085 destroy_workqueue(card->complete_wq);
3089 static void mmc_blk_remove(struct mmc_card *card)
3091 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
3093 mmc_blk_remove_debugfs(card, md);
3094 mmc_blk_remove_parts(card, md);
3095 pm_runtime_get_sync(&card->dev);
3096 if (md->part_curr != md->part_type) {
3097 mmc_claim_host(card->host);
3098 mmc_blk_part_switch(card, md->part_type);
3099 mmc_release_host(card->host);
3101 if (!mmc_card_sd_combo(card))
3102 pm_runtime_disable(&card->dev);
3103 pm_runtime_put_noidle(&card->dev);
3104 mmc_blk_remove_req(md);
3105 destroy_workqueue(card->complete_wq);
3108 static int _mmc_blk_suspend(struct mmc_card *card)
3110 struct mmc_blk_data *part_md;
3111 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
3114 mmc_queue_suspend(&md->queue);
3115 list_for_each_entry(part_md, &md->part, part) {
3116 mmc_queue_suspend(&part_md->queue);
3122 static void mmc_blk_shutdown(struct mmc_card *card)
3124 _mmc_blk_suspend(card);
3127 #ifdef CONFIG_PM_SLEEP
3128 static int mmc_blk_suspend(struct device *dev)
3130 struct mmc_card *card = mmc_dev_to_card(dev);
3132 return _mmc_blk_suspend(card);
3135 static int mmc_blk_resume(struct device *dev)
3137 struct mmc_blk_data *part_md;
3138 struct mmc_blk_data *md = dev_get_drvdata(dev);
3142 * Resume involves the card going into idle state,
3143 * so current partition is always the main one.
3145 md->part_curr = md->part_type;
3146 mmc_queue_resume(&md->queue);
3147 list_for_each_entry(part_md, &md->part, part) {
3148 mmc_queue_resume(&part_md->queue);
3155 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3157 static struct mmc_driver mmc_driver = {
3160 .pm = &mmc_blk_pm_ops,
3162 .probe = mmc_blk_probe,
3163 .remove = mmc_blk_remove,
3164 .shutdown = mmc_blk_shutdown,
3167 static int __init mmc_blk_init(void)
3171 res = bus_register(&mmc_rpmb_bus_type);
3173 pr_err("mmcblk: could not register RPMB bus type\n");
3176 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
3178 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
3182 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3183 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3185 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
3187 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3189 goto out_chrdev_unreg;
3191 res = mmc_register_driver(&mmc_driver);
3193 goto out_blkdev_unreg;
3198 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3200 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3202 bus_unregister(&mmc_rpmb_bus_type);
3206 static void __exit mmc_blk_exit(void)
3208 mmc_unregister_driver(&mmc_driver);
3209 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3210 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3211 bus_unregister(&mmc_rpmb_bus_type);
3214 module_init(mmc_blk_init);
3215 module_exit(mmc_blk_exit);
3217 MODULE_LICENSE("GPL");
3218 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");