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>
38 #include <linux/mmc/ioctl.h>
39 #include <linux/mmc/card.h>
40 #include <linux/mmc/host.h>
41 #include <linux/mmc/mmc.h>
42 #include <linux/mmc/sd.h>
44 #include <asm/uaccess.h>
48 MODULE_ALIAS("mmc:block");
49 #ifdef MODULE_PARAM_PREFIX
50 #undef MODULE_PARAM_PREFIX
52 #define MODULE_PARAM_PREFIX "mmcblk."
54 #define INAND_CMD38_ARG_EXT_CSD 113
55 #define INAND_CMD38_ARG_ERASE 0x00
56 #define INAND_CMD38_ARG_TRIM 0x01
57 #define INAND_CMD38_ARG_SECERASE 0x80
58 #define INAND_CMD38_ARG_SECTRIM1 0x81
59 #define INAND_CMD38_ARG_SECTRIM2 0x88
60 #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
62 static DEFINE_MUTEX(block_mutex);
65 * The defaults come from config options but can be overriden by module
68 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
71 * We've only got one major, so number of mmcblk devices is
72 * limited to 256 / number of minors per device.
74 static int max_devices;
76 /* 256 minors, so at most 256 separate devices */
77 static DECLARE_BITMAP(dev_use, 256);
78 static DECLARE_BITMAP(name_use, 256);
81 * There is one mmc_blk_data per slot.
86 struct mmc_queue queue;
87 struct list_head part;
90 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
91 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
94 unsigned int read_only;
95 unsigned int part_type;
96 unsigned int name_idx;
97 unsigned int reset_done;
98 #define MMC_BLK_READ BIT(0)
99 #define MMC_BLK_WRITE BIT(1)
100 #define MMC_BLK_DISCARD BIT(2)
101 #define MMC_BLK_SECDISCARD BIT(3)
104 * Only set in main mmc_blk_data associated
105 * with mmc_card with mmc_set_drvdata, and keeps
106 * track of the current selected device partition.
108 unsigned int part_curr;
109 struct device_attribute force_ro;
110 struct device_attribute power_ro_lock;
114 static DEFINE_MUTEX(open_lock);
116 module_param(perdev_minors, int, 0444);
117 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
119 static inline int mmc_blk_part_switch(struct mmc_card *card,
120 struct mmc_blk_data *md);
121 static int get_card_status(struct mmc_card *card, u32 *status, int retries);
123 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
125 struct mmc_blk_data *md;
127 mutex_lock(&open_lock);
128 md = disk->private_data;
129 if (md && md->usage == 0)
133 mutex_unlock(&open_lock);
138 static inline int mmc_get_devidx(struct gendisk *disk)
140 int devmaj = MAJOR(disk_devt(disk));
141 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
144 devidx = disk->first_minor / perdev_minors;
148 static void mmc_blk_put(struct mmc_blk_data *md)
150 mutex_lock(&open_lock);
152 if (md->usage == 0) {
153 int devidx = mmc_get_devidx(md->disk);
154 blk_cleanup_queue(md->queue.queue);
156 __clear_bit(devidx, dev_use);
161 mutex_unlock(&open_lock);
164 static ssize_t power_ro_lock_show(struct device *dev,
165 struct device_attribute *attr, char *buf)
168 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
169 struct mmc_card *card = md->queue.card;
172 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
174 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
177 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
182 static ssize_t power_ro_lock_store(struct device *dev,
183 struct device_attribute *attr, const char *buf, size_t count)
186 struct mmc_blk_data *md, *part_md;
187 struct mmc_card *card;
190 if (kstrtoul(buf, 0, &set))
196 md = mmc_blk_get(dev_to_disk(dev));
197 card = md->queue.card;
199 mmc_claim_host(card->host);
201 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
202 card->ext_csd.boot_ro_lock |
203 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
204 card->ext_csd.part_time);
206 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
208 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
210 mmc_release_host(card->host);
213 pr_info("%s: Locking boot partition ro until next power on\n",
214 md->disk->disk_name);
215 set_disk_ro(md->disk, 1);
217 list_for_each_entry(part_md, &md->part, part)
218 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
219 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
220 set_disk_ro(part_md->disk, 1);
228 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
232 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
234 ret = snprintf(buf, PAGE_SIZE, "%d",
235 get_disk_ro(dev_to_disk(dev)) ^
241 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
242 const char *buf, size_t count)
246 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
247 unsigned long set = simple_strtoul(buf, &end, 0);
253 set_disk_ro(dev_to_disk(dev), set || md->read_only);
260 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
262 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
265 mutex_lock(&block_mutex);
268 check_disk_change(bdev);
271 if ((mode & FMODE_WRITE) && md->read_only) {
276 mutex_unlock(&block_mutex);
281 static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
283 struct mmc_blk_data *md = disk->private_data;
285 mutex_lock(&block_mutex);
287 mutex_unlock(&block_mutex);
292 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
294 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
300 struct mmc_blk_ioc_data {
301 struct mmc_ioc_cmd ic;
306 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
307 struct mmc_ioc_cmd __user *user)
309 struct mmc_blk_ioc_data *idata;
312 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
318 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
323 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
324 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
329 if (!idata->buf_bytes)
332 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
338 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
339 idata->ic.data_ptr, idata->buf_bytes)) {
354 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
360 if (!status || !retries_max)
364 err = get_card_status(card, status, 5);
368 if (!R1_STATUS(*status) &&
369 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
370 break; /* RPMB programming operation complete */
373 * Rechedule to give the MMC device a chance to continue
374 * processing the previous command without being polled too
377 usleep_range(1000, 5000);
378 } while (++retry_count < retries_max);
380 if (retry_count == retries_max)
386 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
387 struct mmc_ioc_cmd __user *ic_ptr)
389 struct mmc_blk_ioc_data *idata;
390 struct mmc_blk_data *md;
391 struct mmc_card *card;
392 struct mmc_command cmd = {0};
393 struct mmc_data data = {0};
394 struct mmc_request mrq = {NULL};
395 struct scatterlist sg;
401 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
402 * whole block device, not on a partition. This prevents overspray
403 * between sibling partitions.
405 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
408 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
410 return PTR_ERR(idata);
412 md = mmc_blk_get(bdev->bd_disk);
418 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
421 card = md->queue.card;
427 cmd.opcode = idata->ic.opcode;
428 cmd.arg = idata->ic.arg;
429 cmd.flags = idata->ic.flags;
431 if (idata->buf_bytes) {
434 data.blksz = idata->ic.blksz;
435 data.blocks = idata->ic.blocks;
437 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
439 if (idata->ic.write_flag)
440 data.flags = MMC_DATA_WRITE;
442 data.flags = MMC_DATA_READ;
444 /* data.flags must already be set before doing this. */
445 mmc_set_data_timeout(&data, card);
447 /* Allow overriding the timeout_ns for empirical tuning. */
448 if (idata->ic.data_timeout_ns)
449 data.timeout_ns = idata->ic.data_timeout_ns;
451 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
453 * Pretend this is a data transfer and rely on the
454 * host driver to compute timeout. When all host
455 * drivers support cmd.cmd_timeout for R1B, this
459 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
461 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
469 mmc_claim_host(card->host);
471 err = mmc_blk_part_switch(card, md);
475 if (idata->ic.is_acmd) {
476 err = mmc_app_cmd(card->host, card);
482 err = mmc_set_blockcount(card, data.blocks,
483 idata->ic.write_flag & (1 << 31));
488 mmc_wait_for_req(card->host, &mrq);
491 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
492 __func__, cmd.error);
497 dev_err(mmc_dev(card->host), "%s: data error %d\n",
498 __func__, data.error);
504 * According to the SD specs, some commands require a delay after
505 * issuing the command.
507 if (idata->ic.postsleep_min_us)
508 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
510 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
515 if (!idata->ic.write_flag) {
516 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
517 idata->buf, idata->buf_bytes)) {
525 * Ensure RPMB command has completed by polling CMD13
528 err = ioctl_rpmb_card_status_poll(card, &status, 5);
530 dev_err(mmc_dev(card->host),
531 "%s: Card Status=0x%08X, error %d\n",
532 __func__, status, err);
536 mmc_release_host(card->host);
546 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
547 unsigned int cmd, unsigned long arg)
550 if (cmd == MMC_IOC_CMD)
551 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
556 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
557 unsigned int cmd, unsigned long arg)
559 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
563 static const struct block_device_operations mmc_bdops = {
564 .open = mmc_blk_open,
565 .release = mmc_blk_release,
566 .getgeo = mmc_blk_getgeo,
567 .owner = THIS_MODULE,
568 .ioctl = mmc_blk_ioctl,
570 .compat_ioctl = mmc_blk_compat_ioctl,
574 static inline int mmc_blk_part_switch(struct mmc_card *card,
575 struct mmc_blk_data *md)
578 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
580 if (main_md->part_curr == md->part_type)
583 if (mmc_card_mmc(card)) {
584 u8 part_config = card->ext_csd.part_config;
586 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
587 part_config |= md->part_type;
589 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
590 EXT_CSD_PART_CONFIG, part_config,
591 card->ext_csd.part_time);
595 card->ext_csd.part_config = part_config;
598 main_md->part_curr = md->part_type;
602 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
608 struct mmc_request mrq = {NULL};
609 struct mmc_command cmd = {0};
610 struct mmc_data data = {0};
612 struct scatterlist sg;
614 cmd.opcode = MMC_APP_CMD;
615 cmd.arg = card->rca << 16;
616 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
618 err = mmc_wait_for_cmd(card->host, &cmd, 0);
621 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
624 memset(&cmd, 0, sizeof(struct mmc_command));
626 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
628 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
632 data.flags = MMC_DATA_READ;
635 mmc_set_data_timeout(&data, card);
640 blocks = kmalloc(4, GFP_KERNEL);
644 sg_init_one(&sg, blocks, 4);
646 mmc_wait_for_req(card->host, &mrq);
648 result = ntohl(*blocks);
651 if (cmd.error || data.error)
657 static int send_stop(struct mmc_card *card, u32 *status)
659 struct mmc_command cmd = {0};
662 cmd.opcode = MMC_STOP_TRANSMISSION;
663 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
664 err = mmc_wait_for_cmd(card->host, &cmd, 5);
666 *status = cmd.resp[0];
670 static int get_card_status(struct mmc_card *card, u32 *status, int retries)
672 struct mmc_command cmd = {0};
675 cmd.opcode = MMC_SEND_STATUS;
676 if (!mmc_host_is_spi(card->host))
677 cmd.arg = card->rca << 16;
678 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
679 err = mmc_wait_for_cmd(card->host, &cmd, retries);
681 *status = cmd.resp[0];
685 #define ERR_NOMEDIUM 3
688 #define ERR_CONTINUE 0
690 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
691 bool status_valid, u32 status)
695 /* response crc error, retry the r/w cmd */
696 pr_err("%s: %s sending %s command, card status %#x\n",
697 req->rq_disk->disk_name, "response CRC error",
702 pr_err("%s: %s sending %s command, card status %#x\n",
703 req->rq_disk->disk_name, "timed out", name, status);
705 /* If the status cmd initially failed, retry the r/w cmd */
710 * If it was a r/w cmd crc error, or illegal command
711 * (eg, issued in wrong state) then retry - we should
712 * have corrected the state problem above.
714 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
717 /* Otherwise abort the command */
721 /* We don't understand the error code the driver gave us */
722 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
723 req->rq_disk->disk_name, error, status);
729 * Initial r/w and stop cmd error recovery.
730 * We don't know whether the card received the r/w cmd or not, so try to
731 * restore things back to a sane state. Essentially, we do this as follows:
732 * - Obtain card status. If the first attempt to obtain card status fails,
733 * the status word will reflect the failed status cmd, not the failed
734 * r/w cmd. If we fail to obtain card status, it suggests we can no
735 * longer communicate with the card.
736 * - Check the card state. If the card received the cmd but there was a
737 * transient problem with the response, it might still be in a data transfer
738 * mode. Try to send it a stop command. If this fails, we can't recover.
739 * - If the r/w cmd failed due to a response CRC error, it was probably
740 * transient, so retry the cmd.
741 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
742 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
743 * illegal cmd, retry.
744 * Otherwise we don't understand what happened, so abort.
746 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
747 struct mmc_blk_request *brq, int *ecc_err)
749 bool prev_cmd_status_valid = true;
750 u32 status, stop_status = 0;
753 if (mmc_card_removed(card))
757 * Try to get card status which indicates both the card state
758 * and why there was no response. If the first attempt fails,
759 * we can't be sure the returned status is for the r/w command.
761 for (retry = 2; retry >= 0; retry--) {
762 err = get_card_status(card, &status, 0);
766 prev_cmd_status_valid = false;
767 pr_err("%s: error %d sending status command, %sing\n",
768 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
771 /* We couldn't get a response from the card. Give up. */
773 /* Check if the card is removed */
774 if (mmc_detect_card_removed(card->host))
779 /* Flag ECC errors */
780 if ((status & R1_CARD_ECC_FAILED) ||
781 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
782 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
786 * Check the current card state. If it is in some data transfer
787 * mode, tell it to stop (and hopefully transition back to TRAN.)
789 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
790 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
791 err = send_stop(card, &stop_status);
793 pr_err("%s: error %d sending stop command\n",
794 req->rq_disk->disk_name, err);
797 * If the stop cmd also timed out, the card is probably
798 * not present, so abort. Other errors are bad news too.
802 if (stop_status & R1_CARD_ECC_FAILED)
806 /* Check for set block count errors */
808 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
809 prev_cmd_status_valid, status);
811 /* Check for r/w command errors */
813 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
814 prev_cmd_status_valid, status);
817 if (!brq->stop.error)
820 /* Now for stop errors. These aren't fatal to the transfer. */
821 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
822 req->rq_disk->disk_name, brq->stop.error,
823 brq->cmd.resp[0], status);
826 * Subsitute in our own stop status as this will give the error
827 * state which happened during the execution of the r/w command.
830 brq->stop.resp[0] = stop_status;
836 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
841 if (md->reset_done & type)
844 md->reset_done |= type;
845 err = mmc_hw_reset(host);
846 /* Ensure we switch back to the correct partition */
847 if (err != -EOPNOTSUPP) {
848 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
851 main_md->part_curr = main_md->part_type;
852 part_err = mmc_blk_part_switch(host->card, md);
855 * We have failed to get back into the correct
856 * partition, so we need to abort the whole request.
864 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
866 md->reset_done &= ~type;
869 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
871 struct mmc_blk_data *md = mq->data;
872 struct mmc_card *card = md->queue.card;
873 unsigned int from, nr, arg;
874 int err = 0, type = MMC_BLK_DISCARD;
876 if (!mmc_can_erase(card)) {
881 from = blk_rq_pos(req);
882 nr = blk_rq_sectors(req);
884 if (mmc_can_discard(card))
885 arg = MMC_DISCARD_ARG;
886 else if (mmc_can_trim(card))
891 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
892 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
893 INAND_CMD38_ARG_EXT_CSD,
894 arg == MMC_TRIM_ARG ?
895 INAND_CMD38_ARG_TRIM :
896 INAND_CMD38_ARG_ERASE,
901 err = mmc_erase(card, from, nr, arg);
903 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
906 mmc_blk_reset_success(md, type);
907 blk_end_request(req, err, blk_rq_bytes(req));
912 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
915 struct mmc_blk_data *md = mq->data;
916 struct mmc_card *card = md->queue.card;
917 unsigned int from, nr, arg, trim_arg, erase_arg;
918 int err = 0, type = MMC_BLK_SECDISCARD;
920 if (!(mmc_can_secure_erase_trim(card) || mmc_can_sanitize(card))) {
925 from = blk_rq_pos(req);
926 nr = blk_rq_sectors(req);
928 /* The sanitize operation is supported at v4.5 only */
929 if (mmc_can_sanitize(card)) {
930 erase_arg = MMC_ERASE_ARG;
931 trim_arg = MMC_TRIM_ARG;
933 erase_arg = MMC_SECURE_ERASE_ARG;
934 trim_arg = MMC_SECURE_TRIM1_ARG;
937 if (mmc_erase_group_aligned(card, from, nr))
939 else if (mmc_can_trim(card))
946 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
947 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
948 INAND_CMD38_ARG_EXT_CSD,
949 arg == MMC_SECURE_TRIM1_ARG ?
950 INAND_CMD38_ARG_SECTRIM1 :
951 INAND_CMD38_ARG_SECERASE,
957 err = mmc_erase(card, from, nr, arg);
963 if (arg == MMC_SECURE_TRIM1_ARG) {
964 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
965 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
966 INAND_CMD38_ARG_EXT_CSD,
967 INAND_CMD38_ARG_SECTRIM2,
973 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
980 if (mmc_can_sanitize(card))
981 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
982 EXT_CSD_SANITIZE_START, 1, 0);
984 if (err && !mmc_blk_reset(md, card->host, type))
987 mmc_blk_reset_success(md, type);
989 blk_end_request(req, err, blk_rq_bytes(req));
994 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
996 struct mmc_blk_data *md = mq->data;
997 struct mmc_card *card = md->queue.card;
1000 ret = mmc_flush_cache(card);
1004 blk_end_request_all(req, ret);
1010 * Reformat current write as a reliable write, supporting
1011 * both legacy and the enhanced reliable write MMC cards.
1012 * In each transfer we'll handle only as much as a single
1013 * reliable write can handle, thus finish the request in
1014 * partial completions.
1016 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1017 struct mmc_card *card,
1018 struct request *req)
1020 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1021 /* Legacy mode imposes restrictions on transfers. */
1022 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1023 brq->data.blocks = 1;
1025 if (brq->data.blocks > card->ext_csd.rel_sectors)
1026 brq->data.blocks = card->ext_csd.rel_sectors;
1027 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1028 brq->data.blocks = 1;
1032 #define CMD_ERRORS \
1033 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1034 R1_ADDRESS_ERROR | /* Misaligned address */ \
1035 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1036 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1037 R1_CC_ERROR | /* Card controller error */ \
1038 R1_ERROR) /* General/unknown error */
1040 static int mmc_blk_err_check(struct mmc_card *card,
1041 struct mmc_async_req *areq)
1043 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1045 struct mmc_blk_request *brq = &mq_mrq->brq;
1046 struct request *req = mq_mrq->req;
1050 * sbc.error indicates a problem with the set block count
1051 * command. No data will have been transferred.
1053 * cmd.error indicates a problem with the r/w command. No
1054 * data will have been transferred.
1056 * stop.error indicates a problem with the stop command. Data
1057 * may have been transferred, or may still be transferring.
1059 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1061 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
1063 return MMC_BLK_RETRY;
1065 return MMC_BLK_ABORT;
1067 return MMC_BLK_NOMEDIUM;
1074 * Check for errors relating to the execution of the
1075 * initial command - such as address errors. No data
1076 * has been transferred.
1078 if (brq->cmd.resp[0] & CMD_ERRORS) {
1079 pr_err("%s: r/w command failed, status = %#x\n",
1080 req->rq_disk->disk_name, brq->cmd.resp[0]);
1081 return MMC_BLK_ABORT;
1085 * Everything else is either success, or a data error of some
1086 * kind. If it was a write, we may have transitioned to
1087 * program mode, which we have to wait for it to complete.
1089 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1091 unsigned long timeout;
1093 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
1095 int err = get_card_status(card, &status, 5);
1097 pr_err("%s: error %d requesting status\n",
1098 req->rq_disk->disk_name, err);
1099 return MMC_BLK_CMD_ERR;
1102 /* Timeout if the device never becomes ready for data
1103 * and never leaves the program state.
1105 if (time_after(jiffies, timeout)) {
1106 pr_err("%s: Card stuck in programming state!"\
1107 " %s %s\n", mmc_hostname(card->host),
1108 req->rq_disk->disk_name, __func__);
1110 return MMC_BLK_CMD_ERR;
1113 * Some cards mishandle the status bits,
1114 * so make sure to check both the busy
1115 * indication and the card state.
1117 } while (!(status & R1_READY_FOR_DATA) ||
1118 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1121 if (brq->data.error) {
1122 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1123 req->rq_disk->disk_name, brq->data.error,
1124 (unsigned)blk_rq_pos(req),
1125 (unsigned)blk_rq_sectors(req),
1126 brq->cmd.resp[0], brq->stop.resp[0]);
1128 if (rq_data_dir(req) == READ) {
1130 return MMC_BLK_ECC_ERR;
1131 return MMC_BLK_DATA_ERR;
1133 return MMC_BLK_CMD_ERR;
1137 if (!brq->data.bytes_xfered)
1138 return MMC_BLK_RETRY;
1140 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1141 return MMC_BLK_PARTIAL;
1143 return MMC_BLK_SUCCESS;
1146 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1147 struct mmc_card *card,
1149 struct mmc_queue *mq)
1151 u32 readcmd, writecmd;
1152 struct mmc_blk_request *brq = &mqrq->brq;
1153 struct request *req = mqrq->req;
1154 struct mmc_blk_data *md = mq->data;
1158 * Reliable writes are used to implement Forced Unit Access and
1159 * REQ_META accesses, and are supported only on MMCs.
1161 * XXX: this really needs a good explanation of why REQ_META
1162 * is treated special.
1164 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1165 (req->cmd_flags & REQ_META)) &&
1166 (rq_data_dir(req) == WRITE) &&
1167 (md->flags & MMC_BLK_REL_WR);
1169 memset(brq, 0, sizeof(struct mmc_blk_request));
1170 brq->mrq.cmd = &brq->cmd;
1171 brq->mrq.data = &brq->data;
1173 brq->cmd.arg = blk_rq_pos(req);
1174 if (!mmc_card_blockaddr(card))
1176 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1177 brq->data.blksz = 512;
1178 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1180 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1181 brq->data.blocks = blk_rq_sectors(req);
1184 * The block layer doesn't support all sector count
1185 * restrictions, so we need to be prepared for too big
1188 if (brq->data.blocks > card->host->max_blk_count)
1189 brq->data.blocks = card->host->max_blk_count;
1191 if (brq->data.blocks > 1) {
1193 * After a read error, we redo the request one sector
1194 * at a time in order to accurately determine which
1195 * sectors can be read successfully.
1198 brq->data.blocks = 1;
1200 /* Some controllers can't do multiblock reads due to hw bugs */
1201 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1202 rq_data_dir(req) == READ)
1203 brq->data.blocks = 1;
1206 if (brq->data.blocks > 1 || do_rel_wr) {
1207 /* SPI multiblock writes terminate using a special
1208 * token, not a STOP_TRANSMISSION request.
1210 if (!mmc_host_is_spi(card->host) ||
1211 rq_data_dir(req) == READ)
1212 brq->mrq.stop = &brq->stop;
1213 readcmd = MMC_READ_MULTIPLE_BLOCK;
1214 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1216 brq->mrq.stop = NULL;
1217 readcmd = MMC_READ_SINGLE_BLOCK;
1218 writecmd = MMC_WRITE_BLOCK;
1220 if (rq_data_dir(req) == READ) {
1221 brq->cmd.opcode = readcmd;
1222 brq->data.flags |= MMC_DATA_READ;
1224 brq->cmd.opcode = writecmd;
1225 brq->data.flags |= MMC_DATA_WRITE;
1229 mmc_apply_rel_rw(brq, card, req);
1232 * Data tag is used only during writing meta data to speed
1233 * up write and any subsequent read of this meta data
1235 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1236 (req->cmd_flags & REQ_META) &&
1237 (rq_data_dir(req) == WRITE) &&
1238 ((brq->data.blocks * brq->data.blksz) >=
1239 card->ext_csd.data_tag_unit_size);
1242 * Pre-defined multi-block transfers are preferable to
1243 * open ended-ones (and necessary for reliable writes).
1244 * However, it is not sufficient to just send CMD23,
1245 * and avoid the final CMD12, as on an error condition
1246 * CMD12 (stop) needs to be sent anyway. This, coupled
1247 * with Auto-CMD23 enhancements provided by some
1248 * hosts, means that the complexity of dealing
1249 * with this is best left to the host. If CMD23 is
1250 * supported by card and host, we'll fill sbc in and let
1251 * the host deal with handling it correctly. This means
1252 * that for hosts that don't expose MMC_CAP_CMD23, no
1253 * change of behavior will be observed.
1255 * N.B: Some MMC cards experience perf degradation.
1256 * We'll avoid using CMD23-bounded multiblock writes for
1257 * these, while retaining features like reliable writes.
1259 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1260 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1262 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1263 brq->sbc.arg = brq->data.blocks |
1264 (do_rel_wr ? (1 << 31) : 0) |
1265 (do_data_tag ? (1 << 29) : 0);
1266 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1267 brq->mrq.sbc = &brq->sbc;
1270 mmc_set_data_timeout(&brq->data, card);
1272 brq->data.sg = mqrq->sg;
1273 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1276 * Adjust the sg list so it is the same size as the
1279 if (brq->data.blocks != blk_rq_sectors(req)) {
1280 int i, data_size = brq->data.blocks << 9;
1281 struct scatterlist *sg;
1283 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1284 data_size -= sg->length;
1285 if (data_size <= 0) {
1286 sg->length += data_size;
1291 brq->data.sg_len = i;
1294 mqrq->mmc_active.mrq = &brq->mrq;
1295 mqrq->mmc_active.err_check = mmc_blk_err_check;
1297 mmc_queue_bounce_pre(mqrq);
1300 static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1301 struct mmc_blk_request *brq, struct request *req,
1305 * If this is an SD card and we're writing, we can first
1306 * mark the known good sectors as ok.
1308 * If the card is not SD, we can still ok written sectors
1309 * as reported by the controller (which might be less than
1310 * the real number of written sectors, but never more).
1312 if (mmc_card_sd(card)) {
1315 blocks = mmc_sd_num_wr_blocks(card);
1316 if (blocks != (u32)-1) {
1317 ret = blk_end_request(req, 0, blocks << 9);
1320 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1325 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
1327 struct mmc_blk_data *md = mq->data;
1328 struct mmc_card *card = md->queue.card;
1329 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
1330 int ret = 1, disable_multi = 0, retry = 0, type;
1331 enum mmc_blk_status status;
1332 struct mmc_queue_req *mq_rq;
1333 struct request *req = rqc;
1334 struct mmc_async_req *areq;
1336 if (!rqc && !mq->mqrq_prev->req)
1342 * When 4KB native sector is enabled, only 8 blocks
1343 * multiple read or write is allowed
1345 if ((brq->data.blocks & 0x07) &&
1346 (card->ext_csd.data_sector_size == 4096)) {
1347 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1348 req->rq_disk->disk_name);
1351 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1352 areq = &mq->mqrq_cur->mmc_active;
1355 areq = mmc_start_req(card->host, areq, (int *) &status);
1357 if (status == MMC_BLK_NEW_REQUEST)
1358 mq->flags |= MMC_QUEUE_NEW_REQUEST;
1362 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1365 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1366 mmc_queue_bounce_post(mq_rq);
1369 case MMC_BLK_SUCCESS:
1370 case MMC_BLK_PARTIAL:
1372 * A block was successfully transferred.
1374 mmc_blk_reset_success(md, type);
1375 ret = blk_end_request(req, 0,
1376 brq->data.bytes_xfered);
1378 * If the blk_end_request function returns non-zero even
1379 * though all data has been transferred and no errors
1380 * were returned by the host controller, it's a bug.
1382 if (status == MMC_BLK_SUCCESS && ret) {
1383 pr_err("%s BUG rq_tot %d d_xfer %d\n",
1384 __func__, blk_rq_bytes(req),
1385 brq->data.bytes_xfered);
1390 case MMC_BLK_CMD_ERR:
1391 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1392 if (!mmc_blk_reset(md, card->host, type))
1400 if (!mmc_blk_reset(md, card->host, type))
1403 case MMC_BLK_DATA_ERR: {
1406 err = mmc_blk_reset(md, card->host, type);
1413 case MMC_BLK_ECC_ERR:
1414 if (brq->data.blocks > 1) {
1415 /* Redo read one sector at a time */
1416 pr_warning("%s: retrying using single block read\n",
1417 req->rq_disk->disk_name);
1422 * After an error, we redo I/O one sector at a
1423 * time, so we only reach here after trying to
1424 * read a single sector.
1426 ret = blk_end_request(req, -EIO,
1431 case MMC_BLK_NOMEDIUM:
1434 pr_err("%s: Unhandled return value (%d)",
1435 req->rq_disk->disk_name, status);
1441 * In case of a incomplete request
1442 * prepare it again and resend.
1444 mmc_blk_rw_rq_prep(mq_rq, card, disable_multi, mq);
1445 mmc_start_req(card->host, &mq_rq->mmc_active, NULL);
1452 if (mmc_card_removed(card))
1453 req->cmd_flags |= REQ_QUIET;
1455 ret = blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
1459 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1460 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1466 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1469 struct mmc_blk_data *md = mq->data;
1470 struct mmc_card *card = md->queue.card;
1471 struct mmc_host *host = card->host;
1472 unsigned long flags;
1474 if (req && !mq->mqrq_prev->req)
1475 /* claim host only for the first request */
1476 mmc_claim_host(card->host);
1478 ret = mmc_blk_part_switch(card, md);
1481 blk_end_request_all(req, -EIO);
1487 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
1488 if (req && req->cmd_flags & REQ_DISCARD) {
1489 /* complete ongoing async transfer before issuing discard */
1490 if (card->host->areq)
1491 mmc_blk_issue_rw_rq(mq, NULL);
1492 if (req->cmd_flags & REQ_SECURE &&
1493 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
1494 ret = mmc_blk_issue_secdiscard_rq(mq, req);
1496 ret = mmc_blk_issue_discard_rq(mq, req);
1497 } else if (req && req->cmd_flags & REQ_FLUSH) {
1498 /* complete ongoing async transfer before issuing flush */
1499 if (card->host->areq)
1500 mmc_blk_issue_rw_rq(mq, NULL);
1501 ret = mmc_blk_issue_flush(mq, req);
1503 if (!req && host->areq) {
1504 spin_lock_irqsave(&host->context_info.lock, flags);
1505 host->context_info.is_waiting_last_req = true;
1506 spin_unlock_irqrestore(&host->context_info.lock, flags);
1508 ret = mmc_blk_issue_rw_rq(mq, req);
1512 if (!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST))
1513 /* release host only when there are no more requests */
1514 mmc_release_host(card->host);
1518 static inline int mmc_blk_readonly(struct mmc_card *card)
1520 return mmc_card_readonly(card) ||
1521 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1524 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1525 struct device *parent,
1528 const char *subname,
1531 struct mmc_blk_data *md;
1534 devidx = find_first_zero_bit(dev_use, max_devices);
1535 if (devidx >= max_devices)
1536 return ERR_PTR(-ENOSPC);
1537 __set_bit(devidx, dev_use);
1539 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
1546 * !subname implies we are creating main mmc_blk_data that will be
1547 * associated with mmc_card with mmc_set_drvdata. Due to device
1548 * partitions, devidx will not coincide with a per-physical card
1549 * index anymore so we keep track of a name index.
1552 md->name_idx = find_first_zero_bit(name_use, max_devices);
1553 __set_bit(md->name_idx, name_use);
1555 md->name_idx = ((struct mmc_blk_data *)
1556 dev_to_disk(parent)->private_data)->name_idx;
1558 md->area_type = area_type;
1561 * Set the read-only status based on the supported commands
1562 * and the write protect switch.
1564 md->read_only = mmc_blk_readonly(card);
1566 md->disk = alloc_disk(perdev_minors);
1567 if (md->disk == NULL) {
1572 spin_lock_init(&md->lock);
1573 INIT_LIST_HEAD(&md->part);
1576 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
1580 md->queue.issue_fn = mmc_blk_issue_rq;
1581 md->queue.data = md;
1583 md->disk->major = MMC_BLOCK_MAJOR;
1584 md->disk->first_minor = devidx * perdev_minors;
1585 md->disk->fops = &mmc_bdops;
1586 md->disk->private_data = md;
1587 md->disk->queue = md->queue.queue;
1588 md->disk->driverfs_dev = parent;
1589 set_disk_ro(md->disk, md->read_only || default_ro);
1590 if (area_type & MMC_BLK_DATA_AREA_RPMB)
1591 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
1594 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1596 * - be set for removable media with permanent block devices
1597 * - be unset for removable block devices with permanent media
1599 * Since MMC block devices clearly fall under the second
1600 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1601 * should use the block device creation/destruction hotplug
1602 * messages to tell when the card is present.
1605 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1606 "mmcblk%d%s", md->name_idx, subname ? subname : "");
1608 if (mmc_card_mmc(card))
1609 blk_queue_logical_block_size(md->queue.queue,
1610 card->ext_csd.data_sector_size);
1612 blk_queue_logical_block_size(md->queue.queue, 512);
1614 set_capacity(md->disk, size);
1616 if (mmc_host_cmd23(card->host)) {
1617 if (mmc_card_mmc(card) ||
1618 (mmc_card_sd(card) &&
1619 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1620 md->flags |= MMC_BLK_CMD23;
1623 if (mmc_card_mmc(card) &&
1624 md->flags & MMC_BLK_CMD23 &&
1625 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1626 card->ext_csd.rel_sectors)) {
1627 md->flags |= MMC_BLK_REL_WR;
1628 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1638 return ERR_PTR(ret);
1641 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1644 struct mmc_blk_data *md;
1646 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1648 * The EXT_CSD sector count is in number or 512 byte
1651 size = card->ext_csd.sectors;
1654 * The CSD capacity field is in units of read_blkbits.
1655 * set_capacity takes units of 512 bytes.
1657 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1660 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
1661 MMC_BLK_DATA_AREA_MAIN);
1665 static int mmc_blk_alloc_part(struct mmc_card *card,
1666 struct mmc_blk_data *md,
1667 unsigned int part_type,
1670 const char *subname,
1674 struct mmc_blk_data *part_md;
1676 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1677 subname, area_type);
1678 if (IS_ERR(part_md))
1679 return PTR_ERR(part_md);
1680 part_md->part_type = part_type;
1681 list_add(&part_md->part, &md->part);
1683 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1684 cap_str, sizeof(cap_str));
1685 pr_info("%s: %s %s partition %u %s\n",
1686 part_md->disk->disk_name, mmc_card_id(card),
1687 mmc_card_name(card), part_md->part_type, cap_str);
1691 /* MMC Physical partitions consist of two boot partitions and
1692 * up to four general purpose partitions.
1693 * For each partition enabled in EXT_CSD a block device will be allocatedi
1694 * to provide access to the partition.
1697 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1701 if (!mmc_card_mmc(card))
1704 for (idx = 0; idx < card->nr_parts; idx++) {
1705 if (card->part[idx].size) {
1706 ret = mmc_blk_alloc_part(card, md,
1707 card->part[idx].part_cfg,
1708 card->part[idx].size >> 9,
1709 card->part[idx].force_ro,
1710 card->part[idx].name,
1711 card->part[idx].area_type);
1720 static void mmc_blk_remove_req(struct mmc_blk_data *md)
1722 struct mmc_card *card;
1725 card = md->queue.card;
1726 if (md->disk->flags & GENHD_FL_UP) {
1727 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1728 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
1729 card->ext_csd.boot_ro_lockable)
1730 device_remove_file(disk_to_dev(md->disk),
1731 &md->power_ro_lock);
1733 /* Stop new requests from getting into the queue */
1734 del_gendisk(md->disk);
1737 /* Then flush out any already in there */
1738 mmc_cleanup_queue(&md->queue);
1743 static void mmc_blk_remove_parts(struct mmc_card *card,
1744 struct mmc_blk_data *md)
1746 struct list_head *pos, *q;
1747 struct mmc_blk_data *part_md;
1749 __clear_bit(md->name_idx, name_use);
1750 list_for_each_safe(pos, q, &md->part) {
1751 part_md = list_entry(pos, struct mmc_blk_data, part);
1753 mmc_blk_remove_req(part_md);
1757 static int mmc_add_disk(struct mmc_blk_data *md)
1760 struct mmc_card *card = md->queue.card;
1763 md->force_ro.show = force_ro_show;
1764 md->force_ro.store = force_ro_store;
1765 sysfs_attr_init(&md->force_ro.attr);
1766 md->force_ro.attr.name = "force_ro";
1767 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1768 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1772 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
1773 card->ext_csd.boot_ro_lockable) {
1776 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
1779 mode = S_IRUGO | S_IWUSR;
1781 md->power_ro_lock.show = power_ro_lock_show;
1782 md->power_ro_lock.store = power_ro_lock_store;
1783 sysfs_attr_init(&md->power_ro_lock.attr);
1784 md->power_ro_lock.attr.mode = mode;
1785 md->power_ro_lock.attr.name =
1786 "ro_lock_until_next_power_on";
1787 ret = device_create_file(disk_to_dev(md->disk),
1788 &md->power_ro_lock);
1790 goto power_ro_lock_fail;
1795 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1797 del_gendisk(md->disk);
1802 #define CID_MANFID_SANDISK 0x2
1803 #define CID_MANFID_TOSHIBA 0x11
1804 #define CID_MANFID_MICRON 0x13
1805 #define CID_MANFID_SAMSUNG 0x15
1807 static const struct mmc_fixup blk_fixups[] =
1809 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
1810 MMC_QUIRK_INAND_CMD38),
1811 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
1812 MMC_QUIRK_INAND_CMD38),
1813 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
1814 MMC_QUIRK_INAND_CMD38),
1815 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
1816 MMC_QUIRK_INAND_CMD38),
1817 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
1818 MMC_QUIRK_INAND_CMD38),
1821 * Some MMC cards experience performance degradation with CMD23
1822 * instead of CMD12-bounded multiblock transfers. For now we'll
1823 * black list what's bad...
1824 * - Certain Toshiba cards.
1826 * N.B. This doesn't affect SD cards.
1828 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
1829 MMC_QUIRK_BLK_NO_CMD23),
1830 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
1831 MMC_QUIRK_BLK_NO_CMD23),
1832 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
1833 MMC_QUIRK_BLK_NO_CMD23),
1836 * Some Micron MMC cards needs longer data read timeout than
1839 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
1840 MMC_QUIRK_LONG_READ_TIME),
1843 * On these Samsung MoviNAND parts, performing secure erase or
1844 * secure trim can result in unrecoverable corruption due to a
1847 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1848 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1849 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1850 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1851 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1852 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1853 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1854 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1855 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1856 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1857 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1858 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1859 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1860 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1861 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1862 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1867 static int mmc_blk_probe(struct mmc_card *card)
1869 struct mmc_blk_data *md, *part_md;
1873 * Check that the card supports the command class(es) we need.
1875 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
1878 md = mmc_blk_alloc(card);
1882 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
1883 cap_str, sizeof(cap_str));
1884 pr_info("%s: %s %s %s %s\n",
1885 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
1886 cap_str, md->read_only ? "(ro)" : "");
1888 if (mmc_blk_alloc_parts(card, md))
1891 mmc_set_drvdata(card, md);
1892 mmc_fixup_device(card, blk_fixups);
1894 if (mmc_add_disk(md))
1897 list_for_each_entry(part_md, &md->part, part) {
1898 if (mmc_add_disk(part_md))
1904 mmc_blk_remove_parts(card, md);
1905 mmc_blk_remove_req(md);
1909 static void mmc_blk_remove(struct mmc_card *card)
1911 struct mmc_blk_data *md = mmc_get_drvdata(card);
1913 mmc_blk_remove_parts(card, md);
1914 mmc_claim_host(card->host);
1915 mmc_blk_part_switch(card, md);
1916 mmc_release_host(card->host);
1917 mmc_blk_remove_req(md);
1918 mmc_set_drvdata(card, NULL);
1922 static int mmc_blk_suspend(struct mmc_card *card)
1924 struct mmc_blk_data *part_md;
1925 struct mmc_blk_data *md = mmc_get_drvdata(card);
1928 mmc_queue_suspend(&md->queue);
1929 list_for_each_entry(part_md, &md->part, part) {
1930 mmc_queue_suspend(&part_md->queue);
1936 static int mmc_blk_resume(struct mmc_card *card)
1938 struct mmc_blk_data *part_md;
1939 struct mmc_blk_data *md = mmc_get_drvdata(card);
1943 * Resume involves the card going into idle state,
1944 * so current partition is always the main one.
1946 md->part_curr = md->part_type;
1947 mmc_queue_resume(&md->queue);
1948 list_for_each_entry(part_md, &md->part, part) {
1949 mmc_queue_resume(&part_md->queue);
1955 #define mmc_blk_suspend NULL
1956 #define mmc_blk_resume NULL
1959 static struct mmc_driver mmc_driver = {
1963 .probe = mmc_blk_probe,
1964 .remove = mmc_blk_remove,
1965 .suspend = mmc_blk_suspend,
1966 .resume = mmc_blk_resume,
1969 static int __init mmc_blk_init(void)
1973 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1974 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1976 max_devices = 256 / perdev_minors;
1978 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1982 res = mmc_register_driver(&mmc_driver);
1988 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1993 static void __exit mmc_blk_exit(void)
1995 mmc_unregister_driver(&mmc_driver);
1996 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1999 module_init(mmc_blk_init);
2000 module_exit(mmc_blk_exit);
2002 MODULE_LICENSE("GPL");
2003 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");