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);
131 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
133 struct mmc_blk_data *md;
135 mutex_lock(&open_lock);
136 md = disk->private_data;
137 if (md && md->usage == 0)
141 mutex_unlock(&open_lock);
146 static inline int mmc_get_devidx(struct gendisk *disk)
148 int devidx = disk->first_minor / perdev_minors;
152 static void mmc_blk_put(struct mmc_blk_data *md)
154 mutex_lock(&open_lock);
156 if (md->usage == 0) {
157 int devidx = mmc_get_devidx(md->disk);
158 blk_cleanup_queue(md->queue.queue);
159 ida_simple_remove(&mmc_blk_ida, devidx);
163 mutex_unlock(&open_lock);
166 static ssize_t power_ro_lock_show(struct device *dev,
167 struct device_attribute *attr, char *buf)
170 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
171 struct mmc_card *card = md->queue.card;
174 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
176 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
179 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
186 static ssize_t power_ro_lock_store(struct device *dev,
187 struct device_attribute *attr, const char *buf, size_t count)
190 struct mmc_blk_data *md, *part_md;
191 struct mmc_card *card;
192 struct mmc_queue *mq;
196 if (kstrtoul(buf, 0, &set))
202 md = mmc_blk_get(dev_to_disk(dev));
204 card = md->queue.card;
206 /* Dispatch locking to the block layer */
207 req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, __GFP_RECLAIM);
208 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
209 blk_execute_rq(mq->queue, NULL, req, 0);
210 ret = req_to_mmc_queue_req(req)->drv_op_result;
213 pr_info("%s: Locking boot partition ro until next power on\n",
214 md->disk->disk_name);
215 set_disk_ro(md->disk, 1);
217 list_for_each_entry(part_md, &md->part, part)
218 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
219 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
220 set_disk_ro(part_md->disk, 1);
228 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
232 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
234 ret = snprintf(buf, PAGE_SIZE, "%d\n",
235 get_disk_ro(dev_to_disk(dev)) ^
241 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
242 const char *buf, size_t count)
246 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
247 unsigned long set = simple_strtoul(buf, &end, 0);
253 set_disk_ro(dev_to_disk(dev), set || md->read_only);
260 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
262 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
265 mutex_lock(&block_mutex);
268 check_disk_change(bdev);
271 if ((mode & FMODE_WRITE) && md->read_only) {
276 mutex_unlock(&block_mutex);
281 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
283 struct mmc_blk_data *md = disk->private_data;
285 mutex_lock(&block_mutex);
287 mutex_unlock(&block_mutex);
291 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
293 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
299 struct mmc_blk_ioc_data {
300 struct mmc_ioc_cmd ic;
305 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
306 struct mmc_ioc_cmd __user *user)
308 struct mmc_blk_ioc_data *idata;
311 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
317 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
322 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
323 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
328 if (!idata->buf_bytes) {
333 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
339 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
340 idata->ic.data_ptr, idata->buf_bytes)) {
355 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
356 struct mmc_blk_ioc_data *idata)
358 struct mmc_ioc_cmd *ic = &idata->ic;
360 if (copy_to_user(&(ic_ptr->response), ic->response,
361 sizeof(ic->response)))
364 if (!idata->ic.write_flag) {
365 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
366 idata->buf, idata->buf_bytes))
373 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
379 if (!status || !retries_max)
383 err = __mmc_send_status(card, status, 5);
387 if (!R1_STATUS(*status) &&
388 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
389 break; /* RPMB programming operation complete */
392 * Rechedule to give the MMC device a chance to continue
393 * processing the previous command without being polled too
396 usleep_range(1000, 5000);
397 } while (++retry_count < retries_max);
399 if (retry_count == retries_max)
405 static int ioctl_do_sanitize(struct mmc_card *card)
409 if (!mmc_can_sanitize(card)) {
410 pr_warn("%s: %s - SANITIZE is not supported\n",
411 mmc_hostname(card->host), __func__);
416 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
417 mmc_hostname(card->host), __func__);
419 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
420 EXT_CSD_SANITIZE_START, 1,
421 MMC_SANITIZE_REQ_TIMEOUT);
424 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
425 mmc_hostname(card->host), __func__, err);
427 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
433 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
434 struct mmc_blk_ioc_data *idata)
436 struct mmc_command cmd = {};
437 struct mmc_data data = {};
438 struct mmc_request mrq = {};
439 struct scatterlist sg;
441 bool is_rpmb = false;
444 if (!card || !md || !idata)
447 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
450 cmd.opcode = idata->ic.opcode;
451 cmd.arg = idata->ic.arg;
452 cmd.flags = idata->ic.flags;
454 if (idata->buf_bytes) {
457 data.blksz = idata->ic.blksz;
458 data.blocks = idata->ic.blocks;
460 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
462 if (idata->ic.write_flag)
463 data.flags = MMC_DATA_WRITE;
465 data.flags = MMC_DATA_READ;
467 /* data.flags must already be set before doing this. */
468 mmc_set_data_timeout(&data, card);
470 /* Allow overriding the timeout_ns for empirical tuning. */
471 if (idata->ic.data_timeout_ns)
472 data.timeout_ns = idata->ic.data_timeout_ns;
474 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
476 * Pretend this is a data transfer and rely on the
477 * host driver to compute timeout. When all host
478 * drivers support cmd.cmd_timeout for R1B, this
482 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
484 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
492 err = mmc_blk_part_switch(card, md);
496 if (idata->ic.is_acmd) {
497 err = mmc_app_cmd(card->host, card);
503 err = mmc_set_blockcount(card, data.blocks,
504 idata->ic.write_flag & (1 << 31));
509 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
510 (cmd.opcode == MMC_SWITCH)) {
511 err = ioctl_do_sanitize(card);
514 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
520 mmc_wait_for_req(card->host, &mrq);
523 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
524 __func__, cmd.error);
528 dev_err(mmc_dev(card->host), "%s: data error %d\n",
529 __func__, data.error);
534 * According to the SD specs, some commands require a delay after
535 * issuing the command.
537 if (idata->ic.postsleep_min_us)
538 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
540 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
544 * Ensure RPMB command has completed by polling CMD13
547 err = ioctl_rpmb_card_status_poll(card, &status, 5);
549 dev_err(mmc_dev(card->host),
550 "%s: Card Status=0x%08X, error %d\n",
551 __func__, status, err);
557 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
558 struct mmc_ioc_cmd __user *ic_ptr)
560 struct mmc_blk_ioc_data *idata;
561 struct mmc_blk_ioc_data *idatas[1];
562 struct mmc_blk_data *md;
563 struct mmc_queue *mq;
564 struct mmc_card *card;
565 int err = 0, ioc_err = 0;
569 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
570 * whole block device, not on a partition. This prevents overspray
571 * between sibling partitions.
573 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
576 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
578 return PTR_ERR(idata);
580 md = mmc_blk_get(bdev->bd_disk);
586 card = md->queue.card;
593 * Dispatch the ioctl() into the block request queue.
596 req = blk_get_request(mq->queue,
597 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
600 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_IOCTL;
601 req_to_mmc_queue_req(req)->idata = idatas;
602 req_to_mmc_queue_req(req)->ioc_count = 1;
603 blk_execute_rq(mq->queue, NULL, req, 0);
604 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
605 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
606 blk_put_request(req);
613 return ioc_err ? ioc_err : err;
616 static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
617 struct mmc_ioc_multi_cmd __user *user)
619 struct mmc_blk_ioc_data **idata = NULL;
620 struct mmc_ioc_cmd __user *cmds = user->cmds;
621 struct mmc_card *card;
622 struct mmc_blk_data *md;
623 struct mmc_queue *mq;
624 int i, err = 0, ioc_err = 0;
629 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
630 * whole block device, not on a partition. This prevents overspray
631 * between sibling partitions.
633 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
636 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
637 sizeof(num_of_cmds)))
643 if (num_of_cmds > MMC_IOC_MAX_CMDS)
646 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
650 for (i = 0; i < num_of_cmds; i++) {
651 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
652 if (IS_ERR(idata[i])) {
653 err = PTR_ERR(idata[i]);
659 md = mmc_blk_get(bdev->bd_disk);
665 card = md->queue.card;
673 * Dispatch the ioctl()s into the block request queue.
676 req = blk_get_request(mq->queue,
677 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
679 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_IOCTL;
680 req_to_mmc_queue_req(req)->idata = idata;
681 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
682 blk_execute_rq(mq->queue, NULL, req, 0);
683 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
685 /* copy to user if data and response */
686 for (i = 0; i < num_of_cmds && !err; i++)
687 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
689 blk_put_request(req);
694 for (i = 0; i < num_of_cmds; i++) {
695 kfree(idata[i]->buf);
699 return ioc_err ? ioc_err : err;
702 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
703 unsigned int cmd, unsigned long arg)
707 return mmc_blk_ioctl_cmd(bdev,
708 (struct mmc_ioc_cmd __user *)arg);
709 case MMC_IOC_MULTI_CMD:
710 return mmc_blk_ioctl_multi_cmd(bdev,
711 (struct mmc_ioc_multi_cmd __user *)arg);
718 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
719 unsigned int cmd, unsigned long arg)
721 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
725 static const struct block_device_operations mmc_bdops = {
726 .open = mmc_blk_open,
727 .release = mmc_blk_release,
728 .getgeo = mmc_blk_getgeo,
729 .owner = THIS_MODULE,
730 .ioctl = mmc_blk_ioctl,
732 .compat_ioctl = mmc_blk_compat_ioctl,
736 static int mmc_blk_part_switch_pre(struct mmc_card *card,
737 unsigned int part_type)
741 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
742 if (card->ext_csd.cmdq_en) {
743 ret = mmc_cmdq_disable(card);
747 mmc_retune_pause(card->host);
753 static int mmc_blk_part_switch_post(struct mmc_card *card,
754 unsigned int part_type)
758 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
759 mmc_retune_unpause(card->host);
760 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
761 ret = mmc_cmdq_enable(card);
767 static inline int mmc_blk_part_switch(struct mmc_card *card,
768 struct mmc_blk_data *md)
771 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
773 if (main_md->part_curr == md->part_type)
776 if (mmc_card_mmc(card)) {
777 u8 part_config = card->ext_csd.part_config;
779 ret = mmc_blk_part_switch_pre(card, md->part_type);
783 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
784 part_config |= md->part_type;
786 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
787 EXT_CSD_PART_CONFIG, part_config,
788 card->ext_csd.part_time);
790 mmc_blk_part_switch_post(card, md->part_type);
794 card->ext_csd.part_config = part_config;
796 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
799 main_md->part_curr = md->part_type;
803 static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
809 struct mmc_request mrq = {};
810 struct mmc_command cmd = {};
811 struct mmc_data data = {};
813 struct scatterlist sg;
815 cmd.opcode = MMC_APP_CMD;
816 cmd.arg = card->rca << 16;
817 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
819 err = mmc_wait_for_cmd(card->host, &cmd, 0);
822 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
825 memset(&cmd, 0, sizeof(struct mmc_command));
827 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
829 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
833 data.flags = MMC_DATA_READ;
836 mmc_set_data_timeout(&data, card);
841 blocks = kmalloc(4, GFP_KERNEL);
845 sg_init_one(&sg, blocks, 4);
847 mmc_wait_for_req(card->host, &mrq);
849 result = ntohl(*blocks);
852 if (cmd.error || data.error)
855 *written_blocks = result;
860 static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
861 bool hw_busy_detect, struct request *req, bool *gen_err)
863 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
868 err = __mmc_send_status(card, &status, 5);
870 pr_err("%s: error %d requesting status\n",
871 req->rq_disk->disk_name, err);
875 if (status & R1_ERROR) {
876 pr_err("%s: %s: error sending status cmd, status %#x\n",
877 req->rq_disk->disk_name, __func__, status);
881 /* We may rely on the host hw to handle busy detection.*/
882 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
887 * Timeout if the device never becomes ready for data and never
888 * leaves the program state.
890 if (time_after(jiffies, timeout)) {
891 pr_err("%s: Card stuck in programming state! %s %s\n",
892 mmc_hostname(card->host),
893 req->rq_disk->disk_name, __func__);
898 * Some cards mishandle the status bits,
899 * so make sure to check both the busy
900 * indication and the card state.
902 } while (!(status & R1_READY_FOR_DATA) ||
903 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
908 static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
909 struct request *req, bool *gen_err, u32 *stop_status)
911 struct mmc_host *host = card->host;
912 struct mmc_command cmd = {};
914 bool use_r1b_resp = rq_data_dir(req) == WRITE;
917 * Normally we use R1B responses for WRITE, but in cases where the host
918 * has specified a max_busy_timeout we need to validate it. A failure
919 * means we need to prevent the host from doing hw busy detection, which
920 * is done by converting to a R1 response instead.
922 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
923 use_r1b_resp = false;
925 cmd.opcode = MMC_STOP_TRANSMISSION;
927 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
928 cmd.busy_timeout = timeout_ms;
930 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
933 err = mmc_wait_for_cmd(host, &cmd, 5);
937 *stop_status = cmd.resp[0];
939 /* No need to check card status in case of READ. */
940 if (rq_data_dir(req) == READ)
943 if (!mmc_host_is_spi(host) &&
944 (*stop_status & R1_ERROR)) {
945 pr_err("%s: %s: general error sending stop command, resp %#x\n",
946 req->rq_disk->disk_name, __func__, *stop_status);
950 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
953 #define ERR_NOMEDIUM 3
956 #define ERR_CONTINUE 0
958 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
959 bool status_valid, u32 status)
963 /* response crc error, retry the r/w cmd */
964 pr_err("%s: %s sending %s command, card status %#x\n",
965 req->rq_disk->disk_name, "response CRC error",
970 pr_err("%s: %s sending %s command, card status %#x\n",
971 req->rq_disk->disk_name, "timed out", name, status);
973 /* If the status cmd initially failed, retry the r/w cmd */
975 pr_err("%s: status not valid, retrying timeout\n",
976 req->rq_disk->disk_name);
981 * If it was a r/w cmd crc error, or illegal command
982 * (eg, issued in wrong state) then retry - we should
983 * have corrected the state problem above.
985 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
986 pr_err("%s: command error, retrying timeout\n",
987 req->rq_disk->disk_name);
991 /* Otherwise abort the command */
995 /* We don't understand the error code the driver gave us */
996 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
997 req->rq_disk->disk_name, error, status);
1003 * Initial r/w and stop cmd error recovery.
1004 * We don't know whether the card received the r/w cmd or not, so try to
1005 * restore things back to a sane state. Essentially, we do this as follows:
1006 * - Obtain card status. If the first attempt to obtain card status fails,
1007 * the status word will reflect the failed status cmd, not the failed
1008 * r/w cmd. If we fail to obtain card status, it suggests we can no
1009 * longer communicate with the card.
1010 * - Check the card state. If the card received the cmd but there was a
1011 * transient problem with the response, it might still be in a data transfer
1012 * mode. Try to send it a stop command. If this fails, we can't recover.
1013 * - If the r/w cmd failed due to a response CRC error, it was probably
1014 * transient, so retry the cmd.
1015 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1016 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1017 * illegal cmd, retry.
1018 * Otherwise we don't understand what happened, so abort.
1020 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
1021 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
1023 bool prev_cmd_status_valid = true;
1024 u32 status, stop_status = 0;
1027 if (mmc_card_removed(card))
1028 return ERR_NOMEDIUM;
1031 * Try to get card status which indicates both the card state
1032 * and why there was no response. If the first attempt fails,
1033 * we can't be sure the returned status is for the r/w command.
1035 for (retry = 2; retry >= 0; retry--) {
1036 err = __mmc_send_status(card, &status, 0);
1040 /* Re-tune if needed */
1041 mmc_retune_recheck(card->host);
1043 prev_cmd_status_valid = false;
1044 pr_err("%s: error %d sending status command, %sing\n",
1045 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1048 /* We couldn't get a response from the card. Give up. */
1050 /* Check if the card is removed */
1051 if (mmc_detect_card_removed(card->host))
1052 return ERR_NOMEDIUM;
1056 /* Flag ECC errors */
1057 if ((status & R1_CARD_ECC_FAILED) ||
1058 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1059 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1062 /* Flag General errors */
1063 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1064 if ((status & R1_ERROR) ||
1065 (brq->stop.resp[0] & R1_ERROR)) {
1066 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1067 req->rq_disk->disk_name, __func__,
1068 brq->stop.resp[0], status);
1073 * Check the current card state. If it is in some data transfer
1074 * mode, tell it to stop (and hopefully transition back to TRAN.)
1076 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1077 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
1078 err = send_stop(card,
1079 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1080 req, gen_err, &stop_status);
1082 pr_err("%s: error %d sending stop command\n",
1083 req->rq_disk->disk_name, err);
1085 * If the stop cmd also timed out, the card is probably
1086 * not present, so abort. Other errors are bad news too.
1091 if (stop_status & R1_CARD_ECC_FAILED)
1095 /* Check for set block count errors */
1097 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1098 prev_cmd_status_valid, status);
1100 /* Check for r/w command errors */
1102 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1103 prev_cmd_status_valid, status);
1106 if (!brq->stop.error)
1107 return ERR_CONTINUE;
1109 /* Now for stop errors. These aren't fatal to the transfer. */
1110 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
1111 req->rq_disk->disk_name, brq->stop.error,
1112 brq->cmd.resp[0], status);
1115 * Subsitute in our own stop status as this will give the error
1116 * state which happened during the execution of the r/w command.
1119 brq->stop.resp[0] = stop_status;
1120 brq->stop.error = 0;
1122 return ERR_CONTINUE;
1125 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1130 if (md->reset_done & type)
1133 md->reset_done |= type;
1134 err = mmc_hw_reset(host);
1135 /* Ensure we switch back to the correct partition */
1136 if (err != -EOPNOTSUPP) {
1137 struct mmc_blk_data *main_md =
1138 dev_get_drvdata(&host->card->dev);
1141 main_md->part_curr = main_md->part_type;
1142 part_err = mmc_blk_part_switch(host->card, md);
1145 * We have failed to get back into the correct
1146 * partition, so we need to abort the whole request.
1154 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1156 md->reset_done &= ~type;
1159 int mmc_access_rpmb(struct mmc_queue *mq)
1161 struct mmc_blk_data *md = mq->blkdata;
1163 * If this is a RPMB partition access, return ture
1165 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1172 * The non-block commands come back from the block layer after it queued it and
1173 * processed it with all other requests and then they get issued in this
1176 static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1178 struct mmc_queue_req *mq_rq;
1179 struct mmc_card *card = mq->card;
1180 struct mmc_blk_data *md = mq->blkdata;
1184 mq_rq = req_to_mmc_queue_req(req);
1186 switch (mq_rq->drv_op) {
1187 case MMC_DRV_OP_IOCTL:
1188 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
1189 ret = __mmc_blk_ioctl_cmd(card, md, mq_rq->idata[i]);
1193 /* Always switch back to main area after RPMB access */
1194 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1195 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1197 case MMC_DRV_OP_BOOT_WP:
1198 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1199 card->ext_csd.boot_ro_lock |
1200 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1201 card->ext_csd.part_time);
1203 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1204 md->disk->disk_name, ret);
1206 card->ext_csd.boot_ro_lock |=
1207 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
1210 pr_err("%s: unknown driver specific operation\n",
1211 md->disk->disk_name);
1215 mq_rq->drv_op_result = ret;
1216 blk_end_request_all(req, ret);
1219 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1221 struct mmc_blk_data *md = mq->blkdata;
1222 struct mmc_card *card = md->queue.card;
1223 unsigned int from, nr, arg;
1224 int err = 0, type = MMC_BLK_DISCARD;
1225 blk_status_t status = BLK_STS_OK;
1227 if (!mmc_can_erase(card)) {
1228 status = BLK_STS_NOTSUPP;
1232 from = blk_rq_pos(req);
1233 nr = blk_rq_sectors(req);
1235 if (mmc_can_discard(card))
1236 arg = MMC_DISCARD_ARG;
1237 else if (mmc_can_trim(card))
1240 arg = MMC_ERASE_ARG;
1243 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1244 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1245 INAND_CMD38_ARG_EXT_CSD,
1246 arg == MMC_TRIM_ARG ?
1247 INAND_CMD38_ARG_TRIM :
1248 INAND_CMD38_ARG_ERASE,
1252 err = mmc_erase(card, from, nr, arg);
1253 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
1255 status = BLK_STS_IOERR;
1257 mmc_blk_reset_success(md, type);
1259 blk_end_request(req, status, blk_rq_bytes(req));
1262 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1263 struct request *req)
1265 struct mmc_blk_data *md = mq->blkdata;
1266 struct mmc_card *card = md->queue.card;
1267 unsigned int from, nr, arg;
1268 int err = 0, type = MMC_BLK_SECDISCARD;
1269 blk_status_t status = BLK_STS_OK;
1271 if (!(mmc_can_secure_erase_trim(card))) {
1272 status = BLK_STS_NOTSUPP;
1276 from = blk_rq_pos(req);
1277 nr = blk_rq_sectors(req);
1279 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1280 arg = MMC_SECURE_TRIM1_ARG;
1282 arg = MMC_SECURE_ERASE_ARG;
1285 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1286 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1287 INAND_CMD38_ARG_EXT_CSD,
1288 arg == MMC_SECURE_TRIM1_ARG ?
1289 INAND_CMD38_ARG_SECTRIM1 :
1290 INAND_CMD38_ARG_SECERASE,
1296 err = mmc_erase(card, from, nr, arg);
1300 status = BLK_STS_IOERR;
1304 if (arg == MMC_SECURE_TRIM1_ARG) {
1305 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1306 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1307 INAND_CMD38_ARG_EXT_CSD,
1308 INAND_CMD38_ARG_SECTRIM2,
1314 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1318 status = BLK_STS_IOERR;
1324 if (err && !mmc_blk_reset(md, card->host, type))
1327 mmc_blk_reset_success(md, type);
1329 blk_end_request(req, status, blk_rq_bytes(req));
1332 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1334 struct mmc_blk_data *md = mq->blkdata;
1335 struct mmc_card *card = md->queue.card;
1338 ret = mmc_flush_cache(card);
1339 blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1343 * Reformat current write as a reliable write, supporting
1344 * both legacy and the enhanced reliable write MMC cards.
1345 * In each transfer we'll handle only as much as a single
1346 * reliable write can handle, thus finish the request in
1347 * partial completions.
1349 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1350 struct mmc_card *card,
1351 struct request *req)
1353 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1354 /* Legacy mode imposes restrictions on transfers. */
1355 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
1356 brq->data.blocks = 1;
1358 if (brq->data.blocks > card->ext_csd.rel_sectors)
1359 brq->data.blocks = card->ext_csd.rel_sectors;
1360 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1361 brq->data.blocks = 1;
1365 #define CMD_ERRORS \
1366 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1367 R1_ADDRESS_ERROR | /* Misaligned address */ \
1368 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1369 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1370 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
1371 R1_CC_ERROR | /* Card controller error */ \
1372 R1_ERROR) /* General/unknown error */
1374 static bool mmc_blk_has_cmd_err(struct mmc_command *cmd)
1376 if (!cmd->error && cmd->resp[0] & CMD_ERRORS)
1382 static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1383 struct mmc_async_req *areq)
1385 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1387 struct mmc_blk_request *brq = &mq_mrq->brq;
1388 struct request *req = mmc_queue_req_to_req(mq_mrq);
1389 int need_retune = card->host->need_retune;
1390 bool ecc_err = false;
1391 bool gen_err = false;
1394 * sbc.error indicates a problem with the set block count
1395 * command. No data will have been transferred.
1397 * cmd.error indicates a problem with the r/w command. No
1398 * data will have been transferred.
1400 * stop.error indicates a problem with the stop command. Data
1401 * may have been transferred, or may still be transferring.
1403 if (brq->sbc.error || brq->cmd.error || mmc_blk_has_cmd_err(&brq->stop) ||
1405 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
1407 return MMC_BLK_RETRY;
1409 return MMC_BLK_ABORT;
1411 return MMC_BLK_NOMEDIUM;
1418 * Check for errors relating to the execution of the
1419 * initial command - such as address errors. No data
1420 * has been transferred.
1422 if (brq->cmd.resp[0] & CMD_ERRORS) {
1423 pr_err("%s: r/w command failed, status = %#x\n",
1424 req->rq_disk->disk_name, brq->cmd.resp[0]);
1425 return MMC_BLK_ABORT;
1429 * Everything else is either success, or a data error of some
1430 * kind. If it was a write, we may have transitioned to
1431 * program mode, which we have to wait for it to complete.
1433 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1436 /* Check stop command response */
1437 if (brq->stop.resp[0] & R1_ERROR) {
1438 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1439 req->rq_disk->disk_name, __func__,
1444 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1447 return MMC_BLK_CMD_ERR;
1450 /* if general error occurs, retry the write operation. */
1452 pr_warn("%s: retrying write for general error\n",
1453 req->rq_disk->disk_name);
1454 return MMC_BLK_RETRY;
1457 /* Some errors (ECC) are flagged on the next commmand, so check stop, too */
1458 if (brq->data.error || brq->stop.error) {
1459 if (need_retune && !brq->retune_retry_done) {
1460 pr_debug("%s: retrying because a re-tune was needed\n",
1461 req->rq_disk->disk_name);
1462 brq->retune_retry_done = 1;
1463 return MMC_BLK_RETRY;
1465 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1466 req->rq_disk->disk_name, brq->data.error ?: brq->stop.error,
1467 (unsigned)blk_rq_pos(req),
1468 (unsigned)blk_rq_sectors(req),
1469 brq->cmd.resp[0], brq->stop.resp[0]);
1471 if (rq_data_dir(req) == READ) {
1473 return MMC_BLK_ECC_ERR;
1474 return MMC_BLK_DATA_ERR;
1476 return MMC_BLK_CMD_ERR;
1480 if (!brq->data.bytes_xfered)
1481 return MMC_BLK_RETRY;
1483 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1484 return MMC_BLK_PARTIAL;
1486 return MMC_BLK_SUCCESS;
1489 static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
1490 int disable_multi, bool *do_rel_wr,
1493 struct mmc_blk_data *md = mq->blkdata;
1494 struct mmc_card *card = md->queue.card;
1495 struct mmc_blk_request *brq = &mqrq->brq;
1496 struct request *req = mmc_queue_req_to_req(mqrq);
1499 * Reliable writes are used to implement Forced Unit Access and
1500 * are supported only on MMCs.
1502 *do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1503 rq_data_dir(req) == WRITE &&
1504 (md->flags & MMC_BLK_REL_WR);
1506 memset(brq, 0, sizeof(struct mmc_blk_request));
1508 brq->mrq.data = &brq->data;
1510 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1513 if (rq_data_dir(req) == READ) {
1514 brq->data.flags = MMC_DATA_READ;
1515 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1517 brq->data.flags = MMC_DATA_WRITE;
1518 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1521 brq->data.blksz = 512;
1522 brq->data.blocks = blk_rq_sectors(req);
1525 * The block layer doesn't support all sector count
1526 * restrictions, so we need to be prepared for too big
1529 if (brq->data.blocks > card->host->max_blk_count)
1530 brq->data.blocks = card->host->max_blk_count;
1532 if (brq->data.blocks > 1) {
1534 * After a read error, we redo the request one sector
1535 * at a time in order to accurately determine which
1536 * sectors can be read successfully.
1539 brq->data.blocks = 1;
1542 * Some controllers have HW issues while operating
1543 * in multiple I/O mode
1545 if (card->host->ops->multi_io_quirk)
1546 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1547 (rq_data_dir(req) == READ) ?
1548 MMC_DATA_READ : MMC_DATA_WRITE,
1553 mmc_apply_rel_rw(brq, card, req);
1556 * Data tag is used only during writing meta data to speed
1557 * up write and any subsequent read of this meta data
1559 *do_data_tag = card->ext_csd.data_tag_unit_size &&
1560 (req->cmd_flags & REQ_META) &&
1561 (rq_data_dir(req) == WRITE) &&
1562 ((brq->data.blocks * brq->data.blksz) >=
1563 card->ext_csd.data_tag_unit_size);
1565 mmc_set_data_timeout(&brq->data, card);
1567 brq->data.sg = mqrq->sg;
1568 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1571 * Adjust the sg list so it is the same size as the
1574 if (brq->data.blocks != blk_rq_sectors(req)) {
1575 int i, data_size = brq->data.blocks << 9;
1576 struct scatterlist *sg;
1578 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1579 data_size -= sg->length;
1580 if (data_size <= 0) {
1581 sg->length += data_size;
1586 brq->data.sg_len = i;
1589 mqrq->areq.mrq = &brq->mrq;
1591 mmc_queue_bounce_pre(mqrq);
1594 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1595 struct mmc_card *card,
1597 struct mmc_queue *mq)
1599 u32 readcmd, writecmd;
1600 struct mmc_blk_request *brq = &mqrq->brq;
1601 struct request *req = mmc_queue_req_to_req(mqrq);
1602 struct mmc_blk_data *md = mq->blkdata;
1603 bool do_rel_wr, do_data_tag;
1605 mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1607 brq->mrq.cmd = &brq->cmd;
1609 brq->cmd.arg = blk_rq_pos(req);
1610 if (!mmc_card_blockaddr(card))
1612 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1614 if (brq->data.blocks > 1 || do_rel_wr) {
1615 /* SPI multiblock writes terminate using a special
1616 * token, not a STOP_TRANSMISSION request.
1618 if (!mmc_host_is_spi(card->host) ||
1619 rq_data_dir(req) == READ)
1620 brq->mrq.stop = &brq->stop;
1621 readcmd = MMC_READ_MULTIPLE_BLOCK;
1622 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1624 brq->mrq.stop = NULL;
1625 readcmd = MMC_READ_SINGLE_BLOCK;
1626 writecmd = MMC_WRITE_BLOCK;
1628 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
1631 * Pre-defined multi-block transfers are preferable to
1632 * open ended-ones (and necessary for reliable writes).
1633 * However, it is not sufficient to just send CMD23,
1634 * and avoid the final CMD12, as on an error condition
1635 * CMD12 (stop) needs to be sent anyway. This, coupled
1636 * with Auto-CMD23 enhancements provided by some
1637 * hosts, means that the complexity of dealing
1638 * with this is best left to the host. If CMD23 is
1639 * supported by card and host, we'll fill sbc in and let
1640 * the host deal with handling it correctly. This means
1641 * that for hosts that don't expose MMC_CAP_CMD23, no
1642 * change of behavior will be observed.
1644 * N.B: Some MMC cards experience perf degradation.
1645 * We'll avoid using CMD23-bounded multiblock writes for
1646 * these, while retaining features like reliable writes.
1648 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1649 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1651 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1652 brq->sbc.arg = brq->data.blocks |
1653 (do_rel_wr ? (1 << 31) : 0) |
1654 (do_data_tag ? (1 << 29) : 0);
1655 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1656 brq->mrq.sbc = &brq->sbc;
1659 mqrq->areq.err_check = mmc_blk_err_check;
1662 static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1663 struct mmc_blk_request *brq, struct request *req,
1664 bool old_req_pending)
1669 * If this is an SD card and we're writing, we can first
1670 * mark the known good sectors as ok.
1672 * If the card is not SD, we can still ok written sectors
1673 * as reported by the controller (which might be less than
1674 * the real number of written sectors, but never more).
1676 if (mmc_card_sd(card)) {
1680 err = mmc_sd_num_wr_blocks(card, &blocks);
1682 req_pending = old_req_pending;
1684 req_pending = blk_end_request(req, 0, blocks << 9);
1686 req_pending = blk_end_request(req, 0, brq->data.bytes_xfered);
1691 static void mmc_blk_rw_cmd_abort(struct mmc_queue *mq, struct mmc_card *card,
1692 struct request *req,
1693 struct mmc_queue_req *mqrq)
1695 if (mmc_card_removed(card))
1696 req->rq_flags |= RQF_QUIET;
1697 while (blk_end_request(req, BLK_STS_IOERR, blk_rq_cur_bytes(req)));
1702 * mmc_blk_rw_try_restart() - tries to restart the current async request
1703 * @mq: the queue with the card and host to restart
1704 * @req: a new request that want to be started after the current one
1706 static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req,
1707 struct mmc_queue_req *mqrq)
1713 * If the card was removed, just cancel everything and return.
1715 if (mmc_card_removed(mq->card)) {
1716 req->rq_flags |= RQF_QUIET;
1717 blk_end_request_all(req, BLK_STS_IOERR);
1718 mq->qcnt--; /* FIXME: just set to 0? */
1721 /* Else proceed and try to restart the current async request */
1722 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
1723 mmc_start_areq(mq->card->host, &mqrq->areq, NULL);
1726 static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req)
1728 struct mmc_blk_data *md = mq->blkdata;
1729 struct mmc_card *card = md->queue.card;
1730 struct mmc_blk_request *brq;
1731 int disable_multi = 0, retry = 0, type, retune_retry_done = 0;
1732 enum mmc_blk_status status;
1733 struct mmc_queue_req *mqrq_cur = NULL;
1734 struct mmc_queue_req *mq_rq;
1735 struct request *old_req;
1736 struct mmc_async_req *new_areq;
1737 struct mmc_async_req *old_areq;
1738 bool req_pending = true;
1741 mqrq_cur = req_to_mmc_queue_req(new_req);
1751 * When 4KB native sector is enabled, only 8 blocks
1752 * multiple read or write is allowed
1754 if (mmc_large_sector(card) &&
1755 !IS_ALIGNED(blk_rq_sectors(new_req), 8)) {
1756 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1757 new_req->rq_disk->disk_name);
1758 mmc_blk_rw_cmd_abort(mq, card, new_req, mqrq_cur);
1762 mmc_blk_rw_rq_prep(mqrq_cur, card, 0, mq);
1763 new_areq = &mqrq_cur->areq;
1767 old_areq = mmc_start_areq(card->host, new_areq, &status);
1770 * We have just put the first request into the pipeline
1771 * and there is nothing more to do until it is
1778 * An asynchronous request has been completed and we proceed
1779 * to handle the result of it.
1781 mq_rq = container_of(old_areq, struct mmc_queue_req, areq);
1783 old_req = mmc_queue_req_to_req(mq_rq);
1784 type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1785 mmc_queue_bounce_post(mq_rq);
1788 case MMC_BLK_SUCCESS:
1789 case MMC_BLK_PARTIAL:
1791 * A block was successfully transferred.
1793 mmc_blk_reset_success(md, type);
1795 req_pending = blk_end_request(old_req, BLK_STS_OK,
1796 brq->data.bytes_xfered);
1798 * If the blk_end_request function returns non-zero even
1799 * though all data has been transferred and no errors
1800 * were returned by the host controller, it's a bug.
1802 if (status == MMC_BLK_SUCCESS && req_pending) {
1803 pr_err("%s BUG rq_tot %d d_xfer %d\n",
1804 __func__, blk_rq_bytes(old_req),
1805 brq->data.bytes_xfered);
1806 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1810 case MMC_BLK_CMD_ERR:
1811 req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending);
1812 if (mmc_blk_reset(md, card->host, type)) {
1814 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1817 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
1822 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
1827 retune_retry_done = brq->retune_retry_done;
1832 if (!mmc_blk_reset(md, card->host, type))
1834 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1835 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
1837 case MMC_BLK_DATA_ERR: {
1840 err = mmc_blk_reset(md, card->host, type);
1843 if (err == -ENODEV) {
1844 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1845 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
1850 case MMC_BLK_ECC_ERR:
1851 if (brq->data.blocks > 1) {
1852 /* Redo read one sector at a time */
1853 pr_warn("%s: retrying using single block read\n",
1854 old_req->rq_disk->disk_name);
1859 * After an error, we redo I/O one sector at a
1860 * time, so we only reach here after trying to
1861 * read a single sector.
1863 req_pending = blk_end_request(old_req, BLK_STS_IOERR,
1867 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
1871 case MMC_BLK_NOMEDIUM:
1872 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1873 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
1876 pr_err("%s: Unhandled return value (%d)",
1877 old_req->rq_disk->disk_name, status);
1878 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1879 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
1885 * In case of a incomplete request
1886 * prepare it again and resend.
1888 mmc_blk_rw_rq_prep(mq_rq, card,
1890 mmc_start_areq(card->host,
1891 &mq_rq->areq, NULL);
1892 mq_rq->brq.retune_retry_done = retune_retry_done;
1894 } while (req_pending);
1899 void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1902 struct mmc_blk_data *md = mq->blkdata;
1903 struct mmc_card *card = md->queue.card;
1905 if (req && !mq->qcnt)
1906 /* claim host only for the first request */
1909 ret = mmc_blk_part_switch(card, md);
1912 blk_end_request_all(req, BLK_STS_IOERR);
1918 switch (req_op(req)) {
1920 case REQ_OP_DRV_OUT:
1922 * Complete ongoing async transfer before issuing
1926 mmc_blk_issue_rw_rq(mq, NULL);
1927 mmc_blk_issue_drv_op(mq, req);
1929 case REQ_OP_DISCARD:
1931 * Complete ongoing async transfer before issuing
1935 mmc_blk_issue_rw_rq(mq, NULL);
1936 mmc_blk_issue_discard_rq(mq, req);
1938 case REQ_OP_SECURE_ERASE:
1940 * Complete ongoing async transfer before issuing
1944 mmc_blk_issue_rw_rq(mq, NULL);
1945 mmc_blk_issue_secdiscard_rq(mq, req);
1949 * Complete ongoing async transfer before issuing
1953 mmc_blk_issue_rw_rq(mq, NULL);
1954 mmc_blk_issue_flush(mq, req);
1957 /* Normal request, just issue it */
1958 mmc_blk_issue_rw_rq(mq, req);
1959 card->host->context_info.is_waiting_last_req = false;
1963 /* No request, flushing the pipeline with NULL */
1964 mmc_blk_issue_rw_rq(mq, NULL);
1965 card->host->context_info.is_waiting_last_req = false;
1973 static inline int mmc_blk_readonly(struct mmc_card *card)
1975 return mmc_card_readonly(card) ||
1976 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1979 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1980 struct device *parent,
1983 const char *subname,
1986 struct mmc_blk_data *md;
1989 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
1991 return ERR_PTR(devidx);
1993 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
1999 md->area_type = area_type;
2002 * Set the read-only status based on the supported commands
2003 * and the write protect switch.
2005 md->read_only = mmc_blk_readonly(card);
2007 md->disk = alloc_disk(perdev_minors);
2008 if (md->disk == NULL) {
2013 spin_lock_init(&md->lock);
2014 INIT_LIST_HEAD(&md->part);
2017 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2021 md->queue.blkdata = md;
2023 md->disk->major = MMC_BLOCK_MAJOR;
2024 md->disk->first_minor = devidx * perdev_minors;
2025 md->disk->fops = &mmc_bdops;
2026 md->disk->private_data = md;
2027 md->disk->queue = md->queue.queue;
2028 md->parent = parent;
2029 set_disk_ro(md->disk, md->read_only || default_ro);
2030 md->disk->flags = GENHD_FL_EXT_DEVT;
2031 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2032 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
2035 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2037 * - be set for removable media with permanent block devices
2038 * - be unset for removable block devices with permanent media
2040 * Since MMC block devices clearly fall under the second
2041 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2042 * should use the block device creation/destruction hotplug
2043 * messages to tell when the card is present.
2046 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2047 "mmcblk%u%s", card->host->index, subname ? subname : "");
2049 if (mmc_card_mmc(card))
2050 blk_queue_logical_block_size(md->queue.queue,
2051 card->ext_csd.data_sector_size);
2053 blk_queue_logical_block_size(md->queue.queue, 512);
2055 set_capacity(md->disk, size);
2057 if (mmc_host_cmd23(card->host)) {
2058 if ((mmc_card_mmc(card) &&
2059 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
2060 (mmc_card_sd(card) &&
2061 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2062 md->flags |= MMC_BLK_CMD23;
2065 if (mmc_card_mmc(card) &&
2066 md->flags & MMC_BLK_CMD23 &&
2067 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2068 card->ext_csd.rel_sectors)) {
2069 md->flags |= MMC_BLK_REL_WR;
2070 blk_queue_write_cache(md->queue.queue, true, true);
2080 ida_simple_remove(&mmc_blk_ida, devidx);
2081 return ERR_PTR(ret);
2084 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2088 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2090 * The EXT_CSD sector count is in number or 512 byte
2093 size = card->ext_csd.sectors;
2096 * The CSD capacity field is in units of read_blkbits.
2097 * set_capacity takes units of 512 bytes.
2099 size = (typeof(sector_t))card->csd.capacity
2100 << (card->csd.read_blkbits - 9);
2103 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2104 MMC_BLK_DATA_AREA_MAIN);
2107 static int mmc_blk_alloc_part(struct mmc_card *card,
2108 struct mmc_blk_data *md,
2109 unsigned int part_type,
2112 const char *subname,
2116 struct mmc_blk_data *part_md;
2118 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2119 subname, area_type);
2120 if (IS_ERR(part_md))
2121 return PTR_ERR(part_md);
2122 part_md->part_type = part_type;
2123 list_add(&part_md->part, &md->part);
2125 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
2126 cap_str, sizeof(cap_str));
2127 pr_info("%s: %s %s partition %u %s\n",
2128 part_md->disk->disk_name, mmc_card_id(card),
2129 mmc_card_name(card), part_md->part_type, cap_str);
2133 /* MMC Physical partitions consist of two boot partitions and
2134 * up to four general purpose partitions.
2135 * For each partition enabled in EXT_CSD a block device will be allocatedi
2136 * to provide access to the partition.
2139 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2143 if (!mmc_card_mmc(card))
2146 for (idx = 0; idx < card->nr_parts; idx++) {
2147 if (card->part[idx].size) {
2148 ret = mmc_blk_alloc_part(card, md,
2149 card->part[idx].part_cfg,
2150 card->part[idx].size >> 9,
2151 card->part[idx].force_ro,
2152 card->part[idx].name,
2153 card->part[idx].area_type);
2162 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2164 struct mmc_card *card;
2168 * Flush remaining requests and free queues. It
2169 * is freeing the queue that stops new requests
2170 * from being accepted.
2172 card = md->queue.card;
2173 queue_flag_set(QUEUE_FLAG_BYPASS, md->queue.queue);
2174 blk_set_queue_dying(md->queue.queue);
2175 mmc_cleanup_queue(&md->queue);
2176 if (md->disk->flags & GENHD_FL_UP) {
2177 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2178 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2179 card->ext_csd.boot_ro_lockable)
2180 device_remove_file(disk_to_dev(md->disk),
2181 &md->power_ro_lock);
2183 del_gendisk(md->disk);
2189 static void mmc_blk_remove_parts(struct mmc_card *card,
2190 struct mmc_blk_data *md)
2192 struct list_head *pos, *q;
2193 struct mmc_blk_data *part_md;
2195 list_for_each_safe(pos, q, &md->part) {
2196 part_md = list_entry(pos, struct mmc_blk_data, part);
2198 mmc_blk_remove_req(part_md);
2202 static int mmc_add_disk(struct mmc_blk_data *md)
2205 struct mmc_card *card = md->queue.card;
2207 device_add_disk(md->parent, md->disk);
2208 md->force_ro.show = force_ro_show;
2209 md->force_ro.store = force_ro_store;
2210 sysfs_attr_init(&md->force_ro.attr);
2211 md->force_ro.attr.name = "force_ro";
2212 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2213 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2217 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2218 card->ext_csd.boot_ro_lockable) {
2221 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2224 mode = S_IRUGO | S_IWUSR;
2226 md->power_ro_lock.show = power_ro_lock_show;
2227 md->power_ro_lock.store = power_ro_lock_store;
2228 sysfs_attr_init(&md->power_ro_lock.attr);
2229 md->power_ro_lock.attr.mode = mode;
2230 md->power_ro_lock.attr.name =
2231 "ro_lock_until_next_power_on";
2232 ret = device_create_file(disk_to_dev(md->disk),
2233 &md->power_ro_lock);
2235 goto power_ro_lock_fail;
2240 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2242 del_gendisk(md->disk);
2247 static int mmc_blk_probe(struct mmc_card *card)
2249 struct mmc_blk_data *md, *part_md;
2253 * Check that the card supports the command class(es) we need.
2255 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2258 mmc_fixup_device(card, mmc_blk_fixups);
2260 md = mmc_blk_alloc(card);
2264 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
2265 cap_str, sizeof(cap_str));
2266 pr_info("%s: %s %s %s %s\n",
2267 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2268 cap_str, md->read_only ? "(ro)" : "");
2270 if (mmc_blk_alloc_parts(card, md))
2273 dev_set_drvdata(&card->dev, md);
2275 if (mmc_add_disk(md))
2278 list_for_each_entry(part_md, &md->part, part) {
2279 if (mmc_add_disk(part_md))
2283 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2284 pm_runtime_use_autosuspend(&card->dev);
2287 * Don't enable runtime PM for SD-combo cards here. Leave that
2288 * decision to be taken during the SDIO init sequence instead.
2290 if (card->type != MMC_TYPE_SD_COMBO) {
2291 pm_runtime_set_active(&card->dev);
2292 pm_runtime_enable(&card->dev);
2298 mmc_blk_remove_parts(card, md);
2299 mmc_blk_remove_req(md);
2303 static void mmc_blk_remove(struct mmc_card *card)
2305 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2307 mmc_blk_remove_parts(card, md);
2308 pm_runtime_get_sync(&card->dev);
2309 mmc_claim_host(card->host);
2310 mmc_blk_part_switch(card, md);
2311 mmc_release_host(card->host);
2312 if (card->type != MMC_TYPE_SD_COMBO)
2313 pm_runtime_disable(&card->dev);
2314 pm_runtime_put_noidle(&card->dev);
2315 mmc_blk_remove_req(md);
2316 dev_set_drvdata(&card->dev, NULL);
2319 static int _mmc_blk_suspend(struct mmc_card *card)
2321 struct mmc_blk_data *part_md;
2322 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2325 mmc_queue_suspend(&md->queue);
2326 list_for_each_entry(part_md, &md->part, part) {
2327 mmc_queue_suspend(&part_md->queue);
2333 static void mmc_blk_shutdown(struct mmc_card *card)
2335 _mmc_blk_suspend(card);
2338 #ifdef CONFIG_PM_SLEEP
2339 static int mmc_blk_suspend(struct device *dev)
2341 struct mmc_card *card = mmc_dev_to_card(dev);
2343 return _mmc_blk_suspend(card);
2346 static int mmc_blk_resume(struct device *dev)
2348 struct mmc_blk_data *part_md;
2349 struct mmc_blk_data *md = dev_get_drvdata(dev);
2353 * Resume involves the card going into idle state,
2354 * so current partition is always the main one.
2356 md->part_curr = md->part_type;
2357 mmc_queue_resume(&md->queue);
2358 list_for_each_entry(part_md, &md->part, part) {
2359 mmc_queue_resume(&part_md->queue);
2366 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2368 static struct mmc_driver mmc_driver = {
2371 .pm = &mmc_blk_pm_ops,
2373 .probe = mmc_blk_probe,
2374 .remove = mmc_blk_remove,
2375 .shutdown = mmc_blk_shutdown,
2378 static int __init mmc_blk_init(void)
2382 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2383 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2385 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
2387 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2391 res = mmc_register_driver(&mmc_driver);
2397 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2402 static void __exit mmc_blk_exit(void)
2404 mmc_unregister_driver(&mmc_driver);
2405 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2408 module_init(mmc_blk_init);
2409 module_exit(mmc_blk_exit);
2411 MODULE_LICENSE("GPL");
2412 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");