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/mutex.h>
32 #include <linux/scatterlist.h>
33 #include <linux/string_helpers.h>
34 #include <linux/delay.h>
35 #include <linux/capability.h>
36 #include <linux/compat.h>
37 #include <linux/pm_runtime.h>
40 #include <linux/mmc/ioctl.h>
41 #include <linux/mmc/card.h>
42 #include <linux/mmc/host.h>
43 #include <linux/mmc/mmc.h>
44 #include <linux/mmc/sd.h>
46 #include <asm/uaccess.h>
50 MODULE_ALIAS("mmc:block");
51 #ifdef MODULE_PARAM_PREFIX
52 #undef MODULE_PARAM_PREFIX
54 #define MODULE_PARAM_PREFIX "mmcblk."
56 #define INAND_CMD38_ARG_EXT_CSD 113
57 #define INAND_CMD38_ARG_ERASE 0x00
58 #define INAND_CMD38_ARG_TRIM 0x01
59 #define INAND_CMD38_ARG_SECERASE 0x80
60 #define INAND_CMD38_ARG_SECTRIM1 0x81
61 #define INAND_CMD38_ARG_SECTRIM2 0x88
62 #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
63 #define MMC_SANITIZE_REQ_TIMEOUT 240000
64 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
66 #define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
67 (req->cmd_flags & REQ_META)) && \
68 (rq_data_dir(req) == WRITE))
69 #define PACKED_CMD_VER 0x01
70 #define PACKED_CMD_WR 0x02
72 static DEFINE_MUTEX(block_mutex);
75 * The defaults come from config options but can be overriden by module
78 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
81 * We've only got one major, so number of mmcblk devices is
82 * limited to 256 / number of minors per device.
84 static int max_devices;
86 /* 256 minors, so at most 256 separate devices */
87 static DECLARE_BITMAP(dev_use, 256);
88 static DECLARE_BITMAP(name_use, 256);
91 * There is one mmc_blk_data per slot.
96 struct mmc_queue queue;
97 struct list_head part;
100 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
101 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
102 #define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
105 unsigned int read_only;
106 unsigned int part_type;
107 unsigned int name_idx;
108 unsigned int reset_done;
109 #define MMC_BLK_READ BIT(0)
110 #define MMC_BLK_WRITE BIT(1)
111 #define MMC_BLK_DISCARD BIT(2)
112 #define MMC_BLK_SECDISCARD BIT(3)
115 * Only set in main mmc_blk_data associated
116 * with mmc_card with mmc_set_drvdata, and keeps
117 * track of the current selected device partition.
119 unsigned int part_curr;
120 struct device_attribute force_ro;
121 struct device_attribute power_ro_lock;
125 static DEFINE_MUTEX(open_lock);
128 MMC_PACKED_NR_IDX = -1,
130 MMC_PACKED_NR_SINGLE,
133 module_param(perdev_minors, int, 0444);
134 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
136 static inline int mmc_blk_part_switch(struct mmc_card *card,
137 struct mmc_blk_data *md);
138 static int get_card_status(struct mmc_card *card, u32 *status, int retries);
140 static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
142 struct mmc_packed *packed = mqrq->packed;
146 mqrq->cmd_type = MMC_PACKED_NONE;
147 packed->nr_entries = MMC_PACKED_NR_ZERO;
148 packed->idx_failure = MMC_PACKED_NR_IDX;
153 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
155 struct mmc_blk_data *md;
157 mutex_lock(&open_lock);
158 md = disk->private_data;
159 if (md && md->usage == 0)
163 mutex_unlock(&open_lock);
168 static inline int mmc_get_devidx(struct gendisk *disk)
170 int devidx = disk->first_minor / perdev_minors;
174 static void mmc_blk_put(struct mmc_blk_data *md)
176 mutex_lock(&open_lock);
178 if (md->usage == 0) {
179 int devidx = mmc_get_devidx(md->disk);
180 blk_cleanup_queue(md->queue.queue);
182 __clear_bit(devidx, dev_use);
187 mutex_unlock(&open_lock);
190 static ssize_t power_ro_lock_show(struct device *dev,
191 struct device_attribute *attr, char *buf)
194 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
195 struct mmc_card *card = md->queue.card;
198 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
200 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
203 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
208 static ssize_t power_ro_lock_store(struct device *dev,
209 struct device_attribute *attr, const char *buf, size_t count)
212 struct mmc_blk_data *md, *part_md;
213 struct mmc_card *card;
216 if (kstrtoul(buf, 0, &set))
222 md = mmc_blk_get(dev_to_disk(dev));
223 card = md->queue.card;
227 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
228 card->ext_csd.boot_ro_lock |
229 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
230 card->ext_csd.part_time);
232 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
234 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
239 pr_info("%s: Locking boot partition ro until next power on\n",
240 md->disk->disk_name);
241 set_disk_ro(md->disk, 1);
243 list_for_each_entry(part_md, &md->part, part)
244 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
245 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
246 set_disk_ro(part_md->disk, 1);
254 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
258 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
260 ret = snprintf(buf, PAGE_SIZE, "%d\n",
261 get_disk_ro(dev_to_disk(dev)) ^
267 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
268 const char *buf, size_t count)
272 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
273 unsigned long set = simple_strtoul(buf, &end, 0);
279 set_disk_ro(dev_to_disk(dev), set || md->read_only);
286 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
288 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
291 mutex_lock(&block_mutex);
294 check_disk_change(bdev);
297 if ((mode & FMODE_WRITE) && md->read_only) {
302 mutex_unlock(&block_mutex);
307 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
309 struct mmc_blk_data *md = disk->private_data;
311 mutex_lock(&block_mutex);
313 mutex_unlock(&block_mutex);
317 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
319 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
325 struct mmc_blk_ioc_data {
326 struct mmc_ioc_cmd ic;
331 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
332 struct mmc_ioc_cmd __user *user)
334 struct mmc_blk_ioc_data *idata;
337 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
343 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
348 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
349 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
354 if (!idata->buf_bytes)
357 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
363 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
364 idata->ic.data_ptr, idata->buf_bytes)) {
379 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
385 if (!status || !retries_max)
389 err = get_card_status(card, status, 5);
393 if (!R1_STATUS(*status) &&
394 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
395 break; /* RPMB programming operation complete */
398 * Rechedule to give the MMC device a chance to continue
399 * processing the previous command without being polled too
402 usleep_range(1000, 5000);
403 } while (++retry_count < retries_max);
405 if (retry_count == retries_max)
411 static int ioctl_do_sanitize(struct mmc_card *card)
415 if (!(mmc_can_sanitize(card) &&
416 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
417 pr_warn("%s: %s - SANITIZE is not supported\n",
418 mmc_hostname(card->host), __func__);
423 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
424 mmc_hostname(card->host), __func__);
426 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
427 EXT_CSD_SANITIZE_START, 1,
428 MMC_SANITIZE_REQ_TIMEOUT);
431 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
432 mmc_hostname(card->host), __func__, err);
434 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
440 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
441 struct mmc_ioc_cmd __user *ic_ptr)
443 struct mmc_blk_ioc_data *idata;
444 struct mmc_blk_data *md;
445 struct mmc_card *card;
446 struct mmc_command cmd = {0};
447 struct mmc_data data = {0};
448 struct mmc_request mrq = {NULL};
449 struct scatterlist sg;
455 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
456 * whole block device, not on a partition. This prevents overspray
457 * between sibling partitions.
459 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
462 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
464 return PTR_ERR(idata);
466 md = mmc_blk_get(bdev->bd_disk);
472 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
475 card = md->queue.card;
481 cmd.opcode = idata->ic.opcode;
482 cmd.arg = idata->ic.arg;
483 cmd.flags = idata->ic.flags;
485 if (idata->buf_bytes) {
488 data.blksz = idata->ic.blksz;
489 data.blocks = idata->ic.blocks;
491 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
493 if (idata->ic.write_flag)
494 data.flags = MMC_DATA_WRITE;
496 data.flags = MMC_DATA_READ;
498 /* data.flags must already be set before doing this. */
499 mmc_set_data_timeout(&data, card);
501 /* Allow overriding the timeout_ns for empirical tuning. */
502 if (idata->ic.data_timeout_ns)
503 data.timeout_ns = idata->ic.data_timeout_ns;
505 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
507 * Pretend this is a data transfer and rely on the
508 * host driver to compute timeout. When all host
509 * drivers support cmd.cmd_timeout for R1B, this
513 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
515 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
525 err = mmc_blk_part_switch(card, md);
529 if (idata->ic.is_acmd) {
530 err = mmc_app_cmd(card->host, card);
536 err = mmc_set_blockcount(card, data.blocks,
537 idata->ic.write_flag & (1 << 31));
542 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
543 (cmd.opcode == MMC_SWITCH)) {
544 err = ioctl_do_sanitize(card);
547 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
553 mmc_wait_for_req(card->host, &mrq);
556 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
557 __func__, cmd.error);
562 dev_err(mmc_dev(card->host), "%s: data error %d\n",
563 __func__, data.error);
569 * According to the SD specs, some commands require a delay after
570 * issuing the command.
572 if (idata->ic.postsleep_min_us)
573 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
575 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
580 if (!idata->ic.write_flag) {
581 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
582 idata->buf, idata->buf_bytes)) {
590 * Ensure RPMB command has completed by polling CMD13
593 err = ioctl_rpmb_card_status_poll(card, &status, 5);
595 dev_err(mmc_dev(card->host),
596 "%s: Card Status=0x%08X, error %d\n",
597 __func__, status, err);
611 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
612 unsigned int cmd, unsigned long arg)
615 if (cmd == MMC_IOC_CMD)
616 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
621 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
622 unsigned int cmd, unsigned long arg)
624 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
628 static const struct block_device_operations mmc_bdops = {
629 .open = mmc_blk_open,
630 .release = mmc_blk_release,
631 .getgeo = mmc_blk_getgeo,
632 .owner = THIS_MODULE,
633 .ioctl = mmc_blk_ioctl,
635 .compat_ioctl = mmc_blk_compat_ioctl,
639 static inline int mmc_blk_part_switch(struct mmc_card *card,
640 struct mmc_blk_data *md)
643 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
645 if (main_md->part_curr == md->part_type)
648 if (mmc_card_mmc(card)) {
649 u8 part_config = card->ext_csd.part_config;
651 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
652 part_config |= md->part_type;
654 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
655 EXT_CSD_PART_CONFIG, part_config,
656 card->ext_csd.part_time);
660 card->ext_csd.part_config = part_config;
663 main_md->part_curr = md->part_type;
667 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
673 struct mmc_request mrq = {NULL};
674 struct mmc_command cmd = {0};
675 struct mmc_data data = {0};
677 struct scatterlist sg;
679 cmd.opcode = MMC_APP_CMD;
680 cmd.arg = card->rca << 16;
681 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
683 err = mmc_wait_for_cmd(card->host, &cmd, 0);
686 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
689 memset(&cmd, 0, sizeof(struct mmc_command));
691 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
693 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
697 data.flags = MMC_DATA_READ;
700 mmc_set_data_timeout(&data, card);
705 blocks = kmalloc(4, GFP_KERNEL);
709 sg_init_one(&sg, blocks, 4);
711 mmc_wait_for_req(card->host, &mrq);
713 result = ntohl(*blocks);
716 if (cmd.error || data.error)
722 static int send_stop(struct mmc_card *card, u32 *status)
724 struct mmc_command cmd = {0};
727 cmd.opcode = MMC_STOP_TRANSMISSION;
728 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
729 err = mmc_wait_for_cmd(card->host, &cmd, 5);
731 *status = cmd.resp[0];
735 static int get_card_status(struct mmc_card *card, u32 *status, int retries)
737 struct mmc_command cmd = {0};
740 cmd.opcode = MMC_SEND_STATUS;
741 if (!mmc_host_is_spi(card->host))
742 cmd.arg = card->rca << 16;
743 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
744 err = mmc_wait_for_cmd(card->host, &cmd, retries);
746 *status = cmd.resp[0];
750 #define ERR_NOMEDIUM 3
753 #define ERR_CONTINUE 0
755 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
756 bool status_valid, u32 status)
760 /* response crc error, retry the r/w cmd */
761 pr_err("%s: %s sending %s command, card status %#x\n",
762 req->rq_disk->disk_name, "response CRC error",
767 pr_err("%s: %s sending %s command, card status %#x\n",
768 req->rq_disk->disk_name, "timed out", name, status);
770 /* If the status cmd initially failed, retry the r/w cmd */
772 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
776 * If it was a r/w cmd crc error, or illegal command
777 * (eg, issued in wrong state) then retry - we should
778 * have corrected the state problem above.
780 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
781 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
785 /* Otherwise abort the command */
786 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
790 /* We don't understand the error code the driver gave us */
791 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
792 req->rq_disk->disk_name, error, status);
798 * Initial r/w and stop cmd error recovery.
799 * We don't know whether the card received the r/w cmd or not, so try to
800 * restore things back to a sane state. Essentially, we do this as follows:
801 * - Obtain card status. If the first attempt to obtain card status fails,
802 * the status word will reflect the failed status cmd, not the failed
803 * r/w cmd. If we fail to obtain card status, it suggests we can no
804 * longer communicate with the card.
805 * - Check the card state. If the card received the cmd but there was a
806 * transient problem with the response, it might still be in a data transfer
807 * mode. Try to send it a stop command. If this fails, we can't recover.
808 * - If the r/w cmd failed due to a response CRC error, it was probably
809 * transient, so retry the cmd.
810 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
811 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
812 * illegal cmd, retry.
813 * Otherwise we don't understand what happened, so abort.
815 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
816 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
818 bool prev_cmd_status_valid = true;
819 u32 status, stop_status = 0;
822 if (mmc_card_removed(card))
826 * Try to get card status which indicates both the card state
827 * and why there was no response. If the first attempt fails,
828 * we can't be sure the returned status is for the r/w command.
830 for (retry = 2; retry >= 0; retry--) {
831 err = get_card_status(card, &status, 0);
835 prev_cmd_status_valid = false;
836 pr_err("%s: error %d sending status command, %sing\n",
837 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
840 /* We couldn't get a response from the card. Give up. */
842 /* Check if the card is removed */
843 if (mmc_detect_card_removed(card->host))
848 /* Flag ECC errors */
849 if ((status & R1_CARD_ECC_FAILED) ||
850 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
851 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
854 /* Flag General errors */
855 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
856 if ((status & R1_ERROR) ||
857 (brq->stop.resp[0] & R1_ERROR)) {
858 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
859 req->rq_disk->disk_name, __func__,
860 brq->stop.resp[0], status);
865 * Check the current card state. If it is in some data transfer
866 * mode, tell it to stop (and hopefully transition back to TRAN.)
868 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
869 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
870 err = send_stop(card, &stop_status);
872 pr_err("%s: error %d sending stop command\n",
873 req->rq_disk->disk_name, err);
876 * If the stop cmd also timed out, the card is probably
877 * not present, so abort. Other errors are bad news too.
881 if (stop_status & R1_CARD_ECC_FAILED)
883 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
884 if (stop_status & R1_ERROR) {
885 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
886 req->rq_disk->disk_name, __func__,
892 /* Check for set block count errors */
894 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
895 prev_cmd_status_valid, status);
897 /* Check for r/w command errors */
899 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
900 prev_cmd_status_valid, status);
903 if (!brq->stop.error)
906 /* Now for stop errors. These aren't fatal to the transfer. */
907 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
908 req->rq_disk->disk_name, brq->stop.error,
909 brq->cmd.resp[0], status);
912 * Subsitute in our own stop status as this will give the error
913 * state which happened during the execution of the r/w command.
916 brq->stop.resp[0] = stop_status;
922 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
927 if (md->reset_done & type)
930 md->reset_done |= type;
931 err = mmc_hw_reset(host);
932 /* Ensure we switch back to the correct partition */
933 if (err != -EOPNOTSUPP) {
934 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
937 main_md->part_curr = main_md->part_type;
938 part_err = mmc_blk_part_switch(host->card, md);
941 * We have failed to get back into the correct
942 * partition, so we need to abort the whole request.
950 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
952 md->reset_done &= ~type;
955 int mmc_access_rpmb(struct mmc_queue *mq)
957 struct mmc_blk_data *md = mq->data;
959 * If this is a RPMB partition access, return ture
961 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
967 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
969 struct mmc_blk_data *md = mq->data;
970 struct mmc_card *card = md->queue.card;
971 unsigned int from, nr, arg;
972 int err = 0, type = MMC_BLK_DISCARD;
974 if (!mmc_can_erase(card)) {
979 from = blk_rq_pos(req);
980 nr = blk_rq_sectors(req);
982 if (mmc_can_discard(card))
983 arg = MMC_DISCARD_ARG;
984 else if (mmc_can_trim(card))
989 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
990 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
991 INAND_CMD38_ARG_EXT_CSD,
992 arg == MMC_TRIM_ARG ?
993 INAND_CMD38_ARG_TRIM :
994 INAND_CMD38_ARG_ERASE,
999 err = mmc_erase(card, from, nr, arg);
1001 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1004 mmc_blk_reset_success(md, type);
1005 blk_end_request(req, err, blk_rq_bytes(req));
1010 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1011 struct request *req)
1013 struct mmc_blk_data *md = mq->data;
1014 struct mmc_card *card = md->queue.card;
1015 unsigned int from, nr, arg;
1016 int err = 0, type = MMC_BLK_SECDISCARD;
1018 if (!(mmc_can_secure_erase_trim(card))) {
1023 from = blk_rq_pos(req);
1024 nr = blk_rq_sectors(req);
1026 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1027 arg = MMC_SECURE_TRIM1_ARG;
1029 arg = MMC_SECURE_ERASE_ARG;
1032 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1033 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1034 INAND_CMD38_ARG_EXT_CSD,
1035 arg == MMC_SECURE_TRIM1_ARG ?
1036 INAND_CMD38_ARG_SECTRIM1 :
1037 INAND_CMD38_ARG_SECERASE,
1043 err = mmc_erase(card, from, nr, arg);
1049 if (arg == MMC_SECURE_TRIM1_ARG) {
1050 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1051 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1052 INAND_CMD38_ARG_EXT_CSD,
1053 INAND_CMD38_ARG_SECTRIM2,
1059 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1067 if (err && !mmc_blk_reset(md, card->host, type))
1070 mmc_blk_reset_success(md, type);
1072 blk_end_request(req, err, blk_rq_bytes(req));
1077 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1079 struct mmc_blk_data *md = mq->data;
1080 struct mmc_card *card = md->queue.card;
1083 ret = mmc_flush_cache(card);
1087 blk_end_request_all(req, ret);
1093 * Reformat current write as a reliable write, supporting
1094 * both legacy and the enhanced reliable write MMC cards.
1095 * In each transfer we'll handle only as much as a single
1096 * reliable write can handle, thus finish the request in
1097 * partial completions.
1099 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1100 struct mmc_card *card,
1101 struct request *req)
1103 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1104 /* Legacy mode imposes restrictions on transfers. */
1105 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1106 brq->data.blocks = 1;
1108 if (brq->data.blocks > card->ext_csd.rel_sectors)
1109 brq->data.blocks = card->ext_csd.rel_sectors;
1110 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1111 brq->data.blocks = 1;
1115 #define CMD_ERRORS \
1116 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1117 R1_ADDRESS_ERROR | /* Misaligned address */ \
1118 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1119 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1120 R1_CC_ERROR | /* Card controller error */ \
1121 R1_ERROR) /* General/unknown error */
1123 static int mmc_blk_err_check(struct mmc_card *card,
1124 struct mmc_async_req *areq)
1126 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1128 struct mmc_blk_request *brq = &mq_mrq->brq;
1129 struct request *req = mq_mrq->req;
1130 int need_retune = card->host->need_retune;
1131 int ecc_err = 0, gen_err = 0;
1134 * sbc.error indicates a problem with the set block count
1135 * command. No data will have been transferred.
1137 * cmd.error indicates a problem with the r/w command. No
1138 * data will have been transferred.
1140 * stop.error indicates a problem with the stop command. Data
1141 * may have been transferred, or may still be transferring.
1143 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1145 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
1147 return MMC_BLK_RETRY;
1149 return MMC_BLK_ABORT;
1151 return MMC_BLK_NOMEDIUM;
1158 * Check for errors relating to the execution of the
1159 * initial command - such as address errors. No data
1160 * has been transferred.
1162 if (brq->cmd.resp[0] & CMD_ERRORS) {
1163 pr_err("%s: r/w command failed, status = %#x\n",
1164 req->rq_disk->disk_name, brq->cmd.resp[0]);
1165 return MMC_BLK_ABORT;
1169 * Everything else is either success, or a data error of some
1170 * kind. If it was a write, we may have transitioned to
1171 * program mode, which we have to wait for it to complete.
1173 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1175 unsigned long timeout;
1177 /* Check stop command response */
1178 if (brq->stop.resp[0] & R1_ERROR) {
1179 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1180 req->rq_disk->disk_name, __func__,
1185 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
1187 int err = get_card_status(card, &status, 5);
1189 pr_err("%s: error %d requesting status\n",
1190 req->rq_disk->disk_name, err);
1191 return MMC_BLK_CMD_ERR;
1194 if (status & R1_ERROR) {
1195 pr_err("%s: %s: general error sending status command, card status %#x\n",
1196 req->rq_disk->disk_name, __func__,
1201 /* Timeout if the device never becomes ready for data
1202 * and never leaves the program state.
1204 if (time_after(jiffies, timeout)) {
1205 pr_err("%s: Card stuck in programming state!"\
1206 " %s %s\n", mmc_hostname(card->host),
1207 req->rq_disk->disk_name, __func__);
1209 return MMC_BLK_CMD_ERR;
1212 * Some cards mishandle the status bits,
1213 * so make sure to check both the busy
1214 * indication and the card state.
1216 } while (!(status & R1_READY_FOR_DATA) ||
1217 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1220 /* if general error occurs, retry the write operation. */
1222 pr_warn("%s: retrying write for general error\n",
1223 req->rq_disk->disk_name);
1224 return MMC_BLK_RETRY;
1227 if (brq->data.error) {
1228 pr_err("need_retune:%d,brq->retune_retry_done:%d.\n",need_retune,brq->retune_retry_done);
1229 if (need_retune && !brq->retune_retry_done) {
1230 pr_err("%s: retrying because a re-tune was needed\n",
1231 req->rq_disk->disk_name);
1232 brq->retune_retry_done = 1;
1233 return MMC_BLK_RETRY;
1236 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1237 req->rq_disk->disk_name, brq->data.error,
1238 (unsigned)blk_rq_pos(req),
1239 (unsigned)blk_rq_sectors(req),
1240 brq->cmd.resp[0], brq->stop.resp[0]);
1242 if (rq_data_dir(req) == READ) {
1244 return MMC_BLK_ECC_ERR;
1245 return MMC_BLK_DATA_ERR;
1247 return MMC_BLK_CMD_ERR;
1251 if (!brq->data.bytes_xfered)
1252 return MMC_BLK_RETRY;
1254 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1255 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1256 return MMC_BLK_PARTIAL;
1258 return MMC_BLK_SUCCESS;
1261 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1262 return MMC_BLK_PARTIAL;
1264 return MMC_BLK_SUCCESS;
1267 static int mmc_blk_packed_err_check(struct mmc_card *card,
1268 struct mmc_async_req *areq)
1270 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1272 struct request *req = mq_rq->req;
1273 struct mmc_packed *packed = mq_rq->packed;
1274 int err, check, status;
1280 check = mmc_blk_err_check(card, areq);
1281 err = get_card_status(card, &status, 0);
1283 pr_err("%s: error %d sending status command\n",
1284 req->rq_disk->disk_name, err);
1285 return MMC_BLK_ABORT;
1288 if (status & R1_EXCEPTION_EVENT) {
1289 ext_csd = kzalloc(512, GFP_KERNEL);
1291 pr_err("%s: unable to allocate buffer for ext_csd\n",
1292 req->rq_disk->disk_name);
1296 err = mmc_send_ext_csd(card, ext_csd);
1298 pr_err("%s: error %d sending ext_csd\n",
1299 req->rq_disk->disk_name, err);
1300 check = MMC_BLK_ABORT;
1304 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1305 EXT_CSD_PACKED_FAILURE) &&
1306 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1307 EXT_CSD_PACKED_GENERIC_ERROR)) {
1308 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1309 EXT_CSD_PACKED_INDEXED_ERROR) {
1310 packed->idx_failure =
1311 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1312 check = MMC_BLK_PARTIAL;
1314 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1315 "failure index: %d\n",
1316 req->rq_disk->disk_name, packed->nr_entries,
1317 packed->blocks, packed->idx_failure);
1326 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1327 struct mmc_card *card,
1329 struct mmc_queue *mq)
1331 u32 readcmd, writecmd;
1332 struct mmc_blk_request *brq = &mqrq->brq;
1333 struct request *req = mqrq->req;
1334 struct mmc_blk_data *md = mq->data;
1338 * Reliable writes are used to implement Forced Unit Access and
1339 * REQ_META accesses, and are supported only on MMCs.
1341 * XXX: this really needs a good explanation of why REQ_META
1342 * is treated special.
1344 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1345 (req->cmd_flags & REQ_META)) &&
1346 (rq_data_dir(req) == WRITE) &&
1347 (md->flags & MMC_BLK_REL_WR);
1349 memset(brq, 0, sizeof(struct mmc_blk_request));
1350 brq->mrq.cmd = &brq->cmd;
1351 brq->mrq.data = &brq->data;
1353 brq->cmd.arg = blk_rq_pos(req);
1354 if (!mmc_card_blockaddr(card))
1356 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1357 brq->data.blksz = 512;
1358 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1360 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1361 brq->data.blocks = blk_rq_sectors(req);
1364 * The block layer doesn't support all sector count
1365 * restrictions, so we need to be prepared for too big
1368 if (brq->data.blocks > card->host->max_blk_count)
1369 brq->data.blocks = card->host->max_blk_count;
1371 if (brq->data.blocks > 1) {
1373 * After a read error, we redo the request one sector
1374 * at a time in order to accurately determine which
1375 * sectors can be read successfully.
1378 brq->data.blocks = 1;
1380 /* Some controllers can't do multiblock reads due to hw bugs */
1381 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1382 rq_data_dir(req) == READ)
1383 brq->data.blocks = 1;
1386 if (brq->data.blocks > 1 || do_rel_wr) {
1387 /* SPI multiblock writes terminate using a special
1388 * token, not a STOP_TRANSMISSION request.
1390 if (!mmc_host_is_spi(card->host) ||
1391 rq_data_dir(req) == READ)
1392 brq->mrq.stop = &brq->stop;
1393 readcmd = MMC_READ_MULTIPLE_BLOCK;
1394 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1396 brq->mrq.stop = NULL;
1397 readcmd = MMC_READ_SINGLE_BLOCK;
1398 writecmd = MMC_WRITE_BLOCK;
1400 if (rq_data_dir(req) == READ) {
1401 brq->cmd.opcode = readcmd;
1402 brq->data.flags |= MMC_DATA_READ;
1404 brq->cmd.opcode = writecmd;
1405 brq->data.flags |= MMC_DATA_WRITE;
1408 if (req->cmd_flags & REQ_KERNEL)
1409 brq->data.flags |= MMC_DATA_DIRECT;
1412 mmc_apply_rel_rw(brq, card, req);
1415 * Data tag is used only during writing meta data to speed
1416 * up write and any subsequent read of this meta data
1418 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1419 (req->cmd_flags & REQ_META) &&
1420 (rq_data_dir(req) == WRITE) &&
1421 ((brq->data.blocks * brq->data.blksz) >=
1422 card->ext_csd.data_tag_unit_size);
1425 * Pre-defined multi-block transfers are preferable to
1426 * open ended-ones (and necessary for reliable writes).
1427 * However, it is not sufficient to just send CMD23,
1428 * and avoid the final CMD12, as on an error condition
1429 * CMD12 (stop) needs to be sent anyway. This, coupled
1430 * with Auto-CMD23 enhancements provided by some
1431 * hosts, means that the complexity of dealing
1432 * with this is best left to the host. If CMD23 is
1433 * supported by card and host, we'll fill sbc in and let
1434 * the host deal with handling it correctly. This means
1435 * that for hosts that don't expose MMC_CAP_CMD23, no
1436 * change of behavior will be observed.
1438 * N.B: Some MMC cards experience perf degradation.
1439 * We'll avoid using CMD23-bounded multiblock writes for
1440 * these, while retaining features like reliable writes.
1442 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1443 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1445 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1446 brq->sbc.arg = brq->data.blocks |
1447 (do_rel_wr ? (1 << 31) : 0) |
1448 (do_data_tag ? (1 << 29) : 0);
1449 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1450 brq->mrq.sbc = &brq->sbc;
1453 mmc_set_data_timeout(&brq->data, card);
1455 brq->data.sg = mqrq->sg;
1456 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1459 * Adjust the sg list so it is the same size as the
1462 if (brq->data.blocks != blk_rq_sectors(req)) {
1463 int i, data_size = brq->data.blocks << 9;
1464 struct scatterlist *sg;
1466 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1467 data_size -= sg->length;
1468 if (data_size <= 0) {
1469 sg->length += data_size;
1474 brq->data.sg_len = i;
1477 mqrq->mmc_active.mrq = &brq->mrq;
1478 mqrq->mmc_active.err_check = mmc_blk_err_check;
1480 mmc_queue_bounce_pre(mqrq);
1483 static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1484 struct mmc_card *card)
1486 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1487 unsigned int max_seg_sz = queue_max_segment_size(q);
1488 unsigned int len, nr_segs = 0;
1491 len = min(hdr_sz, max_seg_sz);
1499 static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1501 struct request_queue *q = mq->queue;
1502 struct mmc_card *card = mq->card;
1503 struct request *cur = req, *next = NULL;
1504 struct mmc_blk_data *md = mq->data;
1505 struct mmc_queue_req *mqrq = mq->mqrq_cur;
1506 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1507 unsigned int req_sectors = 0, phys_segments = 0;
1508 unsigned int max_blk_count, max_phys_segs;
1509 bool put_back = true;
1510 u8 max_packed_rw = 0;
1513 if (!(md->flags & MMC_BLK_PACKED_CMD))
1516 if ((rq_data_dir(cur) == WRITE) &&
1517 mmc_host_packed_wr(card->host))
1518 max_packed_rw = card->ext_csd.max_packed_writes;
1520 if (max_packed_rw == 0)
1523 if (mmc_req_rel_wr(cur) &&
1524 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1527 if (mmc_large_sector(card) &&
1528 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1531 mmc_blk_clear_packed(mqrq);
1533 max_blk_count = min(card->host->max_blk_count,
1534 card->host->max_req_size >> 9);
1535 if (unlikely(max_blk_count > 0xffff))
1536 max_blk_count = 0xffff;
1538 max_phys_segs = queue_max_segments(q);
1539 req_sectors += blk_rq_sectors(cur);
1540 phys_segments += cur->nr_phys_segments;
1542 if (rq_data_dir(cur) == WRITE) {
1543 req_sectors += mmc_large_sector(card) ? 8 : 1;
1544 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1548 if (reqs >= max_packed_rw - 1) {
1553 spin_lock_irq(q->queue_lock);
1554 next = blk_fetch_request(q);
1555 spin_unlock_irq(q->queue_lock);
1561 if (mmc_large_sector(card) &&
1562 !IS_ALIGNED(blk_rq_sectors(next), 8))
1565 if (next->cmd_flags & REQ_DISCARD ||
1566 next->cmd_flags & REQ_FLUSH)
1569 if (rq_data_dir(cur) != rq_data_dir(next))
1572 if (mmc_req_rel_wr(next) &&
1573 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1576 req_sectors += blk_rq_sectors(next);
1577 if (req_sectors > max_blk_count)
1580 phys_segments += next->nr_phys_segments;
1581 if (phys_segments > max_phys_segs)
1584 list_add_tail(&next->queuelist, &mqrq->packed->list);
1590 spin_lock_irq(q->queue_lock);
1591 blk_requeue_request(q, next);
1592 spin_unlock_irq(q->queue_lock);
1596 list_add(&req->queuelist, &mqrq->packed->list);
1597 mqrq->packed->nr_entries = ++reqs;
1598 mqrq->packed->retries = reqs;
1603 mqrq->cmd_type = MMC_PACKED_NONE;
1607 static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1608 struct mmc_card *card,
1609 struct mmc_queue *mq)
1611 struct mmc_blk_request *brq = &mqrq->brq;
1612 struct request *req = mqrq->req;
1613 struct request *prq;
1614 struct mmc_blk_data *md = mq->data;
1615 struct mmc_packed *packed = mqrq->packed;
1616 bool do_rel_wr, do_data_tag;
1617 u32 *packed_cmd_hdr;
1623 mqrq->cmd_type = MMC_PACKED_WRITE;
1625 packed->idx_failure = MMC_PACKED_NR_IDX;
1627 packed_cmd_hdr = packed->cmd_hdr;
1628 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
1629 packed_cmd_hdr[0] = (packed->nr_entries << 16) |
1630 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1631 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1634 * Argument for each entry of packed group
1636 list_for_each_entry(prq, &packed->list, queuelist) {
1637 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1638 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1639 (prq->cmd_flags & REQ_META) &&
1640 (rq_data_dir(prq) == WRITE) &&
1641 ((brq->data.blocks * brq->data.blksz) >=
1642 card->ext_csd.data_tag_unit_size);
1643 /* Argument of CMD23 */
1644 packed_cmd_hdr[(i * 2)] =
1645 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1646 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1647 blk_rq_sectors(prq);
1648 /* Argument of CMD18 or CMD25 */
1649 packed_cmd_hdr[((i * 2)) + 1] =
1650 mmc_card_blockaddr(card) ?
1651 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1652 packed->blocks += blk_rq_sectors(prq);
1656 memset(brq, 0, sizeof(struct mmc_blk_request));
1657 brq->mrq.cmd = &brq->cmd;
1658 brq->mrq.data = &brq->data;
1659 brq->mrq.sbc = &brq->sbc;
1660 brq->mrq.stop = &brq->stop;
1662 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1663 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1664 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1666 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1667 brq->cmd.arg = blk_rq_pos(req);
1668 if (!mmc_card_blockaddr(card))
1670 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1672 brq->data.blksz = 512;
1673 brq->data.blocks = packed->blocks + hdr_blocks;
1674 brq->data.flags |= MMC_DATA_WRITE;
1676 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1678 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1680 mmc_set_data_timeout(&brq->data, card);
1682 brq->data.sg = mqrq->sg;
1683 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1685 mqrq->mmc_active.mrq = &brq->mrq;
1686 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1688 mmc_queue_bounce_pre(mqrq);
1691 static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1692 struct mmc_blk_request *brq, struct request *req,
1695 struct mmc_queue_req *mq_rq;
1696 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1699 * If this is an SD card and we're writing, we can first
1700 * mark the known good sectors as ok.
1702 * If the card is not SD, we can still ok written sectors
1703 * as reported by the controller (which might be less than
1704 * the real number of written sectors, but never more).
1706 if (mmc_card_sd(card)) {
1709 blocks = mmc_sd_num_wr_blocks(card);
1710 if (blocks != (u32)-1) {
1711 ret = blk_end_request(req, 0, blocks << 9);
1714 if (!mmc_packed_cmd(mq_rq->cmd_type))
1715 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1720 static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1722 struct request *prq;
1723 struct mmc_packed *packed = mq_rq->packed;
1724 int idx = packed->idx_failure, i = 0;
1729 while (!list_empty(&packed->list)) {
1730 prq = list_entry_rq(packed->list.next);
1732 /* retry from error index */
1733 packed->nr_entries -= idx;
1737 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1738 list_del_init(&prq->queuelist);
1739 mmc_blk_clear_packed(mq_rq);
1743 list_del_init(&prq->queuelist);
1744 blk_end_request(prq, 0, blk_rq_bytes(prq));
1748 mmc_blk_clear_packed(mq_rq);
1752 static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1754 struct request *prq;
1755 struct mmc_packed *packed = mq_rq->packed;
1759 while (!list_empty(&packed->list)) {
1760 prq = list_entry_rq(packed->list.next);
1761 list_del_init(&prq->queuelist);
1762 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1765 mmc_blk_clear_packed(mq_rq);
1768 static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1769 struct mmc_queue_req *mq_rq)
1771 struct request *prq;
1772 struct request_queue *q = mq->queue;
1773 struct mmc_packed *packed = mq_rq->packed;
1777 while (!list_empty(&packed->list)) {
1778 prq = list_entry_rq(packed->list.prev);
1779 if (prq->queuelist.prev != &packed->list) {
1780 list_del_init(&prq->queuelist);
1781 spin_lock_irq(q->queue_lock);
1782 blk_requeue_request(mq->queue, prq);
1783 spin_unlock_irq(q->queue_lock);
1785 list_del_init(&prq->queuelist);
1789 mmc_blk_clear_packed(mq_rq);
1792 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
1794 struct mmc_blk_data *md = mq->data;
1795 struct mmc_card *card = md->queue.card;
1796 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
1797 int ret = 1, disable_multi = 0, retry = 0, retune_retry_done = 0, type;
1798 enum mmc_blk_status status;
1799 struct mmc_queue_req *mq_rq;
1800 struct request *req = rqc;
1801 struct mmc_async_req *areq;
1802 const u8 packed_nr = 2;
1805 if (!rqc && !mq->mqrq_prev->req)
1809 reqs = mmc_blk_prep_packed_list(mq, rqc);
1814 * When 4KB native sector is enabled, only 8 blocks
1815 * multiple read or write is allowed
1817 if ((brq->data.blocks & 0x07) &&
1818 (card->ext_csd.data_sector_size == 4096)) {
1819 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1820 req->rq_disk->disk_name);
1821 mq_rq = mq->mqrq_cur;
1825 if (reqs >= packed_nr)
1826 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1829 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1830 areq = &mq->mqrq_cur->mmc_active;
1833 areq = mmc_start_req(card->host, areq, (int *) &status);
1835 if (status == MMC_BLK_NEW_REQUEST)
1836 mq->flags |= MMC_QUEUE_NEW_REQUEST;
1840 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1843 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1844 mmc_queue_bounce_post(mq_rq);
1847 case MMC_BLK_SUCCESS:
1848 case MMC_BLK_PARTIAL:
1850 * A block was successfully transferred.
1852 mmc_blk_reset_success(md, type);
1854 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1855 ret = mmc_blk_end_packed_req(mq_rq);
1858 ret = blk_end_request(req, 0,
1859 brq->data.bytes_xfered);
1863 * If the blk_end_request function returns non-zero even
1864 * though all data has been transferred and no errors
1865 * were returned by the host controller, it's a bug.
1867 if (status == MMC_BLK_SUCCESS && ret) {
1868 pr_err("%s BUG rq_tot %d d_xfer %d\n",
1869 __func__, blk_rq_bytes(req),
1870 brq->data.bytes_xfered);
1875 case MMC_BLK_CMD_ERR:
1876 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1877 if (mmc_blk_reset(md, card->host, type))
1883 retune_retry_done = brq->retune_retry_done;
1888 if (!mmc_blk_reset(md, card->host, type))
1891 case MMC_BLK_DATA_ERR: {
1894 err = mmc_blk_reset(md, card->host, type);
1897 if (err == -ENODEV ||
1898 mmc_packed_cmd(mq_rq->cmd_type))
1902 case MMC_BLK_ECC_ERR:
1903 if (brq->data.blocks > 1) {
1904 /* Redo read one sector at a time */
1905 pr_warning("%s: retrying using single block read\n",
1906 req->rq_disk->disk_name);
1911 * After an error, we redo I/O one sector at a
1912 * time, so we only reach here after trying to
1913 * read a single sector.
1915 ret = blk_end_request(req, -EIO,
1920 case MMC_BLK_NOMEDIUM:
1923 pr_err("%s: Unhandled return value (%d)",
1924 req->rq_disk->disk_name, status);
1929 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1930 if (!mq_rq->packed->retries)
1932 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1933 mmc_start_req(card->host,
1934 &mq_rq->mmc_active, NULL);
1938 * In case of a incomplete request
1939 * prepare it again and resend.
1941 mmc_blk_rw_rq_prep(mq_rq, card,
1943 mmc_start_req(card->host,
1944 &mq_rq->mmc_active, NULL);
1946 mq_rq->brq.retune_retry_done = retune_retry_done;
1953 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1954 mmc_blk_abort_packed_req(mq_rq);
1956 if (mmc_card_removed(card))
1957 req->cmd_flags |= REQ_QUIET;
1959 ret = blk_end_request(req, -EIO,
1960 blk_rq_cur_bytes(req));
1965 if (mmc_card_removed(card)) {
1966 rqc->cmd_flags |= REQ_QUIET;
1967 blk_end_request_all(rqc, -EIO);
1970 * If current request is packed, it needs to put back.
1972 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
1973 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1975 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1976 mmc_start_req(card->host,
1977 &mq->mqrq_cur->mmc_active, NULL);
1984 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1987 struct mmc_blk_data *md = mq->data;
1988 struct mmc_card *card = md->queue.card;
1989 struct mmc_host *host = card->host;
1990 unsigned long flags;
1991 unsigned int cmd_flags = req ? req->cmd_flags : 0;
1993 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1994 if (mmc_bus_needs_resume(card->host))
1995 mmc_resume_bus(card->host);
1998 if (req && !mq->mqrq_prev->req)
1999 /* claim host only for the first request */
2002 ret = mmc_blk_part_switch(card, md);
2005 blk_end_request_all(req, -EIO);
2011 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
2012 if (cmd_flags & REQ_DISCARD) {
2013 /* complete ongoing async transfer before issuing discard */
2014 if (card->host->areq)
2015 mmc_blk_issue_rw_rq(mq, NULL);
2016 if (req->cmd_flags & REQ_SECURE &&
2017 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
2018 ret = mmc_blk_issue_secdiscard_rq(mq, req);
2020 ret = mmc_blk_issue_discard_rq(mq, req);
2021 } else if (cmd_flags & REQ_FLUSH) {
2022 /* complete ongoing async transfer before issuing flush */
2023 if (card->host->areq)
2024 mmc_blk_issue_rw_rq(mq, NULL);
2025 ret = mmc_blk_issue_flush(mq, req);
2027 if (!req && host->areq) {
2028 spin_lock_irqsave(&host->context_info.lock, flags);
2029 host->context_info.is_waiting_last_req = true;
2030 spin_unlock_irqrestore(&host->context_info.lock, flags);
2032 ret = mmc_blk_issue_rw_rq(mq, req);
2036 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
2037 (cmd_flags & MMC_REQ_SPECIAL_MASK))
2039 * Release host when there are no more requests
2040 * and after special request(discard, flush) is done.
2041 * In case sepecial request, there is no reentry to
2042 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2048 static inline int mmc_blk_readonly(struct mmc_card *card)
2050 return mmc_card_readonly(card) ||
2051 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2054 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2055 struct device *parent,
2058 const char *subname,
2061 struct mmc_blk_data *md;
2064 devidx = find_first_zero_bit(dev_use, max_devices);
2065 if (devidx >= max_devices)
2066 return ERR_PTR(-ENOSPC);
2067 __set_bit(devidx, dev_use);
2069 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2076 * !subname implies we are creating main mmc_blk_data that will be
2077 * associated with mmc_card with mmc_set_drvdata. Due to device
2078 * partitions, devidx will not coincide with a per-physical card
2079 * index anymore so we keep track of a name index.
2082 md->name_idx = find_first_zero_bit(name_use, max_devices);
2083 __set_bit(md->name_idx, name_use);
2085 md->name_idx = ((struct mmc_blk_data *)
2086 dev_to_disk(parent)->private_data)->name_idx;
2088 md->area_type = area_type;
2091 * Set the read-only status based on the supported commands
2092 * and the write protect switch.
2094 md->read_only = mmc_blk_readonly(card);
2096 md->disk = alloc_disk(perdev_minors);
2097 if (md->disk == NULL) {
2102 spin_lock_init(&md->lock);
2103 INIT_LIST_HEAD(&md->part);
2106 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2110 md->queue.issue_fn = mmc_blk_issue_rq;
2111 md->queue.data = md;
2113 md->disk->major = MMC_BLOCK_MAJOR;
2114 md->disk->first_minor = devidx * perdev_minors;
2115 md->disk->fops = &mmc_bdops;
2116 md->disk->private_data = md;
2117 md->disk->queue = md->queue.queue;
2118 md->disk->driverfs_dev = parent;
2119 set_disk_ro(md->disk, md->read_only || default_ro);
2120 md->disk->flags = GENHD_FL_EXT_DEVT;
2121 if (area_type & MMC_BLK_DATA_AREA_RPMB)
2122 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
2125 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2127 * - be set for removable media with permanent block devices
2128 * - be unset for removable block devices with permanent media
2130 * Since MMC block devices clearly fall under the second
2131 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2132 * should use the block device creation/destruction hotplug
2133 * messages to tell when the card is present.
2136 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2137 "mmcblk%d%s", md->name_idx, subname ? subname : "");
2139 if (mmc_card_mmc(card))
2140 blk_queue_logical_block_size(md->queue.queue,
2141 card->ext_csd.data_sector_size);
2143 blk_queue_logical_block_size(md->queue.queue, 512);
2145 set_capacity(md->disk, size);
2147 if (mmc_host_cmd23(card->host)) {
2148 if (mmc_card_mmc(card) ||
2149 (mmc_card_sd(card) &&
2150 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2151 md->flags |= MMC_BLK_CMD23;
2154 if (mmc_card_mmc(card) &&
2155 md->flags & MMC_BLK_CMD23 &&
2156 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2157 card->ext_csd.rel_sectors)) {
2158 md->flags |= MMC_BLK_REL_WR;
2159 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2162 if (mmc_card_mmc(card) &&
2163 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2164 (md->flags & MMC_BLK_CMD23) &&
2165 card->ext_csd.packed_event_en) {
2166 if (!mmc_packed_init(&md->queue, card))
2167 md->flags |= MMC_BLK_PACKED_CMD;
2177 return ERR_PTR(ret);
2180 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2183 struct mmc_blk_data *md;
2185 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2187 * The EXT_CSD sector count is in number or 512 byte
2190 size = card->ext_csd.sectors;
2193 * The CSD capacity field is in units of read_blkbits.
2194 * set_capacity takes units of 512 bytes.
2196 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2199 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2200 MMC_BLK_DATA_AREA_MAIN);
2204 static int mmc_blk_alloc_part(struct mmc_card *card,
2205 struct mmc_blk_data *md,
2206 unsigned int part_type,
2209 const char *subname,
2213 struct mmc_blk_data *part_md;
2215 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2216 subname, area_type);
2217 if (IS_ERR(part_md))
2218 return PTR_ERR(part_md);
2219 part_md->part_type = part_type;
2220 list_add(&part_md->part, &md->part);
2222 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2223 cap_str, sizeof(cap_str));
2224 pr_info("%s: %s %s partition %u %s\n",
2225 part_md->disk->disk_name, mmc_card_id(card),
2226 mmc_card_name(card), part_md->part_type, cap_str);
2230 /* MMC Physical partitions consist of two boot partitions and
2231 * up to four general purpose partitions.
2232 * For each partition enabled in EXT_CSD a block device will be allocatedi
2233 * to provide access to the partition.
2236 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2240 if (!mmc_card_mmc(card))
2243 for (idx = 0; idx < card->nr_parts; idx++) {
2244 if (card->part[idx].size) {
2245 ret = mmc_blk_alloc_part(card, md,
2246 card->part[idx].part_cfg,
2247 card->part[idx].size >> 9,
2248 card->part[idx].force_ro,
2249 card->part[idx].name,
2250 card->part[idx].area_type);
2259 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2261 struct mmc_card *card;
2265 * Flush remaining requests and free queues. It
2266 * is freeing the queue that stops new requests
2267 * from being accepted.
2269 card = md->queue.card;
2270 mmc_cleanup_queue(&md->queue);
2271 if (md->flags & MMC_BLK_PACKED_CMD)
2272 mmc_packed_clean(&md->queue);
2273 if (md->disk->flags & GENHD_FL_UP) {
2274 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2275 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2276 card->ext_csd.boot_ro_lockable)
2277 device_remove_file(disk_to_dev(md->disk),
2278 &md->power_ro_lock);
2280 del_gendisk(md->disk);
2286 static void mmc_blk_remove_parts(struct mmc_card *card,
2287 struct mmc_blk_data *md)
2289 struct list_head *pos, *q;
2290 struct mmc_blk_data *part_md;
2292 __clear_bit(md->name_idx, name_use);
2293 list_for_each_safe(pos, q, &md->part) {
2294 part_md = list_entry(pos, struct mmc_blk_data, part);
2296 mmc_blk_remove_req(part_md);
2300 static int mmc_add_disk(struct mmc_blk_data *md)
2303 struct mmc_card *card = md->queue.card;
2306 md->force_ro.show = force_ro_show;
2307 md->force_ro.store = force_ro_store;
2308 sysfs_attr_init(&md->force_ro.attr);
2309 md->force_ro.attr.name = "force_ro";
2310 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2311 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2315 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2316 card->ext_csd.boot_ro_lockable) {
2319 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2322 mode = S_IRUGO | S_IWUSR;
2324 md->power_ro_lock.show = power_ro_lock_show;
2325 md->power_ro_lock.store = power_ro_lock_store;
2326 sysfs_attr_init(&md->power_ro_lock.attr);
2327 md->power_ro_lock.attr.mode = mode;
2328 md->power_ro_lock.attr.name =
2329 "ro_lock_until_next_power_on";
2330 ret = device_create_file(disk_to_dev(md->disk),
2331 &md->power_ro_lock);
2333 goto power_ro_lock_fail;
2338 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2340 del_gendisk(md->disk);
2345 #define CID_MANFID_SANDISK 0x2
2346 #define CID_MANFID_TOSHIBA 0x11
2347 #define CID_MANFID_MICRON 0x13
2348 #define CID_MANFID_SAMSUNG 0x15
2349 #define CID_MANFID_HYNIX 0x90
2351 static const struct mmc_fixup blk_fixups[] =
2353 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2354 MMC_QUIRK_INAND_CMD38),
2355 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2356 MMC_QUIRK_INAND_CMD38),
2357 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2358 MMC_QUIRK_INAND_CMD38),
2359 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2360 MMC_QUIRK_INAND_CMD38),
2361 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2362 MMC_QUIRK_INAND_CMD38),
2365 * Some MMC cards experience performance degradation with CMD23
2366 * instead of CMD12-bounded multiblock transfers. For now we'll
2367 * black list what's bad...
2368 * - Certain Toshiba cards.
2370 * N.B. This doesn't affect SD cards.
2372 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2373 MMC_QUIRK_BLK_NO_CMD23),
2374 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2375 MMC_QUIRK_BLK_NO_CMD23),
2376 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2377 MMC_QUIRK_BLK_NO_CMD23),
2380 * Some Micron MMC cards needs longer data read timeout than
2383 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
2384 MMC_QUIRK_LONG_READ_TIME),
2387 * On these Samsung MoviNAND parts, performing secure erase or
2388 * secure trim can result in unrecoverable corruption due to a
2391 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2392 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2393 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2394 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2395 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2396 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2397 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2398 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2399 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2400 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2401 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2402 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2403 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2404 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2405 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2406 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2408 /* Hynix 4.41 iNAND execute trim will lead boot up failed. */
2409 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_HYNIX, CID_OEMID_ANY, add_quirk_mmc,
2410 MMC_QUIRK_TRIM_UNSTABLE),
2415 #if defined(CONFIG_MMC_DW_ROCKCHIP)
2416 extern struct mmc_card *this_card;
2419 static int mmc_blk_probe(struct mmc_card *card)
2421 struct mmc_blk_data *md, *part_md;
2425 * Check that the card supports the command class(es) we need.
2427 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2430 md = mmc_blk_alloc(card);
2434 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
2435 cap_str, sizeof(cap_str));
2436 pr_info("%s: %s %s %s %s\n",
2437 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2438 cap_str, md->read_only ? "(ro)" : "");
2440 if (mmc_blk_alloc_parts(card, md))
2443 mmc_set_drvdata(card, md);
2444 mmc_fixup_device(card, blk_fixups);
2446 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2447 mmc_set_bus_resume_policy(card->host, 1);
2449 #if defined(CONFIG_MMC_DW_ROCKCHIP)
2450 if (card->host->restrict_caps & RESTRICT_CARD_TYPE_EMMC) {
2452 md->disk->emmc_disk = 1;
2454 md->disk->emmc_disk = 0;
2457 if (mmc_add_disk(md))
2460 list_for_each_entry(part_md, &md->part, part) {
2461 if (mmc_add_disk(part_md))
2465 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2466 pm_runtime_use_autosuspend(&card->dev);
2469 * Don't enable runtime PM for SD-combo cards here. Leave that
2470 * decision to be taken during the SDIO init sequence instead.
2472 if (card->type != MMC_TYPE_SD_COMBO) {
2473 pm_runtime_set_active(&card->dev);
2474 pm_runtime_enable(&card->dev);
2480 mmc_blk_remove_parts(card, md);
2481 mmc_blk_remove_req(md);
2485 static void mmc_blk_remove(struct mmc_card *card)
2487 struct mmc_blk_data *md = mmc_get_drvdata(card);
2489 #if defined(CONFIG_MMC_DW_ROCKCHIP)
2490 if (card->host->restrict_caps & RESTRICT_CARD_TYPE_EMMC)
2493 mmc_blk_remove_parts(card, md);
2494 pm_runtime_get_sync(&card->dev);
2495 mmc_claim_host(card->host);
2496 mmc_blk_part_switch(card, md);
2497 mmc_release_host(card->host);
2498 if (card->type != MMC_TYPE_SD_COMBO)
2499 pm_runtime_disable(&card->dev);
2500 pm_runtime_put_noidle(&card->dev);
2501 mmc_blk_remove_req(md);
2502 mmc_set_drvdata(card, NULL);
2503 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2504 mmc_set_bus_resume_policy(card->host, 0);
2508 static int _mmc_blk_suspend(struct mmc_card *card)
2510 struct mmc_blk_data *part_md;
2511 struct mmc_blk_data *md = mmc_get_drvdata(card);
2514 mmc_queue_suspend(&md->queue);
2515 list_for_each_entry(part_md, &md->part, part) {
2516 mmc_queue_suspend(&part_md->queue);
2522 static void mmc_blk_shutdown(struct mmc_card *card)
2524 _mmc_blk_suspend(card);
2528 static int mmc_blk_suspend(struct mmc_card *card)
2530 return _mmc_blk_suspend(card);
2533 static int mmc_blk_resume(struct mmc_card *card)
2535 struct mmc_blk_data *part_md;
2536 struct mmc_blk_data *md = mmc_get_drvdata(card);
2540 * Resume involves the card going into idle state,
2541 * so current partition is always the main one.
2543 md->part_curr = md->part_type;
2544 mmc_queue_resume(&md->queue);
2545 list_for_each_entry(part_md, &md->part, part) {
2546 mmc_queue_resume(&part_md->queue);
2552 #define mmc_blk_suspend NULL
2553 #define mmc_blk_resume NULL
2556 static struct mmc_driver mmc_driver = {
2560 .probe = mmc_blk_probe,
2561 .remove = mmc_blk_remove,
2562 .suspend = mmc_blk_suspend,
2563 .resume = mmc_blk_resume,
2564 .shutdown = mmc_blk_shutdown,
2567 static int __init mmc_blk_init(void)
2571 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2572 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2574 max_devices = 256 / perdev_minors;
2576 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2580 res = mmc_register_driver(&mmc_driver);
2586 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2591 static void __exit mmc_blk_exit(void)
2593 mmc_unregister_driver(&mmc_driver);
2594 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2597 module_init(mmc_blk_init);
2598 module_exit(mmc_blk_exit);
2600 MODULE_LICENSE("GPL");
2601 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");