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 = kmalloc(idata->buf_bytes, GFP_KERNEL);
385 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
386 idata->ic.data_ptr, idata->buf_bytes)) {
401 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
402 struct mmc_blk_ioc_data *idata)
404 struct mmc_ioc_cmd *ic = &idata->ic;
406 if (copy_to_user(&(ic_ptr->response), ic->response,
407 sizeof(ic->response)))
410 if (!idata->ic.write_flag) {
411 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
412 idata->buf, idata->buf_bytes))
419 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
425 if (!status || !retries_max)
429 err = __mmc_send_status(card, status, 5);
433 if (!R1_STATUS(*status) &&
434 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
435 break; /* RPMB programming operation complete */
438 * Rechedule to give the MMC device a chance to continue
439 * processing the previous command without being polled too
442 usleep_range(1000, 5000);
443 } while (++retry_count < retries_max);
445 if (retry_count == retries_max)
451 static int ioctl_do_sanitize(struct mmc_card *card)
455 if (!mmc_can_sanitize(card)) {
456 pr_warn("%s: %s - SANITIZE is not supported\n",
457 mmc_hostname(card->host), __func__);
462 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
463 mmc_hostname(card->host), __func__);
465 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
466 EXT_CSD_SANITIZE_START, 1,
467 MMC_SANITIZE_REQ_TIMEOUT);
470 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
471 mmc_hostname(card->host), __func__, err);
473 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
479 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
480 struct mmc_blk_ioc_data *idata)
482 struct mmc_command cmd = {};
483 struct mmc_data data = {};
484 struct mmc_request mrq = {};
485 struct scatterlist sg;
487 unsigned int target_part;
490 if (!card || !md || !idata)
494 * The RPMB accesses comes in from the character device, so we
495 * need to target these explicitly. Else we just target the
496 * partition type for the block device the ioctl() was issued
500 /* Support multiple RPMB partitions */
501 target_part = idata->rpmb->part_index;
502 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
504 target_part = md->part_type;
507 cmd.opcode = idata->ic.opcode;
508 cmd.arg = idata->ic.arg;
509 cmd.flags = idata->ic.flags;
511 if (idata->buf_bytes) {
514 data.blksz = idata->ic.blksz;
515 data.blocks = idata->ic.blocks;
517 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
519 if (idata->ic.write_flag)
520 data.flags = MMC_DATA_WRITE;
522 data.flags = MMC_DATA_READ;
524 /* data.flags must already be set before doing this. */
525 mmc_set_data_timeout(&data, card);
527 /* Allow overriding the timeout_ns for empirical tuning. */
528 if (idata->ic.data_timeout_ns)
529 data.timeout_ns = idata->ic.data_timeout_ns;
531 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
533 * Pretend this is a data transfer and rely on the
534 * host driver to compute timeout. When all host
535 * drivers support cmd.cmd_timeout for R1B, this
539 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
541 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
549 err = mmc_blk_part_switch(card, target_part);
553 if (idata->ic.is_acmd) {
554 err = mmc_app_cmd(card->host, card);
560 err = mmc_set_blockcount(card, data.blocks,
561 idata->ic.write_flag & (1 << 31));
566 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
567 (cmd.opcode == MMC_SWITCH)) {
568 err = ioctl_do_sanitize(card);
571 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
577 mmc_wait_for_req(card->host, &mrq);
580 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
581 __func__, cmd.error);
585 dev_err(mmc_dev(card->host), "%s: data error %d\n",
586 __func__, data.error);
591 * Make sure the cache of the PARTITION_CONFIG register and
592 * PARTITION_ACCESS bits is updated in case the ioctl ext_csd write
593 * changed it successfully.
595 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_PART_CONFIG) &&
596 (cmd.opcode == MMC_SWITCH)) {
597 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
598 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg);
601 * Update cache so the next mmc_blk_part_switch call operates
602 * on up-to-date data.
604 card->ext_csd.part_config = value;
605 main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
609 * According to the SD specs, some commands require a delay after
610 * issuing the command.
612 if (idata->ic.postsleep_min_us)
613 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
615 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
619 * Ensure RPMB command has completed by polling CMD13
622 err = ioctl_rpmb_card_status_poll(card, &status, 5);
624 dev_err(mmc_dev(card->host),
625 "%s: Card Status=0x%08X, error %d\n",
626 __func__, status, err);
632 static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
633 struct mmc_ioc_cmd __user *ic_ptr,
634 struct mmc_rpmb_data *rpmb)
636 struct mmc_blk_ioc_data *idata;
637 struct mmc_blk_ioc_data *idatas[1];
638 struct mmc_queue *mq;
639 struct mmc_card *card;
640 int err = 0, ioc_err = 0;
643 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
645 return PTR_ERR(idata);
646 /* This will be NULL on non-RPMB ioctl():s */
649 card = md->queue.card;
656 * Dispatch the ioctl() into the block request queue.
659 req = blk_get_request(mq->queue,
660 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
667 req_to_mmc_queue_req(req)->drv_op =
668 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
669 req_to_mmc_queue_req(req)->drv_op_data = idatas;
670 req_to_mmc_queue_req(req)->ioc_count = 1;
671 blk_execute_rq(mq->queue, NULL, req, 0);
672 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
673 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
674 blk_put_request(req);
679 return ioc_err ? ioc_err : err;
682 static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
683 struct mmc_ioc_multi_cmd __user *user,
684 struct mmc_rpmb_data *rpmb)
686 struct mmc_blk_ioc_data **idata = NULL;
687 struct mmc_ioc_cmd __user *cmds = user->cmds;
688 struct mmc_card *card;
689 struct mmc_queue *mq;
690 int i, err = 0, ioc_err = 0;
694 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
695 sizeof(num_of_cmds)))
701 if (num_of_cmds > MMC_IOC_MAX_CMDS)
704 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
708 for (i = 0; i < num_of_cmds; i++) {
709 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
710 if (IS_ERR(idata[i])) {
711 err = PTR_ERR(idata[i]);
715 /* This will be NULL on non-RPMB ioctl():s */
716 idata[i]->rpmb = rpmb;
719 card = md->queue.card;
727 * Dispatch the ioctl()s into the block request queue.
730 req = blk_get_request(mq->queue,
731 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
737 req_to_mmc_queue_req(req)->drv_op =
738 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
739 req_to_mmc_queue_req(req)->drv_op_data = idata;
740 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
741 blk_execute_rq(mq->queue, NULL, req, 0);
742 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
744 /* copy to user if data and response */
745 for (i = 0; i < num_of_cmds && !err; i++)
746 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
748 blk_put_request(req);
751 for (i = 0; i < num_of_cmds; i++) {
752 kfree(idata[i]->buf);
756 return ioc_err ? ioc_err : err;
759 static int mmc_blk_check_blkdev(struct block_device *bdev)
762 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
763 * whole block device, not on a partition. This prevents overspray
764 * between sibling partitions.
766 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
771 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
772 unsigned int cmd, unsigned long arg)
774 struct mmc_blk_data *md;
779 ret = mmc_blk_check_blkdev(bdev);
782 md = mmc_blk_get(bdev->bd_disk);
785 ret = mmc_blk_ioctl_cmd(md,
786 (struct mmc_ioc_cmd __user *)arg,
790 case MMC_IOC_MULTI_CMD:
791 ret = mmc_blk_check_blkdev(bdev);
794 md = mmc_blk_get(bdev->bd_disk);
797 ret = mmc_blk_ioctl_multi_cmd(md,
798 (struct mmc_ioc_multi_cmd __user *)arg,
808 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
809 unsigned int cmd, unsigned long arg)
811 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
815 static const struct block_device_operations mmc_bdops = {
816 .open = mmc_blk_open,
817 .release = mmc_blk_release,
818 .getgeo = mmc_blk_getgeo,
819 .owner = THIS_MODULE,
820 .ioctl = mmc_blk_ioctl,
822 .compat_ioctl = mmc_blk_compat_ioctl,
826 static int mmc_blk_part_switch_pre(struct mmc_card *card,
827 unsigned int part_type)
831 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
832 if (card->ext_csd.cmdq_en) {
833 ret = mmc_cmdq_disable(card);
837 mmc_retune_pause(card->host);
843 static int mmc_blk_part_switch_post(struct mmc_card *card,
844 unsigned int part_type)
848 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
849 mmc_retune_unpause(card->host);
850 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
851 ret = mmc_cmdq_enable(card);
857 static inline int mmc_blk_part_switch(struct mmc_card *card,
858 unsigned int part_type)
861 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
863 if (main_md->part_curr == part_type)
866 if (mmc_card_mmc(card)) {
867 u8 part_config = card->ext_csd.part_config;
869 ret = mmc_blk_part_switch_pre(card, part_type);
873 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
874 part_config |= part_type;
876 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
877 EXT_CSD_PART_CONFIG, part_config,
878 card->ext_csd.part_time);
880 mmc_blk_part_switch_post(card, part_type);
884 card->ext_csd.part_config = part_config;
886 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
889 main_md->part_curr = part_type;
893 static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
899 struct mmc_request mrq = {};
900 struct mmc_command cmd = {};
901 struct mmc_data data = {};
903 struct scatterlist sg;
905 cmd.opcode = MMC_APP_CMD;
906 cmd.arg = card->rca << 16;
907 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
909 err = mmc_wait_for_cmd(card->host, &cmd, 0);
912 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
915 memset(&cmd, 0, sizeof(struct mmc_command));
917 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
919 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
923 data.flags = MMC_DATA_READ;
926 mmc_set_data_timeout(&data, card);
931 blocks = kmalloc(4, GFP_KERNEL);
935 sg_init_one(&sg, blocks, 4);
937 mmc_wait_for_req(card->host, &mrq);
939 result = ntohl(*blocks);
942 if (cmd.error || data.error)
945 *written_blocks = result;
950 static unsigned int mmc_blk_clock_khz(struct mmc_host *host)
952 if (host->actual_clock)
953 return host->actual_clock / 1000;
955 /* Clock may be subject to a divisor, fudge it by a factor of 2. */
957 return host->ios.clock / 2000;
959 /* How can there be no clock */
961 return 100; /* 100 kHz is minimum possible value */
964 static unsigned int mmc_blk_data_timeout_ms(struct mmc_host *host,
965 struct mmc_data *data)
967 unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000);
970 if (data->timeout_clks) {
971 khz = mmc_blk_clock_khz(host);
972 ms += DIV_ROUND_UP(data->timeout_clks, khz);
978 static inline bool mmc_blk_in_tran_state(u32 status)
981 * Some cards mishandle the status bits, so make sure to check both the
982 * busy indication and the card state.
984 return status & R1_READY_FOR_DATA &&
985 (R1_CURRENT_STATE(status) == R1_STATE_TRAN);
988 static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
989 struct request *req, u32 *resp_errs)
991 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
996 bool done = time_after(jiffies, timeout);
998 err = __mmc_send_status(card, &status, 5);
1000 pr_err("%s: error %d requesting status\n",
1001 req->rq_disk->disk_name, err);
1005 /* Accumulate any response error bits seen */
1007 *resp_errs |= status;
1010 * Timeout if the device never becomes ready for data and never
1011 * leaves the program state.
1014 pr_err("%s: Card stuck in wrong state! %s %s status: %#x\n",
1015 mmc_hostname(card->host),
1016 req->rq_disk->disk_name, __func__, status);
1021 * Some cards mishandle the status bits,
1022 * so make sure to check both the busy
1023 * indication and the card state.
1025 } while (!mmc_blk_in_tran_state(status));
1030 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1035 if (md->reset_done & type)
1038 md->reset_done |= type;
1039 err = mmc_hw_reset(host);
1040 /* Ensure we switch back to the correct partition */
1041 if (err != -EOPNOTSUPP) {
1042 struct mmc_blk_data *main_md =
1043 dev_get_drvdata(&host->card->dev);
1046 main_md->part_curr = main_md->part_type;
1047 part_err = mmc_blk_part_switch(host->card, md->part_type);
1050 * We have failed to get back into the correct
1051 * partition, so we need to abort the whole request.
1059 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1061 md->reset_done &= ~type;
1065 * The non-block commands come back from the block layer after it queued it and
1066 * processed it with all other requests and then they get issued in this
1069 static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1071 struct mmc_queue_req *mq_rq;
1072 struct mmc_card *card = mq->card;
1073 struct mmc_blk_data *md = mq->blkdata;
1074 struct mmc_blk_ioc_data **idata;
1081 mq_rq = req_to_mmc_queue_req(req);
1082 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
1084 switch (mq_rq->drv_op) {
1085 case MMC_DRV_OP_IOCTL:
1086 case MMC_DRV_OP_IOCTL_RPMB:
1087 idata = mq_rq->drv_op_data;
1088 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
1089 ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
1093 /* Always switch back to main area after RPMB access */
1095 mmc_blk_part_switch(card, 0);
1097 case MMC_DRV_OP_BOOT_WP:
1098 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1099 card->ext_csd.boot_ro_lock |
1100 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1101 card->ext_csd.part_time);
1103 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1104 md->disk->disk_name, ret);
1106 card->ext_csd.boot_ro_lock |=
1107 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
1109 case MMC_DRV_OP_GET_CARD_STATUS:
1110 ret = mmc_send_status(card, &status);
1114 case MMC_DRV_OP_GET_EXT_CSD:
1115 ext_csd = mq_rq->drv_op_data;
1116 ret = mmc_get_ext_csd(card, ext_csd);
1119 pr_err("%s: unknown driver specific operation\n",
1120 md->disk->disk_name);
1124 mq_rq->drv_op_result = ret;
1125 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1128 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1130 struct mmc_blk_data *md = mq->blkdata;
1131 struct mmc_card *card = md->queue.card;
1132 unsigned int from, nr, arg;
1133 int err = 0, type = MMC_BLK_DISCARD;
1134 blk_status_t status = BLK_STS_OK;
1136 if (!mmc_can_erase(card)) {
1137 status = BLK_STS_NOTSUPP;
1141 from = blk_rq_pos(req);
1142 nr = blk_rq_sectors(req);
1144 if (mmc_can_discard(card))
1145 arg = MMC_DISCARD_ARG;
1146 else if (mmc_can_trim(card))
1149 arg = MMC_ERASE_ARG;
1152 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1153 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1154 INAND_CMD38_ARG_EXT_CSD,
1155 arg == MMC_TRIM_ARG ?
1156 INAND_CMD38_ARG_TRIM :
1157 INAND_CMD38_ARG_ERASE,
1161 err = mmc_erase(card, from, nr, arg);
1162 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
1164 status = BLK_STS_IOERR;
1166 mmc_blk_reset_success(md, type);
1168 blk_mq_end_request(req, status);
1171 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1172 struct request *req)
1174 struct mmc_blk_data *md = mq->blkdata;
1175 struct mmc_card *card = md->queue.card;
1176 unsigned int from, nr, arg;
1177 int err = 0, type = MMC_BLK_SECDISCARD;
1178 blk_status_t status = BLK_STS_OK;
1180 if (!(mmc_can_secure_erase_trim(card))) {
1181 status = BLK_STS_NOTSUPP;
1185 from = blk_rq_pos(req);
1186 nr = blk_rq_sectors(req);
1188 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1189 arg = MMC_SECURE_TRIM1_ARG;
1191 arg = MMC_SECURE_ERASE_ARG;
1194 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1195 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1196 INAND_CMD38_ARG_EXT_CSD,
1197 arg == MMC_SECURE_TRIM1_ARG ?
1198 INAND_CMD38_ARG_SECTRIM1 :
1199 INAND_CMD38_ARG_SECERASE,
1205 err = mmc_erase(card, from, nr, arg);
1209 status = BLK_STS_IOERR;
1213 if (arg == MMC_SECURE_TRIM1_ARG) {
1214 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1215 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1216 INAND_CMD38_ARG_EXT_CSD,
1217 INAND_CMD38_ARG_SECTRIM2,
1223 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1227 status = BLK_STS_IOERR;
1233 if (err && !mmc_blk_reset(md, card->host, type))
1236 mmc_blk_reset_success(md, type);
1238 blk_mq_end_request(req, status);
1241 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1243 struct mmc_blk_data *md = mq->blkdata;
1244 struct mmc_card *card = md->queue.card;
1247 ret = mmc_flush_cache(card);
1248 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1252 * Reformat current write as a reliable write, supporting
1253 * both legacy and the enhanced reliable write MMC cards.
1254 * In each transfer we'll handle only as much as a single
1255 * reliable write can handle, thus finish the request in
1256 * partial completions.
1258 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1259 struct mmc_card *card,
1260 struct request *req)
1262 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1263 /* Legacy mode imposes restrictions on transfers. */
1264 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
1265 brq->data.blocks = 1;
1267 if (brq->data.blocks > card->ext_csd.rel_sectors)
1268 brq->data.blocks = card->ext_csd.rel_sectors;
1269 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1270 brq->data.blocks = 1;
1274 #define CMD_ERRORS_EXCL_OOR \
1275 (R1_ADDRESS_ERROR | /* Misaligned address */ \
1276 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1277 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1278 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
1279 R1_CC_ERROR | /* Card controller error */ \
1280 R1_ERROR) /* General/unknown error */
1282 #define CMD_ERRORS \
1283 (CMD_ERRORS_EXCL_OOR | \
1284 R1_OUT_OF_RANGE) /* Command argument out of range */ \
1286 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
1291 * Per the SD specification(physical layer version 4.10)[1],
1292 * section 4.3.3, it explicitly states that "When the last
1293 * block of user area is read using CMD18, the host should
1294 * ignore OUT_OF_RANGE error that may occur even the sequence
1295 * is correct". And JESD84-B51 for eMMC also has a similar
1296 * statement on section 6.8.3.
1298 * Multiple block read/write could be done by either predefined
1299 * method, namely CMD23, or open-ending mode. For open-ending mode,
1300 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1302 * However the spec[1] doesn't tell us whether we should also
1303 * ignore that for predefined method. But per the spec[1], section
1304 * 4.15 Set Block Count Command, it says"If illegal block count
1305 * is set, out of range error will be indicated during read/write
1306 * operation (For example, data transfer is stopped at user area
1307 * boundary)." In another word, we could expect a out of range error
1308 * in the response for the following CMD18/25. And if argument of
1309 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1310 * we could also expect to get a -ETIMEDOUT or any error number from
1311 * the host drivers due to missing data response(for write)/data(for
1312 * read), as the cards will stop the data transfer by itself per the
1313 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1316 if (!brq->stop.error) {
1317 bool oor_with_open_end;
1318 /* If there is no error yet, check R1 response */
1320 val = brq->stop.resp[0] & CMD_ERRORS;
1321 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1323 if (val && !oor_with_open_end)
1324 brq->stop.error = -EIO;
1328 static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
1329 int disable_multi, bool *do_rel_wr_p,
1330 bool *do_data_tag_p)
1332 struct mmc_blk_data *md = mq->blkdata;
1333 struct mmc_card *card = md->queue.card;
1334 struct mmc_blk_request *brq = &mqrq->brq;
1335 struct request *req = mmc_queue_req_to_req(mqrq);
1336 bool do_rel_wr, do_data_tag;
1339 * Reliable writes are used to implement Forced Unit Access and
1340 * are supported only on MMCs.
1342 do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1343 rq_data_dir(req) == WRITE &&
1344 (md->flags & MMC_BLK_REL_WR);
1346 memset(brq, 0, sizeof(struct mmc_blk_request));
1348 brq->mrq.data = &brq->data;
1349 brq->mrq.tag = req->tag;
1351 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1354 if (rq_data_dir(req) == READ) {
1355 brq->data.flags = MMC_DATA_READ;
1356 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1358 brq->data.flags = MMC_DATA_WRITE;
1359 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1362 brq->data.blksz = 512;
1363 brq->data.blocks = blk_rq_sectors(req);
1364 brq->data.blk_addr = blk_rq_pos(req);
1367 * The command queue supports 2 priorities: "high" (1) and "simple" (0).
1368 * The eMMC will give "high" priority tasks priority over "simple"
1369 * priority tasks. Here we always set "simple" priority by not setting
1374 * The block layer doesn't support all sector count
1375 * restrictions, so we need to be prepared for too big
1378 if (brq->data.blocks > card->host->max_blk_count)
1379 brq->data.blocks = card->host->max_blk_count;
1381 if (brq->data.blocks > 1) {
1383 * After a read error, we redo the request one sector
1384 * at a time in order to accurately determine which
1385 * sectors can be read successfully.
1388 brq->data.blocks = 1;
1391 * Some controllers have HW issues while operating
1392 * in multiple I/O mode
1394 if (card->host->ops->multi_io_quirk)
1395 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1396 (rq_data_dir(req) == READ) ?
1397 MMC_DATA_READ : MMC_DATA_WRITE,
1402 mmc_apply_rel_rw(brq, card, req);
1403 brq->data.flags |= MMC_DATA_REL_WR;
1407 * Data tag is used only during writing meta data to speed
1408 * up write and any subsequent read of this meta data
1410 do_data_tag = card->ext_csd.data_tag_unit_size &&
1411 (req->cmd_flags & REQ_META) &&
1412 (rq_data_dir(req) == WRITE) &&
1413 ((brq->data.blocks * brq->data.blksz) >=
1414 card->ext_csd.data_tag_unit_size);
1417 brq->data.flags |= MMC_DATA_DAT_TAG;
1419 mmc_set_data_timeout(&brq->data, card);
1421 brq->data.sg = mqrq->sg;
1422 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1425 * Adjust the sg list so it is the same size as the
1428 if (brq->data.blocks != blk_rq_sectors(req)) {
1429 int i, data_size = brq->data.blocks << 9;
1430 struct scatterlist *sg;
1432 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1433 data_size -= sg->length;
1434 if (data_size <= 0) {
1435 sg->length += data_size;
1440 brq->data.sg_len = i;
1444 *do_rel_wr_p = do_rel_wr;
1447 *do_data_tag_p = do_data_tag;
1450 #define MMC_CQE_RETRIES 2
1452 static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req)
1454 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1455 struct mmc_request *mrq = &mqrq->brq.mrq;
1456 struct request_queue *q = req->q;
1457 struct mmc_host *host = mq->card->host;
1458 unsigned long flags;
1462 mmc_cqe_post_req(host, mrq);
1464 if (mrq->cmd && mrq->cmd->error)
1465 err = mrq->cmd->error;
1466 else if (mrq->data && mrq->data->error)
1467 err = mrq->data->error;
1472 if (mqrq->retries++ < MMC_CQE_RETRIES)
1473 blk_mq_requeue_request(req, true);
1475 blk_mq_end_request(req, BLK_STS_IOERR);
1476 } else if (mrq->data) {
1477 if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered))
1478 blk_mq_requeue_request(req, true);
1480 __blk_mq_end_request(req, BLK_STS_OK);
1482 blk_mq_end_request(req, BLK_STS_OK);
1485 spin_lock_irqsave(q->queue_lock, flags);
1487 mq->in_flight[mmc_issue_type(mq, req)] -= 1;
1489 put_card = (mmc_tot_in_flight(mq) == 0);
1491 mmc_cqe_check_busy(mq);
1493 spin_unlock_irqrestore(q->queue_lock, flags);
1496 blk_mq_run_hw_queues(q, true);
1499 mmc_put_card(mq->card, &mq->ctx);
1502 void mmc_blk_cqe_recovery(struct mmc_queue *mq)
1504 struct mmc_card *card = mq->card;
1505 struct mmc_host *host = card->host;
1508 pr_debug("%s: CQE recovery start\n", mmc_hostname(host));
1510 err = mmc_cqe_recovery(host);
1512 mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY);
1514 mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY);
1516 pr_debug("%s: CQE recovery done\n", mmc_hostname(host));
1519 static void mmc_blk_cqe_req_done(struct mmc_request *mrq)
1521 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
1523 struct request *req = mmc_queue_req_to_req(mqrq);
1524 struct request_queue *q = req->q;
1525 struct mmc_queue *mq = q->queuedata;
1528 * Block layer timeouts race with completions which means the normal
1529 * completion path cannot be used during recovery.
1531 if (mq->in_recovery)
1532 mmc_blk_cqe_complete_rq(mq, req);
1534 blk_mq_complete_request(req);
1537 static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
1539 mrq->done = mmc_blk_cqe_req_done;
1540 mrq->recovery_notifier = mmc_cqe_recovery_notifier;
1542 return mmc_cqe_start_req(host, mrq);
1545 static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq,
1546 struct request *req)
1548 struct mmc_blk_request *brq = &mqrq->brq;
1550 memset(brq, 0, sizeof(*brq));
1552 brq->mrq.cmd = &brq->cmd;
1553 brq->mrq.tag = req->tag;
1558 static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
1560 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1561 struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);
1563 mrq->cmd->opcode = MMC_SWITCH;
1564 mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1565 (EXT_CSD_FLUSH_CACHE << 16) |
1567 EXT_CSD_CMD_SET_NORMAL;
1568 mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B;
1570 return mmc_blk_cqe_start_req(mq->card->host, mrq);
1573 static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1575 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1577 mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL);
1579 return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq);
1582 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1583 struct mmc_card *card,
1585 struct mmc_queue *mq)
1587 u32 readcmd, writecmd;
1588 struct mmc_blk_request *brq = &mqrq->brq;
1589 struct request *req = mmc_queue_req_to_req(mqrq);
1590 struct mmc_blk_data *md = mq->blkdata;
1591 bool do_rel_wr, do_data_tag;
1593 mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1595 brq->mrq.cmd = &brq->cmd;
1597 brq->cmd.arg = blk_rq_pos(req);
1598 if (!mmc_card_blockaddr(card))
1600 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1602 if (brq->data.blocks > 1 || do_rel_wr) {
1603 /* SPI multiblock writes terminate using a special
1604 * token, not a STOP_TRANSMISSION request.
1606 if (!mmc_host_is_spi(card->host) ||
1607 rq_data_dir(req) == READ)
1608 brq->mrq.stop = &brq->stop;
1609 readcmd = MMC_READ_MULTIPLE_BLOCK;
1610 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1612 brq->mrq.stop = NULL;
1613 readcmd = MMC_READ_SINGLE_BLOCK;
1614 writecmd = MMC_WRITE_BLOCK;
1616 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
1619 * Pre-defined multi-block transfers are preferable to
1620 * open ended-ones (and necessary for reliable writes).
1621 * However, it is not sufficient to just send CMD23,
1622 * and avoid the final CMD12, as on an error condition
1623 * CMD12 (stop) needs to be sent anyway. This, coupled
1624 * with Auto-CMD23 enhancements provided by some
1625 * hosts, means that the complexity of dealing
1626 * with this is best left to the host. If CMD23 is
1627 * supported by card and host, we'll fill sbc in and let
1628 * the host deal with handling it correctly. This means
1629 * that for hosts that don't expose MMC_CAP_CMD23, no
1630 * change of behavior will be observed.
1632 * N.B: Some MMC cards experience perf degradation.
1633 * We'll avoid using CMD23-bounded multiblock writes for
1634 * these, while retaining features like reliable writes.
1636 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1637 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1639 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1640 brq->sbc.arg = brq->data.blocks |
1641 (do_rel_wr ? (1 << 31) : 0) |
1642 (do_data_tag ? (1 << 29) : 0);
1643 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1644 brq->mrq.sbc = &brq->sbc;
1648 #define MMC_MAX_RETRIES 5
1649 #define MMC_DATA_RETRIES 2
1650 #define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1)
1652 static int mmc_blk_send_stop(struct mmc_card *card, unsigned int timeout)
1654 struct mmc_command cmd = {
1655 .opcode = MMC_STOP_TRANSMISSION,
1656 .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC,
1657 /* Some hosts wait for busy anyway, so provide a busy timeout */
1658 .busy_timeout = timeout,
1661 return mmc_wait_for_cmd(card->host, &cmd, 5);
1664 static int mmc_blk_fix_state(struct mmc_card *card, struct request *req)
1666 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1667 struct mmc_blk_request *brq = &mqrq->brq;
1668 unsigned int timeout = mmc_blk_data_timeout_ms(card->host, &brq->data);
1671 mmc_retune_hold_now(card->host);
1673 mmc_blk_send_stop(card, timeout);
1675 err = card_busy_detect(card, timeout, req, NULL);
1677 mmc_retune_release(card->host);
1682 #define MMC_READ_SINGLE_RETRIES 2
1684 /* Single sector read during recovery */
1685 static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req)
1687 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1688 struct mmc_request *mrq = &mqrq->brq.mrq;
1689 struct mmc_card *card = mq->card;
1690 struct mmc_host *host = card->host;
1691 blk_status_t error = BLK_STS_OK;
1698 mmc_blk_rw_rq_prep(mqrq, card, 1, mq);
1700 mmc_wait_for_req(host, mrq);
1702 err = mmc_send_status(card, &status);
1706 if (!mmc_host_is_spi(host) &&
1707 !mmc_blk_in_tran_state(status)) {
1708 err = mmc_blk_fix_state(card, req);
1713 if (mrq->cmd->error && retries++ < MMC_READ_SINGLE_RETRIES)
1718 if (mrq->cmd->error ||
1720 (!mmc_host_is_spi(host) &&
1721 (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS)))
1722 error = BLK_STS_IOERR;
1726 } while (blk_update_request(req, error, 512));
1731 mrq->data->bytes_xfered = 0;
1732 blk_update_request(req, BLK_STS_IOERR, 512);
1733 /* Let it try the remaining request again */
1734 if (mqrq->retries > MMC_MAX_RETRIES - 1)
1735 mqrq->retries = MMC_MAX_RETRIES - 1;
1738 static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq)
1740 return !!brq->mrq.sbc;
1743 static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq)
1745 return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR;
1749 * Check for errors the host controller driver might not have seen such as
1750 * response mode errors or invalid card state.
1752 static bool mmc_blk_status_error(struct request *req, u32 status)
1754 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1755 struct mmc_blk_request *brq = &mqrq->brq;
1756 struct mmc_queue *mq = req->q->queuedata;
1759 if (mmc_host_is_spi(mq->card->host))
1762 stop_err_bits = mmc_blk_stop_err_bits(brq);
1764 return brq->cmd.resp[0] & CMD_ERRORS ||
1765 brq->stop.resp[0] & stop_err_bits ||
1766 status & stop_err_bits ||
1767 (rq_data_dir(req) == WRITE && !mmc_blk_in_tran_state(status));
1770 static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq)
1772 return !brq->sbc.error && !brq->cmd.error &&
1773 !(brq->cmd.resp[0] & CMD_ERRORS);
1777 * Requests are completed by mmc_blk_mq_complete_rq() which sets simple
1779 * 1. A request that has transferred at least some data is considered
1780 * successful and will be requeued if there is remaining data to
1782 * 2. Otherwise the number of retries is incremented and the request
1783 * will be requeued if there are remaining retries.
1784 * 3. Otherwise the request will be errored out.
1785 * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and
1786 * mqrq->retries. So there are only 4 possible actions here:
1787 * 1. do not accept the bytes_xfered value i.e. set it to zero
1788 * 2. change mqrq->retries to determine the number of retries
1789 * 3. try to reset the card
1790 * 4. read one sector at a time
1792 static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req)
1794 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1795 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1796 struct mmc_blk_request *brq = &mqrq->brq;
1797 struct mmc_blk_data *md = mq->blkdata;
1798 struct mmc_card *card = mq->card;
1804 * Some errors the host driver might not have seen. Set the number of
1805 * bytes transferred to zero in that case.
1807 err = __mmc_send_status(card, &status, 0);
1808 if (err || mmc_blk_status_error(req, status))
1809 brq->data.bytes_xfered = 0;
1811 mmc_retune_release(card->host);
1814 * Try again to get the status. This also provides an opportunity for
1818 err = __mmc_send_status(card, &status, 0);
1821 * Nothing more to do after the number of bytes transferred has been
1822 * updated and there is no card.
1824 if (err && mmc_detect_card_removed(card->host))
1827 /* Try to get back to "tran" state */
1828 if (!mmc_host_is_spi(mq->card->host) &&
1829 (err || !mmc_blk_in_tran_state(status)))
1830 err = mmc_blk_fix_state(mq->card, req);
1833 * Special case for SD cards where the card might record the number of
1836 if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) &&
1837 rq_data_dir(req) == WRITE) {
1838 if (mmc_sd_num_wr_blocks(card, &blocks))
1839 brq->data.bytes_xfered = 0;
1841 brq->data.bytes_xfered = blocks << 9;
1844 /* Reset if the card is in a bad state */
1845 if (!mmc_host_is_spi(mq->card->host) &&
1846 err && mmc_blk_reset(md, card->host, type)) {
1847 pr_err("%s: recovery failed!\n", req->rq_disk->disk_name);
1848 mqrq->retries = MMC_NO_RETRIES;
1853 * If anything was done, just return and if there is anything remaining
1854 * on the request it will get requeued.
1856 if (brq->data.bytes_xfered)
1859 /* Reset before last retry */
1860 if (mqrq->retries + 1 == MMC_MAX_RETRIES)
1861 mmc_blk_reset(md, card->host, type);
1863 /* Command errors fail fast, so use all MMC_MAX_RETRIES */
1864 if (brq->sbc.error || brq->cmd.error)
1867 /* Reduce the remaining retries for data errors */
1868 if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) {
1869 mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES;
1873 /* FIXME: Missing single sector read for large sector size */
1874 if (!mmc_large_sector(card) && rq_data_dir(req) == READ &&
1875 brq->data.blocks > 1) {
1876 /* Read one sector at a time */
1877 mmc_blk_read_single(mq, req);
1882 static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq)
1884 mmc_blk_eval_resp_error(brq);
1886 return brq->sbc.error || brq->cmd.error || brq->stop.error ||
1887 brq->data.error || brq->cmd.resp[0] & CMD_ERRORS;
1890 static int mmc_blk_card_busy(struct mmc_card *card, struct request *req)
1892 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1896 if (mmc_host_is_spi(card->host) || rq_data_dir(req) == READ)
1899 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, req, &status);
1902 * Do not assume data transferred correctly if there are any error bits
1905 if (status & mmc_blk_stop_err_bits(&mqrq->brq)) {
1906 mqrq->brq.data.bytes_xfered = 0;
1907 err = err ? err : -EIO;
1910 /* Copy the exception bit so it will be seen later on */
1911 if (mmc_card_mmc(card) && status & R1_EXCEPTION_EVENT)
1912 mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
1917 static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
1918 struct request *req)
1920 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1922 mmc_blk_reset_success(mq->blkdata, type);
1925 static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)
1927 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1928 unsigned int nr_bytes = mqrq->brq.data.bytes_xfered;
1931 if (blk_update_request(req, BLK_STS_OK, nr_bytes))
1932 blk_mq_requeue_request(req, true);
1934 __blk_mq_end_request(req, BLK_STS_OK);
1935 } else if (!blk_rq_bytes(req)) {
1936 __blk_mq_end_request(req, BLK_STS_IOERR);
1937 } else if (mqrq->retries++ < MMC_MAX_RETRIES) {
1938 blk_mq_requeue_request(req, true);
1940 if (mmc_card_removed(mq->card))
1941 req->rq_flags |= RQF_QUIET;
1942 blk_mq_end_request(req, BLK_STS_IOERR);
1946 static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
1947 struct mmc_queue_req *mqrq)
1949 return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) &&
1950 (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
1951 mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
1954 static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
1955 struct mmc_queue_req *mqrq)
1957 if (mmc_blk_urgent_bkops_needed(mq, mqrq))
1958 mmc_start_bkops(mq->card, true);
1961 void mmc_blk_mq_complete(struct request *req)
1963 struct mmc_queue *mq = req->q->queuedata;
1966 mmc_blk_cqe_complete_rq(mq, req);
1968 mmc_blk_mq_complete_rq(mq, req);
1971 static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,
1972 struct request *req)
1974 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1975 struct mmc_host *host = mq->card->host;
1977 if (mmc_blk_rq_error(&mqrq->brq) ||
1978 mmc_blk_card_busy(mq->card, req)) {
1979 mmc_blk_mq_rw_recovery(mq, req);
1981 mmc_blk_rw_reset_success(mq, req);
1982 mmc_retune_release(host);
1985 mmc_blk_urgent_bkops(mq, mqrq);
1988 static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, struct request *req)
1990 struct request_queue *q = req->q;
1991 unsigned long flags;
1994 spin_lock_irqsave(q->queue_lock, flags);
1996 mq->in_flight[mmc_issue_type(mq, req)] -= 1;
1998 put_card = (mmc_tot_in_flight(mq) == 0);
2000 spin_unlock_irqrestore(q->queue_lock, flags);
2003 mmc_put_card(mq->card, &mq->ctx);
2006 static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req)
2008 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2009 struct mmc_request *mrq = &mqrq->brq.mrq;
2010 struct mmc_host *host = mq->card->host;
2012 mmc_post_req(host, mrq, 0);
2015 * Block layer timeouts race with completions which means the normal
2016 * completion path cannot be used during recovery.
2018 if (mq->in_recovery)
2019 mmc_blk_mq_complete_rq(mq, req);
2021 blk_mq_complete_request(req);
2023 mmc_blk_mq_dec_in_flight(mq, req);
2026 void mmc_blk_mq_recovery(struct mmc_queue *mq)
2028 struct request *req = mq->recovery_req;
2029 struct mmc_host *host = mq->card->host;
2030 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2032 mq->recovery_req = NULL;
2033 mq->rw_wait = false;
2035 if (mmc_blk_rq_error(&mqrq->brq)) {
2036 mmc_retune_hold_now(host);
2037 mmc_blk_mq_rw_recovery(mq, req);
2040 mmc_blk_urgent_bkops(mq, mqrq);
2042 mmc_blk_mq_post_req(mq, req);
2045 static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq,
2046 struct request **prev_req)
2048 if (mmc_host_done_complete(mq->card->host))
2051 mutex_lock(&mq->complete_lock);
2053 if (!mq->complete_req)
2056 mmc_blk_mq_poll_completion(mq, mq->complete_req);
2059 *prev_req = mq->complete_req;
2061 mmc_blk_mq_post_req(mq, mq->complete_req);
2063 mq->complete_req = NULL;
2066 mutex_unlock(&mq->complete_lock);
2069 void mmc_blk_mq_complete_work(struct work_struct *work)
2071 struct mmc_queue *mq = container_of(work, struct mmc_queue,
2074 mmc_blk_mq_complete_prev_req(mq, NULL);
2077 static void mmc_blk_mq_req_done(struct mmc_request *mrq)
2079 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
2081 struct request *req = mmc_queue_req_to_req(mqrq);
2082 struct request_queue *q = req->q;
2083 struct mmc_queue *mq = q->queuedata;
2084 struct mmc_host *host = mq->card->host;
2085 unsigned long flags;
2087 if (!mmc_host_done_complete(host)) {
2091 * We cannot complete the request in this context, so record
2092 * that there is a request to complete, and that a following
2093 * request does not need to wait (although it does need to
2094 * complete complete_req first).
2096 spin_lock_irqsave(q->queue_lock, flags);
2097 mq->complete_req = req;
2098 mq->rw_wait = false;
2099 waiting = mq->waiting;
2100 spin_unlock_irqrestore(q->queue_lock, flags);
2103 * If 'waiting' then the waiting task will complete this
2104 * request, otherwise queue a work to do it. Note that
2105 * complete_work may still race with the dispatch of a following
2111 kblockd_schedule_work(&mq->complete_work);
2116 /* Take the recovery path for errors or urgent background operations */
2117 if (mmc_blk_rq_error(&mqrq->brq) ||
2118 mmc_blk_urgent_bkops_needed(mq, mqrq)) {
2119 spin_lock_irqsave(q->queue_lock, flags);
2120 mq->recovery_needed = true;
2121 mq->recovery_req = req;
2122 spin_unlock_irqrestore(q->queue_lock, flags);
2124 schedule_work(&mq->recovery_work);
2128 mmc_blk_rw_reset_success(mq, req);
2130 mq->rw_wait = false;
2133 mmc_blk_mq_post_req(mq, req);
2136 static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err)
2138 struct request_queue *q = mq->queue;
2139 unsigned long flags;
2143 * Wait while there is another request in progress, but not if recovery
2144 * is needed. Also indicate whether there is a request waiting to start.
2146 spin_lock_irqsave(q->queue_lock, flags);
2147 if (mq->recovery_needed) {
2151 done = !mq->rw_wait;
2153 mq->waiting = !done;
2154 spin_unlock_irqrestore(q->queue_lock, flags);
2159 static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req)
2163 wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err));
2165 /* Always complete the previous request if there is one */
2166 mmc_blk_mq_complete_prev_req(mq, prev_req);
2171 static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq,
2172 struct request *req)
2174 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2175 struct mmc_host *host = mq->card->host;
2176 struct request *prev_req = NULL;
2179 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
2181 mqrq->brq.mrq.done = mmc_blk_mq_req_done;
2183 mmc_pre_req(host, &mqrq->brq.mrq);
2185 err = mmc_blk_rw_wait(mq, &prev_req);
2191 err = mmc_start_request(host, &mqrq->brq.mrq);
2194 mmc_blk_mq_post_req(mq, prev_req);
2197 mq->rw_wait = false;
2199 /* Release re-tuning here where there is no synchronization required */
2200 if (err || mmc_host_done_complete(host))
2201 mmc_retune_release(host);
2205 mmc_post_req(host, &mqrq->brq.mrq, err);
2210 static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host)
2213 return host->cqe_ops->cqe_wait_for_idle(host);
2215 return mmc_blk_rw_wait(mq, NULL);
2218 enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
2220 struct mmc_blk_data *md = mq->blkdata;
2221 struct mmc_card *card = md->queue.card;
2222 struct mmc_host *host = card->host;
2225 ret = mmc_blk_part_switch(card, md->part_type);
2227 return MMC_REQ_FAILED_TO_START;
2229 switch (mmc_issue_type(mq, req)) {
2230 case MMC_ISSUE_SYNC:
2231 ret = mmc_blk_wait_for_idle(mq, host);
2233 return MMC_REQ_BUSY;
2234 switch (req_op(req)) {
2236 case REQ_OP_DRV_OUT:
2237 mmc_blk_issue_drv_op(mq, req);
2239 case REQ_OP_DISCARD:
2240 mmc_blk_issue_discard_rq(mq, req);
2242 case REQ_OP_SECURE_ERASE:
2243 mmc_blk_issue_secdiscard_rq(mq, req);
2246 mmc_blk_issue_flush(mq, req);
2250 return MMC_REQ_FAILED_TO_START;
2252 return MMC_REQ_FINISHED;
2253 case MMC_ISSUE_DCMD:
2254 case MMC_ISSUE_ASYNC:
2255 switch (req_op(req)) {
2257 ret = mmc_blk_cqe_issue_flush(mq, req);
2262 ret = mmc_blk_cqe_issue_rw_rq(mq, req);
2264 ret = mmc_blk_mq_issue_rw_rq(mq, req);
2271 return MMC_REQ_STARTED;
2272 return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START;
2275 return MMC_REQ_FAILED_TO_START;
2279 static inline int mmc_blk_readonly(struct mmc_card *card)
2281 return mmc_card_readonly(card) ||
2282 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2285 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2286 struct device *parent,
2289 const char *subname,
2292 struct mmc_blk_data *md;
2295 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
2298 * We get -ENOSPC because there are no more any available
2299 * devidx. The reason may be that, either userspace haven't yet
2300 * unmounted the partitions, which postpones mmc_blk_release()
2301 * from being called, or the device has more partitions than
2304 if (devidx == -ENOSPC)
2305 dev_err(mmc_dev(card->host),
2306 "no more device IDs available\n");
2308 return ERR_PTR(devidx);
2311 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2317 md->area_type = area_type;
2320 * Set the read-only status based on the supported commands
2321 * and the write protect switch.
2323 md->read_only = mmc_blk_readonly(card);
2325 md->disk = alloc_disk(perdev_minors);
2326 if (md->disk == NULL) {
2331 spin_lock_init(&md->lock);
2332 INIT_LIST_HEAD(&md->part);
2333 INIT_LIST_HEAD(&md->rpmbs);
2336 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2340 md->queue.blkdata = md;
2343 * Keep an extra reference to the queue so that we can shutdown the
2344 * queue (i.e. call blk_cleanup_queue()) while there are still
2345 * references to the 'md'. The corresponding blk_put_queue() is in
2348 if (!blk_get_queue(md->queue.queue)) {
2349 mmc_cleanup_queue(&md->queue);
2354 md->disk->major = MMC_BLOCK_MAJOR;
2355 md->disk->first_minor = devidx * perdev_minors;
2356 md->disk->fops = &mmc_bdops;
2357 md->disk->private_data = md;
2358 md->disk->queue = md->queue.queue;
2359 md->parent = parent;
2360 set_disk_ro(md->disk, md->read_only || default_ro);
2361 md->disk->flags = GENHD_FL_EXT_DEVT;
2362 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2363 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
2366 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2368 * - be set for removable media with permanent block devices
2369 * - be unset for removable block devices with permanent media
2371 * Since MMC block devices clearly fall under the second
2372 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2373 * should use the block device creation/destruction hotplug
2374 * messages to tell when the card is present.
2377 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2378 "mmcblk%u%s", card->host->index, subname ? subname : "");
2380 if (mmc_card_mmc(card))
2381 blk_queue_logical_block_size(md->queue.queue,
2382 card->ext_csd.data_sector_size);
2384 blk_queue_logical_block_size(md->queue.queue, 512);
2386 set_capacity(md->disk, size);
2388 if (mmc_host_cmd23(card->host)) {
2389 if ((mmc_card_mmc(card) &&
2390 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
2391 (mmc_card_sd(card) &&
2392 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2393 md->flags |= MMC_BLK_CMD23;
2396 if (mmc_card_mmc(card) &&
2397 md->flags & MMC_BLK_CMD23 &&
2398 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2399 card->ext_csd.rel_sectors)) {
2400 md->flags |= MMC_BLK_REL_WR;
2401 blk_queue_write_cache(md->queue.queue, true, true);
2411 ida_simple_remove(&mmc_blk_ida, devidx);
2412 return ERR_PTR(ret);
2415 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2419 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2421 * The EXT_CSD sector count is in number or 512 byte
2424 size = card->ext_csd.sectors;
2427 * The CSD capacity field is in units of read_blkbits.
2428 * set_capacity takes units of 512 bytes.
2430 size = (typeof(sector_t))card->csd.capacity
2431 << (card->csd.read_blkbits - 9);
2434 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2435 MMC_BLK_DATA_AREA_MAIN);
2438 static int mmc_blk_alloc_part(struct mmc_card *card,
2439 struct mmc_blk_data *md,
2440 unsigned int part_type,
2443 const char *subname,
2447 struct mmc_blk_data *part_md;
2449 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2450 subname, area_type);
2451 if (IS_ERR(part_md))
2452 return PTR_ERR(part_md);
2453 part_md->part_type = part_type;
2454 list_add(&part_md->part, &md->part);
2456 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
2457 cap_str, sizeof(cap_str));
2458 pr_info("%s: %s %s partition %u %s\n",
2459 part_md->disk->disk_name, mmc_card_id(card),
2460 mmc_card_name(card), part_md->part_type, cap_str);
2465 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2466 * @filp: the character device file
2467 * @cmd: the ioctl() command
2468 * @arg: the argument from userspace
2470 * This will essentially just redirect the ioctl()s coming in over to
2471 * the main block device spawning the RPMB character device.
2473 static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
2476 struct mmc_rpmb_data *rpmb = filp->private_data;
2481 ret = mmc_blk_ioctl_cmd(rpmb->md,
2482 (struct mmc_ioc_cmd __user *)arg,
2485 case MMC_IOC_MULTI_CMD:
2486 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
2487 (struct mmc_ioc_multi_cmd __user *)arg,
2498 #ifdef CONFIG_COMPAT
2499 static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
2502 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
2506 static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
2508 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2509 struct mmc_rpmb_data, chrdev);
2511 get_device(&rpmb->dev);
2512 filp->private_data = rpmb;
2513 mmc_blk_get(rpmb->md->disk);
2515 return nonseekable_open(inode, filp);
2518 static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
2520 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2521 struct mmc_rpmb_data, chrdev);
2523 put_device(&rpmb->dev);
2524 mmc_blk_put(rpmb->md);
2529 static const struct file_operations mmc_rpmb_fileops = {
2530 .release = mmc_rpmb_chrdev_release,
2531 .open = mmc_rpmb_chrdev_open,
2532 .owner = THIS_MODULE,
2533 .llseek = no_llseek,
2534 .unlocked_ioctl = mmc_rpmb_ioctl,
2535 #ifdef CONFIG_COMPAT
2536 .compat_ioctl = mmc_rpmb_ioctl_compat,
2540 static void mmc_blk_rpmb_device_release(struct device *dev)
2542 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2544 ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2548 static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
2549 struct mmc_blk_data *md,
2550 unsigned int part_index,
2552 const char *subname)
2555 char rpmb_name[DISK_NAME_LEN];
2557 struct mmc_rpmb_data *rpmb;
2559 /* This creates the minor number for the RPMB char device */
2560 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
2564 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
2566 ida_simple_remove(&mmc_rpmb_ida, devidx);
2570 snprintf(rpmb_name, sizeof(rpmb_name),
2571 "mmcblk%u%s", card->host->index, subname ? subname : "");
2574 rpmb->part_index = part_index;
2575 rpmb->dev.init_name = rpmb_name;
2576 rpmb->dev.bus = &mmc_rpmb_bus_type;
2577 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
2578 rpmb->dev.parent = &card->dev;
2579 rpmb->dev.release = mmc_blk_rpmb_device_release;
2580 device_initialize(&rpmb->dev);
2581 dev_set_drvdata(&rpmb->dev, rpmb);
2584 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
2585 rpmb->chrdev.owner = THIS_MODULE;
2586 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
2588 pr_err("%s: could not add character device\n", rpmb_name);
2589 goto out_put_device;
2592 list_add(&rpmb->node, &md->rpmbs);
2594 string_get_size((u64)size, 512, STRING_UNITS_2,
2595 cap_str, sizeof(cap_str));
2597 pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n",
2598 rpmb_name, mmc_card_id(card),
2599 mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str,
2600 MAJOR(mmc_rpmb_devt), rpmb->id);
2605 put_device(&rpmb->dev);
2609 static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
2612 cdev_device_del(&rpmb->chrdev, &rpmb->dev);
2613 put_device(&rpmb->dev);
2616 /* MMC Physical partitions consist of two boot partitions and
2617 * up to four general purpose partitions.
2618 * For each partition enabled in EXT_CSD a block device will be allocatedi
2619 * to provide access to the partition.
2622 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2626 if (!mmc_card_mmc(card))
2629 for (idx = 0; idx < card->nr_parts; idx++) {
2630 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
2632 * RPMB partitions does not provide block access, they
2633 * are only accessed using ioctl():s. Thus create
2634 * special RPMB block devices that do not have a
2635 * backing block queue for these.
2637 ret = mmc_blk_alloc_rpmb_part(card, md,
2638 card->part[idx].part_cfg,
2639 card->part[idx].size >> 9,
2640 card->part[idx].name);
2643 } else if (card->part[idx].size) {
2644 ret = mmc_blk_alloc_part(card, md,
2645 card->part[idx].part_cfg,
2646 card->part[idx].size >> 9,
2647 card->part[idx].force_ro,
2648 card->part[idx].name,
2649 card->part[idx].area_type);
2658 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2660 struct mmc_card *card;
2664 * Flush remaining requests and free queues. It
2665 * is freeing the queue that stops new requests
2666 * from being accepted.
2668 card = md->queue.card;
2669 mmc_cleanup_queue(&md->queue);
2670 if (md->disk->flags & GENHD_FL_UP) {
2671 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2672 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2673 card->ext_csd.boot_ro_lockable)
2674 device_remove_file(disk_to_dev(md->disk),
2675 &md->power_ro_lock);
2677 del_gendisk(md->disk);
2683 static void mmc_blk_remove_parts(struct mmc_card *card,
2684 struct mmc_blk_data *md)
2686 struct list_head *pos, *q;
2687 struct mmc_blk_data *part_md;
2688 struct mmc_rpmb_data *rpmb;
2690 /* Remove RPMB partitions */
2691 list_for_each_safe(pos, q, &md->rpmbs) {
2692 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
2694 mmc_blk_remove_rpmb_part(rpmb);
2696 /* Remove block partitions */
2697 list_for_each_safe(pos, q, &md->part) {
2698 part_md = list_entry(pos, struct mmc_blk_data, part);
2700 mmc_blk_remove_req(part_md);
2704 static int mmc_add_disk(struct mmc_blk_data *md)
2707 struct mmc_card *card = md->queue.card;
2709 device_add_disk(md->parent, md->disk);
2710 md->force_ro.show = force_ro_show;
2711 md->force_ro.store = force_ro_store;
2712 sysfs_attr_init(&md->force_ro.attr);
2713 md->force_ro.attr.name = "force_ro";
2714 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2715 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2719 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2720 card->ext_csd.boot_ro_lockable) {
2723 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2726 mode = S_IRUGO | S_IWUSR;
2728 md->power_ro_lock.show = power_ro_lock_show;
2729 md->power_ro_lock.store = power_ro_lock_store;
2730 sysfs_attr_init(&md->power_ro_lock.attr);
2731 md->power_ro_lock.attr.mode = mode;
2732 md->power_ro_lock.attr.name =
2733 "ro_lock_until_next_power_on";
2734 ret = device_create_file(disk_to_dev(md->disk),
2735 &md->power_ro_lock);
2737 goto power_ro_lock_fail;
2742 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2744 del_gendisk(md->disk);
2749 #ifdef CONFIG_DEBUG_FS
2751 static int mmc_dbg_card_status_get(void *data, u64 *val)
2753 struct mmc_card *card = data;
2754 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2755 struct mmc_queue *mq = &md->queue;
2756 struct request *req;
2759 /* Ask the block layer about the card status */
2760 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
2762 return PTR_ERR(req);
2763 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2764 blk_execute_rq(mq->queue, NULL, req, 0);
2765 ret = req_to_mmc_queue_req(req)->drv_op_result;
2770 blk_put_request(req);
2774 DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2777 /* That is two digits * 512 + 1 for newline */
2778 #define EXT_CSD_STR_LEN 1025
2780 static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2782 struct mmc_card *card = inode->i_private;
2783 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2784 struct mmc_queue *mq = &md->queue;
2785 struct request *req;
2791 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2795 /* Ask the block layer for the EXT CSD */
2796 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
2801 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2802 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2803 blk_execute_rq(mq->queue, NULL, req, 0);
2804 err = req_to_mmc_queue_req(req)->drv_op_result;
2805 blk_put_request(req);
2807 pr_err("FAILED %d\n", err);
2811 for (i = 0; i < 512; i++)
2812 n += sprintf(buf + n, "%02x", ext_csd[i]);
2813 n += sprintf(buf + n, "\n");
2815 if (n != EXT_CSD_STR_LEN) {
2821 filp->private_data = buf;
2830 static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2831 size_t cnt, loff_t *ppos)
2833 char *buf = filp->private_data;
2835 return simple_read_from_buffer(ubuf, cnt, ppos,
2836 buf, EXT_CSD_STR_LEN);
2839 static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2841 kfree(file->private_data);
2845 static const struct file_operations mmc_dbg_ext_csd_fops = {
2846 .open = mmc_ext_csd_open,
2847 .read = mmc_ext_csd_read,
2848 .release = mmc_ext_csd_release,
2849 .llseek = default_llseek,
2852 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2854 struct dentry *root;
2856 if (!card->debugfs_root)
2859 root = card->debugfs_root;
2861 if (mmc_card_mmc(card) || mmc_card_sd(card)) {
2863 debugfs_create_file("status", S_IRUSR, root, card,
2864 &mmc_dbg_card_status_fops);
2865 if (!md->status_dentry)
2869 if (mmc_card_mmc(card)) {
2870 md->ext_csd_dentry =
2871 debugfs_create_file("ext_csd", S_IRUSR, root, card,
2872 &mmc_dbg_ext_csd_fops);
2873 if (!md->ext_csd_dentry)
2880 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2881 struct mmc_blk_data *md)
2883 if (!card->debugfs_root)
2886 if (!IS_ERR_OR_NULL(md->status_dentry)) {
2887 debugfs_remove(md->status_dentry);
2888 md->status_dentry = NULL;
2891 if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) {
2892 debugfs_remove(md->ext_csd_dentry);
2893 md->ext_csd_dentry = NULL;
2899 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2904 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2905 struct mmc_blk_data *md)
2909 #endif /* CONFIG_DEBUG_FS */
2911 static int mmc_blk_probe(struct mmc_card *card)
2913 struct mmc_blk_data *md, *part_md;
2917 * Check that the card supports the command class(es) we need.
2919 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2922 mmc_fixup_device(card, mmc_blk_fixups);
2924 md = mmc_blk_alloc(card);
2928 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
2929 cap_str, sizeof(cap_str));
2930 pr_info("%s: %s %s %s %s\n",
2931 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2932 cap_str, md->read_only ? "(ro)" : "");
2934 if (mmc_blk_alloc_parts(card, md))
2937 dev_set_drvdata(&card->dev, md);
2939 if (mmc_add_disk(md))
2942 list_for_each_entry(part_md, &md->part, part) {
2943 if (mmc_add_disk(part_md))
2947 /* Add two debugfs entries */
2948 mmc_blk_add_debugfs(card, md);
2950 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2951 pm_runtime_use_autosuspend(&card->dev);
2954 * Don't enable runtime PM for SD-combo cards here. Leave that
2955 * decision to be taken during the SDIO init sequence instead.
2957 if (card->type != MMC_TYPE_SD_COMBO) {
2958 pm_runtime_set_active(&card->dev);
2959 pm_runtime_enable(&card->dev);
2965 mmc_blk_remove_parts(card, md);
2966 mmc_blk_remove_req(md);
2970 static void mmc_blk_remove(struct mmc_card *card)
2972 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2974 mmc_blk_remove_debugfs(card, md);
2975 mmc_blk_remove_parts(card, md);
2976 pm_runtime_get_sync(&card->dev);
2977 mmc_claim_host(card->host);
2978 mmc_blk_part_switch(card, md->part_type);
2979 mmc_release_host(card->host);
2980 if (card->type != MMC_TYPE_SD_COMBO)
2981 pm_runtime_disable(&card->dev);
2982 pm_runtime_put_noidle(&card->dev);
2983 mmc_blk_remove_req(md);
2984 dev_set_drvdata(&card->dev, NULL);
2987 static int _mmc_blk_suspend(struct mmc_card *card)
2989 struct mmc_blk_data *part_md;
2990 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2993 mmc_queue_suspend(&md->queue);
2994 list_for_each_entry(part_md, &md->part, part) {
2995 mmc_queue_suspend(&part_md->queue);
3001 static void mmc_blk_shutdown(struct mmc_card *card)
3003 _mmc_blk_suspend(card);
3006 #ifdef CONFIG_PM_SLEEP
3007 static int mmc_blk_suspend(struct device *dev)
3009 struct mmc_card *card = mmc_dev_to_card(dev);
3011 return _mmc_blk_suspend(card);
3014 static int mmc_blk_resume(struct device *dev)
3016 struct mmc_blk_data *part_md;
3017 struct mmc_blk_data *md = dev_get_drvdata(dev);
3021 * Resume involves the card going into idle state,
3022 * so current partition is always the main one.
3024 md->part_curr = md->part_type;
3025 mmc_queue_resume(&md->queue);
3026 list_for_each_entry(part_md, &md->part, part) {
3027 mmc_queue_resume(&part_md->queue);
3034 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3036 static struct mmc_driver mmc_driver = {
3039 .pm = &mmc_blk_pm_ops,
3041 .probe = mmc_blk_probe,
3042 .remove = mmc_blk_remove,
3043 .shutdown = mmc_blk_shutdown,
3046 static int __init mmc_blk_init(void)
3050 res = bus_register(&mmc_rpmb_bus_type);
3052 pr_err("mmcblk: could not register RPMB bus type\n");
3055 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
3057 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
3061 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3062 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3064 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
3066 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3068 goto out_chrdev_unreg;
3070 res = mmc_register_driver(&mmc_driver);
3072 goto out_blkdev_unreg;
3077 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3079 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3081 bus_unregister(&mmc_rpmb_bus_type);
3085 static void __exit mmc_blk_exit(void)
3087 mmc_unregister_driver(&mmc_driver);
3088 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3089 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3092 module_init(mmc_blk_init);
3093 module_exit(mmc_blk_exit);
3095 MODULE_LICENSE("GPL");
3096 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");