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>
39 #include <linux/mmc/ioctl.h>
40 #include <linux/mmc/card.h>
41 #include <linux/mmc/host.h>
42 #include <linux/mmc/mmc.h>
43 #include <linux/mmc/sd.h>
45 #include <asm/uaccess.h>
49 MODULE_ALIAS("mmc:block");
52 #ifdef MODULE_PARAM_PREFIX
53 #undef MODULE_PARAM_PREFIX
55 #define MODULE_PARAM_PREFIX "mmcblk."
58 #define INAND_CMD38_ARG_EXT_CSD 113
59 #define INAND_CMD38_ARG_ERASE 0x00
60 #define INAND_CMD38_ARG_TRIM 0x01
61 #define INAND_CMD38_ARG_SECERASE 0x80
62 #define INAND_CMD38_ARG_SECTRIM1 0x81
63 #define INAND_CMD38_ARG_SECTRIM2 0x88
64 #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
65 #define MMC_SANITIZE_REQ_TIMEOUT 240000
66 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
68 #define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
69 (req->cmd_flags & REQ_META)) && \
70 (rq_data_dir(req) == WRITE))
71 #define PACKED_CMD_VER 0x01
72 #define PACKED_CMD_WR 0x02
74 static DEFINE_MUTEX(block_mutex);
77 * The defaults come from config options but can be overriden by module
80 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
83 * We've only got one major, so number of mmcblk devices is
84 * limited to (1 << 20) / number of minors per device. It is also
85 * currently limited by the size of the static bitmaps below.
87 static int max_devices;
89 #define MAX_DEVICES 256
91 /* TODO: Replace these with struct ida */
92 static DECLARE_BITMAP(dev_use, MAX_DEVICES);
93 static DECLARE_BITMAP(name_use, MAX_DEVICES);
96 * There is one mmc_blk_data per slot.
100 struct gendisk *disk;
101 struct mmc_queue queue;
102 struct list_head part;
105 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
106 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
107 #define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
110 unsigned int read_only;
111 unsigned int part_type;
112 unsigned int name_idx;
113 unsigned int reset_done;
114 #define MMC_BLK_READ BIT(0)
115 #define MMC_BLK_WRITE BIT(1)
116 #define MMC_BLK_DISCARD BIT(2)
117 #define MMC_BLK_SECDISCARD BIT(3)
120 * Only set in main mmc_blk_data associated
121 * with mmc_card with dev_set_drvdata, and keeps
122 * track of the current selected device partition.
124 unsigned int part_curr;
125 struct device_attribute force_ro;
126 struct device_attribute power_ro_lock;
130 static DEFINE_MUTEX(open_lock);
133 MMC_PACKED_NR_IDX = -1,
135 MMC_PACKED_NR_SINGLE,
138 module_param(perdev_minors, int, 0444);
139 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
141 static inline int mmc_blk_part_switch(struct mmc_card *card,
142 struct mmc_blk_data *md);
143 static int get_card_status(struct mmc_card *card, u32 *status, int retries);
145 static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
147 struct mmc_packed *packed = mqrq->packed;
151 mqrq->cmd_type = MMC_PACKED_NONE;
152 packed->nr_entries = MMC_PACKED_NR_ZERO;
153 packed->idx_failure = MMC_PACKED_NR_IDX;
158 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
160 struct mmc_blk_data *md;
162 mutex_lock(&open_lock);
163 md = disk->private_data;
164 if (md && md->usage == 0)
168 mutex_unlock(&open_lock);
173 static inline int mmc_get_devidx(struct gendisk *disk)
175 int devmaj = MAJOR(disk_devt(disk));
176 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
179 devidx = disk->first_minor / perdev_minors;
183 static void mmc_blk_put(struct mmc_blk_data *md)
185 mutex_lock(&open_lock);
187 if (md->usage == 0) {
188 int devidx = mmc_get_devidx(md->disk);
189 blk_cleanup_queue(md->queue.queue);
191 __clear_bit(devidx, dev_use);
196 mutex_unlock(&open_lock);
199 static ssize_t power_ro_lock_show(struct device *dev,
200 struct device_attribute *attr, char *buf)
203 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
204 struct mmc_card *card = md->queue.card;
207 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
209 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
212 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
219 static ssize_t power_ro_lock_store(struct device *dev,
220 struct device_attribute *attr, const char *buf, size_t count)
223 struct mmc_blk_data *md, *part_md;
224 struct mmc_card *card;
227 if (kstrtoul(buf, 0, &set))
233 md = mmc_blk_get(dev_to_disk(dev));
234 card = md->queue.card;
238 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
239 card->ext_csd.boot_ro_lock |
240 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
241 card->ext_csd.part_time);
243 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
245 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
250 pr_info("%s: Locking boot partition ro until next power on\n",
251 md->disk->disk_name);
252 set_disk_ro(md->disk, 1);
254 list_for_each_entry(part_md, &md->part, part)
255 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
256 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
257 set_disk_ro(part_md->disk, 1);
265 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
269 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
271 ret = snprintf(buf, PAGE_SIZE, "%d\n",
272 get_disk_ro(dev_to_disk(dev)) ^
278 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
279 const char *buf, size_t count)
283 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
284 unsigned long set = simple_strtoul(buf, &end, 0);
290 set_disk_ro(dev_to_disk(dev), set || md->read_only);
297 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
299 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
302 mutex_lock(&block_mutex);
305 check_disk_change(bdev);
308 if ((mode & FMODE_WRITE) && md->read_only) {
313 mutex_unlock(&block_mutex);
318 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
320 struct mmc_blk_data *md = disk->private_data;
322 mutex_lock(&block_mutex);
324 mutex_unlock(&block_mutex);
328 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
330 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
336 struct mmc_blk_ioc_data {
337 struct mmc_ioc_cmd ic;
342 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
343 struct mmc_ioc_cmd __user *user)
345 struct mmc_blk_ioc_data *idata;
348 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
354 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
359 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
360 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
365 if (!idata->buf_bytes)
368 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
374 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
375 idata->ic.data_ptr, idata->buf_bytes)) {
390 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
396 if (!status || !retries_max)
400 err = get_card_status(card, status, 5);
404 if (!R1_STATUS(*status) &&
405 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
406 break; /* RPMB programming operation complete */
409 * Rechedule to give the MMC device a chance to continue
410 * processing the previous command without being polled too
413 usleep_range(1000, 5000);
414 } while (++retry_count < retries_max);
416 if (retry_count == retries_max)
422 static int ioctl_do_sanitize(struct mmc_card *card)
426 if (!mmc_can_sanitize(card)) {
427 pr_warn("%s: %s - SANITIZE is not supported\n",
428 mmc_hostname(card->host), __func__);
433 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
434 mmc_hostname(card->host), __func__);
436 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
437 EXT_CSD_SANITIZE_START, 1,
438 MMC_SANITIZE_REQ_TIMEOUT);
441 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
442 mmc_hostname(card->host), __func__, err);
444 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
450 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
451 struct mmc_ioc_cmd __user *ic_ptr)
453 struct mmc_blk_ioc_data *idata;
454 struct mmc_blk_data *md;
455 struct mmc_card *card;
456 struct mmc_command cmd = {0};
457 struct mmc_data data = {0};
458 struct mmc_request mrq = {NULL};
459 struct scatterlist sg;
465 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
466 * whole block device, not on a partition. This prevents overspray
467 * between sibling partitions.
469 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
472 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
474 return PTR_ERR(idata);
476 md = mmc_blk_get(bdev->bd_disk);
482 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
485 card = md->queue.card;
491 cmd.opcode = idata->ic.opcode;
492 cmd.arg = idata->ic.arg;
493 cmd.flags = idata->ic.flags;
495 if (idata->buf_bytes) {
498 data.blksz = idata->ic.blksz;
499 data.blocks = idata->ic.blocks;
501 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
503 if (idata->ic.write_flag)
504 data.flags = MMC_DATA_WRITE;
506 data.flags = MMC_DATA_READ;
508 /* data.flags must already be set before doing this. */
509 mmc_set_data_timeout(&data, card);
511 /* Allow overriding the timeout_ns for empirical tuning. */
512 if (idata->ic.data_timeout_ns)
513 data.timeout_ns = idata->ic.data_timeout_ns;
515 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
517 * Pretend this is a data transfer and rely on the
518 * host driver to compute timeout. When all host
519 * drivers support cmd.cmd_timeout for R1B, this
523 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
525 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
535 err = mmc_blk_part_switch(card, md);
539 if (idata->ic.is_acmd) {
540 err = mmc_app_cmd(card->host, card);
546 err = mmc_set_blockcount(card, data.blocks,
547 idata->ic.write_flag & (1 << 31));
552 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
553 (cmd.opcode == MMC_SWITCH)) {
554 err = ioctl_do_sanitize(card);
557 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
563 mmc_wait_for_req(card->host, &mrq);
566 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
567 __func__, cmd.error);
572 dev_err(mmc_dev(card->host), "%s: data error %d\n",
573 __func__, data.error);
579 * According to the SD specs, some commands require a delay after
580 * issuing the command.
582 if (idata->ic.postsleep_min_us)
583 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
585 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
590 if (!idata->ic.write_flag) {
591 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
592 idata->buf, idata->buf_bytes)) {
600 * Ensure RPMB command has completed by polling CMD13
603 err = ioctl_rpmb_card_status_poll(card, &status, 5);
605 dev_err(mmc_dev(card->host),
606 "%s: Card Status=0x%08X, error %d\n",
607 __func__, status, err);
621 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
622 unsigned int cmd, unsigned long arg)
625 if (cmd == MMC_IOC_CMD)
626 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
631 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
632 unsigned int cmd, unsigned long arg)
634 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
638 static const struct block_device_operations mmc_bdops = {
639 .open = mmc_blk_open,
640 .release = mmc_blk_release,
641 .getgeo = mmc_blk_getgeo,
642 .owner = THIS_MODULE,
643 .ioctl = mmc_blk_ioctl,
645 .compat_ioctl = mmc_blk_compat_ioctl,
649 static inline int mmc_blk_part_switch(struct mmc_card *card,
650 struct mmc_blk_data *md)
653 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
655 if (main_md->part_curr == md->part_type)
658 if (mmc_card_mmc(card)) {
659 u8 part_config = card->ext_csd.part_config;
661 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
662 part_config |= md->part_type;
664 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
665 EXT_CSD_PART_CONFIG, part_config,
666 card->ext_csd.part_time);
670 card->ext_csd.part_config = part_config;
673 main_md->part_curr = md->part_type;
677 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
683 struct mmc_request mrq = {NULL};
684 struct mmc_command cmd = {0};
685 struct mmc_data data = {0};
687 struct scatterlist sg;
689 cmd.opcode = MMC_APP_CMD;
690 cmd.arg = card->rca << 16;
691 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
693 err = mmc_wait_for_cmd(card->host, &cmd, 0);
696 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
699 memset(&cmd, 0, sizeof(struct mmc_command));
701 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
703 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
707 data.flags = MMC_DATA_READ;
710 mmc_set_data_timeout(&data, card);
715 blocks = kmalloc(4, GFP_KERNEL);
719 sg_init_one(&sg, blocks, 4);
721 mmc_wait_for_req(card->host, &mrq);
723 result = ntohl(*blocks);
726 if (cmd.error || data.error)
732 static int get_card_status(struct mmc_card *card, u32 *status, int retries)
734 struct mmc_command cmd = {0};
737 cmd.opcode = MMC_SEND_STATUS;
738 if (!mmc_host_is_spi(card->host))
739 cmd.arg = card->rca << 16;
740 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
741 err = mmc_wait_for_cmd(card->host, &cmd, retries);
743 *status = cmd.resp[0];
747 static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
748 bool hw_busy_detect, struct request *req, int *gen_err)
750 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
755 err = get_card_status(card, &status, 5);
757 pr_err("%s: error %d requesting status\n",
758 req->rq_disk->disk_name, err);
762 if (status & R1_ERROR) {
763 pr_err("%s: %s: error sending status cmd, status %#x\n",
764 req->rq_disk->disk_name, __func__, status);
768 /* We may rely on the host hw to handle busy detection.*/
769 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
774 * Timeout if the device never becomes ready for data and never
775 * leaves the program state.
777 if (time_after(jiffies, timeout)) {
778 pr_err("%s: Card stuck in programming state! %s %s\n",
779 mmc_hostname(card->host),
780 req->rq_disk->disk_name, __func__);
785 * Some cards mishandle the status bits,
786 * so make sure to check both the busy
787 * indication and the card state.
789 } while (!(status & R1_READY_FOR_DATA) ||
790 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
795 static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
796 struct request *req, int *gen_err, u32 *stop_status)
798 struct mmc_host *host = card->host;
799 struct mmc_command cmd = {0};
801 bool use_r1b_resp = rq_data_dir(req) == WRITE;
804 * Normally we use R1B responses for WRITE, but in cases where the host
805 * has specified a max_busy_timeout we need to validate it. A failure
806 * means we need to prevent the host from doing hw busy detection, which
807 * is done by converting to a R1 response instead.
809 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
810 use_r1b_resp = false;
812 cmd.opcode = MMC_STOP_TRANSMISSION;
814 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
815 cmd.busy_timeout = timeout_ms;
817 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
820 err = mmc_wait_for_cmd(host, &cmd, 5);
824 *stop_status = cmd.resp[0];
826 /* No need to check card status in case of READ. */
827 if (rq_data_dir(req) == READ)
830 if (!mmc_host_is_spi(host) &&
831 (*stop_status & R1_ERROR)) {
832 pr_err("%s: %s: general error sending stop command, resp %#x\n",
833 req->rq_disk->disk_name, __func__, *stop_status);
837 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
840 #define ERR_NOMEDIUM 3
843 #define ERR_CONTINUE 0
845 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
846 bool status_valid, u32 status)
850 /* response crc error, retry the r/w cmd */
851 pr_err("%s: %s sending %s command, card status %#x\n",
852 req->rq_disk->disk_name, "response CRC error",
857 pr_err("%s: %s sending %s command, card status %#x\n",
858 req->rq_disk->disk_name, "timed out", name, status);
860 /* If the status cmd initially failed, retry the r/w cmd */
865 * If it was a r/w cmd crc error, or illegal command
866 * (eg, issued in wrong state) then retry - we should
867 * have corrected the state problem above.
869 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
872 /* Otherwise abort the command */
876 /* We don't understand the error code the driver gave us */
877 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
878 req->rq_disk->disk_name, error, status);
884 * Initial r/w and stop cmd error recovery.
885 * We don't know whether the card received the r/w cmd or not, so try to
886 * restore things back to a sane state. Essentially, we do this as follows:
887 * - Obtain card status. If the first attempt to obtain card status fails,
888 * the status word will reflect the failed status cmd, not the failed
889 * r/w cmd. If we fail to obtain card status, it suggests we can no
890 * longer communicate with the card.
891 * - Check the card state. If the card received the cmd but there was a
892 * transient problem with the response, it might still be in a data transfer
893 * mode. Try to send it a stop command. If this fails, we can't recover.
894 * - If the r/w cmd failed due to a response CRC error, it was probably
895 * transient, so retry the cmd.
896 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
897 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
898 * illegal cmd, retry.
899 * Otherwise we don't understand what happened, so abort.
901 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
902 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
904 bool prev_cmd_status_valid = true;
905 u32 status, stop_status = 0;
908 if (mmc_card_removed(card))
912 * Try to get card status which indicates both the card state
913 * and why there was no response. If the first attempt fails,
914 * we can't be sure the returned status is for the r/w command.
916 for (retry = 2; retry >= 0; retry--) {
917 err = get_card_status(card, &status, 0);
921 /* Re-tune if needed */
922 mmc_retune_recheck(card->host);
924 prev_cmd_status_valid = false;
925 pr_err("%s: error %d sending status command, %sing\n",
926 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
929 /* We couldn't get a response from the card. Give up. */
931 /* Check if the card is removed */
932 if (mmc_detect_card_removed(card->host))
937 /* Flag ECC errors */
938 if ((status & R1_CARD_ECC_FAILED) ||
939 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
940 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
943 /* Flag General errors */
944 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
945 if ((status & R1_ERROR) ||
946 (brq->stop.resp[0] & R1_ERROR)) {
947 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
948 req->rq_disk->disk_name, __func__,
949 brq->stop.resp[0], status);
954 * Check the current card state. If it is in some data transfer
955 * mode, tell it to stop (and hopefully transition back to TRAN.)
957 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
958 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
959 err = send_stop(card,
960 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
961 req, gen_err, &stop_status);
963 pr_err("%s: error %d sending stop command\n",
964 req->rq_disk->disk_name, err);
966 * If the stop cmd also timed out, the card is probably
967 * not present, so abort. Other errors are bad news too.
972 if (stop_status & R1_CARD_ECC_FAILED)
976 /* Check for set block count errors */
978 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
979 prev_cmd_status_valid, status);
981 /* Check for r/w command errors */
983 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
984 prev_cmd_status_valid, status);
987 if (!brq->stop.error)
990 /* Now for stop errors. These aren't fatal to the transfer. */
991 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
992 req->rq_disk->disk_name, brq->stop.error,
993 brq->cmd.resp[0], status);
996 * Subsitute in our own stop status as this will give the error
997 * state which happened during the execution of the r/w command.
1000 brq->stop.resp[0] = stop_status;
1001 brq->stop.error = 0;
1003 return ERR_CONTINUE;
1006 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1011 if (md->reset_done & type)
1014 md->reset_done |= type;
1015 err = mmc_hw_reset(host);
1016 /* Ensure we switch back to the correct partition */
1017 if (err != -EOPNOTSUPP) {
1018 struct mmc_blk_data *main_md =
1019 dev_get_drvdata(&host->card->dev);
1022 main_md->part_curr = main_md->part_type;
1023 part_err = mmc_blk_part_switch(host->card, md);
1026 * We have failed to get back into the correct
1027 * partition, so we need to abort the whole request.
1035 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1037 md->reset_done &= ~type;
1040 int mmc_access_rpmb(struct mmc_queue *mq)
1042 struct mmc_blk_data *md = mq->data;
1044 * If this is a RPMB partition access, return ture
1046 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1052 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1054 struct mmc_blk_data *md = mq->data;
1055 struct mmc_card *card = md->queue.card;
1056 unsigned int from, nr, arg;
1057 int err = 0, type = MMC_BLK_DISCARD;
1059 if (!mmc_can_erase(card)) {
1064 from = blk_rq_pos(req);
1065 nr = blk_rq_sectors(req);
1067 if (mmc_can_discard(card))
1068 arg = MMC_DISCARD_ARG;
1069 else if (mmc_can_trim(card))
1072 arg = MMC_ERASE_ARG;
1074 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1075 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1076 INAND_CMD38_ARG_EXT_CSD,
1077 arg == MMC_TRIM_ARG ?
1078 INAND_CMD38_ARG_TRIM :
1079 INAND_CMD38_ARG_ERASE,
1084 err = mmc_erase(card, from, nr, arg);
1086 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1089 mmc_blk_reset_success(md, type);
1090 blk_end_request(req, err, blk_rq_bytes(req));
1095 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1096 struct request *req)
1098 struct mmc_blk_data *md = mq->data;
1099 struct mmc_card *card = md->queue.card;
1100 unsigned int from, nr, arg;
1101 int err = 0, type = MMC_BLK_SECDISCARD;
1103 if (!(mmc_can_secure_erase_trim(card))) {
1108 from = blk_rq_pos(req);
1109 nr = blk_rq_sectors(req);
1111 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1112 arg = MMC_SECURE_TRIM1_ARG;
1114 arg = MMC_SECURE_ERASE_ARG;
1117 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1118 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1119 INAND_CMD38_ARG_EXT_CSD,
1120 arg == MMC_SECURE_TRIM1_ARG ?
1121 INAND_CMD38_ARG_SECTRIM1 :
1122 INAND_CMD38_ARG_SECERASE,
1128 err = mmc_erase(card, from, nr, arg);
1134 if (arg == MMC_SECURE_TRIM1_ARG) {
1135 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1136 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1137 INAND_CMD38_ARG_EXT_CSD,
1138 INAND_CMD38_ARG_SECTRIM2,
1144 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1152 if (err && !mmc_blk_reset(md, card->host, type))
1155 mmc_blk_reset_success(md, type);
1157 blk_end_request(req, err, blk_rq_bytes(req));
1162 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1164 struct mmc_blk_data *md = mq->data;
1165 struct mmc_card *card = md->queue.card;
1168 ret = mmc_flush_cache(card);
1172 blk_end_request_all(req, ret);
1178 * Reformat current write as a reliable write, supporting
1179 * both legacy and the enhanced reliable write MMC cards.
1180 * In each transfer we'll handle only as much as a single
1181 * reliable write can handle, thus finish the request in
1182 * partial completions.
1184 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1185 struct mmc_card *card,
1186 struct request *req)
1188 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1189 /* Legacy mode imposes restrictions on transfers. */
1190 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1191 brq->data.blocks = 1;
1193 if (brq->data.blocks > card->ext_csd.rel_sectors)
1194 brq->data.blocks = card->ext_csd.rel_sectors;
1195 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1196 brq->data.blocks = 1;
1200 #define CMD_ERRORS \
1201 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1202 R1_ADDRESS_ERROR | /* Misaligned address */ \
1203 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1204 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1205 R1_CC_ERROR | /* Card controller error */ \
1206 R1_ERROR) /* General/unknown error */
1208 static int mmc_blk_err_check(struct mmc_card *card,
1209 struct mmc_async_req *areq)
1211 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1213 struct mmc_blk_request *brq = &mq_mrq->brq;
1214 struct request *req = mq_mrq->req;
1215 int need_retune = card->host->need_retune;
1216 int ecc_err = 0, gen_err = 0;
1219 * sbc.error indicates a problem with the set block count
1220 * command. No data will have been transferred.
1222 * cmd.error indicates a problem with the r/w command. No
1223 * data will have been transferred.
1225 * stop.error indicates a problem with the stop command. Data
1226 * may have been transferred, or may still be transferring.
1228 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1230 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
1232 return MMC_BLK_RETRY;
1234 return MMC_BLK_ABORT;
1236 return MMC_BLK_NOMEDIUM;
1243 * Check for errors relating to the execution of the
1244 * initial command - such as address errors. No data
1245 * has been transferred.
1247 if (brq->cmd.resp[0] & CMD_ERRORS) {
1248 pr_err("%s: r/w command failed, status = %#x\n",
1249 req->rq_disk->disk_name, brq->cmd.resp[0]);
1250 return MMC_BLK_ABORT;
1254 * Everything else is either success, or a data error of some
1255 * kind. If it was a write, we may have transitioned to
1256 * program mode, which we have to wait for it to complete.
1258 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1261 /* Check stop command response */
1262 if (brq->stop.resp[0] & R1_ERROR) {
1263 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1264 req->rq_disk->disk_name, __func__,
1269 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1272 return MMC_BLK_CMD_ERR;
1275 /* if general error occurs, retry the write operation. */
1277 pr_warn("%s: retrying write for general error\n",
1278 req->rq_disk->disk_name);
1279 return MMC_BLK_RETRY;
1282 if (brq->data.error) {
1283 if (need_retune && !brq->retune_retry_done) {
1284 pr_info("%s: retrying because a re-tune was needed\n",
1285 req->rq_disk->disk_name);
1286 brq->retune_retry_done = 1;
1287 return MMC_BLK_RETRY;
1289 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1290 req->rq_disk->disk_name, brq->data.error,
1291 (unsigned)blk_rq_pos(req),
1292 (unsigned)blk_rq_sectors(req),
1293 brq->cmd.resp[0], brq->stop.resp[0]);
1295 if (rq_data_dir(req) == READ) {
1297 return MMC_BLK_ECC_ERR;
1298 return MMC_BLK_DATA_ERR;
1300 return MMC_BLK_CMD_ERR;
1304 if (!brq->data.bytes_xfered)
1305 return MMC_BLK_RETRY;
1307 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1308 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1309 return MMC_BLK_PARTIAL;
1311 return MMC_BLK_SUCCESS;
1314 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1315 return MMC_BLK_PARTIAL;
1317 return MMC_BLK_SUCCESS;
1320 static int mmc_blk_packed_err_check(struct mmc_card *card,
1321 struct mmc_async_req *areq)
1323 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1325 struct request *req = mq_rq->req;
1326 struct mmc_packed *packed = mq_rq->packed;
1327 int err, check, status;
1333 check = mmc_blk_err_check(card, areq);
1334 err = get_card_status(card, &status, 0);
1336 pr_err("%s: error %d sending status command\n",
1337 req->rq_disk->disk_name, err);
1338 return MMC_BLK_ABORT;
1341 if (status & R1_EXCEPTION_EVENT) {
1342 err = mmc_get_ext_csd(card, &ext_csd);
1344 pr_err("%s: error %d sending ext_csd\n",
1345 req->rq_disk->disk_name, err);
1346 return MMC_BLK_ABORT;
1349 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1350 EXT_CSD_PACKED_FAILURE) &&
1351 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1352 EXT_CSD_PACKED_GENERIC_ERROR)) {
1353 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1354 EXT_CSD_PACKED_INDEXED_ERROR) {
1355 packed->idx_failure =
1356 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1357 check = MMC_BLK_PARTIAL;
1359 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1360 "failure index: %d\n",
1361 req->rq_disk->disk_name, packed->nr_entries,
1362 packed->blocks, packed->idx_failure);
1370 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1371 struct mmc_card *card,
1373 struct mmc_queue *mq)
1375 u32 readcmd, writecmd;
1376 struct mmc_blk_request *brq = &mqrq->brq;
1377 struct request *req = mqrq->req;
1378 struct mmc_blk_data *md = mq->data;
1382 * Reliable writes are used to implement Forced Unit Access and
1383 * REQ_META accesses, and are supported only on MMCs.
1385 * XXX: this really needs a good explanation of why REQ_META
1386 * is treated special.
1388 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1389 (req->cmd_flags & REQ_META)) &&
1390 (rq_data_dir(req) == WRITE) &&
1391 (md->flags & MMC_BLK_REL_WR);
1393 memset(brq, 0, sizeof(struct mmc_blk_request));
1394 brq->mrq.cmd = &brq->cmd;
1395 brq->mrq.data = &brq->data;
1397 brq->cmd.arg = blk_rq_pos(req);
1398 if (!mmc_card_blockaddr(card))
1400 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1401 brq->data.blksz = 512;
1402 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1404 brq->data.blocks = blk_rq_sectors(req);
1407 * The block layer doesn't support all sector count
1408 * restrictions, so we need to be prepared for too big
1411 if (brq->data.blocks > card->host->max_blk_count)
1412 brq->data.blocks = card->host->max_blk_count;
1414 if (brq->data.blocks > 1) {
1416 * After a read error, we redo the request one sector
1417 * at a time in order to accurately determine which
1418 * sectors can be read successfully.
1421 brq->data.blocks = 1;
1424 * Some controllers have HW issues while operating
1425 * in multiple I/O mode
1427 if (card->host->ops->multi_io_quirk)
1428 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1429 (rq_data_dir(req) == READ) ?
1430 MMC_DATA_READ : MMC_DATA_WRITE,
1434 if (brq->data.blocks > 1 || do_rel_wr) {
1435 /* SPI multiblock writes terminate using a special
1436 * token, not a STOP_TRANSMISSION request.
1438 if (!mmc_host_is_spi(card->host) ||
1439 rq_data_dir(req) == READ)
1440 brq->mrq.stop = &brq->stop;
1441 readcmd = MMC_READ_MULTIPLE_BLOCK;
1442 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1444 brq->mrq.stop = NULL;
1445 readcmd = MMC_READ_SINGLE_BLOCK;
1446 writecmd = MMC_WRITE_BLOCK;
1448 if (rq_data_dir(req) == READ) {
1449 brq->cmd.opcode = readcmd;
1450 brq->data.flags |= MMC_DATA_READ;
1452 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1455 brq->cmd.opcode = writecmd;
1456 brq->data.flags |= MMC_DATA_WRITE;
1458 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1463 mmc_apply_rel_rw(brq, card, req);
1466 * Data tag is used only during writing meta data to speed
1467 * up write and any subsequent read of this meta data
1469 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1470 (req->cmd_flags & REQ_META) &&
1471 (rq_data_dir(req) == WRITE) &&
1472 ((brq->data.blocks * brq->data.blksz) >=
1473 card->ext_csd.data_tag_unit_size);
1476 * Pre-defined multi-block transfers are preferable to
1477 * open ended-ones (and necessary for reliable writes).
1478 * However, it is not sufficient to just send CMD23,
1479 * and avoid the final CMD12, as on an error condition
1480 * CMD12 (stop) needs to be sent anyway. This, coupled
1481 * with Auto-CMD23 enhancements provided by some
1482 * hosts, means that the complexity of dealing
1483 * with this is best left to the host. If CMD23 is
1484 * supported by card and host, we'll fill sbc in and let
1485 * the host deal with handling it correctly. This means
1486 * that for hosts that don't expose MMC_CAP_CMD23, no
1487 * change of behavior will be observed.
1489 * N.B: Some MMC cards experience perf degradation.
1490 * We'll avoid using CMD23-bounded multiblock writes for
1491 * these, while retaining features like reliable writes.
1493 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1494 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1496 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1497 brq->sbc.arg = brq->data.blocks |
1498 (do_rel_wr ? (1 << 31) : 0) |
1499 (do_data_tag ? (1 << 29) : 0);
1500 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1501 brq->mrq.sbc = &brq->sbc;
1504 mmc_set_data_timeout(&brq->data, card);
1506 brq->data.sg = mqrq->sg;
1507 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1510 * Adjust the sg list so it is the same size as the
1513 if (brq->data.blocks != blk_rq_sectors(req)) {
1514 int i, data_size = brq->data.blocks << 9;
1515 struct scatterlist *sg;
1517 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1518 data_size -= sg->length;
1519 if (data_size <= 0) {
1520 sg->length += data_size;
1525 brq->data.sg_len = i;
1528 mqrq->mmc_active.mrq = &brq->mrq;
1529 mqrq->mmc_active.err_check = mmc_blk_err_check;
1531 mmc_queue_bounce_pre(mqrq);
1534 static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1535 struct mmc_card *card)
1537 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1538 unsigned int max_seg_sz = queue_max_segment_size(q);
1539 unsigned int len, nr_segs = 0;
1542 len = min(hdr_sz, max_seg_sz);
1550 static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1552 struct request_queue *q = mq->queue;
1553 struct mmc_card *card = mq->card;
1554 struct request *cur = req, *next = NULL;
1555 struct mmc_blk_data *md = mq->data;
1556 struct mmc_queue_req *mqrq = mq->mqrq_cur;
1557 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1558 unsigned int req_sectors = 0, phys_segments = 0;
1559 unsigned int max_blk_count, max_phys_segs;
1560 bool put_back = true;
1561 u8 max_packed_rw = 0;
1564 if (!(md->flags & MMC_BLK_PACKED_CMD))
1567 if ((rq_data_dir(cur) == WRITE) &&
1568 mmc_host_packed_wr(card->host))
1569 max_packed_rw = card->ext_csd.max_packed_writes;
1571 if (max_packed_rw == 0)
1574 if (mmc_req_rel_wr(cur) &&
1575 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1578 if (mmc_large_sector(card) &&
1579 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1582 mmc_blk_clear_packed(mqrq);
1584 max_blk_count = min(card->host->max_blk_count,
1585 card->host->max_req_size >> 9);
1586 if (unlikely(max_blk_count > 0xffff))
1587 max_blk_count = 0xffff;
1589 max_phys_segs = queue_max_segments(q);
1590 req_sectors += blk_rq_sectors(cur);
1591 phys_segments += cur->nr_phys_segments;
1593 if (rq_data_dir(cur) == WRITE) {
1594 req_sectors += mmc_large_sector(card) ? 8 : 1;
1595 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1599 if (reqs >= max_packed_rw - 1) {
1604 spin_lock_irq(q->queue_lock);
1605 next = blk_fetch_request(q);
1606 spin_unlock_irq(q->queue_lock);
1612 if (mmc_large_sector(card) &&
1613 !IS_ALIGNED(blk_rq_sectors(next), 8))
1616 if (next->cmd_flags & REQ_DISCARD ||
1617 next->cmd_flags & REQ_FLUSH)
1620 if (rq_data_dir(cur) != rq_data_dir(next))
1623 if (mmc_req_rel_wr(next) &&
1624 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1627 req_sectors += blk_rq_sectors(next);
1628 if (req_sectors > max_blk_count)
1631 phys_segments += next->nr_phys_segments;
1632 if (phys_segments > max_phys_segs)
1635 list_add_tail(&next->queuelist, &mqrq->packed->list);
1641 spin_lock_irq(q->queue_lock);
1642 blk_requeue_request(q, next);
1643 spin_unlock_irq(q->queue_lock);
1647 list_add(&req->queuelist, &mqrq->packed->list);
1648 mqrq->packed->nr_entries = ++reqs;
1649 mqrq->packed->retries = reqs;
1654 mqrq->cmd_type = MMC_PACKED_NONE;
1658 static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1659 struct mmc_card *card,
1660 struct mmc_queue *mq)
1662 struct mmc_blk_request *brq = &mqrq->brq;
1663 struct request *req = mqrq->req;
1664 struct request *prq;
1665 struct mmc_blk_data *md = mq->data;
1666 struct mmc_packed *packed = mqrq->packed;
1667 bool do_rel_wr, do_data_tag;
1668 u32 *packed_cmd_hdr;
1674 mqrq->cmd_type = MMC_PACKED_WRITE;
1676 packed->idx_failure = MMC_PACKED_NR_IDX;
1678 packed_cmd_hdr = packed->cmd_hdr;
1679 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
1680 packed_cmd_hdr[0] = (packed->nr_entries << 16) |
1681 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1682 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1685 * Argument for each entry of packed group
1687 list_for_each_entry(prq, &packed->list, queuelist) {
1688 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1689 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1690 (prq->cmd_flags & REQ_META) &&
1691 (rq_data_dir(prq) == WRITE) &&
1692 ((brq->data.blocks * brq->data.blksz) >=
1693 card->ext_csd.data_tag_unit_size);
1694 /* Argument of CMD23 */
1695 packed_cmd_hdr[(i * 2)] =
1696 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1697 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1698 blk_rq_sectors(prq);
1699 /* Argument of CMD18 or CMD25 */
1700 packed_cmd_hdr[((i * 2)) + 1] =
1701 mmc_card_blockaddr(card) ?
1702 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1703 packed->blocks += blk_rq_sectors(prq);
1707 memset(brq, 0, sizeof(struct mmc_blk_request));
1708 brq->mrq.cmd = &brq->cmd;
1709 brq->mrq.data = &brq->data;
1710 brq->mrq.sbc = &brq->sbc;
1711 brq->mrq.stop = &brq->stop;
1713 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1714 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1715 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1717 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1718 brq->cmd.arg = blk_rq_pos(req);
1719 if (!mmc_card_blockaddr(card))
1721 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1723 brq->data.blksz = 512;
1724 brq->data.blocks = packed->blocks + hdr_blocks;
1725 brq->data.flags |= MMC_DATA_WRITE;
1727 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1729 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1731 mmc_set_data_timeout(&brq->data, card);
1733 brq->data.sg = mqrq->sg;
1734 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1736 mqrq->mmc_active.mrq = &brq->mrq;
1737 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1739 mmc_queue_bounce_pre(mqrq);
1742 static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1743 struct mmc_blk_request *brq, struct request *req,
1746 struct mmc_queue_req *mq_rq;
1747 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1750 * If this is an SD card and we're writing, we can first
1751 * mark the known good sectors as ok.
1753 * If the card is not SD, we can still ok written sectors
1754 * as reported by the controller (which might be less than
1755 * the real number of written sectors, but never more).
1757 if (mmc_card_sd(card)) {
1760 blocks = mmc_sd_num_wr_blocks(card);
1761 if (blocks != (u32)-1) {
1762 ret = blk_end_request(req, 0, blocks << 9);
1765 if (!mmc_packed_cmd(mq_rq->cmd_type))
1766 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1771 static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1773 struct request *prq;
1774 struct mmc_packed *packed = mq_rq->packed;
1775 int idx = packed->idx_failure, i = 0;
1780 while (!list_empty(&packed->list)) {
1781 prq = list_entry_rq(packed->list.next);
1783 /* retry from error index */
1784 packed->nr_entries -= idx;
1788 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1789 list_del_init(&prq->queuelist);
1790 mmc_blk_clear_packed(mq_rq);
1794 list_del_init(&prq->queuelist);
1795 blk_end_request(prq, 0, blk_rq_bytes(prq));
1799 mmc_blk_clear_packed(mq_rq);
1803 static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1805 struct request *prq;
1806 struct mmc_packed *packed = mq_rq->packed;
1810 while (!list_empty(&packed->list)) {
1811 prq = list_entry_rq(packed->list.next);
1812 list_del_init(&prq->queuelist);
1813 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1816 mmc_blk_clear_packed(mq_rq);
1819 static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1820 struct mmc_queue_req *mq_rq)
1822 struct request *prq;
1823 struct request_queue *q = mq->queue;
1824 struct mmc_packed *packed = mq_rq->packed;
1828 while (!list_empty(&packed->list)) {
1829 prq = list_entry_rq(packed->list.prev);
1830 if (prq->queuelist.prev != &packed->list) {
1831 list_del_init(&prq->queuelist);
1832 spin_lock_irq(q->queue_lock);
1833 blk_requeue_request(mq->queue, prq);
1834 spin_unlock_irq(q->queue_lock);
1836 list_del_init(&prq->queuelist);
1840 mmc_blk_clear_packed(mq_rq);
1843 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
1845 struct mmc_blk_data *md = mq->data;
1846 struct mmc_card *card = md->queue.card;
1847 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
1848 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
1849 enum mmc_blk_status status;
1850 struct mmc_queue_req *mq_rq;
1851 struct request *req = rqc;
1852 struct mmc_async_req *areq;
1853 const u8 packed_nr = 2;
1856 if (!rqc && !mq->mqrq_prev->req)
1860 reqs = mmc_blk_prep_packed_list(mq, rqc);
1865 * When 4KB native sector is enabled, only 8 blocks
1866 * multiple read or write is allowed
1868 if ((brq->data.blocks & 0x07) &&
1869 (card->ext_csd.data_sector_size == 4096)) {
1870 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1871 req->rq_disk->disk_name);
1872 mq_rq = mq->mqrq_cur;
1876 if (reqs >= packed_nr)
1877 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1880 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1881 areq = &mq->mqrq_cur->mmc_active;
1884 areq = mmc_start_req(card->host, areq, (int *) &status);
1886 if (status == MMC_BLK_NEW_REQUEST)
1887 mq->flags |= MMC_QUEUE_NEW_REQUEST;
1891 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1894 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1895 mmc_queue_bounce_post(mq_rq);
1898 case MMC_BLK_SUCCESS:
1899 case MMC_BLK_PARTIAL:
1901 * A block was successfully transferred.
1903 mmc_blk_reset_success(md, type);
1905 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1906 ret = mmc_blk_end_packed_req(mq_rq);
1909 ret = blk_end_request(req, 0,
1910 brq->data.bytes_xfered);
1914 * If the blk_end_request function returns non-zero even
1915 * though all data has been transferred and no errors
1916 * were returned by the host controller, it's a bug.
1918 if (status == MMC_BLK_SUCCESS && ret) {
1919 pr_err("%s BUG rq_tot %d d_xfer %d\n",
1920 __func__, blk_rq_bytes(req),
1921 brq->data.bytes_xfered);
1926 case MMC_BLK_CMD_ERR:
1927 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1928 if (mmc_blk_reset(md, card->host, type))
1934 retune_retry_done = brq->retune_retry_done;
1939 if (!mmc_blk_reset(md, card->host, type))
1942 case MMC_BLK_DATA_ERR: {
1945 err = mmc_blk_reset(md, card->host, type);
1948 if (err == -ENODEV ||
1949 mmc_packed_cmd(mq_rq->cmd_type))
1953 case MMC_BLK_ECC_ERR:
1954 if (brq->data.blocks > 1) {
1955 /* Redo read one sector at a time */
1956 pr_warn("%s: retrying using single block read\n",
1957 req->rq_disk->disk_name);
1962 * After an error, we redo I/O one sector at a
1963 * time, so we only reach here after trying to
1964 * read a single sector.
1966 ret = blk_end_request(req, -EIO,
1971 case MMC_BLK_NOMEDIUM:
1974 pr_err("%s: Unhandled return value (%d)",
1975 req->rq_disk->disk_name, status);
1980 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1981 if (!mq_rq->packed->retries)
1983 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1984 mmc_start_req(card->host,
1985 &mq_rq->mmc_active, NULL);
1989 * In case of a incomplete request
1990 * prepare it again and resend.
1992 mmc_blk_rw_rq_prep(mq_rq, card,
1994 mmc_start_req(card->host,
1995 &mq_rq->mmc_active, NULL);
1997 mq_rq->brq.retune_retry_done = retune_retry_done;
2004 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2005 mmc_blk_abort_packed_req(mq_rq);
2007 if (mmc_card_removed(card))
2008 req->cmd_flags |= REQ_QUIET;
2010 ret = blk_end_request(req, -EIO,
2011 blk_rq_cur_bytes(req));
2016 if (mmc_card_removed(card)) {
2017 rqc->cmd_flags |= REQ_QUIET;
2018 blk_end_request_all(rqc, -EIO);
2021 * If current request is packed, it needs to put back.
2023 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2024 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2026 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2027 mmc_start_req(card->host,
2028 &mq->mqrq_cur->mmc_active, NULL);
2035 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2038 struct mmc_blk_data *md = mq->data;
2039 struct mmc_card *card = md->queue.card;
2040 struct mmc_host *host = card->host;
2041 unsigned long flags;
2042 unsigned int cmd_flags = req ? req->cmd_flags : 0;
2044 if (req && !mq->mqrq_prev->req)
2045 /* claim host only for the first request */
2048 ret = mmc_blk_part_switch(card, md);
2051 blk_end_request_all(req, -EIO);
2057 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
2058 if (cmd_flags & REQ_DISCARD) {
2059 /* complete ongoing async transfer before issuing discard */
2060 if (card->host->areq)
2061 mmc_blk_issue_rw_rq(mq, NULL);
2062 if (req->cmd_flags & REQ_SECURE)
2063 ret = mmc_blk_issue_secdiscard_rq(mq, req);
2065 ret = mmc_blk_issue_discard_rq(mq, req);
2066 } else if (cmd_flags & REQ_FLUSH) {
2067 /* complete ongoing async transfer before issuing flush */
2068 if (card->host->areq)
2069 mmc_blk_issue_rw_rq(mq, NULL);
2070 ret = mmc_blk_issue_flush(mq, req);
2072 if (!req && host->areq) {
2073 spin_lock_irqsave(&host->context_info.lock, flags);
2074 host->context_info.is_waiting_last_req = true;
2075 spin_unlock_irqrestore(&host->context_info.lock, flags);
2077 ret = mmc_blk_issue_rw_rq(mq, req);
2081 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
2082 (cmd_flags & MMC_REQ_SPECIAL_MASK))
2084 * Release host when there are no more requests
2085 * and after special request(discard, flush) is done.
2086 * In case sepecial request, there is no reentry to
2087 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2093 static inline int mmc_blk_readonly(struct mmc_card *card)
2095 return mmc_card_readonly(card) ||
2096 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2099 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2100 struct device *parent,
2103 const char *subname,
2106 struct mmc_blk_data *md;
2109 devidx = find_first_zero_bit(dev_use, max_devices);
2110 if (devidx >= max_devices)
2111 return ERR_PTR(-ENOSPC);
2112 __set_bit(devidx, dev_use);
2114 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2121 * !subname implies we are creating main mmc_blk_data that will be
2122 * associated with mmc_card with dev_set_drvdata. Due to device
2123 * partitions, devidx will not coincide with a per-physical card
2124 * index anymore so we keep track of a name index.
2127 md->name_idx = find_first_zero_bit(name_use, max_devices);
2128 __set_bit(md->name_idx, name_use);
2130 md->name_idx = ((struct mmc_blk_data *)
2131 dev_to_disk(parent)->private_data)->name_idx;
2133 md->area_type = area_type;
2136 * Set the read-only status based on the supported commands
2137 * and the write protect switch.
2139 md->read_only = mmc_blk_readonly(card);
2141 md->disk = alloc_disk(perdev_minors);
2142 if (md->disk == NULL) {
2147 spin_lock_init(&md->lock);
2148 INIT_LIST_HEAD(&md->part);
2151 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2155 md->queue.issue_fn = mmc_blk_issue_rq;
2156 md->queue.data = md;
2158 md->disk->major = MMC_BLOCK_MAJOR;
2159 md->disk->first_minor = devidx * perdev_minors;
2160 md->disk->fops = &mmc_bdops;
2161 md->disk->private_data = md;
2162 md->disk->queue = md->queue.queue;
2163 md->disk->driverfs_dev = parent;
2164 set_disk_ro(md->disk, md->read_only || default_ro);
2165 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2166 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
2169 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2171 * - be set for removable media with permanent block devices
2172 * - be unset for removable block devices with permanent media
2174 * Since MMC block devices clearly fall under the second
2175 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2176 * should use the block device creation/destruction hotplug
2177 * messages to tell when the card is present.
2180 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2181 "mmcblk%u%s", md->name_idx, subname ? subname : "");
2183 if (mmc_card_mmc(card))
2184 blk_queue_logical_block_size(md->queue.queue,
2185 card->ext_csd.data_sector_size);
2187 blk_queue_logical_block_size(md->queue.queue, 512);
2189 set_capacity(md->disk, size);
2191 if (mmc_host_cmd23(card->host)) {
2192 if (mmc_card_mmc(card) ||
2193 (mmc_card_sd(card) &&
2194 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2195 md->flags |= MMC_BLK_CMD23;
2198 if (mmc_card_mmc(card) &&
2199 md->flags & MMC_BLK_CMD23 &&
2200 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2201 card->ext_csd.rel_sectors)) {
2202 md->flags |= MMC_BLK_REL_WR;
2203 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2206 if (mmc_card_mmc(card) &&
2207 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2208 (md->flags & MMC_BLK_CMD23) &&
2209 card->ext_csd.packed_event_en) {
2210 if (!mmc_packed_init(&md->queue, card))
2211 md->flags |= MMC_BLK_PACKED_CMD;
2221 return ERR_PTR(ret);
2224 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2228 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2230 * The EXT_CSD sector count is in number or 512 byte
2233 size = card->ext_csd.sectors;
2236 * The CSD capacity field is in units of read_blkbits.
2237 * set_capacity takes units of 512 bytes.
2239 size = (typeof(sector_t))card->csd.capacity
2240 << (card->csd.read_blkbits - 9);
2243 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2244 MMC_BLK_DATA_AREA_MAIN);
2247 static int mmc_blk_alloc_part(struct mmc_card *card,
2248 struct mmc_blk_data *md,
2249 unsigned int part_type,
2252 const char *subname,
2256 struct mmc_blk_data *part_md;
2258 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2259 subname, area_type);
2260 if (IS_ERR(part_md))
2261 return PTR_ERR(part_md);
2262 part_md->part_type = part_type;
2263 list_add(&part_md->part, &md->part);
2265 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
2266 cap_str, sizeof(cap_str));
2267 pr_info("%s: %s %s partition %u %s\n",
2268 part_md->disk->disk_name, mmc_card_id(card),
2269 mmc_card_name(card), part_md->part_type, cap_str);
2273 /* MMC Physical partitions consist of two boot partitions and
2274 * up to four general purpose partitions.
2275 * For each partition enabled in EXT_CSD a block device will be allocatedi
2276 * to provide access to the partition.
2279 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2283 if (!mmc_card_mmc(card))
2286 for (idx = 0; idx < card->nr_parts; idx++) {
2287 if (card->part[idx].size) {
2288 ret = mmc_blk_alloc_part(card, md,
2289 card->part[idx].part_cfg,
2290 card->part[idx].size >> 9,
2291 card->part[idx].force_ro,
2292 card->part[idx].name,
2293 card->part[idx].area_type);
2302 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2304 struct mmc_card *card;
2308 * Flush remaining requests and free queues. It
2309 * is freeing the queue that stops new requests
2310 * from being accepted.
2312 card = md->queue.card;
2313 mmc_cleanup_queue(&md->queue);
2314 if (md->flags & MMC_BLK_PACKED_CMD)
2315 mmc_packed_clean(&md->queue);
2316 if (md->disk->flags & GENHD_FL_UP) {
2317 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2318 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2319 card->ext_csd.boot_ro_lockable)
2320 device_remove_file(disk_to_dev(md->disk),
2321 &md->power_ro_lock);
2323 del_gendisk(md->disk);
2329 static void mmc_blk_remove_parts(struct mmc_card *card,
2330 struct mmc_blk_data *md)
2332 struct list_head *pos, *q;
2333 struct mmc_blk_data *part_md;
2335 __clear_bit(md->name_idx, name_use);
2336 list_for_each_safe(pos, q, &md->part) {
2337 part_md = list_entry(pos, struct mmc_blk_data, part);
2339 mmc_blk_remove_req(part_md);
2343 static int mmc_add_disk(struct mmc_blk_data *md)
2346 struct mmc_card *card = md->queue.card;
2349 md->force_ro.show = force_ro_show;
2350 md->force_ro.store = force_ro_store;
2351 sysfs_attr_init(&md->force_ro.attr);
2352 md->force_ro.attr.name = "force_ro";
2353 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2354 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2358 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2359 card->ext_csd.boot_ro_lockable) {
2362 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2365 mode = S_IRUGO | S_IWUSR;
2367 md->power_ro_lock.show = power_ro_lock_show;
2368 md->power_ro_lock.store = power_ro_lock_store;
2369 sysfs_attr_init(&md->power_ro_lock.attr);
2370 md->power_ro_lock.attr.mode = mode;
2371 md->power_ro_lock.attr.name =
2372 "ro_lock_until_next_power_on";
2373 ret = device_create_file(disk_to_dev(md->disk),
2374 &md->power_ro_lock);
2376 goto power_ro_lock_fail;
2381 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2383 del_gendisk(md->disk);
2388 #define CID_MANFID_SANDISK 0x2
2389 #define CID_MANFID_TOSHIBA 0x11
2390 #define CID_MANFID_MICRON 0x13
2391 #define CID_MANFID_SAMSUNG 0x15
2392 #define CID_MANFID_KINGSTON 0x70
2394 static const struct mmc_fixup blk_fixups[] =
2396 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2397 MMC_QUIRK_INAND_CMD38),
2398 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2399 MMC_QUIRK_INAND_CMD38),
2400 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2401 MMC_QUIRK_INAND_CMD38),
2402 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2403 MMC_QUIRK_INAND_CMD38),
2404 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2405 MMC_QUIRK_INAND_CMD38),
2408 * Some MMC cards experience performance degradation with CMD23
2409 * instead of CMD12-bounded multiblock transfers. For now we'll
2410 * black list what's bad...
2411 * - Certain Toshiba cards.
2413 * N.B. This doesn't affect SD cards.
2415 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2416 MMC_QUIRK_BLK_NO_CMD23),
2417 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2418 MMC_QUIRK_BLK_NO_CMD23),
2419 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2420 MMC_QUIRK_BLK_NO_CMD23),
2421 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2422 MMC_QUIRK_BLK_NO_CMD23),
2423 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2424 MMC_QUIRK_BLK_NO_CMD23),
2427 * Some Micron MMC cards needs longer data read timeout than
2430 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
2431 MMC_QUIRK_LONG_READ_TIME),
2434 * On these Samsung MoviNAND parts, performing secure erase or
2435 * secure trim can result in unrecoverable corruption due to a
2438 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2439 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2440 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2441 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2442 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2443 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2444 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2445 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2446 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2447 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2448 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2449 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2450 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2451 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2452 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2453 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2456 * On Some Kingston eMMCs, performing trim can result in
2457 * unrecoverable data conrruption occasionally due to a firmware bug.
2459 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2460 MMC_QUIRK_TRIM_BROKEN),
2461 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2462 MMC_QUIRK_TRIM_BROKEN),
2467 static int mmc_blk_probe(struct mmc_card *card)
2469 struct mmc_blk_data *md, *part_md;
2473 * Check that the card supports the command class(es) we need.
2475 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2478 mmc_fixup_device(card, blk_fixups);
2480 md = mmc_blk_alloc(card);
2484 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
2485 cap_str, sizeof(cap_str));
2486 pr_info("%s: %s %s %s %s\n",
2487 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2488 cap_str, md->read_only ? "(ro)" : "");
2490 if (mmc_blk_alloc_parts(card, md))
2493 dev_set_drvdata(&card->dev, md);
2495 if (mmc_add_disk(md))
2498 list_for_each_entry(part_md, &md->part, part) {
2499 if (mmc_add_disk(part_md))
2503 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2504 pm_runtime_use_autosuspend(&card->dev);
2507 * Don't enable runtime PM for SD-combo cards here. Leave that
2508 * decision to be taken during the SDIO init sequence instead.
2510 if (card->type != MMC_TYPE_SD_COMBO) {
2511 pm_runtime_set_active(&card->dev);
2512 pm_runtime_enable(&card->dev);
2518 mmc_blk_remove_parts(card, md);
2519 mmc_blk_remove_req(md);
2523 static void mmc_blk_remove(struct mmc_card *card)
2525 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2527 mmc_blk_remove_parts(card, md);
2528 pm_runtime_get_sync(&card->dev);
2529 mmc_claim_host(card->host);
2530 mmc_blk_part_switch(card, md);
2531 mmc_release_host(card->host);
2532 if (card->type != MMC_TYPE_SD_COMBO)
2533 pm_runtime_disable(&card->dev);
2534 pm_runtime_put_noidle(&card->dev);
2535 mmc_blk_remove_req(md);
2536 dev_set_drvdata(&card->dev, NULL);
2539 static int _mmc_blk_suspend(struct mmc_card *card)
2541 struct mmc_blk_data *part_md;
2542 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2545 mmc_queue_suspend(&md->queue);
2546 list_for_each_entry(part_md, &md->part, part) {
2547 mmc_queue_suspend(&part_md->queue);
2553 static void mmc_blk_shutdown(struct mmc_card *card)
2555 _mmc_blk_suspend(card);
2558 #ifdef CONFIG_PM_SLEEP
2559 static int mmc_blk_suspend(struct device *dev)
2561 struct mmc_card *card = mmc_dev_to_card(dev);
2563 return _mmc_blk_suspend(card);
2566 static int mmc_blk_resume(struct device *dev)
2568 struct mmc_blk_data *part_md;
2569 struct mmc_blk_data *md = dev_get_drvdata(dev);
2573 * Resume involves the card going into idle state,
2574 * so current partition is always the main one.
2576 md->part_curr = md->part_type;
2577 mmc_queue_resume(&md->queue);
2578 list_for_each_entry(part_md, &md->part, part) {
2579 mmc_queue_resume(&part_md->queue);
2586 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2588 static struct mmc_driver mmc_driver = {
2591 .pm = &mmc_blk_pm_ops,
2593 .probe = mmc_blk_probe,
2594 .remove = mmc_blk_remove,
2595 .shutdown = mmc_blk_shutdown,
2598 static int __init mmc_blk_init(void)
2602 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2603 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2605 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
2607 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2611 res = mmc_register_driver(&mmc_driver);
2617 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2622 static void __exit mmc_blk_exit(void)
2624 mmc_unregister_driver(&mmc_driver);
2625 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2628 module_init(mmc_blk_init);
2629 module_exit(mmc_blk_exit);
2631 MODULE_LICENSE("GPL");
2632 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");