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/blkdev.h>
32 #include <linux/cdev.h>
33 #include <linux/mutex.h>
34 #include <linux/scatterlist.h>
35 #include <linux/string_helpers.h>
36 #include <linux/delay.h>
37 #include <linux/capability.h>
38 #include <linux/compat.h>
39 #include <linux/pm_runtime.h>
40 #include <linux/idr.h>
41 #include <linux/debugfs.h>
43 #include <linux/mmc/ioctl.h>
44 #include <linux/mmc/card.h>
45 #include <linux/mmc/host.h>
46 #include <linux/mmc/mmc.h>
47 #include <linux/mmc/sd.h>
49 #include <linux/uaccess.h>
62 MODULE_ALIAS("mmc:block");
63 #ifdef MODULE_PARAM_PREFIX
64 #undef MODULE_PARAM_PREFIX
66 #define MODULE_PARAM_PREFIX "mmcblk."
69 * Set a 10 second timeout for polling write request busy state. Note, mmc core
70 * is setting a 3 second timeout for SD cards, and SDHCI has long had a 10
71 * second software timer to timeout the whole request, so 10 seconds should be
74 #define MMC_BLK_TIMEOUT_MS (10 * 1000)
75 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
76 #define MMC_EXTRACT_VALUE_FROM_ARG(x) ((x & 0x0000FF00) >> 8)
78 #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
79 (rq_data_dir(req) == WRITE))
80 static DEFINE_MUTEX(block_mutex);
83 * The defaults come from config options but can be overriden by module
86 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
89 * We've only got one major, so number of mmcblk devices is
90 * limited to (1 << 20) / number of minors per device. It is also
91 * limited by the MAX_DEVICES below.
93 static int max_devices;
95 #define MAX_DEVICES 256
97 static DEFINE_IDA(mmc_blk_ida);
98 static DEFINE_IDA(mmc_rpmb_ida);
101 * There is one mmc_blk_data per slot.
103 struct mmc_blk_data {
104 struct device *parent;
105 struct gendisk *disk;
106 struct mmc_queue queue;
107 struct list_head part;
108 struct list_head rpmbs;
111 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
112 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
115 unsigned int read_only;
116 unsigned int part_type;
117 unsigned int reset_done;
118 #define MMC_BLK_READ BIT(0)
119 #define MMC_BLK_WRITE BIT(1)
120 #define MMC_BLK_DISCARD BIT(2)
121 #define MMC_BLK_SECDISCARD BIT(3)
122 #define MMC_BLK_CQE_RECOVERY BIT(4)
125 * Only set in main mmc_blk_data associated
126 * with mmc_card with dev_set_drvdata, and keeps
127 * track of the current selected device partition.
129 unsigned int part_curr;
130 struct device_attribute force_ro;
131 struct device_attribute power_ro_lock;
134 /* debugfs files (only in main mmc_blk_data) */
135 struct dentry *status_dentry;
136 struct dentry *ext_csd_dentry;
139 /* Device type for RPMB character devices */
140 static dev_t mmc_rpmb_devt;
142 /* Bus type for RPMB character devices */
143 static struct bus_type mmc_rpmb_bus_type = {
148 * struct mmc_rpmb_data - special RPMB device type for these areas
149 * @dev: the device for the RPMB area
150 * @chrdev: character device for the RPMB area
151 * @id: unique device ID number
152 * @part_index: partition index (0 on first)
153 * @md: parent MMC block device
154 * @node: list item, so we can put this device on a list
156 struct mmc_rpmb_data {
160 unsigned int part_index;
161 struct mmc_blk_data *md;
162 struct list_head node;
165 static DEFINE_MUTEX(open_lock);
167 module_param(perdev_minors, int, 0444);
168 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
170 static inline int mmc_blk_part_switch(struct mmc_card *card,
171 unsigned int part_type);
172 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
173 struct mmc_card *card,
175 struct mmc_queue *mq);
176 static void mmc_blk_hsq_req_done(struct mmc_request *mrq);
178 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
180 struct mmc_blk_data *md;
182 mutex_lock(&open_lock);
183 md = disk->private_data;
184 if (md && md->usage == 0)
188 mutex_unlock(&open_lock);
193 static inline int mmc_get_devidx(struct gendisk *disk)
195 int devidx = disk->first_minor / perdev_minors;
199 static void mmc_blk_put(struct mmc_blk_data *md)
201 mutex_lock(&open_lock);
203 if (md->usage == 0) {
204 int devidx = mmc_get_devidx(md->disk);
205 blk_put_queue(md->queue.queue);
206 ida_simple_remove(&mmc_blk_ida, devidx);
210 mutex_unlock(&open_lock);
213 static ssize_t power_ro_lock_show(struct device *dev,
214 struct device_attribute *attr, char *buf)
217 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
218 struct mmc_card *card = md->queue.card;
221 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
223 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
226 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
233 static ssize_t power_ro_lock_store(struct device *dev,
234 struct device_attribute *attr, const char *buf, size_t count)
237 struct mmc_blk_data *md, *part_md;
238 struct mmc_queue *mq;
242 if (kstrtoul(buf, 0, &set))
248 md = mmc_blk_get(dev_to_disk(dev));
251 /* Dispatch locking to the block layer */
252 req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, 0);
254 count = PTR_ERR(req);
257 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
258 blk_execute_rq(NULL, req, 0);
259 ret = req_to_mmc_queue_req(req)->drv_op_result;
260 blk_put_request(req);
263 pr_info("%s: Locking boot partition ro until next power on\n",
264 md->disk->disk_name);
265 set_disk_ro(md->disk, 1);
267 list_for_each_entry(part_md, &md->part, part)
268 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
269 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
270 set_disk_ro(part_md->disk, 1);
278 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
282 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
284 ret = snprintf(buf, PAGE_SIZE, "%d\n",
285 get_disk_ro(dev_to_disk(dev)) ^
291 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
292 const char *buf, size_t count)
296 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
297 unsigned long set = simple_strtoul(buf, &end, 0);
303 set_disk_ro(dev_to_disk(dev), set || md->read_only);
310 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
312 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
315 mutex_lock(&block_mutex);
318 if ((mode & FMODE_WRITE) && md->read_only) {
323 mutex_unlock(&block_mutex);
328 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
330 struct mmc_blk_data *md = disk->private_data;
332 mutex_lock(&block_mutex);
334 mutex_unlock(&block_mutex);
338 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
340 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
346 struct mmc_blk_ioc_data {
347 struct mmc_ioc_cmd ic;
350 struct mmc_rpmb_data *rpmb;
353 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
354 struct mmc_ioc_cmd __user *user)
356 struct mmc_blk_ioc_data *idata;
359 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
365 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
370 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
371 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
376 if (!idata->buf_bytes) {
381 idata->buf = memdup_user((void __user *)(unsigned long)
382 idata->ic.data_ptr, idata->buf_bytes);
383 if (IS_ERR(idata->buf)) {
384 err = PTR_ERR(idata->buf);
396 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
397 struct mmc_blk_ioc_data *idata)
399 struct mmc_ioc_cmd *ic = &idata->ic;
401 if (copy_to_user(&(ic_ptr->response), ic->response,
402 sizeof(ic->response)))
405 if (!idata->ic.write_flag) {
406 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
407 idata->buf, idata->buf_bytes))
414 static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
417 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
422 bool done = time_after(jiffies, timeout);
424 err = __mmc_send_status(card, &status, 5);
426 dev_err(mmc_dev(card->host),
427 "error %d requesting status\n", err);
431 /* Accumulate any response error bits seen */
433 *resp_errs |= status;
436 * Timeout if the device never becomes ready for data and never
437 * leaves the program state.
440 dev_err(mmc_dev(card->host),
441 "Card stuck in wrong state! %s status: %#x\n",
445 } while (!mmc_ready_for_data(status));
450 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
451 struct mmc_blk_ioc_data *idata)
453 struct mmc_command cmd = {}, sbc = {};
454 struct mmc_data data = {};
455 struct mmc_request mrq = {};
456 struct scatterlist sg;
458 unsigned int target_part;
460 if (!card || !md || !idata)
464 * The RPMB accesses comes in from the character device, so we
465 * need to target these explicitly. Else we just target the
466 * partition type for the block device the ioctl() was issued
470 /* Support multiple RPMB partitions */
471 target_part = idata->rpmb->part_index;
472 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
474 target_part = md->part_type;
477 cmd.opcode = idata->ic.opcode;
478 cmd.arg = idata->ic.arg;
479 cmd.flags = idata->ic.flags;
481 if (idata->buf_bytes) {
484 data.blksz = idata->ic.blksz;
485 data.blocks = idata->ic.blocks;
487 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
489 if (idata->ic.write_flag)
490 data.flags = MMC_DATA_WRITE;
492 data.flags = MMC_DATA_READ;
494 /* data.flags must already be set before doing this. */
495 mmc_set_data_timeout(&data, card);
497 /* Allow overriding the timeout_ns for empirical tuning. */
498 if (idata->ic.data_timeout_ns)
499 data.timeout_ns = idata->ic.data_timeout_ns;
501 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
503 * Pretend this is a data transfer and rely on the
504 * host driver to compute timeout. When all host
505 * drivers support cmd.cmd_timeout for R1B, this
509 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
511 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
519 err = mmc_blk_part_switch(card, target_part);
523 if (idata->ic.is_acmd) {
524 err = mmc_app_cmd(card->host, card);
530 sbc.opcode = MMC_SET_BLOCK_COUNT;
532 * We don't do any blockcount validation because the max size
533 * may be increased by a future standard. We just copy the
534 * 'Reliable Write' bit here.
536 sbc.arg = data.blocks | (idata->ic.write_flag & BIT(31));
537 sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
541 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
542 (cmd.opcode == MMC_SWITCH))
543 return mmc_sanitize(card, idata->ic.cmd_timeout_ms);
545 mmc_wait_for_req(card->host, &mrq);
548 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
549 __func__, cmd.error);
553 dev_err(mmc_dev(card->host), "%s: data error %d\n",
554 __func__, data.error);
559 * Make sure the cache of the PARTITION_CONFIG register and
560 * PARTITION_ACCESS bits is updated in case the ioctl ext_csd write
561 * changed it successfully.
563 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_PART_CONFIG) &&
564 (cmd.opcode == MMC_SWITCH)) {
565 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
566 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg);
569 * Update cache so the next mmc_blk_part_switch call operates
570 * on up-to-date data.
572 card->ext_csd.part_config = value;
573 main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
577 * Make sure to update CACHE_CTRL in case it was changed. The cache
578 * will get turned back on if the card is re-initialized, e.g.
579 * suspend/resume or hw reset in recovery.
581 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_CACHE_CTRL) &&
582 (cmd.opcode == MMC_SWITCH)) {
583 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg) & 1;
585 card->ext_csd.cache_ctrl = value;
589 * According to the SD specs, some commands require a delay after
590 * issuing the command.
592 if (idata->ic.postsleep_min_us)
593 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
595 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
597 if (idata->rpmb || (cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
599 * Ensure RPMB/R1B command has completed by polling CMD13
602 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, NULL);
608 static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
609 struct mmc_ioc_cmd __user *ic_ptr,
610 struct mmc_rpmb_data *rpmb)
612 struct mmc_blk_ioc_data *idata;
613 struct mmc_blk_ioc_data *idatas[1];
614 struct mmc_queue *mq;
615 struct mmc_card *card;
616 int err = 0, ioc_err = 0;
619 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
621 return PTR_ERR(idata);
622 /* This will be NULL on non-RPMB ioctl():s */
625 card = md->queue.card;
632 * Dispatch the ioctl() into the block request queue.
635 req = blk_get_request(mq->queue,
636 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
642 req_to_mmc_queue_req(req)->drv_op =
643 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
644 req_to_mmc_queue_req(req)->drv_op_data = idatas;
645 req_to_mmc_queue_req(req)->ioc_count = 1;
646 blk_execute_rq(NULL, req, 0);
647 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
648 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
649 blk_put_request(req);
654 return ioc_err ? ioc_err : err;
657 static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
658 struct mmc_ioc_multi_cmd __user *user,
659 struct mmc_rpmb_data *rpmb)
661 struct mmc_blk_ioc_data **idata = NULL;
662 struct mmc_ioc_cmd __user *cmds = user->cmds;
663 struct mmc_card *card;
664 struct mmc_queue *mq;
665 int i, err = 0, ioc_err = 0;
669 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
670 sizeof(num_of_cmds)))
676 if (num_of_cmds > MMC_IOC_MAX_CMDS)
679 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
683 for (i = 0; i < num_of_cmds; i++) {
684 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
685 if (IS_ERR(idata[i])) {
686 err = PTR_ERR(idata[i]);
690 /* This will be NULL on non-RPMB ioctl():s */
691 idata[i]->rpmb = rpmb;
694 card = md->queue.card;
702 * Dispatch the ioctl()s into the block request queue.
705 req = blk_get_request(mq->queue,
706 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
711 req_to_mmc_queue_req(req)->drv_op =
712 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
713 req_to_mmc_queue_req(req)->drv_op_data = idata;
714 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
715 blk_execute_rq(NULL, req, 0);
716 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
718 /* copy to user if data and response */
719 for (i = 0; i < num_of_cmds && !err; i++)
720 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
722 blk_put_request(req);
725 for (i = 0; i < num_of_cmds; i++) {
726 kfree(idata[i]->buf);
730 return ioc_err ? ioc_err : err;
733 static int mmc_blk_check_blkdev(struct block_device *bdev)
736 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
737 * whole block device, not on a partition. This prevents overspray
738 * between sibling partitions.
740 if (!capable(CAP_SYS_RAWIO) || bdev_is_partition(bdev))
745 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
746 unsigned int cmd, unsigned long arg)
748 struct mmc_blk_data *md;
753 ret = mmc_blk_check_blkdev(bdev);
756 md = mmc_blk_get(bdev->bd_disk);
759 ret = mmc_blk_ioctl_cmd(md,
760 (struct mmc_ioc_cmd __user *)arg,
764 case MMC_IOC_MULTI_CMD:
765 ret = mmc_blk_check_blkdev(bdev);
768 md = mmc_blk_get(bdev->bd_disk);
771 ret = mmc_blk_ioctl_multi_cmd(md,
772 (struct mmc_ioc_multi_cmd __user *)arg,
782 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
783 unsigned int cmd, unsigned long arg)
785 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
789 static const struct block_device_operations mmc_bdops = {
790 .open = mmc_blk_open,
791 .release = mmc_blk_release,
792 .getgeo = mmc_blk_getgeo,
793 .owner = THIS_MODULE,
794 .ioctl = mmc_blk_ioctl,
796 .compat_ioctl = mmc_blk_compat_ioctl,
800 static int mmc_blk_part_switch_pre(struct mmc_card *card,
801 unsigned int part_type)
805 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
806 if (card->ext_csd.cmdq_en) {
807 ret = mmc_cmdq_disable(card);
811 mmc_retune_pause(card->host);
817 static int mmc_blk_part_switch_post(struct mmc_card *card,
818 unsigned int part_type)
822 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
823 mmc_retune_unpause(card->host);
824 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
825 ret = mmc_cmdq_enable(card);
831 static inline int mmc_blk_part_switch(struct mmc_card *card,
832 unsigned int part_type)
835 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
837 if (main_md->part_curr == part_type)
840 if (mmc_card_mmc(card)) {
841 u8 part_config = card->ext_csd.part_config;
843 ret = mmc_blk_part_switch_pre(card, part_type);
847 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
848 part_config |= part_type;
850 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
851 EXT_CSD_PART_CONFIG, part_config,
852 card->ext_csd.part_time);
854 mmc_blk_part_switch_post(card, part_type);
858 card->ext_csd.part_config = part_config;
860 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
863 main_md->part_curr = part_type;
867 static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
873 struct mmc_request mrq = {};
874 struct mmc_command cmd = {};
875 struct mmc_data data = {};
877 struct scatterlist sg;
879 cmd.opcode = MMC_APP_CMD;
880 cmd.arg = card->rca << 16;
881 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
883 err = mmc_wait_for_cmd(card->host, &cmd, 0);
886 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
889 memset(&cmd, 0, sizeof(struct mmc_command));
891 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
893 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
897 data.flags = MMC_DATA_READ;
900 mmc_set_data_timeout(&data, card);
905 blocks = kmalloc(4, GFP_KERNEL);
909 sg_init_one(&sg, blocks, 4);
911 mmc_wait_for_req(card->host, &mrq);
913 result = ntohl(*blocks);
916 if (cmd.error || data.error)
919 *written_blocks = result;
924 static unsigned int mmc_blk_clock_khz(struct mmc_host *host)
926 if (host->actual_clock)
927 return host->actual_clock / 1000;
929 /* Clock may be subject to a divisor, fudge it by a factor of 2. */
931 return host->ios.clock / 2000;
933 /* How can there be no clock */
935 return 100; /* 100 kHz is minimum possible value */
938 static unsigned int mmc_blk_data_timeout_ms(struct mmc_host *host,
939 struct mmc_data *data)
941 unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000);
944 if (data->timeout_clks) {
945 khz = mmc_blk_clock_khz(host);
946 ms += DIV_ROUND_UP(data->timeout_clks, khz);
952 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
957 if (md->reset_done & type)
960 md->reset_done |= type;
961 err = mmc_hw_reset(host);
962 /* Ensure we switch back to the correct partition */
964 struct mmc_blk_data *main_md =
965 dev_get_drvdata(&host->card->dev);
968 main_md->part_curr = main_md->part_type;
969 part_err = mmc_blk_part_switch(host->card, md->part_type);
972 * We have failed to get back into the correct
973 * partition, so we need to abort the whole request.
981 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
983 md->reset_done &= ~type;
987 * The non-block commands come back from the block layer after it queued it and
988 * processed it with all other requests and then they get issued in this
991 static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
993 struct mmc_queue_req *mq_rq;
994 struct mmc_card *card = mq->card;
995 struct mmc_blk_data *md = mq->blkdata;
996 struct mmc_blk_ioc_data **idata;
1003 mq_rq = req_to_mmc_queue_req(req);
1004 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
1006 switch (mq_rq->drv_op) {
1007 case MMC_DRV_OP_IOCTL:
1008 if (card->ext_csd.cmdq_en) {
1009 ret = mmc_cmdq_disable(card);
1014 case MMC_DRV_OP_IOCTL_RPMB:
1015 idata = mq_rq->drv_op_data;
1016 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
1017 ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
1021 /* Always switch back to main area after RPMB access */
1023 mmc_blk_part_switch(card, 0);
1024 else if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
1025 mmc_cmdq_enable(card);
1027 case MMC_DRV_OP_BOOT_WP:
1028 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1029 card->ext_csd.boot_ro_lock |
1030 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1031 card->ext_csd.part_time);
1033 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1034 md->disk->disk_name, ret);
1036 card->ext_csd.boot_ro_lock |=
1037 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
1039 case MMC_DRV_OP_GET_CARD_STATUS:
1040 ret = mmc_send_status(card, &status);
1044 case MMC_DRV_OP_GET_EXT_CSD:
1045 ext_csd = mq_rq->drv_op_data;
1046 ret = mmc_get_ext_csd(card, ext_csd);
1049 pr_err("%s: unknown driver specific operation\n",
1050 md->disk->disk_name);
1054 mq_rq->drv_op_result = ret;
1055 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1058 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1060 struct mmc_blk_data *md = mq->blkdata;
1061 struct mmc_card *card = md->queue.card;
1062 unsigned int from, nr;
1063 int err = 0, type = MMC_BLK_DISCARD;
1064 blk_status_t status = BLK_STS_OK;
1066 if (!mmc_can_erase(card)) {
1067 status = BLK_STS_NOTSUPP;
1071 from = blk_rq_pos(req);
1072 nr = blk_rq_sectors(req);
1076 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1077 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1078 INAND_CMD38_ARG_EXT_CSD,
1079 card->erase_arg == MMC_TRIM_ARG ?
1080 INAND_CMD38_ARG_TRIM :
1081 INAND_CMD38_ARG_ERASE,
1082 card->ext_csd.generic_cmd6_time);
1085 err = mmc_erase(card, from, nr, card->erase_arg);
1086 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
1088 status = BLK_STS_IOERR;
1090 mmc_blk_reset_success(md, type);
1092 blk_mq_end_request(req, status);
1095 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1096 struct request *req)
1098 struct mmc_blk_data *md = mq->blkdata;
1099 struct mmc_card *card = md->queue.card;
1100 unsigned int from, nr, arg;
1101 int err = 0, type = MMC_BLK_SECDISCARD;
1102 blk_status_t status = BLK_STS_OK;
1104 if (!(mmc_can_secure_erase_trim(card))) {
1105 status = BLK_STS_NOTSUPP;
1109 from = blk_rq_pos(req);
1110 nr = blk_rq_sectors(req);
1112 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1113 arg = MMC_SECURE_TRIM1_ARG;
1115 arg = MMC_SECURE_ERASE_ARG;
1118 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1119 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1120 INAND_CMD38_ARG_EXT_CSD,
1121 arg == MMC_SECURE_TRIM1_ARG ?
1122 INAND_CMD38_ARG_SECTRIM1 :
1123 INAND_CMD38_ARG_SECERASE,
1124 card->ext_csd.generic_cmd6_time);
1129 err = mmc_erase(card, from, nr, arg);
1133 status = BLK_STS_IOERR;
1137 if (arg == MMC_SECURE_TRIM1_ARG) {
1138 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1139 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1140 INAND_CMD38_ARG_EXT_CSD,
1141 INAND_CMD38_ARG_SECTRIM2,
1142 card->ext_csd.generic_cmd6_time);
1147 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1151 status = BLK_STS_IOERR;
1157 if (err && !mmc_blk_reset(md, card->host, type))
1160 mmc_blk_reset_success(md, type);
1162 blk_mq_end_request(req, status);
1165 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1167 struct mmc_blk_data *md = mq->blkdata;
1168 struct mmc_card *card = md->queue.card;
1171 ret = mmc_flush_cache(card->host);
1172 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1176 * Reformat current write as a reliable write, supporting
1177 * both legacy and the enhanced reliable write MMC cards.
1178 * In each transfer we'll handle only as much as a single
1179 * reliable write can handle, thus finish the request in
1180 * partial completions.
1182 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1183 struct mmc_card *card,
1184 struct request *req)
1186 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1187 /* Legacy mode imposes restrictions on transfers. */
1188 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
1189 brq->data.blocks = 1;
1191 if (brq->data.blocks > card->ext_csd.rel_sectors)
1192 brq->data.blocks = card->ext_csd.rel_sectors;
1193 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1194 brq->data.blocks = 1;
1198 #define CMD_ERRORS_EXCL_OOR \
1199 (R1_ADDRESS_ERROR | /* Misaligned address */ \
1200 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1201 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1202 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
1203 R1_CC_ERROR | /* Card controller error */ \
1204 R1_ERROR) /* General/unknown error */
1206 #define CMD_ERRORS \
1207 (CMD_ERRORS_EXCL_OOR | \
1208 R1_OUT_OF_RANGE) /* Command argument out of range */ \
1210 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
1215 * Per the SD specification(physical layer version 4.10)[1],
1216 * section 4.3.3, it explicitly states that "When the last
1217 * block of user area is read using CMD18, the host should
1218 * ignore OUT_OF_RANGE error that may occur even the sequence
1219 * is correct". And JESD84-B51 for eMMC also has a similar
1220 * statement on section 6.8.3.
1222 * Multiple block read/write could be done by either predefined
1223 * method, namely CMD23, or open-ending mode. For open-ending mode,
1224 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1226 * However the spec[1] doesn't tell us whether we should also
1227 * ignore that for predefined method. But per the spec[1], section
1228 * 4.15 Set Block Count Command, it says"If illegal block count
1229 * is set, out of range error will be indicated during read/write
1230 * operation (For example, data transfer is stopped at user area
1231 * boundary)." In another word, we could expect a out of range error
1232 * in the response for the following CMD18/25. And if argument of
1233 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1234 * we could also expect to get a -ETIMEDOUT or any error number from
1235 * the host drivers due to missing data response(for write)/data(for
1236 * read), as the cards will stop the data transfer by itself per the
1237 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1240 if (!brq->stop.error) {
1241 bool oor_with_open_end;
1242 /* If there is no error yet, check R1 response */
1244 val = brq->stop.resp[0] & CMD_ERRORS;
1245 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1247 if (val && !oor_with_open_end)
1248 brq->stop.error = -EIO;
1252 static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
1253 int disable_multi, bool *do_rel_wr_p,
1254 bool *do_data_tag_p)
1256 struct mmc_blk_data *md = mq->blkdata;
1257 struct mmc_card *card = md->queue.card;
1258 struct mmc_blk_request *brq = &mqrq->brq;
1259 struct request *req = mmc_queue_req_to_req(mqrq);
1260 bool do_rel_wr, do_data_tag;
1263 * Reliable writes are used to implement Forced Unit Access and
1264 * are supported only on MMCs.
1266 do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1267 rq_data_dir(req) == WRITE &&
1268 (md->flags & MMC_BLK_REL_WR);
1270 memset(brq, 0, sizeof(struct mmc_blk_request));
1272 mmc_crypto_prepare_req(mqrq);
1274 brq->mrq.data = &brq->data;
1275 brq->mrq.tag = req->tag;
1277 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1280 if (rq_data_dir(req) == READ) {
1281 brq->data.flags = MMC_DATA_READ;
1282 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1284 brq->data.flags = MMC_DATA_WRITE;
1285 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1288 brq->data.blksz = 512;
1289 brq->data.blocks = blk_rq_sectors(req);
1290 brq->data.blk_addr = blk_rq_pos(req);
1293 * The command queue supports 2 priorities: "high" (1) and "simple" (0).
1294 * The eMMC will give "high" priority tasks priority over "simple"
1295 * priority tasks. Here we always set "simple" priority by not setting
1300 * The block layer doesn't support all sector count
1301 * restrictions, so we need to be prepared for too big
1304 if (brq->data.blocks > card->host->max_blk_count)
1305 brq->data.blocks = card->host->max_blk_count;
1307 if (brq->data.blocks > 1) {
1309 * Some SD cards in SPI mode return a CRC error or even lock up
1310 * completely when trying to read the last block using a
1311 * multiblock read command.
1313 if (mmc_host_is_spi(card->host) && (rq_data_dir(req) == READ) &&
1314 (blk_rq_pos(req) + blk_rq_sectors(req) ==
1315 get_capacity(md->disk)))
1319 * After a read error, we redo the request one sector
1320 * at a time in order to accurately determine which
1321 * sectors can be read successfully.
1324 brq->data.blocks = 1;
1327 * Some controllers have HW issues while operating
1328 * in multiple I/O mode
1330 if (card->host->ops->multi_io_quirk)
1331 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1332 (rq_data_dir(req) == READ) ?
1333 MMC_DATA_READ : MMC_DATA_WRITE,
1338 mmc_apply_rel_rw(brq, card, req);
1339 brq->data.flags |= MMC_DATA_REL_WR;
1343 * Data tag is used only during writing meta data to speed
1344 * up write and any subsequent read of this meta data
1346 do_data_tag = card->ext_csd.data_tag_unit_size &&
1347 (req->cmd_flags & REQ_META) &&
1348 (rq_data_dir(req) == WRITE) &&
1349 ((brq->data.blocks * brq->data.blksz) >=
1350 card->ext_csd.data_tag_unit_size);
1353 brq->data.flags |= MMC_DATA_DAT_TAG;
1355 mmc_set_data_timeout(&brq->data, card);
1357 brq->data.sg = mqrq->sg;
1358 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1361 * Adjust the sg list so it is the same size as the
1364 if (brq->data.blocks != blk_rq_sectors(req)) {
1365 int i, data_size = brq->data.blocks << 9;
1366 struct scatterlist *sg;
1368 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1369 data_size -= sg->length;
1370 if (data_size <= 0) {
1371 sg->length += data_size;
1376 brq->data.sg_len = i;
1380 *do_rel_wr_p = do_rel_wr;
1383 *do_data_tag_p = do_data_tag;
1386 #define MMC_CQE_RETRIES 2
1388 static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req)
1390 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1391 struct mmc_request *mrq = &mqrq->brq.mrq;
1392 struct request_queue *q = req->q;
1393 struct mmc_host *host = mq->card->host;
1394 enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
1395 unsigned long flags;
1399 mmc_cqe_post_req(host, mrq);
1401 if (mrq->cmd && mrq->cmd->error)
1402 err = mrq->cmd->error;
1403 else if (mrq->data && mrq->data->error)
1404 err = mrq->data->error;
1409 if (mqrq->retries++ < MMC_CQE_RETRIES)
1410 blk_mq_requeue_request(req, true);
1412 blk_mq_end_request(req, BLK_STS_IOERR);
1413 } else if (mrq->data) {
1414 if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered))
1415 blk_mq_requeue_request(req, true);
1417 __blk_mq_end_request(req, BLK_STS_OK);
1419 blk_mq_end_request(req, BLK_STS_OK);
1422 spin_lock_irqsave(&mq->lock, flags);
1424 mq->in_flight[issue_type] -= 1;
1426 put_card = (mmc_tot_in_flight(mq) == 0);
1428 mmc_cqe_check_busy(mq);
1430 spin_unlock_irqrestore(&mq->lock, flags);
1433 blk_mq_run_hw_queues(q, true);
1436 mmc_put_card(mq->card, &mq->ctx);
1439 void mmc_blk_cqe_recovery(struct mmc_queue *mq)
1441 struct mmc_card *card = mq->card;
1442 struct mmc_host *host = card->host;
1445 pr_debug("%s: CQE recovery start\n", mmc_hostname(host));
1447 err = mmc_cqe_recovery(host);
1449 mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY);
1451 mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY);
1453 pr_debug("%s: CQE recovery done\n", mmc_hostname(host));
1456 static void mmc_blk_cqe_req_done(struct mmc_request *mrq)
1458 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
1460 struct request *req = mmc_queue_req_to_req(mqrq);
1461 struct request_queue *q = req->q;
1462 struct mmc_queue *mq = q->queuedata;
1465 * Block layer timeouts race with completions which means the normal
1466 * completion path cannot be used during recovery.
1468 if (mq->in_recovery)
1469 mmc_blk_cqe_complete_rq(mq, req);
1470 else if (likely(!blk_should_fake_timeout(req->q)))
1471 blk_mq_complete_request(req);
1474 static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
1476 mrq->done = mmc_blk_cqe_req_done;
1477 mrq->recovery_notifier = mmc_cqe_recovery_notifier;
1479 return mmc_cqe_start_req(host, mrq);
1482 static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq,
1483 struct request *req)
1485 struct mmc_blk_request *brq = &mqrq->brq;
1487 memset(brq, 0, sizeof(*brq));
1489 brq->mrq.cmd = &brq->cmd;
1490 brq->mrq.tag = req->tag;
1495 static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
1497 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1498 struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);
1500 mrq->cmd->opcode = MMC_SWITCH;
1501 mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1502 (EXT_CSD_FLUSH_CACHE << 16) |
1504 EXT_CSD_CMD_SET_NORMAL;
1505 mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B;
1507 return mmc_blk_cqe_start_req(mq->card->host, mrq);
1510 static int mmc_blk_hsq_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1512 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1513 struct mmc_host *host = mq->card->host;
1516 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
1517 mqrq->brq.mrq.done = mmc_blk_hsq_req_done;
1518 mmc_pre_req(host, &mqrq->brq.mrq);
1520 err = mmc_cqe_start_req(host, &mqrq->brq.mrq);
1522 mmc_post_req(host, &mqrq->brq.mrq, err);
1527 static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1529 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1530 struct mmc_host *host = mq->card->host;
1532 if (host->hsq_enabled)
1533 return mmc_blk_hsq_issue_rw_rq(mq, req);
1535 mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL);
1537 return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq);
1540 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1541 struct mmc_card *card,
1543 struct mmc_queue *mq)
1545 u32 readcmd, writecmd;
1546 struct mmc_blk_request *brq = &mqrq->brq;
1547 struct request *req = mmc_queue_req_to_req(mqrq);
1548 struct mmc_blk_data *md = mq->blkdata;
1549 bool do_rel_wr, do_data_tag;
1551 mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1553 brq->mrq.cmd = &brq->cmd;
1555 brq->cmd.arg = blk_rq_pos(req);
1556 if (!mmc_card_blockaddr(card))
1558 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1560 if (brq->data.blocks > 1 || do_rel_wr) {
1561 /* SPI multiblock writes terminate using a special
1562 * token, not a STOP_TRANSMISSION request.
1564 if (!mmc_host_is_spi(card->host) ||
1565 rq_data_dir(req) == READ)
1566 brq->mrq.stop = &brq->stop;
1567 readcmd = MMC_READ_MULTIPLE_BLOCK;
1568 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1570 brq->mrq.stop = NULL;
1571 readcmd = MMC_READ_SINGLE_BLOCK;
1572 writecmd = MMC_WRITE_BLOCK;
1574 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
1577 * Pre-defined multi-block transfers are preferable to
1578 * open ended-ones (and necessary for reliable writes).
1579 * However, it is not sufficient to just send CMD23,
1580 * and avoid the final CMD12, as on an error condition
1581 * CMD12 (stop) needs to be sent anyway. This, coupled
1582 * with Auto-CMD23 enhancements provided by some
1583 * hosts, means that the complexity of dealing
1584 * with this is best left to the host. If CMD23 is
1585 * supported by card and host, we'll fill sbc in and let
1586 * the host deal with handling it correctly. This means
1587 * that for hosts that don't expose MMC_CAP_CMD23, no
1588 * change of behavior will be observed.
1590 * N.B: Some MMC cards experience perf degradation.
1591 * We'll avoid using CMD23-bounded multiblock writes for
1592 * these, while retaining features like reliable writes.
1594 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1595 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1597 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1598 brq->sbc.arg = brq->data.blocks |
1599 (do_rel_wr ? (1 << 31) : 0) |
1600 (do_data_tag ? (1 << 29) : 0);
1601 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1602 brq->mrq.sbc = &brq->sbc;
1606 #define MMC_MAX_RETRIES 5
1607 #define MMC_DATA_RETRIES 2
1608 #define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1)
1610 static int mmc_blk_send_stop(struct mmc_card *card, unsigned int timeout)
1612 struct mmc_command cmd = {
1613 .opcode = MMC_STOP_TRANSMISSION,
1614 .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC,
1615 /* Some hosts wait for busy anyway, so provide a busy timeout */
1616 .busy_timeout = timeout,
1619 return mmc_wait_for_cmd(card->host, &cmd, 5);
1622 static int mmc_blk_fix_state(struct mmc_card *card, struct request *req)
1624 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1625 struct mmc_blk_request *brq = &mqrq->brq;
1626 unsigned int timeout = mmc_blk_data_timeout_ms(card->host, &brq->data);
1629 mmc_retune_hold_now(card->host);
1631 mmc_blk_send_stop(card, timeout);
1633 err = card_busy_detect(card, timeout, NULL);
1635 mmc_retune_release(card->host);
1640 #define MMC_READ_SINGLE_RETRIES 2
1642 /* Single sector read during recovery */
1643 static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req)
1645 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1646 struct mmc_request *mrq = &mqrq->brq.mrq;
1647 struct mmc_card *card = mq->card;
1648 struct mmc_host *host = card->host;
1649 blk_status_t error = BLK_STS_OK;
1656 mmc_blk_rw_rq_prep(mqrq, card, 1, mq);
1658 mmc_wait_for_req(host, mrq);
1660 err = mmc_send_status(card, &status);
1664 if (!mmc_host_is_spi(host) &&
1665 !mmc_ready_for_data(status)) {
1666 err = mmc_blk_fix_state(card, req);
1671 if (mrq->cmd->error && retries++ < MMC_READ_SINGLE_RETRIES)
1676 if (mrq->cmd->error ||
1678 (!mmc_host_is_spi(host) &&
1679 (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS)))
1680 error = BLK_STS_IOERR;
1684 } while (blk_update_request(req, error, 512));
1689 mrq->data->bytes_xfered = 0;
1690 blk_update_request(req, BLK_STS_IOERR, 512);
1691 /* Let it try the remaining request again */
1692 if (mqrq->retries > MMC_MAX_RETRIES - 1)
1693 mqrq->retries = MMC_MAX_RETRIES - 1;
1696 static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq)
1698 return !!brq->mrq.sbc;
1701 static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq)
1703 return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR;
1707 * Check for errors the host controller driver might not have seen such as
1708 * response mode errors or invalid card state.
1710 static bool mmc_blk_status_error(struct request *req, u32 status)
1712 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1713 struct mmc_blk_request *brq = &mqrq->brq;
1714 struct mmc_queue *mq = req->q->queuedata;
1717 if (mmc_host_is_spi(mq->card->host))
1720 stop_err_bits = mmc_blk_stop_err_bits(brq);
1722 return brq->cmd.resp[0] & CMD_ERRORS ||
1723 brq->stop.resp[0] & stop_err_bits ||
1724 status & stop_err_bits ||
1725 (rq_data_dir(req) == WRITE && !mmc_ready_for_data(status));
1728 static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq)
1730 return !brq->sbc.error && !brq->cmd.error &&
1731 !(brq->cmd.resp[0] & CMD_ERRORS);
1735 * Requests are completed by mmc_blk_mq_complete_rq() which sets simple
1737 * 1. A request that has transferred at least some data is considered
1738 * successful and will be requeued if there is remaining data to
1740 * 2. Otherwise the number of retries is incremented and the request
1741 * will be requeued if there are remaining retries.
1742 * 3. Otherwise the request will be errored out.
1743 * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and
1744 * mqrq->retries. So there are only 4 possible actions here:
1745 * 1. do not accept the bytes_xfered value i.e. set it to zero
1746 * 2. change mqrq->retries to determine the number of retries
1747 * 3. try to reset the card
1748 * 4. read one sector at a time
1750 static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req)
1752 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1753 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1754 struct mmc_blk_request *brq = &mqrq->brq;
1755 struct mmc_blk_data *md = mq->blkdata;
1756 struct mmc_card *card = mq->card;
1762 * Some errors the host driver might not have seen. Set the number of
1763 * bytes transferred to zero in that case.
1765 err = __mmc_send_status(card, &status, 0);
1766 if (err || mmc_blk_status_error(req, status))
1767 brq->data.bytes_xfered = 0;
1769 mmc_retune_release(card->host);
1772 * Try again to get the status. This also provides an opportunity for
1776 err = __mmc_send_status(card, &status, 0);
1779 * Nothing more to do after the number of bytes transferred has been
1780 * updated and there is no card.
1782 if (err && mmc_detect_card_removed(card->host))
1785 /* Try to get back to "tran" state */
1786 if (!mmc_host_is_spi(mq->card->host) &&
1787 (err || !mmc_ready_for_data(status)))
1788 err = mmc_blk_fix_state(mq->card, req);
1791 * Special case for SD cards where the card might record the number of
1794 if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) &&
1795 rq_data_dir(req) == WRITE) {
1796 if (mmc_sd_num_wr_blocks(card, &blocks))
1797 brq->data.bytes_xfered = 0;
1799 brq->data.bytes_xfered = blocks << 9;
1802 /* Reset if the card is in a bad state */
1803 if (!mmc_host_is_spi(mq->card->host) &&
1804 err && mmc_blk_reset(md, card->host, type)) {
1805 pr_err("%s: recovery failed!\n", req->rq_disk->disk_name);
1806 mqrq->retries = MMC_NO_RETRIES;
1811 * If anything was done, just return and if there is anything remaining
1812 * on the request it will get requeued.
1814 if (brq->data.bytes_xfered)
1817 /* Reset before last retry */
1818 if (mqrq->retries + 1 == MMC_MAX_RETRIES)
1819 mmc_blk_reset(md, card->host, type);
1821 /* Command errors fail fast, so use all MMC_MAX_RETRIES */
1822 if (brq->sbc.error || brq->cmd.error)
1825 /* Reduce the remaining retries for data errors */
1826 if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) {
1827 mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES;
1831 /* FIXME: Missing single sector read for large sector size */
1832 if (!mmc_large_sector(card) && rq_data_dir(req) == READ &&
1833 brq->data.blocks > 1) {
1834 /* Read one sector at a time */
1835 mmc_blk_read_single(mq, req);
1840 static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq)
1842 mmc_blk_eval_resp_error(brq);
1844 return brq->sbc.error || brq->cmd.error || brq->stop.error ||
1845 brq->data.error || brq->cmd.resp[0] & CMD_ERRORS;
1848 static int mmc_blk_card_busy(struct mmc_card *card, struct request *req)
1850 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1854 if (mmc_host_is_spi(card->host) || rq_data_dir(req) == READ)
1857 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, &status);
1860 * Do not assume data transferred correctly if there are any error bits
1863 if (status & mmc_blk_stop_err_bits(&mqrq->brq)) {
1864 mqrq->brq.data.bytes_xfered = 0;
1865 err = err ? err : -EIO;
1868 /* Copy the exception bit so it will be seen later on */
1869 if (mmc_card_mmc(card) && status & R1_EXCEPTION_EVENT)
1870 mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
1875 static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
1876 struct request *req)
1878 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1880 mmc_blk_reset_success(mq->blkdata, type);
1883 static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)
1885 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1886 unsigned int nr_bytes = mqrq->brq.data.bytes_xfered;
1889 if (blk_update_request(req, BLK_STS_OK, nr_bytes))
1890 blk_mq_requeue_request(req, true);
1892 __blk_mq_end_request(req, BLK_STS_OK);
1893 } else if (!blk_rq_bytes(req)) {
1894 __blk_mq_end_request(req, BLK_STS_IOERR);
1895 } else if (mqrq->retries++ < MMC_MAX_RETRIES) {
1896 blk_mq_requeue_request(req, true);
1898 if (mmc_card_removed(mq->card))
1899 req->rq_flags |= RQF_QUIET;
1900 blk_mq_end_request(req, BLK_STS_IOERR);
1904 static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
1905 struct mmc_queue_req *mqrq)
1907 return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) &&
1908 (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
1909 mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
1912 static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
1913 struct mmc_queue_req *mqrq)
1915 if (mmc_blk_urgent_bkops_needed(mq, mqrq))
1916 mmc_run_bkops(mq->card);
1919 static void mmc_blk_hsq_req_done(struct mmc_request *mrq)
1921 struct mmc_queue_req *mqrq =
1922 container_of(mrq, struct mmc_queue_req, brq.mrq);
1923 struct request *req = mmc_queue_req_to_req(mqrq);
1924 struct request_queue *q = req->q;
1925 struct mmc_queue *mq = q->queuedata;
1926 struct mmc_host *host = mq->card->host;
1927 unsigned long flags;
1929 if (mmc_blk_rq_error(&mqrq->brq) ||
1930 mmc_blk_urgent_bkops_needed(mq, mqrq)) {
1931 spin_lock_irqsave(&mq->lock, flags);
1932 mq->recovery_needed = true;
1933 mq->recovery_req = req;
1934 spin_unlock_irqrestore(&mq->lock, flags);
1936 host->cqe_ops->cqe_recovery_start(host);
1938 schedule_work(&mq->recovery_work);
1942 mmc_blk_rw_reset_success(mq, req);
1945 * Block layer timeouts race with completions which means the normal
1946 * completion path cannot be used during recovery.
1948 if (mq->in_recovery)
1949 mmc_blk_cqe_complete_rq(mq, req);
1950 else if (likely(!blk_should_fake_timeout(req->q)))
1951 blk_mq_complete_request(req);
1954 void mmc_blk_mq_complete(struct request *req)
1956 struct mmc_queue *mq = req->q->queuedata;
1957 struct mmc_host *host = mq->card->host;
1959 if (host->cqe_enabled)
1960 mmc_blk_cqe_complete_rq(mq, req);
1961 else if (likely(!blk_should_fake_timeout(req->q)))
1962 mmc_blk_mq_complete_rq(mq, req);
1965 static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,
1966 struct request *req)
1968 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1969 struct mmc_host *host = mq->card->host;
1971 if (mmc_blk_rq_error(&mqrq->brq) ||
1972 mmc_blk_card_busy(mq->card, req)) {
1973 mmc_blk_mq_rw_recovery(mq, req);
1975 mmc_blk_rw_reset_success(mq, req);
1976 mmc_retune_release(host);
1979 mmc_blk_urgent_bkops(mq, mqrq);
1982 static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, struct request *req)
1984 unsigned long flags;
1987 spin_lock_irqsave(&mq->lock, flags);
1989 mq->in_flight[mmc_issue_type(mq, req)] -= 1;
1991 put_card = (mmc_tot_in_flight(mq) == 0);
1993 spin_unlock_irqrestore(&mq->lock, flags);
1996 mmc_put_card(mq->card, &mq->ctx);
1999 static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req)
2001 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2002 struct mmc_request *mrq = &mqrq->brq.mrq;
2003 struct mmc_host *host = mq->card->host;
2005 mmc_post_req(host, mrq, 0);
2008 * Block layer timeouts race with completions which means the normal
2009 * completion path cannot be used during recovery.
2011 if (mq->in_recovery)
2012 mmc_blk_mq_complete_rq(mq, req);
2013 else if (likely(!blk_should_fake_timeout(req->q)))
2014 blk_mq_complete_request(req);
2016 mmc_blk_mq_dec_in_flight(mq, req);
2019 void mmc_blk_mq_recovery(struct mmc_queue *mq)
2021 struct request *req = mq->recovery_req;
2022 struct mmc_host *host = mq->card->host;
2023 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2025 mq->recovery_req = NULL;
2026 mq->rw_wait = false;
2028 if (mmc_blk_rq_error(&mqrq->brq)) {
2029 mmc_retune_hold_now(host);
2030 mmc_blk_mq_rw_recovery(mq, req);
2033 mmc_blk_urgent_bkops(mq, mqrq);
2035 mmc_blk_mq_post_req(mq, req);
2038 static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq,
2039 struct request **prev_req)
2041 if (mmc_host_done_complete(mq->card->host))
2044 mutex_lock(&mq->complete_lock);
2046 if (!mq->complete_req)
2049 mmc_blk_mq_poll_completion(mq, mq->complete_req);
2052 *prev_req = mq->complete_req;
2054 mmc_blk_mq_post_req(mq, mq->complete_req);
2056 mq->complete_req = NULL;
2059 mutex_unlock(&mq->complete_lock);
2062 void mmc_blk_mq_complete_work(struct work_struct *work)
2064 struct mmc_queue *mq = container_of(work, struct mmc_queue,
2067 mmc_blk_mq_complete_prev_req(mq, NULL);
2070 static void mmc_blk_mq_req_done(struct mmc_request *mrq)
2072 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
2074 struct request *req = mmc_queue_req_to_req(mqrq);
2075 struct request_queue *q = req->q;
2076 struct mmc_queue *mq = q->queuedata;
2077 struct mmc_host *host = mq->card->host;
2078 unsigned long flags;
2080 if (!mmc_host_done_complete(host)) {
2084 * We cannot complete the request in this context, so record
2085 * that there is a request to complete, and that a following
2086 * request does not need to wait (although it does need to
2087 * complete complete_req first).
2089 spin_lock_irqsave(&mq->lock, flags);
2090 mq->complete_req = req;
2091 mq->rw_wait = false;
2092 waiting = mq->waiting;
2093 spin_unlock_irqrestore(&mq->lock, flags);
2096 * If 'waiting' then the waiting task will complete this
2097 * request, otherwise queue a work to do it. Note that
2098 * complete_work may still race with the dispatch of a following
2104 queue_work(mq->card->complete_wq, &mq->complete_work);
2109 /* Take the recovery path for errors or urgent background operations */
2110 if (mmc_blk_rq_error(&mqrq->brq) ||
2111 mmc_blk_urgent_bkops_needed(mq, mqrq)) {
2112 spin_lock_irqsave(&mq->lock, flags);
2113 mq->recovery_needed = true;
2114 mq->recovery_req = req;
2115 spin_unlock_irqrestore(&mq->lock, flags);
2117 schedule_work(&mq->recovery_work);
2121 mmc_blk_rw_reset_success(mq, req);
2123 mq->rw_wait = false;
2126 mmc_blk_mq_post_req(mq, req);
2129 static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err)
2131 unsigned long flags;
2135 * Wait while there is another request in progress, but not if recovery
2136 * is needed. Also indicate whether there is a request waiting to start.
2138 spin_lock_irqsave(&mq->lock, flags);
2139 if (mq->recovery_needed) {
2143 done = !mq->rw_wait;
2145 mq->waiting = !done;
2146 spin_unlock_irqrestore(&mq->lock, flags);
2151 static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req)
2155 wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err));
2157 /* Always complete the previous request if there is one */
2158 mmc_blk_mq_complete_prev_req(mq, prev_req);
2163 static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq,
2164 struct request *req)
2166 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2167 struct mmc_host *host = mq->card->host;
2168 struct request *prev_req = NULL;
2171 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
2173 mqrq->brq.mrq.done = mmc_blk_mq_req_done;
2175 mmc_pre_req(host, &mqrq->brq.mrq);
2177 err = mmc_blk_rw_wait(mq, &prev_req);
2183 err = mmc_start_request(host, &mqrq->brq.mrq);
2186 mmc_blk_mq_post_req(mq, prev_req);
2189 mq->rw_wait = false;
2191 /* Release re-tuning here where there is no synchronization required */
2192 if (err || mmc_host_done_complete(host))
2193 mmc_retune_release(host);
2197 mmc_post_req(host, &mqrq->brq.mrq, err);
2202 static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host)
2204 if (host->cqe_enabled)
2205 return host->cqe_ops->cqe_wait_for_idle(host);
2207 return mmc_blk_rw_wait(mq, NULL);
2210 enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
2212 struct mmc_blk_data *md = mq->blkdata;
2213 struct mmc_card *card = md->queue.card;
2214 struct mmc_host *host = card->host;
2217 ret = mmc_blk_part_switch(card, md->part_type);
2219 return MMC_REQ_FAILED_TO_START;
2221 switch (mmc_issue_type(mq, req)) {
2222 case MMC_ISSUE_SYNC:
2223 ret = mmc_blk_wait_for_idle(mq, host);
2225 return MMC_REQ_BUSY;
2226 switch (req_op(req)) {
2228 case REQ_OP_DRV_OUT:
2229 mmc_blk_issue_drv_op(mq, req);
2231 case REQ_OP_DISCARD:
2232 mmc_blk_issue_discard_rq(mq, req);
2234 case REQ_OP_SECURE_ERASE:
2235 mmc_blk_issue_secdiscard_rq(mq, req);
2238 mmc_blk_issue_flush(mq, req);
2242 return MMC_REQ_FAILED_TO_START;
2244 return MMC_REQ_FINISHED;
2245 case MMC_ISSUE_DCMD:
2246 case MMC_ISSUE_ASYNC:
2247 switch (req_op(req)) {
2249 if (!mmc_cache_enabled(host)) {
2250 blk_mq_end_request(req, BLK_STS_OK);
2251 return MMC_REQ_FINISHED;
2253 ret = mmc_blk_cqe_issue_flush(mq, req);
2257 if (host->cqe_enabled)
2258 ret = mmc_blk_cqe_issue_rw_rq(mq, req);
2260 ret = mmc_blk_mq_issue_rw_rq(mq, req);
2267 return MMC_REQ_STARTED;
2268 return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START;
2271 return MMC_REQ_FAILED_TO_START;
2275 static inline int mmc_blk_readonly(struct mmc_card *card)
2277 return mmc_card_readonly(card) ||
2278 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2281 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2282 struct device *parent,
2285 const char *subname,
2288 struct mmc_blk_data *md;
2292 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
2295 * We get -ENOSPC because there are no more any available
2296 * devidx. The reason may be that, either userspace haven't yet
2297 * unmounted the partitions, which postpones mmc_blk_release()
2298 * from being called, or the device has more partitions than
2301 if (devidx == -ENOSPC)
2302 dev_err(mmc_dev(card->host),
2303 "no more device IDs available\n");
2305 return ERR_PTR(devidx);
2308 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2314 md->area_type = area_type;
2317 * Set the read-only status based on the supported commands
2318 * and the write protect switch.
2320 md->read_only = mmc_blk_readonly(card);
2322 md->disk = alloc_disk(perdev_minors);
2323 if (md->disk == NULL) {
2328 INIT_LIST_HEAD(&md->part);
2329 INIT_LIST_HEAD(&md->rpmbs);
2332 ret = mmc_init_queue(&md->queue, card);
2336 md->queue.blkdata = md;
2339 * Keep an extra reference to the queue so that we can shutdown the
2340 * queue (i.e. call blk_cleanup_queue()) while there are still
2341 * references to the 'md'. The corresponding blk_put_queue() is in
2344 if (!blk_get_queue(md->queue.queue)) {
2345 mmc_cleanup_queue(&md->queue);
2350 md->disk->major = MMC_BLOCK_MAJOR;
2351 md->disk->first_minor = devidx * perdev_minors;
2352 md->disk->fops = &mmc_bdops;
2353 md->disk->private_data = md;
2354 md->disk->queue = md->queue.queue;
2355 md->parent = parent;
2356 set_disk_ro(md->disk, md->read_only || default_ro);
2357 md->disk->flags = GENHD_FL_EXT_DEVT;
2358 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2359 md->disk->flags |= GENHD_FL_NO_PART_SCAN
2360 | GENHD_FL_SUPPRESS_PARTITION_INFO;
2363 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2365 * - be set for removable media with permanent block devices
2366 * - be unset for removable block devices with permanent media
2368 * Since MMC block devices clearly fall under the second
2369 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2370 * should use the block device creation/destruction hotplug
2371 * messages to tell when the card is present.
2374 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2375 "mmcblk%u%s", card->host->index, subname ? subname : "");
2377 set_capacity(md->disk, size);
2379 if (mmc_host_cmd23(card->host)) {
2380 if ((mmc_card_mmc(card) &&
2381 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
2382 (mmc_card_sd(card) &&
2383 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2384 md->flags |= MMC_BLK_CMD23;
2387 if (mmc_card_mmc(card) &&
2388 md->flags & MMC_BLK_CMD23 &&
2389 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2390 card->ext_csd.rel_sectors)) {
2391 md->flags |= MMC_BLK_REL_WR;
2392 blk_queue_write_cache(md->queue.queue, true, true);
2395 string_get_size((u64)size, 512, STRING_UNITS_2,
2396 cap_str, sizeof(cap_str));
2397 pr_info("%s: %s %s %s %s\n",
2398 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2399 cap_str, md->read_only ? "(ro)" : "");
2408 ida_simple_remove(&mmc_blk_ida, devidx);
2409 return ERR_PTR(ret);
2412 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2416 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2418 * The EXT_CSD sector count is in number or 512 byte
2421 size = card->ext_csd.sectors;
2424 * The CSD capacity field is in units of read_blkbits.
2425 * set_capacity takes units of 512 bytes.
2427 size = (typeof(sector_t))card->csd.capacity
2428 << (card->csd.read_blkbits - 9);
2431 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2432 MMC_BLK_DATA_AREA_MAIN);
2435 static int mmc_blk_alloc_part(struct mmc_card *card,
2436 struct mmc_blk_data *md,
2437 unsigned int part_type,
2440 const char *subname,
2443 struct mmc_blk_data *part_md;
2445 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2446 subname, area_type);
2447 if (IS_ERR(part_md))
2448 return PTR_ERR(part_md);
2449 part_md->part_type = part_type;
2450 list_add(&part_md->part, &md->part);
2456 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2457 * @filp: the character device file
2458 * @cmd: the ioctl() command
2459 * @arg: the argument from userspace
2461 * This will essentially just redirect the ioctl()s coming in over to
2462 * the main block device spawning the RPMB character device.
2464 static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
2467 struct mmc_rpmb_data *rpmb = filp->private_data;
2472 ret = mmc_blk_ioctl_cmd(rpmb->md,
2473 (struct mmc_ioc_cmd __user *)arg,
2476 case MMC_IOC_MULTI_CMD:
2477 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
2478 (struct mmc_ioc_multi_cmd __user *)arg,
2489 #ifdef CONFIG_COMPAT
2490 static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
2493 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
2497 static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
2499 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2500 struct mmc_rpmb_data, chrdev);
2502 get_device(&rpmb->dev);
2503 filp->private_data = rpmb;
2504 mmc_blk_get(rpmb->md->disk);
2506 return nonseekable_open(inode, filp);
2509 static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
2511 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2512 struct mmc_rpmb_data, chrdev);
2514 mmc_blk_put(rpmb->md);
2515 put_device(&rpmb->dev);
2520 static const struct file_operations mmc_rpmb_fileops = {
2521 .release = mmc_rpmb_chrdev_release,
2522 .open = mmc_rpmb_chrdev_open,
2523 .owner = THIS_MODULE,
2524 .llseek = no_llseek,
2525 .unlocked_ioctl = mmc_rpmb_ioctl,
2526 #ifdef CONFIG_COMPAT
2527 .compat_ioctl = mmc_rpmb_ioctl_compat,
2531 static void mmc_blk_rpmb_device_release(struct device *dev)
2533 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2535 ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2539 static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
2540 struct mmc_blk_data *md,
2541 unsigned int part_index,
2543 const char *subname)
2546 char rpmb_name[DISK_NAME_LEN];
2548 struct mmc_rpmb_data *rpmb;
2550 /* This creates the minor number for the RPMB char device */
2551 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
2555 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
2557 ida_simple_remove(&mmc_rpmb_ida, devidx);
2561 snprintf(rpmb_name, sizeof(rpmb_name),
2562 "mmcblk%u%s", card->host->index, subname ? subname : "");
2565 rpmb->part_index = part_index;
2566 rpmb->dev.init_name = rpmb_name;
2567 rpmb->dev.bus = &mmc_rpmb_bus_type;
2568 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
2569 rpmb->dev.parent = &card->dev;
2570 rpmb->dev.release = mmc_blk_rpmb_device_release;
2571 device_initialize(&rpmb->dev);
2572 dev_set_drvdata(&rpmb->dev, rpmb);
2575 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
2576 rpmb->chrdev.owner = THIS_MODULE;
2577 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
2579 pr_err("%s: could not add character device\n", rpmb_name);
2580 goto out_put_device;
2583 list_add(&rpmb->node, &md->rpmbs);
2585 string_get_size((u64)size, 512, STRING_UNITS_2,
2586 cap_str, sizeof(cap_str));
2588 pr_info("%s: %s %s %s, chardev (%d:%d)\n",
2589 rpmb_name, mmc_card_id(card), mmc_card_name(card), cap_str,
2590 MAJOR(mmc_rpmb_devt), rpmb->id);
2595 put_device(&rpmb->dev);
2599 static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
2602 cdev_device_del(&rpmb->chrdev, &rpmb->dev);
2603 put_device(&rpmb->dev);
2606 /* MMC Physical partitions consist of two boot partitions and
2607 * up to four general purpose partitions.
2608 * For each partition enabled in EXT_CSD a block device will be allocatedi
2609 * to provide access to the partition.
2612 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2616 if (!mmc_card_mmc(card))
2619 for (idx = 0; idx < card->nr_parts; idx++) {
2620 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
2622 * RPMB partitions does not provide block access, they
2623 * are only accessed using ioctl():s. Thus create
2624 * special RPMB block devices that do not have a
2625 * backing block queue for these.
2627 ret = mmc_blk_alloc_rpmb_part(card, md,
2628 card->part[idx].part_cfg,
2629 card->part[idx].size >> 9,
2630 card->part[idx].name);
2633 } else if (card->part[idx].size) {
2634 ret = mmc_blk_alloc_part(card, md,
2635 card->part[idx].part_cfg,
2636 card->part[idx].size >> 9,
2637 card->part[idx].force_ro,
2638 card->part[idx].name,
2639 card->part[idx].area_type);
2648 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2650 struct mmc_card *card;
2654 * Flush remaining requests and free queues. It
2655 * is freeing the queue that stops new requests
2656 * from being accepted.
2658 card = md->queue.card;
2659 if (md->disk->flags & GENHD_FL_UP) {
2660 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2661 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2662 card->ext_csd.boot_ro_lockable)
2663 device_remove_file(disk_to_dev(md->disk),
2664 &md->power_ro_lock);
2666 del_gendisk(md->disk);
2668 mmc_cleanup_queue(&md->queue);
2673 static void mmc_blk_remove_parts(struct mmc_card *card,
2674 struct mmc_blk_data *md)
2676 struct list_head *pos, *q;
2677 struct mmc_blk_data *part_md;
2678 struct mmc_rpmb_data *rpmb;
2680 /* Remove RPMB partitions */
2681 list_for_each_safe(pos, q, &md->rpmbs) {
2682 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
2684 mmc_blk_remove_rpmb_part(rpmb);
2686 /* Remove block partitions */
2687 list_for_each_safe(pos, q, &md->part) {
2688 part_md = list_entry(pos, struct mmc_blk_data, part);
2690 mmc_blk_remove_req(part_md);
2694 static int mmc_add_disk(struct mmc_blk_data *md)
2697 struct mmc_card *card = md->queue.card;
2699 device_add_disk(md->parent, md->disk, NULL);
2700 md->force_ro.show = force_ro_show;
2701 md->force_ro.store = force_ro_store;
2702 sysfs_attr_init(&md->force_ro.attr);
2703 md->force_ro.attr.name = "force_ro";
2704 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2705 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2709 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2710 card->ext_csd.boot_ro_lockable) {
2713 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2716 mode = S_IRUGO | S_IWUSR;
2718 md->power_ro_lock.show = power_ro_lock_show;
2719 md->power_ro_lock.store = power_ro_lock_store;
2720 sysfs_attr_init(&md->power_ro_lock.attr);
2721 md->power_ro_lock.attr.mode = mode;
2722 md->power_ro_lock.attr.name =
2723 "ro_lock_until_next_power_on";
2724 ret = device_create_file(disk_to_dev(md->disk),
2725 &md->power_ro_lock);
2727 goto power_ro_lock_fail;
2732 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2734 del_gendisk(md->disk);
2739 #ifdef CONFIG_DEBUG_FS
2741 static int mmc_dbg_card_status_get(void *data, u64 *val)
2743 struct mmc_card *card = data;
2744 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2745 struct mmc_queue *mq = &md->queue;
2746 struct request *req;
2749 /* Ask the block layer about the card status */
2750 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0);
2752 return PTR_ERR(req);
2753 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2754 blk_execute_rq(NULL, req, 0);
2755 ret = req_to_mmc_queue_req(req)->drv_op_result;
2760 blk_put_request(req);
2764 DEFINE_DEBUGFS_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2767 /* That is two digits * 512 + 1 for newline */
2768 #define EXT_CSD_STR_LEN 1025
2770 static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2772 struct mmc_card *card = inode->i_private;
2773 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2774 struct mmc_queue *mq = &md->queue;
2775 struct request *req;
2781 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2785 /* Ask the block layer for the EXT CSD */
2786 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0);
2791 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2792 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2793 blk_execute_rq(NULL, req, 0);
2794 err = req_to_mmc_queue_req(req)->drv_op_result;
2795 blk_put_request(req);
2797 pr_err("FAILED %d\n", err);
2801 for (i = 0; i < 512; i++)
2802 n += sprintf(buf + n, "%02x", ext_csd[i]);
2803 n += sprintf(buf + n, "\n");
2805 if (n != EXT_CSD_STR_LEN) {
2811 filp->private_data = buf;
2820 static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2821 size_t cnt, loff_t *ppos)
2823 char *buf = filp->private_data;
2825 return simple_read_from_buffer(ubuf, cnt, ppos,
2826 buf, EXT_CSD_STR_LEN);
2829 static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2831 kfree(file->private_data);
2835 static const struct file_operations mmc_dbg_ext_csd_fops = {
2836 .open = mmc_ext_csd_open,
2837 .read = mmc_ext_csd_read,
2838 .release = mmc_ext_csd_release,
2839 .llseek = default_llseek,
2842 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2844 struct dentry *root;
2846 if (!card->debugfs_root)
2849 root = card->debugfs_root;
2851 if (mmc_card_mmc(card) || mmc_card_sd(card)) {
2853 debugfs_create_file_unsafe("status", 0400, root,
2855 &mmc_dbg_card_status_fops);
2856 if (!md->status_dentry)
2860 if (mmc_card_mmc(card)) {
2861 md->ext_csd_dentry =
2862 debugfs_create_file("ext_csd", S_IRUSR, root, card,
2863 &mmc_dbg_ext_csd_fops);
2864 if (!md->ext_csd_dentry)
2871 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2872 struct mmc_blk_data *md)
2874 if (!card->debugfs_root)
2877 if (!IS_ERR_OR_NULL(md->status_dentry)) {
2878 debugfs_remove(md->status_dentry);
2879 md->status_dentry = NULL;
2882 if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) {
2883 debugfs_remove(md->ext_csd_dentry);
2884 md->ext_csd_dentry = NULL;
2890 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2895 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2896 struct mmc_blk_data *md)
2900 #endif /* CONFIG_DEBUG_FS */
2902 static int mmc_blk_probe(struct mmc_card *card)
2904 struct mmc_blk_data *md, *part_md;
2908 * Check that the card supports the command class(es) we need.
2910 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2913 mmc_fixup_device(card, mmc_blk_fixups);
2915 card->complete_wq = alloc_workqueue("mmc_complete",
2916 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2917 if (!card->complete_wq) {
2918 pr_err("Failed to create mmc completion workqueue");
2922 md = mmc_blk_alloc(card);
2928 ret = mmc_blk_alloc_parts(card, md);
2932 dev_set_drvdata(&card->dev, md);
2934 ret = mmc_add_disk(md);
2938 list_for_each_entry(part_md, &md->part, part) {
2939 ret = mmc_add_disk(part_md);
2944 /* Add two debugfs entries */
2945 mmc_blk_add_debugfs(card, md);
2947 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2948 pm_runtime_use_autosuspend(&card->dev);
2951 * Don't enable runtime PM for SD-combo cards here. Leave that
2952 * decision to be taken during the SDIO init sequence instead.
2954 if (card->type != MMC_TYPE_SD_COMBO) {
2955 pm_runtime_set_active(&card->dev);
2956 pm_runtime_enable(&card->dev);
2962 mmc_blk_remove_parts(card, md);
2963 mmc_blk_remove_req(md);
2965 destroy_workqueue(card->complete_wq);
2969 static void mmc_blk_remove(struct mmc_card *card)
2971 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2973 mmc_blk_remove_debugfs(card, md);
2974 mmc_blk_remove_parts(card, md);
2975 pm_runtime_get_sync(&card->dev);
2976 if (md->part_curr != md->part_type) {
2977 mmc_claim_host(card->host);
2978 mmc_blk_part_switch(card, md->part_type);
2979 mmc_release_host(card->host);
2981 if (card->type != MMC_TYPE_SD_COMBO)
2982 pm_runtime_disable(&card->dev);
2983 pm_runtime_put_noidle(&card->dev);
2984 mmc_blk_remove_req(md);
2985 dev_set_drvdata(&card->dev, NULL);
2986 destroy_workqueue(card->complete_wq);
2989 static int _mmc_blk_suspend(struct mmc_card *card)
2991 struct mmc_blk_data *part_md;
2992 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2995 mmc_queue_suspend(&md->queue);
2996 list_for_each_entry(part_md, &md->part, part) {
2997 mmc_queue_suspend(&part_md->queue);
3003 static void mmc_blk_shutdown(struct mmc_card *card)
3005 _mmc_blk_suspend(card);
3008 #ifdef CONFIG_PM_SLEEP
3009 static int mmc_blk_suspend(struct device *dev)
3011 struct mmc_card *card = mmc_dev_to_card(dev);
3013 return _mmc_blk_suspend(card);
3016 static int mmc_blk_resume(struct device *dev)
3018 struct mmc_blk_data *part_md;
3019 struct mmc_blk_data *md = dev_get_drvdata(dev);
3023 * Resume involves the card going into idle state,
3024 * so current partition is always the main one.
3026 md->part_curr = md->part_type;
3027 mmc_queue_resume(&md->queue);
3028 list_for_each_entry(part_md, &md->part, part) {
3029 mmc_queue_resume(&part_md->queue);
3036 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3038 static struct mmc_driver mmc_driver = {
3041 .pm = &mmc_blk_pm_ops,
3043 .probe = mmc_blk_probe,
3044 .remove = mmc_blk_remove,
3045 .shutdown = mmc_blk_shutdown,
3048 static int __init mmc_blk_init(void)
3052 res = bus_register(&mmc_rpmb_bus_type);
3054 pr_err("mmcblk: could not register RPMB bus type\n");
3057 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
3059 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
3063 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3064 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3066 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
3068 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3070 goto out_chrdev_unreg;
3072 res = mmc_register_driver(&mmc_driver);
3074 goto out_blkdev_unreg;
3079 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3081 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3083 bus_unregister(&mmc_rpmb_bus_type);
3087 static void __exit mmc_blk_exit(void)
3089 mmc_unregister_driver(&mmc_driver);
3090 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3091 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3092 bus_unregister(&mmc_rpmb_bus_type);
3095 module_init(mmc_blk_init);
3096 module_exit(mmc_blk_exit);
3098 MODULE_LICENSE("GPL");
3099 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");