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 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
957 struct mmc_blk_data *md = mq->data;
958 struct mmc_card *card = md->queue.card;
959 unsigned int from, nr, arg;
960 int err = 0, type = MMC_BLK_DISCARD;
962 if (!mmc_can_erase(card)) {
967 from = blk_rq_pos(req);
968 nr = blk_rq_sectors(req);
970 if (mmc_can_discard(card))
971 arg = MMC_DISCARD_ARG;
972 else if (mmc_can_trim(card))
977 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
978 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
979 INAND_CMD38_ARG_EXT_CSD,
980 arg == MMC_TRIM_ARG ?
981 INAND_CMD38_ARG_TRIM :
982 INAND_CMD38_ARG_ERASE,
987 err = mmc_erase(card, from, nr, arg);
989 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
992 mmc_blk_reset_success(md, type);
993 blk_end_request(req, err, blk_rq_bytes(req));
998 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1001 struct mmc_blk_data *md = mq->data;
1002 struct mmc_card *card = md->queue.card;
1003 unsigned int from, nr, arg;
1004 int err = 0, type = MMC_BLK_SECDISCARD;
1006 if (!(mmc_can_secure_erase_trim(card))) {
1011 from = blk_rq_pos(req);
1012 nr = blk_rq_sectors(req);
1014 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1015 arg = MMC_SECURE_TRIM1_ARG;
1017 arg = MMC_SECURE_ERASE_ARG;
1020 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1021 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1022 INAND_CMD38_ARG_EXT_CSD,
1023 arg == MMC_SECURE_TRIM1_ARG ?
1024 INAND_CMD38_ARG_SECTRIM1 :
1025 INAND_CMD38_ARG_SECERASE,
1031 err = mmc_erase(card, from, nr, arg);
1037 if (arg == MMC_SECURE_TRIM1_ARG) {
1038 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1039 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1040 INAND_CMD38_ARG_EXT_CSD,
1041 INAND_CMD38_ARG_SECTRIM2,
1047 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1055 if (err && !mmc_blk_reset(md, card->host, type))
1058 mmc_blk_reset_success(md, type);
1060 blk_end_request(req, err, blk_rq_bytes(req));
1065 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1067 struct mmc_blk_data *md = mq->data;
1068 struct mmc_card *card = md->queue.card;
1071 ret = mmc_flush_cache(card);
1075 blk_end_request_all(req, ret);
1081 * Reformat current write as a reliable write, supporting
1082 * both legacy and the enhanced reliable write MMC cards.
1083 * In each transfer we'll handle only as much as a single
1084 * reliable write can handle, thus finish the request in
1085 * partial completions.
1087 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1088 struct mmc_card *card,
1089 struct request *req)
1091 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1092 /* Legacy mode imposes restrictions on transfers. */
1093 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1094 brq->data.blocks = 1;
1096 if (brq->data.blocks > card->ext_csd.rel_sectors)
1097 brq->data.blocks = card->ext_csd.rel_sectors;
1098 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1099 brq->data.blocks = 1;
1103 #define CMD_ERRORS \
1104 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1105 R1_ADDRESS_ERROR | /* Misaligned address */ \
1106 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1107 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1108 R1_CC_ERROR | /* Card controller error */ \
1109 R1_ERROR) /* General/unknown error */
1111 static int mmc_blk_err_check(struct mmc_card *card,
1112 struct mmc_async_req *areq)
1114 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1116 struct mmc_blk_request *brq = &mq_mrq->brq;
1117 struct request *req = mq_mrq->req;
1118 int ecc_err = 0, gen_err = 0;
1121 * sbc.error indicates a problem with the set block count
1122 * command. No data will have been transferred.
1124 * cmd.error indicates a problem with the r/w command. No
1125 * data will have been transferred.
1127 * stop.error indicates a problem with the stop command. Data
1128 * may have been transferred, or may still be transferring.
1130 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1132 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
1134 return MMC_BLK_RETRY;
1136 return MMC_BLK_ABORT;
1138 return MMC_BLK_NOMEDIUM;
1145 * Check for errors relating to the execution of the
1146 * initial command - such as address errors. No data
1147 * has been transferred.
1149 if (brq->cmd.resp[0] & CMD_ERRORS) {
1150 pr_err("%s: r/w command failed, status = %#x\n",
1151 req->rq_disk->disk_name, brq->cmd.resp[0]);
1152 return MMC_BLK_ABORT;
1156 * Everything else is either success, or a data error of some
1157 * kind. If it was a write, we may have transitioned to
1158 * program mode, which we have to wait for it to complete.
1160 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1162 unsigned long timeout;
1164 /* Check stop command response */
1165 if (brq->stop.resp[0] & R1_ERROR) {
1166 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1167 req->rq_disk->disk_name, __func__,
1172 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
1174 int err = get_card_status(card, &status, 5);
1176 pr_err("%s: error %d requesting status\n",
1177 req->rq_disk->disk_name, err);
1178 return MMC_BLK_CMD_ERR;
1181 if (status & R1_ERROR) {
1182 pr_err("%s: %s: general error sending status command, card status %#x\n",
1183 req->rq_disk->disk_name, __func__,
1188 /* Timeout if the device never becomes ready for data
1189 * and never leaves the program state.
1191 if (time_after(jiffies, timeout)) {
1192 pr_err("%s: Card stuck in programming state!"\
1193 " %s %s\n", mmc_hostname(card->host),
1194 req->rq_disk->disk_name, __func__);
1196 return MMC_BLK_CMD_ERR;
1199 * Some cards mishandle the status bits,
1200 * so make sure to check both the busy
1201 * indication and the card state.
1203 } while (!(status & R1_READY_FOR_DATA) ||
1204 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1207 /* if general error occurs, retry the write operation. */
1209 pr_warn("%s: retrying write for general error\n",
1210 req->rq_disk->disk_name);
1211 return MMC_BLK_RETRY;
1214 if (brq->data.error) {
1215 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1216 req->rq_disk->disk_name, brq->data.error,
1217 (unsigned)blk_rq_pos(req),
1218 (unsigned)blk_rq_sectors(req),
1219 brq->cmd.resp[0], brq->stop.resp[0]);
1221 if (rq_data_dir(req) == READ) {
1223 return MMC_BLK_ECC_ERR;
1224 return MMC_BLK_DATA_ERR;
1226 return MMC_BLK_CMD_ERR;
1230 if (!brq->data.bytes_xfered)
1231 return MMC_BLK_RETRY;
1233 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1234 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1235 return MMC_BLK_PARTIAL;
1237 return MMC_BLK_SUCCESS;
1240 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1241 return MMC_BLK_PARTIAL;
1243 return MMC_BLK_SUCCESS;
1246 static int mmc_blk_packed_err_check(struct mmc_card *card,
1247 struct mmc_async_req *areq)
1249 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1251 struct request *req = mq_rq->req;
1252 struct mmc_packed *packed = mq_rq->packed;
1253 int err, check, status;
1259 check = mmc_blk_err_check(card, areq);
1260 err = get_card_status(card, &status, 0);
1262 pr_err("%s: error %d sending status command\n",
1263 req->rq_disk->disk_name, err);
1264 return MMC_BLK_ABORT;
1267 if (status & R1_EXCEPTION_EVENT) {
1268 ext_csd = kzalloc(512, GFP_KERNEL);
1270 pr_err("%s: unable to allocate buffer for ext_csd\n",
1271 req->rq_disk->disk_name);
1275 err = mmc_send_ext_csd(card, ext_csd);
1277 pr_err("%s: error %d sending ext_csd\n",
1278 req->rq_disk->disk_name, err);
1279 check = MMC_BLK_ABORT;
1283 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1284 EXT_CSD_PACKED_FAILURE) &&
1285 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1286 EXT_CSD_PACKED_GENERIC_ERROR)) {
1287 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1288 EXT_CSD_PACKED_INDEXED_ERROR) {
1289 packed->idx_failure =
1290 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1291 check = MMC_BLK_PARTIAL;
1293 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1294 "failure index: %d\n",
1295 req->rq_disk->disk_name, packed->nr_entries,
1296 packed->blocks, packed->idx_failure);
1305 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1306 struct mmc_card *card,
1308 struct mmc_queue *mq)
1310 u32 readcmd, writecmd;
1311 struct mmc_blk_request *brq = &mqrq->brq;
1312 struct request *req = mqrq->req;
1313 struct mmc_blk_data *md = mq->data;
1317 * Reliable writes are used to implement Forced Unit Access and
1318 * REQ_META accesses, and are supported only on MMCs.
1320 * XXX: this really needs a good explanation of why REQ_META
1321 * is treated special.
1323 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1324 (req->cmd_flags & REQ_META)) &&
1325 (rq_data_dir(req) == WRITE) &&
1326 (md->flags & MMC_BLK_REL_WR);
1328 memset(brq, 0, sizeof(struct mmc_blk_request));
1329 brq->mrq.cmd = &brq->cmd;
1330 brq->mrq.data = &brq->data;
1332 brq->cmd.arg = blk_rq_pos(req);
1333 if (!mmc_card_blockaddr(card))
1335 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1336 brq->data.blksz = 512;
1337 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1339 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1340 brq->data.blocks = blk_rq_sectors(req);
1343 * The block layer doesn't support all sector count
1344 * restrictions, so we need to be prepared for too big
1347 if (brq->data.blocks > card->host->max_blk_count)
1348 brq->data.blocks = card->host->max_blk_count;
1350 if (brq->data.blocks > 1) {
1352 * After a read error, we redo the request one sector
1353 * at a time in order to accurately determine which
1354 * sectors can be read successfully.
1357 brq->data.blocks = 1;
1359 /* Some controllers can't do multiblock reads due to hw bugs */
1360 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1361 rq_data_dir(req) == READ)
1362 brq->data.blocks = 1;
1365 if (brq->data.blocks > 1 || do_rel_wr) {
1366 /* SPI multiblock writes terminate using a special
1367 * token, not a STOP_TRANSMISSION request.
1369 if (!mmc_host_is_spi(card->host) ||
1370 rq_data_dir(req) == READ)
1371 brq->mrq.stop = &brq->stop;
1372 readcmd = MMC_READ_MULTIPLE_BLOCK;
1373 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1375 brq->mrq.stop = NULL;
1376 readcmd = MMC_READ_SINGLE_BLOCK;
1377 writecmd = MMC_WRITE_BLOCK;
1379 if (rq_data_dir(req) == READ) {
1380 brq->cmd.opcode = readcmd;
1381 brq->data.flags |= MMC_DATA_READ;
1383 brq->cmd.opcode = writecmd;
1384 brq->data.flags |= MMC_DATA_WRITE;
1388 mmc_apply_rel_rw(brq, card, req);
1391 * Data tag is used only during writing meta data to speed
1392 * up write and any subsequent read of this meta data
1394 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1395 (req->cmd_flags & REQ_META) &&
1396 (rq_data_dir(req) == WRITE) &&
1397 ((brq->data.blocks * brq->data.blksz) >=
1398 card->ext_csd.data_tag_unit_size);
1401 * Pre-defined multi-block transfers are preferable to
1402 * open ended-ones (and necessary for reliable writes).
1403 * However, it is not sufficient to just send CMD23,
1404 * and avoid the final CMD12, as on an error condition
1405 * CMD12 (stop) needs to be sent anyway. This, coupled
1406 * with Auto-CMD23 enhancements provided by some
1407 * hosts, means that the complexity of dealing
1408 * with this is best left to the host. If CMD23 is
1409 * supported by card and host, we'll fill sbc in and let
1410 * the host deal with handling it correctly. This means
1411 * that for hosts that don't expose MMC_CAP_CMD23, no
1412 * change of behavior will be observed.
1414 * N.B: Some MMC cards experience perf degradation.
1415 * We'll avoid using CMD23-bounded multiblock writes for
1416 * these, while retaining features like reliable writes.
1418 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1419 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1421 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1422 brq->sbc.arg = brq->data.blocks |
1423 (do_rel_wr ? (1 << 31) : 0) |
1424 (do_data_tag ? (1 << 29) : 0);
1425 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1426 brq->mrq.sbc = &brq->sbc;
1429 mmc_set_data_timeout(&brq->data, card);
1431 brq->data.sg = mqrq->sg;
1432 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1435 * Adjust the sg list so it is the same size as the
1438 if (brq->data.blocks != blk_rq_sectors(req)) {
1439 int i, data_size = brq->data.blocks << 9;
1440 struct scatterlist *sg;
1442 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1443 data_size -= sg->length;
1444 if (data_size <= 0) {
1445 sg->length += data_size;
1450 brq->data.sg_len = i;
1453 mqrq->mmc_active.mrq = &brq->mrq;
1454 mqrq->mmc_active.err_check = mmc_blk_err_check;
1456 mmc_queue_bounce_pre(mqrq);
1459 static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1460 struct mmc_card *card)
1462 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1463 unsigned int max_seg_sz = queue_max_segment_size(q);
1464 unsigned int len, nr_segs = 0;
1467 len = min(hdr_sz, max_seg_sz);
1475 static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1477 struct request_queue *q = mq->queue;
1478 struct mmc_card *card = mq->card;
1479 struct request *cur = req, *next = NULL;
1480 struct mmc_blk_data *md = mq->data;
1481 struct mmc_queue_req *mqrq = mq->mqrq_cur;
1482 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1483 unsigned int req_sectors = 0, phys_segments = 0;
1484 unsigned int max_blk_count, max_phys_segs;
1485 bool put_back = true;
1486 u8 max_packed_rw = 0;
1489 if (!(md->flags & MMC_BLK_PACKED_CMD))
1492 if ((rq_data_dir(cur) == WRITE) &&
1493 mmc_host_packed_wr(card->host))
1494 max_packed_rw = card->ext_csd.max_packed_writes;
1496 if (max_packed_rw == 0)
1499 if (mmc_req_rel_wr(cur) &&
1500 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1503 if (mmc_large_sector(card) &&
1504 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1507 mmc_blk_clear_packed(mqrq);
1509 max_blk_count = min(card->host->max_blk_count,
1510 card->host->max_req_size >> 9);
1511 if (unlikely(max_blk_count > 0xffff))
1512 max_blk_count = 0xffff;
1514 max_phys_segs = queue_max_segments(q);
1515 req_sectors += blk_rq_sectors(cur);
1516 phys_segments += cur->nr_phys_segments;
1518 if (rq_data_dir(cur) == WRITE) {
1519 req_sectors += mmc_large_sector(card) ? 8 : 1;
1520 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1524 if (reqs >= max_packed_rw - 1) {
1529 spin_lock_irq(q->queue_lock);
1530 next = blk_fetch_request(q);
1531 spin_unlock_irq(q->queue_lock);
1537 if (mmc_large_sector(card) &&
1538 !IS_ALIGNED(blk_rq_sectors(next), 8))
1541 if (next->cmd_flags & REQ_DISCARD ||
1542 next->cmd_flags & REQ_FLUSH)
1545 if (rq_data_dir(cur) != rq_data_dir(next))
1548 if (mmc_req_rel_wr(next) &&
1549 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1552 req_sectors += blk_rq_sectors(next);
1553 if (req_sectors > max_blk_count)
1556 phys_segments += next->nr_phys_segments;
1557 if (phys_segments > max_phys_segs)
1560 list_add_tail(&next->queuelist, &mqrq->packed->list);
1566 spin_lock_irq(q->queue_lock);
1567 blk_requeue_request(q, next);
1568 spin_unlock_irq(q->queue_lock);
1572 list_add(&req->queuelist, &mqrq->packed->list);
1573 mqrq->packed->nr_entries = ++reqs;
1574 mqrq->packed->retries = reqs;
1579 mqrq->cmd_type = MMC_PACKED_NONE;
1583 static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1584 struct mmc_card *card,
1585 struct mmc_queue *mq)
1587 struct mmc_blk_request *brq = &mqrq->brq;
1588 struct request *req = mqrq->req;
1589 struct request *prq;
1590 struct mmc_blk_data *md = mq->data;
1591 struct mmc_packed *packed = mqrq->packed;
1592 bool do_rel_wr, do_data_tag;
1593 u32 *packed_cmd_hdr;
1599 mqrq->cmd_type = MMC_PACKED_WRITE;
1601 packed->idx_failure = MMC_PACKED_NR_IDX;
1603 packed_cmd_hdr = packed->cmd_hdr;
1604 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
1605 packed_cmd_hdr[0] = (packed->nr_entries << 16) |
1606 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1607 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1610 * Argument for each entry of packed group
1612 list_for_each_entry(prq, &packed->list, queuelist) {
1613 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1614 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1615 (prq->cmd_flags & REQ_META) &&
1616 (rq_data_dir(prq) == WRITE) &&
1617 ((brq->data.blocks * brq->data.blksz) >=
1618 card->ext_csd.data_tag_unit_size);
1619 /* Argument of CMD23 */
1620 packed_cmd_hdr[(i * 2)] =
1621 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1622 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1623 blk_rq_sectors(prq);
1624 /* Argument of CMD18 or CMD25 */
1625 packed_cmd_hdr[((i * 2)) + 1] =
1626 mmc_card_blockaddr(card) ?
1627 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1628 packed->blocks += blk_rq_sectors(prq);
1632 memset(brq, 0, sizeof(struct mmc_blk_request));
1633 brq->mrq.cmd = &brq->cmd;
1634 brq->mrq.data = &brq->data;
1635 brq->mrq.sbc = &brq->sbc;
1636 brq->mrq.stop = &brq->stop;
1638 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1639 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1640 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1642 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1643 brq->cmd.arg = blk_rq_pos(req);
1644 if (!mmc_card_blockaddr(card))
1646 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1648 brq->data.blksz = 512;
1649 brq->data.blocks = packed->blocks + hdr_blocks;
1650 brq->data.flags |= MMC_DATA_WRITE;
1652 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1654 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1656 mmc_set_data_timeout(&brq->data, card);
1658 brq->data.sg = mqrq->sg;
1659 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1661 mqrq->mmc_active.mrq = &brq->mrq;
1662 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1664 mmc_queue_bounce_pre(mqrq);
1667 static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1668 struct mmc_blk_request *brq, struct request *req,
1671 struct mmc_queue_req *mq_rq;
1672 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1675 * If this is an SD card and we're writing, we can first
1676 * mark the known good sectors as ok.
1678 * If the card is not SD, we can still ok written sectors
1679 * as reported by the controller (which might be less than
1680 * the real number of written sectors, but never more).
1682 if (mmc_card_sd(card)) {
1685 blocks = mmc_sd_num_wr_blocks(card);
1686 if (blocks != (u32)-1) {
1687 ret = blk_end_request(req, 0, blocks << 9);
1690 if (!mmc_packed_cmd(mq_rq->cmd_type))
1691 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1696 static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1698 struct request *prq;
1699 struct mmc_packed *packed = mq_rq->packed;
1700 int idx = packed->idx_failure, i = 0;
1705 while (!list_empty(&packed->list)) {
1706 prq = list_entry_rq(packed->list.next);
1708 /* retry from error index */
1709 packed->nr_entries -= idx;
1713 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1714 list_del_init(&prq->queuelist);
1715 mmc_blk_clear_packed(mq_rq);
1719 list_del_init(&prq->queuelist);
1720 blk_end_request(prq, 0, blk_rq_bytes(prq));
1724 mmc_blk_clear_packed(mq_rq);
1728 static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1730 struct request *prq;
1731 struct mmc_packed *packed = mq_rq->packed;
1735 while (!list_empty(&packed->list)) {
1736 prq = list_entry_rq(packed->list.next);
1737 list_del_init(&prq->queuelist);
1738 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1741 mmc_blk_clear_packed(mq_rq);
1744 static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1745 struct mmc_queue_req *mq_rq)
1747 struct request *prq;
1748 struct request_queue *q = mq->queue;
1749 struct mmc_packed *packed = mq_rq->packed;
1753 while (!list_empty(&packed->list)) {
1754 prq = list_entry_rq(packed->list.prev);
1755 if (prq->queuelist.prev != &packed->list) {
1756 list_del_init(&prq->queuelist);
1757 spin_lock_irq(q->queue_lock);
1758 blk_requeue_request(mq->queue, prq);
1759 spin_unlock_irq(q->queue_lock);
1761 list_del_init(&prq->queuelist);
1765 mmc_blk_clear_packed(mq_rq);
1768 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
1770 struct mmc_blk_data *md = mq->data;
1771 struct mmc_card *card = md->queue.card;
1772 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
1773 int ret = 1, disable_multi = 0, retry = 0, type;
1774 enum mmc_blk_status status;
1775 struct mmc_queue_req *mq_rq;
1776 struct request *req = rqc;
1777 struct mmc_async_req *areq;
1778 const u8 packed_nr = 2;
1781 if (!rqc && !mq->mqrq_prev->req)
1785 reqs = mmc_blk_prep_packed_list(mq, rqc);
1790 * When 4KB native sector is enabled, only 8 blocks
1791 * multiple read or write is allowed
1793 if ((brq->data.blocks & 0x07) &&
1794 (card->ext_csd.data_sector_size == 4096)) {
1795 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1796 req->rq_disk->disk_name);
1797 mq_rq = mq->mqrq_cur;
1801 if (reqs >= packed_nr)
1802 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1805 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1806 areq = &mq->mqrq_cur->mmc_active;
1809 areq = mmc_start_req(card->host, areq, (int *) &status);
1811 if (status == MMC_BLK_NEW_REQUEST)
1812 mq->flags |= MMC_QUEUE_NEW_REQUEST;
1816 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1819 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1820 mmc_queue_bounce_post(mq_rq);
1823 case MMC_BLK_SUCCESS:
1824 case MMC_BLK_PARTIAL:
1826 * A block was successfully transferred.
1828 mmc_blk_reset_success(md, type);
1830 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1831 ret = mmc_blk_end_packed_req(mq_rq);
1834 ret = blk_end_request(req, 0,
1835 brq->data.bytes_xfered);
1839 * If the blk_end_request function returns non-zero even
1840 * though all data has been transferred and no errors
1841 * were returned by the host controller, it's a bug.
1843 if (status == MMC_BLK_SUCCESS && ret) {
1844 pr_err("%s BUG rq_tot %d d_xfer %d\n",
1845 __func__, blk_rq_bytes(req),
1846 brq->data.bytes_xfered);
1851 case MMC_BLK_CMD_ERR:
1852 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1853 if (!mmc_blk_reset(md, card->host, type))
1861 if (!mmc_blk_reset(md, card->host, type))
1864 case MMC_BLK_DATA_ERR: {
1867 err = mmc_blk_reset(md, card->host, type);
1870 if (err == -ENODEV ||
1871 mmc_packed_cmd(mq_rq->cmd_type))
1875 case MMC_BLK_ECC_ERR:
1876 if (brq->data.blocks > 1) {
1877 /* Redo read one sector at a time */
1878 pr_warning("%s: retrying using single block read\n",
1879 req->rq_disk->disk_name);
1884 * After an error, we redo I/O one sector at a
1885 * time, so we only reach here after trying to
1886 * read a single sector.
1888 ret = blk_end_request(req, -EIO,
1893 case MMC_BLK_NOMEDIUM:
1896 pr_err("%s: Unhandled return value (%d)",
1897 req->rq_disk->disk_name, status);
1902 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1903 if (!mq_rq->packed->retries)
1905 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1906 mmc_start_req(card->host,
1907 &mq_rq->mmc_active, NULL);
1911 * In case of a incomplete request
1912 * prepare it again and resend.
1914 mmc_blk_rw_rq_prep(mq_rq, card,
1916 mmc_start_req(card->host,
1917 &mq_rq->mmc_active, NULL);
1925 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1926 mmc_blk_abort_packed_req(mq_rq);
1928 if (mmc_card_removed(card))
1929 req->cmd_flags |= REQ_QUIET;
1931 ret = blk_end_request(req, -EIO,
1932 blk_rq_cur_bytes(req));
1937 if (mmc_card_removed(card)) {
1938 rqc->cmd_flags |= REQ_QUIET;
1939 blk_end_request_all(rqc, -EIO);
1942 * If current request is packed, it needs to put back.
1944 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
1945 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1947 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1948 mmc_start_req(card->host,
1949 &mq->mqrq_cur->mmc_active, NULL);
1956 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1959 struct mmc_blk_data *md = mq->data;
1960 struct mmc_card *card = md->queue.card;
1961 struct mmc_host *host = card->host;
1962 unsigned long flags;
1963 unsigned int cmd_flags = req ? req->cmd_flags : 0;
1965 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1966 if (mmc_bus_needs_resume(card->host))
1967 mmc_resume_bus(card->host);
1970 if (req && !mq->mqrq_prev->req)
1971 /* claim host only for the first request */
1974 ret = mmc_blk_part_switch(card, md);
1977 blk_end_request_all(req, -EIO);
1983 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
1984 if (cmd_flags & REQ_DISCARD) {
1985 /* complete ongoing async transfer before issuing discard */
1986 if (card->host->areq)
1987 mmc_blk_issue_rw_rq(mq, NULL);
1988 if (req->cmd_flags & REQ_SECURE &&
1989 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
1990 ret = mmc_blk_issue_secdiscard_rq(mq, req);
1992 ret = mmc_blk_issue_discard_rq(mq, req);
1993 } else if (cmd_flags & REQ_FLUSH) {
1994 /* complete ongoing async transfer before issuing flush */
1995 if (card->host->areq)
1996 mmc_blk_issue_rw_rq(mq, NULL);
1997 ret = mmc_blk_issue_flush(mq, req);
1999 if (!req && host->areq) {
2000 spin_lock_irqsave(&host->context_info.lock, flags);
2001 host->context_info.is_waiting_last_req = true;
2002 spin_unlock_irqrestore(&host->context_info.lock, flags);
2004 ret = mmc_blk_issue_rw_rq(mq, req);
2008 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
2009 (cmd_flags & MMC_REQ_SPECIAL_MASK))
2011 * Release host when there are no more requests
2012 * and after special request(discard, flush) is done.
2013 * In case sepecial request, there is no reentry to
2014 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2020 static inline int mmc_blk_readonly(struct mmc_card *card)
2022 return mmc_card_readonly(card) ||
2023 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2026 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2027 struct device *parent,
2030 const char *subname,
2033 struct mmc_blk_data *md;
2036 devidx = find_first_zero_bit(dev_use, max_devices);
2037 if (devidx >= max_devices)
2038 return ERR_PTR(-ENOSPC);
2039 __set_bit(devidx, dev_use);
2041 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2048 * !subname implies we are creating main mmc_blk_data that will be
2049 * associated with mmc_card with mmc_set_drvdata. Due to device
2050 * partitions, devidx will not coincide with a per-physical card
2051 * index anymore so we keep track of a name index.
2054 md->name_idx = find_first_zero_bit(name_use, max_devices);
2055 __set_bit(md->name_idx, name_use);
2057 md->name_idx = ((struct mmc_blk_data *)
2058 dev_to_disk(parent)->private_data)->name_idx;
2060 md->area_type = area_type;
2063 * Set the read-only status based on the supported commands
2064 * and the write protect switch.
2066 md->read_only = mmc_blk_readonly(card);
2068 md->disk = alloc_disk(perdev_minors);
2069 if (md->disk == NULL) {
2074 spin_lock_init(&md->lock);
2075 INIT_LIST_HEAD(&md->part);
2078 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2082 md->queue.issue_fn = mmc_blk_issue_rq;
2083 md->queue.data = md;
2085 md->disk->major = MMC_BLOCK_MAJOR;
2086 md->disk->first_minor = devidx * perdev_minors;
2087 md->disk->fops = &mmc_bdops;
2088 md->disk->private_data = md;
2089 md->disk->queue = md->queue.queue;
2090 md->disk->driverfs_dev = parent;
2091 set_disk_ro(md->disk, md->read_only || default_ro);
2092 md->disk->flags = GENHD_FL_EXT_DEVT;
2093 if (area_type & MMC_BLK_DATA_AREA_RPMB)
2094 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
2097 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2099 * - be set for removable media with permanent block devices
2100 * - be unset for removable block devices with permanent media
2102 * Since MMC block devices clearly fall under the second
2103 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2104 * should use the block device creation/destruction hotplug
2105 * messages to tell when the card is present.
2108 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2109 "mmcblk%d%s", md->name_idx, subname ? subname : "");
2111 if (mmc_card_mmc(card))
2112 blk_queue_logical_block_size(md->queue.queue,
2113 card->ext_csd.data_sector_size);
2115 blk_queue_logical_block_size(md->queue.queue, 512);
2117 set_capacity(md->disk, size);
2119 if (mmc_host_cmd23(card->host)) {
2120 if (mmc_card_mmc(card) ||
2121 (mmc_card_sd(card) &&
2122 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2123 md->flags |= MMC_BLK_CMD23;
2126 if (mmc_card_mmc(card) &&
2127 md->flags & MMC_BLK_CMD23 &&
2128 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2129 card->ext_csd.rel_sectors)) {
2130 md->flags |= MMC_BLK_REL_WR;
2131 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2134 if (mmc_card_mmc(card) &&
2135 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2136 (md->flags & MMC_BLK_CMD23) &&
2137 card->ext_csd.packed_event_en) {
2138 if (!mmc_packed_init(&md->queue, card))
2139 md->flags |= MMC_BLK_PACKED_CMD;
2149 return ERR_PTR(ret);
2152 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2155 struct mmc_blk_data *md;
2157 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2159 * The EXT_CSD sector count is in number or 512 byte
2162 size = card->ext_csd.sectors;
2165 * The CSD capacity field is in units of read_blkbits.
2166 * set_capacity takes units of 512 bytes.
2168 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2171 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2172 MMC_BLK_DATA_AREA_MAIN);
2176 static int mmc_blk_alloc_part(struct mmc_card *card,
2177 struct mmc_blk_data *md,
2178 unsigned int part_type,
2181 const char *subname,
2185 struct mmc_blk_data *part_md;
2187 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2188 subname, area_type);
2189 if (IS_ERR(part_md))
2190 return PTR_ERR(part_md);
2191 part_md->part_type = part_type;
2192 list_add(&part_md->part, &md->part);
2194 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2195 cap_str, sizeof(cap_str));
2196 pr_info("%s: %s %s partition %u %s\n",
2197 part_md->disk->disk_name, mmc_card_id(card),
2198 mmc_card_name(card), part_md->part_type, cap_str);
2202 /* MMC Physical partitions consist of two boot partitions and
2203 * up to four general purpose partitions.
2204 * For each partition enabled in EXT_CSD a block device will be allocatedi
2205 * to provide access to the partition.
2208 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2212 if (!mmc_card_mmc(card))
2215 for (idx = 0; idx < card->nr_parts; idx++) {
2216 if (card->part[idx].size) {
2217 ret = mmc_blk_alloc_part(card, md,
2218 card->part[idx].part_cfg,
2219 card->part[idx].size >> 9,
2220 card->part[idx].force_ro,
2221 card->part[idx].name,
2222 card->part[idx].area_type);
2231 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2233 struct mmc_card *card;
2237 * Flush remaining requests and free queues. It
2238 * is freeing the queue that stops new requests
2239 * from being accepted.
2241 card = md->queue.card;
2242 mmc_cleanup_queue(&md->queue);
2243 if (md->flags & MMC_BLK_PACKED_CMD)
2244 mmc_packed_clean(&md->queue);
2245 if (md->disk->flags & GENHD_FL_UP) {
2246 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2247 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2248 card->ext_csd.boot_ro_lockable)
2249 device_remove_file(disk_to_dev(md->disk),
2250 &md->power_ro_lock);
2252 del_gendisk(md->disk);
2258 static void mmc_blk_remove_parts(struct mmc_card *card,
2259 struct mmc_blk_data *md)
2261 struct list_head *pos, *q;
2262 struct mmc_blk_data *part_md;
2264 __clear_bit(md->name_idx, name_use);
2265 list_for_each_safe(pos, q, &md->part) {
2266 part_md = list_entry(pos, struct mmc_blk_data, part);
2268 mmc_blk_remove_req(part_md);
2272 static int mmc_add_disk(struct mmc_blk_data *md)
2275 struct mmc_card *card = md->queue.card;
2278 md->force_ro.show = force_ro_show;
2279 md->force_ro.store = force_ro_store;
2280 sysfs_attr_init(&md->force_ro.attr);
2281 md->force_ro.attr.name = "force_ro";
2282 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2283 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2287 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2288 card->ext_csd.boot_ro_lockable) {
2291 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2294 mode = S_IRUGO | S_IWUSR;
2296 md->power_ro_lock.show = power_ro_lock_show;
2297 md->power_ro_lock.store = power_ro_lock_store;
2298 sysfs_attr_init(&md->power_ro_lock.attr);
2299 md->power_ro_lock.attr.mode = mode;
2300 md->power_ro_lock.attr.name =
2301 "ro_lock_until_next_power_on";
2302 ret = device_create_file(disk_to_dev(md->disk),
2303 &md->power_ro_lock);
2305 goto power_ro_lock_fail;
2310 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2312 del_gendisk(md->disk);
2317 #define CID_MANFID_SANDISK 0x2
2318 #define CID_MANFID_TOSHIBA 0x11
2319 #define CID_MANFID_MICRON 0x13
2320 #define CID_MANFID_SAMSUNG 0x15
2321 #define CID_MANFID_HYNIX 0x90
2323 static const struct mmc_fixup blk_fixups[] =
2325 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2326 MMC_QUIRK_INAND_CMD38),
2327 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2328 MMC_QUIRK_INAND_CMD38),
2329 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2330 MMC_QUIRK_INAND_CMD38),
2331 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2332 MMC_QUIRK_INAND_CMD38),
2333 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2334 MMC_QUIRK_INAND_CMD38),
2337 * Some MMC cards experience performance degradation with CMD23
2338 * instead of CMD12-bounded multiblock transfers. For now we'll
2339 * black list what's bad...
2340 * - Certain Toshiba cards.
2342 * N.B. This doesn't affect SD cards.
2344 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2345 MMC_QUIRK_BLK_NO_CMD23),
2346 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2347 MMC_QUIRK_BLK_NO_CMD23),
2348 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2349 MMC_QUIRK_BLK_NO_CMD23),
2352 * Some Micron MMC cards needs longer data read timeout than
2355 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
2356 MMC_QUIRK_LONG_READ_TIME),
2359 * On these Samsung MoviNAND parts, performing secure erase or
2360 * secure trim can result in unrecoverable corruption due to a
2363 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2364 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2365 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2366 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2367 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2368 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2369 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2370 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2371 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2372 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2373 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2374 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2375 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2376 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2377 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2378 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2380 /* Hynix 4.41 iNAND execute trim will lead boot up failed. */
2381 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_HYNIX, CID_OEMID_ANY, add_quirk_mmc,
2382 MMC_QUIRK_TRIM_UNSTABLE),
2387 #if defined(CONFIG_MMC_DW_ROCKCHIP)
2388 extern struct mmc_card *this_card;
2391 static int mmc_blk_probe(struct mmc_card *card)
2393 struct mmc_blk_data *md, *part_md;
2397 * Check that the card supports the command class(es) we need.
2399 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2402 md = mmc_blk_alloc(card);
2406 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
2407 cap_str, sizeof(cap_str));
2408 pr_info("%s: %s %s %s %s\n",
2409 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2410 cap_str, md->read_only ? "(ro)" : "");
2412 if (mmc_blk_alloc_parts(card, md))
2415 mmc_set_drvdata(card, md);
2416 mmc_fixup_device(card, blk_fixups);
2418 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2419 mmc_set_bus_resume_policy(card->host, 1);
2421 #if defined(CONFIG_MMC_DW_ROCKCHIP)
2422 if(card->host->restrict_caps & RESTRICT_CARD_TYPE_EMMC){
2424 md->disk->emmc_disk = 1;
2426 md->disk->emmc_disk = 0;
2429 if (mmc_add_disk(md))
2432 list_for_each_entry(part_md, &md->part, part) {
2433 if (mmc_add_disk(part_md))
2437 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2438 pm_runtime_use_autosuspend(&card->dev);
2441 * Don't enable runtime PM for SD-combo cards here. Leave that
2442 * decision to be taken during the SDIO init sequence instead.
2444 if (card->type != MMC_TYPE_SD_COMBO) {
2445 pm_runtime_set_active(&card->dev);
2446 pm_runtime_enable(&card->dev);
2452 mmc_blk_remove_parts(card, md);
2453 mmc_blk_remove_req(md);
2457 static void mmc_blk_remove(struct mmc_card *card)
2459 struct mmc_blk_data *md = mmc_get_drvdata(card);
2461 #if defined(CONFIG_MMC_DW_ROCKCHIP)
2462 if(card->host->restrict_caps & RESTRICT_CARD_TYPE_EMMC)
2465 mmc_blk_remove_parts(card, md);
2466 pm_runtime_get_sync(&card->dev);
2467 mmc_claim_host(card->host);
2468 mmc_blk_part_switch(card, md);
2469 mmc_release_host(card->host);
2470 if (card->type != MMC_TYPE_SD_COMBO)
2471 pm_runtime_disable(&card->dev);
2472 pm_runtime_put_noidle(&card->dev);
2473 mmc_blk_remove_req(md);
2474 mmc_set_drvdata(card, NULL);
2475 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2476 mmc_set_bus_resume_policy(card->host, 0);
2480 static int _mmc_blk_suspend(struct mmc_card *card)
2482 struct mmc_blk_data *part_md;
2483 struct mmc_blk_data *md = mmc_get_drvdata(card);
2486 mmc_queue_suspend(&md->queue);
2487 list_for_each_entry(part_md, &md->part, part) {
2488 mmc_queue_suspend(&part_md->queue);
2494 static void mmc_blk_shutdown(struct mmc_card *card)
2496 _mmc_blk_suspend(card);
2500 static int mmc_blk_suspend(struct mmc_card *card)
2502 return _mmc_blk_suspend(card);
2505 static int mmc_blk_resume(struct mmc_card *card)
2507 struct mmc_blk_data *part_md;
2508 struct mmc_blk_data *md = mmc_get_drvdata(card);
2512 * Resume involves the card going into idle state,
2513 * so current partition is always the main one.
2515 md->part_curr = md->part_type;
2516 mmc_queue_resume(&md->queue);
2517 list_for_each_entry(part_md, &md->part, part) {
2518 mmc_queue_resume(&part_md->queue);
2524 #define mmc_blk_suspend NULL
2525 #define mmc_blk_resume NULL
2528 static struct mmc_driver mmc_driver = {
2532 .probe = mmc_blk_probe,
2533 .remove = mmc_blk_remove,
2534 .suspend = mmc_blk_suspend,
2535 .resume = mmc_blk_resume,
2536 .shutdown = mmc_blk_shutdown,
2539 static int __init mmc_blk_init(void)
2543 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2544 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2546 max_devices = 256 / perdev_minors;
2548 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2552 res = mmc_register_driver(&mmc_driver);
2558 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2563 static void __exit mmc_blk_exit(void)
2565 mmc_unregister_driver(&mmc_driver);
2566 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2569 module_init(mmc_blk_init);
2570 module_exit(mmc_blk_exit);
2572 MODULE_LICENSE("GPL");
2573 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");