2 * Block driver for media (i.e., flash cards)
4 * Copyright 2002 Hewlett-Packard Company
5 * Copyright 2005-2008 Pierre Ossman
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
17 * Author: Andrew Christian
20 #include <linux/moduleparam.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
24 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/errno.h>
28 #include <linux/hdreg.h>
29 #include <linux/kdev_t.h>
30 #include <linux/blkdev.h>
31 #include <linux/mutex.h>
32 #include <linux/scatterlist.h>
33 #include <linux/string_helpers.h>
34 #include <linux/delay.h>
35 #include <linux/capability.h>
36 #include <linux/compat.h>
37 #include <linux/pm_runtime.h>
39 #include <trace/events/mmc.h>
41 #include <linux/mmc/ioctl.h>
42 #include <linux/mmc/card.h>
43 #include <linux/mmc/host.h>
44 #include <linux/mmc/mmc.h>
45 #include <linux/mmc/sd.h>
47 #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 * currently limited by the size of the static bitmaps below.
85 static int max_devices;
87 #define MAX_DEVICES 256
89 /* TODO: Replace these with struct ida */
90 static DECLARE_BITMAP(dev_use, MAX_DEVICES);
91 static DECLARE_BITMAP(name_use, MAX_DEVICES);
94 * There is one mmc_blk_data per slot.
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 name_idx;
111 unsigned int reset_done;
112 #define MMC_BLK_READ BIT(0)
113 #define MMC_BLK_WRITE BIT(1)
114 #define MMC_BLK_DISCARD BIT(2)
115 #define MMC_BLK_SECDISCARD BIT(3)
118 * Only set in main mmc_blk_data associated
119 * with mmc_card with dev_set_drvdata, and keeps
120 * track of the current selected device partition.
122 unsigned int part_curr;
123 struct device_attribute force_ro;
124 struct device_attribute power_ro_lock;
128 static DEFINE_MUTEX(open_lock);
131 MMC_PACKED_NR_IDX = -1,
133 MMC_PACKED_NR_SINGLE,
136 module_param(perdev_minors, int, 0444);
137 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
139 static inline int mmc_blk_part_switch(struct mmc_card *card,
140 struct mmc_blk_data *md);
141 static int get_card_status(struct mmc_card *card, u32 *status, int retries);
143 static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
145 struct mmc_packed *packed = mqrq->packed;
149 mqrq->cmd_type = MMC_PACKED_NONE;
150 packed->nr_entries = MMC_PACKED_NR_ZERO;
151 packed->idx_failure = MMC_PACKED_NR_IDX;
156 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
158 struct mmc_blk_data *md;
160 mutex_lock(&open_lock);
161 md = disk->private_data;
162 if (md && md->usage == 0)
166 mutex_unlock(&open_lock);
171 static inline int mmc_get_devidx(struct gendisk *disk)
173 int devidx = disk->first_minor / perdev_minors;
177 static void mmc_blk_put(struct mmc_blk_data *md)
179 mutex_lock(&open_lock);
181 if (md->usage == 0) {
182 int devidx = mmc_get_devidx(md->disk);
183 blk_cleanup_queue(md->queue.queue);
185 __clear_bit(devidx, dev_use);
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 = kzalloc(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)
362 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
368 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
369 idata->ic.data_ptr, idata->buf_bytes)) {
384 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
385 struct mmc_blk_ioc_data *idata)
387 struct mmc_ioc_cmd *ic = &idata->ic;
389 if (copy_to_user(&(ic_ptr->response), ic->response,
390 sizeof(ic->response)))
393 if (!idata->ic.write_flag) {
394 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
395 idata->buf, idata->buf_bytes))
402 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
408 if (!status || !retries_max)
412 err = get_card_status(card, status, 5);
416 if (!R1_STATUS(*status) &&
417 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
418 break; /* RPMB programming operation complete */
421 * Rechedule to give the MMC device a chance to continue
422 * processing the previous command without being polled too
425 usleep_range(1000, 5000);
426 } while (++retry_count < retries_max);
428 if (retry_count == retries_max)
434 static int ioctl_do_sanitize(struct mmc_card *card)
438 if (!mmc_can_sanitize(card)) {
439 pr_warn("%s: %s - SANITIZE is not supported\n",
440 mmc_hostname(card->host), __func__);
445 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
446 mmc_hostname(card->host), __func__);
448 trace_mmc_blk_erase_start(EXT_CSD_SANITIZE_START, 0, 0);
449 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
450 EXT_CSD_SANITIZE_START, 1,
451 MMC_SANITIZE_REQ_TIMEOUT);
452 trace_mmc_blk_erase_end(EXT_CSD_SANITIZE_START, 0, 0);
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;
596 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
598 return PTR_ERR(idata);
600 md = mmc_blk_get(bdev->bd_disk);
606 card = md->queue.card;
614 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
618 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
625 return ioc_err ? ioc_err : err;
628 static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
629 struct mmc_ioc_multi_cmd __user *user)
631 struct mmc_blk_ioc_data **idata = NULL;
632 struct mmc_ioc_cmd __user *cmds = user->cmds;
633 struct mmc_card *card;
634 struct mmc_blk_data *md;
635 int i, err = 0, ioc_err = 0;
638 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
639 sizeof(num_of_cmds)))
642 if (num_of_cmds > MMC_IOC_MAX_CMDS)
645 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
649 for (i = 0; i < num_of_cmds; i++) {
650 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
651 if (IS_ERR(idata[i])) {
652 err = PTR_ERR(idata[i]);
658 md = mmc_blk_get(bdev->bd_disk);
662 card = md->queue.card;
670 for (i = 0; i < num_of_cmds && !ioc_err; i++)
671 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
675 /* copy to user if data and response */
676 for (i = 0; i < num_of_cmds && !err; i++)
677 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
682 for (i = 0; i < num_of_cmds; i++) {
683 kfree(idata[i]->buf);
687 return ioc_err ? ioc_err : err;
690 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
691 unsigned int cmd, unsigned long arg)
693 #ifdef CONFIG_ARCH_ROCKCHIP
694 if (cmd != MMC_IOC_CMD && cmd != MMC_IOC_MULTI_CMD)
699 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
700 * whole block device, not on a partition. This prevents overspray
701 * between sibling partitions.
703 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
708 return mmc_blk_ioctl_cmd(bdev,
709 (struct mmc_ioc_cmd __user *)arg);
710 case MMC_IOC_MULTI_CMD:
711 return mmc_blk_ioctl_multi_cmd(bdev,
712 (struct mmc_ioc_multi_cmd __user *)arg);
719 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
720 unsigned int cmd, unsigned long arg)
722 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
726 static const struct block_device_operations mmc_bdops = {
727 .open = mmc_blk_open,
728 .release = mmc_blk_release,
729 .getgeo = mmc_blk_getgeo,
730 .owner = THIS_MODULE,
731 .ioctl = mmc_blk_ioctl,
733 .compat_ioctl = mmc_blk_compat_ioctl,
737 static inline int mmc_blk_part_switch(struct mmc_card *card,
738 struct mmc_blk_data *md)
741 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
743 if (main_md->part_curr == md->part_type)
746 if (mmc_card_mmc(card)) {
747 u8 part_config = card->ext_csd.part_config;
749 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
750 part_config |= md->part_type;
752 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
753 EXT_CSD_PART_CONFIG, part_config,
754 card->ext_csd.part_time);
758 card->ext_csd.part_config = part_config;
761 main_md->part_curr = md->part_type;
765 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
771 struct mmc_request mrq = {NULL};
772 struct mmc_command cmd = {0};
773 struct mmc_data data = {0};
775 struct scatterlist sg;
777 cmd.opcode = MMC_APP_CMD;
778 cmd.arg = card->rca << 16;
779 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
781 err = mmc_wait_for_cmd(card->host, &cmd, 0);
784 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
787 memset(&cmd, 0, sizeof(struct mmc_command));
789 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
791 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
795 data.flags = MMC_DATA_READ;
798 mmc_set_data_timeout(&data, card);
803 blocks = kmalloc(4, GFP_KERNEL);
807 sg_init_one(&sg, blocks, 4);
809 mmc_wait_for_req(card->host, &mrq);
811 result = ntohl(*blocks);
814 if (cmd.error || data.error)
820 static int get_card_status(struct mmc_card *card, u32 *status, int retries)
822 struct mmc_command cmd = {0};
825 cmd.opcode = MMC_SEND_STATUS;
826 if (!mmc_host_is_spi(card->host))
827 cmd.arg = card->rca << 16;
828 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
829 err = mmc_wait_for_cmd(card->host, &cmd, retries);
831 *status = cmd.resp[0];
835 static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
836 bool hw_busy_detect, struct request *req, int *gen_err)
838 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
843 err = get_card_status(card, &status, 5);
845 pr_err("%s: error %d requesting status\n",
846 req->rq_disk->disk_name, err);
850 if (status & R1_ERROR) {
851 pr_err("%s: %s: error sending status cmd, status %#x\n",
852 req->rq_disk->disk_name, __func__, status);
856 /* We may rely on the host hw to handle busy detection.*/
857 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
862 * Timeout if the device never becomes ready for data and never
863 * leaves the program state.
865 if (time_after(jiffies, timeout)) {
866 pr_err("%s: Card stuck in programming state! %s %s\n",
867 mmc_hostname(card->host),
868 req->rq_disk->disk_name, __func__);
873 * Some cards mishandle the status bits,
874 * so make sure to check both the busy
875 * indication and the card state.
877 } while (!(status & R1_READY_FOR_DATA) ||
878 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
883 static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
884 struct request *req, int *gen_err, u32 *stop_status)
886 struct mmc_host *host = card->host;
887 struct mmc_command cmd = {0};
889 bool use_r1b_resp = rq_data_dir(req) == WRITE;
892 * Normally we use R1B responses for WRITE, but in cases where the host
893 * has specified a max_busy_timeout we need to validate it. A failure
894 * means we need to prevent the host from doing hw busy detection, which
895 * is done by converting to a R1 response instead.
897 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
898 use_r1b_resp = false;
900 cmd.opcode = MMC_STOP_TRANSMISSION;
902 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
903 cmd.busy_timeout = timeout_ms;
905 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
908 err = mmc_wait_for_cmd(host, &cmd, 5);
912 *stop_status = cmd.resp[0];
914 /* No need to check card status in case of READ. */
915 if (rq_data_dir(req) == READ)
918 if (!mmc_host_is_spi(host) &&
919 (*stop_status & R1_ERROR)) {
920 pr_err("%s: %s: general error sending stop command, resp %#x\n",
921 req->rq_disk->disk_name, __func__, *stop_status);
925 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
928 #define ERR_NOMEDIUM 3
931 #define ERR_CONTINUE 0
933 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
934 bool status_valid, u32 status)
938 /* response crc error, retry the r/w cmd */
939 pr_err("%s: %s sending %s command, card status %#x\n",
940 req->rq_disk->disk_name, "response CRC error",
945 pr_err("%s: %s sending %s command, card status %#x\n",
946 req->rq_disk->disk_name, "timed out", name, status);
948 /* If the status cmd initially failed, retry the r/w cmd */
950 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
954 * If it was a r/w cmd crc error, or illegal command
955 * (eg, issued in wrong state) then retry - we should
956 * have corrected the state problem above.
958 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
959 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
963 /* Otherwise abort the command */
964 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
968 /* We don't understand the error code the driver gave us */
969 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
970 req->rq_disk->disk_name, error, status);
976 * Initial r/w and stop cmd error recovery.
977 * We don't know whether the card received the r/w cmd or not, so try to
978 * restore things back to a sane state. Essentially, we do this as follows:
979 * - Obtain card status. If the first attempt to obtain card status fails,
980 * the status word will reflect the failed status cmd, not the failed
981 * r/w cmd. If we fail to obtain card status, it suggests we can no
982 * longer communicate with the card.
983 * - Check the card state. If the card received the cmd but there was a
984 * transient problem with the response, it might still be in a data transfer
985 * mode. Try to send it a stop command. If this fails, we can't recover.
986 * - If the r/w cmd failed due to a response CRC error, it was probably
987 * transient, so retry the cmd.
988 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
989 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
990 * illegal cmd, retry.
991 * Otherwise we don't understand what happened, so abort.
993 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
994 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
996 bool prev_cmd_status_valid = true;
997 u32 status, stop_status = 0;
1000 if (mmc_card_removed(card))
1001 return ERR_NOMEDIUM;
1004 * Try to get card status which indicates both the card state
1005 * and why there was no response. If the first attempt fails,
1006 * we can't be sure the returned status is for the r/w command.
1008 for (retry = 2; retry >= 0; retry--) {
1009 err = get_card_status(card, &status, 0);
1013 /* Re-tune if needed */
1014 mmc_retune_recheck(card->host);
1016 prev_cmd_status_valid = false;
1017 pr_err("%s: error %d sending status command, %sing\n",
1018 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1021 /* We couldn't get a response from the card. Give up. */
1023 /* Check if the card is removed */
1024 if (mmc_detect_card_removed(card->host))
1025 return ERR_NOMEDIUM;
1029 /* Flag ECC errors */
1030 if ((status & R1_CARD_ECC_FAILED) ||
1031 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1032 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1035 /* Flag General errors */
1036 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1037 if ((status & R1_ERROR) ||
1038 (brq->stop.resp[0] & R1_ERROR)) {
1039 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1040 req->rq_disk->disk_name, __func__,
1041 brq->stop.resp[0], status);
1046 * Check the current card state. If it is in some data transfer
1047 * mode, tell it to stop (and hopefully transition back to TRAN.)
1049 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1050 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
1051 err = send_stop(card,
1052 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1053 req, gen_err, &stop_status);
1055 pr_err("%s: error %d sending stop command\n",
1056 req->rq_disk->disk_name, err);
1058 * If the stop cmd also timed out, the card is probably
1059 * not present, so abort. Other errors are bad news too.
1064 if (stop_status & R1_CARD_ECC_FAILED)
1068 /* Check for set block count errors */
1070 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1071 prev_cmd_status_valid, status);
1073 /* Check for r/w command errors */
1075 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1076 prev_cmd_status_valid, status);
1079 if (!brq->stop.error)
1080 return ERR_CONTINUE;
1082 /* Now for stop errors. These aren't fatal to the transfer. */
1083 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
1084 req->rq_disk->disk_name, brq->stop.error,
1085 brq->cmd.resp[0], status);
1088 * Subsitute in our own stop status as this will give the error
1089 * state which happened during the execution of the r/w command.
1092 brq->stop.resp[0] = stop_status;
1093 brq->stop.error = 0;
1095 return ERR_CONTINUE;
1098 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1103 if (md->reset_done & type)
1106 md->reset_done |= type;
1107 err = mmc_hw_reset(host);
1108 /* Ensure we switch back to the correct partition */
1109 if (err != -EOPNOTSUPP) {
1110 struct mmc_blk_data *main_md =
1111 dev_get_drvdata(&host->card->dev);
1114 main_md->part_curr = main_md->part_type;
1115 part_err = mmc_blk_part_switch(host->card, md);
1118 * We have failed to get back into the correct
1119 * partition, so we need to abort the whole request.
1127 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1129 md->reset_done &= ~type;
1132 int mmc_access_rpmb(struct mmc_queue *mq)
1134 struct mmc_blk_data *md = mq->data;
1136 * If this is a RPMB partition access, return ture
1138 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1144 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1146 struct mmc_blk_data *md = mq->data;
1147 struct mmc_card *card = md->queue.card;
1148 unsigned int from, nr, arg;
1149 int err = 0, type = MMC_BLK_DISCARD;
1151 if (!mmc_can_erase(card)) {
1156 from = blk_rq_pos(req);
1157 nr = blk_rq_sectors(req);
1159 if (mmc_can_discard(card))
1160 arg = MMC_DISCARD_ARG;
1161 else if (mmc_can_trim(card))
1164 arg = MMC_ERASE_ARG;
1166 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1167 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1168 INAND_CMD38_ARG_EXT_CSD,
1169 arg == MMC_TRIM_ARG ?
1170 INAND_CMD38_ARG_TRIM :
1171 INAND_CMD38_ARG_ERASE,
1176 err = mmc_erase(card, from, nr, arg);
1178 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1181 mmc_blk_reset_success(md, type);
1182 blk_end_request(req, err, blk_rq_bytes(req));
1187 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1188 struct request *req)
1190 struct mmc_blk_data *md = mq->data;
1191 struct mmc_card *card = md->queue.card;
1192 unsigned int from, nr, arg;
1193 int err = 0, type = MMC_BLK_SECDISCARD;
1195 if (!(mmc_can_secure_erase_trim(card))) {
1200 from = blk_rq_pos(req);
1201 nr = blk_rq_sectors(req);
1203 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1204 arg = MMC_SECURE_TRIM1_ARG;
1206 arg = MMC_SECURE_ERASE_ARG;
1209 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1210 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1211 INAND_CMD38_ARG_EXT_CSD,
1212 arg == MMC_SECURE_TRIM1_ARG ?
1213 INAND_CMD38_ARG_SECTRIM1 :
1214 INAND_CMD38_ARG_SECERASE,
1220 err = mmc_erase(card, from, nr, arg);
1226 if (arg == MMC_SECURE_TRIM1_ARG) {
1227 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1228 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1229 INAND_CMD38_ARG_EXT_CSD,
1230 INAND_CMD38_ARG_SECTRIM2,
1236 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1244 if (err && !mmc_blk_reset(md, card->host, type))
1247 mmc_blk_reset_success(md, type);
1249 blk_end_request(req, err, blk_rq_bytes(req));
1254 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1256 struct mmc_blk_data *md = mq->data;
1257 struct mmc_card *card = md->queue.card;
1260 ret = mmc_flush_cache(card);
1264 blk_end_request_all(req, ret);
1270 * Reformat current write as a reliable write, supporting
1271 * both legacy and the enhanced reliable write MMC cards.
1272 * In each transfer we'll handle only as much as a single
1273 * reliable write can handle, thus finish the request in
1274 * partial completions.
1276 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1277 struct mmc_card *card,
1278 struct request *req)
1280 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1281 /* Legacy mode imposes restrictions on transfers. */
1282 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1283 brq->data.blocks = 1;
1285 if (brq->data.blocks > card->ext_csd.rel_sectors)
1286 brq->data.blocks = card->ext_csd.rel_sectors;
1287 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1288 brq->data.blocks = 1;
1292 #define CMD_ERRORS \
1293 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1294 R1_ADDRESS_ERROR | /* Misaligned address */ \
1295 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1296 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1297 R1_CC_ERROR | /* Card controller error */ \
1298 R1_ERROR) /* General/unknown error */
1300 static int mmc_blk_err_check(struct mmc_card *card,
1301 struct mmc_async_req *areq)
1303 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1305 struct mmc_blk_request *brq = &mq_mrq->brq;
1306 struct request *req = mq_mrq->req;
1307 int need_retune = card->host->need_retune;
1308 int ecc_err = 0, gen_err = 0;
1311 * sbc.error indicates a problem with the set block count
1312 * command. No data will have been transferred.
1314 * cmd.error indicates a problem with the r/w command. No
1315 * data will have been transferred.
1317 * stop.error indicates a problem with the stop command. Data
1318 * may have been transferred, or may still be transferring.
1320 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1322 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
1324 return MMC_BLK_RETRY;
1326 return MMC_BLK_ABORT;
1328 return MMC_BLK_NOMEDIUM;
1335 * Check for errors relating to the execution of the
1336 * initial command - such as address errors. No data
1337 * has been transferred.
1339 if (brq->cmd.resp[0] & CMD_ERRORS) {
1340 pr_err("%s: r/w command failed, status = %#x\n",
1341 req->rq_disk->disk_name, brq->cmd.resp[0]);
1342 return MMC_BLK_ABORT;
1346 * Everything else is either success, or a data error of some
1347 * kind. If it was a write, we may have transitioned to
1348 * program mode, which we have to wait for it to complete.
1350 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1353 /* Check stop command response */
1354 if (brq->stop.resp[0] & R1_ERROR) {
1355 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1356 req->rq_disk->disk_name, __func__,
1361 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1364 return MMC_BLK_CMD_ERR;
1367 /* if general error occurs, retry the write operation. */
1369 pr_warn("%s: retrying write for general error\n",
1370 req->rq_disk->disk_name);
1371 return MMC_BLK_RETRY;
1374 if (brq->data.error) {
1375 if (need_retune && !brq->retune_retry_done) {
1376 pr_info("%s: retrying because a re-tune was needed\n",
1377 req->rq_disk->disk_name);
1378 brq->retune_retry_done = 1;
1379 return MMC_BLK_RETRY;
1381 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1382 req->rq_disk->disk_name, brq->data.error,
1383 (unsigned)blk_rq_pos(req),
1384 (unsigned)blk_rq_sectors(req),
1385 brq->cmd.resp[0], brq->stop.resp[0]);
1387 if (rq_data_dir(req) == READ) {
1389 return MMC_BLK_ECC_ERR;
1390 return MMC_BLK_DATA_ERR;
1392 return MMC_BLK_CMD_ERR;
1396 if (!brq->data.bytes_xfered)
1397 return MMC_BLK_RETRY;
1399 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1400 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1401 return MMC_BLK_PARTIAL;
1403 return MMC_BLK_SUCCESS;
1406 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1407 return MMC_BLK_PARTIAL;
1409 return MMC_BLK_SUCCESS;
1412 static int mmc_blk_packed_err_check(struct mmc_card *card,
1413 struct mmc_async_req *areq)
1415 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1417 struct request *req = mq_rq->req;
1418 struct mmc_packed *packed = mq_rq->packed;
1419 int err, check, status;
1425 check = mmc_blk_err_check(card, areq);
1426 err = get_card_status(card, &status, 0);
1428 pr_err("%s: error %d sending status command\n",
1429 req->rq_disk->disk_name, err);
1430 return MMC_BLK_ABORT;
1433 if (status & R1_EXCEPTION_EVENT) {
1434 err = mmc_get_ext_csd(card, &ext_csd);
1436 pr_err("%s: error %d sending ext_csd\n",
1437 req->rq_disk->disk_name, err);
1438 return MMC_BLK_ABORT;
1441 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1442 EXT_CSD_PACKED_FAILURE) &&
1443 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1444 EXT_CSD_PACKED_GENERIC_ERROR)) {
1445 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1446 EXT_CSD_PACKED_INDEXED_ERROR) {
1447 packed->idx_failure =
1448 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1449 check = MMC_BLK_PARTIAL;
1451 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1452 "failure index: %d\n",
1453 req->rq_disk->disk_name, packed->nr_entries,
1454 packed->blocks, packed->idx_failure);
1462 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1463 struct mmc_card *card,
1465 struct mmc_queue *mq)
1467 u32 readcmd, writecmd;
1468 struct mmc_blk_request *brq = &mqrq->brq;
1469 struct request *req = mqrq->req;
1470 struct mmc_blk_data *md = mq->data;
1474 * Reliable writes are used to implement Forced Unit Access and
1475 * are supported only on MMCs.
1477 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1478 (rq_data_dir(req) == WRITE) &&
1479 (md->flags & MMC_BLK_REL_WR);
1481 memset(brq, 0, sizeof(struct mmc_blk_request));
1482 brq->mrq.cmd = &brq->cmd;
1483 brq->mrq.data = &brq->data;
1485 brq->cmd.arg = blk_rq_pos(req);
1486 if (!mmc_card_blockaddr(card))
1488 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1489 brq->data.blksz = 512;
1490 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1492 brq->data.blocks = blk_rq_sectors(req);
1495 * The block layer doesn't support all sector count
1496 * restrictions, so we need to be prepared for too big
1499 if (brq->data.blocks > card->host->max_blk_count)
1500 brq->data.blocks = card->host->max_blk_count;
1502 if (brq->data.blocks > 1) {
1504 * After a read error, we redo the request one sector
1505 * at a time in order to accurately determine which
1506 * sectors can be read successfully.
1509 brq->data.blocks = 1;
1512 * Some controllers have HW issues while operating
1513 * in multiple I/O mode
1515 if (card->host->ops->multi_io_quirk)
1516 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1517 (rq_data_dir(req) == READ) ?
1518 MMC_DATA_READ : MMC_DATA_WRITE,
1522 if (brq->data.blocks > 1 || do_rel_wr) {
1523 /* SPI multiblock writes terminate using a special
1524 * token, not a STOP_TRANSMISSION request.
1526 if (!mmc_host_is_spi(card->host) ||
1527 rq_data_dir(req) == READ)
1528 brq->mrq.stop = &brq->stop;
1529 readcmd = MMC_READ_MULTIPLE_BLOCK;
1530 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1532 brq->mrq.stop = NULL;
1533 readcmd = MMC_READ_SINGLE_BLOCK;
1534 writecmd = MMC_WRITE_BLOCK;
1536 if (rq_data_dir(req) == READ) {
1537 brq->cmd.opcode = readcmd;
1538 brq->data.flags |= MMC_DATA_READ;
1540 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1543 brq->cmd.opcode = writecmd;
1544 brq->data.flags |= MMC_DATA_WRITE;
1546 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1551 mmc_apply_rel_rw(brq, card, req);
1554 * Data tag is used only during writing meta data to speed
1555 * up write and any subsequent read of this meta data
1557 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1558 (req->cmd_flags & REQ_META) &&
1559 (rq_data_dir(req) == WRITE) &&
1560 ((brq->data.blocks * brq->data.blksz) >=
1561 card->ext_csd.data_tag_unit_size);
1564 * Pre-defined multi-block transfers are preferable to
1565 * open ended-ones (and necessary for reliable writes).
1566 * However, it is not sufficient to just send CMD23,
1567 * and avoid the final CMD12, as on an error condition
1568 * CMD12 (stop) needs to be sent anyway. This, coupled
1569 * with Auto-CMD23 enhancements provided by some
1570 * hosts, means that the complexity of dealing
1571 * with this is best left to the host. If CMD23 is
1572 * supported by card and host, we'll fill sbc in and let
1573 * the host deal with handling it correctly. This means
1574 * that for hosts that don't expose MMC_CAP_CMD23, no
1575 * change of behavior will be observed.
1577 * N.B: Some MMC cards experience perf degradation.
1578 * We'll avoid using CMD23-bounded multiblock writes for
1579 * these, while retaining features like reliable writes.
1581 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1582 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1584 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1585 brq->sbc.arg = brq->data.blocks |
1586 (do_rel_wr ? (1 << 31) : 0) |
1587 (do_data_tag ? (1 << 29) : 0);
1588 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1589 brq->mrq.sbc = &brq->sbc;
1592 mmc_set_data_timeout(&brq->data, card);
1594 brq->data.sg = mqrq->sg;
1595 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1598 * Adjust the sg list so it is the same size as the
1601 if (brq->data.blocks != blk_rq_sectors(req)) {
1602 int i, data_size = brq->data.blocks << 9;
1603 struct scatterlist *sg;
1605 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1606 data_size -= sg->length;
1607 if (data_size <= 0) {
1608 sg->length += data_size;
1613 brq->data.sg_len = i;
1616 mqrq->mmc_active.mrq = &brq->mrq;
1617 mqrq->mmc_active.err_check = mmc_blk_err_check;
1619 mmc_queue_bounce_pre(mqrq);
1622 static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1623 struct mmc_card *card)
1625 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1626 unsigned int max_seg_sz = queue_max_segment_size(q);
1627 unsigned int len, nr_segs = 0;
1630 len = min(hdr_sz, max_seg_sz);
1638 static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1640 struct request_queue *q = mq->queue;
1641 struct mmc_card *card = mq->card;
1642 struct request *cur = req, *next = NULL;
1643 struct mmc_blk_data *md = mq->data;
1644 struct mmc_queue_req *mqrq = mq->mqrq_cur;
1645 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1646 unsigned int req_sectors = 0, phys_segments = 0;
1647 unsigned int max_blk_count, max_phys_segs;
1648 bool put_back = true;
1649 u8 max_packed_rw = 0;
1652 if (!(md->flags & MMC_BLK_PACKED_CMD))
1655 if ((rq_data_dir(cur) == WRITE) &&
1656 mmc_host_packed_wr(card->host))
1657 max_packed_rw = card->ext_csd.max_packed_writes;
1659 if (max_packed_rw == 0)
1662 if (mmc_req_rel_wr(cur) &&
1663 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1666 if (mmc_large_sector(card) &&
1667 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1670 mmc_blk_clear_packed(mqrq);
1672 max_blk_count = min(card->host->max_blk_count,
1673 card->host->max_req_size >> 9);
1674 if (unlikely(max_blk_count > 0xffff))
1675 max_blk_count = 0xffff;
1677 max_phys_segs = queue_max_segments(q);
1678 req_sectors += blk_rq_sectors(cur);
1679 phys_segments += cur->nr_phys_segments;
1681 if (rq_data_dir(cur) == WRITE) {
1682 req_sectors += mmc_large_sector(card) ? 8 : 1;
1683 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1687 if (reqs >= max_packed_rw - 1) {
1692 spin_lock_irq(q->queue_lock);
1693 next = blk_fetch_request(q);
1694 spin_unlock_irq(q->queue_lock);
1700 if (mmc_large_sector(card) &&
1701 !IS_ALIGNED(blk_rq_sectors(next), 8))
1704 if (next->cmd_flags & REQ_DISCARD ||
1705 next->cmd_flags & REQ_FLUSH)
1708 if (rq_data_dir(cur) != rq_data_dir(next))
1711 if (mmc_req_rel_wr(next) &&
1712 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1715 req_sectors += blk_rq_sectors(next);
1716 if (req_sectors > max_blk_count)
1719 phys_segments += next->nr_phys_segments;
1720 if (phys_segments > max_phys_segs)
1723 list_add_tail(&next->queuelist, &mqrq->packed->list);
1729 spin_lock_irq(q->queue_lock);
1730 blk_requeue_request(q, next);
1731 spin_unlock_irq(q->queue_lock);
1735 list_add(&req->queuelist, &mqrq->packed->list);
1736 mqrq->packed->nr_entries = ++reqs;
1737 mqrq->packed->retries = reqs;
1742 mqrq->cmd_type = MMC_PACKED_NONE;
1746 static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1747 struct mmc_card *card,
1748 struct mmc_queue *mq)
1750 struct mmc_blk_request *brq = &mqrq->brq;
1751 struct request *req = mqrq->req;
1752 struct request *prq;
1753 struct mmc_blk_data *md = mq->data;
1754 struct mmc_packed *packed = mqrq->packed;
1755 bool do_rel_wr, do_data_tag;
1756 u32 *packed_cmd_hdr;
1762 mqrq->cmd_type = MMC_PACKED_WRITE;
1764 packed->idx_failure = MMC_PACKED_NR_IDX;
1766 packed_cmd_hdr = packed->cmd_hdr;
1767 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
1768 packed_cmd_hdr[0] = (packed->nr_entries << 16) |
1769 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1770 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1773 * Argument for each entry of packed group
1775 list_for_each_entry(prq, &packed->list, queuelist) {
1776 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1777 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1778 (prq->cmd_flags & REQ_META) &&
1779 (rq_data_dir(prq) == WRITE) &&
1780 ((brq->data.blocks * brq->data.blksz) >=
1781 card->ext_csd.data_tag_unit_size);
1782 /* Argument of CMD23 */
1783 packed_cmd_hdr[(i * 2)] =
1784 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1785 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1786 blk_rq_sectors(prq);
1787 /* Argument of CMD18 or CMD25 */
1788 packed_cmd_hdr[((i * 2)) + 1] =
1789 mmc_card_blockaddr(card) ?
1790 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1791 packed->blocks += blk_rq_sectors(prq);
1795 memset(brq, 0, sizeof(struct mmc_blk_request));
1796 brq->mrq.cmd = &brq->cmd;
1797 brq->mrq.data = &brq->data;
1798 brq->mrq.sbc = &brq->sbc;
1799 brq->mrq.stop = &brq->stop;
1801 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1802 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1803 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1805 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1806 brq->cmd.arg = blk_rq_pos(req);
1807 if (!mmc_card_blockaddr(card))
1809 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1811 brq->data.blksz = 512;
1812 brq->data.blocks = packed->blocks + hdr_blocks;
1813 brq->data.flags |= MMC_DATA_WRITE;
1815 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1817 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1819 mmc_set_data_timeout(&brq->data, card);
1821 brq->data.sg = mqrq->sg;
1822 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1824 mqrq->mmc_active.mrq = &brq->mrq;
1825 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1827 mmc_queue_bounce_pre(mqrq);
1830 static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1831 struct mmc_blk_request *brq, struct request *req,
1834 struct mmc_queue_req *mq_rq;
1835 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1838 * If this is an SD card and we're writing, we can first
1839 * mark the known good sectors as ok.
1841 * If the card is not SD, we can still ok written sectors
1842 * as reported by the controller (which might be less than
1843 * the real number of written sectors, but never more).
1845 if (mmc_card_sd(card)) {
1848 blocks = mmc_sd_num_wr_blocks(card);
1849 if (blocks != (u32)-1) {
1850 ret = blk_end_request(req, 0, blocks << 9);
1853 if (!mmc_packed_cmd(mq_rq->cmd_type))
1854 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1859 static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1861 struct request *prq;
1862 struct mmc_packed *packed = mq_rq->packed;
1863 int idx = packed->idx_failure, i = 0;
1868 while (!list_empty(&packed->list)) {
1869 prq = list_entry_rq(packed->list.next);
1871 /* retry from error index */
1872 packed->nr_entries -= idx;
1876 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1877 list_del_init(&prq->queuelist);
1878 mmc_blk_clear_packed(mq_rq);
1882 list_del_init(&prq->queuelist);
1883 blk_end_request(prq, 0, blk_rq_bytes(prq));
1887 mmc_blk_clear_packed(mq_rq);
1891 static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1893 struct request *prq;
1894 struct mmc_packed *packed = mq_rq->packed;
1898 while (!list_empty(&packed->list)) {
1899 prq = list_entry_rq(packed->list.next);
1900 list_del_init(&prq->queuelist);
1901 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1904 mmc_blk_clear_packed(mq_rq);
1907 static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1908 struct mmc_queue_req *mq_rq)
1910 struct request *prq;
1911 struct request_queue *q = mq->queue;
1912 struct mmc_packed *packed = mq_rq->packed;
1916 while (!list_empty(&packed->list)) {
1917 prq = list_entry_rq(packed->list.prev);
1918 if (prq->queuelist.prev != &packed->list) {
1919 list_del_init(&prq->queuelist);
1920 spin_lock_irq(q->queue_lock);
1921 blk_requeue_request(mq->queue, prq);
1922 spin_unlock_irq(q->queue_lock);
1924 list_del_init(&prq->queuelist);
1928 mmc_blk_clear_packed(mq_rq);
1931 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
1933 struct mmc_blk_data *md = mq->data;
1934 struct mmc_card *card = md->queue.card;
1935 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
1936 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
1937 enum mmc_blk_status status;
1938 struct mmc_queue_req *mq_rq;
1939 struct request *req = rqc;
1940 struct mmc_async_req *areq;
1941 const u8 packed_nr = 2;
1944 if (!rqc && !mq->mqrq_prev->req)
1948 reqs = mmc_blk_prep_packed_list(mq, rqc);
1953 * When 4KB native sector is enabled, only 8 blocks
1954 * multiple read or write is allowed
1956 if ((brq->data.blocks & 0x07) &&
1957 (card->ext_csd.data_sector_size == 4096)) {
1958 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1959 req->rq_disk->disk_name);
1960 mq_rq = mq->mqrq_cur;
1964 if (reqs >= packed_nr)
1965 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1968 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1969 areq = &mq->mqrq_cur->mmc_active;
1972 areq = mmc_start_req(card->host, areq, (int *) &status);
1974 if (status == MMC_BLK_NEW_REQUEST)
1975 mq->flags |= MMC_QUEUE_NEW_REQUEST;
1979 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1982 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1983 mmc_queue_bounce_post(mq_rq);
1986 case MMC_BLK_SUCCESS:
1987 case MMC_BLK_PARTIAL:
1989 * A block was successfully transferred.
1991 mmc_blk_reset_success(md, type);
1993 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1994 ret = mmc_blk_end_packed_req(mq_rq);
1997 ret = blk_end_request(req, 0,
1998 brq->data.bytes_xfered);
2002 * If the blk_end_request function returns non-zero even
2003 * though all data has been transferred and no errors
2004 * were returned by the host controller, it's a bug.
2006 if (status == MMC_BLK_SUCCESS && ret) {
2007 pr_err("%s BUG rq_tot %d d_xfer %d\n",
2008 __func__, blk_rq_bytes(req),
2009 brq->data.bytes_xfered);
2014 case MMC_BLK_CMD_ERR:
2015 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
2016 if (mmc_blk_reset(md, card->host, type))
2022 retune_retry_done = brq->retune_retry_done;
2027 if (!mmc_blk_reset(md, card->host, type))
2030 case MMC_BLK_DATA_ERR: {
2033 err = mmc_blk_reset(md, card->host, type);
2036 if (err == -ENODEV ||
2037 mmc_packed_cmd(mq_rq->cmd_type))
2041 case MMC_BLK_ECC_ERR:
2042 if (brq->data.blocks > 1) {
2043 /* Redo read one sector at a time */
2044 pr_warn("%s: retrying using single block read\n",
2045 req->rq_disk->disk_name);
2050 * After an error, we redo I/O one sector at a
2051 * time, so we only reach here after trying to
2052 * read a single sector.
2054 ret = blk_end_request(req, -EIO,
2059 case MMC_BLK_NOMEDIUM:
2062 pr_err("%s: Unhandled return value (%d)",
2063 req->rq_disk->disk_name, status);
2068 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2069 if (!mq_rq->packed->retries)
2071 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2072 mmc_start_req(card->host,
2073 &mq_rq->mmc_active, NULL);
2077 * In case of a incomplete request
2078 * prepare it again and resend.
2080 mmc_blk_rw_rq_prep(mq_rq, card,
2082 mmc_start_req(card->host,
2083 &mq_rq->mmc_active, NULL);
2085 mq_rq->brq.retune_retry_done = retune_retry_done;
2092 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2093 mmc_blk_abort_packed_req(mq_rq);
2095 if (mmc_card_removed(card))
2096 req->cmd_flags |= REQ_QUIET;
2098 ret = blk_end_request(req, -EIO,
2099 blk_rq_cur_bytes(req));
2104 if (mmc_card_removed(card)) {
2105 rqc->cmd_flags |= REQ_QUIET;
2106 blk_end_request_all(rqc, -EIO);
2109 * If current request is packed, it needs to put back.
2111 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2112 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2114 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2115 mmc_start_req(card->host,
2116 &mq->mqrq_cur->mmc_active, NULL);
2123 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2126 struct mmc_blk_data *md = mq->data;
2127 struct mmc_card *card = md->queue.card;
2128 struct mmc_host *host = card->host;
2129 unsigned long flags;
2130 unsigned int cmd_flags = req ? req->cmd_flags : 0;
2132 if (req && !mq->mqrq_prev->req)
2133 /* claim host only for the first request */
2136 ret = mmc_blk_part_switch(card, md);
2139 blk_end_request_all(req, -EIO);
2145 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
2146 if (cmd_flags & REQ_DISCARD) {
2147 /* complete ongoing async transfer before issuing discard */
2148 if (card->host->areq)
2149 mmc_blk_issue_rw_rq(mq, NULL);
2150 if (req->cmd_flags & REQ_SECURE)
2151 ret = mmc_blk_issue_secdiscard_rq(mq, req);
2153 ret = mmc_blk_issue_discard_rq(mq, req);
2154 } else if (cmd_flags & REQ_FLUSH) {
2155 /* complete ongoing async transfer before issuing flush */
2156 if (card->host->areq)
2157 mmc_blk_issue_rw_rq(mq, NULL);
2158 ret = mmc_blk_issue_flush(mq, req);
2160 if (!req && host->areq) {
2161 spin_lock_irqsave(&host->context_info.lock, flags);
2162 host->context_info.is_waiting_last_req = true;
2163 spin_unlock_irqrestore(&host->context_info.lock, flags);
2165 ret = mmc_blk_issue_rw_rq(mq, req);
2169 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
2170 (cmd_flags & MMC_REQ_SPECIAL_MASK))
2172 * Release host when there are no more requests
2173 * and after special request(discard, flush) is done.
2174 * In case sepecial request, there is no reentry to
2175 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2181 static inline int mmc_blk_readonly(struct mmc_card *card)
2183 return mmc_card_readonly(card) ||
2184 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2187 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2188 struct device *parent,
2191 const char *subname,
2194 struct mmc_blk_data *md;
2197 devidx = find_first_zero_bit(dev_use, max_devices);
2198 if (devidx >= max_devices)
2199 return ERR_PTR(-ENOSPC);
2200 __set_bit(devidx, dev_use);
2202 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2209 * !subname implies we are creating main mmc_blk_data that will be
2210 * associated with mmc_card with dev_set_drvdata. Due to device
2211 * partitions, devidx will not coincide with a per-physical card
2212 * index anymore so we keep track of a name index.
2215 md->name_idx = find_first_zero_bit(name_use, max_devices);
2216 __set_bit(md->name_idx, name_use);
2218 md->name_idx = ((struct mmc_blk_data *)
2219 dev_to_disk(parent)->private_data)->name_idx;
2221 md->area_type = area_type;
2224 * Set the read-only status based on the supported commands
2225 * and the write protect switch.
2227 md->read_only = mmc_blk_readonly(card);
2229 md->disk = alloc_disk(perdev_minors);
2230 if (md->disk == NULL) {
2235 spin_lock_init(&md->lock);
2236 INIT_LIST_HEAD(&md->part);
2239 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2243 md->queue.issue_fn = mmc_blk_issue_rq;
2244 md->queue.data = md;
2246 md->disk->major = MMC_BLOCK_MAJOR;
2247 md->disk->first_minor = devidx * perdev_minors;
2248 md->disk->fops = &mmc_bdops;
2249 md->disk->private_data = md;
2250 md->disk->queue = md->queue.queue;
2251 md->disk->driverfs_dev = parent;
2252 set_disk_ro(md->disk, md->read_only || default_ro);
2253 md->disk->flags = GENHD_FL_EXT_DEVT;
2254 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2255 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
2258 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2260 * - be set for removable media with permanent block devices
2261 * - be unset for removable block devices with permanent media
2263 * Since MMC block devices clearly fall under the second
2264 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2265 * should use the block device creation/destruction hotplug
2266 * messages to tell when the card is present.
2269 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2270 "mmcblk%u%s", md->name_idx, subname ? subname : "");
2272 if (mmc_card_mmc(card))
2273 blk_queue_logical_block_size(md->queue.queue,
2274 card->ext_csd.data_sector_size);
2276 blk_queue_logical_block_size(md->queue.queue, 512);
2278 set_capacity(md->disk, size);
2280 if (mmc_host_cmd23(card->host)) {
2281 if (mmc_card_mmc(card) ||
2282 (mmc_card_sd(card) &&
2283 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2284 md->flags |= MMC_BLK_CMD23;
2287 if (mmc_card_mmc(card) &&
2288 md->flags & MMC_BLK_CMD23 &&
2289 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2290 card->ext_csd.rel_sectors)) {
2291 md->flags |= MMC_BLK_REL_WR;
2292 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2295 if (mmc_card_mmc(card) &&
2296 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2297 (md->flags & MMC_BLK_CMD23) &&
2298 card->ext_csd.packed_event_en) {
2299 if (!mmc_packed_init(&md->queue, card))
2300 md->flags |= MMC_BLK_PACKED_CMD;
2310 return ERR_PTR(ret);
2313 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2317 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2319 * The EXT_CSD sector count is in number or 512 byte
2322 size = card->ext_csd.sectors;
2325 * The CSD capacity field is in units of read_blkbits.
2326 * set_capacity takes units of 512 bytes.
2328 size = (typeof(sector_t))card->csd.capacity
2329 << (card->csd.read_blkbits - 9);
2332 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2333 MMC_BLK_DATA_AREA_MAIN);
2336 static int mmc_blk_alloc_part(struct mmc_card *card,
2337 struct mmc_blk_data *md,
2338 unsigned int part_type,
2341 const char *subname,
2345 struct mmc_blk_data *part_md;
2347 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2348 subname, area_type);
2349 if (IS_ERR(part_md))
2350 return PTR_ERR(part_md);
2351 part_md->part_type = part_type;
2352 list_add(&part_md->part, &md->part);
2354 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
2355 cap_str, sizeof(cap_str));
2356 pr_info("%s: %s %s partition %u %s\n",
2357 part_md->disk->disk_name, mmc_card_id(card),
2358 mmc_card_name(card), part_md->part_type, cap_str);
2362 /* MMC Physical partitions consist of two boot partitions and
2363 * up to four general purpose partitions.
2364 * For each partition enabled in EXT_CSD a block device will be allocatedi
2365 * to provide access to the partition.
2368 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2372 if (!mmc_card_mmc(card))
2375 for (idx = 0; idx < card->nr_parts; idx++) {
2376 if (card->part[idx].size) {
2377 ret = mmc_blk_alloc_part(card, md,
2378 card->part[idx].part_cfg,
2379 card->part[idx].size >> 9,
2380 card->part[idx].force_ro,
2381 card->part[idx].name,
2382 card->part[idx].area_type);
2391 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2393 struct mmc_card *card;
2397 * Flush remaining requests and free queues. It
2398 * is freeing the queue that stops new requests
2399 * from being accepted.
2401 card = md->queue.card;
2402 mmc_cleanup_queue(&md->queue);
2403 if (md->flags & MMC_BLK_PACKED_CMD)
2404 mmc_packed_clean(&md->queue);
2405 if (md->disk->flags & GENHD_FL_UP) {
2406 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2407 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2408 card->ext_csd.boot_ro_lockable)
2409 device_remove_file(disk_to_dev(md->disk),
2410 &md->power_ro_lock);
2412 del_gendisk(md->disk);
2418 static void mmc_blk_remove_parts(struct mmc_card *card,
2419 struct mmc_blk_data *md)
2421 struct list_head *pos, *q;
2422 struct mmc_blk_data *part_md;
2424 __clear_bit(md->name_idx, name_use);
2425 list_for_each_safe(pos, q, &md->part) {
2426 part_md = list_entry(pos, struct mmc_blk_data, part);
2428 mmc_blk_remove_req(part_md);
2432 static int mmc_add_disk(struct mmc_blk_data *md)
2435 struct mmc_card *card = md->queue.card;
2438 md->force_ro.show = force_ro_show;
2439 md->force_ro.store = force_ro_store;
2440 sysfs_attr_init(&md->force_ro.attr);
2441 md->force_ro.attr.name = "force_ro";
2442 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2443 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2447 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2448 card->ext_csd.boot_ro_lockable) {
2451 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2454 mode = S_IRUGO | S_IWUSR;
2456 md->power_ro_lock.show = power_ro_lock_show;
2457 md->power_ro_lock.store = power_ro_lock_store;
2458 sysfs_attr_init(&md->power_ro_lock.attr);
2459 md->power_ro_lock.attr.mode = mode;
2460 md->power_ro_lock.attr.name =
2461 "ro_lock_until_next_power_on";
2462 ret = device_create_file(disk_to_dev(md->disk),
2463 &md->power_ro_lock);
2465 goto power_ro_lock_fail;
2470 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2472 del_gendisk(md->disk);
2477 #define CID_MANFID_SANDISK 0x2
2478 #define CID_MANFID_TOSHIBA 0x11
2479 #define CID_MANFID_MICRON 0x13
2480 #define CID_MANFID_SAMSUNG 0x15
2481 #define CID_MANFID_KINGSTON 0x70
2483 static const struct mmc_fixup blk_fixups[] =
2485 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2486 MMC_QUIRK_INAND_CMD38),
2487 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2488 MMC_QUIRK_INAND_CMD38),
2489 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2490 MMC_QUIRK_INAND_CMD38),
2491 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2492 MMC_QUIRK_INAND_CMD38),
2493 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2494 MMC_QUIRK_INAND_CMD38),
2497 * Some MMC cards experience performance degradation with CMD23
2498 * instead of CMD12-bounded multiblock transfers. For now we'll
2499 * black list what's bad...
2500 * - Certain Toshiba cards.
2502 * N.B. This doesn't affect SD cards.
2504 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2505 MMC_QUIRK_BLK_NO_CMD23),
2506 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2507 MMC_QUIRK_BLK_NO_CMD23),
2508 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2509 MMC_QUIRK_BLK_NO_CMD23),
2510 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2511 MMC_QUIRK_BLK_NO_CMD23),
2512 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2513 MMC_QUIRK_BLK_NO_CMD23),
2516 * Some Micron MMC cards needs longer data read timeout than
2519 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
2520 MMC_QUIRK_LONG_READ_TIME),
2523 * On these Samsung MoviNAND parts, performing secure erase or
2524 * secure trim can result in unrecoverable corruption due to a
2527 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2528 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2529 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2530 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2531 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2532 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2533 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2534 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2535 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2536 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2537 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2538 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2539 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2540 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2541 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2542 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2545 * On Some Kingston eMMCs, performing trim can result in
2546 * unrecoverable data conrruption occasionally due to a firmware bug.
2548 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2549 MMC_QUIRK_TRIM_BROKEN),
2550 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2551 MMC_QUIRK_TRIM_BROKEN),
2556 extern struct mmc_card *this_card;
2557 static int mmc_blk_probe(struct mmc_card *card)
2559 struct mmc_blk_data *md, *part_md;
2563 * Check that the card supports the command class(es) we need.
2565 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2568 mmc_fixup_device(card, blk_fixups);
2570 md = mmc_blk_alloc(card);
2574 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
2575 cap_str, sizeof(cap_str));
2576 pr_info("%s: %s %s %s %s\n",
2577 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2578 cap_str, md->read_only ? "(ro)" : "");
2580 if (mmc_blk_alloc_parts(card, md))
2583 dev_set_drvdata(&card->dev, md);
2585 #if defined(CONFIG_MMC_DW_ROCKCHIP)
2586 if (card->host->restrict_caps & RESTRICT_CARD_TYPE_EMMC) {
2588 md->disk->emmc_disk = 1;
2590 md->disk->emmc_disk = 0;
2594 if (mmc_add_disk(md))
2597 list_for_each_entry(part_md, &md->part, part) {
2598 if (mmc_add_disk(part_md))
2602 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2603 pm_runtime_use_autosuspend(&card->dev);
2606 * Don't enable runtime PM for SD-combo cards here. Leave that
2607 * decision to be taken during the SDIO init sequence instead.
2609 if (card->type != MMC_TYPE_SD_COMBO) {
2610 pm_runtime_set_active(&card->dev);
2611 pm_runtime_enable(&card->dev);
2617 mmc_blk_remove_parts(card, md);
2618 mmc_blk_remove_req(md);
2622 static void mmc_blk_remove(struct mmc_card *card)
2624 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2626 #if defined(CONFIG_MMC_DW_ROCKCHIP)
2627 if (card->host->restrict_caps & RESTRICT_CARD_TYPE_EMMC)
2631 mmc_blk_remove_parts(card, md);
2632 pm_runtime_get_sync(&card->dev);
2633 mmc_claim_host(card->host);
2634 mmc_blk_part_switch(card, md);
2635 mmc_release_host(card->host);
2636 if (card->type != MMC_TYPE_SD_COMBO)
2637 pm_runtime_disable(&card->dev);
2638 pm_runtime_put_noidle(&card->dev);
2639 mmc_blk_remove_req(md);
2640 dev_set_drvdata(&card->dev, NULL);
2643 static int _mmc_blk_suspend(struct mmc_card *card)
2645 struct mmc_blk_data *part_md;
2646 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2649 mmc_queue_suspend(&md->queue);
2650 list_for_each_entry(part_md, &md->part, part) {
2651 mmc_queue_suspend(&part_md->queue);
2657 static void mmc_blk_shutdown(struct mmc_card *card)
2659 _mmc_blk_suspend(card);
2662 #ifdef CONFIG_PM_SLEEP
2663 static int mmc_blk_suspend(struct device *dev)
2665 struct mmc_card *card = mmc_dev_to_card(dev);
2667 return _mmc_blk_suspend(card);
2670 static int mmc_blk_resume(struct device *dev)
2672 struct mmc_blk_data *part_md;
2673 struct mmc_blk_data *md = dev_get_drvdata(dev);
2677 * Resume involves the card going into idle state,
2678 * so current partition is always the main one.
2680 md->part_curr = md->part_type;
2681 mmc_queue_resume(&md->queue);
2682 list_for_each_entry(part_md, &md->part, part) {
2683 mmc_queue_resume(&part_md->queue);
2690 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2692 static struct mmc_driver mmc_driver = {
2695 .pm = &mmc_blk_pm_ops,
2697 .probe = mmc_blk_probe,
2698 .remove = mmc_blk_remove,
2699 .shutdown = mmc_blk_shutdown,
2702 static int __init mmc_blk_init(void)
2706 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2707 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2709 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
2711 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2715 res = mmc_register_driver(&mmc_driver);
2721 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2726 static void __exit mmc_blk_exit(void)
2728 mmc_unregister_driver(&mmc_driver);
2729 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2732 module_init(mmc_blk_init);
2733 module_exit(mmc_blk_exit);
2735 MODULE_LICENSE("GPL");
2736 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");