2 * Block driver for media (i.e., flash cards)
4 * Copyright 2002 Hewlett-Packard Company
5 * Copyright 2005-2008 Pierre Ossman
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
17 * Author: Andrew Christian
20 #include <linux/moduleparam.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
24 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/errno.h>
28 #include <linux/hdreg.h>
29 #include <linux/kdev_t.h>
30 #include <linux/blkdev.h>
31 #include <linux/cdev.h>
32 #include <linux/mutex.h>
33 #include <linux/scatterlist.h>
34 #include <linux/string_helpers.h>
35 #include <linux/delay.h>
36 #include <linux/capability.h>
37 #include <linux/compat.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/idr.h>
40 #include <linux/debugfs.h>
42 #include <linux/mmc/ioctl.h>
43 #include <linux/mmc/card.h>
44 #include <linux/mmc/host.h>
45 #include <linux/mmc/mmc.h>
46 #include <linux/mmc/sd.h>
48 #include <linux/uaccess.h>
60 MODULE_ALIAS("mmc:block");
61 #ifdef MODULE_PARAM_PREFIX
62 #undef MODULE_PARAM_PREFIX
64 #define MODULE_PARAM_PREFIX "mmcblk."
67 * Set a 10 second timeout for polling write request busy state. Note, mmc core
68 * is setting a 3 second timeout for SD cards, and SDHCI has long had a 10
69 * second software timer to timeout the whole request, so 10 seconds should be
72 #define MMC_BLK_TIMEOUT_MS (10 * 1000)
73 #define MMC_SANITIZE_REQ_TIMEOUT 240000
74 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
75 #define MMC_EXTRACT_VALUE_FROM_ARG(x) ((x & 0x0000FF00) >> 8)
77 #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
78 (rq_data_dir(req) == WRITE))
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);
100 * There is one mmc_blk_data per slot.
102 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);
173 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
175 struct mmc_blk_data *md;
177 mutex_lock(&open_lock);
178 md = disk->private_data;
179 if (md && md->usage == 0)
183 mutex_unlock(&open_lock);
188 static inline int mmc_get_devidx(struct gendisk *disk)
190 int devidx = disk->first_minor / perdev_minors;
194 static void mmc_blk_put(struct mmc_blk_data *md)
196 mutex_lock(&open_lock);
198 if (md->usage == 0) {
199 int devidx = mmc_get_devidx(md->disk);
200 blk_put_queue(md->queue.queue);
201 ida_simple_remove(&mmc_blk_ida, devidx);
205 mutex_unlock(&open_lock);
208 static ssize_t power_ro_lock_show(struct device *dev,
209 struct device_attribute *attr, char *buf)
212 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
213 struct mmc_card *card = md->queue.card;
216 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
218 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
221 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
228 static ssize_t power_ro_lock_store(struct device *dev,
229 struct device_attribute *attr, const char *buf, size_t count)
232 struct mmc_blk_data *md, *part_md;
233 struct mmc_queue *mq;
237 if (kstrtoul(buf, 0, &set))
243 md = mmc_blk_get(dev_to_disk(dev));
246 /* Dispatch locking to the block layer */
247 req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, __GFP_RECLAIM);
249 count = PTR_ERR(req);
252 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
253 blk_execute_rq(mq->queue, NULL, req, 0);
254 ret = req_to_mmc_queue_req(req)->drv_op_result;
255 blk_put_request(req);
258 pr_info("%s: Locking boot partition ro until next power on\n",
259 md->disk->disk_name);
260 set_disk_ro(md->disk, 1);
262 list_for_each_entry(part_md, &md->part, part)
263 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
264 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
265 set_disk_ro(part_md->disk, 1);
273 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
277 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
279 ret = snprintf(buf, PAGE_SIZE, "%d\n",
280 get_disk_ro(dev_to_disk(dev)) ^
286 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
287 const char *buf, size_t count)
291 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
292 unsigned long set = simple_strtoul(buf, &end, 0);
298 set_disk_ro(dev_to_disk(dev), set || md->read_only);
305 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
307 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
310 mutex_lock(&block_mutex);
313 check_disk_change(bdev);
316 if ((mode & FMODE_WRITE) && md->read_only) {
321 mutex_unlock(&block_mutex);
326 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
328 struct mmc_blk_data *md = disk->private_data;
330 mutex_lock(&block_mutex);
332 mutex_unlock(&block_mutex);
336 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
338 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
344 struct mmc_blk_ioc_data {
345 struct mmc_ioc_cmd ic;
348 struct mmc_rpmb_data *rpmb;
351 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
352 struct mmc_ioc_cmd __user *user)
354 struct mmc_blk_ioc_data *idata;
357 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
363 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
368 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
369 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
374 if (!idata->buf_bytes) {
379 idata->buf = memdup_user((void __user *)(unsigned long)
380 idata->ic.data_ptr, idata->buf_bytes);
381 if (IS_ERR(idata->buf)) {
382 err = PTR_ERR(idata->buf);
394 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
395 struct mmc_blk_ioc_data *idata)
397 struct mmc_ioc_cmd *ic = &idata->ic;
399 if (copy_to_user(&(ic_ptr->response), ic->response,
400 sizeof(ic->response)))
403 if (!idata->ic.write_flag) {
404 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
405 idata->buf, idata->buf_bytes))
412 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
418 if (!status || !retries_max)
422 err = __mmc_send_status(card, status, 5);
426 if (!R1_STATUS(*status) &&
427 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
428 break; /* RPMB programming operation complete */
431 * Rechedule to give the MMC device a chance to continue
432 * processing the previous command without being polled too
435 usleep_range(1000, 5000);
436 } while (++retry_count < retries_max);
438 if (retry_count == retries_max)
444 static int ioctl_do_sanitize(struct mmc_card *card)
448 if (!mmc_can_sanitize(card)) {
449 pr_warn("%s: %s - SANITIZE is not supported\n",
450 mmc_hostname(card->host), __func__);
455 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
456 mmc_hostname(card->host), __func__);
458 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
459 EXT_CSD_SANITIZE_START, 1,
460 MMC_SANITIZE_REQ_TIMEOUT);
463 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
464 mmc_hostname(card->host), __func__, err);
466 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
472 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
473 struct mmc_blk_ioc_data *idata)
475 struct mmc_command cmd = {};
476 struct mmc_data data = {};
477 struct mmc_request mrq = {};
478 struct scatterlist sg;
480 unsigned int target_part;
483 if (!card || !md || !idata)
487 * The RPMB accesses comes in from the character device, so we
488 * need to target these explicitly. Else we just target the
489 * partition type for the block device the ioctl() was issued
493 /* Support multiple RPMB partitions */
494 target_part = idata->rpmb->part_index;
495 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
497 target_part = md->part_type;
500 cmd.opcode = idata->ic.opcode;
501 cmd.arg = idata->ic.arg;
502 cmd.flags = idata->ic.flags;
504 if (idata->buf_bytes) {
507 data.blksz = idata->ic.blksz;
508 data.blocks = idata->ic.blocks;
510 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
512 if (idata->ic.write_flag)
513 data.flags = MMC_DATA_WRITE;
515 data.flags = MMC_DATA_READ;
517 /* data.flags must already be set before doing this. */
518 mmc_set_data_timeout(&data, card);
520 /* Allow overriding the timeout_ns for empirical tuning. */
521 if (idata->ic.data_timeout_ns)
522 data.timeout_ns = idata->ic.data_timeout_ns;
524 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
526 * Pretend this is a data transfer and rely on the
527 * host driver to compute timeout. When all host
528 * drivers support cmd.cmd_timeout for R1B, this
532 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
534 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
542 err = mmc_blk_part_switch(card, target_part);
546 if (idata->ic.is_acmd) {
547 err = mmc_app_cmd(card->host, card);
553 err = mmc_set_blockcount(card, data.blocks,
554 idata->ic.write_flag & (1 << 31));
559 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
560 (cmd.opcode == MMC_SWITCH)) {
561 err = ioctl_do_sanitize(card);
564 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
570 mmc_wait_for_req(card->host, &mrq);
573 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
574 __func__, cmd.error);
578 dev_err(mmc_dev(card->host), "%s: data error %d\n",
579 __func__, data.error);
584 * Make sure the cache of the PARTITION_CONFIG register and
585 * PARTITION_ACCESS bits is updated in case the ioctl ext_csd write
586 * changed it successfully.
588 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_PART_CONFIG) &&
589 (cmd.opcode == MMC_SWITCH)) {
590 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
591 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg);
594 * Update cache so the next mmc_blk_part_switch call operates
595 * on up-to-date data.
597 card->ext_csd.part_config = value;
598 main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
602 * According to the SD specs, some commands require a delay after
603 * issuing the command.
605 if (idata->ic.postsleep_min_us)
606 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
608 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
612 * Ensure RPMB command has completed by polling CMD13
615 err = ioctl_rpmb_card_status_poll(card, &status, 5);
617 dev_err(mmc_dev(card->host),
618 "%s: Card Status=0x%08X, error %d\n",
619 __func__, status, err);
625 static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
626 struct mmc_ioc_cmd __user *ic_ptr,
627 struct mmc_rpmb_data *rpmb)
629 struct mmc_blk_ioc_data *idata;
630 struct mmc_blk_ioc_data *idatas[1];
631 struct mmc_queue *mq;
632 struct mmc_card *card;
633 int err = 0, ioc_err = 0;
636 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
638 return PTR_ERR(idata);
639 /* This will be NULL on non-RPMB ioctl():s */
642 card = md->queue.card;
649 * Dispatch the ioctl() into the block request queue.
652 req = blk_get_request(mq->queue,
653 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
660 req_to_mmc_queue_req(req)->drv_op =
661 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
662 req_to_mmc_queue_req(req)->drv_op_data = idatas;
663 req_to_mmc_queue_req(req)->ioc_count = 1;
664 blk_execute_rq(mq->queue, NULL, req, 0);
665 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
666 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
667 blk_put_request(req);
672 return ioc_err ? ioc_err : err;
675 static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
676 struct mmc_ioc_multi_cmd __user *user,
677 struct mmc_rpmb_data *rpmb)
679 struct mmc_blk_ioc_data **idata = NULL;
680 struct mmc_ioc_cmd __user *cmds = user->cmds;
681 struct mmc_card *card;
682 struct mmc_queue *mq;
683 int i, err = 0, ioc_err = 0;
687 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
688 sizeof(num_of_cmds)))
694 if (num_of_cmds > MMC_IOC_MAX_CMDS)
697 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
701 for (i = 0; i < num_of_cmds; i++) {
702 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
703 if (IS_ERR(idata[i])) {
704 err = PTR_ERR(idata[i]);
708 /* This will be NULL on non-RPMB ioctl():s */
709 idata[i]->rpmb = rpmb;
712 card = md->queue.card;
720 * Dispatch the ioctl()s into the block request queue.
723 req = blk_get_request(mq->queue,
724 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
730 req_to_mmc_queue_req(req)->drv_op =
731 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
732 req_to_mmc_queue_req(req)->drv_op_data = idata;
733 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
734 blk_execute_rq(mq->queue, NULL, req, 0);
735 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
737 /* copy to user if data and response */
738 for (i = 0; i < num_of_cmds && !err; i++)
739 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
741 blk_put_request(req);
744 for (i = 0; i < num_of_cmds; i++) {
745 kfree(idata[i]->buf);
749 return ioc_err ? ioc_err : err;
752 static int mmc_blk_check_blkdev(struct block_device *bdev)
755 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
756 * whole block device, not on a partition. This prevents overspray
757 * between sibling partitions.
759 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
764 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
765 unsigned int cmd, unsigned long arg)
767 struct mmc_blk_data *md;
772 ret = mmc_blk_check_blkdev(bdev);
775 md = mmc_blk_get(bdev->bd_disk);
778 ret = mmc_blk_ioctl_cmd(md,
779 (struct mmc_ioc_cmd __user *)arg,
783 case MMC_IOC_MULTI_CMD:
784 ret = mmc_blk_check_blkdev(bdev);
787 md = mmc_blk_get(bdev->bd_disk);
790 ret = mmc_blk_ioctl_multi_cmd(md,
791 (struct mmc_ioc_multi_cmd __user *)arg,
801 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
802 unsigned int cmd, unsigned long arg)
804 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
808 static const struct block_device_operations mmc_bdops = {
809 .open = mmc_blk_open,
810 .release = mmc_blk_release,
811 .getgeo = mmc_blk_getgeo,
812 .owner = THIS_MODULE,
813 .ioctl = mmc_blk_ioctl,
815 .compat_ioctl = mmc_blk_compat_ioctl,
819 static int mmc_blk_part_switch_pre(struct mmc_card *card,
820 unsigned int part_type)
824 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
825 if (card->ext_csd.cmdq_en) {
826 ret = mmc_cmdq_disable(card);
830 mmc_retune_pause(card->host);
836 static int mmc_blk_part_switch_post(struct mmc_card *card,
837 unsigned int part_type)
841 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
842 mmc_retune_unpause(card->host);
843 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
844 ret = mmc_cmdq_enable(card);
850 static inline int mmc_blk_part_switch(struct mmc_card *card,
851 unsigned int part_type)
854 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
856 if (main_md->part_curr == part_type)
859 if (mmc_card_mmc(card)) {
860 u8 part_config = card->ext_csd.part_config;
862 ret = mmc_blk_part_switch_pre(card, part_type);
866 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
867 part_config |= part_type;
869 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
870 EXT_CSD_PART_CONFIG, part_config,
871 card->ext_csd.part_time);
873 mmc_blk_part_switch_post(card, part_type);
877 card->ext_csd.part_config = part_config;
879 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
882 main_md->part_curr = part_type;
886 static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
892 struct mmc_request mrq = {};
893 struct mmc_command cmd = {};
894 struct mmc_data data = {};
896 struct scatterlist sg;
898 cmd.opcode = MMC_APP_CMD;
899 cmd.arg = card->rca << 16;
900 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
902 err = mmc_wait_for_cmd(card->host, &cmd, 0);
905 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
908 memset(&cmd, 0, sizeof(struct mmc_command));
910 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
912 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
916 data.flags = MMC_DATA_READ;
919 mmc_set_data_timeout(&data, card);
924 blocks = kmalloc(4, GFP_KERNEL);
928 sg_init_one(&sg, blocks, 4);
930 mmc_wait_for_req(card->host, &mrq);
932 result = ntohl(*blocks);
935 if (cmd.error || data.error)
938 *written_blocks = result;
943 static unsigned int mmc_blk_clock_khz(struct mmc_host *host)
945 if (host->actual_clock)
946 return host->actual_clock / 1000;
948 /* Clock may be subject to a divisor, fudge it by a factor of 2. */
950 return host->ios.clock / 2000;
952 /* How can there be no clock */
954 return 100; /* 100 kHz is minimum possible value */
957 static unsigned int mmc_blk_data_timeout_ms(struct mmc_host *host,
958 struct mmc_data *data)
960 unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000);
963 if (data->timeout_clks) {
964 khz = mmc_blk_clock_khz(host);
965 ms += DIV_ROUND_UP(data->timeout_clks, khz);
971 static inline bool mmc_blk_in_tran_state(u32 status)
974 * Some cards mishandle the status bits, so make sure to check both the
975 * busy indication and the card state.
977 return status & R1_READY_FOR_DATA &&
978 (R1_CURRENT_STATE(status) == R1_STATE_TRAN);
981 static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
982 struct request *req, u32 *resp_errs)
984 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
989 bool done = time_after(jiffies, timeout);
991 err = __mmc_send_status(card, &status, 5);
993 pr_err("%s: error %d requesting status\n",
994 req->rq_disk->disk_name, err);
998 /* Accumulate any response error bits seen */
1000 *resp_errs |= status;
1003 * Timeout if the device never becomes ready for data and never
1004 * leaves the program state.
1007 pr_err("%s: Card stuck in wrong state! %s %s status: %#x\n",
1008 mmc_hostname(card->host),
1009 req->rq_disk->disk_name, __func__, status);
1014 * Some cards mishandle the status bits,
1015 * so make sure to check both the busy
1016 * indication and the card state.
1018 } while (!mmc_blk_in_tran_state(status));
1023 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1028 if (md->reset_done & type)
1031 md->reset_done |= type;
1032 err = mmc_hw_reset(host);
1033 /* Ensure we switch back to the correct partition */
1034 if (err != -EOPNOTSUPP) {
1035 struct mmc_blk_data *main_md =
1036 dev_get_drvdata(&host->card->dev);
1039 main_md->part_curr = main_md->part_type;
1040 part_err = mmc_blk_part_switch(host->card, md->part_type);
1043 * We have failed to get back into the correct
1044 * partition, so we need to abort the whole request.
1052 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1054 md->reset_done &= ~type;
1058 * The non-block commands come back from the block layer after it queued it and
1059 * processed it with all other requests and then they get issued in this
1062 static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1064 struct mmc_queue_req *mq_rq;
1065 struct mmc_card *card = mq->card;
1066 struct mmc_blk_data *md = mq->blkdata;
1067 struct mmc_blk_ioc_data **idata;
1074 mq_rq = req_to_mmc_queue_req(req);
1075 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
1077 switch (mq_rq->drv_op) {
1078 case MMC_DRV_OP_IOCTL:
1079 case MMC_DRV_OP_IOCTL_RPMB:
1080 idata = mq_rq->drv_op_data;
1081 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
1082 ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
1086 /* Always switch back to main area after RPMB access */
1088 mmc_blk_part_switch(card, 0);
1090 case MMC_DRV_OP_BOOT_WP:
1091 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1092 card->ext_csd.boot_ro_lock |
1093 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1094 card->ext_csd.part_time);
1096 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1097 md->disk->disk_name, ret);
1099 card->ext_csd.boot_ro_lock |=
1100 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
1102 case MMC_DRV_OP_GET_CARD_STATUS:
1103 ret = mmc_send_status(card, &status);
1107 case MMC_DRV_OP_GET_EXT_CSD:
1108 ext_csd = mq_rq->drv_op_data;
1109 ret = mmc_get_ext_csd(card, ext_csd);
1112 pr_err("%s: unknown driver specific operation\n",
1113 md->disk->disk_name);
1117 mq_rq->drv_op_result = ret;
1118 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1121 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1123 struct mmc_blk_data *md = mq->blkdata;
1124 struct mmc_card *card = md->queue.card;
1125 unsigned int from, nr, arg;
1126 int err = 0, type = MMC_BLK_DISCARD;
1127 blk_status_t status = BLK_STS_OK;
1129 if (!mmc_can_erase(card)) {
1130 status = BLK_STS_NOTSUPP;
1134 from = blk_rq_pos(req);
1135 nr = blk_rq_sectors(req);
1137 if (mmc_can_discard(card))
1138 arg = MMC_DISCARD_ARG;
1139 else if (mmc_can_trim(card))
1142 arg = MMC_ERASE_ARG;
1145 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1146 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1147 INAND_CMD38_ARG_EXT_CSD,
1148 arg == MMC_TRIM_ARG ?
1149 INAND_CMD38_ARG_TRIM :
1150 INAND_CMD38_ARG_ERASE,
1154 err = mmc_erase(card, from, nr, arg);
1155 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
1157 status = BLK_STS_IOERR;
1159 mmc_blk_reset_success(md, type);
1161 blk_mq_end_request(req, status);
1164 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1165 struct request *req)
1167 struct mmc_blk_data *md = mq->blkdata;
1168 struct mmc_card *card = md->queue.card;
1169 unsigned int from, nr, arg;
1170 int err = 0, type = MMC_BLK_SECDISCARD;
1171 blk_status_t status = BLK_STS_OK;
1173 if (!(mmc_can_secure_erase_trim(card))) {
1174 status = BLK_STS_NOTSUPP;
1178 from = blk_rq_pos(req);
1179 nr = blk_rq_sectors(req);
1181 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1182 arg = MMC_SECURE_TRIM1_ARG;
1184 arg = MMC_SECURE_ERASE_ARG;
1187 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1188 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1189 INAND_CMD38_ARG_EXT_CSD,
1190 arg == MMC_SECURE_TRIM1_ARG ?
1191 INAND_CMD38_ARG_SECTRIM1 :
1192 INAND_CMD38_ARG_SECERASE,
1198 err = mmc_erase(card, from, nr, arg);
1202 status = BLK_STS_IOERR;
1206 if (arg == MMC_SECURE_TRIM1_ARG) {
1207 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1208 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1209 INAND_CMD38_ARG_EXT_CSD,
1210 INAND_CMD38_ARG_SECTRIM2,
1216 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1220 status = BLK_STS_IOERR;
1226 if (err && !mmc_blk_reset(md, card->host, type))
1229 mmc_blk_reset_success(md, type);
1231 blk_mq_end_request(req, status);
1234 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1236 struct mmc_blk_data *md = mq->blkdata;
1237 struct mmc_card *card = md->queue.card;
1240 ret = mmc_flush_cache(card);
1241 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1245 * Reformat current write as a reliable write, supporting
1246 * both legacy and the enhanced reliable write MMC cards.
1247 * In each transfer we'll handle only as much as a single
1248 * reliable write can handle, thus finish the request in
1249 * partial completions.
1251 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1252 struct mmc_card *card,
1253 struct request *req)
1255 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1256 /* Legacy mode imposes restrictions on transfers. */
1257 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
1258 brq->data.blocks = 1;
1260 if (brq->data.blocks > card->ext_csd.rel_sectors)
1261 brq->data.blocks = card->ext_csd.rel_sectors;
1262 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1263 brq->data.blocks = 1;
1267 #define CMD_ERRORS_EXCL_OOR \
1268 (R1_ADDRESS_ERROR | /* Misaligned address */ \
1269 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1270 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1271 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
1272 R1_CC_ERROR | /* Card controller error */ \
1273 R1_ERROR) /* General/unknown error */
1275 #define CMD_ERRORS \
1276 (CMD_ERRORS_EXCL_OOR | \
1277 R1_OUT_OF_RANGE) /* Command argument out of range */ \
1279 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
1284 * Per the SD specification(physical layer version 4.10)[1],
1285 * section 4.3.3, it explicitly states that "When the last
1286 * block of user area is read using CMD18, the host should
1287 * ignore OUT_OF_RANGE error that may occur even the sequence
1288 * is correct". And JESD84-B51 for eMMC also has a similar
1289 * statement on section 6.8.3.
1291 * Multiple block read/write could be done by either predefined
1292 * method, namely CMD23, or open-ending mode. For open-ending mode,
1293 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1295 * However the spec[1] doesn't tell us whether we should also
1296 * ignore that for predefined method. But per the spec[1], section
1297 * 4.15 Set Block Count Command, it says"If illegal block count
1298 * is set, out of range error will be indicated during read/write
1299 * operation (For example, data transfer is stopped at user area
1300 * boundary)." In another word, we could expect a out of range error
1301 * in the response for the following CMD18/25. And if argument of
1302 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1303 * we could also expect to get a -ETIMEDOUT or any error number from
1304 * the host drivers due to missing data response(for write)/data(for
1305 * read), as the cards will stop the data transfer by itself per the
1306 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1309 if (!brq->stop.error) {
1310 bool oor_with_open_end;
1311 /* If there is no error yet, check R1 response */
1313 val = brq->stop.resp[0] & CMD_ERRORS;
1314 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1316 if (val && !oor_with_open_end)
1317 brq->stop.error = -EIO;
1321 static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
1322 int disable_multi, bool *do_rel_wr_p,
1323 bool *do_data_tag_p)
1325 struct mmc_blk_data *md = mq->blkdata;
1326 struct mmc_card *card = md->queue.card;
1327 struct mmc_blk_request *brq = &mqrq->brq;
1328 struct request *req = mmc_queue_req_to_req(mqrq);
1329 bool do_rel_wr, do_data_tag;
1332 * Reliable writes are used to implement Forced Unit Access and
1333 * are supported only on MMCs.
1335 do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1336 rq_data_dir(req) == WRITE &&
1337 (md->flags & MMC_BLK_REL_WR);
1339 memset(brq, 0, sizeof(struct mmc_blk_request));
1341 brq->mrq.data = &brq->data;
1342 brq->mrq.tag = req->tag;
1344 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1347 if (rq_data_dir(req) == READ) {
1348 brq->data.flags = MMC_DATA_READ;
1349 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1351 brq->data.flags = MMC_DATA_WRITE;
1352 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1355 brq->data.blksz = 512;
1356 brq->data.blocks = blk_rq_sectors(req);
1357 brq->data.blk_addr = blk_rq_pos(req);
1360 * The command queue supports 2 priorities: "high" (1) and "simple" (0).
1361 * The eMMC will give "high" priority tasks priority over "simple"
1362 * priority tasks. Here we always set "simple" priority by not setting
1367 * The block layer doesn't support all sector count
1368 * restrictions, so we need to be prepared for too big
1371 if (brq->data.blocks > card->host->max_blk_count)
1372 brq->data.blocks = card->host->max_blk_count;
1374 if (brq->data.blocks > 1) {
1376 * After a read error, we redo the request one sector
1377 * at a time in order to accurately determine which
1378 * sectors can be read successfully.
1381 brq->data.blocks = 1;
1384 * Some controllers have HW issues while operating
1385 * in multiple I/O mode
1387 if (card->host->ops->multi_io_quirk)
1388 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1389 (rq_data_dir(req) == READ) ?
1390 MMC_DATA_READ : MMC_DATA_WRITE,
1395 mmc_apply_rel_rw(brq, card, req);
1396 brq->data.flags |= MMC_DATA_REL_WR;
1400 * Data tag is used only during writing meta data to speed
1401 * up write and any subsequent read of this meta data
1403 do_data_tag = card->ext_csd.data_tag_unit_size &&
1404 (req->cmd_flags & REQ_META) &&
1405 (rq_data_dir(req) == WRITE) &&
1406 ((brq->data.blocks * brq->data.blksz) >=
1407 card->ext_csd.data_tag_unit_size);
1410 brq->data.flags |= MMC_DATA_DAT_TAG;
1412 mmc_set_data_timeout(&brq->data, card);
1414 brq->data.sg = mqrq->sg;
1415 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1418 * Adjust the sg list so it is the same size as the
1421 if (brq->data.blocks != blk_rq_sectors(req)) {
1422 int i, data_size = brq->data.blocks << 9;
1423 struct scatterlist *sg;
1425 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1426 data_size -= sg->length;
1427 if (data_size <= 0) {
1428 sg->length += data_size;
1433 brq->data.sg_len = i;
1437 *do_rel_wr_p = do_rel_wr;
1440 *do_data_tag_p = do_data_tag;
1443 #define MMC_CQE_RETRIES 2
1445 static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req)
1447 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1448 struct mmc_request *mrq = &mqrq->brq.mrq;
1449 struct request_queue *q = req->q;
1450 struct mmc_host *host = mq->card->host;
1451 unsigned long flags;
1455 mmc_cqe_post_req(host, mrq);
1457 if (mrq->cmd && mrq->cmd->error)
1458 err = mrq->cmd->error;
1459 else if (mrq->data && mrq->data->error)
1460 err = mrq->data->error;
1465 if (mqrq->retries++ < MMC_CQE_RETRIES)
1466 blk_mq_requeue_request(req, true);
1468 blk_mq_end_request(req, BLK_STS_IOERR);
1469 } else if (mrq->data) {
1470 if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered))
1471 blk_mq_requeue_request(req, true);
1473 __blk_mq_end_request(req, BLK_STS_OK);
1475 blk_mq_end_request(req, BLK_STS_OK);
1478 spin_lock_irqsave(q->queue_lock, flags);
1480 mq->in_flight[mmc_issue_type(mq, req)] -= 1;
1482 put_card = (mmc_tot_in_flight(mq) == 0);
1484 mmc_cqe_check_busy(mq);
1486 spin_unlock_irqrestore(q->queue_lock, flags);
1489 blk_mq_run_hw_queues(q, true);
1492 mmc_put_card(mq->card, &mq->ctx);
1495 void mmc_blk_cqe_recovery(struct mmc_queue *mq)
1497 struct mmc_card *card = mq->card;
1498 struct mmc_host *host = card->host;
1501 pr_debug("%s: CQE recovery start\n", mmc_hostname(host));
1503 err = mmc_cqe_recovery(host);
1505 mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY);
1507 mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY);
1509 pr_debug("%s: CQE recovery done\n", mmc_hostname(host));
1512 static void mmc_blk_cqe_req_done(struct mmc_request *mrq)
1514 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
1516 struct request *req = mmc_queue_req_to_req(mqrq);
1517 struct request_queue *q = req->q;
1518 struct mmc_queue *mq = q->queuedata;
1521 * Block layer timeouts race with completions which means the normal
1522 * completion path cannot be used during recovery.
1524 if (mq->in_recovery)
1525 mmc_blk_cqe_complete_rq(mq, req);
1527 blk_mq_complete_request(req);
1530 static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
1532 mrq->done = mmc_blk_cqe_req_done;
1533 mrq->recovery_notifier = mmc_cqe_recovery_notifier;
1535 return mmc_cqe_start_req(host, mrq);
1538 static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq,
1539 struct request *req)
1541 struct mmc_blk_request *brq = &mqrq->brq;
1543 memset(brq, 0, sizeof(*brq));
1545 brq->mrq.cmd = &brq->cmd;
1546 brq->mrq.tag = req->tag;
1551 static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
1553 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1554 struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);
1556 mrq->cmd->opcode = MMC_SWITCH;
1557 mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1558 (EXT_CSD_FLUSH_CACHE << 16) |
1560 EXT_CSD_CMD_SET_NORMAL;
1561 mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B;
1563 return mmc_blk_cqe_start_req(mq->card->host, mrq);
1566 static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1568 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1570 mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL);
1572 return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq);
1575 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1576 struct mmc_card *card,
1578 struct mmc_queue *mq)
1580 u32 readcmd, writecmd;
1581 struct mmc_blk_request *brq = &mqrq->brq;
1582 struct request *req = mmc_queue_req_to_req(mqrq);
1583 struct mmc_blk_data *md = mq->blkdata;
1584 bool do_rel_wr, do_data_tag;
1586 mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1588 brq->mrq.cmd = &brq->cmd;
1590 brq->cmd.arg = blk_rq_pos(req);
1591 if (!mmc_card_blockaddr(card))
1593 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1595 if (brq->data.blocks > 1 || do_rel_wr) {
1596 /* SPI multiblock writes terminate using a special
1597 * token, not a STOP_TRANSMISSION request.
1599 if (!mmc_host_is_spi(card->host) ||
1600 rq_data_dir(req) == READ)
1601 brq->mrq.stop = &brq->stop;
1602 readcmd = MMC_READ_MULTIPLE_BLOCK;
1603 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1605 brq->mrq.stop = NULL;
1606 readcmd = MMC_READ_SINGLE_BLOCK;
1607 writecmd = MMC_WRITE_BLOCK;
1609 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
1612 * Pre-defined multi-block transfers are preferable to
1613 * open ended-ones (and necessary for reliable writes).
1614 * However, it is not sufficient to just send CMD23,
1615 * and avoid the final CMD12, as on an error condition
1616 * CMD12 (stop) needs to be sent anyway. This, coupled
1617 * with Auto-CMD23 enhancements provided by some
1618 * hosts, means that the complexity of dealing
1619 * with this is best left to the host. If CMD23 is
1620 * supported by card and host, we'll fill sbc in and let
1621 * the host deal with handling it correctly. This means
1622 * that for hosts that don't expose MMC_CAP_CMD23, no
1623 * change of behavior will be observed.
1625 * N.B: Some MMC cards experience perf degradation.
1626 * We'll avoid using CMD23-bounded multiblock writes for
1627 * these, while retaining features like reliable writes.
1629 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1630 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1632 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1633 brq->sbc.arg = brq->data.blocks |
1634 (do_rel_wr ? (1 << 31) : 0) |
1635 (do_data_tag ? (1 << 29) : 0);
1636 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1637 brq->mrq.sbc = &brq->sbc;
1641 #define MMC_MAX_RETRIES 5
1642 #define MMC_DATA_RETRIES 2
1643 #define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1)
1645 static int mmc_blk_send_stop(struct mmc_card *card, unsigned int timeout)
1647 struct mmc_command cmd = {
1648 .opcode = MMC_STOP_TRANSMISSION,
1649 .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC,
1650 /* Some hosts wait for busy anyway, so provide a busy timeout */
1651 .busy_timeout = timeout,
1654 return mmc_wait_for_cmd(card->host, &cmd, 5);
1657 static int mmc_blk_fix_state(struct mmc_card *card, struct request *req)
1659 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1660 struct mmc_blk_request *brq = &mqrq->brq;
1661 unsigned int timeout = mmc_blk_data_timeout_ms(card->host, &brq->data);
1664 mmc_retune_hold_now(card->host);
1666 mmc_blk_send_stop(card, timeout);
1668 err = card_busy_detect(card, timeout, req, NULL);
1670 mmc_retune_release(card->host);
1675 #define MMC_READ_SINGLE_RETRIES 2
1677 /* Single sector read during recovery */
1678 static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req)
1680 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1681 struct mmc_request *mrq = &mqrq->brq.mrq;
1682 struct mmc_card *card = mq->card;
1683 struct mmc_host *host = card->host;
1684 blk_status_t error = BLK_STS_OK;
1691 mmc_blk_rw_rq_prep(mqrq, card, 1, mq);
1693 mmc_wait_for_req(host, mrq);
1695 err = mmc_send_status(card, &status);
1699 if (!mmc_host_is_spi(host) &&
1700 !mmc_blk_in_tran_state(status)) {
1701 err = mmc_blk_fix_state(card, req);
1706 if (mrq->cmd->error && retries++ < MMC_READ_SINGLE_RETRIES)
1711 if (mrq->cmd->error ||
1713 (!mmc_host_is_spi(host) &&
1714 (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS)))
1715 error = BLK_STS_IOERR;
1719 } while (blk_update_request(req, error, 512));
1724 mrq->data->bytes_xfered = 0;
1725 blk_update_request(req, BLK_STS_IOERR, 512);
1726 /* Let it try the remaining request again */
1727 if (mqrq->retries > MMC_MAX_RETRIES - 1)
1728 mqrq->retries = MMC_MAX_RETRIES - 1;
1731 static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq)
1733 return !!brq->mrq.sbc;
1736 static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq)
1738 return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR;
1742 * Check for errors the host controller driver might not have seen such as
1743 * response mode errors or invalid card state.
1745 static bool mmc_blk_status_error(struct request *req, u32 status)
1747 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1748 struct mmc_blk_request *brq = &mqrq->brq;
1749 struct mmc_queue *mq = req->q->queuedata;
1752 if (mmc_host_is_spi(mq->card->host))
1755 stop_err_bits = mmc_blk_stop_err_bits(brq);
1757 return brq->cmd.resp[0] & CMD_ERRORS ||
1758 brq->stop.resp[0] & stop_err_bits ||
1759 status & stop_err_bits ||
1760 (rq_data_dir(req) == WRITE && !mmc_blk_in_tran_state(status));
1763 static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq)
1765 return !brq->sbc.error && !brq->cmd.error &&
1766 !(brq->cmd.resp[0] & CMD_ERRORS);
1770 * Requests are completed by mmc_blk_mq_complete_rq() which sets simple
1772 * 1. A request that has transferred at least some data is considered
1773 * successful and will be requeued if there is remaining data to
1775 * 2. Otherwise the number of retries is incremented and the request
1776 * will be requeued if there are remaining retries.
1777 * 3. Otherwise the request will be errored out.
1778 * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and
1779 * mqrq->retries. So there are only 4 possible actions here:
1780 * 1. do not accept the bytes_xfered value i.e. set it to zero
1781 * 2. change mqrq->retries to determine the number of retries
1782 * 3. try to reset the card
1783 * 4. read one sector at a time
1785 static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req)
1787 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1788 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1789 struct mmc_blk_request *brq = &mqrq->brq;
1790 struct mmc_blk_data *md = mq->blkdata;
1791 struct mmc_card *card = mq->card;
1797 * Some errors the host driver might not have seen. Set the number of
1798 * bytes transferred to zero in that case.
1800 err = __mmc_send_status(card, &status, 0);
1801 if (err || mmc_blk_status_error(req, status))
1802 brq->data.bytes_xfered = 0;
1804 mmc_retune_release(card->host);
1807 * Try again to get the status. This also provides an opportunity for
1811 err = __mmc_send_status(card, &status, 0);
1814 * Nothing more to do after the number of bytes transferred has been
1815 * updated and there is no card.
1817 if (err && mmc_detect_card_removed(card->host))
1820 /* Try to get back to "tran" state */
1821 if (!mmc_host_is_spi(mq->card->host) &&
1822 (err || !mmc_blk_in_tran_state(status)))
1823 err = mmc_blk_fix_state(mq->card, req);
1826 * Special case for SD cards where the card might record the number of
1829 if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) &&
1830 rq_data_dir(req) == WRITE) {
1831 if (mmc_sd_num_wr_blocks(card, &blocks))
1832 brq->data.bytes_xfered = 0;
1834 brq->data.bytes_xfered = blocks << 9;
1837 /* Reset if the card is in a bad state */
1838 if (!mmc_host_is_spi(mq->card->host) &&
1839 err && mmc_blk_reset(md, card->host, type)) {
1840 pr_err("%s: recovery failed!\n", req->rq_disk->disk_name);
1841 mqrq->retries = MMC_NO_RETRIES;
1846 * If anything was done, just return and if there is anything remaining
1847 * on the request it will get requeued.
1849 if (brq->data.bytes_xfered)
1852 /* Reset before last retry */
1853 if (mqrq->retries + 1 == MMC_MAX_RETRIES)
1854 mmc_blk_reset(md, card->host, type);
1856 /* Command errors fail fast, so use all MMC_MAX_RETRIES */
1857 if (brq->sbc.error || brq->cmd.error)
1860 /* Reduce the remaining retries for data errors */
1861 if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) {
1862 mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES;
1866 /* FIXME: Missing single sector read for large sector size */
1867 if (!mmc_large_sector(card) && rq_data_dir(req) == READ &&
1868 brq->data.blocks > 1) {
1869 /* Read one sector at a time */
1870 mmc_blk_read_single(mq, req);
1875 static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq)
1877 mmc_blk_eval_resp_error(brq);
1879 return brq->sbc.error || brq->cmd.error || brq->stop.error ||
1880 brq->data.error || brq->cmd.resp[0] & CMD_ERRORS;
1883 static int mmc_blk_card_busy(struct mmc_card *card, struct request *req)
1885 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1889 if (mmc_host_is_spi(card->host) || rq_data_dir(req) == READ)
1892 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, req, &status);
1895 * Do not assume data transferred correctly if there are any error bits
1898 if (status & mmc_blk_stop_err_bits(&mqrq->brq)) {
1899 mqrq->brq.data.bytes_xfered = 0;
1900 err = err ? err : -EIO;
1903 /* Copy the exception bit so it will be seen later on */
1904 if (mmc_card_mmc(card) && status & R1_EXCEPTION_EVENT)
1905 mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
1910 static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
1911 struct request *req)
1913 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1915 mmc_blk_reset_success(mq->blkdata, type);
1918 static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)
1920 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1921 unsigned int nr_bytes = mqrq->brq.data.bytes_xfered;
1924 if (blk_update_request(req, BLK_STS_OK, nr_bytes))
1925 blk_mq_requeue_request(req, true);
1927 __blk_mq_end_request(req, BLK_STS_OK);
1928 } else if (!blk_rq_bytes(req)) {
1929 __blk_mq_end_request(req, BLK_STS_IOERR);
1930 } else if (mqrq->retries++ < MMC_MAX_RETRIES) {
1931 blk_mq_requeue_request(req, true);
1933 if (mmc_card_removed(mq->card))
1934 req->rq_flags |= RQF_QUIET;
1935 blk_mq_end_request(req, BLK_STS_IOERR);
1939 static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
1940 struct mmc_queue_req *mqrq)
1942 return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) &&
1943 (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
1944 mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
1947 static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
1948 struct mmc_queue_req *mqrq)
1950 if (mmc_blk_urgent_bkops_needed(mq, mqrq))
1951 mmc_start_bkops(mq->card, true);
1954 void mmc_blk_mq_complete(struct request *req)
1956 struct mmc_queue *mq = req->q->queuedata;
1959 mmc_blk_cqe_complete_rq(mq, req);
1961 mmc_blk_mq_complete_rq(mq, req);
1964 static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,
1965 struct request *req)
1967 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1968 struct mmc_host *host = mq->card->host;
1970 if (mmc_blk_rq_error(&mqrq->brq) ||
1971 mmc_blk_card_busy(mq->card, req)) {
1972 mmc_blk_mq_rw_recovery(mq, req);
1974 mmc_blk_rw_reset_success(mq, req);
1975 mmc_retune_release(host);
1978 mmc_blk_urgent_bkops(mq, mqrq);
1981 static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, struct request *req)
1983 struct request_queue *q = req->q;
1984 unsigned long flags;
1987 spin_lock_irqsave(q->queue_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(q->queue_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);
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(q->queue_lock, flags);
2090 mq->complete_req = req;
2091 mq->rw_wait = false;
2092 waiting = mq->waiting;
2093 spin_unlock_irqrestore(q->queue_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 kblockd_schedule_work(&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(q->queue_lock, flags);
2113 mq->recovery_needed = true;
2114 mq->recovery_req = req;
2115 spin_unlock_irqrestore(q->queue_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 struct request_queue *q = mq->queue;
2132 unsigned long flags;
2136 * Wait while there is another request in progress, but not if recovery
2137 * is needed. Also indicate whether there is a request waiting to start.
2139 spin_lock_irqsave(q->queue_lock, flags);
2140 if (mq->recovery_needed) {
2144 done = !mq->rw_wait;
2146 mq->waiting = !done;
2147 spin_unlock_irqrestore(q->queue_lock, flags);
2152 static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req)
2156 wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err));
2158 /* Always complete the previous request if there is one */
2159 mmc_blk_mq_complete_prev_req(mq, prev_req);
2164 static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq,
2165 struct request *req)
2167 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2168 struct mmc_host *host = mq->card->host;
2169 struct request *prev_req = NULL;
2172 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
2174 mqrq->brq.mrq.done = mmc_blk_mq_req_done;
2176 mmc_pre_req(host, &mqrq->brq.mrq);
2178 err = mmc_blk_rw_wait(mq, &prev_req);
2184 err = mmc_start_request(host, &mqrq->brq.mrq);
2187 mmc_blk_mq_post_req(mq, prev_req);
2190 mq->rw_wait = false;
2192 /* Release re-tuning here where there is no synchronization required */
2193 if (err || mmc_host_done_complete(host))
2194 mmc_retune_release(host);
2198 mmc_post_req(host, &mqrq->brq.mrq, err);
2203 static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host)
2206 return host->cqe_ops->cqe_wait_for_idle(host);
2208 return mmc_blk_rw_wait(mq, NULL);
2211 enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
2213 struct mmc_blk_data *md = mq->blkdata;
2214 struct mmc_card *card = md->queue.card;
2215 struct mmc_host *host = card->host;
2218 ret = mmc_blk_part_switch(card, md->part_type);
2220 return MMC_REQ_FAILED_TO_START;
2222 switch (mmc_issue_type(mq, req)) {
2223 case MMC_ISSUE_SYNC:
2224 ret = mmc_blk_wait_for_idle(mq, host);
2226 return MMC_REQ_BUSY;
2227 switch (req_op(req)) {
2229 case REQ_OP_DRV_OUT:
2230 mmc_blk_issue_drv_op(mq, req);
2232 case REQ_OP_DISCARD:
2233 mmc_blk_issue_discard_rq(mq, req);
2235 case REQ_OP_SECURE_ERASE:
2236 mmc_blk_issue_secdiscard_rq(mq, req);
2239 mmc_blk_issue_flush(mq, req);
2243 return MMC_REQ_FAILED_TO_START;
2245 return MMC_REQ_FINISHED;
2246 case MMC_ISSUE_DCMD:
2247 case MMC_ISSUE_ASYNC:
2248 switch (req_op(req)) {
2250 ret = mmc_blk_cqe_issue_flush(mq, req);
2255 ret = mmc_blk_cqe_issue_rw_rq(mq, req);
2257 ret = mmc_blk_mq_issue_rw_rq(mq, req);
2264 return MMC_REQ_STARTED;
2265 return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START;
2268 return MMC_REQ_FAILED_TO_START;
2272 static inline int mmc_blk_readonly(struct mmc_card *card)
2274 return mmc_card_readonly(card) ||
2275 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2278 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2279 struct device *parent,
2282 const char *subname,
2285 struct mmc_blk_data *md;
2288 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
2291 * We get -ENOSPC because there are no more any available
2292 * devidx. The reason may be that, either userspace haven't yet
2293 * unmounted the partitions, which postpones mmc_blk_release()
2294 * from being called, or the device has more partitions than
2297 if (devidx == -ENOSPC)
2298 dev_err(mmc_dev(card->host),
2299 "no more device IDs available\n");
2301 return ERR_PTR(devidx);
2304 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2310 md->area_type = area_type;
2313 * Set the read-only status based on the supported commands
2314 * and the write protect switch.
2316 md->read_only = mmc_blk_readonly(card);
2318 md->disk = alloc_disk(perdev_minors);
2319 if (md->disk == NULL) {
2324 spin_lock_init(&md->lock);
2325 INIT_LIST_HEAD(&md->part);
2326 INIT_LIST_HEAD(&md->rpmbs);
2329 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2333 md->queue.blkdata = md;
2336 * Keep an extra reference to the queue so that we can shutdown the
2337 * queue (i.e. call blk_cleanup_queue()) while there are still
2338 * references to the 'md'. The corresponding blk_put_queue() is in
2341 if (!blk_get_queue(md->queue.queue)) {
2342 mmc_cleanup_queue(&md->queue);
2347 md->disk->major = MMC_BLOCK_MAJOR;
2348 md->disk->first_minor = devidx * perdev_minors;
2349 md->disk->fops = &mmc_bdops;
2350 md->disk->private_data = md;
2351 md->disk->queue = md->queue.queue;
2352 md->parent = parent;
2353 set_disk_ro(md->disk, md->read_only || default_ro);
2354 md->disk->flags = GENHD_FL_EXT_DEVT;
2355 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2356 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
2359 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2361 * - be set for removable media with permanent block devices
2362 * - be unset for removable block devices with permanent media
2364 * Since MMC block devices clearly fall under the second
2365 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2366 * should use the block device creation/destruction hotplug
2367 * messages to tell when the card is present.
2370 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2371 "mmcblk%u%s", card->host->index, subname ? subname : "");
2373 if (mmc_card_mmc(card))
2374 blk_queue_logical_block_size(md->queue.queue,
2375 card->ext_csd.data_sector_size);
2377 blk_queue_logical_block_size(md->queue.queue, 512);
2379 set_capacity(md->disk, size);
2381 if (mmc_host_cmd23(card->host)) {
2382 if ((mmc_card_mmc(card) &&
2383 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
2384 (mmc_card_sd(card) &&
2385 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2386 md->flags |= MMC_BLK_CMD23;
2389 if (mmc_card_mmc(card) &&
2390 md->flags & MMC_BLK_CMD23 &&
2391 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2392 card->ext_csd.rel_sectors)) {
2393 md->flags |= MMC_BLK_REL_WR;
2394 blk_queue_write_cache(md->queue.queue, true, true);
2404 ida_simple_remove(&mmc_blk_ida, devidx);
2405 return ERR_PTR(ret);
2408 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2412 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2414 * The EXT_CSD sector count is in number or 512 byte
2417 size = card->ext_csd.sectors;
2420 * The CSD capacity field is in units of read_blkbits.
2421 * set_capacity takes units of 512 bytes.
2423 size = (typeof(sector_t))card->csd.capacity
2424 << (card->csd.read_blkbits - 9);
2427 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2428 MMC_BLK_DATA_AREA_MAIN);
2431 static int mmc_blk_alloc_part(struct mmc_card *card,
2432 struct mmc_blk_data *md,
2433 unsigned int part_type,
2436 const char *subname,
2440 struct mmc_blk_data *part_md;
2442 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2443 subname, area_type);
2444 if (IS_ERR(part_md))
2445 return PTR_ERR(part_md);
2446 part_md->part_type = part_type;
2447 list_add(&part_md->part, &md->part);
2449 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
2450 cap_str, sizeof(cap_str));
2451 pr_info("%s: %s %s partition %u %s\n",
2452 part_md->disk->disk_name, mmc_card_id(card),
2453 mmc_card_name(card), part_md->part_type, cap_str);
2458 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2459 * @filp: the character device file
2460 * @cmd: the ioctl() command
2461 * @arg: the argument from userspace
2463 * This will essentially just redirect the ioctl()s coming in over to
2464 * the main block device spawning the RPMB character device.
2466 static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
2469 struct mmc_rpmb_data *rpmb = filp->private_data;
2474 ret = mmc_blk_ioctl_cmd(rpmb->md,
2475 (struct mmc_ioc_cmd __user *)arg,
2478 case MMC_IOC_MULTI_CMD:
2479 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
2480 (struct mmc_ioc_multi_cmd __user *)arg,
2491 #ifdef CONFIG_COMPAT
2492 static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
2495 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
2499 static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
2501 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2502 struct mmc_rpmb_data, chrdev);
2504 get_device(&rpmb->dev);
2505 filp->private_data = rpmb;
2506 mmc_blk_get(rpmb->md->disk);
2508 return nonseekable_open(inode, filp);
2511 static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
2513 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2514 struct mmc_rpmb_data, chrdev);
2516 put_device(&rpmb->dev);
2517 mmc_blk_put(rpmb->md);
2522 static const struct file_operations mmc_rpmb_fileops = {
2523 .release = mmc_rpmb_chrdev_release,
2524 .open = mmc_rpmb_chrdev_open,
2525 .owner = THIS_MODULE,
2526 .llseek = no_llseek,
2527 .unlocked_ioctl = mmc_rpmb_ioctl,
2528 #ifdef CONFIG_COMPAT
2529 .compat_ioctl = mmc_rpmb_ioctl_compat,
2533 static void mmc_blk_rpmb_device_release(struct device *dev)
2535 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2537 ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2541 static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
2542 struct mmc_blk_data *md,
2543 unsigned int part_index,
2545 const char *subname)
2548 char rpmb_name[DISK_NAME_LEN];
2550 struct mmc_rpmb_data *rpmb;
2552 /* This creates the minor number for the RPMB char device */
2553 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
2557 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
2559 ida_simple_remove(&mmc_rpmb_ida, devidx);
2563 snprintf(rpmb_name, sizeof(rpmb_name),
2564 "mmcblk%u%s", card->host->index, subname ? subname : "");
2567 rpmb->part_index = part_index;
2568 rpmb->dev.init_name = rpmb_name;
2569 rpmb->dev.bus = &mmc_rpmb_bus_type;
2570 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
2571 rpmb->dev.parent = &card->dev;
2572 rpmb->dev.release = mmc_blk_rpmb_device_release;
2573 device_initialize(&rpmb->dev);
2574 dev_set_drvdata(&rpmb->dev, rpmb);
2577 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
2578 rpmb->chrdev.owner = THIS_MODULE;
2579 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
2581 pr_err("%s: could not add character device\n", rpmb_name);
2582 goto out_put_device;
2585 list_add(&rpmb->node, &md->rpmbs);
2587 string_get_size((u64)size, 512, STRING_UNITS_2,
2588 cap_str, sizeof(cap_str));
2590 pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n",
2591 rpmb_name, mmc_card_id(card),
2592 mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str,
2593 MAJOR(mmc_rpmb_devt), rpmb->id);
2598 put_device(&rpmb->dev);
2602 static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
2605 cdev_device_del(&rpmb->chrdev, &rpmb->dev);
2606 put_device(&rpmb->dev);
2609 /* MMC Physical partitions consist of two boot partitions and
2610 * up to four general purpose partitions.
2611 * For each partition enabled in EXT_CSD a block device will be allocatedi
2612 * to provide access to the partition.
2615 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2619 if (!mmc_card_mmc(card))
2622 for (idx = 0; idx < card->nr_parts; idx++) {
2623 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
2625 * RPMB partitions does not provide block access, they
2626 * are only accessed using ioctl():s. Thus create
2627 * special RPMB block devices that do not have a
2628 * backing block queue for these.
2630 ret = mmc_blk_alloc_rpmb_part(card, md,
2631 card->part[idx].part_cfg,
2632 card->part[idx].size >> 9,
2633 card->part[idx].name);
2636 } else if (card->part[idx].size) {
2637 ret = mmc_blk_alloc_part(card, md,
2638 card->part[idx].part_cfg,
2639 card->part[idx].size >> 9,
2640 card->part[idx].force_ro,
2641 card->part[idx].name,
2642 card->part[idx].area_type);
2651 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2653 struct mmc_card *card;
2657 * Flush remaining requests and free queues. It
2658 * is freeing the queue that stops new requests
2659 * from being accepted.
2661 card = md->queue.card;
2662 if (md->disk->flags & GENHD_FL_UP) {
2663 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2664 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2665 card->ext_csd.boot_ro_lockable)
2666 device_remove_file(disk_to_dev(md->disk),
2667 &md->power_ro_lock);
2669 del_gendisk(md->disk);
2671 mmc_cleanup_queue(&md->queue);
2676 static void mmc_blk_remove_parts(struct mmc_card *card,
2677 struct mmc_blk_data *md)
2679 struct list_head *pos, *q;
2680 struct mmc_blk_data *part_md;
2681 struct mmc_rpmb_data *rpmb;
2683 /* Remove RPMB partitions */
2684 list_for_each_safe(pos, q, &md->rpmbs) {
2685 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
2687 mmc_blk_remove_rpmb_part(rpmb);
2689 /* Remove block partitions */
2690 list_for_each_safe(pos, q, &md->part) {
2691 part_md = list_entry(pos, struct mmc_blk_data, part);
2693 mmc_blk_remove_req(part_md);
2697 static int mmc_add_disk(struct mmc_blk_data *md)
2700 struct mmc_card *card = md->queue.card;
2702 device_add_disk(md->parent, md->disk);
2703 md->force_ro.show = force_ro_show;
2704 md->force_ro.store = force_ro_store;
2705 sysfs_attr_init(&md->force_ro.attr);
2706 md->force_ro.attr.name = "force_ro";
2707 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2708 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2712 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2713 card->ext_csd.boot_ro_lockable) {
2716 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2719 mode = S_IRUGO | S_IWUSR;
2721 md->power_ro_lock.show = power_ro_lock_show;
2722 md->power_ro_lock.store = power_ro_lock_store;
2723 sysfs_attr_init(&md->power_ro_lock.attr);
2724 md->power_ro_lock.attr.mode = mode;
2725 md->power_ro_lock.attr.name =
2726 "ro_lock_until_next_power_on";
2727 ret = device_create_file(disk_to_dev(md->disk),
2728 &md->power_ro_lock);
2730 goto power_ro_lock_fail;
2735 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2737 del_gendisk(md->disk);
2742 #ifdef CONFIG_DEBUG_FS
2744 static int mmc_dbg_card_status_get(void *data, u64 *val)
2746 struct mmc_card *card = data;
2747 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2748 struct mmc_queue *mq = &md->queue;
2749 struct request *req;
2752 /* Ask the block layer about the card status */
2753 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
2755 return PTR_ERR(req);
2756 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2757 blk_execute_rq(mq->queue, NULL, req, 0);
2758 ret = req_to_mmc_queue_req(req)->drv_op_result;
2763 blk_put_request(req);
2767 DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2770 /* That is two digits * 512 + 1 for newline */
2771 #define EXT_CSD_STR_LEN 1025
2773 static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2775 struct mmc_card *card = inode->i_private;
2776 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2777 struct mmc_queue *mq = &md->queue;
2778 struct request *req;
2784 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2788 /* Ask the block layer for the EXT CSD */
2789 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
2794 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2795 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2796 blk_execute_rq(mq->queue, NULL, req, 0);
2797 err = req_to_mmc_queue_req(req)->drv_op_result;
2798 blk_put_request(req);
2800 pr_err("FAILED %d\n", err);
2804 for (i = 0; i < 512; i++)
2805 n += sprintf(buf + n, "%02x", ext_csd[i]);
2806 n += sprintf(buf + n, "\n");
2808 if (n != EXT_CSD_STR_LEN) {
2814 filp->private_data = buf;
2823 static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2824 size_t cnt, loff_t *ppos)
2826 char *buf = filp->private_data;
2828 return simple_read_from_buffer(ubuf, cnt, ppos,
2829 buf, EXT_CSD_STR_LEN);
2832 static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2834 kfree(file->private_data);
2838 static const struct file_operations mmc_dbg_ext_csd_fops = {
2839 .open = mmc_ext_csd_open,
2840 .read = mmc_ext_csd_read,
2841 .release = mmc_ext_csd_release,
2842 .llseek = default_llseek,
2845 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2847 struct dentry *root;
2849 if (!card->debugfs_root)
2852 root = card->debugfs_root;
2854 if (mmc_card_mmc(card) || mmc_card_sd(card)) {
2856 debugfs_create_file("status", S_IRUSR, root, card,
2857 &mmc_dbg_card_status_fops);
2858 if (!md->status_dentry)
2862 if (mmc_card_mmc(card)) {
2863 md->ext_csd_dentry =
2864 debugfs_create_file("ext_csd", S_IRUSR, root, card,
2865 &mmc_dbg_ext_csd_fops);
2866 if (!md->ext_csd_dentry)
2873 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2874 struct mmc_blk_data *md)
2876 if (!card->debugfs_root)
2879 if (!IS_ERR_OR_NULL(md->status_dentry)) {
2880 debugfs_remove(md->status_dentry);
2881 md->status_dentry = NULL;
2884 if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) {
2885 debugfs_remove(md->ext_csd_dentry);
2886 md->ext_csd_dentry = NULL;
2892 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2897 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2898 struct mmc_blk_data *md)
2902 #endif /* CONFIG_DEBUG_FS */
2904 static int mmc_blk_probe(struct mmc_card *card)
2906 struct mmc_blk_data *md, *part_md;
2910 * Check that the card supports the command class(es) we need.
2912 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2915 mmc_fixup_device(card, mmc_blk_fixups);
2917 md = mmc_blk_alloc(card);
2921 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
2922 cap_str, sizeof(cap_str));
2923 pr_info("%s: %s %s %s %s\n",
2924 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2925 cap_str, md->read_only ? "(ro)" : "");
2927 if (mmc_blk_alloc_parts(card, md))
2930 dev_set_drvdata(&card->dev, md);
2932 if (mmc_add_disk(md))
2935 list_for_each_entry(part_md, &md->part, part) {
2936 if (mmc_add_disk(part_md))
2940 /* Add two debugfs entries */
2941 mmc_blk_add_debugfs(card, md);
2943 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2944 pm_runtime_use_autosuspend(&card->dev);
2947 * Don't enable runtime PM for SD-combo cards here. Leave that
2948 * decision to be taken during the SDIO init sequence instead.
2950 if (card->type != MMC_TYPE_SD_COMBO) {
2951 pm_runtime_set_active(&card->dev);
2952 pm_runtime_enable(&card->dev);
2958 mmc_blk_remove_parts(card, md);
2959 mmc_blk_remove_req(md);
2963 static void mmc_blk_remove(struct mmc_card *card)
2965 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2967 mmc_blk_remove_debugfs(card, md);
2968 mmc_blk_remove_parts(card, md);
2969 pm_runtime_get_sync(&card->dev);
2970 mmc_claim_host(card->host);
2971 mmc_blk_part_switch(card, md->part_type);
2972 mmc_release_host(card->host);
2973 if (card->type != MMC_TYPE_SD_COMBO)
2974 pm_runtime_disable(&card->dev);
2975 pm_runtime_put_noidle(&card->dev);
2976 mmc_blk_remove_req(md);
2977 dev_set_drvdata(&card->dev, NULL);
2980 static int _mmc_blk_suspend(struct mmc_card *card)
2982 struct mmc_blk_data *part_md;
2983 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2986 mmc_queue_suspend(&md->queue);
2987 list_for_each_entry(part_md, &md->part, part) {
2988 mmc_queue_suspend(&part_md->queue);
2994 static void mmc_blk_shutdown(struct mmc_card *card)
2996 _mmc_blk_suspend(card);
2999 #ifdef CONFIG_PM_SLEEP
3000 static int mmc_blk_suspend(struct device *dev)
3002 struct mmc_card *card = mmc_dev_to_card(dev);
3004 return _mmc_blk_suspend(card);
3007 static int mmc_blk_resume(struct device *dev)
3009 struct mmc_blk_data *part_md;
3010 struct mmc_blk_data *md = dev_get_drvdata(dev);
3014 * Resume involves the card going into idle state,
3015 * so current partition is always the main one.
3017 md->part_curr = md->part_type;
3018 mmc_queue_resume(&md->queue);
3019 list_for_each_entry(part_md, &md->part, part) {
3020 mmc_queue_resume(&part_md->queue);
3027 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3029 static struct mmc_driver mmc_driver = {
3032 .pm = &mmc_blk_pm_ops,
3034 .probe = mmc_blk_probe,
3035 .remove = mmc_blk_remove,
3036 .shutdown = mmc_blk_shutdown,
3039 static int __init mmc_blk_init(void)
3043 res = bus_register(&mmc_rpmb_bus_type);
3045 pr_err("mmcblk: could not register RPMB bus type\n");
3048 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
3050 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
3054 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3055 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3057 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
3059 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3061 goto out_chrdev_unreg;
3063 res = mmc_register_driver(&mmc_driver);
3065 goto out_blkdev_unreg;
3070 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3072 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3074 bus_unregister(&mmc_rpmb_bus_type);
3078 static void __exit mmc_blk_exit(void)
3080 mmc_unregister_driver(&mmc_driver);
3081 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3082 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3083 bus_unregister(&mmc_rpmb_bus_type);
3086 module_init(mmc_blk_init);
3087 module_exit(mmc_blk_exit);
3089 MODULE_LICENSE("GPL");
3090 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");