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>
50 MODULE_ALIAS("mmc:block");
51 #ifdef MODULE_PARAM_PREFIX
52 #undef MODULE_PARAM_PREFIX
54 #define MODULE_PARAM_PREFIX "mmcblk."
56 #define INAND_CMD38_ARG_EXT_CSD 113
57 #define INAND_CMD38_ARG_ERASE 0x00
58 #define INAND_CMD38_ARG_TRIM 0x01
59 #define INAND_CMD38_ARG_SECERASE 0x80
60 #define INAND_CMD38_ARG_SECTRIM1 0x81
61 #define INAND_CMD38_ARG_SECTRIM2 0x88
62 #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
63 #define MMC_SANITIZE_REQ_TIMEOUT 240000
64 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
66 #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
67 (rq_data_dir(req) == WRITE))
68 #define PACKED_CMD_VER 0x01
69 #define PACKED_CMD_WR 0x02
71 static DEFINE_MUTEX(block_mutex);
74 * The defaults come from config options but can be overriden by module
77 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
80 * We've only got one major, so number of mmcblk devices is
81 * limited to (1 << 20) / number of minors per device. It is also
82 * limited by the MAX_DEVICES below.
84 static int max_devices;
86 #define MAX_DEVICES 256
88 static DEFINE_IDA(mmc_blk_ida);
89 static DEFINE_SPINLOCK(mmc_blk_lock);
92 * There is one mmc_blk_data per slot.
96 struct device *parent;
98 struct mmc_queue queue;
99 struct list_head part;
102 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
103 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
104 #define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
107 unsigned int read_only;
108 unsigned int part_type;
109 unsigned int reset_done;
110 #define MMC_BLK_READ BIT(0)
111 #define MMC_BLK_WRITE BIT(1)
112 #define MMC_BLK_DISCARD BIT(2)
113 #define MMC_BLK_SECDISCARD BIT(3)
116 * Only set in main mmc_blk_data associated
117 * with mmc_card with dev_set_drvdata, and keeps
118 * track of the current selected device partition.
120 unsigned int part_curr;
121 struct device_attribute force_ro;
122 struct device_attribute power_ro_lock;
126 static DEFINE_MUTEX(open_lock);
129 MMC_PACKED_NR_IDX = -1,
131 MMC_PACKED_NR_SINGLE,
134 module_param(perdev_minors, int, 0444);
135 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
137 static inline int mmc_blk_part_switch(struct mmc_card *card,
138 struct mmc_blk_data *md);
139 static int get_card_status(struct mmc_card *card, u32 *status, int retries);
141 static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
143 struct mmc_packed *packed = mqrq->packed;
147 mqrq->cmd_type = MMC_PACKED_NONE;
148 packed->nr_entries = MMC_PACKED_NR_ZERO;
149 packed->idx_failure = MMC_PACKED_NR_IDX;
154 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
156 struct mmc_blk_data *md;
158 mutex_lock(&open_lock);
159 md = disk->private_data;
160 if (md && md->usage == 0)
164 mutex_unlock(&open_lock);
169 static inline int mmc_get_devidx(struct gendisk *disk)
171 int devidx = disk->first_minor / perdev_minors;
175 static void mmc_blk_put(struct mmc_blk_data *md)
177 mutex_lock(&open_lock);
179 if (md->usage == 0) {
180 int devidx = mmc_get_devidx(md->disk);
181 blk_cleanup_queue(md->queue.queue);
183 spin_lock(&mmc_blk_lock);
184 ida_remove(&mmc_blk_ida, devidx);
185 spin_unlock(&mmc_blk_lock);
190 mutex_unlock(&open_lock);
193 static ssize_t power_ro_lock_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
197 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
198 struct mmc_card *card = md->queue.card;
201 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
203 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
206 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
213 static ssize_t power_ro_lock_store(struct device *dev,
214 struct device_attribute *attr, const char *buf, size_t count)
217 struct mmc_blk_data *md, *part_md;
218 struct mmc_card *card;
221 if (kstrtoul(buf, 0, &set))
227 md = mmc_blk_get(dev_to_disk(dev));
228 card = md->queue.card;
232 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
233 card->ext_csd.boot_ro_lock |
234 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
235 card->ext_csd.part_time);
237 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
239 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
244 pr_info("%s: Locking boot partition ro until next power on\n",
245 md->disk->disk_name);
246 set_disk_ro(md->disk, 1);
248 list_for_each_entry(part_md, &md->part, part)
249 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
250 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
251 set_disk_ro(part_md->disk, 1);
259 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
263 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
265 ret = snprintf(buf, PAGE_SIZE, "%d\n",
266 get_disk_ro(dev_to_disk(dev)) ^
272 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
273 const char *buf, size_t count)
277 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
278 unsigned long set = simple_strtoul(buf, &end, 0);
284 set_disk_ro(dev_to_disk(dev), set || md->read_only);
291 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
293 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
296 mutex_lock(&block_mutex);
299 check_disk_change(bdev);
302 if ((mode & FMODE_WRITE) && md->read_only) {
307 mutex_unlock(&block_mutex);
312 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
314 struct mmc_blk_data *md = disk->private_data;
316 mutex_lock(&block_mutex);
318 mutex_unlock(&block_mutex);
322 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
324 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
330 struct mmc_blk_ioc_data {
331 struct mmc_ioc_cmd ic;
336 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
337 struct mmc_ioc_cmd __user *user)
339 struct mmc_blk_ioc_data *idata;
342 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
348 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
353 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
354 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
359 if (!idata->buf_bytes) {
364 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
370 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
371 idata->ic.data_ptr, idata->buf_bytes)) {
386 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
387 struct mmc_blk_ioc_data *idata)
389 struct mmc_ioc_cmd *ic = &idata->ic;
391 if (copy_to_user(&(ic_ptr->response), ic->response,
392 sizeof(ic->response)))
395 if (!idata->ic.write_flag) {
396 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
397 idata->buf, idata->buf_bytes))
404 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
410 if (!status || !retries_max)
414 err = get_card_status(card, status, 5);
418 if (!R1_STATUS(*status) &&
419 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
420 break; /* RPMB programming operation complete */
423 * Rechedule to give the MMC device a chance to continue
424 * processing the previous command without being polled too
427 usleep_range(1000, 5000);
428 } while (++retry_count < retries_max);
430 if (retry_count == retries_max)
436 static int ioctl_do_sanitize(struct mmc_card *card)
440 if (!mmc_can_sanitize(card)) {
441 pr_warn("%s: %s - SANITIZE is not supported\n",
442 mmc_hostname(card->host), __func__);
447 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
448 mmc_hostname(card->host), __func__);
450 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
451 EXT_CSD_SANITIZE_START, 1,
452 MMC_SANITIZE_REQ_TIMEOUT);
455 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
456 mmc_hostname(card->host), __func__, err);
458 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
464 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
465 struct mmc_blk_ioc_data *idata)
467 struct mmc_command cmd = {0};
468 struct mmc_data data = {0};
469 struct mmc_request mrq = {NULL};
470 struct scatterlist sg;
475 if (!card || !md || !idata)
478 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
481 cmd.opcode = idata->ic.opcode;
482 cmd.arg = idata->ic.arg;
483 cmd.flags = idata->ic.flags;
485 if (idata->buf_bytes) {
488 data.blksz = idata->ic.blksz;
489 data.blocks = idata->ic.blocks;
491 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
493 if (idata->ic.write_flag)
494 data.flags = MMC_DATA_WRITE;
496 data.flags = MMC_DATA_READ;
498 /* data.flags must already be set before doing this. */
499 mmc_set_data_timeout(&data, card);
501 /* Allow overriding the timeout_ns for empirical tuning. */
502 if (idata->ic.data_timeout_ns)
503 data.timeout_ns = idata->ic.data_timeout_ns;
505 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
507 * Pretend this is a data transfer and rely on the
508 * host driver to compute timeout. When all host
509 * drivers support cmd.cmd_timeout for R1B, this
513 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
515 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
523 err = mmc_blk_part_switch(card, md);
527 if (idata->ic.is_acmd) {
528 err = mmc_app_cmd(card->host, card);
534 err = mmc_set_blockcount(card, data.blocks,
535 idata->ic.write_flag & (1 << 31));
540 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
541 (cmd.opcode == MMC_SWITCH)) {
542 err = ioctl_do_sanitize(card);
545 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
551 mmc_wait_for_req(card->host, &mrq);
554 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
555 __func__, cmd.error);
559 dev_err(mmc_dev(card->host), "%s: data error %d\n",
560 __func__, data.error);
565 * According to the SD specs, some commands require a delay after
566 * issuing the command.
568 if (idata->ic.postsleep_min_us)
569 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
571 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
575 * Ensure RPMB command has completed by polling CMD13
578 err = ioctl_rpmb_card_status_poll(card, &status, 5);
580 dev_err(mmc_dev(card->host),
581 "%s: Card Status=0x%08X, error %d\n",
582 __func__, status, err);
588 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
589 struct mmc_ioc_cmd __user *ic_ptr)
591 struct mmc_blk_ioc_data *idata;
592 struct mmc_blk_data *md;
593 struct mmc_card *card;
594 int err = 0, ioc_err = 0;
597 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
598 * whole block device, not on a partition. This prevents overspray
599 * between sibling partitions.
601 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
604 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
606 return PTR_ERR(idata);
608 md = mmc_blk_get(bdev->bd_disk);
614 card = md->queue.card;
622 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
624 /* Always switch back to main area after RPMB access */
625 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
626 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
630 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
637 return ioc_err ? ioc_err : err;
640 static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
641 struct mmc_ioc_multi_cmd __user *user)
643 struct mmc_blk_ioc_data **idata = NULL;
644 struct mmc_ioc_cmd __user *cmds = user->cmds;
645 struct mmc_card *card;
646 struct mmc_blk_data *md;
647 int i, err = 0, ioc_err = 0;
651 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
652 * whole block device, not on a partition. This prevents overspray
653 * between sibling partitions.
655 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
658 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
659 sizeof(num_of_cmds)))
662 if (num_of_cmds > MMC_IOC_MAX_CMDS)
665 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
669 for (i = 0; i < num_of_cmds; i++) {
670 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
671 if (IS_ERR(idata[i])) {
672 err = PTR_ERR(idata[i]);
678 md = mmc_blk_get(bdev->bd_disk);
684 card = md->queue.card;
692 for (i = 0; i < num_of_cmds && !ioc_err; i++)
693 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
695 /* Always switch back to main area after RPMB access */
696 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
697 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
701 /* copy to user if data and response */
702 for (i = 0; i < num_of_cmds && !err; i++)
703 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
708 for (i = 0; i < num_of_cmds; i++) {
709 kfree(idata[i]->buf);
713 return ioc_err ? ioc_err : err;
716 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
717 unsigned int cmd, unsigned long arg)
721 return mmc_blk_ioctl_cmd(bdev,
722 (struct mmc_ioc_cmd __user *)arg);
723 case MMC_IOC_MULTI_CMD:
724 return mmc_blk_ioctl_multi_cmd(bdev,
725 (struct mmc_ioc_multi_cmd __user *)arg);
732 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
733 unsigned int cmd, unsigned long arg)
735 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
739 static const struct block_device_operations mmc_bdops = {
740 .open = mmc_blk_open,
741 .release = mmc_blk_release,
742 .getgeo = mmc_blk_getgeo,
743 .owner = THIS_MODULE,
744 .ioctl = mmc_blk_ioctl,
746 .compat_ioctl = mmc_blk_compat_ioctl,
750 static inline int mmc_blk_part_switch(struct mmc_card *card,
751 struct mmc_blk_data *md)
754 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
756 if (main_md->part_curr == md->part_type)
759 if (mmc_card_mmc(card)) {
760 u8 part_config = card->ext_csd.part_config;
762 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
763 mmc_retune_pause(card->host);
765 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
766 part_config |= md->part_type;
768 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
769 EXT_CSD_PART_CONFIG, part_config,
770 card->ext_csd.part_time);
772 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
773 mmc_retune_unpause(card->host);
777 card->ext_csd.part_config = part_config;
779 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
780 mmc_retune_unpause(card->host);
783 main_md->part_curr = md->part_type;
787 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
793 struct mmc_request mrq = {NULL};
794 struct mmc_command cmd = {0};
795 struct mmc_data data = {0};
797 struct scatterlist sg;
799 cmd.opcode = MMC_APP_CMD;
800 cmd.arg = card->rca << 16;
801 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
803 err = mmc_wait_for_cmd(card->host, &cmd, 0);
806 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
809 memset(&cmd, 0, sizeof(struct mmc_command));
811 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
813 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
817 data.flags = MMC_DATA_READ;
820 mmc_set_data_timeout(&data, card);
825 blocks = kmalloc(4, GFP_KERNEL);
829 sg_init_one(&sg, blocks, 4);
831 mmc_wait_for_req(card->host, &mrq);
833 result = ntohl(*blocks);
836 if (cmd.error || data.error)
842 static int get_card_status(struct mmc_card *card, u32 *status, int retries)
844 struct mmc_command cmd = {0};
847 cmd.opcode = MMC_SEND_STATUS;
848 if (!mmc_host_is_spi(card->host))
849 cmd.arg = card->rca << 16;
850 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
851 err = mmc_wait_for_cmd(card->host, &cmd, retries);
853 *status = cmd.resp[0];
857 static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
858 bool hw_busy_detect, struct request *req, int *gen_err)
860 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
865 err = get_card_status(card, &status, 5);
867 pr_err("%s: error %d requesting status\n",
868 req->rq_disk->disk_name, err);
872 if (status & R1_ERROR) {
873 pr_err("%s: %s: error sending status cmd, status %#x\n",
874 req->rq_disk->disk_name, __func__, status);
878 /* We may rely on the host hw to handle busy detection.*/
879 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
884 * Timeout if the device never becomes ready for data and never
885 * leaves the program state.
887 if (time_after(jiffies, timeout)) {
888 pr_err("%s: Card stuck in programming state! %s %s\n",
889 mmc_hostname(card->host),
890 req->rq_disk->disk_name, __func__);
895 * Some cards mishandle the status bits,
896 * so make sure to check both the busy
897 * indication and the card state.
899 } while (!(status & R1_READY_FOR_DATA) ||
900 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
905 static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
906 struct request *req, int *gen_err, u32 *stop_status)
908 struct mmc_host *host = card->host;
909 struct mmc_command cmd = {0};
911 bool use_r1b_resp = rq_data_dir(req) == WRITE;
914 * Normally we use R1B responses for WRITE, but in cases where the host
915 * has specified a max_busy_timeout we need to validate it. A failure
916 * means we need to prevent the host from doing hw busy detection, which
917 * is done by converting to a R1 response instead.
919 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
920 use_r1b_resp = false;
922 cmd.opcode = MMC_STOP_TRANSMISSION;
924 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
925 cmd.busy_timeout = timeout_ms;
927 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
930 err = mmc_wait_for_cmd(host, &cmd, 5);
934 *stop_status = cmd.resp[0];
936 /* No need to check card status in case of READ. */
937 if (rq_data_dir(req) == READ)
940 if (!mmc_host_is_spi(host) &&
941 (*stop_status & R1_ERROR)) {
942 pr_err("%s: %s: general error sending stop command, resp %#x\n",
943 req->rq_disk->disk_name, __func__, *stop_status);
947 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
950 #define ERR_NOMEDIUM 3
953 #define ERR_CONTINUE 0
955 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
956 bool status_valid, u32 status)
960 /* response crc error, retry the r/w cmd */
961 pr_err("%s: %s sending %s command, card status %#x\n",
962 req->rq_disk->disk_name, "response CRC error",
967 pr_err("%s: %s sending %s command, card status %#x\n",
968 req->rq_disk->disk_name, "timed out", name, status);
970 /* If the status cmd initially failed, retry the r/w cmd */
972 pr_err("%s: status not valid, retrying timeout\n",
973 req->rq_disk->disk_name);
978 * If it was a r/w cmd crc error, or illegal command
979 * (eg, issued in wrong state) then retry - we should
980 * have corrected the state problem above.
982 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
983 pr_err("%s: command error, retrying timeout\n",
984 req->rq_disk->disk_name);
988 /* Otherwise abort the command */
992 /* We don't understand the error code the driver gave us */
993 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
994 req->rq_disk->disk_name, error, status);
1000 * Initial r/w and stop cmd error recovery.
1001 * We don't know whether the card received the r/w cmd or not, so try to
1002 * restore things back to a sane state. Essentially, we do this as follows:
1003 * - Obtain card status. If the first attempt to obtain card status fails,
1004 * the status word will reflect the failed status cmd, not the failed
1005 * r/w cmd. If we fail to obtain card status, it suggests we can no
1006 * longer communicate with the card.
1007 * - Check the card state. If the card received the cmd but there was a
1008 * transient problem with the response, it might still be in a data transfer
1009 * mode. Try to send it a stop command. If this fails, we can't recover.
1010 * - If the r/w cmd failed due to a response CRC error, it was probably
1011 * transient, so retry the cmd.
1012 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1013 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1014 * illegal cmd, retry.
1015 * Otherwise we don't understand what happened, so abort.
1017 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
1018 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
1020 bool prev_cmd_status_valid = true;
1021 u32 status, stop_status = 0;
1024 if (mmc_card_removed(card))
1025 return ERR_NOMEDIUM;
1028 * Try to get card status which indicates both the card state
1029 * and why there was no response. If the first attempt fails,
1030 * we can't be sure the returned status is for the r/w command.
1032 for (retry = 2; retry >= 0; retry--) {
1033 err = get_card_status(card, &status, 0);
1037 /* Re-tune if needed */
1038 mmc_retune_recheck(card->host);
1040 prev_cmd_status_valid = false;
1041 pr_err("%s: error %d sending status command, %sing\n",
1042 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1045 /* We couldn't get a response from the card. Give up. */
1047 /* Check if the card is removed */
1048 if (mmc_detect_card_removed(card->host))
1049 return ERR_NOMEDIUM;
1053 /* Flag ECC errors */
1054 if ((status & R1_CARD_ECC_FAILED) ||
1055 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1056 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1059 /* Flag General errors */
1060 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1061 if ((status & R1_ERROR) ||
1062 (brq->stop.resp[0] & R1_ERROR)) {
1063 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1064 req->rq_disk->disk_name, __func__,
1065 brq->stop.resp[0], status);
1070 * Check the current card state. If it is in some data transfer
1071 * mode, tell it to stop (and hopefully transition back to TRAN.)
1073 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1074 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
1075 err = send_stop(card,
1076 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1077 req, gen_err, &stop_status);
1079 pr_err("%s: error %d sending stop command\n",
1080 req->rq_disk->disk_name, err);
1082 * If the stop cmd also timed out, the card is probably
1083 * not present, so abort. Other errors are bad news too.
1088 if (stop_status & R1_CARD_ECC_FAILED)
1092 /* Check for set block count errors */
1094 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1095 prev_cmd_status_valid, status);
1097 /* Check for r/w command errors */
1099 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1100 prev_cmd_status_valid, status);
1103 if (!brq->stop.error)
1104 return ERR_CONTINUE;
1106 /* Now for stop errors. These aren't fatal to the transfer. */
1107 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
1108 req->rq_disk->disk_name, brq->stop.error,
1109 brq->cmd.resp[0], status);
1112 * Subsitute in our own stop status as this will give the error
1113 * state which happened during the execution of the r/w command.
1116 brq->stop.resp[0] = stop_status;
1117 brq->stop.error = 0;
1119 return ERR_CONTINUE;
1122 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1127 if (md->reset_done & type)
1130 md->reset_done |= type;
1131 err = mmc_hw_reset(host);
1132 /* Ensure we switch back to the correct partition */
1133 if (err != -EOPNOTSUPP) {
1134 struct mmc_blk_data *main_md =
1135 dev_get_drvdata(&host->card->dev);
1138 main_md->part_curr = main_md->part_type;
1139 part_err = mmc_blk_part_switch(host->card, md);
1142 * We have failed to get back into the correct
1143 * partition, so we need to abort the whole request.
1151 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1153 md->reset_done &= ~type;
1156 int mmc_access_rpmb(struct mmc_queue *mq)
1158 struct mmc_blk_data *md = mq->data;
1160 * If this is a RPMB partition access, return ture
1162 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1168 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1170 struct mmc_blk_data *md = mq->data;
1171 struct mmc_card *card = md->queue.card;
1172 unsigned int from, nr, arg;
1173 int err = 0, type = MMC_BLK_DISCARD;
1175 if (!mmc_can_erase(card)) {
1180 from = blk_rq_pos(req);
1181 nr = blk_rq_sectors(req);
1183 if (mmc_can_discard(card))
1184 arg = MMC_DISCARD_ARG;
1185 else if (mmc_can_trim(card))
1188 arg = MMC_ERASE_ARG;
1190 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1191 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1192 INAND_CMD38_ARG_EXT_CSD,
1193 arg == MMC_TRIM_ARG ?
1194 INAND_CMD38_ARG_TRIM :
1195 INAND_CMD38_ARG_ERASE,
1200 err = mmc_erase(card, from, nr, arg);
1202 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1205 mmc_blk_reset_success(md, type);
1206 blk_end_request(req, err, blk_rq_bytes(req));
1211 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1212 struct request *req)
1214 struct mmc_blk_data *md = mq->data;
1215 struct mmc_card *card = md->queue.card;
1216 unsigned int from, nr, arg;
1217 int err = 0, type = MMC_BLK_SECDISCARD;
1219 if (!(mmc_can_secure_erase_trim(card))) {
1224 from = blk_rq_pos(req);
1225 nr = blk_rq_sectors(req);
1227 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1228 arg = MMC_SECURE_TRIM1_ARG;
1230 arg = MMC_SECURE_ERASE_ARG;
1233 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1234 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1235 INAND_CMD38_ARG_EXT_CSD,
1236 arg == MMC_SECURE_TRIM1_ARG ?
1237 INAND_CMD38_ARG_SECTRIM1 :
1238 INAND_CMD38_ARG_SECERASE,
1244 err = mmc_erase(card, from, nr, arg);
1250 if (arg == MMC_SECURE_TRIM1_ARG) {
1251 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1252 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1253 INAND_CMD38_ARG_EXT_CSD,
1254 INAND_CMD38_ARG_SECTRIM2,
1260 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1268 if (err && !mmc_blk_reset(md, card->host, type))
1271 mmc_blk_reset_success(md, type);
1273 blk_end_request(req, err, blk_rq_bytes(req));
1278 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1280 struct mmc_blk_data *md = mq->data;
1281 struct mmc_card *card = md->queue.card;
1284 ret = mmc_flush_cache(card);
1288 blk_end_request_all(req, ret);
1294 * Reformat current write as a reliable write, supporting
1295 * both legacy and the enhanced reliable write MMC cards.
1296 * In each transfer we'll handle only as much as a single
1297 * reliable write can handle, thus finish the request in
1298 * partial completions.
1300 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1301 struct mmc_card *card,
1302 struct request *req)
1304 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1305 /* Legacy mode imposes restrictions on transfers. */
1306 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1307 brq->data.blocks = 1;
1309 if (brq->data.blocks > card->ext_csd.rel_sectors)
1310 brq->data.blocks = card->ext_csd.rel_sectors;
1311 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1312 brq->data.blocks = 1;
1316 #define CMD_ERRORS \
1317 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1318 R1_ADDRESS_ERROR | /* Misaligned address */ \
1319 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1320 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1321 R1_CC_ERROR | /* Card controller error */ \
1322 R1_ERROR) /* General/unknown error */
1324 static int mmc_blk_err_check(struct mmc_card *card,
1325 struct mmc_async_req *areq)
1327 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1329 struct mmc_blk_request *brq = &mq_mrq->brq;
1330 struct request *req = mq_mrq->req;
1331 int need_retune = card->host->need_retune;
1332 int ecc_err = 0, gen_err = 0;
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;
1449 check = mmc_blk_err_check(card, areq);
1450 err = get_card_status(card, &status, 0);
1452 pr_err("%s: error %d sending status command\n",
1453 req->rq_disk->disk_name, err);
1454 return MMC_BLK_ABORT;
1457 if (status & R1_EXCEPTION_EVENT) {
1458 err = mmc_get_ext_csd(card, &ext_csd);
1460 pr_err("%s: error %d sending ext_csd\n",
1461 req->rq_disk->disk_name, err);
1462 return MMC_BLK_ABORT;
1465 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1466 EXT_CSD_PACKED_FAILURE) &&
1467 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1468 EXT_CSD_PACKED_GENERIC_ERROR)) {
1469 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1470 EXT_CSD_PACKED_INDEXED_ERROR) {
1471 packed->idx_failure =
1472 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1473 check = MMC_BLK_PARTIAL;
1475 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1476 "failure index: %d\n",
1477 req->rq_disk->disk_name, packed->nr_entries,
1478 packed->blocks, packed->idx_failure);
1486 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1487 struct mmc_card *card,
1489 struct mmc_queue *mq)
1491 u32 readcmd, writecmd;
1492 struct mmc_blk_request *brq = &mqrq->brq;
1493 struct request *req = mqrq->req;
1494 struct mmc_blk_data *md = mq->data;
1498 * Reliable writes are used to implement Forced Unit Access and
1499 * are supported only on MMCs.
1501 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1502 (rq_data_dir(req) == WRITE) &&
1503 (md->flags & MMC_BLK_REL_WR);
1505 memset(brq, 0, sizeof(struct mmc_blk_request));
1506 brq->mrq.cmd = &brq->cmd;
1507 brq->mrq.data = &brq->data;
1509 brq->cmd.arg = blk_rq_pos(req);
1510 if (!mmc_card_blockaddr(card))
1512 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1513 brq->data.blksz = 512;
1514 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1516 brq->data.blocks = blk_rq_sectors(req);
1519 * The block layer doesn't support all sector count
1520 * restrictions, so we need to be prepared for too big
1523 if (brq->data.blocks > card->host->max_blk_count)
1524 brq->data.blocks = card->host->max_blk_count;
1526 if (brq->data.blocks > 1) {
1528 * After a read error, we redo the request one sector
1529 * at a time in order to accurately determine which
1530 * sectors can be read successfully.
1533 brq->data.blocks = 1;
1536 * Some controllers have HW issues while operating
1537 * in multiple I/O mode
1539 if (card->host->ops->multi_io_quirk)
1540 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1541 (rq_data_dir(req) == READ) ?
1542 MMC_DATA_READ : MMC_DATA_WRITE,
1546 if (brq->data.blocks > 1 || do_rel_wr) {
1547 /* SPI multiblock writes terminate using a special
1548 * token, not a STOP_TRANSMISSION request.
1550 if (!mmc_host_is_spi(card->host) ||
1551 rq_data_dir(req) == READ)
1552 brq->mrq.stop = &brq->stop;
1553 readcmd = MMC_READ_MULTIPLE_BLOCK;
1554 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1556 brq->mrq.stop = NULL;
1557 readcmd = MMC_READ_SINGLE_BLOCK;
1558 writecmd = MMC_WRITE_BLOCK;
1560 if (rq_data_dir(req) == READ) {
1561 brq->cmd.opcode = readcmd;
1562 brq->data.flags = MMC_DATA_READ;
1564 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1567 brq->cmd.opcode = writecmd;
1568 brq->data.flags = MMC_DATA_WRITE;
1570 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1575 mmc_apply_rel_rw(brq, card, req);
1578 * Data tag is used only during writing meta data to speed
1579 * up write and any subsequent read of this meta data
1581 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1582 (req->cmd_flags & REQ_META) &&
1583 (rq_data_dir(req) == WRITE) &&
1584 ((brq->data.blocks * brq->data.blksz) >=
1585 card->ext_csd.data_tag_unit_size);
1588 * Pre-defined multi-block transfers are preferable to
1589 * open ended-ones (and necessary for reliable writes).
1590 * However, it is not sufficient to just send CMD23,
1591 * and avoid the final CMD12, as on an error condition
1592 * CMD12 (stop) needs to be sent anyway. This, coupled
1593 * with Auto-CMD23 enhancements provided by some
1594 * hosts, means that the complexity of dealing
1595 * with this is best left to the host. If CMD23 is
1596 * supported by card and host, we'll fill sbc in and let
1597 * the host deal with handling it correctly. This means
1598 * that for hosts that don't expose MMC_CAP_CMD23, no
1599 * change of behavior will be observed.
1601 * N.B: Some MMC cards experience perf degradation.
1602 * We'll avoid using CMD23-bounded multiblock writes for
1603 * these, while retaining features like reliable writes.
1605 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1606 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1608 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1609 brq->sbc.arg = brq->data.blocks |
1610 (do_rel_wr ? (1 << 31) : 0) |
1611 (do_data_tag ? (1 << 29) : 0);
1612 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1613 brq->mrq.sbc = &brq->sbc;
1616 mmc_set_data_timeout(&brq->data, card);
1618 brq->data.sg = mqrq->sg;
1619 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1622 * Adjust the sg list so it is the same size as the
1625 if (brq->data.blocks != blk_rq_sectors(req)) {
1626 int i, data_size = brq->data.blocks << 9;
1627 struct scatterlist *sg;
1629 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1630 data_size -= sg->length;
1631 if (data_size <= 0) {
1632 sg->length += data_size;
1637 brq->data.sg_len = i;
1640 mqrq->mmc_active.mrq = &brq->mrq;
1641 mqrq->mmc_active.err_check = mmc_blk_err_check;
1643 mmc_queue_bounce_pre(mqrq);
1646 static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1647 struct mmc_card *card)
1649 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1650 unsigned int max_seg_sz = queue_max_segment_size(q);
1651 unsigned int len, nr_segs = 0;
1654 len = min(hdr_sz, max_seg_sz);
1662 static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1664 struct request_queue *q = mq->queue;
1665 struct mmc_card *card = mq->card;
1666 struct request *cur = req, *next = NULL;
1667 struct mmc_blk_data *md = mq->data;
1668 struct mmc_queue_req *mqrq = mq->mqrq_cur;
1669 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1670 unsigned int req_sectors = 0, phys_segments = 0;
1671 unsigned int max_blk_count, max_phys_segs;
1672 bool put_back = true;
1673 u8 max_packed_rw = 0;
1676 if (!(md->flags & MMC_BLK_PACKED_CMD))
1679 if ((rq_data_dir(cur) == WRITE) &&
1680 mmc_host_packed_wr(card->host))
1681 max_packed_rw = card->ext_csd.max_packed_writes;
1683 if (max_packed_rw == 0)
1686 if (mmc_req_rel_wr(cur) &&
1687 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1690 if (mmc_large_sector(card) &&
1691 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1694 mmc_blk_clear_packed(mqrq);
1696 max_blk_count = min(card->host->max_blk_count,
1697 card->host->max_req_size >> 9);
1698 if (unlikely(max_blk_count > 0xffff))
1699 max_blk_count = 0xffff;
1701 max_phys_segs = queue_max_segments(q);
1702 req_sectors += blk_rq_sectors(cur);
1703 phys_segments += cur->nr_phys_segments;
1705 if (rq_data_dir(cur) == WRITE) {
1706 req_sectors += mmc_large_sector(card) ? 8 : 1;
1707 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1711 if (reqs >= max_packed_rw - 1) {
1716 spin_lock_irq(q->queue_lock);
1717 next = blk_fetch_request(q);
1718 spin_unlock_irq(q->queue_lock);
1724 if (mmc_large_sector(card) &&
1725 !IS_ALIGNED(blk_rq_sectors(next), 8))
1728 if (req_op(next) == REQ_OP_DISCARD ||
1729 req_op(next) == REQ_OP_FLUSH)
1732 if (rq_data_dir(cur) != rq_data_dir(next))
1735 if (mmc_req_rel_wr(next) &&
1736 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1739 req_sectors += blk_rq_sectors(next);
1740 if (req_sectors > max_blk_count)
1743 phys_segments += next->nr_phys_segments;
1744 if (phys_segments > max_phys_segs)
1747 list_add_tail(&next->queuelist, &mqrq->packed->list);
1753 spin_lock_irq(q->queue_lock);
1754 blk_requeue_request(q, next);
1755 spin_unlock_irq(q->queue_lock);
1759 list_add(&req->queuelist, &mqrq->packed->list);
1760 mqrq->packed->nr_entries = ++reqs;
1761 mqrq->packed->retries = reqs;
1766 mqrq->cmd_type = MMC_PACKED_NONE;
1770 static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1771 struct mmc_card *card,
1772 struct mmc_queue *mq)
1774 struct mmc_blk_request *brq = &mqrq->brq;
1775 struct request *req = mqrq->req;
1776 struct request *prq;
1777 struct mmc_blk_data *md = mq->data;
1778 struct mmc_packed *packed = mqrq->packed;
1779 bool do_rel_wr, do_data_tag;
1780 u32 *packed_cmd_hdr;
1786 mqrq->cmd_type = MMC_PACKED_WRITE;
1788 packed->idx_failure = MMC_PACKED_NR_IDX;
1790 packed_cmd_hdr = packed->cmd_hdr;
1791 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
1792 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
1793 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
1794 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1797 * Argument for each entry of packed group
1799 list_for_each_entry(prq, &packed->list, queuelist) {
1800 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1801 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1802 (prq->cmd_flags & REQ_META) &&
1803 (rq_data_dir(prq) == WRITE) &&
1804 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
1805 /* Argument of CMD23 */
1806 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
1807 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1808 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1809 blk_rq_sectors(prq));
1810 /* Argument of CMD18 or CMD25 */
1811 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
1812 mmc_card_blockaddr(card) ?
1813 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
1814 packed->blocks += blk_rq_sectors(prq);
1818 memset(brq, 0, sizeof(struct mmc_blk_request));
1819 brq->mrq.cmd = &brq->cmd;
1820 brq->mrq.data = &brq->data;
1821 brq->mrq.sbc = &brq->sbc;
1822 brq->mrq.stop = &brq->stop;
1824 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1825 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1826 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1828 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1829 brq->cmd.arg = blk_rq_pos(req);
1830 if (!mmc_card_blockaddr(card))
1832 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1834 brq->data.blksz = 512;
1835 brq->data.blocks = packed->blocks + hdr_blocks;
1836 brq->data.flags = MMC_DATA_WRITE;
1838 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1840 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1842 mmc_set_data_timeout(&brq->data, card);
1844 brq->data.sg = mqrq->sg;
1845 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1847 mqrq->mmc_active.mrq = &brq->mrq;
1848 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1850 mmc_queue_bounce_pre(mqrq);
1853 static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1854 struct mmc_blk_request *brq, struct request *req,
1857 struct mmc_queue_req *mq_rq;
1858 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1861 * If this is an SD card and we're writing, we can first
1862 * mark the known good sectors as ok.
1864 * If the card is not SD, we can still ok written sectors
1865 * as reported by the controller (which might be less than
1866 * the real number of written sectors, but never more).
1868 if (mmc_card_sd(card)) {
1871 blocks = mmc_sd_num_wr_blocks(card);
1872 if (blocks != (u32)-1) {
1873 ret = blk_end_request(req, 0, blocks << 9);
1876 if (!mmc_packed_cmd(mq_rq->cmd_type))
1877 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1882 static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1884 struct request *prq;
1885 struct mmc_packed *packed = mq_rq->packed;
1886 int idx = packed->idx_failure, i = 0;
1891 while (!list_empty(&packed->list)) {
1892 prq = list_entry_rq(packed->list.next);
1894 /* retry from error index */
1895 packed->nr_entries -= idx;
1899 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1900 list_del_init(&prq->queuelist);
1901 mmc_blk_clear_packed(mq_rq);
1905 list_del_init(&prq->queuelist);
1906 blk_end_request(prq, 0, blk_rq_bytes(prq));
1910 mmc_blk_clear_packed(mq_rq);
1914 static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1916 struct request *prq;
1917 struct mmc_packed *packed = mq_rq->packed;
1921 while (!list_empty(&packed->list)) {
1922 prq = list_entry_rq(packed->list.next);
1923 list_del_init(&prq->queuelist);
1924 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1927 mmc_blk_clear_packed(mq_rq);
1930 static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1931 struct mmc_queue_req *mq_rq)
1933 struct request *prq;
1934 struct request_queue *q = mq->queue;
1935 struct mmc_packed *packed = mq_rq->packed;
1939 while (!list_empty(&packed->list)) {
1940 prq = list_entry_rq(packed->list.prev);
1941 if (prq->queuelist.prev != &packed->list) {
1942 list_del_init(&prq->queuelist);
1943 spin_lock_irq(q->queue_lock);
1944 blk_requeue_request(mq->queue, prq);
1945 spin_unlock_irq(q->queue_lock);
1947 list_del_init(&prq->queuelist);
1951 mmc_blk_clear_packed(mq_rq);
1954 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
1956 struct mmc_blk_data *md = mq->data;
1957 struct mmc_card *card = md->queue.card;
1958 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
1959 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
1960 enum mmc_blk_status status;
1961 struct mmc_queue_req *mq_rq;
1962 struct request *req = rqc;
1963 struct mmc_async_req *areq;
1964 const u8 packed_nr = 2;
1967 if (!rqc && !mq->mqrq_prev->req)
1971 reqs = mmc_blk_prep_packed_list(mq, rqc);
1976 * When 4KB native sector is enabled, only 8 blocks
1977 * multiple read or write is allowed
1979 if (mmc_large_sector(card) &&
1980 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
1981 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1982 req->rq_disk->disk_name);
1983 mq_rq = mq->mqrq_cur;
1987 if (reqs >= packed_nr)
1988 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1991 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1992 areq = &mq->mqrq_cur->mmc_active;
1995 areq = mmc_start_req(card->host, areq, (int *) &status);
1997 if (status == MMC_BLK_NEW_REQUEST)
1998 mq->flags |= MMC_QUEUE_NEW_REQUEST;
2002 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2005 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
2006 mmc_queue_bounce_post(mq_rq);
2009 case MMC_BLK_SUCCESS:
2010 case MMC_BLK_PARTIAL:
2012 * A block was successfully transferred.
2014 mmc_blk_reset_success(md, type);
2016 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2017 ret = mmc_blk_end_packed_req(mq_rq);
2020 ret = blk_end_request(req, 0,
2021 brq->data.bytes_xfered);
2025 * If the blk_end_request function returns non-zero even
2026 * though all data has been transferred and no errors
2027 * were returned by the host controller, it's a bug.
2029 if (status == MMC_BLK_SUCCESS && ret) {
2030 pr_err("%s BUG rq_tot %d d_xfer %d\n",
2031 __func__, blk_rq_bytes(req),
2032 brq->data.bytes_xfered);
2037 case MMC_BLK_CMD_ERR:
2038 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
2039 if (mmc_blk_reset(md, card->host, type))
2045 retune_retry_done = brq->retune_retry_done;
2050 if (!mmc_blk_reset(md, card->host, type))
2053 case MMC_BLK_DATA_ERR: {
2056 err = mmc_blk_reset(md, card->host, type);
2059 if (err == -ENODEV ||
2060 mmc_packed_cmd(mq_rq->cmd_type))
2064 case MMC_BLK_ECC_ERR:
2065 if (brq->data.blocks > 1) {
2066 /* Redo read one sector at a time */
2067 pr_warn("%s: retrying using single block read\n",
2068 req->rq_disk->disk_name);
2073 * After an error, we redo I/O one sector at a
2074 * time, so we only reach here after trying to
2075 * read a single sector.
2077 ret = blk_end_request(req, -EIO,
2082 case MMC_BLK_NOMEDIUM:
2085 pr_err("%s: Unhandled return value (%d)",
2086 req->rq_disk->disk_name, status);
2091 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2092 if (!mq_rq->packed->retries)
2094 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2095 mmc_start_req(card->host,
2096 &mq_rq->mmc_active, NULL);
2100 * In case of a incomplete request
2101 * prepare it again and resend.
2103 mmc_blk_rw_rq_prep(mq_rq, card,
2105 mmc_start_req(card->host,
2106 &mq_rq->mmc_active, NULL);
2108 mq_rq->brq.retune_retry_done = retune_retry_done;
2115 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2116 mmc_blk_abort_packed_req(mq_rq);
2118 if (mmc_card_removed(card))
2119 req->cmd_flags |= REQ_QUIET;
2121 ret = blk_end_request(req, -EIO,
2122 blk_rq_cur_bytes(req));
2127 if (mmc_card_removed(card)) {
2128 rqc->cmd_flags |= REQ_QUIET;
2129 blk_end_request_all(rqc, -EIO);
2132 * If current request is packed, it needs to put back.
2134 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2135 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2137 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2138 mmc_start_req(card->host,
2139 &mq->mqrq_cur->mmc_active, NULL);
2146 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2149 struct mmc_blk_data *md = mq->data;
2150 struct mmc_card *card = md->queue.card;
2151 struct mmc_host *host = card->host;
2152 unsigned long flags;
2154 if (req && !mq->mqrq_prev->req)
2155 /* claim host only for the first request */
2158 ret = mmc_blk_part_switch(card, md);
2161 blk_end_request_all(req, -EIO);
2167 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
2168 if (req && req_op(req) == REQ_OP_DISCARD) {
2169 /* complete ongoing async transfer before issuing discard */
2170 if (card->host->areq)
2171 mmc_blk_issue_rw_rq(mq, NULL);
2172 ret = mmc_blk_issue_discard_rq(mq, req);
2173 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
2174 /* complete ongoing async transfer before issuing secure erase*/
2175 if (card->host->areq)
2176 mmc_blk_issue_rw_rq(mq, NULL);
2177 ret = mmc_blk_issue_secdiscard_rq(mq, req);
2178 } else if (req && req_op(req) == REQ_OP_FLUSH) {
2179 /* complete ongoing async transfer before issuing flush */
2180 if (card->host->areq)
2181 mmc_blk_issue_rw_rq(mq, NULL);
2182 ret = mmc_blk_issue_flush(mq, req);
2184 if (!req && host->areq) {
2185 spin_lock_irqsave(&host->context_info.lock, flags);
2186 host->context_info.is_waiting_last_req = true;
2187 spin_unlock_irqrestore(&host->context_info.lock, flags);
2189 ret = mmc_blk_issue_rw_rq(mq, req);
2193 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
2194 mmc_req_is_special(req))
2196 * Release host when there are no more requests
2197 * and after special request(discard, flush) is done.
2198 * In case sepecial request, there is no reentry to
2199 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2205 static inline int mmc_blk_readonly(struct mmc_card *card)
2207 return mmc_card_readonly(card) ||
2208 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2211 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2212 struct device *parent,
2215 const char *subname,
2218 struct mmc_blk_data *md;
2222 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
2223 return ERR_PTR(-ENOMEM);
2225 spin_lock(&mmc_blk_lock);
2226 ret = ida_get_new(&mmc_blk_ida, &devidx);
2227 spin_unlock(&mmc_blk_lock);
2232 return ERR_PTR(ret);
2234 if (devidx >= max_devices) {
2239 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2245 md->area_type = area_type;
2248 * Set the read-only status based on the supported commands
2249 * and the write protect switch.
2251 md->read_only = mmc_blk_readonly(card);
2253 md->disk = alloc_disk(perdev_minors);
2254 if (md->disk == NULL) {
2259 spin_lock_init(&md->lock);
2260 INIT_LIST_HEAD(&md->part);
2263 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2267 md->queue.issue_fn = mmc_blk_issue_rq;
2268 md->queue.data = md;
2270 md->disk->major = MMC_BLOCK_MAJOR;
2271 md->disk->first_minor = devidx * perdev_minors;
2272 md->disk->fops = &mmc_bdops;
2273 md->disk->private_data = md;
2274 md->disk->queue = md->queue.queue;
2275 md->parent = parent;
2276 set_disk_ro(md->disk, md->read_only || default_ro);
2277 md->disk->flags = GENHD_FL_EXT_DEVT;
2278 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2279 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
2282 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2284 * - be set for removable media with permanent block devices
2285 * - be unset for removable block devices with permanent media
2287 * Since MMC block devices clearly fall under the second
2288 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2289 * should use the block device creation/destruction hotplug
2290 * messages to tell when the card is present.
2293 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2294 "mmcblk%u%s", card->host->index, subname ? subname : "");
2296 if (mmc_card_mmc(card))
2297 blk_queue_logical_block_size(md->queue.queue,
2298 card->ext_csd.data_sector_size);
2300 blk_queue_logical_block_size(md->queue.queue, 512);
2302 set_capacity(md->disk, size);
2304 if (mmc_host_cmd23(card->host)) {
2305 if (mmc_card_mmc(card) ||
2306 (mmc_card_sd(card) &&
2307 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2308 md->flags |= MMC_BLK_CMD23;
2311 if (mmc_card_mmc(card) &&
2312 md->flags & MMC_BLK_CMD23 &&
2313 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2314 card->ext_csd.rel_sectors)) {
2315 md->flags |= MMC_BLK_REL_WR;
2316 blk_queue_write_cache(md->queue.queue, true, true);
2319 if (mmc_card_mmc(card) &&
2320 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2321 (md->flags & MMC_BLK_CMD23) &&
2322 card->ext_csd.packed_event_en) {
2323 if (!mmc_packed_init(&md->queue, card))
2324 md->flags |= MMC_BLK_PACKED_CMD;
2334 spin_lock(&mmc_blk_lock);
2335 ida_remove(&mmc_blk_ida, devidx);
2336 spin_unlock(&mmc_blk_lock);
2337 return ERR_PTR(ret);
2340 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2344 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2346 * The EXT_CSD sector count is in number or 512 byte
2349 size = card->ext_csd.sectors;
2352 * The CSD capacity field is in units of read_blkbits.
2353 * set_capacity takes units of 512 bytes.
2355 size = (typeof(sector_t))card->csd.capacity
2356 << (card->csd.read_blkbits - 9);
2359 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2360 MMC_BLK_DATA_AREA_MAIN);
2363 static int mmc_blk_alloc_part(struct mmc_card *card,
2364 struct mmc_blk_data *md,
2365 unsigned int part_type,
2368 const char *subname,
2372 struct mmc_blk_data *part_md;
2374 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2375 subname, area_type);
2376 if (IS_ERR(part_md))
2377 return PTR_ERR(part_md);
2378 part_md->part_type = part_type;
2379 list_add(&part_md->part, &md->part);
2381 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
2382 cap_str, sizeof(cap_str));
2383 pr_info("%s: %s %s partition %u %s\n",
2384 part_md->disk->disk_name, mmc_card_id(card),
2385 mmc_card_name(card), part_md->part_type, cap_str);
2389 /* MMC Physical partitions consist of two boot partitions and
2390 * up to four general purpose partitions.
2391 * For each partition enabled in EXT_CSD a block device will be allocatedi
2392 * to provide access to the partition.
2395 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2399 if (!mmc_card_mmc(card))
2402 for (idx = 0; idx < card->nr_parts; idx++) {
2403 if (card->part[idx].size) {
2404 ret = mmc_blk_alloc_part(card, md,
2405 card->part[idx].part_cfg,
2406 card->part[idx].size >> 9,
2407 card->part[idx].force_ro,
2408 card->part[idx].name,
2409 card->part[idx].area_type);
2418 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2420 struct mmc_card *card;
2424 * Flush remaining requests and free queues. It
2425 * is freeing the queue that stops new requests
2426 * from being accepted.
2428 card = md->queue.card;
2429 mmc_cleanup_queue(&md->queue);
2430 if (md->flags & MMC_BLK_PACKED_CMD)
2431 mmc_packed_clean(&md->queue);
2432 if (md->disk->flags & GENHD_FL_UP) {
2433 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2434 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2435 card->ext_csd.boot_ro_lockable)
2436 device_remove_file(disk_to_dev(md->disk),
2437 &md->power_ro_lock);
2439 del_gendisk(md->disk);
2445 static void mmc_blk_remove_parts(struct mmc_card *card,
2446 struct mmc_blk_data *md)
2448 struct list_head *pos, *q;
2449 struct mmc_blk_data *part_md;
2451 list_for_each_safe(pos, q, &md->part) {
2452 part_md = list_entry(pos, struct mmc_blk_data, part);
2454 mmc_blk_remove_req(part_md);
2458 static int mmc_add_disk(struct mmc_blk_data *md)
2461 struct mmc_card *card = md->queue.card;
2463 device_add_disk(md->parent, md->disk);
2464 md->force_ro.show = force_ro_show;
2465 md->force_ro.store = force_ro_store;
2466 sysfs_attr_init(&md->force_ro.attr);
2467 md->force_ro.attr.name = "force_ro";
2468 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2469 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2473 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2474 card->ext_csd.boot_ro_lockable) {
2477 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2480 mode = S_IRUGO | S_IWUSR;
2482 md->power_ro_lock.show = power_ro_lock_show;
2483 md->power_ro_lock.store = power_ro_lock_store;
2484 sysfs_attr_init(&md->power_ro_lock.attr);
2485 md->power_ro_lock.attr.mode = mode;
2486 md->power_ro_lock.attr.name =
2487 "ro_lock_until_next_power_on";
2488 ret = device_create_file(disk_to_dev(md->disk),
2489 &md->power_ro_lock);
2491 goto power_ro_lock_fail;
2496 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2498 del_gendisk(md->disk);
2503 static const struct mmc_fixup blk_fixups[] =
2505 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2506 MMC_QUIRK_INAND_CMD38),
2507 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2508 MMC_QUIRK_INAND_CMD38),
2509 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2510 MMC_QUIRK_INAND_CMD38),
2511 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2512 MMC_QUIRK_INAND_CMD38),
2513 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2514 MMC_QUIRK_INAND_CMD38),
2517 * Some MMC cards experience performance degradation with CMD23
2518 * instead of CMD12-bounded multiblock transfers. For now we'll
2519 * black list what's bad...
2520 * - Certain Toshiba cards.
2522 * N.B. This doesn't affect SD cards.
2524 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2525 MMC_QUIRK_BLK_NO_CMD23),
2526 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2527 MMC_QUIRK_BLK_NO_CMD23),
2528 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2529 MMC_QUIRK_BLK_NO_CMD23),
2530 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2531 MMC_QUIRK_BLK_NO_CMD23),
2532 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2533 MMC_QUIRK_BLK_NO_CMD23),
2536 * Some MMC cards need longer data read timeout than indicated in CSD.
2538 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
2539 MMC_QUIRK_LONG_READ_TIME),
2540 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2541 MMC_QUIRK_LONG_READ_TIME),
2544 * On these Samsung MoviNAND parts, performing secure erase or
2545 * secure trim can result in unrecoverable corruption due to a
2548 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2549 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2550 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2551 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2552 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2553 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2554 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2555 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2556 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2557 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2558 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2559 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2560 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2561 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2562 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2563 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2566 * On Some Kingston eMMCs, performing trim can result in
2567 * unrecoverable data conrruption occasionally due to a firmware bug.
2569 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2570 MMC_QUIRK_TRIM_BROKEN),
2571 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2572 MMC_QUIRK_TRIM_BROKEN),
2577 static int mmc_blk_probe(struct mmc_card *card)
2579 struct mmc_blk_data *md, *part_md;
2583 * Check that the card supports the command class(es) we need.
2585 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2588 mmc_fixup_device(card, blk_fixups);
2590 md = mmc_blk_alloc(card);
2594 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
2595 cap_str, sizeof(cap_str));
2596 pr_info("%s: %s %s %s %s\n",
2597 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2598 cap_str, md->read_only ? "(ro)" : "");
2600 if (mmc_blk_alloc_parts(card, md))
2603 dev_set_drvdata(&card->dev, md);
2605 if (mmc_add_disk(md))
2608 list_for_each_entry(part_md, &md->part, part) {
2609 if (mmc_add_disk(part_md))
2613 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2614 pm_runtime_use_autosuspend(&card->dev);
2617 * Don't enable runtime PM for SD-combo cards here. Leave that
2618 * decision to be taken during the SDIO init sequence instead.
2620 if (card->type != MMC_TYPE_SD_COMBO) {
2621 pm_runtime_set_active(&card->dev);
2622 pm_runtime_enable(&card->dev);
2628 mmc_blk_remove_parts(card, md);
2629 mmc_blk_remove_req(md);
2633 static void mmc_blk_remove(struct mmc_card *card)
2635 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2637 mmc_blk_remove_parts(card, md);
2638 pm_runtime_get_sync(&card->dev);
2639 mmc_claim_host(card->host);
2640 mmc_blk_part_switch(card, md);
2641 mmc_release_host(card->host);
2642 if (card->type != MMC_TYPE_SD_COMBO)
2643 pm_runtime_disable(&card->dev);
2644 pm_runtime_put_noidle(&card->dev);
2645 mmc_blk_remove_req(md);
2646 dev_set_drvdata(&card->dev, NULL);
2649 static int _mmc_blk_suspend(struct mmc_card *card)
2651 struct mmc_blk_data *part_md;
2652 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2655 mmc_queue_suspend(&md->queue);
2656 list_for_each_entry(part_md, &md->part, part) {
2657 mmc_queue_suspend(&part_md->queue);
2663 static void mmc_blk_shutdown(struct mmc_card *card)
2665 _mmc_blk_suspend(card);
2668 #ifdef CONFIG_PM_SLEEP
2669 static int mmc_blk_suspend(struct device *dev)
2671 struct mmc_card *card = mmc_dev_to_card(dev);
2673 return _mmc_blk_suspend(card);
2676 static int mmc_blk_resume(struct device *dev)
2678 struct mmc_blk_data *part_md;
2679 struct mmc_blk_data *md = dev_get_drvdata(dev);
2683 * Resume involves the card going into idle state,
2684 * so current partition is always the main one.
2686 md->part_curr = md->part_type;
2687 mmc_queue_resume(&md->queue);
2688 list_for_each_entry(part_md, &md->part, part) {
2689 mmc_queue_resume(&part_md->queue);
2696 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2698 static struct mmc_driver mmc_driver = {
2701 .pm = &mmc_blk_pm_ops,
2703 .probe = mmc_blk_probe,
2704 .remove = mmc_blk_remove,
2705 .shutdown = mmc_blk_shutdown,
2708 static int __init mmc_blk_init(void)
2712 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2713 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2715 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
2717 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2721 res = mmc_register_driver(&mmc_driver);
2727 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2732 static void __exit mmc_blk_exit(void)
2734 mmc_unregister_driver(&mmc_driver);
2735 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2738 module_init(mmc_blk_init);
2739 module_exit(mmc_blk_exit);
2741 MODULE_LICENSE("GPL");
2742 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");