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 <linux/uaccess.h>
58 MODULE_ALIAS("mmc:block");
59 #ifdef MODULE_PARAM_PREFIX
60 #undef MODULE_PARAM_PREFIX
62 #define MODULE_PARAM_PREFIX "mmcblk."
64 #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
65 #define MMC_SANITIZE_REQ_TIMEOUT 240000
66 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
68 #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
69 (rq_data_dir(req) == WRITE))
70 static DEFINE_MUTEX(block_mutex);
73 * The defaults come from config options but can be overriden by module
76 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
79 * We've only got one major, so number of mmcblk devices is
80 * limited to (1 << 20) / number of minors per device. It is also
81 * limited by the MAX_DEVICES below.
83 static int max_devices;
85 #define MAX_DEVICES 256
87 static DEFINE_IDA(mmc_blk_ida);
90 * There is one mmc_blk_data per slot.
94 struct device *parent;
96 struct mmc_queue queue;
97 struct list_head part;
100 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
101 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
104 unsigned int read_only;
105 unsigned int part_type;
106 unsigned int reset_done;
107 #define MMC_BLK_READ BIT(0)
108 #define MMC_BLK_WRITE BIT(1)
109 #define MMC_BLK_DISCARD BIT(2)
110 #define MMC_BLK_SECDISCARD BIT(3)
113 * Only set in main mmc_blk_data associated
114 * with mmc_card with dev_set_drvdata, and keeps
115 * track of the current selected device partition.
117 unsigned int part_curr;
118 struct device_attribute force_ro;
119 struct device_attribute power_ro_lock;
123 static DEFINE_MUTEX(open_lock);
125 module_param(perdev_minors, int, 0444);
126 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
128 static inline int mmc_blk_part_switch(struct mmc_card *card,
129 struct mmc_blk_data *md);
130 static int get_card_status(struct mmc_card *card, u32 *status, int retries);
132 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
134 struct mmc_blk_data *md;
136 mutex_lock(&open_lock);
137 md = disk->private_data;
138 if (md && md->usage == 0)
142 mutex_unlock(&open_lock);
147 static inline int mmc_get_devidx(struct gendisk *disk)
149 int devidx = disk->first_minor / perdev_minors;
153 static void mmc_blk_put(struct mmc_blk_data *md)
155 mutex_lock(&open_lock);
157 if (md->usage == 0) {
158 int devidx = mmc_get_devidx(md->disk);
159 blk_cleanup_queue(md->queue.queue);
160 ida_simple_remove(&mmc_blk_ida, devidx);
164 mutex_unlock(&open_lock);
167 static ssize_t power_ro_lock_show(struct device *dev,
168 struct device_attribute *attr, char *buf)
171 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
172 struct mmc_card *card = md->queue.card;
175 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
177 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
180 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
187 static ssize_t power_ro_lock_store(struct device *dev,
188 struct device_attribute *attr, const char *buf, size_t count)
191 struct mmc_blk_data *md, *part_md;
192 struct mmc_card *card;
195 if (kstrtoul(buf, 0, &set))
201 md = mmc_blk_get(dev_to_disk(dev));
202 card = md->queue.card;
206 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
207 card->ext_csd.boot_ro_lock |
208 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
209 card->ext_csd.part_time);
211 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
213 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
218 pr_info("%s: Locking boot partition ro until next power on\n",
219 md->disk->disk_name);
220 set_disk_ro(md->disk, 1);
222 list_for_each_entry(part_md, &md->part, part)
223 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
224 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
225 set_disk_ro(part_md->disk, 1);
233 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
237 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
239 ret = snprintf(buf, PAGE_SIZE, "%d\n",
240 get_disk_ro(dev_to_disk(dev)) ^
246 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
247 const char *buf, size_t count)
251 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
252 unsigned long set = simple_strtoul(buf, &end, 0);
258 set_disk_ro(dev_to_disk(dev), set || md->read_only);
265 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
267 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
270 mutex_lock(&block_mutex);
273 check_disk_change(bdev);
276 if ((mode & FMODE_WRITE) && md->read_only) {
281 mutex_unlock(&block_mutex);
286 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
288 struct mmc_blk_data *md = disk->private_data;
290 mutex_lock(&block_mutex);
292 mutex_unlock(&block_mutex);
296 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
298 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
304 struct mmc_blk_ioc_data {
305 struct mmc_ioc_cmd ic;
310 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
311 struct mmc_ioc_cmd __user *user)
313 struct mmc_blk_ioc_data *idata;
316 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
322 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
327 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
328 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
333 if (!idata->buf_bytes) {
338 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
344 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
345 idata->ic.data_ptr, idata->buf_bytes)) {
360 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
361 struct mmc_blk_ioc_data *idata)
363 struct mmc_ioc_cmd *ic = &idata->ic;
365 if (copy_to_user(&(ic_ptr->response), ic->response,
366 sizeof(ic->response)))
369 if (!idata->ic.write_flag) {
370 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
371 idata->buf, idata->buf_bytes))
378 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
384 if (!status || !retries_max)
388 err = get_card_status(card, status, 5);
392 if (!R1_STATUS(*status) &&
393 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
394 break; /* RPMB programming operation complete */
397 * Rechedule to give the MMC device a chance to continue
398 * processing the previous command without being polled too
401 usleep_range(1000, 5000);
402 } while (++retry_count < retries_max);
404 if (retry_count == retries_max)
410 static int ioctl_do_sanitize(struct mmc_card *card)
414 if (!mmc_can_sanitize(card)) {
415 pr_warn("%s: %s - SANITIZE is not supported\n",
416 mmc_hostname(card->host), __func__);
421 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
422 mmc_hostname(card->host), __func__);
424 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
425 EXT_CSD_SANITIZE_START, 1,
426 MMC_SANITIZE_REQ_TIMEOUT);
429 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
430 mmc_hostname(card->host), __func__, err);
432 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
438 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
439 struct mmc_blk_ioc_data *idata)
441 struct mmc_command cmd = {};
442 struct mmc_data data = {};
443 struct mmc_request mrq = {};
444 struct scatterlist sg;
449 if (!card || !md || !idata)
452 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
455 cmd.opcode = idata->ic.opcode;
456 cmd.arg = idata->ic.arg;
457 cmd.flags = idata->ic.flags;
459 if (idata->buf_bytes) {
462 data.blksz = idata->ic.blksz;
463 data.blocks = idata->ic.blocks;
465 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
467 if (idata->ic.write_flag)
468 data.flags = MMC_DATA_WRITE;
470 data.flags = MMC_DATA_READ;
472 /* data.flags must already be set before doing this. */
473 mmc_set_data_timeout(&data, card);
475 /* Allow overriding the timeout_ns for empirical tuning. */
476 if (idata->ic.data_timeout_ns)
477 data.timeout_ns = idata->ic.data_timeout_ns;
479 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
481 * Pretend this is a data transfer and rely on the
482 * host driver to compute timeout. When all host
483 * drivers support cmd.cmd_timeout for R1B, this
487 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
489 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
497 err = mmc_blk_part_switch(card, md);
501 if (idata->ic.is_acmd) {
502 err = mmc_app_cmd(card->host, card);
508 err = mmc_set_blockcount(card, data.blocks,
509 idata->ic.write_flag & (1 << 31));
514 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
515 (cmd.opcode == MMC_SWITCH)) {
516 err = ioctl_do_sanitize(card);
519 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
525 mmc_wait_for_req(card->host, &mrq);
528 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
529 __func__, cmd.error);
533 dev_err(mmc_dev(card->host), "%s: data error %d\n",
534 __func__, data.error);
539 * According to the SD specs, some commands require a delay after
540 * issuing the command.
542 if (idata->ic.postsleep_min_us)
543 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
545 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
549 * Ensure RPMB command has completed by polling CMD13
552 err = ioctl_rpmb_card_status_poll(card, &status, 5);
554 dev_err(mmc_dev(card->host),
555 "%s: Card Status=0x%08X, error %d\n",
556 __func__, status, err);
562 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
563 struct mmc_ioc_cmd __user *ic_ptr)
565 struct mmc_blk_ioc_data *idata;
566 struct mmc_blk_data *md;
567 struct mmc_card *card;
568 int err = 0, ioc_err = 0;
571 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
572 * whole block device, not on a partition. This prevents overspray
573 * between sibling partitions.
575 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
578 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
580 return PTR_ERR(idata);
582 md = mmc_blk_get(bdev->bd_disk);
588 card = md->queue.card;
596 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
598 /* Always switch back to main area after RPMB access */
599 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
600 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
604 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
611 return ioc_err ? ioc_err : err;
614 static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
615 struct mmc_ioc_multi_cmd __user *user)
617 struct mmc_blk_ioc_data **idata = NULL;
618 struct mmc_ioc_cmd __user *cmds = user->cmds;
619 struct mmc_card *card;
620 struct mmc_blk_data *md;
621 int i, err = 0, ioc_err = 0;
625 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
626 * whole block device, not on a partition. This prevents overspray
627 * between sibling partitions.
629 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
632 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
633 sizeof(num_of_cmds)))
636 if (num_of_cmds > MMC_IOC_MAX_CMDS)
639 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
643 for (i = 0; i < num_of_cmds; i++) {
644 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
645 if (IS_ERR(idata[i])) {
646 err = PTR_ERR(idata[i]);
652 md = mmc_blk_get(bdev->bd_disk);
658 card = md->queue.card;
666 for (i = 0; i < num_of_cmds && !ioc_err; i++)
667 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
669 /* Always switch back to main area after RPMB access */
670 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
671 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
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)
695 return mmc_blk_ioctl_cmd(bdev,
696 (struct mmc_ioc_cmd __user *)arg);
697 case MMC_IOC_MULTI_CMD:
698 return mmc_blk_ioctl_multi_cmd(bdev,
699 (struct mmc_ioc_multi_cmd __user *)arg);
706 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
707 unsigned int cmd, unsigned long arg)
709 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
713 static const struct block_device_operations mmc_bdops = {
714 .open = mmc_blk_open,
715 .release = mmc_blk_release,
716 .getgeo = mmc_blk_getgeo,
717 .owner = THIS_MODULE,
718 .ioctl = mmc_blk_ioctl,
720 .compat_ioctl = mmc_blk_compat_ioctl,
724 static inline int mmc_blk_part_switch(struct mmc_card *card,
725 struct mmc_blk_data *md)
728 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
730 if (main_md->part_curr == md->part_type)
733 if (mmc_card_mmc(card)) {
734 u8 part_config = card->ext_csd.part_config;
736 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
737 mmc_retune_pause(card->host);
739 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
740 part_config |= md->part_type;
742 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
743 EXT_CSD_PART_CONFIG, part_config,
744 card->ext_csd.part_time);
746 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
747 mmc_retune_unpause(card->host);
751 card->ext_csd.part_config = part_config;
753 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
754 mmc_retune_unpause(card->host);
757 main_md->part_curr = md->part_type;
761 static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
767 struct mmc_request mrq = {};
768 struct mmc_command cmd = {};
769 struct mmc_data data = {};
771 struct scatterlist sg;
773 cmd.opcode = MMC_APP_CMD;
774 cmd.arg = card->rca << 16;
775 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
777 err = mmc_wait_for_cmd(card->host, &cmd, 0);
780 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
783 memset(&cmd, 0, sizeof(struct mmc_command));
785 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
787 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
791 data.flags = MMC_DATA_READ;
794 mmc_set_data_timeout(&data, card);
799 blocks = kmalloc(4, GFP_KERNEL);
803 sg_init_one(&sg, blocks, 4);
805 mmc_wait_for_req(card->host, &mrq);
807 result = ntohl(*blocks);
810 if (cmd.error || data.error)
813 *written_blocks = result;
818 static int get_card_status(struct mmc_card *card, u32 *status, int retries)
820 struct mmc_command cmd = {};
823 cmd.opcode = MMC_SEND_STATUS;
824 if (!mmc_host_is_spi(card->host))
825 cmd.arg = card->rca << 16;
826 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
827 err = mmc_wait_for_cmd(card->host, &cmd, retries);
829 *status = cmd.resp[0];
833 static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
834 bool hw_busy_detect, struct request *req, bool *gen_err)
836 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
841 err = get_card_status(card, &status, 5);
843 pr_err("%s: error %d requesting status\n",
844 req->rq_disk->disk_name, err);
848 if (status & R1_ERROR) {
849 pr_err("%s: %s: error sending status cmd, status %#x\n",
850 req->rq_disk->disk_name, __func__, status);
854 /* We may rely on the host hw to handle busy detection.*/
855 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
860 * Timeout if the device never becomes ready for data and never
861 * leaves the program state.
863 if (time_after(jiffies, timeout)) {
864 pr_err("%s: Card stuck in programming state! %s %s\n",
865 mmc_hostname(card->host),
866 req->rq_disk->disk_name, __func__);
871 * Some cards mishandle the status bits,
872 * so make sure to check both the busy
873 * indication and the card state.
875 } while (!(status & R1_READY_FOR_DATA) ||
876 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
881 static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
882 struct request *req, bool *gen_err, u32 *stop_status)
884 struct mmc_host *host = card->host;
885 struct mmc_command cmd = {};
887 bool use_r1b_resp = rq_data_dir(req) == WRITE;
890 * Normally we use R1B responses for WRITE, but in cases where the host
891 * has specified a max_busy_timeout we need to validate it. A failure
892 * means we need to prevent the host from doing hw busy detection, which
893 * is done by converting to a R1 response instead.
895 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
896 use_r1b_resp = false;
898 cmd.opcode = MMC_STOP_TRANSMISSION;
900 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
901 cmd.busy_timeout = timeout_ms;
903 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
906 err = mmc_wait_for_cmd(host, &cmd, 5);
910 *stop_status = cmd.resp[0];
912 /* No need to check card status in case of READ. */
913 if (rq_data_dir(req) == READ)
916 if (!mmc_host_is_spi(host) &&
917 (*stop_status & R1_ERROR)) {
918 pr_err("%s: %s: general error sending stop command, resp %#x\n",
919 req->rq_disk->disk_name, __func__, *stop_status);
923 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
926 #define ERR_NOMEDIUM 3
929 #define ERR_CONTINUE 0
931 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
932 bool status_valid, u32 status)
936 /* response crc error, retry the r/w cmd */
937 pr_err("%s: %s sending %s command, card status %#x\n",
938 req->rq_disk->disk_name, "response CRC error",
943 pr_err("%s: %s sending %s command, card status %#x\n",
944 req->rq_disk->disk_name, "timed out", name, status);
946 /* If the status cmd initially failed, retry the r/w cmd */
948 pr_err("%s: status not valid, retrying timeout\n",
949 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",
960 req->rq_disk->disk_name);
964 /* Otherwise abort the command */
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, bool *ecc_err, bool *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->blkdata;
1136 * If this is a RPMB partition access, return ture
1138 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1144 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1146 struct mmc_blk_data *md = mq->blkdata;
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;
1167 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1168 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1169 INAND_CMD38_ARG_EXT_CSD,
1170 arg == MMC_TRIM_ARG ?
1171 INAND_CMD38_ARG_TRIM :
1172 INAND_CMD38_ARG_ERASE,
1176 err = mmc_erase(card, from, nr, arg);
1177 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
1179 mmc_blk_reset_success(md, type);
1181 blk_end_request(req, err, blk_rq_bytes(req));
1184 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1185 struct request *req)
1187 struct mmc_blk_data *md = mq->blkdata;
1188 struct mmc_card *card = md->queue.card;
1189 unsigned int from, nr, arg;
1190 int err = 0, type = MMC_BLK_SECDISCARD;
1192 if (!(mmc_can_secure_erase_trim(card))) {
1197 from = blk_rq_pos(req);
1198 nr = blk_rq_sectors(req);
1200 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1201 arg = MMC_SECURE_TRIM1_ARG;
1203 arg = MMC_SECURE_ERASE_ARG;
1206 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1207 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1208 INAND_CMD38_ARG_EXT_CSD,
1209 arg == MMC_SECURE_TRIM1_ARG ?
1210 INAND_CMD38_ARG_SECTRIM1 :
1211 INAND_CMD38_ARG_SECERASE,
1217 err = mmc_erase(card, from, nr, arg);
1223 if (arg == MMC_SECURE_TRIM1_ARG) {
1224 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1225 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1226 INAND_CMD38_ARG_EXT_CSD,
1227 INAND_CMD38_ARG_SECTRIM2,
1233 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1241 if (err && !mmc_blk_reset(md, card->host, type))
1244 mmc_blk_reset_success(md, type);
1246 blk_end_request(req, err, blk_rq_bytes(req));
1249 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1251 struct mmc_blk_data *md = mq->blkdata;
1252 struct mmc_card *card = md->queue.card;
1255 ret = mmc_flush_cache(card);
1259 blk_end_request_all(req, ret);
1263 * Reformat current write as a reliable write, supporting
1264 * both legacy and the enhanced reliable write MMC cards.
1265 * In each transfer we'll handle only as much as a single
1266 * reliable write can handle, thus finish the request in
1267 * partial completions.
1269 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1270 struct mmc_card *card,
1271 struct request *req)
1273 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1274 /* Legacy mode imposes restrictions on transfers. */
1275 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1276 brq->data.blocks = 1;
1278 if (brq->data.blocks > card->ext_csd.rel_sectors)
1279 brq->data.blocks = card->ext_csd.rel_sectors;
1280 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1281 brq->data.blocks = 1;
1285 #define CMD_ERRORS \
1286 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1287 R1_ADDRESS_ERROR | /* Misaligned address */ \
1288 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1289 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1290 R1_CC_ERROR | /* Card controller error */ \
1291 R1_ERROR) /* General/unknown error */
1293 static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1294 struct mmc_async_req *areq)
1296 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1298 struct mmc_blk_request *brq = &mq_mrq->brq;
1299 struct request *req = mq_mrq->req;
1300 int need_retune = card->host->need_retune;
1301 bool ecc_err = false;
1302 bool gen_err = false;
1305 * sbc.error indicates a problem with the set block count
1306 * command. No data will have been transferred.
1308 * cmd.error indicates a problem with the r/w command. No
1309 * data will have been transferred.
1311 * stop.error indicates a problem with the stop command. Data
1312 * may have been transferred, or may still be transferring.
1314 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1316 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
1318 return MMC_BLK_RETRY;
1320 return MMC_BLK_ABORT;
1322 return MMC_BLK_NOMEDIUM;
1329 * Check for errors relating to the execution of the
1330 * initial command - such as address errors. No data
1331 * has been transferred.
1333 if (brq->cmd.resp[0] & CMD_ERRORS) {
1334 pr_err("%s: r/w command failed, status = %#x\n",
1335 req->rq_disk->disk_name, brq->cmd.resp[0]);
1336 return MMC_BLK_ABORT;
1340 * Everything else is either success, or a data error of some
1341 * kind. If it was a write, we may have transitioned to
1342 * program mode, which we have to wait for it to complete.
1344 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1347 /* Check stop command response */
1348 if (brq->stop.resp[0] & R1_ERROR) {
1349 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1350 req->rq_disk->disk_name, __func__,
1355 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1358 return MMC_BLK_CMD_ERR;
1361 /* if general error occurs, retry the write operation. */
1363 pr_warn("%s: retrying write for general error\n",
1364 req->rq_disk->disk_name);
1365 return MMC_BLK_RETRY;
1368 if (brq->data.error) {
1369 if (need_retune && !brq->retune_retry_done) {
1370 pr_debug("%s: retrying because a re-tune was needed\n",
1371 req->rq_disk->disk_name);
1372 brq->retune_retry_done = 1;
1373 return MMC_BLK_RETRY;
1375 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1376 req->rq_disk->disk_name, brq->data.error,
1377 (unsigned)blk_rq_pos(req),
1378 (unsigned)blk_rq_sectors(req),
1379 brq->cmd.resp[0], brq->stop.resp[0]);
1381 if (rq_data_dir(req) == READ) {
1383 return MMC_BLK_ECC_ERR;
1384 return MMC_BLK_DATA_ERR;
1386 return MMC_BLK_CMD_ERR;
1390 if (!brq->data.bytes_xfered)
1391 return MMC_BLK_RETRY;
1393 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1394 return MMC_BLK_PARTIAL;
1396 return MMC_BLK_SUCCESS;
1399 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1400 struct mmc_card *card,
1402 struct mmc_queue *mq)
1404 u32 readcmd, writecmd;
1405 struct mmc_blk_request *brq = &mqrq->brq;
1406 struct request *req = mqrq->req;
1407 struct mmc_blk_data *md = mq->blkdata;
1411 * Reliable writes are used to implement Forced Unit Access and
1412 * are supported only on MMCs.
1414 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1415 (rq_data_dir(req) == WRITE) &&
1416 (md->flags & MMC_BLK_REL_WR);
1418 memset(brq, 0, sizeof(struct mmc_blk_request));
1419 brq->mrq.cmd = &brq->cmd;
1420 brq->mrq.data = &brq->data;
1422 brq->cmd.arg = blk_rq_pos(req);
1423 if (!mmc_card_blockaddr(card))
1425 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1426 brq->data.blksz = 512;
1427 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1429 brq->data.blocks = blk_rq_sectors(req);
1432 * The block layer doesn't support all sector count
1433 * restrictions, so we need to be prepared for too big
1436 if (brq->data.blocks > card->host->max_blk_count)
1437 brq->data.blocks = card->host->max_blk_count;
1439 if (brq->data.blocks > 1) {
1441 * After a read error, we redo the request one sector
1442 * at a time in order to accurately determine which
1443 * sectors can be read successfully.
1446 brq->data.blocks = 1;
1449 * Some controllers have HW issues while operating
1450 * in multiple I/O mode
1452 if (card->host->ops->multi_io_quirk)
1453 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1454 (rq_data_dir(req) == READ) ?
1455 MMC_DATA_READ : MMC_DATA_WRITE,
1459 if (brq->data.blocks > 1 || do_rel_wr) {
1460 /* SPI multiblock writes terminate using a special
1461 * token, not a STOP_TRANSMISSION request.
1463 if (!mmc_host_is_spi(card->host) ||
1464 rq_data_dir(req) == READ)
1465 brq->mrq.stop = &brq->stop;
1466 readcmd = MMC_READ_MULTIPLE_BLOCK;
1467 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1469 brq->mrq.stop = NULL;
1470 readcmd = MMC_READ_SINGLE_BLOCK;
1471 writecmd = MMC_WRITE_BLOCK;
1473 if (rq_data_dir(req) == READ) {
1474 brq->cmd.opcode = readcmd;
1475 brq->data.flags = MMC_DATA_READ;
1477 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1480 brq->cmd.opcode = writecmd;
1481 brq->data.flags = MMC_DATA_WRITE;
1483 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1488 mmc_apply_rel_rw(brq, card, req);
1491 * Data tag is used only during writing meta data to speed
1492 * up write and any subsequent read of this meta data
1494 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1495 (req->cmd_flags & REQ_META) &&
1496 (rq_data_dir(req) == WRITE) &&
1497 ((brq->data.blocks * brq->data.blksz) >=
1498 card->ext_csd.data_tag_unit_size);
1501 * Pre-defined multi-block transfers are preferable to
1502 * open ended-ones (and necessary for reliable writes).
1503 * However, it is not sufficient to just send CMD23,
1504 * and avoid the final CMD12, as on an error condition
1505 * CMD12 (stop) needs to be sent anyway. This, coupled
1506 * with Auto-CMD23 enhancements provided by some
1507 * hosts, means that the complexity of dealing
1508 * with this is best left to the host. If CMD23 is
1509 * supported by card and host, we'll fill sbc in and let
1510 * the host deal with handling it correctly. This means
1511 * that for hosts that don't expose MMC_CAP_CMD23, no
1512 * change of behavior will be observed.
1514 * N.B: Some MMC cards experience perf degradation.
1515 * We'll avoid using CMD23-bounded multiblock writes for
1516 * these, while retaining features like reliable writes.
1518 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1519 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1521 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1522 brq->sbc.arg = brq->data.blocks |
1523 (do_rel_wr ? (1 << 31) : 0) |
1524 (do_data_tag ? (1 << 29) : 0);
1525 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1526 brq->mrq.sbc = &brq->sbc;
1529 mmc_set_data_timeout(&brq->data, card);
1531 brq->data.sg = mqrq->sg;
1532 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1535 * Adjust the sg list so it is the same size as the
1538 if (brq->data.blocks != blk_rq_sectors(req)) {
1539 int i, data_size = brq->data.blocks << 9;
1540 struct scatterlist *sg;
1542 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1543 data_size -= sg->length;
1544 if (data_size <= 0) {
1545 sg->length += data_size;
1550 brq->data.sg_len = i;
1553 mqrq->areq.mrq = &brq->mrq;
1554 mqrq->areq.err_check = mmc_blk_err_check;
1556 mmc_queue_bounce_pre(mqrq);
1559 static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1560 struct mmc_blk_request *brq, struct request *req,
1561 bool old_req_pending)
1563 struct mmc_queue_req *mq_rq;
1566 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1569 * If this is an SD card and we're writing, we can first
1570 * mark the known good sectors as ok.
1572 * If the card is not SD, we can still ok written sectors
1573 * as reported by the controller (which might be less than
1574 * the real number of written sectors, but never more).
1576 if (mmc_card_sd(card)) {
1580 err = mmc_sd_num_wr_blocks(card, &blocks);
1582 req_pending = old_req_pending;
1584 req_pending = blk_end_request(req, 0, blocks << 9);
1586 req_pending = blk_end_request(req, 0, brq->data.bytes_xfered);
1591 static void mmc_blk_rw_cmd_abort(struct mmc_card *card, struct request *req)
1593 if (mmc_card_removed(card))
1594 req->rq_flags |= RQF_QUIET;
1595 while (blk_end_request(req, -EIO, blk_rq_cur_bytes(req)));
1599 * mmc_blk_rw_try_restart() - tries to restart the current async request
1600 * @mq: the queue with the card and host to restart
1601 * @req: a new request that want to be started after the current one
1603 static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req)
1609 * If the card was removed, just cancel everything and return.
1611 if (mmc_card_removed(mq->card)) {
1612 req->rq_flags |= RQF_QUIET;
1613 blk_end_request_all(req, -EIO);
1616 /* Else proceed and try to restart the current async request */
1617 mmc_blk_rw_rq_prep(mq->mqrq_cur, mq->card, 0, mq);
1618 mmc_start_areq(mq->card->host, &mq->mqrq_cur->areq, NULL);
1621 static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req)
1623 struct mmc_blk_data *md = mq->blkdata;
1624 struct mmc_card *card = md->queue.card;
1625 struct mmc_blk_request *brq;
1626 int disable_multi = 0, retry = 0, type, retune_retry_done = 0;
1627 enum mmc_blk_status status;
1628 struct mmc_queue_req *mq_rq;
1629 struct request *old_req;
1630 struct mmc_async_req *new_areq;
1631 struct mmc_async_req *old_areq;
1632 bool req_pending = true;
1634 if (!new_req && !mq->mqrq_prev->req)
1640 * When 4KB native sector is enabled, only 8 blocks
1641 * multiple read or write is allowed
1643 if (mmc_large_sector(card) &&
1644 !IS_ALIGNED(blk_rq_sectors(new_req), 8)) {
1645 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1646 new_req->rq_disk->disk_name);
1647 mmc_blk_rw_cmd_abort(card, new_req);
1651 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1652 new_areq = &mq->mqrq_cur->areq;
1656 old_areq = mmc_start_areq(card->host, new_areq, &status);
1659 * We have just put the first request into the pipeline
1660 * and there is nothing more to do until it is
1663 if (status == MMC_BLK_NEW_REQUEST)
1664 mq->new_request = true;
1669 * An asynchronous request has been completed and we proceed
1670 * to handle the result of it.
1672 mq_rq = container_of(old_areq, struct mmc_queue_req, areq);
1674 old_req = mq_rq->req;
1675 type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1676 mmc_queue_bounce_post(mq_rq);
1679 case MMC_BLK_SUCCESS:
1680 case MMC_BLK_PARTIAL:
1682 * A block was successfully transferred.
1684 mmc_blk_reset_success(md, type);
1686 req_pending = blk_end_request(old_req, 0,
1687 brq->data.bytes_xfered);
1689 * If the blk_end_request function returns non-zero even
1690 * though all data has been transferred and no errors
1691 * were returned by the host controller, it's a bug.
1693 if (status == MMC_BLK_SUCCESS && req_pending) {
1694 pr_err("%s BUG rq_tot %d d_xfer %d\n",
1695 __func__, blk_rq_bytes(old_req),
1696 brq->data.bytes_xfered);
1697 mmc_blk_rw_cmd_abort(card, old_req);
1701 case MMC_BLK_CMD_ERR:
1702 req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending);
1703 if (mmc_blk_reset(md, card->host, type)) {
1704 mmc_blk_rw_cmd_abort(card, old_req);
1705 mmc_blk_rw_try_restart(mq, new_req);
1709 mmc_blk_rw_try_restart(mq, new_req);
1714 retune_retry_done = brq->retune_retry_done;
1719 if (!mmc_blk_reset(md, card->host, type))
1721 mmc_blk_rw_cmd_abort(card, old_req);
1722 mmc_blk_rw_try_restart(mq, new_req);
1724 case MMC_BLK_DATA_ERR: {
1727 err = mmc_blk_reset(md, card->host, type);
1730 if (err == -ENODEV) {
1731 mmc_blk_rw_cmd_abort(card, old_req);
1732 mmc_blk_rw_try_restart(mq, new_req);
1737 case MMC_BLK_ECC_ERR:
1738 if (brq->data.blocks > 1) {
1739 /* Redo read one sector at a time */
1740 pr_warn("%s: retrying using single block read\n",
1741 old_req->rq_disk->disk_name);
1746 * After an error, we redo I/O one sector at a
1747 * time, so we only reach here after trying to
1748 * read a single sector.
1750 req_pending = blk_end_request(old_req, -EIO,
1753 mmc_blk_rw_try_restart(mq, new_req);
1757 case MMC_BLK_NOMEDIUM:
1758 mmc_blk_rw_cmd_abort(card, old_req);
1759 mmc_blk_rw_try_restart(mq, new_req);
1762 pr_err("%s: Unhandled return value (%d)",
1763 old_req->rq_disk->disk_name, status);
1764 mmc_blk_rw_cmd_abort(card, old_req);
1765 mmc_blk_rw_try_restart(mq, new_req);
1771 * In case of a incomplete request
1772 * prepare it again and resend.
1774 mmc_blk_rw_rq_prep(mq_rq, card,
1776 mmc_start_areq(card->host,
1777 &mq_rq->areq, NULL);
1778 mq_rq->brq.retune_retry_done = retune_retry_done;
1780 } while (req_pending);
1783 void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1786 struct mmc_blk_data *md = mq->blkdata;
1787 struct mmc_card *card = md->queue.card;
1788 bool req_is_special = mmc_req_is_special(req);
1790 if (req && !mq->mqrq_prev->req)
1791 /* claim host only for the first request */
1794 ret = mmc_blk_part_switch(card, md);
1797 blk_end_request_all(req, -EIO);
1802 mq->new_request = false;
1803 if (req && req_op(req) == REQ_OP_DISCARD) {
1804 /* complete ongoing async transfer before issuing discard */
1805 if (card->host->areq)
1806 mmc_blk_issue_rw_rq(mq, NULL);
1807 mmc_blk_issue_discard_rq(mq, req);
1808 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
1809 /* complete ongoing async transfer before issuing secure erase*/
1810 if (card->host->areq)
1811 mmc_blk_issue_rw_rq(mq, NULL);
1812 mmc_blk_issue_secdiscard_rq(mq, req);
1813 } else if (req && req_op(req) == REQ_OP_FLUSH) {
1814 /* complete ongoing async transfer before issuing flush */
1815 if (card->host->areq)
1816 mmc_blk_issue_rw_rq(mq, NULL);
1817 mmc_blk_issue_flush(mq, req);
1819 mmc_blk_issue_rw_rq(mq, req);
1823 if ((!req && !mq->new_request) || req_is_special)
1825 * Release host when there are no more requests
1826 * and after special request(discard, flush) is done.
1827 * In case sepecial request, there is no reentry to
1828 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
1833 static inline int mmc_blk_readonly(struct mmc_card *card)
1835 return mmc_card_readonly(card) ||
1836 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1839 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1840 struct device *parent,
1843 const char *subname,
1846 struct mmc_blk_data *md;
1849 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
1851 return ERR_PTR(devidx);
1853 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
1859 md->area_type = area_type;
1862 * Set the read-only status based on the supported commands
1863 * and the write protect switch.
1865 md->read_only = mmc_blk_readonly(card);
1867 md->disk = alloc_disk(perdev_minors);
1868 if (md->disk == NULL) {
1873 spin_lock_init(&md->lock);
1874 INIT_LIST_HEAD(&md->part);
1877 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
1881 md->queue.blkdata = md;
1883 md->disk->major = MMC_BLOCK_MAJOR;
1884 md->disk->first_minor = devidx * perdev_minors;
1885 md->disk->fops = &mmc_bdops;
1886 md->disk->private_data = md;
1887 md->disk->queue = md->queue.queue;
1888 md->parent = parent;
1889 set_disk_ro(md->disk, md->read_only || default_ro);
1890 md->disk->flags = GENHD_FL_EXT_DEVT;
1891 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
1892 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
1895 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1897 * - be set for removable media with permanent block devices
1898 * - be unset for removable block devices with permanent media
1900 * Since MMC block devices clearly fall under the second
1901 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1902 * should use the block device creation/destruction hotplug
1903 * messages to tell when the card is present.
1906 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1907 "mmcblk%u%s", card->host->index, subname ? subname : "");
1909 if (mmc_card_mmc(card))
1910 blk_queue_logical_block_size(md->queue.queue,
1911 card->ext_csd.data_sector_size);
1913 blk_queue_logical_block_size(md->queue.queue, 512);
1915 set_capacity(md->disk, size);
1917 if (mmc_host_cmd23(card->host)) {
1918 if ((mmc_card_mmc(card) &&
1919 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
1920 (mmc_card_sd(card) &&
1921 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1922 md->flags |= MMC_BLK_CMD23;
1925 if (mmc_card_mmc(card) &&
1926 md->flags & MMC_BLK_CMD23 &&
1927 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1928 card->ext_csd.rel_sectors)) {
1929 md->flags |= MMC_BLK_REL_WR;
1930 blk_queue_write_cache(md->queue.queue, true, true);
1940 ida_simple_remove(&mmc_blk_ida, devidx);
1941 return ERR_PTR(ret);
1944 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1948 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1950 * The EXT_CSD sector count is in number or 512 byte
1953 size = card->ext_csd.sectors;
1956 * The CSD capacity field is in units of read_blkbits.
1957 * set_capacity takes units of 512 bytes.
1959 size = (typeof(sector_t))card->csd.capacity
1960 << (card->csd.read_blkbits - 9);
1963 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
1964 MMC_BLK_DATA_AREA_MAIN);
1967 static int mmc_blk_alloc_part(struct mmc_card *card,
1968 struct mmc_blk_data *md,
1969 unsigned int part_type,
1972 const char *subname,
1976 struct mmc_blk_data *part_md;
1978 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1979 subname, area_type);
1980 if (IS_ERR(part_md))
1981 return PTR_ERR(part_md);
1982 part_md->part_type = part_type;
1983 list_add(&part_md->part, &md->part);
1985 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
1986 cap_str, sizeof(cap_str));
1987 pr_info("%s: %s %s partition %u %s\n",
1988 part_md->disk->disk_name, mmc_card_id(card),
1989 mmc_card_name(card), part_md->part_type, cap_str);
1993 /* MMC Physical partitions consist of two boot partitions and
1994 * up to four general purpose partitions.
1995 * For each partition enabled in EXT_CSD a block device will be allocatedi
1996 * to provide access to the partition.
1999 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2003 if (!mmc_card_mmc(card))
2006 for (idx = 0; idx < card->nr_parts; idx++) {
2007 if (card->part[idx].size) {
2008 ret = mmc_blk_alloc_part(card, md,
2009 card->part[idx].part_cfg,
2010 card->part[idx].size >> 9,
2011 card->part[idx].force_ro,
2012 card->part[idx].name,
2013 card->part[idx].area_type);
2022 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2024 struct mmc_card *card;
2028 * Flush remaining requests and free queues. It
2029 * is freeing the queue that stops new requests
2030 * from being accepted.
2032 card = md->queue.card;
2033 mmc_cleanup_queue(&md->queue);
2034 if (md->disk->flags & GENHD_FL_UP) {
2035 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2036 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2037 card->ext_csd.boot_ro_lockable)
2038 device_remove_file(disk_to_dev(md->disk),
2039 &md->power_ro_lock);
2041 del_gendisk(md->disk);
2047 static void mmc_blk_remove_parts(struct mmc_card *card,
2048 struct mmc_blk_data *md)
2050 struct list_head *pos, *q;
2051 struct mmc_blk_data *part_md;
2053 list_for_each_safe(pos, q, &md->part) {
2054 part_md = list_entry(pos, struct mmc_blk_data, part);
2056 mmc_blk_remove_req(part_md);
2060 static int mmc_add_disk(struct mmc_blk_data *md)
2063 struct mmc_card *card = md->queue.card;
2065 device_add_disk(md->parent, md->disk);
2066 md->force_ro.show = force_ro_show;
2067 md->force_ro.store = force_ro_store;
2068 sysfs_attr_init(&md->force_ro.attr);
2069 md->force_ro.attr.name = "force_ro";
2070 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2071 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2075 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2076 card->ext_csd.boot_ro_lockable) {
2079 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2082 mode = S_IRUGO | S_IWUSR;
2084 md->power_ro_lock.show = power_ro_lock_show;
2085 md->power_ro_lock.store = power_ro_lock_store;
2086 sysfs_attr_init(&md->power_ro_lock.attr);
2087 md->power_ro_lock.attr.mode = mode;
2088 md->power_ro_lock.attr.name =
2089 "ro_lock_until_next_power_on";
2090 ret = device_create_file(disk_to_dev(md->disk),
2091 &md->power_ro_lock);
2093 goto power_ro_lock_fail;
2098 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2100 del_gendisk(md->disk);
2105 static int mmc_blk_probe(struct mmc_card *card)
2107 struct mmc_blk_data *md, *part_md;
2111 * Check that the card supports the command class(es) we need.
2113 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2116 mmc_fixup_device(card, mmc_blk_fixups);
2118 md = mmc_blk_alloc(card);
2122 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
2123 cap_str, sizeof(cap_str));
2124 pr_info("%s: %s %s %s %s\n",
2125 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2126 cap_str, md->read_only ? "(ro)" : "");
2128 if (mmc_blk_alloc_parts(card, md))
2131 dev_set_drvdata(&card->dev, md);
2133 if (mmc_add_disk(md))
2136 list_for_each_entry(part_md, &md->part, part) {
2137 if (mmc_add_disk(part_md))
2141 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2142 pm_runtime_use_autosuspend(&card->dev);
2145 * Don't enable runtime PM for SD-combo cards here. Leave that
2146 * decision to be taken during the SDIO init sequence instead.
2148 if (card->type != MMC_TYPE_SD_COMBO) {
2149 pm_runtime_set_active(&card->dev);
2150 pm_runtime_enable(&card->dev);
2156 mmc_blk_remove_parts(card, md);
2157 mmc_blk_remove_req(md);
2161 static void mmc_blk_remove(struct mmc_card *card)
2163 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2165 mmc_blk_remove_parts(card, md);
2166 pm_runtime_get_sync(&card->dev);
2167 mmc_claim_host(card->host);
2168 mmc_blk_part_switch(card, md);
2169 mmc_release_host(card->host);
2170 if (card->type != MMC_TYPE_SD_COMBO)
2171 pm_runtime_disable(&card->dev);
2172 pm_runtime_put_noidle(&card->dev);
2173 mmc_blk_remove_req(md);
2174 dev_set_drvdata(&card->dev, NULL);
2177 static int _mmc_blk_suspend(struct mmc_card *card)
2179 struct mmc_blk_data *part_md;
2180 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2183 mmc_queue_suspend(&md->queue);
2184 list_for_each_entry(part_md, &md->part, part) {
2185 mmc_queue_suspend(&part_md->queue);
2191 static void mmc_blk_shutdown(struct mmc_card *card)
2193 _mmc_blk_suspend(card);
2196 #ifdef CONFIG_PM_SLEEP
2197 static int mmc_blk_suspend(struct device *dev)
2199 struct mmc_card *card = mmc_dev_to_card(dev);
2201 return _mmc_blk_suspend(card);
2204 static int mmc_blk_resume(struct device *dev)
2206 struct mmc_blk_data *part_md;
2207 struct mmc_blk_data *md = dev_get_drvdata(dev);
2211 * Resume involves the card going into idle state,
2212 * so current partition is always the main one.
2214 md->part_curr = md->part_type;
2215 mmc_queue_resume(&md->queue);
2216 list_for_each_entry(part_md, &md->part, part) {
2217 mmc_queue_resume(&part_md->queue);
2224 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2226 static struct mmc_driver mmc_driver = {
2229 .pm = &mmc_blk_pm_ops,
2231 .probe = mmc_blk_probe,
2232 .remove = mmc_blk_remove,
2233 .shutdown = mmc_blk_shutdown,
2236 static int __init mmc_blk_init(void)
2240 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2241 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2243 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
2245 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2249 res = mmc_register_driver(&mmc_driver);
2255 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2260 static void __exit mmc_blk_exit(void)
2262 mmc_unregister_driver(&mmc_driver);
2263 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2266 module_init(mmc_blk_init);
2267 module_exit(mmc_blk_exit);
2269 MODULE_LICENSE("GPL");
2270 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");