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."
66 #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
67 #define MMC_SANITIZE_REQ_TIMEOUT 240000
68 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
69 #define MMC_EXTRACT_VALUE_FROM_ARG(x) ((x & 0x0000FF00) >> 8)
71 #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
72 (rq_data_dir(req) == WRITE))
73 static DEFINE_MUTEX(block_mutex);
76 * The defaults come from config options but can be overriden by module
79 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
82 * We've only got one major, so number of mmcblk devices is
83 * limited to (1 << 20) / number of minors per device. It is also
84 * limited by the MAX_DEVICES below.
86 static int max_devices;
88 #define MAX_DEVICES 256
90 static DEFINE_IDA(mmc_blk_ida);
91 static DEFINE_IDA(mmc_rpmb_ida);
94 * There is one mmc_blk_data per slot.
98 struct device *parent;
100 struct mmc_queue queue;
101 struct list_head part;
102 struct list_head rpmbs;
105 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
106 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
109 unsigned int read_only;
110 unsigned int part_type;
111 unsigned int reset_done;
112 #define MMC_BLK_READ BIT(0)
113 #define MMC_BLK_WRITE BIT(1)
114 #define MMC_BLK_DISCARD BIT(2)
115 #define MMC_BLK_SECDISCARD BIT(3)
118 * Only set in main mmc_blk_data associated
119 * with mmc_card with dev_set_drvdata, and keeps
120 * track of the current selected device partition.
122 unsigned int part_curr;
123 struct device_attribute force_ro;
124 struct device_attribute power_ro_lock;
127 /* debugfs files (only in main mmc_blk_data) */
128 struct dentry *status_dentry;
129 struct dentry *ext_csd_dentry;
132 /* Device type for RPMB character devices */
133 static dev_t mmc_rpmb_devt;
135 /* Bus type for RPMB character devices */
136 static struct bus_type mmc_rpmb_bus_type = {
141 * struct mmc_rpmb_data - special RPMB device type for these areas
142 * @dev: the device for the RPMB area
143 * @chrdev: character device for the RPMB area
144 * @id: unique device ID number
145 * @part_index: partition index (0 on first)
146 * @md: parent MMC block device
147 * @node: list item, so we can put this device on a list
149 struct mmc_rpmb_data {
153 unsigned int part_index;
154 struct mmc_blk_data *md;
155 struct list_head node;
158 static DEFINE_MUTEX(open_lock);
160 module_param(perdev_minors, int, 0444);
161 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
163 static inline int mmc_blk_part_switch(struct mmc_card *card,
164 unsigned int part_type);
166 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
168 struct mmc_blk_data *md;
170 mutex_lock(&open_lock);
171 md = disk->private_data;
172 if (md && md->usage == 0)
176 mutex_unlock(&open_lock);
181 static inline int mmc_get_devidx(struct gendisk *disk)
183 int devidx = disk->first_minor / perdev_minors;
187 static void mmc_blk_put(struct mmc_blk_data *md)
189 mutex_lock(&open_lock);
191 if (md->usage == 0) {
192 int devidx = mmc_get_devidx(md->disk);
193 blk_cleanup_queue(md->queue.queue);
194 ida_simple_remove(&mmc_blk_ida, devidx);
198 mutex_unlock(&open_lock);
201 static ssize_t power_ro_lock_show(struct device *dev,
202 struct device_attribute *attr, char *buf)
205 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
206 struct mmc_card *card = md->queue.card;
209 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
211 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
214 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
221 static ssize_t power_ro_lock_store(struct device *dev,
222 struct device_attribute *attr, const char *buf, size_t count)
225 struct mmc_blk_data *md, *part_md;
226 struct mmc_queue *mq;
230 if (kstrtoul(buf, 0, &set))
236 md = mmc_blk_get(dev_to_disk(dev));
239 /* Dispatch locking to the block layer */
240 req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, __GFP_RECLAIM);
242 count = PTR_ERR(req);
245 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
246 blk_execute_rq(mq->queue, NULL, req, 0);
247 ret = req_to_mmc_queue_req(req)->drv_op_result;
248 blk_put_request(req);
251 pr_info("%s: Locking boot partition ro until next power on\n",
252 md->disk->disk_name);
253 set_disk_ro(md->disk, 1);
255 list_for_each_entry(part_md, &md->part, part)
256 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
257 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
258 set_disk_ro(part_md->disk, 1);
266 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
270 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
272 ret = snprintf(buf, PAGE_SIZE, "%d\n",
273 get_disk_ro(dev_to_disk(dev)) ^
279 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
280 const char *buf, size_t count)
284 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
285 unsigned long set = simple_strtoul(buf, &end, 0);
291 set_disk_ro(dev_to_disk(dev), set || md->read_only);
298 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
300 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
303 mutex_lock(&block_mutex);
306 check_disk_change(bdev);
309 if ((mode & FMODE_WRITE) && md->read_only) {
314 mutex_unlock(&block_mutex);
319 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
321 struct mmc_blk_data *md = disk->private_data;
323 mutex_lock(&block_mutex);
325 mutex_unlock(&block_mutex);
329 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
331 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
337 struct mmc_blk_ioc_data {
338 struct mmc_ioc_cmd ic;
341 struct mmc_rpmb_data *rpmb;
344 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
345 struct mmc_ioc_cmd __user *user)
347 struct mmc_blk_ioc_data *idata;
350 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
356 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
361 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
362 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
367 if (!idata->buf_bytes) {
372 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
378 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
379 idata->ic.data_ptr, idata->buf_bytes)) {
394 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
395 struct mmc_blk_ioc_data *idata)
397 struct mmc_ioc_cmd *ic = &idata->ic;
399 if (copy_to_user(&(ic_ptr->response), ic->response,
400 sizeof(ic->response)))
403 if (!idata->ic.write_flag) {
404 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
405 idata->buf, idata->buf_bytes))
412 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
418 if (!status || !retries_max)
422 err = __mmc_send_status(card, status, 5);
426 if (!R1_STATUS(*status) &&
427 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
428 break; /* RPMB programming operation complete */
431 * Rechedule to give the MMC device a chance to continue
432 * processing the previous command without being polled too
435 usleep_range(1000, 5000);
436 } while (++retry_count < retries_max);
438 if (retry_count == retries_max)
444 static int ioctl_do_sanitize(struct mmc_card *card)
448 if (!mmc_can_sanitize(card)) {
449 pr_warn("%s: %s - SANITIZE is not supported\n",
450 mmc_hostname(card->host), __func__);
455 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
456 mmc_hostname(card->host), __func__);
458 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
459 EXT_CSD_SANITIZE_START, 1,
460 MMC_SANITIZE_REQ_TIMEOUT);
463 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
464 mmc_hostname(card->host), __func__, err);
466 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
472 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
473 struct mmc_blk_ioc_data *idata)
475 struct mmc_command cmd = {};
476 struct mmc_data data = {};
477 struct mmc_request mrq = {};
478 struct scatterlist sg;
480 unsigned int target_part;
483 if (!card || !md || !idata)
487 * The RPMB accesses comes in from the character device, so we
488 * need to target these explicitly. Else we just target the
489 * partition type for the block device the ioctl() was issued
493 /* Support multiple RPMB partitions */
494 target_part = idata->rpmb->part_index;
495 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
497 target_part = md->part_type;
500 cmd.opcode = idata->ic.opcode;
501 cmd.arg = idata->ic.arg;
502 cmd.flags = idata->ic.flags;
504 if (idata->buf_bytes) {
507 data.blksz = idata->ic.blksz;
508 data.blocks = idata->ic.blocks;
510 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
512 if (idata->ic.write_flag)
513 data.flags = MMC_DATA_WRITE;
515 data.flags = MMC_DATA_READ;
517 /* data.flags must already be set before doing this. */
518 mmc_set_data_timeout(&data, card);
520 /* Allow overriding the timeout_ns for empirical tuning. */
521 if (idata->ic.data_timeout_ns)
522 data.timeout_ns = idata->ic.data_timeout_ns;
524 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
526 * Pretend this is a data transfer and rely on the
527 * host driver to compute timeout. When all host
528 * drivers support cmd.cmd_timeout for R1B, this
532 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
534 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
542 err = mmc_blk_part_switch(card, target_part);
546 if (idata->ic.is_acmd) {
547 err = mmc_app_cmd(card->host, card);
553 err = mmc_set_blockcount(card, data.blocks,
554 idata->ic.write_flag & (1 << 31));
559 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
560 (cmd.opcode == MMC_SWITCH)) {
561 err = ioctl_do_sanitize(card);
564 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
570 mmc_wait_for_req(card->host, &mrq);
573 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
574 __func__, cmd.error);
578 dev_err(mmc_dev(card->host), "%s: data error %d\n",
579 __func__, data.error);
584 * Make sure the cache of the PARTITION_CONFIG register and
585 * PARTITION_ACCESS bits is updated in case the ioctl ext_csd write
586 * changed it successfully.
588 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_PART_CONFIG) &&
589 (cmd.opcode == MMC_SWITCH)) {
590 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
591 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg);
594 * Update cache so the next mmc_blk_part_switch call operates
595 * on up-to-date data.
597 card->ext_csd.part_config = value;
598 main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
602 * Make sure to update CACHE_CTRL in case it was changed. The cache
603 * will get turned back on if the card is re-initialized, e.g.
604 * suspend/resume or hw reset in recovery.
606 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_CACHE_CTRL) &&
607 (cmd.opcode == MMC_SWITCH)) {
608 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg) & 1;
610 card->ext_csd.cache_ctrl = value;
614 * According to the SD specs, some commands require a delay after
615 * issuing the command.
617 if (idata->ic.postsleep_min_us)
618 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
620 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
624 * Ensure RPMB command has completed by polling CMD13
627 err = ioctl_rpmb_card_status_poll(card, &status, 5);
629 dev_err(mmc_dev(card->host),
630 "%s: Card Status=0x%08X, error %d\n",
631 __func__, status, err);
637 static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
638 struct mmc_ioc_cmd __user *ic_ptr,
639 struct mmc_rpmb_data *rpmb)
641 struct mmc_blk_ioc_data *idata;
642 struct mmc_blk_ioc_data *idatas[1];
643 struct mmc_queue *mq;
644 struct mmc_card *card;
645 int err = 0, ioc_err = 0;
648 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
650 return PTR_ERR(idata);
651 /* This will be NULL on non-RPMB ioctl():s */
654 card = md->queue.card;
661 * Dispatch the ioctl() into the block request queue.
664 req = blk_get_request(mq->queue,
665 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
672 req_to_mmc_queue_req(req)->drv_op =
673 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
674 req_to_mmc_queue_req(req)->drv_op_data = idatas;
675 req_to_mmc_queue_req(req)->ioc_count = 1;
676 blk_execute_rq(mq->queue, NULL, req, 0);
677 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
678 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
679 blk_put_request(req);
684 return ioc_err ? ioc_err : err;
687 static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
688 struct mmc_ioc_multi_cmd __user *user,
689 struct mmc_rpmb_data *rpmb)
691 struct mmc_blk_ioc_data **idata = NULL;
692 struct mmc_ioc_cmd __user *cmds = user->cmds;
693 struct mmc_card *card;
694 struct mmc_queue *mq;
695 int i, err = 0, ioc_err = 0;
699 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
700 sizeof(num_of_cmds)))
706 if (num_of_cmds > MMC_IOC_MAX_CMDS)
709 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
713 for (i = 0; i < num_of_cmds; i++) {
714 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
715 if (IS_ERR(idata[i])) {
716 err = PTR_ERR(idata[i]);
720 /* This will be NULL on non-RPMB ioctl():s */
721 idata[i]->rpmb = rpmb;
724 card = md->queue.card;
732 * Dispatch the ioctl()s into the block request queue.
735 req = blk_get_request(mq->queue,
736 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
742 req_to_mmc_queue_req(req)->drv_op =
743 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
744 req_to_mmc_queue_req(req)->drv_op_data = idata;
745 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
746 blk_execute_rq(mq->queue, NULL, req, 0);
747 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
749 /* copy to user if data and response */
750 for (i = 0; i < num_of_cmds && !err; i++)
751 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
753 blk_put_request(req);
756 for (i = 0; i < num_of_cmds; i++) {
757 kfree(idata[i]->buf);
761 return ioc_err ? ioc_err : err;
764 static int mmc_blk_check_blkdev(struct block_device *bdev)
767 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
768 * whole block device, not on a partition. This prevents overspray
769 * between sibling partitions.
771 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
776 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
777 unsigned int cmd, unsigned long arg)
779 struct mmc_blk_data *md;
784 ret = mmc_blk_check_blkdev(bdev);
787 md = mmc_blk_get(bdev->bd_disk);
790 ret = mmc_blk_ioctl_cmd(md,
791 (struct mmc_ioc_cmd __user *)arg,
795 case MMC_IOC_MULTI_CMD:
796 ret = mmc_blk_check_blkdev(bdev);
799 md = mmc_blk_get(bdev->bd_disk);
802 ret = mmc_blk_ioctl_multi_cmd(md,
803 (struct mmc_ioc_multi_cmd __user *)arg,
813 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
814 unsigned int cmd, unsigned long arg)
816 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
820 static const struct block_device_operations mmc_bdops = {
821 .open = mmc_blk_open,
822 .release = mmc_blk_release,
823 .getgeo = mmc_blk_getgeo,
824 .owner = THIS_MODULE,
825 .ioctl = mmc_blk_ioctl,
827 .compat_ioctl = mmc_blk_compat_ioctl,
831 static int mmc_blk_part_switch_pre(struct mmc_card *card,
832 unsigned int part_type)
836 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
837 if (card->ext_csd.cmdq_en) {
838 ret = mmc_cmdq_disable(card);
842 mmc_retune_pause(card->host);
848 static int mmc_blk_part_switch_post(struct mmc_card *card,
849 unsigned int part_type)
853 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
854 mmc_retune_unpause(card->host);
855 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
856 ret = mmc_cmdq_enable(card);
862 static inline int mmc_blk_part_switch(struct mmc_card *card,
863 unsigned int part_type)
866 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
868 if (main_md->part_curr == part_type)
871 if (mmc_card_mmc(card)) {
872 u8 part_config = card->ext_csd.part_config;
874 ret = mmc_blk_part_switch_pre(card, part_type);
878 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
879 part_config |= part_type;
881 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
882 EXT_CSD_PART_CONFIG, part_config,
883 card->ext_csd.part_time);
885 mmc_blk_part_switch_post(card, part_type);
889 card->ext_csd.part_config = part_config;
891 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
894 main_md->part_curr = part_type;
898 static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
904 struct mmc_request mrq = {};
905 struct mmc_command cmd = {};
906 struct mmc_data data = {};
908 struct scatterlist sg;
910 cmd.opcode = MMC_APP_CMD;
911 cmd.arg = card->rca << 16;
912 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
914 err = mmc_wait_for_cmd(card->host, &cmd, 0);
917 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
920 memset(&cmd, 0, sizeof(struct mmc_command));
922 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
924 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
928 data.flags = MMC_DATA_READ;
931 mmc_set_data_timeout(&data, card);
936 blocks = kmalloc(4, GFP_KERNEL);
940 sg_init_one(&sg, blocks, 4);
942 mmc_wait_for_req(card->host, &mrq);
944 result = ntohl(*blocks);
947 if (cmd.error || data.error)
950 *written_blocks = result;
955 static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
956 bool hw_busy_detect, struct request *req, bool *gen_err)
958 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
963 err = __mmc_send_status(card, &status, 5);
965 pr_err("%s: error %d requesting status\n",
966 req->rq_disk->disk_name, err);
970 if (status & R1_ERROR) {
971 pr_err("%s: %s: error sending status cmd, status %#x\n",
972 req->rq_disk->disk_name, __func__, status);
976 /* We may rely on the host hw to handle busy detection.*/
977 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
982 * Timeout if the device never becomes ready for data and never
983 * leaves the program state.
985 if (time_after(jiffies, timeout)) {
986 pr_err("%s: Card stuck in programming state! %s %s\n",
987 mmc_hostname(card->host),
988 req->rq_disk->disk_name, __func__);
993 * Some cards mishandle the status bits,
994 * so make sure to check both the busy
995 * indication and the card state.
997 } while (!(status & R1_READY_FOR_DATA) ||
998 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1003 static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1004 struct request *req, bool *gen_err, u32 *stop_status)
1006 struct mmc_host *host = card->host;
1007 struct mmc_command cmd = {};
1009 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1012 * Normally we use R1B responses for WRITE, but in cases where the host
1013 * has specified a max_busy_timeout we need to validate it. A failure
1014 * means we need to prevent the host from doing hw busy detection, which
1015 * is done by converting to a R1 response instead.
1017 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1018 use_r1b_resp = false;
1020 cmd.opcode = MMC_STOP_TRANSMISSION;
1022 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1023 cmd.busy_timeout = timeout_ms;
1025 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1028 err = mmc_wait_for_cmd(host, &cmd, 5);
1032 *stop_status = cmd.resp[0];
1034 /* No need to check card status in case of READ. */
1035 if (rq_data_dir(req) == READ)
1038 if (!mmc_host_is_spi(host) &&
1039 (*stop_status & R1_ERROR)) {
1040 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1041 req->rq_disk->disk_name, __func__, *stop_status);
1045 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1048 #define ERR_NOMEDIUM 3
1051 #define ERR_CONTINUE 0
1053 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1054 bool status_valid, u32 status)
1058 /* response crc error, retry the r/w cmd */
1059 pr_err("%s: %s sending %s command, card status %#x\n",
1060 req->rq_disk->disk_name, "response CRC error",
1065 pr_err("%s: %s sending %s command, card status %#x\n",
1066 req->rq_disk->disk_name, "timed out", name, status);
1068 /* If the status cmd initially failed, retry the r/w cmd */
1069 if (!status_valid) {
1070 pr_err("%s: status not valid, retrying timeout\n",
1071 req->rq_disk->disk_name);
1076 * If it was a r/w cmd crc error, or illegal command
1077 * (eg, issued in wrong state) then retry - we should
1078 * have corrected the state problem above.
1080 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1081 pr_err("%s: command error, retrying timeout\n",
1082 req->rq_disk->disk_name);
1086 /* Otherwise abort the command */
1090 /* We don't understand the error code the driver gave us */
1091 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1092 req->rq_disk->disk_name, error, status);
1098 * Initial r/w and stop cmd error recovery.
1099 * We don't know whether the card received the r/w cmd or not, so try to
1100 * restore things back to a sane state. Essentially, we do this as follows:
1101 * - Obtain card status. If the first attempt to obtain card status fails,
1102 * the status word will reflect the failed status cmd, not the failed
1103 * r/w cmd. If we fail to obtain card status, it suggests we can no
1104 * longer communicate with the card.
1105 * - Check the card state. If the card received the cmd but there was a
1106 * transient problem with the response, it might still be in a data transfer
1107 * mode. Try to send it a stop command. If this fails, we can't recover.
1108 * - If the r/w cmd failed due to a response CRC error, it was probably
1109 * transient, so retry the cmd.
1110 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1111 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1112 * illegal cmd, retry.
1113 * Otherwise we don't understand what happened, so abort.
1115 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
1116 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
1118 bool prev_cmd_status_valid = true;
1119 u32 status, stop_status = 0;
1122 if (mmc_card_removed(card))
1123 return ERR_NOMEDIUM;
1126 * Try to get card status which indicates both the card state
1127 * and why there was no response. If the first attempt fails,
1128 * we can't be sure the returned status is for the r/w command.
1130 for (retry = 2; retry >= 0; retry--) {
1131 err = __mmc_send_status(card, &status, 0);
1135 /* Re-tune if needed */
1136 mmc_retune_recheck(card->host);
1138 prev_cmd_status_valid = false;
1139 pr_err("%s: error %d sending status command, %sing\n",
1140 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1143 /* We couldn't get a response from the card. Give up. */
1145 /* Check if the card is removed */
1146 if (mmc_detect_card_removed(card->host))
1147 return ERR_NOMEDIUM;
1151 /* Flag ECC errors */
1152 if ((status & R1_CARD_ECC_FAILED) ||
1153 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1154 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1157 /* Flag General errors */
1158 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1159 if ((status & R1_ERROR) ||
1160 (brq->stop.resp[0] & R1_ERROR)) {
1161 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1162 req->rq_disk->disk_name, __func__,
1163 brq->stop.resp[0], status);
1168 * Check the current card state. If it is in some data transfer
1169 * mode, tell it to stop (and hopefully transition back to TRAN.)
1171 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1172 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
1173 err = send_stop(card,
1174 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1175 req, gen_err, &stop_status);
1177 pr_err("%s: error %d sending stop command\n",
1178 req->rq_disk->disk_name, err);
1180 * If the stop cmd also timed out, the card is probably
1181 * not present, so abort. Other errors are bad news too.
1186 if (stop_status & R1_CARD_ECC_FAILED)
1190 /* Check for set block count errors */
1192 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1193 prev_cmd_status_valid, status);
1195 /* Check for r/w command errors */
1197 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1198 prev_cmd_status_valid, status);
1201 if (!brq->stop.error)
1202 return ERR_CONTINUE;
1204 /* Now for stop errors. These aren't fatal to the transfer. */
1205 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
1206 req->rq_disk->disk_name, brq->stop.error,
1207 brq->cmd.resp[0], status);
1210 * Subsitute in our own stop status as this will give the error
1211 * state which happened during the execution of the r/w command.
1214 brq->stop.resp[0] = stop_status;
1215 brq->stop.error = 0;
1217 return ERR_CONTINUE;
1220 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1225 if (md->reset_done & type)
1228 md->reset_done |= type;
1229 err = mmc_hw_reset(host);
1230 /* Ensure we switch back to the correct partition */
1231 if (err != -EOPNOTSUPP) {
1232 struct mmc_blk_data *main_md =
1233 dev_get_drvdata(&host->card->dev);
1236 main_md->part_curr = main_md->part_type;
1237 part_err = mmc_blk_part_switch(host->card, md->part_type);
1240 * We have failed to get back into the correct
1241 * partition, so we need to abort the whole request.
1249 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1251 md->reset_done &= ~type;
1255 * The non-block commands come back from the block layer after it queued it and
1256 * processed it with all other requests and then they get issued in this
1259 static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1261 struct mmc_queue_req *mq_rq;
1262 struct mmc_card *card = mq->card;
1263 struct mmc_blk_data *md = mq->blkdata;
1264 struct mmc_blk_ioc_data **idata;
1271 mq_rq = req_to_mmc_queue_req(req);
1272 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
1274 switch (mq_rq->drv_op) {
1275 case MMC_DRV_OP_IOCTL:
1276 case MMC_DRV_OP_IOCTL_RPMB:
1277 idata = mq_rq->drv_op_data;
1278 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
1279 ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
1283 /* Always switch back to main area after RPMB access */
1285 mmc_blk_part_switch(card, 0);
1287 case MMC_DRV_OP_BOOT_WP:
1288 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1289 card->ext_csd.boot_ro_lock |
1290 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1291 card->ext_csd.part_time);
1293 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1294 md->disk->disk_name, ret);
1296 card->ext_csd.boot_ro_lock |=
1297 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
1299 case MMC_DRV_OP_GET_CARD_STATUS:
1300 ret = mmc_send_status(card, &status);
1304 case MMC_DRV_OP_GET_EXT_CSD:
1305 ext_csd = mq_rq->drv_op_data;
1306 ret = mmc_get_ext_csd(card, ext_csd);
1309 pr_err("%s: unknown driver specific operation\n",
1310 md->disk->disk_name);
1314 mq_rq->drv_op_result = ret;
1315 blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1318 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1320 struct mmc_blk_data *md = mq->blkdata;
1321 struct mmc_card *card = md->queue.card;
1322 unsigned int from, nr, arg;
1323 int err = 0, type = MMC_BLK_DISCARD;
1324 blk_status_t status = BLK_STS_OK;
1326 if (!mmc_can_erase(card)) {
1327 status = BLK_STS_NOTSUPP;
1331 from = blk_rq_pos(req);
1332 nr = blk_rq_sectors(req);
1334 if (mmc_can_discard(card))
1335 arg = MMC_DISCARD_ARG;
1336 else if (mmc_can_trim(card))
1339 arg = MMC_ERASE_ARG;
1342 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1343 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1344 INAND_CMD38_ARG_EXT_CSD,
1345 arg == MMC_TRIM_ARG ?
1346 INAND_CMD38_ARG_TRIM :
1347 INAND_CMD38_ARG_ERASE,
1351 err = mmc_erase(card, from, nr, arg);
1352 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
1354 status = BLK_STS_IOERR;
1356 mmc_blk_reset_success(md, type);
1358 blk_end_request(req, status, blk_rq_bytes(req));
1361 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1362 struct request *req)
1364 struct mmc_blk_data *md = mq->blkdata;
1365 struct mmc_card *card = md->queue.card;
1366 unsigned int from, nr, arg;
1367 int err = 0, type = MMC_BLK_SECDISCARD;
1368 blk_status_t status = BLK_STS_OK;
1370 if (!(mmc_can_secure_erase_trim(card))) {
1371 status = BLK_STS_NOTSUPP;
1375 from = blk_rq_pos(req);
1376 nr = blk_rq_sectors(req);
1378 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1379 arg = MMC_SECURE_TRIM1_ARG;
1381 arg = MMC_SECURE_ERASE_ARG;
1384 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1385 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1386 INAND_CMD38_ARG_EXT_CSD,
1387 arg == MMC_SECURE_TRIM1_ARG ?
1388 INAND_CMD38_ARG_SECTRIM1 :
1389 INAND_CMD38_ARG_SECERASE,
1395 err = mmc_erase(card, from, nr, arg);
1399 status = BLK_STS_IOERR;
1403 if (arg == MMC_SECURE_TRIM1_ARG) {
1404 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1405 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1406 INAND_CMD38_ARG_EXT_CSD,
1407 INAND_CMD38_ARG_SECTRIM2,
1413 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1417 status = BLK_STS_IOERR;
1423 if (err && !mmc_blk_reset(md, card->host, type))
1426 mmc_blk_reset_success(md, type);
1428 blk_end_request(req, status, blk_rq_bytes(req));
1431 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1433 struct mmc_blk_data *md = mq->blkdata;
1434 struct mmc_card *card = md->queue.card;
1437 ret = mmc_flush_cache(card);
1438 blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1442 * Reformat current write as a reliable write, supporting
1443 * both legacy and the enhanced reliable write MMC cards.
1444 * In each transfer we'll handle only as much as a single
1445 * reliable write can handle, thus finish the request in
1446 * partial completions.
1448 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1449 struct mmc_card *card,
1450 struct request *req)
1452 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1453 /* Legacy mode imposes restrictions on transfers. */
1454 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
1455 brq->data.blocks = 1;
1457 if (brq->data.blocks > card->ext_csd.rel_sectors)
1458 brq->data.blocks = card->ext_csd.rel_sectors;
1459 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1460 brq->data.blocks = 1;
1464 #define CMD_ERRORS \
1465 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1466 R1_ADDRESS_ERROR | /* Misaligned address */ \
1467 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1468 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1469 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
1470 R1_CC_ERROR | /* Card controller error */ \
1471 R1_ERROR) /* General/unknown error */
1473 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
1478 * Per the SD specification(physical layer version 4.10)[1],
1479 * section 4.3.3, it explicitly states that "When the last
1480 * block of user area is read using CMD18, the host should
1481 * ignore OUT_OF_RANGE error that may occur even the sequence
1482 * is correct". And JESD84-B51 for eMMC also has a similar
1483 * statement on section 6.8.3.
1485 * Multiple block read/write could be done by either predefined
1486 * method, namely CMD23, or open-ending mode. For open-ending mode,
1487 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1489 * However the spec[1] doesn't tell us whether we should also
1490 * ignore that for predefined method. But per the spec[1], section
1491 * 4.15 Set Block Count Command, it says"If illegal block count
1492 * is set, out of range error will be indicated during read/write
1493 * operation (For example, data transfer is stopped at user area
1494 * boundary)." In another word, we could expect a out of range error
1495 * in the response for the following CMD18/25. And if argument of
1496 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1497 * we could also expect to get a -ETIMEDOUT or any error number from
1498 * the host drivers due to missing data response(for write)/data(for
1499 * read), as the cards will stop the data transfer by itself per the
1500 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1503 if (!brq->stop.error) {
1504 bool oor_with_open_end;
1505 /* If there is no error yet, check R1 response */
1507 val = brq->stop.resp[0] & CMD_ERRORS;
1508 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1510 if (val && !oor_with_open_end)
1511 brq->stop.error = -EIO;
1515 static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1516 struct mmc_async_req *areq)
1518 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1520 struct mmc_blk_request *brq = &mq_mrq->brq;
1521 struct request *req = mmc_queue_req_to_req(mq_mrq);
1522 int need_retune = card->host->need_retune;
1523 bool ecc_err = false;
1524 bool gen_err = false;
1527 * sbc.error indicates a problem with the set block count
1528 * command. No data will have been transferred.
1530 * cmd.error indicates a problem with the r/w command. No
1531 * data will have been transferred.
1533 * stop.error indicates a problem with the stop command. Data
1534 * may have been transferred, or may still be transferring.
1537 mmc_blk_eval_resp_error(brq);
1539 if (brq->sbc.error || brq->cmd.error ||
1540 brq->stop.error || brq->data.error) {
1541 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
1543 return MMC_BLK_RETRY;
1545 return MMC_BLK_ABORT;
1547 return MMC_BLK_NOMEDIUM;
1554 * Check for errors relating to the execution of the
1555 * initial command - such as address errors. No data
1556 * has been transferred.
1558 if (brq->cmd.resp[0] & CMD_ERRORS) {
1559 pr_err("%s: r/w command failed, status = %#x\n",
1560 req->rq_disk->disk_name, brq->cmd.resp[0]);
1561 return MMC_BLK_ABORT;
1565 * Everything else is either success, or a data error of some
1566 * kind. If it was a write, we may have transitioned to
1567 * program mode, which we have to wait for it to complete.
1569 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1572 /* Check stop command response */
1573 if (brq->stop.resp[0] & R1_ERROR) {
1574 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1575 req->rq_disk->disk_name, __func__,
1580 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1583 return MMC_BLK_CMD_ERR;
1586 /* if general error occurs, retry the write operation. */
1588 pr_warn("%s: retrying write for general error\n",
1589 req->rq_disk->disk_name);
1590 return MMC_BLK_RETRY;
1593 /* Some errors (ECC) are flagged on the next commmand, so check stop, too */
1594 if (brq->data.error || brq->stop.error) {
1595 if (need_retune && !brq->retune_retry_done) {
1596 pr_debug("%s: retrying because a re-tune was needed\n",
1597 req->rq_disk->disk_name);
1598 brq->retune_retry_done = 1;
1599 return MMC_BLK_RETRY;
1601 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1602 req->rq_disk->disk_name, brq->data.error ?: brq->stop.error,
1603 (unsigned)blk_rq_pos(req),
1604 (unsigned)blk_rq_sectors(req),
1605 brq->cmd.resp[0], brq->stop.resp[0]);
1607 if (rq_data_dir(req) == READ) {
1609 return MMC_BLK_ECC_ERR;
1610 return MMC_BLK_DATA_ERR;
1612 return MMC_BLK_CMD_ERR;
1616 if (!brq->data.bytes_xfered)
1617 return MMC_BLK_RETRY;
1619 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1620 return MMC_BLK_PARTIAL;
1622 return MMC_BLK_SUCCESS;
1625 static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
1626 int disable_multi, bool *do_rel_wr,
1629 struct mmc_blk_data *md = mq->blkdata;
1630 struct mmc_card *card = md->queue.card;
1631 struct mmc_blk_request *brq = &mqrq->brq;
1632 struct request *req = mmc_queue_req_to_req(mqrq);
1635 * Reliable writes are used to implement Forced Unit Access and
1636 * are supported only on MMCs.
1638 *do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1639 rq_data_dir(req) == WRITE &&
1640 (md->flags & MMC_BLK_REL_WR);
1642 memset(brq, 0, sizeof(struct mmc_blk_request));
1644 brq->mrq.data = &brq->data;
1646 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1649 if (rq_data_dir(req) == READ) {
1650 brq->data.flags = MMC_DATA_READ;
1651 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1653 brq->data.flags = MMC_DATA_WRITE;
1654 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1657 brq->data.blksz = 512;
1658 brq->data.blocks = blk_rq_sectors(req);
1661 * The block layer doesn't support all sector count
1662 * restrictions, so we need to be prepared for too big
1665 if (brq->data.blocks > card->host->max_blk_count)
1666 brq->data.blocks = card->host->max_blk_count;
1668 if (brq->data.blocks > 1) {
1670 * Some SD cards in SPI mode return a CRC error or even lock up
1671 * completely when trying to read the last block using a
1672 * multiblock read command.
1674 if (mmc_host_is_spi(card->host) && (rq_data_dir(req) == READ) &&
1675 (blk_rq_pos(req) + blk_rq_sectors(req) ==
1676 get_capacity(md->disk)))
1680 * After a read error, we redo the request one sector
1681 * at a time in order to accurately determine which
1682 * sectors can be read successfully.
1685 brq->data.blocks = 1;
1688 * Some controllers have HW issues while operating
1689 * in multiple I/O mode
1691 if (card->host->ops->multi_io_quirk)
1692 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1693 (rq_data_dir(req) == READ) ?
1694 MMC_DATA_READ : MMC_DATA_WRITE,
1699 mmc_apply_rel_rw(brq, card, req);
1702 * Data tag is used only during writing meta data to speed
1703 * up write and any subsequent read of this meta data
1705 *do_data_tag = card->ext_csd.data_tag_unit_size &&
1706 (req->cmd_flags & REQ_META) &&
1707 (rq_data_dir(req) == WRITE) &&
1708 ((brq->data.blocks * brq->data.blksz) >=
1709 card->ext_csd.data_tag_unit_size);
1711 mmc_set_data_timeout(&brq->data, card);
1713 brq->data.sg = mqrq->sg;
1714 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1717 * Adjust the sg list so it is the same size as the
1720 if (brq->data.blocks != blk_rq_sectors(req)) {
1721 int i, data_size = brq->data.blocks << 9;
1722 struct scatterlist *sg;
1724 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1725 data_size -= sg->length;
1726 if (data_size <= 0) {
1727 sg->length += data_size;
1732 brq->data.sg_len = i;
1735 mqrq->areq.mrq = &brq->mrq;
1738 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1739 struct mmc_card *card,
1741 struct mmc_queue *mq)
1743 u32 readcmd, writecmd;
1744 struct mmc_blk_request *brq = &mqrq->brq;
1745 struct request *req = mmc_queue_req_to_req(mqrq);
1746 struct mmc_blk_data *md = mq->blkdata;
1747 bool do_rel_wr, do_data_tag;
1749 mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1751 brq->mrq.cmd = &brq->cmd;
1753 brq->cmd.arg = blk_rq_pos(req);
1754 if (!mmc_card_blockaddr(card))
1756 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1758 if (brq->data.blocks > 1 || do_rel_wr) {
1759 /* SPI multiblock writes terminate using a special
1760 * token, not a STOP_TRANSMISSION request.
1762 if (!mmc_host_is_spi(card->host) ||
1763 rq_data_dir(req) == READ)
1764 brq->mrq.stop = &brq->stop;
1765 readcmd = MMC_READ_MULTIPLE_BLOCK;
1766 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1768 brq->mrq.stop = NULL;
1769 readcmd = MMC_READ_SINGLE_BLOCK;
1770 writecmd = MMC_WRITE_BLOCK;
1772 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
1775 * Pre-defined multi-block transfers are preferable to
1776 * open ended-ones (and necessary for reliable writes).
1777 * However, it is not sufficient to just send CMD23,
1778 * and avoid the final CMD12, as on an error condition
1779 * CMD12 (stop) needs to be sent anyway. This, coupled
1780 * with Auto-CMD23 enhancements provided by some
1781 * hosts, means that the complexity of dealing
1782 * with this is best left to the host. If CMD23 is
1783 * supported by card and host, we'll fill sbc in and let
1784 * the host deal with handling it correctly. This means
1785 * that for hosts that don't expose MMC_CAP_CMD23, no
1786 * change of behavior will be observed.
1788 * N.B: Some MMC cards experience perf degradation.
1789 * We'll avoid using CMD23-bounded multiblock writes for
1790 * these, while retaining features like reliable writes.
1792 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1793 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1795 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1796 brq->sbc.arg = brq->data.blocks |
1797 (do_rel_wr ? (1 << 31) : 0) |
1798 (do_data_tag ? (1 << 29) : 0);
1799 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1800 brq->mrq.sbc = &brq->sbc;
1803 mqrq->areq.err_check = mmc_blk_err_check;
1806 static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1807 struct mmc_blk_request *brq, struct request *req,
1808 bool old_req_pending)
1813 * If this is an SD card and we're writing, we can first
1814 * mark the known good sectors as ok.
1816 * If the card is not SD, we can still ok written sectors
1817 * as reported by the controller (which might be less than
1818 * the real number of written sectors, but never more).
1820 if (mmc_card_sd(card)) {
1824 err = mmc_sd_num_wr_blocks(card, &blocks);
1826 req_pending = old_req_pending;
1828 req_pending = blk_end_request(req, BLK_STS_OK, blocks << 9);
1830 req_pending = blk_end_request(req, BLK_STS_OK, brq->data.bytes_xfered);
1835 static void mmc_blk_rw_cmd_abort(struct mmc_queue *mq, struct mmc_card *card,
1836 struct request *req,
1837 struct mmc_queue_req *mqrq)
1839 if (mmc_card_removed(card))
1840 req->rq_flags |= RQF_QUIET;
1841 while (blk_end_request(req, BLK_STS_IOERR, blk_rq_cur_bytes(req)));
1846 * mmc_blk_rw_try_restart() - tries to restart the current async request
1847 * @mq: the queue with the card and host to restart
1848 * @req: a new request that want to be started after the current one
1850 static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req,
1851 struct mmc_queue_req *mqrq)
1857 * If the card was removed, just cancel everything and return.
1859 if (mmc_card_removed(mq->card)) {
1860 req->rq_flags |= RQF_QUIET;
1861 blk_end_request_all(req, BLK_STS_IOERR);
1862 mq->qcnt--; /* FIXME: just set to 0? */
1865 /* Else proceed and try to restart the current async request */
1866 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
1867 mmc_start_areq(mq->card->host, &mqrq->areq, NULL);
1870 static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req)
1872 struct mmc_blk_data *md = mq->blkdata;
1873 struct mmc_card *card = md->queue.card;
1874 struct mmc_blk_request *brq;
1875 int disable_multi = 0, retry = 0, type, retune_retry_done = 0;
1876 enum mmc_blk_status status;
1877 struct mmc_queue_req *mqrq_cur = NULL;
1878 struct mmc_queue_req *mq_rq;
1879 struct request *old_req;
1880 struct mmc_async_req *new_areq;
1881 struct mmc_async_req *old_areq;
1882 bool req_pending = true;
1885 mqrq_cur = req_to_mmc_queue_req(new_req);
1895 * When 4KB native sector is enabled, only 8 blocks
1896 * multiple read or write is allowed
1898 if (mmc_large_sector(card) &&
1899 !IS_ALIGNED(blk_rq_sectors(new_req), 8)) {
1900 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1901 new_req->rq_disk->disk_name);
1902 mmc_blk_rw_cmd_abort(mq, card, new_req, mqrq_cur);
1906 mmc_blk_rw_rq_prep(mqrq_cur, card, 0, mq);
1907 new_areq = &mqrq_cur->areq;
1911 old_areq = mmc_start_areq(card->host, new_areq, &status);
1914 * We have just put the first request into the pipeline
1915 * and there is nothing more to do until it is
1922 * An asynchronous request has been completed and we proceed
1923 * to handle the result of it.
1925 mq_rq = container_of(old_areq, struct mmc_queue_req, areq);
1927 old_req = mmc_queue_req_to_req(mq_rq);
1928 type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1931 case MMC_BLK_SUCCESS:
1932 case MMC_BLK_PARTIAL:
1934 * A block was successfully transferred.
1936 mmc_blk_reset_success(md, type);
1938 req_pending = blk_end_request(old_req, BLK_STS_OK,
1939 brq->data.bytes_xfered);
1941 * If the blk_end_request function returns non-zero even
1942 * though all data has been transferred and no errors
1943 * were returned by the host controller, it's a bug.
1945 if (status == MMC_BLK_SUCCESS && req_pending) {
1946 pr_err("%s BUG rq_tot %d d_xfer %d\n",
1947 __func__, blk_rq_bytes(old_req),
1948 brq->data.bytes_xfered);
1949 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1953 case MMC_BLK_CMD_ERR:
1954 req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending);
1955 if (mmc_blk_reset(md, card->host, type)) {
1957 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1960 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
1965 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
1970 retune_retry_done = brq->retune_retry_done;
1975 if (!mmc_blk_reset(md, card->host, type))
1977 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1978 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
1980 case MMC_BLK_DATA_ERR: {
1983 err = mmc_blk_reset(md, card->host, type);
1986 if (err == -ENODEV) {
1987 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1988 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
1993 case MMC_BLK_ECC_ERR:
1994 if (brq->data.blocks > 1) {
1995 /* Redo read one sector at a time */
1996 pr_warn("%s: retrying using single block read\n",
1997 old_req->rq_disk->disk_name);
2002 * After an error, we redo I/O one sector at a
2003 * time, so we only reach here after trying to
2004 * read a single sector.
2006 req_pending = blk_end_request(old_req, BLK_STS_IOERR,
2010 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
2014 case MMC_BLK_NOMEDIUM:
2015 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
2016 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
2019 pr_err("%s: Unhandled return value (%d)",
2020 old_req->rq_disk->disk_name, status);
2021 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
2022 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
2028 * In case of a incomplete request
2029 * prepare it again and resend.
2031 mmc_blk_rw_rq_prep(mq_rq, card,
2033 mmc_start_areq(card->host,
2034 &mq_rq->areq, NULL);
2035 mq_rq->brq.retune_retry_done = retune_retry_done;
2037 } while (req_pending);
2042 void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2045 struct mmc_blk_data *md = mq->blkdata;
2046 struct mmc_card *card = md->queue.card;
2048 if (req && !mq->qcnt)
2049 /* claim host only for the first request */
2052 ret = mmc_blk_part_switch(card, md->part_type);
2055 blk_end_request_all(req, BLK_STS_IOERR);
2061 switch (req_op(req)) {
2063 case REQ_OP_DRV_OUT:
2065 * Complete ongoing async transfer before issuing
2069 mmc_blk_issue_rw_rq(mq, NULL);
2070 mmc_blk_issue_drv_op(mq, req);
2072 case REQ_OP_DISCARD:
2074 * Complete ongoing async transfer before issuing
2078 mmc_blk_issue_rw_rq(mq, NULL);
2079 mmc_blk_issue_discard_rq(mq, req);
2081 case REQ_OP_SECURE_ERASE:
2083 * Complete ongoing async transfer before issuing
2087 mmc_blk_issue_rw_rq(mq, NULL);
2088 mmc_blk_issue_secdiscard_rq(mq, req);
2092 * Complete ongoing async transfer before issuing
2096 mmc_blk_issue_rw_rq(mq, NULL);
2097 mmc_blk_issue_flush(mq, req);
2100 /* Normal request, just issue it */
2101 mmc_blk_issue_rw_rq(mq, req);
2102 card->host->context_info.is_waiting_last_req = false;
2106 /* No request, flushing the pipeline with NULL */
2107 mmc_blk_issue_rw_rq(mq, NULL);
2108 card->host->context_info.is_waiting_last_req = false;
2116 static inline int mmc_blk_readonly(struct mmc_card *card)
2118 return mmc_card_readonly(card) ||
2119 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2122 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2123 struct device *parent,
2126 const char *subname,
2129 struct mmc_blk_data *md;
2132 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
2135 * We get -ENOSPC because there are no more any available
2136 * devidx. The reason may be that, either userspace haven't yet
2137 * unmounted the partitions, which postpones mmc_blk_release()
2138 * from being called, or the device has more partitions than
2141 if (devidx == -ENOSPC)
2142 dev_err(mmc_dev(card->host),
2143 "no more device IDs available\n");
2145 return ERR_PTR(devidx);
2148 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2154 md->area_type = area_type;
2157 * Set the read-only status based on the supported commands
2158 * and the write protect switch.
2160 md->read_only = mmc_blk_readonly(card);
2162 md->disk = alloc_disk(perdev_minors);
2163 if (md->disk == NULL) {
2168 spin_lock_init(&md->lock);
2169 INIT_LIST_HEAD(&md->part);
2170 INIT_LIST_HEAD(&md->rpmbs);
2173 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2177 md->queue.blkdata = md;
2179 md->disk->major = MMC_BLOCK_MAJOR;
2180 md->disk->first_minor = devidx * perdev_minors;
2181 md->disk->fops = &mmc_bdops;
2182 md->disk->private_data = md;
2183 md->disk->queue = md->queue.queue;
2184 md->parent = parent;
2185 set_disk_ro(md->disk, md->read_only || default_ro);
2186 md->disk->flags = GENHD_FL_EXT_DEVT;
2187 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2188 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
2191 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2193 * - be set for removable media with permanent block devices
2194 * - be unset for removable block devices with permanent media
2196 * Since MMC block devices clearly fall under the second
2197 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2198 * should use the block device creation/destruction hotplug
2199 * messages to tell when the card is present.
2202 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2203 "mmcblk%u%s", card->host->index, subname ? subname : "");
2205 if (mmc_card_mmc(card))
2206 blk_queue_logical_block_size(md->queue.queue,
2207 card->ext_csd.data_sector_size);
2209 blk_queue_logical_block_size(md->queue.queue, 512);
2211 set_capacity(md->disk, size);
2213 if (mmc_host_cmd23(card->host)) {
2214 if ((mmc_card_mmc(card) &&
2215 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
2216 (mmc_card_sd(card) &&
2217 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2218 md->flags |= MMC_BLK_CMD23;
2221 if (mmc_card_mmc(card) &&
2222 md->flags & MMC_BLK_CMD23 &&
2223 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2224 card->ext_csd.rel_sectors)) {
2225 md->flags |= MMC_BLK_REL_WR;
2226 blk_queue_write_cache(md->queue.queue, true, true);
2236 ida_simple_remove(&mmc_blk_ida, devidx);
2237 return ERR_PTR(ret);
2240 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2244 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2246 * The EXT_CSD sector count is in number or 512 byte
2249 size = card->ext_csd.sectors;
2252 * The CSD capacity field is in units of read_blkbits.
2253 * set_capacity takes units of 512 bytes.
2255 size = (typeof(sector_t))card->csd.capacity
2256 << (card->csd.read_blkbits - 9);
2259 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2260 MMC_BLK_DATA_AREA_MAIN);
2263 static int mmc_blk_alloc_part(struct mmc_card *card,
2264 struct mmc_blk_data *md,
2265 unsigned int part_type,
2268 const char *subname,
2272 struct mmc_blk_data *part_md;
2274 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2275 subname, area_type);
2276 if (IS_ERR(part_md))
2277 return PTR_ERR(part_md);
2278 part_md->part_type = part_type;
2279 list_add(&part_md->part, &md->part);
2281 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
2282 cap_str, sizeof(cap_str));
2283 pr_info("%s: %s %s partition %u %s\n",
2284 part_md->disk->disk_name, mmc_card_id(card),
2285 mmc_card_name(card), part_md->part_type, cap_str);
2290 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2291 * @filp: the character device file
2292 * @cmd: the ioctl() command
2293 * @arg: the argument from userspace
2295 * This will essentially just redirect the ioctl()s coming in over to
2296 * the main block device spawning the RPMB character device.
2298 static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
2301 struct mmc_rpmb_data *rpmb = filp->private_data;
2306 ret = mmc_blk_ioctl_cmd(rpmb->md,
2307 (struct mmc_ioc_cmd __user *)arg,
2310 case MMC_IOC_MULTI_CMD:
2311 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
2312 (struct mmc_ioc_multi_cmd __user *)arg,
2323 #ifdef CONFIG_COMPAT
2324 static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
2327 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
2331 static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
2333 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2334 struct mmc_rpmb_data, chrdev);
2336 get_device(&rpmb->dev);
2337 filp->private_data = rpmb;
2338 mmc_blk_get(rpmb->md->disk);
2340 return nonseekable_open(inode, filp);
2343 static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
2345 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2346 struct mmc_rpmb_data, chrdev);
2348 mmc_blk_put(rpmb->md);
2349 put_device(&rpmb->dev);
2354 static const struct file_operations mmc_rpmb_fileops = {
2355 .release = mmc_rpmb_chrdev_release,
2356 .open = mmc_rpmb_chrdev_open,
2357 .owner = THIS_MODULE,
2358 .llseek = no_llseek,
2359 .unlocked_ioctl = mmc_rpmb_ioctl,
2360 #ifdef CONFIG_COMPAT
2361 .compat_ioctl = mmc_rpmb_ioctl_compat,
2365 static void mmc_blk_rpmb_device_release(struct device *dev)
2367 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2369 ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2373 static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
2374 struct mmc_blk_data *md,
2375 unsigned int part_index,
2377 const char *subname)
2380 char rpmb_name[DISK_NAME_LEN];
2382 struct mmc_rpmb_data *rpmb;
2384 /* This creates the minor number for the RPMB char device */
2385 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
2389 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
2391 ida_simple_remove(&mmc_rpmb_ida, devidx);
2395 snprintf(rpmb_name, sizeof(rpmb_name),
2396 "mmcblk%u%s", card->host->index, subname ? subname : "");
2399 rpmb->part_index = part_index;
2400 rpmb->dev.init_name = rpmb_name;
2401 rpmb->dev.bus = &mmc_rpmb_bus_type;
2402 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
2403 rpmb->dev.parent = &card->dev;
2404 rpmb->dev.release = mmc_blk_rpmb_device_release;
2405 device_initialize(&rpmb->dev);
2406 dev_set_drvdata(&rpmb->dev, rpmb);
2409 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
2410 rpmb->chrdev.owner = THIS_MODULE;
2411 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
2413 pr_err("%s: could not add character device\n", rpmb_name);
2414 goto out_put_device;
2417 list_add(&rpmb->node, &md->rpmbs);
2419 string_get_size((u64)size, 512, STRING_UNITS_2,
2420 cap_str, sizeof(cap_str));
2422 pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n",
2423 rpmb_name, mmc_card_id(card),
2424 mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str,
2425 MAJOR(mmc_rpmb_devt), rpmb->id);
2430 put_device(&rpmb->dev);
2434 static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
2437 cdev_device_del(&rpmb->chrdev, &rpmb->dev);
2438 put_device(&rpmb->dev);
2441 /* MMC Physical partitions consist of two boot partitions and
2442 * up to four general purpose partitions.
2443 * For each partition enabled in EXT_CSD a block device will be allocatedi
2444 * to provide access to the partition.
2447 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2451 if (!mmc_card_mmc(card))
2454 for (idx = 0; idx < card->nr_parts; idx++) {
2455 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
2457 * RPMB partitions does not provide block access, they
2458 * are only accessed using ioctl():s. Thus create
2459 * special RPMB block devices that do not have a
2460 * backing block queue for these.
2462 ret = mmc_blk_alloc_rpmb_part(card, md,
2463 card->part[idx].part_cfg,
2464 card->part[idx].size >> 9,
2465 card->part[idx].name);
2468 } else if (card->part[idx].size) {
2469 ret = mmc_blk_alloc_part(card, md,
2470 card->part[idx].part_cfg,
2471 card->part[idx].size >> 9,
2472 card->part[idx].force_ro,
2473 card->part[idx].name,
2474 card->part[idx].area_type);
2483 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2485 struct mmc_card *card;
2489 * Flush remaining requests and free queues. It
2490 * is freeing the queue that stops new requests
2491 * from being accepted.
2493 card = md->queue.card;
2494 spin_lock_irq(md->queue.queue->queue_lock);
2495 queue_flag_set(QUEUE_FLAG_BYPASS, md->queue.queue);
2496 spin_unlock_irq(md->queue.queue->queue_lock);
2497 blk_set_queue_dying(md->queue.queue);
2498 mmc_cleanup_queue(&md->queue);
2499 if (md->disk->flags & GENHD_FL_UP) {
2500 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2501 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2502 card->ext_csd.boot_ro_lockable)
2503 device_remove_file(disk_to_dev(md->disk),
2504 &md->power_ro_lock);
2506 del_gendisk(md->disk);
2512 static void mmc_blk_remove_parts(struct mmc_card *card,
2513 struct mmc_blk_data *md)
2515 struct list_head *pos, *q;
2516 struct mmc_blk_data *part_md;
2517 struct mmc_rpmb_data *rpmb;
2519 /* Remove RPMB partitions */
2520 list_for_each_safe(pos, q, &md->rpmbs) {
2521 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
2523 mmc_blk_remove_rpmb_part(rpmb);
2525 /* Remove block partitions */
2526 list_for_each_safe(pos, q, &md->part) {
2527 part_md = list_entry(pos, struct mmc_blk_data, part);
2529 mmc_blk_remove_req(part_md);
2533 static int mmc_add_disk(struct mmc_blk_data *md)
2536 struct mmc_card *card = md->queue.card;
2538 device_add_disk(md->parent, md->disk);
2539 md->force_ro.show = force_ro_show;
2540 md->force_ro.store = force_ro_store;
2541 sysfs_attr_init(&md->force_ro.attr);
2542 md->force_ro.attr.name = "force_ro";
2543 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2544 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2548 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2549 card->ext_csd.boot_ro_lockable) {
2552 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2555 mode = S_IRUGO | S_IWUSR;
2557 md->power_ro_lock.show = power_ro_lock_show;
2558 md->power_ro_lock.store = power_ro_lock_store;
2559 sysfs_attr_init(&md->power_ro_lock.attr);
2560 md->power_ro_lock.attr.mode = mode;
2561 md->power_ro_lock.attr.name =
2562 "ro_lock_until_next_power_on";
2563 ret = device_create_file(disk_to_dev(md->disk),
2564 &md->power_ro_lock);
2566 goto power_ro_lock_fail;
2571 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2573 del_gendisk(md->disk);
2578 #ifdef CONFIG_DEBUG_FS
2580 static int mmc_dbg_card_status_get(void *data, u64 *val)
2582 struct mmc_card *card = data;
2583 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2584 struct mmc_queue *mq = &md->queue;
2585 struct request *req;
2588 /* Ask the block layer about the card status */
2589 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
2591 return PTR_ERR(req);
2592 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2593 blk_execute_rq(mq->queue, NULL, req, 0);
2594 ret = req_to_mmc_queue_req(req)->drv_op_result;
2599 blk_put_request(req);
2603 DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2606 /* That is two digits * 512 + 1 for newline */
2607 #define EXT_CSD_STR_LEN 1025
2609 static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2611 struct mmc_card *card = inode->i_private;
2612 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2613 struct mmc_queue *mq = &md->queue;
2614 struct request *req;
2620 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2624 /* Ask the block layer for the EXT CSD */
2625 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
2630 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2631 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2632 blk_execute_rq(mq->queue, NULL, req, 0);
2633 err = req_to_mmc_queue_req(req)->drv_op_result;
2634 blk_put_request(req);
2636 pr_err("FAILED %d\n", err);
2640 for (i = 0; i < 512; i++)
2641 n += sprintf(buf + n, "%02x", ext_csd[i]);
2642 n += sprintf(buf + n, "\n");
2644 if (n != EXT_CSD_STR_LEN) {
2650 filp->private_data = buf;
2659 static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2660 size_t cnt, loff_t *ppos)
2662 char *buf = filp->private_data;
2664 return simple_read_from_buffer(ubuf, cnt, ppos,
2665 buf, EXT_CSD_STR_LEN);
2668 static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2670 kfree(file->private_data);
2674 static const struct file_operations mmc_dbg_ext_csd_fops = {
2675 .open = mmc_ext_csd_open,
2676 .read = mmc_ext_csd_read,
2677 .release = mmc_ext_csd_release,
2678 .llseek = default_llseek,
2681 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2683 struct dentry *root;
2685 if (!card->debugfs_root)
2688 root = card->debugfs_root;
2690 if (mmc_card_mmc(card) || mmc_card_sd(card)) {
2692 debugfs_create_file("status", S_IRUSR, root, card,
2693 &mmc_dbg_card_status_fops);
2694 if (!md->status_dentry)
2698 if (mmc_card_mmc(card)) {
2699 md->ext_csd_dentry =
2700 debugfs_create_file("ext_csd", S_IRUSR, root, card,
2701 &mmc_dbg_ext_csd_fops);
2702 if (!md->ext_csd_dentry)
2709 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2710 struct mmc_blk_data *md)
2712 if (!card->debugfs_root)
2715 if (!IS_ERR_OR_NULL(md->status_dentry)) {
2716 debugfs_remove(md->status_dentry);
2717 md->status_dentry = NULL;
2720 if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) {
2721 debugfs_remove(md->ext_csd_dentry);
2722 md->ext_csd_dentry = NULL;
2728 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2733 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2734 struct mmc_blk_data *md)
2738 #endif /* CONFIG_DEBUG_FS */
2740 static int mmc_blk_probe(struct mmc_card *card)
2742 struct mmc_blk_data *md, *part_md;
2746 * Check that the card supports the command class(es) we need.
2748 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2751 mmc_fixup_device(card, mmc_blk_fixups);
2753 md = mmc_blk_alloc(card);
2757 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
2758 cap_str, sizeof(cap_str));
2759 pr_info("%s: %s %s %s %s\n",
2760 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2761 cap_str, md->read_only ? "(ro)" : "");
2763 if (mmc_blk_alloc_parts(card, md))
2766 dev_set_drvdata(&card->dev, md);
2768 if (mmc_add_disk(md))
2771 list_for_each_entry(part_md, &md->part, part) {
2772 if (mmc_add_disk(part_md))
2776 /* Add two debugfs entries */
2777 mmc_blk_add_debugfs(card, md);
2779 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2780 pm_runtime_use_autosuspend(&card->dev);
2783 * Don't enable runtime PM for SD-combo cards here. Leave that
2784 * decision to be taken during the SDIO init sequence instead.
2786 if (card->type != MMC_TYPE_SD_COMBO) {
2787 pm_runtime_set_active(&card->dev);
2788 pm_runtime_enable(&card->dev);
2794 mmc_blk_remove_parts(card, md);
2795 mmc_blk_remove_req(md);
2799 static void mmc_blk_remove(struct mmc_card *card)
2801 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2803 mmc_blk_remove_debugfs(card, md);
2804 mmc_blk_remove_parts(card, md);
2805 pm_runtime_get_sync(&card->dev);
2806 mmc_claim_host(card->host);
2807 mmc_blk_part_switch(card, md->part_type);
2808 mmc_release_host(card->host);
2809 if (card->type != MMC_TYPE_SD_COMBO)
2810 pm_runtime_disable(&card->dev);
2811 pm_runtime_put_noidle(&card->dev);
2812 mmc_blk_remove_req(md);
2813 dev_set_drvdata(&card->dev, NULL);
2816 static int _mmc_blk_suspend(struct mmc_card *card)
2818 struct mmc_blk_data *part_md;
2819 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2822 mmc_queue_suspend(&md->queue);
2823 list_for_each_entry(part_md, &md->part, part) {
2824 mmc_queue_suspend(&part_md->queue);
2830 static void mmc_blk_shutdown(struct mmc_card *card)
2832 _mmc_blk_suspend(card);
2835 #ifdef CONFIG_PM_SLEEP
2836 static int mmc_blk_suspend(struct device *dev)
2838 struct mmc_card *card = mmc_dev_to_card(dev);
2840 return _mmc_blk_suspend(card);
2843 static int mmc_blk_resume(struct device *dev)
2845 struct mmc_blk_data *part_md;
2846 struct mmc_blk_data *md = dev_get_drvdata(dev);
2850 * Resume involves the card going into idle state,
2851 * so current partition is always the main one.
2853 md->part_curr = md->part_type;
2854 mmc_queue_resume(&md->queue);
2855 list_for_each_entry(part_md, &md->part, part) {
2856 mmc_queue_resume(&part_md->queue);
2863 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2865 static struct mmc_driver mmc_driver = {
2868 .pm = &mmc_blk_pm_ops,
2870 .probe = mmc_blk_probe,
2871 .remove = mmc_blk_remove,
2872 .shutdown = mmc_blk_shutdown,
2875 static int __init mmc_blk_init(void)
2879 res = bus_register(&mmc_rpmb_bus_type);
2881 pr_err("mmcblk: could not register RPMB bus type\n");
2884 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
2886 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
2890 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2891 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2893 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
2895 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2897 goto out_chrdev_unreg;
2899 res = mmc_register_driver(&mmc_driver);
2901 goto out_blkdev_unreg;
2906 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2908 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
2910 bus_unregister(&mmc_rpmb_bus_type);
2914 static void __exit mmc_blk_exit(void)
2916 mmc_unregister_driver(&mmc_driver);
2917 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2918 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
2919 bus_unregister(&mmc_rpmb_bus_type);
2922 module_init(mmc_blk_init);
2923 module_exit(mmc_blk_exit);
2925 MODULE_LICENSE("GPL");
2926 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");