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 {
103 struct device *parent;
104 struct gendisk *disk;
105 struct mmc_queue queue;
106 struct list_head part;
107 struct list_head rpmbs;
110 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
111 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
114 unsigned int read_only;
115 unsigned int part_type;
116 unsigned int reset_done;
117 #define MMC_BLK_READ BIT(0)
118 #define MMC_BLK_WRITE BIT(1)
119 #define MMC_BLK_DISCARD BIT(2)
120 #define MMC_BLK_SECDISCARD BIT(3)
121 #define MMC_BLK_CQE_RECOVERY BIT(4)
124 * Only set in main mmc_blk_data associated
125 * with mmc_card with dev_set_drvdata, and keeps
126 * track of the current selected device partition.
128 unsigned int part_curr;
129 struct device_attribute force_ro;
130 struct device_attribute power_ro_lock;
133 /* debugfs files (only in main mmc_blk_data) */
134 struct dentry *status_dentry;
135 struct dentry *ext_csd_dentry;
138 /* Device type for RPMB character devices */
139 static dev_t mmc_rpmb_devt;
141 /* Bus type for RPMB character devices */
142 static struct bus_type mmc_rpmb_bus_type = {
147 * struct mmc_rpmb_data - special RPMB device type for these areas
148 * @dev: the device for the RPMB area
149 * @chrdev: character device for the RPMB area
150 * @id: unique device ID number
151 * @part_index: partition index (0 on first)
152 * @md: parent MMC block device
153 * @node: list item, so we can put this device on a list
155 struct mmc_rpmb_data {
159 unsigned int part_index;
160 struct mmc_blk_data *md;
161 struct list_head node;
164 static DEFINE_MUTEX(open_lock);
166 module_param(perdev_minors, int, 0444);
167 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
169 static inline int mmc_blk_part_switch(struct mmc_card *card,
170 unsigned int part_type);
172 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
174 struct mmc_blk_data *md;
176 mutex_lock(&open_lock);
177 md = disk->private_data;
178 if (md && md->usage == 0)
182 mutex_unlock(&open_lock);
187 static inline int mmc_get_devidx(struct gendisk *disk)
189 int devidx = disk->first_minor / perdev_minors;
193 static void mmc_blk_put(struct mmc_blk_data *md)
195 mutex_lock(&open_lock);
197 if (md->usage == 0) {
198 int devidx = mmc_get_devidx(md->disk);
199 blk_put_queue(md->queue.queue);
200 ida_simple_remove(&mmc_blk_ida, devidx);
204 mutex_unlock(&open_lock);
207 static ssize_t power_ro_lock_show(struct device *dev,
208 struct device_attribute *attr, char *buf)
211 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
212 struct mmc_card *card = md->queue.card;
215 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
217 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
220 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
227 static ssize_t power_ro_lock_store(struct device *dev,
228 struct device_attribute *attr, const char *buf, size_t count)
231 struct mmc_blk_data *md, *part_md;
232 struct mmc_queue *mq;
236 if (kstrtoul(buf, 0, &set))
242 md = mmc_blk_get(dev_to_disk(dev));
245 /* Dispatch locking to the block layer */
246 req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, 0);
248 count = PTR_ERR(req);
251 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
252 blk_execute_rq(mq->queue, NULL, req, 0);
253 ret = req_to_mmc_queue_req(req)->drv_op_result;
254 blk_put_request(req);
257 pr_info("%s: Locking boot partition ro until next power on\n",
258 md->disk->disk_name);
259 set_disk_ro(md->disk, 1);
261 list_for_each_entry(part_md, &md->part, part)
262 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
263 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
264 set_disk_ro(part_md->disk, 1);
272 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
276 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
278 ret = snprintf(buf, PAGE_SIZE, "%d\n",
279 get_disk_ro(dev_to_disk(dev)) ^
285 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
286 const char *buf, size_t count)
290 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
291 unsigned long set = simple_strtoul(buf, &end, 0);
297 set_disk_ro(dev_to_disk(dev), set || md->read_only);
304 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
306 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
309 mutex_lock(&block_mutex);
312 check_disk_change(bdev);
315 if ((mode & FMODE_WRITE) && md->read_only) {
320 mutex_unlock(&block_mutex);
325 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
327 struct mmc_blk_data *md = disk->private_data;
329 mutex_lock(&block_mutex);
331 mutex_unlock(&block_mutex);
335 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
337 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
343 struct mmc_blk_ioc_data {
344 struct mmc_ioc_cmd ic;
347 struct mmc_rpmb_data *rpmb;
350 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
351 struct mmc_ioc_cmd __user *user)
353 struct mmc_blk_ioc_data *idata;
356 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
362 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
367 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
368 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
373 if (!idata->buf_bytes) {
378 idata->buf = memdup_user((void __user *)(unsigned long)
379 idata->ic.data_ptr, idata->buf_bytes);
380 if (IS_ERR(idata->buf)) {
381 err = PTR_ERR(idata->buf);
393 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
394 struct mmc_blk_ioc_data *idata)
396 struct mmc_ioc_cmd *ic = &idata->ic;
398 if (copy_to_user(&(ic_ptr->response), ic->response,
399 sizeof(ic->response)))
402 if (!idata->ic.write_flag) {
403 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
404 idata->buf, idata->buf_bytes))
411 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
417 if (!status || !retries_max)
421 err = __mmc_send_status(card, status, 5);
425 if (!R1_STATUS(*status) &&
426 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
427 break; /* RPMB programming operation complete */
430 * Rechedule to give the MMC device a chance to continue
431 * processing the previous command without being polled too
434 usleep_range(1000, 5000);
435 } while (++retry_count < retries_max);
437 if (retry_count == retries_max)
443 static int ioctl_do_sanitize(struct mmc_card *card)
447 if (!mmc_can_sanitize(card)) {
448 pr_warn("%s: %s - SANITIZE is not supported\n",
449 mmc_hostname(card->host), __func__);
454 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
455 mmc_hostname(card->host), __func__);
457 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
458 EXT_CSD_SANITIZE_START, 1,
459 MMC_SANITIZE_REQ_TIMEOUT);
462 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
463 mmc_hostname(card->host), __func__, err);
465 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
471 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
472 struct mmc_blk_ioc_data *idata)
474 struct mmc_command cmd = {}, sbc = {};
475 struct mmc_data data = {};
476 struct mmc_request mrq = {};
477 struct scatterlist sg;
479 unsigned int target_part;
482 if (!card || !md || !idata)
486 * The RPMB accesses comes in from the character device, so we
487 * need to target these explicitly. Else we just target the
488 * partition type for the block device the ioctl() was issued
492 /* Support multiple RPMB partitions */
493 target_part = idata->rpmb->part_index;
494 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
496 target_part = md->part_type;
499 cmd.opcode = idata->ic.opcode;
500 cmd.arg = idata->ic.arg;
501 cmd.flags = idata->ic.flags;
503 if (idata->buf_bytes) {
506 data.blksz = idata->ic.blksz;
507 data.blocks = idata->ic.blocks;
509 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
511 if (idata->ic.write_flag)
512 data.flags = MMC_DATA_WRITE;
514 data.flags = MMC_DATA_READ;
516 /* data.flags must already be set before doing this. */
517 mmc_set_data_timeout(&data, card);
519 /* Allow overriding the timeout_ns for empirical tuning. */
520 if (idata->ic.data_timeout_ns)
521 data.timeout_ns = idata->ic.data_timeout_ns;
523 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
525 * Pretend this is a data transfer and rely on the
526 * host driver to compute timeout. When all host
527 * drivers support cmd.cmd_timeout for R1B, this
531 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
533 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
541 err = mmc_blk_part_switch(card, target_part);
545 if (idata->ic.is_acmd) {
546 err = mmc_app_cmd(card->host, card);
552 sbc.opcode = MMC_SET_BLOCK_COUNT;
554 * We don't do any blockcount validation because the max size
555 * may be increased by a future standard. We just copy the
556 * 'Reliable Write' bit here.
558 sbc.arg = data.blocks | (idata->ic.write_flag & BIT(31));
559 sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
563 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
564 (cmd.opcode == MMC_SWITCH)) {
565 err = ioctl_do_sanitize(card);
568 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
574 mmc_wait_for_req(card->host, &mrq);
577 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
578 __func__, cmd.error);
582 dev_err(mmc_dev(card->host), "%s: data error %d\n",
583 __func__, data.error);
588 * Make sure the cache of the PARTITION_CONFIG register and
589 * PARTITION_ACCESS bits is updated in case the ioctl ext_csd write
590 * changed it successfully.
592 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_PART_CONFIG) &&
593 (cmd.opcode == MMC_SWITCH)) {
594 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
595 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg);
598 * Update cache so the next mmc_blk_part_switch call operates
599 * on up-to-date data.
601 card->ext_csd.part_config = value;
602 main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
606 * According to the SD specs, some commands require a delay after
607 * issuing the command.
609 if (idata->ic.postsleep_min_us)
610 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
612 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
616 * Ensure RPMB command has completed by polling CMD13
619 err = ioctl_rpmb_card_status_poll(card, &status, 5);
621 dev_err(mmc_dev(card->host),
622 "%s: Card Status=0x%08X, error %d\n",
623 __func__, status, err);
629 static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
630 struct mmc_ioc_cmd __user *ic_ptr,
631 struct mmc_rpmb_data *rpmb)
633 struct mmc_blk_ioc_data *idata;
634 struct mmc_blk_ioc_data *idatas[1];
635 struct mmc_queue *mq;
636 struct mmc_card *card;
637 int err = 0, ioc_err = 0;
640 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
642 return PTR_ERR(idata);
643 /* This will be NULL on non-RPMB ioctl():s */
646 card = md->queue.card;
653 * Dispatch the ioctl() into the block request queue.
656 req = blk_get_request(mq->queue,
657 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
663 req_to_mmc_queue_req(req)->drv_op =
664 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
665 req_to_mmc_queue_req(req)->drv_op_data = idatas;
666 req_to_mmc_queue_req(req)->ioc_count = 1;
667 blk_execute_rq(mq->queue, NULL, req, 0);
668 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
669 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
670 blk_put_request(req);
675 return ioc_err ? ioc_err : err;
678 static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
679 struct mmc_ioc_multi_cmd __user *user,
680 struct mmc_rpmb_data *rpmb)
682 struct mmc_blk_ioc_data **idata = NULL;
683 struct mmc_ioc_cmd __user *cmds = user->cmds;
684 struct mmc_card *card;
685 struct mmc_queue *mq;
686 int i, err = 0, ioc_err = 0;
690 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
691 sizeof(num_of_cmds)))
697 if (num_of_cmds > MMC_IOC_MAX_CMDS)
700 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
704 for (i = 0; i < num_of_cmds; i++) {
705 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
706 if (IS_ERR(idata[i])) {
707 err = PTR_ERR(idata[i]);
711 /* This will be NULL on non-RPMB ioctl():s */
712 idata[i]->rpmb = rpmb;
715 card = md->queue.card;
723 * Dispatch the ioctl()s into the block request queue.
726 req = blk_get_request(mq->queue,
727 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
732 req_to_mmc_queue_req(req)->drv_op =
733 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
734 req_to_mmc_queue_req(req)->drv_op_data = idata;
735 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
736 blk_execute_rq(mq->queue, NULL, req, 0);
737 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
739 /* copy to user if data and response */
740 for (i = 0; i < num_of_cmds && !err; i++)
741 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
743 blk_put_request(req);
746 for (i = 0; i < num_of_cmds; i++) {
747 kfree(idata[i]->buf);
751 return ioc_err ? ioc_err : err;
754 static int mmc_blk_check_blkdev(struct block_device *bdev)
757 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
758 * whole block device, not on a partition. This prevents overspray
759 * between sibling partitions.
761 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
766 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
767 unsigned int cmd, unsigned long arg)
769 struct mmc_blk_data *md;
774 ret = mmc_blk_check_blkdev(bdev);
777 md = mmc_blk_get(bdev->bd_disk);
780 ret = mmc_blk_ioctl_cmd(md,
781 (struct mmc_ioc_cmd __user *)arg,
785 case MMC_IOC_MULTI_CMD:
786 ret = mmc_blk_check_blkdev(bdev);
789 md = mmc_blk_get(bdev->bd_disk);
792 ret = mmc_blk_ioctl_multi_cmd(md,
793 (struct mmc_ioc_multi_cmd __user *)arg,
803 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
804 unsigned int cmd, unsigned long arg)
806 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
810 static const struct block_device_operations mmc_bdops = {
811 .open = mmc_blk_open,
812 .release = mmc_blk_release,
813 .getgeo = mmc_blk_getgeo,
814 .owner = THIS_MODULE,
815 .ioctl = mmc_blk_ioctl,
817 .compat_ioctl = mmc_blk_compat_ioctl,
821 static int mmc_blk_part_switch_pre(struct mmc_card *card,
822 unsigned int part_type)
826 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
827 if (card->ext_csd.cmdq_en) {
828 ret = mmc_cmdq_disable(card);
832 mmc_retune_pause(card->host);
838 static int mmc_blk_part_switch_post(struct mmc_card *card,
839 unsigned int part_type)
843 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
844 mmc_retune_unpause(card->host);
845 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
846 ret = mmc_cmdq_enable(card);
852 static inline int mmc_blk_part_switch(struct mmc_card *card,
853 unsigned int part_type)
856 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
858 if (main_md->part_curr == part_type)
861 if (mmc_card_mmc(card)) {
862 u8 part_config = card->ext_csd.part_config;
864 ret = mmc_blk_part_switch_pre(card, part_type);
868 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
869 part_config |= part_type;
871 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
872 EXT_CSD_PART_CONFIG, part_config,
873 card->ext_csd.part_time);
875 mmc_blk_part_switch_post(card, part_type);
879 card->ext_csd.part_config = part_config;
881 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
884 main_md->part_curr = part_type;
888 static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
894 struct mmc_request mrq = {};
895 struct mmc_command cmd = {};
896 struct mmc_data data = {};
898 struct scatterlist sg;
900 cmd.opcode = MMC_APP_CMD;
901 cmd.arg = card->rca << 16;
902 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
904 err = mmc_wait_for_cmd(card->host, &cmd, 0);
907 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
910 memset(&cmd, 0, sizeof(struct mmc_command));
912 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
914 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
918 data.flags = MMC_DATA_READ;
921 mmc_set_data_timeout(&data, card);
926 blocks = kmalloc(4, GFP_KERNEL);
930 sg_init_one(&sg, blocks, 4);
932 mmc_wait_for_req(card->host, &mrq);
934 result = ntohl(*blocks);
937 if (cmd.error || data.error)
940 *written_blocks = result;
945 static unsigned int mmc_blk_clock_khz(struct mmc_host *host)
947 if (host->actual_clock)
948 return host->actual_clock / 1000;
950 /* Clock may be subject to a divisor, fudge it by a factor of 2. */
952 return host->ios.clock / 2000;
954 /* How can there be no clock */
956 return 100; /* 100 kHz is minimum possible value */
959 static unsigned int mmc_blk_data_timeout_ms(struct mmc_host *host,
960 struct mmc_data *data)
962 unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000);
965 if (data->timeout_clks) {
966 khz = mmc_blk_clock_khz(host);
967 ms += DIV_ROUND_UP(data->timeout_clks, khz);
973 static inline bool mmc_blk_in_tran_state(u32 status)
976 * Some cards mishandle the status bits, so make sure to check both the
977 * busy indication and the card state.
979 return status & R1_READY_FOR_DATA &&
980 (R1_CURRENT_STATE(status) == R1_STATE_TRAN);
983 static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
984 struct request *req, u32 *resp_errs)
986 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
991 bool done = time_after(jiffies, timeout);
993 err = __mmc_send_status(card, &status, 5);
995 pr_err("%s: error %d requesting status\n",
996 req->rq_disk->disk_name, err);
1000 /* Accumulate any response error bits seen */
1002 *resp_errs |= status;
1005 * Timeout if the device never becomes ready for data and never
1006 * leaves the program state.
1009 pr_err("%s: Card stuck in wrong state! %s %s status: %#x\n",
1010 mmc_hostname(card->host),
1011 req->rq_disk->disk_name, __func__, status);
1016 * Some cards mishandle the status bits,
1017 * so make sure to check both the busy
1018 * indication and the card state.
1020 } while (!mmc_blk_in_tran_state(status));
1025 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1030 if (md->reset_done & type)
1033 md->reset_done |= type;
1034 err = mmc_hw_reset(host);
1035 /* Ensure we switch back to the correct partition */
1036 if (err != -EOPNOTSUPP) {
1037 struct mmc_blk_data *main_md =
1038 dev_get_drvdata(&host->card->dev);
1041 main_md->part_curr = main_md->part_type;
1042 part_err = mmc_blk_part_switch(host->card, md->part_type);
1045 * We have failed to get back into the correct
1046 * partition, so we need to abort the whole request.
1054 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1056 md->reset_done &= ~type;
1060 * The non-block commands come back from the block layer after it queued it and
1061 * processed it with all other requests and then they get issued in this
1064 static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1066 struct mmc_queue_req *mq_rq;
1067 struct mmc_card *card = mq->card;
1068 struct mmc_blk_data *md = mq->blkdata;
1069 struct mmc_blk_ioc_data **idata;
1076 mq_rq = req_to_mmc_queue_req(req);
1077 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
1079 switch (mq_rq->drv_op) {
1080 case MMC_DRV_OP_IOCTL:
1081 case MMC_DRV_OP_IOCTL_RPMB:
1082 idata = mq_rq->drv_op_data;
1083 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
1084 ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
1088 /* Always switch back to main area after RPMB access */
1090 mmc_blk_part_switch(card, 0);
1092 case MMC_DRV_OP_BOOT_WP:
1093 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1094 card->ext_csd.boot_ro_lock |
1095 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1096 card->ext_csd.part_time);
1098 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1099 md->disk->disk_name, ret);
1101 card->ext_csd.boot_ro_lock |=
1102 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
1104 case MMC_DRV_OP_GET_CARD_STATUS:
1105 ret = mmc_send_status(card, &status);
1109 case MMC_DRV_OP_GET_EXT_CSD:
1110 ext_csd = mq_rq->drv_op_data;
1111 ret = mmc_get_ext_csd(card, ext_csd);
1114 pr_err("%s: unknown driver specific operation\n",
1115 md->disk->disk_name);
1119 mq_rq->drv_op_result = ret;
1120 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1123 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1125 struct mmc_blk_data *md = mq->blkdata;
1126 struct mmc_card *card = md->queue.card;
1127 unsigned int from, nr, arg;
1128 int err = 0, type = MMC_BLK_DISCARD;
1129 blk_status_t status = BLK_STS_OK;
1131 if (!mmc_can_erase(card)) {
1132 status = BLK_STS_NOTSUPP;
1136 from = blk_rq_pos(req);
1137 nr = blk_rq_sectors(req);
1139 if (mmc_can_discard(card))
1140 arg = MMC_DISCARD_ARG;
1141 else if (mmc_can_trim(card))
1144 arg = MMC_ERASE_ARG;
1147 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1148 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1149 INAND_CMD38_ARG_EXT_CSD,
1150 arg == MMC_TRIM_ARG ?
1151 INAND_CMD38_ARG_TRIM :
1152 INAND_CMD38_ARG_ERASE,
1156 err = mmc_erase(card, from, nr, arg);
1157 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
1159 status = BLK_STS_IOERR;
1161 mmc_blk_reset_success(md, type);
1163 blk_mq_end_request(req, status);
1166 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1167 struct request *req)
1169 struct mmc_blk_data *md = mq->blkdata;
1170 struct mmc_card *card = md->queue.card;
1171 unsigned int from, nr, arg;
1172 int err = 0, type = MMC_BLK_SECDISCARD;
1173 blk_status_t status = BLK_STS_OK;
1175 if (!(mmc_can_secure_erase_trim(card))) {
1176 status = BLK_STS_NOTSUPP;
1180 from = blk_rq_pos(req);
1181 nr = blk_rq_sectors(req);
1183 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1184 arg = MMC_SECURE_TRIM1_ARG;
1186 arg = MMC_SECURE_ERASE_ARG;
1189 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1190 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1191 INAND_CMD38_ARG_EXT_CSD,
1192 arg == MMC_SECURE_TRIM1_ARG ?
1193 INAND_CMD38_ARG_SECTRIM1 :
1194 INAND_CMD38_ARG_SECERASE,
1200 err = mmc_erase(card, from, nr, arg);
1204 status = BLK_STS_IOERR;
1208 if (arg == MMC_SECURE_TRIM1_ARG) {
1209 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1210 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1211 INAND_CMD38_ARG_EXT_CSD,
1212 INAND_CMD38_ARG_SECTRIM2,
1218 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1222 status = BLK_STS_IOERR;
1228 if (err && !mmc_blk_reset(md, card->host, type))
1231 mmc_blk_reset_success(md, type);
1233 blk_mq_end_request(req, status);
1236 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1238 struct mmc_blk_data *md = mq->blkdata;
1239 struct mmc_card *card = md->queue.card;
1242 ret = mmc_flush_cache(card);
1243 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1247 * Reformat current write as a reliable write, supporting
1248 * both legacy and the enhanced reliable write MMC cards.
1249 * In each transfer we'll handle only as much as a single
1250 * reliable write can handle, thus finish the request in
1251 * partial completions.
1253 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1254 struct mmc_card *card,
1255 struct request *req)
1257 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1258 /* Legacy mode imposes restrictions on transfers. */
1259 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
1260 brq->data.blocks = 1;
1262 if (brq->data.blocks > card->ext_csd.rel_sectors)
1263 brq->data.blocks = card->ext_csd.rel_sectors;
1264 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1265 brq->data.blocks = 1;
1269 #define CMD_ERRORS_EXCL_OOR \
1270 (R1_ADDRESS_ERROR | /* Misaligned address */ \
1271 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1272 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1273 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
1274 R1_CC_ERROR | /* Card controller error */ \
1275 R1_ERROR) /* General/unknown error */
1277 #define CMD_ERRORS \
1278 (CMD_ERRORS_EXCL_OOR | \
1279 R1_OUT_OF_RANGE) /* Command argument out of range */ \
1281 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
1286 * Per the SD specification(physical layer version 4.10)[1],
1287 * section 4.3.3, it explicitly states that "When the last
1288 * block of user area is read using CMD18, the host should
1289 * ignore OUT_OF_RANGE error that may occur even the sequence
1290 * is correct". And JESD84-B51 for eMMC also has a similar
1291 * statement on section 6.8.3.
1293 * Multiple block read/write could be done by either predefined
1294 * method, namely CMD23, or open-ending mode. For open-ending mode,
1295 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1297 * However the spec[1] doesn't tell us whether we should also
1298 * ignore that for predefined method. But per the spec[1], section
1299 * 4.15 Set Block Count Command, it says"If illegal block count
1300 * is set, out of range error will be indicated during read/write
1301 * operation (For example, data transfer is stopped at user area
1302 * boundary)." In another word, we could expect a out of range error
1303 * in the response for the following CMD18/25. And if argument of
1304 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1305 * we could also expect to get a -ETIMEDOUT or any error number from
1306 * the host drivers due to missing data response(for write)/data(for
1307 * read), as the cards will stop the data transfer by itself per the
1308 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1311 if (!brq->stop.error) {
1312 bool oor_with_open_end;
1313 /* If there is no error yet, check R1 response */
1315 val = brq->stop.resp[0] & CMD_ERRORS;
1316 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1318 if (val && !oor_with_open_end)
1319 brq->stop.error = -EIO;
1323 static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
1324 int disable_multi, bool *do_rel_wr_p,
1325 bool *do_data_tag_p)
1327 struct mmc_blk_data *md = mq->blkdata;
1328 struct mmc_card *card = md->queue.card;
1329 struct mmc_blk_request *brq = &mqrq->brq;
1330 struct request *req = mmc_queue_req_to_req(mqrq);
1331 bool do_rel_wr, do_data_tag;
1334 * Reliable writes are used to implement Forced Unit Access and
1335 * are supported only on MMCs.
1337 do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1338 rq_data_dir(req) == WRITE &&
1339 (md->flags & MMC_BLK_REL_WR);
1341 memset(brq, 0, sizeof(struct mmc_blk_request));
1343 brq->mrq.data = &brq->data;
1344 brq->mrq.tag = req->tag;
1346 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1349 if (rq_data_dir(req) == READ) {
1350 brq->data.flags = MMC_DATA_READ;
1351 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1353 brq->data.flags = MMC_DATA_WRITE;
1354 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1357 brq->data.blksz = 512;
1358 brq->data.blocks = blk_rq_sectors(req);
1359 brq->data.blk_addr = blk_rq_pos(req);
1362 * The command queue supports 2 priorities: "high" (1) and "simple" (0).
1363 * The eMMC will give "high" priority tasks priority over "simple"
1364 * priority tasks. Here we always set "simple" priority by not setting
1369 * The block layer doesn't support all sector count
1370 * restrictions, so we need to be prepared for too big
1373 if (brq->data.blocks > card->host->max_blk_count)
1374 brq->data.blocks = card->host->max_blk_count;
1376 if (brq->data.blocks > 1) {
1378 * Some SD cards in SPI mode return a CRC error or even lock up
1379 * completely when trying to read the last block using a
1380 * multiblock read command.
1382 if (mmc_host_is_spi(card->host) && (rq_data_dir(req) == READ) &&
1383 (blk_rq_pos(req) + blk_rq_sectors(req) ==
1384 get_capacity(md->disk)))
1388 * After a read error, we redo the request one sector
1389 * at a time in order to accurately determine which
1390 * sectors can be read successfully.
1393 brq->data.blocks = 1;
1396 * Some controllers have HW issues while operating
1397 * in multiple I/O mode
1399 if (card->host->ops->multi_io_quirk)
1400 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1401 (rq_data_dir(req) == READ) ?
1402 MMC_DATA_READ : MMC_DATA_WRITE,
1407 mmc_apply_rel_rw(brq, card, req);
1408 brq->data.flags |= MMC_DATA_REL_WR;
1412 * Data tag is used only during writing meta data to speed
1413 * up write and any subsequent read of this meta data
1415 do_data_tag = card->ext_csd.data_tag_unit_size &&
1416 (req->cmd_flags & REQ_META) &&
1417 (rq_data_dir(req) == WRITE) &&
1418 ((brq->data.blocks * brq->data.blksz) >=
1419 card->ext_csd.data_tag_unit_size);
1422 brq->data.flags |= MMC_DATA_DAT_TAG;
1424 mmc_set_data_timeout(&brq->data, card);
1426 brq->data.sg = mqrq->sg;
1427 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1430 * Adjust the sg list so it is the same size as the
1433 if (brq->data.blocks != blk_rq_sectors(req)) {
1434 int i, data_size = brq->data.blocks << 9;
1435 struct scatterlist *sg;
1437 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1438 data_size -= sg->length;
1439 if (data_size <= 0) {
1440 sg->length += data_size;
1445 brq->data.sg_len = i;
1449 *do_rel_wr_p = do_rel_wr;
1452 *do_data_tag_p = do_data_tag;
1455 #define MMC_CQE_RETRIES 2
1457 static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req)
1459 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1460 struct mmc_request *mrq = &mqrq->brq.mrq;
1461 struct request_queue *q = req->q;
1462 struct mmc_host *host = mq->card->host;
1463 unsigned long flags;
1467 mmc_cqe_post_req(host, mrq);
1469 if (mrq->cmd && mrq->cmd->error)
1470 err = mrq->cmd->error;
1471 else if (mrq->data && mrq->data->error)
1472 err = mrq->data->error;
1477 if (mqrq->retries++ < MMC_CQE_RETRIES)
1478 blk_mq_requeue_request(req, true);
1480 blk_mq_end_request(req, BLK_STS_IOERR);
1481 } else if (mrq->data) {
1482 if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered))
1483 blk_mq_requeue_request(req, true);
1485 __blk_mq_end_request(req, BLK_STS_OK);
1487 blk_mq_end_request(req, BLK_STS_OK);
1490 spin_lock_irqsave(&mq->lock, flags);
1492 mq->in_flight[mmc_issue_type(mq, req)] -= 1;
1494 put_card = (mmc_tot_in_flight(mq) == 0);
1496 mmc_cqe_check_busy(mq);
1498 spin_unlock_irqrestore(&mq->lock, flags);
1501 blk_mq_run_hw_queues(q, true);
1504 mmc_put_card(mq->card, &mq->ctx);
1507 void mmc_blk_cqe_recovery(struct mmc_queue *mq)
1509 struct mmc_card *card = mq->card;
1510 struct mmc_host *host = card->host;
1513 pr_debug("%s: CQE recovery start\n", mmc_hostname(host));
1515 err = mmc_cqe_recovery(host);
1517 mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY);
1519 mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY);
1521 pr_debug("%s: CQE recovery done\n", mmc_hostname(host));
1524 static void mmc_blk_cqe_req_done(struct mmc_request *mrq)
1526 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
1528 struct request *req = mmc_queue_req_to_req(mqrq);
1529 struct request_queue *q = req->q;
1530 struct mmc_queue *mq = q->queuedata;
1533 * Block layer timeouts race with completions which means the normal
1534 * completion path cannot be used during recovery.
1536 if (mq->in_recovery)
1537 mmc_blk_cqe_complete_rq(mq, req);
1539 blk_mq_complete_request(req);
1542 static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
1544 mrq->done = mmc_blk_cqe_req_done;
1545 mrq->recovery_notifier = mmc_cqe_recovery_notifier;
1547 return mmc_cqe_start_req(host, mrq);
1550 static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq,
1551 struct request *req)
1553 struct mmc_blk_request *brq = &mqrq->brq;
1555 memset(brq, 0, sizeof(*brq));
1557 brq->mrq.cmd = &brq->cmd;
1558 brq->mrq.tag = req->tag;
1563 static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
1565 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1566 struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);
1568 mrq->cmd->opcode = MMC_SWITCH;
1569 mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1570 (EXT_CSD_FLUSH_CACHE << 16) |
1572 EXT_CSD_CMD_SET_NORMAL;
1573 mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B;
1575 return mmc_blk_cqe_start_req(mq->card->host, mrq);
1578 static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1580 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1582 mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL);
1584 return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq);
1587 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1588 struct mmc_card *card,
1590 struct mmc_queue *mq)
1592 u32 readcmd, writecmd;
1593 struct mmc_blk_request *brq = &mqrq->brq;
1594 struct request *req = mmc_queue_req_to_req(mqrq);
1595 struct mmc_blk_data *md = mq->blkdata;
1596 bool do_rel_wr, do_data_tag;
1598 mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1600 brq->mrq.cmd = &brq->cmd;
1602 brq->cmd.arg = blk_rq_pos(req);
1603 if (!mmc_card_blockaddr(card))
1605 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1607 if (brq->data.blocks > 1 || do_rel_wr) {
1608 /* SPI multiblock writes terminate using a special
1609 * token, not a STOP_TRANSMISSION request.
1611 if (!mmc_host_is_spi(card->host) ||
1612 rq_data_dir(req) == READ)
1613 brq->mrq.stop = &brq->stop;
1614 readcmd = MMC_READ_MULTIPLE_BLOCK;
1615 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1617 brq->mrq.stop = NULL;
1618 readcmd = MMC_READ_SINGLE_BLOCK;
1619 writecmd = MMC_WRITE_BLOCK;
1621 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
1624 * Pre-defined multi-block transfers are preferable to
1625 * open ended-ones (and necessary for reliable writes).
1626 * However, it is not sufficient to just send CMD23,
1627 * and avoid the final CMD12, as on an error condition
1628 * CMD12 (stop) needs to be sent anyway. This, coupled
1629 * with Auto-CMD23 enhancements provided by some
1630 * hosts, means that the complexity of dealing
1631 * with this is best left to the host. If CMD23 is
1632 * supported by card and host, we'll fill sbc in and let
1633 * the host deal with handling it correctly. This means
1634 * that for hosts that don't expose MMC_CAP_CMD23, no
1635 * change of behavior will be observed.
1637 * N.B: Some MMC cards experience perf degradation.
1638 * We'll avoid using CMD23-bounded multiblock writes for
1639 * these, while retaining features like reliable writes.
1641 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1642 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1644 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1645 brq->sbc.arg = brq->data.blocks |
1646 (do_rel_wr ? (1 << 31) : 0) |
1647 (do_data_tag ? (1 << 29) : 0);
1648 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1649 brq->mrq.sbc = &brq->sbc;
1653 #define MMC_MAX_RETRIES 5
1654 #define MMC_DATA_RETRIES 2
1655 #define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1)
1657 static int mmc_blk_send_stop(struct mmc_card *card, unsigned int timeout)
1659 struct mmc_command cmd = {
1660 .opcode = MMC_STOP_TRANSMISSION,
1661 .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC,
1662 /* Some hosts wait for busy anyway, so provide a busy timeout */
1663 .busy_timeout = timeout,
1666 return mmc_wait_for_cmd(card->host, &cmd, 5);
1669 static int mmc_blk_fix_state(struct mmc_card *card, struct request *req)
1671 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1672 struct mmc_blk_request *brq = &mqrq->brq;
1673 unsigned int timeout = mmc_blk_data_timeout_ms(card->host, &brq->data);
1676 mmc_retune_hold_now(card->host);
1678 mmc_blk_send_stop(card, timeout);
1680 err = card_busy_detect(card, timeout, req, NULL);
1682 mmc_retune_release(card->host);
1687 #define MMC_READ_SINGLE_RETRIES 2
1689 /* Single sector read during recovery */
1690 static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req)
1692 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1693 struct mmc_request *mrq = &mqrq->brq.mrq;
1694 struct mmc_card *card = mq->card;
1695 struct mmc_host *host = card->host;
1696 blk_status_t error = BLK_STS_OK;
1703 mmc_blk_rw_rq_prep(mqrq, card, 1, mq);
1705 mmc_wait_for_req(host, mrq);
1707 err = mmc_send_status(card, &status);
1711 if (!mmc_host_is_spi(host) &&
1712 !mmc_blk_in_tran_state(status)) {
1713 err = mmc_blk_fix_state(card, req);
1718 if (mrq->cmd->error && retries++ < MMC_READ_SINGLE_RETRIES)
1723 if (mrq->cmd->error ||
1725 (!mmc_host_is_spi(host) &&
1726 (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS)))
1727 error = BLK_STS_IOERR;
1731 } while (blk_update_request(req, error, 512));
1736 mrq->data->bytes_xfered = 0;
1737 blk_update_request(req, BLK_STS_IOERR, 512);
1738 /* Let it try the remaining request again */
1739 if (mqrq->retries > MMC_MAX_RETRIES - 1)
1740 mqrq->retries = MMC_MAX_RETRIES - 1;
1743 static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq)
1745 return !!brq->mrq.sbc;
1748 static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq)
1750 return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR;
1754 * Check for errors the host controller driver might not have seen such as
1755 * response mode errors or invalid card state.
1757 static bool mmc_blk_status_error(struct request *req, u32 status)
1759 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1760 struct mmc_blk_request *brq = &mqrq->brq;
1761 struct mmc_queue *mq = req->q->queuedata;
1764 if (mmc_host_is_spi(mq->card->host))
1767 stop_err_bits = mmc_blk_stop_err_bits(brq);
1769 return brq->cmd.resp[0] & CMD_ERRORS ||
1770 brq->stop.resp[0] & stop_err_bits ||
1771 status & stop_err_bits ||
1772 (rq_data_dir(req) == WRITE && !mmc_blk_in_tran_state(status));
1775 static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq)
1777 return !brq->sbc.error && !brq->cmd.error &&
1778 !(brq->cmd.resp[0] & CMD_ERRORS);
1782 * Requests are completed by mmc_blk_mq_complete_rq() which sets simple
1784 * 1. A request that has transferred at least some data is considered
1785 * successful and will be requeued if there is remaining data to
1787 * 2. Otherwise the number of retries is incremented and the request
1788 * will be requeued if there are remaining retries.
1789 * 3. Otherwise the request will be errored out.
1790 * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and
1791 * mqrq->retries. So there are only 4 possible actions here:
1792 * 1. do not accept the bytes_xfered value i.e. set it to zero
1793 * 2. change mqrq->retries to determine the number of retries
1794 * 3. try to reset the card
1795 * 4. read one sector at a time
1797 static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req)
1799 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1800 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1801 struct mmc_blk_request *brq = &mqrq->brq;
1802 struct mmc_blk_data *md = mq->blkdata;
1803 struct mmc_card *card = mq->card;
1809 * Some errors the host driver might not have seen. Set the number of
1810 * bytes transferred to zero in that case.
1812 err = __mmc_send_status(card, &status, 0);
1813 if (err || mmc_blk_status_error(req, status))
1814 brq->data.bytes_xfered = 0;
1816 mmc_retune_release(card->host);
1819 * Try again to get the status. This also provides an opportunity for
1823 err = __mmc_send_status(card, &status, 0);
1826 * Nothing more to do after the number of bytes transferred has been
1827 * updated and there is no card.
1829 if (err && mmc_detect_card_removed(card->host))
1832 /* Try to get back to "tran" state */
1833 if (!mmc_host_is_spi(mq->card->host) &&
1834 (err || !mmc_blk_in_tran_state(status)))
1835 err = mmc_blk_fix_state(mq->card, req);
1838 * Special case for SD cards where the card might record the number of
1841 if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) &&
1842 rq_data_dir(req) == WRITE) {
1843 if (mmc_sd_num_wr_blocks(card, &blocks))
1844 brq->data.bytes_xfered = 0;
1846 brq->data.bytes_xfered = blocks << 9;
1849 /* Reset if the card is in a bad state */
1850 if (!mmc_host_is_spi(mq->card->host) &&
1851 err && mmc_blk_reset(md, card->host, type)) {
1852 pr_err("%s: recovery failed!\n", req->rq_disk->disk_name);
1853 mqrq->retries = MMC_NO_RETRIES;
1858 * If anything was done, just return and if there is anything remaining
1859 * on the request it will get requeued.
1861 if (brq->data.bytes_xfered)
1864 /* Reset before last retry */
1865 if (mqrq->retries + 1 == MMC_MAX_RETRIES)
1866 mmc_blk_reset(md, card->host, type);
1868 /* Command errors fail fast, so use all MMC_MAX_RETRIES */
1869 if (brq->sbc.error || brq->cmd.error)
1872 /* Reduce the remaining retries for data errors */
1873 if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) {
1874 mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES;
1878 /* FIXME: Missing single sector read for large sector size */
1879 if (!mmc_large_sector(card) && rq_data_dir(req) == READ &&
1880 brq->data.blocks > 1) {
1881 /* Read one sector at a time */
1882 mmc_blk_read_single(mq, req);
1887 static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq)
1889 mmc_blk_eval_resp_error(brq);
1891 return brq->sbc.error || brq->cmd.error || brq->stop.error ||
1892 brq->data.error || brq->cmd.resp[0] & CMD_ERRORS;
1895 static int mmc_blk_card_busy(struct mmc_card *card, struct request *req)
1897 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1901 if (mmc_host_is_spi(card->host) || rq_data_dir(req) == READ)
1904 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, req, &status);
1907 * Do not assume data transferred correctly if there are any error bits
1910 if (status & mmc_blk_stop_err_bits(&mqrq->brq)) {
1911 mqrq->brq.data.bytes_xfered = 0;
1912 err = err ? err : -EIO;
1915 /* Copy the exception bit so it will be seen later on */
1916 if (mmc_card_mmc(card) && status & R1_EXCEPTION_EVENT)
1917 mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
1922 static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
1923 struct request *req)
1925 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1927 mmc_blk_reset_success(mq->blkdata, type);
1930 static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)
1932 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1933 unsigned int nr_bytes = mqrq->brq.data.bytes_xfered;
1936 if (blk_update_request(req, BLK_STS_OK, nr_bytes))
1937 blk_mq_requeue_request(req, true);
1939 __blk_mq_end_request(req, BLK_STS_OK);
1940 } else if (!blk_rq_bytes(req)) {
1941 __blk_mq_end_request(req, BLK_STS_IOERR);
1942 } else if (mqrq->retries++ < MMC_MAX_RETRIES) {
1943 blk_mq_requeue_request(req, true);
1945 if (mmc_card_removed(mq->card))
1946 req->rq_flags |= RQF_QUIET;
1947 blk_mq_end_request(req, BLK_STS_IOERR);
1951 static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
1952 struct mmc_queue_req *mqrq)
1954 return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) &&
1955 (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
1956 mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
1959 static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
1960 struct mmc_queue_req *mqrq)
1962 if (mmc_blk_urgent_bkops_needed(mq, mqrq))
1963 mmc_run_bkops(mq->card);
1966 void mmc_blk_mq_complete(struct request *req)
1968 struct mmc_queue *mq = req->q->queuedata;
1971 mmc_blk_cqe_complete_rq(mq, req);
1973 mmc_blk_mq_complete_rq(mq, req);
1976 static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,
1977 struct request *req)
1979 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1980 struct mmc_host *host = mq->card->host;
1982 if (mmc_blk_rq_error(&mqrq->brq) ||
1983 mmc_blk_card_busy(mq->card, req)) {
1984 mmc_blk_mq_rw_recovery(mq, req);
1986 mmc_blk_rw_reset_success(mq, req);
1987 mmc_retune_release(host);
1990 mmc_blk_urgent_bkops(mq, mqrq);
1993 static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, struct request *req)
1995 unsigned long flags;
1998 spin_lock_irqsave(&mq->lock, flags);
2000 mq->in_flight[mmc_issue_type(mq, req)] -= 1;
2002 put_card = (mmc_tot_in_flight(mq) == 0);
2004 spin_unlock_irqrestore(&mq->lock, flags);
2007 mmc_put_card(mq->card, &mq->ctx);
2010 static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req)
2012 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2013 struct mmc_request *mrq = &mqrq->brq.mrq;
2014 struct mmc_host *host = mq->card->host;
2016 mmc_post_req(host, mrq, 0);
2019 * Block layer timeouts race with completions which means the normal
2020 * completion path cannot be used during recovery.
2022 if (mq->in_recovery)
2023 mmc_blk_mq_complete_rq(mq, req);
2025 blk_mq_complete_request(req);
2027 mmc_blk_mq_dec_in_flight(mq, req);
2030 void mmc_blk_mq_recovery(struct mmc_queue *mq)
2032 struct request *req = mq->recovery_req;
2033 struct mmc_host *host = mq->card->host;
2034 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2036 mq->recovery_req = NULL;
2037 mq->rw_wait = false;
2039 if (mmc_blk_rq_error(&mqrq->brq)) {
2040 mmc_retune_hold_now(host);
2041 mmc_blk_mq_rw_recovery(mq, req);
2044 mmc_blk_urgent_bkops(mq, mqrq);
2046 mmc_blk_mq_post_req(mq, req);
2049 static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq,
2050 struct request **prev_req)
2052 if (mmc_host_done_complete(mq->card->host))
2055 mutex_lock(&mq->complete_lock);
2057 if (!mq->complete_req)
2060 mmc_blk_mq_poll_completion(mq, mq->complete_req);
2063 *prev_req = mq->complete_req;
2065 mmc_blk_mq_post_req(mq, mq->complete_req);
2067 mq->complete_req = NULL;
2070 mutex_unlock(&mq->complete_lock);
2073 void mmc_blk_mq_complete_work(struct work_struct *work)
2075 struct mmc_queue *mq = container_of(work, struct mmc_queue,
2078 mmc_blk_mq_complete_prev_req(mq, NULL);
2081 static void mmc_blk_mq_req_done(struct mmc_request *mrq)
2083 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
2085 struct request *req = mmc_queue_req_to_req(mqrq);
2086 struct request_queue *q = req->q;
2087 struct mmc_queue *mq = q->queuedata;
2088 struct mmc_host *host = mq->card->host;
2089 unsigned long flags;
2091 if (!mmc_host_done_complete(host)) {
2095 * We cannot complete the request in this context, so record
2096 * that there is a request to complete, and that a following
2097 * request does not need to wait (although it does need to
2098 * complete complete_req first).
2100 spin_lock_irqsave(&mq->lock, flags);
2101 mq->complete_req = req;
2102 mq->rw_wait = false;
2103 waiting = mq->waiting;
2104 spin_unlock_irqrestore(&mq->lock, flags);
2107 * If 'waiting' then the waiting task will complete this
2108 * request, otherwise queue a work to do it. Note that
2109 * complete_work may still race with the dispatch of a following
2115 kblockd_schedule_work(&mq->complete_work);
2120 /* Take the recovery path for errors or urgent background operations */
2121 if (mmc_blk_rq_error(&mqrq->brq) ||
2122 mmc_blk_urgent_bkops_needed(mq, mqrq)) {
2123 spin_lock_irqsave(&mq->lock, flags);
2124 mq->recovery_needed = true;
2125 mq->recovery_req = req;
2126 spin_unlock_irqrestore(&mq->lock, flags);
2128 schedule_work(&mq->recovery_work);
2132 mmc_blk_rw_reset_success(mq, req);
2134 mq->rw_wait = false;
2137 mmc_blk_mq_post_req(mq, req);
2140 static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err)
2142 unsigned long flags;
2146 * Wait while there is another request in progress, but not if recovery
2147 * is needed. Also indicate whether there is a request waiting to start.
2149 spin_lock_irqsave(&mq->lock, flags);
2150 if (mq->recovery_needed) {
2154 done = !mq->rw_wait;
2156 mq->waiting = !done;
2157 spin_unlock_irqrestore(&mq->lock, flags);
2162 static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req)
2166 wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err));
2168 /* Always complete the previous request if there is one */
2169 mmc_blk_mq_complete_prev_req(mq, prev_req);
2174 static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq,
2175 struct request *req)
2177 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2178 struct mmc_host *host = mq->card->host;
2179 struct request *prev_req = NULL;
2182 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
2184 mqrq->brq.mrq.done = mmc_blk_mq_req_done;
2186 mmc_pre_req(host, &mqrq->brq.mrq);
2188 err = mmc_blk_rw_wait(mq, &prev_req);
2194 err = mmc_start_request(host, &mqrq->brq.mrq);
2197 mmc_blk_mq_post_req(mq, prev_req);
2200 mq->rw_wait = false;
2202 /* Release re-tuning here where there is no synchronization required */
2203 if (err || mmc_host_done_complete(host))
2204 mmc_retune_release(host);
2208 mmc_post_req(host, &mqrq->brq.mrq, err);
2213 static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host)
2216 return host->cqe_ops->cqe_wait_for_idle(host);
2218 return mmc_blk_rw_wait(mq, NULL);
2221 enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
2223 struct mmc_blk_data *md = mq->blkdata;
2224 struct mmc_card *card = md->queue.card;
2225 struct mmc_host *host = card->host;
2228 ret = mmc_blk_part_switch(card, md->part_type);
2230 return MMC_REQ_FAILED_TO_START;
2232 switch (mmc_issue_type(mq, req)) {
2233 case MMC_ISSUE_SYNC:
2234 ret = mmc_blk_wait_for_idle(mq, host);
2236 return MMC_REQ_BUSY;
2237 switch (req_op(req)) {
2239 case REQ_OP_DRV_OUT:
2240 mmc_blk_issue_drv_op(mq, req);
2242 case REQ_OP_DISCARD:
2243 mmc_blk_issue_discard_rq(mq, req);
2245 case REQ_OP_SECURE_ERASE:
2246 mmc_blk_issue_secdiscard_rq(mq, req);
2249 mmc_blk_issue_flush(mq, req);
2253 return MMC_REQ_FAILED_TO_START;
2255 return MMC_REQ_FINISHED;
2256 case MMC_ISSUE_DCMD:
2257 case MMC_ISSUE_ASYNC:
2258 switch (req_op(req)) {
2260 ret = mmc_blk_cqe_issue_flush(mq, req);
2265 ret = mmc_blk_cqe_issue_rw_rq(mq, req);
2267 ret = mmc_blk_mq_issue_rw_rq(mq, req);
2274 return MMC_REQ_STARTED;
2275 return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START;
2278 return MMC_REQ_FAILED_TO_START;
2282 static inline int mmc_blk_readonly(struct mmc_card *card)
2284 return mmc_card_readonly(card) ||
2285 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2288 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2289 struct device *parent,
2292 const char *subname,
2295 struct mmc_blk_data *md;
2298 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
2301 * We get -ENOSPC because there are no more any available
2302 * devidx. The reason may be that, either userspace haven't yet
2303 * unmounted the partitions, which postpones mmc_blk_release()
2304 * from being called, or the device has more partitions than
2307 if (devidx == -ENOSPC)
2308 dev_err(mmc_dev(card->host),
2309 "no more device IDs available\n");
2311 return ERR_PTR(devidx);
2314 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2320 md->area_type = area_type;
2323 * Set the read-only status based on the supported commands
2324 * and the write protect switch.
2326 md->read_only = mmc_blk_readonly(card);
2328 md->disk = alloc_disk(perdev_minors);
2329 if (md->disk == NULL) {
2334 INIT_LIST_HEAD(&md->part);
2335 INIT_LIST_HEAD(&md->rpmbs);
2338 ret = mmc_init_queue(&md->queue, card);
2342 md->queue.blkdata = md;
2345 * Keep an extra reference to the queue so that we can shutdown the
2346 * queue (i.e. call blk_cleanup_queue()) while there are still
2347 * references to the 'md'. The corresponding blk_put_queue() is in
2350 if (!blk_get_queue(md->queue.queue)) {
2351 mmc_cleanup_queue(&md->queue);
2356 md->disk->major = MMC_BLOCK_MAJOR;
2357 md->disk->first_minor = devidx * perdev_minors;
2358 md->disk->fops = &mmc_bdops;
2359 md->disk->private_data = md;
2360 md->disk->queue = md->queue.queue;
2361 md->parent = parent;
2362 set_disk_ro(md->disk, md->read_only || default_ro);
2363 md->disk->flags = GENHD_FL_EXT_DEVT;
2364 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2365 md->disk->flags |= GENHD_FL_NO_PART_SCAN
2366 | GENHD_FL_SUPPRESS_PARTITION_INFO;
2369 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2371 * - be set for removable media with permanent block devices
2372 * - be unset for removable block devices with permanent media
2374 * Since MMC block devices clearly fall under the second
2375 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2376 * should use the block device creation/destruction hotplug
2377 * messages to tell when the card is present.
2380 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2381 "mmcblk%u%s", card->host->index, subname ? subname : "");
2383 if (mmc_card_mmc(card))
2384 blk_queue_logical_block_size(md->queue.queue,
2385 card->ext_csd.data_sector_size);
2387 blk_queue_logical_block_size(md->queue.queue, 512);
2389 set_capacity(md->disk, size);
2391 if (mmc_host_cmd23(card->host)) {
2392 if ((mmc_card_mmc(card) &&
2393 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
2394 (mmc_card_sd(card) &&
2395 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2396 md->flags |= MMC_BLK_CMD23;
2399 if (mmc_card_mmc(card) &&
2400 md->flags & MMC_BLK_CMD23 &&
2401 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2402 card->ext_csd.rel_sectors)) {
2403 md->flags |= MMC_BLK_REL_WR;
2404 blk_queue_write_cache(md->queue.queue, true, true);
2414 ida_simple_remove(&mmc_blk_ida, devidx);
2415 return ERR_PTR(ret);
2418 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2422 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2424 * The EXT_CSD sector count is in number or 512 byte
2427 size = card->ext_csd.sectors;
2430 * The CSD capacity field is in units of read_blkbits.
2431 * set_capacity takes units of 512 bytes.
2433 size = (typeof(sector_t))card->csd.capacity
2434 << (card->csd.read_blkbits - 9);
2437 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2438 MMC_BLK_DATA_AREA_MAIN);
2441 static int mmc_blk_alloc_part(struct mmc_card *card,
2442 struct mmc_blk_data *md,
2443 unsigned int part_type,
2446 const char *subname,
2450 struct mmc_blk_data *part_md;
2452 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2453 subname, area_type);
2454 if (IS_ERR(part_md))
2455 return PTR_ERR(part_md);
2456 part_md->part_type = part_type;
2457 list_add(&part_md->part, &md->part);
2459 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
2460 cap_str, sizeof(cap_str));
2461 pr_info("%s: %s %s partition %u %s\n",
2462 part_md->disk->disk_name, mmc_card_id(card),
2463 mmc_card_name(card), part_md->part_type, cap_str);
2468 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2469 * @filp: the character device file
2470 * @cmd: the ioctl() command
2471 * @arg: the argument from userspace
2473 * This will essentially just redirect the ioctl()s coming in over to
2474 * the main block device spawning the RPMB character device.
2476 static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
2479 struct mmc_rpmb_data *rpmb = filp->private_data;
2484 ret = mmc_blk_ioctl_cmd(rpmb->md,
2485 (struct mmc_ioc_cmd __user *)arg,
2488 case MMC_IOC_MULTI_CMD:
2489 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
2490 (struct mmc_ioc_multi_cmd __user *)arg,
2501 #ifdef CONFIG_COMPAT
2502 static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
2505 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
2509 static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
2511 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2512 struct mmc_rpmb_data, chrdev);
2514 get_device(&rpmb->dev);
2515 filp->private_data = rpmb;
2516 mmc_blk_get(rpmb->md->disk);
2518 return nonseekable_open(inode, filp);
2521 static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
2523 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2524 struct mmc_rpmb_data, chrdev);
2526 put_device(&rpmb->dev);
2527 mmc_blk_put(rpmb->md);
2532 static const struct file_operations mmc_rpmb_fileops = {
2533 .release = mmc_rpmb_chrdev_release,
2534 .open = mmc_rpmb_chrdev_open,
2535 .owner = THIS_MODULE,
2536 .llseek = no_llseek,
2537 .unlocked_ioctl = mmc_rpmb_ioctl,
2538 #ifdef CONFIG_COMPAT
2539 .compat_ioctl = mmc_rpmb_ioctl_compat,
2543 static void mmc_blk_rpmb_device_release(struct device *dev)
2545 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2547 ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2551 static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
2552 struct mmc_blk_data *md,
2553 unsigned int part_index,
2555 const char *subname)
2558 char rpmb_name[DISK_NAME_LEN];
2560 struct mmc_rpmb_data *rpmb;
2562 /* This creates the minor number for the RPMB char device */
2563 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
2567 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
2569 ida_simple_remove(&mmc_rpmb_ida, devidx);
2573 snprintf(rpmb_name, sizeof(rpmb_name),
2574 "mmcblk%u%s", card->host->index, subname ? subname : "");
2577 rpmb->part_index = part_index;
2578 rpmb->dev.init_name = rpmb_name;
2579 rpmb->dev.bus = &mmc_rpmb_bus_type;
2580 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
2581 rpmb->dev.parent = &card->dev;
2582 rpmb->dev.release = mmc_blk_rpmb_device_release;
2583 device_initialize(&rpmb->dev);
2584 dev_set_drvdata(&rpmb->dev, rpmb);
2587 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
2588 rpmb->chrdev.owner = THIS_MODULE;
2589 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
2591 pr_err("%s: could not add character device\n", rpmb_name);
2592 goto out_put_device;
2595 list_add(&rpmb->node, &md->rpmbs);
2597 string_get_size((u64)size, 512, STRING_UNITS_2,
2598 cap_str, sizeof(cap_str));
2600 pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n",
2601 rpmb_name, mmc_card_id(card),
2602 mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str,
2603 MAJOR(mmc_rpmb_devt), rpmb->id);
2608 put_device(&rpmb->dev);
2612 static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
2615 cdev_device_del(&rpmb->chrdev, &rpmb->dev);
2616 put_device(&rpmb->dev);
2619 /* MMC Physical partitions consist of two boot partitions and
2620 * up to four general purpose partitions.
2621 * For each partition enabled in EXT_CSD a block device will be allocatedi
2622 * to provide access to the partition.
2625 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2629 if (!mmc_card_mmc(card))
2632 for (idx = 0; idx < card->nr_parts; idx++) {
2633 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
2635 * RPMB partitions does not provide block access, they
2636 * are only accessed using ioctl():s. Thus create
2637 * special RPMB block devices that do not have a
2638 * backing block queue for these.
2640 ret = mmc_blk_alloc_rpmb_part(card, md,
2641 card->part[idx].part_cfg,
2642 card->part[idx].size >> 9,
2643 card->part[idx].name);
2646 } else if (card->part[idx].size) {
2647 ret = mmc_blk_alloc_part(card, md,
2648 card->part[idx].part_cfg,
2649 card->part[idx].size >> 9,
2650 card->part[idx].force_ro,
2651 card->part[idx].name,
2652 card->part[idx].area_type);
2661 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2663 struct mmc_card *card;
2667 * Flush remaining requests and free queues. It
2668 * is freeing the queue that stops new requests
2669 * from being accepted.
2671 card = md->queue.card;
2672 if (md->disk->flags & GENHD_FL_UP) {
2673 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2674 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2675 card->ext_csd.boot_ro_lockable)
2676 device_remove_file(disk_to_dev(md->disk),
2677 &md->power_ro_lock);
2679 del_gendisk(md->disk);
2681 mmc_cleanup_queue(&md->queue);
2686 static void mmc_blk_remove_parts(struct mmc_card *card,
2687 struct mmc_blk_data *md)
2689 struct list_head *pos, *q;
2690 struct mmc_blk_data *part_md;
2691 struct mmc_rpmb_data *rpmb;
2693 /* Remove RPMB partitions */
2694 list_for_each_safe(pos, q, &md->rpmbs) {
2695 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
2697 mmc_blk_remove_rpmb_part(rpmb);
2699 /* Remove block partitions */
2700 list_for_each_safe(pos, q, &md->part) {
2701 part_md = list_entry(pos, struct mmc_blk_data, part);
2703 mmc_blk_remove_req(part_md);
2707 static int mmc_add_disk(struct mmc_blk_data *md)
2710 struct mmc_card *card = md->queue.card;
2712 device_add_disk(md->parent, md->disk, NULL);
2713 md->force_ro.show = force_ro_show;
2714 md->force_ro.store = force_ro_store;
2715 sysfs_attr_init(&md->force_ro.attr);
2716 md->force_ro.attr.name = "force_ro";
2717 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2718 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2722 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2723 card->ext_csd.boot_ro_lockable) {
2726 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2729 mode = S_IRUGO | S_IWUSR;
2731 md->power_ro_lock.show = power_ro_lock_show;
2732 md->power_ro_lock.store = power_ro_lock_store;
2733 sysfs_attr_init(&md->power_ro_lock.attr);
2734 md->power_ro_lock.attr.mode = mode;
2735 md->power_ro_lock.attr.name =
2736 "ro_lock_until_next_power_on";
2737 ret = device_create_file(disk_to_dev(md->disk),
2738 &md->power_ro_lock);
2740 goto power_ro_lock_fail;
2745 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2747 del_gendisk(md->disk);
2752 #ifdef CONFIG_DEBUG_FS
2754 static int mmc_dbg_card_status_get(void *data, u64 *val)
2756 struct mmc_card *card = data;
2757 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2758 struct mmc_queue *mq = &md->queue;
2759 struct request *req;
2762 /* Ask the block layer about the card status */
2763 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0);
2765 return PTR_ERR(req);
2766 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2767 blk_execute_rq(mq->queue, NULL, req, 0);
2768 ret = req_to_mmc_queue_req(req)->drv_op_result;
2773 blk_put_request(req);
2777 DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2780 /* That is two digits * 512 + 1 for newline */
2781 #define EXT_CSD_STR_LEN 1025
2783 static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2785 struct mmc_card *card = inode->i_private;
2786 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2787 struct mmc_queue *mq = &md->queue;
2788 struct request *req;
2794 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2798 /* Ask the block layer for the EXT CSD */
2799 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0);
2804 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2805 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2806 blk_execute_rq(mq->queue, NULL, req, 0);
2807 err = req_to_mmc_queue_req(req)->drv_op_result;
2808 blk_put_request(req);
2810 pr_err("FAILED %d\n", err);
2814 for (i = 0; i < 512; i++)
2815 n += sprintf(buf + n, "%02x", ext_csd[i]);
2816 n += sprintf(buf + n, "\n");
2818 if (n != EXT_CSD_STR_LEN) {
2824 filp->private_data = buf;
2833 static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2834 size_t cnt, loff_t *ppos)
2836 char *buf = filp->private_data;
2838 return simple_read_from_buffer(ubuf, cnt, ppos,
2839 buf, EXT_CSD_STR_LEN);
2842 static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2844 kfree(file->private_data);
2848 static const struct file_operations mmc_dbg_ext_csd_fops = {
2849 .open = mmc_ext_csd_open,
2850 .read = mmc_ext_csd_read,
2851 .release = mmc_ext_csd_release,
2852 .llseek = default_llseek,
2855 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2857 struct dentry *root;
2859 if (!card->debugfs_root)
2862 root = card->debugfs_root;
2864 if (mmc_card_mmc(card) || mmc_card_sd(card)) {
2866 debugfs_create_file("status", S_IRUSR, root, card,
2867 &mmc_dbg_card_status_fops);
2868 if (!md->status_dentry)
2872 if (mmc_card_mmc(card)) {
2873 md->ext_csd_dentry =
2874 debugfs_create_file("ext_csd", S_IRUSR, root, card,
2875 &mmc_dbg_ext_csd_fops);
2876 if (!md->ext_csd_dentry)
2883 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2884 struct mmc_blk_data *md)
2886 if (!card->debugfs_root)
2889 if (!IS_ERR_OR_NULL(md->status_dentry)) {
2890 debugfs_remove(md->status_dentry);
2891 md->status_dentry = NULL;
2894 if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) {
2895 debugfs_remove(md->ext_csd_dentry);
2896 md->ext_csd_dentry = NULL;
2902 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2907 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2908 struct mmc_blk_data *md)
2912 #endif /* CONFIG_DEBUG_FS */
2914 static int mmc_blk_probe(struct mmc_card *card)
2916 struct mmc_blk_data *md, *part_md;
2920 * Check that the card supports the command class(es) we need.
2922 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2925 mmc_fixup_device(card, mmc_blk_fixups);
2927 md = mmc_blk_alloc(card);
2931 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
2932 cap_str, sizeof(cap_str));
2933 pr_info("%s: %s %s %s %s\n",
2934 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2935 cap_str, md->read_only ? "(ro)" : "");
2937 if (mmc_blk_alloc_parts(card, md))
2940 dev_set_drvdata(&card->dev, md);
2942 if (mmc_add_disk(md))
2945 list_for_each_entry(part_md, &md->part, part) {
2946 if (mmc_add_disk(part_md))
2950 /* Add two debugfs entries */
2951 mmc_blk_add_debugfs(card, md);
2953 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2954 pm_runtime_use_autosuspend(&card->dev);
2957 * Don't enable runtime PM for SD-combo cards here. Leave that
2958 * decision to be taken during the SDIO init sequence instead.
2960 if (card->type != MMC_TYPE_SD_COMBO) {
2961 pm_runtime_set_active(&card->dev);
2962 pm_runtime_enable(&card->dev);
2968 mmc_blk_remove_parts(card, md);
2969 mmc_blk_remove_req(md);
2973 static void mmc_blk_remove(struct mmc_card *card)
2975 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2977 mmc_blk_remove_debugfs(card, md);
2978 mmc_blk_remove_parts(card, md);
2979 pm_runtime_get_sync(&card->dev);
2980 if (md->part_curr != md->part_type) {
2981 mmc_claim_host(card->host);
2982 mmc_blk_part_switch(card, md->part_type);
2983 mmc_release_host(card->host);
2985 if (card->type != MMC_TYPE_SD_COMBO)
2986 pm_runtime_disable(&card->dev);
2987 pm_runtime_put_noidle(&card->dev);
2988 mmc_blk_remove_req(md);
2989 dev_set_drvdata(&card->dev, NULL);
2992 static int _mmc_blk_suspend(struct mmc_card *card)
2994 struct mmc_blk_data *part_md;
2995 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2998 mmc_queue_suspend(&md->queue);
2999 list_for_each_entry(part_md, &md->part, part) {
3000 mmc_queue_suspend(&part_md->queue);
3006 static void mmc_blk_shutdown(struct mmc_card *card)
3008 _mmc_blk_suspend(card);
3011 #ifdef CONFIG_PM_SLEEP
3012 static int mmc_blk_suspend(struct device *dev)
3014 struct mmc_card *card = mmc_dev_to_card(dev);
3016 return _mmc_blk_suspend(card);
3019 static int mmc_blk_resume(struct device *dev)
3021 struct mmc_blk_data *part_md;
3022 struct mmc_blk_data *md = dev_get_drvdata(dev);
3026 * Resume involves the card going into idle state,
3027 * so current partition is always the main one.
3029 md->part_curr = md->part_type;
3030 mmc_queue_resume(&md->queue);
3031 list_for_each_entry(part_md, &md->part, part) {
3032 mmc_queue_resume(&part_md->queue);
3039 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3041 static struct mmc_driver mmc_driver = {
3044 .pm = &mmc_blk_pm_ops,
3046 .probe = mmc_blk_probe,
3047 .remove = mmc_blk_remove,
3048 .shutdown = mmc_blk_shutdown,
3051 static int __init mmc_blk_init(void)
3055 res = bus_register(&mmc_rpmb_bus_type);
3057 pr_err("mmcblk: could not register RPMB bus type\n");
3060 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
3062 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
3066 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3067 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3069 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
3071 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3073 goto out_chrdev_unreg;
3075 res = mmc_register_driver(&mmc_driver);
3077 goto out_blkdev_unreg;
3082 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3084 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3086 bus_unregister(&mmc_rpmb_bus_type);
3090 static void __exit mmc_blk_exit(void)
3092 mmc_unregister_driver(&mmc_driver);
3093 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3094 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3095 bus_unregister(&mmc_rpmb_bus_type);
3098 module_init(mmc_blk_init);
3099 module_exit(mmc_blk_exit);
3101 MODULE_LICENSE("GPL");
3102 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");