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/cdev.h>
32 #include <linux/mutex.h>
33 #include <linux/scatterlist.h>
34 #include <linux/string_helpers.h>
35 #include <linux/delay.h>
36 #include <linux/capability.h>
37 #include <linux/compat.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/idr.h>
40 #include <linux/debugfs.h>
42 #include <linux/mmc/ioctl.h>
43 #include <linux/mmc/card.h>
44 #include <linux/mmc/host.h>
45 #include <linux/mmc/mmc.h>
46 #include <linux/mmc/sd.h>
48 #include <linux/uaccess.h>
60 MODULE_ALIAS("mmc:block");
61 #ifdef MODULE_PARAM_PREFIX
62 #undef MODULE_PARAM_PREFIX
64 #define MODULE_PARAM_PREFIX "mmcblk."
67 * Set a 10 second timeout for polling write request busy state. Note, mmc core
68 * is setting a 3 second timeout for SD cards, and SDHCI has long had a 10
69 * second software timer to timeout the whole request, so 10 seconds should be
72 #define MMC_BLK_TIMEOUT_MS (10 * 1000)
73 #define MMC_SANITIZE_REQ_TIMEOUT 240000
74 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
75 #define MMC_EXTRACT_VALUE_FROM_ARG(x) ((x & 0x0000FF00) >> 8)
77 #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
78 (rq_data_dir(req) == WRITE))
79 static DEFINE_MUTEX(block_mutex);
82 * The defaults come from config options but can be overriden by module
85 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
88 * We've only got one major, so number of mmcblk devices is
89 * limited to (1 << 20) / number of minors per device. It is also
90 * limited by the MAX_DEVICES below.
92 static int max_devices;
94 #define MAX_DEVICES 256
96 static DEFINE_IDA(mmc_blk_ida);
97 static DEFINE_IDA(mmc_rpmb_ida);
100 * There is one mmc_blk_data per slot.
102 struct mmc_blk_data {
104 struct device *parent;
105 struct gendisk *disk;
106 struct mmc_queue queue;
107 struct list_head part;
108 struct list_head rpmbs;
111 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
112 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
115 unsigned int read_only;
116 unsigned int part_type;
117 unsigned int reset_done;
118 #define MMC_BLK_READ BIT(0)
119 #define MMC_BLK_WRITE BIT(1)
120 #define MMC_BLK_DISCARD BIT(2)
121 #define MMC_BLK_SECDISCARD BIT(3)
122 #define MMC_BLK_CQE_RECOVERY BIT(4)
125 * Only set in main mmc_blk_data associated
126 * with mmc_card with dev_set_drvdata, and keeps
127 * track of the current selected device partition.
129 unsigned int part_curr;
130 struct device_attribute force_ro;
131 struct device_attribute power_ro_lock;
134 /* debugfs files (only in main mmc_blk_data) */
135 struct dentry *status_dentry;
136 struct dentry *ext_csd_dentry;
139 /* Device type for RPMB character devices */
140 static dev_t mmc_rpmb_devt;
142 /* Bus type for RPMB character devices */
143 static struct bus_type mmc_rpmb_bus_type = {
148 * struct mmc_rpmb_data - special RPMB device type for these areas
149 * @dev: the device for the RPMB area
150 * @chrdev: character device for the RPMB area
151 * @id: unique device ID number
152 * @part_index: partition index (0 on first)
153 * @md: parent MMC block device
154 * @node: list item, so we can put this device on a list
156 struct mmc_rpmb_data {
160 unsigned int part_index;
161 struct mmc_blk_data *md;
162 struct list_head node;
165 static DEFINE_MUTEX(open_lock);
167 module_param(perdev_minors, int, 0444);
168 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
170 static inline int mmc_blk_part_switch(struct mmc_card *card,
171 unsigned int part_type);
173 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
175 struct mmc_blk_data *md;
177 mutex_lock(&open_lock);
178 md = disk->private_data;
179 if (md && md->usage == 0)
183 mutex_unlock(&open_lock);
188 static inline int mmc_get_devidx(struct gendisk *disk)
190 int devidx = disk->first_minor / perdev_minors;
194 static void mmc_blk_put(struct mmc_blk_data *md)
196 mutex_lock(&open_lock);
198 if (md->usage == 0) {
199 int devidx = mmc_get_devidx(md->disk);
200 blk_put_queue(md->queue.queue);
201 ida_simple_remove(&mmc_blk_ida, devidx);
205 mutex_unlock(&open_lock);
208 static ssize_t power_ro_lock_show(struct device *dev,
209 struct device_attribute *attr, char *buf)
212 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
213 struct mmc_card *card = md->queue.card;
216 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
218 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
221 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
228 static ssize_t power_ro_lock_store(struct device *dev,
229 struct device_attribute *attr, const char *buf, size_t count)
232 struct mmc_blk_data *md, *part_md;
233 struct mmc_queue *mq;
237 if (kstrtoul(buf, 0, &set))
243 md = mmc_blk_get(dev_to_disk(dev));
246 /* Dispatch locking to the block layer */
247 req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, 0);
249 count = PTR_ERR(req);
252 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
253 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
254 blk_execute_rq(mq->queue, NULL, req, 0);
255 ret = req_to_mmc_queue_req(req)->drv_op_result;
256 blk_put_request(req);
259 pr_info("%s: Locking boot partition ro until next power on\n",
260 md->disk->disk_name);
261 set_disk_ro(md->disk, 1);
263 list_for_each_entry(part_md, &md->part, part)
264 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
265 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
266 set_disk_ro(part_md->disk, 1);
274 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
278 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
280 ret = snprintf(buf, PAGE_SIZE, "%d\n",
281 get_disk_ro(dev_to_disk(dev)) ^
287 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
288 const char *buf, size_t count)
292 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
293 unsigned long set = simple_strtoul(buf, &end, 0);
299 set_disk_ro(dev_to_disk(dev), set || md->read_only);
306 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
308 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
311 mutex_lock(&block_mutex);
314 check_disk_change(bdev);
317 if ((mode & FMODE_WRITE) && md->read_only) {
322 mutex_unlock(&block_mutex);
327 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
329 struct mmc_blk_data *md = disk->private_data;
331 mutex_lock(&block_mutex);
333 mutex_unlock(&block_mutex);
337 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
339 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
345 struct mmc_blk_ioc_data {
346 struct mmc_ioc_cmd ic;
349 struct mmc_rpmb_data *rpmb;
352 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
353 struct mmc_ioc_cmd __user *user)
355 struct mmc_blk_ioc_data *idata;
358 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
364 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
369 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
370 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
375 if (!idata->buf_bytes) {
380 idata->buf = memdup_user((void __user *)(unsigned long)
381 idata->ic.data_ptr, idata->buf_bytes);
382 if (IS_ERR(idata->buf)) {
383 err = PTR_ERR(idata->buf);
395 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
396 struct mmc_blk_ioc_data *idata)
398 struct mmc_ioc_cmd *ic = &idata->ic;
400 if (copy_to_user(&(ic_ptr->response), ic->response,
401 sizeof(ic->response)))
404 if (!idata->ic.write_flag) {
405 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
406 idata->buf, idata->buf_bytes))
413 static int ioctl_do_sanitize(struct mmc_card *card)
417 if (!mmc_can_sanitize(card)) {
418 pr_warn("%s: %s - SANITIZE is not supported\n",
419 mmc_hostname(card->host), __func__);
424 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
425 mmc_hostname(card->host), __func__);
427 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
428 EXT_CSD_SANITIZE_START, 1,
429 MMC_SANITIZE_REQ_TIMEOUT);
432 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
433 mmc_hostname(card->host), __func__, err);
435 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
441 static inline bool mmc_blk_in_tran_state(u32 status)
444 * Some cards mishandle the status bits, so make sure to check both the
445 * busy indication and the card state.
447 return status & R1_READY_FOR_DATA &&
448 (R1_CURRENT_STATE(status) == R1_STATE_TRAN);
451 static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
454 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
459 bool done = time_after(jiffies, timeout);
461 err = __mmc_send_status(card, &status, 5);
463 dev_err(mmc_dev(card->host),
464 "error %d requesting status\n", err);
468 /* Accumulate any response error bits seen */
470 *resp_errs |= status;
473 * Timeout if the device never becomes ready for data and never
474 * leaves the program state.
477 dev_err(mmc_dev(card->host),
478 "Card stuck in wrong state! %s status: %#x\n",
484 * Some cards mishandle the status bits,
485 * so make sure to check both the busy
486 * indication and the card state.
488 } while (!mmc_blk_in_tran_state(status));
493 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
494 struct mmc_blk_ioc_data *idata)
496 struct mmc_command cmd = {}, sbc = {};
497 struct mmc_data data = {};
498 struct mmc_request mrq = {};
499 struct scatterlist sg;
501 unsigned int target_part;
503 if (!card || !md || !idata)
507 * The RPMB accesses comes in from the character device, so we
508 * need to target these explicitly. Else we just target the
509 * partition type for the block device the ioctl() was issued
513 /* Support multiple RPMB partitions */
514 target_part = idata->rpmb->part_index;
515 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
517 target_part = md->part_type;
520 cmd.opcode = idata->ic.opcode;
521 cmd.arg = idata->ic.arg;
522 cmd.flags = idata->ic.flags;
524 if (idata->buf_bytes) {
527 data.blksz = idata->ic.blksz;
528 data.blocks = idata->ic.blocks;
530 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
532 if (idata->ic.write_flag)
533 data.flags = MMC_DATA_WRITE;
535 data.flags = MMC_DATA_READ;
537 /* data.flags must already be set before doing this. */
538 mmc_set_data_timeout(&data, card);
540 /* Allow overriding the timeout_ns for empirical tuning. */
541 if (idata->ic.data_timeout_ns)
542 data.timeout_ns = idata->ic.data_timeout_ns;
544 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
546 * Pretend this is a data transfer and rely on the
547 * host driver to compute timeout. When all host
548 * drivers support cmd.cmd_timeout for R1B, this
552 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
554 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
562 err = mmc_blk_part_switch(card, target_part);
566 if (idata->ic.is_acmd) {
567 err = mmc_app_cmd(card->host, card);
573 sbc.opcode = MMC_SET_BLOCK_COUNT;
575 * We don't do any blockcount validation because the max size
576 * may be increased by a future standard. We just copy the
577 * 'Reliable Write' bit here.
579 sbc.arg = data.blocks | (idata->ic.write_flag & BIT(31));
580 sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
584 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
585 (cmd.opcode == MMC_SWITCH)) {
586 err = ioctl_do_sanitize(card);
589 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
595 mmc_wait_for_req(card->host, &mrq);
596 memcpy(&idata->ic.response, cmd.resp, sizeof(cmd.resp));
599 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
600 __func__, cmd.error);
604 dev_err(mmc_dev(card->host), "%s: data error %d\n",
605 __func__, data.error);
610 * Make sure the cache of the PARTITION_CONFIG register and
611 * PARTITION_ACCESS bits is updated in case the ioctl ext_csd write
612 * changed it successfully.
614 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_PART_CONFIG) &&
615 (cmd.opcode == MMC_SWITCH)) {
616 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
617 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg);
620 * Update cache so the next mmc_blk_part_switch call operates
621 * on up-to-date data.
623 card->ext_csd.part_config = value;
624 main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
628 * Make sure to update CACHE_CTRL in case it was changed. The cache
629 * will get turned back on if the card is re-initialized, e.g.
630 * suspend/resume or hw reset in recovery.
632 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_CACHE_CTRL) &&
633 (cmd.opcode == MMC_SWITCH)) {
634 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg) & 1;
636 card->ext_csd.cache_ctrl = value;
640 * According to the SD specs, some commands require a delay after
641 * issuing the command.
643 if (idata->ic.postsleep_min_us)
644 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
646 if (idata->rpmb || (cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
648 * Ensure RPMB/R1B command has completed by polling CMD13
651 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, NULL);
657 static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
658 struct mmc_ioc_cmd __user *ic_ptr,
659 struct mmc_rpmb_data *rpmb)
661 struct mmc_blk_ioc_data *idata;
662 struct mmc_blk_ioc_data *idatas[1];
663 struct mmc_queue *mq;
664 struct mmc_card *card;
665 int err = 0, ioc_err = 0;
668 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
670 return PTR_ERR(idata);
671 /* This will be NULL on non-RPMB ioctl():s */
674 card = md->queue.card;
681 * Dispatch the ioctl() into the block request queue.
684 req = blk_get_request(mq->queue,
685 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
691 req_to_mmc_queue_req(req)->drv_op =
692 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
693 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
694 req_to_mmc_queue_req(req)->drv_op_data = idatas;
695 req_to_mmc_queue_req(req)->ioc_count = 1;
696 blk_execute_rq(mq->queue, NULL, req, 0);
697 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
698 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
699 blk_put_request(req);
704 return ioc_err ? ioc_err : err;
707 static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
708 struct mmc_ioc_multi_cmd __user *user,
709 struct mmc_rpmb_data *rpmb)
711 struct mmc_blk_ioc_data **idata = NULL;
712 struct mmc_ioc_cmd __user *cmds = user->cmds;
713 struct mmc_card *card;
714 struct mmc_queue *mq;
715 int i, err = 0, ioc_err = 0;
719 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
720 sizeof(num_of_cmds)))
726 if (num_of_cmds > MMC_IOC_MAX_CMDS)
729 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
733 for (i = 0; i < num_of_cmds; i++) {
734 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
735 if (IS_ERR(idata[i])) {
736 err = PTR_ERR(idata[i]);
740 /* This will be NULL on non-RPMB ioctl():s */
741 idata[i]->rpmb = rpmb;
744 card = md->queue.card;
752 * Dispatch the ioctl()s into the block request queue.
755 req = blk_get_request(mq->queue,
756 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
761 req_to_mmc_queue_req(req)->drv_op =
762 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
763 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
764 req_to_mmc_queue_req(req)->drv_op_data = idata;
765 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
766 blk_execute_rq(mq->queue, NULL, req, 0);
767 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
769 /* copy to user if data and response */
770 for (i = 0; i < num_of_cmds && !err; i++)
771 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
773 blk_put_request(req);
776 for (i = 0; i < num_of_cmds; i++) {
777 kfree(idata[i]->buf);
781 return ioc_err ? ioc_err : err;
784 static int mmc_blk_check_blkdev(struct block_device *bdev)
787 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
788 * whole block device, not on a partition. This prevents overspray
789 * between sibling partitions.
791 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
796 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
797 unsigned int cmd, unsigned long arg)
799 struct mmc_blk_data *md;
804 ret = mmc_blk_check_blkdev(bdev);
807 md = mmc_blk_get(bdev->bd_disk);
810 ret = mmc_blk_ioctl_cmd(md,
811 (struct mmc_ioc_cmd __user *)arg,
815 case MMC_IOC_MULTI_CMD:
816 ret = mmc_blk_check_blkdev(bdev);
819 md = mmc_blk_get(bdev->bd_disk);
822 ret = mmc_blk_ioctl_multi_cmd(md,
823 (struct mmc_ioc_multi_cmd __user *)arg,
833 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
834 unsigned int cmd, unsigned long arg)
836 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
840 static const struct block_device_operations mmc_bdops = {
841 .open = mmc_blk_open,
842 .release = mmc_blk_release,
843 .getgeo = mmc_blk_getgeo,
844 .owner = THIS_MODULE,
845 .ioctl = mmc_blk_ioctl,
847 .compat_ioctl = mmc_blk_compat_ioctl,
851 static int mmc_blk_part_switch_pre(struct mmc_card *card,
852 unsigned int part_type)
856 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
857 if (card->ext_csd.cmdq_en) {
858 ret = mmc_cmdq_disable(card);
862 mmc_retune_pause(card->host);
868 static int mmc_blk_part_switch_post(struct mmc_card *card,
869 unsigned int part_type)
873 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
874 mmc_retune_unpause(card->host);
875 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
876 ret = mmc_cmdq_enable(card);
882 static inline int mmc_blk_part_switch(struct mmc_card *card,
883 unsigned int part_type)
886 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
888 if (main_md->part_curr == part_type)
891 if (mmc_card_mmc(card)) {
892 u8 part_config = card->ext_csd.part_config;
894 ret = mmc_blk_part_switch_pre(card, part_type);
898 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
899 part_config |= part_type;
901 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
902 EXT_CSD_PART_CONFIG, part_config,
903 card->ext_csd.part_time);
905 mmc_blk_part_switch_post(card, part_type);
909 card->ext_csd.part_config = part_config;
911 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
914 main_md->part_curr = part_type;
918 static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
924 struct mmc_request mrq = {};
925 struct mmc_command cmd = {};
926 struct mmc_data data = {};
928 struct scatterlist sg;
930 cmd.opcode = MMC_APP_CMD;
931 cmd.arg = card->rca << 16;
932 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
934 err = mmc_wait_for_cmd(card->host, &cmd, 0);
937 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
940 memset(&cmd, 0, sizeof(struct mmc_command));
942 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
944 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
948 data.flags = MMC_DATA_READ;
951 mmc_set_data_timeout(&data, card);
956 blocks = kmalloc(4, GFP_KERNEL);
960 sg_init_one(&sg, blocks, 4);
962 mmc_wait_for_req(card->host, &mrq);
964 result = ntohl(*blocks);
967 if (cmd.error || data.error)
970 *written_blocks = result;
975 static unsigned int mmc_blk_clock_khz(struct mmc_host *host)
977 if (host->actual_clock)
978 return host->actual_clock / 1000;
980 /* Clock may be subject to a divisor, fudge it by a factor of 2. */
982 return host->ios.clock / 2000;
984 /* How can there be no clock */
986 return 100; /* 100 kHz is minimum possible value */
989 static unsigned int mmc_blk_data_timeout_ms(struct mmc_host *host,
990 struct mmc_data *data)
992 unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000);
995 if (data->timeout_clks) {
996 khz = mmc_blk_clock_khz(host);
997 ms += DIV_ROUND_UP(data->timeout_clks, khz);
1003 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1008 if (md->reset_done & type)
1011 md->reset_done |= type;
1012 err = mmc_hw_reset(host);
1013 /* Ensure we switch back to the correct partition */
1014 if (err != -EOPNOTSUPP) {
1015 struct mmc_blk_data *main_md =
1016 dev_get_drvdata(&host->card->dev);
1019 main_md->part_curr = main_md->part_type;
1020 part_err = mmc_blk_part_switch(host->card, md->part_type);
1023 * We have failed to get back into the correct
1024 * partition, so we need to abort the whole request.
1032 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1034 md->reset_done &= ~type;
1038 * The non-block commands come back from the block layer after it queued it and
1039 * processed it with all other requests and then they get issued in this
1042 static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1044 struct mmc_queue_req *mq_rq;
1045 struct mmc_card *card = mq->card;
1046 struct mmc_blk_data *md = mq->blkdata;
1047 struct mmc_blk_ioc_data **idata;
1054 mq_rq = req_to_mmc_queue_req(req);
1055 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
1057 switch (mq_rq->drv_op) {
1058 case MMC_DRV_OP_IOCTL:
1059 if (card->ext_csd.cmdq_en) {
1060 ret = mmc_cmdq_disable(card);
1065 case MMC_DRV_OP_IOCTL_RPMB:
1066 idata = mq_rq->drv_op_data;
1067 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
1068 ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
1072 /* Always switch back to main area after RPMB access */
1074 mmc_blk_part_switch(card, 0);
1075 else if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
1076 mmc_cmdq_enable(card);
1078 case MMC_DRV_OP_BOOT_WP:
1079 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1080 card->ext_csd.boot_ro_lock |
1081 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1082 card->ext_csd.part_time);
1084 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1085 md->disk->disk_name, ret);
1087 card->ext_csd.boot_ro_lock |=
1088 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
1090 case MMC_DRV_OP_GET_CARD_STATUS:
1091 ret = mmc_send_status(card, &status);
1095 case MMC_DRV_OP_GET_EXT_CSD:
1096 ext_csd = mq_rq->drv_op_data;
1097 ret = mmc_get_ext_csd(card, ext_csd);
1100 pr_err("%s: unknown driver specific operation\n",
1101 md->disk->disk_name);
1105 mq_rq->drv_op_result = ret;
1106 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1109 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1111 struct mmc_blk_data *md = mq->blkdata;
1112 struct mmc_card *card = md->queue.card;
1113 unsigned int from, nr, arg;
1114 int err = 0, type = MMC_BLK_DISCARD;
1115 blk_status_t status = BLK_STS_OK;
1117 if (!mmc_can_erase(card)) {
1118 status = BLK_STS_NOTSUPP;
1122 from = blk_rq_pos(req);
1123 nr = blk_rq_sectors(req);
1125 if (mmc_can_discard(card))
1126 arg = MMC_DISCARD_ARG;
1127 else if (mmc_can_trim(card))
1130 arg = MMC_ERASE_ARG;
1133 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1134 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1135 INAND_CMD38_ARG_EXT_CSD,
1136 arg == MMC_TRIM_ARG ?
1137 INAND_CMD38_ARG_TRIM :
1138 INAND_CMD38_ARG_ERASE,
1139 card->ext_csd.generic_cmd6_time);
1142 err = mmc_erase(card, from, nr, arg);
1143 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
1145 status = BLK_STS_IOERR;
1147 mmc_blk_reset_success(md, type);
1149 blk_mq_end_request(req, status);
1152 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1153 struct request *req)
1155 struct mmc_blk_data *md = mq->blkdata;
1156 struct mmc_card *card = md->queue.card;
1157 unsigned int from, nr, arg;
1158 int err = 0, type = MMC_BLK_SECDISCARD;
1159 blk_status_t status = BLK_STS_OK;
1161 if (!(mmc_can_secure_erase_trim(card))) {
1162 status = BLK_STS_NOTSUPP;
1166 from = blk_rq_pos(req);
1167 nr = blk_rq_sectors(req);
1169 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1170 arg = MMC_SECURE_TRIM1_ARG;
1172 arg = MMC_SECURE_ERASE_ARG;
1175 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1176 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1177 INAND_CMD38_ARG_EXT_CSD,
1178 arg == MMC_SECURE_TRIM1_ARG ?
1179 INAND_CMD38_ARG_SECTRIM1 :
1180 INAND_CMD38_ARG_SECERASE,
1181 card->ext_csd.generic_cmd6_time);
1186 err = mmc_erase(card, from, nr, arg);
1190 status = BLK_STS_IOERR;
1194 if (arg == MMC_SECURE_TRIM1_ARG) {
1195 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1196 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1197 INAND_CMD38_ARG_EXT_CSD,
1198 INAND_CMD38_ARG_SECTRIM2,
1199 card->ext_csd.generic_cmd6_time);
1204 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1208 status = BLK_STS_IOERR;
1214 if (err && !mmc_blk_reset(md, card->host, type))
1217 mmc_blk_reset_success(md, type);
1219 blk_mq_end_request(req, status);
1222 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1224 struct mmc_blk_data *md = mq->blkdata;
1225 struct mmc_card *card = md->queue.card;
1228 ret = mmc_flush_cache(card);
1229 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1233 * Reformat current write as a reliable write, supporting
1234 * both legacy and the enhanced reliable write MMC cards.
1235 * In each transfer we'll handle only as much as a single
1236 * reliable write can handle, thus finish the request in
1237 * partial completions.
1239 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1240 struct mmc_card *card,
1241 struct request *req)
1243 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1244 /* Legacy mode imposes restrictions on transfers. */
1245 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
1246 brq->data.blocks = 1;
1248 if (brq->data.blocks > card->ext_csd.rel_sectors)
1249 brq->data.blocks = card->ext_csd.rel_sectors;
1250 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1251 brq->data.blocks = 1;
1255 #define CMD_ERRORS_EXCL_OOR \
1256 (R1_ADDRESS_ERROR | /* Misaligned address */ \
1257 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1258 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1259 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
1260 R1_CC_ERROR | /* Card controller error */ \
1261 R1_ERROR) /* General/unknown error */
1263 #define CMD_ERRORS \
1264 (CMD_ERRORS_EXCL_OOR | \
1265 R1_OUT_OF_RANGE) /* Command argument out of range */ \
1267 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
1272 * Per the SD specification(physical layer version 4.10)[1],
1273 * section 4.3.3, it explicitly states that "When the last
1274 * block of user area is read using CMD18, the host should
1275 * ignore OUT_OF_RANGE error that may occur even the sequence
1276 * is correct". And JESD84-B51 for eMMC also has a similar
1277 * statement on section 6.8.3.
1279 * Multiple block read/write could be done by either predefined
1280 * method, namely CMD23, or open-ending mode. For open-ending mode,
1281 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1283 * However the spec[1] doesn't tell us whether we should also
1284 * ignore that for predefined method. But per the spec[1], section
1285 * 4.15 Set Block Count Command, it says"If illegal block count
1286 * is set, out of range error will be indicated during read/write
1287 * operation (For example, data transfer is stopped at user area
1288 * boundary)." In another word, we could expect a out of range error
1289 * in the response for the following CMD18/25. And if argument of
1290 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1291 * we could also expect to get a -ETIMEDOUT or any error number from
1292 * the host drivers due to missing data response(for write)/data(for
1293 * read), as the cards will stop the data transfer by itself per the
1294 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1297 if (!brq->stop.error) {
1298 bool oor_with_open_end;
1299 /* If there is no error yet, check R1 response */
1301 val = brq->stop.resp[0] & CMD_ERRORS;
1302 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1304 if (val && !oor_with_open_end)
1305 brq->stop.error = -EIO;
1309 static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
1310 int disable_multi, bool *do_rel_wr_p,
1311 bool *do_data_tag_p)
1313 struct mmc_blk_data *md = mq->blkdata;
1314 struct mmc_card *card = md->queue.card;
1315 struct mmc_blk_request *brq = &mqrq->brq;
1316 struct request *req = mmc_queue_req_to_req(mqrq);
1317 bool do_rel_wr, do_data_tag;
1320 * Reliable writes are used to implement Forced Unit Access and
1321 * are supported only on MMCs.
1323 do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1324 rq_data_dir(req) == WRITE &&
1325 (md->flags & MMC_BLK_REL_WR);
1327 memset(brq, 0, sizeof(struct mmc_blk_request));
1329 brq->mrq.data = &brq->data;
1330 brq->mrq.tag = req->tag;
1332 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1335 if (rq_data_dir(req) == READ) {
1336 brq->data.flags = MMC_DATA_READ;
1337 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1339 brq->data.flags = MMC_DATA_WRITE;
1340 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1343 brq->data.blksz = 512;
1344 brq->data.blocks = blk_rq_sectors(req);
1345 brq->data.blk_addr = blk_rq_pos(req);
1348 * The command queue supports 2 priorities: "high" (1) and "simple" (0).
1349 * The eMMC will give "high" priority tasks priority over "simple"
1350 * priority tasks. Here we always set "simple" priority by not setting
1355 * The block layer doesn't support all sector count
1356 * restrictions, so we need to be prepared for too big
1359 if (brq->data.blocks > card->host->max_blk_count)
1360 brq->data.blocks = card->host->max_blk_count;
1362 if (brq->data.blocks > 1) {
1364 * Some SD cards in SPI mode return a CRC error or even lock up
1365 * completely when trying to read the last block using a
1366 * multiblock read command.
1368 if (mmc_host_is_spi(card->host) && (rq_data_dir(req) == READ) &&
1369 (blk_rq_pos(req) + blk_rq_sectors(req) ==
1370 get_capacity(md->disk)))
1374 * After a read error, we redo the request one sector
1375 * at a time in order to accurately determine which
1376 * sectors can be read successfully.
1379 brq->data.blocks = 1;
1382 * Some controllers have HW issues while operating
1383 * in multiple I/O mode
1385 if (card->host->ops->multi_io_quirk)
1386 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1387 (rq_data_dir(req) == READ) ?
1388 MMC_DATA_READ : MMC_DATA_WRITE,
1393 mmc_apply_rel_rw(brq, card, req);
1394 brq->data.flags |= MMC_DATA_REL_WR;
1398 * Data tag is used only during writing meta data to speed
1399 * up write and any subsequent read of this meta data
1401 do_data_tag = card->ext_csd.data_tag_unit_size &&
1402 (req->cmd_flags & REQ_META) &&
1403 (rq_data_dir(req) == WRITE) &&
1404 ((brq->data.blocks * brq->data.blksz) >=
1405 card->ext_csd.data_tag_unit_size);
1408 brq->data.flags |= MMC_DATA_DAT_TAG;
1410 mmc_set_data_timeout(&brq->data, card);
1412 brq->data.sg = mqrq->sg;
1413 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1416 * Adjust the sg list so it is the same size as the
1419 if (brq->data.blocks != blk_rq_sectors(req)) {
1420 int i, data_size = brq->data.blocks << 9;
1421 struct scatterlist *sg;
1423 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1424 data_size -= sg->length;
1425 if (data_size <= 0) {
1426 sg->length += data_size;
1431 brq->data.sg_len = i;
1435 *do_rel_wr_p = do_rel_wr;
1438 *do_data_tag_p = do_data_tag;
1441 #define MMC_CQE_RETRIES 2
1443 static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req)
1445 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1446 struct mmc_request *mrq = &mqrq->brq.mrq;
1447 struct request_queue *q = req->q;
1448 struct mmc_host *host = mq->card->host;
1449 enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
1450 unsigned long flags;
1454 mmc_cqe_post_req(host, mrq);
1456 if (mrq->cmd && mrq->cmd->error)
1457 err = mrq->cmd->error;
1458 else if (mrq->data && mrq->data->error)
1459 err = mrq->data->error;
1464 if (mqrq->retries++ < MMC_CQE_RETRIES)
1465 blk_mq_requeue_request(req, true);
1467 blk_mq_end_request(req, BLK_STS_IOERR);
1468 } else if (mrq->data) {
1469 if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered))
1470 blk_mq_requeue_request(req, true);
1472 __blk_mq_end_request(req, BLK_STS_OK);
1474 blk_mq_end_request(req, BLK_STS_OK);
1477 spin_lock_irqsave(q->queue_lock, flags);
1479 mq->in_flight[issue_type] -= 1;
1481 put_card = (mmc_tot_in_flight(mq) == 0);
1483 mmc_cqe_check_busy(mq);
1485 spin_unlock_irqrestore(q->queue_lock, flags);
1488 blk_mq_run_hw_queues(q, true);
1491 mmc_put_card(mq->card, &mq->ctx);
1494 void mmc_blk_cqe_recovery(struct mmc_queue *mq)
1496 struct mmc_card *card = mq->card;
1497 struct mmc_host *host = card->host;
1500 pr_debug("%s: CQE recovery start\n", mmc_hostname(host));
1502 err = mmc_cqe_recovery(host);
1504 mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY);
1505 mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY);
1507 pr_debug("%s: CQE recovery done\n", mmc_hostname(host));
1510 static void mmc_blk_cqe_req_done(struct mmc_request *mrq)
1512 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
1514 struct request *req = mmc_queue_req_to_req(mqrq);
1515 struct request_queue *q = req->q;
1516 struct mmc_queue *mq = q->queuedata;
1519 * Block layer timeouts race with completions which means the normal
1520 * completion path cannot be used during recovery.
1522 if (mq->in_recovery)
1523 mmc_blk_cqe_complete_rq(mq, req);
1525 blk_mq_complete_request(req);
1528 static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
1530 mrq->done = mmc_blk_cqe_req_done;
1531 mrq->recovery_notifier = mmc_cqe_recovery_notifier;
1533 return mmc_cqe_start_req(host, mrq);
1536 static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq,
1537 struct request *req)
1539 struct mmc_blk_request *brq = &mqrq->brq;
1541 memset(brq, 0, sizeof(*brq));
1543 brq->mrq.cmd = &brq->cmd;
1544 brq->mrq.tag = req->tag;
1549 static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
1551 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1552 struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);
1554 mrq->cmd->opcode = MMC_SWITCH;
1555 mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1556 (EXT_CSD_FLUSH_CACHE << 16) |
1558 EXT_CSD_CMD_SET_NORMAL;
1559 mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B;
1561 return mmc_blk_cqe_start_req(mq->card->host, mrq);
1564 static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1566 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1568 mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL);
1570 return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq);
1573 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1574 struct mmc_card *card,
1576 struct mmc_queue *mq)
1578 u32 readcmd, writecmd;
1579 struct mmc_blk_request *brq = &mqrq->brq;
1580 struct request *req = mmc_queue_req_to_req(mqrq);
1581 struct mmc_blk_data *md = mq->blkdata;
1582 bool do_rel_wr, do_data_tag;
1584 mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1586 brq->mrq.cmd = &brq->cmd;
1588 brq->cmd.arg = blk_rq_pos(req);
1589 if (!mmc_card_blockaddr(card))
1591 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1593 if (brq->data.blocks > 1 || do_rel_wr) {
1594 /* SPI multiblock writes terminate using a special
1595 * token, not a STOP_TRANSMISSION request.
1597 if (!mmc_host_is_spi(card->host) ||
1598 rq_data_dir(req) == READ)
1599 brq->mrq.stop = &brq->stop;
1600 readcmd = MMC_READ_MULTIPLE_BLOCK;
1601 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1603 brq->mrq.stop = NULL;
1604 readcmd = MMC_READ_SINGLE_BLOCK;
1605 writecmd = MMC_WRITE_BLOCK;
1607 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
1610 * Pre-defined multi-block transfers are preferable to
1611 * open ended-ones (and necessary for reliable writes).
1612 * However, it is not sufficient to just send CMD23,
1613 * and avoid the final CMD12, as on an error condition
1614 * CMD12 (stop) needs to be sent anyway. This, coupled
1615 * with Auto-CMD23 enhancements provided by some
1616 * hosts, means that the complexity of dealing
1617 * with this is best left to the host. If CMD23 is
1618 * supported by card and host, we'll fill sbc in and let
1619 * the host deal with handling it correctly. This means
1620 * that for hosts that don't expose MMC_CAP_CMD23, no
1621 * change of behavior will be observed.
1623 * N.B: Some MMC cards experience perf degradation.
1624 * We'll avoid using CMD23-bounded multiblock writes for
1625 * these, while retaining features like reliable writes.
1627 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1628 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1630 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1631 brq->sbc.arg = brq->data.blocks |
1632 (do_rel_wr ? (1 << 31) : 0) |
1633 (do_data_tag ? (1 << 29) : 0);
1634 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1635 brq->mrq.sbc = &brq->sbc;
1639 #define MMC_MAX_RETRIES 5
1640 #define MMC_DATA_RETRIES 2
1641 #define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1)
1643 static int mmc_blk_send_stop(struct mmc_card *card, unsigned int timeout)
1645 struct mmc_command cmd = {
1646 .opcode = MMC_STOP_TRANSMISSION,
1647 .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC,
1648 /* Some hosts wait for busy anyway, so provide a busy timeout */
1649 .busy_timeout = timeout,
1652 return mmc_wait_for_cmd(card->host, &cmd, 5);
1655 static int mmc_blk_fix_state(struct mmc_card *card, struct request *req)
1657 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1658 struct mmc_blk_request *brq = &mqrq->brq;
1659 unsigned int timeout = mmc_blk_data_timeout_ms(card->host, &brq->data);
1662 mmc_retune_hold_now(card->host);
1664 mmc_blk_send_stop(card, timeout);
1666 err = card_busy_detect(card, timeout, NULL);
1668 mmc_retune_release(card->host);
1673 #define MMC_READ_SINGLE_RETRIES 2
1675 /* Single sector read during recovery */
1676 static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req)
1678 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1679 struct mmc_request *mrq = &mqrq->brq.mrq;
1680 struct mmc_card *card = mq->card;
1681 struct mmc_host *host = card->host;
1682 blk_status_t error = BLK_STS_OK;
1689 while (retries++ <= MMC_READ_SINGLE_RETRIES) {
1690 mmc_blk_rw_rq_prep(mqrq, card, 1, mq);
1692 mmc_wait_for_req(host, mrq);
1694 err = mmc_send_status(card, &status);
1698 if (!mmc_host_is_spi(host) &&
1699 !mmc_blk_in_tran_state(status)) {
1700 err = mmc_blk_fix_state(card, req);
1705 if (!mrq->cmd->error)
1709 if (mrq->cmd->error ||
1711 (!mmc_host_is_spi(host) &&
1712 (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS)))
1713 error = BLK_STS_IOERR;
1717 } while (blk_update_request(req, error, 512));
1722 mrq->data->bytes_xfered = 0;
1723 blk_update_request(req, BLK_STS_IOERR, 512);
1724 /* Let it try the remaining request again */
1725 if (mqrq->retries > MMC_MAX_RETRIES - 1)
1726 mqrq->retries = MMC_MAX_RETRIES - 1;
1729 static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq)
1731 return !!brq->mrq.sbc;
1734 static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq)
1736 return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR;
1740 * Check for errors the host controller driver might not have seen such as
1741 * response mode errors or invalid card state.
1743 static bool mmc_blk_status_error(struct request *req, u32 status)
1745 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1746 struct mmc_blk_request *brq = &mqrq->brq;
1747 struct mmc_queue *mq = req->q->queuedata;
1750 if (mmc_host_is_spi(mq->card->host))
1753 stop_err_bits = mmc_blk_stop_err_bits(brq);
1755 return brq->cmd.resp[0] & CMD_ERRORS ||
1756 brq->stop.resp[0] & stop_err_bits ||
1757 status & stop_err_bits ||
1758 (rq_data_dir(req) == WRITE && !mmc_blk_in_tran_state(status));
1761 static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq)
1763 return !brq->sbc.error && !brq->cmd.error &&
1764 !(brq->cmd.resp[0] & CMD_ERRORS);
1768 * Requests are completed by mmc_blk_mq_complete_rq() which sets simple
1770 * 1. A request that has transferred at least some data is considered
1771 * successful and will be requeued if there is remaining data to
1773 * 2. Otherwise the number of retries is incremented and the request
1774 * will be requeued if there are remaining retries.
1775 * 3. Otherwise the request will be errored out.
1776 * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and
1777 * mqrq->retries. So there are only 4 possible actions here:
1778 * 1. do not accept the bytes_xfered value i.e. set it to zero
1779 * 2. change mqrq->retries to determine the number of retries
1780 * 3. try to reset the card
1781 * 4. read one sector at a time
1783 static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req)
1785 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1786 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1787 struct mmc_blk_request *brq = &mqrq->brq;
1788 struct mmc_blk_data *md = mq->blkdata;
1789 struct mmc_card *card = mq->card;
1795 * Some errors the host driver might not have seen. Set the number of
1796 * bytes transferred to zero in that case.
1798 err = __mmc_send_status(card, &status, 0);
1799 if (err || mmc_blk_status_error(req, status))
1800 brq->data.bytes_xfered = 0;
1802 mmc_retune_release(card->host);
1805 * Try again to get the status. This also provides an opportunity for
1809 err = __mmc_send_status(card, &status, 0);
1812 * Nothing more to do after the number of bytes transferred has been
1813 * updated and there is no card.
1815 if (err && mmc_detect_card_removed(card->host))
1818 /* Try to get back to "tran" state */
1819 if (!mmc_host_is_spi(mq->card->host) &&
1820 (err || !mmc_blk_in_tran_state(status)))
1821 err = mmc_blk_fix_state(mq->card, req);
1824 * Special case for SD cards where the card might record the number of
1827 if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) &&
1828 rq_data_dir(req) == WRITE) {
1829 if (mmc_sd_num_wr_blocks(card, &blocks))
1830 brq->data.bytes_xfered = 0;
1832 brq->data.bytes_xfered = blocks << 9;
1835 /* Reset if the card is in a bad state */
1836 if (!mmc_host_is_spi(mq->card->host) &&
1837 err && mmc_blk_reset(md, card->host, type)) {
1838 pr_err("%s: recovery failed!\n", req->rq_disk->disk_name);
1839 mqrq->retries = MMC_NO_RETRIES;
1844 * If anything was done, just return and if there is anything remaining
1845 * on the request it will get requeued.
1847 if (brq->data.bytes_xfered)
1850 /* Reset before last retry */
1851 if (mqrq->retries + 1 == MMC_MAX_RETRIES)
1852 mmc_blk_reset(md, card->host, type);
1854 /* Command errors fail fast, so use all MMC_MAX_RETRIES */
1855 if (brq->sbc.error || brq->cmd.error)
1858 /* Reduce the remaining retries for data errors */
1859 if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) {
1860 mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES;
1864 /* FIXME: Missing single sector read for large sector size */
1865 if (!mmc_large_sector(card) && rq_data_dir(req) == READ &&
1866 brq->data.blocks > 1) {
1867 /* Read one sector at a time */
1868 mmc_blk_read_single(mq, req);
1873 static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq)
1875 mmc_blk_eval_resp_error(brq);
1877 return brq->sbc.error || brq->cmd.error || brq->stop.error ||
1878 brq->data.error || brq->cmd.resp[0] & CMD_ERRORS;
1881 static int mmc_blk_card_busy(struct mmc_card *card, struct request *req)
1883 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1887 if (mmc_host_is_spi(card->host) || rq_data_dir(req) == READ)
1890 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, &status);
1893 * Do not assume data transferred correctly if there are any error bits
1896 if (status & mmc_blk_stop_err_bits(&mqrq->brq)) {
1897 mqrq->brq.data.bytes_xfered = 0;
1898 err = err ? err : -EIO;
1901 /* Copy the exception bit so it will be seen later on */
1902 if (mmc_card_mmc(card) && status & R1_EXCEPTION_EVENT)
1903 mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
1908 static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
1909 struct request *req)
1911 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1913 mmc_blk_reset_success(mq->blkdata, type);
1916 static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)
1918 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1919 unsigned int nr_bytes = mqrq->brq.data.bytes_xfered;
1922 if (blk_update_request(req, BLK_STS_OK, nr_bytes))
1923 blk_mq_requeue_request(req, true);
1925 __blk_mq_end_request(req, BLK_STS_OK);
1926 } else if (!blk_rq_bytes(req)) {
1927 __blk_mq_end_request(req, BLK_STS_IOERR);
1928 } else if (mqrq->retries++ < MMC_MAX_RETRIES) {
1929 blk_mq_requeue_request(req, true);
1931 if (mmc_card_removed(mq->card))
1932 req->rq_flags |= RQF_QUIET;
1933 blk_mq_end_request(req, BLK_STS_IOERR);
1937 static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
1938 struct mmc_queue_req *mqrq)
1940 return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) &&
1941 (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
1942 mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
1945 static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
1946 struct mmc_queue_req *mqrq)
1948 if (mmc_blk_urgent_bkops_needed(mq, mqrq))
1949 mmc_run_bkops(mq->card);
1952 void mmc_blk_mq_complete(struct request *req)
1954 struct mmc_queue *mq = req->q->queuedata;
1957 mmc_blk_cqe_complete_rq(mq, req);
1959 mmc_blk_mq_complete_rq(mq, req);
1962 static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,
1963 struct request *req)
1965 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1966 struct mmc_host *host = mq->card->host;
1968 if (mmc_blk_rq_error(&mqrq->brq) ||
1969 mmc_blk_card_busy(mq->card, req)) {
1970 mmc_blk_mq_rw_recovery(mq, req);
1972 mmc_blk_rw_reset_success(mq, req);
1973 mmc_retune_release(host);
1976 mmc_blk_urgent_bkops(mq, mqrq);
1979 static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq,
1980 struct request_queue *q,
1981 enum mmc_issue_type issue_type)
1983 unsigned long flags;
1986 spin_lock_irqsave(q->queue_lock, flags);
1988 mq->in_flight[issue_type] -= 1;
1990 put_card = (mmc_tot_in_flight(mq) == 0);
1992 spin_unlock_irqrestore(q->queue_lock, flags);
1995 mmc_put_card(mq->card, &mq->ctx);
1998 static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req)
2000 enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
2001 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2002 struct mmc_request *mrq = &mqrq->brq.mrq;
2003 struct mmc_host *host = mq->card->host;
2004 struct request_queue *q = req->q;
2006 mmc_post_req(host, mrq, 0);
2009 * Block layer timeouts race with completions which means the normal
2010 * completion path cannot be used during recovery.
2012 if (mq->in_recovery)
2013 mmc_blk_mq_complete_rq(mq, req);
2015 blk_mq_complete_request(req);
2017 mmc_blk_mq_dec_in_flight(mq, q, issue_type);
2020 void mmc_blk_mq_recovery(struct mmc_queue *mq)
2022 struct request *req = mq->recovery_req;
2023 struct mmc_host *host = mq->card->host;
2024 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2026 mq->recovery_req = NULL;
2027 mq->rw_wait = false;
2029 if (mmc_blk_rq_error(&mqrq->brq)) {
2030 mmc_retune_hold_now(host);
2031 mmc_blk_mq_rw_recovery(mq, req);
2034 mmc_blk_urgent_bkops(mq, mqrq);
2036 mmc_blk_mq_post_req(mq, req);
2039 static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq,
2040 struct request **prev_req)
2042 if (mmc_host_done_complete(mq->card->host))
2045 mutex_lock(&mq->complete_lock);
2047 if (!mq->complete_req)
2050 mmc_blk_mq_poll_completion(mq, mq->complete_req);
2053 *prev_req = mq->complete_req;
2055 mmc_blk_mq_post_req(mq, mq->complete_req);
2057 mq->complete_req = NULL;
2060 mutex_unlock(&mq->complete_lock);
2063 void mmc_blk_mq_complete_work(struct work_struct *work)
2065 struct mmc_queue *mq = container_of(work, struct mmc_queue,
2068 mmc_blk_mq_complete_prev_req(mq, NULL);
2071 static void mmc_blk_mq_req_done(struct mmc_request *mrq)
2073 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
2075 struct request *req = mmc_queue_req_to_req(mqrq);
2076 struct request_queue *q = req->q;
2077 struct mmc_queue *mq = q->queuedata;
2078 struct mmc_host *host = mq->card->host;
2079 unsigned long flags;
2081 if (!mmc_host_done_complete(host)) {
2085 * We cannot complete the request in this context, so record
2086 * that there is a request to complete, and that a following
2087 * request does not need to wait (although it does need to
2088 * complete complete_req first).
2090 spin_lock_irqsave(q->queue_lock, flags);
2091 mq->complete_req = req;
2092 mq->rw_wait = false;
2093 waiting = mq->waiting;
2094 spin_unlock_irqrestore(q->queue_lock, flags);
2097 * If 'waiting' then the waiting task will complete this
2098 * request, otherwise queue a work to do it. Note that
2099 * complete_work may still race with the dispatch of a following
2105 queue_work(mq->card->complete_wq, &mq->complete_work);
2110 /* Take the recovery path for errors or urgent background operations */
2111 if (mmc_blk_rq_error(&mqrq->brq) ||
2112 mmc_blk_urgent_bkops_needed(mq, mqrq)) {
2113 spin_lock_irqsave(q->queue_lock, flags);
2114 mq->recovery_needed = true;
2115 mq->recovery_req = req;
2116 spin_unlock_irqrestore(q->queue_lock, flags);
2118 schedule_work(&mq->recovery_work);
2122 mmc_blk_rw_reset_success(mq, req);
2124 mq->rw_wait = false;
2127 mmc_blk_mq_post_req(mq, req);
2130 static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err)
2132 struct request_queue *q = mq->queue;
2133 unsigned long flags;
2137 * Wait while there is another request in progress, but not if recovery
2138 * is needed. Also indicate whether there is a request waiting to start.
2140 spin_lock_irqsave(q->queue_lock, flags);
2141 if (mq->recovery_needed) {
2145 done = !mq->rw_wait;
2147 mq->waiting = !done;
2148 spin_unlock_irqrestore(q->queue_lock, flags);
2153 static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req)
2157 wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err));
2159 /* Always complete the previous request if there is one */
2160 mmc_blk_mq_complete_prev_req(mq, prev_req);
2165 static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq,
2166 struct request *req)
2168 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2169 struct mmc_host *host = mq->card->host;
2170 struct request *prev_req = NULL;
2173 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
2175 mqrq->brq.mrq.done = mmc_blk_mq_req_done;
2177 mmc_pre_req(host, &mqrq->brq.mrq);
2179 err = mmc_blk_rw_wait(mq, &prev_req);
2185 err = mmc_start_request(host, &mqrq->brq.mrq);
2188 mmc_blk_mq_post_req(mq, prev_req);
2191 mq->rw_wait = false;
2193 /* Release re-tuning here where there is no synchronization required */
2194 if (err || mmc_host_done_complete(host))
2195 mmc_retune_release(host);
2199 mmc_post_req(host, &mqrq->brq.mrq, err);
2204 static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host)
2207 return host->cqe_ops->cqe_wait_for_idle(host);
2209 return mmc_blk_rw_wait(mq, NULL);
2212 enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
2214 struct mmc_blk_data *md = mq->blkdata;
2215 struct mmc_card *card = md->queue.card;
2216 struct mmc_host *host = card->host;
2219 ret = mmc_blk_part_switch(card, md->part_type);
2221 return MMC_REQ_FAILED_TO_START;
2223 switch (mmc_issue_type(mq, req)) {
2224 case MMC_ISSUE_SYNC:
2225 ret = mmc_blk_wait_for_idle(mq, host);
2227 return MMC_REQ_BUSY;
2228 switch (req_op(req)) {
2230 case REQ_OP_DRV_OUT:
2231 mmc_blk_issue_drv_op(mq, req);
2233 case REQ_OP_DISCARD:
2234 mmc_blk_issue_discard_rq(mq, req);
2236 case REQ_OP_SECURE_ERASE:
2237 mmc_blk_issue_secdiscard_rq(mq, req);
2240 mmc_blk_issue_flush(mq, req);
2244 return MMC_REQ_FAILED_TO_START;
2246 return MMC_REQ_FINISHED;
2247 case MMC_ISSUE_DCMD:
2248 case MMC_ISSUE_ASYNC:
2249 switch (req_op(req)) {
2251 if (!mmc_cache_enabled(host)) {
2252 blk_mq_end_request(req, BLK_STS_OK);
2253 return MMC_REQ_FINISHED;
2255 ret = mmc_blk_cqe_issue_flush(mq, req);
2260 ret = mmc_blk_cqe_issue_rw_rq(mq, req);
2262 ret = mmc_blk_mq_issue_rw_rq(mq, req);
2269 return MMC_REQ_STARTED;
2270 return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START;
2273 return MMC_REQ_FAILED_TO_START;
2277 static inline int mmc_blk_readonly(struct mmc_card *card)
2279 return mmc_card_readonly(card) ||
2280 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2283 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2284 struct device *parent,
2287 const char *subname,
2290 struct mmc_blk_data *md;
2293 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
2296 * We get -ENOSPC because there are no more any available
2297 * devidx. The reason may be that, either userspace haven't yet
2298 * unmounted the partitions, which postpones mmc_blk_release()
2299 * from being called, or the device has more partitions than
2302 if (devidx == -ENOSPC)
2303 dev_err(mmc_dev(card->host),
2304 "no more device IDs available\n");
2306 return ERR_PTR(devidx);
2309 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2315 md->area_type = area_type;
2318 * Set the read-only status based on the supported commands
2319 * and the write protect switch.
2321 md->read_only = mmc_blk_readonly(card);
2323 md->disk = alloc_disk(perdev_minors);
2324 if (md->disk == NULL) {
2329 spin_lock_init(&md->lock);
2330 INIT_LIST_HEAD(&md->part);
2331 INIT_LIST_HEAD(&md->rpmbs);
2334 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2338 md->queue.blkdata = md;
2341 * Keep an extra reference to the queue so that we can shutdown the
2342 * queue (i.e. call blk_cleanup_queue()) while there are still
2343 * references to the 'md'. The corresponding blk_put_queue() is in
2346 if (!blk_get_queue(md->queue.queue)) {
2347 mmc_cleanup_queue(&md->queue);
2352 md->disk->major = MMC_BLOCK_MAJOR;
2353 md->disk->first_minor = devidx * perdev_minors;
2354 md->disk->fops = &mmc_bdops;
2355 md->disk->private_data = md;
2356 md->disk->queue = md->queue.queue;
2357 md->parent = parent;
2358 set_disk_ro(md->disk, md->read_only || default_ro);
2359 md->disk->flags = GENHD_FL_EXT_DEVT;
2360 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2361 md->disk->flags |= GENHD_FL_NO_PART_SCAN
2362 | GENHD_FL_SUPPRESS_PARTITION_INFO;
2365 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2367 * - be set for removable media with permanent block devices
2368 * - be unset for removable block devices with permanent media
2370 * Since MMC block devices clearly fall under the second
2371 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2372 * should use the block device creation/destruction hotplug
2373 * messages to tell when the card is present.
2376 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2377 "mmcblk%u%s", card->host->index, subname ? subname : "");
2379 set_capacity(md->disk, size);
2381 if (mmc_host_cmd23(card->host)) {
2382 if ((mmc_card_mmc(card) &&
2383 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
2384 (mmc_card_sd(card) &&
2385 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2386 md->flags |= MMC_BLK_CMD23;
2389 if (mmc_card_mmc(card) &&
2390 md->flags & MMC_BLK_CMD23 &&
2391 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2392 card->ext_csd.rel_sectors)) {
2393 md->flags |= MMC_BLK_REL_WR;
2394 blk_queue_write_cache(md->queue.queue, true, true);
2404 ida_simple_remove(&mmc_blk_ida, devidx);
2405 return ERR_PTR(ret);
2408 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2412 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2414 * The EXT_CSD sector count is in number or 512 byte
2417 size = card->ext_csd.sectors;
2420 * The CSD capacity field is in units of read_blkbits.
2421 * set_capacity takes units of 512 bytes.
2423 size = (typeof(sector_t))card->csd.capacity
2424 << (card->csd.read_blkbits - 9);
2427 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2428 MMC_BLK_DATA_AREA_MAIN);
2431 static int mmc_blk_alloc_part(struct mmc_card *card,
2432 struct mmc_blk_data *md,
2433 unsigned int part_type,
2436 const char *subname,
2440 struct mmc_blk_data *part_md;
2442 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2443 subname, area_type);
2444 if (IS_ERR(part_md))
2445 return PTR_ERR(part_md);
2446 part_md->part_type = part_type;
2447 list_add(&part_md->part, &md->part);
2449 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
2450 cap_str, sizeof(cap_str));
2451 pr_info("%s: %s %s partition %u %s\n",
2452 part_md->disk->disk_name, mmc_card_id(card),
2453 mmc_card_name(card), part_md->part_type, cap_str);
2458 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2459 * @filp: the character device file
2460 * @cmd: the ioctl() command
2461 * @arg: the argument from userspace
2463 * This will essentially just redirect the ioctl()s coming in over to
2464 * the main block device spawning the RPMB character device.
2466 static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
2469 struct mmc_rpmb_data *rpmb = filp->private_data;
2474 ret = mmc_blk_ioctl_cmd(rpmb->md,
2475 (struct mmc_ioc_cmd __user *)arg,
2478 case MMC_IOC_MULTI_CMD:
2479 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
2480 (struct mmc_ioc_multi_cmd __user *)arg,
2491 #ifdef CONFIG_COMPAT
2492 static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
2495 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
2499 static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
2501 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2502 struct mmc_rpmb_data, chrdev);
2504 get_device(&rpmb->dev);
2505 filp->private_data = rpmb;
2506 mmc_blk_get(rpmb->md->disk);
2508 return nonseekable_open(inode, filp);
2511 static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
2513 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2514 struct mmc_rpmb_data, chrdev);
2516 mmc_blk_put(rpmb->md);
2517 put_device(&rpmb->dev);
2522 static const struct file_operations mmc_rpmb_fileops = {
2523 .release = mmc_rpmb_chrdev_release,
2524 .open = mmc_rpmb_chrdev_open,
2525 .owner = THIS_MODULE,
2526 .llseek = no_llseek,
2527 .unlocked_ioctl = mmc_rpmb_ioctl,
2528 #ifdef CONFIG_COMPAT
2529 .compat_ioctl = mmc_rpmb_ioctl_compat,
2533 static void mmc_blk_rpmb_device_release(struct device *dev)
2535 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2537 ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2541 static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
2542 struct mmc_blk_data *md,
2543 unsigned int part_index,
2545 const char *subname)
2548 char rpmb_name[DISK_NAME_LEN];
2550 struct mmc_rpmb_data *rpmb;
2552 /* This creates the minor number for the RPMB char device */
2553 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
2557 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
2559 ida_simple_remove(&mmc_rpmb_ida, devidx);
2563 snprintf(rpmb_name, sizeof(rpmb_name),
2564 "mmcblk%u%s", card->host->index, subname ? subname : "");
2567 rpmb->part_index = part_index;
2568 rpmb->dev.init_name = rpmb_name;
2569 rpmb->dev.bus = &mmc_rpmb_bus_type;
2570 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
2571 rpmb->dev.parent = &card->dev;
2572 rpmb->dev.release = mmc_blk_rpmb_device_release;
2573 device_initialize(&rpmb->dev);
2574 dev_set_drvdata(&rpmb->dev, rpmb);
2577 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
2578 rpmb->chrdev.owner = THIS_MODULE;
2579 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
2581 pr_err("%s: could not add character device\n", rpmb_name);
2582 goto out_put_device;
2585 list_add(&rpmb->node, &md->rpmbs);
2587 string_get_size((u64)size, 512, STRING_UNITS_2,
2588 cap_str, sizeof(cap_str));
2590 pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n",
2591 rpmb_name, mmc_card_id(card),
2592 mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str,
2593 MAJOR(mmc_rpmb_devt), rpmb->id);
2598 put_device(&rpmb->dev);
2602 static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
2605 cdev_device_del(&rpmb->chrdev, &rpmb->dev);
2606 put_device(&rpmb->dev);
2609 /* MMC Physical partitions consist of two boot partitions and
2610 * up to four general purpose partitions.
2611 * For each partition enabled in EXT_CSD a block device will be allocatedi
2612 * to provide access to the partition.
2615 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2619 if (!mmc_card_mmc(card))
2622 for (idx = 0; idx < card->nr_parts; idx++) {
2623 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
2625 * RPMB partitions does not provide block access, they
2626 * are only accessed using ioctl():s. Thus create
2627 * special RPMB block devices that do not have a
2628 * backing block queue for these.
2630 ret = mmc_blk_alloc_rpmb_part(card, md,
2631 card->part[idx].part_cfg,
2632 card->part[idx].size >> 9,
2633 card->part[idx].name);
2636 } else if (card->part[idx].size) {
2637 ret = mmc_blk_alloc_part(card, md,
2638 card->part[idx].part_cfg,
2639 card->part[idx].size >> 9,
2640 card->part[idx].force_ro,
2641 card->part[idx].name,
2642 card->part[idx].area_type);
2651 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2653 struct mmc_card *card;
2657 * Flush remaining requests and free queues. It
2658 * is freeing the queue that stops new requests
2659 * from being accepted.
2661 card = md->queue.card;
2662 if (md->disk->flags & GENHD_FL_UP) {
2663 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2664 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2665 card->ext_csd.boot_ro_lockable)
2666 device_remove_file(disk_to_dev(md->disk),
2667 &md->power_ro_lock);
2669 del_gendisk(md->disk);
2671 mmc_cleanup_queue(&md->queue);
2676 static void mmc_blk_remove_parts(struct mmc_card *card,
2677 struct mmc_blk_data *md)
2679 struct list_head *pos, *q;
2680 struct mmc_blk_data *part_md;
2681 struct mmc_rpmb_data *rpmb;
2683 /* Remove RPMB partitions */
2684 list_for_each_safe(pos, q, &md->rpmbs) {
2685 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
2687 mmc_blk_remove_rpmb_part(rpmb);
2689 /* Remove block partitions */
2690 list_for_each_safe(pos, q, &md->part) {
2691 part_md = list_entry(pos, struct mmc_blk_data, part);
2693 mmc_blk_remove_req(part_md);
2697 static int mmc_add_disk(struct mmc_blk_data *md)
2700 struct mmc_card *card = md->queue.card;
2702 device_add_disk(md->parent, md->disk, NULL);
2703 md->force_ro.show = force_ro_show;
2704 md->force_ro.store = force_ro_store;
2705 sysfs_attr_init(&md->force_ro.attr);
2706 md->force_ro.attr.name = "force_ro";
2707 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2708 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2712 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2713 card->ext_csd.boot_ro_lockable) {
2716 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2719 mode = S_IRUGO | S_IWUSR;
2721 md->power_ro_lock.show = power_ro_lock_show;
2722 md->power_ro_lock.store = power_ro_lock_store;
2723 sysfs_attr_init(&md->power_ro_lock.attr);
2724 md->power_ro_lock.attr.mode = mode;
2725 md->power_ro_lock.attr.name =
2726 "ro_lock_until_next_power_on";
2727 ret = device_create_file(disk_to_dev(md->disk),
2728 &md->power_ro_lock);
2730 goto power_ro_lock_fail;
2735 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2737 del_gendisk(md->disk);
2742 #ifdef CONFIG_DEBUG_FS
2744 static int mmc_dbg_card_status_get(void *data, u64 *val)
2746 struct mmc_card *card = data;
2747 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2748 struct mmc_queue *mq = &md->queue;
2749 struct request *req;
2752 /* Ask the block layer about the card status */
2753 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0);
2755 return PTR_ERR(req);
2756 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2757 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
2758 blk_execute_rq(mq->queue, NULL, req, 0);
2759 ret = req_to_mmc_queue_req(req)->drv_op_result;
2764 blk_put_request(req);
2768 DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2771 /* That is two digits * 512 + 1 for newline */
2772 #define EXT_CSD_STR_LEN 1025
2774 static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2776 struct mmc_card *card = inode->i_private;
2777 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2778 struct mmc_queue *mq = &md->queue;
2779 struct request *req;
2785 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2789 /* Ask the block layer for the EXT CSD */
2790 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0);
2795 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2796 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
2797 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2798 blk_execute_rq(mq->queue, NULL, req, 0);
2799 err = req_to_mmc_queue_req(req)->drv_op_result;
2800 blk_put_request(req);
2802 pr_err("FAILED %d\n", err);
2806 for (i = 0; i < 512; i++)
2807 n += sprintf(buf + n, "%02x", ext_csd[i]);
2808 n += sprintf(buf + n, "\n");
2810 if (n != EXT_CSD_STR_LEN) {
2816 filp->private_data = buf;
2825 static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2826 size_t cnt, loff_t *ppos)
2828 char *buf = filp->private_data;
2830 return simple_read_from_buffer(ubuf, cnt, ppos,
2831 buf, EXT_CSD_STR_LEN);
2834 static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2836 kfree(file->private_data);
2840 static const struct file_operations mmc_dbg_ext_csd_fops = {
2841 .open = mmc_ext_csd_open,
2842 .read = mmc_ext_csd_read,
2843 .release = mmc_ext_csd_release,
2844 .llseek = default_llseek,
2847 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2849 struct dentry *root;
2851 if (!card->debugfs_root)
2854 root = card->debugfs_root;
2856 if (mmc_card_mmc(card) || mmc_card_sd(card)) {
2858 debugfs_create_file("status", S_IRUSR, root, card,
2859 &mmc_dbg_card_status_fops);
2860 if (!md->status_dentry)
2864 if (mmc_card_mmc(card)) {
2865 md->ext_csd_dentry =
2866 debugfs_create_file("ext_csd", S_IRUSR, root, card,
2867 &mmc_dbg_ext_csd_fops);
2868 if (!md->ext_csd_dentry)
2875 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2876 struct mmc_blk_data *md)
2878 if (!card->debugfs_root)
2881 if (!IS_ERR_OR_NULL(md->status_dentry)) {
2882 debugfs_remove(md->status_dentry);
2883 md->status_dentry = NULL;
2886 if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) {
2887 debugfs_remove(md->ext_csd_dentry);
2888 md->ext_csd_dentry = NULL;
2894 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2899 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2900 struct mmc_blk_data *md)
2904 #endif /* CONFIG_DEBUG_FS */
2906 static int mmc_blk_probe(struct mmc_card *card)
2908 struct mmc_blk_data *md, *part_md;
2912 * Check that the card supports the command class(es) we need.
2914 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2917 mmc_fixup_device(card, mmc_blk_fixups);
2919 card->complete_wq = alloc_workqueue("mmc_complete",
2920 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2921 if (unlikely(!card->complete_wq)) {
2922 pr_err("Failed to create mmc completion workqueue");
2926 md = mmc_blk_alloc(card);
2930 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
2931 cap_str, sizeof(cap_str));
2932 pr_info("%s: %s %s %s %s\n",
2933 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2934 cap_str, md->read_only ? "(ro)" : "");
2936 if (mmc_blk_alloc_parts(card, md))
2939 dev_set_drvdata(&card->dev, md);
2941 if (mmc_add_disk(md))
2944 list_for_each_entry(part_md, &md->part, part) {
2945 if (mmc_add_disk(part_md))
2949 /* Add two debugfs entries */
2950 mmc_blk_add_debugfs(card, md);
2952 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2953 pm_runtime_use_autosuspend(&card->dev);
2956 * Don't enable runtime PM for SD-combo cards here. Leave that
2957 * decision to be taken during the SDIO init sequence instead.
2959 if (card->type != MMC_TYPE_SD_COMBO) {
2960 pm_runtime_set_active(&card->dev);
2961 pm_runtime_enable(&card->dev);
2967 mmc_blk_remove_parts(card, md);
2968 mmc_blk_remove_req(md);
2972 static void mmc_blk_remove(struct mmc_card *card)
2974 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2976 mmc_blk_remove_debugfs(card, md);
2977 mmc_blk_remove_parts(card, md);
2978 pm_runtime_get_sync(&card->dev);
2979 if (md->part_curr != md->part_type) {
2980 mmc_claim_host(card->host);
2981 mmc_blk_part_switch(card, md->part_type);
2982 mmc_release_host(card->host);
2984 if (card->type != MMC_TYPE_SD_COMBO)
2985 pm_runtime_disable(&card->dev);
2986 pm_runtime_put_noidle(&card->dev);
2987 mmc_blk_remove_req(md);
2988 dev_set_drvdata(&card->dev, NULL);
2989 destroy_workqueue(card->complete_wq);
2992 static int _mmc_blk_suspend(struct mmc_card *card)
2994 struct mmc_blk_data *part_md;
2995 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2998 mmc_queue_suspend(&md->queue);
2999 list_for_each_entry(part_md, &md->part, part) {
3000 mmc_queue_suspend(&part_md->queue);
3006 static void mmc_blk_shutdown(struct mmc_card *card)
3008 _mmc_blk_suspend(card);
3011 #ifdef CONFIG_PM_SLEEP
3012 static int mmc_blk_suspend(struct device *dev)
3014 struct mmc_card *card = mmc_dev_to_card(dev);
3016 return _mmc_blk_suspend(card);
3019 static int mmc_blk_resume(struct device *dev)
3021 struct mmc_blk_data *part_md;
3022 struct mmc_blk_data *md = dev_get_drvdata(dev);
3026 * Resume involves the card going into idle state,
3027 * so current partition is always the main one.
3029 md->part_curr = md->part_type;
3030 mmc_queue_resume(&md->queue);
3031 list_for_each_entry(part_md, &md->part, part) {
3032 mmc_queue_resume(&part_md->queue);
3039 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3041 static struct mmc_driver mmc_driver = {
3044 .pm = &mmc_blk_pm_ops,
3046 .probe = mmc_blk_probe,
3047 .remove = mmc_blk_remove,
3048 .shutdown = mmc_blk_shutdown,
3051 static int __init mmc_blk_init(void)
3055 res = bus_register(&mmc_rpmb_bus_type);
3057 pr_err("mmcblk: could not register RPMB bus type\n");
3060 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
3062 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
3066 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3067 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3069 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
3071 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3073 goto out_chrdev_unreg;
3075 res = mmc_register_driver(&mmc_driver);
3077 goto out_blkdev_unreg;
3082 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3084 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3086 bus_unregister(&mmc_rpmb_bus_type);
3090 static void __exit mmc_blk_exit(void)
3092 mmc_unregister_driver(&mmc_driver);
3093 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3094 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3095 bus_unregister(&mmc_rpmb_bus_type);
3098 module_init(mmc_blk_init);
3099 module_exit(mmc_blk_exit);
3101 MODULE_LICENSE("GPL");
3102 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");