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>
38 #include <linux/idr.h>
40 #include <linux/mmc/ioctl.h>
41 #include <linux/mmc/card.h>
42 #include <linux/mmc/host.h>
43 #include <linux/mmc/mmc.h>
44 #include <linux/mmc/sd.h>
46 #include <asm/uaccess.h>
51 MODULE_ALIAS("mmc:block");
52 #ifdef MODULE_PARAM_PREFIX
53 #undef MODULE_PARAM_PREFIX
55 #define MODULE_PARAM_PREFIX "mmcblk."
57 #define INAND_CMD38_ARG_EXT_CSD 113
58 #define INAND_CMD38_ARG_ERASE 0x00
59 #define INAND_CMD38_ARG_TRIM 0x01
60 #define INAND_CMD38_ARG_SECERASE 0x80
61 #define INAND_CMD38_ARG_SECTRIM1 0x81
62 #define INAND_CMD38_ARG_SECTRIM2 0x88
63 #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
64 #define MMC_SANITIZE_REQ_TIMEOUT 240000
65 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
67 #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
68 (rq_data_dir(req) == WRITE))
69 #define PACKED_CMD_VER 0x01
70 #define PACKED_CMD_WR 0x02
72 static DEFINE_MUTEX(block_mutex);
75 * The defaults come from config options but can be overriden by module
78 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
81 * We've only got one major, so number of mmcblk devices is
82 * limited to (1 << 20) / number of minors per device. It is also
83 * limited by the MAX_DEVICES below.
85 static int max_devices;
87 #define MAX_DEVICES 256
89 static DEFINE_IDA(mmc_blk_ida);
90 static DEFINE_SPINLOCK(mmc_blk_lock);
93 * There is one mmc_blk_data per slot.
97 struct device *parent;
99 struct mmc_queue queue;
100 struct list_head part;
103 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
104 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
105 #define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
108 unsigned int read_only;
109 unsigned int part_type;
110 unsigned int reset_done;
111 #define MMC_BLK_READ BIT(0)
112 #define MMC_BLK_WRITE BIT(1)
113 #define MMC_BLK_DISCARD BIT(2)
114 #define MMC_BLK_SECDISCARD BIT(3)
117 * Only set in main mmc_blk_data associated
118 * with mmc_card with dev_set_drvdata, and keeps
119 * track of the current selected device partition.
121 unsigned int part_curr;
122 struct device_attribute force_ro;
123 struct device_attribute power_ro_lock;
127 static DEFINE_MUTEX(open_lock);
130 MMC_PACKED_NR_IDX = -1,
132 MMC_PACKED_NR_SINGLE,
135 module_param(perdev_minors, int, 0444);
136 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
138 static inline int mmc_blk_part_switch(struct mmc_card *card,
139 struct mmc_blk_data *md);
140 static int get_card_status(struct mmc_card *card, u32 *status, int retries);
142 static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
144 struct mmc_packed *packed = mqrq->packed;
146 mqrq->cmd_type = MMC_PACKED_NONE;
147 packed->nr_entries = MMC_PACKED_NR_ZERO;
148 packed->idx_failure = MMC_PACKED_NR_IDX;
153 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
155 struct mmc_blk_data *md;
157 mutex_lock(&open_lock);
158 md = disk->private_data;
159 if (md && md->usage == 0)
163 mutex_unlock(&open_lock);
168 static inline int mmc_get_devidx(struct gendisk *disk)
170 int devidx = disk->first_minor / perdev_minors;
174 static void mmc_blk_put(struct mmc_blk_data *md)
176 mutex_lock(&open_lock);
178 if (md->usage == 0) {
179 int devidx = mmc_get_devidx(md->disk);
180 blk_cleanup_queue(md->queue.queue);
182 spin_lock(&mmc_blk_lock);
183 ida_remove(&mmc_blk_ida, devidx);
184 spin_unlock(&mmc_blk_lock);
189 mutex_unlock(&open_lock);
192 static ssize_t power_ro_lock_show(struct device *dev,
193 struct device_attribute *attr, char *buf)
196 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
197 struct mmc_card *card = md->queue.card;
200 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
202 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
205 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
212 static ssize_t power_ro_lock_store(struct device *dev,
213 struct device_attribute *attr, const char *buf, size_t count)
216 struct mmc_blk_data *md, *part_md;
217 struct mmc_card *card;
220 if (kstrtoul(buf, 0, &set))
226 md = mmc_blk_get(dev_to_disk(dev));
227 card = md->queue.card;
231 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
232 card->ext_csd.boot_ro_lock |
233 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
234 card->ext_csd.part_time);
236 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
238 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
243 pr_info("%s: Locking boot partition ro until next power on\n",
244 md->disk->disk_name);
245 set_disk_ro(md->disk, 1);
247 list_for_each_entry(part_md, &md->part, part)
248 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
249 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
250 set_disk_ro(part_md->disk, 1);
258 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
262 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
264 ret = snprintf(buf, PAGE_SIZE, "%d\n",
265 get_disk_ro(dev_to_disk(dev)) ^
271 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
272 const char *buf, size_t count)
276 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
277 unsigned long set = simple_strtoul(buf, &end, 0);
283 set_disk_ro(dev_to_disk(dev), set || md->read_only);
290 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
292 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
295 mutex_lock(&block_mutex);
298 check_disk_change(bdev);
301 if ((mode & FMODE_WRITE) && md->read_only) {
306 mutex_unlock(&block_mutex);
311 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
313 struct mmc_blk_data *md = disk->private_data;
315 mutex_lock(&block_mutex);
317 mutex_unlock(&block_mutex);
321 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
323 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
329 struct mmc_blk_ioc_data {
330 struct mmc_ioc_cmd ic;
335 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
336 struct mmc_ioc_cmd __user *user)
338 struct mmc_blk_ioc_data *idata;
341 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
347 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
352 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
353 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
358 if (!idata->buf_bytes) {
363 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
369 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
370 idata->ic.data_ptr, idata->buf_bytes)) {
385 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
386 struct mmc_blk_ioc_data *idata)
388 struct mmc_ioc_cmd *ic = &idata->ic;
390 if (copy_to_user(&(ic_ptr->response), ic->response,
391 sizeof(ic->response)))
394 if (!idata->ic.write_flag) {
395 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
396 idata->buf, idata->buf_bytes))
403 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
409 if (!status || !retries_max)
413 err = get_card_status(card, status, 5);
417 if (!R1_STATUS(*status) &&
418 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
419 break; /* RPMB programming operation complete */
422 * Rechedule to give the MMC device a chance to continue
423 * processing the previous command without being polled too
426 usleep_range(1000, 5000);
427 } while (++retry_count < retries_max);
429 if (retry_count == retries_max)
435 static int ioctl_do_sanitize(struct mmc_card *card)
439 if (!mmc_can_sanitize(card)) {
440 pr_warn("%s: %s - SANITIZE is not supported\n",
441 mmc_hostname(card->host), __func__);
446 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
447 mmc_hostname(card->host), __func__);
449 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
450 EXT_CSD_SANITIZE_START, 1,
451 MMC_SANITIZE_REQ_TIMEOUT);
454 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
455 mmc_hostname(card->host), __func__, err);
457 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
463 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
464 struct mmc_blk_ioc_data *idata)
466 struct mmc_command cmd = {0};
467 struct mmc_data data = {0};
468 struct mmc_request mrq = {NULL};
469 struct scatterlist sg;
474 if (!card || !md || !idata)
477 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
480 cmd.opcode = idata->ic.opcode;
481 cmd.arg = idata->ic.arg;
482 cmd.flags = idata->ic.flags;
484 if (idata->buf_bytes) {
487 data.blksz = idata->ic.blksz;
488 data.blocks = idata->ic.blocks;
490 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
492 if (idata->ic.write_flag)
493 data.flags = MMC_DATA_WRITE;
495 data.flags = MMC_DATA_READ;
497 /* data.flags must already be set before doing this. */
498 mmc_set_data_timeout(&data, card);
500 /* Allow overriding the timeout_ns for empirical tuning. */
501 if (idata->ic.data_timeout_ns)
502 data.timeout_ns = idata->ic.data_timeout_ns;
504 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
506 * Pretend this is a data transfer and rely on the
507 * host driver to compute timeout. When all host
508 * drivers support cmd.cmd_timeout for R1B, this
512 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
514 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
522 err = mmc_blk_part_switch(card, md);
526 if (idata->ic.is_acmd) {
527 err = mmc_app_cmd(card->host, card);
533 err = mmc_set_blockcount(card, data.blocks,
534 idata->ic.write_flag & (1 << 31));
539 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
540 (cmd.opcode == MMC_SWITCH)) {
541 err = ioctl_do_sanitize(card);
544 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
550 mmc_wait_for_req(card->host, &mrq);
553 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
554 __func__, cmd.error);
558 dev_err(mmc_dev(card->host), "%s: data error %d\n",
559 __func__, data.error);
564 * According to the SD specs, some commands require a delay after
565 * issuing the command.
567 if (idata->ic.postsleep_min_us)
568 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
570 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
574 * Ensure RPMB command has completed by polling CMD13
577 err = ioctl_rpmb_card_status_poll(card, &status, 5);
579 dev_err(mmc_dev(card->host),
580 "%s: Card Status=0x%08X, error %d\n",
581 __func__, status, err);
587 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
588 struct mmc_ioc_cmd __user *ic_ptr)
590 struct mmc_blk_ioc_data *idata;
591 struct mmc_blk_data *md;
592 struct mmc_card *card;
593 int err = 0, ioc_err = 0;
596 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
597 * whole block device, not on a partition. This prevents overspray
598 * between sibling partitions.
600 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
603 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
605 return PTR_ERR(idata);
607 md = mmc_blk_get(bdev->bd_disk);
613 card = md->queue.card;
621 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
623 /* Always switch back to main area after RPMB access */
624 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
625 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
629 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
636 return ioc_err ? ioc_err : err;
639 static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
640 struct mmc_ioc_multi_cmd __user *user)
642 struct mmc_blk_ioc_data **idata = NULL;
643 struct mmc_ioc_cmd __user *cmds = user->cmds;
644 struct mmc_card *card;
645 struct mmc_blk_data *md;
646 int i, err = 0, ioc_err = 0;
650 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
651 * whole block device, not on a partition. This prevents overspray
652 * between sibling partitions.
654 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
657 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
658 sizeof(num_of_cmds)))
661 if (num_of_cmds > MMC_IOC_MAX_CMDS)
664 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
668 for (i = 0; i < num_of_cmds; i++) {
669 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
670 if (IS_ERR(idata[i])) {
671 err = PTR_ERR(idata[i]);
677 md = mmc_blk_get(bdev->bd_disk);
683 card = md->queue.card;
691 for (i = 0; i < num_of_cmds && !ioc_err; i++)
692 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
694 /* Always switch back to main area after RPMB access */
695 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
696 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
700 /* copy to user if data and response */
701 for (i = 0; i < num_of_cmds && !err; i++)
702 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
707 for (i = 0; i < num_of_cmds; i++) {
708 kfree(idata[i]->buf);
712 return ioc_err ? ioc_err : err;
715 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
716 unsigned int cmd, unsigned long arg)
720 return mmc_blk_ioctl_cmd(bdev,
721 (struct mmc_ioc_cmd __user *)arg);
722 case MMC_IOC_MULTI_CMD:
723 return mmc_blk_ioctl_multi_cmd(bdev,
724 (struct mmc_ioc_multi_cmd __user *)arg);
731 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
732 unsigned int cmd, unsigned long arg)
734 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
738 static const struct block_device_operations mmc_bdops = {
739 .open = mmc_blk_open,
740 .release = mmc_blk_release,
741 .getgeo = mmc_blk_getgeo,
742 .owner = THIS_MODULE,
743 .ioctl = mmc_blk_ioctl,
745 .compat_ioctl = mmc_blk_compat_ioctl,
749 static inline int mmc_blk_part_switch(struct mmc_card *card,
750 struct mmc_blk_data *md)
753 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
755 if (main_md->part_curr == md->part_type)
758 if (mmc_card_mmc(card)) {
759 u8 part_config = card->ext_csd.part_config;
761 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
762 mmc_retune_pause(card->host);
764 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
765 part_config |= md->part_type;
767 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
768 EXT_CSD_PART_CONFIG, part_config,
769 card->ext_csd.part_time);
771 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
772 mmc_retune_unpause(card->host);
776 card->ext_csd.part_config = part_config;
778 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
779 mmc_retune_unpause(card->host);
782 main_md->part_curr = md->part_type;
786 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
792 struct mmc_request mrq = {NULL};
793 struct mmc_command cmd = {0};
794 struct mmc_data data = {0};
796 struct scatterlist sg;
798 cmd.opcode = MMC_APP_CMD;
799 cmd.arg = card->rca << 16;
800 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
802 err = mmc_wait_for_cmd(card->host, &cmd, 0);
805 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
808 memset(&cmd, 0, sizeof(struct mmc_command));
810 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
812 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
816 data.flags = MMC_DATA_READ;
819 mmc_set_data_timeout(&data, card);
824 blocks = kmalloc(4, GFP_KERNEL);
828 sg_init_one(&sg, blocks, 4);
830 mmc_wait_for_req(card->host, &mrq);
832 result = ntohl(*blocks);
835 if (cmd.error || data.error)
841 static int get_card_status(struct mmc_card *card, u32 *status, int retries)
843 struct mmc_command cmd = {0};
846 cmd.opcode = MMC_SEND_STATUS;
847 if (!mmc_host_is_spi(card->host))
848 cmd.arg = card->rca << 16;
849 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
850 err = mmc_wait_for_cmd(card->host, &cmd, retries);
852 *status = cmd.resp[0];
856 static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
857 bool hw_busy_detect, struct request *req, bool *gen_err)
859 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
864 err = get_card_status(card, &status, 5);
866 pr_err("%s: error %d requesting status\n",
867 req->rq_disk->disk_name, err);
871 if (status & R1_ERROR) {
872 pr_err("%s: %s: error sending status cmd, status %#x\n",
873 req->rq_disk->disk_name, __func__, status);
877 /* We may rely on the host hw to handle busy detection.*/
878 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
883 * Timeout if the device never becomes ready for data and never
884 * leaves the program state.
886 if (time_after(jiffies, timeout)) {
887 pr_err("%s: Card stuck in programming state! %s %s\n",
888 mmc_hostname(card->host),
889 req->rq_disk->disk_name, __func__);
894 * Some cards mishandle the status bits,
895 * so make sure to check both the busy
896 * indication and the card state.
898 } while (!(status & R1_READY_FOR_DATA) ||
899 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
904 static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
905 struct request *req, bool *gen_err, u32 *stop_status)
907 struct mmc_host *host = card->host;
908 struct mmc_command cmd = {0};
910 bool use_r1b_resp = rq_data_dir(req) == WRITE;
913 * Normally we use R1B responses for WRITE, but in cases where the host
914 * has specified a max_busy_timeout we need to validate it. A failure
915 * means we need to prevent the host from doing hw busy detection, which
916 * is done by converting to a R1 response instead.
918 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
919 use_r1b_resp = false;
921 cmd.opcode = MMC_STOP_TRANSMISSION;
923 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
924 cmd.busy_timeout = timeout_ms;
926 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
929 err = mmc_wait_for_cmd(host, &cmd, 5);
933 *stop_status = cmd.resp[0];
935 /* No need to check card status in case of READ. */
936 if (rq_data_dir(req) == READ)
939 if (!mmc_host_is_spi(host) &&
940 (*stop_status & R1_ERROR)) {
941 pr_err("%s: %s: general error sending stop command, resp %#x\n",
942 req->rq_disk->disk_name, __func__, *stop_status);
946 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
949 #define ERR_NOMEDIUM 3
952 #define ERR_CONTINUE 0
954 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
955 bool status_valid, u32 status)
959 /* response crc error, retry the r/w cmd */
960 pr_err("%s: %s sending %s command, card status %#x\n",
961 req->rq_disk->disk_name, "response CRC error",
966 pr_err("%s: %s sending %s command, card status %#x\n",
967 req->rq_disk->disk_name, "timed out", name, status);
969 /* If the status cmd initially failed, retry the r/w cmd */
971 pr_err("%s: status not valid, retrying timeout\n",
972 req->rq_disk->disk_name);
977 * If it was a r/w cmd crc error, or illegal command
978 * (eg, issued in wrong state) then retry - we should
979 * have corrected the state problem above.
981 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
982 pr_err("%s: command error, retrying timeout\n",
983 req->rq_disk->disk_name);
987 /* Otherwise abort the command */
991 /* We don't understand the error code the driver gave us */
992 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
993 req->rq_disk->disk_name, error, status);
999 * Initial r/w and stop cmd error recovery.
1000 * We don't know whether the card received the r/w cmd or not, so try to
1001 * restore things back to a sane state. Essentially, we do this as follows:
1002 * - Obtain card status. If the first attempt to obtain card status fails,
1003 * the status word will reflect the failed status cmd, not the failed
1004 * r/w cmd. If we fail to obtain card status, it suggests we can no
1005 * longer communicate with the card.
1006 * - Check the card state. If the card received the cmd but there was a
1007 * transient problem with the response, it might still be in a data transfer
1008 * mode. Try to send it a stop command. If this fails, we can't recover.
1009 * - If the r/w cmd failed due to a response CRC error, it was probably
1010 * transient, so retry the cmd.
1011 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1012 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1013 * illegal cmd, retry.
1014 * Otherwise we don't understand what happened, so abort.
1016 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
1017 struct mmc_blk_request *brq, int *ecc_err, bool *gen_err)
1019 bool prev_cmd_status_valid = true;
1020 u32 status, stop_status = 0;
1023 if (mmc_card_removed(card))
1024 return ERR_NOMEDIUM;
1027 * Try to get card status which indicates both the card state
1028 * and why there was no response. If the first attempt fails,
1029 * we can't be sure the returned status is for the r/w command.
1031 for (retry = 2; retry >= 0; retry--) {
1032 err = get_card_status(card, &status, 0);
1036 /* Re-tune if needed */
1037 mmc_retune_recheck(card->host);
1039 prev_cmd_status_valid = false;
1040 pr_err("%s: error %d sending status command, %sing\n",
1041 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1044 /* We couldn't get a response from the card. Give up. */
1046 /* Check if the card is removed */
1047 if (mmc_detect_card_removed(card->host))
1048 return ERR_NOMEDIUM;
1052 /* Flag ECC errors */
1053 if ((status & R1_CARD_ECC_FAILED) ||
1054 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1055 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1058 /* Flag General errors */
1059 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1060 if ((status & R1_ERROR) ||
1061 (brq->stop.resp[0] & R1_ERROR)) {
1062 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1063 req->rq_disk->disk_name, __func__,
1064 brq->stop.resp[0], status);
1069 * Check the current card state. If it is in some data transfer
1070 * mode, tell it to stop (and hopefully transition back to TRAN.)
1072 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1073 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
1074 err = send_stop(card,
1075 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1076 req, gen_err, &stop_status);
1078 pr_err("%s: error %d sending stop command\n",
1079 req->rq_disk->disk_name, err);
1081 * If the stop cmd also timed out, the card is probably
1082 * not present, so abort. Other errors are bad news too.
1087 if (stop_status & R1_CARD_ECC_FAILED)
1091 /* Check for set block count errors */
1093 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1094 prev_cmd_status_valid, status);
1096 /* Check for r/w command errors */
1098 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1099 prev_cmd_status_valid, status);
1102 if (!brq->stop.error)
1103 return ERR_CONTINUE;
1105 /* Now for stop errors. These aren't fatal to the transfer. */
1106 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
1107 req->rq_disk->disk_name, brq->stop.error,
1108 brq->cmd.resp[0], status);
1111 * Subsitute in our own stop status as this will give the error
1112 * state which happened during the execution of the r/w command.
1115 brq->stop.resp[0] = stop_status;
1116 brq->stop.error = 0;
1118 return ERR_CONTINUE;
1121 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1126 if (md->reset_done & type)
1129 md->reset_done |= type;
1130 err = mmc_hw_reset(host);
1131 /* Ensure we switch back to the correct partition */
1132 if (err != -EOPNOTSUPP) {
1133 struct mmc_blk_data *main_md =
1134 dev_get_drvdata(&host->card->dev);
1137 main_md->part_curr = main_md->part_type;
1138 part_err = mmc_blk_part_switch(host->card, md);
1141 * We have failed to get back into the correct
1142 * partition, so we need to abort the whole request.
1150 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1152 md->reset_done &= ~type;
1155 int mmc_access_rpmb(struct mmc_queue *mq)
1157 struct mmc_blk_data *md = mq->data;
1159 * If this is a RPMB partition access, return ture
1161 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1167 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1169 struct mmc_blk_data *md = mq->data;
1170 struct mmc_card *card = md->queue.card;
1171 unsigned int from, nr, arg;
1172 int err = 0, type = MMC_BLK_DISCARD;
1174 if (!mmc_can_erase(card)) {
1179 from = blk_rq_pos(req);
1180 nr = blk_rq_sectors(req);
1182 if (mmc_can_discard(card))
1183 arg = MMC_DISCARD_ARG;
1184 else if (mmc_can_trim(card))
1187 arg = MMC_ERASE_ARG;
1189 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1190 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1191 INAND_CMD38_ARG_EXT_CSD,
1192 arg == MMC_TRIM_ARG ?
1193 INAND_CMD38_ARG_TRIM :
1194 INAND_CMD38_ARG_ERASE,
1199 err = mmc_erase(card, from, nr, arg);
1201 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1204 mmc_blk_reset_success(md, type);
1205 blk_end_request(req, err, blk_rq_bytes(req));
1210 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1211 struct request *req)
1213 struct mmc_blk_data *md = mq->data;
1214 struct mmc_card *card = md->queue.card;
1215 unsigned int from, nr, arg;
1216 int err = 0, type = MMC_BLK_SECDISCARD;
1218 if (!(mmc_can_secure_erase_trim(card))) {
1223 from = blk_rq_pos(req);
1224 nr = blk_rq_sectors(req);
1226 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1227 arg = MMC_SECURE_TRIM1_ARG;
1229 arg = MMC_SECURE_ERASE_ARG;
1232 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1233 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1234 INAND_CMD38_ARG_EXT_CSD,
1235 arg == MMC_SECURE_TRIM1_ARG ?
1236 INAND_CMD38_ARG_SECTRIM1 :
1237 INAND_CMD38_ARG_SECERASE,
1243 err = mmc_erase(card, from, nr, arg);
1249 if (arg == MMC_SECURE_TRIM1_ARG) {
1250 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1251 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1252 INAND_CMD38_ARG_EXT_CSD,
1253 INAND_CMD38_ARG_SECTRIM2,
1259 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1267 if (err && !mmc_blk_reset(md, card->host, type))
1270 mmc_blk_reset_success(md, type);
1272 blk_end_request(req, err, blk_rq_bytes(req));
1277 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1279 struct mmc_blk_data *md = mq->data;
1280 struct mmc_card *card = md->queue.card;
1283 ret = mmc_flush_cache(card);
1287 blk_end_request_all(req, ret);
1293 * Reformat current write as a reliable write, supporting
1294 * both legacy and the enhanced reliable write MMC cards.
1295 * In each transfer we'll handle only as much as a single
1296 * reliable write can handle, thus finish the request in
1297 * partial completions.
1299 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1300 struct mmc_card *card,
1301 struct request *req)
1303 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1304 /* Legacy mode imposes restrictions on transfers. */
1305 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1306 brq->data.blocks = 1;
1308 if (brq->data.blocks > card->ext_csd.rel_sectors)
1309 brq->data.blocks = card->ext_csd.rel_sectors;
1310 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1311 brq->data.blocks = 1;
1315 #define CMD_ERRORS \
1316 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1317 R1_ADDRESS_ERROR | /* Misaligned address */ \
1318 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1319 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1320 R1_CC_ERROR | /* Card controller error */ \
1321 R1_ERROR) /* General/unknown error */
1323 static int mmc_blk_err_check(struct mmc_card *card,
1324 struct mmc_async_req *areq)
1326 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1328 struct mmc_blk_request *brq = &mq_mrq->brq;
1329 struct request *req = mq_mrq->req;
1330 int need_retune = card->host->need_retune;
1332 bool gen_err = false;
1335 * sbc.error indicates a problem with the set block count
1336 * command. No data will have been transferred.
1338 * cmd.error indicates a problem with the r/w command. No
1339 * data will have been transferred.
1341 * stop.error indicates a problem with the stop command. Data
1342 * may have been transferred, or may still be transferring.
1344 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1346 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
1348 return MMC_BLK_RETRY;
1350 return MMC_BLK_ABORT;
1352 return MMC_BLK_NOMEDIUM;
1359 * Check for errors relating to the execution of the
1360 * initial command - such as address errors. No data
1361 * has been transferred.
1363 if (brq->cmd.resp[0] & CMD_ERRORS) {
1364 pr_err("%s: r/w command failed, status = %#x\n",
1365 req->rq_disk->disk_name, brq->cmd.resp[0]);
1366 return MMC_BLK_ABORT;
1370 * Everything else is either success, or a data error of some
1371 * kind. If it was a write, we may have transitioned to
1372 * program mode, which we have to wait for it to complete.
1374 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1377 /* Check stop command response */
1378 if (brq->stop.resp[0] & R1_ERROR) {
1379 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1380 req->rq_disk->disk_name, __func__,
1385 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1388 return MMC_BLK_CMD_ERR;
1391 /* if general error occurs, retry the write operation. */
1393 pr_warn("%s: retrying write for general error\n",
1394 req->rq_disk->disk_name);
1395 return MMC_BLK_RETRY;
1398 if (brq->data.error) {
1399 if (need_retune && !brq->retune_retry_done) {
1400 pr_debug("%s: retrying because a re-tune was needed\n",
1401 req->rq_disk->disk_name);
1402 brq->retune_retry_done = 1;
1403 return MMC_BLK_RETRY;
1405 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1406 req->rq_disk->disk_name, brq->data.error,
1407 (unsigned)blk_rq_pos(req),
1408 (unsigned)blk_rq_sectors(req),
1409 brq->cmd.resp[0], brq->stop.resp[0]);
1411 if (rq_data_dir(req) == READ) {
1413 return MMC_BLK_ECC_ERR;
1414 return MMC_BLK_DATA_ERR;
1416 return MMC_BLK_CMD_ERR;
1420 if (!brq->data.bytes_xfered)
1421 return MMC_BLK_RETRY;
1423 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1424 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1425 return MMC_BLK_PARTIAL;
1427 return MMC_BLK_SUCCESS;
1430 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1431 return MMC_BLK_PARTIAL;
1433 return MMC_BLK_SUCCESS;
1436 static int mmc_blk_packed_err_check(struct mmc_card *card,
1437 struct mmc_async_req *areq)
1439 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1441 struct request *req = mq_rq->req;
1442 struct mmc_packed *packed = mq_rq->packed;
1443 int err, check, status;
1447 check = mmc_blk_err_check(card, areq);
1448 err = get_card_status(card, &status, 0);
1450 pr_err("%s: error %d sending status command\n",
1451 req->rq_disk->disk_name, err);
1452 return MMC_BLK_ABORT;
1455 if (status & R1_EXCEPTION_EVENT) {
1456 err = mmc_get_ext_csd(card, &ext_csd);
1458 pr_err("%s: error %d sending ext_csd\n",
1459 req->rq_disk->disk_name, err);
1460 return MMC_BLK_ABORT;
1463 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1464 EXT_CSD_PACKED_FAILURE) &&
1465 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1466 EXT_CSD_PACKED_GENERIC_ERROR)) {
1467 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1468 EXT_CSD_PACKED_INDEXED_ERROR) {
1469 packed->idx_failure =
1470 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1471 check = MMC_BLK_PARTIAL;
1473 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1474 "failure index: %d\n",
1475 req->rq_disk->disk_name, packed->nr_entries,
1476 packed->blocks, packed->idx_failure);
1484 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1485 struct mmc_card *card,
1487 struct mmc_queue *mq)
1489 u32 readcmd, writecmd;
1490 struct mmc_blk_request *brq = &mqrq->brq;
1491 struct request *req = mqrq->req;
1492 struct mmc_blk_data *md = mq->data;
1496 * Reliable writes are used to implement Forced Unit Access and
1497 * are supported only on MMCs.
1499 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1500 (rq_data_dir(req) == WRITE) &&
1501 (md->flags & MMC_BLK_REL_WR);
1503 memset(brq, 0, sizeof(struct mmc_blk_request));
1504 brq->mrq.cmd = &brq->cmd;
1505 brq->mrq.data = &brq->data;
1507 brq->cmd.arg = blk_rq_pos(req);
1508 if (!mmc_card_blockaddr(card))
1510 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1511 brq->data.blksz = 512;
1512 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1514 brq->data.blocks = blk_rq_sectors(req);
1517 * The block layer doesn't support all sector count
1518 * restrictions, so we need to be prepared for too big
1521 if (brq->data.blocks > card->host->max_blk_count)
1522 brq->data.blocks = card->host->max_blk_count;
1524 if (brq->data.blocks > 1) {
1526 * After a read error, we redo the request one sector
1527 * at a time in order to accurately determine which
1528 * sectors can be read successfully.
1531 brq->data.blocks = 1;
1534 * Some controllers have HW issues while operating
1535 * in multiple I/O mode
1537 if (card->host->ops->multi_io_quirk)
1538 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1539 (rq_data_dir(req) == READ) ?
1540 MMC_DATA_READ : MMC_DATA_WRITE,
1544 if (brq->data.blocks > 1 || do_rel_wr) {
1545 /* SPI multiblock writes terminate using a special
1546 * token, not a STOP_TRANSMISSION request.
1548 if (!mmc_host_is_spi(card->host) ||
1549 rq_data_dir(req) == READ)
1550 brq->mrq.stop = &brq->stop;
1551 readcmd = MMC_READ_MULTIPLE_BLOCK;
1552 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1554 brq->mrq.stop = NULL;
1555 readcmd = MMC_READ_SINGLE_BLOCK;
1556 writecmd = MMC_WRITE_BLOCK;
1558 if (rq_data_dir(req) == READ) {
1559 brq->cmd.opcode = readcmd;
1560 brq->data.flags = MMC_DATA_READ;
1562 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1565 brq->cmd.opcode = writecmd;
1566 brq->data.flags = MMC_DATA_WRITE;
1568 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1573 mmc_apply_rel_rw(brq, card, req);
1576 * Data tag is used only during writing meta data to speed
1577 * up write and any subsequent read of this meta data
1579 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1580 (req->cmd_flags & REQ_META) &&
1581 (rq_data_dir(req) == WRITE) &&
1582 ((brq->data.blocks * brq->data.blksz) >=
1583 card->ext_csd.data_tag_unit_size);
1586 * Pre-defined multi-block transfers are preferable to
1587 * open ended-ones (and necessary for reliable writes).
1588 * However, it is not sufficient to just send CMD23,
1589 * and avoid the final CMD12, as on an error condition
1590 * CMD12 (stop) needs to be sent anyway. This, coupled
1591 * with Auto-CMD23 enhancements provided by some
1592 * hosts, means that the complexity of dealing
1593 * with this is best left to the host. If CMD23 is
1594 * supported by card and host, we'll fill sbc in and let
1595 * the host deal with handling it correctly. This means
1596 * that for hosts that don't expose MMC_CAP_CMD23, no
1597 * change of behavior will be observed.
1599 * N.B: Some MMC cards experience perf degradation.
1600 * We'll avoid using CMD23-bounded multiblock writes for
1601 * these, while retaining features like reliable writes.
1603 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1604 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1606 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1607 brq->sbc.arg = brq->data.blocks |
1608 (do_rel_wr ? (1 << 31) : 0) |
1609 (do_data_tag ? (1 << 29) : 0);
1610 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1611 brq->mrq.sbc = &brq->sbc;
1614 mmc_set_data_timeout(&brq->data, card);
1616 brq->data.sg = mqrq->sg;
1617 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1620 * Adjust the sg list so it is the same size as the
1623 if (brq->data.blocks != blk_rq_sectors(req)) {
1624 int i, data_size = brq->data.blocks << 9;
1625 struct scatterlist *sg;
1627 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1628 data_size -= sg->length;
1629 if (data_size <= 0) {
1630 sg->length += data_size;
1635 brq->data.sg_len = i;
1638 mqrq->mmc_active.mrq = &brq->mrq;
1639 mqrq->mmc_active.err_check = mmc_blk_err_check;
1641 mmc_queue_bounce_pre(mqrq);
1644 static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1645 struct mmc_card *card)
1647 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1648 unsigned int max_seg_sz = queue_max_segment_size(q);
1649 unsigned int len, nr_segs = 0;
1652 len = min(hdr_sz, max_seg_sz);
1660 static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1662 struct request_queue *q = mq->queue;
1663 struct mmc_card *card = mq->card;
1664 struct request *cur = req, *next = NULL;
1665 struct mmc_blk_data *md = mq->data;
1666 struct mmc_queue_req *mqrq = mq->mqrq_cur;
1667 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1668 unsigned int req_sectors = 0, phys_segments = 0;
1669 unsigned int max_blk_count, max_phys_segs;
1670 bool put_back = true;
1671 u8 max_packed_rw = 0;
1675 * We don't need to check packed for any further
1676 * operation of packed stuff as we set MMC_PACKED_NONE
1677 * and return zero for reqs if geting null packed. Also
1678 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
1679 * it again when removing blk req.
1681 if (!mqrq->packed) {
1682 md->flags &= (~MMC_BLK_PACKED_CMD);
1686 if (!(md->flags & MMC_BLK_PACKED_CMD))
1689 if ((rq_data_dir(cur) == WRITE) &&
1690 mmc_host_packed_wr(card->host))
1691 max_packed_rw = card->ext_csd.max_packed_writes;
1693 if (max_packed_rw == 0)
1696 if (mmc_req_rel_wr(cur) &&
1697 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1700 if (mmc_large_sector(card) &&
1701 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1704 mmc_blk_clear_packed(mqrq);
1706 max_blk_count = min(card->host->max_blk_count,
1707 card->host->max_req_size >> 9);
1708 if (unlikely(max_blk_count > 0xffff))
1709 max_blk_count = 0xffff;
1711 max_phys_segs = queue_max_segments(q);
1712 req_sectors += blk_rq_sectors(cur);
1713 phys_segments += cur->nr_phys_segments;
1715 if (rq_data_dir(cur) == WRITE) {
1716 req_sectors += mmc_large_sector(card) ? 8 : 1;
1717 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1721 if (reqs >= max_packed_rw - 1) {
1726 spin_lock_irq(q->queue_lock);
1727 next = blk_fetch_request(q);
1728 spin_unlock_irq(q->queue_lock);
1734 if (mmc_large_sector(card) &&
1735 !IS_ALIGNED(blk_rq_sectors(next), 8))
1738 if (req_op(next) == REQ_OP_DISCARD ||
1739 req_op(next) == REQ_OP_SECURE_ERASE ||
1740 req_op(next) == REQ_OP_FLUSH)
1743 if (rq_data_dir(cur) != rq_data_dir(next))
1746 if (mmc_req_rel_wr(next) &&
1747 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1750 req_sectors += blk_rq_sectors(next);
1751 if (req_sectors > max_blk_count)
1754 phys_segments += next->nr_phys_segments;
1755 if (phys_segments > max_phys_segs)
1758 list_add_tail(&next->queuelist, &mqrq->packed->list);
1764 spin_lock_irq(q->queue_lock);
1765 blk_requeue_request(q, next);
1766 spin_unlock_irq(q->queue_lock);
1770 list_add(&req->queuelist, &mqrq->packed->list);
1771 mqrq->packed->nr_entries = ++reqs;
1772 mqrq->packed->retries = reqs;
1777 mqrq->cmd_type = MMC_PACKED_NONE;
1781 static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1782 struct mmc_card *card,
1783 struct mmc_queue *mq)
1785 struct mmc_blk_request *brq = &mqrq->brq;
1786 struct request *req = mqrq->req;
1787 struct request *prq;
1788 struct mmc_blk_data *md = mq->data;
1789 struct mmc_packed *packed = mqrq->packed;
1790 bool do_rel_wr, do_data_tag;
1791 __le32 *packed_cmd_hdr;
1795 mqrq->cmd_type = MMC_PACKED_WRITE;
1797 packed->idx_failure = MMC_PACKED_NR_IDX;
1799 packed_cmd_hdr = packed->cmd_hdr;
1800 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
1801 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
1802 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
1803 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1806 * Argument for each entry of packed group
1808 list_for_each_entry(prq, &packed->list, queuelist) {
1809 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1810 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1811 (prq->cmd_flags & REQ_META) &&
1812 (rq_data_dir(prq) == WRITE) &&
1813 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
1814 /* Argument of CMD23 */
1815 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
1816 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1817 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1818 blk_rq_sectors(prq));
1819 /* Argument of CMD18 or CMD25 */
1820 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
1821 mmc_card_blockaddr(card) ?
1822 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
1823 packed->blocks += blk_rq_sectors(prq);
1827 memset(brq, 0, sizeof(struct mmc_blk_request));
1828 brq->mrq.cmd = &brq->cmd;
1829 brq->mrq.data = &brq->data;
1830 brq->mrq.sbc = &brq->sbc;
1831 brq->mrq.stop = &brq->stop;
1833 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1834 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1835 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1837 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1838 brq->cmd.arg = blk_rq_pos(req);
1839 if (!mmc_card_blockaddr(card))
1841 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1843 brq->data.blksz = 512;
1844 brq->data.blocks = packed->blocks + hdr_blocks;
1845 brq->data.flags = MMC_DATA_WRITE;
1847 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1849 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1851 mmc_set_data_timeout(&brq->data, card);
1853 brq->data.sg = mqrq->sg;
1854 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1856 mqrq->mmc_active.mrq = &brq->mrq;
1857 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1859 mmc_queue_bounce_pre(mqrq);
1862 static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1863 struct mmc_blk_request *brq, struct request *req,
1866 struct mmc_queue_req *mq_rq;
1867 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1870 * If this is an SD card and we're writing, we can first
1871 * mark the known good sectors as ok.
1873 * If the card is not SD, we can still ok written sectors
1874 * as reported by the controller (which might be less than
1875 * the real number of written sectors, but never more).
1877 if (mmc_card_sd(card)) {
1880 blocks = mmc_sd_num_wr_blocks(card);
1881 if (blocks != (u32)-1) {
1882 ret = blk_end_request(req, 0, blocks << 9);
1885 if (!mmc_packed_cmd(mq_rq->cmd_type))
1886 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1891 static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1893 struct request *prq;
1894 struct mmc_packed *packed = mq_rq->packed;
1895 int idx = packed->idx_failure, i = 0;
1898 while (!list_empty(&packed->list)) {
1899 prq = list_entry_rq(packed->list.next);
1901 /* retry from error index */
1902 packed->nr_entries -= idx;
1906 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1907 list_del_init(&prq->queuelist);
1908 mmc_blk_clear_packed(mq_rq);
1912 list_del_init(&prq->queuelist);
1913 blk_end_request(prq, 0, blk_rq_bytes(prq));
1917 mmc_blk_clear_packed(mq_rq);
1921 static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1923 struct request *prq;
1924 struct mmc_packed *packed = mq_rq->packed;
1926 while (!list_empty(&packed->list)) {
1927 prq = list_entry_rq(packed->list.next);
1928 list_del_init(&prq->queuelist);
1929 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1932 mmc_blk_clear_packed(mq_rq);
1935 static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1936 struct mmc_queue_req *mq_rq)
1938 struct request *prq;
1939 struct request_queue *q = mq->queue;
1940 struct mmc_packed *packed = mq_rq->packed;
1942 while (!list_empty(&packed->list)) {
1943 prq = list_entry_rq(packed->list.prev);
1944 if (prq->queuelist.prev != &packed->list) {
1945 list_del_init(&prq->queuelist);
1946 spin_lock_irq(q->queue_lock);
1947 blk_requeue_request(mq->queue, prq);
1948 spin_unlock_irq(q->queue_lock);
1950 list_del_init(&prq->queuelist);
1954 mmc_blk_clear_packed(mq_rq);
1957 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
1959 struct mmc_blk_data *md = mq->data;
1960 struct mmc_card *card = md->queue.card;
1961 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
1962 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
1963 enum mmc_blk_status status;
1964 struct mmc_queue_req *mq_rq;
1965 struct request *req = rqc;
1966 struct mmc_async_req *areq;
1967 const u8 packed_nr = 2;
1970 if (!rqc && !mq->mqrq_prev->req)
1974 reqs = mmc_blk_prep_packed_list(mq, rqc);
1979 * When 4KB native sector is enabled, only 8 blocks
1980 * multiple read or write is allowed
1982 if (mmc_large_sector(card) &&
1983 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
1984 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1985 req->rq_disk->disk_name);
1986 mq_rq = mq->mqrq_cur;
1990 if (reqs >= packed_nr)
1991 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1994 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1995 areq = &mq->mqrq_cur->mmc_active;
1998 areq = mmc_start_req(card->host, areq, (int *) &status);
2000 if (status == MMC_BLK_NEW_REQUEST)
2001 mq->flags |= MMC_QUEUE_NEW_REQUEST;
2005 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2008 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
2009 mmc_queue_bounce_post(mq_rq);
2012 case MMC_BLK_SUCCESS:
2013 case MMC_BLK_PARTIAL:
2015 * A block was successfully transferred.
2017 mmc_blk_reset_success(md, type);
2019 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2020 ret = mmc_blk_end_packed_req(mq_rq);
2023 ret = blk_end_request(req, 0,
2024 brq->data.bytes_xfered);
2028 * If the blk_end_request function returns non-zero even
2029 * though all data has been transferred and no errors
2030 * were returned by the host controller, it's a bug.
2032 if (status == MMC_BLK_SUCCESS && ret) {
2033 pr_err("%s BUG rq_tot %d d_xfer %d\n",
2034 __func__, blk_rq_bytes(req),
2035 brq->data.bytes_xfered);
2040 case MMC_BLK_CMD_ERR:
2041 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
2042 if (mmc_blk_reset(md, card->host, type))
2048 retune_retry_done = brq->retune_retry_done;
2053 if (!mmc_blk_reset(md, card->host, type))
2056 case MMC_BLK_DATA_ERR: {
2059 err = mmc_blk_reset(md, card->host, type);
2062 if (err == -ENODEV ||
2063 mmc_packed_cmd(mq_rq->cmd_type))
2067 case MMC_BLK_ECC_ERR:
2068 if (brq->data.blocks > 1) {
2069 /* Redo read one sector at a time */
2070 pr_warn("%s: retrying using single block read\n",
2071 req->rq_disk->disk_name);
2076 * After an error, we redo I/O one sector at a
2077 * time, so we only reach here after trying to
2078 * read a single sector.
2080 ret = blk_end_request(req, -EIO,
2085 case MMC_BLK_NOMEDIUM:
2088 pr_err("%s: Unhandled return value (%d)",
2089 req->rq_disk->disk_name, status);
2094 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2095 if (!mq_rq->packed->retries)
2097 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2098 mmc_start_req(card->host,
2099 &mq_rq->mmc_active, NULL);
2103 * In case of a incomplete request
2104 * prepare it again and resend.
2106 mmc_blk_rw_rq_prep(mq_rq, card,
2108 mmc_start_req(card->host,
2109 &mq_rq->mmc_active, NULL);
2111 mq_rq->brq.retune_retry_done = retune_retry_done;
2118 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2119 mmc_blk_abort_packed_req(mq_rq);
2121 if (mmc_card_removed(card))
2122 req->cmd_flags |= REQ_QUIET;
2124 ret = blk_end_request(req, -EIO,
2125 blk_rq_cur_bytes(req));
2130 if (mmc_card_removed(card)) {
2131 rqc->cmd_flags |= REQ_QUIET;
2132 blk_end_request_all(rqc, -EIO);
2135 * If current request is packed, it needs to put back.
2137 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2138 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2140 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2141 mmc_start_req(card->host,
2142 &mq->mqrq_cur->mmc_active, NULL);
2149 int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2152 struct mmc_blk_data *md = mq->data;
2153 struct mmc_card *card = md->queue.card;
2154 struct mmc_host *host = card->host;
2155 unsigned long flags;
2156 bool req_is_special = mmc_req_is_special(req);
2158 if (req && !mq->mqrq_prev->req)
2159 /* claim host only for the first request */
2162 ret = mmc_blk_part_switch(card, md);
2165 blk_end_request_all(req, -EIO);
2171 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
2172 if (req && req_op(req) == REQ_OP_DISCARD) {
2173 /* complete ongoing async transfer before issuing discard */
2174 if (card->host->areq)
2175 mmc_blk_issue_rw_rq(mq, NULL);
2176 ret = mmc_blk_issue_discard_rq(mq, req);
2177 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
2178 /* complete ongoing async transfer before issuing secure erase*/
2179 if (card->host->areq)
2180 mmc_blk_issue_rw_rq(mq, NULL);
2181 ret = mmc_blk_issue_secdiscard_rq(mq, req);
2182 } else if (req && req_op(req) == REQ_OP_FLUSH) {
2183 /* complete ongoing async transfer before issuing flush */
2184 if (card->host->areq)
2185 mmc_blk_issue_rw_rq(mq, NULL);
2186 ret = mmc_blk_issue_flush(mq, req);
2188 if (!req && host->areq) {
2189 spin_lock_irqsave(&host->context_info.lock, flags);
2190 host->context_info.is_waiting_last_req = true;
2191 spin_unlock_irqrestore(&host->context_info.lock, flags);
2193 ret = mmc_blk_issue_rw_rq(mq, req);
2197 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
2199 * Release host when there are no more requests
2200 * and after special request(discard, flush) is done.
2201 * In case sepecial request, there is no reentry to
2202 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2208 static inline int mmc_blk_readonly(struct mmc_card *card)
2210 return mmc_card_readonly(card) ||
2211 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2214 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2215 struct device *parent,
2218 const char *subname,
2221 struct mmc_blk_data *md;
2225 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
2226 return ERR_PTR(-ENOMEM);
2228 spin_lock(&mmc_blk_lock);
2229 ret = ida_get_new(&mmc_blk_ida, &devidx);
2230 spin_unlock(&mmc_blk_lock);
2235 return ERR_PTR(ret);
2237 if (devidx >= max_devices) {
2242 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2248 md->area_type = area_type;
2251 * Set the read-only status based on the supported commands
2252 * and the write protect switch.
2254 md->read_only = mmc_blk_readonly(card);
2256 md->disk = alloc_disk(perdev_minors);
2257 if (md->disk == NULL) {
2262 spin_lock_init(&md->lock);
2263 INIT_LIST_HEAD(&md->part);
2266 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2270 md->queue.data = md;
2272 md->disk->major = MMC_BLOCK_MAJOR;
2273 md->disk->first_minor = devidx * perdev_minors;
2274 md->disk->fops = &mmc_bdops;
2275 md->disk->private_data = md;
2276 md->disk->queue = md->queue.queue;
2277 md->parent = parent;
2278 set_disk_ro(md->disk, md->read_only || default_ro);
2279 md->disk->flags = GENHD_FL_EXT_DEVT;
2280 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2281 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
2284 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2286 * - be set for removable media with permanent block devices
2287 * - be unset for removable block devices with permanent media
2289 * Since MMC block devices clearly fall under the second
2290 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2291 * should use the block device creation/destruction hotplug
2292 * messages to tell when the card is present.
2295 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2296 "mmcblk%u%s", card->host->index, subname ? subname : "");
2298 if (mmc_card_mmc(card))
2299 blk_queue_logical_block_size(md->queue.queue,
2300 card->ext_csd.data_sector_size);
2302 blk_queue_logical_block_size(md->queue.queue, 512);
2304 set_capacity(md->disk, size);
2306 if (mmc_host_cmd23(card->host)) {
2307 if ((mmc_card_mmc(card) &&
2308 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
2309 (mmc_card_sd(card) &&
2310 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2311 md->flags |= MMC_BLK_CMD23;
2314 if (mmc_card_mmc(card) &&
2315 md->flags & MMC_BLK_CMD23 &&
2316 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2317 card->ext_csd.rel_sectors)) {
2318 md->flags |= MMC_BLK_REL_WR;
2319 blk_queue_write_cache(md->queue.queue, true, true);
2322 if (mmc_card_mmc(card) &&
2323 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2324 (md->flags & MMC_BLK_CMD23) &&
2325 card->ext_csd.packed_event_en) {
2326 if (!mmc_packed_init(&md->queue, card))
2327 md->flags |= MMC_BLK_PACKED_CMD;
2337 spin_lock(&mmc_blk_lock);
2338 ida_remove(&mmc_blk_ida, devidx);
2339 spin_unlock(&mmc_blk_lock);
2340 return ERR_PTR(ret);
2343 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2347 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2349 * The EXT_CSD sector count is in number or 512 byte
2352 size = card->ext_csd.sectors;
2355 * The CSD capacity field is in units of read_blkbits.
2356 * set_capacity takes units of 512 bytes.
2358 size = (typeof(sector_t))card->csd.capacity
2359 << (card->csd.read_blkbits - 9);
2362 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2363 MMC_BLK_DATA_AREA_MAIN);
2366 static int mmc_blk_alloc_part(struct mmc_card *card,
2367 struct mmc_blk_data *md,
2368 unsigned int part_type,
2371 const char *subname,
2375 struct mmc_blk_data *part_md;
2377 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2378 subname, area_type);
2379 if (IS_ERR(part_md))
2380 return PTR_ERR(part_md);
2381 part_md->part_type = part_type;
2382 list_add(&part_md->part, &md->part);
2384 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
2385 cap_str, sizeof(cap_str));
2386 pr_info("%s: %s %s partition %u %s\n",
2387 part_md->disk->disk_name, mmc_card_id(card),
2388 mmc_card_name(card), part_md->part_type, cap_str);
2392 /* MMC Physical partitions consist of two boot partitions and
2393 * up to four general purpose partitions.
2394 * For each partition enabled in EXT_CSD a block device will be allocatedi
2395 * to provide access to the partition.
2398 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2402 if (!mmc_card_mmc(card))
2405 for (idx = 0; idx < card->nr_parts; idx++) {
2406 if (card->part[idx].size) {
2407 ret = mmc_blk_alloc_part(card, md,
2408 card->part[idx].part_cfg,
2409 card->part[idx].size >> 9,
2410 card->part[idx].force_ro,
2411 card->part[idx].name,
2412 card->part[idx].area_type);
2421 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2423 struct mmc_card *card;
2427 * Flush remaining requests and free queues. It
2428 * is freeing the queue that stops new requests
2429 * from being accepted.
2431 card = md->queue.card;
2432 mmc_cleanup_queue(&md->queue);
2433 if (md->flags & MMC_BLK_PACKED_CMD)
2434 mmc_packed_clean(&md->queue);
2435 if (md->disk->flags & GENHD_FL_UP) {
2436 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2437 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2438 card->ext_csd.boot_ro_lockable)
2439 device_remove_file(disk_to_dev(md->disk),
2440 &md->power_ro_lock);
2442 del_gendisk(md->disk);
2448 static void mmc_blk_remove_parts(struct mmc_card *card,
2449 struct mmc_blk_data *md)
2451 struct list_head *pos, *q;
2452 struct mmc_blk_data *part_md;
2454 list_for_each_safe(pos, q, &md->part) {
2455 part_md = list_entry(pos, struct mmc_blk_data, part);
2457 mmc_blk_remove_req(part_md);
2461 static int mmc_add_disk(struct mmc_blk_data *md)
2464 struct mmc_card *card = md->queue.card;
2466 device_add_disk(md->parent, md->disk);
2467 md->force_ro.show = force_ro_show;
2468 md->force_ro.store = force_ro_store;
2469 sysfs_attr_init(&md->force_ro.attr);
2470 md->force_ro.attr.name = "force_ro";
2471 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2472 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2476 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2477 card->ext_csd.boot_ro_lockable) {
2480 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2483 mode = S_IRUGO | S_IWUSR;
2485 md->power_ro_lock.show = power_ro_lock_show;
2486 md->power_ro_lock.store = power_ro_lock_store;
2487 sysfs_attr_init(&md->power_ro_lock.attr);
2488 md->power_ro_lock.attr.mode = mode;
2489 md->power_ro_lock.attr.name =
2490 "ro_lock_until_next_power_on";
2491 ret = device_create_file(disk_to_dev(md->disk),
2492 &md->power_ro_lock);
2494 goto power_ro_lock_fail;
2499 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2501 del_gendisk(md->disk);
2506 static const struct mmc_fixup blk_fixups[] =
2508 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2509 MMC_QUIRK_INAND_CMD38),
2510 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2511 MMC_QUIRK_INAND_CMD38),
2512 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2513 MMC_QUIRK_INAND_CMD38),
2514 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2515 MMC_QUIRK_INAND_CMD38),
2516 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2517 MMC_QUIRK_INAND_CMD38),
2520 * Some MMC cards experience performance degradation with CMD23
2521 * instead of CMD12-bounded multiblock transfers. For now we'll
2522 * black list what's bad...
2523 * - Certain Toshiba cards.
2525 * N.B. This doesn't affect SD cards.
2527 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2528 MMC_QUIRK_BLK_NO_CMD23),
2529 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2530 MMC_QUIRK_BLK_NO_CMD23),
2531 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2532 MMC_QUIRK_BLK_NO_CMD23),
2533 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2534 MMC_QUIRK_BLK_NO_CMD23),
2535 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2536 MMC_QUIRK_BLK_NO_CMD23),
2539 * Some MMC cards need longer data read timeout than indicated in CSD.
2541 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
2542 MMC_QUIRK_LONG_READ_TIME),
2543 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2544 MMC_QUIRK_LONG_READ_TIME),
2547 * On these Samsung MoviNAND parts, performing secure erase or
2548 * secure trim can result in unrecoverable corruption due to a
2551 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2552 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2553 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2554 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2555 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2556 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2557 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2558 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2559 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2560 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2561 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2562 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2563 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2564 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2565 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2566 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2569 * On Some Kingston eMMCs, performing trim can result in
2570 * unrecoverable data conrruption occasionally due to a firmware bug.
2572 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2573 MMC_QUIRK_TRIM_BROKEN),
2574 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2575 MMC_QUIRK_TRIM_BROKEN),
2580 static int mmc_blk_probe(struct mmc_card *card)
2582 struct mmc_blk_data *md, *part_md;
2586 * Check that the card supports the command class(es) we need.
2588 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2591 mmc_fixup_device(card, blk_fixups);
2593 md = mmc_blk_alloc(card);
2597 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
2598 cap_str, sizeof(cap_str));
2599 pr_info("%s: %s %s %s %s\n",
2600 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2601 cap_str, md->read_only ? "(ro)" : "");
2603 if (mmc_blk_alloc_parts(card, md))
2606 dev_set_drvdata(&card->dev, md);
2608 if (mmc_add_disk(md))
2611 list_for_each_entry(part_md, &md->part, part) {
2612 if (mmc_add_disk(part_md))
2616 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2617 pm_runtime_use_autosuspend(&card->dev);
2620 * Don't enable runtime PM for SD-combo cards here. Leave that
2621 * decision to be taken during the SDIO init sequence instead.
2623 if (card->type != MMC_TYPE_SD_COMBO) {
2624 pm_runtime_set_active(&card->dev);
2625 pm_runtime_enable(&card->dev);
2631 mmc_blk_remove_parts(card, md);
2632 mmc_blk_remove_req(md);
2636 static void mmc_blk_remove(struct mmc_card *card)
2638 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2640 mmc_blk_remove_parts(card, md);
2641 pm_runtime_get_sync(&card->dev);
2642 mmc_claim_host(card->host);
2643 mmc_blk_part_switch(card, md);
2644 mmc_release_host(card->host);
2645 if (card->type != MMC_TYPE_SD_COMBO)
2646 pm_runtime_disable(&card->dev);
2647 pm_runtime_put_noidle(&card->dev);
2648 mmc_blk_remove_req(md);
2649 dev_set_drvdata(&card->dev, NULL);
2652 static int _mmc_blk_suspend(struct mmc_card *card)
2654 struct mmc_blk_data *part_md;
2655 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2658 mmc_queue_suspend(&md->queue);
2659 list_for_each_entry(part_md, &md->part, part) {
2660 mmc_queue_suspend(&part_md->queue);
2666 static void mmc_blk_shutdown(struct mmc_card *card)
2668 _mmc_blk_suspend(card);
2671 #ifdef CONFIG_PM_SLEEP
2672 static int mmc_blk_suspend(struct device *dev)
2674 struct mmc_card *card = mmc_dev_to_card(dev);
2676 return _mmc_blk_suspend(card);
2679 static int mmc_blk_resume(struct device *dev)
2681 struct mmc_blk_data *part_md;
2682 struct mmc_blk_data *md = dev_get_drvdata(dev);
2686 * Resume involves the card going into idle state,
2687 * so current partition is always the main one.
2689 md->part_curr = md->part_type;
2690 mmc_queue_resume(&md->queue);
2691 list_for_each_entry(part_md, &md->part, part) {
2692 mmc_queue_resume(&part_md->queue);
2699 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2701 static struct mmc_driver mmc_driver = {
2704 .pm = &mmc_blk_pm_ops,
2706 .probe = mmc_blk_probe,
2707 .remove = mmc_blk_remove,
2708 .shutdown = mmc_blk_shutdown,
2711 static int __init mmc_blk_init(void)
2715 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2716 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2718 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
2720 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2724 res = mmc_register_driver(&mmc_driver);
2730 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2735 static void __exit mmc_blk_exit(void)
2737 mmc_unregister_driver(&mmc_driver);
2738 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2741 module_init(mmc_blk_init);
2742 module_exit(mmc_blk_exit);
2744 MODULE_LICENSE("GPL");
2745 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");